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 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
|
/* Copyright (C) 2009 Matthew Fluet.
* Copyright (C) 2004-2008 Henry Cejtin, Matthew Fluet, Suresh
* Jagannathan, and Stephen Weeks.
*
* MLton is released under a BSD-style license.
* See the file MLton-LICENSE for details.
*/
#define MLTON_GC_INTERNAL_TYPES
#ifndef MLTON_CODEGEN_STATIC_INLINE
#define MLTON_CODEGEN_STATIC_INLINE static inline
#endif
/* No need to declare inlined math functions, since <math.h> comes
* with "platform.h".
*/
#ifndef MLTON_CODEGEN_MATHFN
#define MLTON_CODEGEN_MATHFN(decl)
#endif
/* WordS<N>_quot and WordS<N>_rem can be inlined with the
* bytecode-codegen, since they will be used in a context where the
* arguments are variables.
*/
#ifndef MLTON_CODEGEN_WORDSQUOTREM
#define MLTON_CODEGEN_WORDSQUOTREM(func) func
#endif
#ifndef MLTON_CODEGEN_WORDSQUOTREM_IMPL
#define MLTON_CODEGEN_WORDSQUOTREM_IMPL(func) func
#endif
/* No need to declare memcpy, since <string.h> comes with platform.h.
*/
#ifndef MLTON_CODEGEN_MEMCPY
#define MLTON_CODEGEN_MEMCPY(decl)
#endif
#include "platform.h"
#include "c-chunk.h" // c-chunk.h must come before opcode.h because it
// redefines some opcode symbols
#include "interpret.h"
#include "opcode.h"
enum {
DEBUG_BYTECODE = FALSE,
};
#if defined (GC_MODEL_NATIVE32)
#define WordPointer Word32
#define WordArrayIndex Word32
#elif defined (GC_MODEL_NATIVE64)
#define WordPointer Word64
#define WordArrayIndex Word64
#else
#error GC_MODEL_* undefined
#endif
typedef WordArrayIndex ArrayIndex;
typedef Word16 ArrayOffset;
typedef Word16 CallCIndex;
typedef Word16 GlobalIndex;
typedef uintptr_t Label;
typedef Int16 Offset; // Offset must be signed.
typedef Pointer ProgramCounter;
typedef Word16 RegIndex;
typedef Word8 Scale;
typedef Int16 StackOffset; // StackOffset must be signed.
PRIVATE extern struct GC_state gcState;
//----------------------------------------------------------------------
// Imports
//----------------------------------------------------------------------
#define regs(ty) \
int ty##RegI = 0; \
PRIVATE extern ty global##ty[]; \
static ty ty##VReg[1000]; \
ty ty##Reg[1000] = { 0 }
regs(CPointer);
regs(Objptr);
regs(Real32);
regs(Real64);
regs(Word8);
regs(Word16);
regs(Word32);
regs(Word64);
PRIVATE extern Objptr globalObjptrNonRoot[];
#undef regs
//
// Virtual Registers. Explicitly referenced by the Machine IL.
//
#define R(ty, i) (ty##VReg [i])
//----------------------------------------------------------------------
#define Fetch(t, z) \
do { \
z = *(t*)pc; \
if (DEBUG or DEBUG_BYTECODE or disassemble) { \
if (! strcmp(#z,"label")) \
fprintf (stderr, " %s", offsetToLabel[z]); \
else if (! strcmp(#z, "opc")) \
fprintf (stderr, " %d", (int)z); \
} \
pc += sizeof (t); \
} while (0)
enum {
MODE_load,
MODE_store,
};
#define maybe unless (disassemble)
#define StoreReg(t, z) maybe PushReg(t) = z
#define loadStoreGen(mode, t, t2, z) \
switch (MODE_##mode) { \
case MODE_load: \
StoreReg (t2, (t2)z); \
break; \
case MODE_store: \
maybe z = (t) (PopReg (t2)); \
break; \
default: \
assert (FALSE); \
}
#define loadStore(mode, t, z) loadStoreGen(mode, t, t, z)
#define loadStoreArrayOffset(mode, ty) \
case opcodeSymOfTy2 (ty, mode##ArrayOffset): \
{ \
ArrayOffset arrayOffset; \
Pointer arrayBase; \
ArrayIndex arrayIndex; \
Scale arrayScale; \
Fetch (ArrayOffset, arrayOffset); \
Fetch (Scale, arrayScale); \
if (disassemble) goto mainLoop; \
arrayIndex = PopRegX (WordArrayIndex); \
arrayBase = (Pointer) (PopRegX (WordPointer)); \
loadStore (mode, ty, \
*(ty*)(arrayBase + (arrayIndex * arrayScale) + arrayOffset)); \
goto mainLoop; \
}
#define loadStoreContents(mode, ty) \
case opcodeSymOfTy2 (ty, mode##Contents): \
if (disassemble) goto mainLoop; \
{ \
Pointer base; \
base = (Pointer) (PopRegX (WordPointer)); \
loadStore (mode, ty, C (ty, base)); \
goto mainLoop; \
}
#define loadStoreFrontier(mode) \
case opcodeSym (mode##Frontier): \
if (disassemble) goto mainLoop; \
loadStoreGen (mode, Pointer, WordPointer, Frontier); \
goto mainLoop;
#define loadGCState() \
case opcodeSym (loadGCState): \
if (disassemble) goto mainLoop; \
StoreReg (WordPointer, (WordPointer)&gcState); \
goto mainLoop;
#define loadStoreGlobal(mode, ty) \
case opcodeSymOfTy2 (ty, mode##Global): \
{ \
GlobalIndex globalIndex; \
Fetch (GlobalIndex, globalIndex); \
if (disassemble) goto mainLoop; \
loadStoreGen (mode, ty, ty, G (ty, globalIndex)); \
goto mainLoop; \
}
#define loadStoreGlobalPointer(mode, ty) \
case opcodeSymOfTy2 (ty, mode##Global): \
{ \
GlobalIndex globalIndex; \
Fetch (GlobalIndex, globalIndex); \
if (disassemble) goto mainLoop; \
loadStoreGen (mode, ty, WordPointer, G (ty, globalIndex)); \
goto mainLoop; \
}
#define loadStoreGPNR(mode) \
case opcodeSym (mode##GPNR): \
{ \
GlobalIndex globalIndex; \
Fetch (GlobalIndex, globalIndex); \
if (disassemble) goto mainLoop; \
loadStoreGen (mode, Objptr, WordPointer, GPNR (globalIndex)); \
goto mainLoop; \
}
#define loadStoreOffset(mode, ty) \
case opcodeSymOfTy2 (ty, mode##Offset): \
{ \
Pointer base; \
Offset offset; \
Fetch (Offset, offset); \
if (disassemble) goto mainLoop; \
base = (Pointer) (PopRegX (WordPointer)); \
maybe loadStore (mode, ty, O (ty, base, offset)); \
goto mainLoop; \
}
#define loadStoreRegister(mode, ty) \
case opcodeSymOfTy2 (ty, mode##Register): \
{ \
RegIndex regIndex; \
Fetch (RegIndex, regIndex); \
if (disassemble) goto mainLoop; \
loadStoreGen (mode, ty, ty, R (ty, regIndex)); \
goto mainLoop; \
}
#define loadStoreRegisterPointer(mode, ty) \
case opcodeSymOfTy2 (ty, mode##Register): \
{ \
RegIndex regIndex; \
Fetch (RegIndex, regIndex); \
if (disassemble) goto mainLoop; \
loadStoreGen (mode, ty, WordPointer, R (ty, regIndex)); \
goto mainLoop; \
}
#define loadStoreStackOffset(mode, ty) \
case opcodeSymOfTy2 (ty, mode##StackOffset): \
{ \
StackOffset stackOffset; \
Fetch (StackOffset, stackOffset); \
if (disassemble) goto mainLoop; \
loadStore (mode, ty, S (ty, stackOffset)); \
goto mainLoop; \
}
#define loadStoreStackTop(mode) \
case opcodeSym (mode##StackTop): \
if (disassemble) goto mainLoop; \
loadStoreGen (mode, Pointer, WordPointer, StackTop); \
goto mainLoop;
#define loadWord(size) \
case opcodeSymOfTy (Word, size, loadWord): \
{ \
Word##size t0; \
Fetch (Word##size, t0); \
if (disassemble) goto mainLoop; \
loadStore (load, Word##size, t0); \
goto mainLoop; \
}
#define binary(ty, f) \
case opcodeSym (f): \
if (disassemble) goto mainLoop; \
{ \
ty t0 = PopReg (ty); \
ty t1 = PopReg (ty); \
PushReg (ty) = f (t0, t1); \
goto mainLoop; \
}
/* The bytecode interpreter relies on the fact that the overflow checking
* primitives implemented in c-chunk.h only set the result if the operation does
* not overflow. When the result overflow, the interpreter pushes a zero on
* the stack for the result.
*/
#define binaryCheck(ty, f) \
case opcodeSym (f): \
if (disassemble) goto mainLoop; \
{ \
ty t0 = PopReg (ty); \
ty t1 = PopReg (ty); \
f (PushReg (ty), t0, t1, f##Overflow); \
overflow = FALSE; \
goto mainLoop; \
f##Overflow: \
PushReg (ty) = 0; /* overflow, push 0 */ \
overflow = TRUE; \
goto mainLoop; \
}
#define coerceOp(n, f, t) opcodeSym (f##_##n##To##t)
#define coerce(n, f1, t1, f2, t2) \
case coerceOp (n, f2, t2): \
if (disassemble) goto mainLoop; \
{ \
f1 t0 = PopReg (f1); \
PushReg (t1) = f2##_##n##To##t2 (t0); \
goto mainLoop; \
}
#define compare(ty, f) \
case opcodeSym (f): \
if (disassemble) goto mainLoop; \
{ \
ty t0 = PopReg (ty); \
ty t1 = PopReg (ty); \
PushReg (Word32) = f (t0, t1); \
goto mainLoop; \
}
#define cpointerBinary(f) \
case opcodeSym (f): \
if (disassemble) goto mainLoop; \
{ \
Pointer t0; \
t0 = (Pointer) (PopRegX (WordPointer)); \
WordPointer t1 = PopRegX (WordPointer); \
Pointer t2 = f (t0, t1); \
PushRegX (WordPointer) = (WordPointer) t2; \
goto mainLoop; \
}
#define cpointerCompare(f) \
case opcodeSym (f): \
if (disassemble) goto mainLoop; \
{ \
Pointer t0, t1; \
t0 = (Pointer) (PopRegX (WordPointer)); \
t1 = (Pointer) (PopRegX (WordPointer)); \
PushReg (Word32) = f (t0, t1); \
goto mainLoop; \
}
#define cpointerCoerceFrom(f) \
case opcodeSym (f): \
if (disassemble) goto mainLoop; \
{ \
WordPointer t0 = PopRegX (WordPointer); \
Pointer t1 = f (t0); \
PushRegX (WordPointer) = (WordPointer) t1; \
goto mainLoop; \
}
#define cpointerCoerceTo(f) \
case opcodeSym (f): \
if (disassemble) goto mainLoop; \
{ \
Pointer t0; \
t0 = (Pointer) (PopRegX (WordPointer)); \
PushRegX (WordPointer) = f (t0); \
goto mainLoop; \
}
#define cpointerDiff(f) \
case opcodeSym (f): \
if (disassemble) goto mainLoop; \
{ \
Pointer t0, t1; \
t0 = (Pointer) (PopRegX (WordPointer)); \
t1 = (Pointer) (PopRegX (WordPointer)); \
PushRegX (WordPointer) = f (t0, t1); \
goto mainLoop; \
}
#define cpointerLoadWord(f) \
case opcodeSym (f): \
{ \
size_t t0; \
Fetch (WordPointer, t0); \
if (disassemble) goto mainLoop; \
StoreReg (CPointer, (CPointer)t0); \
goto mainLoop; \
}
#define shift(ty, f) \
case opcodeSym (f): \
if (disassemble) goto mainLoop; \
{ \
ty w = PopReg (ty); \
Word32 s = PopReg (Word32); \
ty w2 = f (w, s); \
PushReg (ty) = w2; \
goto mainLoop; \
}
#define unary(ty, f) \
case opcodeSym (f): \
if (disassemble) goto mainLoop; \
{ \
ty t0 = PopReg (ty); \
PushReg (ty) = f (t0); \
goto mainLoop; \
}
/* The bytecode interpreter relies on the fact that the overflow checking
* primitives implemented in c-chunk.h only set the result if the operation does
* not overflow. When the result overflow, the interpreter pushes a zero on
* the stack for the result.
*/
#define unaryCheck(ty, f) \
case opcodeSym (f): \
if (disassemble) goto mainLoop; \
{ \
ty t0 = PopReg (ty); \
f (PushReg (ty), t0, f##Overflow); \
overflow = FALSE; \
goto mainLoop; \
f##Overflow: \
PushReg (ty) = 0; /* overflow, push 0 */ \
overflow = TRUE; \
goto mainLoop; \
}
#define Goto(l) \
do { \
maybe pc = code + l; \
goto mainLoop; \
} while (0)
#define Switch(size) \
case OPCODE_Switch##size: \
{ \
Label label; \
ProgramCounter lastCase; \
Word##size test = 0; \
Word16 numCases; \
\
Fetch (Word16, numCases); \
if (sizeof(Label) == 4) { \
lastCase = pc + (4 + size/8) * numCases; \
} else if (sizeof(Label) == 8) { \
lastCase = pc + (8 + size/8) * numCases; \
} else { assert (FALSE); } \
maybe test = PopReg (Word##size); \
assertRegsEmpty (); \
while (pc < lastCase) { \
Word##size caseWord; \
if (DEBUG or DEBUG_BYTECODE or disassemble) \
fprintf (stderr, "\n\t "); \
Fetch (Word##size, caseWord); \
if (DEBUG or DEBUG_BYTECODE or disassemble) \
fprintf (stderr, " =>"); \
Fetch (Label, label); \
if (not disassemble and test == caseWord) \
Goto (label); \
} \
goto mainLoop; \
}
typedef char *String;
#undef CacheFrontier
#undef CacheStackTop
#undef FlushFrontier
#undef FlushStackTop
#define CacheFrontier() \
do { \
frontier = (Pointer)(gcState.frontier); \
} while (0)
#define CacheStackTop() \
do { \
stackTop = (Pointer)(gcState.stackTop); \
} while (0)
#define FlushFrontier() \
do { \
gcState.frontier = (pointer)frontier; \
} while (0)
#define FlushStackTop() \
do { \
gcState.stackTop = (pointer)stackTop; \
} while (0)
#define disp(ty,ty2,fmt) \
for (i = 0; i < ty##RegI; ++i) \
fprintf (stderr, "\n" #ty "Reg[%d] = "fmt, \
i, (ty2)(ty##Reg[i]))
static inline void displayRegs (void) {
int i;
disp (CPointer,uintptr_t,FMTPTR);
disp (Objptr,uintptr_t,FMTPTR);
disp (Word8,Word8,"0x%02"PRIx8);
disp (Word16,Word16,"0x%04"PRIx16);
disp (Word32,Word32,"0x%08"PRIx32);
disp (Word64,Word64,"0x%016"PRIx64);
disp (Real32,Real32,"%f");
disp (Real64,Real64,"%f");
}
static void interpret (Bytecode b, CodeOffset codeOffset, Bool disassemble) {
CallCIndex callCIndex;
Pointer code;
Pointer frontier;
unsigned int i;
String name;
String *offsetToLabel = NULL;
Opcode opc;
Bool overflow = FALSE;
ProgramCounter pc;
ProgramCounter pcMax;
Pointer stackTop;
code = b->code;
pcMax = b->code + b->codeSize;
if (DEBUG or DEBUG_BYTECODE or disassemble) {
offsetToLabel =
(String*)(calloc_safe (b->codeSize, sizeof(*offsetToLabel)));
for (i = 0; i < b->nameOffsetsSize; ++i)
offsetToLabel [b->nameOffsets[i].codeOffset] =
b->addressNames + b->nameOffsets[i].nameOffset;
}
if (disassemble)
pc = code;
else {
pc = code + codeOffset;
}
CacheFrontier ();
CacheStackTop ();
mainLoop:
if (DEBUG_BYTECODE)
displayRegs ();
if (DEBUG or DEBUG_BYTECODE or disassemble) {
if (pc == pcMax)
goto done;
name = offsetToLabel [pc - b->code];
unless (NULL == name)
fprintf (stderr, "\n%s:", name);
fprintf (stderr, "\n\t");
}
assert (code <= pc and pc < pcMax);
Fetch (Opcode, opc);
assert (opc < (cardof (opcodeStrings)));
if (DEBUG or DEBUG_BYTECODE or disassemble)
fprintf (stderr, "%s", opcodeStrings[opc]);
switch ((enum OpcodeEnum)opc) {
prims ();
case opcodeSym (CacheFrontier):
{
if (disassemble) goto mainLoop;
CacheFrontier ();
goto mainLoop;
}
case opcodeSym (FlushFrontier):
{
if (disassemble) goto mainLoop;
FlushFrontier ();
goto mainLoop;
}
case opcodeSym (CacheStackTop):
{
if (disassemble) goto mainLoop;
CacheStackTop ();
goto mainLoop;
}
case opcodeSym (FlushStackTop):
{
if (disassemble) goto mainLoop;
FlushStackTop ();
goto mainLoop;
}
case opcodeSym (BranchIfZero):
{
Label label;
Fetch (Label, label);
if (disassemble) goto mainLoop;
if (0 == PopReg (Word32))
Goto (label);
goto mainLoop;
}
case opcodeSym (CallC):
Fetch (CallCIndex, callCIndex);
unless (disassemble) {
FlushFrontier ();
FlushStackTop ();
MLton_callC (callCIndex);
CacheFrontier ();
CacheStackTop ();
}
goto mainLoop;
case opcodeSym (Goto):
{
Label label;
Fetch (Label, label);
Goto (label);
}
loadStoreGPNR(load);
loadStoreGPNR(store);
case opcodeSym (JumpOnOverflow):
{
Label label;
Fetch (Label, label);
if (overflow)
Goto (label);
goto mainLoop;
}
case opcodeSym (Raise):
maybe StackTop = (Pointer)(gcState.stackBottom + gcState.exnStack);
// fall through to Return.
case opcodeSym (Return):
Goto (*(Label*)(StackTop - sizeof (Label)));
Switch(8);
Switch(16);
Switch(32);
Switch(64);
case opcodeSym (Thread_returnToC):
if (disassemble) goto mainLoop;
FlushFrontier ();
FlushStackTop ();
goto done;
default:
assert (FALSE);
}
assert (FALSE);
done:
if (DEBUG or DEBUG_BYTECODE or disassemble)
free (offsetToLabel);
return;
}
static void disassemble (Bytecode b, CodeOffset codeOffset) {
interpret (b, codeOffset, TRUE);
fprintf (stderr, "\n");
}
void MLton_Bytecode_interpret (Bytecode b, CodeOffset codeOffset) {
if (DEBUG or DEBUG_BYTECODE) {
fprintf (stderr, "MLton_Bytecode_interpret ("FMTPTR", %"PRIxPTR")\n",
(uintptr_t)b,
codeOffset);
disassemble (b, codeOffset);
fprintf (stderr, "interpret starting\n");
}
interpret (b, codeOffset, FALSE);
}
|