Home | History | Annotate | Line # | Download | only in lint1
emit1.c revision 1.76
      1 /* $NetBSD: emit1.c,v 1.76 2023/08/12 21:32:16 rillig Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
      5  * Copyright (c) 1994, 1995 Jochen Pohl
      6  * All Rights Reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     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
     19  *	The NetBSD Project.
     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.
     22  *
     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.
     33  */
     34 
     35 #if HAVE_NBTOOL_CONFIG_H
     36 #include "nbtool_config.h"
     37 #endif
     38 
     39 #include <sys/cdefs.h>
     40 #if defined(__RCSID)
     41 __RCSID("$NetBSD: emit1.c,v 1.76 2023/08/12 21:32:16 rillig Exp $");
     42 #endif
     43 
     44 #include "lint1.h"
     45 
     46 static	void	outtt(sym_t *, sym_t *);
     47 static	void	outfstrg(strg_t *);
     48 
     49 /*
     50  * Write type into the output file, encoded as follows:
     51  *	const			c
     52  *	volatile		v
     53  *	_Bool			B
     54  *	_Complex float		s X
     55  *	_Complex double		X
     56  *	_Complex long double	l X
     57  *	char			C
     58  *	signed char		s C
     59  *	unsigned char		u C
     60  *	short			S
     61  *	unsigned short		u S
     62  *	int			I
     63  *	unsigned int		u I
     64  *	long			L
     65  *	unsigned long		u L
     66  *	long long		Q
     67  *	unsigned long long	u Q
     68  *	float			s D
     69  *	double			D
     70  *	long double		l D
     71  *	void			V
     72  *	*			P
     73  *	[n]			A n
     74  *	()			F
     75  *	(void)			F 0
     76  *	(n parameters)		F n arg1 arg2 ... argn
     77  *	(n parameters, ...)	F n arg1 arg2 ... argn E
     78  *	enum tag		e T tag_or_typename
     79  *	struct tag		s T tag_or_typename
     80  *	union tag		u T tag_or_typename
     81  *
     82  *	tag_or_typename		0 (obsolete)		no tag or type name
     83  *				1 n tag			tagged type
     84  *				2 n typename		only typedef name
     85  *				3 line.file.uniq	anonymous types
     86  */
     87 void
     88 outtype(const type_t *tp)
     89 {
     90 	/* Available letters: ------GH--K-MNO--R--U-W-YZ */
     91 #ifdef INT128_SIZE
     92 	static const char tt[NTSPEC] = "???BCCCSSIILLQQJJDDD?XXXVTTTPAF";
     93 	static const char ss[NTSPEC] = "???  su u u u u us l?s l sue   ";
     94 #else
     95 	static const char tt[NTSPEC] = "???BCCCSSIILLQQDDD?XXXVTTTPAF";
     96 	static const char ss[NTSPEC] = "???  su u u u us l?s l sue   ";
     97 #endif
     98 	int na;
     99 	tspec_t ts;
    100 
    101 	while (tp != NULL) {
    102 		if ((ts = tp->t_tspec) == INT && tp->t_is_enum)
    103 			ts = ENUM;
    104 		lint_assert(tt[ts] != '?' && ss[ts] != '?');
    105 		if (tp->t_const)
    106 			outchar('c');
    107 		if (tp->t_volatile)
    108 			outchar('v');
    109 		if (ss[ts] != ' ')
    110 			outchar(ss[ts]);
    111 		outchar(tt[ts]);
    112 
    113 		if (ts == ARRAY) {
    114 			outint(tp->t_dim);
    115 		} else if (ts == ENUM) {
    116 			outtt(tp->t_enum->en_tag, tp->t_enum->en_first_typedef);
    117 		} else if (is_struct_or_union(ts)) {
    118 			outtt(tp->t_sou->sou_tag, tp->t_sou->sou_first_typedef);
    119 		} else if (ts == FUNC && tp->t_proto) {
    120 			na = 0;
    121 			for (const sym_t *param = tp->t_params;
    122 			     param != NULL; param = param->s_next)
    123 				na++;
    124 			if (tp->t_vararg)
    125 				na++;
    126 			outint(na);
    127 			for (const sym_t *param = tp->t_params;
    128 			     param != NULL; param = param->s_next)
    129 				outtype(param->s_type);
    130 			if (tp->t_vararg)
    131 				outchar('E');
    132 		}
    133 		tp = tp->t_subt;
    134 	}
    135 }
    136 
    137 /*
    138  * write the name of a tag or typename
    139  *
    140  * if the tag is named, the name of the tag is written,
    141  * otherwise, if a typename exists which refers to this tag,
    142  * this typename is written
    143  */
    144 static void
    145 outtt(sym_t *tag, sym_t *tdef)
    146 {
    147 
    148 	/* 0 is no longer used. */
    149 
    150 	if (tag->s_name != unnamed) {
    151 		outint(1);
    152 		outname(tag->s_name);
    153 	} else if (tdef != NULL) {
    154 		outint(2);
    155 		outname(tdef->s_name);
    156 	} else {
    157 		outint(3);
    158 		outint(tag->s_def_pos.p_line);
    159 		outchar('.');
    160 		outint(get_filename_id(tag->s_def_pos.p_file));
    161 		outchar('.');
    162 		outint(tag->s_def_pos.p_uniq);
    163 	}
    164 }
    165 
    166 /*
    167  * write information about a globally declared/defined symbol
    168  * with storage class extern
    169  *
    170  * information about function definitions are written in outfdef(),
    171  * not here
    172  */
    173 void
    174 outsym(const sym_t *sym, scl_t sc, def_t def)
    175 {
    176 
    177 	/*
    178 	 * Static function declarations must also be written to the output
    179 	 * file. Compatibility of function declarations (for both static
    180 	 * and extern functions) must be checked in lint2. Lint1 can't do
    181 	 * this, especially not if functions are declared at block level
    182 	 * before their first declaration at level 0.
    183 	 */
    184 	if (sc != EXTERN && !(sc == STATIC && sym->s_type->t_tspec == FUNC))
    185 		return;
    186 	if (ch_isdigit(sym->s_name[0]))	/* 00000000_tmp */
    187 		return;
    188 
    189 	outint(csrc_pos.p_line);
    190 	outchar('d');		/* declaration */
    191 	outint(get_filename_id(sym->s_def_pos.p_file));
    192 	outchar('.');
    193 	outint(sym->s_def_pos.p_line);
    194 
    195 	/* flags */
    196 
    197 	if (def == DEF)
    198 		outchar('d');	/* defined */
    199 	else if (def == TDEF)
    200 		outchar('t');	/* tentative defined */
    201 	else {
    202 		lint_assert(def == DECL);
    203 		outchar('e');	/* declared */
    204 	}
    205 
    206 	if (llibflg && def != DECL) {
    207 		/*
    208 		 * mark it as used so lint2 does not complain about
    209 		 * unused symbols in libraries
    210 		 */
    211 		outchar('u');
    212 	}
    213 
    214 	if (sc == STATIC)
    215 		outchar('s');
    216 
    217 	outname(sym->s_name);
    218 
    219 	if (sym->s_rename != NULL) {
    220 		outchar('r');
    221 		outname(sym->s_rename);
    222 	}
    223 
    224 	outtype(sym->s_type);
    225 	outchar('\n');
    226 }
    227 
    228 /*
    229  * Write information about a function definition. This is also done for static
    230  * functions, to later check if they are called with proper argument types.
    231  */
    232 void
    233 outfdef(const sym_t *fsym, const pos_t *posp, bool rval, bool osdef,
    234 	const sym_t *args)
    235 {
    236 	int narg;
    237 	const sym_t *arg;
    238 
    239 	if (posp->p_file == csrc_pos.p_file) {
    240 		outint(posp->p_line);
    241 	} else {
    242 		outint(csrc_pos.p_line);
    243 	}
    244 	outchar('d');		/* declaration */
    245 	outint(get_filename_id(posp->p_file));
    246 	outchar('.');
    247 	outint(posp->p_line);
    248 
    249 	/* both SCANFLIKE and PRINTFLIKE imply VARARGS */
    250 	if (printflike_argnum != -1) {
    251 		nvararg = printflike_argnum;
    252 	} else if (scanflike_argnum != -1) {
    253 		nvararg = scanflike_argnum;
    254 	}
    255 
    256 	if (nvararg != -1) {
    257 		outchar('v');
    258 		outint(nvararg);
    259 	}
    260 	if (scanflike_argnum != -1) {
    261 		outchar('S');
    262 		outint(scanflike_argnum);
    263 	}
    264 	if (printflike_argnum != -1) {
    265 		outchar('P');
    266 		outint(printflike_argnum);
    267 	}
    268 	nvararg = printflike_argnum = scanflike_argnum = -1;
    269 
    270 	outchar('d');
    271 
    272 	if (rval)
    273 		outchar('r');	/* has return value */
    274 
    275 	if (llibflg)
    276 		/*
    277 		 * mark it as used so lint2 does not complain about
    278 		 * unused symbols in libraries
    279 		 */
    280 		outchar('u');
    281 
    282 	if (osdef)
    283 		outchar('o');	/* old-style function definition */
    284 
    285 	if (fsym->s_inline)
    286 		outchar('i');
    287 
    288 	if (fsym->s_scl == STATIC)
    289 		outchar('s');
    290 
    291 	outname(fsym->s_name);
    292 
    293 	if (fsym->s_rename != NULL) {
    294 		outchar('r');
    295 		outname(fsym->s_rename);
    296 	}
    297 
    298 	/* parameter types and return value */
    299 	if (osdef) {
    300 		narg = 0;
    301 		for (arg = args; arg != NULL; arg = arg->s_next)
    302 			narg++;
    303 		outchar('f');
    304 		outint(narg);
    305 		for (arg = args; arg != NULL; arg = arg->s_next)
    306 			outtype(arg->s_type);
    307 		outtype(fsym->s_type->t_subt);
    308 	} else {
    309 		outtype(fsym->s_type);
    310 	}
    311 	outchar('\n');
    312 }
    313 
    314 /*
    315  * write out all information necessary for lint2 to check function
    316  * calls
    317  *
    318  * retval_used is set if the return value is used (assigned to a variable)
    319  * retval_discarded is set if the return value is neither used nor ignored
    320  * (that is, cast to void)
    321  */
    322 void
    323 outcall(const tnode_t *tn, bool retval_used, bool retval_discarded)
    324 {
    325 	tnode_t *args, *arg;
    326 	int narg, n, i;
    327 	tspec_t t;
    328 
    329 	outint(csrc_pos.p_line);
    330 	outchar('c');		/* function call */
    331 	outint(get_filename_id(curr_pos.p_file));
    332 	outchar('.');
    333 	outint(curr_pos.p_line);
    334 
    335 	/*
    336 	 * flags; 'u' and 'i' must be last to make sure a letter
    337 	 * is between the numeric argument of a flag and the name of
    338 	 * the function
    339 	 */
    340 	narg = 0;
    341 	args = tn->tn_right;
    342 	for (arg = args; arg != NULL; arg = arg->tn_right)
    343 		narg++;
    344 	/* information about arguments */
    345 	for (n = 1; n <= narg; n++) {
    346 		/* the last argument is the top one in the tree */
    347 		for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
    348 			continue;
    349 		arg = arg->tn_left;
    350 		if (arg->tn_op == CON) {
    351 			if (is_integer(t = arg->tn_type->t_tspec)) {
    352 				/*
    353 				 * XXX it would probably be better to
    354 				 * explicitly test the sign
    355 				 */
    356 				int64_t si = arg->tn_val.u.integer;
    357 				if (si == 0) {
    358 					/* zero constant */
    359 					outchar('z');
    360 				} else if (!msb(si, t)) {
    361 					/* positive if cast to signed */
    362 					outchar('p');
    363 				} else {
    364 					/* negative if cast to signed */
    365 					outchar('n');
    366 				}
    367 				outint(n);
    368 			}
    369 		} else if (arg->tn_op == ADDR &&
    370 			   arg->tn_left->tn_op == STRING &&
    371 			   arg->tn_left->tn_string->st_char) {
    372 			/* constant string, write all format specifiers */
    373 			outchar('s');
    374 			outint(n);
    375 			outfstrg(arg->tn_left->tn_string);
    376 		}
    377 
    378 	}
    379 	outchar((char)(retval_discarded ? 'd' : retval_used ? 'u' : 'i'));
    380 
    381 	/* name of the called function */
    382 	outname(tn->tn_left->tn_left->tn_sym->s_name);
    383 
    384 	/* types of arguments */
    385 	outchar('f');
    386 	outint(narg);
    387 	for (n = 1; n <= narg; n++) {
    388 		/* the last argument is the top one in the tree */
    389 		for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
    390 			continue;
    391 		outtype(arg->tn_left->tn_type);
    392 	}
    393 	/* expected type of return value */
    394 	outtype(tn->tn_type);
    395 	outchar('\n');
    396 }
    397 
    398 /* write a character to the output file, quoted if necessary */
    399 static void
    400 outqchar(char c)
    401 {
    402 
    403 	if (ch_isprint(c) && c != '\\' && c != '"' && c != '\'') {
    404 		outchar(c);
    405 		return;
    406 	}
    407 
    408 	outchar('\\');
    409 	switch (c) {
    410 	case '\\':
    411 		outchar('\\');
    412 		break;
    413 	case '"':
    414 		outchar('"');
    415 		break;
    416 	case '\'':
    417 		outchar('\'');
    418 		break;
    419 	case '\b':
    420 		outchar('b');
    421 		break;
    422 	case '\t':
    423 		outchar('t');
    424 		break;
    425 	case '\n':
    426 		outchar('n');
    427 		break;
    428 	case '\f':
    429 		outchar('f');
    430 		break;
    431 	case '\r':
    432 		outchar('r');
    433 		break;
    434 	case '\v':
    435 		outchar('v');
    436 		break;
    437 	case '\a':
    438 		outchar('a');
    439 		break;
    440 	default:
    441 		outchar((char)((((unsigned char)c >> 6) & 07) + '0'));
    442 		outchar((char)((((unsigned char)c >> 3) & 07) + '0'));
    443 		outchar((char)((c & 07) + '0'));
    444 		break;
    445 	}
    446 }
    447 
    448 /*
    449  * extracts potential format specifiers for printf() and scanf() and
    450  * writes them, enclosed in "" and quoted if necessary, to the output file
    451  */
    452 static void
    453 outfstrg(strg_t *strg)
    454 {
    455 	char c, oc;
    456 	bool first;
    457 	const char *cp;
    458 
    459 	lint_assert(strg->st_char);
    460 	cp = strg->st_mem;
    461 
    462 	outchar('"');
    463 
    464 	c = *cp++;
    465 
    466 	while (c != '\0') {
    467 
    468 		if (c != '%') {
    469 			c = *cp++;
    470 			continue;
    471 		}
    472 
    473 		outchar('%');
    474 		c = *cp++;
    475 
    476 		/* flags for printf and scanf and *-fieldwidth for printf */
    477 		while (c == '-' || c == '+' || c == ' ' ||
    478 		       c == '#' || c == '0' || c == '*') {
    479 			outchar(c);
    480 			c = *cp++;
    481 		}
    482 
    483 		/* numeric field width */
    484 		while (ch_isdigit(c)) {
    485 			outchar(c);
    486 			c = *cp++;
    487 		}
    488 
    489 		/* precision for printf */
    490 		if (c == '.') {
    491 			outchar(c);
    492 			c = *cp++;
    493 			if (c == '*') {
    494 				outchar(c);
    495 				c = *cp++;
    496 			} else {
    497 				while (ch_isdigit(c)) {
    498 					outchar(c);
    499 					c = *cp++;
    500 				}
    501 			}
    502 		}
    503 
    504 		/* h, l, L and q flags for printf and scanf */
    505 		if (c == 'h' || c == 'l' || c == 'L' || c == 'q') {
    506 			outchar(c);
    507 			c = *cp++;
    508 		}
    509 
    510 		/*
    511 		 * The last character. It is always written, so we can detect
    512 		 * invalid format specifiers.
    513 		 */
    514 		if (c != '\0') {
    515 			outqchar(c);
    516 			oc = c;
    517 			c = *cp++;
    518 			/*
    519 			 * handle [ for scanf. [-] means that a minus sign
    520 			 * was found at an undefined position.
    521 			 */
    522 			if (oc == '[') {
    523 				if (c == '^')
    524 					c = *cp++;
    525 				if (c == ']')
    526 					c = *cp++;
    527 				first = true;
    528 				while (c != '\0' && c != ']') {
    529 					if (c == '-') {
    530 						if (!first && *cp != ']')
    531 							outchar(c);
    532 					}
    533 					first = false;
    534 					c = *cp++;
    535 				}
    536 				if (c == ']') {
    537 					outchar(c);
    538 					c = *cp++;
    539 				}
    540 			}
    541 		}
    542 
    543 	}
    544 
    545 	outchar('"');
    546 }
    547 
    548 /* writes a record if sym was used */
    549 void
    550 outusg(const sym_t *sym)
    551 {
    552 	if (ch_isdigit(sym->s_name[0]))	/* 00000000_tmp, from mktempsym */
    553 		return;
    554 
    555 	outint(csrc_pos.p_line);
    556 	outchar('u');		/* used */
    557 	outint(get_filename_id(curr_pos.p_file));
    558 	outchar('.');
    559 	outint(curr_pos.p_line);
    560 	outchar('x');		/* separate the two numbers */
    561 	outname(sym->s_name);
    562 	outchar('\n');
    563 }
    564