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