# A table is a stream of (key, value) rows. # # Each row consists of an 8-byte key -- a (handle array byte) -- and a variable-size value. # # Accessing the table performs a linear scan for a key string, and always # requires passing in the row size. # # Table primitives have the form (stream, , row-size, ...) -> address/eax # # The following table shows available options for : # if not found: | arg=string arg=slice # ------------------------+--------------------------------------------------- # abort | get get-slice # insert key | get-or-insert get-or-insert-slice # | get-or-insert-handle # stop | get-or-stop get-slice-or-stop # return null | maybe-get maybe-get-slice # Some variants may take extra args. == 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 # if no row is found, abort get: # table: (addr stream {(handle array byte), T}), key: (addr array byte), row-size: int, abort-message-prefix: (addr array byte) -> result/eax: (addr T) # pseudocode: # curr = table->data # max = &table->data[table->write] # while curr < max # var c: (addr array byte) = lookup(*curr) # if string-equal?(key, c) # return curr+8 # curr += row-size # abort # # . 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 # esi = table 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 8/disp8 . # copy *(ebp+8) to esi # var curr/ecx: (addr handle array byte) = table->data 8d/copy-address 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 0xc/disp8 . # copy esi+12 to ecx # var max/edx: (addr byte) = &table->data[table->write] 8b/copy 0/mod/indirect 6/rm32/esi . . . 2/r32/edx . . # copy *esi to edx 8d/copy-address 0/mod/indirect 4/rm32/sib 1/base/ecx 2/index/edx . 2/r32/edx . . # copy ecx+edx to edx $get:search-loop: # if (curr >= max) abort 39/compare 3/mod/direct 1/rm32/ecx . . . 2/r32/edx . . # compare ecx with edx 73/jump-if-addr>= $get:abort/disp8 # var c/eax: (addr array byte) = lookup(*curr) # . . push args ff 6/subop/push 1/mod/*+disp8 1/rm32/ecx . . . . 4/disp8 . # push *(ecx+4) ff 6/subop/push 0/mod/indirect 1/rm32/ecx . . . . . . # push *ecx # . . call e8/call lookup/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # if (string-equal?(key, c)) return curr+8 # . eax = string-equal?(key, c) # . . push args 50/push-eax ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12) # . . call e8/call string-equal?/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # . if (eax != false) return eax = curr+8 3d/compare-eax-and 0/imm32/false 74/jump-if-= $get:mismatch/disp8 8d/copy-address 1/mod/*+disp8 1/rm32/ecx . . . 0/r32/eax 8/disp8 . # copy ecx+8 to eax eb/jump $get:end/disp8 $get:mismatch: # curr += row-size 03/add 1/mod/*+disp8 5/rm32/ebp . . . 1/r32/ecx 0x10/disp8 . # add *(ebp+16) to ecx # loop eb/jump $get:search-loop/disp8 $get:end: # . restore registers 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 $get:abort: # . _write(2/stderr, abort-message-prefix) # . . push args ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0x14/disp8 . # push *(ebp+20) 68/push 2/imm32/stderr # . . call e8/call _write/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # . _write(2/stderr, error) # . . push args 68/push ": get: key not found: "/imm32 68/push 2/imm32/stderr # . . call e8/call _write/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # . _write(2/stderr, key) # . . push args ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12) 68/push 2/imm32/stderr # . . call e8/call _write/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # . _write(2/stderr, "\n") # . . push args 68/push Newline/imm32 68/push 2/imm32/stderr # . . call e8/call _write/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # . syscall(exit, 1) bb/copy-to-ebx 1/imm32 e8/call syscall_exit/disp32 # never gets here test-get: # . prologue 55/push-ebp 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp # - setup: create a table with a couple of keys # var table/ecx: (stream {(handle array byte), number} 24) # 2 rows * 12 bytes/row 81 5/subop/subtract 3/mod/direct 4/rm32/esp . . . . . 0x18/imm32 # subtract from esp 68/push 0x18/imm32/size 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 # insert(table, "code", 12 bytes/row, Heap) # . . push args 68/push Heap/imm32 68/push 0xc/imm32/row-size 68/push "code"/imm32 51/push-ecx # . . call e8/call get-or-insert/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp # insert(table, "data", 12 bytes/row, Heap) # . . push args 68/push Heap/imm32 68/push 0xc/imm32/row-size 68/push "data"/imm32 51/push-ecx # . . call e8/call get-or-insert/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp $test-get:check1: # eax = get(table, "code", 12 bytes/row) # . . push args 68/push 0xc/imm32/row-size 68/push "code"/imm32 51/push-ecx # . . call e8/call get/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp # check-ints-equal(eax - table->data, 8, msg) # . check-ints-equal(eax - table, 20, msg) # . . push args 68/push "F - test-get/0"/imm32 68/push 0x14/imm32 29/subtract 3/mod/direct 0/rm32/eax . . . 1/r32/ecx . . # subtract ecx from eax 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 $test-get:check2: # eax = get(table, "data", 12 bytes/row) # . . push args 68/push 0xc/imm32/row-size 68/push "data"/imm32 51/push-ecx # . . call e8/call get/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp # check-ints-equal(eax - table->data, 20, msg) # . check-ints-equal(eax - table, 32, msg) # . . push args 68/push "F - test-get/1"/imm32 68/push 0x20/imm32 29/subtract 3/mod/direct 0/rm32/eax . . . 1/r32/ecx . . # subtract ecx from eax 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 $test-get:end: # . epilogue 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp 5d/pop-to-ebp c3/return # if no row is found, abort get-slice: # table: (addr stream {(handle array byte), T}), key: (addr slice), row-size: int, abort-message-prefix: (addr array byte) -> result/eax: (addr T) # pseudocode: # curr = table->data # max = &table->data[table->write] # while curr < max # var c: (addr array byte) = lookup(*curr) # if slice-equal?(key, c) # return curr+8 # curr += row-size # abort # # . 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 # esi = table 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 8/disp8 . # copy *(ebp+8) to esi # var curr/ecx: (addr handle array byte) = table->data 8d/copy-address 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 0xc/disp8 . # copy esi+12 to ecx # var max/edx: (addr byte) = &table->data[table->write] 8b/copy 0/mod/indirect 6/rm32/esi . . . 2/r32/edx . . # copy *esi to edx 8d/copy-address 0/mod/indirect 4/rm32/sib 1/base/ecx 2/index/edx . 2/r32/edx . . # copy ecx+edx to edx $get-slice:search-loop: # if (curr >= max) abort 39/compare 3/mod/direct 1/rm32/ecx . . . 2/r32/edx . . # compare ecx with edx 73/jump-if-addr>= $get-slice:abort/disp8 # var c/eax: (addr array byte) = lookup(*curr) # . . push args ff 6/subop/push 1/mod/*+disp8 1/rm32/ecx . . . . 4/disp8 . # push *(ecx+4) ff 6/subop/push 0/mod/indirect 1/rm32/ecx . . . . . . # push *ecx # . . call e8/call lookup/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # if (slice-equal?(key, c)) return curr+8 # . eax = slice-equal?(key, c) # . . push args 50/push-eax ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12) # . . call e8/call slice-equal?/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # . if (eax != false) return eax = curr+8 3d/compare-eax-and 0/imm32/false 74/jump-if-= $get-slice:mismatch/disp8 8d/copy-address 1/mod/*+disp8 1/rm32/ecx . . . 0/r32/eax 8/disp8 . # copy ecx+8 to eax eb/jump $get-slice:end/disp8 $get-slice:mismatch: # curr += row-size 03/add 1/mod/*+disp8 5/rm32/ebp . . . 1/r32/ecx 0x10/disp8 . # add *(ebp+16) to ecx # loop eb/jump $get-slice:search-loop/disp8 $get-slice:end: # . restore registers 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 $get-slice:abort: # . _write(2/stderr, abort-message-prefix) # . . push args ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0x14/disp8 . # push *(ebp+20) 68/push 2/imm32/stderr # . . call e8/call _write/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # . _write(2/stderr, error) # . . push args 68/push ": get-slice: key not found: "/imm32 68/push 2/imm32/stderr # . . call e8/call _write/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # . write-slice-buffered(Stderr, key) # . . push args ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12) 68/push Stderr/imm32 # . . call e8/call write-slice-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # . flush(Stderr) # . . push args 68/push Stderr/imm32 # . . call e8/call flush/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp # . _write(2/stderr, "\n") # . . push args 68/push Newline/imm32 68/push 2/imm32/stderr # . . call e8/call _write/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # . syscall(exit, 1) bb/copy-to-ebx 1/imm32 e8/call syscall_exit/disp32 # never gets here test-get-slice: # . prologue 55/push-ebp 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp # - setup: create a table with a couple of keys # var table/ecx: (stream {string, number} 24) # 2 rows * 12 bytes/row 81 5/subop/subtract 3/mod/direct 4/rm32/esp . . . . . 0x18/imm32 # subtract from esp 68/push 0x18/imm32/size 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 # insert(table, "code", 12 bytes/row, Heap) # . . push args 68/push Heap/imm32 68/push 0xc/imm32/row-size 68/push "code"/imm32 51/push-ecx # . . call e8/call get-or-insert/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp # insert(table, "data", 12 bytes/row, Heap) # . . push args 68/push Heap/imm32 68/push 0xc/imm32/row-size 68/push "data"/imm32 51/push-ecx # . . call e8/call get-or-insert/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp $test-get-slice:check1: # (eax..edx) = "code" b8/copy-to-eax "code"/imm32 8b/copy 0/mod/indirect 0/rm32/eax . . . 2/r32/edx . . # copy *eax to edx 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 2/index/edx . 2/r32/edx 4/disp8 . # copy eax+edx+4 to edx 05/add-to-eax 4/imm32 # var slice/edx: slice = {eax, edx} 52/push-edx 50/push-eax 89/copy 3/mod/direct 2/rm32/edx . . . 4/r32/esp . . # copy esp to edx # eax = get-slice(table, "code", 12 bytes/row) # . . push args 68/push 0xc/imm32/row-size 52/push-edx 51/push-ecx # . . call e8/call get-slice/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp # check-ints-equal(eax - table->data, 8, msg) # first row's value slot returned # . check-ints-equal(eax - table, 20, msg) # . . push args 68/push "F - test-get-slice/0"/imm32 68/push 0x14/imm32 29/subtract 3/mod/direct 0/rm32/eax . . . 1/r32/ecx . . # subtract ecx from eax 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 $test-get-slice:check2: # (eax..edx) = "data" b8/copy-to-eax "data"/imm32 8b/copy 0/mod/indirect 0/rm32/eax . . . 2/r32/edx . . # copy *eax to edx 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 2/index/edx . 2/r32/edx 4/disp8 . # copy eax+edx+4 to edx 05/add-to-eax 4/imm32 # var slice/edx: slice = {eax, edx} 52/push-edx 50/push-eax 89/copy 3/mod/direct 2/rm32/edx . . . 4/r32/esp . . # copy esp to edx # eax = get-slice(table, "data" slice, 12 bytes/row) # . . push args 68/push 0xc/imm32/row-size 52/push-edx 51/push-ecx # . . call e8/call get-slice/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp # check-ints-equal(eax - table->data, 20, msg) # . check-ints-equal(eax - table, 32, msg) # . . push args 68/push "F - test-get-slice/1"/imm32 68/push 0x20/imm32 29/subtract 3/mod/direct 0/rm32/eax . . . 1/r32/ecx . . # subtract ecx from eax 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 $test-get-slice:end: # . epilogue 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp 5d/pop-to-ebp c3/return # if no row is found, save 'key' to the next available row # if there are no rows free, abort # return the address of the value get-or-insert: # table: (addr stream {(handle array byte), T}), key: (addr array byte), row-size: int, ad: (addr allocation-descriptor) -> result/eax: (addr T) # pseudocode: # curr = table->data # max = &table->data[table->write] # while curr < max # var c: (addr array byte) = lookup(*curr) # if string-equal?(key, c) # return curr+8 # curr += row-size # if table->write >= table->size # abort # zero-out(max, row-size) # copy-array(ad, key, max) # table->write += row-size # return max+8 # # . 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 # esi = table 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 8/disp8 . # copy *(ebp+8) to esi # var curr/ecx: (addr handle array byte) = table->data 8d/copy-address 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 0xc/disp8 . # copy esi+12 to ecx # var max/edx: (addr byte) = &table->data[table->write] 8b/copy 0/mod/indirect 6/rm32/esi . . . 2/r32/edx . . # copy *esi to edx 8d/copy-address 0/mod/indirect 4/rm32/sib 1/base/ecx 2/index/edx . 2/r32/edx . . # copy ecx+edx to edx $get-or-insert:search-loop: # if (curr >= max) break 39/compare 3/mod/direct 1/rm32/ecx . . . 2/r32/edx . . # compare ecx with edx 73/jump-if-addr>= $get-or-insert:not-found/disp8 # var c/eax: (addr array byte) = lookup(*curr) # . . push args ff 6/subop/push 1/mod/*+disp8 1/rm32/ecx . . . . 4/disp8 . # push *(ecx+4) ff 6/subop/push 0/mod/indirect 1/rm32/ecx . . . . . . # push *ecx # . . call e8/call lookup/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # if (string-equal?(key, c)) return curr+8 # . eax = string-equal?(key, c) # . . push args 50/push-eax ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12) # . . call e8/call string-equal?/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # . if (eax != false) return eax = curr+8 3d/compare-eax-and 0/imm32/false 74/jump-if-= $get-or-insert:mismatch/disp8 8d/copy-address 1/mod/*+disp8 1/rm32/ecx . . . 0/r32/eax 8/disp8 . # copy ecx+8 to eax eb/jump $get-or-insert:end/disp8 $get-or-insert:mismatch: # curr += row-size 03/add 1/mod/*+disp8 5/rm32/ebp . . . 1/r32/ecx 0x10/disp8 . # add *(ebp+16) to ecx # loop eb/jump $get-or-insert:search-loop/disp8 $get-or-insert:not-found: # if (table->write >= table->size) abort 8b/copy 0/mod/indirect 6/rm32/esi . . . 1/r32/ecx . . # copy *esi to ecx 3b/compare 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 8/disp8 . # compare ecx with *(esi+8) 73/jump-if-addr>= $get-or-insert:abort/disp8 # zero-out(max, row-size) # . . push args ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0x10/disp8 . # push *(ebp+16) 52/push-edx # . . call e8/call zero-out/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # copy-array(ad, key, max) # . . push args 52/push-edx 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 . . . . 0x14/disp8 . # push *(ebp+20) # . . call e8/call copy-array/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp # table->write += row-size # . eax = row-size 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 0x10/disp8 . # copy *(ebp+16) to eax # . table->write += eax 01/add 0/mod/indirect 6/rm32/esi . . . 0/r32/eax . . # add eax to *esi # return max+8 # . eax = max 89/copy 3/mod/direct 0/rm32/eax . . . 2/r32/edx . . # copy edx to eax # . eax += 8 05/add-to-eax 8/imm32 $get-or-insert:end: # . restore registers 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 $get-or-insert:abort: # . _write(2/stderr, error) # . . push args 68/push "get-or-insert: table is full\n"/imm32 68/push 2/imm32/stderr # . . call e8/call _write/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # . syscall(exit, 1) bb/copy-to-ebx 1/imm32 e8/call syscall_exit/disp32 # never gets here test-get-or-insert: # . prologue 55/push-ebp 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp # var table/ecx: (stream {(handle array byte), number} 24) # 2 rows * 12 bytes/row 81 5/subop/subtract 3/mod/direct 4/rm32/esp . . . . . 0x18/imm32 # subtract from esp 68/push 0x18/imm32/size 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 $test-get-or-insert:first-call: # - start with an empty table, insert one key, verify that it was inserted # eax = get-or-insert(table, "code", 12 bytes/row, Heap) # . . push args 68/push Heap/imm32 68/push 0xc/imm32/row-size 68/push "code"/imm32 51/push-ecx # . . call e8/call get-or-insert/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp # check-ints-equal(eax - table->data, 8, msg) # first row's value slot returned # . check-ints-equal(eax - table, 20, msg) # . . push args 68/push "F - test-get-or-insert/0"/imm32 68/push 0x14/imm32 29/subtract 3/mod/direct 0/rm32/eax . . . 1/r32/ecx . . # subtract ecx from eax 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-ints-equal(table->write, row-size = 12, msg) # . . push args 68/push "F - test-get-or-insert/1"/imm32 68/push 0xc/imm32/row-size ff 6/subop/push 0/mod/indirect 1/rm32/ecx . . . . . . # push *ecx # . . call e8/call check-ints-equal/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp # var curr-addr/eax: (addr array byte) = lookup(table->data) # . . push args ff 6/subop/push 1/mod/*+disp8 1/rm32/ecx . . . . 0x10/disp8 . # push *(ecx+16) ff 6/subop/push 1/mod/*+disp8 1/rm32/ecx . . . . 0xc/disp8 . # push *(ecx+12) # . . call e8/call lookup/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # check-strings-equal(curr-addr, "code", msg) # . . push args 68/push "F - test-get-or-insert/2"/imm32 68/push "code"/imm32 50/push-eax # . . call e8/call check-strings-equal/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp $test-get-or-insert:second-call: # - insert the same key again, verify that it was reused # eax = get-or-insert(table, "code", 12 bytes/row, Heap) # . . push args 68/push Heap/imm32 68/push 0xc/imm32/row-size 68/push "code"/imm32 51/push-ecx # . . call e8/call get-or-insert/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp # check-ints-equal(eax - table->data, 8, msg) # . check-ints-equal(eax - table, 20, msg) # . . push args 68/push "F - test-get-or-insert/3"/imm32 68/push 0x14/imm32 29/subtract 3/mod/direct 0/rm32/eax . . . 1/r32/ecx . . # subtract ecx from eax 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 # no new row inserted # . check-ints-equal(table->write, row-size = 12, msg) # . . push args 68/push "F - test-get-or-insert/4"/imm32 68/push 0xc/imm32/row-size ff 6/subop/push 0/mod/indirect 1/rm32/ecx . . . . . . # push *ecx # . . call e8/call check-ints-equal/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp # curr-addr = lookup(table->data) # . . push args ff 6/subop/push 1/mod/*+disp8 1/rm32/ecx . . . . 0x10/disp8 . # push *(ecx+16) ff 6/subop/push 1/mod/*+disp8 1/rm32/ecx . . . . 0xc/disp8 . # push *(ecx+12) # . . call e8/call lookup/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # check-strings-equal(curr-addr, "code", msg) # . . push args 68/push "F - test-get-or-insert/5"/imm32 68/push "code"/imm32 50/push-eax # . . call e8/call check-strings-equal/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp $test-get-or-insert:third-call: # - insert a new key, verify that it was inserted # eax = get-or-insert(table, "data", 12 bytes/row, Heap) # . . push args 68/push Heap/imm32 68/push 0xc/imm32/row-size 68/push "data"/imm32 51/push-ecx # . . call e8/call get-or-insert/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp # table gets a new row # check-ints-equal(eax - table->data, 20, msg) # second row's value slot returned # . check-ints-equal(eax - table, 32, msg) # . . push args 68/push "F - test-get-or-insert/6"/imm32 68/push 0x20/imm32 29/subtract 3/mod/direct 0/rm32/eax . . . 1/r32/ecx . . # subtract ecx from eax 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-ints-equal(table->write, 2 rows = 24, msg) # . . push args 68/push "F - test-get-or-insert/7"/imm32 68/push 0x18/imm32/two-rows ff 6/subop/push 0/mod/indirect 1/rm32/ecx . . . . . . # push *ecx # . . call e8/call check-ints-equal/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp # curr-addr = lookup(table->data+12) # . . push args ff 6/subop/push 1/mod/*+disp8 1/rm32/ecx . . . . 0x1c/disp8 . # push *(ecx+28) ff 6/subop/push 1/mod/*+disp8 1/rm32/ecx . . . . 0x18/disp8 . # push *(ecx+24) # . . call e8/call lookup/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # check-strings-equal(curr-addr, "data", msg) # . . push args 68/push "F - test-get-or-insert/8"/imm32 68/push "data"/imm32 50/push-eax # . . call e8/call check-strings-equal/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp $test-get-or-insert:end: # . epilogue 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp 5d/pop-to-ebp c3/return # if no row is found, save 'key' to the next available row # if there are no rows free, abort # return the address of the value get-or-insert-handle: # table: (addr stream {(handle array byte), T}), key: (handle array byte), row-size: int -> result/eax: (addr T) # pseudocode: # var curr: (addr handle stream) = table->data # var max: (addr byte) = &table->data[table->write] # var k: (addr array byte) = lookup(key) # while curr < max # var c: (addr array byte) = lookup(*curr) # if string-equal?(k, c) # return curr+8 # curr += row-size # if table->write >= table->size # abort # *max = key # table->write += row-size # return max+8 # # . 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 53/push-ebx 56/push-esi # esi = table 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 8/disp8 . # copy *(ebp+8) to esi # var k/ebx: (addr array byte) = lookup(key) # . eax = lookup(key) # . . push args ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0x10/disp8 . # push *(ebp+16) ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12) # . . call e8/call lookup/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # . ebx = eax 89/copy 3/mod/direct 3/rm32/ebx . . . 0/r32/eax . . # copy eax to ebx # var curr/ecx: (addr handle array byte) = table->data 8d/copy-address 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 0xc/disp8 . # copy esi+12 to ecx # var max/edx: (addr byte) = &table->data[table->write] 8b/copy 0/mod/indirect 6/rm32/esi . . . 2/r32/edx . . # copy *esi to edx 8d/copy-address 0/mod/indirect 4/rm32/sib 1/base/ecx 2/index/edx . 2/r32/edx . . # copy ecx+edx to edx $get-or-insert-handle:search-loop: # if (curr >= max) break 39/compare 3/mod/direct 1/rm32/ecx . . . 2/r32/edx . . # compare ecx with edx 73/jump-if-addr>= $get-or-insert-handle:not-found/disp8 # var c/eax: (addr array byte) = lookup(*curr) # . . push args ff 6/subop/push 1/mod/*+disp8 1/rm32/ecx . . . . 4/disp8 . # push *(ecx+4) ff 6/subop/push 0/mod/indirect 1/rm32/ecx . . . . . . # push *ecx # . . call e8/call lookup/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # if (string-equal?(k, c)) return curr+8 # . eax = string-equal?(k, c) # . . push args 50/push-eax 53/push-ebx # . . call e8/call string-equal?/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # . if (eax != false) return eax = curr+8 3d/compare-eax-and 0/imm32/false 74/jump-if-= $get-or-insert-handle:mismatch/disp8 8d/copy-address 1/mod/*+disp8 1/rm32/ecx . . . 0/r32/eax 8/disp8 . # copy ecx+8 to eax eb/jump $get-or-insert-handle:end/disp8 $get-or-insert-handle:mismatch: # curr += row-size 03/add 1/mod/*+disp8 5/rm32/ebp . . . 1/r32/ecx 0x14/disp8 . # add *(ebp+20) to ecx # loop eb/jump $get-or-insert-handle:search-loop/disp8 $get-or-insert-handle:not-found: # if (table->write >= table->size) abort 8b/copy 0/mod/indirect 6/rm32/esi . . . 1/r32/ecx . . # copy *esi to ecx 3b/compare 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 8/disp8 . # compare ecx with *(esi+8) 73/jump-if-addr>= $get-or-insert-handle:abort/disp8 # table->write += row-size # . eax = row-size 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 0x14/disp8 . # copy *(ebp+20) to eax # . table->write += eax 01/add 0/mod/indirect 6/rm32/esi . . . 0/r32/eax . . # add eax to *esi # *max = key 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 0xc/disp8 . # copy *(ebp+12) to eax 89/copy 0/mod/indirect 2/rm32/edx . . . 0/r32/eax . . # copy eax to *edx 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 0x10/disp8 . # copy *(ebp+16) to eax 89/copy 1/mod/*+disp8 2/rm32/edx . . . 0/r32/eax 4/disp8 . # copy eax to *(edx+4) # return max+8 # . eax = max 89/copy 3/mod/direct 0/rm32/eax . . . 2/r32/edx . . # copy edx to eax # . eax += 8 05/add-to-eax 8/imm32 $get-or-insert-handle:end: # . restore registers 5e/pop-to-esi 5b/pop-to-ebx 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 $get-or-insert-handle:abort: # . _write(2/stderr, error) # . . push args 68/push "get-or-insert-handle: table is full\n"/imm32 68/push 2/imm32/stderr # . . call e8/call _write/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # . syscall(exit, 1) bb/copy-to-ebx 1/imm32 e8/call syscall_exit/disp32 # never gets here test-get-or-insert-handle: # . prologue 55/push-ebp 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp # var table/ecx: (stream {(handle array byte), number} 24) # 2 rows * 12 bytes/row 81 5/subop/subtract 3/mod/direct 4/rm32/esp . . . . . 0x18/imm32 # subtract from esp 68/push 0x18/imm32/size 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 # var h/edx: (handle array byte) 68/push 0/imm32 68/push 0/imm32 89/copy 3/mod/direct 2/rm32/edx . . . 4/r32/esp . . # copy esp to edx $test-get-or-insert-handle:first-call: # - start with an empty table, insert one key, verify that it was inserted # copy-array(Heap, "code", h) # . . push args 52/push-edx 68/push "code"/imm32 68/push Heap/imm32 # . . call e8/call copy-array/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp # eax = get-or-insert-handle(table, h, 12 bytes/row) # . . push args 68/push 0xc/imm32/row-size ff 6/subop/push 1/mod/*+disp8 2/rm32/edx . . . . 4/disp8 . # push *(edx+4) ff 6/subop/push 0/mod/indirect 2/rm32/edx . . . . . . # push *edx 51/push-ecx # . . call e8/call get-or-insert-handle/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp # check-ints-equal(eax - table->data, 8, msg) # first row's value slot returned # . check-ints-equal(eax - table, 20, msg) # . . push args 68/push "F - test-get-or-insert-handle/0"/imm32 68/push 0x14/imm32 29/subtract 3/mod/direct 0/rm32/eax . . . 1/r32/ecx . . # subtract ecx from eax 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-ints-equal(table->write, row-size = 12, msg) # . . push args 68/push "F - test-get-or-insert-handle/1"/imm32 68/push 0xc/imm32/row-size ff 6/subop/push 0/mod/indirect 1/rm32/ecx . . . . . . # push *ecx # . . call e8/call check-ints-equal/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp # var curr-addr/eax: (addr array byte) = lookup(table->data) # . . push args ff 6/subop/push 1/mod/*+disp8 1/rm32/ecx . . . . 0x10/disp8 . # push *(ecx+16) ff 6/subop/push 1/mod/*+disp8 1/rm32/ecx . . . . 0xc/disp8 . # push *(ecx+12) # . . call e8/call lookup/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # check-strings-equal(curr-addr, "code", msg) # . . push args 68/push "F - test-get-or-insert-handle/2"/imm32 68/push "code"/imm32 50/push-eax # . . call e8/call check-strings-equal/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp $test-get-or-insert-handle:second-call: # - insert the same key again, verify that it was reused # copy-array(Heap, "code", h) # . . push args 52/push-edx 68/push "code"/imm32 68/push Heap/imm32 # . . call e8/call copy-array/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp # eax = get-or-insert-handle(table, h, 12 bytes/row) # . . push args 68/push 0xc/imm32/row-size ff 6/subop/push 1/mod/*+disp8 2/rm32/edx . . . . 4/disp8 . # push *(edx+4) ff 6/subop/push 0/mod/indirect 2/rm32/edx . . . . . . # push *edx 51/push-ecx # . . call e8/call get-or-insert-handle/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp # check-ints-equal(eax - table->data, 8, msg) # . check-ints-equal(eax - table, 20, msg) # . . push args 68/push "F - test-get-or-insert-handle/3"/imm32 68/push 0x14/imm32 29/subtract 3/mod/direct 0/rm32/eax . . . 1/r32/ecx . . # subtract ecx from eax 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 # no new row inserted # . check-ints-equal(table->write, row-size = 12, msg) # . . push args 68/push "F - test-get-or-insert-handle/4"/imm32 68/push 0xc/imm32/row-size ff 6/subop/push 0/mod/indirect 1/rm32/ecx . . . . . . # push *ecx # . . call e8/call check-ints-equal/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp # curr-addr = lookup(table->data) # . . push args ff 6/subop/push 1/mod/*+disp8 1/rm32/ecx . . . . 0x10/disp8 . # push *(ecx+16) ff 6/subop/push 1/mod/*+disp8 1/rm32/ecx . . . . 0xc/disp8 . # push *(ecx+12) # . . call e8/call lookup/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # check-strings-equal(curr-addr, "code", msg) # . . push args 68/push "F - test-get-or-insert-handle/5"/imm32 68/push "code"/imm32 50/push-eax # . . call e8/call check-strings-equal/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp $test-get-or-insert-handle:third-call: # - insert a new key, verify that it was inserted # copy-array(Heap, "data", h) # . . push args 52/push-edx 68/push "data"/imm32 68/push Heap/imm32 # . . call e8/call copy-array/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp # eax = get-or-insert-handle(table, h, 12 bytes/row) # . . push args 68/push 0xc/imm32/row-size ff 6/subop/push 1/mod/*+disp8 2/rm32/edx . . . . 4/disp8 . # push *(edx+4) ff 6/subop/push 0/mod/indirect 2/rm32/edx . . . . . . # push *edx 51/push-ecx # . . call e8/call get-or-insert-handle/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp # table gets a new row # check-ints-equal(eax - table->data, 20, msg) # second row's value slot returned # . check-ints-equal(eax - table, 32, msg) # . . push args 68/push "F - test-get-or-insert-handle/6"/imm32 68/push 0x20/imm32 29/subtract 3/mod/direct 0/rm32/eax . . . 1/r32/ecx . . # subtract ecx from eax 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-ints-equal(table->write, 2 rows = 24, msg) # . . push args 68/push "F - test-get-or-insert-handle/7"/imm32 68/push 0x18/imm32/two-rows ff 6/subop/push 0/mod/indirect 1/rm32/ecx . . . . . . # push *ecx # . . call e8/call check-ints-equal/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp # curr-addr = lookup(table->data+12) # . . push args ff 6/subop/push 1/mod/*+disp8 1/rm32/ecx . . . . 0x1c/disp8 . # push *(ecx+28) ff 6/subop/push 1/mod/*+disp8 1/rm32/ecx . . . . 0x18/disp8 . # push *(ecx+24) # . . call e8/call lookup/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # check-strings-equal(curr-addr, "data", msg) # . . push args 68/push "F - test-get-or-insert-handle/8"/imm32 68/push "data"/imm32 50/push-eax # . . call e8/call check-strings-equal/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp $test-get-or-insert-handle:end: # . epilogue 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp 5d/pop-to-ebp c3/return # if no row is found, save 'key' in the next available row # if there are no rows free, abort get-or-insert-slice: # table: (addr stream {(handle array byte), T}), key: (addr slice), row-size: int, ad: (addr allocation-descriptor) -> result/eax: (addr T) # pseudocode: # curr = table->data # max = &table->data[table->write] # while curr < max # var c: (addr array byte) = lookup(*curr) # if slice-equal?(key, *curr) # return curr+8 # curr += row-size # if table->write >= table->size # abort # zero-out(max, row-size) # slice-to-string(ad, key, max) # table->write += row-size # return max+8 # # . 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 # esi = table 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 8/disp8 . # copy *(ebp+8) to esi # var curr/ecx: (addr handle array byte) = table->data 8d/copy-address 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 0xc/disp8 . # copy esi+12 to ecx # var max/edx: (addr byte) = &table->data[table->write] 8b/copy 0/mod/indirect 6/rm32/esi . . . 2/r32/edx . . # copy *esi to edx 8d/copy-address 0/mod/indirect 4/rm32/sib 1/base/ecx 2/index/edx . 2/r32/edx . . # copy ecx+edx to edx $get-or-insert-slice:search-loop: # if (curr >= max) break 39/compare 3/mod/direct 1/rm32/ecx . . . 2/r32/edx . . # compare ecx with edx 73/jump-if-addr>= $get-or-insert-slice:not-found/disp8 # var c/eax: (addr array byte) = lookup(*curr) # . . push args ff 6/subop/push 1/mod/*+disp8 1/rm32/ecx . . . . 4/disp8 . # push *(ecx+4) ff 6/subop/push 0/mod/indirect 1/rm32/ecx . . . . . . # push *ecx # . . call e8/call lookup/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # if (slice-equal?(key, c)) return curr+4 # . eax = slice-equal?(key, c) # . . push args 50/push-eax ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12) # . . call e8/call slice-equal?/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # . if (eax != false) return eax = curr+8 3d/compare-eax-and 0/imm32/false 74/jump-if-= $get-or-insert-slice:mismatch/disp8 8d/copy-address 1/mod/*+disp8 1/rm32/ecx . . . 0/r32/eax 8/disp8 . # copy ecx+8 to eax eb/jump $get-or-insert-slice:end/disp8 $get-or-insert-slice:mismatch: # curr += row-size 03/add 1/mod/*+disp8 5/rm32/ebp . . . 1/r32/ecx 0x10/disp8 . # add *(ebp+16) to ecx # loop eb/jump $get-or-insert-slice:search-loop/disp8 $get-or-insert-slice:not-found: # result/eax = 0 31/xor 3/mod/direct 0/rm32/eax . . . 0/r32/eax . . # clear eax # if (table->write >= table->size) abort 8b/copy 0/mod/indirect 6/rm32/esi . . . 1/r32/ecx . . # copy *esi to ecx 3b/compare 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 8/disp8 . # compare ecx with *(esi+8) 7d/jump-if->= $get-or-insert-slice:abort/disp8 # zero-out(max, row-size) # . . push args ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0x10/disp8 . # push *(ebp+16) 52/push-edx # . . call e8/call zero-out/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # slice-to-string(ad, key, max) # . . push args 52/push-edx 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 . . . . 0x14/disp8 . # push *(ebp+20) # . . call e8/call slice-to-string/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp # table->write += row-size # . eax = row-size 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 0x10/disp8 . # copy *(ebp+16) to eax # . table->write += eax 01/add 0/mod/indirect 6/rm32/esi . . . 0/r32/eax . . # add eax to *esi # return max+8 # . eax = max 89/copy 3/mod/direct 0/rm32/eax . . . 2/r32/edx . . # copy edx to eax # . eax += 8 05/add-to-eax 8/imm32 $get-or-insert-slice:end: # . restore registers 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 $get-or-insert-slice:abort: # . _write(2/stderr, error) # . . push args 68/push "get-or-insert-slice: table is full\n"/imm32 68/push 2/imm32/stderr # . . call e8/call _write/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # . syscall(exit, 1) bb/copy-to-ebx 1/imm32 e8/call syscall_exit/disp32 # never gets here test-get-or-insert-slice: # . prologue 55/push-ebp 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp # var table/ecx: (stream {string, number} 24) # 2 rows * 12 bytes/row 81 5/subop/subtract 3/mod/direct 4/rm32/esp . . . . . 0x18/imm32 # subtract from esp 68/push 0x18/imm32/size 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 # (eax..edx) = "code" b8/copy-to-eax "code"/imm32 8b/copy 0/mod/indirect 0/rm32/eax . . . 2/r32/edx . . # copy *eax to edx 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 2/index/edx . 2/r32/edx 4/disp8 . # copy eax+edx+4 to edx 05/add-to-eax 4/imm32 # var slice/edx: slice = {eax, edx} 52/push-edx 50/push-eax 89/copy 3/mod/direct 2/rm32/edx . . . 4/r32/esp . . # copy esp to edx $test-get-or-insert-slice:first-call: # - start with an empty table, insert one key, verify that it was inserted # eax = get-or-insert-slice(table, "code" slice, 12 bytes/row, Heap) # . . push args 68/push Heap/imm32 68/push 0xc/imm32/row-size 52/push-edx 51/push-ecx # . . call e8/call get-or-insert-slice/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp # check-ints-equal(eax - table->data, 8, msg) # first row's value slot returned # . check-ints-equal(eax - table, 20, msg) # . . push args 68/push "F - test-get-or-insert-slice/0"/imm32 68/push 0x14/imm32 29/subtract 3/mod/direct 0/rm32/eax . . . 1/r32/ecx . . # subtract ecx from eax 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-ints-equal(table->write, row-size = 12, msg) # . . push args 68/push "F - test-get-or-insert-slice/1"/imm32 68/push 0xc/imm32/row-size ff 6/subop/push 0/mod/indirect 1/rm32/ecx . . . . . . # push *ecx # . . call e8/call check-ints-equal/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp # var curr-addr/eax: (addr array byte) = lookup(table->data) # . . push args ff 6/subop/push 1/mod/*+disp8 1/rm32/ecx . . . . 0x10/disp8 . # push *(ecx+16) ff 6/subop/push 1/mod/*+disp8 1/rm32/ecx . . . . 0xc/disp8 . # push *(ecx+12) # . . call e8/call lookup/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # check-strings-equal(curr-addr, "code", msg) # . . push args 68/push "F - test-get-or-insert-slice/2"/imm32 68/push "code"/imm32 50/push-eax # . . call e8/call check-strings-equal/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp $test-get-or-insert-slice:second-call: # - insert the same key again, verify that it was reused # eax = get-or-insert-slice(table, "code" slice, 12 bytes/row) # . . push args 68/push Heap/imm32 68/push 0xc/imm32/row-size 52/push-edx 51/push-ecx # . . call e8/call get-or-insert-slice/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp # check-ints-equal(eax - table->data, 8, msg) # . check-ints-equal(eax - table, 20, msg) # . . push args 68/push "F - test-get-or-insert-slice/3"/imm32 68/push 0x14/imm32 29/subtract 3/mod/direct 0/rm32/eax . . . 1/r32/ecx . . # subtract ecx from eax 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 # no new row inserted # . check-ints-equal(table->write, row-size = 12, msg) # . . push args 68/push "F - test-get-or-insert-slice/4"/imm32 68/push 0xc/imm32/row-size ff 6/subop/push 0/mod/indirect 1/rm32/ecx . . . . . . # push *ecx # . . call e8/call check-ints-equal/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp # curr-addr = lookup(table->data) # . . push args ff 6/subop/push 1/mod/*+disp8 1/rm32/ecx . . . . 0x10/disp8 . # push *(ecx+16) ff 6/subop/push 1/mod/*+disp8 1/rm32/ecx . . . . 0xc/disp8 . # push *(ecx+12) # . . call e8/call lookup/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # check-str
## undo/redo

