extensions
Steven Massey 3 years ago
parent 7a1848cbf6
commit b6f07a0bc0

@ -496,6 +496,21 @@ M3Result Pop (IM3Compilation o)
return result;
}
M3Result PopType (IM3Compilation o, u8 i_type)
{
M3Result result = m3Err_none;
u8 topType = GetStackTopType (o);
if (i_type == topType)
{
result = Pop (o);
}
else result = m3Err_typeMismatch;
return result;
}
M3Result UnwindBlockStack (IM3Compilation o)
{
@ -995,8 +1010,10 @@ M3Result Compile_Return (IM3Compilation o, m3opcode_t i_opcode)
if (GetFunctionNumReturns (o->function))
{
u8 type = GetFunctionReturnType (o->function, 0);
_ (ReturnStackTop (o));
_ (Pop (o));
_ (PopType (o, type));
}
_ (EmitOp (o, op_Return));
@ -1014,10 +1031,13 @@ M3Result Compile_End (IM3Compilation o, m3opcode_t i_opcode)
// function end:
if (o->block.depth == 0)
{
u8 valueType = GetSingleRetType(o->block.type);
u8 type = GetSingleRetType (o->block.type);
if (valueType)
if (type)
{
if (type != GetStackTopType (o))
_throw (m3Err_typeMismatch);
// if there are branches to the function end, then their values are in a register
// if the block happens to have its top in a register too, then we can patch the branch
// to here. Otherwise, an ReturnStackTop is appended to the end of the function (at B) and
@ -1034,11 +1054,11 @@ _ (EmitOp (o, op_Return));
_ (UnwindBlockStack (o));
// B: move register to return slot for branchehs
if (valueType)
if (type)
{
if (PatchBranches (o))
{
_ (PushRegister (o, valueType));
_ (PushRegister (o, type));
ReturnStackTop (o);
_ (EmitOp (o, op_Return));
}
@ -2179,7 +2199,7 @@ M3Result Compile_BlockStatements (IM3Compilation o)
#endif
IM3OpInfo opinfo = GetOpInfo(opcode);
_throwif(m3Err_unknownOpcode, opinfo == NULL);
_throwif (m3Err_unknownOpcode, opinfo == NULL);
if (opinfo->compiler) {
result = (* opinfo->compiler) (o, opcode);
@ -2209,7 +2229,7 @@ M3Result ValidateBlockEnd (IM3Compilation o, bool * o_copyStackTopToRegister)
* o_copyStackTopToRegister = false;
u8 valueType = GetSingleRetType(o->block.type);
u8 valueType = GetSingleRetType (o->block.type);
if (valueType != c_m3Type_none)
{

@ -159,6 +159,20 @@ u32 GetFunctionNumReturns (IM3Function i_function)
return numReturns;
}
u8 GetFunctionReturnType (IM3Function i_function, u32 i_index)
{
u8 type = c_m3Type_none;
if (i_index < GetFunctionNumReturns (i_function))
{
type = i_function->funcType->types [i_index];
}
return type;
}
u32 GetFunctionNumArgsAndLocals (IM3Function i_function)
{
if (i_function)

@ -68,6 +68,7 @@ cstr_t GetFunctionImportModuleName (IM3Function i_function);
cstr_t * GetFunctionNames (IM3Function i_function, u16 * o_numNames);
u32 GetFunctionNumArgs (IM3Function i_function);
u32 GetFunctionNumReturns (IM3Function i_function);
u8 GetFunctionReturnType (IM3Function i_function, u32 i_index);
u32 GetFunctionNumArgsAndLocals (IM3Function i_function);

@ -135,6 +135,7 @@ d_m3ErrorConst (functionStackOverflow, "compiling function overran its
d_m3ErrorConst (functionStackUnderrun, "compiling function underran the stack")
d_m3ErrorConst (mallocFailedCodePage, "memory allocation failed when acquiring a new M3 code page")
d_m3ErrorConst (settingImmutableGlobal, "attempting to set an immutable global")
d_m3ErrorConst (typeMismatch, "malformed Wasm: incorrect type on stack")
// runtime errors
d_m3ErrorConst (missingCompiledCode, "function is missing compiled m3 code")

Loading…
Cancel
Save