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.
35 static char rcsid[] = "$NetBSD: emit1.c,v 1.4 1995/10/02 17:21:28 jpo Exp $";
42 static void outtt __P((sym_t *, sym_t *));
43 static void outfstrg __P((strg_t *));
46 * Write type into the output buffer.
47 * The type is written as a sequence of substrings, each of which describes a
49 * a node is coded as follows:
60 * unsigned long long u Q
69 * (n arguments) F n arg1 arg2 ... argn
70 * (n arguments, ...) F n arg1 arg2 ... argn-1 E
71 * (a, b, c, ...) f n arg1 arg2 ...
72 * enum tag e T tag_or_typename
73 * struct tag s T tag_or_typename
74 * union tag u T tag_or_typename
76 * tag_or_typename 0 no tag or type name
78 * 2 n typename only type name
80 * spaces are only for better readability
81 * additionaly it is possible to prepend the characters 'c' (for const)
82 * and 'v' (for volatile)
93 if ((ts = tp->t_tspec) == INT && tp->t_isenum)
96 case CHAR: t = 'C'; s = '\0'; break;
97 case SCHAR: t = 'C'; s = 's'; break;
98 case UCHAR: t = 'C'; s = 'u'; break;
99 case SHORT: t = 'S'; s = '\0'; break;
100 case USHORT: t = 'S'; s = 'u'; break;
101 case INT: t = 'I'; s = '\0'; break;
102 case UINT: t = 'I'; s = 'u'; break;
103 case LONG: t = 'L'; s = '\0'; break;
104 case ULONG: t = 'L'; s = 'u'; break;
105 case QUAD: t = 'Q'; s = '\0'; break;
106 case UQUAD: t = 'Q'; s = 'u'; break;
107 case FLOAT: t = 'D'; s = 's'; break;
108 case DOUBLE: t = 'D'; s = '\0'; break;
109 case LDOUBLE: t = 'D'; s = 'l'; break;
110 case VOID: t = 'V'; s = '\0'; break;
111 case PTR: t = 'P'; s = '\0'; break;
112 case ARRAY: t = 'A'; s = '\0'; break;
113 case FUNC: t = 'F'; s = '\0'; break;
114 case ENUM: t = 'T'; s = 'e'; break;
115 case STRUCT: t = 'T'; s = 's'; break;
116 case UNION: t = 'T'; s = 'u'; break;
118 lerror("outtyp() 1");
129 } else if (ts == ENUM) {
130 outtt(tp->t_enum->etag, tp->t_enum->etdef);
131 } else if (ts == STRUCT || ts == UNION) {
132 outtt(tp->t_str->stag, tp->t_str->stdef);
133 } else if (ts == FUNC && tp->t_proto) {
135 for (arg = tp->t_args; arg != NULL; arg = arg->s_nxt)
140 for (arg = tp->t_args; arg != NULL; arg = arg->s_nxt)
141 outtype(arg->s_type);
151 * used for debugging output
153 * it uses its own output buffer for conversion
162 if (tob.o_buf == NULL) {
164 tob.o_buf = tob.o_nxt = xmalloc(tob.o_len);
165 tob.o_end = tob.o_buf + tob.o_len;
180 * write the name of a tag or typename
182 * if the tag is named, the name of the
183 * tag is written, otherwise, if a typename exists which
184 * refers to this tag, this typename is written
190 if (tag->s_name != unnamed) {
192 outname(tag->s_name);
193 } else if (tdef != NULL) {
195 outname(tdef->s_name);
202 * write information about an global declared/defined symbol
203 * with storage class extern
205 * informations about function definitions are written in outfdef(),
215 * Static function declarations must also be written to the output
216 * file. Compatibility of function declarations (for both static
217 * and extern functions) must be checked in lint2. Lint1 can't do
218 * this, especially not, if functions are declared at block level
219 * before their first declaration at level 0.
221 if (sc != EXTERN && !(sc == STATIC && sym->s_type->t_tspec == FUNC))
228 * line number of .c source, 'd' for declaration, Id of current
229 * source (.c or .h), and line in current source.
231 outint(csrc_pos.p_line);
233 outint(getfnid(sym->s_dpos.p_file));
235 outint(sym->s_dpos.p_line);
245 /* tentative defined */
253 lerror("outsym() 2");
255 if (llibflg && def != DECL) {
257 * mark it as used so we get no warnings from lint2 about
258 * unused symbols in libraries.
266 /* name of the symbol */
267 outname(sym->s_name);
269 /* type of the symbol */
270 outtype(sym->s_type);
274 * write information about function definition
276 * this is also done for static functions so we are able to check if
277 * they are called with proper argument types
280 outfdef(fsym, posp, rval, osdef, args)
288 /* reset the buffer */
292 * line number of .c source, 'd' for declaration, Id of current
293 * source (.c or .h), and line in current source
295 * we are already at the end of the function. If we are in the
296 * .c source, posp->p_line is correct, otherwise csrc_pos.p_line
297 * (for functions defined in header files).
299 if (posp->p_file == csrc_pos.p_file) {
300 outint(posp->p_line);
302 outint(csrc_pos.p_line);
305 outint(getfnid(posp->p_file));
307 outint(posp->p_line);
311 /* both SCANFLIKE and PRINTFLIKE imply VARARGS */
312 if (prflstrg != -1) {
314 } else if (scflstrg != -1) {
322 if (scflstrg != -1) {
326 if (prflstrg != -1) {
330 nvararg = prflstrg = scflstrg = -1;
335 /* has return value */
340 * mark it as used so lint2 does not complain about
341 * unused symbols in libraries
346 /* old style function definition */
349 if (fsym->s_scl == STATIC)
352 /* name of function */
353 outname(fsym->s_name);
355 /* argument types and return value */
358 for (arg = args; arg != NULL; arg = arg->s_nxt)
362 for (arg = args; arg != NULL; arg = arg->s_nxt)
363 outtype(arg->s_type);
364 outtype(fsym->s_type->t_subt);
366 outtype(fsym->s_type);
371 * write out all information necessary for lint2 to check function
374 * rvused is set if the return value is used (asigned to a variable)
375 * rvdisc is set if the return value is not used and not ignored
379 outcall(tn, rvused, rvdisc)
392 * line number of .c source, 'c' for function call, Id of current
393 * source (.c or .h), and line in current source
395 outint(csrc_pos.p_line);
397 outint(getfnid(curr_pos.p_file));
399 outint(curr_pos.p_line);
402 * flags; 'u' and 'i' must be last to make sure a letter
403 * is between the numeric argument of a flag and the name of
408 for (arg = args; arg != NULL; arg = arg->tn_right)
410 /* informations about arguments */
411 for (n = 1; n <= narg; n++) {
412 /* the last argument is the top one in the tree */
413 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right) ;
415 if (arg->tn_op == CON) {
416 if (isityp(t = arg->tn_type->t_tspec)) {
418 * XXX it would probably be better to
419 * explizitly test the sign
421 if ((q = arg->tn_val->v_quad) == 0) {
424 } else if (msb(q, t, 0) == 0) {
425 /* positive if casted to signed */
428 /* negative if casted to signed */
433 } else if (arg->tn_op == AMPER &&
434 arg->tn_left->tn_op == STRING &&
435 arg->tn_left->tn_strg->st_tspec == CHAR) {
436 /* constant string, write all format specifiers */
439 outfstrg(arg->tn_left->tn_strg);
443 /* return value discarded/used/ignored */
444 outchar(rvdisc ? 'd' : (rvused ? 'u' : 'i'));
446 /* name of the called function */
447 outname(tn->tn_left->tn_left->tn_sym->s_name);
449 /* types of arguments */
452 for (n = 1; n <= narg; n++) {
453 /* the last argument is the top one in the tree */
454 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right) ;
455 outtype(arg->tn_left->tn_type);
457 /* expected type of return value */
458 outtype(tn->tn_type);
462 * extracts potential format specifiers for printf() and scanf() and
463 * writes them, enclosed in "" and qouted if necessary, to the output buffer
472 if (strg->st_tspec != CHAR)
473 lerror("outfstrg() 1");
491 /* flags for printf and scanf and *-fieldwidth for printf */
492 while (c != '\0' && (c == '-' || c == '+' || c == ' ' ||
493 c == '#' || c == '0' || c == '*')) {
498 /* numeric field width */
499 while (c != '\0' && isdigit(c)) {
504 /* precision for printf */
507 if ((c = *cp++) == '*') {
511 while (c != '\0' && isdigit(c)) {
518 /* h, l, L and q flags fpr printf and scanf */
519 if (c == 'h' || c == 'l' || c == 'L' || c == 'q') {
525 * The last character. It is always written so we can detect
526 * invalid format specifiers.
533 * handle [ for scanf. [-] means that a minus sign
534 * was found at an undefined position.
542 while (c != '\0' && c != ']') {
544 if (!first && *cp != ']')
563 * writes a record if sym was used
573 * line number of .c source, 'u' for used, Id of current
574 * source (.c or .h), and line in current source
576 outint(csrc_pos.p_line);
578 outint(getfnid(curr_pos.p_file));
580 outint(curr_pos.p_line);
582 /* necessary to delimit both numbers */
585 /* Den Namen des Symbols ausgeben */
586 outname(sym->s_name);