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 )
35 StgWord16* instrs = (StgWord16*)(bco->instrs->payload);
37 StgArrWords* literal_arr = bco->literals;
38 StgWord* literals = (StgWord*)(&literal_arr->payload[0]);
40 StgMutArrPtrs* ptrs_arr = bco->ptrs;
41 StgPtr* ptrs = (StgPtr*)(&ptrs_arr->payload[0]);
43 StgArrWords* itbls_arr = bco->itbls;
44 StgInfoTable** itbls = (StgInfoTable**)(&itbls_arr->payload[0]);
46 switch (instrs[pc++]) {
48 debugBelch("SWIZZLE stkoff %d by %d\n",
49 instrs[pc], (signed int)instrs[pc+1]);
52 debugBelch("CCALL marshaller at 0x%x\n",
53 literals[instrs[pc]] );
56 debugBelch("STKCHECK %d\n", instrs[pc] );
59 debugBelch("PUSH_L %d\n", instrs[pc] );
62 debugBelch("PUSH_LL %d %d\n", instrs[pc], instrs[pc+1] );
65 debugBelch("PUSH_LLL %d %d %d\n", instrs[pc], instrs[pc+1],
69 debugBelch("PUSH_G " ); printPtr( ptrs[instrs[pc]] );
74 debugBelch("PUSH_ALTS " ); printPtr( ptrs[instrs[pc]] );
78 debugBelch("PUSH_ALTS_P " ); printPtr( ptrs[instrs[pc]] );
82 debugBelch("PUSH_ALTS_N " ); printPtr( ptrs[instrs[pc]] );
86 debugBelch("PUSH_ALTS_F " ); printPtr( ptrs[instrs[pc]] );
90 debugBelch("PUSH_ALTS_D " ); printPtr( ptrs[instrs[pc]] );
94 debugBelch("PUSH_ALTS_L " ); printPtr( ptrs[instrs[pc]] );
98 debugBelch("PUSH_ALTS_V " ); printPtr( ptrs[instrs[pc]] );
103 debugBelch("PUSH_UBX ");
104 for (i = 0; i < instrs[pc+1]; i++)
105 debugBelch("0x%x ", literals[i + instrs[pc]] );
108 case bci_PUSH_APPLY_N:
109 debugBelch("PUSH_APPLY_N\n");
111 case bci_PUSH_APPLY_V:
112 debugBelch("PUSH_APPLY_V\n");
114 case bci_PUSH_APPLY_F:
115 debugBelch("PUSH_APPLY_F\n");
117 case bci_PUSH_APPLY_D:
118 debugBelch("PUSH_APPLY_D\n");
120 case bci_PUSH_APPLY_L:
121 debugBelch("PUSH_APPLY_L\n");
123 case bci_PUSH_APPLY_P:
124 debugBelch("PUSH_APPLY_P\n");
126 case bci_PUSH_APPLY_PP:
127 debugBelch("PUSH_APPLY_PP\n");
129 case bci_PUSH_APPLY_PPP:
130 debugBelch("PUSH_APPLY_PPP\n");
132 case bci_PUSH_APPLY_PPPP:
133 debugBelch("PUSH_APPLY_PPPP\n");
135 case bci_PUSH_APPLY_PPPPP:
136 debugBelch("PUSH_APPLY_PPPPP\n");
138 case bci_PUSH_APPLY_PPPPPP:
139 debugBelch("PUSH_APPLY_PPPPPP\n");
142 debugBelch("SLIDE %d down by %d\n", instrs[pc], instrs[pc+1] );
145 debugBelch("ALLOC_AP %d words\n", instrs[pc] );
148 debugBelch("ALLOC_PAP %d words, %d arity\n",
149 instrs[pc], instrs[pc+1] );
152 debugBelch("MKAP %d words, %d stkoff\n", instrs[pc+1],
156 debugBelch("UNPACK %d\n", instrs[pc] );
159 debugBelch("PACK %d words with itbl ", instrs[pc+1] );
160 printPtr( (StgPtr)itbls[instrs[pc]] );
165 debugBelch("TESTLT_I %d, fail to %d\n", literals[instrs[pc]],
169 debugBelch("TESTEQ_I %d, fail to %d\n", literals[instrs[pc]],
174 debugBelch("TESTLT_F %d, fail to %d\n", literals[instrs[pc]],
178 debugBelch("TESTEQ_F %d, fail to %d\n", literals[instrs[pc]],
183 debugBelch("TESTLT_D %d, fail to %d\n", literals[instrs[pc]],
187 debugBelch("TESTEQ_D %d, fail to %d\n", literals[instrs[pc]],
192 debugBelch("TESTLT_P %d, fail to %d\n", instrs[pc],
196 debugBelch("TESTEQ_P %d, fail to %d\n", instrs[pc],
200 debugBelch("CASEFAIL\n" );
203 debugBelch("JMP to %d\n", instrs[pc]);
207 debugBelch("ENTER\n");
211 debugBelch("RETURN\n" );
214 debugBelch("RETURN_P\n" );
217 debugBelch("RETURN_N\n" );
220 debugBelch("RETURN_F\n" );
223 debugBelch("RETURN_D\n" );
226 debugBelch("RETURN_L\n" );
229 debugBelch("RETURN_V\n" );
233 barf("disInstr: unknown opcode");
239 /* Something of a kludge .. how do we know where the end of the insn
240 array is, since it isn't recorded anywhere? Answer: the first
241 short is the number of bytecodes which follow it.
242 See ByteCodeGen.linkBCO.insns_arr for construction ...
244 void disassemble( StgBCO *bco )
247 StgWord16* instrs = (StgWord16*)(bco->instrs->payload);
248 StgMutArrPtrs* ptrs = bco->ptrs;
249 nat nbcs = (int)instrs[0];
252 debugBelch("BCO\n" );
255 debugBelch("\t%2d: ", pc );
256 pc = disInstr ( bco, pc );
259 debugBelch("INSTRS:\n " );
261 for (i = 0; i < nbcs; i++) {
262 debugBelch("%3d ", (int)instrs[i] );
264 if (j == 0) { j = 16; debugBelch("\n "); };
268 debugBelch("PTRS:\n " );
270 for (i = 0; i < ptrs->ptrs; i++) {
271 debugBelch("%8p ", ptrs->payload[i] );
273 if (j == 0) { j = 8; debugBelch("\n "); };
278 ASSERT(pc == nbcs+1);