about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2019-08-16 00:17:40 -0700
committerKartik Agaram <vc@akkartik.com>2019-08-16 00:28:44 -0700
commit343666bea3adebc0dbe11957c694a445499f43f0 (patch)
treea4f24cb1a9a1677ff95db646220d0337efcdcca6
parenta8adfff398efb277fade0c96276953ce773b80bf (diff)
downloadmu-343666bea3adebc0dbe11957c694a445499f43f0.tar.gz
plan for supporting indirect mode
It may superficially seem like there's a simpler approach:

- emit '3/mod' or '0/mod' depending on whether the character is '%' or '*'
- increment the start of word-slice
- look up the register and emit the appropriate /rm32

But that won't work for 1/mod or 2/mod. We're doing a little bit of
upfront planning.
-rwxr-xr-xapps/desugarbin35814 -> 35819 bytes
-rw-r--r--apps/desugar.subx50
2 files changed, 28 insertions, 22 deletions
diff --git a/apps/desugar b/apps/desugar
index b46bfb76..da730820 100755
--- a/apps/desugar
+++ b/apps/desugar
Binary files differdiff --git a/apps/desugar.subx b/apps/desugar.subx
index 80c0eb5e..3b9029cd 100644
--- a/apps/desugar.subx
+++ b/apps/desugar.subx
@@ -74,8 +74,10 @@ convert:  # in : (address buffered-file), out : (address buffered-file) -> <void
     #         break
     #       if slice-starts-with?(word-slice, "#")        # comment
     #         continue
-    #       if slice-starts-with?(word-slice, '%')        # register literal <== what we're here for
-    #         desugar-register(word-slice, out)
+    #       if slice-starts-with?(word-slice, '%')        # direct mode
+    #         emit-direct-mode(word-slice, out)
+    #       else if slice-starts-with?(word-slice, '*')   # indirect mode
+    #         emit-indirect-mode(word-slice, out)
     #       else
     #         write-slice-buffered(out, word-slice)
     #       write(out, " ")
@@ -151,11 +153,11 @@ $convert:check-for-comment:
     # . if (EAX == '#') continue
     3d/compare-EAX-and  0x23/imm32/hash
     74/jump-if-equal  $convert:word-loop/disp8
-$convert:check-for-register-literal:
+$convert:check-for-direct-mode:
     # if (!slice-starts-with?(word-slice, "%")) goto next check
     3d/compare-EAX-and  0x25/imm32/percent
     75/jump-if-not-equal  $convert:regular-word/disp8
-$convert:register-literal:
+$convert:direct-mode:
 #?     # dump word-slice {{{
 #?     # . write(2/stderr, "w: ")
 #?     # . . push args
@@ -202,14 +204,12 @@ $convert:register-literal:
 #?     # . . discard args
 #?     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
 #?     # }}}
-    # ++word-slice->start
-    ff          0/subop/increment   0/mod/indirect  2/rm32/EDX    .           .             .           .           .               .                 # increment *EDX
-    # desugar-register(word-slice, out)
+    # emit-direct-mode(word-slice, out)
     # . . push args
     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0xc/disp8       .                 # push *(EBP+12)
     52/push-EDX
     # . . call
-    e8/call  desugar-register/disp32
+    e8/call  emit-direct-mode/disp32
     # . . discard args
     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
     # continue
@@ -267,12 +267,18 @@ $convert:end:
     5d/pop-to-EBP
     c3/return
 
-desugar-register: # word : (address slice), out : (address buffered-file)
+# beware: modifies 'word'
+emit-direct-mode: # word : (address slice), out : (address buffered-file)
     # . prolog
     55/push-EBP
     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
     # . save registers
     50/push-EAX
+    # ++word->start
+    # . EAX = word
+    8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           0/r32/EAX   8/disp8         .                 # copy *(EBP+8) to EAX
+    # . ++(*EAX)
+    ff          0/subop/increment   0/mod/indirect  0/rm32/EAX    .           .             .           .           .               .                 # increment *EAX
     # reg-num/EAX = get-slice(Registers, word, row-size=8)
     # . . push args
     68/push  "Registers"/imm32
@@ -307,7 +313,7 @@ desugar-register: # word : (address slice), out : (address buffered-file)
     e8/call  write-buffered/disp32
     # . . discard args
     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
-$desugar-register:end:
+$emit-direct-mode:end:
     # . restore registers
     58/pop-to-EAX
     # . epilog
@@ -315,7 +321,7 @@ $desugar-register:end:
     5d/pop-to-EBP
     c3/return
 
-test-desugar-register:
+test-emit-direct-mode:
     # . prolog
     55/push-EBP
     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
@@ -336,8 +342,8 @@ test-desugar-register:
     e8/call  clear-stream/disp32
     # . . discard args
     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
-    # var slice/ECX = "eax"
-    b8/copy-to-EAX  "eax"/imm32
+    # var slice/ECX = "%eax"
+    b8/copy-to-EAX  "%eax"/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
@@ -345,12 +351,12 @@ test-desugar-register:
     51/push-ECX
     50/push-EAX
     89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           4/r32/ESP   .               .                 # copy ESP to ECX
-    # desugar-register(str, _test-output-buffered-file)
+    # emit-direct-mode(str, _test-output-buffered-file)
     # . . push args
     68/push  _test-output-buffered-file/imm32
     51/push-ECX
     # . . call
-    e8/call  desugar-register/disp32
+    e8/call  emit-direct-mode/disp32
     # . . discard args
     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32         # add to ESP
     # . flush(_test-output-buffered-file)
@@ -388,7 +394,7 @@ test-desugar-register:
 #?     # }}}
     # check-stream-equal(_test-output-stream, "3/mod/direct 0/rm32", msg)
     # . . push args
-    68/push  "F - test-desugar-register/0"/imm32
+    68/push  "F - test-emit-direct-mode/0"/imm32
     68/push  "3/mod/direct 0x00000000/rm32"/imm32
     68/push  _test-output-stream/imm32
     # . . call
@@ -400,7 +406,7 @@ test-desugar-register:
     5d/pop-to-EBP
     c3/return
 
-test-desugar-register-2:
+test-emit-direct-mode-2:
     # . prolog
     55/push-EBP
     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
@@ -421,8 +427,8 @@ test-desugar-register-2:
     e8/call  clear-stream/disp32
     # . . discard args
     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
-    # var slice/ECX = "edi"
-    b8/copy-to-EAX  "edi"/imm32
+    # var slice/ECX = "%edi"
+    b8/copy-to-EAX  "%edi"/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
@@ -430,12 +436,12 @@ test-desugar-register-2:
     51/push-ECX
     50/push-EAX
     89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           4/r32/ESP   .               .                 # copy ESP to ECX
-    # desugar-register(str/ECX, _test-output-buffered-file)
+    # emit-direct-mode(str/ECX, _test-output-buffered-file)
     # . . push args
     68/push  _test-output-buffered-file/imm32
     51/push-ECX
     # . . call
-    e8/call  desugar-register/disp32
+    e8/call  emit-direct-mode/disp32
     # . . discard args
     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32         # add to ESP
     # . flush(_test-output-buffered-file)
@@ -473,7 +479,7 @@ test-desugar-register-2:
 #?     # }}}
     # check-stream-equal(_test-output-stream, "3/mod/direct 7/rm32", msg)
     # . . push args
-    68/push  "F - test-desugar-register/1"/imm32
+    68/push  "F - test-emit-direct-mode/1"/imm32
     68/push  "3/mod/direct 0x00000007/rm32"/imm32
     68/push  _test-output-stream/imm32
     # . . call