extensions
Steven Massey 5 years ago
parent ae705f0316
commit 6391a179e1

@ -85,61 +85,62 @@ typedef M3ImportInfo * IM3ImportInfo;
// -------------------------------------------------------------------------------------------------------------------------------
// error codes
// -------------------------------------------------------------------------------------------------------------------------------
# ifndef d_m3ErrorConst
# define d_m3ErrorConst(LABEL, STRING) extern const M3Result c_m3Err_##LABEL;
# define d_m3ErrorConst(LABEL, STRING) extern const M3Result c_m3Err_##LABEL;
# endif
// -------------------------------------------------------------------------------------------------------------------------------
// error codes
// -------------------------------------------------------------------------------------------------------------------------------
d_m3ErrorConst (none, NULL)
d_m3ErrorConst (none, NULL)
// general errors
d_m3ErrorConst (typeListOverflow, "type list count exceeds 32 types")
d_m3ErrorConst (mallocFailed, "memory allocation failed")
d_m3ErrorConst (typeListOverflow, "type list count exceeds 32 types")
d_m3ErrorConst (mallocFailed, "memory allocation failed")
// parse errors
d_m3ErrorConst (incompatibleWasmVersion, "incompatible WASM binary version")
d_m3ErrorConst (wasmMalformed, "malformed WASM binary")
d_m3ErrorConst (misorderedWasmSection, "out of ordr WASM section")
d_m3ErrorConst (wasmUnderrun, "underrun while parsing WASM binary")
d_m3ErrorConst (wasmOverrun, "overrun while parsing WASM binary")
d_m3ErrorConst (wasmMissingInitExpr, "missing init_expr in WASM binary")
d_m3ErrorConst (lebOverflow, "LEB encoded value overflow")
d_m3ErrorConst (missingUTF8, "zero length UTF-8 string")
d_m3ErrorConst (wasmSectionUnderrun, "section underrun while parsing WASM binary")
d_m3ErrorConst (wasmSectionOverrun, "section overrun while parsing WASM binary")
d_m3ErrorConst (invalidTypeId, "unknown value_type")
d_m3ErrorConst (incompatibleWasmVersion, "incompatible WASM binary version")
d_m3ErrorConst (wasmMalformed, "malformed WASM binary")
d_m3ErrorConst (misorderedWasmSection, "out of ordr WASM section")
d_m3ErrorConst (wasmUnderrun, "underrun while parsing WASM binary")
d_m3ErrorConst (wasmOverrun, "overrun while parsing WASM binary")
d_m3ErrorConst (wasmMissingInitExpr, "missing init_expr in WASM binary")
d_m3ErrorConst (lebOverflow, "LEB encoded value overflow")
d_m3ErrorConst (missingUTF8, "zero length UTF-8 string")
d_m3ErrorConst (wasmSectionUnderrun, "section underrun while parsing WASM binary")
d_m3ErrorConst (wasmSectionOverrun, "section overrun while parsing WASM binary")
d_m3ErrorConst (invalidTypeId, "unknown value_type")
// link errors
d_m3ErrorConst (moduleAlreadyLinked, "attempting to bind module to multiple runtimes")
d_m3ErrorConst (functionLookupFailed, "function lookup failed")
d_m3ErrorConst (functionImportMissing, "missing imported function");
d_m3ErrorConst (moduleAlreadyLinked, "attempting to bind module to multiple runtimes")
d_m3ErrorConst (functionLookupFailed, "function lookup failed")
d_m3ErrorConst (functionImportMissing, "missing imported function");
// compilation errors
d_m3ErrorConst (noCompiler, "no compiler found for opcode")
d_m3ErrorConst (unknownOpcode, "unknown opcode")
d_m3ErrorConst (functionStackOverflow, "compiling function overran its stack height limit")
d_m3ErrorConst (functionStackUnderrun, "compiling function underran the stack")
d_m3ErrorConst (mallocFailedCodePage, "memory allocation failed when acquiring a new M3 code page")
d_m3ErrorConst (optimizerFailed, "optimizer failed") // not a fatal error. a result,
d_m3ErrorConst (noCompiler, "no compiler found for opcode")
d_m3ErrorConst (unknownOpcode, "unknown opcode")
d_m3ErrorConst (functionStackOverflow, "compiling function overran its stack height limit")
d_m3ErrorConst (functionStackUnderrun, "compiling function underran the stack")
d_m3ErrorConst (mallocFailedCodePage, "memory allocation failed when acquiring a new M3 code page")
d_m3ErrorConst (optimizerFailed, "optimizer failed") // not a fatal error. a result,
// runtime errors
d_m3ErrorConst (missingCompiledCode, "function is missing compiled m3 code")
d_m3ErrorConst (wasmMemoryOverflow, "runtime ran out of memory")
d_m3ErrorConst (globalMemoryNotAllocated, "global memory is missing from a module")
d_m3ErrorConst (globaIndexOutOfBounds, "global index is too large")
d_m3ErrorConst (missingCompiledCode, "function is missing compiled m3 code")
d_m3ErrorConst (wasmMemoryOverflow, "runtime ran out of memory")
d_m3ErrorConst (globalMemoryNotAllocated, "global memory is missing from a module")
d_m3ErrorConst (globaIndexOutOfBounds, "global index is too large")
// traps
d_m3ErrorConst (trapTruncationOverflow, "[trap] truncation from floating point to integer overflow")
d_m3ErrorConst (trapOutOfBoundsMemoryAccess, "[trap] out of bounds memory access")
d_m3ErrorConst (trapDivisionByZero, "[trap] division by zero")
d_m3ErrorConst (trapRemainderByZero, "[trap] remainder by zero")
d_m3ErrorConst (trapTableIndexOutOfRange, "[trap] table index is out of range")
d_m3ErrorConst (trapExit, "[trap] program called exit")
d_m3ErrorConst (runtimeTrap, "[trap] unspecified runtime trap")
d_m3ErrorConst (trapTruncationOverflow, "[trap] truncation from floating point to integer overflow")
d_m3ErrorConst (trapOutOfBoundsMemoryAccess, "[trap] out of bounds memory access")
d_m3ErrorConst (trapDivisionByZero, "[trap] division by zero")
d_m3ErrorConst (trapRemainderByZero, "[trap] remainder by zero")
d_m3ErrorConst (trapTableIndexOutOfRange, "[trap] table index is out of range")
d_m3ErrorConst (trapExit, "[trap] program called exit")
d_m3ErrorConst (runtimeTrap, "[trap] unspecified runtime trap")
typedef void (* M3Free) (const void * i_data, void * i_ref);
@ -189,22 +190,22 @@ typedef int64_t (* M3Callback) (IM3Function i_currentFunction, void * i_ref);
M3Result m3_ParseModule (IM3Module * o_module,
const uint8_t * const i_wasmBytes,
uint32_t i_numWasmBytes
// M3Free i_releasHandler // i_ref argument type provided to M3Free() handler is <IM3Module>
// M3Free i_releaseHandler // i_ref argument type provided to M3Free() handler is <IM3Module>
);
// If i_wasmReleaseHandler is provided, then i_wasmBytes must be persistent until the handler is invoked.
// If the handler is NULL, ParseModule will make a copy of i_wasmBytes and releases ownership of the pointer.
// if a result is return, * o_module will be set to NULL
void m3_FreeModule (IM3Module i_module);
void m3_FreeModule (IM3Module i_module);
// Only unloaded modules need to be freed.
// M3Result m3_EnableOptimizer (IM3Module io_module, bool i_enable);
M3Result m3_LoadModule (IM3Runtime io_runtime, IM3Module io_module);
// LoadModule transfers ownership of a module to the runtime. Do not free modules once successfully imported into the runtime.
// LoadModule transfers ownership of a module to the runtime. Do not free modules once imported into the runtime.
M3Result m3_LinkFunction (IM3Module io_module,
const char * const i_functionName,

@ -992,7 +992,7 @@ _ (EmitOp (o, op));
EmitPointer (o, operand);
EmitOffset (o, execTop);
}
else result = ErrorCompile (c_m3Err_functionImportMissing, o, "missing function: '%s'", GetFunctionName (function));
else result = ErrorCompile (c_m3Err_functionImportMissing, o, "'%s'", GetFunctionName (function));
}
else result = c_m3Err_functionLookupFailed;

