tabs->spaces

extensions
Steven Massey 3 years ago
parent a1c75493ad
commit 2fa4a11556

@ -14,97 +14,97 @@
IM3Module m3_NewModule (IM3Environment i_environment) IM3Module m3_NewModule (IM3Environment i_environment)
{ {
IM3Module module = m3_AllocStruct (M3Module); IM3Module module = m3_AllocStruct (M3Module);
if (module) if (module)
{ {
module->name = ".unnamed"; module->name = ".unnamed";
module->startFunction = -1; module->startFunction = -1;
module->environment = i_environment; module->environment = i_environment;
module->wasmStart = NULL; module->wasmStart = NULL;
module->wasmEnd = NULL; module->wasmEnd = NULL;
} }
return module; return module;
} }
M3Result m3_InjectFunction (IM3Module i_module, M3Result m3_InjectFunction (IM3Module i_module,
int32_t * io_functionIndex, int32_t * io_functionIndex,
const char * const i_signature, const char * const i_signature,
const uint8_t * const i_wasmBytes, const uint8_t * const i_wasmBytes,
bool i_doCompilation) bool i_doCompilation)
{ {
M3Result result = m3Err_none; d_m3Assert (io_functionIndex); M3Result result = m3Err_none; d_m3Assert (io_functionIndex);
IM3Function function = NULL; IM3Function function = NULL;
IM3FuncType ftype = NULL; IM3FuncType ftype = NULL;
_ (SignatureToFuncType (& ftype, i_signature)); _ (SignatureToFuncType (& ftype, i_signature));
i32 index = * io_functionIndex; i32 index = * io_functionIndex;
bytes_t bytes = i_wasmBytes; bytes_t bytes = i_wasmBytes;
bytes_t end = i_wasmBytes + 5; bytes_t end = i_wasmBytes + 5;
u32 size; u32 size;
_ (ReadLEB_u32 (& size, & bytes, end)); _ (ReadLEB_u32 (& size, & bytes, end));
end = bytes + size; end = bytes + size;
if (index >= 0) if (index >= 0)
{ {
_throwif ("function index out of bounds", index >= i_module->numFunctions); _throwif ("function index out of bounds", index >= i_module->numFunctions);
function = & i_module->functions [index]; function = & i_module->functions [index];
if (not AreFuncTypesEqual (ftype, function->funcType)) if (not AreFuncTypesEqual (ftype, function->funcType))
_throw ("function type mismatch"); _throw ("function type mismatch");
} }
else else
{ {
// add slot to function type table in the module // add slot to function type table in the module
u32 funcTypeIndex = i_module->numFuncTypes++; u32 funcTypeIndex = i_module->numFuncTypes++;
i_module->funcTypes = m3_ReallocArray (IM3FuncType, i_module->funcTypes, i_module->numFuncTypes, funcTypeIndex); i_module->funcTypes = m3_ReallocArray (IM3FuncType, i_module->funcTypes, i_module->numFuncTypes, funcTypeIndex);
_throwifnull (i_module->funcTypes); _throwifnull (i_module->funcTypes);
// add functype object to the environment // add functype object to the environment
Environment_AddFuncType (i_module->environment, & ftype); Environment_AddFuncType (i_module->environment, & ftype);
i_module->funcTypes [funcTypeIndex] = ftype; i_module->funcTypes [funcTypeIndex] = ftype;
ftype = NULL; // prevent freeing below ftype = NULL; // prevent freeing below
index = (i32) i_module->numFunctions; index = (i32) i_module->numFunctions;
_ (Module_AddFunction (i_module, funcTypeIndex, NULL)); _ (Module_AddFunction (i_module, funcTypeIndex, NULL));
function = Module_GetFunction (i_module, index); function = Module_GetFunction (i_module, index);
* io_functionIndex = index; * io_functionIndex = index;
} }
function->compiled = NULL; function->compiled = NULL;
if (function->ownsWasmCode) if (function->ownsWasmCode)
m3_Free (function->wasm); m3_Free (function->wasm);
size_t numBytes = end - i_wasmBytes; size_t numBytes = end - i_wasmBytes;
function->wasm = m3_CopyMem (i_wasmBytes, numBytes); function->wasm = m3_CopyMem (i_wasmBytes, numBytes);
_throwifnull (function->wasm); _throwifnull (function->wasm);
function->wasmEnd = function->wasm + numBytes; function->wasmEnd = function->wasm + numBytes;
function->ownsWasmCode = true; function->ownsWasmCode = true;
if (i_doCompilation and not i_module->runtime) if (i_doCompilation and not i_module->runtime)
_throw ("module must be loaded into runtime to compile function"); _throw ("module must be loaded into runtime to compile function");
_ (Compile_Function (function)); _ (Compile_Function (function));
_catch: _catch:
m3_Free (ftype); m3_Free (ftype);
return result; return result;
} }
IM3Function m3_GetFunctionByIndex (IM3Module i_module, uint32_t i_index) IM3Function m3_GetFunctionByIndex (IM3Module i_module, uint32_t i_index)
{ {
return Module_GetFunction (i_module, i_index); return Module_GetFunction (i_module, i_index);
} }