# for every undoable event, create a type of *operation* that contains all the
# information needed to reverse it
exclusive-container operation [
  typing:insert-operation
  move:move-operation
  delete:delete-operation
]

container insert-operation [
  before-row:num
  before-column:num
  before-top-of-screen:&:duplex-list:char
  after-row:num
  after-column:num
  after-top-of-screen:&:duplex-list:char
  # inserted text is from 'insert-from' until 'insert-until'; list doesn't have to terminate
  insert-from:&:duplex-list:char
  insert-until:&:duplex-list:char
  tag:num  # event causing this operation; might be used to coalesce runs of similar events
    # 0: no coalesce (enter+indent)
    # 1: regular alphanumeric characters
]

container move-operation [
  before-row:num
  before-column:num
  before-top-of-screen:&:duplex-list:char
  after-row:num
  after-column:num
  after-top-of-screen:&:duplex-list:char
  tag:num  # event causing this operation; might be used to coalesce runs of similar events
    # 0: no coalesce (touch events, etc)
    # 1: left arrow
    # 2: right arrow
    # 3: up arrow
    # 4: down arrow
]

container delete-operation [
  before-row:num
  before-column:num
  before-top-of-screen:&:duplex-list:char
  after-row:num
  after-column:num
  after-top-of-screen:&:duplex-list:char
  deleted-text:&:duplex-list:char
  delete-from:&:duplex-list:char
  delete-until:&:duplex-list:char
  tag:num  # event causing this operation; might be used to coalesce runs of similar events
    # 0: no coalesce (ctrl-k, ctrl-u)
    # 1: backspace
    # 2: delete
]

