-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_t31.wat
152 lines (152 loc) · 3.65 KB
/
gen_t31.wat
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
;; C Compilation to J-- Code
;; File: final/gen_t31.wat
;; Standard prelude:
(module
(import "host" "exit" (func $exit))
(import "host" "getchar" (func $getchar (result i32)))
(import "host" "putchar" (func $putchar (param i32)))
(func $halt
call $exit
)
(func $printc (param $char i32)
local.get $char
call $putchar
)
(func $prints (param $offset i32) (param $length i32)
(local $I0 i32)
i32.const 0
local.set $I0
(block $B1
(loop $L1
local.get $length
local.get $I0
i32.le_s
br_if $B1
local.get $offset
local.get $I0
i32.add
i32.load
call $printc
local.get $I0
i32.const 1
i32.add
local.set $I0
br $L1
)
)
)
(func $printb (param $b i32)
;; select with 3rd operand 0 selects 2nd operand
;; select with 3rd operand 1 selects 1st operand
(local $I0 i32)
i32.const 5
i32.const 6
local.get $b
select
local.set $I0
i32.const 0
i32.const 5
local.get $b
select
local.get $I0
call $prints
)
(func $printi (param $n i32)
(local $I0 i32)
(local $I1 i32)
(block $B5
local.get $n
i32.const 0
i32.ne
br_if $B5
i32.const 48
call $printc
return
)
i32.const 1000000000
local.set $I0
(block $B0
(loop $L0
local.get $n
local.get $I0
i32.div_s
i32.const 0
i32.gt_u
br_if $B0
local.get $I0
i32.const 10
i32.div_s
local.set $I0
br $L0
)
)
(block $B2
(loop $L2
local.get $n
i32.const 0
i32.ge_s
br_if $B2
i32.const 45
call $printc
)
)
(block $B1
(loop $L1
local.get $I0
i32.eqz
br_if $B1
local.get $n
local.get $I0
i32.div_s
local.set $I1
(block $B3
local.get $I1
i32.const 0
i32.gt_s
br_if $B3
i32.const 0
local.get $I1
i32.sub
local.set $I1
)
local.get $I1
i32.const 10
i32.rem_u
i32.const 48
i32.add
call $printc
local.get $I0
i32.const 10
i32.div_s
local.set $I0
br $L1
)
)
)
;; End of standard prelude.
(global $Gj (mut i32) (i32.const 0))
(global $Gi (mut i32) (i32.const 0))
(func $main
(local $T0 i32)
(local $T1 i32)
i32.const 12
global.set $Gj
call $foo
global.get $Gj
global.get $Gi
i32.const 2
i32.sub
i32.div_s
call $printi
)
(func $foo
(local $T0 i32)
(local $T1 i32)
i32.const 2
global.set $Gi
)
(start $main)
(data 0 (i32.const 0) "true")
(data 0 (i32.const 5) "false")
(memory 1)
)