-
Notifications
You must be signed in to change notification settings - Fork 63
/
vm.d
1717 lines (1410 loc) · 41.2 KB
/
vm.d
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
/*****************************************************************************
*
* Higgs JavaScript Virtual Machine
*
* This file is part of the Higgs project. The project is distributed at:
* https://github.com/maximecb/Higgs
*
* Copyright (c) 2012-2014, Maxime Chevalier-Boisvert. All rights reserved.
*
* This software is licensed under the following license (Modified BSD
* License):
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*****************************************************************************/
module runtime.vm;
import core.memory;
import std.c.string;
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.stdint;
import std.typecons;
import std.path;
import std.file;
import options;
import stats;
import util.misc;
import parser.parser;
import parser.ast;
import ir.ir;
import ir.ast;
import runtime.layout;
import runtime.string;
import runtime.object;
import runtime.gc;
import jit.codeblock;
import jit.jit;
/**
Run-time error
*/
class RunError : Error
{
/// Associated virtual machine
VM vm;
/// Exception value
ValuePair excVal;
/// Error constructor name
string name;
/// Error message
string message;
/// Stack trace
IRInstr[] trace;
this(VM vm, ValuePair excVal, IRInstr[] trace)
{
this.vm = vm;
this.excVal = excVal;
this.trace = trace;
this.name = "run-time error";
if (excVal.tag is Tag.OBJECT)
{
auto errName = getProp(
vm,
excVal,
"name"w
);
this.name = errName.toString;
auto msgStr = getProp(
vm,
excVal,
"message"w
);
this.message = msgStr.toString;
}
else
{
this.message = excVal.toString;
}
super(toString());
}
override string toString()
{
string str = name ~ ": " ~ message;
foreach (instr; trace)
{
auto fun = instr.block.fun;
auto pos = instr.srcPos? instr.srcPos:fun.ast.pos;
str ~= "\n" ~ fun.getName ~ " (" ~ to!string(pos) ~ ")";
}
return str;
}
}
/**
Memory word union
*/
union Word
{
static Word int32v(int32 i) { Word w; w.int64Val = 0; w.int32Val = i; return w; }
static Word int64v(int64 i) { Word w; w.int64Val = i; return w; }
static Word uint32v(uint32 i) { Word w; w.int64Val = 0; w.uint32Val = i; return w; }
static Word uint64v(uint64 i) { Word w; w.uint64Val = i; return w; }
static Word float64v(float64 f) { Word w; w.floatVal = f; return w; }
static Word refv(refptr p) { Word w; w.ptrVal = p; return w; }
static Word ptrv(rawptr p) { Word w; w.ptrVal = p; return w; }
static Word funv(IRFunction f) { Word w; w.funVal = f; return w; }
int8 int8Val;
int16 int16Val;
int32 int32Val;
int64 int64Val;
uint8 uint8Val;
uint16 uint16Val;
uint32 uint32Val;
uint64 uint64Val;
float64 floatVal;
refptr refVal;
rawptr ptrVal;
IRFunction funVal;
IRInstr insVal;
ObjShape shapeVal;
}
unittest
{
assert (
Word.sizeof == rawptr.sizeof,
"word size does not match pointer size"
);
}
/// Type tag values
enum Tag : ubyte
{
CONST = 0,
INT32,
INT64,
FLOAT64,
RAWPTR,
RETADDR,
FUNPTR,
SHAPEPTR,
// GC heap pointer tags
REFPTR,
OBJECT,
ARRAY,
CLOSURE,
STRING,
ROPE
}
/**
Test if a given tag is a heap pointer
*/
bool isHeapPtr(Tag tag)
{
switch (tag)
{
case Tag.REFPTR:
case Tag.OBJECT:
case Tag.ARRAY:
case Tag.CLOSURE:
case Tag.STRING:
case Tag.ROPE:
return true;
case Tag.CONST:
case Tag.INT32:
case Tag.INT64:
case Tag.FLOAT64:
case Tag.RAWPTR:
case Tag.RETADDR:
case Tag.FUNPTR:
case Tag.SHAPEPTR:
return false;
default:
assert (false);
}
}
/**
Test if a tag is an object of some kind
*/
bool isObject(Tag tag)
{
switch (tag)
{
case Tag.OBJECT:
case Tag.ARRAY:
case Tag.CLOSURE:
return true;
default:
return false;
}
}
/**
Produce a string representation of a type tag
*/
string tagToString(Tag tag)
{
// Switch on the type tag
switch (tag)
{
case Tag.INT32: return "int32";
case Tag.INT64: return "int64";
case Tag.FLOAT64: return "float64";
case Tag.RAWPTR: return "rawptr";
case Tag.RETADDR: return "retaddr";
case Tag.CONST: return "const";
case Tag.FUNPTR: return "funptr";
case Tag.SHAPEPTR: return "shapeptr";
case Tag.REFPTR: return "refptr";
case Tag.OBJECT: return "object";
case Tag.ARRAY: return "array";
case Tag.CLOSURE: return "closure";
case Tag.STRING: return "string";
case Tag.ROPE: return "rope";
default:
assert (false, "unsupported type tag");
}
}
/**
Test if a reference points to an object of a given layout
*/
bool refIsLayout(refptr ptr, uint32 layoutId)
{
return (ptr !is null && obj_get_header(ptr) == layoutId);
}
/// Word and tag pair
struct ValuePair
{
Word word;
Tag tag;
this(Word word, Tag tag)
{
this.word = word;
this.tag = tag;
}
this(refptr ptr, Tag tag)
{
this.word.ptrVal = ptr;
this.tag = tag;
}
this(int32 int32Val)
{
this.word = Word.int32v(int32Val);
this.tag = Tag.INT32;
}
this(IRFunction fun)
{
this.word.funVal = fun;
this.tag = Tag.FUNPTR;
}
/**
Test if a value is an object of a given layout
*/
bool isLayout(uint32 layoutId)
{
return (
tag is Tag.REFPTR &&
refIsLayout(word.ptrVal, layoutId)
);
}
/**
Produce a string representation of a value pair
*/
string toString()
{
// Switch on the type tag
switch (tag)
{
case Tag.INT32:
return to!string(word.int32Val);
case Tag.FLOAT64:
if (word.floatVal != word.floatVal)
return "NaN";
if (word.floatVal == 1.0/0)
return "Infinity";
if (word.floatVal == -1.0/0)
return "-Infinity";
return format("%f", word.floatVal);
case Tag.RAWPTR:
if (word.ptrVal is null)
return "nullptr";
return to!string(word.ptrVal);
case Tag.RETADDR:
return to!string(word.ptrVal);
case Tag.CONST:
if (this == TRUE)
return "true";
if (this == FALSE)
return "false";
if (this == UNDEF)
return "undefined";
assert (
false,
"unsupported constant " ~ to!string(word.uint64Val)
);
case Tag.FUNPTR:
return "funptr";
case Tag.REFPTR:
if (this == NULL)
return "null";
if (ptrValid(word.ptrVal) is false)
return "invalid refptr";
if (isLayout(LAYOUT_OBJ))
return "object";
if (isLayout(LAYOUT_CLOS))
return "function";
if (isLayout(LAYOUT_ARR))
return "array";
return "refptr";
case Tag.OBJECT:
return "object";
case Tag.ARRAY:
if (ptrValid(word.ptrVal) is false)
return "invalid array ptr";
auto len = getArrLen(word.ptrVal);
auto tbl = getArrTbl(word.ptrVal);
auto output = "[";
for (uint32_t i = 0; i < len; ++i)
{
auto elWord = Word.uint64v(arrtbl_get_word(tbl, i));
auto elTag = cast(Tag)arrtbl_get_tag(tbl, i);
output ~= ValuePair(elWord, elTag).toString;
if (i < len - 1)
output ~= ",";
}
return output ~ "]";
case Tag.CLOSURE:
return "closure";
case Tag.STRING:
return extractStr(word.ptrVal);
case Tag.ROPE:
return ropeToStr(word.ptrVal);
default:
assert (false, "unsupported value type");
}
}
refptr ptr()
{
return word.ptrVal;
}
}
// Note: low byte is set to allow for one byte immediate comparison
immutable NULL = ValuePair(Word(0x00), Tag.REFPTR);
immutable TRUE = ValuePair(Word(0x01), Tag.CONST);
immutable FALSE = ValuePair(Word(0x02), Tag.CONST);
immutable UNDEF = ValuePair(Word(0x03), Tag.CONST);
/// Stack size, 256K words (2MB)
immutable size_t STACK_SIZE = 2^^18;
/// Initial object heap size, release 128M, debug 16M bytes
version (release)
immutable size_t HEAP_INIT_SIZE = 2 ^^ 27;
else
immutable size_t HEAP_INIT_SIZE = 2 ^^ 24;
/// Initial link table size
immutable size_t LINK_TBL_INIT_SIZE = 16384;
/// Initial global object size
immutable size_t GLOBAL_OBJ_INIT_SIZE = 1024;
/// Initial executable heap size 16M bytes
immutable size_t EXEC_HEAP_INIT_SIZE = 2 ^^ 24;
/// Fraction of the executable heap reserved for stubs
immutable size_t EXEC_HEAP_STUB_FRAC = 8;
/// Initial subroutine heap size, 64K bytes
immutable size_t SUBS_HEAP_INIT_SIZE = 2 ^^ 16;
/// Global VM instance
VM vm = null;
/**
Virtual Machine (VM) instance
*/
class VM
{
/// Word stack
Word* wStack;
/// Type stack
Tag* tStack;
/// Word stack upper limit
Word* wUpperLimit;
/// Type stack upper limit
Tag* tUpperLimit;
/// Word stack pointer (stack top)
Word* wsp;
/// Tag stack pointer (stack top)
Tag* tsp;
/// Heap start pointer
ubyte* heapStart;
/// Heap size
size_t heapSize = HEAP_INIT_SIZE;
/// Heap upper limit
ubyte* heapLimit;
/// Allocation pointer
ubyte* allocPtr;
/// To-space heap pointers, for garbage collection
ubyte* toStart;
ubyte* toLimit;
ubyte* toAlloc;
/// Linked list of GC roots
GCRoot* firstRoot;
/// Set of weak references to functions referenced in the heap
/// To be cleaned up by the GC
IRFunction[void*] funRefs;
/// Set of functions found live by the GC during collection
IRFunction[void*] liveFuns;
/// Garbage collection count
size_t gcCount = 0;
/// Link table words
Word* wLinkTable;
/// Link table tags
Tag* tLinkTable;
/// Link table size
uint32 linkTblSize;
/// Free link table entries
uint32[] linkTblFree;
/// String table reference
refptr strTbl;
/// Empty object shape
ObjShape emptyShape;
/// Initial array shape
ObjShape arrayShape;
/// Object prototype object
ValuePair objProto;
/// Array prototype object
ValuePair arrProto;
/// Function prototype object
ValuePair funProto;
/// String prototype object
ValuePair strProto;
/// Global object reference
ValuePair globalObj;
/// Runtime error value (uncaught exceptions)
RunError runError;
/// Current instruction (set when calling into host code)
IRInstr curInstr;
/// Executable heap
CodeBlock execHeap;
/// Stub space start position
size_t stubStartPos;
/// Current stub space write position
size_t stubWritePos;
/// Subroutine heap
CodeBlock subsHeap;
/// Map of return addresses to return entries
RetEntry[CodePtr] retAddrMap;
/// List of code fragments, in memory order
CodeFragment[] fragList;
/// Queue of block versions to be compiled
CodeFragment[] compQueue;
/// List of references to code fragments to be linked
FragmentRef[] refList;
/// Function entry stub
EntryStub entryStub;
/// Generic branch target stubs
BranchStub[] branchStubs;
/// Shape lookup fallback subroutine
CodePtr defShapeSub;
/// Space to save registers when calling into hosted code
Word* regSave;
/**
Initialize or reinitialize the global VM object
*/
static void init(bool loadRuntime = true, bool loadStdLib = true)
{
assert (
!(loadStdLib && !loadRuntime),
"cannot load stdlib without loading runtime"
);
// If a VM object was already created
if (vm !is null)
{
// Explicitly free the VM object and its resources
VM.free();
}
// Allocate the global VM object
vm = new VM();
with (vm)
{
// Allocate the word stack
wStack = cast(Word*)GC.malloc(
Word.sizeof * STACK_SIZE,
GC.BlkAttr.NO_SCAN |
GC.BlkAttr.NO_INTERIOR |
GC.BlkAttr.NO_MOVE
);
// Allocate the tag stack
tStack = cast(Tag*)GC.malloc(
Tag.sizeof * STACK_SIZE,
GC.BlkAttr.NO_SCAN |
GC.BlkAttr.NO_INTERIOR |
GC.BlkAttr.NO_MOVE
);
// Initialize the stack limit pointers
wUpperLimit = wStack + STACK_SIZE;
tUpperLimit = tStack + STACK_SIZE;
// Initialize the stack pointers just past the end of the stack
wsp = wUpperLimit;
tsp = tUpperLimit;
// Allocate two blocks of immovable memory
// for the from-space and to-space heaps
heapStart = allocHeapBlock(vm, heapSize);
toStart = allocHeapBlock(vm, heapSize);
// Initialize the from-space heap to zero
memset(heapStart, 0, heapSize);
// Initialize the allocation and limit pointers
allocPtr = heapStart;
heapLimit = heapStart + heapSize;
toAlloc = toStart;
toLimit = toStart + heapSize;
/// Link table size
linkTblSize = LINK_TBL_INIT_SIZE;
/// Free link table entries
linkTblFree = new LinkIdx[linkTblSize];
for (uint32 i = 0; i < linkTblSize; ++i)
linkTblFree[i] = i;
/// Link table words
wLinkTable = cast(Word*)GC.malloc(
Word.sizeof * linkTblSize,
GC.BlkAttr.NO_SCAN |
GC.BlkAttr.NO_INTERIOR |
GC.BlkAttr.NO_MOVE
);
/// Link table types
tLinkTable = cast(Tag*)GC.malloc(
Tag.sizeof * linkTblSize,
GC.BlkAttr.NO_SCAN |
GC.BlkAttr.NO_INTERIOR |
GC.BlkAttr.NO_MOVE
);
// Initialize the link table
for (size_t i = 0; i < linkTblSize; ++i)
{
wLinkTable[i].int32Val = 0;
tLinkTable[i] = Tag.INT32;
}
// Allocate and initialize the string table
strTbl = strtbl_alloc(vm, STR_TBL_INIT_SIZE);
// Allocate the empty object shape
emptyShape = new ObjShape(vm);
// Initialize the initial array shape
arrayShape = emptyShape.defProp(
vm,
"__proto__",
ValType(Tag.OBJECT),
0,
null
).defProp(
vm,
"__arrTbl__",
ValType(Tag.REFPTR),
0,
null
).defProp(
vm,
"__arrLen__",
ValType(Tag.INT32),
0,
null
);
// Allocate the object prototype object
objProto = newObj(
vm,
NULL
);
// Allocate the array prototype object
arrProto = newObj(
vm,
objProto
);
// Allocate the string prototype object
strProto = newObj(
vm,
objProto
);
// Allocate the function prototype object
funProto = newObj(
vm,
objProto
);
// Allocate the global object
globalObj = newObj(
vm,
objProto,
GLOBAL_OBJ_INIT_SIZE
);
// Allocate the executable heap
execHeap = new CodeBlock(EXEC_HEAP_INIT_SIZE, opts.genasm);
// Compute the stub space start position
stubStartPos = execHeap.getSize - (execHeap.getSize / EXEC_HEAP_STUB_FRAC);
stubWritePos = stubStartPos;
// Allocate the subroutine heap
subsHeap = new CodeBlock(SUBS_HEAP_INIT_SIZE, opts.genasm);
// Allocate the register save space
regSave = cast(Word*)GC.malloc(
Word.sizeof * allocRegs.length,
GC.BlkAttr.NO_SCAN |
GC.BlkAttr.NO_INTERIOR
);
// Define the object-related constants
defObjConsts(vm);
// If the runtime library should be loaded
if (loadRuntime)
{
// Load the layout code
load("runtime/layout.js", true);
// Load the runtime library
load("runtime/runtime.js", true);
}
// If the standard library should be loaded
if (loadStdLib)
{
load("stdlib/object.js");
load("stdlib/error.js");
load("stdlib/function.js");
load("stdlib/math.js");
load("stdlib/string.js");
load("stdlib/array.js");
load("stdlib/number.js");
load("stdlib/boolean.js");
load("stdlib/date.js");
load("stdlib/json.js");
load("stdlib/regexp.js");
load("stdlib/map.js");
load("stdlib/set.js");
load("stdlib/global.js");
load("stdlib/commonjs.js");
}
}
}
/**
Free the global VM object and its allocated resources
Note: we intentionally do not rely on the VM destructor
because D does not guarantee destructor call order
*/
static void free()
{
with (vm)
{
// Free the stacks
GC.free(wStack);
GC.free(tStack);
// Free the heap blocks
GC.free(heapStart);
GC.free(toStart);
// Free the IRFunction references
foreach (ptr, fun; funRefs)
destroy(fun);
//writeln("destroying shapes");
/*
//Want to free the children shapes first, then the parents
// Free the shape objects
ObjShape[] stack;
stack.reserve(32768);
stack.assumeSafeAppend() ~= emptyShape;
while (stack.length > 0)
{
// Pop the top of the stack
auto shape = stack.back();
stack.length--;
foreach (typeMap; shape.propDefs)
foreach (shapeList; typeMap)
foreach (subShape; shapeList)
stack.assumeSafeAppend() ~= subShape;
destroy(shape);
}
destroy(stack);
*/
//writeln("destroyed shapes");
// Destroy the executable heaps
destroy(execHeap);
destroy(subsHeap);
// TODO:
// Check that all GC roots were freed
//assert (firstRoot is null);
// Unregister all the GC roots to prevent them from
// touching the VM object after the it is destroyed
/*for (auto root = firstRoot; root !is null;)
{
auto next = root.nextRoot;
destroy(root);
root = next;
}*/
}
// Destroy the VM object itself
destroy(vm);
// Nullify the global VM object pointer
vm = null;
}
/**
Do not call the VM constructor directly
*/
private this()
{
}
/**
Do not call the VM destructor directly
*/
private ~this()
{
}
/**
Set the value and type of a stack slot
*/
void setSlot(StackIdx idx, Word w, Tag t)
{
assert (
&wsp[idx] >= wStack && &wsp[idx] < wUpperLimit,
format("invalid stack slot index (%s/%s)", idx, stackSize)
);
assert (
!isHeapPtr(t) ||
w.ptrVal == null ||
(w.ptrVal >= heapStart && w.ptrVal < heapLimit),
"ref ptr out of heap in setSlot: " ~
to!string(w.ptrVal)
);
wsp[idx] = w;
tsp[idx] = t;
}
/**
Set the value and type of a stack slot from a value/type pair
*/
void setSlot(StackIdx idx, ValuePair val)
{
setSlot(idx, val.word, val.tag);
}
/**
Set a stack slot to an integer value
*/
void setSlot(StackIdx idx, uint32 val)
{
setSlot(idx, Word.int32v(val), Tag.INT32);
}
/**
Set a stack slot to a float value
*/
void setSlot(StackIdx idx, float64 val)
{
setSlot(idx, Word.float64v(val), Tag.FLOAT64);
}
/**
Get a word from the word stack
*/
Word getWord(StackIdx idx)
{
assert (
&wsp[idx] >= wStack && &wsp[idx] < wUpperLimit,
format("invalid stack slot index (%s/%s)", idx, stackSize)
);
return wsp[idx];
}
/**
Get a type tag from the tag stack
*/
Tag getTag(StackIdx idx)
{
assert (
&tsp[idx] >= tStack && &tsp[idx] < tUpperLimit,
"invalid stack slot index"
);
return tsp[idx];
}
/**
Get a value/type pair from the stack
*/
ValuePair getSlot(StackIdx idx)
{
return ValuePair(getWord(idx), getTag(idx));
}
/**
Copy a value from one stack slot to another
*/
void move(StackIdx src, StackIdx dst)
{
assert (
&wsp[src] >= wStack && &wsp[src] < wUpperLimit,
"invalid move src index"
);
assert (
&wsp[dst] >= wStack && &wsp[dst] < wUpperLimit,
"invalid move dst index"
);
wsp[dst] = wsp[src];
tsp[dst] = tsp[src];
}
/**
Push a word and tag on the stack
*/
void push(Word w, Tag t)
{
push(1);
setSlot(0, w, t);
}
/**
Push a value pair on the stack
*/
void push(ValuePair val)
{
push(1);
setSlot(0, val.word, val.tag);
}
/**
Allocate space on the stack
*/
void push(size_t numWords)
{
wsp -= numWords;