more type validation during compilation

extensions
Steven Massey 3 years ago
parent b6f07a0bc0
commit 33b501fe26

@ -498,17 +498,17 @@ M3Result Pop (IM3Compilation o)
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 result = m3Err_none;
u8 topType = GetStackTopType (o);
if (i_type == topType or o->block.isPolymorphic)
{
result = Pop (o);
}
else result = m3Err_typeMismatch;
return result;
}
@ -1010,8 +1010,8 @@ M3Result Compile_Return (IM3Compilation o, m3opcode_t i_opcode)
if (GetFunctionNumReturns (o->function))
{
u8 type = GetFunctionReturnType (o->function, 0);
u8 type = GetFunctionReturnType (o->function, 0);
_ (ReturnStackTop (o));
_ (PopType (o, type));
}
@ -1035,9 +1035,9 @@ M3Result Compile_End (IM3Compilation o, m3opcode_t i_opcode)
if (type)
{
if (type != GetStackTopType (o))
_throw (m3Err_typeMismatch);
if (not o->block.isPolymorphic and 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

@ -162,14 +162,14 @@ u32 GetFunctionNumReturns (IM3Function i_function)
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;
u8 type = c_m3Type_none;
if (i_index < GetFunctionNumReturns (i_function))
{
type = i_function->funcType->types [i_index];
}
return type;
}

@ -68,7 +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);
u8 GetFunctionReturnType (IM3Function i_function, u32 i_index);
u32 GetFunctionNumArgsAndLocals (IM3Function i_function);

@ -135,7 +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")
d_m3ErrorConst (typeMismatch, "malformed Wasm: incorrect type on stack")
// runtime errors
d_m3ErrorConst (missingCompiledCode, "function is missing compiled m3 code")

Loading…
Cancel
Save