From fe15bafeca284ded5e1757c23c1156288734a425 Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Sat, 20 Jul 2019 18:07:52 -0400 Subject: [PATCH] zasm: fix bug with registry selection During expression parsing, if a local label was parsed, it would select the local registry and keep that selection, making subsequent global labels register in the wrong place. --- apps/zasm/parse.asm | 14 ++++++-------- apps/zasm/symbol.asm | 26 +++++++++++++++++--------- tools/emul/zasm/kernel.bin | Bin 1577 -> 1642 bytes tools/emul/zasm/zasm.bin | Bin 4566 -> 4570 bytes tools/tests/unit/test_parse_z.asm | 3 +-- tools/tests/unit/test_symbol.asm | 3 +-- tools/tests/zasm/runtests.sh | 11 +++++++---- tools/tests/zasm/test7.asm.expected | Bin 4564 -> 4567 bytes tools/tests/zasm/test9.asm | 11 +++++++++++ tools/tests/zasm/test9.asm.expected | Bin 0 -> 6 bytes 10 files changed, 43 insertions(+), 25 deletions(-) create mode 100644 tools/tests/zasm/test9.asm create mode 100644 tools/tests/zasm/test9.asm.expected diff --git a/apps/zasm/parse.asm b/apps/zasm/parse.asm index fe6e4a7..41769c2 100644 --- a/apps/zasm/parse.asm +++ b/apps/zasm/parse.asm @@ -169,24 +169,22 @@ parseNumberOrSymbol: call parseLiteral ret z ; Not a number. Try PC - push de + push de ; --> lvl 1 ld de, .sDollar call strcmp - pop de + pop de ; <-- lvl 1 jr z, .returnPC ; Not PC either, try symbol - call symSelect - call symFind + push de ; --> lvl 1 + call symFindVal ; --> DE jr nz, .notfound - ; Found! let's fetch value - push de - call symGetVal ; value in DE. We need it in IX push de \ pop ix - pop de + pop de ; <-- lvl 1 cp a ; ensure Z ret .notfound: + pop de ; <-- lvl 1 ; If not found, check if we're in first pass. If we are, it doesn't ; matter that we didn't find our symbol. Return success anyhow. ; Otherwise return error. Z is already unset, so in fact, this is the diff --git a/apps/zasm/symbol.asm b/apps/zasm/symbol.asm index 69e483a..3c57dc4 100644 --- a/apps/zasm/symbol.asm +++ b/apps/zasm/symbol.asm @@ -238,12 +238,6 @@ symRegister: ld a, ERR_DUPSYM jp unsetZ ; return -; Select global or local registry according to label name in (HL) -symSelect: - call symIsLabelLocal - jp z, symSelectLocalRegistry - jp symSelectGlobalRegistry - ; Find name (HL) in (SYM_CTX_NAMES) and make (SYM_CTX_PTR) point to the ; corresponding entry in (SYM_CTX_VALUES). ; If we find something, Z is set, otherwise unset. @@ -277,7 +271,21 @@ symFind: pop ix ret -; Return value that (SYM_CTX_PTR) is pointing at in DE. -symGetVal: +; For a given symbol name in (HL), find it in the appropriate symbol register +; and return its value in DE. If (HL) is a local label, the local register is +; searched. Otherwise, the global one. It is assumed that this routine is +; always called when the global registry is selected. Therefore, we always +; reselect it afterwards. +symFindVal: + call symIsLabelLocal + jp nz, .notLocal + call symSelectLocalRegistry +.notLocal: + call symFind + jr nz, .end + ; Found! let's fetch value + ; Return value that (SYM_CTX_PTR) is pointing at in DE. ld de, (SYM_CTX_PTR) - jp intoDE + call intoDE +.end: + jp symSelectGlobalRegistry diff --git a/tools/emul/zasm/kernel.bin b/tools/emul/zasm/kernel.bin index 5972a29d1bc613dd12ad1c2e854a0c5a415f1561..673bef5e9de34883778a98a5fed1c984c94f433b 100644 GIT binary patch delta 863 zcmY*Y&1=*^6rW^fVk~X9s}@4Bni#3El&%!9^lO+^sM3qjf)_tPO2w0&bP?Jv{s&4f z9)#_s2T!Y~;$GS%EEr+LpnDNR$zh5LEfn1rDXhLpUUmNH!~rp$ zW3PXO1Qrcm(%>lyrnO*S3vzm}pa)xe(1+h#yQOJA{Z*}4UeJ@>FqW}{xY*a?B$Q3D zrHf}MN~|xLJ*R^`mMIgvv754SV%0Lfpm^DUc$=2op8fT@a!-$(Ii!|FPPfVb)Pkb? zDgCi~=(?3=xkKU`G=5;j_l|^~Q9%_nW)zKC|AX$9Phi*DgK`^kSpe?&oz%k?>jKO= zU{Qo)a`71%x;T#&xhO8n*_d*TKRtZg5f(nArP~67$fdW^i=TOxtaAQcUqEPJ6 z4cTmM-yG$GW1Jy@=x&sVb0nydh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wxkm_Jf95vECJqxgHf)VH)9Ls{sSM30+)kv6yOsK>x|EL)M0$(dvf z&Iw{QCK(I=fH5>RmWwkgZjlT5h!QiiQ*9${%aeXTU@a{aX%odb0+A8M80a5aqOH{I z;t9@t^F*Bat9)d-H_Mo2famgzdbyB6CRnQ2LE-ZP2hVp|)o&csZ=}ljrFc2VzSpkm znhsoPmnpP5P$Oz78lR4%nt%hA23Qj2`0W~F;O~cAVvkL%P{_*P4!K3AL3L6p(OA0- zSy<;?lw|;_DN7$ao@z)-P`2GHKA-A&0^3)EO_{iWEduqjyhagtXUeRe%EqxH4 zgYXZC7dQ<458^yvkVAWH$`_WVDYwWk0z4@A$Jz1S-~oUH7Qm~JPmG{hdFzSYY5Xtj zVG1h)#8}Mr^B4l>vIr7hvQr=VRJZ++JRVjw;GX6Eo&*`@2;Io*ch#PTeXX3NP&TwpfMDL zf+Kmj1kMPY7a$;-?0K9)+ta(-ax#f=oU`*5QnP>ZUIyXLp$xZrXTWvg zb?a7xuOVR^pFk0iNwf{59lftyv}NfpmBaP}tM0^`k`%hD+_b+4Eh})lPCt# zj#A2xwbI&N@tF9%__w%P>XY7(baIRJfsQepj&hwiq6|9w>4@^FGq|Bm<~BozA`ZxN zXpHWo7SEB5k9bD}f+u|I`5@CX5~Bg(`&z#0#+HX4Dxj7nKbT^QEk9S_GhbG#q8;w+9T&CDix@iW!xqs zGoF!|1?XY7c1=|1xpBE-n0rUA7&&*PQs6z1R5H&FC-aI#E(}DvAW5!;r0>F~Wu}g7 zt04AAMb|ot3}@9UWx`;vb!=UCx|&UBbNXzk`t$19V4lMEvxzU diff --git a/tools/tests/unit/test_parse_z.asm b/tools/tests/unit/test_parse_z.asm index a7f765a..f6a8023 100644 --- a/tools/tests/unit/test_parse_z.asm +++ b/tools/tests/unit/test_parse_z.asm @@ -11,8 +11,7 @@ jp test zasmGetPC: zasmIsFirstPass: symSelect: -symFind: -symGetVal: +symFindVal: jp fail testNum: .db 1 diff --git a/tools/tests/unit/test_symbol.asm b/tools/tests/unit/test_symbol.asm index bff4b45..66cb5bd 100644 --- a/tools/tests/unit/test_symbol.asm +++ b/tools/tests/unit/test_symbol.asm @@ -34,9 +34,8 @@ test: jp nz, fail ld hl, sFOO - call symFind ; don't match FOOBAR + call symFindVal ; don't match FOOBAR jp nz, fail - call symGetVal ld a, d or a jp nz, fail diff --git a/tools/tests/zasm/runtests.sh b/tools/tests/zasm/runtests.sh index ec217ca..2c20154 100755 --- a/tools/tests/zasm/runtests.sh +++ b/tools/tests/zasm/runtests.sh @@ -2,7 +2,6 @@ set -e -TMPFILE=$(mktemp) KERNEL=../../../kernel APPS=../../../apps ZASM=../../zasm.sh @@ -10,9 +9,8 @@ ASMFILE=${APPS}/zasm/instr.asm cmpas() { FN=$1 - shift 1 EXPECTED=$(xxd ${FN}.expected) - ACTUAL=$(cat ${FN} | $ZASM "$@" | xxd) + ACTUAL=$(cat ${FN} | $ZASM "${KERNEL}" "${APPS}" | xxd) if [ "$ACTUAL" == "$EXPECTED" ]; then echo ok else @@ -24,9 +22,14 @@ cmpas() { fi } +if [[ ! -z $1 ]]; then + cmpas $1 + exit 0 +fi + for fn in *.asm; do echo "Comparing ${fn}" - cmpas $fn "${KERNEL}" "${APPS}" + cmpas $fn done ./errtests.sh diff --git a/tools/tests/zasm/test7.asm.expected b/tools/tests/zasm/test7.asm.expected index 2ee334eed33ce2a947bb51443b7bdfd5d07eaf8f..2046b754eb6eb216f1c7fa5987d4d16b3398f82e 100644 GIT binary patch delta 1571 zcmYjReQXp}5Z|xc``Gqww=LXSIC;mW?UQoXaxHgAVG9u`m{3$AQ9g{Kgh&FEh|-I7 zRU{!XG5+!VLp1RN6BGZzBEg!N(1Wu{4;+$9I2Fmp9LZ|%tfnQ@7)*2N>v7Jfc3Xz=Y6E})tnH`hvZy^TNyd|Sf4NRdrUb5#GV*>kL>P*3a zbRi%^T7t4ff>S$29cU7Yamnxg?wf8u(qHl_Zc-4OF}f65SW-@lZ?T8ujp7w{Lf+B!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tools/tests/zasm/test9.asm new file mode 100644 index 0000000..a10bbda --- /dev/null +++ b/tools/tests/zasm/test9.asm @@ -0,0 +1,11 @@ +; test some weird label bug zasm had at some point. Simply to refer to a local +; label in a .dw directive would mess up future label references. +foo: + inc a +.bar: + inc b +.baz: + .dw .bar + +loop: + jr loop diff --git a/tools/tests/zasm/test9.asm.expected b/tools/tests/zasm/test9.asm.expected new file mode 100644 index 0000000000000000000000000000000000000000..19baa68d1d9dcc28f796fac650e9bc490bf8ea03 GIT binary patch literal 6 NcmcCvVPuf_2LJ-J0ayS4 literal 0 HcmV?d00001