diff options
author | Kartik Agaram <vc@akkartik.com> | 2019-06-28 18:20:21 -0700 |
---|---|---|
committer | Kartik Agaram <vc@akkartik.com> | 2019-06-28 18:20:21 -0700 |
commit | 8e9fde42108905e2f5d450c76df696d55cce5639 (patch) | |
tree | ffcd0859388fc471aba3f58d8804c4c286bf122d | |
parent | 397f7c4687a85e999efe07e803b16f900d411442 (diff) | |
download | mu-8e9fde42108905e2f5d450c76df696d55cce5639.tar.gz |
initial draft of solution for 'compute-addresses'
No trace statements yet, so we don't know if it works.
-rwxr-xr-x | subx/apps/assort | bin | 27376 -> 28146 bytes | |||
-rw-r--r-- | subx/apps/assort.subx | 4 | ||||
-rwxr-xr-x | subx/apps/pack | bin | 42290 -> 43060 bytes | |||
-rw-r--r-- | subx/apps/subx-common.subx | 265 | ||||
-rwxr-xr-x | subx/apps/survey | bin | 27609 -> 28481 bytes | |||
-rw-r--r-- | subx/apps/survey.subx | 77 |
6 files changed, 331 insertions, 15 deletions
diff --git a/subx/apps/assort b/subx/apps/assort index 188fc4b4..f35c0d85 100755 --- a/subx/apps/assort +++ b/subx/apps/assort Binary files differdiff --git a/subx/apps/assort.subx b/subx/apps/assort.subx index 2f9ca98e..55f0c9a8 100644 --- a/subx/apps/assort.subx +++ b/subx/apps/assort.subx @@ -667,9 +667,9 @@ $read-segments:check-for-segment-header: #? # . . discard args #? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP #? # }}} - # segment-slot/EAX = get-or-insert-slice(table, segment-name, value-size=8) + # segment-slot/EAX = get-or-insert-slice(table, segment-name, row-size=8) # . . push args - 68/push 8/imm32/value-size + 68/push 8/imm32/row-size 52/push-EDX ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) # . . call diff --git a/subx/apps/pack b/subx/apps/pack index d90d266a..e50a6843 100755 --- a/subx/apps/pack +++ b/subx/apps/pack Binary files differdiff --git a/subx/apps/subx-common.subx b/subx/apps/subx-common.subx index 4555d75e..ca0fa943 100644 --- a/subx/apps/subx-common.subx +++ b/subx/apps/subx-common.subx @@ -8,21 +8,268 @@ # 'table' is a stream of (key, value) rows # keys are always strings (addresses; size 4 bytes) # values may be any type, but rows (key+value) always occupy 'row-size' bytes -# scan 'table' for a row with a key 's' and return the address of the corresponding value -# if no row is found, save 's' in the next available row +# scan 'table' for a row with a key 'key' and return the address of the corresponding value +# 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 +# Beware: assume keys are immutable; they're inserted by reference # TODO: pass in an allocation descriptor -get-or-insert-slice: # table : (address stream {string, _}), s : (address slice), row-size : int -> EAX : (address _) +get-or-insert: # table : (address stream {string, _}), key : (address string), row-size : int -> EAX : (address _) # pseudocode: # curr = table->data # max = &table->data[table->write] # while curr < max - # if slice-equal?(s, *curr) + # if string-equal?(key, *curr) # return curr+4 # curr += row-size # if table->write >= table->length # abort - # *max = slice-to-string(Heap, s) + # *max = key + # table->write += row-size + # return max+4 + # + # . prolog + 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 + # curr/ECX = table->data + 8d/copy-address 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 0xc/disp8 . # copy ESI+12 to ECX + # max/EDX = 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 + 7d/jump-if-greater-or-equal $get-or-insert:not-found/disp8 + # if (string-equal?(key, *curr)) return *(curr+4) + # . EAX = string-equal?(key, *curr) + # . . push args + ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX + 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 != 0) return EAX = curr+4 + 3d/compare-EAX-and 0/imm32 + 74/jump-if-equal $get-or-insert:mismatch/disp8 + 8d/copy-address 1/mod/*+disp8 1/rm32/ECX . . . 0/r32/EAX 4/disp8 . # copy ECX+4 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: + # result/EAX = 0 + 31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . . # clear EAX + # if (table->write >= table->length) 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-greater-or-equal $get-or-insert:abort/disp8 + # *max = key + # . EAX = key + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 0/r32/EAX 0xc/disp8 . # copy *(EBP+12) to EAX + # . *max = EAX + 89/copy 0/mod/indirect 2/rm32/EDX . . . 0/r32/EAX . . # copy EAX to *EDX + # 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+4 + # . EAX = max + 89/copy 3/mod/direct 0/rm32/EAX . . . 2/r32/EDX . . # copy EDX to EAX + # . EAX += 4 + 05/add-to-EAX 4/imm32 +$get-or-insert:end: + # . restore registers + 5e/pop-to-ESI + 5a/pop-to-EDX + 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 + +$get-or-insert:abort: + # . _write(2/stderr, error) + # . . push args + 68/push "get-or-insert: too many segments"/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 + b8/copy-to-EAX 1/imm32/exit + cd/syscall 0x80/imm8 + # never gets here + +test-get-or-insert: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # var table/ECX : (address stream {string, number}) = stream(2 rows * 8 bytes) + 81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # subtract from ESP + 68/push 0x10/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 +$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", 8 bytes per row) + # . . push args + 68/push 8/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 . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EAX - table->data, 4, msg) # first row's value slot returned + # . check-ints-equal(EAX - table, 16, msg) + # . . push args + 68/push "F - test-get-or-insert/0"/imm32 + 68/push 0x10/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-or-insert:check2: + # check-ints-equal(table->write, row-size = 8, msg) + # . . push args + 68/push "F - test-get-or-insert/1"/imm32 + 68/push 8/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 + # check-string-equal(*table->data, "code", msg) + # . . push args + 68/push "F - test-get-or-insert/2"/imm32 + 68/push "code"/imm32 + ff 6/subop/push 1/mod/*+disp8 1/rm32/ECX . . . . 0xc/disp8 . # push *(ECX+12) + # . . call + e8/call check-string-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", 8 bytes per row) + # . . push args + 68/push 8/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 . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EAX - table->data, 4, msg) + # . check-ints-equal(EAX - table, 16, msg) + # . . push args + 68/push "F - test-get-or-insert/3"/imm32 + 68/push 0x10/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 = 8, msg) + # . . push args + 68/push "F - test-get-or-insert/4"/imm32 + 68/push 8/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 + # check-string-equal(*table->data, "code", msg) + # . . push args + 68/push "F - test-get-or-insert/5"/imm32 + 68/push "code"/imm32 + ff 6/subop/push 1/mod/*+disp8 1/rm32/ECX . . . . 0xc/disp8 . # push *(ECX+12) + # . . call + e8/call check-string-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", 8 bytes per row) + # . . push args + 68/push 8/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 . . . . . 0xc/imm32 # add to ESP + # table gets a new row + # check-ints-equal(EAX - table->data, 12, msg) # second row's value slot returned + # . check-ints-equal(EAX - table, 24, msg) + # . . push args + 68/push "F - test-get-or-insert/6"/imm32 + 68/push 0x18/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 = 16, msg) + # . . push args + 68/push "F - test-get-or-insert/7"/imm32 + 68/push 0x10/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 + # check-string-equal(*table->data+8, "data", msg) + # check-string-equal(*(table+20), "data", msg) + # . . push args + 68/push "F - test-get-or-insert/8"/imm32 + 68/push "data"/imm32 + ff 6/subop/push 1/mod/*+disp8 1/rm32/ECX . . . . 0x14/disp8 . # push *(ECX+20) + # . . call + e8/call check-string-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-get-or-insert:end: + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +# 'table' is a stream of (key, value) rows +# keys are always strings (addresses; size 4 bytes) +# values may be any type, but rows (key+value) always occupy 'row-size' bytes +# scan 'table' for a row with a key 'key' and return the address of the corresponding value +# if no row is found, save 'key' in the next available row +# if there are no rows free, abort +# TODO: pass in an allocation descriptor +get-or-insert-slice: # table : (address stream {string, _}), key : (address slice), row-size : int -> EAX : (address _) + # pseudocode: + # curr = table->data + # max = &table->data[table->write] + # while curr < max + # if slice-equal?(key, *curr) + # return curr+4 + # curr += row-size + # if table->write >= table->length + # abort + # *max = slice-to-string(Heap, key) # table->write += row-size # return max+4 # @@ -44,8 +291,8 @@ $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 7d/jump-if-greater-or-equal $get-or-insert-slice:not-found/disp8 - # if (slice-equal?(s, *curr)) return *(curr+4) - # . EAX = slice-equal?(s, *curr) + # if (slice-equal?(key, *curr)) return *(curr+4) + # . EAX = slice-equal?(key, *curr) # . . push args ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) @@ -70,8 +317,8 @@ $get-or-insert-slice:not-found: 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-greater-or-equal $get-or-insert-slice:abort/disp8 - # *max = slice-to-string(Heap, s) - # . EAX = slice-to-string(Heap, s) + # *max = slice-to-string(Heap, key) + # . EAX = slice-to-string(Heap, key) # . . push args ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) 68/push Heap/imm32 diff --git a/subx/apps/survey b/subx/apps/survey index dedf6a9b..e1d3185d 100755 --- a/subx/apps/survey +++ b/subx/apps/survey Binary files differdiff --git a/subx/apps/survey.subx b/subx/apps/survey.subx index a73bb661..579dfbad 100644 --- a/subx/apps/survey.subx +++ b/subx/apps/survey.subx @@ -545,22 +545,91 @@ compute-addresses: # segments : (address stream {string, segment-info}), labels # if (s >= max) break # s->address &= 0xfffff000 # clear last 12 bits for p_align # s->address += (s->file-offset & 0x00000fff) - # s += 16 # size of segment-info + # s += 16 # size of row # l : (address label-info) = labels->data + 4 # skip key # max = labels->data + labels->write # while true # if (l >= max) break # seg-name : (address string) = l->segment-name - # label-seg : (address segment-info) = get(labels, seg-name) + # label-seg : (address segment-info) = get-or-insert(segments, seg-name) # l->address = label-seg->address + l->segment-offset + # l += 16 # size of row # # . prolog 55/push-EBP 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP # . save registers + 50/push-EAX + 51/push-ECX + 52/push-EDX + 53/push-EBX + 56/push-ESI + # ESI = segments + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI + # s/EAX = segments->data + 4 + 8d/copy-address 1/mod/*+disp8 6/rm32/ESI . . . 0/r32/EAX 4/disp8 . # copy ESI+16 to EAX + # max/ECX = segments->data + segments->write + 8b/copy 0/mod/indirect 6/rm32/ESI . . . 1/r32/ECX . . # copy *ESI to ECX + 01/add 3/mod/direct 1/rm32/ECX . . . 6/r32/ESI . . # add ESI to ECX +$compute-addresses:segment-loop: + # if (s >= max) break + 39/compare 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # compare EAX with ECX + 73/jump-if-greater-or-equal-unsigned $compute-addresses:segment-break/disp8 + # clear last 12 bits of s->address for p_align=0x1000 + # . EDX = s->address + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 2/r32/EDX . . # copy *EAX to EDX + # . EDX &= 0xfffff000 + 81 4/subop/and 3/mod/direct 2/rm32/EDX . . . . . 0xfffff000/imm32 # bitwise and of EDX + # update last 12 bits from s->file-offset + # . EBX = s->file-offset + 8b/copy 1/mod/*+disp8 0/rm32/EAX . . . 3/r32/EBX 4/disp8 . # copy *(EAX+4) to EBX + # . EBX &= 0xfff + 81 4/subop/and 3/mod/direct 3/rm32/EBX . . . . . 0x00000fff/imm32 # bitwise and of EBX + # . s->address = EDX | EBX + 09/or 3/mod/direct 2/rm32/EDX . . . 3/r32/EBX . . # EDX = bitwise OR with EBX + 89/copy 0/mod/indirect 0/rm32/EAX . . . 2/r32/EDX . . # copy EDX to *EAX + # s += 16 # size of row + 05/add-to-EAX 0x10/imm32 + eb/jump $compute-addresses:segment-loop/disp8 +$compute-addresses:segment-break: + # ESI = labels + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 0xc/disp8 . # copy *(EBP+12) to ESI + # l/EAX = labels->data + 4 + 8d/copy-address 1/mod/*+disp8 6/rm32/ESI . . . 0/r32/EAX 4/disp8 . # copy ESI+16 to EAX + # max/ECX = labels->data + labels->write + 8b/copy 0/mod/indirect 6/rm32/ESI . . . 1/r32/ECX . . # copy *ESI to ECX + 01/add 3/mod/direct 1/rm32/ECX . . . 6/r32/ESI . . # add ESI to ECX +$compute-addresses:label-loop: + # if (l >= max) break + 39/compare 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # compare EAX with ECX + 73/jump-if-greater-or-equal-unsigned $compute-addresses:end/disp8 + # seg-name/EDX = l->segment-name + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 2/r32/EDX . . # copy *EAX to EDX + # label-seg/EDX : (address label-info) = get-or-insert(labels, seg-name, row-size=16) + # . . push args + 68/push 0x10/imm32/row-size + 52/push-EDX + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + # . . call + e8/call get-or-insert/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # EBX = label-seg->address + 8b/copy 0/mod/indirect 2/rm32/EDX . . . 3/r32/EBX . . # copy *EDX to EBX + # EBX += l->segment-offset + 03/add 1/mod/*+disp8 5/rm32/EBP . . . 3/r32/EBX 4/disp8 . # add *(EAX+4) to EBX + # l->address = EBX + 89/copy 0/mod/indirect 0/rm32/EAX . . . 3/r32/EBX . . # copy EBX to *EAX + # l += 16 # size of row + 05/add-to-EAX 0x10/imm32 + eb/jump $compute-addresses:label-loop/disp8 $compute-addresses:end: - # . reclaim locals # . restore registers + 5e/pop-to-ESI + 5b/pop-to-EBX + 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 5d/pop-to-EBP @@ -696,7 +765,7 @@ stream-add4: # in : (address stream byte), key : address, val1 : address, val2 52/push-EDX 56/push-ESI # ESI = in - 8b/copy 1/mod/*+disp8 5/rm32/EBP . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI # curr/EAX = in->data + in->write # . EAX = in->write 8b/copy 0/mod/indirect 6/rm32/ESI . . . 0/r32/EAX . . # copy *ESI to EAX |