Skip to content

Commit

Permalink
Update to Wasm3 v0.5.0 (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
atdrendel authored Jun 4, 2021
1 parent da60d7f commit 1e7c4db
Show file tree
Hide file tree
Showing 35 changed files with 3,736 additions and 1,838 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ To use `CWasm3` with the Swift Package Manager, add a dependency to your Package
```swift
let package = Package(
dependencies: [
.package(name: "CWasm3", url: "https://github.com/shareup/cwasm3.git", .upToNextMinor(from: "0.4.7"))
.package(name: "CWasm3", url: "https://github.com/shareup/cwasm3.git", .upToNextMinor(from: "0.5.0"))
]
)
```
Expand Down
48 changes: 0 additions & 48 deletions Sources/CWasm3/include/m3_api_defs.h

This file was deleted.

92 changes: 35 additions & 57 deletions Sources/CWasm3/include/m3_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "m3_code.h"
#include "m3_exec_defs.h"
#include "m3_function.h"

d_m3BeginExternC

Expand All @@ -27,47 +28,34 @@ enum
c_waOp_getLocal = 0x20,
c_waOp_setLocal = 0x21,
c_waOp_teeLocal = 0x22,
};

typedef struct M3FuncType
{
struct M3FuncType * next;
c_waOp_getGlobal = 0x23,

u32 numRets;
u32 numArgs;
u8 types[]; // returns, then args
}
M3FuncType;
c_waOp_i32_const = 0x41,
c_waOp_i64_const = 0x42,
c_waOp_f32_const = 0x43,
c_waOp_f64_const = 0x44,

c_waOp_memoryCopy = 0xfc0a,
c_waOp_memoryFill = 0xfc0b
};

typedef M3FuncType * IM3FuncType;

#define d_FuncRetType(ftype,i) ((ftype)->types[(i)])
#define d_FuncArgType(ftype,i) ((ftype)->types[(ftype)->numRets + (i)])

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

// since the end location of a block is unknown when a branch is compiled, writing
// the actual address must deferred. A linked-list of patch locations is kept in
// M3CompilationScope. When the block compilation exits, it patches these addresses.
typedef struct M3BranchPatch
{
struct M3BranchPatch * next;
pc_t * location;
}
M3BranchPatch;

typedef M3BranchPatch * IM3BranchPatch;