@ -22,23 +22,23 @@ extern "C" {
// API extensions // API extensions
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
// Creates an empty module. // Creates an empty module.
IM3Module m3_NewModule (IM3Environment i_environment); IM3Module m3_NewModule (IM3Environment i_environment);
// To append a new function, set io_functionIndex to negative. On return, the new function index will be set. // To append a new function, set io_functionIndex to negative. On return, the new function index will be set.
// To overwrite an existing function, set io_functionIndex to the desired element. i_signature must match the existing // To overwrite an existing function, set io_functionIndex to the desired element. i_signature must match the existing
// function signature. // function signature.
// ** InjectFunction invalidates any existing IM3Function pointers // ** InjectFunction invalidates any existing IM3Function pointers
M3Result m3_InjectFunction (IM3Module i_module, M3Result m3_InjectFunction (IM3Module i_module,
int32_t * io_functionIndex, int32_t * io_functionIndex,
const char * const i_signature, const char * const i_signature,
const uint8_t * const i_wasmBytes, // i_wasmBytes is copied const uint8_t * const i_wasmBytes, // i_wasmBytes is copied
bool i_doCompilation); bool i_doCompilation);
IM3Function m3_GetFunctionByIndex (IM3Module i_module, IM3Function m3_GetFunctionByIndex (IM3Module i_module,
uint32_t i_index); uint32_t i_index);
#if defined(__cplusplus) #if defined(__cplusplus)
} }

