about summary refs log tree commit diff stats
path: root/apps/desugar.subx
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2019-08-24 11:32:30 -0700
committerKartik Agaram <vc@akkartik.com>2019-08-24 11:36:37 -0700
commit23ee933e12b9800398a1ee5cd0a73a483f154860 (patch)
tree72f2d2f22e02a9a31f27971b9564c7bf18fc9df8 /apps/desugar.subx
parent33de6b80310da89950b0d1c63c5b39d5893a68c3 (diff)
downloadmu-23ee933e12b9800398a1ee5cd0a73a483f154860.tar.gz
parsing *(reg)
Diffstat (limited to 'apps/desugar.subx')
-rw-r--r--apps/desugar.subx121
1 files changed, 121 insertions, 0 deletions
diff --git a/apps/desugar.subx b/apps/desugar.subx
index db53139b..a6d3af7f 100644
--- a/apps/desugar.subx
+++ b/apps/desugar.subx
@@ -1141,7 +1141,26 @@ $parse-effective-address:compound-expression:
     # ++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
     # read register into base
+    # . 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
+    # . EDI = *EAX
+    8b/copy                         0/mod/indirect  0/rm32/EAX    .           .             .           7/r32/EDI   .               .                 # copy *EAX to EAX
     # skip whitespace
     # if (*word->start == ')') goto end
     # if (*word->start != '+') goto error2
@@ -1176,6 +1195,46 @@ $parse-effective-address:end:
     5d/pop-to-EBP
     c3/return
 
+# assumes 'in' starts with a register name, and returns its code
+# side-effect: modifies 'in' to scan past the initial register name
+next-register:  # in : (address slice) -> reg/EAX : int
+    # . prolog
+    55/push-EBP
+    89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
+    # . save registers
+    51/push-ECX
+    56/push-ESI
+    # ESI = in
+    8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           6/r32/ESI   8/disp8         .                 # copy *(EBP+8) to ESI
+    # var reg-slice/ECX : (address slice) = {in->start, in->start + 3}
+    8b/copy                         0/mod/indirect  6/rm32/ESI    .           .             .           0/r32/EAX   .               .                 # copy *ESI to EAX
+    05/add-to-EAX  3/imm32
+    50/push-EAX
+    ff          6/subop/push        0/mod/indirect  6/rm32/ESI    .           .             .           .           .               .                 # push *ESI
+    89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           4/r32/ESP   .               .                 # copy ESP to ECX
+    # in->start += 3
+    81          0/subop/add         0/mod/indirect  6/rm32/ESI    .           .             .           .           .               3/imm32           # add to *ESI
+    # EAX = get-slice(Registers, word, row-size=8)
+    # . . push args
+    68/push  "next-register"/imm32
+    68/push  8/imm32/row-size
+    51/push-ECX
+    68/push  Registers/imm32
+    # . . call
+    e8/call  get-slice/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0x10/imm32        # add to ESP
+$next-register:end:
+    # reclaim locals
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
+    # . restore registers
+    5e/pop-to-ESI
+    59/pop-to-ECX
+    # . 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-simple:
     # . prolog
     55/push-EBP
@@ -1238,6 +1297,68 @@ test-parse-effective-address-simple:
     5d/pop-to-EBP
     c3/return
 
+test-parse-effective-address-base:
+    # . prolog
+    55/push-EBP
+    89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
+    # var slice/ECX = "*(esi  )"
+    b8/copy-to-EAX  "*(esi  )"/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/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, 4, msg)
+    # . . push args
+    68/push  "F - test-parse-effective-address-base/index"/imm32
+    68/push  4/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/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/displacement"/imm32
+    68/push  0/imm32/none
+    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
+
 # Code generation:
 #   if index is none and disp is 0, then mod = 0 and rm32 = base
 #   if index is none, then mod = 2 and rm32 = base and disp32 = disp