@ -12,6 +12,7 @@
#include "m3_exception.h"
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
void m3_printf (cstr_t i_format, const void * i_varArgs)
@ -151,6 +152,12 @@ i32 m3_fwrite (void * i_ptr, i32 i_size, i32 i_count, FILE * i_file)
}
i32 m3_write (i32 i_fd, const void * i_data, i32 i_count)
{
return (i32) write (i_fd, i_data, i_count);
}
M3Result EmbedHost (IM3Runtime i_runtime)
{
M3Result result = c_m3Err_none;
@ -221,7 +228,7 @@ M3Result SuppressLookupFailure (M3Result i_result)
return i_result;
}
#include <sys/ioctl.h>
M3Result m3_LinkCStd (IM3Module io_module)
{
M3Result result = c_m3Err_none;
@ -232,8 +239,13 @@ _ (SuppressLookupFailure (m3_LinkFunction (io_module, "_fopen", "i(M**)", (vo
_ (SuppressLookupFailure (m3_LinkFunction (io_module, "_fread", "i(*ii*)", (void *) m3_fread)));
_ (SuppressLookupFailure (m3_LinkFunction (io_module, "_fwrite", "i(*ii*)", (void *) m3_fwrite)));
_ (SuppressLookupFailure (m3_LinkFunction (io_module, "_write", "i(i*i)", (void *) m3_write)));
_ (SuppressLookupFailure (m3_LinkFunction (io_module, "_ioctl", "i(ii*)", (void *) ioctl)));
_ (SuppressLookupFailure (m3_LinkFunction (io_module, "_exit", "Tv(i)", (void *) m3_exit)));
_ (SuppressLookupFailure (m3_LinkFunction (io_module, "_perror", "v(*)", (void *) perror)));
_ (SuppressLookupFailure (m3_LinkFunction (io_module, "g$_stderr", "i(M)", (void *) m3_getStderr)));
catch: return result;

@ -25,7 +25,7 @@ M3Result ParseSection_Type (IM3Module io_module, bytes_t i_bytes, cbytes_t i_e
M3Result result = c_m3Err_none;
u32 numTypes;
_ (ReadLEB_u32 (& numTypes, & i_bytes, i_end)); m3log (parse, "** Type [%d]", numTypes);
_ (ReadLEB_u32 (& numTypes, & i_bytes, i_end)); m3log (parse, "** Type [%d]", numTypes);
if (numTypes)
{
@ -67,7 +67,7 @@ _ (ReadLEB_u7 /* u1 in spec */ (& returnCount, & i_bytes, i_end));
i8 returnType;
_ (ReadLEB_i7 (& returnType, & i_bytes, i_end));
_ (NormalizeType (& ft->returnType, returnType));
} m3logif (parse, PrintFuncTypeSignature (ft))
} m3logif (parse, PrintFuncTypeSignature (ft))
++ft;
}
@ -91,7 +91,7 @@ M3Result ParseSection_Function (IM3Module io_module, bytes_t i_bytes, cbytes_t
M3Result result = c_m3Err_none;
u32 numFunctions;
_ (ReadLEB_u32 (& numFunctions, & i_bytes, i_end)); m3log (parse, "** Function [%d]", numFunctions);
_ (ReadLEB_u32 (& numFunctions, & i_bytes, i_end)); m3log (parse, "** Function [%d]", numFunctions);
for (u32 i = 0; i < numFunctions; ++i)
{
@ -112,7 +112,7 @@ M3Result ParseSection_Import (IM3Module io_module, bytes_t i_bytes, cbytes_t i
M3ImportInfo import = {}, clearImport = {};
u32 numImports;
_ (ReadLEB_u32 (& numImports, & i_bytes, i_end)); m3log (parse, "** Import [%d]", numImports);
_ (ReadLEB_u32 (& numImports, & i_bytes, i_end)); m3log (parse, "** Import [%d]", numImports);
for (u32 i = 0; i < numImports; ++i)
{
@ -120,7 +120,7 @@ _ (ReadLEB_u32 (& numImports, & i_bytes, i_end)); m3log (parse, "** Impor
_ (Read_utf8 (& import.moduleUtf8, & i_bytes, i_end));
_ (Read_utf8 (& import.fieldUtf8, & i_bytes, i_end));
_ (Read_u8 (& importKind, & i_bytes, i_end)); m3log (parse, " - kind: %d; '%s.%s' ",
_ (Read_u8 (& importKind, & i_bytes, i_end)); m3log (parse, " - kind: %d; '%s.%s' ",
(u32) importKind, import.moduleUtf8, import.fieldUtf8);
switch (importKind)
{
@ -152,7 +152,7 @@ _ (ReadLEB_u32 (& pages, & i_bytes, i_end));
if (flag)
_ (ReadLEB_u32 (& maxPages, & i_bytes, i_end));
io_module->memory.virtualSize = pages * c_m3MemPageSize; m3log (parse, " memory: pages: %d max: %d", pages, maxPages);
io_module->memory.virtualSize = pages * c_m3MemPageSize; m3log (parse, " memory: pages: %d max: %d", pages, maxPages);
}
break;
@ -193,7 +193,7 @@ M3Result ParseSection_Export (IM3Module io_module, bytes_t i_bytes, cbytes_t i
M3Result result = c_m3Err_none;
u32 numExports;
_ (ReadLEB_u32 (& numExports, & i_bytes, i_end)); m3log (parse, "** Export [%d]", numExports);
_ (ReadLEB_u32 (& numExports, & i_bytes, i_end)); m3log (parse, "** Export [%d]", numExports);
for (u32 i = 0; i < numExports; ++i)
{
@ -203,7 +203,7 @@ _ (ReadLEB_u32 (& numExports, & i_bytes, i_end)); m3log (parse, "** Export
_ (Read_utf8 (& utf8, & i_bytes, i_end));
_ (Read_u8 (& exportKind, & i_bytes, i_end));
_ (ReadLEB_u32 (& index, & i_bytes, i_end)); m3log (parse, " - index: %4d; kind: %d; export: '%s'; ", index, (u32) exportKind, utf8);
_ (ReadLEB_u32 (& index, & i_bytes, i_end)); m3log (parse, " - index: %4d; kind: %d; export: '%s'; ", index, (u32) exportKind, utf8);
if (exportKind == c_externalKind_function)
{
@ -264,7 +264,7 @@ M3Result ParseSection_Code (M3Module * io_module, bytes_t i_bytes, cbytes_t i_
M3Result result;
u32 numFunctions;
_ (ReadLEB_u32 (& numFunctions, & i_bytes, i_end)); m3log (parse, "** Code [%d]", numFunctions);
_ (ReadLEB_u32 (& numFunctions, & i_bytes, i_end)); m3log (parse, "** Code [%d]", numFunctions);
if (numFunctions != io_module->numFunctions - io_module->numImports)
{
@ -287,7 +287,7 @@ _ (ReadLEB_u32 (& size, & i_bytes, i_end));
const u8 * start = ptr;
u32 numLocals;
_ (ReadLEB_u32 (& numLocals, & ptr, i_end)); m3log (parse, " - func size: %d; locals: %d", size, numLocals);
_ (ReadLEB_u32 (& numLocals, & ptr, i_end)); m3log (parse, " - func size: %d; locals: %d", size, numLocals);
u32 numLocalVars = 0;
@ -301,7 +301,7 @@ _ (ReadLEB_u32 (& varCount, & ptr, i_end));
_ (ReadLEB_i7 (& varType, & ptr, i_end));
_ (NormalizeType (& normalizedType, varType));
numLocalVars += varCount; m3log (parse, " - %d locals; type: '%s'", varCount, c_waTypes [-varType]);
numLocalVars += varCount; m3log (parse, " - %d locals; type: '%s'", varCount, c_waTypes [-varType]);
}
IM3Function func = Module_GetFunction (io_module, f + io_module->numImports);
@ -329,7 +329,7 @@ M3Result ParseSection_Data (M3Module * io_module, bytes_t i_bytes, cbytes_t i_
M3Result result = c_m3Err_none;
u32 numDataSegments;
_ (ReadLEB_u32 (& numDataSegments, & i_bytes, i_end)); m3log (parse, "** Data [%d]", numDataSegments);
_ (ReadLEB_u32 (& numDataSegments, & i_bytes, i_end)); m3log (parse, "** Data [%d]", numDataSegments);
_ (m3Alloc (& io_module->dataSegments, M3DataSegment, numDataSegments));
@ -350,7 +350,7 @@ _ (Parse_InitExpr (io_module, & i_bytes, i_end));
_ (ReadLEB_u32 (& segment->size, & i_bytes, i_end));
segment->data = i_bytes; m3log (parse, " segment [%u] memory: %u; expr-size: %d; size: %d",
segment->data = i_bytes; m3log (parse, " segment [%u] memory: %u; expr-size: %d; size: %d",
i, segment->memoryRegion, segment->initExprSize, segment->size);
}
@ -366,7 +366,7 @@ M3Result ParseSection_Global (M3Module * io_module, bytes_t i_bytes, cbytes_t
M3Result result = c_m3Err_none;
u32 numGlobals;
_ (ReadLEB_u32 (& numGlobals, & i_bytes, i_end)); m3log (parse, "** Global [%d]", numGlobals);
_ (ReadLEB_u32 (& numGlobals, & i_bytes, i_end)); m3log (parse, "** Global [%d]", numGlobals);
for (u32 i = 0; i < numGlobals; ++i)
{
@ -374,7 +374,7 @@ _ (ReadLEB_u32 (& numGlobals, & i_bytes, i_end)); m3log (parse, "** Globa
u8 type;
_ (ReadLEB_i7 (& waType, & i_bytes, i_end));
_ (NormalizeType (& type, waType)); m3log (parse, " - add global: [%d] %s", i, c_waTypes [type]);
_ (NormalizeType (& type, waType)); m3log (parse, " - add global: [%d] %s", i, c_waTypes [type]);
IM3Global global;
_ (Module_AddGlobal (io_module, & global, type, false /* mutable */, false /* isImport */));
@ -397,7 +397,7 @@ M3Result ParseSection_Custom (M3Module * io_module, bytes_t i_bytes, cbytes_t
cstr_t name;
_ (Read_utf8 (& name, & i_bytes, i_end));
m3log (parse, "** Custom: '%s'", name);
m3log (parse, "** Custom: '%s'", name);
if (strcmp (name, "name") != 0)
i_bytes = i_end;
@ -426,7 +426,7 @@ _ (Read_utf8 (& name, & i_bytes, i_end));
{
if (not io_module->functions [index].name)
{
io_module->functions [index].name = name; m3log (parse, "naming function [%d]: %s", index, name);
io_module->functions [index].name = name; m3log (parse, "naming function [%d]: %s", index, name);
name = NULL;
}
// else m3log (parse, "prenamed: %s", io_module->functions [index].name);

@ -94,6 +94,12 @@ int main (int i_argc, const char * i_argv [])
// result = m3_Call (main);
if (i_argc)
{
--i_argc;
++i_argv;
}
result = m3_CallWithArgs (main, i_argc, i_argv);
clock_t end = clock ();
@ -111,13 +117,15 @@ int main (int i_argc, const char * i_argv [])
if (result)
{
printf ("error: %s\n", result);
printf ("result: %s", result);
if (env)
{
M3ErrorInfo info = m3_GetErrorInfo (env);
printf ("%s\n", info.message);
printf (" (%s)", info.message);
}
printf ("\n");
}
m3_FreeRuntime (env);

Loading…
Cancel
Save