]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/kc/list.c
merge
[plan9front.git] / sys / src / cmd / kc / list.c
1 #define EXTERN
2 #include "gc.h"
3
4 void
5 listinit(void)
6 {
7
8         fmtinstall('A', Aconv);
9         fmtinstall('P', Pconv);
10         fmtinstall('S', Sconv);
11         fmtinstall('N', Nconv);
12         fmtinstall('D', Dconv);
13         fmtinstall('B', Bconv);
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         int a;
40
41         p = va_arg(fp->args, Prog*);
42         a = p->as;
43         if(a == ADATA)
44                 snprint(str, sizeof str, "      %A      %D/%d,%D", a, &p->from, p->reg, &p->to);
45         else
46         if(p->as == ATEXT)
47                 snprint(str, sizeof str, "      %A      %D,%d,%D", a, &p->from, p->reg, &p->to);
48         else
49         if(p->reg == NREG)
50                 snprint(str, sizeof str, "      %A      %D,%D", a, &p->from, &p->to);
51         else
52         if(p->from.type != D_FREG)
53                 snprint(str, sizeof str, "      %A      %D,R%d,%D", a, &p->from, p->reg, &p->to);
54         else
55                 snprint(str, sizeof str, "      %A      %D,F%d,%D", a, &p->from, p->reg, &p->to);
56         return fmtstrcpy(fp, str);
57 }
58
59 int
60 Aconv(Fmt *fp)
61 {
62         char *s;
63         int a;
64
65         a = va_arg(fp->args, int);
66         s = "???";
67         if(a >= AXXX && a <= AEND)
68                 s = anames[a];
69         return fmtstrcpy(fp, s);
70 }
71
72 int
73 Dconv(Fmt *fp)
74 {
75         char str[STRINGSZ];
76         Adr *a;
77
78         a = va_arg(fp->args, Adr*);
79         switch(a->type) {
80
81         default:
82                 snprint(str, sizeof str, "GOK-type(%d)", a->type);
83                 break;
84
85         case D_NONE:
86                 str[0] = 0;
87                 if(a->name != D_NONE || a->reg != NREG || a->sym != S)
88                         snprint(str, sizeof str, "%N(R%d)(NONE)", a, a->reg);
89                 break;
90
91         case D_CONST:
92                 if(a->reg != NREG)
93                         snprint(str, sizeof str, "$%N(R%d)", a, a->reg);
94                 else
95                         snprint(str, sizeof str, "$%N", a);
96                 break;
97
98         case D_OREG:
99                 if(a->reg != NREG)
100                         snprint(str, sizeof str, "%N(R%d)", a, a->reg);
101                 else
102                         snprint(str, sizeof str, "%N", a);
103                 break;
104
105         case D_REG:
106                 snprint(str, sizeof str, "R%d", a->reg);
107                 if(a->name != D_NONE || a->sym != S)
108                         snprint(str, sizeof str, "%N(R%d)(REG)", a, a->reg);
109                 break;
110
111         case D_FREG:
112                 snprint(str, sizeof str, "F%d", a->reg);
113                 if(a->name != D_NONE || a->sym != S)
114                         snprint(str, sizeof str, "%N(F%d)(REG)", a, a->reg);
115                 break;
116
117         case D_CREG:
118                 snprint(str, sizeof str, "C%d", a->reg);
119                 if(a->name != D_NONE || a->sym != S)
120                         snprint(str, sizeof str, "%N(C%d)(REG)", a, a->reg);
121                 break;
122
123         case D_BRANCH:
124                 snprint(str, sizeof str, "%ld(PC)", a->offset-pc);
125                 break;
126
127         case D_FCONST:
128                 snprint(str, sizeof str, "$%.17e", a->dval);
129                 break;
130
131         case D_SCONST:
132                 snprint(str, sizeof str, "$\"%S\"", a->sval);
133                 break;
134         }
135         return fmtstrcpy(fp, str);
136 }
137
138 int
139 Sconv(Fmt *fp)
140 {
141         int i, c;
142         char str[STRINGSZ], *p, *a;
143
144         a = va_arg(fp->args, char*);
145         p = str;
146         for(i=0; i<NSNAME; i++) {
147                 c = a[i] & 0xff;
148                 if(c >= 'a' && c <= 'z' ||
149                    c >= 'A' && c <= 'Z' ||
150                    c >= '0' && c <= '9' ||
151                    c == ' ' || c == '%') {
152                         *p++ = c;
153                         continue;
154                 }
155                 *p++ = '\\';
156                 switch(c) {
157                 case 0:
158                         *p++ = 'z';
159                         continue;
160                 case '\\':
161                 case '"':
162                         *p++ = c;
163                         continue;
164                 case '\n':
165                         *p++ = 'n';
166                         continue;
167                 case '\t':
168                         *p++ = 't';
169                         continue;
170                 case '\r':
171                         *p++ = 'r';
172                         continue;
173                 case '\f':
174                         *p++ = 'f';
175                         continue;
176                 }
177                 *p++ = (c>>6) + '0';
178                 *p++ = ((c>>3) & 7) + '0';
179                 *p++ = (c & 7) + '0';
180         }
181         *p = 0;
182         return fmtstrcpy(fp, str);
183 }
184
185 int
186 Nconv(Fmt *fp)
187 {
188         char str[STRINGSZ];
189         Adr *a;
190         Sym *s;
191
192         a = va_arg(fp->args, Adr*);
193         s = a->sym;
194         if(s == S) {
195                 snprint(str, sizeof str, "%ld", a->offset);
196                 goto out;
197         }
198         switch(a->name) {
199         default:
200                 snprint(str, sizeof str, "GOK-name(%d)", a->name);
201                 break;
202
203         case D_EXTERN:
204                 snprint(str, sizeof str, "%s+%ld(SB)", s->name, a->offset);
205                 break;
206
207         case D_STATIC:
208                 snprint(str, sizeof str, "%s<>+%ld(SB)", s->name, a->offset);
209                 break;
210
211         case D_AUTO:
212                 snprint(str, sizeof str, "%s-%ld(SP)", s->name, -a->offset);
213                 break;
214
215         case D_PARAM:
216                 snprint(str, sizeof str, "%s+%ld(FP)", s->name, a->offset);
217                 break;
218         }
219 out:
220         return fmtstrcpy(fp, str);
221 }