# every editor accumulates a list of operations to undo/redo
container editor [
  undo:&:list:&:operation
  redo:&:list:&:operation
]

# ctrl-z - undo operation
after <handle-special-character> [
  {
    undo?:bool <- equal c, 26/ctrl-z
    break-unless undo?
    undo:&:list:&:operation <- get *editor, undo:offset
    break-unless undo
    op:&:operation <- first undo
    undo <- rest undo
    *editor <- put *editor, undo:offset, undo
    redo:&:list:&:operation <- get *editor, redo:offset
    redo <- push op, redo
    *editor <- put *editor, redo:offset, redo
    <handle-undo>
    return true/go-render
  }
]

# ctrl-y - redo operation
after <handle-special-character> [
  {
    redo?:bool <- equal c, 25/ctrl-y
    break-unless redo?
    redo:&:list:&:operation <- get *editor, redo:offset
    break-unless redo
    op:&:operation <- first redo
    redo <- rest redo
    *editor <- put *editor, redo:offset, redo
    undo:&:list:&:operation <- get *editor, undo:offset
    undo <- push op, undo
    *editor <- put *editor, undo:offset, undo
    <handle-redo>
    return true/go-render
  }
]

# undo typing

scenario editor-can-undo-typing [
  local-scope
  # create an editor and type a character
  assume-screen 10/width, 5/height
  e:&:editor <- new-editor [], 0/left, 10/right
  editor-render screen, e
  assume-console [
    type [0]
  ]
  editor-event-loop screen, console, e
  # undo
  assume-console [
    press ctrl-z
  ]
  run [
    editor-event-loop screen, console, e
  ]
  # character should be gone
  screen-should-contain [
    .          .
    .          .
    .┈┈┈┈┈┈┈┈┈┈.
    .          .
  ]
  # cursor should be in the right place
  assume-console [
    type [1]
  ]
  run [
    editor-event-loop screen, console, e
  ]
  screen-should-contain [
    .          .
    .1         .
    .┈┈┈┈┈┈┈┈┈┈.
    .          .
  ]
]

