1 /* $NetBSD: emit1.c,v 1.4 1995/10/02 17:21:28 jpo Exp $ */
4 * Copyright (c) 1994, 1995 Jochen Pohl
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Jochen Pohl for
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * $NetBSD: emit1.c,v 1.4 1995/10/02 17:21:28 jpo Exp $
34 * $DragonFly: src/usr.bin/xlint/lint1/emit1.c,v 1.5 2004/07/07 12:13:26 asmodai Exp $
41 static void outtt(sym_t *, sym_t *);
42 static void outfstrg(strg_t *);
45 * Write type into the output buffer.
46 * The type is written as a sequence of substrings, each of which describes a
48 * a node is coded as follows:
59 * unsigned long long u Q
68 * (n arguments) F n arg1 arg2 ... argn
69 * (n arguments, ...) F n arg1 arg2 ... argn-1 E
70 * (a, b, c, ...) f n arg1 arg2 ...
71 * enum tag e T tag_or_typename
72 * struct tag s T tag_or_typename
73 * union tag u T tag_or_typename
75 * tag_or_typename 0 no tag or type name
77 * 2 n typename only type name
79 * spaces are only for better readability
80 * additionaly it is possible to prepend the characters 'c' (for const)
81 * and 'v' (for volatile)
91 if ((ts = tp->t_tspec) == INT && tp->t_isenum)
94 case CHAR: t = 'C'; s = '\0'; break;
95 case SCHAR: t = 'C'; s = 's'; break;
96 case UCHAR: t = 'C'; s = 'u'; break;
97 case SHORT: t = 'S'; s = '\0'; break;
98 case USHORT: t = 'S'; s = 'u'; break;
99 case INT: t = 'I'; s = '\0'; break;
100 case UINT: t = 'I'; s = 'u'; break;
101 case LONG: t = 'L'; s = '\0'; break;
102 case ULONG: t = 'L'; s = 'u'; break;
103 case QUAD: t = 'Q'; s = '\0'; break;
104 case UQUAD: t = 'Q'; s = 'u'; break;
105 case FLOAT: t = 'D'; s = 's'; break;
106 case DOUBLE: t = 'D'; s = '\0'; break;
107 case LDOUBLE: t = 'D'; s = 'l'; break;
108 case VOID: t = 'V'; s = '\0'; break;
109 case PTR: t = 'P'; s = '\0'; break;
110 case ARRAY: t = 'A'; s = '\0'; break;
111 case FUNC: t = 'F'; s = '\0'; break;
112 case ENUM: t = 'T'; s = 'e'; break;
113 case STRUCT: t = 'T'; s = 's'; break;
114 case UNION: t = 'T'; s = 'u'; break;
116 lerror("outtyp() 1");
127 } else if (ts == ENUM) {
128 outtt(tp->t_enum->etag, tp->t_enum->etdef);
129 } else if (ts == STRUCT || ts == UNION) {
130 outtt(tp->t_str->stag, tp->t_str->stdef);
131 } else if (ts == FUNC && tp->t_proto) {
133 for (arg = tp->t_args; arg != NULL; arg = arg->s_nxt)
138 for (arg = tp->t_args; arg != NULL; arg = arg->s_nxt)
139 outtype(arg->s_type);
149 * used for debugging output
151 * it uses its own output buffer for conversion
159 if (tob.o_buf == NULL) {
161 tob.o_buf = tob.o_nxt = xmalloc(tob.o_len);
162 tob.o_end = tob.o_buf + tob.o_len;
177 * write the name of a tag or typename
179 * if the tag is named, the name of the
180 * tag is written, otherwise, if a typename exists which
181 * refers to this tag, this typename is written
184 outtt(sym_t *tag, sym_t *tdef)
186 if (tag->s_name != unnamed) {
188 outname(tag->s_name);
189 } else if (tdef != NULL) {
191 outname(tdef->s_name);
198 * write information about an global declared/defined symbol
199 * with storage class extern
201 * informations about function definitions are written in outfdef(),
205 outsym(sym_t *sym, scl_t sc, def_t def)
208 * Static function declarations must also be written to the output
209 * file. Compatibility of function declarations (for both static
210 * and extern functions) must be checked in lint2. Lint1 can't do
211 * this, especially not, if functions are declared at block level
212 * before their first declaration at level 0.
214 if (sc != EXTERN && !(sc == STATIC && sym->s_type->t_tspec == FUNC))
221 * line number of .c source, 'd' for declaration, Id of current
222 * source (.c or .h), and line in current source.
224 outint(csrc_pos.p_line);
226 outint(getfnid(sym->s_dpos.p_file));
228 outint(sym->s_dpos.p_line);
238 /* tentative defined */
246 lerror("outsym() 2");
248 if (llibflg && def != DECL) {
250 * mark it as used so we get no warnings from lint2 about
251 * unused symbols in libraries.
259 /* name of the symbol */
260 outname(sym->s_name);
262 /* type of the symbol */
263 outtype(sym->s_type);
267 * write information about function definition
269 * this is also done for static functions so we are able to check if
270 * they are called with proper argument types
273 outfdef(sym_t *fsym, pos_t *posp, int rval, int osdef, sym_t *args)
278 /* reset the buffer */
282 * line number of .c source, 'd' for declaration, Id of current
283 * source (.c or .h), and line in current source
285 * we are already at the end of the function. If we are in the
286 * .c source, posp->p_line is correct, otherwise csrc_pos.p_line
287 * (for functions defined in header files).
289 if (posp->p_file == csrc_pos.p_file) {
290 outint(posp->p_line);
292 outint(csrc_pos.p_line);
295 outint(getfnid(posp->p_file));
297 outint(posp->p_line);
301 /* both SCANFLIKE and PRINTFLIKE imply VARARGS */
302 if (prflstrg != -1) {
304 } else if (scflstrg != -1) {
312 if (scflstrg != -1) {
316 if (prflstrg != -1) {
320 nvararg = prflstrg = scflstrg = -1;
325 /* has return value */
330 * mark it as used so lint2 does not complain about
331 * unused symbols in libraries
336 /* old style function definition */
339 if (fsym->s_scl == STATIC)
342 /* name of function */
343 outname(fsym->s_name);
345 /* argument types and return value */
348 for (arg = args; arg != NULL; arg = arg->s_nxt)
352 for (arg = args; arg != NULL; arg = arg->s_nxt)
353 outtype(arg->s_type);
354 outtype(fsym->s_type->t_subt);
356 outtype(fsym->s_type);
361 * write out all information necessary for lint2 to check function
364 * rvused is set if the return value is used (asigned to a variable)
365 * rvdisc is set if the return value is not used and not ignored
369 outcall(tnode_t *tn, int rvused, int rvdisc)
380 * line number of .c source, 'c' for function call, Id of current
381 * source (.c or .h), and line in current source
383 outint(csrc_pos.p_line);
385 outint(getfnid(curr_pos.p_file));
387 outint(curr_pos.p_line);
390 * flags; 'u' and 'i' must be last to make sure a letter
391 * is between the numeric argument of a flag and the name of
396 for (arg = args; arg != NULL; arg = arg->tn_right)
398 /* informations about arguments */
399 for (n = 1; n <= narg; n++) {
400 /* the last argument is the top one in the tree */
401 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right) ;
403 if (arg->tn_op == CON) {
404 if (isityp(t = arg->tn_type->t_tspec)) {
406 * XXX it would probably be better to
407 * explizitly test the sign
409 if ((q = arg->tn_val->v_quad) == 0) {
412 } else if (msb(q, t, 0) == 0) {
413 /* positive if casted to signed */
416 /* negative if casted to signed */
421 } else if (arg->tn_op == AMPER &&
422 arg->tn_left->tn_op == STRING &&
423 arg->tn_left->tn_strg->st_tspec == CHAR) {
424 /* constant string, write all format specifiers */
427 outfstrg(arg->tn_left->tn_strg);
431 /* return value discarded/used/ignored */
432 outchar(rvdisc ? 'd' : (rvused ? 'u' : 'i'));
434 /* name of the called function */
435 outname(tn->tn_left->tn_left->tn_sym->s_name);
437 /* types of arguments */
440 for (n = 1; n <= narg; n++) {
441 /* the last argument is the top one in the tree */
442 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right) ;
443 outtype(arg->tn_left->tn_type);
445 /* expected type of return value */
446 outtype(tn->tn_type);
450 * extracts potential format specifiers for printf() and scanf() and
451 * writes them, enclosed in "" and qouted if necessary, to the output buffer
454 outfstrg(strg_t *strg)
459 if (strg->st_tspec != CHAR)
460 lerror("outfstrg() 1");
478 /* flags for printf and scanf and *-fieldwidth for printf */
479 while (c != '\0' && (c == '-' || c == '+' || c == ' ' ||
480 c == '#' || c == '0' || c == '*')) {
485 /* numeric field width */
486 while (c != '\0' && isdigit(c)) {
491 /* precision for printf */
494 if ((c = *cp++) == '*') {
498 while (c != '\0' && isdigit(c)) {
505 /* h, l, L and q flags fpr printf and scanf */
506 if (c == 'h' || c == 'l' || c == 'L' || c == 'q') {
512 * The last character. It is always written so we can detect
513 * invalid format specifiers.
520 * handle [ for scanf. [-] means that a minus sign
521 * was found at an undefined position.
529 while (c != '\0' && c != ']') {
531 if (!first && *cp != ']')
550 * writes a record if sym was used
559 * line number of .c source, 'u' for used, Id of current
560 * source (.c or .h), and line in current source
562 outint(csrc_pos.p_line);
564 outint(getfnid(curr_pos.p_file));
566 outint(curr_pos.p_line);
568 /* necessary to delimit both numbers */
571 /* Den Namen des Symbols ausgeben */
572 outname(sym->s_name);