From 438e5a0db04d7a6e1a7199771cca16720c0b4b8d Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Wed, 15 May 2019 23:47:12 -0700 Subject: complete the skeleton of dquotes.subx Still some failing tests: - emit-string-literal-data doesn't ignore metadata when computing the length of literal strings - emit-string-literal-data doesn't handle escape sequences One issue doesn't have a failing test: - emit-metadata doesn't handle string literals containing '/' All these open issues involve a common design question: how to parse a 'word' that includes a string literal that may include spaces. For everything else I know words can't contain spaces and datums can't contain slashes. But for string literals things are tougher. --- subx/066print-int.subx | 104 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 3 deletions(-) (limited to 'subx/066print-int.subx') diff --git a/subx/066print-int.subx b/subx/066print-int.subx index 0715b414..1280f348 100644 --- a/subx/066print-int.subx +++ b/subx/066print-int.subx @@ -6,7 +6,7 @@ # . 1-3 bytes 3 bits 2 bits 3 bits 3 bits 3 bits 2 bits 2 bits 0/1/2/4 bytes 0/1/2/4 bytes #? Entry: # run a single test, while debugging -#? e8/call test-print-int32-buffered/disp32 +#? e8/call test-print-int32/disp32 #? # syscall(exit, Num-test-failures) #? 8b/copy 0/mod/indirect 5/rm32/.disp32 . . 3/r32/EBX Num-test-failures/disp32 # copy *Num-test-failures to EBX #? b8/copy-to-EAX 1/imm32/exit @@ -171,8 +171,97 @@ test-print-byte-buffered: # . end c3/return +print-int32: # f : (address stream), n : int -> + # pseudocode: + # write(f, "0x") + # ECX = 28 + # while true + # if (ECX < 0) break + # EAX = n >> ECX + # EAX = EAX & 0xf + # append-byte(f, AL) + # ECX -= 4 + # + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers + 50/push-EAX + 51/push-ECX + # ECX = 28 + b9/copy-to-ECX 0x1c/imm32 +$print-int32:print-hex-prefix: + # write(f, "0x") + # . . push args + 68/push "0x"/imm32 + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +$print-int32:loop: + # if (ECX < 0) break + 81 7/subop/compare 3/mod/direct 1/rm32/ECX . . . . . 0/imm32 # compare ECX + 7c/jump-if-lesser $print-int32:end/disp8 + # EAX = n >> ECX + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 0/r32/EAX 0xc/disp8 . # copy *(EBP+12) to EAX + d3/>>ECX 5/subop/pad-zeroes 3/mod/direct 0/rm32/EAX . . . . . . # shift EAX right by ECX bits, padding zeroes + # EAX = to-hex-char(AL) + 25/and-EAX 0xf/imm32 + e8/call to-hex-char/disp32 + # append-byte(f, AL) + # . . push args + 50/push-EAX + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call append-byte/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # ECX -= 4 + 81 5/subop/subtract 3/mod/direct 1/rm32/ECX . . . . . 4/imm32 # subtract from ECX + eb/jump $print-int32:loop/disp8 +$print-int32:end: + # . restore registers + 59/pop-to-ECX + 58/pop-to-EAX + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-print-int32: + # - check that print-int32 prints the hex textual representation + # setup + # . clear-stream(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # print-int32(_test-stream, 0x8899aa) + # . . push args + 68/push 0x8899aa/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call print-int32/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # check-stream-equal(_test-stream, "0x008899aa", msg) + # . . push args + 68/push "F - test-print-int32"/imm32 + 68/push "0x008899aa"/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call check-stream-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . end + c3/return + print-int32-buffered: # f : (address buffered-file), n : int -> # pseudocode: + # write-buffered(f, "0x") # ECX = 28 # while true # if (ECX < 0) break @@ -189,6 +278,15 @@ print-int32-buffered: # f : (address buffered-file), n : int -> 51/push-ECX # ECX = 28 b9/copy-to-ECX 0x1c/imm32 +$print-int32-buffered:print-hex-prefix: + # write-buffered(f, "0x") + # . . push args + 68/push "0x"/imm32 + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP $print-int32-buffered:loop: # if (ECX < 0) break 81 7/subop/compare 3/mod/direct 1/rm32/ECX . . . . . 0/imm32 # compare ECX @@ -271,10 +369,10 @@ test-print-int32-buffered: #? # . . discard args #? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP #? # }}} - # check-stream-equal(_test-stream, "008899aa", msg) + # check-stream-equal(_test-stream, "0x008899aa", msg) # . . push args 68/push "F - test-print-int32-buffered"/imm32 - 68/push "008899aa"/imm32 + 68/push "0x008899aa"/imm32 68/push _test-stream/imm32 # . . call e8/call check-stream-equal/disp32 -- cgit 1.4.1-2-gfad0