SDCC : z80 3.8.1 #10616 (Linux)
published under GNU General Public License (GPL)
Attempting to compile the following sample code, by calling sdcc -mz80 -o label.ihx label.c triggers a bug. The compiler will be confused by the label, and report the following error message...
label.c:14: info 218: z80instructionSize() failed to parse line node, assuming 999 bytes
'label:'
We can check if the current line included a colon, and returns 0 to handle it.
diff -upr sdcc-src-20181016-10616/src/z80/peep.c sdcc-src-20181016-10616.patch/src/z80/peep.c
--- sdcc-src-20181016-10616/src/z80/peep.c 2018-10-24 01:36:50.611826478 +0800
+++ sdcc-src-20181016-10616.patch/src/z80/peep.c 2018-10-24 01:37:22.687963925 +0800
@@ -1086,6 +1086,11 @@ int z80instructionSize(lineNode *pl)
return(i);
}
+ if (strchr(pl->line, ':')) {
+ /* a label, not an instruction */
+ return(0);
+ }
+
/* If the instruction is unrecognized, we shouldn't try to optimize. */
/* For all we know it might be some .ds or similar possibly long line */
/* Return a large value to discourage optimization. */
The patch needed for the change above has been submitted as an attachment.
#include <string.h>
void inline_asm_label(unsigned char *ptr)
{
unsigned char buf[10];
if (strlen(ptr) == NULL) {
goto fail;
}
__asm
label:
.db 0x1234
__endasm;
fail:
return;
}
It should be noted that, labels like
$1:must be used instead oflabel:, but both usage triggers the same bug.NB: The labels should be like
1$:with the $ sign after the number.The patch looks like a workaround, not a fix.
AFAIK, labels shouldn't even reach z80instructionSize().
Is this a failure of isLabelDefinition() in src/SDCCpeeph.c to correctly mark the label as such?
Philipp
Last edit: Philipp Klaus Krause 2018-10-24
I don't know how SDCC works, but I tried your suggestion and added some printf()s in isLabelDefinition() of src/SDCCpeeph.c, but it seems this function is working correctly and the bug is hidden in other logics.
Last edit: Maarten Brock 2018-10-24
Diff:
Should the label lines in inline asm already be marked as such in src/SDCCgen.c? That would be consistent with how other labels are marked in code generation.
Philipp
Thanks for your suggestion, I skimmed through src/SDCCgen.c, and made the following change, and the bug is indeed gone.
But I'm not sure if it's a proper fix.
Me neither, but it looks good on first sight.
I implemented this fix in [r13121] together with better label detection and space removal.
This still doesn’t work, you can’t use named labels in inline assembly that way.
It becomes:
00103$is in the scope oflabel:, which meansjrcan’t find itLast edit: Sebastian Riedel 2022-03-10
Yes, I later realized that named labels are completely unsupported and it was not a bug. But even numerical labels were unusable, which was the actual bug. With this patch, at least numerical labels are usable, so I would say the problem is fixed.
I opened a request for inline-asm specific labels [feature-requests:#792]
Related
Feature Requests: #792