# save operation to undo
after <begin-insert-character> [
  top-before:&:duplex-list:char <- get *editor, top-of-screen:offset
  cursor-before:&:duplex-list:char <- get *editor, before-cursor:offset
]
before <end-insert-character> [
  top-after:&:duplex-list:char <- get *editor, top-of-screen:offset
  cursor-row:num <- get *editor, cursor-row:offset
  cursor-column:num <- get *editor, cursor-column:offset
  undo:&:list:&:operation <- get *editor, undo:offset
  {
    # if previous operation was an insert, coalesce this operation with it
    break-unless undo
    op:&:operation <- first undo
    typing:insert-operation, is-insert?:bool <- maybe-convert *op, typing:variant
    break-unless is-insert?
    previous-coalesce-tag:num <- get typing, tag:offset
    break-unless previous-coalesce-tag
    before-cursor:&:duplex-list:char <- get *editor, before-cursor:offset
    insert-until:&:duplex-list:char <- next before-cursor
    typing <- put typing, insert-until:offset, insert-until
    typing <- put typing, after-row:offset, cursor-row
    typing <- put typing, after-column:offset, cursor-column
    typing <- put typing, after-top-of-screen:offset, top-after
    *op <- merge 0/insert-operation, typing
    break +done-adding-insert-operation
  }
  # if not, create a new