2 /* -----------------------------------------------------------------------------
3 * Bytecode disassembler
5 * Copyright (c) 1994-1998.
7 * $RCSfile: Disassembler.c,v $
9 * $Date: 1999/03/09 14:51:23 $
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",i,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",i,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");
209 return disInt(bco,pc,"ARG_CHECK");
211 return disInt(bco,pc,"ALLOC_AP");
213 return disInt(bco,pc,"ALLOC_PAP");
215 return disInfo(bco,pc,"ALLOC_CONSTR");
217 return disIntInt(bco,pc,"MKAP");
219 return disIntInt16(bco,pc,"MKAP_big");
221 return disIntInt(bco,pc,"MKPAP");
223 return disInt(bco,pc,"PACK");
225 return disIntInt(bco,pc,"SLIDE");
227 return disIntInt16(bco,pc,"SLIDE_big");
229 return disNone(bco,pc,"ENTER");
231 return disConstPtr(bco,pc,"RETADDR");
233 return disConstPtr16(bco,pc,"RETADDR_big");
235 return disIntPC(bco,pc,"TEST");
237 return disNone(bco,pc,"UNPACK");
239 return disInt(bco,pc,"VAR");
241 return disInt16(bco,pc,"VAR_big");
243 return disConstPtr(bco,pc,"CONST");
245 return disConstPtr16(bco,pc,"CONST_big");
248 return disNone(bco,pc,"VOID");
249 case i_RETURN_GENERIC:
250 return disNone(bco,pc,"RETURN_GENERIC");
253 return disInt(bco,pc,"VAR_INT");
255 return disInt16(bco,pc,"VAR_INT_big");
257 return disConstInt(bco,pc,"CONST_INT");
258 case i_CONST_INT_big:
259 return disConstInt16(bco,pc,"CONST_INT_big");
261 return disNone(bco,pc,"RETURN_INT");
263 return disNone(bco,pc,"PACK_INT");
265 return disNone(bco,pc,"UNPACK_INT");
267 return disPC(bco,pc,"TEST_INT");
271 return disInt(bco,pc,"VAR_INT64");
273 return disConstInt(bco,pc,"CONST_INT64");
275 return disNone(bco,pc,"RETURN_INT64");
277 return disNone(bco,pc,"PACK_INT64");
279 return disNone(bco,pc,"UNPACK_INT64");
281 #ifdef PROVIDE_INTEGER
282 case i_CONST_INTEGER:
283 return disConstAddr(bco,pc,"CONST_INTEGER");
284 case i_CONST_INTEGER_big:
285 return disConstAddr16(bco,pc,"CONST_INTEGER_big");
289 return disInt(bco,pc,"VAR_WORD");
291 return disConstInt(bco,pc,"CONST_WORD");
293 return disNone(bco,pc,"RETURN_WORD");
295 return disNone(bco,pc,"PACK_WORD");
297 return disNone(bco,pc,"UNPACK_WORD");
301 return disInt(bco,pc,"VAR_ADDR");
303 return disInt16(bco,pc,"VAR_ADDR_big");
305 return disConstAddr(bco,pc,"CONST_ADDR");
306 case i_CONST_ADDR_big:
307 return disConstAddr16(bco,pc,"CONST_ADDR_big");
309 return disNone(bco,pc,"RETURN_ADDR");
311 return disNone(bco,pc,"PACK_ADDR");
313 return disNone(bco,pc,"UNPACK_ADDR");
316 return disInt(bco,pc,"VAR_CHAR");
318 return disInt16(bco,pc,"VAR_CHAR_big");
320 return disConstChar(bco,pc,"CONST_CHAR");
321 case i_CONST_CHAR_big:
322 return disConstChar16(bco,pc,"CONST_CHAR_big");
324 return disNone(bco,pc,"RETURN_CHAR");
326 return disNone(bco,pc,"PACK_CHAR");
328 return disNone(bco,pc,"UNPACK_CHAR");
331 return disInt(bco,pc,"VAR_FLOAT");
332 case i_VAR_FLOAT_big:
333 return disInt16(bco,pc,"VAR_FLOAT_big");
335 return disConstFloat(bco,pc,"CONST_FLOAT");
336 case i_CONST_FLOAT_big:
337 return disConstFloat16(bco,pc,"CONST_FLOAT_big");
339 return disNone(bco,pc,"RETURN_FLOAT");
341 return disNone(bco,pc,"PACK_FLOAT");
343 return disNone(bco,pc,"UNPACK_FLOAT");
346 return disInt(bco,pc,"VAR_DOUBLE");
347 case i_VAR_DOUBLE_big:
348 return disInt16(bco,pc,"VAR_DOUBLE_big");
350 return disConstDouble(bco,pc,"CONST_DOUBLE");
351 case i_CONST_DOUBLE_big:
352 return disConstDouble16(bco,pc,"CONST_DOUBLE_big");
353 case i_RETURN_DOUBLE:
354 return disNone(bco,pc,"RETURN_DOUBLE");
356 return disNone(bco,pc,"PACK_DOUBLE");
357 case i_UNPACK_DOUBLE:
358 return disNone(bco,pc,"UNPACK_DOUBLE");
360 #ifdef PROVIDE_STABLE
362 return disInt(bco,pc,"VAR_STABLE");
363 case i_RETURN_STABLE:
364 return disNone(bco,pc,"RETURN_STABLE");
366 return disNone(bco,pc,"PACK_STABLE");
367 case i_UNPACK_STABLE:
368 return disNone(bco,pc,"UNPACK_STABLE");
373 Primop1 op = bcoInstr(bco,pc++);
375 case i_INTERNAL_ERROR1:
376 return disNone(bco,pc,"INTERNAL_ERROR1");
378 return disNone(bco,pc,"i_pushseqframe");
379 case i_pushcatchframe:
380 return disNone(bco,pc,"i_pushcatchframe");
383 const AsmPrim* p = asmFindPrimop(i_PRIMOP1,op);
385 return disNone(bco,pc,p->name);
387 barf("Unrecognised primop1 %d\n",op);
393 Primop2 op = bcoInstr(bco,pc++);
395 case i_INTERNAL_ERROR2:
396 return disNone(bco,pc,"INTERNAL_ERROR2");
398 return disNone(bco,pc,"ccall_Id");
400 return disNone(bco,pc,"ccall_IO");
402 return disNone(bco,pc,"primRaise");
405 const AsmPrim* p = asmFindPrimop(i_PRIMOP2,op);
407 return disNone(bco,pc,p->name);
409 barf("Unrecognised primop2 %d\n",op);
414 barf("Unrecognised instruction %d\n",in);
418 void disassemble( StgBCO *bco, char* prefix )
421 int pcLim = bco->n_instrs;
422 ASSERT( get_itbl(bco)->type == BCO);
424 fprintf(stderr,"%s%d:\t",prefix,pc);
425 pc = disInstr(bco,pc);
426 fprintf(stderr,"\n");
429 ppStgExpr(bco->stgexpr);
430 fprintf(stderr, "\n");
433 fprintf(stderr, "\t(no associated tree)\n" );
436 #endif /* INTERPRETER */