@ -30,7 +30,7 @@ M3Result SignatureToFuncType (IM3FuncType * o_functionType, ccstr_t i_signatur
M3Result result = m3Err_none; M3Result result = m3Err_none;
IM3FuncType funcType = NULL; IM3FuncType funcType = NULL;
_try { _try {
if (not o_functionType) if (not o_functionType)
_throw ("null function type"); _throw ("null function type");

@ -2341,9 +2341,9 @@ M3Result Compile_Function (IM3Function io_function)
{ {
IM3FuncType ft = io_function->funcType; IM3FuncType ft = io_function->funcType;
M3Result result = m3Err_none; m3log (compile, "compiling: '%s'; wasm-size: %d; numArgs: %d; return: %s", M3Result result = m3Err_none; m3log (compile, "compiling: '%s'; wasm-size: %d; numArgs: %d; return: %s",
m3_GetFunctionName(io_function), (u32) (io_function->wasmEnd - io_function->wasm), GetFunctionNumArgs (io_function), m3_GetFunctionName(io_function), (u32) (io_function->wasmEnd - io_function->wasm), GetFunctionNumArgs (io_function),
c_waTypes [GetSingleRetType(ft)]); c_waTypes [GetSingleRetType(ft)]);
IM3Runtime runtime = io_function->module->runtime; IM3Runtime runtime = io_function->module->runtime;
IM3Compilation o = & runtime->compilation; IM3Compilation o = & runtime->compilation;
@ -2356,10 +2356,10 @@ M3Result Compile_Function (IM3Function io_function)
o->wasmEnd = io_function->wasmEnd; o->wasmEnd = io_function->wasmEnd;
_try { _try {
// skip over code size. the end was already calculated during parse phase // skip over code size. the end was already calculated during parse phase
u32 size; u32 size;
_ (ReadLEB_u32 (& size, & o->wasm, o->wasmEnd)); d_m3Assert (size == (o->wasmEnd - o->wasm)) _ (ReadLEB_u32 (& size, & o->wasm, o->wasmEnd)); d_m3Assert (size == (o->wasmEnd - o->wasm))
_ (AcquireCompilationCodePage (o, & o->page)); _ (AcquireCompilationCodePage (o, & o->page));
pc_t pc = GetPagePC (o->page); pc_t pc = GetPagePC (o->page);

@ -177,38 +177,38 @@ void FreeImportInfo (M3ImportInfo * i_info)
IM3Environment m3_NewEnvironment () IM3Environment m3_NewEnvironment ()
{ {
M3Result result = m3Err_none; M3Result result = m3Err_none;
IM3Environment env = m3_AllocStruct (M3Environment); IM3Environment env = m3_AllocStruct (M3Environment);
if (env) if (env)
{ {
_try _try
{ {
// create FuncTypes for all simple block return ValueTypes // create FuncTypes for all simple block return ValueTypes
for (u8 t = c_m3Type_none; t <= c_m3Type_f64; t++) for (u8 t = c_m3Type_none; t <= c_m3Type_f64; t++)
{ {
IM3FuncType ftype; IM3FuncType ftype;
_ (AllocFuncType (& ftype, 1)); _ (AllocFuncType (& ftype, 1));
ftype->numArgs = 0; ftype->numArgs = 0;
ftype->numRets = (t == c_m3Type_none) ? 0 : 1; ftype->numRets = (t == c_m3Type_none) ? 0 : 1;
ftype->types [0] = t; ftype->types [0] = t;
Environment_AddFuncType (env, & ftype); Environment_AddFuncType (env, & ftype);
d_m3Assert (t < 5); d_m3Assert (t < 5);
env->retFuncTypes [t] = ftype; env->retFuncTypes [t] = ftype;
} }
} }
_catch: _catch:
if (result) if (result)
{ {
m3_FreeEnvironment (env); m3_FreeEnvironment (env);
env = NULL; env = NULL;
} }
} }
return env; return env;
} }
@ -224,7 +224,7 @@ void Environment_Release (IM3Environment i_environment)
m3_Free (ftype); m3_Free (ftype);
ftype = next; ftype = next;
} }
m3log (runtime, "freeing %d pages from environment", CountCodePages (i_environment->pagesReleased)); m3log (runtime, "freeing %d pages from environment", CountCodePages (i_environment->pagesReleased));
FreeCodePages (& i_environment->pagesReleased); FreeCodePages (& i_environment->pagesReleased);
} }

@ -149,10 +149,10 @@ typedef struct M3Module
cstr_t name; cstr_t name;
u32 numFuncTypes; u32 numFuncTypes;
IM3FuncType * funcTypes; // array of pointers to list of FuncTypes IM3FuncType * funcTypes; // array of pointers to list of FuncTypes
u32 numImports; u32 numImports;
//IM3Function * imports; b // notice: "I" prefix. imports are pointers to functions in another module. //IM3Function * imports; b // notice: "I" prefix. imports are pointers to functions in another module.
u32 numFunctions; u32 numFunctions;
M3Function * functions; M3Function * functions;
@ -193,10 +193,10 @@ typedef struct M3Environment
{ {
// struct M3Runtime * runtimes; // struct M3Runtime * runtimes;
IM3FuncType funcTypes; // linked list IM3FuncType funcTypes; // linked list
IM3FuncType retFuncTypes [c_m3Type_unknown]; // these 'point' to elements in the linked list above. IM3FuncType retFuncTypes [c_m3Type_unknown]; // these 'point' to elements in the linked list above.
// the number of elements must match the basic types as per M3ValueType // the number of elements must match the basic types as per M3ValueType
M3CodePage * pagesReleased; M3CodePage * pagesReleased;
} }
M3Environment; M3Environment;

@ -66,7 +66,7 @@ _try {
M3Result Module_AddFunction (IM3Module io_module, u32 i_typeIndex, IM3ImportInfo i_importInfo) M3Result Module_AddFunction (IM3Module io_module, u32 i_typeIndex, IM3ImportInfo i_importInfo)
{ {
M3Result result = m3Err_none; M3Result result = m3Err_none;
_try { _try {
u32 index = io_module->numFunctions++; u32 index = io_module->numFunctions++;
io_module->functions = m3_ReallocArray (M3Function, io_module->functions, io_module->numFunctions, index); io_module->functions = m3_ReallocArray (M3Function, io_module->functions, io_module->numFunctions, index);
@ -87,8 +87,8 @@ _try {
func->numNames = 1; func->numNames = 1;
func->names[0] = i_importInfo->fieldUtf8; func->names[0] = i_importInfo->fieldUtf8;
} }
m3log (module, " added function: %3d; sig: %d", index, i_typeIndex); m3log (module, " added function: %3d; sig: %d", index, i_typeIndex);
} _catch: } _catch:
return result; return result;
@ -100,10 +100,10 @@ IM3Function Module_GetFunction (IM3Module i_module, u32 i_functionIndex)
IM3Function func = NULL; IM3Function func = NULL;
if (i_functionIndex < i_module->numFunctions) if (i_functionIndex < i_module->numFunctions)
{ {
func = & i_module->functions [i_functionIndex]; func = & i_module->functions [i_functionIndex];
func->module = i_module; func->module = i_module;
} }
return func; return func;
} }

@ -314,8 +314,8 @@ _ (ReadLEB_u32 (& numFunctions, & i_bytes, i_end));
for (u32 f = 0; f < numFunctions; ++f) for (u32 f = 0; f < numFunctions; ++f)
{ {
const u8 * start = i_bytes; const u8 * start = i_bytes;
u32 size; u32 size;
_ (ReadLEB_u32 (& size, & i_bytes, i_end)); _ (ReadLEB_u32 (& size, & i_bytes, i_end));
@ -348,7 +348,7 @@ _ (NormalizeType (& normalType, wasmType));
func->module = io_module; func->module = io_module;
func->wasm = start; func->wasm = start;
func->wasmEnd = i_bytes; func->wasmEnd = i_bytes;
//func->ownsWasmCode = io_module->hasWasmCodeCopy; //func->ownsWasmCode = io_module->hasWasmCodeCopy;
func->numLocals = numLocals; func->numLocals = numLocals;
} }

@ -178,7 +178,7 @@ d_m3ErrorConst (trapStackOverflow, "[trap] stack overflow")
void m3_FreeRuntime (IM3Runtime i_runtime); void m3_FreeRuntime (IM3Runtime i_runtime);
// Wasm currently only supports one memory region. i_memoryIndex should be zero. // Wasm currently only supports one memory region. i_memoryIndex should be zero.
uint8_t * m3_GetMemory (IM3Runtime i_runtime, uint8_t * m3_GetMemory (IM3Runtime i_runtime,
uint32_t * o_memorySizeInBytes, uint32_t * o_memorySizeInBytes,
uint32_t i_memoryIndex); uint32_t i_memoryIndex);
@ -190,19 +190,19 @@ d_m3ErrorConst (trapStackOverflow, "[trap] stack overflow")
// modules // modules
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
// i_wasmBytes data must be persistent during the lifetime of the module // i_wasmBytes data must be persistent during the lifetime of the module
M3Result m3_ParseModule (IM3Environment i_environment, M3Result m3_ParseModule (IM3Environment i_environment,
IM3Module * o_module, IM3Module * o_module,
const uint8_t * const i_wasmBytes, const uint8_t * const i_wasmBytes,
uint32_t i_numWasmBytes); uint32_t i_numWasmBytes);
// Only unloaded modules need to be freed // Only unloaded modules need to be freed
void m3_FreeModule (IM3Module i_module); void m3_FreeModule (IM3Module i_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 successfully imported into the runtime
M3Result m3_LoadModule (IM3Runtime io_runtime, IM3Module io_module); M3Result m3_LoadModule (IM3Runtime io_runtime, IM3Module io_module);
// Calling m3_RunStart is optional // Calling m3_RunStart is optional
M3Result m3_RunStart (IM3Module i_module); M3Result m3_RunStart (IM3Module i_module);
typedef const void * (* M3RawCall) (IM3Runtime runtime, IM3ImportContext _ctx, uint64_t * _sp, void * _mem); typedef const void * (* M3RawCall) (IM3Runtime runtime, IM3ImportContext _ctx, uint64_t * _sp, void * _mem);
@ -228,7 +228,7 @@ d_m3ErrorConst (trapStackOverflow, "[trap] stack overflow")
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
M3Result m3_Yield (void); M3Result m3_Yield (void);
// o_function is valid during the lifetime of the originating runtime // o_function is valid during the lifetime of the originating runtime
M3Result m3_FindFunction (IM3Function * o_function, M3Result m3_FindFunction (IM3Function * o_function,
IM3Runtime i_runtime, IM3Runtime i_runtime,
const char * const i_functionName); const char * const i_functionName);
@ -262,7 +262,7 @@ d_m3ErrorConst (trapStackOverflow, "[trap] stack overflow")
void m3_PrintM3Info (void); void m3_PrintM3Info (void);
void m3_PrintProfilerInfo (void); void m3_PrintProfilerInfo (void);
// The runtime owns the backtrace, do not free the backtrace you obtain. Returns NULL if there's no backtrace. // The runtime owns the backtrace, do not free the backtrace you obtain. Returns NULL if there's no backtrace.
IM3BacktraceInfo m3_GetBacktrace (IM3Runtime i_runtime); IM3BacktraceInfo m3_GetBacktrace (IM3Runtime i_runtime);
#if defined(__cplusplus) #if defined(__cplusplus)

@ -75,7 +75,7 @@ int main (int i_argc, const char * i_argv [])
} }
DisabledTest (codepages.b) DisabledTest (codepages.b)
{ {
const u32 c_numPages = 2000; const u32 c_numPages = 2000;
IM3CodePage pages [2000] = { NULL }; IM3CodePage pages [2000] = { NULL };
@ -126,41 +126,41 @@ int main (int i_argc, const char * i_argv [])
Test (extensions) Test (extensions)
{ {
M3Result result; M3Result result;
IM3Environment env = m3_NewEnvironment (); IM3Environment env = m3_NewEnvironment ();
IM3Runtime runtime = m3_NewRuntime (env, 1024, NULL); IM3Runtime runtime = m3_NewRuntime (env, 1024, NULL);
IM3Module module = m3_NewModule (env); IM3Module module = m3_NewModule (env);
i32 functionIndex = -1; i32 functionIndex = -1;
u8 wasm [5] = { 0x04, // size u8 wasm [5] = { 0x04, // size
0x00, // num local defs 0x00, // num local defs
0x41, 0x37, // i32.const= 55 0x41, 0x37, // i32.const= 55
0x0b // end block 0x0b // end block
}; };
// will partially fail (compilation) because module isn't attached to a runtime yet. // will partially fail (compilation) because module isn't attached to a runtime yet.
result = m3_InjectFunction (module, & functionIndex, "i()", wasm, true); expect (result != m3Err_none) result = m3_InjectFunction (module, & functionIndex, "i()", wasm, true); expect (result != m3Err_none)
expect (functionIndex >= 0) expect (functionIndex >= 0)
result = m3_LoadModule (runtime, module); expect (result == m3Err_none) result = m3_LoadModule (runtime, module); expect (result == m3Err_none)
// try again // try again
result = m3_InjectFunction (module, & functionIndex, "i()", wasm, true); expect (result == m3Err_none) result = m3_InjectFunction (module, & functionIndex, "i()", wasm, true); expect (result == m3Err_none)
IM3Function function = m3_GetFunctionByIndex (module, functionIndex); expect (function) IM3Function function = m3_GetFunctionByIndex (module, functionIndex); expect (function)
if (function) if (function)
{ {
result = m3_CallV (function); expect (result == m3Err_none) result = m3_CallV (function); expect (result == m3Err_none)
u32 ret = 0; u32 ret = 0;
m3_GetResultsV (function, & ret); expect (ret == 55); m3_GetResultsV (function, & ret); expect (ret == 55);
} }
m3_FreeRuntime (runtime); m3_FreeRuntime (runtime);
} }

Loading…
Cancel
Save