1 /* $NetBSD: emit1.c,v 1.14 2004/06/20 22:20:16 jmc Exp $ */
4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
5 * Copyright (c) 1994, 1995 Jochen Pohl
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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.
35 #include <sys/cdefs.h>
36 #if defined(__RCSID) && !defined(lint)
37 __RCSID("$NetBSD: emit1.c,v 1.14 2004/06/20 22:20:16 jmc Exp $");
39 __FBSDID("$FreeBSD$");
45 static void outtt(sym_t *, sym_t *);
46 static void outfstrg(strg_t *);
49 * Write type into the output buffer.
50 * The type is written as a sequence of substrings, each of which describes a
52 * a node is coded as follows:
63 * unsigned long long u Q
72 * (n arguments) F n arg1 arg2 ... argn
73 * (n arguments, ...) F n arg1 arg2 ... argn-1 E
74 * (a, b, c, ...) f n arg1 arg2 ...
75 * enum tag e T tag_or_typename
76 * struct tag s T tag_or_typename
77 * union tag u T tag_or_typename
79 * tag_or_typename 0 no tag or type name
81 * 2 n typename only type name
83 * spaces are only for better readability
84 * additionally it is possible to prepend the characters 'c' (for const)
85 * and 'v' (for volatile)
95 if ((ts = tp->t_tspec) == INT && tp->t_isenum)
98 case CHAR: t = 'C'; s = '\0'; break;
99 case SCHAR: t = 'C'; s = 's'; break;
100 case UCHAR: t = 'C'; s = 'u'; break;
101 case SHORT: t = 'S'; s = '\0'; break;
102 case USHORT: t = 'S'; s = 'u'; break;
103 case INT: t = 'I'; s = '\0'; break;
104 case UINT: t = 'I'; s = 'u'; break;
105 case LONG: t = 'L'; s = '\0'; break;
106 case ULONG: t = 'L'; s = 'u'; break;
107 case QUAD: t = 'Q'; s = '\0'; break;
108 case UQUAD: t = 'Q'; s = 'u'; break;
109 case FLOAT: t = 'D'; s = 's'; break;
110 case DOUBLE: t = 'D'; s = '\0'; break;
111 case LDOUBLE: t = 'D'; s = 'l'; break;
112 case VOID: t = 'V'; s = '\0'; break;
113 case PTR: t = 'P'; s = '\0'; break;
114 case ARRAY: t = 'A'; s = '\0'; break;
115 case FUNC: t = 'F'; s = '\0'; break;
116 case ENUM: t = 'T'; s = 'e'; break;
117 case STRUCT: t = 'T'; s = 's'; break;
118 case UNION: t = 'T'; s = 'u'; break;
131 } else if (ts == ENUM) {
132 outtt(tp->t_enum->etag, tp->t_enum->etdef);
133 } else if (ts == STRUCT || ts == UNION) {
134 outtt(tp->t_str->stag, tp->t_str->stdef);
135 } else if (ts == FUNC && tp->t_proto) {
137 for (arg = tp->t_args; arg != NULL; arg = arg->s_nxt)
142 for (arg = tp->t_args; arg != NULL; arg = arg->s_nxt)
143 outtype(arg->s_type);
153 * used for debugging output
155 * it uses its own output buffer for conversion
163 if (tob.o_buf == NULL) {
165 tob.o_buf = tob.o_nxt = xmalloc(tob.o_len);
166 tob.o_end = tob.o_buf + tob.o_len;
181 * write the name of a tag or typename
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
188 outtt(sym_t *tag, sym_t *tdef)
192 * 0 is no longer used.
194 if (tag->s_name != unnamed) {
196 outname(tag->s_name);
197 } else if (tdef != NULL) {
199 outname(tdef->s_name);
202 outint(tag->s_dpos.p_line);
204 outint(getfnid(tag->s_dpos.p_file));
206 outint(tag->s_dpos.p_uniq);
211 * write information about a global declared/defined symbol
212 * with storage class extern
214 * informations about function definitions are written in outfdef(),
218 outsym(sym_t *sym, scl_t sc, def_t def)
222 * Static function declarations must also be written to the output
223 * file. Compatibility of function declarations (for both static
224 * and extern functions) must be checked in lint2. Lint1 can't do
225 * this, especially not, if functions are declared at block level
226 * before their first declaration at level 0.
228 if (sc != EXTERN && !(sc == STATIC && sym->s_type->t_tspec == FUNC))
235 * line number of .c source, 'd' for declaration, Id of current
236 * source (.c or .h), and line in current source.
238 outint(csrc_pos.p_line);
240 outint(getfnid(sym->s_dpos.p_file));
242 outint(sym->s_dpos.p_line);
252 /* tentative defined */
262 if (llibflg && def != DECL) {
264 * mark it as used so we get no warnings from lint2 about
265 * unused symbols in libraries.
273 /* name of the symbol */
274 outname(sym->s_name);
276 /* renamed name of symbol, if necessary */
279 outname(sym->s_rename);
282 /* type of the symbol */
283 outtype(sym->s_type);
287 * write information about function definition
289 * this is also done for static functions so we are able to check if
290 * they are called with proper argument types
293 outfdef(sym_t *fsym, pos_t *posp, int rval, int osdef, sym_t *args)
298 /* reset the buffer */
302 * line number of .c source, 'd' for declaration, Id of current
303 * source (.c or .h), and line in current source
305 * we are already at the end of the function. If we are in the
306 * .c source, posp->p_line is correct, otherwise csrc_pos.p_line
307 * (for functions defined in header files).
309 if (posp->p_file == csrc_pos.p_file) {
310 outint(posp->p_line);
312 outint(csrc_pos.p_line);
315 outint(getfnid(posp->p_file));
317 outint(posp->p_line);
321 /* both SCANFLIKE and PRINTFLIKE imply VARARGS */
322 if (prflstrg != -1) {
324 } else if (scflstrg != -1) {
332 if (scflstrg != -1) {
336 if (prflstrg != -1) {
340 nvararg = prflstrg = scflstrg = -1;
345 /* has return value */
350 * mark it as used so lint2 does not complain about
351 * unused symbols in libraries
356 /* old style function definition */
359 if (fsym->s_scl == STATIC)
362 /* name of function */
363 outname(fsym->s_name);
365 /* renamed name of function, if necessary */
366 if (fsym->s_rename) {
368 outname(fsym->s_rename);
371 /* argument types and return value */
374 for (arg = args; arg != NULL; arg = arg->s_nxt)
378 for (arg = args; arg != NULL; arg = arg->s_nxt)
379 outtype(arg->s_type);
380 outtype(fsym->s_type->t_subt);
382 outtype(fsym->s_type);
387 * write out all information necessary for lint2 to check function
390 * rvused is set if the return value is used (asigned to a variable)
391 * rvdisc is set if the return value is not used and not ignored
395 outcall(tnode_t *tn, int rvused, int rvdisc)
406 * line number of .c source, 'c' for function call, Id of current
407 * source (.c or .h), and line in current source
409 outint(csrc_pos.p_line);
411 outint(getfnid(curr_pos.p_file));
413 outint(curr_pos.p_line);
416 * flags; 'u' and 'i' must be last to make sure a letter
417 * is between the numeric argument of a flag and the name of
422 for (arg = args; arg != NULL; arg = arg->tn_right)
424 /* informations about arguments */
425 for (n = 1; n <= narg; n++) {
426 /* the last argument is the top one in the tree */
427 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
430 if (arg->tn_op == CON) {
431 if (isityp(t = arg->tn_type->t_tspec)) {
433 * XXX it would probably be better to
434 * explizitly test the sign
436 if ((q = arg->tn_val->v_quad) == 0) {
439 } else if (msb(q, t, 0) == 0) {
440 /* positive if casted to signed */
443 /* negative if casted to signed */
448 } else if (arg->tn_op == AMPER &&
449 arg->tn_left->tn_op == STRING &&
450 arg->tn_left->tn_strg->st_tspec == CHAR) {
451 /* constant string, write all format specifiers */
454 outfstrg(arg->tn_left->tn_strg);
458 /* return value discarded/used/ignored */
459 outchar(rvdisc ? 'd' : (rvused ? 'u' : 'i'));
461 /* name of the called function */
462 outname(tn->tn_left->tn_left->tn_sym->s_name);
464 /* types of arguments */
467 for (n = 1; n <= narg; n++) {
468 /* the last argument is the top one in the tree */
469 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
471 outtype(arg->tn_left->tn_type);
473 /* expected type of return value */
474 outtype(tn->tn_type);
478 * extracts potential format specifiers for printf() and scanf() and
479 * writes them, enclosed in "" and qouted if necessary, to the output buffer
482 outfstrg(strg_t *strg)
487 if (strg->st_tspec != CHAR)
488 LERROR("outfstrg()");
506 /* flags for printf and scanf and *-fieldwidth for printf */
507 while (c != '\0' && (c == '-' || c == '+' || c == ' ' ||
508 c == '#' || c == '0' || c == '*')) {
513 /* numeric field width */
514 while (c != '\0' && isdigit(c)) {
519 /* precision for printf */
522 if ((c = *cp++) == '*') {
526 while (c != '\0' && isdigit(c)) {
533 /* h, l, L and q flags fpr printf and scanf */
534 if (c == 'h' || c == 'l' || c == 'L' || c == 'q') {
540 * The last character. It is always written so we can detect
541 * invalid format specifiers.
548 * handle [ for scanf. [-] means that a minus sign
549 * was found at an undefined position.
557 while (c != '\0' && c != ']') {
559 if (!first && *cp != ']')
578 * writes a record if sym was used
587 * line number of .c source, 'u' for used, Id of current
588 * source (.c or .h), and line in current source
590 outint(csrc_pos.p_line);
592 outint(getfnid(curr_pos.p_file));
594 outint(curr_pos.p_line);
596 /* necessary to delimit both numbers */
599 /* Den Namen des Symbols ausgeben */
600 outname(sym->s_name);