emit2.c revision 1.35
1/* $NetBSD: emit2.c,v 1.35 2023/08/12 21:08:37 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#include <sys/cdefs.h>
36#if defined(__RCSID)
37__RCSID("$NetBSD: emit2.c,v 1.35 2023/08/12 21:08:37 rillig Exp $");
38#endif
39
40#include "lint2.h"
41
42static	void	outtype(type_t *);
43static	void	outdef(hte_t *, sym_t *);
44static	void	dumpname(hte_t *);
45static	void	outfiles(void);
46
47/*
48 * Write type into the output buffer.
49 */
50static void
51outtype(type_t *tp)
52{
53#ifdef INT128_SIZE
54	static const char tt[NTSPEC] = "???BCCCSSIILLQQJJDDD?XXXVTTTPAF";
55	static const char ss[NTSPEC] = "???  su u u u u us l?s l sue   ";
56#else
57	static const char tt[NTSPEC] = "???BCCCSSIILLQQDDD?XXXVTTTPAF";
58	static const char ss[NTSPEC] = "???  su u u u us l?s l sue   ";
59#endif
60
61	while (tp != NULL) {
62		tspec_t ts = tp->t_tspec;
63		if (ts == INT && tp->t_is_enum)
64			ts = ENUM;
65		if (!ch_isupper(tt[ts]))
66			errx(1, "internal error: outtype(%d)", ts);
67		if (tp->t_const)
68			outchar('c');
69		if (tp->t_volatile)
70			outchar('v');
71		if (ss[ts] != ' ')
72			outchar(ss[ts]);
73		if (ts == FUNC && tp->t_args != NULL && !tp->t_proto)
74			outchar('f');
75		else
76			outchar(tt[ts]);
77
78		if (ts == ARRAY) {
79			outint(tp->t_dim);
80		} else if (ts == ENUM || ts == STRUCT || ts == UNION) {
81			if (tp->t_istag) {
82				outint(1);
83				outname(tp->t_tag->h_name);
84			} else if (tp->t_istynam) {
85				outint(2);
86				outname(tp->t_tynam->h_name);
87			} else if (tp->t_isuniqpos) {
88				outint(3);
89				outint(tp->t_uniqpos.p_line);
90				outchar('.');
91				outint(tp->t_uniqpos.p_file);
92				outchar('.');
93				outint(tp->t_uniqpos.p_uniq);
94			} else
95				errx(1, "internal error: outtype");
96		} else if (ts == FUNC && tp->t_args != NULL) {
97			int na = 0;
98			for (type_t **ap = tp->t_args; *ap != NULL; ap++)
99				na++;
100			if (tp->t_vararg)
101				na++;
102			outint(na);
103			for (type_t **ap = tp->t_args; *ap != NULL; ap++)
104				outtype(*ap);
105			if (tp->t_vararg)
106				outchar('E');
107		}
108		tp = tp->t_subt;
109	}
110}
111
112/*
113 * Write a definition.
114 */
115static void
116outdef(hte_t *hte, sym_t *sym)
117{
118
119	outint(0);		/* line number in C source file */
120	outchar('d');		/* definition */
121	outint(0);		/* index of file where symbol was defined */
122	outchar('.');
123	outint(0);		/* line number of definition */
124
125	/* flags */
126	if (sym->s_check_only_first_args) {
127		outchar('v');
128		outint(sym->s_check_num_args);
129	}
130	if (sym->s_scanflike) {
131		outchar('S');
132		outint(sym->s_scanflike_arg);
133	}
134	if (sym->s_printflike) {
135		outchar('P');
136		outint(sym->s_printflike_arg);
137	}
138	/* definition or tentative definition */
139	outchar(sym->s_def == DEF ? 'd' : 't');
140	if (TP(sym->s_type)->t_tspec == FUNC) {
141		if (sym->s_function_has_return_value)
142			outchar('r');
143		if (sym->s_old_style_function)
144			outchar('o');
145	}
146	outchar('u');		/* used (no warning if not used) */
147	outname(hte->h_name);
148	outtype(TP(sym->s_type));
149	outchar('\n');
150}
151
152/*
153 * Write the first definition of a name into the lint library.
154 */
155static void
156dumpname(hte_t *hte)
157{
158	sym_t *sym, *def;
159
160	/* static and undefined symbols are not written */
161	if (hte->h_static || !hte->h_def)
162		return;
163
164	/*
165	 * If there is a definition, write it. Otherwise, write a tentative
166	 * definition. This is necessary because more than one tentative
167	 * definition is allowed (except with sflag).
168	 */
169	def = NULL;
170	for (sym = hte->h_syms; sym != NULL; sym = sym->s_next) {
171		if (sym->s_def == DEF) {
172			def = sym;
173			break;
174		}
175		if (sym->s_def == TDEF && def == NULL)
176			def = sym;
177	}
178	if (def == NULL)
179		errx(1, "internal error: dumpname %s", hte->h_name);
180
181	outdef(hte, def);
182}
183
184/*
185 * Write a new lint library.
186 */
187void
188outlib(const char *name)
189{
190
191	outopen(name);
192
193	outsrc(name);		/* name of the lint library */
194
195	outint(0);		/* filename index of the lint library */
196	outchar('s');
197	outstrg(name);
198	outchar('\n');
199
200	/* All files referenced by unnamed struct/union/enum declarations. */
201	outfiles();
202
203	/* Write all definitions with external linkage. */
204	symtab_forall_sorted(dumpname);
205
206	outclose();
207}
208
209/*
210 * Write out the name of a file referenced by a type.
211 */
212struct outflist {
213	short		ofl_num;
214	struct outflist *ofl_next;
215};
216static struct outflist *outflist;
217
218int
219addoutfile(short num)
220{
221	struct outflist *ofl, **pofl;
222	int i;
223
224	ofl = outflist;
225	pofl = &outflist;
226	i = 1;				/* library is 0 */
227
228	while (ofl != NULL) {
229		if (ofl->ofl_num == num)
230			break;
231
232		pofl = &ofl->ofl_next;
233		ofl = ofl->ofl_next;
234		i++;
235	}
236
237	if (ofl == NULL) {
238		ofl = *pofl = xmalloc(sizeof(**pofl));
239		ofl->ofl_num = num;
240		ofl->ofl_next = NULL;
241	}
242	return i;
243}
244
245static void
246outfiles(void)
247{
248	struct outflist *ofl;
249	int i;
250
251	for (ofl = outflist, i = 1; ofl != NULL; ofl = ofl->ofl_next, i++) {
252		outint(i);
253		outchar('s');
254		outstrg(fnames[ofl->ofl_num]);
255		outchar('\n');
256	}
257}
258