]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/8l/list.c
merge
[plan9front.git] / sys / src / cmd / 8l / list.c
1 #include        "l.h"
2
3 void
4 listinit(void)
5 {
6
7         fmtinstall('R', Rconv);
8         fmtinstall('A', Aconv);
9         fmtinstall('D', Dconv);
10         fmtinstall('S', Sconv);
11         fmtinstall('P', Pconv);
12 }
13
14 static  Prog    *bigP;
15
16 int
17 Pconv(Fmt *fp)
18 {
19         char str[STRINGSZ];
20         Prog *p;
21
22         p = va_arg(fp->args, Prog*);
23         bigP = p;
24         switch(p->as) {
25         case ATEXT:
26         case AGLOBL:
27                 if(p->from.scale) {
28                         snprint(str, sizeof(str), "(%ld)        %A      %D,%d,%D",
29                                 p->line, p->as, &p->from, p->from.scale, &p->to);
30                         break;
31                 }
32         default:
33                 snprint(str, sizeof(str), "(%ld)        %A      %D,%D",
34                         p->line, p->as, &p->from, &p->to);
35                 break;
36         case ADATA:
37         case AINIT:
38         case ADYNT:
39                 snprint(str, sizeof(str), "(%ld)        %A      %D/%d,%D",
40                         p->line, p->as, &p->from, p->from.scale, &p->to);
41                 break;
42         }
43         bigP = P;
44         return fmtstrcpy(fp, str);
45 }
46
47 int
48 Aconv(Fmt *fp)
49 {
50         int i;
51
52         i = va_arg(fp->args, int);
53         return fmtstrcpy(fp, anames[i]);
54 }
55
56 int
57 Dconv(Fmt *fp)
58 {
59         char str[STRINGSZ+40];
60         Adr *a;
61         int i;
62
63         a = va_arg(fp->args, Adr*);
64         i = a->type;
65         if(i >= D_INDIR && i < D_M0) {
66                 if(a->offset)
67                         snprint(str, sizeof(str), "%ld(%R)", a->offset, i-D_INDIR);
68                 else
69                         snprint(str, sizeof(str), "(%R)", i-D_INDIR);
70                 goto brk;
71         }
72         switch(i) {
73
74         default:
75                 snprint(str, sizeof(str), "%R", i);
76                 break;
77
78         case D_NONE:
79                 str[0] = 0;
80                 break;
81
82         case D_BRANCH:
83                 if(bigP != P && bigP->pcond != P)
84                         if(a->sym != S)
85                                 snprint(str, sizeof(str), "%lux+%s", bigP->pcond->pc,
86                                         a->sym->name);
87                         else
88                                 snprint(str, sizeof(str), "%lux", bigP->pcond->pc);
89                 else
90                         snprint(str, sizeof(str), "%ld(PC)", a->offset);
91                 break;
92
93         case D_EXTERN:
94                 snprint(str, sizeof(str), "%s+%ld(SB)", a->sym->name, a->offset);
95                 break;
96
97         case D_STATIC:
98                 snprint(str, sizeof(str), "%s<%d>+%ld(SB)", a->sym->name,
99                         a->sym->version, a->offset);
100                 break;
101
102         case D_AUTO:
103                 snprint(str, sizeof(str), "%s+%ld(SP)", a->sym->name, a->offset);
104                 break;
105
106         case D_PARAM:
107                 if(a->sym)
108                         snprint(str, sizeof(str), "%s+%ld(FP)", a->sym->name, a->offset);
109                 else
110                         snprint(str, sizeof(str), "%ld(FP)", a->offset);
111                 break;
112
113         case D_CONST:
114                 snprint(str, sizeof(str), "$%ld", a->offset);
115                 break;
116
117         case D_FCONST:
118                 snprint(str, sizeof(str), "$(%.8lux,%.8lux)", a->ieee.h, a->ieee.l);
119                 break;
120
121         case D_SCONST:
122                 snprint(str, sizeof(str), "$\"%S\"", a->scon);
123                 break;
124
125         case D_ADDR:
126                 a->type = a->index;
127                 a->index = D_NONE;
128                 snprint(str, sizeof(str), "$%D", a);
129                 a->index = a->type;
130                 a->type = D_ADDR;
131                 goto conv;
132         }
133 brk:
134         if(a->index != D_NONE)
135                 return fmtprint(fp, "%s(%R*%d)", str, a->index, a->scale);
136 conv:
137         return fmtstrcpy(fp, str);
138 }
139
140 char*   regstr[] =
141 {
142         "AL",           /* [D_AL] */
143         "CL",
144         "DL",
145         "BL",
146         "AH",
147         "CH",
148         "DH",
149         "BH",
150
151         "AX",           /* [D_AX] */
152         "CX",
153         "DX",
154         "BX",
155         "SP",
156         "BP",
157         "SI",
158         "DI",
159
160         "F0",           /* [D_F0] */
161         "F1",
162         "F2",
163         "F3",
164         "F4",
165         "F5",
166         "F6",
167         "F7",
168
169         "CS",           /* [D_CS] */
170         "SS",
171         "DS",
172         "ES",
173         "FS",
174         "GS",
175
176         "GDTR",         /* [D_GDTR] */
177         "IDTR",         /* [D_IDTR] */
178         "LDTR",         /* [D_LDTR] */
179         "MSW",          /* [D_MSW] */
180         "TASK",         /* [D_TASK] */
181
182         "CR0",          /* [D_CR] */
183         "CR1",
184         "CR2",
185         "CR3",
186         "CR4",
187         "CR5",
188         "CR6",
189         "CR7",
190
191         "DR0",          /* [D_DR] */
192         "DR1",
193         "DR2",
194         "DR3",
195         "DR4",
196         "DR5",
197         "DR6",
198         "DR7",
199
200         "TR0",          /* [D_TR] */
201         "TR1",
202         "TR2",
203         "TR3",
204         "TR4",
205         "TR5",
206         "TR6",
207         "TR7",
208
209         "NONE",         /* [D_NONE] */
210
211 [D_M0]  "M0",           /* [D_M0] */
212                 "M1",
213                 "M2",
214                 "M3",
215                 "M4",
216                 "M5",
217                 "M6",
218                 "M7",
219
220 [D_X0]  "X0",           /* [D_X0] */
221                 "X1",
222                 "X2",
223                 "X3",
224                 "X4",
225                 "X5",
226                 "X6",
227                 "X7",
228 };
229
230 int
231 Rconv(Fmt *fp)
232 {
233         char str[20];
234         int r;
235
236         r = va_arg(fp->args, int);
237         if((r >= D_AL && r <= D_NONE) || (r >= D_M0 && r <= D_X0+7))
238                 snprint(str, sizeof(str), "%s", regstr[r-D_AL]);
239         else
240                 snprint(str, sizeof(str), "gok(%d)", r);
241
242         return fmtstrcpy(fp, str);
243 }
244
245 int
246 Sconv(Fmt *fp)
247 {
248         int i, c;
249         char str[30], *p, *a;
250
251         a = va_arg(fp->args, char*);
252         p = str;
253         for(i=0; i<sizeof(double); i++) {
254                 c = a[i] & 0xff;
255                 if(c >= 'a' && c <= 'z' ||
256                    c >= 'A' && c <= 'Z' ||
257                    c >= '0' && c <= '9') {
258                         *p++ = c;
259                         continue;
260                 }
261                 *p++ = '\\';
262                 switch(c) {
263                 default:
264                         if(c < 040 || c >= 0177)
265                                 break;  /* not portable */
266                         p[-1] = c;
267                         continue;
268                 case 0:
269                         *p++ = 'z';
270                         continue;
271                 case '\\':
272                 case '"':
273                         *p++ = c;
274                         continue;
275                 case '\n':
276                         *p++ = 'n';
277                         continue;
278                 case '\t':
279                         *p++ = 't';
280                         continue;
281                 }
282                 *p++ = (c>>6) + '0';
283                 *p++ = ((c>>3) & 7) + '0';
284                 *p++ = (c & 7) + '0';
285         }
286         *p = 0;
287         return fmtstrcpy(fp, str);
288 }
289
290 void
291 diag(char *fmt, ...)
292 {
293         char buf[STRINGSZ], *tn;
294         va_list arg;
295
296         tn = "??none??";
297         if(curtext != P && curtext->from.sym != S)
298                 tn = curtext->from.sym->name;
299         va_start(arg, fmt);
300         vseprint(buf, buf+sizeof(buf), fmt, arg);
301         va_end(arg);
302         print("%s: %s\n", tn, buf);
303
304         nerrors++;
305         if(nerrors > 20 && !debug['A']) {
306                 print("too many errors\n");
307                 errorexit();
308         }
309 }