emit2.c revision 1.30
1/* $NetBSD: emit2.c,v 1.30 2023/02/02 22:23:30 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.30 2023/02/02 22:23:30 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] = "???BCCCSSIILLQQJJDDDVTTTPAF?XXX"; 55 static const char ss[NTSPEC] = "??? su u u u u us l sue ?s l"; 56#else 57 static const char tt[NTSPEC] = "???BCCCSSIILLQQDDDVTTTPAF?XXX"; 58 static const char ss[NTSPEC] = "??? su u u u us l sue ?s l"; 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 /* reset output buffer */ 120 outclr(); 121 122 /* line number in C source file */ 123 outint(0); 124 125 /* this is a definition */ 126 outchar('d'); 127 128 /* index of file where symbol was defined and line number of def. */ 129 outint(0); 130 outchar('.'); 131 outint(0); 132 133 /* flags */ 134 if (sym->s_check_only_first_args) { 135 outchar('v'); 136 outint(sym->s_check_num_args); 137 } 138 if (sym->s_scanflike) { 139 outchar('S'); 140 outint(sym->s_scanflike_arg); 141 } 142 if (sym->s_printflike) { 143 outchar('P'); 144 outint(sym->s_printflike_arg); 145 } 146 /* definition or tentative definition */ 147 outchar(sym->s_def == DEF ? 'd' : 't'); 148 if (TP(sym->s_type)->t_tspec == FUNC) { 149 if (sym->s_function_has_return_value) 150 outchar('r'); 151 if (sym->s_old_style_function) 152 outchar('o'); 153 } 154 outchar('u'); /* used (no warning if not used) */ 155 156 /* name */ 157 outname(hte->h_name); 158 159 /* type */ 160 outtype(TP(sym->s_type)); 161} 162 163/* 164 * Write the first definition of a name into the lint library. 165 */ 166static void 167dumpname(hte_t *hte) 168{ 169 sym_t *sym, *def; 170 171 /* static and undefined symbols are not written */ 172 if (hte->h_static || !hte->h_def) 173 return; 174 175 /* 176 * If there is a definition, write it. Otherwise, write a tentative 177 * definition. This is necessary because more than one tentative 178 * definition is allowed (except with sflag). 179 */ 180 def = NULL; 181 for (sym = hte->h_syms; sym != NULL; sym = sym->s_next) { 182 if (sym->s_def == DEF) { 183 def = sym; 184 break; 185 } 186 if (sym->s_def == TDEF && def == NULL) 187 def = sym; 188 } 189 if (def == NULL) 190 errx(1, "internal error: dumpname %s", hte->h_name); 191 192 outdef(hte, def); 193} 194 195/* 196 * Write a new lint library. 197 */ 198void 199outlib(const char *name) 200{ 201 /* Open of output file and initialization of the output buffer */ 202 outopen(name); 203 204 /* write name of lint library */ 205 outsrc(name); 206 207 /* name of lint lib has index 0 */ 208 outclr(); 209 outint(0); 210 outchar('s'); 211 outstrg(name); 212 213 /* 214 * print the names of all files referenced by unnamed 215 * struct/union/enum declarations. 216 */ 217 outfiles(); 218 219 /* write all definitions with external linkage */ 220 symtab_forall_sorted(dumpname); 221 222 /* close the output */ 223 outclose(); 224} 225 226/* 227 * Write out the name of a file referenced by a type. 228 */ 229struct outflist { 230 short ofl_num; 231 struct outflist *ofl_next; 232}; 233static struct outflist *outflist; 234 235int 236addoutfile(short num) 237{ 238 struct outflist *ofl, **pofl; 239 int i; 240 241 ofl = outflist; 242 pofl = &outflist; 243 i = 1; /* library is 0 */ 244 245 while (ofl != NULL) { 246 if (ofl->ofl_num == num) 247 break; 248 249 pofl = &ofl->ofl_next; 250 ofl = ofl->ofl_next; 251 i++; 252 } 253 254 if (ofl == NULL) { 255 ofl = *pofl = xmalloc(sizeof(**pofl)); 256 ofl->ofl_num = num; 257 ofl->ofl_next = NULL; 258 } 259 return i; 260} 261 262static void 263outfiles(void) 264{ 265 struct outflist *ofl; 266 int i; 267 268 for (ofl = outflist, i = 1; ofl != NULL; ofl = ofl->ofl_next, i++) { 269 /* reset output buffer */ 270 outclr(); 271 272 outint(i); 273 outchar('s'); 274 outstrg(fnames[ofl->ofl_num]); 275 } 276} 277