emit1.c revision 1.82 1 /* $NetBSD: emit1.c,v 1.82 2024/01/29 21:30:24 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 #if HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
37 #endif
38
39 #include <sys/cdefs.h>
40 #if defined(__RCSID)
41 __RCSID("$NetBSD: emit1.c,v 1.82 2024/01/29 21:30:24 rillig Exp $");
42 #endif
43
44 #include "lint1.h"
45
46 static void outtt(sym_t *, sym_t *);
47 static void outfstrg(strg_t *);
48
49 /*
50 * Write type into the output file, encoded as follows:
51 * const c
52 * volatile v
53 * _Bool B
54 * _Complex float s X
55 * _Complex double X
56 * _Complex long double l X
57 * char C
58 * signed char s C
59 * unsigned char u C
60 * short S
61 * unsigned short u S
62 * int I
63 * unsigned int u I
64 * long L
65 * unsigned long u L
66 * long long Q
67 * unsigned long long u Q
68 * float s D
69 * double D
70 * long double l D
71 * void V
72 * * P
73 * [n] A n
74 * () F
75 * (void) F 0
76 * (n parameters) F n arg1 arg2 ... argn
77 * (n parameters, ...) F n arg1 arg2 ... argn E
78 * enum tag e T tag_or_typename
79 * struct tag s T tag_or_typename
80 * union tag u T tag_or_typename
81 *
82 * tag_or_typename 0 (obsolete) no tag or type name
83 * 1 n tag tagged type
84 * 2 n typename only typedef name
85 * 3 line.file.uniq anonymous types
86 */
87 void
88 outtype(const type_t *tp)
89 {
90 /* Available letters: ------GH--K-MNO--R--U-W-YZ */
91 #ifdef INT128_SIZE
92 static const char tt[NTSPEC] = "???BCCCSSIILLQQJJDDD?XXXVTTTPAF";
93 static const char ss[NTSPEC] = "??? su u u u u us l?s l sue ";
94 #else
95 static const char tt[NTSPEC] = "???BCCCSSIILLQQDDD?XXXVTTTPAF";
96 static const char ss[NTSPEC] = "??? su u u u us l?s l sue ";
97 #endif
98 int na;
99 tspec_t ts;
100
101 while (tp != NULL) {
102 if ((ts = tp->t_tspec) == INT && tp->t_is_enum)
103 ts = ENUM;
104 lint_assert(tt[ts] != '?' && ss[ts] != '?');
105 if (tp->t_const)
106 outchar('c');
107 if (tp->t_volatile)
108 outchar('v');
109 if (ss[ts] != ' ')
110 outchar(ss[ts]);
111 outchar(tt[ts]);
112
113 if (ts == ARRAY) {
114 outint(tp->t_dim);
115 } else if (ts == ENUM) {
116 outtt(tp->t_enum->en_tag, tp->t_enum->en_first_typedef);
117 } else if (is_struct_or_union(ts)) {
118 outtt(tp->t_sou->sou_tag, tp->t_sou->sou_first_typedef);
119 } else if (ts == FUNC && tp->t_proto) {
120 na = 0;
121 for (const sym_t *param = tp->t_params;
122 param != NULL; param = param->s_next)
123 na++;
124 if (tp->t_vararg)
125 na++;
126 outint(na);
127 for (const sym_t *param = tp->t_params;
128 param != NULL; param = param->s_next)
129 outtype(param->s_type);
130 if (tp->t_vararg)
131 outchar('E');
132 }
133 tp = tp->t_subt;
134 }
135 }
136
137 /*
138 * write the name of a tag or typename
139 *
140 * if the tag is named, the name of the tag is written,
141 * otherwise, if a typename exists which refers to this tag,
142 * this typename is written
143 */
144 static void
145 outtt(sym_t *tag, sym_t *tdef)
146 {
147
148 /* 0 is no longer used. */
149
150 if (tag->s_name != unnamed) {
151 outint(1);
152 outname(tag->s_name);
153 } else if (tdef != NULL) {
154 outint(2);
155 outname(tdef->s_name);
156 } else {
157 outint(3);
158 outint(tag->s_def_pos.p_line);
159 outchar('.');
160 outint(get_filename_id(tag->s_def_pos.p_file));
161 outchar('.');
162 outint(tag->s_def_pos.p_uniq);
163 }
164 }
165
166 /*
167 * write information about a globally declared/defined symbol
168 * with storage class extern
169 *
170 * information about function definitions are written in outfdef(),
171 * not here
172 */
173 void
174 outsym(const sym_t *sym, scl_t sc, def_t def)
175 {
176
177 /*
178 * Static function declarations must also be written to the output
179 * file. Compatibility of function declarations (for both static and
180 * extern functions) must be checked in lint2. Lint1 can't do this,
181 * especially not if functions are declared at block level before their
182 * first declaration at level 0.
183 */
184 if (sc != EXTERN && !(sc == STATIC && sym->s_type->t_tspec == FUNC))
185 return;
186 if (ch_isdigit(sym->s_name[0])) /* 00000000_tmp */
187 return;
188
189 outint(csrc_pos.p_line);
190 outchar('d'); /* declaration */
191 outint(get_filename_id(sym->s_def_pos.p_file));
192 outchar('.');
193 outint(sym->s_def_pos.p_line);
194
195 /* flags */
196
197 if (def == DEF)
198 outchar('d'); /* defined */
199 else if (def == TDEF)
200 outchar('t'); /* tentative defined */
201 else {
202 lint_assert(def == DECL);
203 outchar('e'); /* declared */
204 }
205
206 if (llibflg && def != DECL) {
207 /*
208 * mark it as used so lint2 does not complain about unused
209 * symbols in libraries
210 */
211 outchar('u');
212 }
213
214 if (sc == STATIC)
215 outchar('s');
216
217 outname(sym->s_name);
218
219 if (sym->s_rename != NULL) {
220 outchar('r');
221 outname(sym->s_rename);
222 }
223
224 outtype(sym->s_type);
225 outchar('\n');
226 }
227
228 /*
229 * Write information about a function definition. This is also done for static
230 * functions, to later check if they are called with proper argument types.
231 */
232 void
233 outfdef(const sym_t *fsym, const pos_t *posp, bool rval, bool osdef,
234 const sym_t *args)
235 {
236 int narg;
237 const sym_t *arg;
238
239 if (posp->p_file == csrc_pos.p_file) {
240 outint(posp->p_line);
241 } else {
242 outint(csrc_pos.p_line);
243 }
244 outchar('d'); /* declaration */
245 outint(get_filename_id(posp->p_file));
246 outchar('.');
247 outint(posp->p_line);
248
249 /* both SCANFLIKE and PRINTFLIKE imply VARARGS */
250 if (printflike_argnum != -1) {
251 nvararg = printflike_argnum;
252 } else if (scanflike_argnum != -1) {
253 nvararg = scanflike_argnum;
254 }
255
256 if (nvararg != -1) {
257 outchar('v');
258 outint(nvararg);
259 }
260 if (scanflike_argnum != -1) {
261 outchar('S');
262 outint(scanflike_argnum);
263 }
264 if (printflike_argnum != -1) {
265 outchar('P');
266 outint(printflike_argnum);
267 }
268 nvararg = printflike_argnum = scanflike_argnum = -1;
269
270 outchar('d');
271
272 if (rval)
273 outchar('r'); /* has return value */
274
275 if (llibflg)
276 /*
277 * mark it as used so lint2 does not complain about unused
278 * symbols in libraries
279 */
280 outchar('u');
281
282 if (osdef)
283 outchar('o'); /* old-style function definition */
284
285 if (fsym->s_inline)
286 outchar('i');
287
288 if (fsym->s_scl == STATIC)
289 outchar('s');
290
291 outname(fsym->s_name);
292
293 if (fsym->s_rename != NULL) {
294 outchar('r');
295 outname(fsym->s_rename);
296 }
297
298 /* parameter types and return value */
299 if (osdef) {
300 narg = 0;
301 for (arg = args; arg != NULL; arg = arg->s_next)
302 narg++;
303 outchar('f');
304 outint(narg);
305 for (arg = args; arg != NULL; arg = arg->s_next)
306 outtype(arg->s_type);
307 outtype(fsym->s_type->t_subt);
308 } else {
309 outtype(fsym->s_type);
310 }
311 outchar('\n');
312 }
313
314 /*
315 * write out all information necessary for lint2 to check function
316 * calls
317 *
318 * retval_used is set if the return value is used (assigned to a variable)
319 * retval_discarded is set if the return value is neither used nor ignored
320 * (that is, cast to void)
321 */
322 void
323 outcall(const tnode_t *tn, bool retval_used, bool retval_discarded)
324 {
325 tnode_t *args, *arg;
326 int narg, n, i;
327 tspec_t t;
328
329 outint(csrc_pos.p_line);
330 outchar('c'); /* function call */
331 outint(get_filename_id(curr_pos.p_file));
332 outchar('.');
333 outint(curr_pos.p_line);
334
335 /*
336 * flags; 'u' and 'i' must be last to make sure a letter is between the
337 * numeric argument of a flag and the name of the function
338 */
339 narg = 0;
340 args = tn_ck_right(tn);
341 for (arg = args; arg != NULL; arg = tn_ck_right(arg))
342 narg++;
343 /* information about arguments */
344 for (n = 1; n <= narg; n++) {
345 /* the last argument is the top one in the tree */
346 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
347 continue;
348 arg = arg->tn_left;
349 if (arg->tn_op == CON) {
350 if (is_integer(t = arg->tn_type->t_tspec)) {
351 /*
352 * XXX it would probably be better to
353 * explicitly test the sign
354 */
355 int64_t si = arg->tn_val.u.integer;
356 if (si == 0) {
357 /* zero constant */
358 outchar('z');
359 } else if (!msb(si, t)) {
360 /* positive if cast to signed */
361 outchar('p');
362 } else {
363 /* negative if cast to signed */
364 outchar('n');
365 }
366 outint(n);
367 }
368 } else if (arg->tn_op == ADDR &&
369 arg->tn_left->tn_op == STRING &&
370 arg->tn_left->tn_string->st_char) {
371 /* constant string, write all format specifiers */
372 outchar('s');
373 outint(n);
374 outfstrg(arg->tn_left->tn_string);
375 }
376 }
377 outchar((char)(retval_discarded ? 'd' : retval_used ? 'u' : 'i'));
378
379 /* name of the called function */
380 outname(tn_ck_left(tn->tn_left)->tn_sym->s_name);
381
382 /* types of arguments */
383 outchar('f');
384 outint(narg);
385 for (n = 1; n <= narg; n++) {
386 /* the last argument is the top one in the tree */
387 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
388 continue;
389 outtype(arg->tn_left->tn_type);
390 }
391 /* expected type of return value */
392 outtype(tn->tn_type);
393 outchar('\n');
394 }
395
396 /* write a character to the output file, quoted if necessary */
397 static void
398 outqchar(char c)
399 {
400
401 if (ch_isprint(c) && c != '\\' && c != '"' && c != '\'') {
402 outchar(c);
403 return;
404 }
405
406 outchar('\\');
407 switch (c) {
408 case '\\':
409 outchar('\\');
410 break;
411 case '"':
412 outchar('"');
413 break;
414 case '\'':
415 outchar('\'');
416 break;
417 case '\b':
418 outchar('b');
419 break;
420 case '\t':
421 outchar('t');
422 break;
423 case '\n':
424 outchar('n');
425 break;
426 case '\f':
427 outchar('f');
428 break;
429 case '\r':
430 outchar('r');
431 break;
432 case '\v':
433 outchar('v');
434 break;
435 case '\a':
436 outchar('a');
437 break;
438 default:
439 outchar((char)((((unsigned char)c >> 6) & 07) + '0'));
440 outchar((char)((((unsigned char)c >> 3) & 07) + '0'));
441 outchar((char)((c & 07) + '0'));
442 break;
443 }
444 }
445
446 /*
447 * extracts potential format specifiers for printf() and scanf() and
448 * writes them, enclosed in "" and quoted if necessary, to the output file
449 */
450 static void
451 outfstrg(strg_t *strg)
452 {
453
454 lint_assert(strg->st_char);
455 const char *cp = strg->st_chars;
456
457 outchar('"');
458
459 char c = *cp++;
460 while (c != '\0') {
461
462 if (c != '%') {
463 c = *cp++;
464 continue;
465 }
466
467 outchar('%');
468 c = *cp++;
469
470 /* flags for printf and scanf and *-fieldwidth for printf */
471 while (c == '-' || c == '+' || c == ' ' ||
472 c == '#' || c == '0' || c == '*') {
473 outchar(c);
474 c = *cp++;
475 }
476
477 /* numeric field width */
478 while (ch_isdigit(c)) {
479 outchar(c);
480 c = *cp++;
481 }
482
483 /* precision for printf */
484 if (c == '.') {
485 outchar(c);
486 c = *cp++;
487 if (c == '*') {
488 outchar(c);
489 c = *cp++;
490 } else {
491 while (ch_isdigit(c)) {
492 outchar(c);
493 c = *cp++;
494 }
495 }
496 }
497
498 /* h, l, L and q flags for printf and scanf */
499 if (c == 'h' || c == 'l' || c == 'L' || c == 'q') {
500 outchar(c);
501 c = *cp++;
502 }
503
504 /*
505 * The last character. It is always written, so we can detect
506 * invalid format specifiers.
507 */
508 if (c != '\0') {
509 outqchar(c);
510 char oc = c;
511 c = *cp++;
512 /*
513 * handle [ for scanf. [-] means that a minus sign was
514 * found at an undefined position.
515 */
516 if (oc == '[') {
517 if (c == '^')
518 c = *cp++;
519 if (c == ']')
520 c = *cp++;
521 bool first = true;
522 while (c != '\0' && c != ']') {
523 if (c == '-') {
524 if (!first && *cp != ']')
525 outchar(c);
526 }
527 first = false;
528 c = *cp++;
529 }
530 if (c == ']') {
531 outchar(c);
532 c = *cp++;
533 }
534 }
535 }
536 }
537
538 outchar('"');
539 }
540
541 /* writes a record if sym was used */
542 void
543 outusg(const sym_t *sym)
544 {
545 if (ch_isdigit(sym->s_name[0])) /* 00000000_tmp, from mktempsym */
546 return;
547
548 outint(csrc_pos.p_line);
549 outchar('u'); /* used */
550 outint(get_filename_id(curr_pos.p_file));
551 outchar('.');
552 outint(curr_pos.p_line);
553 outchar('x'); /* separate the two numbers */
554 outname(sym->s_name);
555 outchar('\n');
556 }
557