summaryrefslogtreecommitdiff
path: root/rts/Disassembler.c
blob: b084a29b89705135d4d8ec5a6550cacc850178fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* -----------------------------------------------------------------------------
 * Bytecode disassembler
 *
 * Copyright (c) 1994-2002.
 *
 * $RCSfile: Disassembler.c,v $
 * $Revision: 1.29 $
 * $Date: 2004/09/03 15:28:19 $
 * ---------------------------------------------------------------------------*/

#ifdef DEBUG

#include "PosixSource.h"
#include "Rts.h"
#include "RtsAPI.h"
#include "RtsUtils.h"
#include "Closures.h"
#include "TSO.h"
#include "Schedule.h"

#include "Bytecodes.h"
#include "Printer.h"
#include "Disassembler.h"
#include "Interpreter.h"

/* --------------------------------------------------------------------------
 * Disassembler
 * ------------------------------------------------------------------------*/

int
disInstr ( StgBCO *bco, int pc )
{
   int i;

   StgWord16*     instrs      = (StgWord16*)(bco->instrs->payload);

   StgArrWords*   literal_arr = bco->literals;
   StgWord*       literals    = (StgWord*)(&literal_arr->payload[0]);

   StgMutArrPtrs* ptrs_arr    = bco->ptrs;
   StgPtr*        ptrs        = (StgPtr*)(&ptrs_arr->payload[0]);

   StgArrWords*   itbls_arr   = bco->itbls;
   StgInfoTable** itbls       = (StgInfoTable**)(&itbls_arr->payload[0]);

   switch (instrs[pc++]) {
      case bci_SWIZZLE:
         debugBelch("SWIZZLE stkoff %d by %d\n",
                         instrs[pc], (signed int)instrs[pc+1]);
         pc += 2; break;
      case bci_CCALL:
         debugBelch("CCALL    marshaller at 0x%x\n", 
                         literals[instrs[pc]] );
         pc += 1; break;
      case bci_STKCHECK: 
         debugBelch("STKCHECK %d\n", instrs[pc] );
         pc += 1; break;
      case bci_PUSH_L: 
         debugBelch("PUSH_L   %d\n", instrs[pc] );
         pc += 1; break;
      case bci_PUSH_LL:
         debugBelch("PUSH_LL  %d %d\n", instrs[pc], instrs[pc+1] ); 
         pc += 2; break;
      case bci_PUSH_LLL:
         debugBelch("PUSH_LLL %d %d %d\n", instrs[pc], instrs[pc+1], 
                                                            instrs[pc+2] ); 
         pc += 3; break;
      case bci_PUSH_G:
         debugBelch("PUSH_G   " ); printPtr( ptrs[instrs[pc]] );
         debugBelch("\n" );
         pc += 1; break;

      case bci_PUSH_ALTS:
         debugBelch("PUSH_ALTS  " ); printPtr( ptrs[instrs[pc]] );
         debugBelch("\n");
         pc += 1; break;
      case bci_PUSH_ALTS_P:
         debugBelch("PUSH_ALTS_P  " ); printPtr( ptrs[instrs[pc]] );
         debugBelch("\n");
         pc += 1; break;
      case bci_PUSH_ALTS_N:
         debugBelch("PUSH_ALTS_N  " ); printPtr( ptrs[instrs[pc]] );
         debugBelch("\n");
         pc += 1; break;
      case bci_PUSH_ALTS_F:
         debugBelch("PUSH_ALTS_F  " ); printPtr( ptrs[instrs[pc]] );
         debugBelch("\n");
         pc += 1; break;
      case bci_PUSH_ALTS_D:
         debugBelch("PUSH_ALTS_D  " ); printPtr( ptrs[instrs[pc]] );
         debugBelch("\n");
         pc += 1; break;
      case bci_PUSH_ALTS_L:
         debugBelch("PUSH_ALTS_L  " ); printPtr( ptrs[instrs[pc]] );
         debugBelch("\n");
         pc += 1; break;
      case bci_PUSH_ALTS_V:
         debugBelch("PUSH_ALTS_V  " ); printPtr( ptrs[instrs[pc]] );
         debugBelch("\n");
         pc += 1; break;

      case bci_PUSH_UBX:
         debugBelch("PUSH_UBX ");
         for (i = 0; i < instrs[pc+1]; i++) 
            debugBelch("0x%x ", literals[i + instrs[pc]] );
         debugBelch("\n");
         pc += 2; break;
      case bci_PUSH_APPLY_N:
	  debugBelch("PUSH_APPLY_N\n");
	  break;
      case bci_PUSH_APPLY_V:
	  debugBelch("PUSH_APPLY_V\n");
	  break;
      case bci_PUSH_APPLY_F:
	  debugBelch("PUSH_APPLY_F\n");
	  break;
      case bci_PUSH_APPLY_D:
	  debugBelch("PUSH_APPLY_D\n");
	  break;
      case bci_PUSH_APPLY_L:
	  debugBelch("PUSH_APPLY_L\n");
	  break;
      case bci_PUSH_APPLY_P:
	  debugBelch("PUSH_APPLY_P\n");
	  break;
      case bci_PUSH_APPLY_PP:
	  debugBelch("PUSH_APPLY_PP\n");
	  break;
      case bci_PUSH_APPLY_PPP:
	  debugBelch("PUSH_APPLY_PPP\n");
	  break;
      case bci_PUSH_APPLY_PPPP:
	  debugBelch("PUSH_APPLY_PPPP\n");
	  break;
      case bci_PUSH_APPLY_PPPPP:
	  debugBelch("PUSH_APPLY_PPPPP\n");
	  break;
      case bci_PUSH_APPLY_PPPPPP:
	  debugBelch("PUSH_APPLY_PPPPPP\n");
	  break;
      case bci_SLIDE: 
         debugBelch("SLIDE     %d down by %d\n", instrs[pc], instrs[pc+1] );
         pc += 2; break;
      case bci_ALLOC_AP:
         debugBelch("ALLOC_AP  %d words\n", instrs[pc] );
         pc += 1; break;
      case bci_ALLOC_PAP:
         debugBelch("ALLOC_PAP %d words, %d arity\n",
		 instrs[pc], instrs[pc+1] );
         pc += 2; break;
      case bci_MKAP:
         debugBelch("MKAP      %d words, %d stkoff\n", instrs[pc+1], 
                                                           instrs[pc] );
         pc += 2; break;
      case bci_UNPACK:
         debugBelch("UNPACK    %d\n", instrs[pc] );
         pc += 1; break;
      case bci_PACK:
         debugBelch("PACK      %d words with itbl ", instrs[pc+1] );
         printPtr( (StgPtr)itbls[instrs[pc]] );
         debugBelch("\n");
         pc += 2; break;

      case bci_TESTLT_I:
         debugBelch("TESTLT_I  %d, fail to %d\n", literals[instrs[pc]],
                                                      instrs[pc+1]);
         pc += 2; break;
      case bci_TESTEQ_I:
         debugBelch("TESTEQ_I  %d, fail to %d\n", literals[instrs[pc]],
                                                      instrs[pc+1]);
         pc += 2; break;

      case bci_TESTLT_F:
         debugBelch("TESTLT_F  %d, fail to %d\n", literals[instrs[pc]],
                                                      instrs[pc+1]);
         pc += 2; break;
      case bci_TESTEQ_F:
         debugBelch("TESTEQ_F  %d, fail to %d\n", literals[instrs[pc]],
                                                      instrs[pc+1]);
         pc += 2; break;

      case bci_TESTLT_D:
         debugBelch("TESTLT_D  %d, fail to %d\n", literals[instrs[pc]],
                                                      instrs[pc+1]);
         pc += 2; break;
      case bci_TESTEQ_D:
         debugBelch("TESTEQ_D  %d, fail to %d\n", literals[instrs[pc]],
                                                      instrs[pc+1]);
         pc += 2; break;

      case bci_TESTLT_P:
         debugBelch("TESTLT_P  %d, fail to %d\n", instrs[pc],
                                                      instrs[pc+1]);
         pc += 2; break;
      case bci_TESTEQ_P:
         debugBelch("TESTEQ_P  %d, fail to %d\n", instrs[pc],
                                                      instrs[pc+1]);
         pc += 2; break;
      case bci_CASEFAIL: 
         debugBelch("CASEFAIL\n" );
         break;
      case bci_JMP:
         debugBelch("JMP to    %d\n", instrs[pc]);
         pc += 1; break;

      case bci_ENTER:
         debugBelch("ENTER\n");
         break;

      case bci_RETURN:
         debugBelch("RETURN\n" );
	 break;
      case bci_RETURN_P:
         debugBelch("RETURN_P\n" );
	 break;
      case bci_RETURN_N:
         debugBelch("RETURN_N\n" );
	 break;
      case bci_RETURN_F:
         debugBelch("RETURN_F\n" );
	 break;
      case bci_RETURN_D:
         debugBelch("RETURN_D\n" );
	 break;
      case bci_RETURN_L:
         debugBelch("RETURN_L\n" );
	 break;
      case bci_RETURN_V:
         debugBelch("RETURN_V\n" );
	 break;

      default:
         barf("disInstr: unknown opcode");
   }
   return pc;
}


