1 /* $OpenBSD: emit2.c,v 1.7 2011/09/21 18:08:07 jsg Exp $ */
2 /* $NetBSD: emit2.c,v 1.2 1995/07/03 21:24:44 cgd Exp $ */
3
4 /*
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 <err.h>
36
37 #include "lint2.h"
38
39 static void outtype(type_t *);
40 static void outdef(hte_t *, sym_t *);
41 static void dumpname(hte_t *);
42
43 /*
44 * Write type into the output buffer.
45 */
46 static void
outtype(type_t * tp)47 outtype(type_t *tp)
48 {
49 int t, s, na;
50 tspec_t ts;
51 type_t **ap;
52
53 while (tp != NULL) {
54 if ((ts = tp->t_tspec) == INT && tp->t_isenum)
55 ts = ENUM;
56 switch (ts) {
57 case BOOL: t = 'B'; s = '\0'; break;
58 case CHAR: t = 'C'; s = '\0'; break;
59 case SCHAR: t = 'C'; s = 's'; break;
60 case UCHAR: t = 'C'; s = 'u'; break;
61 case SHORT: t = 'S'; s = '\0'; break;
62 case USHORT: t = 'S'; s = 'u'; break;
63 case INT: t = 'I'; s = '\0'; break;
64 case UINT: t = 'I'; s = 'u'; break;
65 case LONG: t = 'L'; s = '\0'; break;
66 case ULONG: t = 'L'; s = 'u'; break;
67 case QUAD: t = 'Q'; s = '\0'; break;
68 case UQUAD: t = 'Q'; s = 'u'; break;
69 case FLOAT: t = 'D'; s = 's'; break;
70 case DOUBLE: t = 'D'; s = '\0'; break;
71 case LDOUBLE: t = 'D'; s = 'l'; break;
72 case COMPLEX: t = 'X'; s = 's'; break;
73 case DCOMPLEX: t = 'X'; s = '\0'; break;
74 case LDCOMPLEX: t = 'X'; s = 'l'; break;
75 case IMAGINARY: t = 'J'; s = 's'; break;
76 case DIMAGINARY: t = 'J'; s = '\0'; break;
77 case LDIMAGINARY:t = 'J'; s = 'l'; break;
78 case VOID: t = 'V'; s = '\0'; break;
79 case PTR: t = 'P'; s = '\0'; break;
80 case ARRAY: t = 'A'; s = '\0'; break;
81 case ENUM: t = 'T'; s = 'e'; break;
82 case STRUCT: t = 'T'; s = 's'; break;
83 case UNION: t = 'T'; s = 'u'; break;
84 case FUNC:
85 if (tp->t_args != NULL && !tp->t_proto) {
86 t = 'f';
87 } else {
88 t = 'F';
89 }
90 s = '\0';
91 break;
92 default:
93 errx(1, "internal error: outtype() 1");
94 }
95 if (tp->t_const)
96 outchar('c');
97 if (tp->t_volatile)
98 outchar('v');
99 if (s != '\0')
100 outchar(s);
101 outchar(t);
102 if (ts == ARRAY) {
103 outint(tp->t_dim);
104 } else if (ts == ENUM || ts == STRUCT || ts == UNION) {
105 if (tp->t_istag) {
106 outint(1);
107 outname(tp->t_tag->h_name);
108 } else if (tp->t_istynam) {
109 outint(2);
110 outname(tp->t_tynam->h_name);
111 } else {
112 outint(0);
113 }
114 } else if (ts == FUNC && tp->t_args != NULL) {
115 na = 0;
116 for (ap = tp->t_args; *ap != NULL; ap++)
117 na++;
118 if (tp->t_vararg)
119 na++;
120 outint(na);
121 for (ap = tp->t_args; *ap != NULL; ap++)
122 outtype(*ap);
123 if (tp->t_vararg)
124 outchar('E');
125 }
126 tp = tp->t_subt;
127 }
128 }
129
130 /*
131 * Write a definition.
132 */
133 static void
outdef(hte_t * hte,sym_t * sym)134 outdef(hte_t *hte, sym_t *sym)
135 {
136 /* reset output buffer */
137 outclr();
138
139 /* line number in C source file */
140 outint(0);
141
142 /* this is a definition */
143 outchar('d');
144
145 /* index of file where symbol was defined and line number of def. */
146 outint(0);
147 outchar('.');
148 outint(0);
149
150 /* flags */
151 if (sym->s_va) {
152 outchar('v'); /* varargs */
153 outint(sym->s_nva);
154 }
155 if (sym->s_scfl) {
156 outchar('S'); /* scanflike */
157 outint(sym->s_nscfl);
158 }
159 if (sym->s_prfl) {
160 outchar('P'); /* printflike */
161 outint(sym->s_nprfl);
162 }
163 /* definition or tentative definition */
164 outchar(sym->s_def == DEF ? 'd' : 't');
165 if (TP(sym->s_type)->t_tspec == FUNC) {
166 if (sym->s_rval)
167 outchar('r'); /* fkt. has return value */
168 if (sym->s_osdef)
169 outchar('o'); /* old style definition */
170 }
171 outchar('u'); /* used (no warning if not used) */
172
173 /* name */
174 outname(hte->h_name);
175
176 /* type */
177 outtype(TP(sym->s_type));
178 }
179
180 /*
181 * Write the first definition of a name into the lint library.
182 */
183 static void
dumpname(hte_t * hte)184 dumpname(hte_t *hte)
185 {
186 sym_t *sym, *def;
187
188 /* static and undefined symbols are not written */
189 if (hte->h_static || !hte->h_def)
190 return;
191
192 /*
193 * If there is a definition, write it. Otherwise write a tentative
194 * definition. This is necessary because more than one tentative
195 * definition is allowed (except with sflag).
196 */
197 def = NULL;
198 for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
199 if (sym->s_def == DEF) {
200 def = sym;
201 break;
202 }
203 if (sym->s_def == TDEF && def == NULL)
204 def = sym;
205 }
206 if (def == NULL)
207 errx(1, "internal error: dumpname() %s", hte->h_name);
208
209 outdef(hte, def);
210 }
211
212 /*
213 * Write a new lint library.
214 */
215 void
outlib(const char * name)216 outlib(const char *name)
217 {
218 /* Open of output file and initialisation of the output buffer */
219 outopen(name);
220
221 /* write name of lint library */
222 outsrc(name);
223
224 /* name of lint lib has index 0 */
225 outclr();
226 outint(0);
227 outchar('s');
228 outstrg(name);
229
230 /* write all definitions with external linkage */
231 forall(dumpname);
232
233 /* close the output */
234 outclose();
235 }
236