2021-12-04 17:20:22 +00:00
|
|
|
package prog8tests.codegeneration
|
2021-06-01 19:21:33 +00:00
|
|
|
|
2021-11-07 14:40:05 +00:00
|
|
|
import io.kotest.core.spec.style.StringSpec
|
|
|
|
import io.kotest.matchers.shouldBe
|
2022-06-24 00:26:45 +00:00
|
|
|
import io.kotest.matchers.shouldNotBe
|
2021-06-01 19:21:33 +00:00
|
|
|
import prog8.ast.Module
|
|
|
|
import prog8.ast.Program
|
2021-10-10 22:22:04 +00:00
|
|
|
import prog8.ast.expressions.AddressOf
|
|
|
|
import prog8.ast.expressions.IdentifierReference
|
2022-02-10 23:21:40 +00:00
|
|
|
import prog8.ast.expressions.NumericLiteral
|
2021-06-01 19:21:33 +00:00
|
|
|
import prog8.ast.statements.*
|
2023-02-03 23:02:50 +00:00
|
|
|
import prog8.code.SymbolTableMaker
|
2023-01-07 14:25:33 +00:00
|
|
|
import prog8.code.ast.PtAddressOf
|
|
|
|
import prog8.code.ast.PtAssignment
|
|
|
|
import prog8.code.ast.PtIdentifier
|
2023-02-12 13:26:19 +00:00
|
|
|
import prog8.code.ast.PtProgram
|
2022-03-10 22:08:41 +00:00
|
|
|
import prog8.code.core.*
|
2022-03-11 19:35:25 +00:00
|
|
|
import prog8.code.target.C64Target
|
2022-11-29 23:15:13 +00:00
|
|
|
import prog8.code.target.VMTarget
|
2023-02-12 22:49:48 +00:00
|
|
|
import prog8.codegen.cpu6502.AsmGen6502Internal
|
2023-01-07 14:25:33 +00:00
|
|
|
import prog8.compiler.astprocessing.IntermediateAstMaker
|
2022-06-24 00:26:45 +00:00
|
|
|
import prog8tests.helpers.*
|
2021-06-01 19:21:33 +00:00
|
|
|
|
2021-12-16 19:27:22 +00:00
|
|
|
class TestAsmGenSymbols: StringSpec({
|
2022-03-04 22:25:26 +00:00
|
|
|
fun createTestProgram(): Program {
|
2021-06-01 19:21:33 +00:00
|
|
|
/*
|
2021-11-07 14:40:05 +00:00
|
|
|
main {
|
2021-06-02 23:42:11 +00:00
|
|
|
|
2021-11-07 14:40:05 +00:00
|
|
|
label_outside:
|
2021-06-02 23:42:11 +00:00
|
|
|
uword var_outside
|
2021-06-01 19:21:33 +00:00
|
|
|
|
2021-06-02 23:42:11 +00:00
|
|
|
sub start () {
|
|
|
|
uword localvar = 1234
|
|
|
|
uword tgt
|
2021-06-01 19:21:33 +00:00
|
|
|
|
2021-11-07 14:40:05 +00:00
|
|
|
locallabel:
|
2021-06-02 23:42:11 +00:00
|
|
|
tgt = localvar
|
|
|
|
tgt = &locallabel
|
|
|
|
tgt = &var_outside
|
|
|
|
tgt = &label_outside
|
2021-06-04 20:42:28 +00:00
|
|
|
tgt = &main.start.localvar
|
|
|
|
tgt = &main.start.locallabel
|
|
|
|
tgt = &main.var_outside
|
|
|
|
tgt = &main.label_outside
|
2021-06-01 19:21:33 +00:00
|
|
|
}
|
2021-11-07 14:40:05 +00:00
|
|
|
}
|
2021-06-04 20:42:28 +00:00
|
|
|
|
2021-06-01 19:21:33 +00:00
|
|
|
*/
|
2023-05-26 20:56:12 +00:00
|
|
|
val varInSub = VarDecl(VarDeclType.VAR, VarDeclOrigin.USERCODE, DataType.UWORD, ZeropageWish.DONTCARE, null, "localvar", NumericLiteral.optimalInteger(1234, Position.DUMMY), false, false, false, Position.DUMMY)
|
|
|
|
val var2InSub = VarDecl(VarDeclType.VAR, VarDeclOrigin.USERCODE, DataType.UWORD, ZeropageWish.DONTCARE, null, "tgt", null, false, false, false, Position.DUMMY)
|
2021-06-02 23:42:11 +00:00
|
|
|
val labelInSub = Label("locallabel", Position.DUMMY)
|
|
|
|
|
|
|
|
val tgt = AssignTarget(IdentifierReference(listOf("tgt"), Position.DUMMY), null, null, Position.DUMMY)
|
2022-01-10 01:25:02 +00:00
|
|
|
val assign1 = Assignment(tgt, IdentifierReference(listOf("localvar"), Position.DUMMY), AssignmentOrigin.USERCODE, Position.DUMMY)
|
2023-09-17 16:30:57 +00:00
|
|
|
val assign2 = Assignment(tgt, AddressOf(IdentifierReference(listOf("locallabel"), Position.DUMMY), null, Position.DUMMY), AssignmentOrigin.USERCODE, Position.DUMMY)
|
|
|
|
val assign3 = Assignment(tgt, AddressOf(IdentifierReference(listOf("var_outside"), Position.DUMMY), null, Position.DUMMY), AssignmentOrigin.USERCODE, Position.DUMMY)
|
|
|
|
val assign4 = Assignment(tgt, AddressOf(IdentifierReference(listOf("label_outside"), Position.DUMMY), null, Position.DUMMY), AssignmentOrigin.USERCODE, Position.DUMMY)
|
|
|
|
val assign5 = Assignment(tgt, AddressOf(IdentifierReference(listOf("main","start","localvar"), Position.DUMMY), null, Position.DUMMY), AssignmentOrigin.USERCODE, Position.DUMMY)
|
|
|
|
val assign6 = Assignment(tgt, AddressOf(IdentifierReference(listOf("main","start","locallabel"), Position.DUMMY), null, Position.DUMMY), AssignmentOrigin.USERCODE, Position.DUMMY)
|
|
|
|
val assign7 = Assignment(tgt, AddressOf(IdentifierReference(listOf("main","var_outside"), Position.DUMMY), null, Position.DUMMY), AssignmentOrigin.USERCODE, Position.DUMMY)
|
|
|
|
val assign8 = Assignment(tgt, AddressOf(IdentifierReference(listOf("main","label_outside"), Position.DUMMY), null, Position.DUMMY), AssignmentOrigin.USERCODE, Position.DUMMY)
|
2021-06-02 23:42:11 +00:00
|
|
|
|
2021-06-04 20:42:28 +00:00
|
|
|
val statements = mutableListOf(varInSub, var2InSub, labelInSub, assign1, assign2, assign3, assign4, assign5, assign6, assign7, assign8)
|
2023-03-07 22:47:14 +00:00
|
|
|
val subroutine = Subroutine("start", mutableListOf(), emptyList(), emptyList(), emptyList(), emptySet(), null, false, false, false, statements, Position.DUMMY)
|
2021-06-02 23:42:11 +00:00
|
|
|
val labelInBlock = Label("label_outside", Position.DUMMY)
|
2023-05-26 20:56:12 +00:00
|
|
|
val varInBlock = VarDecl(VarDeclType.VAR, VarDeclOrigin.USERCODE, DataType.UWORD, ZeropageWish.DONTCARE, null, "var_outside", null, false, false, false, Position.DUMMY)
|
2021-06-02 23:42:11 +00:00
|
|
|
val block = Block("main", null, mutableListOf(labelInBlock, varInBlock, subroutine), false, Position.DUMMY)
|
|
|
|
|
2021-10-14 21:56:23 +00:00
|
|
|
val module = Module(mutableListOf(block), Position.DUMMY, SourceCode.Generated("test"))
|
2022-02-06 21:56:17 +00:00
|
|
|
val program = Program("test", DummyFunctions, DummyMemsizer, DummyStringEncoder).addModule(module)
|
2022-03-04 22:25:26 +00:00
|
|
|
|
|
|
|
return program
|
2021-06-01 19:21:33 +00:00
|
|
|
}
|
|
|
|
|
2023-02-12 22:49:48 +00:00
|
|
|
fun createTestAsmGen6502(program: Program): AsmGen6502Internal {
|
2021-10-29 14:46:56 +00:00
|
|
|
val errors = ErrorReporterForTests()
|
2023-11-02 22:45:10 +00:00
|
|
|
val options = CompilationOptions(OutputType.RAW, CbmPrgLauncherType.NONE, ZeropageType.FULL, emptyList(), CompilationOptions.AllZeropageAllowed, false, true, C64Target(), 999u)
|
2023-10-03 20:54:28 +00:00
|
|
|
val ptProgram = IntermediateAstMaker(program, errors).transform()
|
2023-02-03 23:02:50 +00:00
|
|
|
val st = SymbolTableMaker(ptProgram, options).make()
|
2023-02-12 22:49:48 +00:00
|
|
|
return AsmGen6502Internal(ptProgram, st, options, errors)
|
2021-06-01 19:21:33 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 15:01:54 +00:00
|
|
|
"symbol and variable names from strings" {
|
2022-03-04 22:25:26 +00:00
|
|
|
val program = createTestProgram()
|
2023-02-12 22:49:48 +00:00
|
|
|
val asmgen = createTestAsmGen6502(program)
|
2021-11-07 14:40:05 +00:00
|
|
|
asmgen.asmSymbolName("name") shouldBe "name"
|
|
|
|
asmgen.asmSymbolName("name") shouldBe "name"
|
|
|
|
asmgen.asmSymbolName("<name>") shouldBe "prog8_name"
|
|
|
|
asmgen.asmSymbolName(RegisterOrPair.R15) shouldBe "cx16.r15"
|
|
|
|
asmgen.asmSymbolName(listOf("a", "b", "name")) shouldBe "a.b.name"
|
|
|
|
asmgen.asmVariableName("name") shouldBe "name"
|
|
|
|
asmgen.asmVariableName("<name>") shouldBe "prog8_name"
|
|
|
|
asmgen.asmVariableName(listOf("a", "b", "name")) shouldBe "a.b.name"
|
2021-06-01 19:21:33 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 15:01:54 +00:00
|
|
|
"symbol and variable names from variable identifiers" {
|
2022-03-04 22:25:26 +00:00
|
|
|
val program = createTestProgram()
|
2023-02-12 22:49:48 +00:00
|
|
|
val asmgen = createTestAsmGen6502(program)
|
2023-01-07 14:25:33 +00:00
|
|
|
val sub = asmgen.program.entrypoint()!!
|
2021-06-02 23:42:11 +00:00
|
|
|
|
2023-01-07 14:25:33 +00:00
|
|
|
val localvarIdent = sub.children.asSequence().filterIsInstance<PtAssignment>().first { it.value is PtIdentifier }.value as PtIdentifier
|
2023-02-12 13:26:19 +00:00
|
|
|
asmgen.asmSymbolName(localvarIdent) shouldBe "localvar"
|
|
|
|
asmgen.asmVariableName(localvarIdent) shouldBe "localvar"
|
2023-01-07 14:25:33 +00:00
|
|
|
val localvarIdentScoped = (sub.children.asSequence().filterIsInstance<PtAssignment>().first { (it.value as? PtAddressOf)?.identifier?.name=="main.start.localvar" }.value as PtAddressOf).identifier
|
2023-02-12 13:26:19 +00:00
|
|
|
asmgen.asmSymbolName(localvarIdentScoped) shouldBe "localvar"
|
|
|
|
asmgen.asmVariableName(localvarIdentScoped) shouldBe "localvar"
|
2021-06-02 23:42:11 +00:00
|
|
|
|
2023-01-31 20:49:40 +00:00
|
|
|
val scopedVarIdent = (sub.children.asSequence().filterIsInstance<PtAssignment>().first { (it.value as? PtAddressOf)?.identifier?.name=="main.var_outside" }.value as PtAddressOf).identifier
|
|
|
|
asmgen.asmSymbolName(scopedVarIdent) shouldBe "main.var_outside"
|
|
|
|
asmgen.asmVariableName(scopedVarIdent) shouldBe "main.var_outside"
|
2023-01-07 14:25:33 +00:00
|
|
|
val scopedVarIdentScoped = (sub.children.asSequence().filterIsInstance<PtAssignment>().first { (it.value as? PtAddressOf)?.identifier?.name=="main.var_outside" }.value as PtAddressOf).identifier
|
2021-11-07 14:40:05 +00:00
|
|
|
asmgen.asmSymbolName(scopedVarIdentScoped) shouldBe "main.var_outside"
|
|
|
|
asmgen.asmVariableName(scopedVarIdentScoped) shouldBe "main.var_outside"
|
2021-06-01 19:21:33 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 15:01:54 +00:00
|
|
|
"symbol and variable names from label identifiers" {
|
2022-03-04 22:25:26 +00:00
|
|
|
val program = createTestProgram()
|
2023-02-12 22:49:48 +00:00
|
|
|
val asmgen = createTestAsmGen6502(program)
|
2023-01-07 14:25:33 +00:00
|
|
|
val sub = asmgen.program.entrypoint()!!
|
2021-06-02 23:42:11 +00:00
|
|
|
|
2023-01-31 20:49:40 +00:00
|
|
|
val localLabelIdent = (sub.children.asSequence().filterIsInstance<PtAssignment>().first { (it.value as? PtAddressOf)?.identifier?.name=="main.start.locallabel" }.value as PtAddressOf).identifier
|
2023-02-12 13:26:19 +00:00
|
|
|
asmgen.asmSymbolName(localLabelIdent) shouldBe "locallabel"
|
|
|
|
asmgen.asmVariableName(localLabelIdent) shouldBe "locallabel"
|
2023-01-07 14:25:33 +00:00
|
|
|
val localLabelIdentScoped = (sub.children.asSequence().filterIsInstance<PtAssignment>().first { (it.value as? PtAddressOf)?.identifier?.name=="main.start.locallabel" }.value as PtAddressOf).identifier
|
2023-02-12 13:26:19 +00:00
|
|
|
asmgen.asmSymbolName(localLabelIdentScoped) shouldBe "locallabel"
|
|
|
|
asmgen.asmVariableName(localLabelIdentScoped) shouldBe "locallabel"
|
2021-06-02 23:42:11 +00:00
|
|
|
|
2023-01-31 20:49:40 +00:00
|
|
|
val scopedLabelIdent = (sub.children.asSequence().filterIsInstance<PtAssignment>().first { (it.value as? PtAddressOf)?.identifier?.name=="main.label_outside" }.value as PtAddressOf).identifier
|
|
|
|
asmgen.asmSymbolName(scopedLabelIdent) shouldBe "main.label_outside"
|
|
|
|
asmgen.asmVariableName(scopedLabelIdent) shouldBe "main.label_outside"
|
2023-01-07 14:25:33 +00:00
|
|
|
val scopedLabelIdentScoped = (sub.children.asSequence().filterIsInstance<PtAssignment>().first { (it.value as? PtAddressOf)?.identifier?.name=="main.label_outside" }.value as PtAddressOf).identifier
|
2022-01-17 21:03:53 +00:00
|
|
|
asmgen.asmSymbolName(scopedLabelIdentScoped) shouldBe "main.label_outside"
|
|
|
|
asmgen.asmVariableName(scopedLabelIdentScoped) shouldBe "main.label_outside"
|
2021-06-01 19:21:33 +00:00
|
|
|
}
|
2021-11-14 15:01:54 +00:00
|
|
|
|
|
|
|
"asm names for hooks to zp temp vars" {
|
|
|
|
/*
|
|
|
|
main {
|
|
|
|
|
|
|
|
sub start() {
|
|
|
|
prog8_lib.P8ZP_SCRATCH_REG = 1
|
|
|
|
prog8_lib.P8ZP_SCRATCH_B1 = 1
|
|
|
|
prog8_lib.P8ZP_SCRATCH_W1 = 1
|
|
|
|
prog8_lib.P8ZP_SCRATCH_W2 = 1
|
|
|
|
*/
|
2022-03-04 22:25:26 +00:00
|
|
|
val program = createTestProgram()
|
2023-02-12 22:49:48 +00:00
|
|
|
val asmgen = createTestAsmGen6502(program)
|
2021-11-14 15:01:54 +00:00
|
|
|
asmgen.asmSymbolName("prog8_lib.P8ZP_SCRATCH_REG") shouldBe "P8ZP_SCRATCH_REG"
|
|
|
|
asmgen.asmSymbolName("prog8_lib.P8ZP_SCRATCH_W2") shouldBe "P8ZP_SCRATCH_W2"
|
|
|
|
asmgen.asmSymbolName(listOf("prog8_lib","P8ZP_SCRATCH_REG")) shouldBe "P8ZP_SCRATCH_REG"
|
|
|
|
asmgen.asmSymbolName(listOf("prog8_lib","P8ZP_SCRATCH_W2")) shouldBe "P8ZP_SCRATCH_W2"
|
2023-01-07 14:25:33 +00:00
|
|
|
val id1 = PtIdentifier("prog8_lib.P8ZP_SCRATCH_REG", DataType.UBYTE, Position.DUMMY)
|
|
|
|
val id2 = PtIdentifier("prog8_lib.P8ZP_SCRATCH_W2", DataType.UWORD, Position.DUMMY)
|
2023-02-12 13:26:19 +00:00
|
|
|
id1.parent = PtProgram("test", DummyMemsizer, DummyStringEncoder)
|
|
|
|
id2.parent = PtProgram("test", DummyMemsizer, DummyStringEncoder)
|
2021-11-14 15:01:54 +00:00
|
|
|
asmgen.asmSymbolName(id1) shouldBe "P8ZP_SCRATCH_REG"
|
|
|
|
asmgen.asmSymbolName(id2) shouldBe "P8ZP_SCRATCH_W2"
|
|
|
|
}
|
2022-06-24 00:26:45 +00:00
|
|
|
|
|
|
|
"no double labels with various loops" {
|
|
|
|
val text="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
if true {
|
|
|
|
}
|
|
|
|
|
|
|
|
repeat 4 {
|
|
|
|
}
|
|
|
|
|
|
|
|
while true {
|
|
|
|
}
|
|
|
|
|
|
|
|
repeat {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
val result = compileText(C64Target(), false, text, writeAssembly = true)
|
|
|
|
result shouldNotBe null
|
|
|
|
}
|
2022-11-29 23:15:13 +00:00
|
|
|
|
|
|
|
"identifiers can have the names of cpu instructions" {
|
|
|
|
val text="""
|
|
|
|
%import textio
|
|
|
|
|
|
|
|
nop {
|
|
|
|
sub lda(ubyte sec) -> ubyte {
|
|
|
|
asl:
|
|
|
|
ubyte brk = sec
|
|
|
|
sec++
|
|
|
|
brk += sec
|
|
|
|
return brk
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main {
|
|
|
|
|
|
|
|
sub ffalse(ubyte arg) -> ubyte {
|
|
|
|
arg++
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
sub ftrue(ubyte arg) -> ubyte {
|
|
|
|
arg++
|
|
|
|
return 128
|
|
|
|
}
|
|
|
|
|
|
|
|
sub start() {
|
|
|
|
ubyte col = 10
|
|
|
|
ubyte row = 20
|
|
|
|
txt.print_ub(nop.lda(42))
|
|
|
|
txt.nl()
|
|
|
|
txt.print_uw(nop.lda.asl)
|
|
|
|
|
|
|
|
void ffalse(99)
|
|
|
|
void ftrue(99)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
val result = compileText(C64Target(), false, text, writeAssembly = true)
|
|
|
|
result shouldNotBe null
|
|
|
|
val result2 = compileText(VMTarget(), false, text, writeAssembly = true)
|
|
|
|
result2 shouldNotBe null
|
|
|
|
|
|
|
|
}
|
2023-06-23 21:36:59 +00:00
|
|
|
|
|
|
|
"3 letter names not prefixed too aggressively" {
|
|
|
|
val text = """
|
|
|
|
%import math
|
|
|
|
main {
|
|
|
|
|
|
|
|
sub start() {
|
|
|
|
ubyte lda = getrandom()
|
|
|
|
lda++
|
|
|
|
cx16.r0 = (math.rnd() % 20) * ${'$'}0010
|
|
|
|
lda = math.rnd() % 5
|
|
|
|
lda++
|
|
|
|
}
|
|
|
|
|
|
|
|
sub getrandom() -> ubyte {
|
|
|
|
return cx16.r0L
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val result = compileText(C64Target(), false, text, writeAssembly = true)
|
|
|
|
result shouldNotBe null
|
|
|
|
val result2 = compileText(VMTarget(), false, text, writeAssembly = true)
|
|
|
|
result2 shouldNotBe null
|
|
|
|
}
|
2021-11-07 14:40:05 +00:00
|
|
|
})
|