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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
|
/* -----------------------------------------------------------------------------
* Bytecode disassembler
*
* Copyright (c) 1994-2002.
*
* $RCSfile: Disassembler.c,v $
* $Revision: 1.29 $
* $Date: 2004/09/03 15:28:19 $
* ---------------------------------------------------------------------------*/
#if defined(DEBUG)
#include "rts/PosixSource.h"
#include "Rts.h"
#include "RtsAPI.h"
#include "rts/Bytecodes.h"
#include "RtsUtils.h"
#include "Schedule.h"
#include "Printer.h"
#include "Disassembler.h"
#include "Interpreter.h"
/* --------------------------------------------------------------------------
* Disassembler
* ------------------------------------------------------------------------*/
int
disInstr ( StgBCO *bco, int pc )
{
StgWord16 instr;
StgWord16* instrs = (StgWord16*)(bco->instrs->payload);
StgArrBytes* literal_arr = bco->literals;
StgWord* literals = (StgWord*)(&literal_arr->payload[0]);
StgMutArrPtrs* ptrs_arr = bco->ptrs;
StgPtr* ptrs = (StgPtr*)(&ptrs_arr->payload[0]);
instr = instrs[pc++];
if (instr & bci_FLAG_LARGE_ARGS) {
debugBelch ("LARGE ");
}
#define BCO_NEXT instrs[pc++]
#define BCO_NEXT_32 (pc += 2)
#define BCO_READ_NEXT_32 (BCO_NEXT_32, (((StgWord) instrs[pc-2]) << 16) \
+ ( (StgWord) instrs[pc-1]))
#define BCO_NEXT_64 (pc += 4)
#define BCO_READ_NEXT_64 (BCO_NEXT_64, (((StgWord) instrs[pc-4]) << 48) \
+ (((StgWord) instrs[pc-3]) << 32) \
+ (((StgWord) instrs[pc-2]) << 16) \
+ ( (StgWord) instrs[pc-1]))
#if WORD_SIZE_IN_BITS == 32
#define BCO_NEXT_WORD BCO_NEXT_32
#define BCO_READ_NEXT_WORD BCO_READ_NEXT_32
#elif WORD_SIZE_IN_BITS == 64
#define BCO_NEXT_WORD BCO_NEXT_64
#define BCO_READ_NEXT_WORD BCO_READ_NEXT_64
#else
#error Cannot cope with WORD_SIZE_IN_BITS being nether 32 nor 64
#endif
#define BCO_GET_LARGE_ARG ((instr & bci_FLAG_LARGE_ARGS) ? BCO_READ_NEXT_WORD : BCO_NEXT)
switch (instr & 0xff) {
case bci_BRK_FUN:
debugBelch ("BRK_FUN " ); printPtr( ptrs[instrs[pc]] );
debugBelch (" %d ", instrs[pc+1]); printPtr( ptrs[instrs[pc+2]] );
CostCentre* cc = (CostCentre*)literals[instrs[pc+3]];
if (cc) {
debugBelch(" %s", cc->label);
}
debugBelch("\n");
pc += 4;
break;
case bci_SWIZZLE: {
W_ stkoff = BCO_GET_LARGE_ARG;
StgInt by = BCO_GET_LARGE_ARG;
debugBelch("SWIZZLE stkoff %" FMT_Word " by %" FMT_Int "\n", stkoff, by);
break; }
case bci_CCALL: {
debugBelch("CCALL marshaller at 0x%" FMT_HexWord "\n",
literals[instrs[pc]] );
pc += 1; break; }
case bci_PRIMCALL:
debugBelch("PRIMCALL\n");
break;
case bci_STKCHECK: {
StgWord stk_words_reqd = BCO_GET_LARGE_ARG + 1;
debugBelch("STKCHECK %" FMT_Word "\n", (W_)stk_words_reqd );
break;
}
case bci_PUSH_L: {
W_ x1 = BCO_GET_LARGE_ARG;
debugBelch("PUSH_L %" FMT_Word "\n", x1 );
break; }
case bci_PUSH_LL: {
W_ x1 = BCO_GET_LARGE_ARG;
W_ x2 = BCO_GET_LARGE_ARG;
debugBelch("PUSH_LL %" FMT_Word " %" FMT_Word "\n", x1, x2 );
break; }
case bci_PUSH_LLL: {
W_ x1 = BCO_GET_LARGE_ARG;
W_ x2 = BCO_GET_LARGE_ARG;
W_ x3 = BCO_GET_LARGE_ARG;
debugBelch("PUSH_LLL %" FMT_Word " %" FMT_Word " %" FMT_Word "\n", x1, x2, x3);
break; }
case bci_PUSH8: {
W_ x1 = BCO_GET_LARGE_ARG;
debugBelch("PUSH8 %" FMT_Word "\n", x1 );
break; }
case bci_PUSH16: {
W_ x1 = BCO_GET_LARGE_ARG;
debugBelch("PUSH16 %" FMT_Word "\n", x1 );
break; }
case bci_PUSH32: {
W_ x1 = BCO_GET_LARGE_ARG;
debugBelch("PUSH32 %" FMT_Word "\n", x1 );
break; }
case bci_PUSH8_W: {
W_ x1 = BCO_GET_LARGE_ARG;
debugBelch("PUSH8_W %" FMT_Word "\n", x1 );
break; }
case bci_PUSH16_W: {
W_ x1 = BCO_GET_LARGE_ARG;
debugBelch("PUSH16_W %" FMT_Word "\n", x1 );
break; }
case bci_PUSH32_W: {
W_ x1 = BCO_GET_LARGE_ARG;
debugBelch("PUSH32_W %" FMT_Word "\n", x1 );
break; }
case bci_PUSH_G:
debugBelch("PUSH_G " ); 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_ALTS_T:
debugBelch("PUSH_ALTS_T ");
printPtr( ptrs[instrs[pc]] );
debugBelch(" 0x%" FMT_HexWord " ", literals[instrs[pc+1]] );
printPtr( ptrs[instrs[pc+2]] );
debugBelch("\n");
pc += 3; break;
case bci_PUSH_PAD8:
debugBelch("PUSH_PAD8\n");
pc += 1; break;
case bci_PUSH_PAD16:
debugBelch("PUSH_PAD16\n");
pc += 1; break;
case bci_PUSH_PAD32:
debugBelch("PUSH_PAD32\n");
pc += 1; break;
case bci_PUSH_UBX8:
debugBelch(
"PUSH_UBX8 0x%" FMT_HexWord8 "\n",
(StgWord8) literals[instrs[pc]] );
pc += 1; break;
case bci_PUSH_UBX16:
debugBelch(
"PUSH_UBX16 0x%" FMT_HexWord16 "\n",
(StgWord16) literals[instrs[pc]] );
pc += 1; break;
case bci_PUSH_UBX32:
debugBelch(
"PUSH_UBX32 0x%" FMT_HexWord32 "\n",
(StgWord32) literals[instrs[pc]] );
pc += 1; break;
case bci_PUSH_UBX: {
debugBelch("PUSH_UBX ");
W_ offset = BCO_GET_LARGE_ARG;
W_ nwords = BCO_GET_LARGE_ARG;
for (W_ i = 0; i < nwords; i++)
debugBelch("0x%" FMT_HexWord " ", literals[i + offset] );
debugBelch("\n");
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: {
W_ nwords = BCO_GET_LARGE_ARG;
W_ by = BCO_GET_LARGE_ARG;
debugBelch("SLIDE %" FMT_Word " down by %" FMT_Word "\n", nwords, by );
break; }
case bci_ALLOC_AP: {
W_ nwords = BCO_GET_LARGE_ARG;
debugBelch("ALLOC_AP %" FMT_Word " words\n", nwords );
break; }
case bci_ALLOC_AP_NOUPD: {
W_ nwords = BCO_GET_LARGE_ARG;
debugBelch("ALLOC_AP_NOUPD %" FMT_Word " words\n", nwords );
break; }
case bci_ALLOC_PAP: {
W_ arity = BCO_GET_LARGE_ARG;
W_ nwords = BCO_GET_LARGE_ARG;
debugBelch("ALLOC_PAP %" FMT_Word " arity, %" FMT_Word " words\n",
arity, nwords );
break; }
case bci_MKAP: {
W_ stkoff = BCO_GET_LARGE_ARG;
W_ nwords = BCO_GET_LARGE_ARG;
debugBelch("MKAP %" FMT_Word " words, %" FMT_Word " stkoff\n", nwords,
stkoff );
break; }
case bci_MKPAP: {
W_ stkoff = BCO_GET_LARGE_ARG;
W_ nwords = BCO_GET_LARGE_ARG;
debugBelch("MKPAP %" FMT_Word " words, %" FMT_Word " stkoff\n", nwords,
stkoff );
break; }
case bci_UNPACK: {
W_ nwords = BCO_GET_LARGE_ARG;
debugBelch("UNPACK %" FMT_Word "\n", nwords );
break; }
case bci_PACK: {
int itbl = BCO_NEXT;
W_ nwords = BCO_GET_LARGE_ARG;
debugBelch("PACK %" FMT_Word " words with itbl ", nwords );
printPtr( (StgPtr)literals[itbl] );
debugBelch("\n");
break; }
case bci_TESTLT_I: {
unsigned int discr = BCO_NEXT;
int failto = BCO_GET_LARGE_ARG;
debugBelch("TESTLT_I %" FMT_Int ", fail to %d\n", literals[discr], failto);
break;
}
case bci_TESTLT_I64: {
unsigned int discr = BCO_NEXT;
int failto = BCO_GET_LARGE_ARG;
debugBelch("TESTLT_I64 %" FMT_Int64 ", fail to %d\n", *((StgInt64*)(literals+discr)), failto);
break;
}
case bci_TESTLT_I32: {
unsigned int discr = BCO_NEXT;
int failto = BCO_GET_LARGE_ARG;
debugBelch("TESTLT_I32 %" FMT_Int ", fail to %d\n", literals[discr], failto);
break;
}
case bci_TESTLT_I16: {
unsigned int discr = BCO_NEXT;
int failto = BCO_GET_LARGE_ARG;
debugBelch("TESTLT_I16 %" FMT_Int ", fail to %d\n", literals[discr], failto);
break;
}
case bci_TESTLT_I8: {
unsigned int discr = BCO_NEXT;
int failto = BCO_GET_LARGE_ARG;
debugBelch("TESTLT_I8 %" FMT_Int ", fail to %d\n", literals[discr], failto);
break;
}
case bci_TESTEQ_I:
debugBelch("TESTEQ_I %" FMT_Int ", fail to %d\n", literals[instrs[pc]],
instrs[pc+1]);
pc += 2; break;
case bci_TESTEQ_I64:
debugBelch("TESTEQ_I64 %" FMT_Int64 ", fail to %d\n", *((StgInt64*)(literals+instrs[pc])),
instrs[pc+1]);
pc += 2; break;
case bci_TESTEQ_I32:
debugBelch("TESTEQ_I32 %" FMT_Int ", fail to %d\n", literals[instrs[pc]],
instrs[pc+1]);
pc += 2; break;
case bci_TESTEQ_I16:
debugBelch("TESTEQ_I16 %" FMT_Int ", fail to %d\n", literals[instrs[pc]],
instrs[pc+1]);
pc += 2; break;
case bci_TESTEQ_I8:
debugBelch("TESTEQ_I8 %" FMT_Int ", fail to %d\n", literals[instrs[pc]],
instrs[pc+1]);
pc += 2; break;
case bci_TESTLT_W: {
unsigned int discr = BCO_NEXT;
int failto = BCO_GET_LARGE_ARG;
debugBelch("TESTLT_W %" FMT_Word ", fail to %d\n", literals[discr], failto);
break;
}
case bci_TESTLT_W64: {
unsigned int discr = BCO_NEXT;
int failto = BCO_GET_LARGE_ARG;
debugBelch("TESTLT_W64 %" FMT_Word64 ", fail to %d\n", *((StgWord64*)(literals+discr)), failto);
break;
}
case bci_TESTLT_W32: {
unsigned int discr = BCO_NEXT;
int failto = BCO_GET_LARGE_ARG;
debugBelch("TESTLT_W32 %" FMT_Word ", fail to %d\n", literals[discr], failto);
break;
}
case bci_TESTLT_W16: {
unsigned int discr = BCO_NEXT;
int failto = BCO_GET_LARGE_ARG;
debugBelch("TESTLT_W16 %" FMT_Word ", fail to %d\n", literals[discr], failto);
break;
}
case bci_TESTLT_W8: {
unsigned int discr = BCO_NEXT;
int failto = BCO_GET_LARGE_ARG;
debugBelch("TESTLT_W8 %" FMT_Word ", fail to %d\n", literals[discr], failto);
break;
}
case bci_TESTEQ_W:
debugBelch("TESTEQ_W %" FMT_Word ", fail to %d\n", literals[instrs[pc]],
instrs[pc+1]);
pc += 2; break;
case bci_TESTEQ_W64:
debugBelch("TESTEQ_W64 %" FMT_Word64 ", fail to %d\n", *((StgWord64*)(literals+instrs[pc])),
instrs[pc+1]);
pc += 2; break;
case bci_TESTEQ_W32:
debugBelch("TESTEQ_W32 %" FMT_Word ", fail to %d\n", literals[instrs[pc]],
instrs[pc+1]);
pc += 2; break;
case bci_TESTEQ_W16:
debugBelch("TESTEQ_W16 %" FMT_Word ", fail to %d\n", literals[instrs[pc]],
instrs[pc+1]);
pc += 2; break;
case bci_TESTEQ_W8:
debugBelch("TESTEQ_W8 %" FMT_Word ", fail to %d\n", literals[instrs[pc]],
instrs[pc+1]);
pc += 2; break;
case bci_TESTLT_F:
debugBelch("TESTLT_F %f, fail to %d\n", *((StgFloat*)literals+instrs[pc]),
instrs[pc+1]);
pc += 2; break;
case bci_TESTEQ_F:
debugBelch("TESTEQ_F %f, fail to %d\n", *((StgFloat*)literals+instrs[pc]),
instrs[pc+1]);
pc += 2; break;
case bci_TESTLT_D:
debugBelch("TESTLT_D %f, fail to %d\n", *((StgDouble*)(literals+instrs[pc])),
instrs[pc+1]);
pc += 2; break;
case bci_TESTEQ_D:
debugBelch("TESTEQ_D %f, fail to %d\n", *((StgDouble*)(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_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;
case bci_RETURN_T:
debugBelch("RETURN_T\n ");
break;
default:
barf("disInstr: unknown opcode %u", (unsigned int) instr);
}
return pc;
}
void disassemble( StgBCO *bco )
{
uint32_t i, j;
StgWord16* instrs = (StgWord16*)(bco->instrs->payload);
StgMutArrPtrs* ptrs = bco->ptrs;
uint32_t nbcs = (uint32_t)(bco->instrs->bytes / sizeof(StgWord16));
uint32_t pc = 1;
debugBelch("BCO\n" );
pc = 0;
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");
}
#endif /* DEBUG */
|