| 1 | /* $NetBSD: emit2.c,v 1.2 1995/07/03 21:24:44 cgd Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1994, 1995 Jochen Pohl |
| 5 | * All Rights Reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. All advertising materials mentioning features or use of this software |
| 16 | * must display the following acknowledgement: |
| 17 | * This product includes software developed by Jochen Pohl for |
| 18 | * The NetBSD Project. |
| 19 | * 4. The name of the author may not be used to endorse or promote products |
| 20 | * derived from this software without specific prior written permission. |
| 21 | * |
| 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 24 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 25 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 26 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 27 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 31 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | * |
| 33 | * $NetBSD: emit2.c,v 1.2 1995/07/03 21:24:44 cgd Exp $ |
| 34 | * $DragonFly: src/usr.bin/xlint/lint2/emit2.c,v 1.4 2003/11/03 19:31:34 eirikn Exp $ |
| 35 | */ |
| 36 | |
| 37 | #include <err.h> |
| 38 | |
| 39 | #include "lint2.h" |
| 40 | |
| 41 | static void outtype(type_t *); |
| 42 | static void outdef(hte_t *, sym_t *); |
| 43 | static void dumpname(hte_t *); |
| 44 | |
| 45 | /* |
| 46 | * Write type into the output buffer. |
| 47 | */ |
| 48 | static void |
| 49 | outtype(tp) |
| 50 | type_t *tp; |
| 51 | { |
| 52 | int t, s, na; |
| 53 | tspec_t ts; |
| 54 | type_t **ap; |
| 55 | |
| 56 | while (tp != NULL) { |
| 57 | if ((ts = tp->t_tspec) == INT && tp->t_isenum) |
| 58 | ts = ENUM; |
| 59 | switch (ts) { |
| 60 | case CHAR: t = 'C'; s = '\0'; break; |
| 61 | case SCHAR: t = 'C'; s = 's'; break; |
| 62 | case UCHAR: t = 'C'; s = 'u'; break; |
| 63 | case SHORT: t = 'S'; s = '\0'; break; |
| 64 | case USHORT: t = 'S'; s = 'u'; break; |
| 65 | case INT: t = 'I'; s = '\0'; break; |
| 66 | case UINT: t = 'I'; s = 'u'; break; |
| 67 | case LONG: t = 'L'; s = '\0'; break; |
| 68 | case ULONG: t = 'L'; s = 'u'; break; |
| 69 | case QUAD: t = 'Q'; s = '\0'; break; |
| 70 | case UQUAD: t = 'Q'; s = 'u'; break; |
| 71 | case FLOAT: t = 'D'; s = 's'; break; |
| 72 | case DOUBLE: t = 'D'; s = '\0'; break; |
| 73 | case LDOUBLE: t = 'D'; s = 'l'; break; |
| 74 | case VOID: t = 'V'; s = '\0'; break; |
| 75 | case PTR: t = 'P'; s = '\0'; break; |
| 76 | case ARRAY: t = 'A'; s = '\0'; break; |
| 77 | case ENUM: t = 'T'; s = 'e'; break; |
| 78 | case STRUCT: t = 'T'; s = 's'; break; |
| 79 | case UNION: t = 'T'; s = 'u'; break; |
| 80 | case FUNC: |
| 81 | if (tp->t_args != NULL && !tp->t_proto) { |
| 82 | t = 'f'; |
| 83 | } else { |
| 84 | t = 'F'; |
| 85 | } |
| 86 | s = '\0'; |
| 87 | break; |
| 88 | default: |
| 89 | errx(1, "internal error: outtype() 1"); |
| 90 | } |
| 91 | if (tp->t_const) |
| 92 | outchar('c'); |
| 93 | if (tp->t_volatile) |
| 94 | outchar('v'); |
| 95 | if (s != '\0') |
| 96 | outchar(s); |
| 97 | outchar(t); |
| 98 | if (ts == ARRAY) { |
| 99 | outint(tp->t_dim); |
| 100 | } else if (ts == ENUM || ts == STRUCT || ts == UNION) { |
| 101 | if (tp->t_istag) { |
| 102 | outint(1); |
| 103 | outname(tp->t_tag->h_name); |
| 104 | } else if (tp->t_istynam) { |
| 105 | outint(2); |
| 106 | outname(tp->t_tynam->h_name); |
| 107 | } else { |
| 108 | outint(0); |
| 109 | } |
| 110 | } else if (ts == FUNC && tp->t_args != NULL) { |
| 111 | na = 0; |
| 112 | for (ap = tp->t_args; *ap != NULL; ap++) |
| 113 | na++; |
| 114 | if (tp->t_vararg) |
| 115 | na++; |
| 116 | outint(na); |
| 117 | for (ap = tp->t_args; *ap != NULL; ap++) |
| 118 | outtype(*ap); |
| 119 | if (tp->t_vararg) |
| 120 | outchar('E'); |
| 121 | } |
| 122 | tp = tp->t_subt; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Write a definition. |
| 128 | */ |
| 129 | static void |
| 130 | outdef(hte, sym) |
| 131 | hte_t *hte; |
| 132 | sym_t *sym; |
| 133 | { |
| 134 | /* reset output buffer */ |
| 135 | outclr(); |
| 136 | |
| 137 | /* line number in C source file */ |
| 138 | outint(0); |
| 139 | |
| 140 | /* this is a definition */ |
| 141 | outchar('d'); |
| 142 | |
| 143 | /* index of file where symbol was defined and line number of def. */ |
| 144 | outint(0); |
| 145 | outchar('.'); |
| 146 | outint(0); |
| 147 | |
| 148 | /* flags */ |
| 149 | if (sym->s_va) { |
| 150 | outchar('v'); /* varargs */ |
| 151 | outint(sym->s_nva); |
| 152 | } |
| 153 | if (sym->s_scfl) { |
| 154 | outchar('S'); /* scanflike */ |
| 155 | outint(sym->s_nscfl); |
| 156 | } |
| 157 | if (sym->s_prfl) { |
| 158 | outchar('P'); /* printflike */ |
| 159 | outint(sym->s_nprfl); |
| 160 | } |
| 161 | /* definition or tentative definition */ |
| 162 | outchar(sym->s_def == DEF ? 'd' : 't'); |
| 163 | if (TP(sym->s_type)->t_tspec == FUNC) { |
| 164 | if (sym->s_rval) |
| 165 | outchar('r'); /* fkt. has return value */ |
| 166 | if (sym->s_osdef) |
| 167 | outchar('o'); /* old style definition */ |
| 168 | } |
| 169 | outchar('u'); /* used (no warning if not used) */ |
| 170 | |
| 171 | /* name */ |
| 172 | outname(hte->h_name); |
| 173 | |
| 174 | /* type */ |
| 175 | outtype(TP(sym->s_type)); |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Write the first definition of a name into the lint library. |
| 180 | */ |
| 181 | static void |
| 182 | dumpname(hte) |
| 183 | hte_t *hte; |
| 184 | { |
| 185 | sym_t *sym, *def; |
| 186 | |
| 187 | /* static and undefined symbols are not written */ |
| 188 | if (hte->h_static || !hte->h_def) |
| 189 | return; |
| 190 | |
| 191 | /* |
| 192 | * If there is a definition, write it. Otherwise write a tentative |
| 193 | * definition. This is neccessary because more than one tentative |
| 194 | * definition is allowed (except with sflag). |
| 195 | */ |
| 196 | def = NULL; |
| 197 | for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) { |
| 198 | if (sym->s_def == DEF) { |
| 199 | def = sym; |
| 200 | break; |
| 201 | } |
| 202 | if (sym->s_def == TDEF && def == NULL) |
| 203 | def = sym; |
| 204 | } |
| 205 | if (def == NULL) |
| 206 | errx(1, "internal error: dumpname() %s", hte->h_name); |
| 207 | |
| 208 | outdef(hte, def); |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Write a new lint library. |
| 213 | */ |
| 214 | void |
| 215 | outlib(name) |
| 216 | 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 | } |