about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2019-08-25 00:24:03 -0700
committerKartik Agaram <vc@akkartik.com>2019-08-25 00:27:42 -0700
commitd230393c924276a8bb3d9661dff688d5b07705ff (patch)
tree4aa4271d75e49db304d75f6e5684e2baf3bb3f8f
parentbe063736d0cd1f4141dc595361b575605683269d (diff)
downloadmu-d230393c924276a8bb3d9661dff688d5b07705ff.tar.gz
parsing *(reg+reg)
Turns out there's an ambiguity even in this simple one-line language:
when you see 'base+' you don't know whether the next token is the index
or displacement. (Whereas a '-' would be unambiguous but is still not
handled.)

Fixing this ambiguity adds 15 instructions worth of complexity.
-rwxr-xr-xapps/desugarbin43605 -> 44097 bytes
-rw-r--r--apps/desugar.subx171
2 files changed, 150 insertions, 21 deletions
diff --git a/apps/desugar b/apps/desugar
index 8b1eb786..88ba5534 100755
--- a/apps/desugar
+++ b/apps/desugar
Binary files differdiff --git a/apps/desugar.subx b/apps/desugar.subx
index 6d2d607b..dc36f401 100644
--- a/apps/desugar.subx
+++ b/apps/desugar.subx
@@ -1497,6 +1497,7 @@ parse-effective-address:  # word : (address slice) -> base/EAX, index/ECX, scale
     #   if (*word->start != '+') goto error2
     #   ++word->start to skip '+'
     #   skip whitespace
+    #   if next 3 characters don't make a register, goto displacement
     #   read register into index
     #   skip whitespace
     #   if (*word->start == ')') goto end
@@ -1509,10 +1510,9 @@ parse-effective-address:  # word : (address slice) -> base/EAX, index/ECX, scale
     #     skip whitespace
     #     if (*word->start == ')') goto end
     #   }
-    #   if (*word->start != '+') goto error4
-    #   ++word->start to skip '+'
-    #   skip whitespace
-    #   read register into disp
+    #   if (*word->start not in '+' '-') goto error4
+    # displacement:
+    #   read integer into disp
     #   skip whitespace
     #   if (*word->start != ')') goto error5
     # . prolog
@@ -1553,7 +1553,7 @@ $parse-effective-address:simple-register:
     # . base = *EAX
     8b/copy                         0/mod/indirect  0/rm32/EAX    .           .             .           7/r32/EDI   .               .                 # copy *EAX to EDI
     # return
-    eb/jump  $parse-effective-address:end/disp8
+    e9/jump  $parse-effective-address:end/disp32
 $parse-effective-address:compound-expression:
     # ++word->start to skip '('
     ff          0/subop/increment   0/mod/indirect  6/rm32/ESI    .           .             .           .           .               .                 # increment *ESI
@@ -1593,24 +1593,93 @@ $parse-effective-address:compound-expression:
     8a/copy-byte                    0/mod/indirect  0/rm32/EAX    .           .             .           0/r32/AL    .               .                 # copy byte at *EAX to AL
     81          4/subop/and         3/mod/direct    0/rm32/EAX    .           .             .           .           .               0xff/imm32        # bitwise and of EAX
     3d/compare-EAX-and  0x29/imm32/close-paren
-    74/jump-if-equal  $parse-effective-address:end/disp8
+    0f 84/jump-if-equal  $parse-effective-address:end/disp32
     # if (*word->start != '+') goto error2
-    # ++word->start
+$parse-effective-address:check-for-index:
+    # ++word->start to skip '+'
+    ff          0/subop/increment   0/mod/indirect  6/rm32/ESI    .           .             .           .           .               .                 # increment *ESI
     # skip whitespace
