forked from reactorlabs/sourir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.ml
1957 lines (1863 loc) · 41.2 KB
/
tests.ml
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
open OUnit
open Instr
let ok _ = true
let trace_is li =
fun conf -> Eval.read_trace conf = li
let has_var x v =
fun conf -> Eval.(lookup conf.heap conf.env x = v)
let returns n =
fun conf -> Eval.(conf.status = Result n)
let (&&&) p1 p2 conf = (p1 conf) && (p2 conf)
let drop_annots = fst
let no_input = IO.no_input
let input = IO.list_input
let run prog input pred () =
Check.well_formed prog;
Scope.check_program prog;
let final_conf = Eval.run_forever input prog in
assert (pred final_conf)
let run_unchecked prog input pred () =
let final_conf = Eval.run_forever input prog in
assert (pred final_conf)
let parse str : program =
try Parse.program_of_string str
with Parse.Error error as exn ->
Parse.report_error error;
raise exn
let test_print = parse
" print 1
print 2
stop 0
"
let test_decl_var = parse
" var x = 1
print x
stop 0
"
let test_incr_var = parse
" var x = 1
x <- (x + 1)
print x
stop 0
"
let test_var = parse
" var x = 1
print x
x <- 2
print x
stop 0
"
let test_jump = parse
" var x = true
goto jmp
x <- false
goto jmp
jmp:
"
let test_overloading = parse
" var b = true
var x = 1
var y = x
goto jump
b <- false
goto jump
jump:
x <- 2
stop 0
"
let test_neg a = parse (
" var x = "^ string_of_int a ^"
var y = (-x)
")
let test_add a b = parse (
" var x = "^a^"
var y = "^b^"
var z = (x + y)
")
let test_sub a b = parse (
" var x = "^a^"
var y = "^b^"
var z = (x - y)
")
let test_mult a b = parse (
" var x = "^a^"
var y = "^b^"
var z = (x * y)
")
let test_div a b = parse (
" var x = "^a^"
var y = "^b^"
var z = (x / y)
")
let test_mod a b = parse (
" var x = "^a^"
var y = "^b^"
var z = (x % y)
")
let test_eq a b = parse (
" var x = "^ string_of_int a ^"
var y = "^ string_of_int b ^"
var z = (x==y)
")
let test_neq a b = parse (
" var x = "^ string_of_int a ^"
var y = "^ string_of_int b ^"
var z = (x!=y)
")
let test_lt a b = parse (
" var x = "^ string_of_int a ^"
var y = "^ string_of_int b ^"
var z = (x<y)
")
let test_lte a b = parse (
" var x = "^ string_of_int a ^"
var y = "^ string_of_int b ^"
var z = (x<=y)
")
let test_gt a b = parse (
" var x = "^ string_of_int a ^"
var y = "^ string_of_int b ^"
var z = (x>y)
")
let test_gte a b = parse (
" var x = "^ string_of_int a ^"
var y = "^ string_of_int b ^"
var z = (x>=y)
")
let test_not a = parse (
" var x = "^ string_of_bool a ^"
var y = (!x)
")
let test_and a b = parse (
" var x = "^ string_of_bool a^"
var y = "^ string_of_bool b^"
var z = (x && y)
")
let test_or a b = parse (
" var x = "^ string_of_bool a^"
var y = "^ string_of_bool b^"
var z = (x || y)
")
let test_sum limit_ = parse (
" var i = 0
var sum = 0
var limit = "^string_of_int limit_^"
goto loop
loop:
var ax = (i==limit)
branch ax $continue $loop_body
$loop_body:
drop ax
sum <- (sum+i)
i <- (i+1)
goto loop
$continue:
drop ax
stop 0
")
let test_broken_scope_1 = parse
" print x
"
let test_broken_scope_2 = parse
" goto l
var x = 0
l:
print x
"
let test_broken_scope_3 = parse
" var y = false
branch y $cont $next
$next:
var x = 0
drop x
goto ft
$cont:
goto ft
ft:
print x
"
let test_broken_scope_4 = parse
"var x = 0
var y = 0
{x} var z = false
z <- (x == y)
"
let test_broken_scope_4_fixed = parse
"var x = 0
var y = 0
{x, ...} var z = false
z <- (x == y)
"
let test_broken_scope_5 = parse
"var x = 0
var y = 0
{w, ...} var z = false
z <- (x == y)
"
let test_scope_1 test_var1 test_var2 = parse (
" var t = false
var c
branch t $a $b
$a:
var a = 0
c <- 0
drop a
goto cont
$b:
var b = 0
c <- 0
drop b
goto cont
cont:
var res = (" ^ test_var1 ^ " + " ^ test_var2 ^ ")
")
let test_read_print = parse
" var n
var b
read b
read n
print n
drop n
print b
drop b
"
let test_read_print_err = parse
" var n
read b
read n
print n
print b
"
let test_read_print_err_2 = parse
" var n
var b
read b
print n
print b
"
let test_read_print_err_3 = parse
" var n
var b
read b
read n
drop b
print n
print b
"
let test_read_print_err_4 = parse
" var n
var b
read b
read n
b <- nil
print n
print b
"
let undeclared missing_vars pos =
Scope.ScopeExceptionAt("main", "anon", Scope.UndeclaredVariable (VarSet.of_list missing_vars, pos))
let extraneous extra_vars pos =
Scope.ScopeExceptionAt("main", "anon", Scope.ExtraneousVariable (VarSet.of_list extra_vars, pos))
let infer_broken_scope program exn = function() ->
let test () = Scope.check_program program in
assert_raises exn test
let assert_equal_string s1 s2 = assert_equal ~printer:(fun x -> x) s1 s2
let test_parse_disasm_file file = function() ->
let prog1 = Parse.program_of_file file in
let disasm1 = Disasm.disassemble_s prog1 in
let prog2 = Parse.program_of_string disasm1 in
let disasm2 = Disasm.disassemble_s prog2 in
assert_equal_string disasm1 disasm2
let test_parse_disasm str = function() ->
let prog1 = Parse.program_of_string str in
let disasm1 = Disasm.disassemble_s prog1 in
let prog2 = Parse.program_of_string disasm1 in
let disasm2 = Disasm.disassemble_s prog2 in
assert_equal_string disasm1 disasm2
let test_disasm_parse prog = function() ->
let disasm1 = Disasm.disassemble_s prog in
let prog2 = Parse.program_of_string disasm1 in
let disasm2 = Disasm.disassemble_s prog2 in
assert_equal_string disasm1 disasm2
let test_branch = parse
"var x = 9
var y = 10
var r = 1
branch (x == y) $l1 $l2
$l1:
r <- 2
goto c
$l2:
r <- 3
goto c
c:
print r
"
let test_branch_pruned =
" var x = 9
var y = 10
osr cp_2 [(x == y)] (main, anon, cp_2) [var x = x, var y = y]
var r = 1
r <- 3
print r
"
let test_loop_branch = parse
"var x = 9
var y = 10
var r = 1
goto loop
$loop_b:
goto loop
loop:
branch (x == y) $l1 $l2
$l1:
r <- 2
goto c
$l2:
r <- 3
goto c
c:
print r
branch (r == 0) $loop_b $end
$end:
"
let test_loop_branch_pruned =
" var x = 9
var y = 10
osr cp_2 [(x == y)] (main, anon, cp_2) [var x = x, var y = y]
var r = 1
goto loop
$loop_b:
goto loop
loop:
r <- 3
print r
branch (r == 0) $loop_b $end
$end:
"
let test_double_loop = parse
"{} var i
i <- 0
var sum = 0
var limit = 4
goto loop1
loop1:
branch (i != limit) $loop_body1 $continue
$loop_body1:
var i2 = 0
var sum2 = 0
goto loop2
loop2:
branch (i2 != limit) $loop_body2 $continue2
$loop_body2:
print i2
sum2 <- (sum + i2)
i2 <- (i2 + 1)
goto loop2
$continue2:
sum <- (sum + sum2)
drop i2
drop sum2
i <- (i + 1)
goto loop1
$continue:
print sum
"
let test_branch_pruning_exp prog expected =
let open Transform in
let prog = try_opt (as_opt_program Transform_assumption.insert_checkpoints) prog in
let prune = try_opt (combine_opt
[branch_prune;
(as_opt_function Transform_assumption.hoist_assumption);
cleanup_all;
(as_opt_function Transform_assumption.remove_empty_osr)]) in
let prog2 = { prog with main = prune prog.main } in
assert_equal_string expected
(Disasm.disassemble_instrs_s (List.hd prog2.main.body).instrs)
let test_branch_pruning prog deopt =
let open Eval in
let open Transform in
let prog = try_opt (as_opt_program Transform_assumption.insert_checkpoints) prog in
let prog2 = { prog with main = try_opt branch_prune prog.main } in
let res1 = Eval.run_forever no_input prog in
let res2 = Eval.run_forever no_input prog2 in
assert_equal res1.trace res2.trace;
assert_equal res2.deopt deopt
let assert_equal_sorted li1 li2 =
assert_equal (List.sort compare li1) (List.sort compare li2)
let test_pred = parse
"l1:
goto l2
l3:
branch x $l1_a $l2_a
$l1_a:
goto l1
$l2_a:
goto l2
l2:
branch x $l1_b $l3_b
$l1_b:
goto l1
$l3_b:
goto l3
stop 0
goto l1
"
let do_test_pred = function () ->
let v = Instr.active_version test_pred.main in
let pred = Analysis.predecessors v.instrs in
let pred pc = pred.(pc) in
assert_equal_sorted (pred 0) [5; 11; 15];
assert_equal_sorted (pred 1) [0];
assert_equal_sorted (pred 2) [13];
assert_equal_sorted (pred 4) [3];
assert_equal_sorted (pred 8) [1; 7];
assert_equal_sorted (pred 9) [8];
assert_equal_sorted (pred 14) [];
assert_equal_sorted (pred 15) []
;;
let test_df = parse
"var a = 1
var b = 2
var d = (a+b)
# space
b <- 3
var z = (a+b)
goto l_
$l1:
goto l_
$l:
goto l_
l_:
var y = (a+b)
b <- 4
branch b $l $l2
# gap
$l2:
var y = (y+b)
branch b $l1 $l3
$l3:
"
let do_test_liveness = function () ->
let open Analysis in
let v = (as_analysis_input test_df.main (Instr.active_version test_df.main)) in
let live = live v in
assert_equal_sorted (live 0) ["a"];
assert_equal_sorted (live 1) ["a";"b"];
assert_equal_sorted (live 2) ["a"];
assert_equal_sorted (live 3) ["a"];
assert_equal_sorted (live 4) ["a";"b"];
assert_equal_sorted (live 5) ["a";"b"];
assert_equal_sorted (live 6) ["a";"b"];
assert_equal_sorted (live 12) ["a";"y"];
assert_equal_sorted (live 13) ["a";"b";"y"];
assert_equal_sorted (live 14) ["a";"b";"y"];
assert_equal_sorted (live 15) ["a";"b";"y"];
assert_equal_sorted (live 16) ["a";"b";"y"];
assert_equal_sorted (live 17) ["a";"b"];
assert_equal_sorted (live 18) ["a";"b"];
assert_equal_sorted (live 0) ["a"]
;;
let do_test_used = function () ->
let open Analysis in
let v = (as_analysis_input test_df.main (Instr.active_version test_df.main)) in
let uses = uses v in
assert_equal_sorted (PcSet.elements (uses 0)) [2;5;12];
assert_equal_sorted (PcSet.elements (uses 1)) [2];
assert_equal_sorted (PcSet.elements (uses 2)) [];
assert_equal_sorted (PcSet.elements (uses 4)) [5;12];
assert_equal_sorted (PcSet.elements (uses 5)) [];
assert_equal_sorted (PcSet.elements (uses 12)) [17];
assert_equal_sorted (PcSet.elements (uses 13)) [12;14;17;18];
assert_equal_sorted (PcSet.elements (uses 14)) [];
assert_equal_sorted (PcSet.elements (uses 16)) [];
assert_equal_sorted (PcSet.elements (uses 17)) [];
assert_equal_sorted (PcSet.elements (uses 18)) [];
assert_equal_sorted (PcSet.elements (uses 6)) []
;;
let do_test_reaching = function () ->
let open Analysis in
let v = (as_analysis_input test_df.main (Instr.active_version test_df.main)) in
let reaching = reaching v in
assert_equal_sorted (PosSet.elements (reaching 0)) [];
assert_equal_sorted (PosSet.elements (reaching 1)) [];
assert_equal_sorted (PosSet.elements (reaching 2)) [Analysis.Instr 0; Analysis.Instr 1];
assert_equal_sorted (PosSet.elements (reaching 5)) [Analysis.Instr 0; Analysis.Instr 4];
assert_equal_sorted (PosSet.elements (reaching 12)) [Analysis.Instr 13; Analysis.Instr 0; Analysis.Instr 4];
assert_equal_sorted (PosSet.elements (reaching 17)) [Analysis.Instr 13; Analysis.Instr 12];
assert_equal_sorted (PosSet.elements (reaching 0)) []
;;
let test_df2 = parse
" goto jmp
$start:
var i = 1
var c = 0
var v = 123
var x = 0
loop:
branch (i==10) $loop_end $loop_begin
$loop_begin:
var w = 3
branch (c==2) $tr $fs
$tr:
w <- 3
goto ct
$fs:
branch (c==4) $tr2 $fs2
$tr2:
stop 0
$fs2:
w <- 4
goto ct
ct:
x <- w
v <- (c+1)
i <- (i+v)
goto loop
$loop_end:
print i
print x
# bla
goto end
jmp:
branch true $start $end
$end:
"
let do_test_codemotion = function () ->
let open Transform in
let t = parse "
goto bla
$loop:
goto loop_
loop_:
y <- z
x <- (x + y)
branch (x==10) $end $loop
$end:
drop x
drop y
drop z
stop 0
bla:
var z = 1
var x = 1
var y = z
goto loop_
" in
let expected = parse "
goto bla
$loop:
goto loop_
loop_:
x <- (x + y_1)
branch (x == 10) $end $loop
$end:
drop x
drop y
drop z
drop y_1
stop 0
bla:
var z = 1
var y_1 = z
var x = 1
var y = z
goto loop_
" in
let res = { t with main = try_opt hoist_assignment t.main } in
assert_equal_string (Disasm.disassemble_s expected) (Disasm.disassemble_s res);
let t = parse "
var x = 1
var y = 2
goto loop_
$loop:
goto loop_
loop_:
y <- 1
x <- (x + y)
branch (x==10) $end $loop
$end:
drop x
drop y
" in
let expected = parse "
var y_1 = 1
var x = 1
var y = 2
goto loop_
$loop:
goto loop_
loop_:
x <- (x + y_1)
branch (x == 10) $end $loop
$end:
drop x
drop y
drop y_1
" in
let res = { t with main = try_opt hoist_assignment t.main } in
assert_equal_string (Disasm.disassemble_s expected) (Disasm.disassemble_s res);
let t = parse "
var a = 1
var b = 2
branch (a==b) $ba $bb_1
$ba:
var d = 1
branch (d==d) $ig $bb_2
$ig:
print d
# move over this comment
d <- d
print d
drop d
goto bb
$bb_1:
goto bb
$bb_2:
goto bb
bb:
drop a
drop b
" in
let expected = parse "
var a = 1
var b = 2
branch (a == b) $ba $bb_1
$ba:
var d = 1
var d_1 = d
branch (d == d) $ig $bb_2
$ig:
print d
# move over this comment
print d_1
drop d
drop d_1
goto bb
$bb_1:
goto bb
$bb_2:
drop d
drop d_1
goto bb
bb:
drop a
drop b
" in
let res = { t with main = try_opt hoist_assignment t.main } in
assert_equal_string (Disasm.disassemble_s expected) (Disasm.disassemble_s res);
let t = parse "
var a = 1
var b = 2
branch (a==b) $ba $bb_1
$ba:
var d = 1
branch (d==d) $ig $bb_2
$ig:
print d
# move over this comment
d <- d
print d
drop d
branch (d==d) $bb_3 $bb_4
$bb_1:
goto bb
$bb_2:
goto bb
$bb_3:
goto bb
$bb_4:
goto bb
bb:
drop a
drop b
" in
let expected = parse "
var a = 1
var b = 2
branch (a == b) $ba $bb_1
$ba:
var d = 1
var d_1 = d
branch (d == d) $ig $bb_2
$ig:
print d
# move over this comment
print d_1
drop d
branch (d_1 == d_1) $bb_3 $bb_4
$bb_1:
goto bb
$bb_2:
drop d
drop d_1
goto bb
$bb_3:
drop d_1
goto bb
$bb_4:
drop d_1
goto bb
bb:
drop a
drop b
" in
let res = { t with main = try_opt hoist_assignment t.main } in
assert_equal_string (Disasm.disassemble_s expected) (Disasm.disassemble_s res);
let t = parse "
var a = true
var b = (a==1)
branch b $isint $cont
$isint:
a <- (a+2)
goto cont_
$cont:
goto cont_
cont_:
" in
let res = { t with main = try_opt hoist_assignment t.main } in
(* unchanged since (a+2) is only valid to execute if (a==1) *)
assert_equal_string (Disasm.disassemble_s t) (Disasm.disassemble_s res);
let t = parse "
var x = 1
var y = 2
goto loop_
$loop:
goto loop_
loop_:
y <- x
x <- (x + y)
branch (x==10) $end $loop
$end:
" in
let res = { t with main = try_opt hoist_assignment t.main } in
(* cannot hoist because depends on previous loop iteration *)
assert_equal_string (Disasm.disassemble_s t) (Disasm.disassemble_s res);
let t = parse "
var x = 1
var y = 2
goto loop_
$loop:
goto loop_
loop_:
branch (x==5) $la $lb
$la:
y <- 1
goto ft
$lb:
goto ft
ft:
x <- (x + y)
branch (x==10) $end $loop
$end:
" in
let res = { t with main = try_opt hoist_assignment t.main } in
(* cannot hoist because if (x==5) then y is modified *)
assert_equal_string (Disasm.disassemble_s t) (Disasm.disassemble_s res);
()
let do_test_minimize_lifetime = function () ->
let open Transform in
let t = parse "
var a = 12
var b = false
var c
branch b $o1 $o2
$o1:
a <- 22
a <- 1
print a
goto ft
$o2:
goto ft
ft:
print b
drop b
goto x
x:
var b = 3
print b
stop 0
" in
let expected = parse "
var a = 12
var b = false
branch b $o1 $o2
$o1:
a <- 1
print a
drop a
goto ft
$o2:
drop a
goto ft
ft:
print b
drop b
goto x
x:
var b = 3
print b
drop b
stop 0
" in
let res = { t with main = try_opt minimize_liverange t.main } in
assert_equal_string (Disasm.disassemble_s expected) (Disasm.disassemble_s res);
()
let do_test_const_fold_driver () =
let open Transform in
let test t e =
let input, expected = (parse t), (parse e) in
let output = try_opt (as_opt_program const_fold) input in
if (Disasm.disassemble_s output) <> (Disasm.disassemble_s expected) then begin
Printf.printf "\ninput: '%s'\noutput: '%s'\nexpected: '%s'\n%!"
(Disasm.disassemble_s input)
(Disasm.disassemble_s output)
(Disasm.disassemble_s expected);
assert false
end in
(* Simple constant propagation test case; sanity check *)
test {input|
var x = 1
print x
|input} {expect|
print 1
|expect};
(* Constant propagation with some mutation *)
test {input|
var w = 0
var z = w
w <- 1
print w
print z
|input} {expect|
var w = 0
print 1
print 0
|expect};
(* Folding and propagation *)
test {input|
var x = 1
var y = (2 + x)
var z = (3 + y)
print z
|input} {expect|
print 6
|expect};
(* Test with branching *)
test {input|
var x = (1 + 0)
branch (1==1) $l1 $l2
$l1:
drop x
goto next
$l2:
print x
drop x
goto next
next:
var y = 1
branch (1!=0) $l3 $l4
$l3:
print y
drop y
stop 0
$l4:
drop y
stop 0
|input} {expect|
var x = 1
branch true $l1 $l2
$l1:
drop x
goto next
$l2:
print 1
drop x
goto next
next:
var y = 1
branch true $l3 $l4
$l3:
print 1
drop y
stop 0
$l4:
drop y
stop 0
|expect};
(* Test with booleans, updating vars, and branching *)
test {input|
var a = 1
var b = 2
var ccc = 5
var cc = ccc
var c = (cc + 0)
c <- (a + b)
var d = true
branch d $l1 $l2
$l1:
c <- (c + a)
print a
goto fin
$l2:
c <- (c + b)
print b
goto fin
fin:
print c
stop 0
|input} {expect|
var c = 5
branch true $l1 $l2
$l1:
c <- 4
print 1
goto fin
$l2:
c <- 5
print 2
goto fin
fin:
print c
stop 0
|expect};
(* Test with a loop *)
test {input|
var n = 10
goto bla
$loop:
goto loop_
loop_:
y <- z
x <- (x + z)
branch (x==n) $end $loop
$end: