Home | History | Annotate | Line # | Download | only in lint2
emit2.c revision 1.3
      1 /*	$NetBSD: emit2.c,v 1.3 1996/12/22 11:31:08 cgd 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 #ifndef lint
     36 static char rcsid[] = "$NetBSD: emit2.c,v 1.3 1996/12/22 11:31:08 cgd Exp $";
     37 #endif
     38 
     39 #include <err.h>
     40 
     41 #include "lint2.h"
     42 
     43 static	void	outtype __P((type_t *));
     44 static	void	outdef __P((hte_t *, sym_t *));
     45 static	void	dumpname __P((hte_t *));
     46 static	void	outfiles __P(());
     47 
     48 /*
     49  * Write type into the output buffer.
     50  */
     51 static void
     52 outtype(tp)
     53 	type_t	*tp;
     54 {
     55 	int	t, s, na;
     56 	tspec_t	ts;
     57 	type_t	**ap;
     58 
     59 	while (tp != NULL) {
     60 		if ((ts = tp->t_tspec) == INT && tp->t_isenum)
     61 			ts = ENUM;
     62 		switch (ts) {
     63 		case CHAR:	t = 'C';	s = '\0';	break;
     64 		case SCHAR:	t = 'C';	s = 's';	break;
     65 		case UCHAR:	t = 'C';	s = 'u';	break;
     66 		case SHORT:	t = 'S';	s = '\0';	break;
     67 		case USHORT:	t = 'S';	s = 'u';	break;
     68 		case INT:	t = 'I';	s = '\0';	break;
     69 		case UINT:	t = 'I';	s = 'u';	break;
     70 		case LONG:	t = 'L';	s = '\0';	break;
     71 		case ULONG:	t = 'L';	s = 'u';	break;
     72 		case QUAD:	t = 'Q';	s = '\0';	break;
     73 		case UQUAD:	t = 'Q';	s = 'u';	break;
     74 		case FLOAT:	t = 'D';	s = 's';	break;
     75 		case DOUBLE:	t = 'D';	s = '\0';	break;
     76 		case LDOUBLE:	t = 'D';	s = 'l';	break;
     77 		case VOID:	t = 'V';	s = '\0';	break;
     78 		case PTR:	t = 'P';	s = '\0';	break;
     79 		case ARRAY:	t = 'A';	s = '\0';	break;
     80 		case ENUM:	t = 'T';	s = 'e';	break;
     81 		case STRUCT:	t = 'T';	s = 's';	break;
     82 		case UNION:	t = 'T';	s = 'u';	break;
     83 		case FUNC:
     84 			if (tp->t_args != NULL && !tp->t_proto) {
     85 				t = 'f';
     86 			} else {
     87 				t = 'F';
     88 			}
     89 			s = '\0';
     90 			break;
     91 		default:
     92 			errx(1, "internal error: outtype() 1");
     93 		}
     94 		if (tp->t_const)
     95 			outchar('c');
     96 		if (tp->t_volatile)
     97 			outchar('v');
     98 		if (s != '\0')
     99 			outchar(s);
    100 		outchar(t);
    101 		if (ts == ARRAY) {
    102 			outint(tp->t_dim);
    103 		} else if (ts == ENUM || ts == STRUCT || ts == UNION) {
    104 			if (tp->t_istag) {
    105 				outint(1);
    106 				outname(tp->t_tag->h_name);
    107 			} else if (tp->t_istynam) {
    108 				outint(2);
    109 				outname(tp->t_tynam->h_name);
    110 			} else if (tp->t_isuniqpos) {
    111 				outint(3);
    112 				outint(tp->t_uniqpos.p_line);
    113 				outchar('.');
    114 				outint(tp->t_uniqpos.p_file);
    115 				outchar('.');
    116 				outint(tp->t_uniqpos.p_uniq);
    117 			} else
    118 				errx(1, "internal error: outtype() 2");
    119 		} else if (ts == FUNC && tp->t_args != NULL) {
    120 			na = 0;
    121 			for (ap = tp->t_args; *ap != NULL; ap++)
    122 				na++;
    123 			if (tp->t_vararg)
    124 				na++;
    125 			outint(na);
    126 			for (ap = tp->t_args; *ap != NULL; ap++)
    127 				outtype(*ap);
    128 			if (tp->t_vararg)
    129 				outchar('E');
    130 		}
    131 		tp = tp->t_subt;
    132 	}
    133 }
    134 
    135 /*
    136  * Write a definition.
    137  */
    138 static void
    139 outdef(hte, sym)
    140 	hte_t	*hte;
    141 	sym_t	*sym;
    142 {
    143 	/* reset output buffer */
    144 	outclr();
    145 
    146 	/* line number in C source file */
    147 	outint(0);
    148 
    149 	/* this is a definition */
    150 	outchar('d');
    151 
    152 	/* index of file where symbol was defined and line number of def. */
    153 	outint(0);
    154 	outchar('.');
    155 	outint(0);
    156 
    157 	/* flags */
    158 	if (sym->s_va) {
    159 		outchar('v');		/* varargs */
    160 		outint(sym->s_nva);
    161 	}
    162 	if (sym->s_scfl) {
    163 		outchar('S');		/* scanflike */
    164 		outint(sym->s_nscfl);
    165 	}
    166 	if (sym->s_prfl) {
    167 		outchar('P');		/* printflike */
    168 		outint(sym->s_nprfl);
    169 	}
    170 	/* definition or tentative definition */
    171 	outchar(sym->s_def == DEF ? 'd' : 't');
    172 	if (TP(sym->s_type)->t_tspec == FUNC) {
    173 		if (sym->s_rval)
    174 			outchar('r');	/* fkt. has return value */
    175 		if (sym->s_osdef)
    176 			outchar('o');	/* old style definition */
    177 	}
    178 	outchar('u');			/* used (no warning if not used) */
    179 
    180 	/* name */
    181 	outname(hte->h_name);
    182 
    183 	/* type */
    184 	outtype(TP(sym->s_type));
    185 }
    186 
    187 /*
    188  * Write the first definition of a name into the lint library.
    189  */
    190 static void
    191 dumpname(hte)
    192 	hte_t	*hte;
    193 {
    194 	sym_t	*sym, *def;
    195 
    196 	/* static and undefined symbols are not written */
    197 	if (hte->h_static || !hte->h_def)
    198 		return;
    199 
    200 	/*
    201 	 * If there is a definition, write it. Otherwise write a tentative
    202 	 * definition. This is neccessary because more than one tentative
    203 	 * definition is allowed (except with sflag).
    204 	 */
    205 	def = NULL;
    206 	for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
    207 		if (sym->s_def == DEF) {
    208 			def = sym;
    209 			break;
    210 		}
    211 		if (sym->s_def == TDEF && def == NULL)
    212 			def = sym;
    213 	}
    214 	if (def == NULL)
    215 		errx(1, "internal error: dumpname() %s", hte->h_name);
    216 
    217 	outdef(hte, def);
    218 }
    219 
    220 /*
    221  * Write a new lint library.
    222  */
    223 void
    224 outlib(name)
    225 	const	char *name;
    226 {
    227 	/* Open of output file and initialisation of the output buffer */
    228 	outopen(name);
    229 
    230 	/* write name of lint library */
    231 	outsrc(name);
    232 
    233 	/* name of lint lib has index 0 */
    234 	outclr();
    235 	outint(0);
    236 	outchar('s');
    237 	outstrg(name);
    238 
    239 	/*
    240 	 * print the names of all files references by unnamed
    241 	 * struct/union/enum declarations.
    242 	 */
    243 	outfiles();
    244 
    245 	/* write all definitions with external linkage */
    246 	forall(dumpname);
    247 
    248 	/* close the output */
    249 	outclose();
    250 }
    251 
    252 /*
    253  * Write out the name of a file referenced by a type.
    254  */
    255 struct outflist {
    256 	short ofl_num;
    257 	struct outflist *ofl_next;
    258 };
    259 static struct outflist *outflist;
    260 
    261 int
    262 addoutfile(num)
    263 	short num;
    264 {
    265 	struct outflist *ofl, **pofl;
    266 	int i;
    267 
    268 	ofl = outflist;
    269 	pofl = &outflist;
    270 	i = 1;				/* library is 0 */
    271 
    272 	while (ofl != NULL) {
    273 		if (ofl->ofl_num == num)
    274 			break;
    275 
    276 		pofl = &ofl->ofl_next;
    277 		ofl = ofl->ofl_next;
    278 		i++;
    279 	}
    280 
    281 	if (ofl == NULL) {
    282 		ofl = *pofl = xmalloc(sizeof (struct outflist));
    283 		ofl->ofl_num = num;
    284 		ofl->ofl_next = NULL;
    285 	}
    286 	return (i);
    287 }
    288 
    289 void
    290 outfiles()
    291 {
    292 	struct outflist *ofl;
    293 	int i;
    294 
    295 	for (ofl = outflist, i = 1; ofl != NULL; ofl = ofl->ofl_next, i++) {
    296 		/* reset output buffer */
    297 		outclr();
    298 
    299 		outint(i);
    300 		outchar('s');
    301 		outstrg(fnames[ofl->ofl_num]);
    302 	}
    303 }
    304