# some common helpers shared by phases of the SubX converter == 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 # 'table' is a stream of (key, value) rows # keys are always strings # values may be any type but have size 'n' # 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 # if there are no rows free, return null # TODO: pass in an allocation descriptor get-or-insert: # table : (address stream {string, _}), s : (address slice), n : int -> EAX : (address _) # pseudocode: # curr = table->data # max = &table->data[table->write] # while curr < max # if slice-equal?(s, *curr) # return curr+4 # curr += n # if table->write >= table->length # abort # *max = slice-to-string(Heap, s) # table->write += n # 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 (slice-equal?(s, *curr)) return *(curr+4) # . EAX = slice-equal?(s, *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 slice-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 += n 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 = slice-to-string(Heap, s) # . EAX = slice-to-string(Heap, s) # . . push args ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) 68/push Heap/imm32 # . . call e8/call slice-to-string/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # . *max = EAX 89/copy 0/mod/indirect 2/rm32/EDX . . . 0/r32/EAX . . # copy EAX to *EDX # table->write += n # . EAX = n 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 # EDX : (address slice) = "code" 68/push _test-code-segment-end/imm32/end 68/push _test-code-segment/imm32/start 89/copy 3/mod/direct 2/rm32/EDX . . . 4/r32/ESP . . # copy ESP to EDX $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" slice, 8 bytes per row) # . . push args 68/push 8/imm32/row-size 52/push-EDX 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" slice, 8 bytes per row) # . . push args 68/push 8/imm32/row-size 52/push-EDX 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 # EDX : (address slice) = "data" c7 0/subop/copy 0/mod/indirect 2/rm32/EDX . . . . . _test-data-segment/imm32 # copy to *EDX c7 0/subop/copy 1/mod/*+disp8 2/rm32/EDX . . . . 4/disp8 _test-data-segment-end/imm32 # copy to *(EDX+4) # EAX = get-or-insert(table, "data" slice, 8 bytes per row) # . . push args 68/push 8/imm32/row-size 52/push-EDX 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 # write an entire stream's contents to a buffered-file # ways to do this: # - construct a 'maximal slice' and pass it to write-slice-buffered # - flush the buffered-file and pass the stream directly to its fd (disabling buffering) # we'll go with the first way for now write-stream-data: # f : (address buffered-file), s : (address stream) -> # . 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 56/push-ESI # ESI = s 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 0xc/disp8 . # copy *(EBP+12) to ESI # var slice/ECX = {s->data, s->data + s->write} # . push s->data + s->write 8b/copy 0/mod/indirect 6/rm32/ESI . . . 0/r32/EAX . . # copy *ESI to EAX 8d/copy-address 1/mod/*+disp8 4/rm32/sib 6/base/ESI 0/index/EAX . 0/r32/EAX 0xc/disp8 . # copy ESI+EAX+12 to EAX 50/push-EAX # . push s->data 8d/copy-address 1/mod/*+disp8 6/rm32/ESI . . . 0/r32/EAX 0xc/disp8 . # copy ESI+12 to EAX 50/push-EAX # . ECX = ESP 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX # write-slice-buffered(f, slice) # . . push args 51/push-ECX ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) # . . call e8/call write-slice-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP $write-stream-data:end: # . restore locals 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # . restore registers 5e/pop-to-ESI 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 c3/return test-write-stream-data: # . prolog 55/push-EBP 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP # setup # . clear-stream(_test-output-stream) # . . push args 68/push _test-output-stream/imm32 # . . call e8/call clear-stream/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP # . clear-stream(_test-output-buffered-file+4) # . . push args b8/copy-to-EAX _test-output-buffered-file/imm32 05/add-to-EAX 4/imm32 50/push-EAX # . . call e8/call clear-stream/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP # . clear-stream(_test-input-stream) # . . push args 68/push _test-input-stream/imm32 # . . call e8/call clear-stream/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP # initialize input # . write(_test-input-stream, "abcd") # . . push args 68/push "abcd"/imm32 68/push _test-input-stream/imm32 # . . call e8/call write/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # write-stream-data(_test-output-buffered-file, _test-input-stream) # . . push args 68/push _test-input-stream/imm32 68/push _test-output-buffered-file/imm32 # . . call e8/call write-stream-data/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # check that the write happened as expected # . flush(_test-output-buffered-file) # . . push args 68/push _test-output-buffered-file/imm32 # . . call e8/call flush/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP # . check-stream-equal(_test-output-stream, "abcd", msg) # . . push args 68/push "F - test-write-stream-data"/imm32 68/push "abcd"/imm32 68/push _test-output-stream/imm32 # . . call e8/call check-stream-equal/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP # . epilog 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP 5d/pop-to-EBP c3/return == data _test-input-stream: # current write index 0/imm32 # current read index 0/imm32 # length 0x100/imm32 # 256 bytes # data (16 lines x 16 bytes/line) 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 # a test buffered file for _test-input-stream _test-input-buffered-file: # file descriptor or (address stream) _test-input-stream/imm32 # current write index 0/imm32 # current read index 0/imm32 # length 6/imm32 # data 00 00 00 00 00 00 # 6 bytes _test-output-stream: # current write index 0/imm32 # current read index 0/imm32 # length 0x100/imm32 # 256 bytes # data (16 lines x 16 bytes/line) 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 # a test buffered file for _test-output-stream _test-output-buffered-file: # file descriptor or (address stream) _test-output-stream/imm32 # current write index 0/imm32 # current read index 0/imm32 # length 6/imm32 # data 00 00 00 00 00 00 # 6 bytes _test-code-segment: 63/c 6f/o 64/d 65/e _test-code-segment-end: _test-data-segment: 64/d 61/a 74/t 61/a _test-data-segment-end: # . . vim:nowrap:textwidth=0