1 #include "cc.h" 2 3 static char *kwd[] = 4 { 5 "$adt", "$aggr", "$append", "$complex", "$defn", 6 "$delete", "$do", "$else", "$eval", "$head", "$if", 7 "$local", "$loop", "$return", "$tail", "$then", 8 "$union", "$whatis", "$while", 9 }; 10 11 char* 12 amap(char *s) 13 { 14 int i, bot, top, new; 15 16 bot = 0; 17 top = bot + nelem(kwd) - 1; 18 while(bot <= top){ 19 new = bot + (top - bot)/2; 20 i = strcmp(kwd[new]+1, s); 21 if(i == 0) 22 return kwd[new]; 23 24 if(i < 0) 25 bot = new + 1; 26 else 27 top = new - 1; 28 } 29 return s; 30 } 31 32 Sym* 33 acidsue(Type *t) 34 { 35 int h; 36 Sym *s; 37 38 if(t != T) 39 for(h=0; h<nelem(hash); h++) 40 for(s = hash[h]; s != S; s = s->link) 41 if(s->suetag && s->suetag->link == t) 42 return s; 43 return 0; 44 } 45 46 Sym* 47 acidfun(Type *t) 48 { 49 int h; 50 Sym *s; 51 52 for(h=0; h<nelem(hash); h++) 53 for(s = hash[h]; s != S; s = s->link) 54 if(s->type == t) 55 return s; 56 return 0; 57 } 58 59 char acidchar[] = 60 { 61 [TCHAR] 'C', 62 [TUCHAR] 'b', 63 [TSHORT] 'd', 64 [TUSHORT] 'u', 65 [TLONG] 'D', 66 [TULONG] 'U', 67 [TVLONG] 'V', 68 [TUVLONG] 'W', 69 [TFLOAT] 'f', 70 [TDOUBLE] 'F', 71 [TARRAY] 'a', 72 [TIND] 'X', 73 }; 74 75 void 76 acidmember(Type *t, long off, int flag) 77 { 78 Sym *s, *s1; 79 Type *l; 80 81 s = t->sym; 82 switch(t->etype) { 83 default: 84 Bprint(&outbuf, " T%d\n", t->etype); 85 break; 86 87 case TIND: 88 if(s == S) 89 break; 90 if(flag) { 91 for(l=t; l->etype==TIND; l=l->link) 92 ; 93 if(typesu[l->etype]) { 94 s1 = acidsue(l->link); 95 if(s1 != S) { 96 Bprint(&outbuf, " 'A' %s %ld %s;\n", 97 amap(s1->name), 98 t->offset+off, amap(s->name)); 99 break; 100 } 101 } 102 } else { 103 Bprint(&outbuf, "\tprint(\"\t%s\t\", addr.%s\\X, \"\\n\");\n", 104 amap(s->name), amap(s->name)); 105 break; 106 } 107 108 case TCHAR: 109 case TUCHAR: 110 case TSHORT: 111 case TUSHORT: 112 case TLONG: 113 case TULONG: 114 case TVLONG: 115 case TUVLONG: 116 case TFLOAT: 117 case TDOUBLE: 118 case TARRAY: 119 if(s == S) 120 break; 121 if(flag) { 122 Bprint(&outbuf, " '%c' %ld %s;\n", 123 acidchar[t->etype], t->offset+off, amap(s->name)); 124 } else { 125 Bprint(&outbuf, "\tprint(\"\t%s\t\", addr.%s, \"\\n\");\n", 126 amap(s->name), amap(s->name)); 127 } 128 break; 129 130 case TSTRUCT: 131 case TUNION: 132 s1 = acidsue(t->link); 133 if(s1 == S) 134 break; 135 if(flag) { 136 if(s == S) { 137 Bprint(&outbuf, " {\n"); 138 for(l = t->link; l != T; l = l->down) 139 acidmember(l, t->offset+off, flag); 140 Bprint(&outbuf, " };\n"); 141 } else { 142 Bprint(&outbuf, " %s %ld %s;\n", 143 amap(s1->name), 144 t->offset+off, amap(s->name)); 145 } 146 } else { 147 if(s != S) { 148 Bprint(&outbuf, "\tprint(\"%s %s {\\n\");\n", 149 amap(s1->name), amap(s->name)); 150 Bprint(&outbuf, "\t%s(addr.%s);\n", 151 amap(s1->name), amap(s->name)); 152 Bprint(&outbuf, "\tprint(\"}\\n\");\n"); 153 } else { 154 Bprint(&outbuf, "\tprint(\"%s {\\n\");\n", 155 amap(s1->name)); 156 Bprint(&outbuf, "\t\t%s(addr+%d);\n", 157 amap(s1->name), t->offset+off); 158 Bprint(&outbuf, "\tprint(\"}\\n\");\n"); 159 } 160 } 161 break; 162 } 163 } 164 165 void 166 acidtype(Type *t) 167 { 168 Sym *s; 169 Type *l; 170 Io *i; 171 int n; 172 char *an; 173 174 if(!debug['a']) 175 return; 176 if(debug['a'] > 1) { 177 n = 0; 178 for(i=iostack; i; i=i->link) 179 n++; 180 if(n > 1) 181 return; 182 } 183 s = acidsue(t->link); 184 if(s == S) 185 return; 186 switch(t->etype) { 187 default: 188 Bprint(&outbuf, "T%d\n", t->etype); 189 return; 190 191 case TUNION: 192 case TSTRUCT: 193 if(debug['s']) 194 goto asmstr; 195 an = amap(s->name); 196 Bprint(&outbuf, "sizeof%s = %d;\n", an, t->width); 197 Bprint(&outbuf, "aggr %s\n{\n", an); 198 for(l = t->link; l != T; l = l->down) 199 acidmember(l, 0, 1); 200 Bprint(&outbuf, "};\n\n"); 201 202 Bprint(&outbuf, "defn\n%s(addr) {\n\tcomplex %s addr;\n", an, an); 203 for(l = t->link; l != T; l = l->down) 204 acidmember(l, 0, 0); 205 Bprint(&outbuf, "};\n\n"); 206 break; 207 asmstr: 208 if(s == S) 209 break; 210 for(l = t->link; l != T; l = l->down) 211 if(l->sym != S) 212 Bprint(&outbuf, "#define\t%s.%s\t%ld\n", 213 s->name, 214 l->sym->name, 215 l->offset); 216 break; 217 } 218 } 219 220 void 221 acidvar(Sym *s) 222 { 223 int n; 224 Io *i; 225 Type *t; 226 Sym *s1, *s2; 227 228 if(!debug['a'] || debug['s']) 229 return; 230 if(debug['a'] > 1) { 231 n = 0; 232 for(i=iostack; i; i=i->link) 233 n++; 234 if(n > 1) 235 return; 236 } 237 t = s->type; 238 while(t && t->etype == TIND) 239 t = t->link; 240 if(t == T) 241 return; 242 if(!typesu[t->etype]) 243 return; 244 s1 = acidsue(t->link); 245 if(s1 == S) 246 return; 247 switch(s->class) { 248 case CAUTO: 249 case CPARAM: 250 s2 = acidfun(thisfn); 251 if(s2) 252 Bprint(&outbuf, "complex %s %s:%s;\n", 253 amap(s1->name), amap(s2->name), amap(s->name)); 254 break; 255 256 case CSTATIC: 257 case CEXTERN: 258 case CGLOBL: 259 case CLOCAL: 260 Bprint(&outbuf, "complex %s %s;\n", 261 amap(s1->name), amap(s->name)); 262 break; 263 } 264 } 265