emit2.c revision 1.6 1 /* $NetBSD: emit2.c,v 1.6 2001/05/28 12:40:38 lukem Exp $ */
2
3 /*
4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
5 * Copyright (c) 1994, 1995 Jochen Pohl
6 * All Rights Reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Jochen Pohl for
19 * The NetBSD Project.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __RCSID("$NetBSD: emit2.c,v 1.6 2001/05/28 12:40:38 lukem Exp $");
38 #endif
39
40 #include <err.h>
41
42 #include "lint2.h"
43
44 static void outtype(type_t *);
45 static void outdef(hte_t *, sym_t *);
46 static void dumpname(hte_t *);
47 static void outfiles(void);
48
49 /*
50 * Write type into the output buffer.
51 */
52 static void
53 outtype(type_t *tp)
54 {
55 int t, s, na;
56 tspec_t ts;
57 type_t **ap;
58
59 while (tp != NULL) {
60 if ((ts = tp->t_tspec) == INT && tp->t_isenum)
61 ts = ENUM;
62 switch (ts) {
63 case CHAR: t = 'C'; s = '\0'; break;
64 case SCHAR: t = 'C'; s = 's'; break;
65 case UCHAR: t = 'C'; s = 'u'; break;
66 case SHORT: t = 'S'; s = '\0'; break;
67 case USHORT: t = 'S'; s = 'u'; break;
68 case INT: t = 'I'; s = '\0'; break;
69 case UINT: t = 'I'; s = 'u'; break;
70 case LONG: t = 'L'; s = '\0'; break;
71 case ULONG: t = 'L'; s = 'u'; break;
72 case QUAD: t = 'Q'; s = '\0'; break;
73 case UQUAD: t = 'Q'; s = 'u'; break;
74 case FLOAT: t = 'D'; s = 's'; break;
75 case DOUBLE: t = 'D'; s = '\0'; break;
76 case LDOUBLE: t = 'D'; s = 'l'; break;
77 case VOID: t = 'V'; s = '\0'; break;
78 case PTR: t = 'P'; s = '\0'; break;
79 case ARRAY: t = 'A'; s = '\0'; break;
80 case ENUM: t = 'T'; s = 'e'; break;
81 case STRUCT: t = 'T'; s = 's'; break;
82 case UNION: t = 'T'; s = 'u'; break;
83 case FUNC:
84 if (tp->t_args != NULL && !tp->t_proto) {
85 t = 'f';
86 } else {
87 t = 'F';
88 }
89 s = '\0';
90 break;
91 default:
92 errx(1, "internal error: outtype() 1");
93 }
94 if (tp->t_const)
95 outchar('c');
96 if (tp->t_volatile)
97 outchar('v');
98 if (s != '\0')
99 outchar(s);
100 outchar(t);
101 if (ts == ARRAY) {
102 outint(tp->t_dim);
103 } else if (ts == ENUM || ts == STRUCT || ts == UNION) {
104 if (tp->t_istag) {
105 outint(1);
106 outname(tp->t_tag->h_name);
107 } else if (tp->t_istynam) {
108 outint(2);
109 outname(tp->t_tynam->h_name);
110 } else if (tp->t_isuniqpos) {
111 outint(3);
112 outint(tp->t_uniqpos.p_line);
113 outchar('.');
114 outint(tp->t_uniqpos.p_file);
115 outchar('.');
116 outint(tp->t_uniqpos.p_uniq);
117 } else
118 errx(1, "internal error: outtype() 2");
119 } else if (ts == FUNC && tp->t_args != NULL) {
120 na = 0;
121 for (ap = tp->t_args; *ap != NULL; ap++)
122 na++;
123 if (tp->t_vararg)
124 na++;
125 outint(na);
126 for (ap = tp->t_args; *ap != NULL; ap++)
127 outtype(*ap);
128 if (tp->t_vararg)
129 outchar('E');
130 }
131 tp = tp->t_subt;
132 }
133 }
134
135 /*
136 * Write a definition.
137 */
138 static void
139 outdef(hte_t *hte, sym_t *sym)
140 {
141
142 /* reset output buffer */
143 outclr();
144
145 /* line number in C source file */
146 outint(0);
147
148 /* this is a definition */
149 outchar('d');
150
151 /* index of file where symbol was defined and line number of def. */
152 outint(0);
153 outchar('.');
154 outint(0);
155
156 /* flags */
157 if (sym->s_va) {
158 outchar('v'); /* varargs */
159 outint(sym->s_nva);
160 }
161 if (sym->s_scfl) {
162 outchar('S'); /* scanflike */
163 outint(sym->s_nscfl);
164 }
165 if (sym->s_prfl) {
166 outchar('P'); /* printflike */
167 outint(sym->s_nprfl);
168 }
169 /* definition or tentative definition */
170 outchar(sym->s_def == DEF ? 'd' : 't');
171 if (TP(sym->s_type)->t_tspec == FUNC) {
172 if (sym->s_rval)
173 outchar('r'); /* fkt. has return value */
174 if (sym->s_osdef)
175 outchar('o'); /* old style definition */
176 }
177 outchar('u'); /* used (no warning if not used) */
178
179 /* name */
180 outname(hte->h_name);
181
182 /* type */
183 outtype(TP(sym->s_type));
184 }
185
186 /*
187 * Write the first definition of a name into the lint library.
188 */
189 static void
190 dumpname(hte_t *hte)
191 {
192 sym_t *sym, *def;
193
194 /* static and undefined symbols are not written */
195 if (hte->h_static || !hte->h_def)
196 return;
197
198 /*
199 * If there is a definition, write it. Otherwise write a tentative
200 * definition. This is neccessary because more than one tentative
201 * definition is allowed (except with sflag).
202 */
203 def = NULL;
204 for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
205 if (sym->s_def == DEF) {
206 def = sym;
207 break;
208 }
209 if (sym->s_def == TDEF && def == NULL)
210 def = sym;
211 }
212 if (def == NULL)
213 errx(1, "internal error: dumpname() %s", hte->h_name);
214
215 outdef(hte, def);
216 }
217
218 /*
219 * Write a new lint library.
220 */
221 void
222 outlib(const char *name)
223 {
224 /* Open of output file and initialisation of the output buffer */
225 outopen(name);
226
227 /* write name of lint library */
228 outsrc(name);
229
230 /* name of lint lib has index 0 */
231 outclr();
232 outint(0);
233 outchar('s');
234 outstrg(name);
235
236 /*
237 * print the names of all files references by unnamed
238 * struct/union/enum declarations.
239 */
240 outfiles();
241
242 /* write all definitions with external linkage */
243 forall(dumpname);
244
245 /* close the output */
246 outclose();
247 }
248
249 /*
250 * Write out the name of a file referenced by a type.
251 */
252 struct outflist {
253 short ofl_num;
254 struct outflist *ofl_next;
255 };
256 static struct outflist *outflist;
257
258 int
259 addoutfile(short num)
260 {
261 struct outflist *ofl, **pofl;
262 int i;
263
264 ofl = outflist;
265 pofl = &outflist;
266 i = 1; /* library is 0 */
267
268 while (ofl != NULL) {
269 if (ofl->ofl_num == num)
270 break;
271
272 pofl = &ofl->ofl_next;
273 ofl = ofl->ofl_next;
274 i++;
275 }
276
277 if (ofl == NULL) {
278 ofl = *pofl = xmalloc(sizeof (struct outflist));
279 ofl->ofl_num = num;
280 ofl->ofl_next = NULL;
281 }
282 return (i);
283 }
284
285 static void
286 outfiles(void)
287 {
288 struct outflist *ofl;
289 int i;
290
291 for (ofl = outflist, i = 1; ofl != NULL; ofl = ofl->ofl_next, i++) {
292 /* reset output buffer */
293 outclr();
294
295 outint(i);
296 outchar('s');
297 outstrg(fnames[ofl->ofl_num]);
298 }
299 }
300