Skip to content

Commit 3ec95e9

Browse files
Save several bytes in parser
1 parent 787810d commit 3ec95e9

1 file changed

Lines changed: 21 additions & 19 deletions

File tree

parser.s

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ next_pvm:
122122
ldy #0
123123
lda (pvm_program_ptr),y ; Load PVM opcode
124124
sta B ; Park opcode in B
125-
iny
126-
jsr rebase_pvm_program_ptr ; Skip past the opcode byte
125+
jsr iny_rebase_pvm_program_ptr ; Skip past the opcode byte
127126
lda B ; Recover opcode from B
128127

129128
; Handle the opcode
@@ -238,8 +237,7 @@ op_match_range:
238237
iny
239238
bne @find_zero
240239
@done:
241-
iny
242-
jsr rebase_pvm_program_ptr ; Update pvm_program_ptr
240+
jsr iny_rebase_pvm_program_ptr ; Update pvm_program_ptr
243241

244242
; Fall through
245243

@@ -286,7 +284,7 @@ op_emit:
286284
ldy #0
287285
lda (pvm_program_ptr),y ; Get argument
288286
jsr write_to_line_buffer
289-
jmp rebase_next_pvm
287+
bne rebase_next_pvm
290288

291289
; COMPOSE: OR the next byte value into the last byte written to the output
292290

@@ -298,7 +296,10 @@ rebase_next_pvm:
298296
ldy #1
299297
jsr rebase_pvm_program_ptr ; Advance past byte
300298
jmp next_pvm
301-
299+
300+
; OR the value in A with the last byte written to line_buffer
301+
; Y SAFE, BC SAFE, DE SAFE
302+
302303
compose_with_last_byte:
303304
ldx line_pos ; Current line_pos
304305
ora line_buffer-1,x ; Subtract one since we want last character
@@ -326,7 +327,7 @@ write_to_line_buffer:
326327
cpy #MAX_LINE_LENGTH
327328
raieq ERR_LINE_TOO_LONG
328329
sta line_buffer,y
329-
inc line_pos
330+
inc line_pos ; Will always exit with Z clear
330331
rts
331332

332333
; Calculates the address of TRY or ACCEPT using the 6-bit offset embedded in the opcode relative to
@@ -353,32 +354,33 @@ calculate_address_12:
353354
@positive:
354355
tax ; Save high byte
355356
lda (pvm_program_ptr),y ; Read the low byte
356-
iny
357+
jsr iny_rebase_pvm_program_ptr
358+
359+
; Fall through
360+
357361
add_to_pvm_program_ptr:
358362
clc
359363
adc pvm_program_ptr ; Add to pvm_program_ptr
360364
pha
361365
txa
362366
adc pvm_program_ptr+1
363367
tax
364-
rebase_pop:
365-
jsr rebase_pvm_program_ptr ; Address low byte is on stack and high byte is safe in X
366368
pla
367369
rts
368370

369371
; Rebases pvm_program_ptr by adding Y.
370372
; Exits with Y=0.
371373
; X SAFE, BC SAFE, DE SAFE
372374

375+
iny_rebase_pvm_program_ptr:
376+
iny
373377
rebase_pvm_program_ptr:
374-
tya ; Move offset into A and add to pvm_program_ptr
375-
ldy #0 ; Reset Y
376-
clc ; Not sure if carry is set or not so clear it now
377-
adc pvm_program_ptr ; Add to pvm_program_ptr
378-
sta pvm_program_ptr
379-
bcc @done
378+
inc pvm_program_ptr
379+
bne @skip_inc
380380
inc pvm_program_ptr+1
381-
@done:
381+
@skip_inc:
382+
dey
383+
bne rebase_pvm_program_ptr
382384
rts
383385

384386
; PVM macros
@@ -456,8 +458,8 @@ rebase_pvm_program_ptr:
456458
.endmacro
457459

458460
.macro write_far_opcode opcode, address
459-
.assert (address - (* + 1)) >= -1024 .and (address - (* + 1)) <= 1023, error, "Address offset out of range"
460-
.byte opcode + >(address - (* + 1)) & $0F, <(address - *)
461+
.assert (address - (* + 2)) >= -1024 .and (address - (* + 2)) <= 1023, error, "Address offset out of range"
462+
.byte opcode + >(address - (* + 2)) & $0F, <(address - (* + 1))
461463
.endmacro
462464

463465
.macro TRY address

0 commit comments

Comments
 (0)