Skip to content

Commit

Permalink
Check overflow errors in the parsing bytecode buffer.
Browse files Browse the repository at this point in the history
Don't simply emit bad code on lines with too long bytecode.
  • Loading branch information
dmsc committed Feb 25, 2018
1 parent 584bd2f commit 64ebdf3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
21 changes: 6 additions & 15 deletions src/actions.asm
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
.import alloc_laddr
.importzp prog_ptr, laddr_ptr, laddr_buf, var_ptr, label_ptr
; From parser.asm
.import parser_error, parser_skipws
.import parser_error, parser_skipws, parser_emit_byte
.importzp TOK_CSTRING
; From error.asm
.importzp ERR_LOOP
Expand Down Expand Up @@ -141,15 +141,10 @@ ok: rts
;
; Emits 16bit AX into codep
.proc emit_AX
ldy opos
new_y: sta (prog_ptr),y
txa
iny
sta (prog_ptr),y
iny
sty opos
clc
rts
jsr parser_emit_byte
txa
jmp parser_emit_byte
.endproc

; Parser external subs
Expand Down Expand Up @@ -286,9 +281,7 @@ nloop:
; Store
store: inx
inc bpos
ldy opos
sta (prog_ptr),y
inc opos
jsr parser_emit_byte
bne nloop
err: ; Restore opos and exit
lda tmp1
Expand Down Expand Up @@ -379,9 +372,7 @@ exit:
.proc emit_varn
; Store VARN
txa
ldy opos
sta (prog_ptr),y
inc opos
jsr parser_emit_byte
; Fall through
.endproc
; Advances variable name in source pointer
Expand Down
3 changes: 2 additions & 1 deletion src/errors.asm
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ error_msg_list:
.endrepeat
.byte .strat(msg, .strlen(msg)-1) ^ $80
.endmacro
def_error ERR_LOOP, "bad loop error"
def_error ERR_TOO_LONG, "too long"
def_error ERR_LOOP, "bad loop"
def_error ERR_PARSE, "parse error"
def_error ERR_NO_ELOOP, "no end loop/proc/if"
def_error ERR_LABEL, "undef label"
Expand Down
25 changes: 14 additions & 11 deletions src/parse.asm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
; Parser state machine interpreter
; --------------------------------

.export parser_start, parser_error, parser_skipws
.export parser_start, parser_error, parser_skipws, parser_emit_byte
; Parser state
.exportzp bptr, bpos, bmax, linenum, buf_ptr, end_ptr
.exportzp loop_sp
Expand All @@ -42,7 +42,7 @@
.importzp DEGFLAG, DEGFLAG_DEG, DEGFLAG_RAD
; From errors.asm
.import error_msg_list
.importzp ERR_PARSE, ERR_NO_ELOOP, ERR_LABEL
.importzp ERR_PARSE, ERR_NO_ELOOP, ERR_LABEL, ERR_TOO_LONG

.zeropage
buf_ptr:.res 2
Expand Down Expand Up @@ -96,6 +96,17 @@ SM_EMIT= SM_EMIT_1
; Now, the rest of the code
.code

emit_sub:
jsr parser_fetch
emit_const:
parser_emit_byte:
ldy opos
sta (prog_ptr),y
inc opos
bne rts1
lda #ERR_TOO_LONG
; Fall through

.proc parser_error
; Prints error message
tax
Expand All @@ -116,7 +127,7 @@ skip: bpl ploop
::restore_stack:
ldx #$ff
txs
rts
::rts1: rts
.endproc
saved_stack = restore_stack + 1

Expand Down Expand Up @@ -393,12 +404,4 @@ set_parse_error:
lda #ERR_PARSE
jmp parser_error

emit_sub:
jsr parser_fetch
emit_const:
ldy opos
sta (prog_ptr),y
inc opos
rts

; vi:syntax=asm_ca65

0 comments on commit 64ebdf3

Please sign in to comment.