about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2020-06-19 20:54:14 -0700
committerKartik Agaram <vc@akkartik.com>2020-06-19 21:01:07 -0700
commit9d5554fbedcfccabdf579cb7c732747b3e8db5b9 (patch)
treecd2f8950e31659e7aa57f83f517682f77a0b057f
parent4d2f171ce1b3efe3cdf56f46a87fb6bc619c05d3 (diff)
downloadmu-9d5554fbedcfccabdf579cb7c732747b3e8db5b9.tar.gz
6554 - snapshot: error on use of a clobbered var
When looking up a var, ensure that it's the var most recently written to
its register.

The new test passes, but a handful of tests now start failing when a new
var in register X is initialized based on the old var in register X. The
way we do lookups is not quite right. At the moment we perform lookups
when we parse words. So we look up outputs of a statement before inputs.
But old bindings are still valid for the entirety of a statement, and outputs
should only take effect for the next statement.
-rw-r--r--apps/mu.subx112
1 files changed, 111 insertions, 1 deletions
diff --git a/apps/mu.subx b/apps/mu.subx
index 0bf2d25f..f596a00e 100644
--- a/apps/mu.subx
+++ b/apps/mu.subx
@@ -1128,6 +1128,52 @@ test-convert-function-with-second-local-var-in-same-reg:
     5d/pop-to-ebp
     c3/return
 
+test-read-clobbered-reg-var:
+    # . prologue
+    55/push-ebp
+    89/<- %ebp 4/r32/esp
+    # setup
+    (clear-stream _test-input-stream)
+    (clear-stream $_test-input-buffered-file->buffer)
+    (clear-stream _test-output-stream)
+    (clear-stream $_test-output-buffered-file->buffer)
+    (clear-stream _test-error-stream)
+    (clear-stream $_test-error-buffered-file->buffer)
+    # var ed/edx: exit-descriptor = tailor-exit-descriptor(16)  # bytes of args in call to convert-mu
+    68/push 0/imm32
+    68/push 0/imm32
+    89/<- %edx 4/r32/esp
+    (tailor-exit-descriptor %edx 0x10)
+    #
+    (write _test-input-stream "fn foo {\n")
+    (write _test-input-stream "  var x/ecx: int <- copy 3\n")
+    (write _test-input-stream "  var y/ecx: int <- copy 4\n")
+    (write _test-input-stream "  x <- increment\n")
+    (write _test-input-stream "}\n")
+    # convert
+    (convert-mu _test-input-buffered-file _test-output-buffered-file _test-error-buffered-file %edx)
+    # registers except esp clobbered at this point
+    # restore ed
+    89/<- %edx 4/r32/esp
+    (flush _test-output-buffered-file)
+    (flush _test-error-buffered-file)
+#?     # dump _test-error-stream {{{
+#?     (write 2 "^")
+#?     (write-stream 2 _test-error-stream)
+#?     (write 2 "$\n")
+#?     (rewind-stream _test-error-stream)
+#?     # }}}
+    # check output
+    (check-stream-equal _test-output-stream  ""  "F - test-read-clobbered-reg-var: output should be empty")
+    (check-next-stream-line-equal _test-error-stream  "register ecx reads var 'x' after writing var 'y'"  "F - test-read-clobbered-reg-var: error message")
+    # check that stop(1) was called
+    (check-ints-equal *(edx+4) 2 "F - test-read-clobbered-reg-var: exit status")
+    # don't restore from ebp
+    81 0/subop/add %esp 8/imm32
+    # . epilogue
+    5d/pop-to-ebp
+    c3/return
+
 test-convert-function-call:
     # . prologue
     55/push-ebp
@@ -7049,6 +7095,7 @@ test-parse-mu-reg-var-def:
     5d/pop-to-ebp
     c3/return
 
+# HERE: we push outputs on the vars stack before we read the inputs that may conflict with them
 parse-mu-stmt:  # line: (addr stream byte), vars: (addr stack live-var), fn: (addr function), out: (addr handle stmt), err: (addr buffered-file), ed: (addr exit-descriptor)
     # pseudocode:
     #   var name: slice
@@ -7403,6 +7450,7 @@ $lookup-var:abort:
     # never gets here
 
 # return first 'name' from the top (back) of 'vars', and 0/null if not found
+# ensure that 'name' if in a register is the topmost variable in that register
 lookup-var-helper:  # name: (addr slice), vars: (addr stack live-var), out: (addr handle var), err: (addr buffered-file), ed: (addr exit-descriptor)
     # pseudocode:
     #   var curr: (addr handle var) = &vars->data[vars->top - 12]
