https://github.com/akkartik/mu/blob/master/apps/tile/value-stack.mu
1
2
3 type value-stack {
4 data: (handle array value)
5 top: int
6 }
7
8 fn initialize-value-stack _self: (addr value-stack), n: int {
9 var self/esi: (addr value-stack) <- copy _self
10 var d/edi: (addr handle array value) <- get self, data
11 populate d, n
12 var top/eax: (addr int) <- get self, top
13 copy-to *top, 0
14 }
15
16 fn clear-value-stack _self: (addr value-stack) {
17 var self/esi: (addr value-stack) <- copy _self
18 var top/eax: (addr int) <- get self, top
19 copy-to *top, 0
20 }
21
22 fn push-int-to-value-stack _self: (addr value-stack), _val: int {
23 var self/esi: (addr value-stack) <- copy _self
24 var top-addr/ecx: (addr int) <- get self, top
25 var data-ah/edx: (addr handle array value) <- get self, data
26 var data/eax: (addr array value) <- lookup *data-ah
27 var top/edx: int <- copy *top-addr
28 var dest-offset/edx: (offset value) <- compute-offset data, top
29 var dest-addr/edx: (addr value) <- index data, dest-offset
30 var dest-addr2/eax: (addr int) <- get dest-addr, int-data
31 var val/esi: int <- copy _val
32
33 copy-to *dest-addr2, val
34 increment *top-addr
35 }
36
37 fn push-string-to-value-stack _self: (addr value-stack), val: (handle array byte) {
38 var self/esi: (addr value-stack) <- copy _self
39 var top-addr/ecx: (addr int) <- get self, top
40 var data-ah/edx: (addr handle array value) <- get self, data
41 var data/eax: (addr array value) <- lookup *data-ah
42 var top/edx: int <- copy *top-addr
43 var dest-offset/edx: (offset value) <- compute-offset data, top
44 var dest-addr/edx: (addr value) <- index data, dest-offset
45 var dest-addr2/eax: (addr handle array byte) <- get dest-addr, text-data
46 copy-handle val, dest-addr2
47 var dest-addr3/eax: (addr int) <- get dest-addr, type
48
49
50
51
52
53
54 copy-to *dest-addr3, 1
55 increment *top-addr
56 }
57
58 fn push-array-to-value-stack _self: (addr value-stack), val: (handle array value) {
59 var self/esi: (addr value-stack) <- copy _self
60 var top-addr/ecx: (addr int) <- get self, top
61 var data-ah/edx: (addr handle array value) <- get self, data
62 var data/eax: (addr array value) <- lookup *data-ah
63 var top/edx: int <- copy *top-addr
64 var dest-offset/edx: (offset value) <- compute-offset data, top
65 var dest-addr/edx: (addr value) <- index data, dest-offset
66 var dest-addr2/eax: (addr handle array value) <- get dest-addr, array-data
67 copy-handle val, dest-addr2
68
69 var dest-addr3/eax: (addr int) <- get dest-addr, type
70 copy-to *dest-addr3, 2
71 increment *top-addr
72 }
73
74 fn push-value-stack _self: (addr value-stack), val: (addr value) {
75 var self/esi: (addr value-stack) <- copy _self
76 var top-addr/ecx: (addr int) <- get self, top
77 var data-ah/edx: (addr handle array value) <- get self, data
78 var data/eax: (addr array value) <- lookup *data-ah
79 var top/edx: int <- copy *top-addr
80 var dest-offset/edx: (offset value) <- compute-offset data, top
81 var dest-addr/edx: (addr value) <- index data, dest-offset
82 copy-object val, dest-addr
83 increment *top-addr
84 }
85
86 fn pop-int-from-value-stack _self: (addr value-stack) -> _/eax: int {
87 var self/esi: (addr value-stack) <- copy _self
88 var top-addr/ecx: (addr int) <- get self, top
89 {
90 compare *top-addr, 0
91 break-if->
92 return -1
93 }
94 decrement *top-addr
95 var data-ah/edx: (addr handle array value) <- get self, data
96 var data/eax: (addr array value) <- lookup *data-ah
97 var top/edx: int <- copy *top-addr
98 var dest-offset/edx: (offset value) <- compute-offset data, top
99 var result-addr/eax: (addr value) <- index data, dest-offset
100 var result-addr2/eax: (addr int) <- get result-addr, int-data
101 return *result-addr2
102 }
103
104 fn value-stack-empty? _self: (addr value-stack) -> _/eax: boolean {
105 var self/esi: (addr value-stack) <- copy _self
106 var top/eax: (addr int) <- get self, top
107 compare *top, 0
108 {
109 break-if-!=
110 return 1
111 }
112 return 0
113 }
114
115 fn value-stack-length _self: (addr value-stack) -> _/eax: int {
116 var self/esi: (addr value-stack) <- copy _self
117 var top-addr/eax: (addr int) <- get self, top
118 return *top-addr
119 }
120
121 fn value-stack-max-width _self: (addr value-stack) -> _/eax: int {
122 var self/esi: (addr value-stack) <- copy _self
123 var data-ah/edi: (addr handle array value) <- get self, data
124 var _data/eax: (addr array value) <- lookup *data-ah
125 var data/edi: (addr array value) <- copy _data
126 var top-addr/ecx: (addr int) <- get self, top
127 var i/ebx: int <- copy 0
128 var result: int
129 {
130 compare i, *top-addr
131 break-if->=
132 var o/edx: (offset value) <- compute-offset data, i
133 var v/edx: (addr value) <- index data, o
134 var w/eax: int <- value-width v, 1
135
136 {
137 compare w, result
138 break-if-<=
139 copy-to result, w
140 }
141 i <- increment
142 loop
143 }
144 return result
145 }
146
147 fn value-width _v: (addr value), top-level: boolean -> _/eax: int {
148 var v/esi: (addr value) <- copy _v
149 var type/eax: (addr int) <- get v, type
150 {
151 compare *type, 0
152 break-if-!=
153 var v-int/edx: (addr int) <- get v, int-data
154 var result/eax: int <- decimal-size *v-int
155 return result
156 }
157 {
158 compare *type, 1
159 break-if-!=
160 var s-ah/eax: (addr handle array byte) <- get v, text-data
161 var s/eax: (addr array byte) <- lookup *s-ah
162 compare s, 0
163 break-if-=
164 var result/eax: int <- length s
165 compare result, 0xd
166 {
167 break-if-<=
168 result <- copy 0xd
169 }
170
171
172
173 compare top-level, 0
174 {
175 break-if-!=
176 result <- add 2
177 }
178 return result
179 }
180 {
181 compare *type, 2
182 break-if-!=
183 var a-ah/eax: (addr handle array value) <- get v, array-data
184 var a/eax: (addr array value) <- lookup *a-ah
185 compare a, 0
186 break-if-=
187 var result/eax: int <- array-width a
188 return result
189 }
190 {
191 compare *type, 3
192 break-if-!=
193 var f-ah/eax: (addr handle buffered-file) <- get v, file-data
194 var f/eax: (addr buffered-file) <- lookup *f-ah
195 compare f, 0
196 break-if-=
197
198 return 4
199 }
200 return 0
201 }
202
203
204 fn array-width _a: (addr array value) -> _/eax: int {
205 var a/esi: (addr array value) <- copy _a
206 var max/ecx: int <- length a
207 var i/eax: int <- copy 0
208 var result/edi: int <- copy 0
209 {
210 compare i, max
211 break-if->=
212 {
213 compare i, 0
214 break-if-=
215 result <- increment
216 }
217 var off/ecx: (offset value) <- compute-offset a, i
218 var x/ecx: (addr value) <- index a, off
219 {
220 var w/eax: int <- value-width x, 0
221 result <- add w
222 }
223 i <- increment
224 loop
225 }
226
227
228 return result
229 }
230
231 fn save-lines in-h: (handle array (handle array byte)), _out-ah: (addr handle array value) {
232 var _in/eax: (addr array (handle array byte)) <- lookup in-h
233 var in/esi: (addr array (handle array byte)) <- copy _in
234 var len/ecx: int <- length in
235 var out-ah/edi: (addr handle array value) <- copy _out-ah
236 populate out-ah, len
237 var out/eax: (addr array value) <- lookup *out-ah
238
239 var i/ebx: int <- copy 0
240 {
241 compare i, len
242 break-if->=
243
244
245 var src/ecx: (addr handle array byte) <- index in, i
246 var dest-offset/edx: (offset value) <- compute-offset out, i
247 var dest-val/edx: (addr value) <- index out, dest-offset
248 var dest/eax: (addr handle array byte) <- get dest-val, text-data
249 copy-object src, dest
250 var type/edx: (addr int) <- get dest-val, type
251 copy-to *type, 1
252 i <- increment
253 loop
254 }
255 }