Skip to content

Commit

Permalink
Update to Wasm3 0.4.8 (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
atdrendel authored Feb 18, 2021
1 parent 93bee1c commit c4bb509
Show file tree
Hide file tree
Showing 23 changed files with 467 additions and 431 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: ci

on:
push:
branches:
- main
pull_request:
branches:
- '*'

jobs:
build:
name: macOS
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- name: Run tests
run: swift test
2 changes: 1 addition & 1 deletion Sources/CWasm3/include/m3_api_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#define m3ApiGetArg(TYPE, NAME) TYPE NAME = * ((TYPE *) (_sp++));
#define m3ApiGetArgMem(TYPE, NAME) TYPE NAME = (TYPE)m3ApiOffsetToPtr(* ((u32 *) (_sp++)));

#define m3ApiRawFunction(NAME) const void * NAME (IM3Runtime runtime, uint64_t * _sp, void * _mem)
#define m3ApiRawFunction(NAME) const void * NAME (IM3Runtime runtime, uint64_t * _sp, void * _mem, void * userdata)
#define m3ApiReturn(VALUE) { *raw_return = (VALUE); return m3Err_none; }
#define m3ApiTrap(VALUE) { return VALUE; }
#define m3ApiSuccess() { return m3Err_none; }
Expand Down
24 changes: 22 additions & 2 deletions Sources/CWasm3/include/m3_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ enum
c_waOp_teeLocal = 0x22,
};

typedef struct M3FuncType
{
struct M3FuncType * next;

u32 numRets;
u32 numArgs;
u8 types[]; // returns, then args
}
M3FuncType;

typedef M3FuncType * IM3FuncType;

//-----------------------------------------------------------------------------------------------------------------------------------

// since the end location of a block is unknown when a branch is compiled, writing
Expand All @@ -53,7 +65,7 @@ typedef struct M3CompilationScope
i32 depth;
// i32 loopDepth;
i16 initStackIndex;
u8 type;
IM3FuncType type;
m3opcode_t opcode;
bool isPolymorphic;
}
Expand Down Expand Up @@ -148,6 +160,12 @@ const M3OpInfo* GetOpInfo(m3opcode_t opcode) {
}
}

// TODO: This helper should be removed, when MultiValue is implemented
static inline
u8 GetSingleRetType(IM3FuncType ftype) {
return (ftype && ftype->numRets) ? ftype->types[0] : c_m3Type_none;
}