@@ -7422,6 +7470,7 @@ lookup-var-helper:  # name: (addr slice), vars: (addr stack live-var), out: (add
     52/push-edx
     53/push-ebx
     56/push-esi
+    57/push-edi
     # clear out
     (zero-out *(ebp+0x10) *Handle-size)
     # esi = vars
@@ -7435,7 +7484,18 @@ lookup-var-helper:  # name: (addr slice), vars: (addr stack live-var), out: (add
     8d/copy-address *(esi+8) 2/r32/edx
     # var curr/ebx: (addr handle var) = &vars->data[vars->top - 12]
     8d/copy-address *(esi+ebx-4) 3/r32/ebx  # vars + 8 + vars->type - 12
+    # var var-in-reg/edi: 8 addrs
+    68/push 0/imm32
+    68/push 0/imm32
+    68/push 0/imm32
+    68/push 0/imm32
+    68/push 0/imm32
+    68/push 0/imm32
+    68/push 0/imm32
+    68/push 0/imm32
+    89/<- %edi 4/r32/esp
     {
+$lookup-var-helper:loop:
       # if (curr < min) return
       39/compare %ebx 2/r32/edx
       0f 82/jump-if-addr< break/disp32
@@ -7449,6 +7509,22 @@ lookup-var-helper:  # name: (addr slice), vars: (addr stack live-var), out: (add
       3d/compare-eax-and 0/imm32/false
       {
         74/jump-if-= break/disp8
+$lookup-var-helper:found:
+        # var vr/eax: (addr array byte) = lookup(v->register)
+        (lookup *(ecx+0x18) *(ecx+0x1c))  # Var-register Var-register => eax
+        3d/compare-eax-and 0/imm32
+        {
+          74/jump-if-= break/disp8
+$lookup-var-helper:found-register:
+          # var reg/eax: int = get(Registers, vr)
+          (get Mu-registers %eax 0xc "Mu-registers")  # => eax
+          8b/-> *eax 0/r32/eax
+          # if (var-in-reg[reg]) error
+          8b/-> *(edi+eax<<2) 0/r32/eax
+          3d/compare-eax-and 0/imm32
+          0f 85/jump-if-!= $lookup-var-helper:error2/disp32
+        }
+$lookup-var-helper:return:
         # esi = out
         8b/-> *(ebp+0x10) 6/r32/esi
         # *out = *curr
@@ -7459,12 +7535,29 @@ lookup-var-helper:  # name: (addr slice), vars: (addr stack live-var), out: (add
         # return
         eb/jump $lookup-var-helper:end/disp8
       }
+      # 'name' not yet found; update var-in-reg if v in register
+      # . var vr/eax: (addr array byte) = lookup(v->register)
+      (lookup *(ecx+0x18) *(ecx+0x1c))  # Var-register Var-register => eax
+      # . if (var == 0) continue
+      3d/compare-eax-and 0/imm32
+      74/jump-if-= $lookup-var-helper:continue/disp8
+      # . var reg/eax: int = get(Registers, vr)
+      (get Mu-registers %eax 0xc "Mu-registers")  # => eax
+      8b/-> *eax 0/r32/eax
+      # . if (var-in-reg[reg] == 0) var-in-reg[reg] = v
+      81 7/subop/compare *(edi+eax<<2) 0/imm32
+      75/jump-if-!= $lookup-var-helper:continue/disp8
+      89/<- *(edi+eax<<2) 1/r32/ecx
+$lookup-var-helper:continue:
       # curr -= 12
       81 5/subop/subtract %ebx 0xc/imm32
       e9/jump loop/disp32
     }
 $lookup-var-helper:end:
+    # . reclaim locals
+    81 0/subop/add %esp 0x20/imm32
     # . restore registers
+    5f/pop-to-edi
     5e/pop-to-esi
     5b/pop-to-ebx
     5a/pop-to-edx
@@ -7483,6 +7576,23 @@ $lookup-var-helper:error1:
     (stop *(ebp+0x18) 1)
     # never gets here
 
