Home | History | Annotate | Line # | Download | only in lint2
emit2.c revision 1.3
      1  1.3  cgd /*	$NetBSD: emit2.c,v 1.3 1996/12/22 11:31:08 cgd Exp $	*/
      2  1.2  cgd 
      3  1.1  cgd /*
      4  1.3  cgd  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
      5  1.1  cgd  * Copyright (c) 1994, 1995 Jochen Pohl
      6  1.1  cgd  * All Rights Reserved.
      7  1.1  cgd  *
      8  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      9  1.1  cgd  * modification, are permitted provided that the following conditions
     10  1.1  cgd  * are met:
     11  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     12  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     13  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     16  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     17  1.1  cgd  *    must display the following acknowledgement:
     18  1.1  cgd  *      This product includes software developed by Jochen Pohl for
     19  1.1  cgd  *	The NetBSD Project.
     20  1.1  cgd  * 4. The name of the author may not be used to endorse or promote products
     21  1.1  cgd  *    derived from this software without specific prior written permission.
     22  1.1  cgd  *
     23  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  1.1  cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  1.1  cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  1.1  cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  1.1  cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  1.1  cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  1.1  cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  1.1  cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  1.1  cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  1.1  cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  1.1  cgd  */
     34  1.1  cgd 
     35  1.1  cgd #ifndef lint
     36  1.3  cgd static char rcsid[] = "$NetBSD: emit2.c,v 1.3 1996/12/22 11:31:08 cgd Exp $";
     37  1.1  cgd #endif
     38  1.1  cgd 
     39  1.1  cgd #include <err.h>
     40  1.1  cgd 
     41  1.1  cgd #include "lint2.h"
     42  1.1  cgd 
     43  1.1  cgd static	void	outtype __P((type_t *));
     44  1.1  cgd static	void	outdef __P((hte_t *, sym_t *));
     45  1.1  cgd static	void	dumpname __P((hte_t *));
     46  1.3  cgd static	void	outfiles __P(());
     47  1.1  cgd 
     48  1.1  cgd /*
     49  1.1  cgd  * Write type into the output buffer.
     50  1.1  cgd  */
     51  1.1  cgd static void
     52  1.1  cgd outtype(tp)
     53  1.1  cgd 	type_t	*tp;
     54  1.1  cgd {
     55  1.1  cgd 	int	t, s, na;
     56  1.1  cgd 	tspec_t	ts;
     57  1.1  cgd 	type_t	**ap;
     58  1.1  cgd 
     59  1.1  cgd 	while (tp != NULL) {
     60  1.1  cgd 		if ((ts = tp->t_tspec) == INT && tp->t_isenum)
     61  1.1  cgd 			ts = ENUM;
     62  1.1  cgd 		switch (ts) {
     63  1.1  cgd 		case CHAR:	t = 'C';	s = '\0';	break;
     64  1.1  cgd 		case SCHAR:	t = 'C';	s = 's';	break;
     65  1.1  cgd 		case UCHAR:	t = 'C';	s = 'u';	break;
     66  1.1  cgd 		case SHORT:	t = 'S';	s = '\0';	break;
     67  1.1  cgd 		case USHORT:	t = 'S';	s = 'u';	break;
     68  1.1  cgd 		case INT:	t = 'I';	s = '\0';	break;
     69  1.1  cgd 		case UINT:	t = 'I';	s = 'u';	break;
     70  1.1  cgd 		case LONG:	t = 'L';	s = '\0';	break;
     71  1.1  cgd 		case ULONG:	t = 'L';	s = 'u';	break;
     72  1.1  cgd 		case QUAD:	t = 'Q';	s = '\0';	break;
     73  1.1  cgd 		case UQUAD:	t = 'Q';	s = 'u';	break;
     74  1.1  cgd 		case FLOAT:	t = 'D';	s = 's';	break;
     75  1.1  cgd 		case DOUBLE:	t = 'D';	s = '\0';	break;
     76  1.1  cgd 		case LDOUBLE:	t = 'D';	s = 'l';	break;
     77  1.1  cgd 		case VOID:	t = 'V';	s = '\0';	break;
     78  1.1  cgd 		case PTR:	t = 'P';	s = '\0';	break;
     79  1.1  cgd 		case ARRAY:	t = 'A';	s = '\0';	break;
     80  1.1  cgd 		case ENUM:	t = 'T';	s = 'e';	break;
     81  1.1  cgd 		case STRUCT:	t = 'T';	s = 's';	break;
     82  1.1  cgd 		case UNION:	t = 'T';	s = 'u';	break;
     83  1.1  cgd 		case FUNC:
     84  1.1  cgd 			if (tp->t_args != NULL && !tp->t_proto) {
     85  1.1  cgd 				t = 'f';
     86  1.1  cgd 			} else {
     87  1.1  cgd 				t = 'F';
     88  1.1  cgd 			}
     89  1.1  cgd 			s = '\0';
     90  1.1  cgd 			break;
     91  1.1  cgd 		default:
     92  1.1  cgd 			errx(1, "internal error: outtype() 1");
     93  1.1  cgd 		}
     94  1.1  cgd 		if (tp->t_const)
     95  1.1  cgd 			outchar('c');
     96  1.1  cgd 		if (tp->t_volatile)
     97  1.1  cgd 			outchar('v');
     98  1.1  cgd 		if (s != '\0')
     99  1.1  cgd 			outchar(s);
    100  1.1  cgd 		outchar(t);
    101  1.1  cgd 		if (ts == ARRAY) {
    102  1.1  cgd 			outint(tp->t_dim);
    103  1.1  cgd 		} else if (ts == ENUM || ts == STRUCT || ts == UNION) {
    104  1.1  cgd 			if (tp->t_istag) {
    105  1.1  cgd 				outint(1);
    106  1.1  cgd 				outname(tp->t_tag->h_name);
    107  1.1  cgd 			} else if (tp->t_istynam) {
    108  1.1  cgd 				outint(2);
    109  1.1  cgd 				outname(tp->t_tynam->h_name);
    110  1.3  cgd 			} else if (tp->t_isuniqpos) {
    111  1.3  cgd 				outint(3);
    112  1.3  cgd 				outint(tp->t_uniqpos.p_line);
    113  1.3  cgd 				outchar('.');
    114  1.3  cgd 				outint(tp->t_uniqpos.p_file);
    115  1.3  cgd 				outchar('.');
    116  1.3  cgd 				outint(tp->t_uniqpos.p_uniq);
    117  1.3  cgd 			} else
    118  1.3  cgd 				errx(1, "internal error: outtype() 2");
    119  1.1  cgd 		} else if (ts == FUNC && tp->t_args != NULL) {
    120  1.1  cgd 			na = 0;
    121  1.1  cgd 			for (ap = tp->t_args; *ap != NULL; ap++)
    122  1.1  cgd 				na++;
    123  1.1  cgd 			if (tp->t_vararg)
    124  1.1  cgd 				na++;
    125  1.1  cgd 			outint(na);
    126  1.1  cgd 			for (ap = tp->t_args; *ap != NULL; ap++)
    127  1.1  cgd 				outtype(*ap);
    128  1.1  cgd 			if (tp->t_vararg)
    129  1.1  cgd 				outchar('E');
    130  1.1  cgd 		}
    131  1.1  cgd 		tp = tp->t_subt;
    132  1.1  cgd 	}
    133  1.1  cgd }
    134  1.1  cgd 
    135  1.1  cgd /*
    136  1.1  cgd  * Write a definition.
    137  1.1  cgd  */
    138  1.1  cgd static void
    139  1.1  cgd outdef(hte, sym)
    140  1.1  cgd 	hte_t	*hte;
    141  1.1  cgd 	sym_t	*sym;
    142  1.1  cgd {
    143  1.1  cgd 	/* reset output buffer */
    144  1.1  cgd 	outclr();
    145  1.1  cgd 
    146  1.1  cgd 	/* line number in C source file */
    147  1.1  cgd 	outint(0);
    148  1.1  cgd 
    149  1.1  cgd 	/* this is a definition */
    150  1.1  cgd 	outchar('d');
    151  1.1  cgd 
    152  1.1  cgd 	/* index of file where symbol was defined and line number of def. */
    153  1.1  cgd 	outint(0);
    154  1.1  cgd 	outchar('.');
    155  1.1  cgd 	outint(0);
    156  1.1  cgd 
    157  1.1  cgd 	/* flags */
    158  1.1  cgd 	if (sym->s_va) {
    159  1.1  cgd 		outchar('v');		/* varargs */
    160  1.1  cgd 		outint(sym->s_nva);
    161  1.1  cgd 	}
    162  1.1  cgd 	if (sym->s_scfl) {
    163  1.1  cgd 		outchar('S');		/* scanflike */
    164  1.1  cgd 		outint(sym->s_nscfl);
    165  1.1  cgd 	}
    166  1.1  cgd 	if (sym->s_prfl) {
    167  1.1  cgd 		outchar('P');		/* printflike */
    168  1.1  cgd 		outint(sym->s_nprfl);
    169  1.1  cgd 	}
    170  1.1  cgd 	/* definition or tentative definition */
    171  1.1  cgd 	outchar(sym->s_def == DEF ? 'd' : 't');
    172  1.1  cgd 	if (TP(sym->s_type)->t_tspec == FUNC) {
    173  1.1  cgd 		if (sym->s_rval)
    174  1.1  cgd 			outchar('r');	/* fkt. has return value */
    175  1.1  cgd 		if (sym->s_osdef)
    176  1.1  cgd 			outchar('o');	/* old style definition */
    177  1.1  cgd 	}
    178  1.1  cgd 	outchar('u');			/* used (no warning if not used) */
    179  1.1  cgd 
    180  1.1  cgd 	/* name */
    181  1.1  cgd 	outname(hte->h_name);
    182  1.1  cgd 
    183  1.1  cgd 	/* type */
    184  1.1  cgd 	outtype(TP(sym->s_type));
    185  1.1  cgd }
    186  1.1  cgd 
    187  1.1  cgd /*
    188  1.1  cgd  * Write the first definition of a name into the lint library.
    189  1.1  cgd  */
    190  1.1  cgd static void
    191  1.1  cgd dumpname(hte)
    192  1.1  cgd 	hte_t	*hte;
    193  1.1  cgd {
    194  1.1  cgd 	sym_t	*sym, *def;
    195  1.1  cgd 
    196  1.1  cgd 	/* static and undefined symbols are not written */
    197  1.1  cgd 	if (hte->h_static || !hte->h_def)
    198  1.1  cgd 		return;
    199  1.1  cgd 
    200  1.1  cgd 	/*
    201  1.1  cgd 	 * If there is a definition, write it. Otherwise write a tentative
    202  1.1  cgd 	 * definition. This is neccessary because more than one tentative
    203  1.1  cgd 	 * definition is allowed (except with sflag).
    204  1.1  cgd 	 */
    205  1.1  cgd 	def = NULL;
    206  1.1  cgd 	for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
    207  1.1  cgd 		if (sym->s_def == DEF) {
    208  1.1  cgd 			def = sym;
    209  1.1  cgd 			break;
    210  1.1  cgd 		}
    211  1.1  cgd 		if (sym->s_def == TDEF && def == NULL)
    212  1.1  cgd 			def = sym;
    213  1.1  cgd 	}
    214  1.1  cgd 	if (def == NULL)
    215  1.1  cgd 		errx(1, "internal error: dumpname() %s", hte->h_name);
    216  1.1  cgd 
    217  1.1  cgd 	outdef(hte, def);
    218  1.1  cgd }
    219  1.1  cgd 
    220  1.1  cgd /*
    221  1.1  cgd  * Write a new lint library.
    222  1.1  cgd  */
    223  1.1  cgd void
    224  1.1  cgd outlib(name)
    225  1.1  cgd 	const	char *name;
    226  1.1  cgd {
    227  1.1  cgd 	/* Open of output file and initialisation of the output buffer */
    228  1.1  cgd 	outopen(name);
    229  1.1  cgd 
    230  1.1  cgd 	/* write name of lint library */
    231  1.1  cgd 	outsrc(name);
    232  1.1  cgd 
    233  1.1  cgd 	/* name of lint lib has index 0 */
    234  1.1  cgd 	outclr();
    235  1.1  cgd 	outint(0);
    236  1.1  cgd 	outchar('s');
    237  1.1  cgd 	outstrg(name);
    238  1.1  cgd 
    239  1.3  cgd 	/*
    240  1.3  cgd 	 * print the names of all files references by unnamed
    241  1.3  cgd 	 * struct/union/enum declarations.
    242  1.3  cgd 	 */
    243  1.3  cgd 	outfiles();
    244  1.3  cgd 
    245  1.1  cgd 	/* write all definitions with external linkage */
    246  1.1  cgd 	forall(dumpname);
    247  1.1  cgd 
    248  1.1  cgd 	/* close the output */
    249  1.1  cgd 	outclose();
    250  1.3  cgd }
    251  1.3  cgd 
    252  1.3  cgd /*
    253  1.3  cgd  * Write out the name of a file referenced by a type.
    254  1.3  cgd  */
    255  1.3  cgd struct outflist {
    256  1.3  cgd 	short ofl_num;
    257  1.3  cgd 	struct outflist *ofl_next;
    258  1.3  cgd };
    259  1.3  cgd static struct outflist *outflist;
    260  1.3  cgd 
    261  1.3  cgd int
    262  1.3  cgd addoutfile(num)
    263  1.3  cgd 	short num;
    264  1.3  cgd {
    265  1.3  cgd 	struct outflist *ofl, **pofl;
    266  1.3  cgd 	int i;
    267  1.3  cgd 
    268  1.3  cgd 	ofl = outflist;
    269  1.3  cgd 	pofl = &outflist;
    270  1.3  cgd 	i = 1;				/* library is 0 */
    271  1.3  cgd 
    272  1.3  cgd 	while (ofl != NULL) {
    273  1.3  cgd 		if (ofl->ofl_num == num)
    274  1.3  cgd 			break;
    275  1.3  cgd 
    276  1.3  cgd 		pofl = &ofl->ofl_next;
    277  1.3  cgd 		ofl = ofl->ofl_next;
    278  1.3  cgd 		i++;
    279  1.3  cgd 	}
    280  1.3  cgd 
    281  1.3  cgd 	if (ofl == NULL) {
    282  1.3  cgd 		ofl = *pofl = xmalloc(sizeof (struct outflist));
    283  1.3  cgd 		ofl->ofl_num = num;
    284  1.3  cgd 		ofl->ofl_next = NULL;
    285  1.3  cgd 	}
    286  1.3  cgd 	return (i);
    287  1.3  cgd }
    288  1.3  cgd 
    289  1.3  cgd void
    290  1.3  cgd outfiles()
    291  1.3  cgd {
    292  1.3  cgd 	struct outflist *ofl;
    293  1.3  cgd 	int i;
    294  1.3  cgd 
    295  1.3  cgd 	for (ofl = outflist, i = 1; ofl != NULL; ofl = ofl->ofl_next, i++) {
    296  1.3  cgd 		/* reset output buffer */
    297  1.3  cgd 		outclr();
    298  1.3  cgd 
    299  1.3  cgd 		outint(i);
    300  1.3  cgd 		outchar('s');
    301  1.3  cgd 		outstrg(fnames[ofl->ofl_num]);
    302  1.3  cgd 	}
    303  1.1  cgd }
    304