From c65dee2d99a7d1a5f0fbc8826b616683713ebef6 Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Sun, 20 Jan 2019 23:19:40 -0800 Subject: 4938 --- subx/071next-token.subx | 67 ++++++++++++++++++++++-------------------------- subx/apps/crenshaw2-1 | Bin 15313 -> 15310 bytes subx/apps/crenshaw2-1b | Bin 15872 -> 15869 bytes subx/apps/factorial | Bin 14231 -> 14228 bytes subx/apps/handle | Bin 15024 -> 15021 bytes subx/apps/hex | Bin 18292 -> 18289 bytes subx/apps/pack.subx | 34 ++++++++++++++++++------ 7 files changed, 57 insertions(+), 44 deletions(-) (limited to 'subx') diff --git a/subx/071next-token.subx b/subx/071next-token.subx index a3cd6035..41dd94f3 100644 --- a/subx/071next-token.subx +++ b/subx/071next-token.subx @@ -4,7 +4,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 # main: -#? e8/call test-skip-chars-matching-in-slice/disp32 +#? e8/call test-next-token-from-slice/disp32 e8/call run-tests/disp32 # 'run-tests' is a function created automatically by SubX. It calls all functions that start with 'test-'. # 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 @@ -166,33 +166,36 @@ test-next-token-eof: # extract the next run of characters that are different from a given 'delimiter' (skipping multiple delimiters if necessary) # on eof return an empty interval -next-token-from-slice: # in : (address slice), delimiter : byte, out : (address slice) +next-token-from-slice: # start : (address byte), end : (address byte), delimiter : byte, out : (address slice) -> # . prolog 55/push-EBP 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP # . save registers 50/push-EAX - 56/push-ESI + 51/push-ECX + 52/push-EDX 57/push-EDI - # ESI = in - 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI + # ECX = end + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 1/r32/ECX 0xc/disp8 . # copy *(EBP+12) to ECX + # EDX = delimiter + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 2/r32/EDX 0x10/disp8 . # copy *(EBP+16) to EDX # EDI = out - 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 7/r32/EDI 0x10/disp8 . # copy *(EBP+16) to EDI - # EAX = skip-chars-matching-in-slice(in->start, in->end, delimiter) + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 7/r32/EDI 0x14/disp8 . # copy *(EBP+20) to EDI + # EAX = skip-chars-matching-in-slice(start, end, delimiter) # . . push args - ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) - 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 + 52/push-EDX + 51/push-ECX + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) # . . call e8/call skip-chars-matching-in-slice/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP # out->start = EAX 89/copy 0/mod/indirect 7/rm32/EDI . . . 0/r32/EAX . . # copy EAX to *EDI - # EAX = skip-chars-not-matching-in-slice(EAX, in->end, delimiter) + # EAX = skip-chars-not-matching-in-slice(EAX, end, delimiter) # . . push args - ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) - ff 6/subop/push 1/mod/*+disp8 6/rm32/ESI . . . . 4/disp8 . # push *(ESI+4) + 52/push-EDX + 51/push-ECX 50/push-EAX # . . call e8/call skip-chars-not-matching-in-slice/disp32 @@ -202,7 +205,8 @@ next-token-from-slice: # in : (address slice), delimiter : byte, out : (address 89/copy 1/mod/*+disp8 7/rm32/EDI . . . 0/r32/EAX 4/disp8 . # copy EAX to *(EDI+4) # . restore registers 5f/pop-to-EDI - 5e/pop-to-ESI + 5a/pop-to-EDX + 59/pop-to-ECX 58/pop-to-EAX # . epilog 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP @@ -218,23 +222,20 @@ test-next-token-from-slice: 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 - # var in/ESI : (address slice) = {EAX, ECX} - 51/push-ECX - 50/push-EAX - 89/copy 3/mod/direct 6/rm32/ESI . . . 4/r32/ESP . . # copy ESP to ESI # var out/EDI : (address slice) = {0, 0} 68/push 0/imm32/end 68/push 0/imm32/start 89/copy 3/mod/direct 7/rm32/EDI . . . 4/r32/ESP . . # copy ESP to EDI - # next-token-from-slice(in, 0x20/space, out) + # next-token-from-slice(EAX, ECX, 0x20/space, out) # . . push args 57/push-EDI 68/push 0x20/imm32 - 56/push-ESI + 51/push-ECX + 50/push-EAX # . . call e8/call next-token-from-slice/disp32 # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # add to ESP # out->start should be at the 'a' # . check-ints-equal(out->start - in->start, 2, msg) # . . push args @@ -242,7 +243,7 @@ test-next-token-from-slice: 68/push 2/imm32 # . . push out->start - in->start 8b/copy 0/mod/indirect 7/rm32/EDI . . . 1/r32/ECX . . # copy *EDI to ECX - 2b/subtract 0/mod/indirect 6/rm32/ESI . . . 1/r32/ECX . . # subtract *ESI from ECX + 2b/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract EAX from ECX 51/push-ECX # . . call e8/call check-ints-equal/disp32 @@ -255,7 +256,7 @@ test-next-token-from-slice: 68/push 4/imm32 # . . push out->end - in->start 8b/copy 1/mod/*+disp8 7/rm32/EDI . . . 1/r32/ECX 4/disp8 . # copy *(EDI+4) to ECX - 2b/subtract 0/mod/indirect 6/rm32/ESI . . . 1/r32/ECX . . # subtract *ESI from ECX + 2b/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract EAX from ECX 51/push-ECX # . . call e8/call check-ints-equal/disp32 @@ -270,23 +271,20 @@ test-next-token-from-slice-eof: # . prolog 55/push-EBP 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP - # var in/ESI : (address slice) = {0, 0} - 51/push-ECX - 50/push-EAX - 89/copy 3/mod/direct 6/rm32/ESI . . . 4/r32/ESP . . # copy ESP to ESI # var out/EDI : (address slice) = {0, 0} 68/push 0/imm32/end 68/push 0/imm32/start 89/copy 3/mod/direct 7/rm32/EDI . . . 4/r32/ESP . . # copy ESP to EDI - # next-token-from-slice(in, 0x20/space, out) + # next-token-from-slice(0, 0, 0x20/space, out) # . . push args 57/push-EDI 68/push 0x20/imm32 - 56/push-ESI + 68/push 0/imm32 + 68/push 0/imm32 # . . call e8/call next-token-from-slice/disp32 # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # add to ESP # out should be empty # . check-ints-equal(out->end - out->start, 0, msg) # . . push args @@ -314,10 +312,6 @@ test-next-token-from-slice-nothing: 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 - # var in/ESI : (address slice) = {EAX, ECX} - 51/push-ECX - 50/push-EAX - 89/copy 3/mod/direct 6/rm32/ESI . . . 4/r32/ESP . . # copy ESP to ESI # var out/EDI : (address slice) = {0, 0} 68/push 0/imm32/end 68/push 0/imm32/start @@ -326,11 +320,12 @@ test-next-token-from-slice-nothing: # . . push args 57/push-EDI 68/push 0x20/imm32 - 56/push-ESI + 51/push-ECX + 50/push-EAX # . . call e8/call next-token-from-slice/disp32 # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # add to ESP # out should be empty # . check-ints-equal(out->end - out->start, 0, msg) # . . push args diff --git a/subx/apps/crenshaw2-1 b/subx/apps/crenshaw2-1 index 448cc762..c21bdb6b 100755 Binary files a/subx/apps/crenshaw2-1 and b/subx/apps/crenshaw2-1 differ diff --git a/subx/apps/crenshaw2-1b b/subx/apps/crenshaw2-1b index b31edcd3..b8408351 100755 Binary files a/subx/apps/crenshaw2-1b and b/subx/apps/crenshaw2-1b differ diff --git a/subx/apps/factorial b/subx/apps/factorial index 2f10831f..83af72ba 100755 Binary files a/subx/apps/factorial and b/subx/apps/factorial differ diff --git a/subx/apps/handle b/subx/apps/handle index 5069d19d..3d3e555d 100755 Binary files a/subx/apps/handle and b/subx/apps/handle differ diff --git a/subx/apps/hex b/subx/apps/hex index 10c02bf6..c7460132 100755 Binary files a/subx/apps/hex and b/subx/apps/hex differ diff --git a/subx/apps/pack.subx b/subx/apps/pack.subx index f09a6b2f..b128b8d3 100644 --- a/subx/apps/pack.subx +++ b/subx/apps/pack.subx @@ -115,14 +115,6 @@ $main:end: # if has metadata 'imm32', emit as 4 bytes # finally, emit line prefixed with a ' # ' -# has-metadata?(word-slice, m): -# next-token2(word-slice, '/') # skip -# while true: -# slice = next-token2(word-slice, '/') -# if empty?(slice) break -# if equal(slice, m) return true -# return false - # simplifications since we perform zero error handling (continuing to rely on the C++ version for that): # missing fields are always 0-filled # bytes never mentioned are silently dropped; if you don't provide /mod, /rm32 or /r32 you don't get a 0 modrm byte. You get *no* modrm byte. @@ -359,6 +351,32 @@ test-next-word-returns-whole-comment: 5d/pop-to-EBP c3/return +has-metadata?: # word : (address slice), s : (address string) -> EAX : boolean + # pseudocode: + # var twig : &slice + # curr = word->start + # while true: + # twig = next-token-from-slice(curr, word->end, '/') + # if twig.empty() break + # if slice-equal?(twig, s) return true + # curr = twig->end + # return false + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers + # ESI = word + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI + # var twig/EDI : (address slice) = {0, 0} + 68/push 0/imm32/end + 68/push 0/imm32/start + 89/copy 3/mod/direct 7/rm32/EDI . . . 4/r32/ESP . . # copy ESP to EDI + # . restore registers + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + # print 'n' in hex in 'width' bytes in lower-endian order, with a space after every byte emit-hex: # out : (address buffered-file), n : int, width : int # . prolog -- cgit 1.4.1-2-gfad0