1-letter symbols now also prefixed with 'p8p_'

to avoid assembly errors caused by confusing variable 'a' with register 'a' etc.
This commit is contained in:
Irmen de Jong 2023-06-28 23:17:59 +02:00
parent 30c531b39e
commit c0cb2438d5
4 changed files with 18 additions and 13 deletions

View File

@ -15,33 +15,33 @@ class AsmInstructionNamesFinder(val target: ICompilationTarget): IAstVisitor {
val labels = mutableSetOf<Label>()
val subroutines = mutableSetOf<Subroutine>()
private fun isPossibleInstructionName(name: String) = name.length==3 && name.all { it.isLetter() }
private fun isPossibleConfusingAsmName(name: String) = (name.length==3 || name.length==1) && name.all { it.isLetter() }
fun foundAny(): Boolean = blocks.isNotEmpty() || variables.isNotEmpty() || subroutines.isNotEmpty() || labels.isNotEmpty()
override fun visit(block: Block) {
if(target.name!=VMTarget.NAME && !block.isInLibrary && isPossibleInstructionName(block.name)) {
if(target.name!=VMTarget.NAME && !block.isInLibrary && isPossibleConfusingAsmName(block.name)) {
blocks += block
}
super.visit(block)
}
override fun visit(decl: VarDecl) {
if(target.name!=VMTarget.NAME && !decl.definingModule.isLibrary && isPossibleInstructionName(decl.name)) {
if(target.name!=VMTarget.NAME && !decl.definingModule.isLibrary && isPossibleConfusingAsmName(decl.name)) {
variables += decl
}
super.visit(decl)
}
override fun visit(label: Label) {
if(target.name!=VMTarget.NAME && !label.definingModule.isLibrary && isPossibleInstructionName(label.name)) {
if(target.name!=VMTarget.NAME && !label.definingModule.isLibrary && isPossibleConfusingAsmName(label.name)) {
labels += label
}
super.visit(label)
}
override fun visit(subroutine: Subroutine) {
if(target.name!=VMTarget.NAME && !subroutine.definingModule.isLibrary && isPossibleInstructionName(subroutine.name)) {
if(target.name!=VMTarget.NAME && !subroutine.definingModule.isLibrary && isPossibleConfusingAsmName(subroutine.name)) {
subroutines += subroutine
}
super.visit(subroutine)

View File

@ -22,7 +22,7 @@ class AsmInstructionNamesReplacer(
}
val newName = identifier.nameInSource.map { ident ->
if(ident.length==3 && !identifier.definingModule.isLibrary) {
if((ident.length==3 || ident.length==1) && !identifier.definingModule.isLibrary) {
val blockTarget = blocks.firstOrNull { it.name==ident }
val subTarget = subroutines.firstOrNull {it.name==ident }
val varTarget = variables.firstOrNull { it.name==ident }
@ -84,7 +84,7 @@ class AsmInstructionNamesReplacer(
override fun after(subroutine: Subroutine, parent: Node): Iterable<IAstModification> {
val changedParams = mutableListOf<Pair<Int, SubroutineParameter>>()
subroutine.parameters.withIndex().forEach { (index, param) ->
if(param.name.length==3 && param.name.all { it.isLetter() } && !param.definingModule.isLibrary) {
if((param.name.length==3 || param.name.length==1) && param.name.all { it.isLetter() } && !param.definingModule.isLibrary) {
changedParams.add(index to SubroutineParameter("p8p_${param.name}", param.type, param.position))
}
}

View File

@ -27,14 +27,15 @@ so that more system ram is available for the program code itself.
Three-letter symbols prefixing in Assembly
------------------------------------------
Symbols consisting of three letters such as "brk" or "tax" will confuse the assembler that
thinks these are cpu instructions. It will likely fail to assemble the program correctly.
Because of this, prog8 will prefix every 3-letter symbol with "``p8p_``" automatically during compilation.
So "tax" will become "p8p_tax" in the resulting assembly code.
Symbols consisting of three letters such as "brk" or "tax", or variables named "a", "x" or "y" could
confuse the assembler to think these are cpu instructions or registers.
It will likely fail to assemble the program correctly.
Because of this, prog8 will prefix every 1- and 3-letter symbol with "``p8p_``" automatically during compilation.
So "tax" will become "p8p_tax", "a" will become "p8p_a" in the resulting assembly code.
If you're referencing symbols from the prog8 program in hand-written assembly code, you have to take
this into account. Either prefix the 3-letter symbols in the assembly with "``p8p_``" as well, or just
choose a shorter or longer symbol name in the first place.
this into account. Either prefix the 1- and 3-letter symbols in the assembly with "``p8p_``" as well, or just
choose a symbol name of a different length in the first place.
Software stack for expression evaluation

View File

@ -4,6 +4,10 @@ TODO
- fix array expression assignment bug from Mark in the discord. ("TODO fix temp var name")
- fix double code gen with tryInplaceModifyWithRemovedRedundantCast() ? word birdX[j] += testbyte
- prog8->asm symbol name prefixing: prefix ALL symbols with p8_ Also update manual.
EXCEPTION: library symbols such as cbm.CHROUT, cx16.r0 etc. should NOT be prefixed.
Solution: add %option no_symbol_prefix to those blocks?
...