package prog8tests.codegen.new6502 import io.kotest.core.spec.style.FunSpec import io.kotest.engine.spec.tempdir import io.kotest.matchers.ints.shouldBeGreaterThan import io.kotest.matchers.ints.shouldBeLessThan import io.kotest.matchers.shouldBe import prog8.code.core.* import prog8.code.target.Cx16Target import prog8.codegen.new6502.AsmGen import prog8.intermediate.* import prog8tests.helpers.ErrorReporterForTests import java.nio.file.Path import kotlin.io.path.exists import kotlin.io.path.readText /** * Tests for the control flow / call translations, which read their operands from the * structured call site (arguments, results, locations) and from the typed register slots. */ class TestControlCodegen : FunSpec({ val tempRoot = tempdir().toPath() fun generateAsm( instructions: List, extraElements: List = emptyList(), name: String ): List { val target = Cx16Target() val options = CompilationOptions.builder(target) .output(OutputType.RAW) .zeropage(ZeropageType.FLOATSAFE) .floats(true) .compilerVersion("test") .memtopAddress(0xffffu) .build() val program = IRProgram("test", IRSymbolTable(), options, DummyStringEncoder) val outputDir: Path = tempRoot.resolve(name) outputDir.toFile().deleteRecursively() outputDir.toFile().mkdirs() program.options.outputDir = outputDir val chunk = IRCodeChunk(null, null) chunk.instructions.addAll(instructions) val sub = IRSubroutine("main.start", emptyList(), emptyList(), Position.DUMMY) sub.chunks.add(chunk) val block = IRBlock("main", false, IRBlock.Options(), Position.DUMMY) block.children.add(sub) extraElements.forEach { block.children.add(it) } program.blocks.add(block) AsmGen(program, target, ErrorReporterForTests()).generate() val asmFile = outputDir.resolve("test.asm") check(asmFile.exists()) { "Assembly file not written: $asmFile" } // drop the '; ' trace comments so only real instructions remain return asmFile.readText().lines().map { it.trim() }.filter { !it.startsWith(";") } } fun regularSub(label: String): IRSubroutine { val sub = IRSubroutine(label, emptyList(), emptyList(), Position.DUMMY) sub.chunks.add(IRCodeChunk(null, null).also { it += IRInstructions.returnVoid() }) return sub } test("call arguments in hardware slots are loaded in slot order, pairs before singles") { // the A-slot argument comes first in the argument list but must be loaded LAST val site = CallSite( CallTarget.Direct(codeLabel("main.testsub")), listOf( Calls.argument(1, IRDataType.BYTE, CallLocation.HardwareRegister(CallingConventionSlot(0))), Calls.argument(2, IRDataType.WORD, CallLocation.HardwareRegister(CallingConventionSlot(3))) ) ) val lines = generateAsm( listOf(IRInstructions.call(site)), listOf(regularSub("main.testsub")), "call-slot-order" ) val jsrIndex = lines.indexOfFirst { it == "jsr main.testsub" } jsrIndex shouldBeGreaterThan -1 // the AX pair (slot 3, register r2) is loaded with lda+ldx, the A slot (r1) with lda val ldxIndex = lines.indexOfFirst { it.startsWith("ldx p8_regfile") } ldxIndex shouldBeGreaterThan -1 ldxIndex shouldBeLessThan jsrIndex val lastLdaBeforeJsr = (jsrIndex - 1 downTo 0).first { lines[it].startsWith("lda p8_regfile") } lastLdaBeforeJsr shouldBeGreaterThan ldxIndex } test("call argument passed in parameter memory is stored to the resolved symbol") { val site = CallSite( CallTarget.Direct(codeLabel("main.testsub")), listOf(Calls.argument(1, IRDataType.WORD, CallLocation.ParameterMemory("value"))) ) val lines = generateAsm( listOf(IRInstructions.call(site)), listOf(regularSub("main.testsub")), "call-param-memory" ) lines.any { it == "sta main.testsub.value" } shouldBe true lines.any { it == "sta main.testsub.value+1" } shouldBe true lines.indexOfFirst { it == "jsr main.testsub" } shouldBeGreaterThan -1 } test("immediate call arguments load directly into A/X/Y hardware slots") { val site = CallSite( CallTarget.Direct(codeLabel("main.testsub")), listOf( Calls.argument(ImmediateOperand.Integer(42, IRDataType.BYTE), CallLocation.HardwareRegister(CallingConventionSlot(0))), Calls.argument(ImmediateOperand.Integer(7, IRDataType.BYTE), CallLocation.HardwareRegister(CallingConventionSlot(1))), Calls.argument(ImmediateOperand.Integer(99, IRDataType.BYTE), CallLocation.HardwareRegister(CallingConventionSlot(2))) ) ) val lines = generateAsm( listOf(IRInstructions.call(site)), listOf(regularSub("main.testsub")), "call-immediate-slots" ) lines.any { it == "lda #42" } shouldBe true lines.any { it == "ldx #7" } shouldBe true lines.any { it == "ldy #99" } shouldBe true lines.none { it == "lda p8_regfile+0" } shouldBe true } test("symbol-offset immediate call arguments preserve offset in A/X/Y slots") { val site = CallSite( CallTarget.Direct(codeLabel("main.testsub")), listOf( Calls.argument( ImmediateOperand.SymbolAddress("main.items", 4, IRDataType.POINTER), CallLocation.HardwareRegister(CallingConventionSlot(0)) ), Calls.argument( ImmediateOperand.SymbolAddress("main.items", 8, IRDataType.POINTER), CallLocation.HardwareRegister(CallingConventionSlot(1)) ), Calls.argument( ImmediateOperand.SymbolAddress("main.items", 12, IRDataType.POINTER), CallLocation.HardwareRegister(CallingConventionSlot(2)) ) ) ) val lines = generateAsm( listOf(IRInstructions.call(site)), listOf(regularSub("main.testsub")), "call-sym-offset-axy" ) lines.any { it == "lda #