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.4 2003/11/03 19:31:34 eirikn 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)
92 if ((ts = tp->t_tspec) == INT && tp->t_isenum)
95 case CHAR: t = 'C'; s = '\0'; break;
96 case SCHAR: t = 'C'; s = 's'; break;
97 case UCHAR: t = 'C'; s = 'u'; break;
98 case SHORT: t = 'S'; s = '\0'; break;
99 case USHORT: t = 'S'; s = 'u'; break;
100 case INT: t = 'I'; s = '\0'; break;
101 case UINT: t = 'I'; s = 'u'; break;
102 case LONG: t = 'L'; s = '\0'; break;
103 case ULONG: t = 'L'; s = 'u'; break;
104 case QUAD: t = 'Q'; s = '\0'; break;
105 case UQUAD: t = 'Q'; s = 'u'; break;
106 case FLOAT: t = 'D'; s = 's'; break;
107 case DOUBLE: t = 'D'; s = '\0'; break;
108 case LDOUBLE: t = 'D'; s = 'l'; break;
109 case VOID: t = 'V'; s = '\0'; break;
110 case PTR: t = 'P'; s = '\0'; break;
111 case ARRAY: t = 'A'; s = '\0'; break;
112 case FUNC: t = 'F'; s = '\0'; break;
113 case ENUM: t = 'T'; s = 'e'; break;
114 case STRUCT: t = 'T'; s = 's'; break;
115 case UNION: t = 'T'; s = 'u'; break;
117 lerror("outtyp() 1");
128 } else if (ts == ENUM) {
129 outtt(tp->t_enum->etag, tp->t_enum->etdef);
130 } else if (ts == STRUCT || ts == UNION) {
131 outtt(tp->t_str->stag, tp->t_str->stdef);
132 } else if (ts == FUNC && tp->t_proto) {
134 for (arg = tp->t_args; arg != NULL; arg = arg->s_nxt)
139 for (arg = tp->t_args; arg != NULL; arg = arg->s_nxt)
140 outtype(arg->s_type);
150 * used for debugging output
152 * it uses its own output buffer for conversion
161 if (tob.o_buf == NULL) {
163 tob.o_buf = tob.o_nxt = xmalloc(tob.o_len);
164 tob.o_end = tob.o_buf + tob.o_len;
179 * write the name of a tag or typename
181 * if the tag is named, the name of the
182 * tag is written, otherwise, if a typename exists which
183 * refers to this tag, this typename is written
189 if (tag->s_name != unnamed) {
191 outname(tag->s_name);
192 } else if (tdef != NULL) {
194 outname(tdef->s_name);
201 * write information about an global declared/defined symbol
202 * with storage class extern
204 * informations about function definitions are written in outfdef(),
214 * Static function declarations must also be written to the output
215 * file. Compatibility of function declarations (for both static
216 * and extern functions) must be checked in lint2. Lint1 can't do
217 * this, especially not, if functions are declared at block level
218 * before their first declaration at level 0.
220 if (sc != EXTERN && !(sc == STATIC && sym->s_type->t_tspec == FUNC))
227 * line number of .c source, 'd' for declaration, Id of current
228 * source (.c or .h), and line in current source.
230 outint(csrc_pos.p_line);
232 outint(getfnid(sym->s_dpos.p_file));
234 outint(sym->s_dpos.p_line);
244 /* tentative defined */
252 lerror("outsym() 2");
254 if (llibflg && def != DECL) {
256 * mark it as used so we get no warnings from lint2 about
257 * unused symbols in libraries.
265 /* name of the symbol */
266 outname(sym->s_name);
268 /* type of the symbol */
269 outtype(sym->s_type);
273 * write information about function definition
275 * this is also done for static functions so we are able to check if
276 * they are called with proper argument types
279 outfdef(fsym, posp, rval, osdef, args)
287 /* reset the buffer */
291 * line number of .c source, 'd' for declaration, Id of current
292 * source (.c or .h), and line in current source
294 * we are already at the end of the function. If we are in the
295 * .c source, posp->p_line is correct, otherwise csrc_pos.p_line
296 * (for functions defined in header files).
298 if (posp->p_file == csrc_pos.p_file) {
299 outint(posp->p_line);
301 outint(csrc_pos.p_line);
304 outint(getfnid(posp->p_file));
306 outint(posp->p_line);
310 /* both SCANFLIKE and PRINTFLIKE imply VARARGS */
311 if (prflstrg != -1) {
313 } else if (scflstrg != -1) {
321 if (scflstrg != -1) {
325 if (prflstrg != -1) {
329 nvararg = prflstrg = scflstrg = -1;
334 /* has return value */
339 * mark it as used so lint2 does not complain about
340 * unused symbols in libraries
345 /* old style function definition */
348 if (fsym->s_scl == STATIC)
351 /* name of function */
352 outname(fsym->s_name);
354 /* argument types and return value */
357 for (arg = args; arg != NULL; arg = arg->s_nxt)
361 for (arg = args; arg != NULL; arg = arg->s_nxt)
362 outtype(arg->s_type);
363 outtype(fsym->s_type->t_subt);
365 outtype(fsym->s_type);
370 * write out all information necessary for lint2 to check function
373 * rvused is set if the return value is used (asigned to a variable)
374 * rvdisc is set if the return value is not used and not ignored
378 outcall(tn, rvused, rvdisc)
391 * line number of .c source, 'c' for function call, Id of current
392 * source (.c or .h), and line in current source
394 outint(csrc_pos.p_line);
396 outint(getfnid(curr_pos.p_file));
398 outint(curr_pos.p_line);
401 * flags; 'u' and 'i' must be last to make sure a letter
402 * is between the numeric argument of a flag and the name of
407 for (arg = args; arg != NULL; arg = arg->tn_right)
409 /* informations about arguments */
410 for (n = 1; n <= narg; n++) {
411 /* the last argument is the top one in the tree */
412 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right) ;
414 if (arg->tn_op == CON) {
415 if (isityp(t = arg->tn_type->t_tspec)) {
417 * XXX it would probably be better to
418 * explizitly test the sign
420 if ((q = arg->tn_val->v_quad) == 0) {
423 } else if (msb(q, t, 0) == 0) {
424 /* positive if casted to signed */
427 /* negative if casted to signed */
432 } else if (arg->tn_op == AMPER &&
433 arg->tn_left->tn_op == STRING &&
434 arg->tn_left->tn_strg->st_tspec == CHAR) {
435 /* constant string, write all format specifiers */
438 outfstrg(arg->tn_left->tn_strg);
442 /* return value discarded/used/ignored */
443 outchar(rvdisc ? 'd' : (rvused ? 'u' : 'i'));
445 /* name of the called function */
446 outname(tn->tn_left->tn_left->tn_sym->s_name);
448 /* types of arguments */
451 for (n = 1; n <= narg; n++) {
452 /* the last argument is the top one in the tree */
453 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right) ;
454 outtype(arg->tn_left->tn_type);
456 /* expected type of return value */
457 outtype(tn->tn_type);
461 * extracts potential format specifiers for printf() and scanf() and
462 * writes them, enclosed in "" and qouted if necessary, to the output buffer
471 if (strg->st_tspec != CHAR)
472 lerror("outfstrg() 1");
490 /* flags for printf and scanf and *-fieldwidth for printf */
491 while (c != '\0' && (c == '-' || c == '+' || c == ' ' ||
492 c == '#' || c == '0' || c == '*')) {
497 /* numeric field width */
498 while (c != '\0' && isdigit(c)) {
503 /* precision for printf */
506 if ((c = *cp++) == '*') {
510 while (c != '\0' && isdigit(c)) {
517 /* h, l, L and q flags fpr printf and scanf */
518 if (c == 'h' || c == 'l' || c == 'L' || c == 'q') {
524 * The last character. It is always written so we can detect
525 * invalid format specifiers.
532 * handle [ for scanf. [-] means that a minus sign
533 * was found at an undefined position.
541 while (c != '\0' && c != ']') {
543 if (!first && *cp != ']')
562 * writes a record if sym was used
572 * line number of .c source, 'u' for used, Id of current
573 * source (.c or .h), and line in current source
575 outint(csrc_pos.p_line);
577 outint(getfnid(curr_pos.p_file));
579 outint(curr_pos.p_line);
581 /* necessary to delimit both numbers */
584 /* Den Namen des Symbols ausgeben */
585 outname(sym->s_name);