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("SWIZZLE stkoff %d by %d\n",
48 instrs[pc], (signed int)instrs[pc+1]);
51 debugBelch("CCALL marshaller at 0x%lx\n",
52 literals[instrs[pc]] );
55 debugBelch("STKCHECK %d\n", instrs[pc] );
58 debugBelch("PUSH_L %d\n", instrs[pc] );
61 debugBelch("PUSH_LL %d %d\n", instrs[pc], instrs[pc+1] );
64 debugBelch("PUSH_LLL %d %d %d\n", instrs[pc], instrs[pc+1],
68 debugBelch("PUSH_G " ); printPtr( ptrs[instrs[pc]] );
73 debugBelch("PUSH_ALTS " ); printPtr( ptrs[instrs[pc]] );
77 debugBelch("PUSH_ALTS_P " ); printPtr( ptrs[instrs[pc]] );
81 debugBelch("PUSH_ALTS_N " ); printPtr( ptrs[instrs[pc]] );
85 debugBelch("PUSH_ALTS_F " ); printPtr( ptrs[instrs[pc]] );
89 debugBelch("PUSH_ALTS_D " ); printPtr( ptrs[instrs[pc]] );
93 debugBelch("PUSH_ALTS_L " ); printPtr( ptrs[instrs[pc]] );
97 debugBelch("PUSH_ALTS_V " ); printPtr( ptrs[instrs[pc]] );
102 debugBelch("PUSH_UBX ");
103 for (i = 0; i < instrs[pc+1]; i++)
104 debugBelch("0x%lx ", literals[i + instrs[pc]] );
107 case bci_PUSH_APPLY_N:
108 debugBelch("PUSH_APPLY_N\n");
110 case bci_PUSH_APPLY_V:
111 debugBelch("PUSH_APPLY_V\n");
113 case bci_PUSH_APPLY_F:
114 debugBelch("PUSH_APPLY_F\n");
116 case bci_PUSH_APPLY_D:
117 debugBelch("PUSH_APPLY_D\n");
119 case bci_PUSH_APPLY_L:
120 debugBelch("PUSH_APPLY_L\n");
122 case bci_PUSH_APPLY_P:
123 debugBelch("PUSH_APPLY_P\n");
125 case bci_PUSH_APPLY_PP:
126 debugBelch("PUSH_APPLY_PP\n");
128 case bci_PUSH_APPLY_PPP:
129 debugBelch("PUSH_APPLY_PPP\n");
131 case bci_PUSH_APPLY_PPPP:
132 debugBelch("PUSH_APPLY_PPPP\n");
134 case bci_PUSH_APPLY_PPPPP:
135 debugBelch("PUSH_APPLY_PPPPP\n");
137 case bci_PUSH_APPLY_PPPPPP:
138 debugBelch("PUSH_APPLY_PPPPPP\n");
141 debugBelch("SLIDE %d down by %d\n", instrs[pc], instrs[pc+1] );
144 debugBelch("ALLOC_AP %d words\n", instrs[pc] );
147 debugBelch("ALLOC_PAP %d arity, %d words\n",
148 instrs[pc], instrs[pc+1] );
151 debugBelch("MKAP %d words, %d stkoff\n", instrs[pc+1],
155 debugBelch("MKPAP %d words, %d stkoff\n", instrs[pc+1],
159 debugBelch("UNPACK %d\n", instrs[pc] );
162 debugBelch("PACK %d words with itbl ", instrs[pc+1] );
163 printPtr( (StgPtr)literals[instrs[pc]] );
168 debugBelch("TESTLT_I %ld, fail to %d\n", literals[instrs[pc]],
172 debugBelch("TESTEQ_I %ld, fail to %d\n", literals[instrs[pc]],
177 debugBelch("TESTLT_F %ld, fail to %d\n", literals[instrs[pc]],
181 debugBelch("TESTEQ_F %ld, fail to %d\n", literals[instrs[pc]],
186 debugBelch("TESTLT_D %ld, fail to %d\n", literals[instrs[pc]],
190 debugBelch("TESTEQ_D %ld, fail to %d\n", literals[instrs[pc]],
195 debugBelch("TESTLT_P %d, fail to %d\n", instrs[pc],
199 debugBelch("TESTEQ_P %d, fail to %d\n", instrs[pc],
203 debugBelch("CASEFAIL\n" );
206 debugBelch("JMP to %d\n", instrs[pc]);
210 debugBelch("ENTER\n");
214 debugBelch("RETURN\n" );
217 debugBelch("RETURN_P\n" );
220 debugBelch("RETURN_N\n" );
223 debugBelch("RETURN_F\n" );
226 debugBelch("RETURN_D\n" );
229 debugBelch("RETURN_L\n" );
232 debugBelch("RETURN_V\n" );
236 barf("disInstr: unknown opcode %u", (unsigned int) instr);
242 /* Something of a kludge .. how do we know where the end of the insn
243 array is, since it isn't recorded anywhere? Answer: the first
244 short is the number of bytecodes which follow it.
245 See ByteCodeGen.linkBCO.insns_arr for construction ...
247 void disassemble( StgBCO *bco )
250 StgWord16* instrs = (StgWord16*)(bco->instrs->payload);
251 StgMutArrPtrs* ptrs = bco->ptrs;
252 nat nbcs = (int)instrs[0];
255 debugBelch("BCO\n" );
258 debugBelch("\t%2d: ", pc );
259 pc = disInstr ( bco, pc );
262 debugBelch("INSTRS:\n " );
264 for (i = 0; i < nbcs; i++) {
265 debugBelch("%3d ", (int)instrs[i] );
267 if (j == 0) { j = 16; debugBelch("\n "); };
271 debugBelch("PTRS:\n " );
273 for (i = 0; i < ptrs->ptrs; i++) {
274 debugBelch("%8p ", ptrs->payload[i] );
276 if (j == 0) { j = 8; debugBelch("\n "); };
281 ASSERT(pc == nbcs+1);