1
0
mirror of https://github.com/hsoft/collapseos.git synced 2024-11-27 15:28:06 +11:00

lib/expr: fix stack imbalance on failure

This commit is contained in:
Virgil Dupras 2019-12-29 16:15:48 -05:00
parent 4760d044c0
commit 981c93bfd4
3 changed files with 35 additions and 1 deletions

View File

@ -45,10 +45,11 @@ _parseApply:
; Here we do some stack kung fu. We have, in HL, a string pointer we ; Here we do some stack kung fu. We have, in HL, a string pointer we
; want to keep. We have, in (SP), our left result we want to use. ; want to keep. We have, in (SP), our left result we want to use.
ex (sp), hl ; <-> lvl 1 ex (sp), hl ; <-> lvl 1
ret nz jr nz, .end
push af ; --> lvl 2, save ending operator push af ; --> lvl 2, save ending operator
call callIX call callIX
pop af ; <-- lvl 2, restore operator. pop af ; <-- lvl 2, restore operator.
.end:
pop hl ; <-- lvl 1, restore str pointer pop hl ; <-- lvl 1, restore str pointer
ret ret

View File

@ -6,6 +6,12 @@
; lib/fmt ; lib/fmt
testNum: .db 1 testNum: .db 1
; Each time we call assertSP, we verify that our stack isn't imbalanced by
; comparing SP to its saved value. Whenever your "base" SP value change,
; generally at the beginning of a test routine, run "ld (testSP), sp" to have
; proper value saved to heap.
testSP: .dw 0xffff
STDIO_PUTC: STDIO_PUTC:
out (0), a out (0), a
@ -69,6 +75,20 @@ testList:
pop hl ; <-- lvl 1 pop hl ; <-- lvl 1
ret ret
; test that SP == testSP
assertSP:
ld hl, (testSP)
; offset the fact that we call assertSP
dec hl \ dec hl
or a ; reset carry
sbc hl, sp
ret z
ld hl, .msg
call printstr
jr fail
.msg:
.db "Wrong SP", CR, LF, 0
nexttest: nexttest:
ld a, (testNum) ld a, (testNum)
inc a inc a

View File

@ -53,6 +53,7 @@ test:
jp nz, fail jp nz, fail
call testParseExpr call testParseExpr
call testSPOnFail
; success ; success
xor a xor a
@ -130,3 +131,15 @@ testParseExpr:
.alltests: .alltests:
.dw .t1, .t2, .t3, .t4, .t5, .t6, .t7, .t8, .t9, .t10, .t11, .t12 .dw .t1, .t2, .t3, .t4, .t5, .t6, .t7, .t8, .t9, .t10, .t11, .t12
.dw .t13, .t14, .t15, 0 .dw .t13, .t14, .t15, 0
; Ensure that stack is balanced on failure
testSPOnFail:
ld (testSP), sp
ld hl, .sFail
call parseExpr
call assertNZ
call assertSP
jp nexttest
.sFail: .db "1+abc123", 0