1 /* -*- mode: hugs-c; -*- */
2 /* -----------------------------------------------------------------------------
3 * Bytecode disassembler
5 * Copyright (c) 1994-1998.
7 * $RCSfile: Disassembler.c,v $
9 * $Date: 1998/12/02 13:28:15 $
10 * ---------------------------------------------------------------------------*/
17 #include "Bytecodes.h"
18 #include "Assembler.h"
20 #include "Disassembler.h"
22 /* --------------------------------------------------------------------------
24 * ------------------------------------------------------------------------*/
26 static InstrPtr disNone ( StgBCO *bco, InstrPtr pc, char* i );
27 static InstrPtr disInt ( StgBCO *bco, InstrPtr pc, char* i );
28 static InstrPtr disIntInt ( StgBCO *bco, InstrPtr pc, char* i );
29 static InstrPtr disInfo ( StgBCO *bco, InstrPtr pc, char* i );
30 static InstrPtr disConstPtr ( StgBCO *bco, InstrPtr pc, char* i );
31 static InstrPtr disConstInt ( StgBCO *bco, InstrPtr pc, char* i );
32 static InstrPtr disConstChar ( StgBCO *bco, InstrPtr pc, char* i );
33 static InstrPtr disConstFloat ( StgBCO *bco, InstrPtr pc, char* i );
35 static InstrPtr disNone ( StgBCO *bco, InstrPtr pc, char* i )
37 fprintf(stderr,"%s",i);
41 static InstrPtr disInt ( StgBCO *bco, InstrPtr pc, char* i )
43 StgInt x = bcoInstr(bco,pc++);
44 ASSERT(pc < bco->n_instrs);
45 fprintf(stderr,"%s %d",i,x);
49 static InstrPtr disIntInt ( StgBCO *bco, InstrPtr pc, char* i )
51 StgInt x = bcoInstr(bco,pc++);
52 StgInt y = bcoInstr(bco,pc++);
53 fprintf(stderr,"%s %d %d",i,x,y);
57 static InstrPtr disIntPC ( StgBCO *bco, InstrPtr pc, char* i )
59 StgInt x = bcoInstr(bco,pc++);
60 StgWord y = bcoInstr(bco,pc++);
61 fprintf(stderr,"%s %d %d",i,x,pc+y);
65 static InstrPtr disPC ( StgBCO *bco, InstrPtr pc, char* i )
67 StgWord y = bcoInstr(bco,pc++);
68 fprintf(stderr,"%s %d",i,pc+y);
72 static InstrPtr disInfo ( StgBCO *bco, InstrPtr pc, char* i )
74 StgInfoTable* info = bcoConstInfoPtr(bco,bcoInstr(bco,pc++));
75 /* ToDo: print contents of infotable */
76 fprintf(stderr,"%s ",i);
77 printPtr(stgCast(StgPtr,info));
81 static InstrPtr disConstPtr ( StgBCO *bco, InstrPtr pc, char* i )
83 StgInt o = bcoInstr(bco,pc++);
84 StgPtr x = bcoConstPtr(bco,o);
85 fprintf(stderr,"%s [%d]=",i,o);
86 printPtr(x); /* bad way to print it... */
90 static InstrPtr disConst2Ptr ( StgBCO *bco, InstrPtr pc, char* i )
92 StgWord o1 = bcoInstr(bco,pc++);
93 StgWord o2 = bcoInstr(bco,pc++);
94 StgWord o = o1*256 + o2;
95 StgPtr x = bcoConstPtr(bco,o);
96 fprintf(stderr,"%s [%d]=",i,o);
97 printPtr(x); /* bad way to print it... */
101 static InstrPtr disConstInt ( StgBCO *bco, InstrPtr pc, char* i )
103 StgInt x = bcoConstInt(bco,bcoInstr(bco,pc++));
104 fprintf(stderr,"%s %d",i,x);
108 static InstrPtr disConstAddr ( StgBCO *bco, InstrPtr pc, char* i )
110 StgAddr x = bcoConstAddr(bco,bcoInstr(bco,pc++));
111 fprintf(stderr,"%s ",i);
116 static InstrPtr disConstChar ( StgBCO *bco, InstrPtr pc, char* i )
118 StgChar x = bcoConstChar(bco,bcoInstr(bco,pc++));
119 fprintf(stderr,"%s '%c'",i,x);
123 static InstrPtr disConstFloat ( StgBCO *bco, InstrPtr pc, char* i )
125 StgFloat x = bcoConstFloat(bco,bcoInstr(bco,pc++));
126 fprintf(stderr,"%s %f",i,x);
130 static InstrPtr disConstDouble ( StgBCO *bco, InstrPtr pc, char* i )
132 StgDouble x = bcoConstDouble(bco,bcoInstr(bco,pc++));
133 fprintf(stderr,"%s %f",i,x);
137 InstrPtr disInstr( StgBCO *bco, InstrPtr pc )
140 ASSERT(pc < bco->n_instrs);
141 in = bcoInstr(bco,pc++);
143 case i_INTERNAL_ERROR:
144 return disNone(bco,pc,"INTERNAL_ERROR");
146 return disNone(bco,pc,"PANIC");
148 return disInt(bco,pc,"HP_CHECK");
150 return disInt(bco,pc,"STK_CHECK");
152 return disInt(bco,pc,"ARG_CHECK");
154 return disInt(bco,pc,"ALLOC_AP");
156 return disInt(bco,pc,"ALLOC_PAP");
158 return disInfo(bco,pc,"ALLOC_CONSTR");
160 return disIntInt(bco,pc,"MKAP");
162 return disIntInt(bco,pc,"MKPAP");
164 return disInt(bco,pc,"PACK");
166 return disIntInt(bco,pc,"SLIDE");
168 return disNone(bco,pc,"ENTER");
170 return disConstPtr(bco,pc,"RETADDR");
172 return disIntPC(bco,pc,"TEST");
174 return disNone(bco,pc,"UNPACK");
176 return disInt(bco,pc,"VAR");
178 return disConstPtr(bco,pc,"CONST");
180 return disConst2Ptr(bco,pc,"CONST2");
183 return disNone(bco,pc,"VOID");
185 case i_RETURN_GENERIC:
186 return disNone(bco,pc,"RETURN_GENERIC");
189 return disInt(bco,pc,"VAR_INT");
191 return disConstInt(bco,pc,"CONST_INT");
193 return disNone(bco,pc,"RETURN_INT");
195 return disNone(bco,pc,"PACK_INT");
197 return disNone(bco,pc,"UNPACK_INT");
199 return disPC(bco,pc,"TEST_INT");
203 return disInt(bco,pc,"VAR_INT64");
205 return disConstInt(bco,pc,"CONST_INT64");
207 return disNone(bco,pc,"RETURN_INT64");
209 return disNone(bco,pc,"PACK_INT64");
211 return disNone(bco,pc,"UNPACK_INT64");
213 #ifdef PROVIDE_INTEGER
214 case i_CONST_INTEGER:
215 return disConstAddr(bco,pc,"CONST_INTEGER");
219 return disInt(bco,pc,"VAR_WORD");
221 return disConstInt(bco,pc,"CONST_WORD");
223 return disNone(bco,pc,"RETURN_WORD");
225 return disNone(bco,pc,"PACK_WORD");
227 return disNone(bco,pc,"UNPACK_WORD");
231 return disInt(bco,pc,"VAR_ADDR");
233 return disConstAddr(bco,pc,"CONST_ADDR");
235 return disNone(bco,pc,"RETURN_ADDR");
237 return disNone(bco,pc,"PACK_ADDR");
239 return disNone(bco,pc,"UNPACK_ADDR");
242 return disInt(bco,pc,"VAR_CHAR");
244 return disConstChar(bco,pc,"CONST_CHAR");
246 return disNone(bco,pc,"RETURN_CHAR");
248 return disNone(bco,pc,"PACK_CHAR");
250 return disNone(bco,pc,"UNPACK_CHAR");
253 return disInt(bco,pc,"VAR_FLOAT");
255 return disConstFloat(bco,pc,"CONST_FLOAT");
257 return disNone(bco,pc,"RETURN_FLOAT");
259 return disNone(bco,pc,"PACK_FLOAT");
261 return disNone(bco,pc,"UNPACK_FLOAT");
264 return disInt(bco,pc,"VAR_DOUBLE");
266 return disConstDouble(bco,pc,"CONST_DOUBLE");
267 case i_RETURN_DOUBLE:
268 return disNone(bco,pc,"RETURN_DOUBLE");
270 return disNone(bco,pc,"PACK_DOUBLE");
271 case i_UNPACK_DOUBLE:
272 return disNone(bco,pc,"UNPACK_DOUBLE");
274 #ifdef PROVIDE_STABLE
276 return disInt(bco,pc,"VAR_STABLE");
277 case i_RETURN_STABLE:
278 return disNone(bco,pc,"RETURN_STABLE");
280 return disNone(bco,pc,"PACK_STABLE");
281 case i_UNPACK_STABLE:
282 return disNone(bco,pc,"UNPACK_STABLE");
287 Primop1 op = bcoInstr(bco,pc++);
289 case i_INTERNAL_ERROR1:
290 return disNone(bco,pc,"INTERNAL_ERROR1");
293 const AsmPrim* p = asmFindPrimop(i_PRIMOP1,op);
295 return disNone(bco,pc,p->name);
297 barf("Unrecognised primop1 %d\n",op);
303 Primop2 op = bcoInstr(bco,pc++);
305 case i_INTERNAL_ERROR2:
306 return disNone(bco,pc,"INTERNAL_ERROR2");
308 return disNone(bco,pc,"ccall_Id");
310 return disNone(bco,pc,"ccall_IO");
313 const AsmPrim* p = asmFindPrimop(i_PRIMOP2,op);
315 return disNone(bco,pc,p->name);
317 barf("Unrecognised primop2 %d\n",op);
322 barf("Unrecognised instruction %d\n",in);
326 void disassemble( StgBCO *bco, char* prefix )
329 int pcLim = bco->n_instrs;
330 ASSERT( get_itbl(bco)->type == BCO);
332 fprintf(stderr,"%s%d:\t",prefix,pc);
333 pc = disInstr(bco,pc);
334 fprintf(stderr,"\n");
338 #endif /* INTERPRETER */