typedef struct M3CompilationScope
{
struct M3CompilationScope * outer;

pc_t pc; // used by ContinueLoop's
IM3BranchPatch patches;
pc_t patches;
i32 depth;
// i32 loopDepth;
i16 initStackIndex;
u16 exitStackIndex;
i16 blockStackIndex;
// u16 topSlot;
IM3FuncType type;
m3opcode_t opcode;
bool isPolymorphic;
Expand All @@ -76,9 +64,6 @@ M3CompilationScope;

typedef M3CompilationScope * IM3CompilationScope;

// double the slot count when using 32-bit slots, since every wasm stack element could be a 64-bit type
//static const u16 c_m3MaxFunctionSlots = d_m3MaxFunctionStackHeight * (d_m3Use32BitSlots + 1);

typedef struct
{
IM3Runtime runtime;
Expand All @@ -94,19 +79,22 @@ typedef struct

IM3CodePage page;

IM3BranchPatch releasedPatches;

#ifdef DEBUG
u32 numEmits;
u32 numOpcodes;
#endif

u16 firstDynamicStackIndex;
u16 stackIndex; // current stack index
u16 stackFirstDynamicIndex; // args and locals are pushed to the stack so that their slot locations can be tracked. the wasm model itself doesn't
// treat these values as being on the stack, so stackFirstDynamicIndex marks the start of the real Wasm stack
u16 stackIndex; // current stack top

u16 firstConstSlotIndex;
u16 maxConstSlotIndex; // as const's are encountered during compilation this tracks their location in the "real" stack
u16 slotFirstConstIndex;
u16 slotMaxConstIndex; // as const's are encountered during compilation this tracks their location in the "real" stack

u16 firstLocalSlotIndex;
u16 firstDynamicSlotIndex; // numArgs + numLocals + numReservedConstants. the first mutable slot available to the compiler.
u16 slotFirstLocalIndex;
u16 slotFirstDynamicIndex; // numArgs + numLocals + numReservedConstants. the first mutable slot available to the compiler.

u16 maxStackSlots;

m3slot_t constants [d_m3MaxConstantTableSize];

Expand All @@ -117,7 +105,7 @@ typedef struct
// 'm3Slots' contains allocation usage counts
u8 m3Slots [d_m3MaxFunctionSlots];

u16 maxAllocatedSlotPlusOne;
u16 slotMaxAllocatedIndexPlusOne;

u16 regStackIndexPlusOne [2];

Expand Down Expand Up @@ -152,22 +140,12 @@ M3OpInfo;

typedef const M3OpInfo * IM3OpInfo;

extern const M3OpInfo c_operations [];
extern const M3OpInfo c_operationsFC [];

static inline
const M3OpInfo* GetOpInfo(m3opcode_t opcode) {
switch (opcode >> 8) {
case 0x00: return &c_operations[opcode];
case 0xFC: return &c_operationsFC[opcode & 0xFF];
default: return NULL;
}
}
IM3OpInfo 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;
return (ftype && ftype->numRets) ? ftype->types[0] : (u8)c_m3Type_none;
}

#ifdef DEBUG
Expand All @@ -190,19 +168,19 @@ u8 GetSingleRetType(IM3FuncType ftype) {
//-----------------------------------------------------------------------------------------------------------------------------------

u16 GetTypeNumSlots (u8 i_type);
void AlignSlotIndexToType (u16 * io_slotIndex, u8 i_type);
void AlignSlotToType (u16 * io_slotIndex, u8 i_type);

bool IsRegisterAllocated (IM3Compilation o, u32 i_register);
bool IsRegisterLocation (i16 i_location);
bool IsFpRegisterLocation (i16 i_location);
bool IsIntRegisterLocation (i16 i_location);
bool IsRegisterSlotAlias (u16 i_slot);
bool IsFpRegisterSlotAlias (u16 i_slot);
bool IsIntRegisterSlotAlias (u16 i_slot);

bool IsStackPolymorphic (IM3Compilation o);

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

M3Result Compile_BlockStatements (IM3Compilation io);
M3Result Compile_Function (IM3Function io_function);
M3Result CompileBlockStatements (IM3Compilation io);
M3Result CompileFunction (IM3Function io_function);

u16 GetMaxUsedSlotPlusOne (IM3Compilation o);

Expand Down
17 changes: 15 additions & 2 deletions Sources/CWasm3/include/m3_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
# define d_m3MaxFunctionStackHeight 2000 // TODO: comment on upper limit
# endif

# ifndef d_m3MaxLinearMemoryPages
# define d_m3MaxLinearMemoryPages 32768
# endif

# ifndef d_m3MaxFunctionSlots
# define d_m3MaxFunctionSlots ((d_m3MaxFunctionStackHeight)*2)
# endif
Expand All @@ -36,8 +40,12 @@
# define d_m3MaxDuplicateFunctionImpl 3
# endif

# ifndef d_m3VerboseLogs
# define d_m3VerboseLogs 1
# ifndef d_m3EnableExtendedOpcodes
# define d_m3EnableExtendedOpcodes 1
# endif

# ifndef d_m3VerboseErrorMessages
# define d_m3VerboseErrorMessages 1
# endif

# ifndef d_m3FixedHeap
Expand All @@ -61,6 +69,11 @@
# define d_m3RecordBacktraces 0
# endif

# ifndef d_m3EnableExceptionBreakpoint
# define d_m3EnableExceptionBreakpoint 0 // see m3_exception.h
# endif


// profiling and tracing ------------------------------------------------------

# ifndef d_m3EnableOpProfiling
Expand Down
Loading

0 comments on commit 1e7c4db

Please sign in to comment.