+    # . EAX = skip-chars-matching-whitespace-in-slice(word->start, word->end)
+    # . . push args
+    ff          6/subop/push        1/mod/*+disp8   6/rm32/ESI    .           .             .           .           4/disp8         .                 # push *(ESI+4)
+    ff          6/subop/push        0/mod/indirect  6/rm32/ESI    .           .             .           .           .               .                 # push *ESI
+    # . . call
+    e8/call  skip-chars-matching-whitespace-in-slice/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
+    # . word->start = EAX
+    89/copy                         0/mod/indirect  6/rm32/ESI    .           .             .           0/r32/EAX   .               .                 # copy EAX to *ESI
+$parse-effective-address:resolve-ambiguity:
+    # if next 3 characters don't make a register, goto displacement
+    # . spill ECX
+    51/push-ECX
+    # . var tmp/ECX = {word->start, word->start+3}
+    # . . ECX = word->start
+    89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           0/r32/EAX   .               .                 # copy EAX to ECX
+    # . . EAX = word->start+3
+    05/add-to-EAX  3/imm32
+    # . . push
+    50/push-EAX
+    51/push-ECX
+    # . . copy ESP to ECX
+    89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           4/r32/ESP   .               .                 # copy ESP to ECX
+    # . EAX = maybe-get-slice(Register, tmp, row-size=8)
+    # . . push args
+    68/push  8/imm32/row-size
+    51/push-ECX
+    68/push  Registers/imm32
+    # . . call
+    e8/call  maybe-get-slice/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
+    # . reclaim tmp
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
+    # . restore ECX
+    59/pop-to-ECX
+    # . if (EAX == 0) goto displacement
+    3d/compare-EAX-and  0/imm32
+    0f 84/jump-if-equal  $parse-effective-address:displacement/disp32
+$parse-effective-address:index:
     # read register into index
+    # . EAX = next-register(word)
+    # . . push args
+    ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         .                 # push *(EBP+8)
+    # . . call
+    e8/call  next-register/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # . ECX = *EAX
+    8b/copy                         0/mod/indirect  0/rm32/EAX    .           .             .           1/r32/ECX   .               .                 # copy *EAX to ECX
     # skip whitespace
+    # . EAX = skip-chars-matching-whitespace-in-slice(word->start, word->end)
+    # . . push args
+    ff          6/subop/push        1/mod/*+disp8   6/rm32/ESI    .           .             .           .           4/disp8         .                 # push *(ESI+4)
+    ff          6/subop/push        0/mod/indirect  6/rm32/ESI    .           .             .           .           .               .                 # push *ESI
+    # . . call
+    e8/call  skip-chars-matching-whitespace-in-slice/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
+    # . word->start = EAX
+    89/copy                         0/mod/indirect  6/rm32/ESI    .           .             .           0/r32/EAX   .               .                 # copy EAX to *ESI
     # if (*word->start == ')') goto end
-    # if (*word->start == '<') {
-    #   ++word->start to skip '<'
-    #   if (*word->start != '<') goto error3
-    #   ++word->start to skip '<'
-    #   skip whitespace
-    #   read register into scale
-    #   skip whitespace
-    #   if (*word->start == ')') goto end
-    # }
+    8a/copy-byte                    0/mod/indirect  0/rm32/EAX    .           .             .           0/r32/AL    .               .                 # copy byte at *EAX to AL
+    81          4/subop/and         3/mod/direct    0/rm32/EAX    .           .             .           .           .               0xff/imm32        # bitwise and of EAX
+    3d/compare-EAX-and  0x29/imm32/close-paren
+    74/jump-if-equal  $parse-effective-address:end/disp8
+$parse-effective-address:check-for-scale:
+    # if (*word->start != '<') goto displacement
+    # ++word->start to skip '<'
+    # if (*word->start != '<') goto error3
+    # ++word->start to skip '<'
+    # skip whitespace
+$parse-effective-address:scale:
+    # read register into scale
+    # skip whitespace
+    # if (*word->start == ')') goto end
+$parse-effective-address:check-for-displacement:
     # if (*word->start not in '+' '-') goto error4
-    # read int into disp
+$parse-effective-address:displacement:
+    # read integer into disp
     # . EAX = next-hex-int(word)
     # . . push args
     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         .                 # push *(EBP+8)
@@ -1869,8 +1938,68 @@ test-parse-effective-address-base-displacement:
     5d/pop-to-EBP
     c3/return
 
-#? test-parse-effective-address-base-index:
-#? 
+test-parse-effective-address-base-index:
+    # . prolog
+    55/push-EBP
+    89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
+    # var slice/ECX = "*(esi+ecx)"
+    b8/copy-to-EAX  "*(esi+ecx)"/imm32
+    8b/copy                         0/mod/indirect  0/rm32/EAX    .           .             .           1/r32/ECX   .               .                 # copy *EAX to ECX
+    8d/copy-address                 1/mod/*+disp8   4/rm32/sib    0/base/EAX  1/index/ECX   .           1/r32/ECX   4/disp8         .                 # copy EAX+ECX+4 to ECX
+    05/add-to-EAX  4/imm32
+    # . ECX = {EAX, ECX}
+    51/push-ECX
+    50/push-EAX
+    89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           4/r32/ESP   .               .                 # copy ESP to ECX
+    # EAX, ECX, EDX, EBX = parse-effective-address(slice)
+    # . . push args
+    51/push-ECX
+    # . . call
+    e8/call  parse-effective-address/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # slice clobbered beyond this point
+    # check-ints-equal(EAX, 6, msg)
+    # . . push args
+    68/push  "F - test-parse-effective-address-base-index/base"/imm32
+    68/push  6/imm32/ESI
+    50/push-EAX
+    # . . call
+    e8/call  check-ints-equal/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
+    # check-ints-equal(ECX, 1, msg)
+    # . . push args
+    68/push  "F - test-parse-effective-address-base-index/index"/imm32
+    68/push  1/imm32/none
+    51/push-ECX
+    # . . call
+    e8/call  check-ints-equal/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
+    # check-ints-equal(EDX, 0, msg)
+    # . . push args
+    68/push  "F - test-parse-effective-address-base-index/scale"/imm32
+    68/push  0/imm32/none
+    52/push-EDX
+    # . . call
+    e8/call  check-ints-equal/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
+    # check-ints-equal(EBX, 0, msg)
+    # . . push args
+    68/push  "F - test-parse-effective-address-base-index/displacement"/imm32
+    68/push  0/imm32
+    53/push-EBX
+    # . . call
+    e8/call  check-ints-equal/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
+    # . epilog
+    89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
+    5d/pop-to-EBP
+    c3/return
+
 #? test-parse-effective-address-base-index-scale:
 #? 
 #? test-parse-effective-address-base-index-scale-displacement:
@@ -2398,7 +2527,7 @@ test-skip-until-close-paren-in-slice-stops-at-end:
     5d/pop-to-EBP
     c3/return
 
-# assumes 'in' starts with a '+' or '-', optional whitespace, and an unsigned integer
+# assumes 'in' starts with optional '+' or '-', optional whitespace, and an unsigned integer
 # returns the value of the integer
 # side-effect: modifies 'in' to skip past the integer
 next-hex-int:  # in : (address slice) -> result/EAX
@@ -2434,7 +2563,7 @@ $next-hex-int:positive:
 $next-hex-int:negative:
     # else if (*curr == '-') ++curr, negate = true
     3d/compare-EAX-and  0x2d/imm32/-
-    75/jump-if-not-equal  $next-hex-int:abort/disp8
+    75/jump-if-not-equal  $next-hex-int:skip-whitespace/disp8
     # . ++curr
     41/increment-ECX
     # . negate = true
ision' href='/akkartik/mu/blame/072scenario_screen.cc?h=hlt&id=af1005e5e53f4ac9cf9c6f0301663b49173c7a6c'>^
de49fb42 ^
8c2d96a9 ^
1326a4ec ^
31401373 ^
ac0e9db5 ^
de49fb42 ^

ac0e9db5 ^

de49fb42 ^
ac0e9db5 ^
de49fb42 ^
ac0e9db5 ^
f46db248 ^


ac0e9db5 ^
f46db248 ^


2d863b1b ^
8c2d96a9 ^
f46db248 ^

1326a4ec ^
8cdfed6a ^



1326a4ec ^




564c65b2 ^
1326a4ec ^











1326a4ec ^
f46db248 ^

f7b31a14 ^
f46db248 ^



f7b31a14 ^
f46db248 ^



8e41a2b8 ^

f7b31a14 ^
8e41a2b8 ^



f46db248 ^
8e41a2b8 ^
3b168e00 ^



de49fb42 ^

f46db248 ^










































de49fb42 ^

8e41a2b8 ^













ac0e9db5 ^
8e41a2b8 ^
ac0e9db5 ^
8e41a2b8 ^
ac0e9db5 ^
8e41a2b8 ^

ac0e9db5 ^

8e41a2b8 ^

ac0e9db5 ^

7b7aa5f6 ^
ac0e9db5 ^
7b7aa5f6 ^
e45c1ce2 ^
7b7aa5f6 ^

1326a4ec ^
8e41a2b8 ^



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