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