emit2.c revision 1.23 1 /* $NetBSD: emit2.c,v 1.23 2021/08/29 10:13:02 rillig 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.23 2021/08/29 10:13:02 rillig 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_is_enum)
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 #ifdef INT128_SIZE
74 case INT128: t = 'J'; s = '\0'; break;
75 case UINT128: t = 'J'; s = 'u'; break;
76 #endif
77 case FLOAT: t = 'D'; s = 's'; break;
78 case DOUBLE: t = 'D'; s = '\0'; break;
79 case LDOUBLE: t = 'D'; s = 'l'; break;
80 case VOID: t = 'V'; s = '\0'; break;
81 case PTR: t = 'P'; s = '\0'; break;
82 case ARRAY: t = 'A'; s = '\0'; break;
83 case ENUM: t = 'T'; s = 'e'; break;
84 case STRUCT: t = 'T'; s = 's'; break;
85 case UNION: t = 'T'; s = 'u'; break;
86 case FCOMPLEX: t = 'X'; s = 's'; break;
87 case DCOMPLEX: t = 'X'; s = '\0'; break;
88 case LCOMPLEX: t = 'X'; s = 'l'; break;
89 case FUNC:
90 if (tp->t_args != NULL && !tp->t_proto) {
91 t = 'f';
92 } else {
93 t = 'F';
94 }
95 s = '\0';
96 break;
97 default:
98 errx(1, "internal error: outtype() 1");
99 }
100 if (tp->t_const)
101 outchar('c');
102 if (tp->t_volatile)
103 outchar('v');
104 if (s != '\0')
105 outchar(s);
106 outchar(t);
107 if (ts == ARRAY) {
108 outint(tp->t_dim);
109 } else if (ts == ENUM || ts == STRUCT || ts == UNION) {
110 if (tp->t_istag) {
111 outint(1);
112 outname(tp->t_tag->h_name);
113 } else if (tp->t_istynam) {
114 outint(2);
115 outname(tp->t_tynam->h_name);
116 } else if (tp->t_isuniqpos) {
117 outint(3);
118 outint(tp->t_uniqpos.p_line);
119 outchar('.');
120 outint(tp->t_uniqpos.p_file);
121 outchar('.');
122 outint(tp->t_uniqpos.p_uniq);
123 } else
124 errx(1, "internal error: outtype() 2");
125 } else if (ts == FUNC && tp->t_args != NULL) {
126 na = 0;
127 for (ap = tp->t_args; *ap != NULL; ap++)
128 na++;
129 if (tp->t_vararg)
130 na++;
131 outint(na);
132 for (ap = tp->t_args; *ap != NULL; ap++)
133 outtype(*ap);
134 if (tp->t_vararg)
135 outchar('E');
136 }
137 tp = tp->t_subt;
138 }
139 }
140
141 /*
142 * Write a definition.
143 */
144 static void
145 outdef(hte_t *hte, sym_t *sym)
146 {
147
148 /* reset output buffer */
149 outclr();
150
151 /* line number in C source file */
152 outint(0);
153
154 /* this is a definition */
155 outchar('d');
156
157 /* index of file where symbol was defined and line number of def. */
158 outint(0);
159 outchar('.');
160 outint(0);
161
162 /* flags */
163 if (sym->s_check_only_first_args) {
164 outchar('v');
165 outint(sym->s_check_num_args);
166 }
167 if (sym->s_scanflike) {
168 outchar('S');
169 outint(sym->s_scanflike_arg);
170 }
171 if (sym->s_printflike) {
172 outchar('P');
173 outint(sym->s_printflike_arg);
174 }
175 /* definition or tentative definition */
176 outchar(sym->s_def == DEF ? 'd' : 't');
177 if (TP(sym->s_type)->t_tspec == FUNC) {
178 if (sym->s_function_has_return_value)
179 outchar('r');
180 if (sym->s_old_style_function)
181 outchar('o');
182 }
183 outchar('u'); /* used (no warning if not used) */
184
185 /* name */
186 outname(hte->h_name);
187
188 /* type */
189 outtype(TP(sym->s_type));
190 }
191
192 /*
193 * Write the first definition of a name into the lint library.
194 */
195 static void
196 dumpname(hte_t *hte)
197 {
198 sym_t *sym, *def;
199
200 /* static and undefined symbols are not written */
201 if (hte->h_static || !hte->h_def)
202 return;
203
204 /*
205 * If there is a definition, write it. Otherwise write a tentative
206 * definition. This is necessary because more than one tentative
207 * definition is allowed (except with sflag).
208 */
209 def = NULL;
210 for (sym = hte->h_syms; sym != NULL; sym = sym->s_next) {
211 if (sym->s_def == DEF) {
212 def = sym;
213 break;
214 }
215 if (sym->s_def == TDEF && def == NULL)
216 def = sym;
217 }
218 if (def == NULL)
219 errx(1, "internal error: dumpname() %s", hte->h_name);
220
221 outdef(hte, def);
222 }
223
224 /*
225 * Write a new lint library.
226 */
227 void
228 outlib(const char *name)
229 {
230 /* Open of output file and initialization of the output buffer */
231 outopen(name);
232
233 /* write name of lint library */
234 outsrc(name);
235
236 /* name of lint lib has index 0 */
237 outclr();
238 outint(0);
239 outchar('s');
240 outstrg(name);
241
242 /*
243 * print the names of all files references by unnamed
244 * struct/union/enum declarations.
245 */
246 outfiles();
247
248 /* write all definitions with external linkage */
249 symtab_forall_sorted(dumpname);
250
251 /* close the output */
252 outclose();
253 }
254
255 /*
256 * Write out the name of a file referenced by a type.
257 */
258 struct outflist {
259 short ofl_num;
260 struct outflist *ofl_next;
261 };
262 static struct outflist *outflist;
263
264 int
265 addoutfile(short num)
266 {
267 struct outflist *ofl, **pofl;
268 int i;
269
270 ofl = outflist;
271 pofl = &outflist;
272 i = 1; /* library is 0 */
273
274 while (ofl != NULL) {
275 if (ofl->ofl_num == num)
276 break;
277
278 pofl = &ofl->ofl_next;
279 ofl = ofl->ofl_next;
280 i++;
281 }
282
283 if (ofl == NULL) {
284 ofl = *pofl = xmalloc(sizeof(**pofl));
285 ofl->ofl_num = num;
286 ofl->ofl_next = NULL;
287 }
288 return i;
289 }
290
291 static void
292 outfiles(void)
293 {
294 struct outflist *ofl;
295 int i;
296
297 for (ofl = outflist, i = 1; ofl != NULL; ofl = ofl->ofl_next, i++) {
298 /* reset output buffer */
299 outclr();
300
301 outint(i);
302 outchar('s');
303 outstrg(fnames[ofl->ofl_num]);
304 }
305 }
306