From a06dee4fcd7ae4f033b14837db2b57e2ee73338c Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Sun, 10 Feb 2019 17:14:15 -0800 Subject: 4955 Starting to build up Phase 2 (apps/pack) out of recently designed primitives. --- subx/apps/pack.subx | 393 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 373 insertions(+), 20 deletions(-) (limited to 'subx/apps/pack.subx') diff --git a/subx/apps/pack.subx b/subx/apps/pack.subx index e35e8e20..fada02f1 100644 --- a/subx/apps/pack.subx +++ b/subx/apps/pack.subx @@ -21,7 +21,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 # for debugging: run a single test -#? e8/call test-emit-non-number-with-metadata/disp32 +#? e8/call test-convert-instruction-passes-comments-through/disp32 #? 8b/copy 0/mod/indirect 5/rm32/.disp32 . . 3/r32/EBX Num-test-failures/disp32 # copy *Num-test-failures to EBX #? eb/jump $main:end/disp8 @@ -76,13 +76,77 @@ $main:end: # allocate memory for converting a single instruction. # # To pack an entire file: -# skip segment headers -# pack every instruction in the code segment -# skip other segments +# read every line +# convert it +# +# primary state: line +# stream of 512 bytes; abort if it ever overflows + +convert: # in : (address buffered-file), out : (address buffered-file) -> + # pseudocode: + # line = new-stream(512, 1) + # repeatedly + # clear-stream(line) + # EAX = read-line(in, line) + # if EAX == EOF break + # convert-instruction(line, out, err, ed) + # flush(out) + # + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers + 51/push-ECX + # var line/ECX : (address stream byte) = stream(512) + 81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 0x200/imm32 # subtract from ESP + 68/push 0x200/imm32/length + 68/push 0/imm32/read + 68/push 0/imm32/write + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX +$convert:loop: + # clear-stream(ECX) + # . . push args + 51/push-ECX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # EAX = read-line(in, line) + # . . push args + 51/push-ECX + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call convert-instruction/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # if EAX == 0xffffffff break + 3d/compare-with-EAX 0xffffffff/imm32 + 74/jump-if-equal $convert:break/disp8 + # convert-instruction(line, out, err, ed) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + 51/push-ECX + # . . call + e8/call convert-instruction/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # add to ESP +$convert:break: + # flush(out) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + # . . call + e8/call flush/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP +$convert:end: + # . restore registers + 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 # - To pack an instruction, following the C++ version: -# read line -# parse words # read first word as opcode and write-slice # if 0f or f2 or f3 read second opcode and write-slice # if 'f2 0f' or 'f3 0f' read third opcode and write-slice @@ -122,33 +186,322 @@ $main:end: # silently drop extraneous operands # unceremoniously abort on non-numeric operands except disp or imm -# primary state: line -# stream of 512 bytes; abort if it ever overflows -# # conceptual hierarchy within a line: # line = words separated by ' ', maybe followed by comment starting with '#' # word = name until '/', then 0 or more metadata separated by '/' # -# we won't bother saving the internal structure of lines; reparsing should be cheap using two primitives: +# we won't bother saving the internal structure of lines; reparsing should be cheap using three primitives: # next-token(stream, delim char) -> slice (start, end pointers) # next-token(stream, slice, delim char) -> slice' # slice-equal?(slice, string) -convert: # in : (address buffered-file), out : (address buffered-file), err : (address buffered-file), ed : (address exit-descriptor) -> +convert-instruction: # line : (address stream byte), out : (address buffered-file) -> # pseudocode: - # line = new-stream(512, 1) - # repeatedly - # clear-stream(line) - # EAX = read-line(in, line, err, ed) - # if EAX == EOF break - # convert-instruction(line, out, err, ed) - # flush(out) + # word-slice = next-word + # if *word-slice->start == '#' + # write-stream-buffered(out, line) + # return + # if starts-with(word-slice, '==') + # segment-name = next-word() + # write-stream-buffered(out, line) + # return + # if segment-name != 'code' + # write-stream-buffered(out, line) + # return # # . prolog 55/push-EBP 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP # . save registers + 51/push-ECX + # var word-slice/ECX = {0, 0} + 68/push 0/imm32/end + 68/push 0/imm32/start + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # next-word(line, word-slice) + # . . push args + 51/push-ECX + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call next-word/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +$convert-instruction:pass-line-through: + # write-stream-buffered(out, line) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + # . . call + e8/call write-stream-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +$convert-instruction:end: # . restore registers + 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-convert-instruction-passes-comments-through: + # if a line starts with '#', pass it along unchanged + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # 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 + # . clear-stream(_test-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-tmp-stream) + # . . push args + 68/push _test-tmp-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 + # initialize input + # . write(_test-tmp-stream, "# abcd") + # . . push args + 68/push "# abcd"/imm32 + 68/push _test-tmp-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # convert-instruction(_test-tmp-stream, _test-buffered-file) + # . . push args + 68/push _test-buffered-file/imm32 + 68/push _test-tmp-stream/imm32 + # . . call + e8/call convert-instruction/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # check that the write happened as expected + # . flush(_test-buffered-file) + # . . push args + 68/push _test-buffered-file/imm32 + # . . call + e8/call flush/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . check-stream-equal(_test-stream, "# abcd", msg) + # . . push args + 68/push "F - test-convert-instruction-passes-comments-through"/imm32 + 68/push "# abcd"/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 + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-convert-instruction-passes-segment-headers-through: + # if a line starts with '==', pass it along unchanged + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # 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 + # . clear-stream(_test-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-tmp-stream) + # . . push args + 68/push _test-tmp-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 + # initialize input + # . write(_test-tmp-stream, "== abcd") + # . . push args + 68/push "== abcd"/imm32 + 68/push _test-tmp-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # convert-instruction(_test-tmp-stream, _test-buffered-file) + # . . push args + 68/push _test-buffered-file/imm32 + 68/push _test-tmp-stream/imm32 + # . . call + e8/call convert-instruction/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # check that the write happened as expected + # . flush(_test-buffered-file) + # . . push args + 68/push _test-buffered-file/imm32 + # . . call + e8/call flush/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . check-stream-equal(_test-stream, "== abcd", msg) + # . . push args + 68/push "F - test-convert-instruction-passes-segment-headers-through"/imm32 + 68/push "== abcd"/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 + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-convert-instruction-passes-empty-lines-through: + # if a line is empty, pass it along unchanged + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # 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 + # . clear-stream(_test-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-tmp-stream) + # . . push args + 68/push _test-tmp-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 + # write nothing to input + # convert-instruction(_test-tmp-stream, _test-buffered-file) + # . . push args + 68/push _test-buffered-file/imm32 + 68/push _test-tmp-stream/imm32 + # . . call + e8/call convert-instruction/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # check that the write happened as expected + # . flush(_test-buffered-file) + # . . push args + 68/push _test-buffered-file/imm32 + # . . call + e8/call flush/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . check-stream-equal(_test-stream, "", msg) + # . . push args + 68/push "F - test-convert-instruction-passes-empty-lines-through"/imm32 + 68/push ""/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 + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-convert-instruction-passes-lines-with-just-whitespace-through: + # if a line is empty, pass it along unchanged + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # 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 + # . clear-stream(_test-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-tmp-stream) + # . . push args + 68/push _test-tmp-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 + # initialize input + # . write(_test-tmp-stream, " ") + # . . push args + 68/push " "/imm32 + 68/push _test-tmp-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # convert-instruction(_test-tmp-stream, _test-buffered-file) + # . . push args + 68/push _test-buffered-file/imm32 + 68/push _test-tmp-stream/imm32 + # . . call + e8/call convert-instruction/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # check that the write happened as expected + # . flush(_test-buffered-file) + # . . push args + 68/push _test-buffered-file/imm32 + # . . call + e8/call flush/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . check-stream-equal(_test-stream, " ", msg) + # . . push args + 68/push "F - test-convert-instruction-passes-with-just-whitespace-through"/imm32 + 68/push " "/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 # . epilog 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP 5d/pop-to-EBP @@ -613,8 +966,8 @@ test-has-metadata-multiple-false: 5d/pop-to-EBP c3/return -# if the name of 'word' is all hex digits, parse and emit it in 'width' bytes -# of hex otherwise just write-slice +# if the name of 'word' is all hex digits, parse and print its value in 'width' bytes of hex, least significant first +# otherwise just print the entire word emit: # out : (address buffered-file), word : (address slice), width : int # . prolog 55/push-EBP -- cgit 1.4.1-2-gfad0