diff --git a/libpolyml/int_opcodes.h b/libpolyml/int_opcodes.h index b7cf3684..278ab5bd 100644 --- a/libpolyml/int_opcodes.h +++ b/libpolyml/int_opcodes.h @@ -1,250 +1,252 @@ /* Title: Definitions for the code-tree instructions. Author: Dave Matthews, Cambridge University Computer Laboratory Copyright (c) 2000 Cambridge University Technical Services Limited - Further development Copyright David C.J. Matthews 2015-18. + Further development Copyright David C.J. Matthews 2015-18, 2020. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #define INSTR_enter_int 0x00 #define INSTR_jump8 0x02 #define INSTR_jump8false 0x03 #define INSTR_alloc_ref 0x06 #define INSTR_case16 0x0a #define INSTR_stack_container 0x0b #define INSTR_call_closure 0x0c #define INSTR_return_w 0x0d #define INSTR_pad 0x0e #define INSTR_raise_ex 0x10 #define INSTR_get_store_w 0x11 #define INSTR_local_w 0x13 #define INSTR_indirect_w 0x14 #define INSTR_move_to_vec_w 0x15 #define INSTR_set_stack_val_w 0x17 #define INSTR_reset_w 0x18 #define INSTR_reset_r_w 0x19 #define INSTR_constAddr16 0x1a #define INSTR_const_int_w 0x1b #define INSTR_callFastRRtoR 0x1c #define INSTR_callFastRGtoR 0x1d #define INSTR_jump_back8 0x1e #define INSTR_return_b 0x1f #define INSTR_jump_back16 0x20 #define INSTR_get_store_b 0x21 #define INSTR_local_b 0x22 #define INSTR_indirect_b 0x23 #define INSTR_move_to_vec_b 0x24 #define INSTR_set_stack_val_b 0x25 #define INSTR_reset_b 0x26 #define INSTR_reset_r_b 0x27 #define INSTR_const_int_b 0x28 #define INSTR_local_0 0x29 #define INSTR_local_1 0x2a #define INSTR_local_2 0x2b #define INSTR_local_3 0x2c #define INSTR_local_4 0x2d #define INSTR_local_5 0x2e #define INSTR_local_6 0x2f #define INSTR_local_7 0x30 #define INSTR_local_8 0x31 #define INSTR_local_9 0x32 #define INSTR_local_10 0x33 #define INSTR_local_11 0x34 #define INSTR_indirect_0 0x35 #define INSTR_indirect_1 0x36 #define INSTR_indirect_2 0x37 #define INSTR_indirect_3 0x38 #define INSTR_indirect_4 0x39 #define INSTR_indirect_5 0x3a #define INSTR_const_0 0x3b #define INSTR_const_1 0x3c #define INSTR_const_2 0x3d #define INSTR_const_3 0x3e #define INSTR_const_4 0x3f #define INSTR_const_10 0x40 #define INSTR_return_0 0x41 #define INSTR_return_1 0x42 #define INSTR_return_2 0x43 #define INSTR_return_3 0x44 #define INSTR_reset_1 0x50 #define INSTR_reset_2 0x51 #define INSTR_get_store_2 0x52 #define INSTR_get_store_3 0x53 #define INSTR_get_store_4 0x54 #define INSTR_tuple_container 0x55 #define INSTR_floatAbs 0x56 #define INSTR_floatNeg 0x57 #define INSTR_fixedIntToFloat 0x58 #define INSTR_floatToReal 0x59 #define INSTR_realToFloat 0x5a #define INSTR_floatEqual 0x5b #define INSTR_floatLess 0x5c #define INSTR_floatLessEq 0x5d #define INSTR_floatGreater 0x5e #define INSTR_floatGreaterEq 0x5f #define INSTR_floatAdd 0x60 #define INSTR_floatSub 0x61 #define INSTR_floatMult 0x62 #define INSTR_floatDiv 0x63 #define INSTR_reset_r_1 0x64 #define INSTR_reset_r_2 0x65 #define INSTR_reset_r_3 0x66 #define INSTR_tuple_w 0x67 #define INSTR_tuple_b 0x68 #define INSTR_tuple_2 0x69 #define INSTR_tuple_3 0x6a #define INSTR_tuple_4 0x6b #define INSTR_lock 0x6c #define INSTR_ldexc 0x6d #define INSTR_realToInt 0x6e #define INSTR_floatToInt 0x6f #define INSTR_callFastFtoF 0x70 #define INSTR_callFastGtoF 0x71 #define INSTR_callFastFFtoF 0x72 #define INSTR_callFastFGtoF 0x73 #define INSTR_push_handler 0x78 #define INSTR_realUnordered 0x79 #define INSTR_floatUnordered 0x7a #define INSTR_tail_b_b 0x7b #define INSTR_tail 0x7c #define INSTR_tail_3_b 0x7d #define INSTR_tail_4_b 0x7e #define INSTR_tail_3_2 0x7f #define INSTR_tail_3_3 0x80 #define INSTR_setHandler8 0x81 #define INSTR_callFastRTS0 0x83 #define INSTR_callFastRTS1 0x84 #define INSTR_callFastRTS2 0x85 #define INSTR_callFastRTS3 0x86 #define INSTR_callFastRTS4 0x87 #define INSTR_callFastRTS5 0x88 #define INSTR_callFullRTS0 0x89 -#define INSTR_callFullRTS1 0x8a -#define INSTR_callFullRTS2 0x8b -#define INSTR_callFullRTS3 0x8c -#define INSTR_callFullRTS4 0x8d -#define INSTR_callFullRTS5 0x8e +#define INSTR_callFullRTS1 0x8a // Legacy +#define INSTR_callFullRTS2 0x8b // Legacy +#define INSTR_callFullRTS3 0x8c // Legacy +#define INSTR_callFullRTS4 0x8d // Legacy +#define INSTR_callFullRTS5 0x8e // Legacy #define INSTR_callFastRtoR 0x8f #define INSTR_callFastGtoR 0x90 #define INSTR_notBoolean 0x91 #define INSTR_isTagged 0x92 #define INSTR_cellLength 0x93 #define INSTR_cellFlags 0x94 #define INSTR_clearMutable 0x95 #define INSTR_stringLength 0x96 #define INSTR_atomicIncr 0x97 #define INSTR_atomicDecr 0x98 #define INSTR_atomicReset 0x99 #define INSTR_longWToTagged 0x9a #define INSTR_signedToLongW 0x9b #define INSTR_unsignedToLongW 0x9c #define INSTR_realAbs 0x9d #define INSTR_realNeg 0x9e #define INSTR_fixedIntToReal 0x9f #define INSTR_equalWord 0xa0 #define INSTR_lessSigned 0xa2 #define INSTR_lessUnsigned 0xa3 #define INSTR_lessEqSigned 0xa4 #define INSTR_lessEqUnsigned 0xa5 #define INSTR_greaterSigned 0xa6 #define INSTR_greaterUnsigned 0xa7 #define INSTR_greaterEqSigned 0xa8 #define INSTR_greaterEqUnsigned 0xa9 #define INSTR_fixedAdd 0xaa #define INSTR_fixedSub 0xab #define INSTR_fixedMult 0xac #define INSTR_fixedQuot 0xad #define INSTR_fixedRem 0xae #define INSTR_fixedDiv 0xaf #define INSTR_fixedMod 0xb0 #define INSTR_wordAdd 0xb1 #define INSTR_wordSub 0xb2 #define INSTR_wordMult 0xb3 #define INSTR_wordDiv 0xb4 #define INSTR_wordMod 0xb5 #define INSTR_wordAnd 0xb7 #define INSTR_wordOr 0xb8 #define INSTR_wordXor 0xb9 #define INSTR_wordShiftLeft 0xba #define INSTR_wordShiftRLog 0xbb #define INSTR_wordShiftRArith 0xbc #define INSTR_allocByteMem 0xbd #define INSTR_lgWordEqual 0xbe #define INSTR_lgWordLess 0xc0 #define INSTR_lgWordLessEq 0xc1 #define INSTR_lgWordGreater 0xc2 #define INSTR_lgWordGreaterEq 0xc3 #define INSTR_lgWordAdd 0xc4 #define INSTR_lgWordSub 0xc5 #define INSTR_lgWordMult 0xc6 #define INSTR_lgWordDiv 0xc7 #define INSTR_lgWordMod 0xc8 #define INSTR_lgWordAnd 0xc9 #define INSTR_lgWordOr 0xca #define INSTR_lgWordXor 0xcb #define INSTR_lgWordShiftLeft 0xcc #define INSTR_lgWordShiftRLog 0xcd #define INSTR_lgWordShiftRArith 0xce #define INSTR_realEqual 0xcf #define INSTR_realLess 0xd1 #define INSTR_realLessEq 0xd2 #define INSTR_realGreater 0xd3 #define INSTR_realGreaterEq 0xd4 #define INSTR_realAdd 0xd5 #define INSTR_realSub 0xd6 #define INSTR_realMult 0xd7 #define INSTR_realDiv 0xd8 #define INSTR_getThreadId 0xd9 #define INSTR_allocWordMemory 0xda #define INSTR_loadMLWord 0xdb #define INSTR_loadMLByte 0xdc #define INSTR_loadC8 0xdd #define INSTR_loadC16 0xde #define INSTR_loadC32 0xdf #define INSTR_loadC64 0xe0 #define INSTR_loadCFloat 0xe1 #define INSTR_loadCDouble 0xe2 #define INSTR_storeMLWord 0xe3 #define INSTR_storeMLByte 0xe4 #define INSTR_storeC8 0xe5 #define INSTR_storeC16 0xe6 #define INSTR_storeC32 0xe7 #define INSTR_storeC64 0xe8 #define INSTR_storeCFloat 0xe9 #define INSTR_storeCDouble 0xea #define INSTR_blockMoveWord 0xeb #define INSTR_blockMoveByte 0xec #define INSTR_blockEqualByte 0xed #define INSTR_blockCompareByte 0xee #define INSTR_loadUntagged 0xef #define INSTR_storeUntagged 0xf0 #define INSTR_deleteHandler 0xf1 #define INSTR_jump32 0xf2 #define INSTR_jump32False 0xf3 #define INSTR_constAddr32 0xf4 #define INSTR_setHandler32 0xf5 #define INSTR_case32 0xf6 #define INSTR_jump16 0xf7 #define INSTR_jump16false 0xf8 #define INSTR_setHandler16 0xf9 #define INSTR_constAddr8 0xfa #define INSTR_stackSize8 0xfb #define INSTR_stackSize16 0xfc +#define INSTR_allocCSpace 0xfd +#define INSTR_freeCSpace 0xfe diff --git a/libpolyml/interpret.cpp b/libpolyml/interpret.cpp index 1c1ccc10..1a66fe9e 100644 --- a/libpolyml/interpret.cpp +++ b/libpolyml/interpret.cpp @@ -1,2335 +1,2510 @@ /* Title: An interpreter for a compact instruction set. Author: Dave Matthews, Cambridge University Computer Laboratory Copyright (c) 2000-7 Cambridge University Technical Services Limited Further development Copyright David C.J. Matthews 2015-18. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifdef HAVE_CONFIG_H #include "config.h" #elif defined(_WIN32) #include "winconfig.h" #else #error "No configuration file" #endif #ifdef HAVE_STDIO_H #include #endif #ifdef HAVE_ASSERT_H #include #define ASSERT(x) assert(x) #else #define ASSERT(x) 0 #endif #ifdef HAVE_STRING_H #include #endif #ifdef HAVE_FLOAT_H #include #endif #ifdef HAVE_MATH_H #include #endif #include // Currently just for isnan. #include "globals.h" #include "int_opcodes.h" #include "machine_dep.h" #include "sys.h" #include "profiling.h" #include "arb.h" #include "reals.h" #include "processes.h" #include "run_time.h" #include "gc.h" #include "diagnostics.h" #include "polystring.h" #include "save_vec.h" #include "memmgr.h" #include "scanaddrs.h" #include "rtsentry.h" #if (SIZEOF_VOIDP == 8) #define IS64BITS 1 #endif #define arg1 (pc[0] + pc[1]*256) #define arg2 (pc[2] + pc[3]*256) const PolyWord True = TAGGED(1); const PolyWord False = TAGGED(0); const PolyWord Zero = TAGGED(0); #define CHECKED_REGS 2 #define UNCHECKED_REGS 0 #define EXTRA_STACK 0 // Don't need any extra - signals aren't handled on the Poly stack. /* the amount of ML stack space to reserve for registers, C exception handling etc. The compiler requires us to reserve 2 stack-frames worth (2 * 20 words) plus whatever we require for the register save area. We actually reserve slightly more than this. SPF 3/3/97 */ #define OVERFLOW_STACK_SIZE \ (50 + \ CHECKED_REGS + \ UNCHECKED_REGS + \ EXTRA_STACK) // This duplicates some code in reals.cpp but is now updated. #define DOUBLESIZE (sizeof(double)/sizeof(POLYUNSIGNED)) union realdb { double dble; POLYUNSIGNED puns[DOUBLESIZE]; }; #define LGWORDSIZE (sizeof(uintptr_t) / sizeof(PolyWord)) // We're using float for Real32 so it needs to be 32-bits. // Assume that's true for the moment. #if (SIZEOF_FLOAT != 4) #error "Float is not 32-bits. Please report this" #endif union flt { float fl; int32_t i; }; class IntTaskData: public TaskData { public: IntTaskData(): interrupt_requested(false), overflowPacket(0), dividePacket(0) {} virtual void GarbageCollect(ScanAddress *process); void ScanStackAddress(ScanAddress *process, PolyWord &val, StackSpace *stack); virtual void EnterPolyCode(); // Start running ML // Switch to Poly and return with the io function to call. int SwitchToPoly(); virtual void SetException(poly_exn *exc); virtual void InterruptCode(); // AddTimeProfileCount is used in time profiling. virtual bool AddTimeProfileCount(SIGNALCONTEXT *context); virtual void InitStackFrame(TaskData *newTask, Handle proc, Handle arg); // Increment or decrement the first word of the object pointed to by the // mutex argument and return the new value. virtual Handle AtomicIncrement(Handle mutexp); // Set a mutex to one. virtual void AtomicReset(Handle mutexp); // Return the minimum space occupied by the stack. Used when setting a limit. virtual uintptr_t currentStackSpace(void) const { return (this->stack->top - this->taskSp) + OVERFLOW_STACK_SIZE; } virtual void addProfileCount(POLYUNSIGNED words) { add_count(this, taskPc, words); } virtual void CopyStackFrame(StackObject *old_stack, uintptr_t old_length, StackObject *new_stack, uintptr_t new_length); bool interrupt_requested; // Allocate memory on the heap. Returns with the address of the cell. Does not set the // length word or any of the data. PolyObject *allocateMemory(POLYUNSIGNED words, POLYCODEPTR &pc, PolyWord *&sp) { words++; // Add the size of the length word. // N.B. The allocation area may be empty so that both of these are zero. if (this->allocPointer >= this->allocLimit + words) { this->allocPointer -= words; return (PolyObject *)(this->allocPointer+1); } // Insufficient space. SaveInterpreterState(pc, sp); // Find some space to allocate in. Returns a pointer to the newly allocated space. // N.B. This may return zero if the heap is exhausted and it has set this // up for an exception. Generally it allocates by decrementing allocPointer // but if the required memory is large it may allocate in a separate area. PolyWord *space = processes->FindAllocationSpace(this, words, true); LoadInterpreterState(pc, sp); if (space == 0) return 0; return (PolyObject *)(space+1); } // Put a real result in a "box" PolyObject *boxDouble(double d, POLYCODEPTR &pc, PolyWord *&sp) { PolyObject *mem = this->allocateMemory(DOUBLESIZE, pc, sp); if (mem == 0) return 0; mem->SetLengthWord(DOUBLESIZE, F_BYTE_OBJ); union realdb uniondb; uniondb.dble = d; // Copy the words. Depending on the word length this may copy one or more words. for (unsigned i = 0; i < DOUBLESIZE; i++) mem->Set(i, PolyWord::FromUnsigned(uniondb.puns[i])); return mem; } // Extract a double value from a box. double unboxDouble(PolyWord p) { union realdb uniondb; for (unsigned i = 0; i < DOUBLESIZE; i++) uniondb.puns[i] = p.AsObjPtr()->Get(i).AsUnsigned(); return uniondb.dble; } // Largely copied from reals.cpp #if (SIZEOF_FLOAT < SIZEOF_POLYWORD) // Typically for 64-bit mode. Use a tagged representation. // The code-generator on the X86/64 assumes the float is in the // high order word. #define FLT_SHIFT ((SIZEOF_POLYWORD-SIZEOF_FLOAT)*8) float unboxFloat(PolyWord p) { union flt argx; argx.i = p.AsSigned() >> FLT_SHIFT; return argx.fl; } PolyObject *boxFloat(float f, POLYCODEPTR &pc, PolyWord *&sp) { union flt argx; argx.fl = f; PolyWord p = PolyWord::FromSigned(((POLYSIGNED)argx.i << FLT_SHIFT) + 1); return p.AsObjPtr(); // Temporarily cast it to this even though it isn't really } #else // Typically for 32-bit mode. Use a boxed representation. PolyObject *boxFloat(float f, POLYCODEPTR &pc, PolyWord *&sp) { PolyObject *mem = this->allocateMemory(1, pc, sp); if (mem == 0) return 0; mem->SetLengthWord(1, F_BYTE_OBJ); union flt argx; argx.fl = f; mem->Set(0, PolyWord::FromSigned(argx.i)); return mem; } // Extract a double value from a box. float unboxFloat(PolyWord p) { union flt argx; argx.i = (int32_t)p.AsObjPtr()->Get(0).AsSigned(); return argx.fl; } #endif // Update the copies in the task object void SaveInterpreterState(POLYCODEPTR pc, PolyWord *sp) { taskPc = pc; taskSp = sp; } // Update the local state void LoadInterpreterState(POLYCODEPTR &pc, PolyWord *&sp) { pc = taskPc; sp = taskSp; } POLYCODEPTR taskPc; /* Program counter. */ PolyWord *taskSp; /* Stack pointer. */ PolyWord *hr; PolyWord exception_arg; bool raiseException; PolyWord *sl; /* Stack limit register. */ PolyObject *overflowPacket, *dividePacket; }; // This lock is used to synchronise all atomic operations. // It is not needed in the X86 version because that can use a global // memory lock. static PLock mutexLock; // Special value for return address. #define SPECIAL_PC_END_THREAD TAGGED(1) class Interpreter : public MachineDependent { public: Interpreter() {} // Create a task data object. virtual TaskData *CreateTaskData(void) { return new IntTaskData(); } virtual Architectures MachineArchitecture(void) { return MA_Interpreted; } }; void IntTaskData::InitStackFrame(TaskData *parentTask, Handle proc, Handle arg) /* Initialise stack frame. */ { StackSpace *space = this->stack; StackObject *stack = (StackObject *)space->stack(); PolyObject *closure = DEREFWORDHANDLE(proc); uintptr_t stack_size = space->spaceSize(); this->taskPc = closure->Get(0).AsCodePtr(); this->exception_arg = TAGGED(0); /* Used for exception argument. */ this->taskSp = (PolyWord*)stack + stack_size; this->raiseException = false; /* Set up exception handler */ /* No previous handler so point it at itself. */ this->taskSp--; *(this->taskSp) = PolyWord::FromStackAddr(this->taskSp); *(--this->taskSp) = SPECIAL_PC_END_THREAD; /* Default return address. */ this->hr = this->taskSp; /* If this function takes an argument store it on the stack. */ if (arg != 0) *(--this->taskSp) = DEREFWORD(arg); *(--this->taskSp) = SPECIAL_PC_END_THREAD; /* Return address. */ *(--this->taskSp) = closure; /* Closure address */ // Make packets for exceptions. overflowPacket = makeExceptionPacket(parentTask, EXC_overflow); dividePacket = makeExceptionPacket(parentTask, EXC_divide); } extern "C" { typedef POLYUNSIGNED(*callFastRts0)(); typedef POLYUNSIGNED(*callFastRts1)(intptr_t); typedef POLYUNSIGNED(*callFastRts2)(intptr_t, intptr_t); typedef POLYUNSIGNED(*callFastRts3)(intptr_t, intptr_t, intptr_t); typedef POLYUNSIGNED(*callFastRts4)(intptr_t, intptr_t, intptr_t, intptr_t); typedef POLYUNSIGNED(*callFastRts5)(intptr_t, intptr_t, intptr_t, intptr_t, intptr_t); typedef POLYUNSIGNED(*callFullRts0)(PolyObject *); typedef POLYUNSIGNED(*callFullRts1)(PolyObject *, intptr_t); typedef POLYUNSIGNED(*callFullRts2)(PolyObject *, intptr_t, intptr_t); typedef POLYUNSIGNED(*callFullRts3)(PolyObject *, intptr_t, intptr_t, intptr_t); typedef double (*callRTSRtoR) (double); typedef double (*callRTSRRtoR) (double, double); typedef double (*callRTSGtoR) (intptr_t); typedef double (*callRTSRGtoR) (double, intptr_t); typedef float(*callRTSFtoF) (float); typedef float(*callRTSFFtoF) (float, float); typedef float(*callRTSGtoF) (intptr_t); typedef float(*callRTSFGtoF) (float, intptr_t); } void IntTaskData::InterruptCode() /* Stop the Poly code at a suitable place. */ /* We may get an asynchronous interrupt at any time. */ { IntTaskData *itd = (IntTaskData *)this; itd->interrupt_requested = true; } void IntTaskData::SetException(poly_exn *exc) /* Set up the stack of a process to raise an exception. */ { this->raiseException = true; *(--this->taskSp) = (PolyWord)exc; /* push exception data */ } int IntTaskData::SwitchToPoly() /* (Re)-enter the Poly code from C. */ { // These are temporary values used where one instruction jumps to // common code. POLYUNSIGNED tailCount; PolyWord *tailPtr; POLYUNSIGNED returnCount; POLYUNSIGNED storeWords; POLYUNSIGNED stackCheck; // Local values. These are copies of member variables but are used so frequently that // it is important that access should be fast. POLYCODEPTR pc; PolyWord *sp; double dv; LoadInterpreterState(pc, sp); sl = (PolyWord*)this->stack->stack() + OVERFLOW_STACK_SIZE; // We may have taken an interrupt which has set an exception. if (this->raiseException) goto RAISE_EXCEPTION; for(;;){ /* Each instruction */ // char buff[1000]; // sprintf(buff, "addr = %p sp=%p instr=%02x *sp=%p\n", pc, sp, *pc, (*sp).AsStackAddr()); // OutputDebugStringA(buff); switch(*pc++) { case INSTR_enter_int: pc++; /* Skip the argument. */ break; case INSTR_jump8false: { PolyWord u = *sp++; /* Pop argument */ if (u == True) { pc += 1; break; } /* else - false - take the jump */ } case INSTR_jump8: pc += *pc + 1; break; case INSTR_jump16false: { PolyWord u = *sp++; /* Pop argument */ if (u == True) { pc += 2; break; } /* else - false - take the jump */ } case INSTR_jump16: pc += arg1 + 2; break; case INSTR_jump32False: { PolyWord u = *sp++; /* Pop argument */ if (u == True) { pc += 4; break; } /* else - false - take the jump */ } case INSTR_jump32: { // This is a 32-bit signed quantity on both 64-bits and 32-bits. POLYSIGNED offset = pc[3] & 0x80 ? -1 : 0; offset = (offset << 8) | pc[3]; offset = (offset << 8) | pc[2]; offset = (offset << 8) | pc[1]; offset = (offset << 8) | pc[0]; pc += offset + 4; break; } case INSTR_push_handler: /* Save the old handler value. */ *(--sp) = PolyWord::FromStackAddr(this->hr); /* Push old handler */ break; case INSTR_setHandler8: /* Set up a handler */ *(--sp) = PolyWord::FromCodePtr(pc + *pc + 1); /* Address of handler */ this->hr = sp; pc += 1; break; case INSTR_setHandler16: /* Set up a handler */ *(--sp) = PolyWord::FromCodePtr(pc + arg1 + 2); /* Address of handler */ this->hr = sp; pc += 2; break; case INSTR_setHandler32: /* Set up a handler */ { POLYUNSIGNED offset = pc[0] + (pc[1] << 8) + (pc[2] << 16) + (pc[3] << 24); *(--sp) = PolyWord::FromCodePtr(pc + offset + 4); /* Address of handler */ this->hr = sp; pc += 4; break; } case INSTR_deleteHandler: /* Delete handler retaining the result. */ { PolyWord u = *sp++; sp = this->hr; sp++; // Remove handler entry point this->hr = (*sp).AsStackAddr(); // Restore old handler *sp = u; // Put back the result break; } case INSTR_case16: { // arg1 is the largest value that is in the range POLYSIGNED u = UNTAGGED(*sp++); /* Get the value */ if (u > arg1 || u < 0) pc += (arg1+2)*2; /* Out of range */ else { pc += 2; pc += /* Index */pc[u*2]+pc[u*2 + 1]*256; } break; } case INSTR_case32: { // arg1 is the number of cases i.e. one more than the largest value // This is followed by that number of 32-bit offsets. // If the value is out of range the default case is immediately after the table. POLYSIGNED u = UNTAGGED(*sp++); /* Get the value */ if (u >= arg1 || u < 0) pc += 2 + arg1 * 4; /* Out of range */ else { pc += 2; pc += /* Index */pc[u*4] + (pc[u*4+1] << 8) + (pc[u*4+2] << 16) + (pc[u*4+3] << 24); } break; } case INSTR_tail_3_b: tailCount = 3; tailPtr = sp + tailCount; sp = tailPtr + *pc; goto TAIL_CALL; case INSTR_tail_3_2: tailCount = 3; tailPtr = sp + tailCount; sp = tailPtr + 2; goto TAIL_CALL; case INSTR_tail_3_3: tailCount = 3; tailPtr = sp + tailCount; sp = tailPtr + 3; goto TAIL_CALL; case INSTR_tail_4_b: tailCount = 4; tailPtr = sp + tailCount; sp = tailPtr + *pc; goto TAIL_CALL; case INSTR_tail_b_b: tailCount = *pc; tailPtr = sp + tailCount; sp = tailPtr + pc[1]; goto TAIL_CALL; case INSTR_tail: /* Tail recursive call. */ /* Move items up the stack. */ /* There may be an overlap if the function we are calling has more args than this one. */ tailCount = arg1; tailPtr = sp + tailCount; sp = tailPtr + arg2; TAIL_CALL: /* For general case. */ if (tailCount < 2) Crash("Invalid argument\n"); for (; tailCount > 0; tailCount--) *(--sp) = *(--tailPtr); pc = (*sp++).AsCodePtr(); /* Pop the original return address. */ /* And drop through. */ case INSTR_call_closure: /* Closure call. */ { POLYCODEPTR newPc = (*sp).AsObjPtr()->Get(0).AsCodePtr(); sp--; *sp = sp[1]; /* Move closure up. */ sp[1] = PolyWord::FromCodePtr(pc); /* Save return address. */ pc = newPc; /* Get entry point. */ this->taskPc = pc; // Update in case we're profiling break; } case INSTR_return_w: returnCount = arg1; /* Get no. of args to remove. */ RETURN: /* Common code for return. */ { PolyWord result = *sp++; /* Result */ sp++; /* Remove the link/closure */ pc = (*sp++).AsCodePtr(); /* Return address */ sp += returnCount; /* Add on number of args. */ if (pc == SPECIAL_PC_END_THREAD.AsCodePtr()) exitThread(this); // This thread is exiting. *(--sp) = result; /* Result */ this->taskPc = pc; // Update in case we're profiling } break; case INSTR_return_b: returnCount = *pc; goto RETURN; case INSTR_return_0: returnCount = 0; goto RETURN; case INSTR_return_1: returnCount = 1; goto RETURN; case INSTR_return_2: returnCount = 2; goto RETURN; case INSTR_return_3: returnCount = 3; goto RETURN; case INSTR_stackSize8: stackCheck = *pc++; goto STACKCHECK; case INSTR_stackSize16: { stackCheck = arg1; pc += 2; STACKCHECK: // Check there is space on the stack if (sp - stackCheck < sl) { uintptr_t min_size = (this->stack->top - (PolyWord*)sp) + OVERFLOW_STACK_SIZE + stackCheck; SaveInterpreterState(pc, sp); CheckAndGrowStack(this, min_size); LoadInterpreterState(pc, sp); sl = (PolyWord*)this->stack->stack() + OVERFLOW_STACK_SIZE; } // Also check for interrupts if (this->interrupt_requested) { // Check for interrupts this->interrupt_requested = false; SaveInterpreterState(pc, sp); return -1; } break; } case INSTR_pad: /* No-op */ break; case INSTR_raise_ex: { RAISE_EXCEPTION: this->raiseException = false; PolyException *exn = (PolyException*)((*sp).AsObjPtr()); this->exception_arg = exn; /* Get exception data */ sp = this->hr; if (*sp == SPECIAL_PC_END_THREAD) exitThread(this); // Default handler for thread. pc = (*sp++).AsCodePtr(); this->hr = (*sp++).AsStackAddr(); break; } case INSTR_get_store_w: // Get_store is now only used for mutually recursive closures. It allocates mutable store // initialised to zero. { storeWords = arg1; pc += 2; GET_STORE: PolyObject *p = this->allocateMemory(storeWords, pc, sp); if (p == 0) goto RAISE_EXCEPTION; p->SetLengthWord(storeWords, F_MUTABLE_BIT); for(; storeWords > 0; ) p->Set(--storeWords, TAGGED(0)); /* Must initialise store! */ *(--sp) = (PolyWord)p; break; } case INSTR_get_store_2: storeWords = 2; goto GET_STORE; case INSTR_get_store_3: storeWords = 3; goto GET_STORE; case INSTR_get_store_4: storeWords = 4; goto GET_STORE; case INSTR_get_store_b: storeWords = *pc; pc++; goto GET_STORE; case INSTR_tuple_w: { storeWords = arg1; pc += 2; TUPLE: /* Common code for tupling. */ PolyObject *p = this->allocateMemory(storeWords, pc, sp); if (p == 0) goto RAISE_EXCEPTION; // Exception p->SetLengthWord(storeWords, 0); for(; storeWords > 0; ) p->Set(--storeWords, *sp++); *(--sp) = (PolyWord)p; break; } case INSTR_tuple_2: storeWords = 2; goto TUPLE; case INSTR_tuple_3: storeWords = 3; goto TUPLE; case INSTR_tuple_4: storeWords = 4; goto TUPLE; case INSTR_tuple_b: storeWords = *pc; pc++; goto TUPLE; case INSTR_local_w: { PolyWord u = sp[arg1]; *(--sp) = u; pc += 2; break; } case INSTR_indirect_w: *sp = (*sp).AsObjPtr()->Get(arg1); pc += 2; break; case INSTR_move_to_vec_w: { PolyWord u = *sp++; (*sp).AsObjPtr()->Set(arg1, u); pc += 2; break; } case INSTR_set_stack_val_w: { PolyWord u = *sp++; sp[arg1-1] = u; pc += 2; break; } case INSTR_reset_w: sp += arg1; pc += 2; break; case INSTR_reset_r_w: { PolyWord u = *sp; sp += arg1; *sp = u; pc += 2; break; } case INSTR_constAddr8: *(--sp) = *(PolyWord*)(pc + pc[0] + 1); pc += 1; break; case INSTR_constAddr16: *(--sp) = *(PolyWord*)(pc + arg1 + 2); pc += 2; break; case INSTR_constAddr32: { POLYUNSIGNED offset = pc[0] + (pc[1] << 8) + (pc[2] << 16) + (pc[3] << 24); *(--sp) = *(PolyWord*)(pc + offset + 4); pc += 4; break; } case INSTR_const_int_w: *(--sp) = TAGGED(arg1); pc += 2; break; case INSTR_jump_back8: pc -= *pc + 1; if (this->interrupt_requested) { // Check for interrupt in case we're in a loop this->interrupt_requested = false; SaveInterpreterState(pc, sp); return -1; } break; case INSTR_jump_back16: pc -= arg1 + 1; if (this->interrupt_requested) { // Check for interrupt in case we're in a loop this->interrupt_requested = false; SaveInterpreterState(pc, sp); return -1; } break; case INSTR_lock: { PolyObject *obj = (*sp).AsObjPtr(); obj->SetLengthWord(obj->LengthWord() & ~_OBJ_MUTABLE_BIT); break; } case INSTR_ldexc: *(--sp) = this->exception_arg; break; case INSTR_local_b: { PolyWord u = sp[*pc]; *(--sp) = u; pc += 1; break; } case INSTR_indirect_b: *sp = (*sp).AsObjPtr()->Get(*pc); pc += 1; break; case INSTR_move_to_vec_b: { PolyWord u = *sp++; (*sp).AsObjPtr()->Set(*pc, u); pc += 1; break; } case INSTR_set_stack_val_b: { PolyWord u = *sp++; sp[*pc-1] = u; pc += 1; break; } case INSTR_reset_b: sp += *pc; pc += 1; break; case INSTR_reset_r_b: { PolyWord u = *sp; sp += *pc; *sp = u; pc += 1; break; } case INSTR_const_int_b: *(--sp) = TAGGED(*pc); pc += 1; break; case INSTR_local_0: { PolyWord u = sp[0]; *(--sp) = u; break; } case INSTR_local_1: { PolyWord u = sp[1]; *(--sp) = u; break; } case INSTR_local_2: { PolyWord u = sp[2]; *(--sp) = u; break; } case INSTR_local_3: { PolyWord u = sp[3]; *(--sp) = u; break; } case INSTR_local_4: { PolyWord u = sp[4]; *(--sp) = u; break; } case INSTR_local_5: { PolyWord u = sp[5]; *(--sp) = u; break; } case INSTR_local_6: { PolyWord u = sp[6]; *(--sp) = u; break; } case INSTR_local_7: { PolyWord u = sp[7]; *(--sp) = u; break; } case INSTR_local_8: { PolyWord u = sp[8]; *(--sp) = u; break; } case INSTR_local_9: { PolyWord u = sp[9]; *(--sp) = u; break; } case INSTR_local_10: { PolyWord u = sp[10]; *(--sp) = u; break; } case INSTR_local_11: { PolyWord u = sp[11]; *(--sp) = u; break; } case INSTR_indirect_0: *sp = (*sp).AsObjPtr()->Get(0); break; case INSTR_indirect_1: *sp = (*sp).AsObjPtr()->Get(1); break; case INSTR_indirect_2: *sp = (*sp).AsObjPtr()->Get(2); break; case INSTR_indirect_3: *sp = (*sp).AsObjPtr()->Get(3); break; case INSTR_indirect_4: *sp = (*sp).AsObjPtr()->Get(4); break; case INSTR_indirect_5: *sp = (*sp).AsObjPtr()->Get(5); break; case INSTR_const_0: *(--sp) = Zero; break; case INSTR_const_1: *(--sp) = TAGGED(1); break; case INSTR_const_2: *(--sp) = TAGGED(2); break; case INSTR_const_3: *(--sp) = TAGGED(3); break; case INSTR_const_4: *(--sp) = TAGGED(4); break; case INSTR_const_10: *(--sp) = TAGGED(10); break; case INSTR_reset_r_1: { PolyWord u = *sp; sp += 1; *sp = u; break; } case INSTR_reset_r_2: { PolyWord u = *sp; sp += 2; *sp = u; break; } case INSTR_reset_r_3: { PolyWord u = *sp; sp += 3; *sp = u; break; } case INSTR_reset_1: sp += 1; break; case INSTR_reset_2: sp += 2; break; case INSTR_stack_container: { POLYUNSIGNED words = arg1; pc += 2; while (words-- > 0) *(--sp) = Zero; sp--; *sp = PolyWord::FromStackAddr(sp + 1); break; } case INSTR_tuple_container: /* Create a tuple from a container. */ { storeWords = arg1; PolyObject *t = this->allocateMemory(storeWords, pc, sp); if (t == 0) goto RAISE_EXCEPTION; t->SetLengthWord(storeWords, 0); for(; storeWords > 0; ) { storeWords--; t->Set(storeWords, (*sp).AsObjPtr()->Get(storeWords)); } *sp = t; pc += 2; break; } case INSTR_callFastRTS0: { callFastRts0 doCall = *(callFastRts0*)(*sp++).AsObjPtr(); + this->raiseException = false; + SaveInterpreterState(pc, sp); POLYUNSIGNED result = doCall(); + LoadInterpreterState(pc, sp); + // If this raised an exception + if (this->raiseException) goto RAISE_EXCEPTION; *(--sp) = PolyWord::FromUnsigned(result); break; } case INSTR_callFastRTS1: { callFastRts1 doCall = *(callFastRts1*)(*sp++).AsObjPtr(); intptr_t rtsArg1 = (*sp++).AsSigned(); + this->raiseException = false; + SaveInterpreterState(pc, sp); POLYUNSIGNED result = doCall(rtsArg1); + LoadInterpreterState(pc, sp); + // If this raised an exception + if (this->raiseException) goto RAISE_EXCEPTION; *(--sp) = PolyWord::FromUnsigned(result); break; } case INSTR_callFastRTS2: { callFastRts2 doCall = *(callFastRts2*)(*sp++).AsObjPtr(); intptr_t rtsArg2 = (*sp++).AsSigned(); // Pop off the args, last arg first. intptr_t rtsArg1 = (*sp++).AsSigned(); + this->raiseException = false; + SaveInterpreterState(pc, sp); POLYUNSIGNED result = doCall(rtsArg1, rtsArg2); + LoadInterpreterState(pc, sp); + // If this raised an exception + if (this->raiseException) goto RAISE_EXCEPTION; *(--sp) = PolyWord::FromUnsigned(result); break; } case INSTR_callFastRTS3: { callFastRts3 doCall = *(callFastRts3*)(*sp++).AsObjPtr(); intptr_t rtsArg3 = (*sp++).AsSigned(); // Pop off the args, last arg first. intptr_t rtsArg2 = (*sp++).AsSigned(); intptr_t rtsArg1 = (*sp++).AsSigned(); + this->raiseException = false; + SaveInterpreterState(pc, sp); POLYUNSIGNED result = doCall(rtsArg1, rtsArg2, rtsArg3); + LoadInterpreterState(pc, sp); + // If this raised an exception + if (this->raiseException) goto RAISE_EXCEPTION; *(--sp) = PolyWord::FromUnsigned(result); break; } case INSTR_callFastRTS4: { callFastRts4 doCall = *(callFastRts4*)(*sp++).AsObjPtr(); intptr_t rtsArg4 = (*sp++).AsSigned(); // Pop off the args, last arg first. intptr_t rtsArg3 = (*sp++).AsSigned(); intptr_t rtsArg2 = (*sp++).AsSigned(); intptr_t rtsArg1 = (*sp++).AsSigned(); + this->raiseException = false; + SaveInterpreterState(pc, sp); POLYUNSIGNED result = doCall(rtsArg1, rtsArg2, rtsArg3, rtsArg4); + LoadInterpreterState(pc, sp); + // If this raised an exception + if (this->raiseException) goto RAISE_EXCEPTION; *(--sp) = PolyWord::FromUnsigned(result); break; } case INSTR_callFastRTS5: { callFastRts5 doCall = *(callFastRts5*)(*sp++).AsObjPtr(); intptr_t rtsArg5 = (*sp++).AsSigned(); // Pop off the args, last arg first. intptr_t rtsArg4 = (*sp++).AsSigned(); intptr_t rtsArg3 = (*sp++).AsSigned(); intptr_t rtsArg2 = (*sp++).AsSigned(); intptr_t rtsArg1 = (*sp++).AsSigned(); + this->raiseException = false; + SaveInterpreterState(pc, sp); POLYUNSIGNED result = doCall(rtsArg1, rtsArg2, rtsArg3, rtsArg4, rtsArg5); + LoadInterpreterState(pc, sp); + // If this raised an exception + if (this->raiseException) goto RAISE_EXCEPTION; *(--sp) = PolyWord::FromUnsigned(result); break; } case INSTR_callFullRTS0: { callFullRts0 doCall = *(callFullRts0*)(*sp++).AsObjPtr(); this->raiseException = false; SaveInterpreterState(pc, sp); POLYUNSIGNED result = doCall(this->threadObject); LoadInterpreterState(pc, sp); // If this raised an exception if (this->raiseException) goto RAISE_EXCEPTION; *(--sp)= PolyWord::FromUnsigned(result); break; } case INSTR_callFullRTS1: { callFullRts1 doCall = *(callFullRts1*)(*sp++).AsObjPtr(); intptr_t rtsArg1 = (*sp++).AsSigned(); this->raiseException = false; SaveInterpreterState(pc, sp); POLYUNSIGNED result = doCall(this->threadObject, rtsArg1); LoadInterpreterState(pc, sp); // If this raised an exception if (this->raiseException) goto RAISE_EXCEPTION; *(--sp) = PolyWord::FromUnsigned(result); break; } case INSTR_callFullRTS2: { callFullRts2 doCall = *(callFullRts2*)(*sp++).AsObjPtr(); intptr_t rtsArg2 = (*sp++).AsSigned(); // Pop off the args, last arg first. intptr_t rtsArg1 = (*sp++).AsSigned(); this->raiseException = false; SaveInterpreterState(pc, sp); POLYUNSIGNED result = doCall(this->threadObject, rtsArg1, rtsArg2); LoadInterpreterState(pc, sp); // If this raised an exception if (this->raiseException) goto RAISE_EXCEPTION; *(--sp) = PolyWord::FromUnsigned(result); break; } case INSTR_callFullRTS3: { callFullRts3 doCall = *(callFullRts3*)(*sp++).AsObjPtr(); intptr_t rtsArg3 = (*sp++).AsSigned(); // Pop off the args, last arg first. intptr_t rtsArg2 = (*sp++).AsSigned(); intptr_t rtsArg1 = (*sp++).AsSigned(); this->raiseException = false; SaveInterpreterState(pc, sp); POLYUNSIGNED result = doCall(this->threadObject, rtsArg1, rtsArg2, rtsArg3); LoadInterpreterState(pc, sp); // If this raised an exception if (this->raiseException) goto RAISE_EXCEPTION; *(--sp) = PolyWord::FromUnsigned(result); break; } case INSTR_callFastRtoR: { // Floating point call. The call itself does not allocate but we // need to put the result into a "box". callRTSRtoR doCall = *(callRTSRtoR*)(*sp++).AsObjPtr(); PolyWord rtsArg1 = *sp++; double argument = unboxDouble(rtsArg1); // Allocate memory for the result. double result = doCall(argument); PolyObject *t = boxDouble(result, pc, sp); if (t == 0) goto RAISE_EXCEPTION; *(--sp) = (PolyWord)t; break; } case INSTR_callFastRRtoR: { // Floating point call. PolyWord rtsCall = (*sp++).AsObjPtr()->Get(0); // Value holds address. PolyWord rtsArg2 = *sp++; PolyWord rtsArg1 = *sp++; callRTSRRtoR doCall = (callRTSRRtoR)rtsCall.AsCodePtr(); double argument1 = unboxDouble(rtsArg1); double argument2 = unboxDouble(rtsArg2); // Allocate memory for the result. double result = doCall(argument1, argument2); PolyObject *t = boxDouble(result, pc, sp); if (t == 0) goto RAISE_EXCEPTION; *(--sp) = t; break; } case INSTR_callFastGtoR: { // Call that takes a POLYUNSIGNED argument and returns a double. callRTSGtoR doCall = *(callRTSGtoR*)(*sp++).AsObjPtr(); intptr_t rtsArg1 = (*sp++).AsSigned(); // Allocate memory for the result. double result = doCall(rtsArg1); PolyObject *t = boxDouble(result, pc, sp); if (t == 0) goto RAISE_EXCEPTION; *(--sp) = (PolyWord)t; break; } case INSTR_callFastRGtoR: { // Call that takes a POLYUNSIGNED argument and returns a double. PolyWord rtsCall = (*sp++).AsObjPtr()->Get(0); // Value holds address. intptr_t rtsArg2 = (*sp++).AsSigned(); PolyWord rtsArg1 = *sp++; callRTSRGtoR doCall = (callRTSRGtoR)rtsCall.AsCodePtr(); double argument1 = unboxDouble(rtsArg1); // Allocate memory for the result. double result = doCall(argument1, rtsArg2); PolyObject *t = boxDouble(result, pc, sp); if (t == 0) goto RAISE_EXCEPTION; *(--sp) = t; break; } case INSTR_callFastFtoF: { // Floating point call. The call itself does not allocate but we // need to put the result into a "box". PolyWord rtsCall = (*sp++).AsObjPtr()->Get(0); // Value holds address. PolyWord rtsArg1 = *sp++; callRTSFtoF doCall = (callRTSFtoF)rtsCall.AsCodePtr(); float argument = unboxFloat(rtsArg1); // Allocate memory for the result. float result = doCall(argument); PolyObject *t = boxFloat(result, pc, sp); if (t == 0) goto RAISE_EXCEPTION; *(--sp) = t; break; } case INSTR_callFastFFtoF: { // Floating point call. PolyWord rtsCall = (*sp++).AsObjPtr()->Get(0); // Value holds address. PolyWord rtsArg2 = *sp++; PolyWord rtsArg1 = *sp++; callRTSFFtoF doCall = (callRTSFFtoF)rtsCall.AsCodePtr(); float argument1 = unboxFloat(rtsArg1); float argument2 = unboxFloat(rtsArg2); // Allocate memory for the result. float result = doCall(argument1, argument2); PolyObject *t = boxFloat(result, pc, sp); if (t == 0) goto RAISE_EXCEPTION; *(--sp) = t; break; } case INSTR_callFastGtoF: { // Call that takes a POLYUNSIGNED argument and returns a double. PolyWord rtsCall = (*sp++).AsObjPtr()->Get(0); // Value holds address. intptr_t rtsArg1 = (*sp++).AsSigned(); callRTSGtoF doCall = (callRTSGtoF)rtsCall.AsCodePtr(); // Allocate memory for the result. float result = doCall(rtsArg1); PolyObject *t = boxFloat(result, pc, sp); if (t == 0) goto RAISE_EXCEPTION; *(--sp) = t; break; } case INSTR_callFastFGtoF: { // Call that takes a POLYUNSIGNED argument and returns a double. PolyWord rtsCall = (*sp++).AsObjPtr()->Get(0); // Value holds address. intptr_t rtsArg2 = (*sp++).AsSigned(); PolyWord rtsArg1 = *sp++; callRTSFGtoF doCall = (callRTSFGtoF)rtsCall.AsCodePtr(); float argument1 = unboxFloat(rtsArg1); // Allocate memory for the result. float result = doCall(argument1, rtsArg2); PolyObject *t = boxFloat(result, pc, sp); if (t == 0) goto RAISE_EXCEPTION; *(--sp) = t; break; } case INSTR_notBoolean: *sp = ((*sp) == True) ? False : True; break; case INSTR_isTagged: *sp = (*sp).IsTagged() ? True : False; break; case INSTR_cellLength: /* Return the length word. */ *sp = TAGGED((*sp).AsObjPtr()->Length()); break; case INSTR_cellFlags: { PolyObject *p = (*sp).AsObjPtr(); POLYUNSIGNED f = (p->LengthWord()) >> OBJ_PRIVATE_FLAGS_SHIFT; *sp = TAGGED(f); break; } case INSTR_clearMutable: { PolyObject *obj = (*sp).AsObjPtr(); POLYUNSIGNED lengthW = obj->LengthWord(); /* Clear the mutable bit. */ obj->SetLengthWord(lengthW & ~_OBJ_MUTABLE_BIT); *sp = Zero; break; } case INSTR_stringLength: // Now replaced by loadUntagged *sp = TAGGED(((PolyStringObject*)(*sp).AsObjPtr())->length); break; case INSTR_atomicIncr: { PLocker l(&mutexLock); PolyObject *p = (*sp).AsObjPtr(); PolyWord newValue = TAGGED(UNTAGGED(p->Get(0))+1); p->Set(0, newValue); *sp = newValue; break; } case INSTR_atomicDecr: { PLocker l(&mutexLock); PolyObject *p = (*sp).AsObjPtr(); PolyWord newValue = TAGGED(UNTAGGED(p->Get(0))-1); p->Set(0, newValue); *sp = newValue; break; } case INSTR_atomicReset: { // This is needed in the interpreted version otherwise there // is a chance that we could set the value to zero while another // thread is between getting the old value and setting it to the new value. PLocker l(&mutexLock); PolyObject *p = (*sp).AsObjPtr(); p->Set(0, TAGGED(1)); // Set this to released. *sp = TAGGED(0); // Push the unit result break; } case INSTR_longWToTagged: { // Extract the first word and return it as a tagged value. This loses the top-bit POLYUNSIGNED wx = (*sp).AsObjPtr()->Get(0).AsUnsigned(); *sp = TAGGED(wx); break; } case INSTR_signedToLongW: { // Shift the tagged value to remove the tag and put it into the first word. // The original sign bit is copied in the shift. intptr_t wx = (*sp).UnTagged(); PolyObject *t = this->allocateMemory(LGWORDSIZE, pc, sp); if (t == 0) goto RAISE_EXCEPTION; t->SetLengthWord(LGWORDSIZE, F_BYTE_OBJ); *(intptr_t*)t = wx; *sp = (PolyWord)t; break; } case INSTR_unsignedToLongW: { // As with the above except the value is treated as an unsigned // value and the top bit is zero. uintptr_t wx = (*sp).UnTaggedUnsigned(); PolyObject *t = this->allocateMemory(LGWORDSIZE, pc, sp); if (t == 0) goto RAISE_EXCEPTION; t->SetLengthWord(LGWORDSIZE, F_BYTE_OBJ); *(uintptr_t*)t = wx; *sp = (PolyWord)t; break; } case INSTR_realAbs: { PolyObject *t = this->boxDouble(fabs(unboxDouble(*sp)), pc, sp); if (t == 0) goto RAISE_EXCEPTION; *sp = (PolyWord)t; break; } case INSTR_realNeg: { PolyObject *t = this->boxDouble(-(unboxDouble(*sp)), pc, sp); if (t == 0) goto RAISE_EXCEPTION; *sp = (PolyWord)t; break; } case INSTR_floatAbs: { PolyObject *t = this->boxFloat(fabs(unboxFloat(*sp)), pc, sp); if (t == 0) goto RAISE_EXCEPTION; *sp = t; break; } case INSTR_floatNeg: { PolyObject *t = this->boxFloat(-(unboxFloat(*sp)), pc, sp); if (t == 0) goto RAISE_EXCEPTION; *sp = t; break; } case INSTR_fixedIntToReal: { POLYSIGNED u = UNTAGGED(*sp); PolyObject *t = this->boxDouble((double)u, pc, sp); if (t == 0) goto RAISE_EXCEPTION; *sp = (PolyWord)t; break; } case INSTR_fixedIntToFloat: { POLYSIGNED u = UNTAGGED(*sp); PolyObject *t = this->boxFloat((float)u, pc, sp); if (t == 0) goto RAISE_EXCEPTION; *sp = t; break; } case INSTR_floatToReal: { float u = unboxFloat(*sp); PolyObject *t = this->boxDouble((double)u, pc, sp); if (t == 0) goto RAISE_EXCEPTION; *sp = t; break; } case INSTR_equalWord: { PolyWord u = *sp++; *sp = u == (*sp) ? True : False; break; } case INSTR_lessSigned: { PolyWord u = *sp++; *sp = ((*sp).AsSigned() < u.AsSigned()) ? True : False; break; } case INSTR_lessUnsigned: { PolyWord u = *sp++; *sp = ((*sp).AsUnsigned() < u.AsUnsigned()) ? True : False; break; } case INSTR_lessEqSigned: { PolyWord u = *sp++; *sp = ((*sp).AsSigned() <= u.AsSigned()) ? True : False; break; } case INSTR_lessEqUnsigned: { PolyWord u = *sp++; *sp = ((*sp).AsUnsigned() <= u.AsUnsigned()) ? True : False; break; } case INSTR_greaterSigned: { PolyWord u = *sp++; *sp = ((*sp).AsSigned() > u.AsSigned()) ? True : False; break; } case INSTR_greaterUnsigned: { PolyWord u = *sp++; *sp = ((*sp).AsUnsigned() > u.AsUnsigned()) ? True : False; break; } case INSTR_greaterEqSigned: { PolyWord u = *sp++; *sp = ((*sp).AsSigned() >= u.AsSigned()) ? True : False; break; } case INSTR_greaterEqUnsigned: { PolyWord u = *sp++; *sp = ((*sp).AsUnsigned() >= u.AsUnsigned()) ? True : False; break; } case INSTR_fixedAdd: { PolyWord x = *sp++; PolyWord y = (*sp); POLYSIGNED t = UNTAGGED(x) + UNTAGGED(y); if (t <= MAXTAGGED && t >= -MAXTAGGED-1) *sp = TAGGED(t); else { *(--sp) = (PolyWord)overflowPacket; goto RAISE_EXCEPTION; } break; } case INSTR_fixedSub: { PolyWord x = *sp++; PolyWord y = (*sp); POLYSIGNED t = UNTAGGED(y) - UNTAGGED(x); if (t <= MAXTAGGED && t >= -MAXTAGGED-1) *sp = TAGGED(t); else { *(--sp) = (PolyWord)overflowPacket; goto RAISE_EXCEPTION; } break; } case INSTR_fixedMult: { POLYSIGNED x = UNTAGGED(*sp++); POLYSIGNED y = (*sp).AsSigned() - 1; // Just remove the tag POLYSIGNED t = x * y; if (x != 0 && t / x != y) { *(--sp) = (PolyWord)overflowPacket; goto RAISE_EXCEPTION; } *sp = PolyWord::FromSigned(t+1); // Add back the tag break; } case INSTR_fixedQuot: { // Zero and overflow are checked for in ML. POLYSIGNED u = UNTAGGED(*sp++); PolyWord y = (*sp); *sp = TAGGED(UNTAGGED(y) / u); break; } case INSTR_fixedRem: { // Zero and overflow are checked for in ML. POLYSIGNED u = UNTAGGED(*sp++); PolyWord y = (*sp); *sp = TAGGED(UNTAGGED(y) % u); break; } case INSTR_wordAdd: { PolyWord u = *sp++; // Because we're not concerned with overflow we can just add the values and subtract the tag. *sp = PolyWord::FromUnsigned((*sp).AsUnsigned() + u.AsUnsigned() - TAGGED(0).AsUnsigned()); break; } case INSTR_wordSub: { PolyWord u = *sp++; *sp = PolyWord::FromUnsigned((*sp).AsUnsigned() - u.AsUnsigned() + TAGGED(0).AsUnsigned()); break; } case INSTR_wordMult: { PolyWord u = *sp++; *sp = TAGGED(UNTAGGED_UNSIGNED(*sp) * UNTAGGED_UNSIGNED(u)); break; } case INSTR_wordDiv: { POLYUNSIGNED u = UNTAGGED_UNSIGNED(*sp++); // Detection of zero is done in ML *sp = TAGGED(UNTAGGED_UNSIGNED(*sp) / u); break; } case INSTR_wordMod: { POLYUNSIGNED u = UNTAGGED_UNSIGNED(*sp++); *sp = TAGGED(UNTAGGED_UNSIGNED(*sp) % u); break; } case INSTR_wordAnd: { PolyWord u = *sp++; // Since both of these should be tagged the tag bit will be preserved. *sp = PolyWord::FromUnsigned((*sp).AsUnsigned() & u.AsUnsigned()); break; } case INSTR_wordOr: { PolyWord u = *sp++; // Since both of these should be tagged the tag bit will be preserved. *sp = PolyWord::FromUnsigned((*sp).AsUnsigned() | u.AsUnsigned()); break; } case INSTR_wordXor: { PolyWord u = *sp++; // This will remove the tag bit so it has to be reinstated. *sp = PolyWord::FromUnsigned(((*sp).AsUnsigned() ^ u.AsUnsigned()) | TAGGED(0).AsUnsigned()); break; } case INSTR_wordShiftLeft: { // ML requires shifts greater than a word to return zero. // That's dealt with at the higher level. PolyWord u = *sp++; *sp = TAGGED(UNTAGGED_UNSIGNED(*sp) << UNTAGGED_UNSIGNED(u)); break; } case INSTR_wordShiftRLog: { PolyWord u = *sp++; *sp = TAGGED(UNTAGGED_UNSIGNED(*sp) >> UNTAGGED_UNSIGNED(u)); break; } case INSTR_wordShiftRArith: { PolyWord u = *sp++; // Strictly speaking, C does not require that this uses // arithmetic shifting so we really ought to set the // high-order bits explicitly. *sp = TAGGED(UNTAGGED(*sp) >> UNTAGGED(u)); break; } case INSTR_allocByteMem: { // Allocate byte segment. This does not need to be initialised. POLYUNSIGNED flags = UNTAGGED_UNSIGNED(*sp++); POLYUNSIGNED length = UNTAGGED_UNSIGNED(*sp); PolyObject *t = this->allocateMemory(length, pc, sp); if (t == 0) goto RAISE_EXCEPTION; // Exception t->SetLengthWord(length, (byte)flags); *sp = (PolyWord)t; break; } case INSTR_lgWordEqual: { uintptr_t wx = *(uintptr_t*)((*sp++).AsObjPtr()); uintptr_t wy = *(uintptr_t*)((*sp).AsObjPtr()); *sp = wx == wy ? True : False; break; } case INSTR_lgWordLess: { uintptr_t wx = *(uintptr_t*)((*sp++).AsObjPtr()); uintptr_t wy = *(uintptr_t*)((*sp).AsObjPtr()); *sp = (wy < wx) ? True : False; break; } case INSTR_lgWordLessEq: { uintptr_t wx = *(uintptr_t*)((*sp++).AsObjPtr()); uintptr_t wy = *(uintptr_t*)((*sp).AsObjPtr()); *sp = (wy <= wx) ? True : False; break; } case INSTR_lgWordGreater: { uintptr_t wx = *(uintptr_t*)((*sp++).AsObjPtr()); uintptr_t wy = *(uintptr_t*)((*sp).AsObjPtr()); *sp = (wy > wx) ? True : False; break; } case INSTR_lgWordGreaterEq: { uintptr_t wx = *(uintptr_t*)((*sp++).AsObjPtr()); uintptr_t wy = *(uintptr_t*)((*sp).AsObjPtr()); *sp = (wy >= wx) ? True : False; break; } case INSTR_lgWordAdd: { uintptr_t wx = *(uintptr_t*)((*sp++).AsObjPtr()); uintptr_t wy = *(uintptr_t*)((*sp).AsObjPtr()); PolyObject *t = this->allocateMemory(LGWORDSIZE, pc, sp); if (t == 0) goto RAISE_EXCEPTION; t->SetLengthWord(LGWORDSIZE, F_BYTE_OBJ); *(uintptr_t*)t = wy+wx; *sp = (PolyWord)t; break; } case INSTR_lgWordSub: { uintptr_t wx = *(uintptr_t*)((*sp++).AsObjPtr()); uintptr_t wy = *(uintptr_t*)((*sp).AsObjPtr()); PolyObject *t = this->allocateMemory(LGWORDSIZE, pc, sp); if (t == 0) goto RAISE_EXCEPTION; t->SetLengthWord(LGWORDSIZE, F_BYTE_OBJ); *(uintptr_t*)t = wy-wx; *sp = (PolyWord)t; break; } case INSTR_lgWordMult: { uintptr_t wx = *(uintptr_t*)((*sp++).AsObjPtr()); uintptr_t wy = *(uintptr_t*)((*sp).AsObjPtr()); PolyObject *t = this->allocateMemory(LGWORDSIZE, pc, sp); if (t == 0) goto RAISE_EXCEPTION; t->SetLengthWord(LGWORDSIZE, F_BYTE_OBJ); *(uintptr_t*)t = wy*wx; *sp = (PolyWord)t; break; } case INSTR_lgWordDiv: { uintptr_t wx = *(uintptr_t*)((*sp++).AsObjPtr()); uintptr_t wy = *(uintptr_t*)((*sp).AsObjPtr()); PolyObject *t = this->allocateMemory(LGWORDSIZE, pc, sp); if (t == 0) goto RAISE_EXCEPTION; t->SetLengthWord(LGWORDSIZE, F_BYTE_OBJ); *(uintptr_t*)t = wy/wx; *sp = (PolyWord)t; break; } case INSTR_lgWordMod: { uintptr_t wx = *(uintptr_t*)((*sp++).AsObjPtr()); uintptr_t wy = *(uintptr_t*)((*sp).AsObjPtr()); PolyObject *t = this->allocateMemory(LGWORDSIZE, pc, sp); if (t == 0) goto RAISE_EXCEPTION; t->SetLengthWord(LGWORDSIZE, F_BYTE_OBJ); *(uintptr_t*)t = wy%wx; *sp = (PolyWord)t; break; } case INSTR_lgWordAnd: { uintptr_t wx = *(uintptr_t*)((*sp++).AsObjPtr()); uintptr_t wy = *(uintptr_t*)((*sp).AsObjPtr()); PolyObject *t = this->allocateMemory(LGWORDSIZE, pc, sp); if (t == 0) goto RAISE_EXCEPTION; t->SetLengthWord(LGWORDSIZE, F_BYTE_OBJ); *(uintptr_t*)t = wy&wx; *sp = (PolyWord)t; break; } case INSTR_lgWordOr: { uintptr_t wx = *(uintptr_t*)((*sp++).AsObjPtr()); uintptr_t wy = *(uintptr_t*)((*sp).AsObjPtr()); PolyObject *t = this->allocateMemory(LGWORDSIZE, pc, sp); if (t == 0) goto RAISE_EXCEPTION; t->SetLengthWord(LGWORDSIZE, F_BYTE_OBJ); *(uintptr_t*)t = wy|wx; *sp = (PolyWord)t; break; } case INSTR_lgWordXor: { uintptr_t wx = *(uintptr_t*)((*sp++).AsObjPtr()); uintptr_t wy = *(uintptr_t*)((*sp).AsObjPtr()); PolyObject *t = this->allocateMemory(LGWORDSIZE, pc, sp); if (t == 0) goto RAISE_EXCEPTION; t->SetLengthWord(LGWORDSIZE, F_BYTE_OBJ); *(uintptr_t*)t = wy^wx; *sp = (PolyWord)t; break; } case INSTR_lgWordShiftLeft: { // The shift amount is a tagged word not a boxed large word POLYUNSIGNED wx = UNTAGGED_UNSIGNED(*sp++); uintptr_t wy = *(uintptr_t*)((*sp).AsObjPtr()); PolyObject *t = this->allocateMemory(LGWORDSIZE, pc, sp); if (t == 0) goto RAISE_EXCEPTION; t->SetLengthWord(LGWORDSIZE, F_BYTE_OBJ); *(uintptr_t*)t = wy << wx; *sp = (PolyWord)t; break; } case INSTR_lgWordShiftRLog: { // The shift amount is a tagged word not a boxed large word POLYUNSIGNED wx = UNTAGGED_UNSIGNED(*sp++); uintptr_t wy = *(uintptr_t*)((*sp).AsObjPtr()); PolyObject *t = this->allocateMemory(LGWORDSIZE, pc, sp); if (t == 0) goto RAISE_EXCEPTION; t->SetLengthWord(LGWORDSIZE, F_BYTE_OBJ); *(uintptr_t*)t = wy >> wx; *sp = (PolyWord)t; break; } case INSTR_lgWordShiftRArith: { // The shift amount is a tagged word not a boxed large word POLYUNSIGNED wx = UNTAGGED_UNSIGNED(*sp++); intptr_t wy = *(intptr_t*)((*sp).AsObjPtr()); PolyObject *t = this->allocateMemory(LGWORDSIZE, pc, sp); if (t == 0) goto RAISE_EXCEPTION; t->SetLengthWord(LGWORDSIZE, F_BYTE_OBJ); *(intptr_t*)t = wy >> wx; *sp = (PolyWord)t; break; } case INSTR_realEqual: { double u = unboxDouble(*sp++); *sp = u == unboxDouble(*sp) ? True: False; break; } case INSTR_realLess: { double u = unboxDouble(*sp++); *sp = unboxDouble(*sp) < u ? True: False; break; } case INSTR_realLessEq: { double u = unboxDouble(*sp++); *sp = unboxDouble(*sp) <= u ? True: False; break; } case INSTR_realGreater: { double u = unboxDouble(*sp++); *sp = unboxDouble(*sp) > u ? True: False; break; } case INSTR_realGreaterEq: { double u = unboxDouble(*sp++); *sp = unboxDouble(*sp) >= u ? True: False; break; } case INSTR_realUnordered: { double u = unboxDouble(*sp++); double v = unboxDouble(*sp); *sp = (std::isnan(u) || std::isnan(v)) ? True : False; break; } case INSTR_realAdd: { double u = unboxDouble(*sp++); double v = unboxDouble(*sp); PolyObject *t = this->boxDouble(v+u, pc, sp); if (t == 0) goto RAISE_EXCEPTION; *sp = (PolyWord)t; break; } case INSTR_realSub: { double u = unboxDouble(*sp++); double v = unboxDouble(*sp); PolyObject *t = this->boxDouble(v-u, pc, sp); if (t == 0) goto RAISE_EXCEPTION; *sp = (PolyWord)t; break; } case INSTR_realMult: { double u = unboxDouble(*sp++); double v = unboxDouble(*sp); PolyObject *t = this->boxDouble(v*u, pc, sp); if (t == 0) goto RAISE_EXCEPTION; *sp = (PolyWord)t; break; } case INSTR_realDiv: { double u = unboxDouble(*sp++); double v = unboxDouble(*sp); PolyObject *t = this->boxDouble(v/u, pc, sp); if (t == 0) goto RAISE_EXCEPTION; *sp = (PolyWord)t; break; } case INSTR_floatEqual: { float u = unboxFloat(*sp++); *sp = u == unboxFloat(*sp) ? True : False; break; } case INSTR_floatLess: { float u = unboxFloat(*sp++); *sp = unboxFloat(*sp) < u ? True : False; break; } case INSTR_floatLessEq: { float u = unboxFloat(*sp++); *sp = unboxFloat(*sp) <= u ? True : False; break; } case INSTR_floatGreater: { float u = unboxFloat(*sp++); *sp = unboxFloat(*sp) > u ? True : False; break; } case INSTR_floatGreaterEq: { float u = unboxFloat(*sp++); *sp = unboxFloat(*sp) >= u ? True : False; break; } case INSTR_floatUnordered: { float u = unboxFloat(*sp++); float v = unboxFloat(*sp); *sp = (std::isnan(u) || std::isnan(v)) ? True : False; break; } case INSTR_floatAdd: { float u = unboxFloat(*sp++); float v = unboxFloat(*sp); PolyObject *t = this->boxFloat(v + u, pc, sp); if (t == 0) goto RAISE_EXCEPTION; *sp = t; break; } case INSTR_floatSub: { float u = unboxFloat(*sp++); float v = unboxFloat(*sp); PolyObject *t = this->boxFloat(v - u, pc, sp); if (t == 0) goto RAISE_EXCEPTION; *sp = t; break; } case INSTR_floatMult: { float u = unboxFloat(*sp++); float v = unboxFloat(*sp); PolyObject *t = this->boxFloat(v*u, pc, sp); if (t == 0) goto RAISE_EXCEPTION; *sp = t; break; } case INSTR_floatDiv: { float u = unboxFloat(*sp++); float v = unboxFloat(*sp); PolyObject *t = this->boxFloat(v / u, pc, sp); if (t == 0) goto RAISE_EXCEPTION; *sp = t; break; } case INSTR_realToFloat: { // Convert a double to a float. It's complicated because it depends on the rounding mode. int rMode = *pc++; int current = getrounding(); // If the rounding is 4 it means "use current rounding". // Don't call unboxDouble until we're set the rounding. GCC seems to convert it // before the actual float cast. if (rMode < 4) setrounding(rMode); double d = unboxDouble(*sp); float v = (float)d; // Convert with the appropriate rounding. setrounding(current); PolyObject *t = this->boxFloat(v, pc, sp); if (t == 0) goto RAISE_EXCEPTION; *sp = t; break; } case INSTR_realToInt: dv = unboxDouble(*sp); goto realtoint; case INSTR_floatToInt: dv = (double)unboxFloat(*sp); realtoint: { // Convert a double or a float to a tagged integer. int rMode = *pc++; // We mustn't try converting a value that will overflow the conversion // but we need to be careful that we don't raise overflow incorrectly due // to rounding. if (dv > (double)(MAXTAGGED + MAXTAGGED / 2) || dv < -(double)(MAXTAGGED + MAXTAGGED / 2)) { *(--sp) = overflowPacket; goto RAISE_EXCEPTION; } POLYSIGNED p; switch (rMode) { case POLY_ROUND_TONEAREST: p = (POLYSIGNED)round(dv); break; case POLY_ROUND_DOWNWARD: p = (POLYSIGNED)floor(dv); break; case POLY_ROUND_UPWARD: p = (POLYSIGNED)ceil(dv); break; case POLY_ROUND_TOZERO: default: // Truncation is the default for C. p = (POLYSIGNED)dv; } // Check that the value can be tagged. if (p > MAXTAGGED || p < -MAXTAGGED - 1) { *(--sp) = overflowPacket; goto RAISE_EXCEPTION; } *sp = TAGGED(p); break; } case INSTR_getThreadId: *(--sp) = (PolyWord)this->threadObject; break; case INSTR_allocWordMemory: { // Allocate word segment. This must be initialised. // We mustn't pop the initialiser until after any potential GC. POLYUNSIGNED length = UNTAGGED_UNSIGNED(sp[2]); PolyObject *t = this->allocateMemory(length, pc, sp); if (t == 0) goto RAISE_EXCEPTION; PolyWord initialiser = *sp++; POLYUNSIGNED flags = UNTAGGED_UNSIGNED(*sp++); t->SetLengthWord(length, (byte)flags); *sp = (PolyWord)t; // Have to initialise the data. for (; length > 0; ) t->Set(--length, initialiser); break; } case INSTR_alloc_ref: { // Allocate a single word mutable cell. This is more common than allocWordMemory on its own. PolyObject *t = this->allocateMemory(1, pc, sp); if (t == 0) goto RAISE_EXCEPTION; PolyWord initialiser = (*sp); t->SetLengthWord(1, F_MUTABLE_BIT); t->Set(0, initialiser); *sp = (PolyWord)t; break; } case INSTR_loadMLWord: { // The values on the stack are base, index and offset. POLYUNSIGNED offset = UNTAGGED(*sp++); POLYUNSIGNED index = UNTAGGED(*sp++); PolyObject *p = (PolyObject*)((*sp).AsCodePtr() + offset); *sp = p->Get(index); break; } case INSTR_loadMLByte: { // The values on the stack are base and index. POLYUNSIGNED index = UNTAGGED(*sp++); POLYCODEPTR p = (*sp).AsCodePtr(); *sp = TAGGED(p[index]); // Have to tag the result break; } case INSTR_loadC8: { // This is similar to loadMLByte except that the base address is a boxed large-word. // Also the index is SIGNED. POLYSIGNED index = UNTAGGED(*sp++); POLYCODEPTR p = *((byte **)((*sp).AsObjPtr())); *sp = TAGGED(p[index]); // Have to tag the result break; } case INSTR_loadC16: { // This and the other loads are similar to loadMLWord with separate // index and offset values. POLYSIGNED offset = UNTAGGED(*sp++); POLYSIGNED index = UNTAGGED(*sp++); POLYCODEPTR p = *((byte **)((*sp).AsObjPtr())) + offset; POLYUNSIGNED r = ((uint16_t*)p)[index]; *sp = TAGGED(r); break; } case INSTR_loadC32: { POLYSIGNED offset = UNTAGGED(*sp++); POLYSIGNED index = UNTAGGED(*sp++); POLYCODEPTR p = *((byte **)((*sp).AsObjPtr())) + offset; uintptr_t r = ((uint32_t*)p)[index]; #ifdef IS64BITS // This is tagged in 64-bit mode *sp = TAGGED(r); #else // But boxed in 32-bit mode. PolyObject *t = this->allocateMemory(LGWORDSIZE, pc, sp); if (t == 0) goto RAISE_EXCEPTION; t->SetLengthWord(LGWORDSIZE, F_BYTE_OBJ); *(uintptr_t*)t = r; *sp = (PolyWord)t; #endif break; } #if (defined(IS64BITS)) case INSTR_loadC64: { POLYSIGNED offset = UNTAGGED(*sp++); POLYSIGNED index = UNTAGGED(*sp++); POLYCODEPTR p = *((byte **)((*sp).AsObjPtr())) + offset; uintptr_t r = ((uint64_t*)p)[index]; // This must be boxed. PolyObject *t = this->allocateMemory(LGWORDSIZE, pc, sp); if (t == 0) goto RAISE_EXCEPTION; t->SetLengthWord(LGWORDSIZE, F_BYTE_OBJ); *(uintptr_t*)t = r; *sp = (PolyWord)t; break; } #endif case INSTR_loadCFloat: { POLYSIGNED offset = UNTAGGED(*sp++); POLYSIGNED index = UNTAGGED(*sp++); POLYCODEPTR p = *((byte **)((*sp).AsObjPtr())) + offset; double r = ((float*)p)[index]; // This must be boxed. PolyObject *t = this->boxDouble(r, pc, sp); if (t == 0) goto RAISE_EXCEPTION; *sp = (PolyWord)t; break; } case INSTR_loadCDouble: { POLYSIGNED offset = UNTAGGED(*sp++); POLYSIGNED index = UNTAGGED(*sp++); POLYCODEPTR p = *((byte **)((*sp).AsObjPtr())) + offset; double r = ((double*)p)[index]; // This must be boxed. PolyObject *t = this->boxDouble(r, pc, sp); if (t == 0) goto RAISE_EXCEPTION; *sp = (PolyWord)t; break; } case INSTR_loadUntagged: { // The values on the stack are base, index and offset. POLYUNSIGNED offset = UNTAGGED(*sp++); POLYUNSIGNED index = UNTAGGED(*sp++); PolyObject *p = (PolyObject*)((*sp).AsCodePtr() + offset); *sp = TAGGED(p->Get(index).AsUnsigned()); break; } case INSTR_storeMLWord: { PolyWord toStore = *sp++; POLYUNSIGNED offset = UNTAGGED(*sp++); POLYUNSIGNED index = UNTAGGED(*sp++); PolyObject *p = (PolyObject*)((*sp).AsCodePtr() + offset); p->Set(index, toStore); *sp = Zero; break; } case INSTR_storeMLByte: { POLYUNSIGNED toStore = UNTAGGED(*sp++); POLYUNSIGNED index = UNTAGGED(*sp++); POLYCODEPTR p = (*sp).AsCodePtr(); p[index] = (byte)toStore; *sp = Zero; break; } case INSTR_storeC8: { // Similar to storeMLByte except that the base address is a boxed large-word. POLYUNSIGNED toStore = UNTAGGED(*sp++); POLYSIGNED index = UNTAGGED(*sp++); POLYCODEPTR p = *((byte **)((*sp).AsObjPtr())); p[index] = (byte)toStore; *sp = Zero; break; } case INSTR_storeC16: { uint16_t toStore = (uint16_t)UNTAGGED(*sp++); POLYSIGNED offset = UNTAGGED(*sp++); POLYSIGNED index = UNTAGGED(*sp++); POLYCODEPTR p = *((byte **)((*sp).AsObjPtr())) + offset; ((uint16_t*)p)[index] = toStore; *sp = Zero; break; } case INSTR_storeC32: { #ifdef IS64BITS // This is a tagged value in 64-bit mode. uint32_t toStore = (uint32_t)UNTAGGED(*sp++); #else // but a boxed value in 32-bit mode. uint32_t toStore = (uint32_t)(*(uintptr_t*)((*sp++).AsObjPtr())); #endif POLYSIGNED offset = UNTAGGED(*sp++); POLYSIGNED index = UNTAGGED(*sp++); POLYCODEPTR p = *((byte **)((*sp).AsObjPtr())) + offset; ((uint32_t*)p)[index] = toStore; *sp = Zero; break; } #if (defined(IS64BITS)) case INSTR_storeC64: { // This is a boxed value. uint64_t toStore = *(uintptr_t*)((*sp++).AsObjPtr()); POLYSIGNED offset = UNTAGGED(*sp++); POLYSIGNED index = UNTAGGED(*sp++); POLYCODEPTR p = *((byte **)((*sp).AsObjPtr())) + offset; ((uint64_t*)p)[index] = toStore; *sp = Zero; break; } #endif case INSTR_storeCFloat: { // This is a boxed value. float toStore = (float)unboxDouble(*sp++); POLYSIGNED offset = UNTAGGED(*sp++); POLYSIGNED index = UNTAGGED(*sp++); POLYCODEPTR p = *((byte **)((*sp).AsObjPtr())) + offset; ((float*)p)[index] = toStore; *sp = Zero; break; } case INSTR_storeCDouble: { // This is a boxed value. double toStore = unboxDouble(*sp++); POLYSIGNED offset = UNTAGGED(*sp++); POLYSIGNED index = UNTAGGED(*sp++); POLYCODEPTR p = *((byte **)((*sp).AsObjPtr())) + offset; ((double*)p)[index] = toStore; *sp = Zero; break; } case INSTR_storeUntagged: { PolyWord toStore = PolyWord::FromUnsigned(UNTAGGED_UNSIGNED(*sp++)); POLYUNSIGNED offset = UNTAGGED(*sp++); POLYUNSIGNED index = UNTAGGED(*sp++); PolyObject *p = (PolyObject*)((*sp).AsCodePtr() + offset); p->Set(index, toStore); *sp = Zero; break; } case INSTR_blockMoveWord: { // The offsets are byte counts but the the indexes are in words. POLYUNSIGNED length = UNTAGGED_UNSIGNED(*sp++); POLYUNSIGNED destOffset = UNTAGGED_UNSIGNED(*sp++); POLYUNSIGNED destIndex = UNTAGGED_UNSIGNED(*sp++); PolyObject *dest = (PolyObject*)((*sp++).AsCodePtr() + destOffset); POLYUNSIGNED srcOffset = UNTAGGED_UNSIGNED(*sp++); POLYUNSIGNED srcIndex = UNTAGGED_UNSIGNED(*sp++); PolyObject *src = (PolyObject*)((*sp).AsCodePtr() + srcOffset); for (POLYUNSIGNED u = 0; u < length; u++) dest->Set(destIndex+u, src->Get(srcIndex+u)); *sp = Zero; break; } case INSTR_blockMoveByte: { POLYUNSIGNED length = UNTAGGED_UNSIGNED(*sp++); POLYUNSIGNED destOffset = UNTAGGED_UNSIGNED(*sp++); POLYCODEPTR dest = (*sp++).AsCodePtr(); POLYUNSIGNED srcOffset = UNTAGGED_UNSIGNED(*sp++); POLYCODEPTR src = (*sp).AsCodePtr(); memcpy(dest+destOffset, src+srcOffset, length); *sp = Zero; break; } case INSTR_blockEqualByte: { POLYUNSIGNED length = UNTAGGED_UNSIGNED(*sp++); POLYUNSIGNED arg2Offset = UNTAGGED_UNSIGNED(*sp++); POLYCODEPTR arg2Ptr = (*sp++).AsCodePtr(); POLYUNSIGNED arg1Offset = UNTAGGED_UNSIGNED(*sp++); POLYCODEPTR arg1Ptr = (*sp).AsCodePtr(); *sp = memcmp(arg1Ptr+arg1Offset, arg2Ptr+arg2Offset, length) == 0 ? True : False; break; } case INSTR_blockCompareByte: { POLYUNSIGNED length = UNTAGGED_UNSIGNED(*sp++); POLYUNSIGNED arg2Offset = UNTAGGED_UNSIGNED(*sp++); POLYCODEPTR arg2Ptr = (*sp++).AsCodePtr(); POLYUNSIGNED arg1Offset = UNTAGGED_UNSIGNED(*sp++); POLYCODEPTR arg1Ptr = (*sp).AsCodePtr(); int result = memcmp(arg1Ptr+arg1Offset, arg2Ptr+arg2Offset, length); *sp = result == 0 ? TAGGED(0) : result < 0 ? TAGGED(-1) : TAGGED(1); break; } + case INSTR_allocCSpace: + { + // Allocate this on the C heap. + POLYUNSIGNED length = UNTAGGED_UNSIGNED(*sp); + void* memory = malloc(length); + *sp = Make_sysword(this, (uintptr_t)memory)->Word(); + break; + } + + case INSTR_freeCSpace: + { + // Both the address and the size are passed as arguments. + PolyWord size = *sp++; + PolyWord addr = *sp; + free(*(void**)(addr.AsObjPtr())); + *sp = TAGGED(0); + break; + } + default: Crash("Unknown instruction %x\n", pc[-1]); } /* switch */ } /* for */ return 0; } /* MD_switch_to_poly */ void IntTaskData::GarbageCollect(ScanAddress *process) { TaskData::GarbageCollect(process); overflowPacket = process->ScanObjectAddress(overflowPacket); dividePacket = process->ScanObjectAddress(dividePacket); if (stack != 0) { StackSpace *stackSpace = stack; PolyWord *stackPtr = this->taskSp; // The exception arg if any ScanStackAddress(process, this->exception_arg, stackSpace); // Now the values on the stack. for (PolyWord *q = stackPtr; q < stackSpace->top; q++) ScanStackAddress(process, *q, stackSpace); } } // Process a value within the stack. void IntTaskData::ScanStackAddress(ScanAddress *process, PolyWord &val, StackSpace *stack) { if (! val.IsDataPtr()) return; MemSpace *space = gMem.LocalSpaceForAddress(val.AsStackAddr()-1); if (space != 0) val = process->ScanObjectAddress(val.AsObjPtr()); } // Copy a stack void IntTaskData::CopyStackFrame(StackObject *old_stack, uintptr_t old_length, StackObject *new_stack, uintptr_t new_length) { /* Moves a stack, updating all references within the stack */ PolyWord *old_base = (PolyWord *)old_stack; PolyWord *new_base = (PolyWord*)new_stack; PolyWord *old_top = old_base + old_length; /* Calculate the offset of the new stack from the old. If the frame is being extended objects in the new frame will be further up the stack than in the old one. */ uintptr_t offset = new_base - old_base + new_length - old_length; PolyWord *oldSp = this->taskSp; this->taskSp = oldSp + offset; this->hr = this->hr + offset; /* Skip the unused part of the stack. */ uintptr_t i = oldSp - old_base; ASSERT(i <= old_length); i = old_length - i; PolyWord *old = oldSp; PolyWord *newp = this->taskSp; while (i--) { // ASSERT(old >= old_base && old < old_base+old_length); // ASSERT(newp >= new_base && newp < new_base+new_length); PolyWord old_word = *old++; if (old_word.IsTagged() || old_word.AsStackAddr() < old_base || old_word.AsStackAddr() >= old_top) *newp++ = old_word; else *newp++ = PolyWord::FromStackAddr(old_word.AsStackAddr() + offset); } ASSERT(old == ((PolyWord*)old_stack) + old_length); ASSERT(newp == ((PolyWord*)new_stack) + new_length); } void IntTaskData::EnterPolyCode() /* Called from "main" to enter the code. */ { Handle hOriginal = this->saveVec.mark(); // Set this up for the IO calls. while (1) { this->saveVec.reset(hOriginal); // Remove old RTS arguments and results. // Run the ML code and return with the function to call. this->inML = true; int ioFunction = SwitchToPoly(); this->inML = false; try { switch (ioFunction) { case -1: // We've been interrupted. This usually involves simulating a // stack overflow so we could come here because of a genuine // stack overflow. // Previously this code was executed on every RTS call but there // were problems on Mac OS X at least with contention on schedLock. // Process any asynchronous events i.e. interrupts or kill processes->ProcessAsynchRequests(this); // Release and re-acquire use of the ML memory to allow another thread // to GC. processes->ThreadReleaseMLMemory(this); processes->ThreadUseMLMemory(this); break; case -2: // A callback has returned. ASSERT(0); // Callbacks aren't implemented default: Crash("Unknown io operation %d\n", ioFunction); } } catch (IOException &) { } } } // As far as possible we want locking and unlocking an ML mutex to be fast so // we try to implement the code in the assembly code using appropriate // interlocked instructions. That does mean that if we need to lock and // unlock an ML mutex in this code we have to use the same, machine-dependent, // code to do it. These are defaults that are used where there is no // machine-specific code. static Handle ProcessAtomicIncrement(TaskData *taskData, Handle mutexp) { PLocker l(&mutexLock); PolyObject *p = DEREFHANDLE(mutexp); // A thread can only call this once so the values will be short PolyWord newValue = TAGGED(UNTAGGED(p->Get(0))+1); p->Set(0, newValue); return taskData->saveVec.push(newValue); } // Release a mutex. We need to lock the mutex to ensure we don't // reset it in the time between one of atomic operations reading // and writing the mutex. static Handle ProcessAtomicReset(TaskData *taskData, Handle mutexp) { PLocker l(&mutexLock); DEREFHANDLE(mutexp)->Set(0, TAGGED(1)); // Set this to released. return taskData->saveVec.push(TAGGED(0)); // Push the unit result } Handle IntTaskData::AtomicIncrement(Handle mutexp) { return ProcessAtomicIncrement(this, mutexp); } void IntTaskData::AtomicReset(Handle mutexp) { (void)ProcessAtomicReset(this, mutexp); } bool IntTaskData::AddTimeProfileCount(SIGNALCONTEXT *context) { if (taskPc != 0) { // See if the PC we've got is an ML code address. MemSpace *space = gMem.SpaceForAddress(taskPc); if (space != 0 && (space->spaceType == ST_CODE || space->spaceType == ST_PERMANENT)) { add_count(this, taskPc, 1); return true; } } return false; } +extern "C" { + POLYEXTERNALSYMBOL POLYUNSIGNED PolyInterpretedGetAbiList(FirstArgument threadId); +} + +// FFI +#if (defined(HAVE_LIBFFI) && defined(HAVE_LIBFFI_H)) + +static struct _abiTable { const char* abiName; ffi_abi abiCode; } abiTable[] = +{ + // Unfortunately the ABI entries are enums rather than #defines so we + // can't test individual entries. + #ifdef X86_WIN32 + {"sysv", FFI_SYSV}, + {"stdcall", FFI_STDCALL}, + {"thiscall", FFI_THISCALL}, + {"fastcall", FFI_FASTCALL}, + {"ms_cdecl", FFI_MS_CDECL}, + #elif defined(X86_WIN64) + {"win64", FFI_WIN64}, + #elif defined(X86_64) || (defined (__x86_64__) && defined (X86_DARWIN)) + {"unix64", FFI_UNIX64}, + #elif defined(X86_ANY) + {"sysv", FFI_SYSV}, + #endif + { "default", FFI_DEFAULT_ABI} +}; + +// Table of constants returned by call 51 +static int constantTable[] = +{ + FFI_DEFAULT_ABI, // Default ABI + FFI_TYPE_VOID, // Type codes + FFI_TYPE_INT, + FFI_TYPE_FLOAT, + FFI_TYPE_DOUBLE, + FFI_TYPE_UINT8, + FFI_TYPE_SINT8, + FFI_TYPE_UINT16, + FFI_TYPE_SINT16, + FFI_TYPE_UINT32, + FFI_TYPE_SINT32, + FFI_TYPE_UINT64, + FFI_TYPE_SINT64, + FFI_TYPE_STRUCT, + FFI_TYPE_POINTER, + FFI_SIZEOF_ARG // Minimum size for result space +}; + +// Table of predefined ffi types +static ffi_type* ffiTypeTable[] = +{ + &ffi_type_void, + &ffi_type_uint8, + &ffi_type_sint8, + &ffi_type_uint16, + &ffi_type_sint16, + &ffi_type_uint32, + &ffi_type_sint32, + &ffi_type_uint64, + &ffi_type_sint64, + &ffi_type_float, + &ffi_type_double, + &ffi_type_pointer, + &ffi_type_uchar, // These are all aliases for the above + &ffi_type_schar, + &ffi_type_ushort, + &ffi_type_sshort, + &ffi_type_uint, + &ffi_type_sint, + &ffi_type_ulong, + &ffi_type_slong +}; + +static Handle mkAbitab(TaskData* taskData, void*, char* p); + +static Handle toSysWord(TaskData* taskData, void* p) +{ + return Make_sysword(taskData, (uintptr_t)p); +} + +#else +// Libffi is not present. + +// A basic table so that the Foreign structure will compile +static struct _abiTable { const char* abiName; int abiCode; } abiTable[] = +{ + { "default", 0} +}; + +#endif + +// Construct an entry in the ABI table. +static Handle mkAbitab(TaskData* taskData, void* arg, char* p) +{ + struct _abiTable* ab = (struct _abiTable*)p; + // Construct a pair of the string and the code + Handle name = taskData->saveVec.push(C_string_to_Poly(taskData, ab->abiName)); + Handle code = Make_arbitrary_precision(taskData, ab->abiCode); + Handle result = alloc_and_save(taskData, 2); + result->WordP()->Set(0, name->Word()); + result->WordP()->Set(1, code->Word()); + return result; +} + +// Get ABI list. This is called once only before the basis library is built. +POLYUNSIGNED PolyInterpretedGetAbiList(FirstArgument threadId) +{ + TaskData* taskData = TaskData::FindTaskForId(threadId); + ASSERT(taskData != 0); + taskData->PreRTSCall(); + Handle reset = taskData->saveVec.mark(); + Handle result = 0; + + try { + result = makeList(taskData, sizeof(abiTable) / sizeof(abiTable[0]), + (char*)abiTable, sizeof(abiTable[0]), 0, mkAbitab); + } + catch (...) {} // If an ML exception is raised + + taskData->saveVec.reset(reset); + taskData->PostRTSCall(); + if (result == 0) return TAGGED(0).AsUnsigned(); + else return result->Word().AsUnsigned(); + +} static Interpreter interpreterObject; MachineDependent *machineDependent = &interpreterObject; // No machine-specific calls in the interpreter. struct _entrypts machineSpecificEPT[] = { + { "PolyInterpretedGetAbiList", (polyRTSFunction)&PolyInterpretedGetAbiList }, { NULL, NULL} // End of list. }; diff --git a/mlsource/MLCompiler/CodeTree/ByteCode/INTCODECONS.ML b/mlsource/MLCompiler/CodeTree/ByteCode/INTCODECONS.ML index 40277465..297fee49 100644 --- a/mlsource/MLCompiler/CodeTree/ByteCode/INTCODECONS.ML +++ b/mlsource/MLCompiler/CodeTree/ByteCode/INTCODECONS.ML @@ -1,1444 +1,1450 @@ (* - Copyright (c) 2015-18 David C.J. Matthews + Copyright (c) 2015-18, 2020 David C.J. Matthews Copyright (c) 2000 Cambridge University Technical Services Limited This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *) functor INTCODECONS ( structure DEBUG: DEBUGSIG structure PRETTY: PRETTYSIG ) : INTCODECONSSIG = struct open CODE_ARRAY open DEBUG open Address open Misc infix 5 << <<+ <<- >> >>+ >>- ~>> ~>>+ ~>>- (* Shift operators *) infix 3 andb orb xorb andbL orbL xorbL andb8 orb8 xorb8 val op << = Word.<< and op >> = Word.>> and op ~>> = Word.~>> val wordToWord8 = Word8.fromLargeWord o Word.toLargeWord and word8ToWord = Word.fromLargeWord o Word8.toLargeWord (* Typically the compiler is built on a little-endian machine but it could be run on a machine with either endian-ness. We have to find out the endian-ness when we run. There are separate versions of the compiler for 32-bit and 64-bit so that can be a constant. *) local val isBigEndian: unit -> bool = RunCall.rtsCallFast1 "PolyIsBigEndian" in val littleEndian = not o isBigEndian end val wordLength = RunCall.bytesPerWord val opcode_enterInt = 0wx00 and opcode_jump = 0wx02 (* 8-bit unsigned jump forward. *) and opcode_jumpFalse = 0wx03 (* Test top of stack. Take 8-bit unsigned jump if false. *) and opcode_delHandler = 0wx05 and opcode_alloc_ref = 0wx06 and opcode_case16 = 0wx0a and opcode_containerW = 0wx0b and opcode_callClosure = 0wx0c and opcode_returnW = 0wx0d and opcode_pad = 0wx0e and opcode_raiseEx = 0wx10 and opcode_getStoreW = 0wx11 and opcode_localW = 0wx13 and opcode_indirectW = 0wx14 and opcode_moveToVecW = 0wx15 and opcode_setStackValW = 0wx17 and opcode_resetW = 0wx18 and opcode_resetR_w = 0wx19 and opcode_constAddr16 = 0wx1a and opcode_constIntW = 0wx1b and opcode_callFastRTSRRtoR = 0wx1c and opcode_callFastRTSRGtoR = 0wx1d and opcode_jumpBack8 = 0wx1e (* 8-bit unsigned jump backwards - relative to end of instr. *) and opcode_returnB = 0wx1f and opcode_jumpBack16 = 0wx20 (* 16-bit unsigned jump backwards - relative to end of instr. *) and opcode_getStoreB = 0wx21 and opcode_localB = 0wx22 and opcode_indirectB = 0wx23 and opcode_moveToVecB = 0wx24 and opcode_setStackValB = 0wx25 and opcode_resetB = 0wx26 and opcode_resetRB = 0wx27 and opcode_constIntB = 0wx28 and opcode_local_0 = 0wx29 and opcode_local_1 = 0wx2a and opcode_local_2 = 0wx2b and opcode_local_3 = 0wx2c and opcode_local_4 = 0wx2d and opcode_local_5 = 0wx2e and opcode_local_6 = 0wx2f and opcode_local_7 = 0wx30 and opcode_local_8 = 0wx31 and opcode_local_9 = 0wx32 and opcode_local_10 = 0wx33 and opcode_local_11 = 0wx34 and opcode_indirect_0 = 0wx35 and opcode_indirect_1 = 0wx36 and opcode_indirect_2 = 0wx37 and opcode_indirect_3 = 0wx38 and opcode_indirect_4 = 0wx39 and opcode_indirect_5 = 0wx3a and opcode_const_0 = 0wx3b and opcode_const_1 = 0wx3c and opcode_const_2 = 0wx3d and opcode_const_3 = 0wx3e and opcode_const_4 = 0wx3f and opcode_const_10 = 0wx40 and opcode_return_0 = 0wx41 and opcode_return_1 = 0wx42 and opcode_return_2 = 0wx43 and opcode_return_3 = 0wx44 (* and opcode_moveToVec_0 = 0wx45 and opcode_moveToVec_1 = 0wx46 and opcode_moveToVec_2 = 0wx47 and opcode_moveToVec_3 = 0wx48 and opcode_moveToVec_4 = 0wx49 and opcode_moveToVec_5 = 0wx4a and opcode_moveToVec_6 = 0wx4b and opcode_moveToVec_7 = 0wx4c *) and opcode_reset_1 = 0wx50 and opcode_reset_2 = 0wx51 and opcode_getStore_2 = 0wx52 and opcode_getStore_3 = 0wx53 and opcode_getStore_4 = 0wx54 and opcode_tuple_containerW = 0wx55 and opcode_floatAbs = 0wx56 and opcode_floatNeg = 0wx57 and opcode_fixedIntToFloat = 0wx58 and opcode_floatToReal = 0wx59 and opcode_realToFloat = 0wx5a and opcode_floatEqual = 0wx5b and opcode_floatLess = 0wx5c and opcode_floatLessEq = 0wx5d and opcode_floatGreater = 0wx5e and opcode_floatGreaterEq = 0wx5f and opcode_floatAdd = 0wx60 and opcode_floatSub = 0wx61 and opcode_floatMult = 0wx62 and opcode_floatDiv = 0wx63 and opcode_resetR_1 = 0wx64 and opcode_resetR_2 = 0wx65 and opcode_resetR_3 = 0wx66 and opcode_tupleW = 0wx67 and opcode_tupleB = 0wx68 and opcode_tuple_2 = 0wx69 and opcode_tuple_3 = 0wx6a and opcode_tuple_4 = 0wx6b and opcode_lock = 0wx6c and opcode_ldexc = 0wx6d and opcode_realToInt = 0wx6e and opcode_floatToInt = 0wx6f and opcode_callFastRTSFtoF = 0wx70 and opcode_callFastRTSGtoF = 0wx71 and opcode_callFastRTSFFtoF = 0wx72 and opcode_callFastRTSFGtoF = 0wx73 and opcode_pushHandler = 0wx78 and opcode_realUnordered = 0wx79 and opcode_floatUnordered = 0wx7a and opcode_tailbb = 0wx7b and opcode_tail = 0wx7c and opcode_tail3b = 0wx7d and opcode_tail4b = 0wx7e and opcode_tail3_2 = 0wx7f and opcode_tail3_3 = 0wx80 and opcode_setHandler = 0wx81 and opcode_callFastRTS0 = 0wx83 and opcode_callFastRTS1 = 0wx84 and opcode_callFastRTS2 = 0wx85 and opcode_callFastRTS3 = 0wx86 and opcode_callFastRTS4 = 0wx87 and opcode_callFastRTS5 = 0wx88 - and opcode_callFullRTS0 = 0wx89 + and opcode_callFullRTS0 = 0wx89 (* Legacy *) and opcode_callFullRTS1 = 0wx8a and opcode_callFullRTS2 = 0wx8b and opcode_callFullRTS3 = 0wx8c and opcode_callFullRTS4 = 0wx8d and opcode_callFullRTS5 = 0wx8e and opcode_callFastRTSRtoR = 0wx8f and opcode_callFastRTSGtoR = 0wx90 and opcode_notBoolean = 0wx91 and opcode_isTagged = 0wx92 and opcode_cellLength = 0wx93 and opcode_cellFlags = 0wx94 and opcode_clearMutable = 0wx95 and opcode_atomicIncr = 0wx97 and opcode_atomicDecr = 0wx98 and opcode_atomicReset = 0wx99 and opcode_longWToTagged = 0wx9a and opcode_signedToLongW = 0wx9b and opcode_unsignedToLongW = 0wx9c and opcode_realAbs = 0wx9d and opcode_realNeg = 0wx9e and opcode_fixedIntToReal = 0wx9f and opcode_equalWord = 0wxa0 and opcode_lessSigned = 0wxa2 and opcode_lessUnsigned = 0wxa3 and opcode_lessEqSigned = 0wxa4 and opcode_lessEqUnsigned = 0wxa5 and opcode_greaterSigned = 0wxa6 and opcode_greaterUnsigned = 0wxa7 and opcode_greaterEqSigned = 0wxa8 and opcode_greaterEqUnsigned = 0wxa9 and opcode_fixedAdd = 0wxaa and opcode_fixedSub = 0wxab and opcode_fixedMult = 0wxac and opcode_fixedQuot = 0wxad and opcode_fixedRem = 0wxae and opcode_fixedDiv = 0wxaf and opcode_fixedMod = 0wxb0 and opcode_wordAdd = 0wxb1 and opcode_wordSub = 0wxb2 and opcode_wordMult = 0wxb3 and opcode_wordDiv = 0wxb4 and opcode_wordMod = 0wxb5 and opcode_wordAnd = 0wxb7 and opcode_wordOr = 0wxb8 and opcode_wordXor = 0wxb9 and opcode_wordShiftLeft = 0wxba and opcode_wordShiftRLog = 0wxbb and opcode_wordShiftRArith = 0wxbc and opcode_allocByteMem = 0wxbd and opcode_lgWordEqual = 0wxbe and opcode_lgWordLess = 0wxc0 and opcode_lgWordLessEq = 0wxc1 and opcode_lgWordGreater = 0wxc2 and opcode_lgWordGreaterEq = 0wxc3 and opcode_lgWordAdd = 0wxc4 and opcode_lgWordSub = 0wxc5 and opcode_lgWordMult = 0wxc6 and opcode_lgWordDiv = 0wxc7 and opcode_lgWordMod = 0wxc8 and opcode_lgWordAnd = 0wxc9 and opcode_lgWordOr = 0wxca and opcode_lgWordXor = 0wxcb and opcode_lgWordShiftLeft = 0wxcc and opcode_lgWordShiftRLog = 0wxcd and opcode_lgWordShiftRArith = 0wxce and opcode_realEqual = 0wxcf and opcode_realLess = 0wxd1 and opcode_realLessEq = 0wxd2 and opcode_realGreater = 0wxd3 and opcode_realGreaterEq = 0wxd4 and opcode_realAdd = 0wxd5 and opcode_realSub = 0wxd6 and opcode_realMult = 0wxd7 and opcode_realDiv = 0wxd8 and opcode_getThreadId = 0wxd9 and opcode_allocWordMemory = 0wxda and opcode_loadMLWord = 0wxdb and opcode_loadMLByte = 0wxdc and opcode_loadC8 = 0wxdd and opcode_loadC16 = 0wxde and opcode_loadC32 = 0wxdf and opcode_loadC64 = 0wxe0 and opcode_loadCFloat = 0wxe1 and opcode_loadCDouble = 0wxe2 and opcode_storeMLWord = 0wxe3 and opcode_storeMLByte = 0wxe4 and opcode_storeC8 = 0wxe5 and opcode_storeC16 = 0wxe6 and opcode_storeC32 = 0wxe7 and opcode_storeC64 = 0wxe8 and opcode_storeCFloat = 0wxe9 and opcode_storeCDouble = 0wxea and opcode_blockMoveWord = 0wxeb and opcode_blockMoveByte = 0wxec and opcode_blockEqualByte = 0wxed and opcode_blockCompareByte = 0wxee and opcode_loadUntagged = 0wxef and opcode_storeUntagged = 0wxf0 and opcode_deleteHandler = 0wxf1 (* Just deletes the handler - no jump. *) and opcode_jump32 = 0wxf2 (* 32-bit signed jump, forwards or backwards. *) and opcode_jump32False = 0wxf3 (* Test top item. Take 32-bit signed jump if false. *) and opcode_constAddr32 = 0wxf4 (* Followed by a 32-bit offset. Load a constant at that address. *) and opcode_setHandler32 = 0wxf5 (* Setup a handler whose address is given by the 32-bit signed offset. *) and opcode_case32 = 0wxf6 (* Indexed case with 32-bit offsets *) and opcode_jump16 = 0wxf7 and opcode_jump16False = 0wxf8 and opcode_setHandler16 = 0wxf9 and opcode_constAddr8 = 0wxfa and opcode_stackSize8 = 0wxfb and opcode_stackSize16 = 0wxfc + and opcode_allocCSpace = 0wxfd + and opcode_freeCSpace = 0wxfe local val repArray : string Array.array = Array.tabulate (256, fn (i) => ""); fun repUpdate (n, s) = Array.update (repArray, Word8.toInt n, s); val () = repUpdate(opcode_enterInt, "enterInt"); val () = repUpdate(opcode_jump, "jump"); val () = repUpdate(opcode_jumpFalse, "jumpFalse"); val () = repUpdate(opcode_delHandler, "delHandler"); val () = repUpdate(opcode_alloc_ref, "alloc_ref"); val () = repUpdate(opcode_case16, "case16"); val () = repUpdate(opcode_callClosure, "callClosure"); val () = repUpdate(opcode_returnW, "returnW"); val () = repUpdate(opcode_pad, "pad"); val () = repUpdate(opcode_raiseEx, "raiseEx"); val () = repUpdate(opcode_getStoreW, "getStoreW"); val () = repUpdate(opcode_localW, "localW"); val () = repUpdate(opcode_indirectW, "indirectW"); val () = repUpdate(opcode_moveToVecW, "moveToVecW"); val () = repUpdate(opcode_setStackValW, "setStackValW"); val () = repUpdate(opcode_resetW, "resetW"); val () = repUpdate(opcode_resetR_w, "resetR_w"); val () = repUpdate(opcode_constAddr16, "constAddr16"); val () = repUpdate(opcode_constIntW, "constIntW"); val () = repUpdate(opcode_callFastRTSRRtoR, "callFullRTSRRtoR") val () = repUpdate(opcode_callFastRTSRGtoR, "callFullRTSRGtoR") val () = repUpdate(opcode_jumpBack8, "jumpBack8"); val () = repUpdate(opcode_returnB, "returnB"); val () = repUpdate(opcode_jumpBack16, "jumpBack16"); val () = repUpdate(opcode_getStoreB, "getStoreB"); val () = repUpdate(opcode_localB, "localB"); val () = repUpdate(opcode_indirectB, "indirectB"); val () = repUpdate(opcode_moveToVecB, "moveToVecB"); val () = repUpdate(opcode_setStackValB, "setStackValB"); val () = repUpdate(opcode_resetB, "resetB"); val () = repUpdate(opcode_resetRB, "resetRB"); val () = repUpdate(opcode_constIntB, "constIntB"); val () = repUpdate(opcode_local_0, "local_0"); val () = repUpdate(opcode_local_1, "local_1"); val () = repUpdate(opcode_local_2, "local_2"); val () = repUpdate(opcode_local_3, "local_3"); val () = repUpdate(opcode_local_4, "local_4"); val () = repUpdate(opcode_local_5, "local_5"); val () = repUpdate(opcode_local_6, "local_6"); val () = repUpdate(opcode_local_7, "local_7"); val () = repUpdate(opcode_local_8, "local_8"); val () = repUpdate(opcode_local_9, "local_9"); val () = repUpdate(opcode_local_10, "local_10"); val () = repUpdate(opcode_local_11, "local_11"); val () = repUpdate(opcode_indirect_0, "indirect_0"); val () = repUpdate(opcode_indirect_1, "indirect_1"); val () = repUpdate(opcode_indirect_2, "indirect_2"); val () = repUpdate(opcode_indirect_3, "indirect_3"); val () = repUpdate(opcode_indirect_4, "indirect_4"); val () = repUpdate(opcode_indirect_5, "indirect_5"); val () = repUpdate(opcode_const_0, "const_0"); val () = repUpdate(opcode_const_1, "const_1"); val () = repUpdate(opcode_const_2, "const_2"); val () = repUpdate(opcode_const_3, "const_3"); val () = repUpdate(opcode_const_4, "const_4"); val () = repUpdate(opcode_const_10, "const_10"); val () = repUpdate(opcode_return_0, "return_0"); val () = repUpdate(opcode_return_1, "return_1"); val () = repUpdate(opcode_return_2, "return_2"); val () = repUpdate(opcode_return_3, "return_3"); val () = repUpdate(opcode_reset_1, "reset_1"); val () = repUpdate(opcode_reset_2, "reset_2"); val () = repUpdate(opcode_getStore_2, "getStore_2"); val () = repUpdate(opcode_getStore_3, "getStore_3"); val () = repUpdate(opcode_getStore_4, "getStore_4"); val () = repUpdate(opcode_tuple_containerW, "tuple_containerW"); val () = repUpdate(opcode_floatAbs, "floatAbs"); val () = repUpdate(opcode_floatNeg, "floatNeg"); val () = repUpdate(opcode_fixedIntToFloat, "opcode_fixedIntToFloat"); val () = repUpdate(opcode_floatToReal, "floatToReal"); val () = repUpdate(opcode_realToFloat, "realToFloat"); val () = repUpdate(opcode_floatEqual, "floatEqual"); val () = repUpdate(opcode_floatLess, "floatLess"); val () = repUpdate(opcode_floatLessEq, "floatLessEq"); val () = repUpdate(opcode_floatGreater, "floatGreater"); val () = repUpdate(opcode_floatGreaterEq,"floatGreaterEq"); val () = repUpdate(opcode_floatAdd, "floatAdd"); val () = repUpdate(opcode_floatSub, "floatSub"); val () = repUpdate(opcode_floatMult, "floatMult"); val () = repUpdate(opcode_floatDiv, "floatDiv"); val () = repUpdate(opcode_resetR_1, "resetR_1"); val () = repUpdate(opcode_resetR_2, "resetR_2"); val () = repUpdate(opcode_resetR_3, "resetR_3"); val () = repUpdate(opcode_tupleW, "tupleW"); val () = repUpdate(opcode_tupleB, "tupleB"); val () = repUpdate(opcode_tuple_2, "tuple_2"); val () = repUpdate(opcode_tuple_3, "tuple_3"); val () = repUpdate(opcode_tuple_4, "tuple_4"); val () = repUpdate(opcode_lock, "lock"); val () = repUpdate(opcode_ldexc, "ldexc"); val () = repUpdate(opcode_realToInt, "realToInt"); val () = repUpdate(opcode_floatToInt, "floatToInt"); val () = repUpdate(opcode_callFastRTSFtoF, "callFastRTSFtoF"); val () = repUpdate(opcode_callFastRTSGtoF, "callFastRTSGtoF"); val () = repUpdate(opcode_callFastRTSFFtoF, "callFastRTSFFtoF"); val () = repUpdate(opcode_callFastRTSFGtoF, "callFastRTSFGtoF"); val () = repUpdate(opcode_setHandler, "setHandler"); val () = repUpdate(opcode_pushHandler, "pushHandler"); val () = repUpdate(opcode_realUnordered, "realUnordered"); val () = repUpdate(opcode_floatUnordered, "floatUnordered"); val () = repUpdate(opcode_tailbb, "tailbb"); val () = repUpdate(opcode_tail, "tail"); val () = repUpdate(opcode_tail3b, "tail3b"); val () = repUpdate(opcode_tail4b, "tail4b"); val () = repUpdate(opcode_tail3_2, "tail3_2"); val () = repUpdate(opcode_tail3_3, "tail3_3"); val () = repUpdate(opcode_callFastRTS0, "callFastRTS0") val () = repUpdate(opcode_callFastRTS1, "callFastRTS1") val () = repUpdate(opcode_callFastRTS2, "callFastRTS2") val () = repUpdate(opcode_callFastRTS3, "callFastRTS3") val () = repUpdate(opcode_callFastRTS4, "callFastRTS4") val () = repUpdate(opcode_callFastRTS5, "callFastRTS5") val () = repUpdate(opcode_callFullRTS0, "callFullRTS0") val () = repUpdate(opcode_callFullRTS1, "callFullRTS1") val () = repUpdate(opcode_callFullRTS2, "callFullRTS2") val () = repUpdate(opcode_callFullRTS3, "callFullRTS3") val () = repUpdate(opcode_callFullRTS4, "callFullRTS4") val () = repUpdate(opcode_callFullRTS5, "callFullRTS5") val () = repUpdate(opcode_callFastRTSRtoR, "callFullRTSRtoR") val () = repUpdate(opcode_callFastRTSGtoR, "callFullRTSGtoR") val () = repUpdate(opcode_notBoolean, "notBoolean") val () = repUpdate(opcode_isTagged, "isTagged") val () = repUpdate(opcode_cellLength, "cellLength") val () = repUpdate(opcode_cellFlags, "cellFlags") val () = repUpdate(opcode_clearMutable, "clearMutable") val () = repUpdate(opcode_atomicIncr, "atomicIncr") val () = repUpdate(opcode_atomicDecr, "atomicDecr") val () = repUpdate(opcode_atomicReset, "atomicReset") val () = repUpdate(opcode_longWToTagged, "longWToTagged") val () = repUpdate(opcode_signedToLongW, "signedToLongW") val () = repUpdate(opcode_unsignedToLongW, "unsignedToLongW") val () = repUpdate(opcode_realAbs, "realAbs") val () = repUpdate(opcode_realNeg, "realNeg") val () = repUpdate(opcode_fixedIntToReal, "fixedIntToReal") val () = repUpdate(opcode_equalWord, "equalWord") val () = repUpdate(opcode_lessSigned, "lessSigned") val () = repUpdate(opcode_lessUnsigned, "lessUnsigned") val () = repUpdate(opcode_lessEqSigned, "lessEqSigned") val () = repUpdate(opcode_lessEqUnsigned, "lessEqUnsigned") val () = repUpdate(opcode_greaterSigned, "greaterSigned") val () = repUpdate(opcode_greaterUnsigned, "greaterUnsigned") val () = repUpdate(opcode_greaterEqSigned, "greaterEqSigned") val () = repUpdate(opcode_greaterEqUnsigned, "greaterEqUnsigned") val () = repUpdate(opcode_fixedAdd, "fixedAdd") val () = repUpdate(opcode_fixedSub, "fixedSub") val () = repUpdate(opcode_fixedMult, "fixedMult") val () = repUpdate(opcode_fixedQuot, "fixedQuot") val () = repUpdate(opcode_fixedRem, "fixedRem") val () = repUpdate(opcode_fixedDiv, "fixedDiv") val () = repUpdate(opcode_fixedMod, "fixedMod") val () = repUpdate(opcode_wordAdd, "wordAdd") val () = repUpdate(opcode_wordSub, "wordSub") val () = repUpdate(opcode_wordMult, "wordMult") val () = repUpdate(opcode_wordDiv, "wordDiv") val () = repUpdate(opcode_wordMod, "wordMod") val () = repUpdate(opcode_wordAnd, "wordAnd") val () = repUpdate(opcode_wordOr, "wordOr") val () = repUpdate(opcode_wordXor, "wordXor") val () = repUpdate(opcode_wordShiftLeft, "wordShiftLeft") val () = repUpdate(opcode_wordShiftRLog, "wordShiftRLog") val () = repUpdate(opcode_wordShiftRArith, "wordShiftRArith") val () = repUpdate(opcode_allocByteMem, "allocByteMem") val () = repUpdate(opcode_lgWordEqual, "lgWordEqual") val () = repUpdate(opcode_lgWordLess, "lgWordLess") val () = repUpdate(opcode_lgWordLessEq, "lgWordLessEq") val () = repUpdate(opcode_lgWordGreater, "lgWordGreater") val () = repUpdate(opcode_lgWordGreaterEq, "lgWordGreaterEq") val () = repUpdate(opcode_lgWordAdd, "lgWordAdd") val () = repUpdate(opcode_lgWordSub, "lgWordSub") val () = repUpdate(opcode_lgWordMult, "lgWordMult") val () = repUpdate(opcode_lgWordDiv, "lgWordDiv") val () = repUpdate(opcode_lgWordMod, "lgWordMod") val () = repUpdate(opcode_lgWordAnd, "lgWordAnd") val () = repUpdate(opcode_lgWordOr, "lgWordOr") val () = repUpdate(opcode_lgWordXor, "lgWordXor") val () = repUpdate(opcode_lgWordShiftLeft, "lgWordShiftLeft") val () = repUpdate(opcode_lgWordShiftRLog, "lgWordShiftRLog") val () = repUpdate(opcode_lgWordShiftRArith, "lgWordShiftRArith") val () = repUpdate(opcode_realEqual, "realEqual") val () = repUpdate(opcode_realLess, "realLess") val () = repUpdate(opcode_realLessEq, "realLessEq") val () = repUpdate(opcode_realGreater, "realGreater") val () = repUpdate(opcode_realGreaterEq, "realGreaterEq") val () = repUpdate(opcode_realAdd, "realAdd") val () = repUpdate(opcode_realSub, "realSub") val () = repUpdate(opcode_realMult, "realMult") val () = repUpdate(opcode_realDiv, "realDiv") val () = repUpdate(opcode_getThreadId, "getThreadId") val () = repUpdate(opcode_allocWordMemory, "allocWordMemory") val () = repUpdate(opcode_loadMLWord, "loadMLWord") val () = repUpdate(opcode_loadMLByte, "loadMLByte") val () = repUpdate(opcode_loadC8, "loadC8") val () = repUpdate(opcode_loadC16, "loadC16") val () = repUpdate(opcode_loadC32, "loadC32") val () = repUpdate(opcode_loadC64, "loadC64") val () = repUpdate(opcode_loadCFloat, "loadCFloat") val () = repUpdate(opcode_loadCDouble, "loadCDouble") val () = repUpdate(opcode_storeMLWord, "storeMLWord") val () = repUpdate(opcode_storeMLByte, "storeMLByte") val () = repUpdate(opcode_storeC8, "storeC8") val () = repUpdate(opcode_storeC16, "storeC16") val () = repUpdate(opcode_storeC32, "storeC32") val () = repUpdate(opcode_storeC64, "storeC64") val () = repUpdate(opcode_storeCFloat, "storeCFloat") val () = repUpdate(opcode_storeCDouble, "storeCDouble") val () = repUpdate(opcode_blockMoveWord, "blockMoveWord") val () = repUpdate(opcode_blockMoveByte, "blockMoveByte") val () = repUpdate(opcode_blockEqualByte, "blockEqualByte") val () = repUpdate(opcode_blockCompareByte, "blockCompareByte") val () = repUpdate(opcode_loadUntagged, "loadUntagged") val () = repUpdate(opcode_deleteHandler, "deleteHandler") val () = repUpdate(opcode_jump32, "jump32") val () = repUpdate(opcode_jump32False, "jump32False") val () = repUpdate(opcode_constAddr32, "constAddr32") val () = repUpdate(opcode_setHandler32, "setHandler32") val () = repUpdate(opcode_jump16, "jump16") val () = repUpdate(opcode_case32, "case32") val () = repUpdate(opcode_jump16False, "jump16false") val () = repUpdate(opcode_setHandler16, "setHandler16") val () = repUpdate(opcode_constAddr8, "constAddr8") val () = repUpdate(opcode_stackSize8, "stackSize8") val () = repUpdate(opcode_stackSize16, "stackSize16") + val () = repUpdate(opcode_allocCSpace, "allocCSpace") + val () = repUpdate(opcode_freeCSpace, "freeCSpace") in fun repr n : string = Array.sub (repArray, Word8.toInt n); end; local val sizeArray : int Array.array = Array.array (256, 1); fun sizeUpdate (n, s) = Array.update (sizeArray, Word8.toInt n, s); val () = sizeUpdate(opcode_enterInt , 2); val () = sizeUpdate(opcode_jump , 2); val () = sizeUpdate(opcode_jumpFalse , 2); val () = sizeUpdate(opcode_delHandler , 2); val () = sizeUpdate(opcode_case16 , 3); val () = sizeUpdate(opcode_returnW , 3); val () = sizeUpdate(opcode_getStoreW , 3); val () = sizeUpdate(opcode_localW , 3); val () = sizeUpdate(opcode_indirectW , 3); val () = sizeUpdate(opcode_moveToVecW , 3); val () = sizeUpdate(opcode_setStackValW, 3); val () = sizeUpdate(opcode_resetW , 3); val () = sizeUpdate(opcode_resetR_w , 3); val () = sizeUpdate(opcode_constAddr16 , 3); val () = sizeUpdate(opcode_constIntW , 3); val () = sizeUpdate(opcode_jumpBack8 , 2); val () = sizeUpdate(opcode_returnB , 2); val () = sizeUpdate(opcode_jumpBack16 , 3); val () = sizeUpdate(opcode_getStoreB , 2); val () = sizeUpdate(opcode_localB , 2); val () = sizeUpdate(opcode_indirectB , 2); val () = sizeUpdate(opcode_moveToVecB , 2); val () = sizeUpdate(opcode_setStackValB, 2); val () = sizeUpdate(opcode_resetB , 2); val () = sizeUpdate(opcode_resetRB , 2); val () = sizeUpdate(opcode_constIntB , 2); val () = sizeUpdate(opcode_tupleW , 3); val () = sizeUpdate(opcode_tupleB , 2); val () = sizeUpdate(opcode_setHandler , 2); val () = sizeUpdate(opcode_tailbb , 3); val () = sizeUpdate(opcode_tail , 5); val () = sizeUpdate(opcode_tail3b , 2); val () = sizeUpdate(opcode_tail4b , 2); val () = sizeUpdate(opcode_case32 , 3); val () = sizeUpdate(opcode_jump32, 5) val () = sizeUpdate(opcode_jump32False, 5) val () = sizeUpdate(opcode_constAddr32, 5) val () = sizeUpdate(opcode_setHandler32, 5) val () = sizeUpdate(opcode_constAddr8 , 2); val () = sizeUpdate(opcode_stackSize8 , 2); val () = sizeUpdate(opcode_stackSize16 , 3); val () = sizeUpdate(opcode_realToFloat , 2); val () = sizeUpdate(opcode_realToInt, 2); val () = sizeUpdate(opcode_floatToInt, 2); in fun size n = Array.sub (sizeArray, Word8.toInt n); end (* A Label is a ref that is later set to the location. *) type labels = {destination: Word.word ref } (* Used for jump, jumpFalse, setHandler and delHandler. *) datatype jumpTypes = Jump | JumpFalse | SetHandler datatype opcode = SimpleCode of Word8.word list (* Bytes that don't need any special treatment *) | LabelCode of labels (* A label - forwards or backwards. *) | JumpInstruction of { label: labels, jumpType: jumpTypes, size : jumpSize ref } (* Jumps or SetHandler. *) | PushConstant of { constNum: int, size : jumpSize ref } | IndexedCase of { labels: labels list, size : jumpSize ref } and jumpSize = Size8 | Size16 | Size32 and code = Code of { constVec: machineWord list ref, (* Vector of words to be put at end *) procName: string, (* Name of the procedure. *) printAssemblyCode:bool, (* Whether to print the code when we finish. *) printStream: string->unit, (* The stream to use *) stage1Code: opcode list ref } (* create and initialise a code segment *) fun codeCreate (name : string, parameters) = let val printStream = PRETTY.getSimplePrinter(parameters, []); in Code { constVec = ref [], procName = name, printAssemblyCode = DEBUG.getParameter DEBUG.assemblyCodeTag parameters, printStream = printStream, stage1Code = ref [] } end (* Find the offset in the constant area of a constant. *) (* The first has offset 0. *) fun addConstToVec (valu, Code{constVec, ...}) = let (* Search the list to see if the constant is already there. *) fun findConst valu [] num = (* Add to the list *) ( constVec := ! constVec @ [valu]; num ) | findConst valu (h :: t) num = if wordEq (valu, h) then num else findConst valu t (num + 1) (* Not equal *) in findConst valu (! constVec) 0 end fun printCode (seg: codeVec, procName: string, endcode, printStream) = let val () = printStream "\n"; val () = if procName = "" (* No name *) then printStream "?" else printStream procName; val () = printStream ":\n"; (* prints a string representation of a number *) fun printHex (v) = printStream(Word.fmt StringCvt.HEX v); val ptr = ref 0w0; (* To make sure we do not print branch extensions as though they were instructions we keep a list of all indirect forward references and print values at those addresses as addresses. This list is sorted with the lowest address first. *) val indirections = ref []; local fun addL (n, []) = [n] | addL (n, l as (x :: xs)) = if n < x then n :: l else if n = x then l else x :: addL (n, xs) in fun addInd (ind) = indirections := addL (ind, !indirections) end (* Gets "length" bytes from locations "addr", "addr"+1... Returns an unsigned number. *) fun getB (0, _, _) = 0w0 | getB (length, addr, seg) = (getB (length - 1, addr + 0w1, seg) << 0w8) + word8ToWord (codeVecGet (seg, addr)) (* Prints a relative address. *) fun printDisp (len, spacer: string, addToList: bool) = let val ad = getB(len, !ptr, seg) + !ptr + Word.fromInt len val () = if addToList then addInd ad else (); val () = printStream spacer; val () = printHex ad; in ptr := !ptr + Word.fromInt len end (* Prints an operand of an instruction *) fun printOp (len, spacer : string) = let val () = printStream spacer; val () = printHex (getB (len, !ptr, seg)) in ptr := !ptr + Word.fromInt len end; in while !ptr < endcode do let val addr = !ptr in printHex addr; (* The address. *) if (case !indirections of v :: _ => v = addr | [] => false) then let (* It's an address. *) val () = printDisp (2, "\t", false); in case !indirections of _ :: vs => indirections := vs | _ => raise InternalError "printCode: indirection list confused" end else let (* It's an instruction. *) val () = printStream "\t"; val opc = codeVecGet (seg, !ptr) (* opcode *) val () = ptr := !ptr + 0w1; val () = printStream (repr opc); val sz = size opc; in if sz = 1 then () else if opc = opcode_jump orelse opc = opcode_jumpFalse orelse opc = opcode_setHandler orelse opc = opcode_delHandler orelse opc = opcode_constAddr16 orelse opc = opcode_jump32 orelse opc = opcode_jump32False orelse opc = opcode_setHandler32 orelse opc = opcode_constAddr8 orelse opc = opcode_constAddr32 then printDisp (sz - 1, "\t", false) else if opc = opcode_jumpBack8 (* Should be negative *) then ( printStream "\t"; printHex((!ptr - 0w1) - getB(1, !ptr, seg)); ptr := !ptr + 0w1 ) else if opc = opcode_jumpBack16 (* Should be negative *) then ( printStream "\t"; printHex((!ptr - 0w1) - getB(2, !ptr, seg)); ptr := !ptr + 0w2 ) else if opc = opcode_case16 then let (* Have to find out how many items there are. *) val limit = getB (2, !ptr, seg); val () = printOp (2, "\t"); val base = !ptr; fun printEntry _ = (printStream "\n\t"; printHex(base + getB(2, !ptr, seg)); ptr := !ptr + 0w2) fun forLoop f i n = if i > n then () else (f i; forLoop f (i + 0w1) n) in forLoop printEntry 0w0 limit end else if opc = opcode_tail then (printOp (2, "\t"); printOp (2, ",")) else if opc = opcode_tailbb then (printOp (1, "\t"); printOp (1, ",")) else printOp (sz - 1, "\t") end; (* an instruction. *) printStream "\n" end (* main loop *) end (* printCode *) fun codeSize (SimpleCode l) = List.length l | codeSize (LabelCode _) = 0 | codeSize (JumpInstruction{size=ref Size8, ...}) = 2 | codeSize (JumpInstruction{size=ref Size16, ...}) = 3 | codeSize (JumpInstruction{size=ref Size32, ...}) = 5 | codeSize (PushConstant{size=ref Size8, ...}) = 2 | codeSize (PushConstant{size=ref Size16, ...}) = 3 | codeSize (PushConstant{size=ref Size32, ...}) = 5 | codeSize (IndexedCase{labels, size=ref Size32, ...}) = 3 + List.length labels * 4 | codeSize (IndexedCase{labels, size=ref Size16, ...}) = 3 + List.length labels * 2 | codeSize (IndexedCase{labels=_, size=ref Size8, ...}) = raise InternalError "codeSize" (* General function to process the code. ic is the byte counter within the original code. *) fun foldCode foldFn n ops = let fun doFold(oper :: operList, ic, acc) = doFold(operList, ic + Word.fromInt(codeSize oper), foldFn(oper, ic, acc)) | doFold(_, _, n) = n in doFold(ops, 0w0, n) end (* Process the code, setting the destination of any labels. Return the length of the code. *) fun setLabels(LabelCode{destination, ...} :: ops, ic) = (destination := ic; setLabels(ops, ic)) | setLabels(oper :: ops, ic) = setLabels(ops, ic + Word.fromInt(codeSize oper)) | setLabels([], ic) = ic (* Set the sizes of branches depending on the distance to the destination. *) fun setLabelsAndSizes ops = let (* Set the labels and adjust the sizes, repeating until it never gets smaller*) fun setLabAndSize(ops, lastSize) = let (* Calculate offsets for constants. *) val endIC = Word.andb(lastSize + wordLength - 0w1, ~ wordLength) val firstConstant = endIC + wordLength * 0w3 (* Because the constant area is word aligned we have to allow for the possibility that the distance between a "load constant" instruction and the target could actually increase. *) val alignment = wordLength - 0w1 fun adjust(JumpInstruction{size as ref Size32, label={destination=ref dest}, ...}, ic, _) = let val diff = if dest <= ic (* N.B. Include infinite loops as backwards. *) then ic - dest (* Backwards - Counts from start of instruction. *) else dest - (ic + 0w5) (* Forwards - Relative to the current end. *) in if diff < 0wx100 then size := Size8 else if diff < 0wx10000 then size := Size16 else () end | adjust(JumpInstruction{size as ref Size16, label={destination=ref dest}, ...}, ic, _) = if dest <= ic then if ic - dest < 0wx100 then size := Size8 else () else if dest - (ic + 0w3) < 0wx100 then size := Size8 else () | adjust(IndexedCase{size as ref Size32, labels}, ic, _) = let val startAddr = ic+0w3 (* Use 16-bit case if all the offsets are 16-bits. *) fun is16bit{destination=ref dest} = dest > startAddr andalso dest < startAddr+0wx10000 in if List.all is16bit labels then size := Size16 else () end | adjust(PushConstant{size as ref Size32, constNum, ...}, ic, _) = let val constAddr = firstConstant + Word.fromInt constNum * wordLength val offset = constAddr - (ic + 0w5) in if offset < 0wx100-alignment then size := Size8 else if offset < 0wx10000-alignment then size := Size16 else () end | adjust(PushConstant{size as ref Size16, constNum, ...}, ic, _) = let val constAddr = firstConstant + Word.fromInt constNum * wordLength val offset = constAddr - (ic + 0w3) in if offset < 0wx100-alignment then size := Size8 else () end | adjust _ = () val () = foldCode adjust () ops val nextSize = setLabels(ops, 0w0) in if nextSize < lastSize then setLabAndSize(ops, nextSize) else if nextSize = lastSize then lastSize else raise InternalError "setLabAndSize - size increased" end in setLabAndSize(ops, setLabels(ops, 0w0)) end fun genCode(ops, Code {constVec, ...}) = let (* First pass - set the labels. *) val codeSize = setLabelsAndSizes ops (* Align to wordLength. *) val endIC = Word.andb(codeSize + wordLength - 0w1, ~ wordLength) val endOfCode = endIC div wordLength val firstConstant = endIC + wordLength * 0w3 (* Add 3 for fn name, unused and profile count. *) val segSize = endOfCode + Word.fromInt(List.length(! constVec)) + 0w4 val codeVec = byteVecMake segSize val ic = ref 0w0 fun genByte b = byteVecSet(codeVec, !ic, b) before ic := !ic + 0w1 fun genByteCode(SimpleCode bytes, _, _) = (* Simple code - just generate the bytes. *) List.app genByte bytes | genByteCode(LabelCode _, _, _) = () | genByteCode(JumpInstruction{label={destination=ref dest}, jumpType, size=ref Size32, ...}, ic, _) = let val opc = case jumpType of SetHandler => opcode_setHandler32 | JumpFalse => opcode_jump32False | Jump => opcode_jump32 val diff = dest - (ic + 0w5) in genByte opc; genByte(wordToWord8 diff); (* This may be negative so we must use an arithmetic shift. *) genByte(wordToWord8(diff ~>> 0w8)); genByte(wordToWord8(diff ~>> 0w16)); genByte(wordToWord8(diff ~>> 0w24)) end | genByteCode(JumpInstruction{label={destination=ref dest}, jumpType, size=ref Size16, ...}, ic, _) = if dest <= ic then (* Jump back. *) let val _ = jumpType = Jump orelse raise InternalError "genByteCode - back jump" val diff = ic - dest val _ = diff < 0wx10000 orelse raise InternalError "genByteCode - jump range" in genByte opcode_jumpBack16; genByte(wordToWord8 diff); genByte(wordToWord8(diff >> 0w8)) end else let val opc = case jumpType of SetHandler => opcode_setHandler16 | JumpFalse => opcode_jump16False | Jump => opcode_jump16 val diff = dest - (ic + 0w3) val _ = diff < 0wx10000 orelse raise InternalError "genByteCode - jump range" in genByte opc; genByte(wordToWord8 diff); genByte(wordToWord8(diff >> 0w8)) end | genByteCode(JumpInstruction{label={destination=ref dest}, jumpType, size=ref Size8, ...}, ic, _) = if dest <= ic then (* Jump back. *) let val _ = jumpType = Jump orelse raise InternalError "genByteCode - back jump" val diff = ic - dest val _ = diff < 0wx100 orelse raise InternalError "genByteCode - jump range" in genByte opcode_jumpBack8; genByte(wordToWord8 diff) end else let val opc = case jumpType of SetHandler => opcode_setHandler | JumpFalse => opcode_jumpFalse | Jump => opcode_jump val diff = dest - (ic + 0w2) val _ = diff < 0wx100 orelse raise InternalError "genByteCode - jump range" in genByte opc; genByte(wordToWord8 diff) end | genByteCode(PushConstant{ constNum, size=ref Size32, ... }, ic, _) = let val constAddr = firstConstant + Word.fromInt constNum * wordLength (* Offsets are calculated from the END of the instruction *) val offset = constAddr - (ic + 0w5) in genByte(opcode_constAddr32); genByte(wordToWord8 offset); genByte(wordToWord8(offset >> 0w8)); genByte(wordToWord8(offset >> 0w16)); genByte(wordToWord8(offset >> 0w24)) end | genByteCode(PushConstant{ constNum, size=ref Size16, ... }, ic, _) = let val constAddr = firstConstant + Word.fromInt constNum * wordLength val offset = constAddr - (ic + 0w3) val _ = offset < 0wx10000 orelse raise InternalError "genByteCode - constant range" in genByte(opcode_constAddr16); genByte(wordToWord8 offset); genByte(wordToWord8(offset >> 0w8)) end | genByteCode(PushConstant{ constNum, size=ref Size8, ... }, ic, _) = let val constAddr = firstConstant + Word.fromInt constNum * wordLength val offset = constAddr - (ic + 0w2) val _ = offset < 0wx100 orelse raise InternalError "genByteCode - constant range" in genByte(opcode_constAddr8); genByte(wordToWord8 offset) end | genByteCode(IndexedCase{labels, size=ref Size32, ...}, ic, _) = let val nCases = List.length labels val () = genByte(opcode_case32) val () = genByte(Word8.fromInt nCases) val () = genByte(Word8.fromInt (nCases div 256)) val startOffset = ic+0w3 (* Offsets are relative to here. *) fun putLabel{destination=ref dest} = let val diff = dest - startOffset val _ = dest > startOffset orelse raise InternalError "genByteCode - indexed case" in genByte(wordToWord8 diff); genByte(wordToWord8(diff >> 0w8)); genByte(wordToWord8(diff >> 0w16)); genByte(wordToWord8(diff >> 0w24)) end in List.app putLabel labels end | genByteCode(IndexedCase{labels, size=ref Size16, ...}, ic, _) = let val nCases = List.length labels val () = genByte(opcode_case16) val () = genByte(Word8.fromInt nCases) val () = genByte(Word8.fromInt (nCases div 256)) val startOffset = ic+0w3 (* Offsets are relative to here. *) fun putLabel{destination=ref dest} = let val diff = dest - startOffset val _ = dest > startOffset orelse raise InternalError "genByteCode - indexed case" val _ = diff < 0wx10000 orelse raise InternalError "genByteCode - indexed case" in genByte(wordToWord8 diff); genByte(wordToWord8(diff >> 0w8)) end in List.app putLabel labels end | genByteCode(IndexedCase{size=ref Size8, ...}, _, _) = raise InternalError "genByteCode - IndexedCase byte" in foldCode genByteCode () ops; (codeVec (* Return the completed code. *), endIC (* And the size. *)) end fun setLong (value, addrs, seg) = let fun putBytes(value, a, seg, i) = if i = wordLength then () else ( byteVecSet(seg, if littleEndian() then a+i else a+wordLength-i-0w1, Word8.fromInt(value mod 256)); putBytes(value div 256, a, seg, i+0w1) ) in putBytes(value, addrs, seg, 0w0) end (* Adds the constants onto the code, and copies the code into a new segment *) fun copyCode (cvec as Code{ printAssemblyCode, printStream, procName, constVec, stage1Code, ...}, maxStack, resultClosure) = let local val revCode = List.rev(!stage1Code) (* Add a stack check. *) val stackCheck = if maxStack < 256 then SimpleCode[opcode_stackSize8, Word8.fromInt maxStack] else SimpleCode[opcode_stackSize16, Word8.fromInt maxStack, Word8.fromInt(maxStack div 256)] in val codeList = stackCheck :: revCode end val (byteVec, endIC) = genCode(codeList, cvec) (* +3 for profile count, function name and constants count *) val numOfConst = List.length(! constVec) val endOfCode = endIC div wordLength val segSize = endOfCode + Word.fromInt numOfConst + 0w4 val firstConstant = endIC + wordLength * 0w3 (* Add 3 for fn name, unused and profile count. *) (* Put in the number of constants. This must go in before we actually put in any constants. *) local val addr = ((segSize - 0w1) * wordLength) in val () = setLong (numOfConst + 3, addr, byteVec) end (* Now we've filled in all the size info we need to convert the segment into a proper code segment before it's safe to put in any ML values. *) val codeVec = byteVecToCodeVec(byteVec, resultClosure) local val name : string = procName val nameWord : machineWord = toMachineWord name in val () = codeVecPutWord (codeVec, endOfCode, nameWord) end (* This used to be used on X86 for the register mask. *) val () = codeVecPutWord (codeVec, endOfCode+0w1, toMachineWord 1) (* Profile ref. A byte ref used by the profiler in the RTS. *) local val v = RunCall.allocateByteMemory(0w1, Word.fromLargeWord(Word8.toLargeWord(Word8.orb(F_mutable, F_bytes)))) fun clear 0w0 = () | clear i = (assignByte(v, i-0w1, 0w0); clear (i-0w1)) val () = clear wordSize in val () = codeVecPutWord (codeVec, endOfCode+0w2, toMachineWord v) end (* and then copy the constants from the constant list. *) local fun setConstant(value, num) = let val constAddr = (firstConstant div wordLength) + num in codeVecPutWord (codeVec, constAddr, value); num+0w1 end in val _ = List.foldl setConstant 0w0 (!constVec) end in if printAssemblyCode then (* print out the code *) (printCode (codeVec, procName, endIC, printStream); printStream"\n") else (); codeVecLock(codeVec, resultClosure) end (* copyCode *) fun addItemToList(item, Code{stage1Code, ...}) = stage1Code := item :: !stage1Code val genOpcode = addItemToList fun putBranchInstruction(brOp, label, cvec) = addItemToList(JumpInstruction{label=label, jumpType=brOp, size = ref Size32}, cvec) fun setLabel(label, cvec) = addItemToList(LabelCode label, cvec) fun createLabel () = { destination=ref 0w0 } local fun genOpc(opc, cvec) = addItemToList(SimpleCode [opc], cvec) and genOpcByte(opc, arg1, cvec) = addItemToList(SimpleCode [opc, Word8.fromInt arg1], cvec) and genOpcWord(opc, arg1, cvec) = addItemToList(SimpleCode[opc, Word8.fromInt arg1, Word8.fromInt (arg1 div 256)], cvec) open IEEEReal fun encodeRound TO_NEAREST = 0 | encodeRound TO_NEGINF = 1 | encodeRound TO_POSINF = 2 | encodeRound TO_ZERO = 3 in fun genRaiseEx cvec = genOpc (opcode_raiseEx, cvec) fun genLock cvec = genOpc (opcode_lock, cvec) fun genLdexc cvec = genOpc (opcode_ldexc, cvec) fun genPushHandler cvec = genOpc (opcode_pushHandler, cvec) fun genRTSCallFast(0, cvec) = genOpc (opcode_callFastRTS0, cvec) | genRTSCallFast(1, cvec) = genOpc (opcode_callFastRTS1, cvec) | genRTSCallFast(2, cvec) = genOpc (opcode_callFastRTS2, cvec) | genRTSCallFast(3, cvec) = genOpc (opcode_callFastRTS3, cvec) | genRTSCallFast(4, cvec) = genOpc (opcode_callFastRTS4, cvec) | genRTSCallFast(5, cvec) = genOpc (opcode_callFastRTS5, cvec) | genRTSCallFast(_, _) = raise InternalError "genRTSFastCall" - fun genRTSCallFull(0, cvec) = genOpc (opcode_callFullRTS0, cvec) +(* fun genRTSCallFull(0, cvec) = genOpc (opcode_callFullRTS0, cvec) | genRTSCallFull(1, cvec) = genOpc (opcode_callFullRTS1, cvec) | genRTSCallFull(2, cvec) = genOpc (opcode_callFullRTS2, cvec) | genRTSCallFull(3, cvec) = genOpc (opcode_callFullRTS3, cvec) | genRTSCallFull(4, cvec) = genOpc (opcode_callFullRTS4, cvec) | genRTSCallFull(5, cvec) = genOpc (opcode_callFullRTS5, cvec) - | genRTSCallFull(_, _) = raise InternalError "genRTSCallFull" + | genRTSCallFull(_, _) = raise InternalError "genRTSCallFull"*) fun genContainer (size, cvec) = genOpcWord(opcode_containerW, size, cvec) and genTupleFromContainer (size, cvec) = genOpcWord(opcode_tuple_containerW, size, cvec) fun genCase (nCases, cvec) = let val labels = List.tabulate(nCases, fn _ => createLabel()) in addItemToList(IndexedCase{labels=labels, size=ref Size32}, cvec); labels end (* For the moment don't try to merge stack resets. *) fun resetStack(0, _, _) = () | resetStack(offset, true, cvec) = if offset < 0 then raise InternalError "resetStack" else if offset > 255 then genOpcWord(opcode_resetR_w, offset, cvec) else if offset > 3 then genOpcByte(opcode_resetRB, offset, cvec) else addItemToList(SimpleCode[opcode_resetR_1 + Word8.fromInt(offset - 1)], cvec) | resetStack(offset, false, cvec) = if offset < 0 then raise InternalError "resetStack" else if offset > 255 then genOpcWord(opcode_resetW, offset, cvec) else if offset > 2 then genOpcByte(opcode_resetB, offset, cvec) else addItemToList(SimpleCode[opcode_reset_1 + Word8.fromInt(offset - 1)], cvec) fun genCallClosure cvec = genOpc (opcode_callClosure, cvec) fun genTailCall (toslide, slideby, cvec) = if toslide < 256 andalso slideby < 256 then case (toslide, slideby) of (3, 2) => genOpc (opcode_tail3_2, cvec) | (3, 3) => genOpc (opcode_tail3_3, cvec) | (3, _) => genOpcByte(opcode_tail3b, slideby, cvec) | (4, _) => genOpcByte(opcode_tail4b, slideby, cvec) | (_, _) => (* General byte case *) addItemToList(SimpleCode[opcode_tailbb, Word8.fromInt toslide, Word8.fromInt slideby], cvec) else (* General case. *) addItemToList( SimpleCode[opcode_tail, Word8.fromInt toslide, Word8.fromInt(toslide div 256), Word8.fromInt slideby, Word8.fromInt (slideby div 256)], cvec) fun pushConst (value : machineWord, cvec) = if isShort value andalso toShort value < 0w32768 then let val iVal = Word.toInt (toShort value); in if iVal = 10 then genOpc (opcode_const_10, cvec) else if iVal <= 4 then genOpc (opcode_const_0 + Word8.fromInt iVal, cvec) else if iVal < 256 then genOpcByte (opcode_constIntB, iVal, cvec) else genOpcWord (opcode_constIntW, iVal, cvec) end else (* address or large short *) addItemToList(PushConstant{constNum = addConstToVec(value, cvec), size=ref Size32}, cvec) fun genRTSCallFastRealtoReal cvec = genOpc (opcode_callFastRTSRtoR, cvec) and genRTSCallFastRealRealtoReal cvec = genOpc (opcode_callFastRTSRRtoR, cvec) and genRTSCallFastGeneraltoReal cvec = genOpc (opcode_callFastRTSGtoR, cvec) and genRTSCallFastRealGeneraltoReal cvec = genOpc (opcode_callFastRTSRGtoR, cvec) and genRTSCallFastFloattoFloat cvec = genOpc (opcode_callFastRTSFtoF, cvec) and genRTSCallFastFloatFloattoFloat cvec = genOpc (opcode_callFastRTSFFtoF, cvec) and genRTSCallFastGeneraltoFloat cvec = genOpc (opcode_callFastRTSGtoF, cvec) and genRTSCallFastFloatGeneraltoFloat cvec = genOpc (opcode_callFastRTSFGtoF, cvec) fun genDoubleToFloat(SOME rnding, cvec) = genOpcByte(opcode_realToFloat, encodeRound rnding, cvec) | genDoubleToFloat(NONE, cvec) = genOpcByte(opcode_realToFloat, 5, cvec) and genRealToInt(rnding, cvec) = genOpcByte(opcode_realToInt, encodeRound rnding, cvec) and genFloatToInt(rnding, cvec) = genOpcByte(opcode_floatToInt, encodeRound rnding, cvec) end local fun gen1 (opW, opB, opF, first, arg1, cvec) = if first <= arg1 andalso arg1 < first+List.length opF then addItemToList(SimpleCode[List.nth(opF, arg1 - first)], cvec) else if 0 <= arg1 andalso arg1 <= 255 then addItemToList(SimpleCode [opB, Word8.fromInt arg1], cvec) else addItemToList( SimpleCode [opW, Word8.fromInt arg1, Word8.fromInt(arg1 div 256)], cvec) in fun genReturn (arg1, cvec) = let val ops = [opcode_return_0, opcode_return_1, opcode_return_2, opcode_return_3] in gen1 (opcode_returnW, opcode_returnB, ops, 0, arg1, cvec) end fun genLocal (arg1, cvec) = let val ops = [opcode_local_0, opcode_local_1, opcode_local_2, opcode_local_3, opcode_local_4, opcode_local_5, opcode_local_6, opcode_local_7, opcode_local_8, opcode_local_9, opcode_local_10, opcode_local_11] in gen1 (opcode_localW, opcode_localB, ops, 0, arg1, cvec) end fun genIndirect (arg1, cvec) = let val ops = [opcode_indirect_0, opcode_indirect_1, opcode_indirect_2, opcode_indirect_3, opcode_indirect_4, opcode_indirect_5] in gen1 (opcode_indirectW, opcode_indirectB, ops, 0, arg1, cvec) end (* genMoveToVec is now only used for mutually recursive closures. *) fun genMoveToVec (arg1, cvec) = gen1 (opcode_moveToVecW, opcode_moveToVecB, [], 0, arg1, cvec) fun genSetStackVal (arg1, cvec) = gen1 (opcode_setStackValW, opcode_setStackValB, [], 0, arg1, cvec) fun genTuple (arg1, cvec) = let val ops = [opcode_tuple_2, opcode_tuple_3, opcode_tuple_4] in gen1 (opcode_tupleW, opcode_tupleB, ops, 2, arg1, cvec) end end fun genEnterIntCatch _ = () and genEnterIntCall _ = () val opcode_notBoolean = SimpleCode [opcode_notBoolean] val opcode_isTagged = SimpleCode [opcode_isTagged] and opcode_cellLength = SimpleCode [opcode_cellLength] and opcode_cellFlags = SimpleCode [opcode_cellFlags] and opcode_clearMutable = SimpleCode [opcode_clearMutable] and opcode_atomicIncr = SimpleCode [opcode_atomicIncr] and opcode_atomicDecr = SimpleCode [opcode_atomicDecr] and opcode_atomicReset = SimpleCode [opcode_atomicReset] and opcode_longWToTagged = SimpleCode [opcode_longWToTagged] and opcode_signedToLongW = SimpleCode [opcode_signedToLongW] and opcode_unsignedToLongW = SimpleCode [opcode_unsignedToLongW] and opcode_realAbs = SimpleCode [opcode_realAbs] and opcode_realNeg = SimpleCode [opcode_realNeg] and opcode_fixedIntToReal = SimpleCode [opcode_fixedIntToReal] and opcode_fixedIntToFloat = SimpleCode [opcode_fixedIntToFloat] and opcode_floatToReal = SimpleCode [opcode_floatToReal] val opcode_equalWord = SimpleCode [opcode_equalWord] and opcode_lessSigned = SimpleCode [opcode_lessSigned] and opcode_lessUnsigned = SimpleCode [opcode_lessUnsigned] and opcode_lessEqSigned = SimpleCode [opcode_lessEqSigned] and opcode_lessEqUnsigned = SimpleCode [opcode_lessEqUnsigned] and opcode_greaterSigned = SimpleCode [opcode_greaterSigned] and opcode_greaterUnsigned = SimpleCode [opcode_greaterUnsigned] and opcode_greaterEqSigned = SimpleCode [opcode_greaterEqSigned] and opcode_greaterEqUnsigned = SimpleCode [opcode_greaterEqUnsigned] val opcode_fixedAdd = SimpleCode [opcode_fixedAdd] val opcode_fixedSub = SimpleCode [opcode_fixedSub] val opcode_fixedMult = SimpleCode [opcode_fixedMult] val opcode_fixedQuot = SimpleCode [opcode_fixedQuot] val opcode_fixedRem = SimpleCode [opcode_fixedRem] val opcode_fixedDiv = SimpleCode [opcode_fixedDiv] val opcode_fixedMod = SimpleCode [opcode_fixedMod] val opcode_wordAdd = SimpleCode [opcode_wordAdd] val opcode_wordSub = SimpleCode [opcode_wordSub] val opcode_wordMult = SimpleCode [opcode_wordMult] val opcode_wordDiv = SimpleCode [opcode_wordDiv] val opcode_wordMod = SimpleCode [opcode_wordMod] val opcode_wordAnd = SimpleCode [opcode_wordAnd] val opcode_wordOr = SimpleCode [opcode_wordOr] val opcode_wordXor = SimpleCode [opcode_wordXor] val opcode_wordShiftLeft = SimpleCode [opcode_wordShiftLeft] val opcode_wordShiftRLog = SimpleCode [opcode_wordShiftRLog] val opcode_wordShiftRArith = SimpleCode [opcode_wordShiftRArith] val opcode_allocByteMem = SimpleCode [opcode_allocByteMem] val opcode_lgWordEqual = SimpleCode [opcode_lgWordEqual] val opcode_lgWordLess = SimpleCode [opcode_lgWordLess] val opcode_lgWordLessEq = SimpleCode [opcode_lgWordLessEq] val opcode_lgWordGreater = SimpleCode [opcode_lgWordGreater] val opcode_lgWordGreaterEq = SimpleCode [opcode_lgWordGreaterEq] val opcode_lgWordAdd = SimpleCode [opcode_lgWordAdd] val opcode_lgWordSub = SimpleCode [opcode_lgWordSub] val opcode_lgWordMult = SimpleCode [opcode_lgWordMult] val opcode_lgWordDiv = SimpleCode [opcode_lgWordDiv] val opcode_lgWordMod = SimpleCode [opcode_lgWordMod] val opcode_lgWordAnd = SimpleCode [opcode_lgWordAnd] val opcode_lgWordOr = SimpleCode [opcode_lgWordOr] val opcode_lgWordXor = SimpleCode [opcode_lgWordXor] val opcode_lgWordShiftLeft = SimpleCode [opcode_lgWordShiftLeft] val opcode_lgWordShiftRLog = SimpleCode [opcode_lgWordShiftRLog] val opcode_lgWordShiftRArith = SimpleCode [opcode_lgWordShiftRArith] val opcode_realEqual = SimpleCode [opcode_realEqual] val opcode_realLess = SimpleCode [opcode_realLess] val opcode_realLessEq = SimpleCode [opcode_realLessEq] val opcode_realGreater = SimpleCode [opcode_realGreater] val opcode_realGreaterEq = SimpleCode [opcode_realGreaterEq] val opcode_realUnordered = SimpleCode [opcode_realUnordered] val opcode_realAdd = SimpleCode [opcode_realAdd] val opcode_realSub = SimpleCode [opcode_realSub] val opcode_realMult = SimpleCode [opcode_realMult] val opcode_realDiv = SimpleCode [opcode_realDiv] and opcode_floatAbs = SimpleCode [opcode_floatAbs] and opcode_floatNeg = SimpleCode [opcode_floatNeg] val opcode_floatEqual = SimpleCode [opcode_floatEqual] val opcode_floatLess = SimpleCode [opcode_floatLess] val opcode_floatLessEq = SimpleCode [opcode_floatLessEq] val opcode_floatGreater = SimpleCode [opcode_floatGreater] val opcode_floatGreaterEq = SimpleCode [opcode_floatGreaterEq] val opcode_floatUnordered = SimpleCode [opcode_floatUnordered] val opcode_floatAdd = SimpleCode [opcode_floatAdd] val opcode_floatSub = SimpleCode [opcode_floatSub] val opcode_floatMult = SimpleCode [opcode_floatMult] val opcode_floatDiv = SimpleCode [opcode_floatDiv] val opcode_getThreadId = SimpleCode [opcode_getThreadId] val opcode_allocWordMemory = SimpleCode [opcode_allocWordMemory] val opcode_alloc_ref = SimpleCode [opcode_alloc_ref] val opcode_loadMLWord = SimpleCode [opcode_loadMLWord] val opcode_loadMLByte = SimpleCode [opcode_loadMLByte] val opcode_loadC8 = SimpleCode [opcode_loadC8] val opcode_loadC16 = SimpleCode [opcode_loadC16] val opcode_loadC32 = SimpleCode [opcode_loadC32] val opcode_loadC64 = SimpleCode [opcode_loadC64] val opcode_loadCFloat = SimpleCode [opcode_loadCFloat] val opcode_loadCDouble = SimpleCode [opcode_loadCDouble] val opcode_loadUntagged = SimpleCode [opcode_loadUntagged] val opcode_storeMLWord = SimpleCode [opcode_storeMLWord] val opcode_storeMLByte = SimpleCode [opcode_storeMLByte] val opcode_storeC8 = SimpleCode [opcode_storeC8] val opcode_storeC16 = SimpleCode [opcode_storeC16] val opcode_storeC32 = SimpleCode [opcode_storeC32] val opcode_storeC64 = SimpleCode [opcode_storeC64] val opcode_storeCFloat = SimpleCode [opcode_storeCFloat] val opcode_storeCDouble = SimpleCode [opcode_storeCDouble] val opcode_storeUntagged = SimpleCode [opcode_storeUntagged] val opcode_blockMoveWord = SimpleCode [opcode_blockMoveWord] val opcode_blockMoveByte = SimpleCode [opcode_blockMoveByte] val opcode_blockEqualByte = SimpleCode [opcode_blockEqualByte] val opcode_blockCompareByte = SimpleCode [opcode_blockCompareByte] val opcode_deleteHandler = SimpleCode [opcode_deleteHandler] + val opcode_allocCSpace = SimpleCode [opcode_allocCSpace] + val opcode_freeCSpace = SimpleCode [opcode_freeCSpace] structure Sharing = struct type code = code type opcode = opcode type labels = labels type closureRef = closureRef end end; diff --git a/mlsource/MLCompiler/CodeTree/ByteCode/INTCODECONSSIG.sml b/mlsource/MLCompiler/CodeTree/ByteCode/INTCODECONSSIG.sml index 6c905f9d..25fec3e1 100644 --- a/mlsource/MLCompiler/CodeTree/ByteCode/INTCODECONSSIG.sml +++ b/mlsource/MLCompiler/CodeTree/ByteCode/INTCODECONSSIG.sml @@ -1,215 +1,216 @@ (* - Copyright (c) 2016-18 David C.J. Matthews + Copyright (c) 2016-18, 2020 David C.J. Matthews This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *) signature INTCODECONSSIG = sig type machineWord = Address.machineWord type address = Address.address type code type opcode type labels type closureRef val opcode_notBoolean: opcode val opcode_isTagged: opcode and opcode_cellLength: opcode and opcode_cellFlags: opcode and opcode_clearMutable: opcode and opcode_atomicIncr: opcode and opcode_atomicDecr: opcode and opcode_atomicReset: opcode and opcode_longWToTagged: opcode and opcode_signedToLongW: opcode and opcode_unsignedToLongW: opcode and opcode_realAbs: opcode and opcode_realNeg: opcode and opcode_fixedIntToReal: opcode and opcode_fixedIntToFloat: opcode and opcode_floatToReal: opcode and opcode_floatAbs: opcode and opcode_floatNeg: opcode val opcode_equalWord: opcode and opcode_lessSigned: opcode and opcode_lessUnsigned: opcode and opcode_lessEqSigned: opcode and opcode_lessEqUnsigned: opcode and opcode_greaterSigned: opcode and opcode_greaterUnsigned: opcode and opcode_greaterEqSigned: opcode and opcode_greaterEqUnsigned: opcode val opcode_fixedAdd: opcode val opcode_fixedSub: opcode val opcode_fixedMult: opcode val opcode_fixedQuot: opcode val opcode_fixedRem: opcode val opcode_fixedDiv: opcode val opcode_fixedMod: opcode val opcode_wordAdd: opcode val opcode_wordSub: opcode val opcode_wordMult: opcode val opcode_wordDiv: opcode val opcode_wordMod: opcode val opcode_wordAnd: opcode val opcode_wordOr: opcode val opcode_wordXor: opcode val opcode_wordShiftLeft: opcode val opcode_wordShiftRLog: opcode val opcode_wordShiftRArith: opcode val opcode_allocByteMem: opcode val opcode_lgWordEqual: opcode val opcode_lgWordLess: opcode val opcode_lgWordLessEq: opcode val opcode_lgWordGreater: opcode val opcode_lgWordGreaterEq: opcode val opcode_lgWordAdd: opcode val opcode_lgWordSub: opcode val opcode_lgWordMult: opcode val opcode_lgWordDiv: opcode val opcode_lgWordMod: opcode val opcode_lgWordAnd: opcode val opcode_lgWordOr: opcode val opcode_lgWordXor: opcode val opcode_lgWordShiftLeft: opcode val opcode_lgWordShiftRLog: opcode val opcode_lgWordShiftRArith: opcode val opcode_realEqual: opcode val opcode_realLess: opcode val opcode_realLessEq: opcode val opcode_realGreater: opcode val opcode_realGreaterEq: opcode val opcode_realUnordered: opcode val opcode_realAdd: opcode val opcode_realSub: opcode val opcode_realMult: opcode val opcode_realDiv: opcode val opcode_floatEqual: opcode val opcode_floatLess: opcode val opcode_floatLessEq: opcode val opcode_floatGreater: opcode val opcode_floatGreaterEq: opcode val opcode_floatUnordered: opcode val opcode_floatAdd: opcode val opcode_floatSub: opcode val opcode_floatMult: opcode val opcode_floatDiv: opcode val opcode_getThreadId: opcode val opcode_allocWordMemory: opcode val opcode_alloc_ref: opcode val opcode_loadMLWord: opcode val opcode_loadMLByte: opcode val opcode_loadC8: opcode val opcode_loadC16: opcode val opcode_loadC32: opcode val opcode_loadC64: opcode val opcode_loadCFloat: opcode val opcode_loadCDouble: opcode val opcode_loadUntagged: opcode val opcode_storeMLWord: opcode val opcode_storeMLByte: opcode val opcode_storeC8: opcode val opcode_storeC16: opcode val opcode_storeC32: opcode val opcode_storeC64: opcode val opcode_storeCFloat: opcode val opcode_storeCDouble: opcode val opcode_storeUntagged: opcode val opcode_blockMoveWord: opcode val opcode_blockMoveByte: opcode val opcode_blockEqualByte: opcode val opcode_blockCompareByte: opcode val opcode_deleteHandler: opcode + val opcode_allocCSpace: opcode + val opcode_freeCSpace: opcode val codeCreate: string * Universal.universal list -> code (* makes the initial segment. *) (* GEN- routines all put a value at the instruction counter and add an appropriate amount to it. *) (* gen... - put instructions and their operands. *) val genCallClosure : code -> unit val genRaiseEx : code -> unit val genLock : code -> unit val genLdexc : code -> unit val genPushHandler : code -> unit val genReturn : int * code -> unit val genLocal : int * code -> unit val genIndirect : int * code -> unit val genMoveToVec : int * code -> unit val genSetStackVal : int * code -> unit val genCase : int * code -> labels list val genTuple : int * code -> unit val genTailCall : int * int * code -> unit val genDoubleToFloat: IEEEReal.rounding_mode option * code -> unit and genRealToInt: IEEEReal.rounding_mode * code -> unit and genFloatToInt: IEEEReal.rounding_mode * code -> unit val genRTSCallFast: int * code -> unit - val genRTSCallFull: int * code -> unit val genRTSCallFastRealtoReal: code -> unit val genRTSCallFastRealRealtoReal: code -> unit val genRTSCallFastGeneraltoReal: code -> unit val genRTSCallFastRealGeneraltoReal: code -> unit val genRTSCallFastFloattoFloat: code -> unit val genRTSCallFastFloatFloattoFloat: code -> unit val genRTSCallFastGeneraltoFloat: code -> unit val genRTSCallFastFloatGeneraltoFloat: code -> unit val genOpcode: opcode * code -> unit (* genEnter instructions are only needed when machine-code routines can call interpreted routines or vice-versa. The enterInt instruction causes the interpreter to be entered and the argument indicates the reason. *) val genEnterIntCatch : code -> unit val genEnterIntCall : code * int -> unit (* pushConst - Generates code to push a constant. *) val pushConst : machineWord * code -> unit (* Create a container on the stack *) val genContainer : int * code -> unit (* Create a tuple from a container. *) val genTupleFromContainer : int * code -> unit (* copyCode - Finish up after compiling a function. *) val copyCode : code * int * closureRef -> unit (* putBranchInstruction puts in an instruction which involves a forward reference. *) datatype jumpTypes = Jump | JumpFalse | SetHandler val putBranchInstruction: jumpTypes * labels * code -> unit val createLabel: unit -> labels (* Define the position of a label. *) val setLabel: labels * code -> unit val resetStack: int * bool * code -> unit (* Set a pending reset *) structure Sharing: sig type code = code type opcode = opcode type labels = labels type closureRef = closureRef end end ; diff --git a/mlsource/MLCompiler/CodeTree/ByteCode/INTGCODE.ML b/mlsource/MLCompiler/CodeTree/ByteCode/INTGCODE.ML index 15646f36..7576c01a 100644 --- a/mlsource/MLCompiler/CodeTree/ByteCode/INTGCODE.ML +++ b/mlsource/MLCompiler/CodeTree/ByteCode/INTGCODE.ML @@ -1,1157 +1,1195 @@ (* Copyright (c) 2000 Cambridge University Technical Services Limited Further development copyright David C.J. Matthews 2016-18,2020 This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *) (* Title: Generate interpretable code for Poly system from the code tree. Author: Dave Matthews, Cambridge University Computer Laboratory Copyright Cambridge University 1985 *) (* This generates byte-code that is interpreted by the run-time system. It is now used as a fall-back to allow Poly/ML to run on non-X86 architectures. Early versions were used as a porting aid while a native code-generator was being developed and the "enter-int" instructions that were needed for that have been retained although they no longer actually generate code. *) functor INTGCODE ( structure CODECONS : INTCODECONSSIG structure BACKENDTREE: BackendIntermediateCodeSig structure CODE_ARRAY: CODEARRAYSIG sharing CODECONS.Sharing = BACKENDTREE.Sharing = CODE_ARRAY.Sharing ) : GENCODESIG = struct open CODECONS open Address open BACKENDTREE open Misc open CODE_ARRAY val word0 = toMachineWord 0; val DummyValue : machineWord = word0; (* used as result of "raise e" etc. *) type caseForm = { cases : (backendIC * word) list, test : backendIC, caseType: caseType, default : backendIC } (* Where the result, if any, should go *) datatype whereto = NoResult (* discard result *) | ToStack (* Need a result but it can stay on the pseudo-stack *); (* Are we at the end of the function. *) datatype tail = EndOfProc | NotEnd (* Code generate a function or global declaration *) fun codegen (pt, cvec, resultClosure, numOfArgs, localCount, parameters) = let datatype decEntry = StackAddr of int | Empty val decVec = Array.array (localCount, Empty) (* Count of number of items on the stack. *) val realstackptr = ref 1 (* The closure ptr is already there *) (* Maximum size of the stack. *) val maxStack = ref 1 (* Exited - set to true if we have jumped out. *) val exited = ref false; (* Push a value onto the stack. *) fun incsp () = ( realstackptr := !realstackptr + 1; if !realstackptr > !maxStack then maxStack := !realstackptr else () ) (* An entry has been removed from the stack. *) fun decsp () = realstackptr := !realstackptr - 1; fun pushLocalStackValue addr = ( genLocal(!realstackptr + addr, cvec); incsp() ) (* Loads a local, argument or closure value; translating local stack addresses to real stack offsets. *) fun locaddr(BICLoadArgument locn) = pushLocalStackValue (numOfArgs-locn) | locaddr(BICLoadLocal locn) = ( (* positive address - on the stack. *) case Array.sub (decVec, locn) of StackAddr n => pushLocalStackValue (~ n) | _ => (* Should be on the stack, not a function. *) raise InternalError "locaddr: bad stack address" ) | locaddr(BICLoadClosure locn) = (* closure-pointer relative *) ( pushLocalStackValue ~1; (* The closure itself. *) genIndirect (locn+1, cvec) (* The value in the closure. +1 because first item is code addr. *) ) | locaddr BICLoadRecursive = pushLocalStackValue ~1 (* The closure itself - first value on the stack. *) (* generates code from the tree *) fun gencde (pt : backendIC, whereto : whereto, tailKind : tail, loopAddr) : unit = let (* Save the stack pointer value here. We may want to reset the stack. *) val oldsp = !realstackptr; (* Load the address and index value for byte operations. For ML memory operations the base is the address of an ML heap cell whereas for C operations it is a large-word box containing an address in C memory. That doesn't affect this code but the interpreter has to deal with these differently. *) fun genByteAddress{base, index, offset} = ( gencde (base, ToStack, NotEnd, loopAddr); (* Because the index and offset are both byte counts we can just add them if we need both. *) case (index, offset) of (NONE, offset) => (pushConst (toMachineWord offset, cvec); incsp()) | (SOME indexVal, 0w0) => gencde (indexVal, ToStack, NotEnd, loopAddr) | (SOME indexVal, offset) => ( gencde (indexVal, ToStack, NotEnd, loopAddr); pushConst (toMachineWord offset, cvec); genOpcode(opcode_wordAdd, cvec) ) ) (* Load the address, index value and offset for non-byte operations. Because the offset has already been scaled by the size of the operand we have to load the index and offset separately. *) fun genNonByteAddress{base, index, offset} = ( gencde (base, ToStack, NotEnd, loopAddr); case index of NONE => (pushConst (toMachineWord 0, cvec); incsp()) | SOME indexVal => gencde (indexVal, ToStack, NotEnd, loopAddr); pushConst (toMachineWord offset, cvec); incsp() ) val () = case pt of BICEval evl => genEval (evl, tailKind) | BICExtract ext => (* This may just be being used to discard a value which isn't used on this branch. *) if whereto = NoResult then () else locaddr ext | BICField {base, offset} => (gencde (base, ToStack, NotEnd, loopAddr); genIndirect (offset, cvec)) | BICLoadContainer {base, offset} => (gencde (base, ToStack, NotEnd, loopAddr); genIndirect (offset, cvec)) | BICLambda lam => genProc (lam, false, fn () => ()) | BICConstnt(w, _) => let val () = pushConst (w, cvec); in incsp () end | BICCond (testPart, thenPart, elsePart) => genCond (testPart, thenPart, elsePart, whereto, tailKind, loopAddr) | BICNewenv(decls, exp) => let (* Processes a list of entries. *) (* Mutually recursive declarations. May be either lambdas or constants. Recurse down the list pushing the addresses of the closure vectors, then unwind the recursion and fill them in. *) fun genMutualDecs [] = () | genMutualDecs ({lambda, addr, ...} :: otherDecs) = genProc (lambda, true, fn() => ( Array.update (decVec, addr, StackAddr (! realstackptr)); genMutualDecs (otherDecs) )) fun codeDecls(BICRecDecs dl) = genMutualDecs dl | codeDecls(BICDecContainer{size, addr}) = ( (* If this is a container we have to process it here otherwise it will be removed in the stack adjustment code. *) genContainer(size, cvec); (* Push the address of this container. *) realstackptr := !realstackptr + size + 1; (* Pushes N words plus the address. *) Array.update (decVec, addr, StackAddr(!realstackptr)) ) | codeDecls(BICDeclar{value, addr, ...}) = ( gencde (value, ToStack, NotEnd, loopAddr); Array.update (decVec, addr, StackAddr(!realstackptr)) ) | codeDecls(BICNullBinding exp) = gencde (exp, NoResult, NotEnd, loopAddr) in List.app codeDecls decls; gencde (exp, whereto, tailKind, loopAddr) end | BICBeginLoop {loop=body, arguments} => (* Execute the body which will contain at least one Loop instruction. There will also be path(s) which don't contain Loops and these will drop through. *) let val args = List.map #1 arguments (* Evaluate each of the arguments, pushing the result onto the stack. *) fun genLoopArg ({addr, value, ...}) = ( gencde (value, ToStack, NotEnd, loopAddr); Array.update (decVec, addr, StackAddr (!realstackptr)); !realstackptr (* Return the posn on the stack. *) ) val argIndexList = map genLoopArg args; val startSp = ! realstackptr; (* Remember the current top of stack. *) val startLoop = createLabel () val () = setLabel(startLoop, cvec) (* Start of loop *) in (* Process the body, passing the jump-back address down for the Loop instruction(s). *) gencde (body, whereto, tailKind, SOME(startLoop, startSp, argIndexList)) (* Leave the arguments on the stack. They can be cleared later if needed. *) end | BICLoop argList => (* Jump back to the enclosing BeginLoop. *) let val (startLoop, startSp, argIndexList) = case loopAddr of SOME l => l | NONE => raise InternalError "No BeginLoop for Loop instr" (* Evaluate the arguments. First push them to the stack because evaluating an argument may depend on the current value of others. Only when we've evaluated all of them can we overwrite the original argument positions. *) fun loadArgs ([], []) = !realstackptr - startSp (* The offset of all the args. *) | loadArgs (arg:: argList, _ :: argIndexList) = let (* Evaluate all the arguments. *) val () = gencde (arg, ToStack, NotEnd, NONE); val argOffset = loadArgs(argList, argIndexList); in genSetStackVal(argOffset, cvec); (* Copy the arg over. *) decsp(); (* The argument has now been popped. *) argOffset end | loadArgs _ = raise InternalError "loadArgs: Mismatched arguments"; val _: int = loadArgs(List.map #1 argList, argIndexList) in if !realstackptr <> startSp then resetStack (!realstackptr - startSp, false, cvec) (* Remove any local variables. *) else (); (* Jump back to the start of the loop. *) putBranchInstruction(Jump, startLoop, cvec) end | BICRaise exp => let val () = gencde (exp, ToStack, NotEnd, loopAddr) val () = genRaiseEx cvec; in exited := true end | BICHandle {exp, handler, exPacketAddr} => let (* Save old handler *) val () = genPushHandler cvec val () = incsp () val handlerLabel = createLabel() val () = putBranchInstruction (SetHandler, handlerLabel, cvec) val () = incsp() (* Code generate the body; "NotEnd" because we have to come back to remove the handler; "ToStack" because delHandler needs a result to carry down. *) val () = gencde (exp, ToStack, NotEnd, loopAddr) (* Now get out of the handler and restore the old one. *) val () = genOpcode(opcode_deleteHandler, cvec) val skipHandler = createLabel() val () = putBranchInstruction (Jump, skipHandler, cvec) (* Now process the handler itself. First we have to reset the stack. Note that we have to use "ToStack" again to be consistent with the stack-handling in the body-part. If we actually wanted "NoResult", the stack adjustment code at the end of gencde will take care of this. This means that I don't want to do any clever "end-of-function" optimisation either. SPF 6/1/97 *) val () = realstackptr := oldsp val () = exited := false val () = setLabel (handlerLabel, cvec) (* If we were executing machine code we must re-enter the interpreter. *) val () = genEnterIntCatch cvec (* Push the exception packet and set the address. *) val () = genLdexc cvec val () = incsp () val () = Array.update (decVec, exPacketAddr, StackAddr(!realstackptr)) val () = gencde (handler, ToStack, NotEnd, loopAddr) (* Have to remove the exception packet. *) val () = resetStack(1, true, cvec) val () = decsp() (* Finally fix-up the jump around the handler *) val () = setLabel (skipHandler, cvec) in exited := false end | BICCase ({cases, test, default, firstIndex, ...}) => let val () = gencde (test, ToStack, NotEnd, loopAddr) (* Label to jump to at the end of each case. *) val exitJump = createLabel() val () = if firstIndex = 0w0 then () else ( (* Subtract lower limit. Don't check for overflow. Instead allow large value to wrap around and check in "case" instruction. *) pushConst(toMachineWord firstIndex, cvec); genOpcode(opcode_wordSub, cvec) ) (* Generate the case instruction followed by the table of jumps. *) val nCases = List.length cases val caseLabels = genCase (nCases, cvec) val () = decsp () (* The default case, if any, follows the case statement. *) (* If we have a jump to the default set it to jump here. *) local fun fixDefault(NONE, defCase) = setLabel(defCase, cvec) | fixDefault(SOME _, _) = () in val () = ListPair.appEq fixDefault (cases, caseLabels) end val () = gencde (default, whereto, tailKind, loopAddr); val () = exited := false; fun genCases(SOME body, label) = ( (* First exit from the previous case or the default if this is the first. *) if !exited then () else putBranchInstruction(Jump, exitJump, cvec); (* Remove the result - the last case will leave it. *) case whereto of ToStack => decsp () | NoResult => (); (* Fix up the jump to come here. *) setLabel(label, cvec); exited := false; gencde (body, whereto, tailKind, loopAddr) ) | genCases(NONE, _) = () val () = ListPair.appEq genCases (cases, caseLabels) (* Finally set the exit jump to come here. *) val () = setLabel (exitJump, cvec) in exited := false end | BICTuple recList => let val size = List.length recList in (* Move the fields into the vector. *) List.app(fn v => gencde (v, ToStack, NotEnd, loopAddr)) recList; genTuple (size, cvec); realstackptr := !realstackptr - (size - 1) end | BICSetContainer{container, tuple, filter} => (* Copy the contents of a tuple into a container. If the tuple is a Tuple instruction we can avoid generating the tuple and then unpacking it and simply copy the fields that make up the tuple directly into the container. *) ( case tuple of BICTuple cl => (* Simply set the container from the values. *) let (* Load the address of the container. *) val _ = gencde (container, ToStack, NotEnd, loopAddr); fun setValues([], _, _) = () | setValues(v::tl, sourceOffset, destOffset) = if sourceOffset < BoolVector.length filter andalso BoolVector.sub(filter, sourceOffset) then ( gencde (v, ToStack, NotEnd, loopAddr); (* Move the entry into the container. This instruction pops the value to be moved but not the destination. *) genMoveToVec(destOffset, cvec); decsp(); setValues(tl, sourceOffset+1, destOffset+1) ) else setValues(tl, sourceOffset+1, destOffset) in setValues(cl, 0, 0) (* The container address is still on the stack. *) end | _ => let (* General case. *) (* First the target tuple, then the container. *) val () = gencde (tuple, ToStack, NotEnd, loopAddr) val () = gencde (container, ToStack, NotEnd, loopAddr) val last = BoolVector.foldli(fn (i, true, _) => i | (_, false, n) => n) ~1 filter fun copy (sourceOffset, destOffset) = if BoolVector.sub(filter, sourceOffset) then ( (* Duplicate the tuple address . *) genLocal(1, cvec); genIndirect(sourceOffset, cvec); genMoveToVec(destOffset, cvec); if sourceOffset = last then () else copy (sourceOffset+1, destOffset+1) ) else copy(sourceOffset+1, destOffset) in copy (0, 0) (* The container and tuple addresses are still on the stack. *) end ) | BICTagTest { test, tag, ... } => ( (* Convert this into a simple equality function. *) gencde (test, ToStack, NotEnd, loopAddr); pushConst (toMachineWord tag, cvec); genOpcode(opcode_equalWord, cvec) ) - | BICGetThreadId => + | BICNullary {oper=BuiltIns.GetCurrentThreadId} => ( genOpcode(opcode_getThreadId, cvec); incsp() ) + + | BICNullary {oper=BuiltIns.CheckRTSException} => + ( (* Do nothing. This is done in the RTS call. *) + ) | BICUnary { oper, arg1 } => let open BuiltIns val () = gencde (arg1, ToStack, NotEnd, loopAddr) in case oper of NotBoolean => genOpcode(opcode_notBoolean, cvec) | IsTaggedValue => genOpcode(opcode_isTagged, cvec) | MemoryCellLength => genOpcode(opcode_cellLength, cvec) | MemoryCellFlags => genOpcode(opcode_cellFlags, cvec) | ClearMutableFlag => genOpcode(opcode_clearMutable, cvec) | AtomicIncrement => genOpcode(opcode_atomicIncr, cvec) | AtomicDecrement => genOpcode(opcode_atomicDecr, cvec) | AtomicReset => genOpcode(opcode_atomicReset, cvec) | LongWordToTagged => genOpcode(opcode_longWToTagged, cvec) | SignedToLongWord => genOpcode(opcode_signedToLongW, cvec) | UnsignedToLongWord => genOpcode(opcode_unsignedToLongW, cvec) | RealAbs PrecDouble => genOpcode(opcode_realAbs, cvec) | RealNeg PrecDouble => genOpcode(opcode_realNeg, cvec) | RealFixedInt PrecDouble => genOpcode(opcode_fixedIntToReal, cvec) | RealAbs PrecSingle => genOpcode(opcode_floatAbs, cvec) | RealNeg PrecSingle => genOpcode(opcode_floatNeg, cvec) | RealFixedInt PrecSingle => genOpcode(opcode_fixedIntToFloat, cvec) | FloatToDouble => genOpcode(opcode_floatToReal, cvec) | DoubleToFloat rnding => genDoubleToFloat(rnding, cvec) | RealToInt (PrecDouble, rnding) => genRealToInt(rnding, cvec) | RealToInt (PrecSingle, rnding) => genFloatToInt(rnding, cvec) | TouchAddress => resetStack(1, false, cvec) (* Discard this *) + | AllocCStack => genOpcode(opcode_allocCSpace, cvec) end | BICBinary { oper, arg1, arg2 } => let open BuiltIns val () = gencde (arg1, ToStack, NotEnd, loopAddr) val () = gencde (arg2, ToStack, NotEnd, loopAddr) in case oper of WordComparison{test=TestEqual, ...} => genOpcode(opcode_equalWord, cvec) | WordComparison{test=TestLess, isSigned=true} => genOpcode(opcode_lessSigned, cvec) | WordComparison{test=TestLessEqual, isSigned=true} => genOpcode(opcode_lessEqSigned, cvec) | WordComparison{test=TestGreater, isSigned=true} => genOpcode(opcode_greaterSigned, cvec) | WordComparison{test=TestGreaterEqual, isSigned=true} => genOpcode(opcode_greaterEqSigned, cvec) | WordComparison{test=TestLess, isSigned=false} => genOpcode(opcode_lessUnsigned, cvec) | WordComparison{test=TestLessEqual, isSigned=false} => genOpcode(opcode_lessEqUnsigned, cvec) | WordComparison{test=TestGreater, isSigned=false} => genOpcode(opcode_greaterUnsigned, cvec) | WordComparison{test=TestGreaterEqual, isSigned=false} => genOpcode(opcode_greaterEqUnsigned, cvec) | WordComparison{test=TestUnordered, ...} => raise InternalError "WordComparison: TestUnordered" | FixedPrecisionArith ArithAdd => genOpcode(opcode_fixedAdd, cvec) | FixedPrecisionArith ArithSub => genOpcode(opcode_fixedSub, cvec) | FixedPrecisionArith ArithMult => genOpcode(opcode_fixedMult, cvec) | FixedPrecisionArith ArithQuot => genOpcode(opcode_fixedQuot, cvec) | FixedPrecisionArith ArithRem => genOpcode(opcode_fixedRem, cvec) | FixedPrecisionArith ArithDiv => raise InternalError "TODO: FixedPrecisionArith ArithDiv" | FixedPrecisionArith ArithMod => raise InternalError "TODO: FixedPrecisionArith ArithMod" | WordArith ArithAdd => genOpcode(opcode_wordAdd, cvec) | WordArith ArithSub => genOpcode(opcode_wordSub, cvec) | WordArith ArithMult => genOpcode(opcode_wordMult, cvec) | WordArith ArithDiv => genOpcode(opcode_wordDiv, cvec) | WordArith ArithMod => genOpcode(opcode_wordMod, cvec) | WordArith _ => raise InternalError "WordArith - unimplemented instruction" | WordLogical LogicalAnd => genOpcode(opcode_wordAnd, cvec) | WordLogical LogicalOr => genOpcode(opcode_wordOr, cvec) | WordLogical LogicalXor => genOpcode(opcode_wordXor, cvec) | WordShift ShiftLeft => genOpcode(opcode_wordShiftLeft, cvec) | WordShift ShiftRightLogical => genOpcode(opcode_wordShiftRLog, cvec) | WordShift ShiftRightArithmetic => genOpcode(opcode_wordShiftRArith, cvec) | AllocateByteMemory => genOpcode(opcode_allocByteMem, cvec) | LargeWordComparison TestEqual => genOpcode(opcode_lgWordEqual, cvec) | LargeWordComparison TestLess => genOpcode(opcode_lgWordLess, cvec) | LargeWordComparison TestLessEqual => genOpcode(opcode_lgWordLessEq, cvec) | LargeWordComparison TestGreater => genOpcode(opcode_lgWordGreater, cvec) | LargeWordComparison TestGreaterEqual => genOpcode(opcode_lgWordGreaterEq, cvec) | LargeWordComparison TestUnordered => raise InternalError "LargeWordComparison: TestUnordered" | LargeWordArith ArithAdd => genOpcode(opcode_lgWordAdd, cvec) | LargeWordArith ArithSub => genOpcode(opcode_lgWordSub, cvec) | LargeWordArith ArithMult => genOpcode(opcode_lgWordMult, cvec) | LargeWordArith ArithDiv => genOpcode(opcode_lgWordDiv, cvec) | LargeWordArith ArithMod => genOpcode(opcode_lgWordMod, cvec) | LargeWordArith _ => raise InternalError "LargeWordArith - unimplemented instruction" | LargeWordLogical LogicalAnd => genOpcode(opcode_lgWordAnd, cvec) | LargeWordLogical LogicalOr => genOpcode(opcode_lgWordOr, cvec) | LargeWordLogical LogicalXor => genOpcode(opcode_lgWordXor, cvec) | LargeWordShift ShiftLeft => genOpcode(opcode_lgWordShiftLeft, cvec) | LargeWordShift ShiftRightLogical => genOpcode(opcode_lgWordShiftRLog, cvec) | LargeWordShift ShiftRightArithmetic => genOpcode(opcode_lgWordShiftRArith, cvec) | RealComparison (TestEqual, PrecDouble) => genOpcode(opcode_realEqual, cvec) | RealComparison (TestLess, PrecDouble) => genOpcode(opcode_realLess, cvec) | RealComparison (TestLessEqual, PrecDouble) => genOpcode(opcode_realLessEq, cvec) | RealComparison (TestGreater, PrecDouble) => genOpcode(opcode_realGreater, cvec) | RealComparison (TestGreaterEqual, PrecDouble) => genOpcode(opcode_realGreaterEq, cvec) | RealComparison (TestUnordered, PrecDouble) => genOpcode(opcode_realUnordered, cvec) | RealComparison (TestEqual, PrecSingle) => genOpcode(opcode_floatEqual, cvec) | RealComparison (TestLess, PrecSingle) => genOpcode(opcode_floatLess, cvec) | RealComparison (TestLessEqual, PrecSingle) => genOpcode(opcode_floatLessEq, cvec) | RealComparison (TestGreater, PrecSingle) => genOpcode(opcode_floatGreater, cvec) | RealComparison (TestGreaterEqual, PrecSingle) => genOpcode(opcode_floatGreaterEq, cvec) | RealComparison (TestUnordered, PrecSingle) => genOpcode(opcode_floatUnordered, cvec) | RealArith (ArithAdd, PrecDouble) => genOpcode(opcode_realAdd, cvec) | RealArith (ArithSub, PrecDouble) => genOpcode(opcode_realSub, cvec) | RealArith (ArithMult, PrecDouble) => genOpcode(opcode_realMult, cvec) | RealArith (ArithDiv, PrecDouble) => genOpcode(opcode_realDiv, cvec) | RealArith (ArithAdd, PrecSingle) => genOpcode(opcode_floatAdd, cvec) | RealArith (ArithSub, PrecSingle) => genOpcode(opcode_floatSub, cvec) | RealArith (ArithMult, PrecSingle) => genOpcode(opcode_floatMult, cvec) | RealArith (ArithDiv, PrecSingle) => genOpcode(opcode_floatDiv, cvec) | RealArith _ => raise InternalError "RealArith - unimplemented instruction" + + | FreeCStack => genOpcode(opcode_freeCSpace, cvec) ; decsp() (* Removes one item from the stack. *) end | BICAllocateWordMemory {numWords as BICConstnt(length, _), flags as BICConstnt(flagByte, _), initial } => if isShort length andalso toShort length = 0w1 andalso isShort flagByte andalso toShort flagByte = 0wx40 then (* This is a very common case. *) ( gencde (initial, ToStack, NotEnd, loopAddr); genOpcode(opcode_alloc_ref, cvec) ) else let val () = gencde (numWords, ToStack, NotEnd, loopAddr) val () = gencde (flags, ToStack, NotEnd, loopAddr) val () = gencde (initial, ToStack, NotEnd, loopAddr) in genOpcode(opcode_allocWordMemory, cvec); decsp(); decsp() end | BICAllocateWordMemory { numWords, flags, initial } => let val () = gencde (numWords, ToStack, NotEnd, loopAddr) val () = gencde (flags, ToStack, NotEnd, loopAddr) val () = gencde (initial, ToStack, NotEnd, loopAddr) in genOpcode(opcode_allocWordMemory, cvec); decsp(); decsp() end | BICLoadOperation { kind=LoadStoreMLWord _, address={base, index=NONE, offset}} => ( (* If the index is a constant, frequently zero, we can use indirection. The offset is a byte count so has to be divided by the word size but it should always be an exact multiple. *) gencde (base, ToStack, NotEnd, loopAddr); offset mod wordSize = 0w0 orelse raise InternalError "gencde: BICLoadOperation - not word multiple"; genIndirect (Word.toInt(offset div wordSize), cvec) ) | BICLoadOperation { kind=LoadStoreMLWord _, address={base, index=SOME indexVal, offset}} => let (* Variable index. *) val () = gencde (base, ToStack, NotEnd, loopAddr) val () = gencde (indexVal, ToStack, NotEnd, loopAddr) val () = (pushConst (toMachineWord offset, cvec); incsp()) in genOpcode(opcode_loadMLWord, cvec); decsp(); decsp() end | BICLoadOperation { kind=LoadStoreMLByte _, address} => ( genByteAddress address; genOpcode(opcode_loadMLByte, cvec); decsp() ) | BICLoadOperation { kind=LoadStoreC8, address} => ( genByteAddress address; genOpcode(opcode_loadC8, cvec); decsp() ) | BICLoadOperation { kind=LoadStoreC16, address} => ( genNonByteAddress address; genOpcode(opcode_loadC16, cvec); decsp(); decsp() ) | BICLoadOperation { kind=LoadStoreC32, address} => ( genNonByteAddress address; genOpcode(opcode_loadC32, cvec); decsp(); decsp() ) | BICLoadOperation { kind=LoadStoreC64, address} => ( wordSize = 0w8 orelse raise InternalError "LoadStoreC64 but not 64-bit mode"; genNonByteAddress address; genOpcode(opcode_loadC64, cvec); decsp(); decsp() ) | BICLoadOperation { kind=LoadStoreCFloat, address} => ( genNonByteAddress address; genOpcode(opcode_loadCFloat, cvec); decsp(); decsp() ) | BICLoadOperation { kind=LoadStoreCDouble, address} => ( genNonByteAddress address; genOpcode(opcode_loadCDouble, cvec); decsp(); decsp() ) | BICLoadOperation { kind=LoadStoreUntaggedUnsigned, address} => ( genNonByteAddress address; genOpcode(opcode_loadUntagged, cvec); decsp(); decsp() ) | BICStoreOperation { kind=LoadStoreMLWord _, address={base, index=NONE, offset}, value } => let (* No index. We could almost use move_to_vec here except that it leaves the destination address on the stack instead of replacing it with "unit". *) val () = gencde (base, ToStack, NotEnd, loopAddr) val () = pushConst (toMachineWord 0, cvec) val () = incsp() val () = pushConst (toMachineWord offset, cvec) val () = incsp() val () = gencde (value, ToStack, NotEnd, loopAddr) in genOpcode(opcode_storeMLWord, cvec); decsp(); decsp(); decsp() end | BICStoreOperation { kind=LoadStoreMLWord _, address={base, index=SOME indexVal, offset}, value } => let (* Variable index *) val () = gencde (base, ToStack, NotEnd, loopAddr) val () = gencde (indexVal, ToStack, NotEnd, loopAddr) val () = pushConst (toMachineWord offset, cvec) val () = incsp() val () = gencde (value, ToStack, NotEnd, loopAddr) in genOpcode(opcode_storeMLWord, cvec); decsp(); decsp(); decsp() end | BICStoreOperation { kind=LoadStoreMLByte _, address, value } => ( genByteAddress address; gencde (value, ToStack, NotEnd, loopAddr); genOpcode(opcode_storeMLByte, cvec); decsp(); decsp() ) | BICStoreOperation { kind=LoadStoreC8, address, value} => ( genByteAddress address; gencde (value, ToStack, NotEnd, loopAddr); genOpcode(opcode_storeC8, cvec); decsp(); decsp() ) | BICStoreOperation { kind=LoadStoreC16, address, value} => ( genNonByteAddress address; gencde (value, ToStack, NotEnd, loopAddr); genOpcode(opcode_storeC16, cvec); decsp(); decsp(); decsp() ) | BICStoreOperation { kind=LoadStoreC32, address, value} => ( genNonByteAddress address; gencde (value, ToStack, NotEnd, loopAddr); genOpcode(opcode_storeC32, cvec); decsp(); decsp(); decsp() ) | BICStoreOperation { kind=LoadStoreC64, address, value} => ( genNonByteAddress address; gencde (value, ToStack, NotEnd, loopAddr); genOpcode(opcode_storeC64, cvec); decsp(); decsp(); decsp() ) | BICStoreOperation { kind=LoadStoreCFloat, address, value} => ( genNonByteAddress address; gencde (value, ToStack, NotEnd, loopAddr); genOpcode(opcode_storeCFloat, cvec); decsp(); decsp(); decsp() ) | BICStoreOperation { kind=LoadStoreCDouble, address, value} => ( genNonByteAddress address; gencde (value, ToStack, NotEnd, loopAddr); genOpcode(opcode_storeCDouble, cvec); decsp(); decsp(); decsp() ) | BICStoreOperation { kind=LoadStoreUntaggedUnsigned, address, value} => ( genNonByteAddress address; gencde (value, ToStack, NotEnd, loopAddr); genOpcode(opcode_storeUntagged, cvec); decsp(); decsp(); decsp() ) | BICBlockOperation { kind=BlockOpMove{isByteMove=true}, sourceLeft, destRight, length } => ( genByteAddress sourceLeft; genByteAddress destRight; gencde (length, ToStack, NotEnd, loopAddr); genOpcode(opcode_blockMoveByte, cvec); decsp(); decsp(); decsp(); decsp() ) | BICBlockOperation { kind=BlockOpMove{isByteMove=false}, sourceLeft, destRight, length } => ( genNonByteAddress sourceLeft; genNonByteAddress destRight; gencde (length, ToStack, NotEnd, loopAddr); genOpcode(opcode_blockMoveWord, cvec); decsp(); decsp(); decsp(); decsp(); decsp(); decsp() ) | BICBlockOperation { kind=BlockOpEqualByte, sourceLeft, destRight, length } => ( genByteAddress sourceLeft; genByteAddress destRight; gencde (length, ToStack, NotEnd, loopAddr); genOpcode(opcode_blockEqualByte, cvec); decsp(); decsp(); decsp(); decsp() ) | BICBlockOperation { kind=BlockOpCompareByte, sourceLeft, destRight, length } => ( genByteAddress sourceLeft; genByteAddress destRight; gencde (length, ToStack, NotEnd, loopAddr); genOpcode(opcode_blockCompareByte, cvec); decsp(); decsp(); decsp(); decsp() ) | BICArbitrary { longCall, ... } => (* Just use the long-precision case in the interpreted version. *) ( gencde (longCall, whereto, tailKind, loopAddr) ) in (* body of gencde *) (* This ensures that there is precisely one item on the stack if whereto = ToStack and no items if whereto = NoResult. There are two points to note carefully here: (1) Negative stack adjustments are legal if we have exited. This is because matchFailFn can cut the stack back too far for its immediately enclosing expression. This is harmless because the code actually exits that expression. (2) A stack adjustment of ~1 is legal if we're generating a declaration in "ToStack" mode, because not all declarations actually generate the dummy value that we expect. This used to be handled in resetStack itself, but it's more transparent to do it here. (In addition, there was a bug in resetStack - it accumulated the stack resets, but didn't correctly accumulate these "~1" dummy value pushes.) It's all much better now. SPF 9/1/97 *) case whereto of ToStack => let val newsp = oldsp + 1; val adjustment = !realstackptr - newsp val () = if !exited orelse adjustment = 0 then () else if adjustment < ~1 then raise InternalError ("gencde: bad adjustment " ^ Int.toString adjustment) (* Hack for declarations that should push values, but don't *) else if adjustment = ~1 then pushConst (DummyValue, cvec) else resetStack (adjustment, true, cvec) in realstackptr := newsp end | NoResult => let val adjustment = !realstackptr - oldsp val () = if !exited orelse adjustment = 0 then () else if adjustment < 0 then raise InternalError ("gencde: bad adjustment " ^ Int.toString adjustment) else resetStack (adjustment, false, cvec) in realstackptr := oldsp end end (* gencde *) (* doNext is only used for mutually recursive functions where a function may not be able to fill in its closure if it does not have all the remaining declarations. *) (* TODO: This always creates the closure on the heap even when makeClosure is false. *) and genProc ({ closure=[], localCount, body, argTypes, name, ...}: bicLambdaForm, mutualDecs, doNext: unit -> unit) : unit = let (* Create a one word item for the closure. This is returned for recursive references and filled in with the address of the code when we've finished. *) val closure = makeConstantClosure() val newCode : code = codeCreate(name, parameters); (* Code-gen function. No non-local references. *) val () = codegen (body, newCode, closure, List.length argTypes, localCount, parameters); val () = pushConst(closureAsAddress closure, cvec); val () = incsp(); in if mutualDecs then doNext () else () end | genProc ({ localCount, body, name, argTypes, closure, ...}, mutualDecs, doNext) = let (* Full closure required. *) val resClosure = makeConstantClosure() val newCode = codeCreate (name, parameters) (* Code-gen function. *) val () = codegen (body, newCode, resClosure, List.length argTypes, localCount, parameters) val sizeOfClosure = List.length closure + 1; in if mutualDecs then let (* Have to make the closure now and fill it in later. *) (* This previously used genGetStore which at one time was widely used. *) val () = pushConst(toMachineWord sizeOfClosure, cvec) (* Length *) val () = pushConst(toMachineWord F_mutable, cvec) (* Flags *) val () = pushConst(toMachineWord 0, cvec) (* Initialise to zero. *) val () = genOpcode(opcode_allocWordMemory, cvec) (* Allocate the memory. *) val () = incsp () (* Put code address into closure *) val () = pushConst(codeAddressFromClosure resClosure, cvec) val () = genMoveToVec(0, cvec) val entryAddr : int = !realstackptr val () = doNext () (* Any mutually recursive functions. *) (* Push the address of the vector - If we have processed other closures the vector will no longer be on the top of the stack. *) val () = pushLocalStackValue (~ entryAddr) (* Load items for the closure. *) fun loadItems ([], _) = () | loadItems (v :: vs, addr : int) = let (* Generate an item and move it into the vector *) val () = gencde (BICExtract v, ToStack, NotEnd, NONE) val () = genMoveToVec(addr, cvec) val () = decsp () in loadItems (vs, addr + 1) end val () = loadItems (closure, 1) val () = genLock cvec (* Lock it. *) (* Remove the extra reference. *) val () = resetStack (1, false, cvec) in realstackptr := !realstackptr - 1 end else let (* Put it on the stack. *) val () = pushConst (codeAddressFromClosure resClosure, cvec) val () = incsp () val () = List.app (fn pt => gencde (BICExtract pt, ToStack, NotEnd, NONE)) closure val () = genTuple (sizeOfClosure, cvec) in realstackptr := !realstackptr - (sizeOfClosure - 1) end end and genCond (testCode, thenCode, elseCode, whereto, tailKind, loopAddr) = let val () = gencde (testCode, ToStack, NotEnd, loopAddr) val toElse = createLabel() and exitJump = createLabel() val () = putBranchInstruction(JumpFalse, toElse, cvec) val () = decsp() val () = gencde (thenCode, whereto, tailKind, loopAddr) (* Get rid of the result from the stack. If there is a result then the ``else-part'' will push it. *) val () = case whereto of ToStack => decsp () | NoResult => () val thenExited = !exited val () = if thenExited then () else putBranchInstruction (Jump, exitJump, cvec) (* start of "else part" *) val () = setLabel (toElse, cvec) val () = exited := false val () = gencde (elseCode, whereto, tailKind, loopAddr) val elseExited = !exited val () = setLabel (exitJump, cvec) in exited := (thenExited andalso elseExited) (* Only exited if both sides did. *) end (* genCond *) and genEval (eval, tailKind : tail) : unit = let val argList : backendIC list = List.map #1 (#argList eval) val argsToPass : int = List.length argList; (* Load arguments *) fun loadArgs [] = () | loadArgs (v :: vs) = let (* Push each expression onto the stack. *) val () = gencde(v, ToStack, NotEnd, NONE) in loadArgs vs end; (* Called after the args and the closure to call have been pushed onto the stack. *) fun callClosure () : unit = case tailKind of NotEnd => (* Normal call. *) genCallClosure cvec | EndOfProc => (* Tail recursive call. *) let (* Get the return address onto the top of the stack. *) val () = pushLocalStackValue 0 (* Slide the return address, closure and args over the old closure, return address and args, and reset the stack. Then jump to the closure. *) val () = genTailCall(argsToPass + 2, !realstackptr - 1 + (numOfArgs - argsToPass), cvec); (* It's "-1" not "-2", because we didn't bump the realstackptr when we pushed the return address. SPF 3/1/97 *) in exited := true end (* Have to guarantee that the expression to return the function is evaluated before the arguments. *) (* Returns true if evaluating it later is safe. *) fun safeToLeave (BICConstnt _) = true | safeToLeave (BICLambda _) = true | safeToLeave (BICExtract _) = true | safeToLeave (BICField {base, ...}) = safeToLeave base | safeToLeave (BICLoadContainer {base, ...}) = safeToLeave base | safeToLeave _ = false val () = if (case argList of [] => true | _ => safeToLeave (#function eval)) then let (* Can load the args first. *) val () = loadArgs argList in gencde (#function eval, ToStack, NotEnd, NONE) end else let (* The expression for the function is too complicated to risk leaving. It might have a side-effect and we must ensure that any side-effects it has are done before the arguments are loaded. *) val () = gencde(#function eval, ToStack, NotEnd, NONE); val () = loadArgs(argList); (* Load the function again. *) val () = genLocal(argsToPass, cvec); in incsp () end val () = callClosure () (* Call the function. *) (* Make sure we interpret when we return from the call *) val () = genEnterIntCall (cvec, argsToPass) in (* body of genEval *) realstackptr := !realstackptr - argsToPass (* Args popped by caller. *) end (* Generate the function. *) (* Assume we always want a result. There is otherwise a problem if the called routine returns a result of type void (i.e. no result) but the caller wants a result (e.g. the identity function). *) val () = gencde (pt, ToStack, EndOfProc, NONE) val () = if !exited then () else genReturn (numOfArgs, cvec); in (* body of codegen *) (* Having code-generated the body of the function, it is copied into a new data segment. *) copyCode(cvec, !maxStack, resultClosure) end (* codegen *); fun gencodeLambda({ name, body, argTypes, localCount, ...}:bicLambdaForm, parameters, closure) = let (* make the code buffer for the new function. *) val newCode : code = codeCreate (name, parameters) (* This function must have no non-local references. *) in codegen (body, newCode, closure, List.length argTypes, localCount, parameters) end local val makeEntryPoint: string -> machineWord = RunCall.rtsCallFull1 "PolyCreateEntryPointObject" fun rtsCall makeCall (entryName: string, numOfArgs, debugArgs: Universal.universal list): machineWord = let open Address val cvec = codeCreate (entryName, debugArgs) val entryPointAddr = makeEntryPoint entryName (* Each argument is at the same offset, essentially we're just shifting them *) fun genLocals 0 = () | genLocals n = (genLocal(numOfArgs +1, cvec); genLocals (n-1)) val () = genLocals numOfArgs val () = pushConst(entryPointAddr, cvec) val () = makeCall(numOfArgs, cvec) val () = genReturn (numOfArgs, cvec) val closure = makeConstantClosure() val () = copyCode(cvec, numOfArgs+1, closure) in closureAsAddress closure end in structure Foreign = struct val rtsCallFast = rtsCall genRTSCallFast - and rtsCallFull = rtsCall genRTSCallFull fun rtsCallFastRealtoReal(entryName, debugArgs) = rtsCall (fn (_, c) => genRTSCallFastRealtoReal c) (entryName, 1, debugArgs) and rtsCallFastRealRealtoReal(entryName, debugArgs) = rtsCall (fn (_, c) => genRTSCallFastRealRealtoReal c) (entryName, 2, debugArgs) and rtsCallFastGeneraltoReal(entryName, debugArgs) = rtsCall (fn (_, c) => genRTSCallFastGeneraltoReal c) (entryName, 1, debugArgs) and rtsCallFastRealGeneraltoReal(entryName, debugArgs) = rtsCall (fn (_, c) => genRTSCallFastRealGeneraltoReal c) (entryName, 2, debugArgs) fun rtsCallFastFloattoFloat(entryName, debugArgs) = rtsCall (fn (_, c) => genRTSCallFastFloattoFloat c) (entryName, 1, debugArgs) and rtsCallFastFloatFloattoFloat(entryName, debugArgs) = rtsCall (fn (_, c) => genRTSCallFastFloatFloattoFloat c) (entryName, 2, debugArgs) and rtsCallFastGeneraltoFloat(entryName, debugArgs) = rtsCall (fn (_, c) => genRTSCallFastGeneraltoFloat c) (entryName, 1, debugArgs) and rtsCallFastFloatGeneraltoFloat(entryName, debugArgs) = rtsCall (fn (_, c) => genRTSCallFastFloatGeneraltoFloat c) (entryName, 2, debugArgs) + + + type abi = int + + (* This must match the type in Foreign.LowLevel. Once this is bootstrapped we could use that + type but note that this is the type we use within the compiler and we build Foreign.LowLevel + AFTER compiling this. *) + datatype cTypeForm = + CTypeFloatingPt | CTypePointer | CTypeSignedInt | CTypeUnsignedInt + | CTypeStruct of cType list | CTypeVoid + withtype cType = { typeForm: cTypeForm, align: word, size: word } + + val abiList: unit -> (string * abi) list = + RunCall.rtsCallFull0 "PolyInterpretedGetAbiList" + + (* foreignCall returns a function that actually calls the foreign function. *) + fun foreignCall(abi, argTypes, resultType) = + let + fun callFFI(fnAddr: LargeWord.word, argVec: LargeWord.word, resMem: LargeWord.word): unit = + raise Foreign.Foreign "foreignCall not implemented" + in + Address.toMachineWord callFFI + end + + fun buildCallBack(abi, argTypes, resultType) = + let + fun buildClosure (mlFun: LargeWord.word*LargeWord.word -> unit) = + (* The result is the SysWord.word holding the C function. *) + raise Foreign.Foreign "foreignCall not implemented" + in + Address.toMachineWord buildClosure + end end end structure Sharing = struct open BACKENDTREE.Sharing type closureRef = closureRef end end;