Skip to content

Commit cbfb1fb

Browse files
committed
Update library version
1 parent 8ce1e38 commit cbfb1fb

14 files changed

+131
-59
lines changed

src/m3_api_libc.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010

1111
#include "m3_core.h"
1212

13-
# if defined(__cplusplus)
13+
#if defined(__cplusplus)
1414
extern "C" {
15-
# endif
15+
#endif
1616

17-
M3Result m3_LinkLibC (IM3Module io_module);
18-
M3Result m3_LinkSpecTest (IM3Module io_module);
17+
M3Result m3_LinkLibC (IM3Module io_module);
18+
M3Result m3_LinkSpecTest (IM3Module io_module);
1919

20-
# if defined(__cplusplus)
20+
#if defined(__cplusplus)
2121
}
22-
# endif
22+
#endif
2323

2424
#endif // m3_api_libc_h

src/m3_code.h

+7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
#include "m3_core.h"
1212

13+
#if defined(__cplusplus)
14+
extern "C" {
15+
#endif
1316

1417
typedef struct M3CodePage
1518
{
@@ -45,4 +48,8 @@ void dump_code_page (IM3CodePage i_codePage, pc_t
4548
# define EmitWord64(page, val) EmitWord_impl(page, (void*)(val))
4649
#endif
4750

51+
#if defined(__cplusplus)
52+
}
53+
#endif
54+
4855
#endif // m3_code_h

src/m3_compile.c

+43-32
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ bool IsFpRegisterLocation (i16 i_location) { return (i_location == d_m3
4242
bool IsIntRegisterLocation (i16 i_location) { return (i_location == d_m3Reg0SlotAlias); }
4343

4444

45+
u32 GetTypeNumSlots (u8 i_type)
46+
{
47+
return Is64BitType (i_type) ? 1 : 1;
48+
}
49+
4550
i16 GetStackTopIndex (IM3Compilation o)
4651
{
4752
return o->stackIndex - 1;
@@ -77,7 +82,6 @@ u8 GetStackBottomType (IM3Compilation o, u16 i_offset)
7782
}
7883

7984

80-
8185
u8 GetBlockType (IM3Compilation o)
8286
{
8387
return o->block.type;
@@ -154,10 +158,10 @@ void MarkSlotAllocated (IM3Compilation o, u16 i_slot)
154158
}
155159

156160

157-
bool AllocateSlot (IM3Compilation o, u16 * o_execSlot)
161+
bool AllocateSlots (IM3Compilation o, u16 * o_execSlot, u8 i_type)
158162
{
159163
bool found = false;
160-
164+
161165
// search for empty slot in the execution stack
162166
i16 i = o->firstSlotIndex;
163167
while (i < d_m3MaxFunctionStackHeight)
@@ -194,11 +198,15 @@ M3Result IncrementSlotUsageCount (IM3Compilation o, u16 i_slot)
194198
}
195199

196200

197-
void DeallocateSlot (IM3Compilation o, i16 i_slotIndex)
201+
void DeallocateSlot (IM3Compilation o, i16 i_slotIndex, u8 i_type)
198202
{ d_m3Assert (i_slotIndex >= o->firstSlotIndex);
199203
d_m3Assert (o->m3Slots [i_slotIndex]);
200-
if (-- o->m3Slots [i_slotIndex] == 0)
201-
o->numAllocatedExecSlots--;
204+
for (u32 i = 0; i < GetTypeNumSlots (i_type); ++i, ++i_slotIndex)
205+
{
206+
if (-- o->m3Slots [i_slotIndex] == 0)
207+
o->numAllocatedExecSlots--;
208+
}
209+
202210
}
203211

204212

@@ -268,15 +276,15 @@ M3Result PreserveRegisterIfOccupied (IM3Compilation o, u8 i_registerType)
268276
{
269277
u16 stackIndex = GetRegisterStackIndex (o, regSelect);
270278
DeallocateRegister (o, regSelect);
279+
280+
u8 type = GetStackBottomType (o, stackIndex);
271281

272282
// and point to a exec slot
273283
u16 slot;
274-
if (AllocateSlot (o, & slot))
284+
if (AllocateSlots (o, & slot, type))
275285
{
276286
o->wasmStack [stackIndex] = slot;
277287

278-
u8 type = o->typeStack [stackIndex];
279-
280288
_ (EmitOp (o, c_setSetOps [type]));
281289
EmitSlotOffset (o, slot);
282290
}
@@ -328,7 +336,7 @@ _ (PreserveRegisterIfOccupied (o, c_m3Type_f64));
328336
//----------------------------------------------------------------------------------------------------------------------
329337

330338

331-
M3Result Push (IM3Compilation o, u8 i_m3Type, i16 i_location)
339+
M3Result Push (IM3Compilation o, u8 i_type, i16 i_location)
332340
{
333341
M3Result result = m3Err_none;
334342

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

346354
o->wasmStack [stackIndex] = i_location;
347-
o->typeStack [stackIndex] = i_m3Type;
355+
o->typeStack [stackIndex] = i_type;
348356

349357
if (IsRegisterLocation (i_location))
350358
{
@@ -358,10 +366,10 @@ M3Result Push (IM3Compilation o, u8 i_m3Type, i16 i_location)
358366
}
359367

360368

361-
M3Result PushRegister (IM3Compilation o, u8 i_m3Type)
369+
M3Result PushRegister (IM3Compilation o, u8 i_type)
362370
{
363-
i16 location = IsFpType (i_m3Type) ? d_m3Fp0SlotAlias : d_m3Reg0SlotAlias; d_m3Assert (i_m3Type or IsStackPolymorphic (o));
364-
return Push (o, i_m3Type, location);
371+
i16 location = IsFpType (i_type) ? d_m3Fp0SlotAlias : d_m3Reg0SlotAlias; d_m3Assert (i_type or IsStackPolymorphic (o));
372+
return Push (o, i_type, location);
365373
}
366374

367375

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

376384
i16 location = o->wasmStack [o->stackIndex];
385+
u8 type = o->typeStack [o->stackIndex];
377386

378387
if (IsRegisterLocation (location))
379388
{
@@ -382,7 +391,7 @@ M3Result Pop (IM3Compilation o)
382391
}
383392
else if (location >= o->firstSlotIndex)
384393
{
385-
DeallocateSlot (o, location);
394+
DeallocateSlot (o, location, type);
386395
}
387396

388397
m3logif (stack, dump_type_stack (o))
@@ -414,15 +423,15 @@ _ (Pop (o));
414423
}
415424

416425

417-
M3Result _PushAllocatedSlotAndEmit (IM3Compilation o, u8 i_m3Type, bool i_doEmit)
426+
M3Result _PushAllocatedSlotAndEmit (IM3Compilation o, u8 i_type, bool i_doEmit)
418427
{
419428
M3Result result = m3Err_none;
420429

421430
u16 slot;
422431

423-
if (AllocateSlot (o, & slot))
432+
if (AllocateSlots (o, & slot, i_type))
424433
{
425-
_ (Push (o, i_m3Type, slot));
434+
_ (Push (o, i_type, slot));
426435

427436
if (i_doEmit)
428437
EmitSlotOffset (o, slot);
@@ -433,19 +442,19 @@ _ (Push (o, i_m3Type, slot));
433442
}
434443

435444

436-
M3Result PushAllocatedSlotAndEmit (IM3Compilation o, u8 i_m3Type)
445+
M3Result PushAllocatedSlotAndEmit (IM3Compilation o, u8 i_type)
437446
{
438-
return _PushAllocatedSlotAndEmit (o, i_m3Type, true);
447+
return _PushAllocatedSlotAndEmit (o, i_type, true);
439448
}
440449

441450

442-
M3Result PushAllocatedSlot (IM3Compilation o, u8 i_m3Type)
451+
M3Result PushAllocatedSlot (IM3Compilation o, u8 i_type)
443452
{
444-
return _PushAllocatedSlotAndEmit (o, i_m3Type, false);
453+
return _PushAllocatedSlotAndEmit (o, i_type, false);
445454
}
446455

447456

448-
M3Result PushConst (IM3Compilation o, u64 i_word, u8 i_m3Type)
457+
M3Result PushConst (IM3Compilation o, u64 i_word, u8 i_type)
449458
{
450459
M3Result result = m3Err_none;
451460

@@ -459,7 +468,7 @@ M3Result PushConst (IM3Compilation o, u64 i_word, u8 i_m3Type)
459468
if (o->constants [i] == i_word)
460469
{
461470
location = o->firstConstSlotIndex + i;
462-
_ (Push (o, i_m3Type, location));
471+
_ (Push (o, i_type, location));
463472
break;
464473
}
465474
}
@@ -471,13 +480,13 @@ _ (Push (o, i_m3Type, location));
471480
o->constants [numConstants] = i_word;
472481
location = o->constSlotIndex++;
473482

474-
_ (Push (o, i_m3Type, location));
483+
_ (Push (o, i_type, location));
475484
}
476485
else
477486
{
478487
_ (EmitOp (o, op_Const));
479488
EmitConstant64 (o, i_word);
480-
_ (PushAllocatedSlotAndEmit (o, i_m3Type));
489+
_ (PushAllocatedSlotAndEmit (o, i_type));
481490
}
482491
}
483492

@@ -686,7 +695,9 @@ M3Result FindReferencedLocalWithinCurrentBlock (IM3Compilation o, u16 * o_pres
686695
{
687696
if (* o_preservedSlotIndex == i_localIndex)
688697
{
689-
if (not AllocateSlot (o, o_preservedSlotIndex))
698+
u8 localType = GetStackBottomType (o, i_localIndex);
699+
700+
if (not AllocateSlots (o, o_preservedSlotIndex, localType))
690701
_throw (m3Err_functionStackOverflow);
691702
}
692703
else
@@ -1239,7 +1250,7 @@ _ (ReadLEB_i7 (& reserved, & o->wasm, o->wasmEnd));
12391250

12401251
_ (EmitOp (o, op_MemCurrent));
12411252

1242-
_ (PushRegister (o, c_m3Type_i32)); // i32?
1253+
_ (PushRegister (o, c_m3Type_i32));
12431254

12441255
_catch: return result;
12451256
}
@@ -1257,7 +1268,7 @@ _ (Pop (o));
12571268

12581269
_ (EmitOp (o, op_MemGrow));
12591270

1260-
_ (PushRegister (o, c_m3Type_i32)); // i32?
1271+
_ (PushRegister (o, c_m3Type_i32));
12611272

12621273
_catch: return result;
12631274
}
@@ -1760,9 +1771,9 @@ const M3OpInfo c_operations [] =
17601771
M3OP( "i32.div_u", -1, i_32, d_binOpList (u32, Divide) ), // 0x6e
17611772
M3OP( "i32.rem_s", -1, i_32, d_binOpList (i32, Remainder) ), // 0x6f
17621773
M3OP( "i32.rem_u", -1, i_32, d_binOpList (u32, Remainder) ), // 0x70
1763-
M3OP( "i32.and", -1, i_32, d_commutativeBinOpList (u64, And) ), // 0x71
1764-
M3OP( "i32.or", -1, i_32, d_commutativeBinOpList (u64, Or) ), // 0x72
1765-
M3OP( "i32.xor", -1, i_32, d_commutativeBinOpList (u64, Xor) ), // 0x73
1774+
M3OP( "i32.and", -1, i_32, d_commutativeBinOpList (u32, And) ), // 0x71
1775+
M3OP( "i32.or", -1, i_32, d_commutativeBinOpList (u32, Or) ), // 0x72
1776+
M3OP( "i32.xor", -1, i_32, d_commutativeBinOpList (u32, Xor) ), // 0x73
17661777
M3OP( "i32.shl", -1, i_32, d_binOpList (u32, ShiftLeft) ), // 0x74
17671778
M3OP( "i32.shr_s", -1, i_32, d_binOpList (i32, ShiftRight) ), // 0x75
17681779
M3OP( "i32.shr_u", -1, i_32, d_binOpList (u32, ShiftRight) ), // 0x76

src/m3_compile.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#include "m3_code.h"
1212
#include "m3_exec_defs.h"
1313

14+
#if defined(__cplusplus)
15+
extern "C" {
16+
#endif
1417

1518
enum
1619
{
@@ -164,6 +167,8 @@ M3Result Compile_Function (IM3Function io_function);
164167
bool PeekNextOpcode (IM3Compilation o, u8 i_opcode);
165168
u16 GetMaxExecSlot (IM3Compilation o);
166169

167-
//M3Result Optimize_ConstOp (IM3Compilation o, u64 i_word, u8 i_waType);
170+
#if defined(__cplusplus)
171+
}
172+
#endif
168173

169174
#endif // m3_compile_h

src/m3_core.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ void m3NotImplemented() {
2222
}
2323

2424
M3_WEAK
25-
void m3Yield ()
25+
M3Result m3_Yield ()
2626
{
27+
return m3Err_none;
2728
}
2829

2930
#if d_m3FixedHeap

src/m3_core.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
#include "wasm3.h"
1818
#include "m3_config.h"
1919

20+
#if defined(__cplusplus)
21+
extern "C" {
22+
#endif
23+
2024
#if !defined(d_m3ShortTypesDefined)
2125
typedef double f64;
2226
typedef float f32;
@@ -193,8 +197,6 @@ size_t m3StackGetMax ();
193197
void m3Abort (const char* message);
194198
void m3NotImplemented (void);
195199

196-
void m3Yield (void);
197-
198200
M3Result m3Malloc (void ** o_ptr, size_t i_size);
199201
void * m3Realloc (void * i_ptr, size_t i_newSize, size_t i_oldSize);
200202
void m3Free_impl (void * o_ptr);
@@ -225,4 +227,8 @@ size_t SPrintArg (char * o_string, size_t i_n, m3stack_t i_sp
225227

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

230+
#if defined(__cplusplus)
231+
}
232+
#endif
233+
228234
#endif // m3_core_h

src/m3_emit.h

+8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
#include "m3_compile.h"
1212

13+
#if defined(__cplusplus)
14+
extern "C" {
15+
#endif
16+
1317
M3Result BridgeToNewPageIfNecessary (IM3Compilation o);
1418
M3Result EnsureCodePageNumLines (IM3Compilation o, u32 i_numLines);
1519

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

2327
pc_t GetPC (IM3Compilation o);
2428

29+
#if defined(__cplusplus)
30+
}
31+
#endif
32+
2533
#endif // m3_emit_h

src/m3_env.c

+4-10
Original file line numberDiff line numberDiff line change
@@ -455,21 +455,15 @@ M3Result InitStartFunc (IM3Module io_module)
455455
{
456456
M3Result result = m3Err_none;
457457

458-
if (io_module->startFunction >= 0) {
459-
if ((u32)io_module->startFunction >= io_module->numFunctions) {
460-
return "start function index out of bounds";
461-
}
462-
IM3Function function = &io_module->functions [io_module->startFunction];
463-
if (not function) {
464-
return "start function not found";
465-
}
458+
if (io_module->startFunction >= 0)
459+
{
460+
IM3Function function = & io_module->functions [io_module->startFunction];
466461

467462
if (not function->compiled)
468463
{
469464
_ (Compile_Function (function));
470-
if (result)
471-
function = NULL;
472465
}
466+
473467
_ (m3_Call(function));
474468

475469
io_module->startFunction = -1;

src/m3_env.h

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
#include "m3_exec.h"
1414
#include "m3_compile.h"
1515

16+
#if defined(__cplusplus)
17+
extern "C" {
18+
#endif
1619

1720
typedef struct M3FuncType
1821
{
@@ -252,5 +255,8 @@ void ReleaseCodePage (IM3Runtime io_runtime,
252255

253256
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, ...);
254257

258+
#if defined(__cplusplus)
259+
}
260+
#endif
255261

256262
#endif // m3_env_h

0 commit comments

Comments
 (0)