-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.rkt
1407 lines (1272 loc) · 53.2 KB
/
main.rkt
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
#lang racket
;; This is a redex model of the semantics of webassembly from the paper
;; "Bringing the Web up to Speed with WebAssembly"
;; Haas et al.
(require redex
data/bit-vector
pict
pict/snip
racket/draw
racket/fixnum
rnrs/arithmetic/bitwise-6)
;; wasm defines memory for module instances in 64Ki increments, but this is
;; unwieldly in redex so we define the increment in bytes here
(define *page-size* 20)
;; some non-terminals (like for modules) differ from the paper due to redex
;; constraints on non-terminals appearing as keywords, and some forms have
;; extra keywords for ease of identification
(define-language wasm-lang
;; types
(t ::= t-i t-f)
(t-f ::= f32 f64)
(t-i ::= i32 i64)
(tp ::= i8 i16 i32)
(sx ::= sx-s sx-u) ; renamed to avoid conflict with s non-terminal
(tf ::= (-> (t ...) (t ...)))
(tg ::= t (mut t))
;; instructions
(e-no-v ::= unreachable
nop
drop
select
(block tf e*)
(loop tf e*)
(if tf e* else e*)
(br i)
(br-if i)
(br-table i i ...)
return
(call i)
(call-indirect tf)
(get-local i)
(set-local i)
(tee-local i)
(get-global i)
(set-global i)
(load t a o)
(load t tp sx a o)
(store t a o)
(store t tp a o)
current-memory
grow-memory
;; prim ops become a little verbose due to type constraints
(unop-i t-i)
(unop-f t-f)
(binop-i t-i)
(binop-f t-f)
(testop-i t-i)
(relop-i t-i)
(relop-f t-f)
(cvtop t t)
(cvtop t t sx))
(e ::= e-no-v
(const t c))
;; primitive operations
[unop ::= unop-i unop-f]
(unop-i ::= clz ctz popcnt)
(unop-f ::= neg abs ceil floor trunc nearest sqrt)
[binop ::= binop-i binop-f]
(binop-i ::= add sub mul div-s div-u rem-s rem-u and
or xor shl shr-s shr-u rotl rotr)
(binop-f ::= add sub mul div min max copysign)
(testop ::= testop-i)
(testop-i ::= eqz)
(relop ::= relop-i relop-f)
(relop-i ::= eq ne lt-u lt-s gt-u gt-s le-u le-s ge-u ge-s)
(relop-f ::= eq ne lt gt le ge)
(cvtop ::= convert reinterpret)
;; sequences of expressions
;; (we use this to avoid splicing)
(e* ::= ϵ
(e e*))
(c ::= number)
;; constant numerals
((i j l k m n a o) integer)
;; bytes
(b ::= integer)
;; modules & functions
(f ::= (func (ex ...) tf local (t ...) e*)
(func (ex ...) tf im))
(m-glob ::= (global (ex ...) tg e ...)
(global (ex ...) tg im))
(m-tab ::= (table (ex ...) n i ...)
(table (ex ...) n im))
(m-mem ::= (memory (ex ...) n)
(memory (ex ...) n im))
(im ::= (import string string))
(ex ::= (export string))
(mod ::= (module f ... m-glob ...)
(module f ... m-glob ... m-tab)
(module f ... m-glob ... m-mem)
(module f ... m-glob ... m-tab m-mem)))
(define-extended-language wasm-runtime-lang wasm-lang
;; administrative expressions
(e-no-v ::= ....
trap
(call cl)
(label n {e*} e*)
(local n {i (v ...)} e*))
;; runtime
(s ::= {(inst modinst ...) (tab tabinst ...) (mem meminst ...)})
(modinst ::= {(func cl ...) (glob v ...)}
{(func cl ...) (glob v ...) (tab i)}
{(func cl ...) (glob v ...) (mem i)}
{(func cl ...) (glob v ...) (tab i) (mem i)})
(tabinst ::= (cl ...))
(meminst ::= (b ...))
(cl ::= {(inst i) (code f)})
(F ::= (v ...))
;; this is needed for the actual spec
#;
(F ::= {(locals v ...) (module modinst)})
(v ::= (const t c))
(v* ::= ϵ
(v v*))
;; evaluation contexts
;; using an inductive definition instead of using sequences because
;; we would need splicing holes otherwise
(E ::= hole
(v E)
((label n (e*) E) e*)))
;; helper for constructing instruction sequences
(define-metafunction wasm-runtime-lang
seq : e ... -> e*
[(seq) ϵ]
[(seq e_0 e_1 ...)
(e_0 (seq e_1 ...))])
(define-metafunction wasm-runtime-lang
seq* : e ... e* -> e*
[(seq* e*) e*]
[(seq* e_0 e_1 ... e*_2)
(e_0 (seq* e_1 ... e*_2))])
;; append two e* expression lists
(define-metafunction wasm-runtime-lang
e*-append : e* e* -> e*
[(e*-append ϵ e*) e*]
[(e*-append (e_0 e*_0) e*_1)
(e_0 (e*-append e*_0 e*_1))])
(module+ test
(test-equal (term (e*-append ϵ (drop ϵ)))
(term (drop ϵ)))
(test-equal (term (e*-append ((const i32 0) ϵ) (drop ϵ)))
(term ((const i32 0) (drop ϵ)))))
;; find the nesting depth of values around the hole in the eval context
(define-metafunction wasm-runtime-lang
v-depth : E -> number
[(v-depth hole) 0]
[(v-depth (in-hole E (v hole)))
,(add1 (term (v-depth E)))]
[(v-depth (in-hole E ((label n {e*_0} hole) e*_1)))
0])
(module+ test
(test-equal (term (v-depth hole)) (term 0))
(test-equal (term (v-depth ((const i32 2) ((const i32 1) hole))))
(term 2))
(test-equal (term (v-depth ((label 1 {ϵ} ((const i32 1) hole)) ϵ)))
(term 1))
(test-equal (term (v-depth ((const i32 2) ((label 1 {ϵ} ((const i32 1) hole)) ϵ))))
(term 1)))
;; split an eval context into two contexts:
;; - an outer context surrounding the second
;; - an inner context with nested values around a hole
;; precondition: E actually has l-nested values when called
(define-metafunction wasm-runtime-lang
v-split : E number -> (E E)
[(v-split hole l)
(hole hole)]
[(v-split (in-hole E (v hole)) 0)
((in-hole E_outer (v hole)) hole)
(where (E_outer hole) (v-split E 0))]
[(v-split (in-hole E (v hole)) l)
(E_outer (in-hole E_v (v hole)))
(where (E_outer E_v) (v-split E ,(sub1 (term l))))]
[(v-split (in-hole E ((label n {e*_0} hole) e*_2)) 0)
((in-hole E_outer ((label n {e*_0} hole) e*_2))
hole)
(where (E_outer hole) (v-split E 0))])
(module+ test
(test-equal (term (v-split hole 0))
(term (hole hole)))
(test-equal (term (v-split ((const i32 2) ((const i32 1) hole)) 1))
(term (((const i32 2) hole)
((const i32 1) hole))))
(test-equal (term (v-split ((label 1 {ϵ} ((const i32 0) ((const i32 1) hole))) ϵ) 1))
(term (((label 1 {ϵ} ((const i32 0) hole)) ϵ)
((const i32 1) hole)))))
;; find the depth of label nestings in E
(define-metafunction wasm-runtime-lang
label-depth : E -> number
[(label-depth hole) 0]
[(label-depth (v E)) (label-depth E)]
[(label-depth ((label n {e*_0} E) e*_1))
,(add1 (term (label-depth E)))])
(module+ test
(test-equal (term (label-depth hole)) 0)
(test-equal (term (label-depth ((const i32 2) ((const i32 1) hole))))
0)
(test-equal (term (label-depth ((label 1 {ϵ} ((const i32 0) ((const i32 1) hole))) ϵ)))
1)
(test-equal (term (label-depth ((label 1 {ϵ} ((const i32 0) ((const i32 1) ((label 1 {ϵ} hole) ϵ)))) ϵ)))
2))
;; extract a closure out of the func part of store
(define-metafunction wasm-runtime-lang
store-func : s i j -> cl
[(store-func {(inst modinst_0 ... modinst modinst_1 ...) any_0 ...} i j)
cl
(side-condition (= (length (term (modinst_0 ...))) (term i)))
(where {(func cl_0 ... cl cl_1 ...) any_1 ...} modinst)
(side-condition (= (length (term (cl_0 ...))) (term j)))])
(module+ test
(let ()
(define f1 (term (func () (-> () ()) local () (seq nop))))
(define modinst1 (term {(func {(inst 0) (code ,f1)}) (glob)}))
(test-equal (term (store-func {(inst ,modinst1) (tab) (mem)} 0 0))
(term {(inst 0) (code ,f1)}))))
;; extract a closure from the tab part of store
(define-metafunction wasm-runtime-lang
store-tab : s i j -> cl
[(store-tab {(inst modinst_0 ... modinst modinst_1 ...)
(tab tabinst_0 ... tabinst tabinst_1 ...)
any_0 ...}
i j)
cl
(side-condition (= (length (term (modinst_0 ...))) (term i)))
(where {any_1 ... (tab i_tab) any_2 ...} modinst)
(side-condition (= (length (term (tabinst_0 ...))) (term i_tab)))
(where (cl_0 ... cl cl_1 ...) tabinst)
(side-condition (= (length (term (cl_0 ...))) (term j)))])
;; read a global value
(define-metafunction wasm-runtime-lang
store-glob : s i j -> v
[(store-glob {(inst modinst_0 ... modinst modinst_1 ...) any_0 ...} i j)
v
(side-condition (= (length (term (modinst_0 ...))) (term i)))
(where {any_f (glob v_0 ... v v_1 ...) any_1 ...} modinst)
(side-condition (= (length (term (v_0 ...))) (term j)))])
;; write a global value
(define-metafunction wasm-runtime-lang
store-glob= : s i j v -> s
[(store-glob= {(inst modinst_0 ... modinst modinst_1 ...) any_0 ...} i j v_new)
{(inst modinst_0 ... modinst_new modinst_1 ...) any_0 ...}
(side-condition (= (length (term (modinst_0 ...))) (term i)))
(where {any_f (glob v_0 ... v v_1 ...) any_1 ...} modinst)
(side-condition (= (length (term (v_0 ...))) (term j)))
(where modinst_new {any_f (glob v_0 ... v_new v_1 ...) any_1 ...})])
;; read from memory
(define-metafunction wasm-runtime-lang
store-mem : s i j n -> (b ...) or #false
[(store-mem {(inst modinst_0 ... modinst modinst_1 ...)
any_1
(mem meminst_0 ... meminst meminst_1 ...)}
i j n)
(b ...)
(side-condition (= (length (term (modinst_0 ...))) (term i)))
(where {any_i ... (mem i_mem)} modinst)
(side-condition (= (length (term (meminst_0 ...))) (term i_mem)))
(where (b_0 ... b_rest ...) meminst)
(side-condition (= (length (term (b_0 ...))) (term j)))
(where (b ... b_end ...) (b_rest ...))
(side-condition (= (length (term (b ...))) (term n)))]
[(store-mem any ...)
#false])
(define-metafunction wasm-runtime-lang
sizeof : any -> n
[(sizeof i8) 1]
[(sizeof i16) 2]
[(sizeof i32) 4]
[(sizeof i64) 8]
[(sizeof f32) 4]
[(sizeof f64) 8])
(define-metafunction wasm-runtime-lang
const-reinterpret-packed : t (b ...) sx -> c
[(const-reinterpret-packed t (b ...) sx-s)
,(integer-bytes->integer (list->bytes (term (b ...))) #t)]
[(const-reinterpret-packed t (b ...) sx-u)
,(integer-bytes->integer (list->bytes (term (b ...))) #f)])
(define-metafunction wasm-runtime-lang
const-reinterpret : t (b ...) -> c
[(const-reinterpret t-i (b ...))
,(integer-bytes->integer (list->bytes (term (b ...))) #t)]
[(const-reinterpret t-f (b ...))
,(floating-point-bytes->real (list->bytes (term (b ...))))])
(define-metafunction wasm-runtime-lang
bits : n t c -> (b ...)
[(bits n i32 i)
,(take (bytes->list (integer->integer-bytes (term i) 4 #t)) (term n))]
[(bits n i64 i)
,(take (bytes->list (integer->integer-bytes (term i) 8 #t)) (term n))]
[(bits n f32 float)
,(take (bytes->list (real->floating-point-bytes (term float) 4)) (term n))]
[(bits n f64 float)
,(take (bytes->list (real->floating-point-bytes (term float) 8)) (term n))])
(define-metafunction wasm-runtime-lang
store-mem= : s i j n (b ...) -> s or #false
[(store-mem= {(name any_0 (inst modinst_0 ... modinst modinst_1 ...))
any_1
(mem meminst_0 ... meminst meminst_1 ...)}
i j n (b_new ...))
{any_0 any_1 (mem meminst_0 ... meminst_new meminst_1 ...)}
(side-condition (= (length (term (modinst_0 ...))) (term i)))
(where {any_i ... (mem i_mem)} modinst)
(side-condition (= (length (term (meminst_0 ...))) (term i_mem)))
(where (b_0 ... b_rest ...) meminst)
(side-condition (= (length (term (b_0 ...))) (term j)))
(where (b ... b_end ...) (b_rest ...))
(side-condition (= (length (term (b ...))) (term n)))
(where meminst_new (b_0 ... b_new ... b_end ...))]
[(store-mem= any ...) #false])
;; metafunctions for manipulating memory size
(define-metafunction wasm-runtime-lang
memory-size : s i -> n
[(memory-size {(name any_0 (inst modinst_0 ... modinst modinst_1 ...))
any_1
(mem meminst_0 ... meminst meminst_1 ...)}
i)
n_size
(side-condition (= (length (term (modinst_0 ...))) (term i)))
(where {any_i ... (mem i_mem)} modinst)
(side-condition (= (length (term (meminst_0 ...))) (term i_mem)))
(where n_size ,(/ (length (term meminst)) *page-size*))])
(define-metafunction wasm-runtime-lang
expand-memory : s i k -> (s n)
[(expand-memory {(name any_0 (inst modinst_0 ... modinst modinst_1 ...))
any_1
(mem meminst_0 ... meminst meminst_1 ...)}
i k)
(s_new n_size)
(side-condition (= (length (term (modinst_0 ...))) (term i)))
(where {any_i ... (mem i_mem)} modinst)
(side-condition (= (length (term (meminst_0 ...))) (term i_mem)))
(where meminst_new ,(append (term meminst)
(flatten (make-list (term k) (make-list *page-size* 0)))))
(where n_size ,(/ (length (term meminst_new)) *page-size*))
(where s_new {any_0 any_1 (mem meminst_0 ... meminst_new meminst_1 ...)})])
;; conversion between types
;; precondition: validation passed
;; FIXME: this is probably not quite right
(define-metafunction wasm-runtime-lang
cvt : t t c -> c
[(cvt t-i_1 t-i_2 c)
,(integer-bytes->integer
(integer->integer-bytes (term c) (term (sizeof t-i)) #t)
#t)]
[(cvt t i32 c) ,(real->single-flonum (term c))]
[(cvt t i64 c) ,(real->double-flonum (term c))]
[(cvt f32 t-i c) ,(fl->fx (real->single-flonum (term c)))]
[(cvt f64 t-i c) ,(fl->fx (real->double-flonum (term c)))])
;; TODO: implement properly
(define-metafunction wasm-runtime-lang
cvt-sx : t t c -> c)
;; extract the code from a closure
(define-metafunction wasm-runtime-lang
cl-code : cl -> f
[(cl-code {any (code f)}) f])
;; extract the instance index from a closure
(define-metafunction wasm-runtime-lang
cl-inst : cl -> i
[(cl-inst {(inst i) any}) i])
;; append two Fs together
(define-metafunction wasm-runtime-lang
F-append : F F -> F
[(F-append () F) F]
[(F-append (v_1 ... v) (v_2 ...))
(F-append (v_1 ...) (v v_2 ...))])
;; convert a nested v* to a (v ...), uses accumulator
(define-metafunction wasm-runtime-lang
v*->F : v* -> F
[(v*->F v*) (v*->F-helper v* ())])
(define-metafunction wasm-runtime-lang
v*->F-helper : v* F -> F
[(v*->F-helper ϵ F) F]
[(v*->F-helper (v v*) (v_acc ...))
(v*->F-helper v* (v_acc ... v))])
;; the opposite of v*->F, just used for visualization
(define-metafunction wasm-runtime-lang
F->v* : F -> v*
[(F->v* ()) ϵ]
[(F->v* (v v_0 ...)) (v (F->v* (v_0 ...)))])
;; ctz / clz
(define (clz n width)
(let loop ([pos (sub1 width)] [count 0])
(cond [(< pos 0) count]
[(not (bitwise-bit-set? n pos))
(loop (sub1 pos) (add1 count))]
[else count])))
(define (ctz n width)
(let loop ([pos 0] [count 0])
(cond [(> pos (sub1 width)) count]
[(not (bitwise-bit-set? n pos))
(loop (add1 pos) (add1 count))]
[else count])))
;; implement primitives
(define-metafunction wasm-runtime-lang
do-unop : unop t c -> c
[(do-unop clz i32 c) ,(clz (term c) 32)]
[(do-unop clz i64 c) ,(clz (term c) 64)]
[(do-unop ctz i32 c) ,(ctz (term c) 32)]
[(do-unop ctz i32 c) ,(ctz (term c) 64)]
[(do-unop popcnt t-i c)
,(bit-vector-popcount (string->bit-vector (number->string (term c) 2)))]
[(do-unop neg t-f c) ,(- (term c))]
[(do-unop abs t-f c) ,(abs (term c))]
[(do-unop ceil t-f c) ,(ceiling (term c))]
[(do-unop floor t-f c) ,(floor (term c))]
[(do-unop trunc t-f c) ,(truncate (term c))]
[(do-unop nearest t-f c) ,(round (term c))]
[(do-unop sqrt t-f c) ,(sqrt (term c))])
(define (clamp type const)
(cond [(< const 0)
(define start (if (eq? type 'i32) 32 64))
(define end (integer-length const))
(for/fold ([const const])
([i (in-range start end)])
(bitwise-and const (arithmetic-shift 1 i)))]
[else
(bitwise-and const
(if (eq? type 'i32)
#xFFFFFFFF
#xFFFFFFFFFFFFFFFF))]))
(define-metafunction wasm-runtime-lang
do-binop : binop t c c -> c
[(do-binop add t-i c_1 c_2) ,(clamp (term t-i) (+ (term c_1) (term c_2)))]
[(do-binop sub t-i c_1 c_2) ,(clamp (term t-i) (- (term c_1) (term c_2)))]
[(do-binop mul t-i c_1 c_2) ,(clamp (term t-i) (* (term c_1) (term c_2)))]
;; FIXME: needs to account for sign and bit range properly
[(do-binop div-s t-i c_1 c_2) ,(quotient (term c_1) (term c_2))]
[(do-binop div-u t-i c_1 c_2) ,(quotient (term c_1) (term c_2))]
[(do-binop div-s t-i c_1 0) #false]
[(do-binop div-u t-i c_1 0) #false]
[(do-binop rem-s t-i c_1 c_2) ,(remainder (term c_1) (term c_2))]
[(do-binop rem-u t-i c_1 c_2) ,(remainder (term c_1) (term c_2))]
[(do-binop rem-s t-i c_1 0) #false]
[(do-binop rem-u t-i c_1 0) #false]
[(do-binop and t-i c_1 c_2) ,(bitwise-and (term c_1) (term c_2))]
[(do-binop or t-i c_1 c_2) ,(bitwise-ior (term c_1) (term c_2))]
[(do-binop shl t-i c_1 c_2) ,(clamp (arithmetic-shift (term c_1) (term c_2)))]
[(do-binop shr-s t-i c_1 c_2) ,(arithmetic-shift (term c_1) (- (term c_2)))]
;; FIXME: sign extension
[(do-binop shr-u t-i c_1 c_2) ,(arithmetic-shift (term c_1) (- (term c_2)))]
[(do-binop rotl i32 c_1 c_2)
,(bitwise-rotate-bit-field (term c_1) 0 32 (term c_2))]
[(do-binop rotl i64 c_1 c_2)
,(bitwise-rotate-bit-field (term c_1) 0 64 (term c_2))]
[(do-binop rotr i32 c_1 c_2)
,(bitwise-rotate-bit-field (term c_1) 0 32 (- 32 (term c_2)))]
[(do-binop rotr i64 c_1 c_2)
,(bitwise-rotate-bit-field (term c_1) 0 64 (- 64 (term c_2)))]
;; FIXME: these cases aren't quite right at boundaries
[(do-binop add t-f c_1 c_2) ,(+ (term c_1) (term c_2))]
[(do-binop sub t-f c_1 c_2) ,(- (term c_1) (term c_2))]
[(do-binop mul t-f c_1 c_2) ,(* (term c_1) (term c_2))]
[(do-binop div t-f c_1 c_2) ,(/ (term c_1) (term c_2))]
[(do-binop min t-f c_1 c_2) ,(min (term c_1) (term c_2))]
[(do-binop max t-f c_1 c_2) ,(max (term c_1) (term c_2))]
[(do-binop copysign t-f c_1 c_2)
;; FIXME: nan cases are tricky
,(cond [(or (equal? (sgn (term c_1)) (sgn (term c_2)))
(zero? (term c_2)))
(term c_1)]
[else
(- (term c_1))])])
(define-metafunction wasm-runtime-lang
do-testop : testop t c -> c
[(do-testop eqz t-i 0) 1]
[(do-testop eqz t-i c) 0])
(define (b->i bool) (if bool 1 0))
(define (s->u int type)
(match type
['i32 (integer-bytes->integer (integer->integer-bytes int 8 #t) #f #f 0 4)]
['i64 (integer-bytes->integer (integer->integer-bytes int 8 #t) #f)]))
(define (u->s int type)
(match type
['i32 (integer-bytes->integer (integer->integer-bytes int 8 #f) #t #f 0 4)]
['i64 (integer-bytes->integer (integer->integer-bytes int 8 #f) #t)]))
(define-metafunction wasm-runtime-lang
do-relop : relop t c c -> c
[(do-relop lt-s t-i c_1 c_2) ,(b->i (< (term c_1) (term c_2)))]
[(do-relop gt-s t-i c_1 c_2) ,(b->i (> (term c_1) (term c_2)))]
[(do-relop le-s t-i c_1 c_2) ,(b->i (<= (term c_1) (term c_2)))]
[(do-relop ge-s t-i c_1 c_2) ,(b->i (>= (term c_1) (term c_2)))]
[(do-relop lt-u t-i c_1 c_2) ,(b->i (< (s->u (term c_1) (term t-i))
(s->u (term c_2) (term t-i))))]
[(do-relop gt-u t-i c_1 c_2) ,(b->i (> (s->u (term c_1) (term t-i))
(s->u (term c_2) (term t-i))))]
[(do-relop le-u t-i c_1 c_2) ,(b->i (<= (s->u (term c_1) (term t-i))
(s->u (term c_2) (term t-i))))]
[(do-relop ge-u t-i c_1 c_2) ,(b->i (>= (s->u (term c_1) (term t-i))
(s->u (term c_2) (term t-i))))]
[(do-relop lt t-f c_1 c_2) ,(b->i (< (term c_1) (term c_2)))]
[(do-relop gt t-f c_1 c_2) ,(b->i (> (term c_2) (term c_2)))]
[(do-relop le t-f c_1 c_2) ,(b->i (<= (term c_1) (term c_2)))]
[(do-relop ge t-f c_1 c_2) ,(b->i (>= (term c_1) (term c_2)))]
[(do-relop eq t c_1 c_2) ,(b->i (= (term c_1) (term c_2)))]
[(do-relop ne t c_1 c_2) ,(b->i (not (= (term c_1) (term c_2))))])
;; helpers for pretty printing / drawing as stack picts
(define (pp/pict state port width txt)
(redex-let
wasm-runtime-lang
([{s F e* i} state])
(define e*-pict (term->pict (term e*)))
(define p
(vl-append (text (format "instance: ~a" (term i)))
(text "instructions:")
(blank 0 5)
e*-pict))
(send txt insert (new pict-snip% [pict p]))))
(define (type->color type)
(match type
['value "lemonchiffon"]
['instr "pale green"]
['control "sky blue"]
['admin "lavender"]))
(define (stack-pict str [type 'instr])
(cc-superimpose
(filled-rectangle 130 35
#:color (type->color type)
#:border-color "gray")
(text str (list (make-object color% "black")))))
(define (indent pict)
(hc-append (blank 30 1) pict))
(define term->pict
(term-match/single
wasm-runtime-lang
[((const t c) e*)
(let ()
(define str (format "~a.const ~a" (term t) (term c)))
(vc-append (stack-pict str 'value)
(term->pict (term e*))))]
[((call cl) e*)
(let ()
(vc-append (stack-pict "call #<closure>")
(term->pict (term e*))))]
[((label n {e*_0} e*_1) e*_2)
(let ()
(vc-append (stack-pict (~a "label " (term n)) 'admin)
(indent (term->pict (term e*_1)))
(term->pict (term e*_2))))]
[((local n {i F} e*_1) e*_2)
(let ()
(vc-append (stack-pict (format "local ~a {~a ; ...}" (term n) (term i))
'admin)
(indent (hc-append (text "frame" null 12 (/ pi 2))
(term->pict (term (F->v* F)))))
(indent (term->pict (term e*_1)))
(term->pict (term e*_2))))]
[((block tf e*_1) e*_2)
(let ()
(vc-append (stack-pict (format "block ~a" (term tf))
'control)
(indent (term->pict (term e*_1)))
(term->pict (term e*_2))))]
[((loop tf e*_1) e*_2)
(let ()
(vc-append (stack-pict (format "loop ~a" (term tf))
'control)
(indent (term->pict (term e*_1)))
(term->pict (term e*_2))))]
[((if tf e*_0 else e*_1) e*_2)
(let ()
(vc-append (stack-pict (format "if ~a" (term tf))
'control)
(indent (term->pict (term e*_0)))
(stack-pict (format "else")
'control)
(indent (term->pict (term e*_1)))
(term->pict (term e*_2))))]
[(e e*)
(vc-append (stack-pict (format "~a" (term e)))
(term->pict (term e*)))]
[ϵ (blank 0 0)]))
(define-syntax-rule (wasm-pp-traces t)
(traces wasm-> #:pp pp/pict t))
;; the actual reduction relation starts here
(define wasm->
(reduction-relation
wasm-runtime-lang
#:domain (s F e* i)
(--> (s F (trap (e e*)) i)
(s F (trap ϵ) i)
trap)
(--> (s F (in-hole E (trap e*)) i)
(s F (trap ϵ) i)
(side-condition (not (redex-match wasm-runtime-lang hole (term E))))
trap-context)
(==> ((const t c) ((unop t) e*))
((const t (do-unop unop t c)) e*)
unop)
(==> ((const t c_1) ((const t c_2) ((binop t) e*)))
((const t c) e*)
(where c (do-binop binop t c_1 c_2))
binop)
(==> ((const t c_1) ((const t c_2) ((binop t) e*)))
(trap e*)
(where #false (do-binop binop t c_1 c_2))
binop-trap)
(==> ((const t c) ((testop t) e*))
((const i32 (do-testop testop t c)) e*)
testop)
(==> ((const t c_1) ((const t c_2) ((relop t) e*)))
((const i32 (do-relop relop t c_1 c_2)) e*)
relop)
(==> ((const t_1 c) ((convert t_2 t_1) e*))
((const t_2 (cvt t_1 t_2 c)))
convert)
(==> ((const t_1 c) ((convert t_2 t_1 sx) e*))
((const t_2 (cvt-sx t_1 t_2 sx c)))
convert-sx)
(==> ((const t_1 c) ((reinterpret t_2 t_1) e*))
((const t_2 (const-reinterpret t_2 (bits (sizeof t_1) t_1 c))) e*)
reinterpret)
;; generally these rules need to mention the "continuation" in the sequence
;; of instructions because Redex does not allow splicing holes with a
;; sequence of s-exps
(==> (unreachable e*) (trap e*))
(==> (nop e*) e*)
(==> (v (drop e*)) e*)
(==> (v_1 (v_2 ((const i32 0) (select e*))))
(v_2 e*)
select-false)
(==> (v_1 (v_2 ((const i32 k) (select e*))))
(v_1 e*)
(side-condition (>= (term k) 1))
select-true)
;; Redex can't express a pattern like n-level nestings of an
;; expression, so we explicitly compute the nesting depth of
;; values around the hole in the context E instead
(++> (in-hole E ((block (-> (t_1 ...) (t_2 ...)) e*_0) e*_1))
(in-hole E_outer (seq* (label k {ϵ} (in-hole E_v e*_0)) e*_1))
(where l ,(length (term (t_2 ...))))
(where k ,(length (term (t_1 ...))))
(where (E_outer E_v) (v-split E k))
block)
(++> (in-hole E ((name e_loop (loop (-> (t_1 ...) (t_2 ...)) e*_0)) e*_1))
(in-hole E_outer (seq* e_lbl e*_1))
(where l ,(length (term (t_2 ...))))
(where k ,(length (term (t_1 ...))))
(where (E_outer E_v) (v-split E k))
(where e_lbl (label l {(seq e_loop)} (in-hole E_v e*_0)))
loop)
(==> ((const i32 0) ((if tf e*_1 else e*_2) e*))
(seq* (block tf e*_2) e*)
if-false)
(==> ((const i32 k) ((if tf e*_1 else e*_2) e*))
(seq* (block tf e*_1) e*)
(side-condition (>= (term k) 1))
if-true)
(==> ((label n {e*_0} v*) e*_1)
(e*-append v* e*_1)
label-value)
(==> ((label n {e*_0} trap) e*_1)
(trap e*_1)
label-trap)
(==> ((label n {e*_0} (in-hole E ((br j) e*_1))) e*_2)
(e*-append (in-hole E_v e*_0) e*_2)
(where j (label-depth E))
(where (E_outer E_v) (v-split E n))
label-br)
(==> ((const i32 0) ((br-if j) e*))
e*
br-if-false)
(==> ((const i32 k) ((br-if j) e*))
(seq (br j) e*)
(side-condition (>= (term k) 1))
br-if-true)
(==> ((const i32 k) ((br-table i_1 ... i i_2 ...) e*))
(seq* (br i) e*)
(side-condition (= (length (term (i_1 ...))) (term k)))
br-table-index)
(==> ((const i32 k) ((br-table i_1 ... i) e*))
(seq* (br i) e*)
(side-condition (>= (term k) (length (term (i_1 ...)))))
br-table-end)
(--> (s F (in-hole E ((call j) e*)) i)
(s F (in-hole E ((call (store-func s i j)) e*)) i)
call-index)
(--> (s F (in-hole E ((const i32 j) ((call-indirect tf) e*))) i)
(s F (in-hole E ((call cl) e*)) i)
(where cl (store-tab s i j))
(where (func () tf local (t ...) e*_f) (cl-code cl))
call-indirect)
;; case where dynamic type check fails
(--> (s F (in-hole E ((const i32 j) ((call-indirect tf_0) e*))) i)
(s F (in-hole E (trap e*)) i)
(where cl (store-tab s i j))
(where (func () tf_1 local (t ...) e*_f) (cl-code cl))
(side-condition (not (equal? (term tf_0) (term tf_1))))
call-indirect-trap)
(++> (in-hole E ((call cl) e*_0))
(in-hole E_outer ((local m {(cl-inst cl) F} e*_block) e*_0))
(where (func () (-> (t_1 ...) (t_2 ...)) local (t ...) e*_code) (cl-code cl))
(where n ,(length (term (t_1 ...))))
(where m ,(length (term (t_2 ...))))
(where (E_outer E_v) (v-split E n))
(where F (F-append (v*->F (in-hole E_v ϵ)) ((const t 0) ...)))
(where e*_block (seq (block (-> () (t_2 ...)) e*_code)))
call-closure)
(==> ((local n {i F} v*) e*)
(e*-append v* e*)
local-value)
(==> ((local n {i F} (trap e*_0)) e*_1)
(trap ϵ)
local-trap)
(==> ((local n {i F} (in-hole E (return e*_0))) e*_1)
(in-hole E_v e*_1)
(where (E_outer E_v) (v-split E n))
local-return)
;; specifies how to reduce inside a local/frame instruction via a
;; recursive use of the reduction relation
(--> (s_0 F_0 (in-hole E ((local n {i F_1} e*_0) e*_2)) j)
(s_1 F_0 (in-hole E ((local n {i F_2} e*_1) e*_2)) j)
;; apply --> recursively
(where any_rec
,(apply-reduction-relation/tag-with-names
wasm-> (term (s_0 F_1 e*_0 i))))
;; only apply this rule if this reduction was valid
(side-condition (not (null? (term any_rec))))
;; the relation should be deterministic, so just take the first
(where (string_tag (s_1 F_2 e*_1 i)) ,(first (term any_rec)))
(computed-name (term string_tag)))
;; reductions for operating on locals in frames
(--> (s (name F (v_1 ... v v_2 ...)) (in-hole E ((get-local j) e*)) i)
(s F (in-hole E (v e*)) i)
(side-condition (= (length (term (v_1 ...))) (term j)))
get-local)
(--> (s (v_1 ... v v_2 ...) (in-hole E (v_new ((set-local j) e*))) i)
(s (v_1 ... v_new v_2 ...) (in-hole E e*) i)
(side-condition (= (length (term (v_1 ...))) (term j)))
set-local)
(==> (v ((tee-local j) e*))
(v (v ((set-local j) e*)))
tee-local)
;; reductions for operating on global store data
(--> (s F (in-hole E ((get-global j) e*)) i)
(s F (in-hole E ((store-glob s i j) e*)) i)
get-global)
(--> (s F (in-hole E (v ((set-global j) e*))) i)
(s_new F (in-hole E e*) i)
(where s_new (store-glob= s i j v))
set-global)
;; reductions for operating on memory
(--> (s F (in-hole E ((const i32 k) ((load t a o) e*))) i)
(s F (in-hole E ((const t (const-reinterpret t (b ...))) e*)) i)
(where (b ...) (store-mem s i ,(+ (term k) (term o)) (sizeof t)))
load)
(--> (s F (in-hole E ((const i32 k) ((load t tp sx a o) e*))) i)
(s F (in-hole E ((const t (const-reinterpret-packed t (b ...) sx)) e*)) i)
(where (b ...) (store-mem s i ,(+ (term k) (term o)) (sizeof tp)))
load-packed)
(--> (s F (in-hole E ((const i32 k) ((load t a o) e*))) i)
(s F (in-hole E (trap e*)) i)
(where #false (store-mem s i ,(+ (term k) (term o)) (sizeof t)))
load-trap)
(--> (s F (in-hole E ((const i32 k) ((load t tp sx a o) e*))) i)
(s F (in-hole E (trap e*)) i)
(where #false (store-mem s i ,(+ (term k) (term o)) (sizeof tp)))
load-trap-packed)
(--> (s F (in-hole E ((const i32 k) ((const t c) ((store t a o) e*)))) i)
(s_new F (in-hole E e*) i)
(where n (sizeof t))
(where s_new (store-mem= s i ,(+ (term k) (term o)) n (bits n t c)))
store)
(--> (s F (in-hole E ((const i32 k) ((const t c) ((store t tp a o) e*)))) i)
(s_new F (in-hole E e*) i)
(where n (sizeof tp))
(where s_new (store-mem= s i ,(+ (term k) (term o)) n (bits n t c)))
store-packed)
(--> (s F (in-hole E ((const i32 k) ((const t c) ((store t tp ... a o) e*)))) i)
(s F (in-hole E (trap e*)) i)
(where n (sizeof t))
(where #false (store-mem= s i ,(+ (term k) (term o)) n (bits n t c)))
store-trap)
(--> (s F (in-hole E ((const i32 k) ((const t c) ((store t tp a o) e*)))) i)
(s F (in-hole E (trap e*)) i)
(where n (sizeof tp))
(where #false (store-mem= s i ,(+ (term k) (term o)) n (bits n t c)))
store-trap-packed)
(--> (s F (in-hole E (current-memory e*)) i)
(s F (in-hole E ((const i32 (memory-size s i)) e*)) i)
current-memory)
(--> (s F (in-hole E ((const i32 k) (grow-memory e*))) i)
(s_new F (in-hole E ((const i32 j_newsize) e*)) i)
(where (s_new j_newsize) (expand-memory s i k))
grow-memory)
;; failure case for grow-memory omitted, alternatively we could institute a cap
;; and return -1 for that cap in the model
with
[(--> (s F (in-hole E x) i)
(s F (in-hole E y) i))
(==> x y)]
[(--> (s F x i)
(s F y i))
(++> x y)]))
(module+ test
(require rackunit)
(define (wasm-eval a-term)
(define results (apply-reduction-relation* wasm-> a-term))
(unless (= (length results) 1)
(error "wasm-> had non-deterministic evaluation or no result"))
(define result (first results))
(define result->eval-result
(term-match/single
wasm-runtime-lang
[{s F (v ϵ) i} (term v)]
[{s F (trap ϵ) i} (term trap)]))
(result->eval-result (first results)))
(define-syntax-rule (test-wasm--> x y)
(test--> wasm-> x y))
(define-syntax-rule (test-wasm-->> x y)
(test-->> wasm-> x y))
;; for testing with side effects
(define-syntax-rule (test-wasm-eval x y)
(test-equal (wasm-eval x) y))
;; test helpers and terms
(define default-memory
(make-list *page-size* 0))
(define mt-s (term {(inst) (tab) (mem)}))
(define-syntax-rule (simple-config e*)
(term (,mt-s () e* 0)))
;; sanity checks for the grammar
(check-not-false
(redex-match wasm-runtime-lang s mt-s))
(check-not-false
(redex-match wasm-runtime-lang F (second (simple-config ϵ))))
(check-not-false
(redex-match wasm-runtime-lang v (term (const i32 42))))
(check-not-false
(redex-match wasm-runtime-lang
(in-hole E (v (drop e*)))
(term ((const i32 42) (drop ϵ)))))
;; test primitives
(test-wasm--> (simple-config (seq (const f32 42.0) (neg f32)))
(simple-config (seq (const f32 -42.0))))
(test-wasm--> (simple-config (seq (const i32 42) (eqz i32)))
(simple-config (seq (const i32 0))))
(test-wasm--> (simple-config (seq (const i32 42) (const i32 42) (ne i32)))
(simple-config (seq (const i32 0))))
(test-wasm--> (simple-config (seq (const i32 1) (const i32 1) (add i32)))
(simple-config (seq (const i32 2))))
(test-wasm--> (simple-config (seq (const i32 1) (const i32 0) (add i32)))
(simple-config (seq (const i32 1))))
(test-wasm--> (simple-config (seq (const i32 -1) (const i32 -1) (add i32)))
(simple-config (seq (const i32 -2))))
(test-wasm--> (simple-config (seq (const i32 -1) (const i32 1) (add i32)))
(simple-config (seq (const i32 0))))
(test-wasm--> (simple-config (seq (const i32 #x7fffffff) (const i32 1) (add i32)))
(simple-config (seq (const i32 #x80000000))))
(test-wasm--> (simple-config (seq (const i32 #x80000000) (const i32 -1) (add i32)))
(simple-config (seq (const i32 #x7fffffff))))
(test-wasm--> (simple-config (seq (const i32 #x3fffffff) (const i32 1) (add i32)))
(simple-config (seq (const i32 #x40000000))))
(test-wasm--> (simple-config (seq (const i32 1) (const i32 1) (sub i32)))
(simple-config (seq (const i32 0))))
(test-wasm--> (simple-config (seq (const i32 1) (const i32 0) (sub i32)))
(simple-config (seq (const i32 1))))
(test-wasm--> (simple-config (seq (const i32 -1) (const i32 -1) (sub i32)))
(simple-config (seq (const i32 0))))
(test-wasm--> (simple-config (seq (const i32 #x7fffffff) (const i32 -1) (sub i32)))
(simple-config (seq (const i32 #x80000000))))
(test-wasm--> (simple-config (seq (const i32 #x80000000) (const i32 1) (sub i32)))
(simple-config (seq (const i32 #x7fffffff))))
(test-wasm--> (simple-config (seq (const i32 #x80000000) (const i32 #x80000000) (sub i32)))
(simple-config (seq (const i32 0))))
(test-wasm--> (simple-config (seq (const i32 #x3fffffff) (const i32 -1) (sub i32)))
(simple-config (seq (const i32 #x40000000))))
(test-wasm--> (simple-config (seq (const i32 1) (const i32 1) (mul i32)))
(simple-config (seq (const i32 1))))
(test-wasm--> (simple-config (seq (const i32 1) (const i32 0) (mul i32)))
(simple-config (seq (const i32 0))))
(test-wasm--> (simple-config (seq (const i32 -1) (const i32 -1) (mul i32)))
(simple-config (seq (const i32 1))))
(test-wasm--> (simple-config (seq (const i32 #x10000000) (const i32 4096) (mul i32)))
(simple-config (seq (const i32 0))))
(test-wasm--> (simple-config (seq (const i32 ,(u->s #x80000000 'i32)) (const i32 0) (mul i32)))
(simple-config (seq (const i32 0))))