zasm: getUpcode -> spitUpcode

Giving I/O responsibility to spitUpcode gives us wiggle room for
upcoming refactorings.
This commit is contained in:
Virgil Dupras 2019-11-10 09:28:10 -05:00
parent c4658591bd
commit 553b346b92

View File

@ -337,7 +337,7 @@ findInGroup:
; If it's not this, then we check if it's a numerical arg. ; If it's not this, then we check if it's a numerical arg.
; If A is a group ID, we do something else: we check that (HL) exists in the ; If A is a group ID, we do something else: we check that (HL) exists in the
; groupspec (argGrpTbl). Moreover, we go and write the group's "value" (index) ; groupspec (argGrpTbl). Moreover, we go and write the group's "value" (index)
; in (HL+1). This will save us significant processing later in getUpcode. ; in (HL+1). This will save us significant processing later in spitUpcode.
; Set Z according to whether we match or not. ; Set Z according to whether we match or not.
matchArg: matchArg:
cp (hl) cp (hl)
@ -407,8 +407,8 @@ matchPrimaryRow:
; *** Special opcodes *** ; *** Special opcodes ***
; The special upcode handling routines below all have the same signature. ; The special upcode handling routines below all have the same signature.
; Instruction row is at IX and we're expected to perform the same task as ; Instruction row is at IX and we're expected to perform the same task as
; getUpcode. The number of bytes, however, must go in C instead of A ; spitUpcode. The number of bytes, however, must go in C instead of A
; No need to preserve HL, DE, BC and IX: it's handled by getUpcode already. ; No need to preserve HL, DE, BC and IX: it's handled by spitUpcode already.
; Handle like a regular "JP (IX+d)" except that we refuse any displacement: if ; Handle like a regular "JP (IX+d)" except that we refuse any displacement: if
; a displacement is specified, we error out. ; a displacement is specified, we error out.
@ -625,11 +625,9 @@ handleLDrr:
ret ret
; Compute the upcode for argspec row at (DE) and arguments in curArg{1,2} and ; Compute the upcode for argspec row at (DE) and arguments in curArg{1,2} and
; writes the resulting upcode in INS_UPCODE. A is the number if bytes written ; writes the resulting upcode to IO.
; to INS_UPCODE. ; A is zero, with Z set, on success. A is non-zero, with Z unset, on error.
; A is zero on error. The only thing that can go wrong in this routine is spitUpcode:
; overflow.
getUpcode:
push ix push ix
push de push de
push hl push hl
@ -645,7 +643,7 @@ getUpcode:
ld h, (ix+5) ld h, (ix+5)
call callHL call callHL
; We have our result written in INS_UPCODE and C is set. ; We have our result written in INS_UPCODE and C is set.
jp .end jp .writeIO
.normalInstr: .normalInstr:
; we begin by writing our "base upcode", which can be one or two bytes ; we begin by writing our "base upcode", which can be one or two bytes
@ -734,7 +732,7 @@ getUpcode:
call checknmxy call checknmxy
jr z, .withByte jr z, .withByte
; nope, no number, alright, we're finished here ; nope, no number, alright, we're finished here
jr .end jr .writeIO
.withByte: .withByte:
inc hl inc hl
; HL points to our number (LSB), with (HL+1) being our MSB which should ; HL points to our number (LSB), with (HL+1) being our MSB which should
@ -751,7 +749,7 @@ getUpcode:
; verification falsely fail. ; verification falsely fail.
inc c ; one extra byte is written inc c ; one extra byte is written
call zasmIsFirstPass call zasmIsFirstPass
jr z, .end jr z, .writeIO
; We're on second pass ; We're on second pass
push de ; Don't let go of this, that's our dest push de ; Don't let go of this, that's our dest
@ -781,7 +779,7 @@ getUpcode:
or a ; cp 0 or a ; cp 0
jr nz, .numberTruncated ; if A is anything but zero, we're out jr nz, .numberTruncated ; if A is anything but zero, we're out
; of bounds. ; of bounds.
jr .end jr .writeIO
.absoluteValue: .absoluteValue:
; verify that the MSB in argument is zero ; verify that the MSB in argument is zero
@ -794,7 +792,7 @@ getUpcode:
ldi ldi
pop bc pop bc
inc c inc c
jr .end jr .writeIO
.withWord: .withWord:
inc hl ; HL now points to LSB inc hl ; HL now points to LSB
@ -805,12 +803,28 @@ getUpcode:
pop bc pop bc
inc c ; two extra bytes are written inc c ; two extra bytes are written
inc c inc c
; to writeIO
.writeIO:
; Let's write INS_UPCODE to IO
ld b, c ; save output byte count
ld hl, INS_UPCODE
.loopWrite:
ld a, (hl)
call ioPutB
jr nz, .ioError
inc hl
djnz .loopWrite
; Z is set by INC HL
jr .end jr .end
.numberTruncated: .numberTruncated:
; problem: not zero, so value is truncated. error ; Z already unset
ld c, 0 ld a, ERR_OVFL
jr .end
.ioError:
; Z already unset
ld a, SHELL_ERR_IO_ERROR
; continue to .end
.end: .end:
ld a, c
pop bc pop bc
pop hl pop hl
pop de pop de
@ -881,26 +895,10 @@ parseInstruction:
.match: .match:
; We have our matching instruction row. We're getting pretty near our ; We have our matching instruction row. We're getting pretty near our
; goal here! ; goal here!
call getUpcode call spitUpcode
or a ; is zero? jr .end ; Z and A set properly, even on error
jr z, .overflow
ld b, a ; save output byte count
ld hl, INS_UPCODE
.loopWrite:
ld a, (hl)
call ioPutB
jr nz, .ioError
inc hl
djnz .loopWrite
cp a ; ensure Z
jr .end
.ioError:
ld a, SHELL_ERR_IO_ERROR
jr .error
.overflow:
ld a, ERR_OVFL
jr .error
.badfmt: .badfmt:
; Z already unset
ld a, ERR_BAD_FMT ld a, ERR_BAD_FMT
.error: .error:
; A is set to error already ; A is set to error already