/* Something of a kludge .. how do we know where the end of the insn
   array is, since it isn't recorded anywhere?  Answer: the first
   short is the number of bytecodes which follow it.  
   See ByteCodeGen.linkBCO.insns_arr for construction ...  
*/
void disassemble( StgBCO *bco )
{
   nat i, j;
   StgWord16*     instrs    = (StgWord16*)(bco->instrs->payload);
   StgMutArrPtrs* ptrs      = bco->ptrs;
   nat            nbcs      = (int)instrs[0];
   nat            pc        = 1;

   debugBelch("BCO\n" );
   pc = 1;
   while (pc <= nbcs) {
      debugBelch("\t%2d:  ", pc );
      pc = disInstr ( bco, pc );
   }

   debugBelch("INSTRS:\n   " );
   j = 16;
   for (i = 0; i < nbcs; i++) {
      debugBelch("%3d ", (int)instrs[i] );
      j--; 
      if (j == 0) { j = 16; debugBelch("\n   "); };
   }
   debugBelch("\n");

   debugBelch("PTRS:\n   " );
   j = 8;
   for (i = 0; i < ptrs->ptrs; i++) {
      debugBelch("%8p ", ptrs->payload[i] );
      j--; 
      if (j == 0) { j = 8; debugBelch("\n   "); };
   }
   debugBelch("\n");

   debugBelch("\n");
   ASSERT(pc == nbcs+1);
}

#endif /* DEBUG */