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]);
44 StgArrWords* itbls_arr = bco->itbls;
45 StgInfoTable** itbls = (StgInfoTable**)(&itbls_arr->payload[0]);
50 debugBelch("SWIZZLE stkoff %d by %d\n",
51 instrs[pc], (signed int)instrs[pc+1]);
54 debugBelch("CCALL marshaller at 0x%lx\n",
55 literals[instrs[pc]] );
58 debugBelch("STKCHECK %d\n", instrs[pc] );
61 debugBelch("PUSH_L %d\n", instrs[pc] );
64 debugBelch("PUSH_LL %d %d\n", instrs[pc], instrs[pc+1] );
67 debugBelch("PUSH_LLL %d %d %d\n", instrs[pc], instrs[pc+1],
71 debugBelch("PUSH_G " ); printPtr( ptrs[instrs[pc]] );
76 debugBelch("PUSH_ALTS " ); printPtr( ptrs[instrs[pc]] );
80 debugBelch("PUSH_ALTS_P " ); printPtr( ptrs[instrs[pc]] );
84 debugBelch("PUSH_ALTS_N " ); printPtr( ptrs[instrs[pc]] );
88 debugBelch("PUSH_ALTS_F " ); printPtr( ptrs[instrs[pc]] );
92 debugBelch("PUSH_ALTS_D " ); printPtr( ptrs[instrs[pc]] );
96 debugBelch("PUSH_ALTS_L " ); printPtr( ptrs[instrs[pc]] );
100 debugBelch("PUSH_ALTS_V " ); printPtr( ptrs[instrs[pc]] );
105 debugBelch("PUSH_UBX ");
106 for (i = 0; i < instrs[pc+1]; i++)
107 debugBelch("0x%lx ", literals[i + instrs[pc]] );
110 case bci_PUSH_APPLY_N:
111 debugBelch("PUSH_APPLY_N\n");
113 case bci_PUSH_APPLY_V:
114 debugBelch("PUSH_APPLY_V\n");
116 case bci_PUSH_APPLY_F:
117 debugBelch("PUSH_APPLY_F\n");
119 case bci_PUSH_APPLY_D:
120 debugBelch("PUSH_APPLY_D\n");
122 case bci_PUSH_APPLY_L:
123 debugBelch("PUSH_APPLY_L\n");
125 case bci_PUSH_APPLY_P:
126 debugBelch("PUSH_APPLY_P\n");
128 case bci_PUSH_APPLY_PP:
129 debugBelch("PUSH_APPLY_PP\n");
131 case bci_PUSH_APPLY_PPP:
132 debugBelch("PUSH_APPLY_PPP\n");
134 case bci_PUSH_APPLY_PPPP:
135 debugBelch("PUSH_APPLY_PPPP\n");
137 case bci_PUSH_APPLY_PPPPP:
138 debugBelch("PUSH_APPLY_PPPPP\n");
140 case bci_PUSH_APPLY_PPPPPP:
141 debugBelch("PUSH_APPLY_PPPPPP\n");
144 debugBelch("SLIDE %d down by %d\n", instrs[pc], instrs[pc+1] );
147 debugBelch("ALLOC_AP %d words\n", instrs[pc] );
150 debugBelch("ALLOC_PAP %d arity, %d words\n",
151 instrs[pc], instrs[pc+1] );
154 debugBelch("MKAP %d words, %d stkoff\n", instrs[pc+1],
158 debugBelch("MKPAP %d words, %d stkoff\n", instrs[pc+1],
162 debugBelch("UNPACK %d\n", instrs[pc] );
165 debugBelch("PACK %d words with itbl ", instrs[pc+1] );
166 printPtr( (StgPtr)itbls[instrs[pc]] );
171 debugBelch("TESTLT_I %ld, fail to %d\n", literals[instrs[pc]],
175 debugBelch("TESTEQ_I %ld, fail to %d\n", literals[instrs[pc]],
180 debugBelch("TESTLT_F %ld, fail to %d\n", literals[instrs[pc]],
184 debugBelch("TESTEQ_F %ld, fail to %d\n", literals[instrs[pc]],
189 debugBelch("TESTLT_D %ld, fail to %d\n", literals[instrs[pc]],
193 debugBelch("TESTEQ_D %ld, fail to %d\n", literals[instrs[pc]],
198 debugBelch("TESTLT_P %d, fail to %d\n", instrs[pc],
202 debugBelch("TESTEQ_P %d, fail to %d\n", instrs[pc],
206 debugBelch("CASEFAIL\n" );
209 debugBelch("JMP to %d\n", instrs[pc]);
213 debugBelch("ENTER\n");
217 debugBelch("RETURN\n" );
220 debugBelch("RETURN_P\n" );
223 debugBelch("RETURN_N\n" );
226 debugBelch("RETURN_F\n" );
229 debugBelch("RETURN_D\n" );
232 debugBelch("RETURN_L\n" );
235 debugBelch("RETURN_V\n" );
239 barf("disInstr: unknown opcode %u", (unsigned int) instr);
245 /* Something of a kludge .. how do we know where the end of the insn
246 array is, since it isn't recorded anywhere? Answer: the first
247 short is the number of bytecodes which follow it.
248 See ByteCodeGen.linkBCO.insns_arr for construction ...
250 void disassemble( StgBCO *bco )
253 StgWord16* instrs = (StgWord16*)(bco->instrs->payload);
254 StgMutArrPtrs* ptrs = bco->ptrs;
255 nat nbcs = (int)instrs[0];
258 debugBelch("BCO\n" );
261 debugBelch("\t%2d: ", pc );
262 pc = disInstr ( bco, pc );
265 debugBelch("INSTRS:\n " );
267 for (i = 0; i < nbcs; i++) {
268 debugBelch("%3d ", (int)instrs[i] );
270 if (j == 0) { j = 16; debugBelch("\n "); };
274 debugBelch("PTRS:\n " );
276 for (i = 0; i < ptrs->ptrs; i++) {
277 debugBelch("%8p ", ptrs->payload[i] );
279 if (j == 0) { j = 8; debugBelch("\n "); };
284 ASSERT(pc == nbcs+1);