emit1.c revision 1.6 1 /* $NetBSD: emit1.c,v 1.6 1997/11/03 22:36:38 cgd 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 #ifndef lint
36 static char rcsid[] = "$NetBSD: emit1.c,v 1.6 1997/11/03 22:36:38 cgd Exp $";
37 #endif
38
39 #include <ctype.h>
40
41 #include "lint1.h"
42
43 static void outtt __P((sym_t *, sym_t *));
44 static void outfstrg __P((strg_t *));
45
46 /*
47 * Write type into the output buffer.
48 * The type is written as a sequence of substrings, each of which describes a
49 * node of type type_t
50 * a node is coded as follows:
51 * char C
52 * signed char s C
53 * unsigned char u C
54 * short S
55 * unsigned short u S
56 * int I
57 * unsigned int u I
58 * long L
59 * unsigned long u L
60 * long long Q
61 * unsigned long long u Q
62 * float s D
63 * double D
64 * long double l D
65 * void V
66 * * P
67 * [n] A n
68 * () F
69 * (void) F 0
70 * (n arguments) F n arg1 arg2 ... argn
71 * (n arguments, ...) F n arg1 arg2 ... argn-1 E
72 * (a, b, c, ...) f n arg1 arg2 ...
73 * enum tag e T tag_or_typename
74 * struct tag s T tag_or_typename
75 * union tag u T tag_or_typename
76 *
77 * tag_or_typename 0 no tag or type name
78 * 1 n tag Tag
79 * 2 n typename only type name
80 *
81 * spaces are only for better readability
82 * additionaly it is possible to prepend the characters 'c' (for const)
83 * and 'v' (for volatile)
84 */
85 void
86 outtype(tp)
87 type_t *tp;
88 {
89 int t, s, na;
90 sym_t *arg;
91 tspec_t ts;
92
93 while (tp != NULL) {
94 if ((ts = tp->t_tspec) == INT && tp->t_isenum)
95 ts = ENUM;
96 switch (ts) {
97 case CHAR: t = 'C'; s = '\0'; break;
98 case SCHAR: t = 'C'; s = 's'; break;
99 case UCHAR: t = 'C'; s = 'u'; break;
100 case SHORT: t = 'S'; s = '\0'; break;
101 case USHORT: t = 'S'; s = 'u'; break;
102 case INT: t = 'I'; s = '\0'; break;
103 case UINT: t = 'I'; s = 'u'; break;
104 case LONG: t = 'L'; s = '\0'; break;
105 case ULONG: t = 'L'; s = 'u'; break;
106 case QUAD: t = 'Q'; s = '\0'; break;
107 case UQUAD: t = 'Q'; s = 'u'; break;
108 case FLOAT: t = 'D'; s = 's'; break;
109 case DOUBLE: t = 'D'; s = '\0'; break;
110 case LDOUBLE: t = 'D'; s = 'l'; break;
111 case VOID: t = 'V'; s = '\0'; break;
112 case PTR: t = 'P'; s = '\0'; break;
113 case ARRAY: t = 'A'; s = '\0'; break;
114 case FUNC: t = 'F'; s = '\0'; break;
115 case ENUM: t = 'T'; s = 'e'; break;
116 case STRUCT: t = 'T'; s = 's'; break;
117 case UNION: t = 'T'; s = 'u'; break;
118 default:
119 lerror("outtyp() 1");
120 }
121 if (tp->t_const)
122 outchar('c');
123 if (tp->t_volatile)
124 outchar('v');
125 if (s != '\0')
126 outchar(s);
127 outchar(t);
128 if (ts == ARRAY) {
129 outint(tp->t_dim);
130 } else if (ts == ENUM) {
131 outtt(tp->t_enum->etag, tp->t_enum->etdef);
132 } else if (ts == STRUCT || ts == UNION) {
133 outtt(tp->t_str->stag, tp->t_str->stdef);
134 } else if (ts == FUNC && tp->t_proto) {
135 na = 0;
136 for (arg = tp->t_args; arg != NULL; arg = arg->s_nxt)
137 na++;
138 if (tp->t_vararg)
139 na++;
140 outint(na);
141 for (arg = tp->t_args; arg != NULL; arg = arg->s_nxt)
142 outtype(arg->s_type);
143 if (tp->t_vararg)
144 outchar('E');
145 }
146 tp = tp->t_subt;
147 }
148 }
149
150 /*
151 * type to string
152 * used for debugging output
153 *
154 * it uses its own output buffer for conversion
155 */
156 const char *
157 ttos(tp)
158 type_t *tp;
159 {
160 static ob_t tob;
161 ob_t tmp;
162
163 if (tob.o_buf == NULL) {
164 tob.o_len = 64;
165 tob.o_buf = tob.o_nxt = xmalloc(tob.o_len);
166 tob.o_end = tob.o_buf + tob.o_len;
167 }
168
169 tmp = ob;
170 ob = tob;
171 ob.o_nxt = ob.o_buf;
172 outtype(tp);
173 outchar('\0');
174 tob = ob;
175 ob = tmp;
176
177 return (tob.o_buf);
178 }
179
180 /*
181 * write the name of a tag or typename
182 *
183 * if the tag is named, the name of the
184 * tag is written, otherwise, if a typename exists which
185 * refers to this tag, this typename is written
186 */
187 static void
188 outtt(tag, tdef)
189 sym_t *tag, *tdef;
190 {
191
192 /*
193 * 0 is no longer used.
194 */
195 if (tag->s_name != unnamed) {
196 outint(1);
197 outname(tag->s_name);
198 } else if (tdef != NULL) {
199 outint(2);
200 outname(tdef->s_name);
201 } else {
202 outint(3);
203 outint(tag->s_dpos.p_line);
204 outchar('.');
205 outint(getfnid(tag->s_dpos.p_file));
206 outchar('.');
207 outint(tag->s_dpos.p_uniq);
208 }
209 }
210
211 /*
212 * write information about an global declared/defined symbol
213 * with storage class extern
214 *
215 * informations about function definitions are written in outfdef(),
216 * not here
217 */
218 void
219 outsym(sym, sc, def)
220 sym_t *sym;
221 scl_t sc;
222 def_t def;
223 {
224 /*
225 * Static function declarations must also be written to the output
226 * file. Compatibility of function declarations (for both static
227 * and extern functions) must be checked in lint2. Lint1 can't do
228 * this, especially not, if functions are declared at block level
229 * before their first declaration at level 0.
230 */
231 if (sc != EXTERN && !(sc == STATIC && sym->s_type->t_tspec == FUNC))
232 return;
233
234 /* reset buffer */
235 outclr();
236
237 /*
238 * line number of .c source, 'd' for declaration, Id of current
239 * source (.c or .h), and line in current source.
240 */
241 outint(csrc_pos.p_line);
242 outchar('d');
243 outint(getfnid(sym->s_dpos.p_file));
244 outchar('.');
245 outint(sym->s_dpos.p_line);
246
247 /* flags */
248
249 switch (def) {
250 case DEF:
251 /* defined */
252 outchar('d');
253 break;
254 case TDEF:
255 /* tentative defined */
256 outchar('t');
257 break;
258 case DECL:
259 /* declared */
260 outchar('e');
261 break;
262 default:
263 lerror("outsym() 2");
264 }
265 if (llibflg && def != DECL) {
266 /*
267 * mark it as used so we get no warnings from lint2 about
268 * unused symbols in libraries.
269 */
270 outchar('u');
271 }
272
273 if (sc == STATIC)
274 outchar('s');
275
276 /* name of the symbol */
277 outname(sym->s_name);
278
279 /* renamed name of symbol, if necessary */
280 if (sym->s_rename) {
281 outchar('r');
282 outname(sym->s_rename);
283 }
284
285 /* type of the symbol */
286 outtype(sym->s_type);
287 }
288
289 /*
290 * write information about function definition
291 *
292 * this is also done for static functions so we are able to check if
293 * they are called with proper argument types
294 */
295 void
296 outfdef(fsym, posp, rval, osdef, args)
297 sym_t *fsym, *args;
298 pos_t *posp;
299 int rval, osdef;
300 {
301 int narg;
302 sym_t *arg;
303
304 /* reset the buffer */
305 outclr();
306
307 /*
308 * line number of .c source, 'd' for declaration, Id of current
309 * source (.c or .h), and line in current source
310 *
311 * we are already at the end of the function. If we are in the
312 * .c source, posp->p_line is correct, otherwise csrc_pos.p_line
313 * (for functions defined in header files).
314 */
315 if (posp->p_file == csrc_pos.p_file) {
316 outint(posp->p_line);
317 } else {
318 outint(csrc_pos.p_line);
319 }
320 outchar('d');
321 outint(getfnid(posp->p_file));
322 outchar('.');
323 outint(posp->p_line);
324
325 /* flags */
326
327 /* both SCANFLIKE and PRINTFLIKE imply VARARGS */
328 if (prflstrg != -1) {
329 nvararg = prflstrg;
330 } else if (scflstrg != -1) {
331 nvararg = scflstrg;
332 }
333
334 if (nvararg != -1) {
335 outchar('v');
336 outint(nvararg);
337 }
338 if (scflstrg != -1) {
339 outchar('S');
340 outint(scflstrg);
341 }
342 if (prflstrg != -1) {
343 outchar('P');
344 outint(prflstrg);
345 }
346 nvararg = prflstrg = scflstrg = -1;
347
348 outchar('d');
349
350 if (rval)
351 /* has return value */
352 outchar('r');
353
354 if (llibflg)
355 /*
356 * mark it as used so lint2 does not complain about
357 * unused symbols in libraries
358 */
359 outchar('u');
360
361 if (osdef)
362 /* old style function definition */
363 outchar('o');
364
365 if (fsym->s_scl == STATIC)
366 outchar('s');
367
368 /* name of function */
369 outname(fsym->s_name);
370
371 /* renamed name of function, if necessary */
372 if (fsym->s_rename) {
373 outchar('r');
374 outname(fsym->s_rename);
375 }
376
377 /* argument types and return value */
378 if (osdef) {
379 narg = 0;
380 for (arg = args; arg != NULL; arg = arg->s_nxt)
381 narg++;
382 outchar('f');
383 outint(narg);
384 for (arg = args; arg != NULL; arg = arg->s_nxt)
385 outtype(arg->s_type);
386 outtype(fsym->s_type->t_subt);
387 } else {
388 outtype(fsym->s_type);
389 }
390 }
391
392 /*
393 * write out all information necessary for lint2 to check function
394 * calls
395 *
396 * rvused is set if the return value is used (asigned to a variable)
397 * rvdisc is set if the return value is not used and not ignored
398 * (casted to void)
399 */
400 void
401 outcall(tn, rvused, rvdisc)
402 tnode_t *tn;
403 int rvused, rvdisc;
404 {
405 tnode_t *args, *arg;
406 int narg, n, i;
407 quad_t q;
408 tspec_t t;
409
410 /* reset buffer */
411 outclr();
412
413 /*
414 * line number of .c source, 'c' for function call, Id of current
415 * source (.c or .h), and line in current source
416 */
417 outint(csrc_pos.p_line);
418 outchar('c');
419 outint(getfnid(curr_pos.p_file));
420 outchar('.');
421 outint(curr_pos.p_line);
422
423 /*
424 * flags; 'u' and 'i' must be last to make sure a letter
425 * is between the numeric argument of a flag and the name of
426 * the function
427 */
428 narg = 0;
429 args = tn->tn_right;
430 for (arg = args; arg != NULL; arg = arg->tn_right)
431 narg++;
432 /* informations about arguments */
433 for (n = 1; n <= narg; n++) {
434 /* the last argument is the top one in the tree */
435 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right) ;
436 arg = arg->tn_left;
437 if (arg->tn_op == CON) {
438 if (isityp(t = arg->tn_type->t_tspec)) {
439 /*
440 * XXX it would probably be better to
441 * explizitly test the sign
442 */
443 if ((q = arg->tn_val->v_quad) == 0) {
444 /* zero constant */
445 outchar('z');
446 } else if (msb(q, t, 0) == 0) {
447 /* positive if casted to signed */
448 outchar('p');
449 } else {
450 /* negative if casted to signed */
451 outchar('n');
452 }
453 outint(n);
454 }
455 } else if (arg->tn_op == AMPER &&
456 arg->tn_left->tn_op == STRING &&
457 arg->tn_left->tn_strg->st_tspec == CHAR) {
458 /* constant string, write all format specifiers */
459 outchar('s');
460 outint(n);
461 outfstrg(arg->tn_left->tn_strg);
462 }
463
464 }
465 /* return value discarded/used/ignored */
466 outchar(rvdisc ? 'd' : (rvused ? 'u' : 'i'));
467
468 /* name of the called function */
469 outname(tn->tn_left->tn_left->tn_sym->s_name);
470
471 /* types of arguments */
472 outchar('f');
473 outint(narg);
474 for (n = 1; n <= narg; n++) {
475 /* the last argument is the top one in the tree */
476 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right) ;
477 outtype(arg->tn_left->tn_type);
478 }
479 /* expected type of return value */
480 outtype(tn->tn_type);
481 }
482
483 /*
484 * extracts potential format specifiers for printf() and scanf() and
485 * writes them, enclosed in "" and qouted if necessary, to the output buffer
486 */
487 static void
488 outfstrg(strg)
489 strg_t *strg;
490 {
491 int c, oc, first;
492 u_char *cp;
493
494 if (strg->st_tspec != CHAR)
495 lerror("outfstrg() 1");
496
497 cp = strg->st_cp;
498
499 outchar('"');
500
501 c = *cp++;
502
503 while (c != '\0') {
504
505 if (c != '%') {
506 c = *cp++;
507 continue;
508 }
509
510 outqchar('%');
511 c = *cp++;
512
513 /* flags for printf and scanf and *-fieldwidth for printf */
514 while (c != '\0' && (c == '-' || c == '+' || c == ' ' ||
515 c == '#' || c == '0' || c == '*')) {
516 outqchar(c);
517 c = *cp++;
518 }
519
520 /* numeric field width */
521 while (c != '\0' && isdigit(c)) {
522 outqchar(c);
523 c = *cp++;
524 }
525
526 /* precision for printf */
527 if (c == '.') {
528 outqchar(c);
529 if ((c = *cp++) == '*') {
530 outqchar(c);
531 c = *cp++;
532 } else {
533 while (c != '\0' && isdigit(c)) {
534 outqchar(c);
535 c = *cp++;
536 }
537 }
538 }
539
540 /* h, l, L and q flags fpr printf and scanf */
541 if (c == 'h' || c == 'l' || c == 'L' || c == 'q') {
542 outqchar(c);
543 c = *cp++;
544 }
545
546 /*
547 * The last character. It is always written so we can detect
548 * invalid format specifiers.
549 */
550 if (c != '\0') {
551 outqchar(c);
552 oc = c;
553 c = *cp++;
554 /*
555 * handle [ for scanf. [-] means that a minus sign
556 * was found at an undefined position.
557 */
558 if (oc == '[') {
559 if (c == '^')
560 c = *cp++;
561 if (c == ']')
562 c = *cp++;
563 first = 1;
564 while (c != '\0' && c != ']') {
565 if (c == '-') {
566 if (!first && *cp != ']')
567 outqchar(c);
568 }
569 first = 0;
570 c = *cp++;
571 }
572 if (c == ']') {
573 outqchar(c);
574 c = *cp++;
575 }
576 }
577 }
578
579 }
580
581 outchar('"');
582 }
583
584 /*
585 * writes a record if sym was used
586 */
587 void
588 outusg(sym)
589 sym_t *sym;
590 {
591 /* reset buffer */
592 outclr();
593
594 /*
595 * line number of .c source, 'u' for used, Id of current
596 * source (.c or .h), and line in current source
597 */
598 outint(csrc_pos.p_line);
599 outchar('u');
600 outint(getfnid(curr_pos.p_file));
601 outchar('.');
602 outint(curr_pos.p_line);
603
604 /* necessary to delimit both numbers */
605 outchar('x');
606
607 /* Den Namen des Symbols ausgeben */
608 outname(sym->s_name);
609 }
610