1 /* -----------------------------------------------------------------------------
2 * Bytecode disassembler
4 * Copyright (c) 1994-2002.
6 * $RCSfile: Disassembler.c,v $
8 * $Date: 2004/09/03 15:28:19 $
9 * ---------------------------------------------------------------------------*/
13 #include "PosixSource.h"
21 #include "Bytecodes.h"
23 #include "Disassembler.h"
24 #include "Interpreter.h"
26 /* --------------------------------------------------------------------------
28 * ------------------------------------------------------------------------*/
31 disInstr ( StgBCO *bco, int pc )
36 StgWord16* instrs = (StgWord16*)(bco->instrs->payload);
38 StgArrWords* literal_arr = bco->literals;
39 StgWord* literals = (StgWord*)(&literal_arr->payload[0]);
41 StgMutArrPtrs* ptrs_arr = bco->ptrs;
42 StgPtr* ptrs = (StgPtr*)(&ptrs_arr->payload[0]);
47 debugBelch ("BRK_FUN " ); printPtr( ptrs[instrs[pc]] );
48 debugBelch (" %d ", instrs[pc+1]); printPtr( ptrs[instrs[pc+2]] ); debugBelch("\n" );
52 debugBelch("SWIZZLE stkoff %d by %d\n",
53 instrs[pc], (signed int)instrs[pc+1]);
56 debugBelch("CCALL marshaller at 0x%lx\n",
57 literals[instrs[pc]] );
60 debugBelch("STKCHECK %d\n", instrs[pc] );
63 debugBelch("PUSH_L %d\n", instrs[pc] );
66 debugBelch("PUSH_LL %d %d\n", instrs[pc], instrs[pc+1] );
69 debugBelch("PUSH_LLL %d %d %d\n", instrs[pc], instrs[pc+1],
73 debugBelch("PUSH_G " ); printPtr( ptrs[instrs[pc]] );
78 debugBelch("PUSH_ALTS " ); printPtr( ptrs[instrs[pc]] );
82 debugBelch("PUSH_ALTS_P " ); printPtr( ptrs[instrs[pc]] );
86 debugBelch("PUSH_ALTS_N " ); printPtr( ptrs[instrs[pc]] );
90 debugBelch("PUSH_ALTS_F " ); printPtr( ptrs[instrs[pc]] );
94 debugBelch("PUSH_ALTS_D " ); printPtr( ptrs[instrs[pc]] );
98 debugBelch("PUSH_ALTS_L " ); printPtr( ptrs[instrs[pc]] );
101 case bci_PUSH_ALTS_V:
102 debugBelch("PUSH_ALTS_V " ); printPtr( ptrs[instrs[pc]] );
107 debugBelch("PUSH_UBX ");
108 for (i = 0; i < instrs[pc+1]; i++)
109 debugBelch("0x%lx ", literals[i + instrs[pc]] );
112 case bci_PUSH_APPLY_N:
113 debugBelch("PUSH_APPLY_N\n");
115 case bci_PUSH_APPLY_V:
116 debugBelch("PUSH_APPLY_V\n");
118 case bci_PUSH_APPLY_F:
119 debugBelch("PUSH_APPLY_F\n");
121 case bci_PUSH_APPLY_D:
122 debugBelch("PUSH_APPLY_D\n");
124 case bci_PUSH_APPLY_L:
125 debugBelch("PUSH_APPLY_L\n");
127 case bci_PUSH_APPLY_P:
128 debugBelch("PUSH_APPLY_P\n");
130 case bci_PUSH_APPLY_PP:
131 debugBelch("PUSH_APPLY_PP\n");
133 case bci_PUSH_APPLY_PPP:
134 debugBelch("PUSH_APPLY_PPP\n");
136 case bci_PUSH_APPLY_PPPP:
137 debugBelch("PUSH_APPLY_PPPP\n");
139 case bci_PUSH_APPLY_PPPPP:
140 debugBelch("PUSH_APPLY_PPPPP\n");
142 case bci_PUSH_APPLY_PPPPPP:
143 debugBelch("PUSH_APPLY_PPPPPP\n");
146 debugBelch("SLIDE %d down by %d\n", instrs[pc], instrs[pc+1] );
149 debugBelch("ALLOC_AP %d words\n", instrs[pc] );
152 debugBelch("ALLOC_PAP %d arity, %d words\n",
153 instrs[pc], instrs[pc+1] );
156 debugBelch("MKAP %d words, %d stkoff\n", instrs[pc+1],
160 debugBelch("MKPAP %d words, %d stkoff\n", instrs[pc+1],
164 debugBelch("UNPACK %d\n", instrs[pc] );
167 debugBelch("PACK %d words with itbl ", instrs[pc+1] );
168 printPtr( (StgPtr)literals[instrs[pc]] );
173 debugBelch("TESTLT_I %ld, fail to %d\n", literals[instrs[pc]],
177 debugBelch("TESTEQ_I %ld, fail to %d\n", literals[instrs[pc]],
182 debugBelch("TESTLT_F %ld, fail to %d\n", literals[instrs[pc]],
186 debugBelch("TESTEQ_F %ld, fail to %d\n", literals[instrs[pc]],
191 debugBelch("TESTLT_D %ld, fail to %d\n", literals[instrs[pc]],
195 debugBelch("TESTEQ_D %ld, fail to %d\n", literals[instrs[pc]],
200 debugBelch("TESTLT_P %d, fail to %d\n", instrs[pc],
204 debugBelch("TESTEQ_P %d, fail to %d\n", instrs[pc],
208 debugBelch("CASEFAIL\n" );
211 debugBelch("JMP to %d\n", instrs[pc]);
215 debugBelch("ENTER\n");
219 debugBelch("RETURN\n" );
222 debugBelch("RETURN_P\n" );
225 debugBelch("RETURN_N\n" );
228 debugBelch("RETURN_F\n" );
231 debugBelch("RETURN_D\n" );
234 debugBelch("RETURN_L\n" );
237 debugBelch("RETURN_V\n" );
241 barf("disInstr: unknown opcode %u", (unsigned int) instr);
247 /* Something of a kludge .. how do we know where the end of the insn
248 array is, since it isn't recorded anywhere? Answer: the first
249 short is the number of bytecodes which follow it.
250 See ByteCodeGen.linkBCO.insns_arr for construction ...
252 void disassemble( StgBCO *bco )
255 StgWord16* instrs = (StgWord16*)(bco->instrs->payload);
256 StgMutArrPtrs* ptrs = bco->ptrs;
257 nat nbcs = (int)instrs[0];
260 debugBelch("BCO\n" );
263 debugBelch("\t%2d: ", pc );
264 pc = disInstr ( bco, pc );
267 debugBelch("INSTRS:\n " );
269 for (i = 0; i < nbcs; i++) {
270 debugBelch("%3d ", (int)instrs[i] );
272 if (j == 0) { j = 16; debugBelch("\n "); };
276 debugBelch("PTRS:\n " );
278 for (i = 0; i < ptrs->ptrs; i++) {
279 debugBelch("%8p ", ptrs->payload[i] );
281 if (j == 0) { j = 8; debugBelch("\n "); };
286 ASSERT(pc == nbcs+1);