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"
22 #include "Bytecodes.h"
24 #include "Disassembler.h"
25 #include "Interpreter.h"
27 /* --------------------------------------------------------------------------
29 * ------------------------------------------------------------------------*/
32 disInstr ( StgBCO *bco, int pc )
37 StgWord16* instrs = (StgWord16*)(bco->instrs->payload);
39 StgArrWords* literal_arr = bco->literals;
40 StgWord* literals = (StgWord*)(&literal_arr->payload[0]);
42 StgMutArrPtrs* ptrs_arr = bco->ptrs;
43 StgPtr* ptrs = (StgPtr*)(&ptrs_arr->payload[0]);
45 StgArrWords* itbls_arr = bco->itbls;
46 StgInfoTable** itbls = (StgInfoTable**)(&itbls_arr->payload[0]);
51 debugBelch("SWIZZLE stkoff %d by %d\n",
52 instrs[pc], (signed int)instrs[pc+1]);
55 debugBelch("CCALL marshaller at 0x%lx\n",
56 literals[instrs[pc]] );
59 debugBelch("STKCHECK %d\n", instrs[pc] );
62 debugBelch("PUSH_L %d\n", instrs[pc] );
65 debugBelch("PUSH_LL %d %d\n", instrs[pc], instrs[pc+1] );
68 debugBelch("PUSH_LLL %d %d %d\n", instrs[pc], instrs[pc+1],
72 debugBelch("PUSH_G " ); printPtr( ptrs[instrs[pc]] );
77 debugBelch("PUSH_ALTS " ); printPtr( ptrs[instrs[pc]] );
81 debugBelch("PUSH_ALTS_P " ); printPtr( ptrs[instrs[pc]] );
85 debugBelch("PUSH_ALTS_N " ); printPtr( ptrs[instrs[pc]] );
89 debugBelch("PUSH_ALTS_F " ); printPtr( ptrs[instrs[pc]] );
93 debugBelch("PUSH_ALTS_D " ); printPtr( ptrs[instrs[pc]] );
97 debugBelch("PUSH_ALTS_L " ); printPtr( ptrs[instrs[pc]] );
100 case bci_PUSH_ALTS_V:
101 debugBelch("PUSH_ALTS_V " ); printPtr( ptrs[instrs[pc]] );
106 debugBelch("PUSH_UBX ");
107 for (i = 0; i < instrs[pc+1]; i++)
108 debugBelch("0x%lx ", literals[i + instrs[pc]] );
111 case bci_PUSH_APPLY_N:
112 debugBelch("PUSH_APPLY_N\n");
114 case bci_PUSH_APPLY_V:
115 debugBelch("PUSH_APPLY_V\n");
117 case bci_PUSH_APPLY_F:
118 debugBelch("PUSH_APPLY_F\n");
120 case bci_PUSH_APPLY_D:
121 debugBelch("PUSH_APPLY_D\n");
123 case bci_PUSH_APPLY_L:
124 debugBelch("PUSH_APPLY_L\n");
126 case bci_PUSH_APPLY_P:
127 debugBelch("PUSH_APPLY_P\n");
129 case bci_PUSH_APPLY_PP:
130 debugBelch("PUSH_APPLY_PP\n");
132 case bci_PUSH_APPLY_PPP:
133 debugBelch("PUSH_APPLY_PPP\n");
135 case bci_PUSH_APPLY_PPPP:
136 debugBelch("PUSH_APPLY_PPPP\n");
138 case bci_PUSH_APPLY_PPPPP:
139 debugBelch("PUSH_APPLY_PPPPP\n");
141 case bci_PUSH_APPLY_PPPPPP:
142 debugBelch("PUSH_APPLY_PPPPPP\n");
145 debugBelch("SLIDE %d down by %d\n", instrs[pc], instrs[pc+1] );
148 debugBelch("ALLOC_AP %d words\n", instrs[pc] );
151 debugBelch("ALLOC_PAP %d arity, %d words\n",
152 instrs[pc], instrs[pc+1] );
155 debugBelch("MKAP %d words, %d stkoff\n", instrs[pc+1],
159 debugBelch("MKPAP %d words, %d stkoff\n", instrs[pc+1],
163 debugBelch("UNPACK %d\n", instrs[pc] );
166 debugBelch("PACK %d words with itbl ", instrs[pc+1] );
167 printPtr( (StgPtr)itbls[instrs[pc]] );
172 debugBelch("TESTLT_I %ld, fail to %d\n", literals[instrs[pc]],
176 debugBelch("TESTEQ_I %ld, fail to %d\n", literals[instrs[pc]],
181 debugBelch("TESTLT_F %ld, fail to %d\n", literals[instrs[pc]],
185 debugBelch("TESTEQ_F %ld, fail to %d\n", literals[instrs[pc]],
190 debugBelch("TESTLT_D %ld, fail to %d\n", literals[instrs[pc]],
194 debugBelch("TESTEQ_D %ld, fail to %d\n", literals[instrs[pc]],
199 debugBelch("TESTLT_P %d, fail to %d\n", instrs[pc],
203 debugBelch("TESTEQ_P %d, fail to %d\n", instrs[pc],
207 debugBelch("CASEFAIL\n" );
210 debugBelch("JMP to %d\n", instrs[pc]);
214 debugBelch("ENTER\n");
218 debugBelch("RETURN\n" );
221 debugBelch("RETURN_P\n" );
224 debugBelch("RETURN_N\n" );
227 debugBelch("RETURN_F\n" );
230 debugBelch("RETURN_D\n" );
233 debugBelch("RETURN_L\n" );
236 debugBelch("RETURN_V\n" );
240 barf("disInstr: unknown opcode %u", (unsigned int) instr);
246 /* Something of a kludge .. how do we know where the end of the insn
247 array is, since it isn't recorded anywhere? Answer: the first
248 short is the number of bytecodes which follow it.
249 See ByteCodeGen.linkBCO.insns_arr for construction ...
251 void disassemble( StgBCO *bco )
254 StgWord16* instrs = (StgWord16*)(bco->instrs->payload);
255 StgMutArrPtrs* ptrs = bco->ptrs;
256 nat nbcs = (int)instrs[0];
259 debugBelch("BCO\n" );
262 debugBelch("\t%2d: ", pc );
263 pc = disInstr ( bco, pc );
266 debugBelch("INSTRS:\n " );
268 for (i = 0; i < nbcs; i++) {
269 debugBelch("%3d ", (int)instrs[i] );
271 if (j == 0) { j = 16; debugBelch("\n "); };
275 debugBelch("PTRS:\n " );
277 for (i = 0; i < ptrs->ptrs; i++) {
278 debugBelch("%8p ", ptrs->payload[i] );
280 if (j == 0) { j = 8; debugBelch("\n "); };
285 ASSERT(pc == nbcs+1);