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 $
40 static void outtt __P((sym_t *, sym_t *));
41 static void outfstrg __P((strg_t *));
44 * Write type into the output buffer.
45 * The type is written as a sequence of substrings, each of which describes a
47 * a node is coded as follows:
58 * unsigned long long u Q
67 * (n arguments) F n arg1 arg2 ... argn
68 * (n arguments, ...) F n arg1 arg2 ... argn-1 E
69 * (a, b, c, ...) f n arg1 arg2 ...
70 * enum tag e T tag_or_typename
71 * struct tag s T tag_or_typename
72 * union tag u T tag_or_typename
74 * tag_or_typename 0 no tag or type name
76 * 2 n typename only type name
78 * spaces are only for better readability
79 * additionaly it is possible to prepend the characters 'c' (for const)
80 * 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
160 if (tob.o_buf == NULL) {
162 tob.o_buf = tob.o_nxt = xmalloc(tob.o_len);
163 tob.o_end = tob.o_buf + tob.o_len;
178 * write the name of a tag or typename
180 * if the tag is named, the name of the
181 * tag is written, otherwise, if a typename exists which
182 * refers to this tag, this typename is written
188 if (tag->s_name != unnamed) {
190 outname(tag->s_name);
191 } else if (tdef != NULL) {
193 outname(tdef->s_name);
200 * write information about an global declared/defined symbol
201 * with storage class extern
203 * informations about function definitions are written in outfdef(),
213 * Static function declarations must also be written to the output
214 * file. Compatibility of function declarations (for both static
215 * and extern functions) must be checked in lint2. Lint1 can't do
216 * this, especially not, if functions are declared at block level
217 * before their first declaration at level 0.
219 if (sc != EXTERN && !(sc == STATIC && sym->s_type->t_tspec == FUNC))
226 * line number of .c source, 'd' for declaration, Id of current
227 * source (.c or .h), and line in current source.
229 outint(csrc_pos.p_line);
231 outint(getfnid(sym->s_dpos.p_file));
233 outint(sym->s_dpos.p_line);
243 /* tentative defined */
251 lerror("outsym() 2");
253 if (llibflg && def != DECL) {
255 * mark it as used so we get no warnings from lint2 about
256 * unused symbols in libraries.
264 /* name of the symbol */
265 outname(sym->s_name);
267 /* type of the symbol */
268 outtype(sym->s_type);
272 * write information about function definition
274 * this is also done for static functions so we are able to check if
275 * they are called with proper argument types
278 outfdef(fsym, posp, rval, osdef, args)
286 /* reset the buffer */
290 * line number of .c source, 'd' for declaration, Id of current
291 * source (.c or .h), and line in current source
293 * we are already at the end of the function. If we are in the
294 * .c source, posp->p_line is correct, otherwise csrc_pos.p_line
295 * (for functions defined in header files).
297 if (posp->p_file == csrc_pos.p_file) {
298 outint(posp->p_line);
300 outint(csrc_pos.p_line);
303 outint(getfnid(posp->p_file));
305 outint(posp->p_line);
309 /* both SCANFLIKE and PRINTFLIKE imply VARARGS */
310 if (prflstrg != -1) {
312 } else if (scflstrg != -1) {
320 if (scflstrg != -1) {
324 if (prflstrg != -1) {
328 nvararg = prflstrg = scflstrg = -1;
333 /* has return value */
338 * mark it as used so lint2 does not complain about
339 * unused symbols in libraries
344 /* old style function definition */
347 if (fsym->s_scl == STATIC)
350 /* name of function */
351 outname(fsym->s_name);
353 /* argument types and return value */
356 for (arg = args; arg != NULL; arg = arg->s_nxt)
360 for (arg = args; arg != NULL; arg = arg->s_nxt)
361 outtype(arg->s_type);
362 outtype(fsym->s_type->t_subt);
364 outtype(fsym->s_type);
369 * write out all information necessary for lint2 to check function
372 * rvused is set if the return value is used (asigned to a variable)
373 * rvdisc is set if the return value is not used and not ignored
377 outcall(tn, rvused, rvdisc)
390 * line number of .c source, 'c' for function call, Id of current
391 * source (.c or .h), and line in current source
393 outint(csrc_pos.p_line);
395 outint(getfnid(curr_pos.p_file));
397 outint(curr_pos.p_line);
400 * flags; 'u' and 'i' must be last to make sure a letter
401 * is between the numeric argument of a flag and the name of
406 for (arg = args; arg != NULL; arg = arg->tn_right)
408 /* informations about arguments */
409 for (n = 1; n <= narg; n++) {
410 /* the last argument is the top one in the tree */
411 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right) ;
413 if (arg->tn_op == CON) {
414 if (isityp(t = arg->tn_type->t_tspec)) {
416 * XXX it would probably be better to
417 * explizitly test the sign
419 if ((q = arg->tn_val->v_quad) == 0) {
422 } else if (msb(q, t, 0) == 0) {
423 /* positive if casted to signed */
426 /* negative if casted to signed */
431 } else if (arg->tn_op == AMPER &&
432 arg->tn_left->tn_op == STRING &&
433 arg->tn_left->tn_strg->st_tspec == CHAR) {
434 /* constant string, write all format specifiers */
437 outfstrg(arg->tn_left->tn_strg);
441 /* return value discarded/used/ignored */
442 outchar(rvdisc ? 'd' : (rvused ? 'u' : 'i'));
444 /* name of the called function */
445 outname(tn->tn_left->tn_left->tn_sym->s_name);
447 /* types of arguments */
450 for (n = 1; n <= narg; n++) {
451 /* the last argument is the top one in the tree */
452 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right) ;
453 outtype(arg->tn_left->tn_type);
455 /* expected type of return value */
456 outtype(tn->tn_type);
460 * extracts potential format specifiers for printf() and scanf() and
461 * writes them, enclosed in "" and qouted if necessary, to the output buffer
470 if (strg->st_tspec != CHAR)
471 lerror("outfstrg() 1");
489 /* flags for printf and scanf and *-fieldwidth for printf */
490 while (c != '\0' && (c == '-' || c == '+' || c == ' ' ||
491 c == '#' || c == '0' || c == '*')) {
496 /* numeric field width */
497 while (c != '\0' && isdigit(c)) {
502 /* precision for printf */
505 if ((c = *cp++) == '*') {
509 while (c != '\0' && isdigit(c)) {
516 /* h, l, L and q flags fpr printf and scanf */
517 if (c == 'h' || c == 'l' || c == 'L' || c == 'q') {
523 * The last character. It is always written so we can detect
524 * invalid format specifiers.
531 * handle [ for scanf. [-] means that a minus sign
532 * was found at an undefined position.
540 while (c != '\0' && c != ']') {
542 if (!first && *cp != ']')
561 * writes a record if sym was used
571 * line number of .c source, 'u' for used, Id of current
572 * source (.c or .h), and line in current source
574 outint(csrc_pos.p_line);
576 outint(getfnid(curr_pos.p_file));
578 outint(curr_pos.p_line);
580 /* necessary to delimit both numbers */
583 /* Den Namen des Symbols ausgeben */
584 outname(sym->s_name);