emit2.c revision 1.10 1 /* $NetBSD: emit2.c,v 1.10 2004/09/12 08:58:52 yamt 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 #if defined(__RCSID) && !defined(lint)
37 __RCSID("$NetBSD: emit2.c,v 1.10 2004/09/12 08:58:52 yamt Exp $");
38 #endif
39
40 #include "lint2.h"
41
42 static void outtype(type_t *);
43 static void outdef(hte_t *, sym_t *);
44 static void dumpname(hte_t *);
45 static void outfiles(void);
46
47 /*
48 * Write type into the output buffer.
49 */
50 static void
51 outtype(type_t *tp)
52 {
53 int t, s, na;
54 tspec_t ts;
55 type_t **ap;
56
57 while (tp != NULL) {
58 if ((ts = tp->t_tspec) == INT && tp->t_isenum)
59 ts = ENUM;
60 switch (ts) {
61 case BOOL: t = 'B'; s = '\0'; break;
62 case CHAR: t = 'C'; s = '\0'; break;
63 case SCHAR: t = 'C'; s = 's'; break;
64 case UCHAR: t = 'C'; s = 'u'; break;
65 case SHORT: t = 'S'; s = '\0'; break;
66 case USHORT: t = 'S'; s = 'u'; break;
67 case INT: t = 'I'; s = '\0'; break;
68 case UINT: t = 'I'; s = 'u'; break;
69 case LONG: t = 'L'; s = '\0'; break;
70 case ULONG: t = 'L'; s = 'u'; break;
71 case QUAD: t = 'Q'; s = '\0'; break;
72 case UQUAD: t = 'Q'; s = 'u'; break;
73 case FLOAT: t = 'D'; s = 's'; break;
74 case DOUBLE: t = 'D'; s = '\0'; break;
75 case LDOUBLE: t = 'D'; s = 'l'; break;
76 case VOID: t = 'V'; s = '\0'; break;
77 case PTR: t = 'P'; s = '\0'; break;
78 case ARRAY: t = 'A'; s = '\0'; break;
79 case ENUM: t = 'T'; s = 'e'; break;
80 case STRUCT: t = 'T'; s = 's'; break;
81 case UNION: t = 'T'; s = 'u'; break;
82 case FUNC:
83 if (tp->t_args != NULL && !tp->t_proto) {
84 t = 'f';
85 } else {
86 t = 'F';
87 }
88 s = '\0';
89 break;
90 default:
91 errx(1, "internal error: outtype() 1");
92 }
93 if (tp->t_const)
94 outchar('c');
95 if (tp->t_volatile)
96 outchar('v');
97 if (s != '\0')
98 outchar(s);
99 outchar(t);
100 if (ts == ARRAY) {
101 outint(tp->t_dim);
102 } else if (ts == ENUM || ts == STRUCT || ts == UNION) {
103 if (tp->t_istag) {
104 outint(1);
105 outname(tp->t_tag->h_name);
106 } else if (tp->t_istynam) {
107 outint(2);
108 outname(tp->t_tynam->h_name);
109 } else if (tp->t_isuniqpos) {
110 outint(3);
111 outint(tp->t_uniqpos.p_line);
112 outchar('.');
113 outint(tp->t_uniqpos.p_file);
114 outchar('.');
115 outint(tp->t_uniqpos.p_uniq);
116 } else
117 errx(1, "internal error: outtype() 2");
118 } else if (ts == FUNC && tp->t_args != NULL) {
119 na = 0;
120 for (ap = tp->t_args; *ap != NULL; ap++)
121 na++;
122 if (tp->t_vararg)
123 na++;
124 outint(na);
125 for (ap = tp->t_args; *ap != NULL; ap++)
126 outtype(*ap);
127 if (tp->t_vararg)
128 outchar('E');
129 }
130 tp = tp->t_subt;
131 }
132 }
133
134 /*
135 * Write a definition.
136 */
137 static void
138 outdef(hte_t *hte, sym_t *sym)
139 {
140
141 /* reset output buffer */
142 outclr();
143
144 /* line number in C source file */
145 outint(0);
146
147 /* this is a definition */
148 outchar('d');
149
150 /* index of file where symbol was defined and line number of def. */
151 outint(0);
152 outchar('.');
153 outint(0);
154
155 /* flags */
156 if (sym->s_va) {
157 outchar('v'); /* varargs */
158 outint(sym->s_nva);
159 }
160 if (sym->s_scfl) {
161 outchar('S'); /* scanflike */
162 outint(sym->s_nscfl);
163 }
164 if (sym->s_prfl) {
165 outchar('P'); /* printflike */
166 outint(sym->s_nprfl);
167 }
168 /* definition or tentative definition */
169 outchar(sym->s_def == DEF ? 'd' : 't');
170 if (TP(sym->s_type)->t_tspec == FUNC) {
171 if (sym->s_rval)
172 outchar('r'); /* fkt. has return value */
173 if (sym->s_osdef)
174 outchar('o'); /* old style definition */
175 }
176 outchar('u'); /* used (no warning if not used) */
177
178 /* name */
179 outname(hte->h_name);
180
181 /* type */
182 outtype(TP(sym->s_type));
183 }
184
185 /*
186 * Write the first definition of a name into the lint library.
187 */
188 static void
189 dumpname(hte_t *hte)
190 {
191 sym_t *sym, *def;
192
193 /* static and undefined symbols are not written */
194 if (hte->h_static || !hte->h_def)
195 return;
196
197 /*
198 * If there is a definition, write it. Otherwise write a tentative
199 * definition. This is necessary because more than one tentative
200 * definition is allowed (except with sflag).
201 */
202 def = NULL;
203 for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
204 if (sym->s_def == DEF) {
205 def = sym;
206 break;
207 }
208 if (sym->s_def == TDEF && def == NULL)
209 def = sym;
210 }
211 if (def == NULL)
212 errx(1, "internal error: dumpname() %s", hte->h_name);
213
214 outdef(hte, def);
215 }
216
217 /*
218 * Write a new lint library.
219 */
220 void
221 outlib(const char *name)
222 {
223 /* Open of output file and initialisation of the output buffer */
224 outopen(name);
225
226 /* write name of lint library */
227 outsrc(name);
228
229 /* name of lint lib has index 0 */
230 outclr();
231 outint(0);
232 outchar('s');
233 outstrg(name);
234
235 /*
236 * print the names of all files references by unnamed
237 * struct/union/enum declarations.
238 */
239 outfiles();
240
241 /* write all definitions with external linkage */
242 forall(dumpname);
243
244 /* close the output */
245 outclose();
246 }
247
248 /*
249 * Write out the name of a file referenced by a type.
250 */
251 struct outflist {
252 short ofl_num;
253 struct outflist *ofl_next;
254 };
255 static struct outflist *outflist;
256
257 int
258 addoutfile(short num)
259 {
260 struct outflist *ofl, **pofl;
261 int i;
262
263 ofl = outflist;
264 pofl = &outflist;
265 i = 1; /* library is 0 */
266
267 while (ofl != NULL) {
268 if (ofl->ofl_num == num)
269 break;
270
271 pofl = &ofl->ofl_next;
272 ofl = ofl->ofl_next;
273 i++;
274 }
275
276 if (ofl == NULL) {
277 ofl = *pofl = xmalloc(sizeof (struct outflist));
278 ofl->ofl_num = num;
279 ofl->ofl_next = NULL;
280 }
281 return (i);
282 }
283
284 static void
285 outfiles(void)
286 {
287 struct outflist *ofl;
288 int i;
289
290 for (ofl = outflist, i = 1; ofl != NULL; ofl = ofl->ofl_next, i++) {
291 /* reset output buffer */
292 outclr();
293
294 outint(i);
295 outchar('s');
296 outstrg(fnames[ofl->ofl_num]);
297 }
298 }
299