# primitive for editing text type gap-buffer { left: code-point-utf8-stack right: code-point-utf8-stack # some fields for scanning incrementally through a gap-buffer left-read-index: int right-read-index: int } fn initialize-gap-buffer _self: (addr gap-buffer), capacity: int { var self/esi: (addr gap-buffer) <- copy _self var left/eax: (addr code-point-utf8-stack) <- get self, left initialize-code-point-utf8-stack left, capacity var right/eax: (addr code-point-utf8-stack) <- get self, right initialize-code-point-utf8-stack right, capacity } fn clear-gap-buffer _self: (addr gap-buffer) { var self/esi: (addr gap-buffer) <- copy _self var left/eax: (addr code-point-utf8-stack) <- get self, left clear-code-point-utf8-stack left var right/eax: (addr code-point-utf8-stack) <- get self, right clear-code-point-utf8-stack right } fn gap-buffer-empty? _self: (addr gap-buffer) -> _/eax: boolean { var self/esi: (addr gap-buffer) <- copy _self # if !empty?(left) return false { var left/eax: (addr code-point-utf8-stack) <- get self, left var result/eax: boolean <- code-point-utf8-stack-empty? left compare result, 0/false break-if-!= return 0/false } # return empty?(right) var left/eax: (addr code-point-utf8-stack) <- get self, left var result/eax: boolean <- code-point-utf8-stack-empty? left return result } fn gap-buffer-capacity _gap: (addr gap-buffer) -> _/edx: int { var gap/esi: (addr gap-buffer) <- copy _gap var left/eax: (addr code-point-utf8-stack) <- get gap, left var left-data-ah/eax: (addr handle array code-point-utf8) <- get left, data var left-data/eax: (addr array code-point-utf8) <- lookup *left-data-ah var result/eax: int <- length left-data return result } fn initialize-gap-buffer-with self: (addr gap-buffer), keys: (addr array byte) { initialize-gap-buffer self, 0x40/capacity var input-stream-storage: (stream byte 0x40/capacity) var input-stream/ecx: (addr stream byte) <- address input-stream-storage write input-stream, keys { var done?/eax: boolean <- stream-empty? input-stream compare done?, 0/false break-if-!= var g/eax: code-point-utf8 <- read-code-point-utf8 input-stream add-code-point-utf8-at-gap self, g loop } } fn load-gap-buffer-from-stream self: (addr gap-buffer), in: (addr stream byte) { rewind-stream in { var done?/eax: boolean <- stream-empty? in compare done?, 0/false break-if-!= var key/eax: byte <- read-byte in compare key, 0/null break-if-= var g/eax: code-point-utf8 <- copy key edit-gap-buffer self, g loop } } fn emit-gap-buffer self: (addr gap-buffer), out: (addr stream byte) { clear-stream out append-gap-buffer self, out } fn append-gap-buffer _self: (addr gap-buffer), out: (addr stream byte) { var self/esi: (addr gap-buffer) <- copy _self var left/eax: (addr code-point-utf8-stack) <- get self, left emit-stack-from-bottom left, out var right/eax: (addr code-point-utf8-stack) <- get self, right emit-stack-from-top right, out } # dump stack from bottom to top fn emit-stack-from-bottom _self: (addr code-point-utf8-stack), out: (addr stream byte) { var self/esi: (addr code-point-utf8-stack) <- copy _self var data-ah/edi: (addr handle array code-point-utf8) <- get self, data var _data/eax: (addr array code-point-utf8) <- lookup *data-ah var data/edi: (addr array code-point-utf8) <- copy _data var top-addr/ecx: (addr int) <- get self, top var i/eax: int <- copy 0 { compare i, *top-addr break-if->= var g/edx: (addr code-point-utf8) <- index data, i write-code-point-utf8 out, *g i <- increment loop } } # dump stack from top to bottom fn emit-stack-from-top _self: (addr code-point-utf8-stack), out: (addr stream byte) { var self/esi: (addr code-point-utf8-stack) <- copy _self var data-ah/edi: (addr handle array code-point-utf8) <- get self, data var _data/eax: (addr array code-point-utf8) <- lookup *data-ah var data/edi: (addr array code-point-utf8) <- copy _data var top-addr/ecx: (addr int) <- get self, top var i/eax: int <- copy *top-addr i <- decrement { compare i, 0 break-if-< var g/edx: (addr code-point-utf8) <- index data, i write-code-point-utf8 out, *g i <- decrement loop } } fn word-at-gap _self: (addr gap-buffer), out: (addr stream byte) { var self/esi: (addr gap-buffer) <- copy _self clear-stream out { var g/eax: code-point-utf8 <- code-point-utf8-at-gap self var at-word?/eax: boolean <- is-ascii-word-code-point-utf8? g compare at-word?, 0/false break-if-!= return } var left/ecx: (addr code-point-utf8-stack) <- get self, left var left-index/eax: int <- top-most-word left emit-stack-from-index left, left-index, out var right/ecx: (addr code-point-utf8-stack) <- get self, right var right-index/eax: int <- top-most-word right emit-stack-to-index right, right-index, out } fn test-word-at-gap-single-word-with-gap-at-end { var _g: gap-buffer var g/esi: (addr gap-buffer) <- address _g initialize-gap-buffer-with g, "abc" # gap is at end (right is empty) var out-storage: (stream byte 0x10) var out/eax: (addr stream byte) <- address out-storage word-at-gap g, out check-stream-equal out, "abc", "F - test-word-at-gap-single-word-with-gap-at-end" } fn test-word-at-gap-single-word-with-gap-at-start { var _g: gap-buffer var g/esi: (addr gap-buffer) <- address _g initialize-gap-buffer-with g, "abc" gap-to-start g # var out-storage: (stream byte 0x10) var out/eax: (addr stream byte) <- address out-storage word-at-gap g, out check-stream-equal out, "abc", "F - test-word-at-gap-single-word-with-gap-at-start" } fn test-word-at-gap-multiple-words-with-gap-at-non-word-code-point-utf8-at-end { var _g: gap-buffer var g/esi: (addr gap-buffer) <- address _g initialize-gap-buffer-with g, "abc " # gap is at end (right is empty) var out-storage: (stream byte 0x10) var out/eax: (addr stream byte) <- address out-storage word-at-gap g, out check-stream-equal out, "", "F - test-word-at-gap-multiple-words-with-gap-at-non-word-code-point-utf8-at-end" } fn test-word-at-gap-multiple-words-with-gap-at-non-word-code-point-utf8-at-start { var _g: gap-buffer var g/esi: (addr gap-buffer) <- address _g initialize-gap-buffer-with g, " abc" gap-to-start g # var out-storage: (stream byte 0x10) var out/eax: (addr stream byte) <- address out-storage word-at-gap g, out check-stream-equal out, "", "F - test-word-at-gap-multiple-words-with-gap-at-non-word-code-point-utf8-at-start" } fn test-word-at-gap-multiple-words-with-gap-at-end { var _g: gap-buffer var g/esi: (addr gap-buffer) <- address _g initialize-gap-buffer-with g, "a bc d" # gap is at end (right is empty) var out-storage: (stream byte 0x10) var out/eax: (addr stream byte) <- address out-storage word-at-gap g, out check-stream-equal out, "d", "F - test-word-at-gap-multiple-words-with-gap-at-end" } fn test-word-at-gap-multiple-words-with-gap-at-initial-word { var _g: gap-buffer var g/esi: (addr gap-buffer) <- address _g initialize-gap-buffer-with g, "a bc d" gap-to-start g # var out-storage: (stream byte 0x10) var out/eax: (addr stream byte) <- address out-storage word-at-gap g, out check-stream-equal out, "a", "F - test-word-at-gap-multiple-words-with-gap-at-initial-word" } fn test-word-at-gap-multiple-words-with-gap-at-final-word { var _g: gap-buffer var g/esi: (addr gap-buffer) <- address _g initialize-gap-buffer-with g, "a bc d" var dummy/eax: code-point-utf8 <- gap-left g # gap is at final word var out-storage: (stream byte 0x10) var out/eax: (addr stream byte) <- address out-storage word-at-gap g, out check-stream-equal out, "d", "F - test-word-at-gap-multiple-words-with-gap-at-final-word" } fn test-word-at-gap-multiple-words-with-gap-at-final-non-word { var _g: gap-buffer var g/esi: (addr gap-buffer) <- address _g initialize-gap-buffer-with g, "abc " var dummy/eax: code-point-utf8 <- gap-left g # gap is at final word var out-storage: (stream byte 0x10) var out/eax: (addr stream byte) <- address out-storage word-at-gap g, out check-stream-equal out, "", "F - test-word-at-gap-multiple-words-with-gap-at-final-non-word" } fn code-point-utf8-at-gap _self: (addr gap-buffer) -> _/eax: code-point-utf8 { # send top of right most of the time var self/esi: (addr gap-buffer) <- copy _self var right/edi: (addr code-point-utf8-stack) <- get self, right var data-ah/eax: (addr handle array code-point-utf8) <- get right, data var data/eax: (addr array code-point-utf8) <- lookup *data-ah var top-addr/ecx: (addr int) <- get right, top { compare *top-addr, 0 break-if-<= var top/ecx: int <- copy *top-addr top <- decrement var result/eax: (addr code-point-utf8) <- index data, top return *result } # send top of left only if right is empty var left/edi: (addr code-point-utf8-stack) <- get self, left var data-ah/eax: (addr handle array code-point-utf8) <- get left, data var data/eax: (addr array code-point-utf8) <- lookup *data-ah var top-addr/ecx: (addr int) <- get left, top { compare *top-addr, 0 break-if-<= var top/ecx: int <- copy *top-addr top <- decrement var result/eax: (addr code-point-utf8) <- index data, top return *result } # send null if everything is empty return 0 } fn top-most-word _self: (addr code-point-utf8-stack) -> _/eax: int { var self/esi: (addr code-point-utf8-stack) <- copy _self var data-ah/edi: (addr handle array code-point-utf8) <- get self, data var _data/eax: (addr array code-point-utf8) <- lookup *data-ah var data/edi: (addr array code-point-utf8) <- copy _data var top-addr/ecx: (addr int) <- get self, top var i/ebx: int <- copy *top-addr i <- decrement { compare i, 0 break-if-< var g/edx: (addr code-point-utf8) <- index data, i var is-word?/eax: boolean <- is-ascii-word-code-point-utf8? *g compare is-word?, 0/false break-if-= i <- decrement loop } i <- increment return i } fn emit-stack-from-index _self: (addr code-point-utf8-stack), start: int, out: (addr stream byte) { var self/esi: (addr code-point-utf8-stack) <- copy _self var data-ah/edi: (addr handle array code-point-utf8) <- get self, data var _data/eax: (addr array code-point-utf8) <- lookup *data-ah var data/edi: (addr array code-point-utf8) <- copy _data var top-addr/ecx: (addr int) <- get self, top var i/eax: int <- copy start { compare i, *top-addr break-if->= var g/edx: (addr code-point-utf8) <- index data, i write-code-point-utf8 out, *g i <- increment loop } } fn emit-stack-to-index _self: (addr code-point-utf8-stack), end: int, out: (addr stream byte) { var self/esi: (addr code-point-utf8-stack) <- copy _self var data-ah/edi: (addr handle array code-point-utf8) <- get self, data var _data/eax: (addr array code-point-utf8) <- lookup *data-ah var data/edi: (addr array code-point-utf8) <- copy _data var top-addr/ecx: (addr int) <- get self, top var i/eax: int <- copy *top-addr i <- decrement { compare i, 0 break-if-< compare i, end break-if-< var g/edx: (addr code-point-utf8) <- index data, i write-code-point-utf8 out, *g i <- decrement loop } } fn is-ascii-word-code-point-utf8? g: code-point-utf8 -> _/eax: boolean { compare g, 0x21/! { break-if-!= return 1/true } compare g, 0x30/0 { break-if->= return 0/false } compare g, 0x39/9 { break-if-> return 1/true } compare g, 0x3f/? { break-if-!=
# Comparing 'regular' size-prefixed strings.
== code
# instruction effective address register displacement immediate
# . op subop mod rm32 base index scale r32
# . 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
string-equal?: # s: (addr array byte), benchmark: (addr array byte) -> result/eax: boolean
# pseudocode:
# if (s->size != benchmark->size) return false
# return string-starts-with?(s, benchmark)
#
# . prologue
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
57/push-edi
# esi = s
8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 8/disp8 . # copy *(ebp+8) to esi
# edi = benchmark
8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 7/r32/edi 0xc/disp8 . # copy *(ebp+12) to edi
# ecx = s->size
8b/copy 0/mod/indirect 6/rm32/esi . . . 1/r32/ecx . . # copy *esi to ecx
$string-equal?:sizes:
# if (ecx != benchmark->size) return false
39/compare 0/mod/indirect 7/rm32/edi . . . 1/r32/ecx . . # compare *edi and ecx
b8/copy-to-eax 0/imm32/false
75/jump-if-!= $string-equal?:end/disp8
$string-equal?:contents:
# string-starts-with?(s, benchmark)
# . . push args
ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12)
ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
# . . call
e8/call string-starts-with?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
$string-equal?:end:
# . restore registers
5f/pop-to-edi
5e/pop-to-esi
59/pop-to-ecx
# . epilogue
89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
5d/pop-to-ebp
c3/return
string-starts-with?: # s: (addr array byte), benchmark: (addr array byte) -> result/eax: boolean
# pseudocode:
# if (s->size < benchmark->size) return false
# currs = s->data
# currb = benchmark->data
# maxb = &benchmark->data[benchmark->size]
# while currb < maxb
# c1 = *currs
# c2 = *currb
# if (c1 != c2) return false
# ++currs, ++currb
# return true
#
# registers:
# currs: esi
# maxs: ecx
# currb: edi
# c1: eax
# c2: ebx
#
# . prologue
55/push-ebp
89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
# . save registers
51/push-ecx
52/push-edx
56/push-esi
57/push-edi
# esi = s
8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 8/disp8 . # copy *(ebp+8) to esi
# edi = benchmark
8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 7/r32/edi 0xc/disp8 . # copy *(ebp+12) to edi
# var bsize/ecx: int = benchmark->size
8b/copy 0/mod/indirect 7/rm32/edi . . . 1/r32/ecx . . # copy *edi to ecx
$string-starts-with?:sizes:
# if (s->size < bsize) return false
39/compare 0/mod/indirect 6/rm32/esi . . . 1/r32/ecx . . # compare *esi with ecx
7c/jump-if-< $string-starts-with?:false/disp8
# var currs/esi: (addr byte) = s->data
81 0/subop/add 3/mod/direct 6/rm32/esi . . . . . 4/imm32 # add to esi
# var currb/edi: (addr byte) = benchmark->data
81 0/subop/add 3/mod/direct 7/rm32/edi . . . . . 4/imm32 # add to edi
# var maxb/ecx: (addr byte) = &benchmark->data[benchmark->size]
01/add 3/mod/direct 1/rm32/ecx . . . 7/r32/edi . . # add edi to ecx
# var c1/eax: byte = 0
31/xor 3/mod/direct 0/rm32/eax . . . 0/r32/eax . . # clear eax
# var c2/edx: byte = 0
31/xor 3/mod/direct 2/rm32/edx . . . 2/r32/edx . . # clear edx
$string-starts-with?:loop:
# if (currs >= maxs) return true
39/compare 3/mod/direct 7/rm32/edi . . . 1/r32/ecx . . # compare edi with ecx
73/jump-if-addr>= $string-starts-with?:true/disp8
# c1 = *currs
8a/copy-byte 0/mod/indirect 6/rm32/esi . . . 0/r32/AL . . # copy byte at *esi to AL
# c2 = *currb
8a/copy-byte 0/mod/indirect 7/rm32/edi . . . 2/r32/DL . . # copy byte at *edi to DL
# if (c1 != c2) return false
39/compare 3/mod/direct 0/rm32/eax . . . 2/r32/edx . . # compare eax and edx
75/jump-if-!= $string-starts-with?:false/disp8
# ++currs
46/increment-esi
# ++currb
47/increment-edi
eb/jump $string-starts-with?:loop/disp8
$string-starts-with?:true:
b8/copy-to-eax 1/imm32
eb/jump $string-starts-with?:end/disp8
$string-starts-with?:false:
b8/copy-to-eax 0/imm32
$string-starts-with?:end:
# . restore registers
5f/pop-to-edi
5e/pop-to-esi
5a/pop-to-edx
59/pop-to-ecx
# . epilogue
89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
5d/pop-to-ebp
c3/return
# - tests
test-compare-empty-with-empty-string:
# eax = string-equal?("", "")
# . . push args
68/push ""/imm32
68/push ""/imm32
# . . call
e8/call string-equal?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# check-ints-equal(eax, 1, msg)
# . . push args
68/push "F - test-compare-empty-with-empty-string"/imm32
68/push 1/imm32/true
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
c3/return
test-compare-empty-with-non-empty-string: # also checks size-mismatch code path
# eax = string-equal?("", "Abc")
# . . push args
68/push "Abc"/imm32
68/push ""/imm32
# . . call
e8/call string-equal?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# check-ints-equal(eax, 0, msg)
# . . push args
68/push "F - test-compare-empty-with-non-empty-string"/imm32
68/push 0/imm32/false
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
c3/return
test-compare-equal-strings:
# eax = string-equal?("Abc", "Abc")
# . . push args
68/push "Abc"/imm32
68/push "Abc"/imm32
# . . call
e8/call string-equal?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# check-ints-equal(eax, 1, msg)
# . . push args
68/push "F - test-compare-equal-strings"/imm32
68/push 1/imm32/true
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
c3/return
test-compare-inequal-strings-equal-sizes:
# eax = string-equal?("Abc", "Adc")
# . . push args
68/push "Adc"/imm32
68/push "Abc"/imm32
# . . call
e8/call string-equal?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# check-ints-equal(eax, 0, msg)
# . . push args
68/push "F - test-compare-inequal-strings-equal-sizes"/imm32
68/push 0/imm32/false
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
c3/return
# helper for later tests
check-strings-equal: # s: (addr array byte), expected: (addr array byte), msg: (addr array byte)
# . prologue
55/push-ebp
89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
# . save registers
50/push-eax
# var eax: boolean = string-equal?(s, expected)
# . . push args
ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12)
ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
# . . call
e8/call string-equal?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# check-ints-equal(eax, 1, msg)
# . . push args
ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0x10/disp8 . # push *(ebp+16)
68/push 1/imm32
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-strings-equal:end:
# . restore registers
58/pop-to-eax
# . epilogue
89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
5d/pop-to-ebp
c3/return
# test the helper
test-check-strings-equal:
# check-strings-equal("Abc", "Abc")
# . . push args
68/push "Abc"/imm32
68/push "Abc"/imm32
# . . call
e8/call check-strings-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
c3/return
# . . vim:nowrap:textwidth=0