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] );
151 case bci_ALLOC_AP_NOUPD:
152 debugBelch("ALLOC_AP_NOUPD %d words\n", instrs[pc] );
155 debugBelch("ALLOC_PAP %d arity, %d words\n",
156 instrs[pc], instrs[pc+1] );
159 debugBelch("MKAP %d words, %d stkoff\n", instrs[pc+1],
163 debugBelch("MKPAP %d words, %d stkoff\n", instrs[pc+1],
167 debugBelch("UNPACK %d\n", instrs[pc] );
170 debugBelch("PACK %d words with itbl ", instrs[pc+1] );
171 printPtr( (StgPtr)literals[instrs[pc]] );
176 debugBelch("TESTLT_I %ld, fail to %d\n", literals[instrs[pc]],
180 debugBelch("TESTEQ_I %ld, fail to %d\n", literals[instrs[pc]],
185 debugBelch("TESTLT_F %ld, fail to %d\n", literals[instrs[pc]],
189 debugBelch("TESTEQ_F %ld, fail to %d\n", literals[instrs[pc]],
194 debugBelch("TESTLT_D %ld, fail to %d\n", literals[instrs[pc]],
198 debugBelch("TESTEQ_D %ld, fail to %d\n", literals[instrs[pc]],
203 debugBelch("TESTLT_P %d, fail to %d\n", instrs[pc],
207 debugBelch("TESTEQ_P %d, fail to %d\n", instrs[pc],
211 debugBelch("CASEFAIL\n" );
214 debugBelch("JMP to %d\n", instrs[pc]);
218 debugBelch("ENTER\n");
222 debugBelch("RETURN\n" );
225 debugBelch("RETURN_P\n" );
228 debugBelch("RETURN_N\n" );
231 debugBelch("RETURN_F\n" );
234 debugBelch("RETURN_D\n" );
237 debugBelch("RETURN_L\n" );
240 debugBelch("RETURN_V\n" );
244 barf("disInstr: unknown opcode %u", (unsigned int) instr);
250 /* Something of a kludge .. how do we know where the end of the insn
251 array is, since it isn't recorded anywhere? Answer: the first
252 short is the number of bytecodes which follow it.
253 See ByteCodeGen.linkBCO.insns_arr for construction ...
255 void disassemble( StgBCO *bco )
258 StgWord16* instrs = (StgWord16*)(bco->instrs->payload);
259 StgMutArrPtrs* ptrs = bco->ptrs;
260 nat nbcs = (int)instrs[0];
263 debugBelch("BCO\n" );
266 debugBelch("\t%2d: ", pc );
267 pc = disInstr ( bco, pc );
270 debugBelch("INSTRS:\n " );
272 for (i = 0; i < nbcs; i++) {
273 debugBelch("%3d ", (int)instrs[i] );
275 if (j == 0) { j = 16; debugBelch("\n "); };
279 debugBelch("PTRS:\n " );
281 for (i = 0; i < ptrs->ptrs; i++) {
282 debugBelch("%8p ", ptrs->payload[i] );
284 if (j == 0) { j = 8; debugBelch("\n "); };
289 ASSERT(pc == nbcs+1);