+$lookup-var-helper:error2:
+    # eax contains the conflicting var at this point
+    (write-buffered *(ebp+0x14) "register ")
+    50/push-eax
+    (lookup *(eax+0x18) *(eax+0x1c))  # Var-register Var-register => eax
+    (write-buffered *(ebp+0x14) %eax)
+    58/pop-to-eax
+    (write-buffered *(ebp+0x14) " reads var '")
+    (write-slice-buffered *(ebp+0x14) *(ebp+8))
+    (write-buffered *(ebp+0x14) "' after writing var '")
+    (lookup *eax *(eax+4))  # Var-name Var-name => eax
+    (write-buffered *(ebp+0x14) %eax)
+    (write-buffered *(ebp+0x14) "'\n")
+    (flush *(ebp+0x14))
+    (stop *(ebp+0x18) 1)
+    # never gets here
+
 == data
 # Like Registers, but no esp or ebp
 Mu-registers:  # (addr stream {(handle array byte), int})
@@ -7509,7 +7619,7 @@ lookup-or-define-var:  # name: (addr slice), vars: (addr stack live-var), fn: (a
     # . save registers
     50/push-eax
     #
-    (lookup-var-helper *(ebp+8) *(ebp+0xc) *(ebp+0x14))
+    (lookup-var-helper *(ebp+8) *(ebp+0xc) *(ebp+0x14) *(ebp+0x18) *(ebp+0x1c))
     {
       # if (out != 0) return
       8b/-> *(ebp+0x14) 0/r32/eax
m> 2017-11-05 02:24:20 -0800 committer Kartik K. Agaram <vc@akkartik.com> 2017-11-05 02:24:20 -0800 4110 - add continuations to docs' href='/akkartik/mu/commit/index.html?h=hlt&id=363652449ce690b856e48ab58320b102ddeaa190'>36365244 ^
d55e7738 ^

78477622 ^



7452d052 ^




af571dd8 ^

a675eda3 ^
f5465e12 ^


f6b44306 ^
2f02189d ^
f6b44306 ^



af571dd8 ^
f6b44306 ^
f5465e12 ^
f6b44306 ^
f5465e12 ^
6a9d8191 ^

f6b44306 ^

ff229aec ^
f6b44306 ^
2f02189d ^
f6b44306 ^
2f02189d ^
0428419c ^
2f02189d ^
f6b44306 ^
2f02189d ^
f5465e12 ^
f6b44306 ^
f5465e12 ^
f6b44306 ^
76755b28 ^
2f02189d ^
76755b28 ^
f6b44306 ^
76755b28 ^
f6b44306 ^
2f02189d ^




5fe060d5 ^
f344b250 ^









2f02189d ^
76755b28 ^



2f02189d ^

76755b28 ^

f6b44306 ^
2f02189d ^


fbbbd625 ^
2f02189d ^





f344b250 ^

2f02189d ^

ff229aec ^
2f02189d ^
c842d90b ^


70471628 ^


32b8fac2 ^
2f02189d ^



23d3a022 ^









f6b44306 ^
81a324be ^
2f02189d ^

81a324be ^
c842d90b ^
81a324be ^
ff229aec ^


81a324be ^
ff229aec ^
2f02189d ^
7df951ea ^


2f02189d ^



76755b28 ^
f6b44306 ^

f5465e12 ^
f6b44306 ^
d009e158 ^
f6b44306 ^

d009e158 ^
f6b44306 ^

d009e158 ^
f6b44306 ^
aa42c864 ^
2f02189d ^
0428419c ^
f5465e12 ^
9570363a ^

f5465e12 ^
2f02189d ^












0428419c ^
f6b44306 ^
ff229aec ^
2f02189d ^
ff229aec ^
f5465e12 ^
2f02189d ^

ff229aec ^
2f02189d ^
4bf7453a ^








c842d90b ^

f6b44306 ^
2f02189d ^
f6b44306 ^
56356de8 ^

c603cd6c ^
f6b44306 ^
c603cd6c ^
f6b44306 ^
c603cd6c ^

762b099e ^
c603cd6c ^
762b099e ^
c603cd6c ^
56356de8 ^











2f02189d ^
56356de8 ^
2f02189d ^
8e7b4429 ^
f5465e12 ^
f6b44306 ^

2f02189d ^




f5465e12 ^
2f02189d ^


f5465e12 ^






a675eda3 ^
f5465e12 ^
eea17dee ^
f5465e12 ^
2f02189d ^

f5465e12 ^
2f02189d ^




f5465e12 ^

2f02189d ^
f5465e12 ^

2f02189d ^



dcc060c7 ^
2f02189d ^
f5465e12 ^
f6b44306 ^
b514fbb9 ^

e877e1fc ^
a675eda3 ^
e877e1fc ^
fd5e720b ^



e877e1fc ^

b514fbb9 ^


70763af3 ^








6845c4d9 ^
70763af3 ^




b514fbb9 ^
f6b44306 ^
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