A little more resizing of buffers. apps/hex.subx is now building an
identical binary.

I'm now aborting on allocation failures. That requires disabling
a couple of tests. (I'm not quite confident enough of this decision to
delete them outright.) I want to treat all segfaults as bugs, and
machine code is no place to add boilerplate checks for return values of
standard library functions.
This commit is contained in:
Kartik Agaram 2019-07-23 23:51:06 -07:00
parent 43e6c3787d
commit f04fdc8e57
14 changed files with 24 additions and 12 deletions

View File

@ -22,7 +22,7 @@
# . 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
# Claim the next 'n' bytes of memory starting at ad->curr and update ad->curr.
# If there isn't enough memory before ad->limit, return 0 and leave 'ad' unmodified.
# Abort if there isn't enough memory in 'ad'.
allocate: # ad : (address allocation-descriptor), n : int -> address-or-null/EAX
# . prolog
55/push-EBP
@ -39,10 +39,7 @@ allocate: # ad : (address allocation-descriptor), n : int -> address-or-null/EA
89/copy 3/mod/direct 2/rm32/EDX . . . 0/r32/EAX . . # copy EAX to EDX
03/add 1/mod/*+disp8 5/rm32/EBP . . . 2/r32/EDX 0xc/disp8 . # add *(EBP+12) to EDX
3b/compare 1/mod/*+disp8 1/rm32/ECX . . . 2/r32/EDX 4/disp8 . # compare EDX with *(ECX+4)
72/jump-if-lesser-signed $allocate:commit/disp8
# return null if not
b8/copy-to-EAX 0/imm32
eb/jump $allocate:end/disp8
73/jump-if-greater-or-equal-signed $allocate:abort/disp8
$allocate:commit:
# update ad->curr
89/copy 0/mod/indirect 1/rm32/ECX . . . 2/r32/EDX . . # copy EDX to *ECX
@ -55,6 +52,21 @@ $allocate:end:
5d/pop-to-EBP
c3/return
$allocate:abort:
# . _write(2/stderr, error)
# . . push args
68/push "allocate: failed to allocate\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
b8/copy-to-EAX 1/imm32/exit
cd/syscall 0x80/imm8
# never gets here
test-allocate-success:
# . prolog
55/push-EBP
@ -94,7 +106,7 @@ test-allocate-success:
5d/pop-to-EBP
c3/return
test-allocate-failure:
_pending-test-allocate-failure:
# . prolog
55/push-EBP
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -143,7 +143,7 @@ test-new:
5d/pop-to-EBP
c3/return
test-new-failure:
_pending-test-new-failure:
# . prolog
55/push-EBP
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP

Binary file not shown.

Binary file not shown.

View File

@ -5,11 +5,11 @@
# maximum memory available for allocation
Heap-size:
0x100000/imm32/1MB
0x200000/imm32/2MB
# maximum size of a single segment
Segment-size:
0x40000/imm32/256KB
0x80000/imm32/512KB
# maximum size of input textual stream (spanning all segments)
Input-size:
@ -17,7 +17,7 @@ Input-size:
# maximum size of the 'labels' table in survey.subx
Max-labels:
0x4000/imm32/1K-labels/16KB
0x10000/imm32/4K-labels/64KB
== code
# instruction effective address register displacement immediate

Binary file not shown.

View File

@ -117,7 +117,7 @@ convert: # infile : (address buffered-file), out : (address buffered-file) -> <
# var in : (address stream byte) = stream(4096)
# slurp(infile, in)
# var segments = new-stream(10 rows, 16 bytes each)
# var labels = new-stream(512 rows, 16 bytes each)
# var labels = new-stream(Max-labels rows, 16 bytes each)
# compute-offsets(in, segments, labels)
# compute-addresses(segments, labels)
# rewind-stream(in)
@ -136,7 +136,7 @@ convert: # infile : (address buffered-file), out : (address buffered-file) -> <
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 labels/EDX = stream(1024 * 16)
# var labels/EDX = stream(Max-labels * 16)
# . data
2b/subtract 0/mod/indirect 5/rm32/.disp32 . . 4/r32/ESP Max-labels/disp32 # subtract *Max-labels from ESP
# . length

Binary file not shown.