4906 - helper to allocate streams on the heap

This commit is contained in:
Kartik Agaram 2019-01-04 15:37:16 -08:00
parent 7b548ff5c5
commit bc20cc3d45
2 changed files with 175 additions and 0 deletions

View File

@ -149,4 +149,53 @@ test-allocate-failure:
5d/pop-to-EBP
c3/return
# helper: create a nested allocation descriptor (useful for tests)
allocate-region: # ad : (address allocation-descriptor), n : int -> new-ad : (address allocation-descriptor)
# . prolog
55/push-EBP
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
# . save registers
51/push-ECX
# EAX = allocate(ad, n)
# . . 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 allocate/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# if EAX == 0 abort
81 7/subop/compare 3/mod/direct 0/rm32/EAX . . . . . 0/imm32 # compare EAX
74/jump-if-equal $allocate-region:abort/disp8
# earmark 8 bytes at the start for a new allocation descriptor
# . *EAX = EAX + 8
89/copy 3/mod/direct 1/rm32/ECX . . . 0/r32/EAX . . # copy EAX to ECX
81 0/subop/add 3/mod/direct 1/rm32/ECX . . . . . 8/imm32 # add to ECX
89/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy ECX to *EAX
# . *(EAX+4) = EAX + n
89/copy 3/mod/direct 1/rm32/ECX . . . 0/r32/EAX . . # copy EAX to ECX
03/add 1/mod/*+disp8 5/rm32/EBP . . . 1/r32/ECX 0xc/disp8 . # add *(EBP+12) to ECX
89/copy 1/mod/*+disp8 0/rm32/EAX . . . 1/r32/ECX 4/disp8 . # copy ECX to *(EAX+4)
# . restore registers
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
$allocate-region:abort:
# . _write(2/stderr, error)
# . . push args
68/push "allocate-region: failed to allocate"/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
# . . vim:nowrap:textwidth=0

126
subx/067new-stream.subx Normal file
View File

@ -0,0 +1,126 @@
# Helper to allocate a stream on the heap.
#
# We'll now start gingerly supporting streams containing arbitrary types.
== 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
# main:
e8/call run-tests/disp32 # 'run-tests' is a function created automatically by SubX. It calls all functions that start with 'test-'.
# syscall(exit, Num-test-failures)
8b/copy 0/mod/indirect 5/rm32/.disp32 . . 3/r32/EBX Num-test-failures/disp32 # copy *Num-test-failures to EBX
b8/copy-to-EAX 1/imm32/exit
cd/syscall 0x80/imm8
new-stream: # ad : (address allocation-descriptor), length : int, elemsize : int -> address/EAX
# . prolog
55/push-EBP
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
# . save registers
52/push-EDX
# n = elemsize * length + 12 (for read, write and length)
# . EAX = elemsize
8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 0/r32/EAX 0x10/disp8 . # copy *(EBP+16) to EAX
# . EAX *= length
31/xor 3/mod/direct 2/rm32/EDX . . . 2/r32/EDX . . # clear EDX
f7 4/subop/multiply 1/mod/*+disp8 5/rm32/EBP . . 0xc/disp8 . # multiply *(EBP+12) into EAX
# . if overflow abort
81 7/subop/compare 3/mod/direct 2/rm32/EDX . . . . . 0/imm32 # compare EDX
75/jump-if-not-equal $new-stream:abort/disp8
# . EDX = elemsize*length
89/copy 3/mod/direct 2/rm32/EDX . . . 0/r32/EAX . . # copy EAX to EDX
# . EAX += 12
05/add-to-EAX 0xc/imm32
# allocate(ad, n)
# . . push args
50/push-EAX
ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8)
# . . call
e8/call allocate/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# EAX->length = elemsize*length
89/copy 1/mod/*+disp8 0/rm32/EAX . . . 2/r32/EDX 8/disp8 . # copy EDX to *(EAX+8)
# clear-stream(EAX)
# . . push args
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
$new-stream:end:
# . restore registers
5a/pop-to-EDX
# . epilog
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
5d/pop-to-EBP
c3/return
$new-stream:abort:
# . _write(2/stderr, error)
# . . push args
68/push "new-stream: size too large"/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-new-stream:
# . prolog
55/push-EBP
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
# var ad/ECX : (address allocation-descriptor) = allocate-region(512)
# . EAX = allocate-region(512)
# . . push args
68/push 0x200/imm32
68/push Heap/imm32
# . . call
e8/call allocate-region/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# . ECX = EAX
89/copy 3/mod/direct 1/rm32/ECX . . . 0/r32/EAX . . # copy EAX to ECX
# var start/EDX = ad->curr
8b/copy 0/mod/indirect 1/rm32/ECX . . . 2/r32/EDX . . # copy *ECX to EDX
# EAX = new-stream(ad, 3, 2)
# . . push args
68/push 2/imm32
68/push 3/imm32
51/push-ECX
# . . call
e8/call new-stream/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
# check-ints-equal(EAX, EDX, msg)
# . . push args
68/push "F - test-new-stream: returns current pointer of allocation descriptor"/imm32
52/push-EDX
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(EAX->length, 6, msg)
# . . push args
68/push "F - test-new-stream: sets length correctly"/imm32
68/push 6/imm32
ff 6/subop/push 1/mod/*+disp8 0/rm32/EAX . . . . . 8/disp8 # push *(EAX+8)
# . . call
e8/call check-ints-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
# the rest is delegated to clear-stream() so we won't bother checking it
# . epilog
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
5d/pop-to-EBP
c3/return
# . . vim:nowrap:textwidth=0