2 /* -----------------------------------------------------------------------------
3 * Bytecode disassembler
5 * Copyright (c) 1994-1998.
7 * $RCSfile: Disassembler.c,v $
9 * $Date: 1999/11/01 18:19:40 $
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 disInt16 ( StgBCO *bco, InstrPtr pc, char* i )
51 StgInt x = bcoInstr16(bco,pc); pc+=2;
52 ASSERT(pc <= bco->n_instrs);
53 fprintf(stderr,"%s %d",i,x);
57 static InstrPtr disIntInt ( StgBCO *bco, InstrPtr pc, char* i )
59 StgInt x = bcoInstr(bco,pc++);
60 StgInt y = bcoInstr(bco,pc++);
61 fprintf(stderr,"%s %d %d",i,x,y);
65 static InstrPtr disIntInt16 ( StgBCO *bco, InstrPtr pc, char* i )
68 x = bcoInstr16(bco,pc); pc += 2;
69 y = bcoInstr16(bco,pc); pc += 2;
70 fprintf(stderr,"%s %d %d",i,x,y);
74 static InstrPtr disIntPC ( StgBCO *bco, InstrPtr pc, char* i )
78 x = bcoInstr(bco,pc++);
79 y = bcoInstr16(bco,pc); pc += 2;
80 fprintf(stderr,"%s %d %d",i,x,pc+y);
84 static InstrPtr disPC ( StgBCO *bco, InstrPtr pc, char* i )
86 StgWord y = bcoInstr16(bco,pc); pc += 2;
87 fprintf(stderr,"%s %d",i,pc+y);
91 static InstrPtr disInfo ( StgBCO *bco, InstrPtr pc, char* i )
93 StgInfoTable* info = bcoConstInfoPtr(bco,bcoInstr(bco,pc++));
94 /* ToDo: print contents of infotable */
95 fprintf(stderr,"%s ",i);
96 printPtr(stgCast(StgPtr,info));
100 static InstrPtr disConstPtr ( StgBCO *bco, InstrPtr pc, char* i )
102 StgInt o = bcoInstr(bco,pc++);
103 StgPtr x = bcoConstPtr(bco,o);
104 fprintf(stderr,"%s [%d]=",i,o);
105 printPtr(x); /* bad way to print it... */
109 static InstrPtr disConstPtr16 ( StgBCO *bco, InstrPtr pc, char* i )
113 o = bcoInstr16(bco,pc); pc += 2;
114 x = bcoConstPtr(bco,o);
115 fprintf(stderr,"%s [%d]=",i,o);
116 printPtr(x); /* bad way to print it... */
120 static InstrPtr disConstInt ( StgBCO *bco, InstrPtr pc, char* i )
122 StgInt x = bcoConstInt(bco,bcoInstr(bco,pc++));
123 fprintf(stderr,"%s %d (0x%x)",i,x,x);
127 static InstrPtr disConstInt16 ( StgBCO *bco, InstrPtr pc, char* i )
129 StgInt x = bcoConstInt(bco,bcoInstr16(bco,pc)); pc += 2;
130 fprintf(stderr,"%s %d (0x%x)",i,x,x);
134 static InstrPtr disConstAddr ( StgBCO *bco, InstrPtr pc, char* i )
136 StgAddr x = bcoConstAddr(bco,bcoInstr(bco,pc++));
137 fprintf(stderr,"%s ",i);
142 static InstrPtr disConstAddr16 ( StgBCO *bco, InstrPtr pc, char* i )
144 StgAddr x = bcoConstAddr(bco,bcoInstr16(bco,pc)); pc += 2;
145 fprintf(stderr,"%s ",i);
150 static InstrPtr disConstChar ( StgBCO *bco, InstrPtr pc, char* i )
152 StgChar x = bcoConstChar(bco,bcoInstr(bco,pc++));
154 fprintf(stderr,"%s '%c'",i,x); else
155 fprintf(stderr,"%s 0x%x",i,(int)x);
159 static InstrPtr disConstChar16 ( StgBCO *bco, InstrPtr pc, char* i )
161 StgChar x = bcoConstChar(bco,bcoInstr16(bco,pc)); pc += 2;
163 fprintf(stderr,"%s '%c'",i,x); else
164 fprintf(stderr,"%s 0x%x",i,(int)x);
168 static InstrPtr disConstFloat ( StgBCO *bco, InstrPtr pc, char* i )
170 StgFloat x = bcoConstFloat(bco,bcoInstr(bco,pc++));
171 fprintf(stderr,"%s %f",i,x);
175 static InstrPtr disConstFloat16 ( StgBCO *bco, InstrPtr pc, char* i )
177 StgFloat x = bcoConstFloat(bco,bcoInstr16(bco,pc)); pc += 2;
178 fprintf(stderr,"%s %f",i,x);
182 static InstrPtr disConstDouble ( StgBCO *bco, InstrPtr pc, char* i )
184 StgDouble x = bcoConstDouble(bco,bcoInstr(bco,pc++));
185 fprintf(stderr,"%s %f",i,x);
189 static InstrPtr disConstDouble16 ( StgBCO *bco, InstrPtr pc, char* i )
191 StgDouble x = bcoConstDouble(bco,bcoInstr16(bco,pc)); pc += 2;
192 fprintf(stderr,"%s %f",i,x);
196 InstrPtr disInstr( StgBCO *bco, InstrPtr pc )
199 ASSERT(pc < bco->n_instrs);
200 in = bcoInstr(bco,pc++);
202 case i_INTERNAL_ERROR:
203 return disNone(bco,pc,"INTERNAL_ERROR");
205 return disNone(bco,pc,"PANIC");
207 return disInt(bco,pc,"STK_CHECK");
208 case i_STK_CHECK_big:
209 return disInt16(bco,pc,"STK_CHECK_big");
211 return disInt(bco,pc,"ARG_CHECK");
213 return disInt(bco,pc,"ALLOC_AP");
215 return disInt(bco,pc,"ALLOC_PAP");
217 return disInfo(bco,pc,"ALLOC_CONSTR");
219 return disIntInt(bco,pc,"MKAP");
221 return disIntInt16(bco,pc,"MKAP_big");
223 return disIntInt(bco,pc,"MKPAP");
225 return disInt(bco,pc,"PACK");
227 return disIntInt(bco,pc,"SLIDE");
229 return disIntInt(bco,pc,"R_V");
231 return disIntInt(bco,pc,"R_V_E");
233 return disIntInt(bco,pc,"V_V");
235 return disIntInt(bco,pc,"S_E");
237 return disIntInt16(bco,pc,"SLIDE_big");
239 return disNone(bco,pc,"ENTER");
241 return disConstPtr(bco,pc,"RETADDR");
243 return disConstPtr16(bco,pc,"RETADDR_big");
245 return disIntPC(bco,pc,"TEST");
247 return disNone(bco,pc,"UNPACK");
249 return disInt(bco,pc,"VAR");
251 return disInt16(bco,pc,"VAR_big");
253 return disConstPtr(bco,pc,"CONST");
255 return disConstPtr16(bco,pc,"CONST_big");
258 return disNone(bco,pc,"VOID");
261 return disInt(bco,pc,"VAR_INT");
263 return disInt16(bco,pc,"VAR_INT_big");
265 return disConstInt(bco,pc,"CONST_INT");
266 case i_CONST_INT_big:
267 return disConstInt16(bco,pc,"CONST_INT_big");
269 return disNone(bco,pc,"PACK_INT");
271 return disNone(bco,pc,"UNPACK_INT");
273 return disPC(bco,pc,"TEST_INT");
275 case i_CONST_INTEGER:
276 return disConstAddr(bco,pc,"CONST_INTEGER");
277 case i_CONST_INTEGER_big:
278 return disConstAddr16(bco,pc,"CONST_INTEGER_big");
281 return disInt(bco,pc,"VAR_WORD");
283 return disConstInt(bco,pc,"CONST_WORD");
285 return disNone(bco,pc,"PACK_WORD");
287 return disNone(bco,pc,"UNPACK_WORD");
290 return disInt(bco,pc,"VAR_ADDR");
292 return disInt16(bco,pc,"VAR_ADDR_big");
294 return disConstAddr(bco,pc,"CONST_ADDR");
295 case i_CONST_ADDR_big:
296 return disConstAddr16(bco,pc,"CONST_ADDR_big");
298 return disNone(bco,pc,"PACK_ADDR");
300 return disNone(bco,pc,"UNPACK_ADDR");
303 return disInt(bco,pc,"VAR_CHAR");
305 return disInt16(bco,pc,"VAR_CHAR_big");
307 return disConstChar(bco,pc,"CONST_CHAR");
308 case i_CONST_CHAR_big:
309 return disConstChar16(bco,pc,"CONST_CHAR_big");
311 return disNone(bco,pc,"PACK_CHAR");
313 return disNone(bco,pc,"UNPACK_CHAR");
316 return disInt(bco,pc,"VAR_FLOAT");
317 case i_VAR_FLOAT_big:
318 return disInt16(bco,pc,"VAR_FLOAT_big");
320 return disConstFloat(bco,pc,"CONST_FLOAT");
321 case i_CONST_FLOAT_big:
322 return disConstFloat16(bco,pc,"CONST_FLOAT_big");
324 return disNone(bco,pc,"PACK_FLOAT");
326 return disNone(bco,pc,"UNPACK_FLOAT");
329 return disInt(bco,pc,"VAR_DOUBLE");
330 case i_VAR_DOUBLE_big:
331 return disInt16(bco,pc,"VAR_DOUBLE_big");
333 return disConstDouble(bco,pc,"CONST_DOUBLE");
334 case i_CONST_DOUBLE_big:
335 return disConstDouble16(bco,pc,"CONST_DOUBLE_big");
337 return disNone(bco,pc,"PACK_DOUBLE");
338 case i_UNPACK_DOUBLE:
339 return disNone(bco,pc,"UNPACK_DOUBLE");
342 return disInt(bco,pc,"VAR_STABLE");
344 return disNone(bco,pc,"PACK_STABLE");
345 case i_UNPACK_STABLE:
346 return disNone(bco,pc,"UNPACK_STABLE");
350 Primop1 op = bcoInstr(bco,pc++);
352 case i_INTERNAL_ERROR1:
353 return disNone(bco,pc,"INTERNAL_ERROR1");
355 return disNone(bco,pc,"i_pushseqframe");
356 case i_pushcatchframe:
357 return disNone(bco,pc,"i_pushcatchframe");
360 const AsmPrim* p = asmFindPrimop(i_PRIMOP1,op);
362 return disNone(bco,pc,p->name);
364 barf("Unrecognised primop1 %d\n",op);
370 Primop2 op = bcoInstr(bco,pc++);
372 case i_INTERNAL_ERROR2:
373 return disNone(bco,pc,"INTERNAL_ERROR2");
374 case i_ccall_ccall_Id:
375 return disNone(bco,pc,"ccall_ccall_Id");
376 case i_ccall_ccall_IO:
377 return disNone(bco,pc,"ccall_ccall_IO");
378 case i_ccall_stdcall_Id:
379 return disNone(bco,pc,"ccall_stdcall_Id");
380 case i_ccall_stdcall_IO:
381 return disNone(bco,pc,"ccall_stdcall_IO");
383 return disNone(bco,pc,"primRaise");
386 const AsmPrim* p = asmFindPrimop(i_PRIMOP2,op);
388 return disNone(bco,pc,p->name);
390 barf("Unrecognised primop2 %d\n",op);
395 barf("Unrecognised instruction %d\n",in);
399 void disassemble( StgBCO *bco, char* prefix )
402 int pcLim = bco->n_instrs;
403 ASSERT( get_itbl(bco)->type == BCO);
405 fprintf(stderr,"%s%d:\t",prefix,pc);
406 pc = disInstr(bco,pc);
407 fprintf(stderr,"\n");
410 ppStgExpr(bco->stgexpr);
411 fprintf(stderr, "\n");
414 fprintf(stderr, "\t(no associated tree)\n" );
417 #endif /* INTERPRETER */