-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwip.cpp
5458 lines (4646 loc) · 119 KB
/
wip.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// A WebAssembly codebase by Jay Krell
//
// https://webassembly.github.io/spec/core/binary/index.html
// https://webassembly.github.io/spec/core/_download/WebAssembly.pdf
#define _CRT_SECURE_NO_WARNINGS 1
#include <assert.h>
#include <algorithm>
#include <vector>
#include <string>
#include <stdio.h>
using std::min;
using std::max;
using std::vector;
using std::string;
#include "w3.h"
#include "w3-2.h"
extern const float wasm_hugef = 1.0e30F;
extern const double wasm_huged = 1.0e300;
#define _DARWIN_USE_64_BIT_INODE 1
//#define __DARWIN_ONLY_64_BIT_INO_T 1
// TODO cmake
// TODO big endian and packed I/O
//#define _LARGEFILE_SOURCE
//#define _LARGEFILE64_SOURCE
#if _MSC_VER
#include <intrin.h>
#endif
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic ignored "-Wunused-const-variable"
#pragma GCC diagnostic ignored "-Wunused-function"
#endif
#ifndef _ISOC99_SOURCE
#define _ISOC99_SOURCE
#endif
#if _WIN32
#define NOMINMAX 1
#include <io.h>
#include <windows.h>
#define DebugBreak __debugbreak
#else
#define IsDebuggerPresent() (0)
#define __debugbreak() ((void)0)
#define DebugBreak() ((void)0)
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#endif
#if _MSC_VER
#include <malloc.h> // for _alloca
#endif
#if 0
void
AssertFailed (const char* file, int line, const char* expr)
{
fprintf (stderr, "Assert failed: %s(%d):%s\n", file, line, expr);
#if _WIN32
DebugBreak();
#else
abort ();
#endif
}
#define Assert(expr) ((void)((expr) || AssertFailed (__FILE__, __LINE__, #expr)))
#endif
void
ThrowString (const std::string& a)
{
//fprintf (stderr, "%s\n", a.c_str ());
throw a + "\n";
//abort ();
}
std::string
StringFormatVa (const char* format, va_list va);
std::string
StringFormat (const char* format, ...)
{
va_list va;
va_start (va, format);
std::string a = StringFormatVa (format, va);
va_end (va);
return a;
}
void
ThrowInt (int i, const char* a)
{
ThrowString (StringFormat ("error 0x%08X %s", i, a));
}
void
ThrowErrno (const char* a)
{
ThrowInt (errno, a);
}
#if _WIN32
void
throw_Win32Error (int err, const char* a)
{
ThrowInt (err, a);
}
void
throw_GetLastError (const char* a)
{
ThrowInt ((int)GetLastError (), a);
}
#endif
#ifdef _WIN64
#define FORMAT_SIZE "I64"
#define long_t __int64 // aka ptrdiff
#else
#define FORMAT_SIZE "l"
#define long_t long // aka ptrdiff
#endif
#if _MSC_VER
#pragma warning (disable:4777) // printf maybe wrong for other platforms
#pragma warning (push)
#pragma warning (disable:4996) // _vsnprintf dangerous
#endif
size_t
string_vformat_length (const char* format, va_list va)
{
#if _MSC_VER
return 2 + _vscprintf (format, va);
#else
return 2 + vsnprintf (0, 0, format, va);
#endif
}
#if _MSC_VER >= 1200
#pragma warning (pop)
#endif
void
AssertFailedFormat (const char* condition, const std::string& extra)
{
fputs (("AssertFailed:" + std::string (condition) + ":" + extra + "\n").c_str (), stderr);
//Assert (0);
//abort ();
#if _WIN32 // TODO
if (IsDebuggerPresent ()) __debugbreak ();
#endif
ThrowString ("AssertFailed:" + std::string (condition) + ":" + extra);
}
void
AssertFailed (const char* expr)
{
fprintf (stderr, "AssertFailed:%s\n", expr);
#if _WIN32 // TODO
if (IsDebuggerPresent ()) __debugbreak ();
#endif
assert (0);
abort ();
}
#define Assert(x) ((x) || ( AssertFailed (#x), (int)0))
#define AssertFormat(x, extra) ((x) || (AssertFailedFormat (#x, StringFormat extra), 0))
//template <class T> void WasmStdConstructN (T* a, size_t n) { for (size_t i = 0; i < n; ++i) new (a++) T (); }
//template <class T> void WasmStdCopyConstructNtoN (T* to, T* from, size_t n) { for (size_t i = 0; i < n; ++i) new (to++) T (*from++); }
//template <class T> void WasmStdCopyConstruct1toN (T* to, const T& from, size_t n) { for (size_t i = 0; i < n; ++i) new (to++) T (from); }
//template <class T> void WasmStdCopyConstruct1 (T& to, const T& from) { WasmStdCopyConstruct1toN (&to, from, 1u); }
struct w3FuncAddr // TODO
{
};
struct w3TableAddr // TODO
{
};
struct w3MemAddr // TODO
{
};
struct w3GlobalAddr // TODO
{
};
// This should probabably be combined with w3ResultType, and called w3Tag.
enum w3ValueType : uint8_t
{
Tag_i32 = 0x7F,
Tag_i64 = 0x7E,
Tag_f32 = 0x7D,
Tag_f64 = 0x7C,
};
std::string
StringFormatVa (const char* format, va_list va)
{
// Some systems, including Linux/amd64, cannot consume a
// va_list multiple times. It must be copied first.
// Passing the parameter twice does not work.
#if !_WIN32
va_list va2;
#ifdef __va_copy
__va_copy (va2, va);
#else
va_copy (va2, va); // C99
#endif
#endif
std::vector <char> s (string_vformat_length (format, va));
#if _WIN32
_vsnprintf (&s [0], s.size (), format, va);
#else
vsnprintf (&s [0], s.size (), format, va2);
#endif
return &s [0];
}
const uint32_t PageShift = 16;
#define NotImplementedYed() (AssertFormat (0, ("not yet implemented %s 0x%08X ", __func__, __LINE__)))
uint32_t
GetUint16LE (const void* a)
{
uint8_t* b = (uint8_t*)a;
return ((b [1]) << 8) | (uint32_t)b [0];
}
uint32_t
GetUint32LE (const void* a)
{
return (GetUint16LE ((char*)a + 2) << 16) | GetUint16LE (a);
}
// C++98 workaround for what C++11 offers.
struct explicit_operator_bool
{
typedef void (explicit_operator_bool::*T) () const;
void True () const;
};
typedef void (explicit_operator_bool::*bool_type) () const;
#if _WIN32
struct w3Handle
{
// TODO w3Handle vs. win32file_t, etc.
uint64_t get_file_size (const char* file_name = "")
{
DWORD hi = 0;
DWORD lo = GetFileSize (h, &hi);
if (lo == INVALID_FILE_SIZE)
{
DWORD err = GetLastError ();
if (err != NO_ERROR)
throw_Win32Error ((int)err, StringFormat ("GetFileSize (%s)", file_name).c_str ());
}
return (((uint64_t)hi) << 32) | lo;
}
void* h;
w3Handle (void* a) : h (a) { }
w3Handle () : h (0) { }
void* get () { return h; }
bool valid () const { return static_valid (h); }
static bool static_valid (void* h) { return h && h != INVALID_HANDLE_VALUE; }
operator void* () { return get (); }
static void static_cleanup (void* h)
{
if (!static_valid (h)) return;
CloseHandle (h);
}
void* detach ()
{
void* const a = h;
h = 0;
return a;
}
void cleanup ()
{
static_cleanup (detach ());
}
w3Handle& operator= (void* a)
{
if (h == a) return *this;
cleanup ();
h = a;
return *this;
}
#if 0 // C++11
explicit operator bool () { return valid (); } // C++11
#else
operator explicit_operator_bool::T () const
{
return valid () ? &explicit_operator_bool::True : NULL;
}
#endif
bool operator ! () { return !valid (); }
~w3Handle ()
{
if (valid ()) CloseHandle (h);
h = 0;
}
};
#endif
struct w3Fd
{
int fd;
#if !_WIN32
uint64_t get_file_size (const char* file_name = "")
{
#if __CYGWIN__
struct stat st = { 0 }; // TODO test more systems
if (fstat (fd, &st))
#else
struct stat64 st = { 0 }; // TODO test more systems
if (fstat64 (fd, &st))
#endif
ThrowErrno (StringFormat ("fstat (%s)", file_name).c_str ());
return st.st_size;
}
#endif
#if 0 // C++11
explicit operator bool () { return valid (); } // C++11
#else
operator explicit_operator_bool::T () const
{
return valid () ? &explicit_operator_bool::True : NULL;
}
#endif
bool operator ! () { return !valid (); }
operator int () { return get (); }
static bool static_valid (int fd) { return fd != -1; }
int get () const { return fd; }
bool valid () const { return static_valid (fd); }
static void static_cleanup (int fd)
{
if (!static_valid (fd)) return;
#if _WIN32
_close (fd);
#else
close (fd);
#endif
}
int detach ()
{
int const a = fd;
fd = -1;
return a;
}
void cleanup ()
{
static_cleanup (detach ());
}
w3Fd (int a = -1) : fd (a) { }
w3Fd& operator = (int a)
{
if (fd == a) return *this;
cleanup ();
fd = a;
return *this;
}
~w3Fd ()
{
cleanup ();
}
};
struct w3MemoryMappedFile
{
// TODO allow for redirection to built-in data (i.e. filesystem emulation with builtin BCL)
// TODO allow for systems that must read, not mmap
void* base;
size_t size;
#if _WIN32
w3Handle file;
#else
w3Fd file;
#endif
w3MemoryMappedFile () : base (0), size (0) { }
~w3MemoryMappedFile ()
{
if (!base)
return;
#if _WIN32
UnmapViewOfFile (base);
#else
munmap (base, size);
#endif
base = 0;
}
void read (const char* a)
{
#if _WIN32
#ifndef FILE_SHARE_DELETE // ifndef required due to Watcom 4 vs. 4L.
#define FILE_SHARE_DELETE 0x00000004 // missing in older headers
#endif
file = CreateFileA (a, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (!file) throw_GetLastError (StringFormat ("CreateFileA (%s)", a).c_str ());
// FIXME check for size==0 and >4GB.
size = (size_t)file.get_file_size (a);
w3Handle h2 = CreateFileMappingW (file, 0, PAGE_READONLY, 0, 0, 0);
if (!h2) throw_GetLastError (StringFormat ("CreateFileMapping (%s)", a).c_str ());
base = MapViewOfFile (h2, FILE_MAP_READ, 0, 0, 0);
if (!base)
throw_GetLastError (StringFormat ("MapViewOfFile (%s)", a).c_str ());
#else
file = open (a, O_RDONLY);
if (!file) ThrowErrno (StringFormat ("open (%s)", a).c_str ());
// FIXME check for size==0 and >4GB.
size = (size_t)file.get_file_size (a);
base = mmap (0, size, PROT_READ, MAP_PRIVATE, file, 0);
if (base == MAP_FAILED)
ThrowErrno (StringFormat ("mmap (%s)", a).c_str ());
#endif
}
};
struct w3stream
{
virtual ~w3stream() { }
virtual void write (const void* bytes, size_t size) = 0;
void prints (const char* a) { write (a, strlen (a)); }
void prints (const std::string& a) { write (a.c_str (), a.length ()); }
void printc (char a) { write (&a, 1); }
void printf (const char* format, ...)
{
va_list va;
va_start (va, format);
printv (format, va);
va_end (va);
}
void printv (const char* format, va_list va)
{
prints (StringFormatVa (format, va));
}
};
struct w3stdout_stream : w3stream
{
virtual void write (const void* bytes, size_t size)
{
fflush (stdout);
const char* pc = (const char*)bytes;
while (size > 0)
{
// TODO: Use smaller number for Win32 console?
uint32_t const n = (uint32_t)std::min (size, (((size_t)1) << 30));
#ifdef _WIN32
::_write (_fileno (stdout), pc, n);
#else
::write (fileno (stdout), pc, n);
#endif
size -= n;
pc += n;
}
}
};
struct w3stderr_stream : w3stream
{
// TODO: Refactor with stdout.
virtual void write (const void* bytes, size_t size)
{
fflush (stderr);
const char* pc = (const char*)bytes;
while (size > 0)
{
uint32_t const n = (uint32_t)std::min (size, (((size_t)1) << 30));
#ifdef _WIN32
::_write (_fileno (stderr), pc, n);
#else
::write (fileno (stderr), pc, n);
#endif
size -= n;
pc += n;
}
}
};
uint8_t
read_byte (uint8_t** cursor, const uint8_t* end)
{
if (*cursor >= end)
ThrowString (StringFormat ("malformed %d", __LINE__)); // UNDONE context (move to module or section)
return *(*cursor)++;
}
uint64_t
read_varuint64 (uint8_t** cursor, const uint8_t* end)
{
uint64_t result = 0;
uint32_t shift = 0;
while (true)
{
const uint32_t byte = read_byte (cursor, end);
printf ("read_varuint64_2:%X\n", byte);
result |= (byte & 0x7F) << shift;
if ((byte & 0x80) == 0)
return result;
shift += 7;
}
}
uint32_t
read_varuint32 (uint8_t** cursor, const uint8_t* end)
{
uint32_t result = 0;
uint32_t shift = 0;
while (true)
{
const uint32_t byte = read_byte (cursor, end);
result |= (byte & 0x7F) << shift;
if ((byte & 0x80) == 0)
return result;
shift += 7;
}
}
uint8_t
read_varuint7 (uint8_t** cursor, const uint8_t* end)
{
const uint8_t result = read_byte (cursor, end);
if (result & 0x80)
ThrowString (StringFormat ("malformed %d", __LINE__)); // UNDONE context (move to module or section)
return result;
}
int64_t
read_varint64 (uint8_t** cursor, const uint8_t* end)
{
int64_t result = 0;
uint32_t shift = 0;
uint32_t size = 64;
uint32_t byte = 0;
do
{
byte = read_byte (cursor, end);
result |= (byte & 0x7F) << shift;
shift += 7;
} while (byte & 0x80);
// sign bit of byte is second high order bit (0x40)
if ((shift < size) && (byte & 0x40))
result |= (~0 << shift); // sign extend
return result;
}
int32_t
read_varint32 (uint8_t** cursor, const uint8_t* end)
{
int32_t result = 0;
uint32_t shift = 0;
uint32_t size = 32;
uint32_t byte = 0;
do
{
byte = read_byte (cursor, end);
result |= (byte & 0x7F) << shift;
shift += 7;
} while (byte & 0x80);
// sign bit of byte is second high order bit (0x40)
if ((shift < size) && (byte & 0x40))
result |= (~0 << shift); // sign extend
return result;
}
typedef enum Type : uint8_t
{
Type_none,
Type_bool, // i32
Type_any, // often has some constraints
Type_i32 = 0x7F,
Type_i64 = 0x7E,
Type_f32 = 0x7D,
Type_f64 = 0x7C,
} Type;
union Value
{
int32_t i32;
uint32_t u32;
uint64_t u64;
int64_t i64;
float f32;
double f64;
};
struct TaggedValue
{
w3ValueType tag;
union
{
int32_t i32;
uint32_t u32;
uint64_t u64;
int64_t i64;
float f32;
double f64;
Value value;
};
};
const char*
TypeToStringCxx (int tag)
{
switch (tag)
{
case Type_none: return "void";
case Type_bool: return "bool";
case Type_any: return "void*"; // union?
case ResultType_i32: return "int";
case ResultType_i64: return "int64_t";
case ResultType_f32: return "float";
case ResultType_f64: return "double";
case ResultType_empty: return "void";
}
return "unknown";
}
const char*
TypeToString (int tag)
{
switch (tag)
{
case Type_none: return "none(0)";
case Type_bool: return "bool(1)";
case Type_any: return "any(2)";
case ResultType_i32: return "i32(7F)";
case ResultType_i64: return "i64(7E)";
case ResultType_f32: return "f32(7D)";
case ResultType_f64: return "f64(7C)";
case ResultType_empty: return "empty(40)";
}
return "unknown";
}
typedef enum w3TableElementType : uint32_t
{
w3TableElementType_funcRef = 0x70,
} w3TableElementType;
typedef enum LimitsTag // specific to tabletype?
{
w3LimitsTag_min = 0,
w3Limits_minMax = 1,
} w3LimitsTag;
struct w3Limits
{
// TODO size_t? null?
w3Limits () : min (0), max (0), hasMax (false) { }
uint32_t min;
uint32_t max;
bool hasMax;
};
const uint32_t FunctionTypeTag = 0x60;
struct w3TableType
{
w3TableType () : elementType ((w3TableElementType)0) { }
w3TableElementType elementType;
w3Limits limits;
bool operator < (const w3TableType&) const; // workaround old compiler
bool operator == (const w3TableType&) const; // workaround old compiler
bool operator != (const w3TableType&) const; // workaround old compiler
};
// Globals are mutable or constant.
typedef enum w3Mutable
{
w3Mutable_constant = 0, // aka false
w3Mutable_variable = 1, // aka true
} w3Mutable;
struct w3Runtime;
struct w3Stack;
struct w3StackValue;
struct w3ModuleInstance;
struct w3Module;
struct w3Section;
// The stack shall use _alloca in a non-recursive interpreter loop.
// This requires some care and macros. Macros that reference locals.
// The interpreter is likely to mostly dispatch to function pointers.
// Some pieces must not be in those functions. They can return values
// to the calling loop and the loop can do some of the work.
//
// In time, the function pointers might be case labels instead.
// However decomposition into separate functions is more elegant, if not less efficient.
//
// Such decomposition will also be good for conversion to JIT, LLVM, C++, etc.
// It is only interpreter, perhaps, that has overwhelming efficiency concern.
//
// w3StackValue initial_stack[1];
// int stack_depth;
// w3StackValue* stack = initial_stack;
// w3StackValue* min_stack = initial_stack;
typedef struct w3FunctionType w3FunctionType;
typedef struct w3Function w3Function;
typedef struct w3Code w3Code;
typedef struct w3Frame w3Frame; // work in progress
typedef struct w3DecodedInstruction w3DecodedInstruction;
typedef enum w3StackTag
{
StackTag_Value = 1, // i32, i64, f32, f64
StackTag_Label, // branch target
StackTag_Frame, // return address + locals + params
} w3StackTag;
const char*
StackTagToString (w3StackTag tag)
{
switch (tag)
{
case StackTag_Value: return "Value(1)";
case StackTag_Label: return "Label(2)";
case StackTag_Frame: return "w3Frame(3)";
}
return "unknown";
}
typedef struct w3LabelValue
{
size_t arity, continuation;
} w3LabelValue;
struct w3StackValue : w3StackValueZeroInit
{
void Init ();
w3StackValue()
{
Init ();
}
w3StackValue (w3StackTag t)
{
Init ();
tag = t;
}
w3StackValue (w3ValueType t)
{
Init ();
tag = StackTag_Value;
value.tag = t;
}
w3StackValue (TaggedValue t)
{
Init ();
tag = StackTag_Value;
value = t;
}
w3StackValue (w3Frame* f)
{
Init ();
tag = StackTag_Frame;
// frame = f;
}
bool operator < (const w3StackValue&) const; // workaround old compiler
bool operator == (const w3StackValue&) const; // workaround old compiler
bool operator != (const w3StackValue&) const; // workaround old compiler
};
// TODO consider a vector instead, but it affects frame.locals staying valid across push/pop
//typedef std::deque <w3StackValue> w3StackBaseBase;
typedef std::vector <w3StackValue> w3StackBaseBase;
struct w3StackBase : private w3StackBaseBase
{
typedef w3StackBaseBase base;
typedef w3StackBaseBase::iterator iterator;
w3StackValue& back () { return base::back (); }
w3StackValue& front () { return base::front (); }
iterator begin () { return base::begin (); }
iterator end () { return base::end (); }
bool empty () const { return base::empty (); }
void resize (size_t newsize) { base::resize (newsize); }
size_t size () { return base::size (); }
w3StackValue& operator [ ] (size_t index) { return base::operator [ ] (index); }
void push (const w3StackValue& a)
{ // While ultimately a stack of values, labels, and frames, values dominate,
// so the usage is made convenient for them.
push_back (a);
}
void pop ()
{
pop_back ();
}
w3StackValue& top ()
{ // While ultimately a stack of values, labels, and frames, values dominate,
// so the usage is made convenient for them.
return back ();
}
};
struct w3Interp;
struct w3Frame
{
// FUTURE spec return_arity
size_t function_index {}; // replace with pointer?
w3ModuleInstance* module_instance {};
w3Module* module {};
// w3Frame* next; // TODO remove this; it is on stack
w3Code* code {};
size_t param_count {};
size_t local_only_count {};
size_t param_and_local_count {};
w3ValueType* local_only_types {};
w3ValueType* param_types {};
w3FunctionType* function_type {};
// TODO locals/params
// This should just be stack pointer, to another stack,
// along with type information (module->module->locals_types[])
w3Interp* interp {};
size_t locals {}; // index in stack to start of params and locals, params first
w3StackValue& Local (size_t index);
};
// work in progress
struct w3Stack : private w3StackBase
{
w3Stack () { }
// old compilers lack using.
typedef w3StackBase base;
typedef base::iterator iterator;
void pop () { base::pop (); }
w3StackValue& top () { return base::top (); }
w3StackValue& back () { return base::back (); }
w3StackValue& front () { return base::front (); }
iterator begin () { return base::begin (); }
iterator end () { return base::end (); }
bool empty () const { return base::empty (); }
void resize (size_t newsize) { base::resize (newsize); }
size_t size () { return base::size (); }
w3StackValue& operator [ ] (size_t index) { return base::operator [ ] (index); }
void reserve (size_t n)
{
// TODO
}
// While ultimately a stack of values, labels, and frames, values dominate.
w3ValueType& tag (w3ValueType tag)
{
AssertTopIsValue ();
w3StackValue& t = top ();
AssertFormat (t.value.tag == tag, ("%X %X", t.value.tag, tag));
return t.value.tag;
}
w3ValueType& tag ()
{
AssertTopIsValue ();
w3StackValue& t = top ();
return t.value.tag;
}
Value& value ()
{
AssertTopIsValue ();
w3StackValue& t = top ();
return t.value.value;
}
Value& value (w3ValueType tag)
{
AssertTopIsValue ();
w3StackValue& t = top ();
AssertFormat (t.value.tag == tag, ("%X %X", t.value.tag, tag));
return t.value.value;
}
void pop_label ()
{
if (size () < 1 || top ().tag != StackTag_Label)
DumpStack ("AssertTopIsValue");
AssertFormat (size () >= 1, ("%" FORMAT_SIZE "X", size ()));
AssertFormat (top ().tag == StackTag_Label, ("%X %X", top ().tag, StackTag_Label));
pop ();
}
void pop_value ()
{
AssertTopIsValue ();
//int t = tag ();
pop ();
//printf ("pop_value tag:%s depth:%" FORMAT_SIZE "X\n", TypeToString (t), size ());
}
void push_value (const w3StackValue& value)
{
AssertFormat (value.tag == StackTag_Value, ("%X %X", value.tag, StackTag_Value));
push (value);
//printf ("push_value tag:%s value:%X depth:%" FORMAT_SIZE "X\n", TypeToString (value.value.tag), value.value.value.i32, size ());
}
void push_label (const w3StackValue& value)
{
AssertFormat (value.tag == StackTag_Label, ("%X %X", value.tag, StackTag_Label));
push (value);
//printf ("push_label depth:%" FORMAT_SIZE "X\n", size ());
}
void push_frame (const w3StackValue& value)
{
AssertFormat (value.tag == StackTag_Frame, ("%X %X", value.tag, StackTag_Frame));
push (value);
//printf ("push_frame depth:%" FORMAT_SIZE "X\n", size ());
}
// type specific pushers
void push_i32 (int32_t i)
{
w3StackValue value (Tag_i32);
value.value.value.i32 = i;
push_value (value);
}
void push_i64 (int64_t i)
{