1 // cmd/9l/list.c from Vita Nuova.
3 // Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
4 // Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
5 // Portions Copyright © 1997-1999 Vita Nuova Limited
6 // Portions Copyright © 2000-2008 Vita Nuova Holdings Limited (www.vitanuova.com)
7 // Portions Copyright © 2004,2006 Bruce Ellis
8 // Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
9 // Revisions Copyright © 2000-2008 Lucent Technologies Inc. and others
10 // Portions Copyright © 2009 The Go Authors. All rights reserved.
12 // Permission is hereby granted, free of charge, to any person obtaining a copy
13 // of this software and associated documentation files (the "Software"), to deal
14 // in the Software without restriction, including without limitation the rights
15 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 // copies of the Software, and to permit persons to whom the Software is
17 // furnished to do so, subject to the following conditions:
19 // The above copyright notice and this permission notice shall be included in
20 // all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34 #include "../cmd/9l/9.out.h"
41 static int Aconv(Fmt*);
42 static int Dconv(Fmt*);
43 static int Pconv(Fmt*);
44 static int Rconv(Fmt*);
45 static int DSconv(Fmt*);
46 static int Mconv(Fmt*);
47 static int DRconv(Fmt*);
51 // %A int Opcodes (instruction mnemonics)
53 // %D Addr* Addresses (instruction operands)
54 // Flags: "%lD": seperate the high and low words of a constant by "-"
56 // %P Prog* Instructions
60 // %$ char* String constant addresses (for internal use only)
61 // %^ int C_* classes (for liblink internal use)
63 #pragma varargck type "$" char*
64 #pragma varargck type "M" Addr*
69 fmtinstall('A', Aconv);
70 fmtinstall('D', Dconv);
71 fmtinstall('P', Pconv);
72 fmtinstall('R', Rconv);
74 // for liblink internal use
75 fmtinstall('^', DRconv);
78 fmtinstall('$', DSconv);
79 fmtinstall('M', Mconv);
87 char str[STRINGSZ], *s;
91 p = va_arg(fp->args, Prog*);
95 if(fp->flags & FmtSharp) {
97 s += sprint(s, "%.5lld (%L) %A", p->pc, p->lineno, a);
98 if(p->from.type != D_NONE)
99 s += sprint(s, " from={%#D}", &p->from);
101 s += sprint(s, " reg=%d", p->reg);
102 if(p->from3.type != D_NONE)
103 s += sprint(s, " from3={%#D}", &p->from3);
104 if(p->to.type != D_NONE)
105 s += sprint(s, " to={%#D}", &p->to);
106 return fmtstrcpy(fp, str);
109 if(a == ADATA || a == AINIT || a == ADYNT)
110 sprint(str, "%.5lld (%L) %A %D/%d,%D", p->pc, p->lineno, a, &p->from, p->reg, &p->to);
111 else if(a == ATEXT) {
113 sprint(str, "%.5lld (%L) %A %D,%d,%lD", p->pc, p->lineno, a, &p->from, p->reg, &p->to);
115 sprint(str, "%.5lld (%L) %A %D,%lD", p->pc, p->lineno, a, &p->from, &p->to);
116 } else if(a == AGLOBL) {
118 sprint(str, "%.5lld (%L) %A %D,%d,%D", p->pc, p->lineno, a, &p->from, p->reg, &p->to);
120 sprint(str, "%.5lld (%L) %A %D,%D", p->pc, p->lineno, a, &p->from, &p->to);
123 if(p->mark & NOSCHED)
125 if(p->reg == NREG && p->from3.type == D_NONE)
126 sprint(s, "%.5lld (%L) %A %D,%D", p->pc, p->lineno, a, &p->from, &p->to);
128 if(a != ATEXT && p->from.type == D_OREG) {
129 sprint(s, "%.5lld (%L) %A %lld(R%d+R%d),%D", p->pc, p->lineno, a,
130 p->from.offset, p->from.reg, p->reg, &p->to);
132 if(p->to.type == D_OREG) {
133 sprint(s, "%.5lld (%L) %A %D,%lld(R%d+R%d)", p->pc, p->lineno, a,
134 &p->from, p->to.offset, p->to.reg, p->reg);
136 s += sprint(s, "%.5lld (%L) %A %D", p->pc, p->lineno, a, &p->from);
138 s += sprint(s, ",%c%d", p->from.type==D_FREG?'F':'R', p->reg);
139 if(p->from3.type != D_NONE)
140 s += sprint(s, ",%D", &p->from3);
141 sprint(s, ",%D", &p->to);
144 return fmtprint(fp, "%s # spadj=%d", str, p->spadj);
146 return fmtstrcpy(fp, str);
155 a = va_arg(fp->args, int);
157 if(a >= AXXX && a < ALAST)
159 return fmtstrcpy(fp, s);
169 a = va_arg(fp->args, Addr*);
171 if(fp->flags & FmtSharp) {
173 if(a->type == D_NONE) {
174 sprint(s, "type=NONE");
177 if(a->type >= 0 && a->type < D_LAST && dnames9[a->type] != nil)
178 s += sprint(s, "type=%s ", dnames9[a->type]);
180 s += sprint(s, "type=%d ", a->type);
181 if(a->name >= 0 && a->name < D_LAST && dnames9[a->name] != nil)
182 s += sprint(s, "name=%s ", dnames9[a->name]);
184 s += sprint(s, "name=%d ", a->name);
185 s += sprint(s, "offset=%ld etype=%E width=%d", a->offset, a->etype, a->width);
187 s += sprint(s, " class=%s", cnames9[a->class]);
189 s += sprint(s, " reg=%d", a->reg);
191 s += sprint(s, " sym=%s", a->sym->name);
192 if(a->type == D_BRANCH && a->u.branch != nil)
193 s += sprint(s, " branch=%.5lld", a->u.branch->pc);
197 if(fp->flags & FmtLong) {
198 if(a->type == D_CONST)
199 sprint(str, "$%d-%d", (int32)a->offset, (int32)(a->offset>>32));
201 // ATEXT dst is not constant
202 sprint(str, "!!%D", a);
209 sprint(str, "GOK-type(%d)", a->type);
214 if(a->name != D_NONE || a->reg != NREG || a->sym != nil)
215 sprint(str, "%M(R%d)(NONE)", a, a->reg);
221 sprint(str, "$%M(R%d)", a, a->reg);
223 sprint(str, "$%M", a);
228 sprint(str, "%M(R%d)", a, a->reg);
230 sprint(str, "%M", a);
234 sprint(str, "R%d", a->reg);
235 if(a->name != D_NONE || a->sym != nil)
236 sprint(str, "%M(R%d)(REG)", a, a->reg);
240 sprint(str, "F%d", a->reg);
241 if(a->name != D_NONE || a->sym != nil)
242 sprint(str, "%M(F%d)(REG)", a, a->reg);
249 sprint(str, "CR%d", a->reg);
250 if(a->name != D_NONE || a->sym != nil)
251 sprint(str, "%M(C%d)(REG)", a, a->reg);
255 if(a->name == D_NONE && a->sym == nil) {
256 switch((ulong)a->offset) {
257 case D_XER: sprint(str, "XER"); break;
258 case D_LR: sprint(str, "LR"); break;
259 case D_CTR: sprint(str, "CTR"); break;
260 default: sprint(str, "SPR(%lld)", a->offset); break;
264 sprint(str, "SPR-GOK(%d)", a->reg);
265 if(a->name != D_NONE || a->sym != nil)
266 sprint(str, "%M(SPR-GOK%d)(REG)", a, a->reg);
270 if(a->name == D_NONE && a->sym == nil) {
271 sprint(str, "DCR(%lld)", a->offset);
274 sprint(str, "DCR-GOK(%d)", a->reg);
275 if(a->name != D_NONE || a->sym != nil)
276 sprint(str, "%M(DCR-GOK%d)(REG)", a, a->reg);
280 sprint(str, "OPT(%d)", a->reg);
285 strcpy(str, "FPSCR");
287 sprint(str, "FPSCR(%d)", a->reg);
295 if(bigP->pcond != nil) {
298 // v -= INITTEXT-HEADR;
300 sprint(str, "%s+%.5ux(BRANCH)", a->sym->name, v);
302 sprint(str, "%.5ux(BRANCH)", v);
303 } else if(a->u.branch != nil)
304 sprint(str, "%lld", a->u.branch->pc);
305 else if(a->sym != nil)
306 sprint(str, "%s+%lld(APC)", a->sym->name, a->offset);
308 sprint(str, "%lld(APC)", a->offset);
312 //sprint(str, "$%lux-%lux", a->ieee.h, a->ieee.l);
313 sprint(str, "$%.17g", a->u.dval);
317 sprint(str, "$\"%$\"", a->u.sval);
322 return fmtstrcpy(fp, str);
333 a = va_arg(fp->args, Addr*);
337 // if((vlong)l != a->offset)
338 // sprint(str, "0x%llux", a->offset);
340 // sprint(str, "%lld", a->offset);
345 sprint(str, "GOK-name(%d)", a->name);
350 if((vlong)l != a->offset)
351 sprint(str, "0x%llux", a->offset);
353 sprint(str, "%lld", a->offset);
358 sprint(str, "%s+%lld(SB)", s->name, a->offset);
360 sprint(str, "%s(SB)", s->name);
364 sprint(str, "%s<>+%lld(SB)", s->name, a->offset);
369 sprint(str, "%lld(SP)", -a->offset);
371 sprint(str, "%s-%lld(SP)", s->name, -a->offset);
376 sprint(str, "%lld(FP)", a->offset);
378 sprint(str, "%s+%lld(FP)", s->name, a->offset);
382 return fmtstrcpy(fp, str);
391 r = va_arg(fp->args, int);
393 sprint(str, "r%d", r);
395 sprint(str, "f%d", r-NREG);
396 return fmtstrcpy(fp, str);
405 a = va_arg(fp->args, int);
407 if(a >= C_NONE && a <= C_NCLASS)
409 return fmtstrcpy(fp, s);
416 char str[STRINGSZ], *p, *a;
418 a = va_arg(fp->args, char*);
420 for(i=0; i<sizeof(int32); i++) {
422 if(c >= 'a' && c <= 'z' ||
423 c >= 'A' && c <= 'Z' ||
424 c >= '0' && c <= '9' ||
425 c == ' ' || c == '%') {
446 *p++ = ((c>>3) & 7) + '0';
447 *p++ = (c & 7) + '0';
450 return fmtstrcpy(fp, str);