Skip to content

Commit

Permalink
Update Wasm3
Browse files Browse the repository at this point in the history
  • Loading branch information
vshymanskyy committed Jun 2, 2021
1 parent f27a974 commit f40e32a
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 74 deletions.
73 changes: 42 additions & 31 deletions src/m3_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ M3Result AllocateSlots (IM3Compilation o, u16 * o_slot, u8 i_type)

M3Result AllocateConstantSlots (IM3Compilation o, u16 * o_slot, u8 i_type)
{
return AllocateSlotsWithinRange (o, o_slot, i_type, o->slotFirstConstIndex, o->slotFirstDynamicIndex);
u16 maxTableIndex = o->slotFirstConstIndex + d_m3MaxConstantTableSize;
return AllocateSlotsWithinRange (o, o_slot, i_type, o->slotFirstConstIndex, M3_MIN(o->slotFirstDynamicIndex, maxTableIndex));
}


Expand Down Expand Up @@ -1116,10 +1117,10 @@ _ (Read_u8 (& opcode, & o->wasm, o->wasmEnd)); m3log (compile, d_i

//printf("Extended opcode: 0x%x\n", i_opcode);

const M3OpInfo* opinfo = GetOpInfo (i_opcode);
_throwif (m3Err_unknownOpcode, not opinfo);
IM3OpInfo opInfo = GetOpInfo (i_opcode);
_throwif (m3Err_unknownOpcode, not opInfo);

M3Compiler compiler = opinfo->compiler;
M3Compiler compiler = opInfo->compiler;
_throwif (m3Err_noCompiler, not compiler);

_ ((* compiler) (o, i_opcode));
Expand Down Expand Up @@ -2011,74 +2012,75 @@ M3Result Compile_Operator (IM3Compilation o, m3opcode_t i_opcode)
{
M3Result result;

const M3OpInfo * op = GetOpInfo(i_opcode);
IM3OpInfo opInfo = GetOpInfo (i_opcode);
_throwif (m3Err_unknownOpcode, not opInfo);

IM3Operation operation;
IM3Operation op;

// This preserve is for for FP compare operations.
// either need additional slot destination operations or the
// easy fix, move _r0 out of the way.
// moving out the way might be the optimal solution most often?
// otherwise, the _r0 reg can get buried down in the stack
// and be idle & wasted for a moment.
if (IsFpType (GetStackTopType (o)) and IsIntType (op->type))
if (IsFpType (GetStackTopType (o)) and IsIntType (opInfo->type))
{
_ (PreserveRegisterIfOccupied (o, op->type));
_ (PreserveRegisterIfOccupied (o, opInfo->type));
}

if (op->stackOffset == 0)
if (opInfo->stackOffset == 0)
{
if (IsStackTopInRegister (o))
{
operation = op->operations [0]; // _s
op = opInfo->operations [0]; // _s
}
else
{
_ (PreserveRegisterIfOccupied (o, op->type));
operation = op->operations [1]; // _r
_ (PreserveRegisterIfOccupied (o, opInfo->type));
op = opInfo->operations [1]; // _r
}
}
else
{
if (IsStackTopInRegister (o))
{
operation = op->operations [0]; // _rs
op = opInfo->operations [0]; // _rs

if (IsStackTopMinus1InRegister (o))
{ d_m3Assert (i_opcode == 0x38 or i_opcode == 0x39);
operation = op->operations [3]; // _rr for fp.store
op = opInfo->operations [3]; // _rr for fp.store
}
}
else if (IsStackTopMinus1InRegister (o))
{
operation = op->operations [1]; // _sr
op = opInfo->operations [1]; // _sr

if (not operation) // must be commutative, then
operation = op->operations [0];
if (not op) // must be commutative, then
op = opInfo->operations [0];
}
else
{
_ (PreserveRegisterIfOccupied (o, op->type)); // _ss
operation = op->operations [2];
_ (PreserveRegisterIfOccupied (o, opInfo->type)); // _ss
op = opInfo->operations [2];
}
}

if (operation)
if (op)
{
_ (EmitOp (o, operation));
_ (EmitOp (o, op));

_ (EmitSlotNumOfStackTopAndPop (o));

if (op->stackOffset < 0)
if (opInfo->stackOffset < 0)
_ (EmitSlotNumOfStackTopAndPop (o));

if (op->type != c_m3Type_none)
_ (PushRegister (o, op->type));
if (opInfo->type != c_m3Type_none)
_ (PushRegister (o, opInfo->type));
}
else
{
# ifdef DEBUG
result = ErrorCompile ("no operation found for opcode", o, "'%s'", op->name);
result = ErrorCompile ("no operation found for opcode", o, "'%s'", opInfo->name);
# else
result = ErrorCompile ("no operation found for opcode", o, "%x", i_opcode);
# endif
Expand All @@ -2093,7 +2095,9 @@ M3Result Compile_Convert (IM3Compilation o, m3opcode_t i_opcode)
{
M3Result result = m3Err_none;

const M3OpInfo * opInfo = GetOpInfo(i_opcode);
_try {
IM3OpInfo opInfo = GetOpInfo (i_opcode);
_throwif (m3Err_unknownOpcode, not opInfo);

bool destInSlot = IsRegisterTypeAllocated (o, opInfo->type);
bool sourceInSlot = IsStackTopInSlot (o);
Expand All @@ -2108,6 +2112,7 @@ _ (PushAllocatedSlotAndEmit (o, opInfo->type))
else
_ (PushRegister (o, opInfo->type))

}
_catch: return result;
}

Expand All @@ -2122,9 +2127,10 @@ _try {
_ (ReadLEB_u32 (& alignHint, & o->wasm, o->wasmEnd));
_ (ReadLEB_u32 (& memoryOffset, & o->wasm, o->wasmEnd));
m3log (compile, d_indent " (offset = %d)", get_indention_string (o), memoryOffset);
const M3OpInfo * op = GetOpInfo(i_opcode);
IM3OpInfo opInfo = GetOpInfo (i_opcode);
_throwif (m3Err_unknownOpcode, not opInfo);

if (IsFpType (op->type))
if (IsFpType (opInfo->type))
_ (PreserveRegisterIfOccupied (o, c_m3Type_f64));

_ (Compile_Operator (o, i_opcode));
Expand Down Expand Up @@ -2439,7 +2445,8 @@ const M3OpInfo c_operationsFC [] =
# endif
};

const M3OpInfo* GetOpInfo (m3opcode_t opcode)

IM3OpInfo GetOpInfo (m3opcode_t opcode)
{
switch (opcode >> 8) {
case 0x00:
Expand Down Expand Up @@ -2480,7 +2487,7 @@ _ (Read_opcode (& opcode, & o->wasm, o->wasmEnd)); log_opco
}
}

IM3OpInfo opinfo = GetOpInfo(opcode);
IM3OpInfo opinfo = GetOpInfo (opcode);

if (opinfo == NULL)
_throw (ErrorCompile (m3Err_unknownOpcode, o, "opcode '%x' not available", opcode));
Expand Down Expand Up @@ -2648,6 +2655,7 @@ M3Result CompileLocals (IM3Compilation o)
{
M3Result result;

u32 numLocals = 0;
u32 numLocalBlocks;
_ (ReadLEB_u32 (& numLocalBlocks, & o->wasm, o->wasmEnd));

Expand All @@ -2660,11 +2668,14 @@ _ (ReadLEB_u32 (& numLocalBlocks, & o->wasm, o->wasmEnd));
_ (ReadLEB_u32 (& varCount, & o->wasm, o->wasmEnd));
_ (ReadLEB_i7 (& waType, & o->wasm, o->wasmEnd));
_ (NormalizeType (& localType, waType));
m3log (compile, "pushing locals. count: %d; type: %s", varCount, c_waTypes [localType]);
numLocals += varCount; m3log (compile, "pushing locals. count: %d; type: %s", varCount, c_waTypes [localType]);
while (varCount--)
_ (PushAllocatedSlot (o, localType));
}

if (o->function)
o->function->numLocals = numLocals;

_catch: return result;
}

Expand Down
2 changes: 1 addition & 1 deletion src/m3_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ M3OpInfo;

typedef const M3OpInfo * IM3OpInfo;

extern const M3OpInfo* GetOpInfo(m3opcode_t opcode);
IM3OpInfo GetOpInfo (m3opcode_t opcode);

// TODO: This helper should be removed, when MultiValue is implemented
static inline
Expand Down
2 changes: 1 addition & 1 deletion src/m3_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# endif

# ifndef d_m3MaxConstantTableSize
# define d_m3MaxConstantTableSize 1024
# define d_m3MaxConstantTableSize 120
# endif

# ifndef d_m3MaxDuplicateFunctionImpl
Expand Down
2 changes: 1 addition & 1 deletion src/m3_config_platforms.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
# define M3_NO_UBSAN
# else
# define M3_WEAK __attribute__((weak))
# define M3_NO_UBSAN __attribute__((no_sanitize("undefined")))
# define M3_NO_UBSAN //__attribute__((no_sanitize("undefined")))
# endif

# ifndef M3_MIN
Expand Down
2 changes: 1 addition & 1 deletion src/m3_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ M3Result ReadLEB_i64 (i64 * o_value, bytes_t * io_bytes, cbytes_t
M3Result Read_utf8 (cstr_t * o_utf8, bytes_t * io_bytes, cbytes_t i_end);

cstr_t SPrintValue (void * i_value, u8 i_type);
size_t SPrintArg (char * o_string, size_t i_stringBufferSize, m3stack_t i_sp, u8 i_type);
size_t SPrintArg (char * o_string, size_t i_stringBufferSize, voidptr_t i_sp, u8 i_type);

void ReportError (IM3Runtime io_runtime, IM3Module i_module, IM3Function i_function, ccstr_t i_errorMessage, ccstr_t i_file, u32 i_lineNum);

Expand Down
66 changes: 44 additions & 22 deletions src/m3_env.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ void Environment_AddFuncType (IM3Environment i_environment, IM3FuncType * io_f
* io_funcType = newType;
}


IM3CodePage RemoveCodePageOfCapacity (M3CodePage ** io_list, u32 i_minimumLineCount)
{
IM3CodePage prev = NULL;
Expand Down Expand Up @@ -194,6 +195,7 @@ void * m3_GetUserData (IM3Runtime i_runtime)
return i_runtime ? i_runtime->userdata : NULL;
}


void * ForEachModule (IM3Runtime i_runtime, ModuleVisitor i_visitor, void * i_info)
{
void * r = NULL;
Expand Down Expand Up @@ -444,6 +446,8 @@ M3Result InitDataSegments (M3Memory * io_memory, IM3Module io_module)
{
M3Result result = m3Err_none;

_throwif ("unallocated linear memory", !(io_memory->mallocated));

for (u32 i = 0; i < io_module->numDataSegments; ++i)
{
M3DataSegment * segment = & io_module->dataSegments [i];
Expand All @@ -454,8 +458,6 @@ _ (EvaluateExpression (io_module, & segmentOffset, c_m3Type_i32, & start,

m3log (runtime, "loading data segment: %d; size: %d; offset: %d", i, segment->size, segmentOffset);

_throwif ("unallocated linear memory", !(io_memory->mallocated));

if (segmentOffset >= 0 && (size_t)(segmentOffset) + segment->size <= io_memory->mallocated->length)
{
u8 * dest = m3MemData (io_memory->mallocated) + segmentOffset;
Expand Down Expand Up @@ -490,14 +492,18 @@ _ (EvaluateExpression (io_module, & offset, c_m3Type_i32, & bytes, end
u32 numElements;
_ (ReadLEB_u32 (& numElements, & bytes, end));

size_t endElement = (size_t)(numElements) + offset;
size_t endElement = (size_t) numElements + offset;
_throwif ("table overflow", endElement > d_m3MaxSaneTableSize);

io_module->table0 = m3_ReallocArray (IM3Function, io_module->table0, endElement, io_module->table0Size);
// is there any requirement that elements must be in increasing sequence?
// make sure the table isn't shrunk.
if (endElement > io_module->table0Size)
{
io_module->table0 = m3_ReallocArray (IM3Function, io_module->table0, endElement, io_module->table0Size);
io_module->table0Size = (u32) endElement;
}
_throwifnull(io_module->table0);

io_module->table0Size = (u32) endElement;

for (u32 e = 0; e < numElements; ++e)
{
u32 functionIndex;
Expand All @@ -513,6 +519,22 @@ _ (ReadLEB_u32 (& functionIndex, & bytes, end));
_catch: return result;
}

M3Result m3_CompileModule (IM3Module io_module)
{
M3Result result = m3Err_none;

for (u32 i = 0; i < io_module->numFunctions; ++i)
{
IM3Function f = & io_module->functions [i];
if (f->wasm and not f->compiled)
{
_ (CompileFunction (f));
}
}

_catch: return result;
}

M3Result m3_RunStart (IM3Module io_module)
{
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
Expand Down Expand Up @@ -779,6 +801,16 @@ M3Result m3_CallV (IM3Function i_function, ...)
return r;
}

static
void ReportNativeStackUsage ()
{
# if d_m3LogNativeStack
int stackUsed = m3StackGetMax();
fprintf (stderr, "Native stack used: %d\n", stackUsed);
# endif
}


M3Result m3_CallVL (IM3Function i_function, va_list i_args)
{
IM3Runtime runtime = i_function->module->runtime;
Expand Down Expand Up @@ -808,14 +840,10 @@ M3Result m3_CallVL (IM3Function i_function, va_list i_args)
}
m3StackCheckInit();
M3Result r = (M3Result) Call (i_function->compiled, (m3stack_t)(runtime->stack), runtime->memory.mallocated, d_m3OpDefaultArgs);
ReportNativeStackUsage ();

runtime->lastCalled = r ? NULL : i_function;

#if d_m3LogNativeStack
int stackUsed = m3StackGetMax();
fprintf (stderr, "Native stack used: %d\n", stackUsed);
#endif

return r;
}

Expand Down Expand Up @@ -852,13 +880,10 @@ M3Result m3_Call (IM3Function i_function, uint32_t i_argc, const void * i_argp

m3StackCheckInit();
M3Result r = (M3Result) Call (i_function->compiled, (m3stack_t)(runtime->stack), runtime->memory.mallocated, d_m3OpDefaultArgs);
ReportNativeStackUsage ();

runtime->lastCalled = r ? NULL : i_function;

#if d_m3LogNativeStack
int stackUsed = m3StackGetMax();
fprintf (stderr, "Native stack used: %d\n", stackUsed);
#endif

return r;
}
Expand Down Expand Up @@ -896,14 +921,10 @@ M3Result m3_CallArgv (IM3Function i_function, uint32_t i_argc, const char * i_

m3StackCheckInit();
M3Result r = (M3Result) Call (i_function->compiled, (m3stack_t)(runtime->stack), runtime->memory.mallocated, d_m3OpDefaultArgs);
ReportNativeStackUsage ();

runtime->lastCalled = r ? NULL : i_function;

#if d_m3LogNativeStack
int stackUsed = m3StackGetMax();
fprintf (stderr, "Native stack used: %d\n", stackUsed);
#endif

return r;
}

Expand Down Expand Up @@ -1090,8 +1111,7 @@ void m3_ResetErrorInfo (IM3Runtime i_runtime)

uint8_t * m3_GetMemory (IM3Runtime i_runtime, uint32_t * o_memorySizeInBytes, uint32_t i_memoryIndex)
{
uint8_t * memory = NULL;
d_m3Assert (i_memoryIndex == 0);
uint8_t * memory = NULL; d_m3Assert (i_memoryIndex == 0);

if (i_runtime)
{
Expand All @@ -1107,11 +1127,13 @@ uint8_t * m3_GetMemory (IM3Runtime i_runtime, uint32_t * o_memorySizeInBytes,
return memory;
}


uint32_t m3_GetMemorySize (IM3Runtime i_runtime)
{
return i_runtime->memory.mallocated->length;
}


M3BacktraceInfo * m3_GetBacktrace (IM3Runtime i_runtime)
{
# if d_m3RecordBacktraces
Expand Down
Loading

0 comments on commit f40e32a

Please sign in to comment.