Skip to content

Commit 45feccf

Browse files
committed
Bugfix: correctly compute labels for JR
Relative jumps (JR, DJNZ) were computer wrongly when being using in a line with several instructions. Fixed. The syntax is now extended, so a LABEL does not need to use colon, and can also be declared in between instructions. So: LOOP ... is valid, but also: ld a, 0 : LOOP : nop : jp LOOP
1 parent a11b196 commit 45feccf

6 files changed

Lines changed: 30 additions & 28 deletions

File tree

asmlex.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from api.config import OPTIONS
1717
from api.errmsg import syntax_error
1818

19-
_tokens = ('STRING', 'NEWLINE', 'LABEL', 'CO',
19+
_tokens = ('STRING', 'NEWLINE', 'CO',
2020
'ID', 'COMMA', 'PLUS', 'MINUS', 'LP', 'RP', 'LPP', 'RPP', 'MUL', 'DIV', 'POW', 'MOD',
2121
'UMINUS', 'APO', 'INTEGER', 'ADDR',
2222
'LSHIFT', 'RSHIFT', 'BAND', 'BOR', 'BXOR'
@@ -251,18 +251,8 @@ def t_INITIAL_preproc_INTEGER(self, t):
251251
return t
252252

253253
def t_INITIAL_ID(self, t):
254-
r'[._a-zA-Z][._a-zA-Z0-9]*([ \t]*[:])?' # Any identifier
254+
r'[._a-zA-Z][._a-zA-Z0-9]*' # Any identifier
255255
tmp = t.value # Saves original value
256-
if tmp[-1] == ':':
257-
c = self.find_column(t)
258-
tmp = t.value = t.value[:-1].strip() # remove the colon ':'
259-
260-
if not self.input_data[t.lexpos - c + 1: t.lexpos].strip() and tmp.lower() not in keywords:
261-
t.type = 'LABEL'
262-
t.value = tmp
263-
return t
264-
265-
t.lexer.lexpos -= 1
266256

267257
t.value = tmp.upper() # Convert it to uppercase, since our internal tables uses uppercase
268258
id_ = tmp.lower()

asmparse.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -601,15 +601,11 @@ def p_program_endline2(p):
601601
def p_program(p):
602602
""" program : line
603603
"""
604-
if p[1] is not None:
605-
[MEMORY.add_instruction(x) for x in p[1] if isinstance(x, Asm)]
606604

607605

608606
def p_program_line(p):
609607
""" program : program line
610608
"""
611-
if p[2] is not None:
612-
[MEMORY.add_instruction(x) for x in p[2] if isinstance(x, Asm)]
613609

614610

615611
def p_def_label(p):
@@ -621,37 +617,41 @@ def p_def_label(p):
621617
MEMORY.declare_label(p[1], p.lineno(1), p[3])
622618

623619

624-
def p_line_label_asm(p):
625-
""" line : LABEL asms NEWLINE
626-
"""
627-
p[0] = p[2]
628-
__DEBUG__("Declaring '%s%s' (value %04Xh) in %i" % (NAMESPACE, p[1], MEMORY.org, p.lineno(1)))
629-
MEMORY.declare_label(p[1], p.lineno(1))
630-
631-
632620
def p_line_asm(p):
633621
""" line : asms NEWLINE
622+
| asms CO NEWLINE
634623
"""
635-
p[0] = p[1]
636624

637625

638626
def p_asms_empty(p):
639627
""" asms :
640628
"""
641-
p[0] = []
629+
p[0] = MEMORY.org
642630

643631

644632
def p_asms_asm(p):
645633
""" asms : asm
646634
"""
647-
p[0] = [p[1]]
635+
p[0] = MEMORY.org
636+
asm = p[1]
637+
if isinstance(asm, Asm):
638+
MEMORY.add_instruction(asm)
648639

649640

650641
def p_asms_asms_asm(p):
651642
""" asms : asms CO asm
652643
"""
653-
p[1].append(p[3])
654644
p[0] = p[1]
645+
asm = p[3]
646+
if isinstance(asm, Asm):
647+
MEMORY.add_instruction(asm)
648+
649+
650+
def p_asm_label(p):
651+
""" asm : ID
652+
"""
653+
__DEBUG__("Declaring '%s%s' (value %04Xh) in %i" % (NAMESPACE, p[1], MEMORY.org, p.lineno(1)))
654+
MEMORY.declare_label(p[1], p.lineno(1))
655655

656656

657657
def p_asm_ld8(p):

tests/functional/asmcolon3.asm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
cp 3 : jr z,nextjump
3+
ld hl,data : jp overjump
4+
nextjump:
5+
ld hl,0
6+
overjump:
7+
ld a,(hl)
8+
9+
data:
10+

tests/functional/asmcolon3.bin

14 Bytes
Binary file not shown.

tests/functional/asmcolon4.asm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ld a, 0 : LOOP : nop : jp LOOP
2+

tests/functional/asmcolon4.bin

6 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)