extensions
Steven Massey 3 years ago
parent c43b6ef42d
commit 34a313555f

@ -106,7 +106,7 @@ u16 GetTypeNumSlots (u8 i_type)
i16 GetStackTopIndex (IM3Compilation o)
{ d_m3Assert (o->stackIndex > 0 or IsStackPolymorphic (o));
{ d_m3Assert (o->stackIndex > o->slotFirstDynamicIndex or IsStackPolymorphic (o));
return o->stackIndex - 1;
}
@ -704,22 +704,19 @@ M3Result SetStackPolymorphic (IM3Compilation o)
}
bool PatchBranches (IM3Compilation o)
void PatchBranches (IM3Compilation o)
{
pc_t pc = GetPC (o);
IM3BranchPatch patches = o->block.patches;
pc_t patches = o->block.patches;
o->block.patches = NULL;
bool didPatch = patches;
while (patches)
{ m3log (compile, "patching location: %p to pc: %p", patches, pc);
patches->location = pc;
patches = patches->next;
pc_t next = * (pc_t *) patches;
* (pc_t *) patches = pc;
patches = next;
}
return didPatch;
}
//-------------------------------------------------------------------------------------------------------------------------
@ -923,7 +920,7 @@ _ (PushAllocatedSlot (o, type))
// TODO: update o->function->maxStackSlots
M3Result MoveStackSlotsR (IM3Compilation o, u16 i_targetSlot, u16 i_stackIndex, u16 i_endStackIndex,
u16 i_fillInSlot, u16 i_tempSlot, bool i_commitToStack)
u16 i_fillInSlot, u16 i_tempSlot)
{
M3Result result = m3Err_none;
@ -990,14 +987,11 @@ _ (CopyStackIndexToSlot (o, checkIndex, i_tempSlot));
_ (CopyStackIndexToSlot (o, i_stackIndex, i_targetSlot)); m3log (compile, " copying stack: %d to slot: %d\n", i_stackIndex, i_targetSlot);
}
_ (MoveStackSlotsR (o, i_targetSlot + numSlots, i_stackIndex + 1, i_endStackIndex, i_fillInSlot, i_tempSlot, i_commitToStack));
_ (MoveStackSlotsR (o, i_targetSlot + numSlots, i_stackIndex + 1, i_endStackIndex, i_fillInSlot, i_tempSlot));
if (not i_commitToStack)
{
// restore the stack state
o->wasmStack [i_stackIndex] = srcSlot;
o->wasmStack [preserveIndex] = collisionSlot;
}
// restore the stack state
o->wasmStack [i_stackIndex] = srcSlot;
o->wasmStack [preserveIndex] = collisionSlot;
}
_catch:
@ -1029,7 +1023,7 @@ _ (CopyStackTopToRegister (o, false));
u16 tempSlot = GetMaxUsedSlotPlusOne (o);
AlignSlotToType (& tempSlot, c_m3Type_i64);
_ (MoveStackSlotsR (o, i_targetBlock->topSlot, stackTop - (numResults - 1), endIndex, c_slotUnused, tempSlot, false));
_ (MoveStackSlotsR (o, i_targetBlock->topSlot, stackTop - (numResults - 1), endIndex, c_slotUnused, tempSlot));
// if (d_m3LogWasmStack) dump_type_stack (o);
}
@ -1069,29 +1063,30 @@ M3Result ReturnValues (IM3Compilation o, IM3CompilationScope i_targetBlock, bo
if (not IsStackPolymorphic (o))
_throwif (m3Err_typeCountMismatch, i_isBranch ? (blockHeight < numReturns) : (blockHeight != numReturns));
// return slots like args are 64-bit aligned
u16 returnSlot = numReturns * c_ioSlotCount;
u16 stackIndex = GetStackTopIndex (o);
for (u16 i = 0; i < numReturns; ++i)
if (numReturns)
{
u8 returnType = GetFuncTypeResultType (i_targetBlock->type, numReturns - 1 - i);
u8 stackType = GetStackTopTypeAtOffset (o, i);
if (IsStackPolymorphic (o) and stackType == c_m3Type_none)
stackType = returnType;
_throwif (m3Err_typeMismatch, returnType != stackType);
// return slots like args are 64-bit aligned
u16 returnSlot = numReturns * c_ioSlotCount;
u16 stackIndex = GetStackTopIndex (o);
returnSlot -= c_ioSlotCount;
_ (CopyStackIndexToSlot (o, stackIndex--, returnSlot));
for (u16 i = 0; i < numReturns; ++i)
{
u8 returnType = GetFuncTypeResultType (i_targetBlock->type, numReturns - 1 - i);
u8 stackType = GetStackTopTypeAtOffset (o, i);
if (IsStackPolymorphic (o) and stackType == c_m3Type_none)
stackType = returnType;
_throwif (m3Err_typeMismatch, returnType != stackType);
returnSlot -= c_ioSlotCount;
_ (CopyStackIndexToSlot (o, stackIndex--, returnSlot));
if (not i_isBranch)
Pop (o);
if (not i_isBranch)
_ (Pop (o));
}
}
_catch: return result;
}
@ -1381,20 +1376,14 @@ _ ((i_opcode == 0x23) ? Compile_GetGlobal (o, global) : Compile_SetGlo
M3Result EmitPatchingBranch (IM3Compilation o, IM3CompilationScope i_scope)
{
M3Result result ;
M3Result result = m3Err_none;
_try {
_ (EmitOp (o, op_Branch));
// IM3BranchPatch is two word struct; reserve two words
IM3BranchPatch patch = (IM3BranchPatch) ReservePointer (o); m3log (compile, "branch patch required at: %p", patch);
ReservePointer (o);
patch->next = i_scope->patches;
i_scope->patches = patch;
pc_t patch = EmitPointer (o, i_scope->patches); m3log (compile, "branch patch required at: %p", patch);
i_scope->patches = patch;
} _catch:
return result;
_catch: return result;
}
@ -1851,8 +1840,6 @@ _ (PreserveArgsAndLocals (o));
_ (EmitOp (o, op));
_ (EmitSlotNumOfStackTopAndPop (o));
i32 stackIndex = o->stackIndex;
pc_t * pc = (pc_t *) ReservePointer (o);
IM3FuncType blockType;

@ -43,26 +43,12 @@ enum
//-----------------------------------------------------------------------------------------------------------------------------------
// since the end location of a block is unknown when a branch is compiled, writing
// the actual address must deferred. A linked-list of patch locations is kept in
// M3CompilationScope. When the block compilation exits, it patches these addresses.
// this data structure is embedded into the code pages themselves
typedef struct M3BranchPatch
{
pc_t location;
struct M3BranchPatch * next;
}
M3BranchPatch;
typedef M3BranchPatch * IM3BranchPatch;
typedef struct M3CompilationScope
{
struct M3CompilationScope * outer;
pc_t pc; // used by ContinueLoop's
IM3BranchPatch patches;
pc_t patches;
i32 depth;
i16 initStackIndex;
u16 topSlot;

@ -87,10 +87,14 @@ void EmitSlotOffset (IM3Compilation o, const i32 i_offset)
}
void EmitPointer (IM3Compilation o, const void * const i_pointer)
pc_t EmitPointer (IM3Compilation o, const void * const i_pointer)
{
pc_t ptr = GetPagePC (o->page);
if (o->page)
EmitWord (o->page, i_pointer);
return ptr;
}
void * ReservePointer (IM3Compilation o)

@ -18,7 +18,7 @@ M3Result EnsureCodePageNumLines (IM3Compilation o, u32 i_numLines);
M3Result EmitOp (IM3Compilation o, IM3Operation i_operation);
void EmitConstant32 (IM3Compilation o, const u32 i_immediate);
void EmitSlotOffset (IM3Compilation o, const i32 i_offset);
void EmitPointer (IM3Compilation o, const void * const i_pointer);
pc_t EmitPointer (IM3Compilation o, const void * const i_pointer);
void * ReservePointer (IM3Compilation o);
pc_t GetPC (IM3Compilation o);

Loading…
Cancel
Save