Skip to content

Commit

Permalink
Update library version
Browse files Browse the repository at this point in the history
  • Loading branch information
vshymanskyy committed Jan 30, 2020
1 parent 8ce1e38 commit cbfb1fb
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 59 deletions.
12 changes: 6 additions & 6 deletions src/m3_api_libc.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

#include "m3_core.h"

# if defined(__cplusplus)
#if defined(__cplusplus)
extern "C" {
# endif
#endif

M3Result m3_LinkLibC (IM3Module io_module);
M3Result m3_LinkSpecTest (IM3Module io_module);
M3Result m3_LinkLibC (IM3Module io_module);
M3Result m3_LinkSpecTest (IM3Module io_module);

# if defined(__cplusplus)
#if defined(__cplusplus)
}
# endif
#endif

#endif // m3_api_libc_h
7 changes: 7 additions & 0 deletions src/m3_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

#include "m3_core.h"

#if defined(__cplusplus)
extern "C" {
#endif

typedef struct M3CodePage
{
Expand Down Expand Up @@ -45,4 +48,8 @@ void dump_code_page (IM3CodePage i_codePage, pc_t
# define EmitWord64(page, val) EmitWord_impl(page, (void*)(val))
#endif

#if defined(__cplusplus)
}
#endif

#endif // m3_code_h
75 changes: 43 additions & 32 deletions src/m3_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ bool IsFpRegisterLocation (i16 i_location) { return (i_location == d_m3
bool IsIntRegisterLocation (i16 i_location) { return (i_location == d_m3Reg0SlotAlias); }


u32 GetTypeNumSlots (u8 i_type)
{
return Is64BitType (i_type) ? 1 : 1;
}

i16 GetStackTopIndex (IM3Compilation o)
{
return o->stackIndex - 1;
Expand Down Expand Up @@ -77,7 +82,6 @@ u8 GetStackBottomType (IM3Compilation o, u16 i_offset)
}



u8 GetBlockType (IM3Compilation o)
{
return o->block.type;
Expand Down Expand Up @@ -154,10 +158,10 @@ void MarkSlotAllocated (IM3Compilation o, u16 i_slot)
}


bool AllocateSlot (IM3Compilation o, u16 * o_execSlot)
bool AllocateSlots (IM3Compilation o, u16 * o_execSlot, u8 i_type)
{
bool found = false;

// search for empty slot in the execution stack
i16 i = o->firstSlotIndex;
while (i < d_m3MaxFunctionStackHeight)
Expand Down Expand Up @@ -194,11 +198,15 @@ M3Result IncrementSlotUsageCount (IM3Compilation o, u16 i_slot)
}


void DeallocateSlot (IM3Compilation o, i16 i_slotIndex)
void DeallocateSlot (IM3Compilation o, i16 i_slotIndex, u8 i_type)
{ d_m3Assert (i_slotIndex >= o->firstSlotIndex);
d_m3Assert (o->m3Slots [i_slotIndex]);
if (-- o->m3Slots [i_slotIndex] == 0)
o->numAllocatedExecSlots--;
for (u32 i = 0; i < GetTypeNumSlots (i_type); ++i, ++i_slotIndex)
{
if (-- o->m3Slots [i_slotIndex] == 0)
o->numAllocatedExecSlots--;
}

}


Expand Down Expand Up @@ -268,15 +276,15 @@ M3Result PreserveRegisterIfOccupied (IM3Compilation o, u8 i_registerType)
{
u16 stackIndex = GetRegisterStackIndex (o, regSelect);
DeallocateRegister (o, regSelect);

u8 type = GetStackBottomType (o, stackIndex);

// and point to a exec slot
u16 slot;
if (AllocateSlot (o, & slot))
if (AllocateSlots (o, & slot, type))
{
o->wasmStack [stackIndex] = slot;

u8 type = o->typeStack [stackIndex];

_ (EmitOp (o, c_setSetOps [type]));
EmitSlotOffset (o, slot);
}
Expand Down Expand Up @@ -328,7 +336,7 @@ _ (PreserveRegisterIfOccupied (o, c_m3Type_f64));
//----------------------------------------------------------------------------------------------------------------------


M3Result Push (IM3Compilation o, u8 i_m3Type, i16 i_location)
M3Result Push (IM3Compilation o, u8 i_type, i16 i_location)
{
M3Result result = m3Err_none;

Expand All @@ -344,7 +352,7 @@ M3Result Push (IM3Compilation o, u8 i_m3Type, i16 i_location)
}

o->wasmStack [stackIndex] = i_location;
o->typeStack [stackIndex] = i_m3Type;
o->typeStack [stackIndex] = i_type;

if (IsRegisterLocation (i_location))
{
Expand All @@ -358,10 +366,10 @@ M3Result Push (IM3Compilation o, u8 i_m3Type, i16 i_location)
}


M3Result PushRegister (IM3Compilation o, u8 i_m3Type)
M3Result PushRegister (IM3Compilation o, u8 i_type)
{
i16 location = IsFpType (i_m3Type) ? d_m3Fp0SlotAlias : d_m3Reg0SlotAlias; d_m3Assert (i_m3Type or IsStackPolymorphic (o));
return Push (o, i_m3Type, location);
i16 location = IsFpType (i_type) ? d_m3Fp0SlotAlias : d_m3Reg0SlotAlias; d_m3Assert (i_type or IsStackPolymorphic (o));
return Push (o, i_type, location);
}


Expand All @@ -374,6 +382,7 @@ M3Result Pop (IM3Compilation o)
o->stackIndex--; // printf ("pop: %d\n", (i32) o->stackIndex);

i16 location = o->wasmStack [o->stackIndex];
u8 type = o->typeStack [o->stackIndex];

if (IsRegisterLocation (location))
{
Expand All @@ -382,7 +391,7 @@ M3Result Pop (IM3Compilation o)
}
else if (location >= o->firstSlotIndex)
{
DeallocateSlot (o, location);
DeallocateSlot (o, location, type);
}

m3logif (stack, dump_type_stack (o))
Expand Down Expand Up @@ -414,15 +423,15 @@ _ (Pop (o));
}


M3Result _PushAllocatedSlotAndEmit (IM3Compilation o, u8 i_m3Type, bool i_doEmit)
M3Result _PushAllocatedSlotAndEmit (IM3Compilation o, u8 i_type, bool i_doEmit)
{
M3Result result = m3Err_none;

u16 slot;

if (AllocateSlot (o, & slot))
if (AllocateSlots (o, & slot, i_type))
{
_ (Push (o, i_m3Type, slot));
_ (Push (o, i_type, slot));

if (i_doEmit)
EmitSlotOffset (o, slot);
Expand All @@ -433,19 +442,19 @@ _ (Push (o, i_m3Type, slot));
}


M3Result PushAllocatedSlotAndEmit (IM3Compilation o, u8 i_m3Type)
M3Result PushAllocatedSlotAndEmit (IM3Compilation o, u8 i_type)
{
return _PushAllocatedSlotAndEmit (o, i_m3Type, true);
return _PushAllocatedSlotAndEmit (o, i_type, true);
}


M3Result PushAllocatedSlot (IM3Compilation o, u8 i_m3Type)
M3Result PushAllocatedSlot (IM3Compilation o, u8 i_type)
{
return _PushAllocatedSlotAndEmit (o, i_m3Type, false);
return _PushAllocatedSlotAndEmit (o, i_type, false);
}


M3Result PushConst (IM3Compilation o, u64 i_word, u8 i_m3Type)
M3Result PushConst (IM3Compilation o, u64 i_word, u8 i_type)
{
M3Result result = m3Err_none;

Expand All @@ -459,7 +468,7 @@ M3Result PushConst (IM3Compilation o, u64 i_word, u8 i_m3Type)
if (o->constants [i] == i_word)
{
location = o->firstConstSlotIndex + i;
_ (Push (o, i_m3Type, location));
_ (Push (o, i_type, location));
break;
}
}
Expand All @@ -471,13 +480,13 @@ _ (Push (o, i_m3Type, location));
o->constants [numConstants] = i_word;
location = o->constSlotIndex++;

_ (Push (o, i_m3Type, location));
_ (Push (o, i_type, location));
}
else
{
_ (EmitOp (o, op_Const));
EmitConstant64 (o, i_word);
_ (PushAllocatedSlotAndEmit (o, i_m3Type));
_ (PushAllocatedSlotAndEmit (o, i_type));
}
}

Expand Down Expand Up @@ -686,7 +695,9 @@ M3Result FindReferencedLocalWithinCurrentBlock (IM3Compilation o, u16 * o_pres
{
if (* o_preservedSlotIndex == i_localIndex)
{
if (not AllocateSlot (o, o_preservedSlotIndex))
u8 localType = GetStackBottomType (o, i_localIndex);

if (not AllocateSlots (o, o_preservedSlotIndex, localType))
_throw (m3Err_functionStackOverflow);
}
else
Expand Down Expand Up @@ -1239,7 +1250,7 @@ _ (ReadLEB_i7 (& reserved, & o->wasm, o->wasmEnd));

_ (EmitOp (o, op_MemCurrent));

_ (PushRegister (o, c_m3Type_i32)); // i32?
_ (PushRegister (o, c_m3Type_i32));

_catch: return result;
}
Expand All @@ -1257,7 +1268,7 @@ _ (Pop (o));

_ (EmitOp (o, op_MemGrow));

_ (PushRegister (o, c_m3Type_i32)); // i32?
_ (PushRegister (o, c_m3Type_i32));

_catch: return result;
}
Expand Down Expand Up @@ -1760,9 +1771,9 @@ const M3OpInfo c_operations [] =
M3OP( "i32.div_u", -1, i_32, d_binOpList (u32, Divide) ), // 0x6e
M3OP( "i32.rem_s", -1, i_32, d_binOpList (i32, Remainder) ), // 0x6f
M3OP( "i32.rem_u", -1, i_32, d_binOpList (u32, Remainder) ), // 0x70
M3OP( "i32.and", -1, i_32, d_commutativeBinOpList (u64, And) ), // 0x71
M3OP( "i32.or", -1, i_32, d_commutativeBinOpList (u64, Or) ), // 0x72
M3OP( "i32.xor", -1, i_32, d_commutativeBinOpList (u64, Xor) ), // 0x73
M3OP( "i32.and", -1, i_32, d_commutativeBinOpList (u32, And) ), // 0x71
M3OP( "i32.or", -1, i_32, d_commutativeBinOpList (u32, Or) ), // 0x72
M3OP( "i32.xor", -1, i_32, d_commutativeBinOpList (u32, Xor) ), // 0x73
M3OP( "i32.shl", -1, i_32, d_binOpList (u32, ShiftLeft) ), // 0x74
M3OP( "i32.shr_s", -1, i_32, d_binOpList (i32, ShiftRight) ), // 0x75
M3OP( "i32.shr_u", -1, i_32, d_binOpList (u32, ShiftRight) ), // 0x76
Expand Down
7 changes: 6 additions & 1 deletion src/m3_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#include "m3_code.h"
#include "m3_exec_defs.h"

#if defined(__cplusplus)
extern "C" {
#endif

enum
{
Expand Down Expand Up @@ -164,6 +167,8 @@ M3Result Compile_Function (IM3Function io_function);
bool PeekNextOpcode (IM3Compilation o, u8 i_opcode);
u16 GetMaxExecSlot (IM3Compilation o);

//M3Result Optimize_ConstOp (IM3Compilation o, u64 i_word, u8 i_waType);
#if defined(__cplusplus)
}
#endif

#endif // m3_compile_h
3 changes: 2 additions & 1 deletion src/m3_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ void m3NotImplemented() {
}

M3_WEAK
void m3Yield ()
M3Result m3_Yield ()
{
return m3Err_none;
}

#if d_m3FixedHeap
Expand Down
10 changes: 8 additions & 2 deletions src/m3_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
#include "wasm3.h"
#include "m3_config.h"

#if defined(__cplusplus)
extern "C" {
#endif

#if !defined(d_m3ShortTypesDefined)
typedef double f64;
typedef float f32;
Expand Down Expand Up @@ -193,8 +197,6 @@ size_t m3StackGetMax ();
void m3Abort (const char* message);
void m3NotImplemented (void);

void m3Yield (void);

M3Result m3Malloc (void ** o_ptr, size_t i_size);
void * m3Realloc (void * i_ptr, size_t i_newSize, size_t i_oldSize);
void m3Free_impl (void * o_ptr);
Expand Down Expand Up @@ -225,4 +227,8 @@ size_t SPrintArg (char * o_string, size_t i_n, m3stack_t i_sp

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

#if defined(__cplusplus)
}
#endif

#endif // m3_core_h
8 changes: 8 additions & 0 deletions src/m3_emit.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

#include "m3_compile.h"

#if defined(__cplusplus)
extern "C" {
#endif

M3Result BridgeToNewPageIfNecessary (IM3Compilation o);
M3Result EnsureCodePageNumLines (IM3Compilation o, u32 i_numLines);

Expand All @@ -22,4 +26,8 @@ void * ReservePointer (IM3Compilation o);

pc_t GetPC (IM3Compilation o);

#if defined(__cplusplus)
}
#endif

#endif // m3_emit_h
14 changes: 4 additions & 10 deletions src/m3_env.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,21 +455,15 @@ M3Result InitStartFunc (IM3Module io_module)
{
M3Result result = m3Err_none;

if (io_module->startFunction >= 0) {
if ((u32)io_module->startFunction >= io_module->numFunctions) {
return "start function index out of bounds";
}
IM3Function function = &io_module->functions [io_module->startFunction];
if (not function) {
return "start function not found";
}
if (io_module->startFunction >= 0)
{
IM3Function function = & io_module->functions [io_module->startFunction];

if (not function->compiled)
{
_ (Compile_Function (function));
if (result)
function = NULL;
}

_ (m3_Call(function));

io_module->startFunction = -1;
Expand Down
6 changes: 6 additions & 0 deletions src/m3_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#include "m3_exec.h"
#include "m3_compile.h"

#if defined(__cplusplus)
extern "C" {
#endif

typedef struct M3FuncType
{
Expand Down Expand Up @@ -252,5 +255,8 @@ void ReleaseCodePage (IM3Runtime io_runtime,

M3Result m3Error (M3Result i_result, IM3Runtime i_runtime, IM3Module i_module, IM3Function i_function, const char * const i_file, u32 i_lineNum, const char * const i_errorMessage, ...);

#if defined(__cplusplus)
}
#endif

#endif // m3_env_h
Loading

0 comments on commit cbfb1fb

Please sign in to comment.