-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.rs
2027 lines (1876 loc) · 62.1 KB
/
compiler.rs
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
use super::assembler::Assembler;
use anyhow::Result;
use parity_wasm::elements::{
BlockType,
Instruction::{self, *},
ValueType,
};
use std::collections::HashMap;
pub enum ColonValue {
XT(&'static str),
Lit(i32),
StringLit(String),
Branch(i32),
QBranch(i32),
}
#[derive(Clone, Copy)]
pub enum ParamType {
I32,
I64,
}
impl ParamType {
pub(crate) fn bytes(&self) -> u32 {
match self {
ParamType::I32 => 4,
ParamType::I64 => 8,
}
}
pub(crate) fn load(&self, offset: u32, instructions: &mut Vec<Instruction>) {
match self {
ParamType::I32 => instructions.push(I32Load(2, offset)),
ParamType::I64 => {
instructions.push(I64Load(2, offset));
instructions.push(I64Const(32));
instructions.push(I64Rotl);
}
}
}
pub(crate) fn store(&self, offset: u32, instructions: &mut Vec<Instruction>) {
match self {
ParamType::I32 => instructions.push(I32Store(2, offset)),
ParamType::I64 => {
instructions.push(I64Const(32));
instructions.push(I64Rotl);
instructions.push(I64Store(2, offset));
}
}
}
}
pub struct Compiler {
assembler: Assembler,
stack: u32,
push: u32,
pop: u32,
push_d: u32,
pop_d: u32,
push_r: u32,
pop_r: u32,
docon: u32,
dovar: u32,
docol: u32,
start: u32,
ip: u32,
cp: i32,
latest_address: i32,
execution_tokens: HashMap<String, i32>,
}
const DICTIONARY_BASE: i32 = 0x1000;
const PARAM_STACK_BASE: i32 = 0xed00;
const RETURN_STACK_BASE: i32 = 0xf100;
const HEAP_BASE: i32 = 0xf100;
const DICTIONARY_CAPACITY: i32 = PARAM_STACK_BASE - DICTIONARY_BASE;
const ALIGNMENT: i32 = 4;
fn required_padding(offset: i32) -> i32 {
-offset & (ALIGNMENT - 1)
}
fn aligned(offset: i32) -> i32 {
offset + required_padding(offset)
}
fn header_size(name: &str) -> i32 {
aligned(1 + name.len() as i32) + 4 + 4
}
impl Compiler {
pub fn define_constant_word(&mut self, name: &str, value: i32) {
let docon = self.docon;
self.define_word(name, docon, &value.to_le_bytes());
}
pub fn define_variable_word(&mut self, name: &str, initial_value: i32) {
let dovar = self.dovar;
self.define_word(name, dovar, &initial_value.to_le_bytes());
}
pub fn define_colon_word(&mut self, name: &str, values: Vec<ColonValue>) {
let docol = self.docol;
let lit_xt = self.get_execution_token("LIT");
let branch_xt = self.get_execution_token("BRANCH");
let q_branch_xt = self.get_execution_token("?BRANCH");
let mut bytes = vec![];
// track the end of the dictionary as we go, to turn relative jumps absolute
let mut cp = self.cp + header_size(name);
for value in values {
match value {
ColonValue::XT(name) => {
cp += 4;
let xt = self.get_execution_token(name);
bytes.extend_from_slice(&xt.to_le_bytes())
}
ColonValue::Lit(value) => {
cp += 8;
bytes.extend_from_slice(&lit_xt.to_le_bytes());
bytes.extend_from_slice(&value.to_le_bytes());
}
ColonValue::StringLit(value) => {
let data_start = cp + 8;
let data_len = value.len() as i32;
let padding = required_padding(data_len);
let target = data_start + data_len + padding;
cp = target + 16;
bytes.extend_from_slice(&branch_xt.to_le_bytes());
bytes.extend_from_slice(&target.to_le_bytes());
bytes.extend_from_slice(value.as_bytes());
bytes.extend_from_slice(&vec![0; padding as usize]);
bytes.extend_from_slice(&lit_xt.to_le_bytes());
bytes.extend_from_slice(&data_start.to_le_bytes());
bytes.extend_from_slice(&lit_xt.to_le_bytes());
bytes.extend_from_slice(&data_len.to_le_bytes());
}
ColonValue::Branch(offset) => {
cp += 8;
let target = cp + offset;
bytes.extend_from_slice(&branch_xt.to_le_bytes());
bytes.extend_from_slice(&target.to_le_bytes());
}
ColonValue::QBranch(offset) => {
cp += 8;
let target = cp + offset;
bytes.extend_from_slice(&q_branch_xt.to_le_bytes());
bytes.extend_from_slice(&target.to_le_bytes());
}
}
}
let exit_xt = self.get_execution_token("EXIT");
bytes.extend_from_slice(&exit_xt.to_le_bytes());
self.define_word(name, docol, &bytes);
}
pub fn define_imported_word(
&mut self,
name: &str,
module: &str,
field: &str,
params: Vec<ParamType>,
results: Vec<ParamType>,
) {
let to_value_types = |types: &[ParamType]| -> Vec<ValueType> {
types
.iter()
.map(|t| match t {
ParamType::I32 => ValueType::I32,
ParamType::I64 => ValueType::I64,
})
.collect()
};
// Define an imported with the given signature
let func = self.assembler.add_imported_func(
module.to_owned(),
field.to_owned(),
to_value_types(¶ms),
to_value_types(&results),
);
let params_bytes = params.iter().map(|p| p.bytes()).sum();
let results_bytes = results.iter().map(|p| p.bytes()).sum();
// Define a native word to call the import using the stack
let locals = if results.is_empty() {
vec![]
} else {
vec![ValueType::I64]
};
let mut instructions = vec![];
if !params.is_empty() {
instructions.push(GetGlobal(self.stack));
instructions.push(TeeLocal(0));
let mut param_offset = params_bytes;
// pass parameters in LIFO order, so stack effects match function signatures
for (param, _type) in params.iter().enumerate() {
if param > 0 {
instructions.push(GetLocal(0));
}
param_offset -= _type.bytes();
_type.load(param_offset, &mut instructions);
}
}
instructions.push(Call(func));
// If the stack size has changed, move the stack pointer appropriately
if params_bytes != results_bytes {
if !params.is_empty() {
instructions.push(GetLocal(0));
} else {
instructions.push(GetGlobal(self.stack));
}
let delta = params_bytes as i32 - results_bytes as i32;
instructions.push(I32Const(delta));
instructions.push(I32Add);
if !results.is_empty() {
// hold onto the new stack head so we can write results
instructions.push(TeeLocal(0));
}
instructions.push(SetGlobal(self.stack));
}
// store results in FIFO order, also so stack effects can match signatures
// at this point, local 0 holds the head (lowest address) of the stack
let mut result_offset = 0;
for _type in results.iter().rev() {
let local = match _type {
ParamType::I32 => 1,
ParamType::I64 => 2,
};
instructions.push(SetLocal(local));
instructions.push(GetLocal(0));
instructions.push(GetLocal(local));
_type.store(result_offset, &mut instructions);
result_offset += _type.bytes();
}
self.define_native_word(name, locals, instructions);
}
pub fn compile(self) -> Result<Vec<u8>> {
self.finalize().assembler.compile()
}
fn initialize(mut self) -> Self {
self.define_stacks();
self.define_memory();
self.define_execution();
self.define_math();
// Define dictionary-related words here as well
// We don't have some real values yet, but other code needs to reference them
self.define_constant_word("DICT-BASE", DICTIONARY_BASE);
self.define_constant_word("DICT-CAPACITY", DICTIONARY_CAPACITY);
self.define_variable_word("CP", DICTIONARY_BASE);
self.define_variable_word("LATEST", DICTIONARY_BASE);
self
}
fn define_stacks(&mut self) {
let define_stack = |assembler: &mut Assembler, stack| {
let push_instructions = vec![
// decrement stack pointer
GetGlobal(stack),
I32Const(4),
I32Sub,
SetGlobal(stack),
// write data
GetGlobal(stack),
GetLocal(0),
I32Store(2, 0),
End,
];
let push =
assembler.add_native_func(vec![ValueType::I32], vec![], vec![], push_instructions);
let pop_instructions = vec![
// read data
GetGlobal(stack),
I32Load(2, 0),
// increment stack pointer
GetGlobal(stack),
I32Const(4),
I32Add,
SetGlobal(stack),
End,
];
let pop =
assembler.add_native_func(vec![], vec![ValueType::I32], vec![], pop_instructions);
(push, pop)
};
// define the normal stack
let stack = self.add_global(PARAM_STACK_BASE);
let (push, pop) = define_stack(&mut self.assembler, stack);
self.stack = stack;
self.push = push;
self.pop = pop;
let push_d = self.assembler.add_native_func(
vec![ValueType::I64],
vec![],
vec![],
vec![
// decrement stack pointer
GetGlobal(stack),
I32Const(8),
I32Sub,
SetGlobal(stack),
// write data
GetGlobal(stack),
GetLocal(0),
I64Const(32),
I64Rotl,
I64Store(3, 0),
End,
],
);
let pop_d = self.assembler.add_native_func(
vec![],
vec![ValueType::I64],
vec![],
vec![
// read data
GetGlobal(stack),
I64Load(3, 0),
I64Const(32),
I64Rotl,
// increment stack pointer
GetGlobal(stack),
I32Const(8),
I32Add,
SetGlobal(stack),
End,
],
);
self.push_d = push_d;
self.pop_d = pop_d;
// define the return stack
let r_stack = self.add_global(RETURN_STACK_BASE);
let (push_r, pop_r) = define_stack(&mut self.assembler, r_stack);
self.push_r = push_r;
self.pop_r = pop_r;
#[cfg(test)]
{
self.assembler.add_exported_func("push", push);
self.assembler.add_exported_func("pop", pop);
self.assembler.add_exported_func("push_d", push_d);
self.assembler.add_exported_func("pop_d", pop_d);
}
self.define_native_word(
"DUP",
vec![],
vec![
// just push the top of the stack onto itself
GetGlobal(stack),
I32Load(2, 0),
Call(push),
],
);
self.define_native_word(
"?DUP",
vec![],
vec![
GetGlobal(stack),
I32Load(2, 0),
TeeLocal(0),
If(BlockType::NoResult),
GetLocal(0),
Call(push),
End,
],
);
self.define_native_word(
"2DUP",
vec![],
vec![
GetGlobal(stack),
I32Const(8),
I32Sub,
TeeLocal(0),
SetGlobal(stack), // reserve room for two new words
GetLocal(0),
GetLocal(0),
I64Load(3, 8),
I64Store(3, 0),
],
);
self.define_native_word(
"DROP",
vec![],
vec![
// just increment the stack pointer
GetGlobal(stack),
I32Const(4),
I32Add,
SetGlobal(stack),
],
);
self.define_native_word(
"2DROP",
vec![],
vec![
// just increment the stack pointer
GetGlobal(stack),
I32Const(8),
I32Add,
SetGlobal(stack),
],
);
self.define_native_word(
"SWAP",
vec![],
vec![
// don't bother touching the stack pointer
GetGlobal(stack),
TeeLocal(0),
GetLocal(0),
I32Load(2, 0),
GetLocal(0),
GetLocal(0),
I32Load(2, 4),
I32Store(2, 0),
I32Store(2, 4),
],
);
self.define_native_word(
"2SWAP",
vec![],
vec![
// don't bother touching the stack pointer
GetGlobal(stack),
TeeLocal(0),
GetLocal(0),
I64Load(3, 0),
GetLocal(0),
GetLocal(0),
I64Load(3, 8),
I64Store(3, 0),
I64Store(3, 8),
],
);
self.define_native_word(
"OVER",
vec![],
vec![
GetGlobal(stack),
I32Const(4),
I32Sub,
TeeLocal(0),
SetGlobal(stack),
GetLocal(0),
GetLocal(0),
I32Load(2, 8),
I32Store(2, 0),
],
);
self.define_native_word(
"2OVER",
vec![],
vec![
GetGlobal(stack),
I32Const(8),
I32Sub,
TeeLocal(0),
SetGlobal(stack),
GetLocal(0),
GetLocal(0),
I64Load(2, 16),
I64Store(2, 0),
],
);
self.define_native_word(
"NIP",
vec![],
vec![
GetGlobal(stack),
TeeLocal(0),
GetLocal(0),
I32Load(2, 0), // retrieve value of head
I32Store(2, 4), // store in head + 4
GetLocal(0),
I32Const(4),
I32Add,
SetGlobal(stack), // head += 4
],
);
self.define_native_word(
"TUCK",
vec![],
vec![
GetGlobal(stack),
I32Const(4),
I32Sub,
TeeLocal(0), // save head - 4
I32Load(2, 4),
SetLocal(1), // save [head]
// start moving
GetLocal(0),
GetLocal(0),
I32Load(2, 8),
I32Store(2, 4), // store [head + 4] in head
GetLocal(0),
GetLocal(1),
I32Store(2, 8), // store old [head] in head + 4
GetLocal(0),
GetLocal(1),
I32Store(2, 0), // store old [head] in head - 4
// and just save the new stack ptr and we're done
GetLocal(0),
SetGlobal(stack),
],
);
self.define_native_word(
"ROT",
vec![],
vec![
// spin your elements round and round
GetGlobal(stack),
TeeLocal(0),
GetLocal(0),
I32Load(2, 0),
GetLocal(0),
GetLocal(0),
I32Load(2, 4),
GetLocal(0),
GetLocal(0),
I32Load(2, 8),
I32Store(2, 0),
I32Store(2, 8),
I32Store(2, 4),
],
);
self.define_native_word(
"-ROT",
vec![],
vec![
// like two rots, or rot backwards
GetGlobal(stack),
TeeLocal(0),
GetLocal(0),
I32Load(2, 0),
GetLocal(0),
GetLocal(0),
I32Load(2, 4),
GetLocal(0),
GetLocal(0),
I32Load(2, 8),
I32Store(2, 4),
I32Store(2, 0),
I32Store(2, 8),
],
);
self.define_native_word(
"PICK",
vec![],
vec![
GetGlobal(stack),
TeeLocal(0),
GetLocal(0),
I32Load(2, 0), // read the head of the stack
I32Const(1),
I32Add, // + to account for the address itself at the top of the stack
I32Const(2),
I32Shl, // * 4 to make it an offset
GetLocal(0),
I32Add,
I32Load(2, 0), // read that offset from the head
I32Store(2, 0), // and store it back in the head
],
);
self.define_native_word(
"DEPTH",
vec![],
vec![
I32Const(PARAM_STACK_BASE),
GetGlobal(stack),
I32Sub,
I32Const(2),
I32ShrU,
Call(push),
],
);
self.define_native_word(">R", vec![], vec![Call(pop), Call(push_r)]);
self.define_native_word("R>", vec![], vec![Call(pop_r), Call(push)]);
self.define_native_word(
"R@",
vec![],
vec![GetGlobal(r_stack), I32Load(2, 0), Call(push)],
);
self.define_native_word(
"R-DEPTH",
vec![],
vec![
I32Const(RETURN_STACK_BASE),
GetGlobal(r_stack),
I32Sub,
I32Const(2),
I32ShrU,
Call(push),
],
);
}
fn define_memory(&mut self) {
let push = self.push;
let pop = self.pop;
// constants
let docon = self.create_native_callable(
vec![],
vec![
// The value of our parameter is the value of the constant, just fetch and push it
GetLocal(0),
I32Load(2, 0),
Call(push),
],
);
self.docon = docon;
self.define_constant_word("(DOCON)", docon as i32);
// variables
let dovar = self.create_native_callable(
vec![],
vec![
// the address of our parameter IS the address of the variable, just push it
GetLocal(0),
Call(push),
],
);
self.dovar = dovar;
self.define_constant_word("(DOVAR)", dovar as i32);
self.define_constant_word("CELL", 4);
self.define_native_word("CELLS", vec![], vec![Call(pop), I32Const(2), I32Shl, Call(push)]);
self.define_native_word("!", vec![], vec![Call(pop), Call(pop), I32Store(2, 0)]);
self.define_native_word("@", vec![], vec![Call(pop), I32Load(2, 0), Call(push)]);
self.define_native_word(
"+!",
vec![],
vec![
Call(pop),
TeeLocal(0),
GetLocal(0),
I32Load(2, 0),
Call(pop),
I32Add,
I32Store(2, 0),
],
);
self.define_native_word("C!", vec![], vec![Call(pop), Call(pop), I32Store8(0, 0)]);
self.define_native_word("C@", vec![], vec![Call(pop), I32Load8U(0, 0), Call(push)]);
// heap words
self.define_constant_word("HEAP-BASE", HEAP_BASE);
self.define_native_word("MEMORY.SIZE", vec![], vec![CurrentMemory(0), Call(push)]);
self.define_native_word(
"MEMORY.GROW",
vec![],
vec![Call(pop), GrowMemory(0), Call(push)],
);
}
fn define_execution(&mut self) {
let push = self.push;
let pop = self.pop;
let push_r = self.push_r;
let pop_r = self.pop_r;
let ip = self.add_global(0);
self.ip = ip;
let stopped = self.add_global(0);
// "execute" takes an XT as a parameter and runs it
let callable_sig = self
.assembler
.add_type(vec![ValueType::I32, ValueType::I32], vec![]);
let execute = self.assembler.add_native_func(
vec![ValueType::I32],
vec![],
vec![ValueType::I32],
vec![
// The argument is an execution token (XT).
// In this system, an execution token is a 32-bit address.
// The low 8 bits of the value are a table index,
// and the rest are a 24-bit "immediate" value.
// Any parameter data is stored immediately after it.
// Call the func with:
// arg0: the address of the parameter data.
// arg1: the immediate
GetLocal(0),
I32Const(4),
I32Add,
GetLocal(0),
I32Load(2, 0),
TeeLocal(1),
I32Const(8),
I32ShrU, // top 24 bits are arg1
GetLocal(1),
I32Const(255),
I32And, // bottom 8 bits are the func index
CallIndirect(callable_sig, 0),
End,
],
);
self.define_native_word("EXECUTE", vec![], vec![Call(pop), Call(execute)]);
// Start is the interpreter's main loop, it calls EXECUTE until the program says to stop.
// Assuming that the caller has set IP to something reasonable first.
let start = self.assembler.add_native_func(
vec![],
vec![],
vec![],
vec![
// mark that we should NOT stop yet
I32Const(0),
SetGlobal(stopped),
// loop until execution is not "in progress"
Loop(BlockType::NoResult),
GetGlobal(ip), // IP is a pointer to an XT
I32Load(2, 0), // Deref it to get our next XT
Call(execute), // Run it
GetGlobal(ip),
I32Const(4),
I32Add,
SetGlobal(ip), // increment the IP
// loop if we still have not been stopped
GetGlobal(stopped),
I32Eqz,
BrIf(0),
End,
End,
],
);
self.start = start;
self.define_native_word("STOP", vec![], vec![I32Const(-1), SetGlobal(stopped)]);
// DOCOL is how a colon word is executed. It just messes with the IP.
let docol = self.create_native_callable(
vec![],
vec![
// push IP onto the return stack
GetGlobal(ip),
Call(push_r),
// Set IP to the head of our parameter
GetLocal(0),
I32Const(4),
I32Sub,
SetGlobal(ip),
],
);
self.docol = docol;
self.define_constant_word("(DOCOL)", docol as i32);
// EXIT is how a colon word returns. It just restores the old IP.
self.define_native_word(
"EXIT",
vec![],
vec![
// Set IP to whatever's the head of the return stack
Call(pop_r),
SetGlobal(ip),
],
);
// DODOES is what lets you customize runtime behavior of a word.
// It does what DOCOL does, except it also pushes a word onto the stack.
let dodoes = self.create_native_callable(
vec![],
vec![
// push the head of our parameter onto the stack
GetLocal(0),
Call(push),
// push IP onto the return stack
GetGlobal(ip),
Call(push_r),
// Set IP to our immediate
GetLocal(1),
I32Const(4),
I32Sub,
SetGlobal(ip),
],
);
self.define_constant_word("(DODOES)", dodoes as i32);
self.define_native_word(
"LIT",
vec![],
vec![
// The instruction pointer is pointing to LIT's XT inside of a colon definition.
// The value after that is a literal; push it.
GetGlobal(ip),
I32Const(4),
I32Add,
TeeLocal(0),
I32Load(2, 0),
Call(push),
// also increment IP appropriately
GetLocal(0),
SetGlobal(ip),
],
);
self.define_native_word(
"BRANCH",
vec![],
vec![
// The instruction pointer is pointing to BRANCH's XT inside of a colon definition.
// The value after that is a literal jump address, jump there
GetGlobal(ip),
I32Load(2, 4),
I32Const(4), // Subtract 4 to account for the main loop incrementing the IP itself
I32Sub,
SetGlobal(ip),
],
);
self.define_native_word(
"?BRANCH",
vec![],
vec![
// Branch if the head of the stack is "false" (0)
Call(pop),
I32Eqz,
If(BlockType::Value(ValueType::I32)),
// Jump to literal-after-the-IP - 4
GetGlobal(ip),
I32Load(2, 4),
I32Const(4),
I32Sub,
Else, // Just jump to 4-after-the-IP
GetGlobal(ip),
I32Const(4),
I32Add,
End,
SetGlobal(ip),
],
);
}
fn define_math(&mut self) {
let push = self.push;
let pop = self.pop;
let push_d = self.push_d;
let pop_d = self.pop_d;
let get_two_i32_args = || {
vec![
//swap the top of the stack before calling the real ops
Call(pop),
SetLocal(0),
Call(pop),
GetLocal(0),
]
};
let get_two_i64_args = || {
vec![
//swap the top of the stack before calling the real ops
Call(pop_d),
SetLocal(2),
Call(pop_d),
GetLocal(2),
]
};
let binary_i32 = |op| {
let mut res = get_two_i32_args();
res.push(op);
res.push(Call(push));
res
};
let binary_i64 = |op| {
let mut res = get_two_i64_args();
res.push(op);
res.push(Call(push_d));
res
};
let binary_i32_bool = |op| {
let mut res = vec![I32Const(0)];
res.extend_from_slice(&get_two_i32_args());
res.push(op);
res.push(I32Sub);
res.push(Call(push));
res
};
let binary_i64_bool = |op| {
let mut res = vec![I32Const(0)];
res.extend_from_slice(&get_two_i64_args());
res.push(op);
res.push(I32Sub);
res.push(Call(push));
res
};
self.define_native_word("+", vec![], binary_i32(I32Add));
self.define_native_word("-", vec![], binary_i32(I32Sub));
self.define_native_word("*", vec![], binary_i32(I32Mul));
self.define_native_word(
"NEGATE",
vec![],
vec![I32Const(0), Call(pop), I32Sub, Call(push)],
);
self.define_native_word(
"ABS",
vec![],
vec![
Call(pop),
TeeLocal(0),
I32Const(31),
I32ShrS,
TeeLocal(1),
GetLocal(0),
I32Xor,
GetLocal(1),
I32Sub,
Call(push),
],
);
self.define_native_word("S>D", vec![], vec![Call(pop), I64ExtendSI32, Call(push_d)]);
self.define_native_word("D>S", vec![], vec![Call(pop_d), I32WrapI64, Call(push)]);
self.define_native_word(
"M+",
vec![],
vec![Call(pop), I64ExtendSI32, Call(pop_d), I64Add, Call(push_d)],
);
self.define_native_word("D+", vec![ValueType::I64], binary_i64(I64Add));
self.define_native_word("D-", vec![ValueType::I64], binary_i64(I64Sub));
self.define_native_word(
"DABS",
vec![ValueType::I64, ValueType::I64],
vec![
Call(pop_d),
TeeLocal(2),
I64Const(63),
I64ShrS,
TeeLocal(3),
GetLocal(2),
I64Xor,
GetLocal(3),
I64Sub,
Call(push_d),
],
);
self.define_native_word(
"DNEGATE",
vec![],
vec![I64Const(0), Call(pop_d), I64Sub, Call(push_d)],
);
self.define_native_word(
"M*",
vec![],
vec![
Call(pop),
SetLocal(0),
Call(pop),
I64ExtendSI32,
GetLocal(0),
I64ExtendSI32,
I64Mul,
Call(push_d),
],
);
self.define_native_word(
"UM*",
vec![],
vec![
Call(pop),
SetLocal(0),
Call(pop),
I64ExtendUI32,
GetLocal(0),
I64ExtendUI32,
I64Mul,
Call(push_d),
],
);
self.define_native_word(
"D*",
vec![],
vec![
Call(pop),
SetLocal(0),
Call(pop_d),
GetLocal(0),
I64ExtendSI32,
I64Mul,
Call(push_d),
],
);
self.define_native_word(
"UD*",
vec![],
vec![
Call(pop),
SetLocal(0),
Call(pop_d),
GetLocal(0),
I64ExtendUI32,
I64Mul,
Call(push_d),
],
);