From 343666bea3adebc0dbe11957c694a445499f43f0 Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Fri, 16 Aug 2019 00:17:40 -0700 Subject: 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. --- apps/desugar | Bin 35814 -> 35819 bytes apps/desugar.subx | 50 ++++++++++++++++++++++++++++---------------------- 2 files changed, 28 insertions(+), 22 deletions(-) (limited to 'apps') diff --git a/apps/desugar b/apps/desugar index b46bfb76..da730820 100755 Binary files a/apps/desugar and b/apps/desugar differ diff --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) -> 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 -- cgit 1.4.1-2-gfad0