#ifdef DEBUG
#define M3OP(...) { __VA_ARGS__ }
#define M3OP_RESERVED { "reserved" }
Expand All @@ -159,6 +177,8 @@ const M3OpInfo* GetOpInfo(m3opcode_t opcode) {

#if d_m3HasFloat
#define M3OP_F M3OP
#elif d_m3NoFloatDynamic
#define M3OP_F(n,o,t,op,...) M3OP(n, o, t, { op_Unsupported, op_Unsupported, op_Unsupported, op_Unsupported }, __VA_ARGS__)
#else
#define M3OP_F(...) { 0 }
#endif
Expand All @@ -175,7 +195,7 @@ bool IsIntRegisterLocation (i16 i_location);

bool IsStackPolymorphic (IM3Compilation o);

M3Result CompileBlock (IM3Compilation io, u8 i_blockType, u8 i_blockOpcode);
M3Result CompileBlock (IM3Compilation io, IM3FuncType i_blockType, u8 i_blockOpcode);

M3Result Compile_BlockStatements (IM3Compilation io);
M3Result Compile_Function (IM3Function io_function);
Expand Down
10 changes: 9 additions & 1 deletion Sources/CWasm3/include/m3_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# endif

# ifndef d_m3MaxFunctionSlots
# define d_m3MaxFunctionSlots 4000 // twice d_m3MaxFunctionStackHeight
# define d_m3MaxFunctionSlots ((d_m3MaxFunctionStackHeight)*2)
# endif

# ifndef d_m3MaxConstantTableSize
Expand Down Expand Up @@ -72,6 +72,10 @@
# define d_m3EnableOpTracing 0 // only works with DEBUG
# endif

# ifndef d_m3EnableStrace
# define d_m3EnableStrace 0 // trace exported function calls
# endif


// logging --------------------------------------------------------------------

Expand Down Expand Up @@ -122,6 +126,10 @@
# define d_m3HasFloat 1 // implement floating point ops
# endif

#if !d_m3HasFloat && !defined(d_m3NoFloatDynamic)
# define d_m3NoFloatDynamic 1 // if no floats, do not fail until flops are actually executed
#endif

# ifndef d_m3SkipStackCheck
# define d_m3SkipStackCheck 0 // skip stack overrun checks
# endif
Expand Down
6 changes: 3 additions & 3 deletions Sources/CWasm3/include/m3_config_platforms.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#define M3_CONCAT__(a,b) a##b
#define M3_CONCAT(a,b) M3_CONCAT__(a,b)

# if !defined(__cplusplus)
# if !defined(__cplusplus) || defined(_MSC_VER)
# define not !
# define and &&
# define or ||
Expand Down Expand Up @@ -405,10 +405,10 @@ typedef int8_t i8;
# if defined(ARDUINO) || defined(PARTICLE) || defined(PLATFORMIO) || defined(__MBED__) || \
defined(ESP8266) || defined(ESP32) || defined(BLUE_PILL) || defined(WM_W600) || defined(FOMU)
# ifndef d_m3LogOutput
# define d_m3LogOutput false
# define d_m3LogOutput 0
# endif
# ifndef d_m3VerboseLogs
# define d_m3VerboseLogs false
# define d_m3VerboseLogs 0
# endif
# ifndef d_m3MaxFunctionStackHeight
# define d_m3MaxFunctionStackHeight 64
Expand Down
22 changes: 7 additions & 15 deletions Sources/CWasm3/include/m3_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
d_m3BeginExternC

#if !defined(d_m3ShortTypesDefined)
#if d_m3HasFloat
#if d_m3HasFloat || d_m3NoFloatDynamic
typedef double f64;
typedef float f32;
#endif
Expand Down Expand Up @@ -65,12 +65,6 @@ typedef m3slot_t * m3stack_t;
typedef
const void * const cvptr_t;

# define d_m3Log_parse d_m3LogParse // required for m3logif
# define d_m3Log_stack d_m3LogWasmStack
# define d_m3Log_runtime d_m3LogRuntime
# define d_m3Log_exec d_m3LogExec
# define d_m3Log_emit d_m3LogEmit

# if d_m3LogOutput && defined (DEBUG)

# define d_m3Log(CATEGORY, FMT, ...) printf (" %8s | " FMT, #CATEGORY, ##__VA_ARGS__);
Expand Down Expand Up @@ -124,10 +118,9 @@ const void * const cvptr_t;
# endif

# define m3log(CATEGORY, FMT, ...) m3log_##CATEGORY (CATEGORY, FMT "\n", ##__VA_ARGS__)
# define m3logif(CATEGORY, STATEMENT) m3log_##CATEGORY (CATEGORY, ""); if (d_m3Log_##CATEGORY) { STATEMENT; printf ("\n"); }
# else
# define d_m3Log(CATEGORY, FMT, ...) {}
# define m3log(CATEGORY, FMT, ...) {}
# define m3logif(CATEGORY, STATEMENT) {}
# endif


Expand Down Expand Up @@ -177,8 +170,8 @@ M3CodePageHeader;
#define d_externalKind_memory 2
#define d_externalKind_global 3

static const char * const c_waTypes [] = { "nil", "i32", "i64", "f32", "f64", "void", "void *" };
static const char * const c_waCompactTypes [] = { "0", "i", "I", "f", "F", "v", "*" };
static const char * const c_waTypes [] = { "nil", "i32", "i64", "f32", "f64", "unknown" };
static const char * const c_waCompactTypes [] = { "_", "i", "I", "f", "F", "?" };


# if d_m3VerboseLogs
Expand All @@ -200,15 +193,14 @@ M3Result m3Error (M3Result i_result, IM3Runtime i_runtime, IM3Module i_module, I
#if d_m3LogNativeStack
void m3StackCheckInit ();
void m3StackCheck ();
size_t m3StackGetMax ();
int m3StackGetMax ();
#else
#define m3StackCheckInit()
#define m3StackCheck()
#define m3StackGetMax() 0
#endif

void m3Abort (const char* message);

void m3_Abort (const char* message);
M3Result m3_Malloc (void ** o_ptr, size_t i_size);
M3Result m3_Realloc (void ** io_ptr, size_t i_newSize, size_t i_oldSize);
void m3_Free (void ** io_ptr);
Expand All @@ -229,7 +221,7 @@ u32 SizeOfType (u8 i_m3Type);

M3Result Read_u64 (u64 * o_value, bytes_t * io_bytes, cbytes_t i_end);
M3Result Read_u32 (u32 * o_value, bytes_t * io_bytes, cbytes_t i_end);
#if d_m3HasFloat
#if d_m3HasFloat || d_m3NoFloatDynamic
M3Result Read_f64 (f64 * o_value, bytes_t * io_bytes, cbytes_t i_end);
M3Result Read_f32 (f32 * o_value, bytes_t * io_bytes, cbytes_t i_end);
#endif
Expand Down
37 changes: 11 additions & 26 deletions Sources/CWasm3/include/m3_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,7 @@

d_m3BeginExternC

typedef struct M3FuncType
{
struct M3FuncType * next;

u32 numArgs;
u8 returnType;
u8 argTypes [3]; // M3FuncType is a dynamically sized object; these are padding
}
M3FuncType;

typedef M3FuncType * IM3FuncType;

M3Result AllocFuncType (IM3FuncType * o_functionType, u32 i_numArgs);
M3Result AllocFuncType (IM3FuncType * o_functionType, u32 i_numTypes);
bool AreFuncTypesEqual (const IM3FuncType i_typeA, const IM3FuncType i_typeB);


Expand All @@ -41,15 +29,15 @@ typedef struct M3Function
bytes_t wasm;
bytes_t wasmEnd;

u16 numNames; // maximum of d_m3MaxDuplicateFunctionImpl
u16 numNames; // maximum of d_m3MaxDuplicateFunctionImpl
cstr_t names[d_m3MaxDuplicateFunctionImpl];

IM3FuncType funcType;

pc_t compiled;

# if (d_m3EnableCodePageRefCounting)
IM3CodePage * codePageRefs; // array of all pages used
IM3CodePage * codePageRefs; // array of all pages used
u32 numCodePageRefs;
# endif

Expand All @@ -61,7 +49,7 @@ typedef struct M3Function

u16 numArgSlots;

u16 numLocals; // not including args
u16 numLocals; // not including args
u16 numLocalBytes;

void * constants;
Expand All @@ -79,7 +67,6 @@ cstr_t GetFunctionName (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 GetFunctionNumArgsAndLocals (IM3Function i_function);

Expand Down Expand Up @@ -198,16 +185,14 @@ IM3Function Module_GetFunction (IM3Module i_module, u32

//---------------------------------------------------------------------------------------------------------------------------------

static const u32 c_m3NumTypesPerPage = 8;

//---------------------------------------------------------------------------------------------------------------------------------

typedef struct M3Environment
{
// struct M3Runtime * runtimes;

IM3FuncType funcTypes; // linked list

IM3FuncType retFuncTypes[5];

M3CodePage * pagesReleased;
}
M3Environment;
Expand All @@ -219,8 +204,6 @@ void Environment_AddFuncType (IM3Environment i_enviro

//---------------------------------------------------------------------------------------------------------------------------------

// OPTZ: function types need to move to the runtime structure so that all modules can share types
// then type equality can be a simple pointer compare for indirect call checks
typedef struct M3Runtime
{
M3Compilation compilation;
Expand All @@ -239,19 +222,21 @@ typedef struct M3Runtime
u32 stackSize;
u32 numStackSlots;

i32 exit_code;
u32 argc;
ccstr_t * argv;

M3Result runtimeError;
void * userdata;

M3Memory memory;
u32 memoryLimit;

M3Result runtimeError;

M3ErrorInfo error;
#if d_m3VerboseLogs
char error_message[256];
char error_message[256]; // the actual buffer. M3ErrorInfo can point to this
#endif
i32 exit_code;
}
M3Runtime;

Expand Down
4 changes: 4 additions & 0 deletions Sources/CWasm3/include/m3_exec.h
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,10 @@ d_m3Op (Const64)
nextOp ();
}

d_m3Op (Unsupported)
{ m3log (exec, "*** unsupported ***");
return "unsupported instruction executed";
}

d_m3Op (Unreachable)
{ m3log (exec, "*** trapping ***");
Expand Down
1 change: 0 additions & 1 deletion Sources/CWasm3/include/m3_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#define m3_info_h

#include "m3_compile.h"
#include "m3_env.h"

d_m3BeginExternC

Expand Down
Loading

0 comments on commit c4bb509

Please sign in to comment.