Skip to content

Commit 3c416b8

Browse files
committedFeb 25, 2020
hmmm
1 parent 7e5a747 commit 3c416b8

10 files changed

+63
-38
lines changed
 

‎source/m3_compile.c

+24-15
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,12 @@ i16 GetNumBlockValues (IM3Compilation o) { return o->stackIndex - o->
5050

5151
u16 GetTypeNumSlots (u8 i_type)
5252
{
53-
// return 1;
54-
u16 n = Is64BitType (i_type) ? 2 : 1;
55-
return n;
53+
# if d_m3Use32BitSlots
54+
u16 n = Is64BitType (i_type) ? 2 : 1;
55+
return n;
56+
# else
57+
return 1;
58+
# endif
5659
}
5760

5861
i16 GetStackTopIndex (IM3Compilation o)
@@ -177,8 +180,9 @@ M3Result AllocateSlotsWithinRange (IM3Compilation o, u16 * o_slot, u8 i_type,
177180

178181
u16 numSlots = GetTypeNumSlots (i_type);
179182
u16 searchOffset = numSlots - 1;
180-
181-
AlignSlotIndexToType (& i_startSlot, i_type);
183+
184+
if (d_m3Use32BitSlots)
185+
AlignSlotIndexToType (& i_startSlot, i_type);
182186

183187
// search for 1 or 2 consecutive slots in the execution stack
184188
u16 i = i_startSlot;
@@ -539,15 +543,15 @@ _ (Push (o, i_type, slot));
539543
{
540544
result = m3Err_none;
541545

542-
_ (EmitOp (o, numRequiredSlots == 1 ? op_Const32 : op_Const64));
546+
_ (EmitOp (o, Is64BitType (i_type) ? op_Const64 : op_Const32));
543547
EmitConstant64 (o, i_word);
544548
_ (PushAllocatedSlotAndEmit (o, i_type));
545549
}
546550
else
547551
{
548552
u16 constTableIndex = slot - o->firstConstSlotIndex;
549553

550-
if (numRequiredSlots == 2)
554+
if (Is64BitType (i_type))
551555
{
552556
u64 * constant64 = (u64 *) & o->constants [constTableIndex];
553557
* constant64 = i_word;
@@ -618,6 +622,9 @@ bool PatchBranches (IM3Compilation o)
618622

619623
while (patches)
620624
{ m3log (compile, "patching location: %p to pc: %p", patches->location, pc);
625+
if (not patches->location)
626+
break;
627+
621628
* (patches->location) = pc;
622629

623630
endPatch = patches;
@@ -1220,8 +1227,6 @@ void AlignSlotIndexToType (u16 * io_slotIndex, u8 i_type)
12201227
// align 64-bit words to even slots
12211228
u16 numSlots = GetTypeNumSlots (i_type);
12221229

1223-
// printf ("%d\n", (u32) numSlots);
1224-
12251230
u16 mask = numSlots - 1;
12261231
* io_slotIndex = (* io_slotIndex + mask) & ~mask;
12271232
}
@@ -1251,12 +1256,14 @@ _ (Pop (o));
12511256

12521257
u32 numArgs = i_type->numArgs;
12531258

1259+
u32 slotsPerArg = sizeof (u64) / sizeof (m3slot_t);
1260+
12541261
// args are 64-bit aligned
1255-
u16 argTop = topSlot + numArgs * 2;
1262+
u16 argTop = topSlot + numArgs * slotsPerArg;
12561263

12571264
while (numArgs--)
12581265
{
1259-
_ (CopyTopSlot (o, argTop -= 2));
1266+
_ (CopyTopSlot (o, argTop -= slotsPerArg));
12601267
_ (Pop (o));
12611268
}
12621269

@@ -1803,7 +1810,7 @@ const M3OpInfo c_operations [] =
18031810
M3OP( "local.get", 1, any, d_emptyOpList, Compile_GetLocal ), // 0x20
18041811
M3OP( "local.set", 1, none, d_emptyOpList, Compile_SetLocal ), // 0x21
18051812
M3OP( "local.tee", 0, any, d_emptyOpList, Compile_SetLocal ), // 0x22
1806-
M3OP( "global.get", 1, none, d_logOp2 (GetGlobal_s32, GetGlobal_s64), Compile_GetSetGlobal ), // 0x23
1813+
M3OP( "global.get", 1, none, d_emptyOpList, Compile_GetSetGlobal ), // 0x23
18071814
M3OP( "global.set", 1, none, d_emptyOpList, Compile_GetSetGlobal ), // 0x24
18081815

18091816
M3OP_RESERVED, M3OP_RESERVED, M3OP_RESERVED, // 0x25 - 0x27
@@ -1999,6 +2006,8 @@ const M3OpInfo c_operations [] =
19992006
# define d_m3DebugTypedOp(OP) M3OP (#OP, 0, none, { op_##OP##_i32, op_##OP##_i64, op_##OP##_f32, op_##OP##_f64, })
20002007

20012008
d_m3DebugOp (Entry), d_m3DebugOp (Compile), d_m3DebugOp (End),
2009+
2010+
d_m3DebugOp (GetGlobal_s32), d_m3DebugOp (GetGlobal_s64),
20022011

20032012
d_m3DebugOp (ContinueLoop), d_m3DebugOp (ContinueLoopIf),
20042013

@@ -2195,7 +2204,7 @@ M3Result Compile_Function (IM3Function io_function)
21952204
M3Result result = m3Err_none; m3log (compile, "compiling: '%s'; wasm-size: %d; numArgs: %d; return: %s",
21962205
io_function->name, (u32) (io_function->wasmEnd - io_function->wasm), GetFunctionNumArgs (io_function), c_waTypes [GetFunctionReturnType (io_function)]);
21972206
IM3Runtime runtime = io_function->module->runtime;
2198-
2207+
21992208
IM3Compilation o = & runtime->compilation;
22002209
SetupCompilation (o);
22012210

@@ -2262,11 +2271,11 @@ _ (Compile_BlockStatements (o));
22622271

22632272
u32 numConstantSlots = o->maxConstSlotIndex - o->firstConstSlotIndex; m3log (compile, "unique constant slots: %d; unused slots: %d", numConstantSlots, o->firstDynamicSlotIndex - o->maxConstSlotIndex);
22642273

2265-
io_function->numConstantBytes = numConstantSlots * sizeof (u32);
2274+
io_function->numConstantBytes = numConstantSlots * sizeof (m3slot_t);
22662275

22672276
if (numConstantSlots)
22682277
{
2269-
_ (m3Alloc (& io_function->constants, u32, numConstantSlots));
2278+
_ (m3Alloc (& io_function->constants, m3slot_t, numConstantSlots));
22702279

22712280
memcpy (io_function->constants, o->constants, io_function->numConstantBytes);
22722281
}

‎source/m3_compile.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ M3CompilationScope;
6161

6262
typedef M3CompilationScope * IM3CompilationScope;
6363

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

6767
typedef struct
6868
{
@@ -92,7 +92,7 @@ typedef struct
9292
u16 firstLocalSlotIndex;
9393
u16 firstDynamicSlotIndex; // numArgs + numLocals + numReservedConstants. the first mutable slot available to the compiler.
9494

95-
u32 constants [d_m3MaxConstantTableSize];
95+
m3slot_t constants [d_m3MaxConstantTableSize];
9696

9797
// 'wasmStack' holds slot locations
9898
u16 wasmStack [d_m3MaxFunctionStackHeight];

‎source/m3_config.h

+8-4
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,14 @@
4040
# define d_m3FixedHeapAlign 16
4141
# endif
4242

43-
# ifndef d_m3EnableOptimizations
44-
# define d_m3EnableOptimizations 0
43+
# ifndef d_m3Use32BitSlots
44+
# define d_m3Use32BitSlots 0
4545
# endif
4646

47+
//# ifndef d_m3EnableOptimizations
48+
//# define d_m3EnableOptimizations 0
49+
//# endif
50+
4751
// logging --------------------------------------------------------------------
4852

4953
# define d_m3EnableOpProfiling 0
@@ -57,9 +61,9 @@
5761
# define d_m3LogParse 0 // .wasm binary decoding info
5862
# define d_m3LogModule 0 // Wasm module info
5963
# define d_m3LogCompile 1 // wasm -> metacode generation phase
60-
# define d_m3LogWasmStack 1 // dump the wasm stack when pushed or popped
64+
# define d_m3LogWasmStack 0 // dump the wasm stack when pushed or popped
6165
# define d_m3LogEmit 0 // metacode generation info
62-
# define d_m3LogCodePages 0 // dump metacode pages when released
66+
# define d_m3LogCodePages 1 // dump metacode pages when released
6367
# define d_m3LogExec 1 // low-level interpreter specific logs
6468
# define d_m3LogRuntime 0 // higher-level runtime information
6569
# define d_m3LogStackTrace 0 // dump the call stack when traps occur

‎source/m3_core.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,19 @@ M3Result m3Malloc (void ** o_ptr, size_t i_size)
116116
else result = m3Err_mallocFailed;
117117

118118
* o_ptr = ptr;
119-
//printf("== alloc %d => %p\n", i_size, ptr);
119+
// printf("== alloc %d => %p\n", (u32) i_size, ptr);
120120

121121
return result;
122122
}
123123

124124
void m3Free_impl (void * i_ptr)
125125
{
126-
//printf("== free %p\n", o_ptr);
126+
// if (i_ptr) printf("== free %p\n", i_ptr);
127127
free (i_ptr);
128128
}
129129

130130
void * m3Realloc (void * i_ptr, size_t i_newSize, size_t i_oldSize)
131131
{
132-
//printf("== realloc %p => %d\n", i_ptr, i_newSize);
133132
void * ptr = i_ptr;
134133

135134
if (i_newSize != i_oldSize)
@@ -145,6 +144,8 @@ void * m3Realloc (void * i_ptr, size_t i_newSize, size_t i_oldSize)
145144
}
146145
else memset (ptr, 0x0, i_newSize);
147146
}
147+
148+
// printf("== realloc %p -> %p => %d\n", i_ptr, ptr, (u32) i_newSize);
148149
}
149150

150151
return ptr;

‎source/m3_core.h

+6
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ typedef const u8 * bytes_t;
4848
typedef const u8 * const cbytes_t;
4949

5050
typedef i64 m3reg_t;
51+
52+
# if d_m3Use32BitSlots
5153
typedef u32 m3slot_t;
54+
# else
55+
typedef u64 m3slot_t;
56+
# endif
57+
5258
typedef m3slot_t * m3stack_t;
5359

5460
typedef

‎source/m3_env.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ _ (ReadLEB_u32 (& numElements, & bytes, end));
426426

427427
if (endElement > offset) // TODO: check this, endElement depends on offset
428428
{
429-
io_module->table0 = (IM3Function*)m3ReallocArray (io_module->table0, IM3Function, endElement, io_module->table0Size);
429+
io_module->table0 = (IM3Function*) m3ReallocArray (io_module->table0, IM3Function, endElement, io_module->table0Size);
430430

431431
if (io_module->table0)
432432
{
@@ -569,11 +569,13 @@ M3Result m3_CallWithArgs (IM3Function i_function, uint32_t i_argc, const char
569569
i_argc = 0;
570570

571571
IM3FuncType ftype = i_function->funcType; m3logif (runtime, PrintFuncTypeSignature (ftype));
572-
u64 * stack = (u64 *) runtime->stack;
573572

574573
if (i_argc != ftype->numArgs)
575574
_throw (m3Err_argumentCountMismatch);
576575

576+
// args are always 64-bit aligned
577+
u64 * stack = (u64 *) runtime->stack;
578+
577579
// The format is currently not user-friendly by default,
578580
// as this is used in spec tests
579581
for (u32 i = 0; i < ftype->numArgs; ++i)

‎source/m3_exec.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,15 @@ d_m3OpDef (Entry)
199199

200200
IM3Function function = immediate (IM3Function);
201201

202-
#if defined(d_m3SkipStackCheck)
202+
#if defined (d_m3SkipStackCheck)
203203
if (true)
204204
#else
205-
if ((void*)(_sp + function->maxStackSlots) < _mem->maxStack)
205+
if ((void *) ((m3slot_t *) _sp + function->maxStackSlots) < _mem->maxStack)
206206
#endif
207207
{
208208
function->hits++; m3log (exec, " enter %p > %s %s", _pc - 2, function->name ? function->name : ".unnamed", SPrintFunctionArgList (function, _sp));
209209

210-
u8 * stack = (u8 *) (_sp + function->numArgSlots);
210+
u8 * stack = (u8 *) ((m3slot_t *) _sp + function->numArgSlots);
211211

212212
memset (stack, 0x0, function->numLocalBytes);
213213
stack += function->numLocalBytes;
@@ -252,7 +252,7 @@ d_m3OpDef (GetGlobal_s32)
252252
d_m3OpDef (GetGlobal_s64)
253253
{
254254
u64 * global = immediate (u64 *);
255-
slot (u64) = * global; // printf ("get global: %p %" PRIi64 "\n", global, *global);
255+
slot (u64) = * global; printf ("get global: %p %" PRIi64 "\n", global, *global);
256256

257257
nextOp ();
258258
}

‎source/m3_exec.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ d_m3Store_i (i64, i64)
875875
#undef m3MemCheck
876876

877877
//---------------------------------------------------------------------------------------------------------------------
878-
# if d_m3EnableOptimizations
878+
# if 0 //d_m3EnableOptimizations
879879
//---------------------------------------------------------------------------------------------------------------------
880880

881881
#define d_m3BinaryOpWith1_i(TYPE, NAME, OPERATION) \

‎source/m3_info.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,18 @@ void DecodeOperation (char * o_string, u8 i_opcode, IM3Operation i_operation,
247247

248248
switch (i_opcode)
249249
{
250-
d_m3Decode (0xc0, Const)
251-
d_m3Decode (0xc1, Entry)
250+
// d_m3Decode (0xc0, Const)
251+
// d_m3Decode (0xc1, Entry)
252252
d_m3Decode (c_waOp_call, Call)
253253
d_m3Decode (c_waOp_branch, Branch)
254254
d_m3Decode (c_waOp_branchTable, BranchTable)
255255
d_m3Decode (0x39, f64_Store)
256256
}
257+
258+
#undef d_m3Decode
259+
#define d_m3Decode(FUNC) if (i_operation == op_##FUNC) Decode_##FUNC (o_string, i_opcode, i_operation, i_opInfo, o_pc);
260+
261+
d_m3Decode (Entry)
257262
}
258263

259264

‎source/m3_parse.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,7 @@ M3Result ParseSection_Import (IM3Module io_module, bytes_t i_bytes, cbytes_t i
126126
{
127127
M3Result result = m3Err_none;
128128

129-
M3ImportInfo import, clearImport;
130-
M3_INIT(import);
131-
M3_INIT(clearImport);
129+
M3ImportInfo import = { NULL, NULL }, clearImport = { NULL, NULL };
132130

133131
u32 numImports;
134132
_ (ReadLEB_u32 (& numImports, & i_bytes, i_end)); m3log (parse, "** Import [%d]", numImports);
@@ -474,7 +472,7 @@ _ (Read_utf8 (& name, & i_bytes, i_end));
474472
if (not io_module->functions [index].name)
475473
{
476474
io_module->functions [index].name = name; m3log (parse, "naming function [%d]: %s", index, name);
477-
name = NULL;
475+
name = NULL; // transfer ownership
478476
}
479477
// else m3log (parse, "prenamed: %s", io_module->functions [index].name);
480478
}

0 commit comments

Comments
 (0)