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