Fixed uninitialised FunBind fun_tick field
[ghc-hetmet.git] / rts / Disassembler.c
1 /* -----------------------------------------------------------------------------
2  * Bytecode disassembler
3  *
4  * Copyright (c) 1994-2002.
5  *
6  * $RCSfile: Disassembler.c,v $
7  * $Revision: 1.29 $
8  * $Date: 2004/09/03 15:28:19 $
9  * ---------------------------------------------------------------------------*/
10
11 #ifdef DEBUG
12
13 #include "PosixSource.h"
14 #include "Rts.h"
15 #include "RtsAPI.h"
16 #include "RtsUtils.h"
17 #include "Closures.h"
18 #include "TSO.h"
19 #include "Schedule.h"
20
21 #include "Bytecodes.h"
22 #include "Printer.h"
23 #include "Disassembler.h"
24 #include "Interpreter.h"
25
26 /* --------------------------------------------------------------------------
27  * Disassembler
28  * ------------------------------------------------------------------------*/
29
30 int
31 disInstr ( StgBCO *bco, int pc )
32 {
33    int i;
34    StgWord16 instr;
35
36    StgWord16*     instrs      = (StgWord16*)(bco->instrs->payload);
37
38    StgArrWords*   literal_arr = bco->literals;
39    StgWord*       literals    = (StgWord*)(&literal_arr->payload[0]);
40
41    StgMutArrPtrs* ptrs_arr    = bco->ptrs;
42    StgPtr*        ptrs        = (StgPtr*)(&ptrs_arr->payload[0]);
43
44    StgArrWords*   itbls_arr   = bco->itbls;
45    StgInfoTable** itbls       = (StgInfoTable**)(&itbls_arr->payload[0]);
46
47    instr = instrs[pc++];
48    switch (instr) {
49       case bci_SWIZZLE:
50          debugBelch("SWIZZLE stkoff %d by %d\n",
51                          instrs[pc], (signed int)instrs[pc+1]);
52          pc += 2; break;
53       case bci_CCALL:
54          debugBelch("CCALL    marshaller at 0x%lx\n", 
55                          literals[instrs[pc]] );
56          pc += 1; break;
57       case bci_STKCHECK: 
58          debugBelch("STKCHECK %d\n", instrs[pc] );
59          pc += 1; break;
60       case bci_PUSH_L: 
61          debugBelch("PUSH_L   %d\n", instrs[pc] );
62          pc += 1; break;
63       case bci_PUSH_LL:
64          debugBelch("PUSH_LL  %d %d\n", instrs[pc], instrs[pc+1] ); 
65          pc += 2; break;
66       case bci_PUSH_LLL:
67          debugBelch("PUSH_LLL %d %d %d\n", instrs[pc], instrs[pc+1], 
68                                                             instrs[pc+2] ); 
69          pc += 3; break;
70       case bci_PUSH_G:
71          debugBelch("PUSH_G   " ); printPtr( ptrs[instrs[pc]] );
72          debugBelch("\n" );
73          pc += 1; break;
74
75       case bci_PUSH_ALTS:
76          debugBelch("PUSH_ALTS  " ); printPtr( ptrs[instrs[pc]] );
77          debugBelch("\n");
78          pc += 1; break;
79       case bci_PUSH_ALTS_P:
80          debugBelch("PUSH_ALTS_P  " ); printPtr( ptrs[instrs[pc]] );
81          debugBelch("\n");
82          pc += 1; break;
83       case bci_PUSH_ALTS_N:
84          debugBelch("PUSH_ALTS_N  " ); printPtr( ptrs[instrs[pc]] );
85          debugBelch("\n");
86          pc += 1; break;
87       case bci_PUSH_ALTS_F:
88          debugBelch("PUSH_ALTS_F  " ); printPtr( ptrs[instrs[pc]] );
89          debugBelch("\n");
90          pc += 1; break;
91       case bci_PUSH_ALTS_D:
92          debugBelch("PUSH_ALTS_D  " ); printPtr( ptrs[instrs[pc]] );
93          debugBelch("\n");
94          pc += 1; break;
95       case bci_PUSH_ALTS_L:
96          debugBelch("PUSH_ALTS_L  " ); printPtr( ptrs[instrs[pc]] );
97          debugBelch("\n");
98          pc += 1; break;
99       case bci_PUSH_ALTS_V:
100          debugBelch("PUSH_ALTS_V  " ); printPtr( ptrs[instrs[pc]] );
101          debugBelch("\n");
102          pc += 1; break;
103
104       case bci_PUSH_UBX:
105          debugBelch("PUSH_UBX ");
106          for (i = 0; i < instrs[pc+1]; i++) 
107             debugBelch("0x%lx ", literals[i + instrs[pc]] );
108          debugBelch("\n");
109          pc += 2; break;
110       case bci_PUSH_APPLY_N:
111           debugBelch("PUSH_APPLY_N\n");
112           break;
113       case bci_PUSH_APPLY_V:
114           debugBelch("PUSH_APPLY_V\n");
115           break;
116       case bci_PUSH_APPLY_F:
117           debugBelch("PUSH_APPLY_F\n");
118           break;
119       case bci_PUSH_APPLY_D:
120           debugBelch("PUSH_APPLY_D\n");
121           break;
122       case bci_PUSH_APPLY_L:
123           debugBelch("PUSH_APPLY_L\n");
124           break;
125       case bci_PUSH_APPLY_P:
126           debugBelch("PUSH_APPLY_P\n");
127           break;
128       case bci_PUSH_APPLY_PP:
129           debugBelch("PUSH_APPLY_PP\n");
130           break;
131       case bci_PUSH_APPLY_PPP:
132           debugBelch("PUSH_APPLY_PPP\n");
133           break;
134       case bci_PUSH_APPLY_PPPP:
135           debugBelch("PUSH_APPLY_PPPP\n");
136           break;
137       case bci_PUSH_APPLY_PPPPP:
138           debugBelch("PUSH_APPLY_PPPPP\n");
139           break;
140       case bci_PUSH_APPLY_PPPPPP:
141           debugBelch("PUSH_APPLY_PPPPPP\n");
142           break;
143       case bci_SLIDE: 
144          debugBelch("SLIDE     %d down by %d\n", instrs[pc], instrs[pc+1] );
145          pc += 2; break;
146       case bci_ALLOC_AP:
147          debugBelch("ALLOC_AP  %d words\n", instrs[pc] );
148          pc += 1; break;
149       case bci_ALLOC_PAP:
150          debugBelch("ALLOC_PAP %d arity, %d words\n",
151                  instrs[pc], instrs[pc+1] );
152          pc += 2; break;
153       case bci_MKAP:
154          debugBelch("MKAP      %d words, %d stkoff\n", instrs[pc+1], 
155                                                            instrs[pc] );
156          pc += 2; break;
157       case bci_MKPAP:
158          debugBelch("MKPAP     %d words, %d stkoff\n", instrs[pc+1], 
159                                                            instrs[pc] );
160          pc += 2; break;
161       case bci_UNPACK:
162          debugBelch("UNPACK    %d\n", instrs[pc] );
163          pc += 1; break;
164       case bci_PACK:
165          debugBelch("PACK      %d words with itbl ", instrs[pc+1] );
166          printPtr( (StgPtr)itbls[instrs[pc]] );
167          debugBelch("\n");
168          pc += 2; break;
169
170       case bci_TESTLT_I:
171          debugBelch("TESTLT_I  %ld, fail to %d\n", literals[instrs[pc]],
172                                                       instrs[pc+1]);
173          pc += 2; break;
174       case bci_TESTEQ_I:
175          debugBelch("TESTEQ_I  %ld, fail to %d\n", literals[instrs[pc]],
176                                                       instrs[pc+1]);
177          pc += 2; break;
178
179       case bci_TESTLT_F:
180          debugBelch("TESTLT_F  %ld, fail to %d\n", literals[instrs[pc]],
181                                                       instrs[pc+1]);
182          pc += 2; break;
183       case bci_TESTEQ_F:
184          debugBelch("TESTEQ_F  %ld, fail to %d\n", literals[instrs[pc]],
185                                                       instrs[pc+1]);
186          pc += 2; break;
187
188       case bci_TESTLT_D:
189          debugBelch("TESTLT_D  %ld, fail to %d\n", literals[instrs[pc]],
190                                                       instrs[pc+1]);
191          pc += 2; break;
192       case bci_TESTEQ_D:
193          debugBelch("TESTEQ_D  %ld, fail to %d\n", literals[instrs[pc]],
194                                                       instrs[pc+1]);
195          pc += 2; break;
196
197       case bci_TESTLT_P:
198          debugBelch("TESTLT_P  %d, fail to %d\n", instrs[pc],
199                                                       instrs[pc+1]);
200          pc += 2; break;
201       case bci_TESTEQ_P:
202          debugBelch("TESTEQ_P  %d, fail to %d\n", instrs[pc],
203                                                       instrs[pc+1]);
204          pc += 2; break;
205       case bci_CASEFAIL: 
206          debugBelch("CASEFAIL\n" );
207          break;
208       case bci_JMP:
209          debugBelch("JMP to    %d\n", instrs[pc]);
210          pc += 1; break;
211
212       case bci_ENTER:
213          debugBelch("ENTER\n");
214          break;
215
216       case bci_RETURN:
217          debugBelch("RETURN\n" );
218          break;
219       case bci_RETURN_P:
220          debugBelch("RETURN_P\n" );
221          break;
222       case bci_RETURN_N:
223          debugBelch("RETURN_N\n" );
224          break;
225       case bci_RETURN_F:
226          debugBelch("RETURN_F\n" );
227          break;
228       case bci_RETURN_D:
229          debugBelch("RETURN_D\n" );
230          break;
231       case bci_RETURN_L:
232          debugBelch("RETURN_L\n" );
233          break;
234       case bci_RETURN_V:
235          debugBelch("RETURN_V\n" );
236          break;
237
238       default:
239          barf("disInstr: unknown opcode %u", (unsigned int) instr);
240    }
241    return pc;
242 }
243
244
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 ...  
249 */
250 void disassemble( StgBCO *bco )
251 {
252    nat i, j;
253    StgWord16*     instrs    = (StgWord16*)(bco->instrs->payload);
254    StgMutArrPtrs* ptrs      = bco->ptrs;
255    nat            nbcs      = (int)instrs[0];
256    nat            pc        = 1;
257
258    debugBelch("BCO\n" );
259    pc = 1;
260    while (pc <= nbcs) {
261       debugBelch("\t%2d:  ", pc );
262       pc = disInstr ( bco, pc );
263    }
264
265    debugBelch("INSTRS:\n   " );
266    j = 16;
267    for (i = 0; i < nbcs; i++) {
268       debugBelch("%3d ", (int)instrs[i] );
269       j--; 
270       if (j == 0) { j = 16; debugBelch("\n   "); };
271    }
272    debugBelch("\n");
273
274    debugBelch("PTRS:\n   " );
275    j = 8;
276    for (i = 0; i < ptrs->ptrs; i++) {
277       debugBelch("%8p ", ptrs->payload[i] );
278       j--; 
279       if (j == 0) { j = 8; debugBelch("\n   "); };
280    }
281    debugBelch("\n");
282
283    debugBelch("\n");
284    ASSERT(pc == nbcs+1);
285 }
286
287 #endif /* DEBUG */