diff --git a/compiler/test/TestPointers.kt b/compiler/test/TestPointers.kt index 58c0da16a..eb2d4d354 100644 --- a/compiler/test/TestPointers.kt +++ b/compiler/test/TestPointers.kt @@ -1707,7 +1707,7 @@ main { errors.errors[1] shouldContain "assigning this value to struct instance not supported" } - test("pointer variable usage detection in other block") { + test("pointer variable usage detection in other block 1") { val src=""" main { sub start() { @@ -1723,4 +1723,80 @@ other { compileText(VMTarget(), true, src, outputDir, writeAssembly = false) shouldNotBe null } + test("pointer variable usage detection in other block 2") { + val src=""" +main { + sub start() { + other.func.variable^^ += 3 + } +} + +other { + sub func() { + ^^ubyte variable + } +}""" + compileText(VMTarget(), false, src, outputDir, writeAssembly = false) shouldNotBe null + compileText(VMTarget(), true, src, outputDir, writeAssembly = false) shouldNotBe null + } + + test("float ptr inplace operations") { + val src=""" +%option enable_floats + +main { + ^^float g_floats + + sub start() { + ^^float l_floats + + f_add() + f_sub() + f_mul() + f_div() + + sub f_add() { + l_floats^^ += 3.0 + g_floats^^ += 3.0 + other.g_floats^^ += 3.0 + other.func.l_floats^^ += 3.0 + } + + sub f_sub() { + l_floats^^ -= 3.0 + g_floats^^ -= 3.0 + other.g_floats^^ -= 3.0 + other.func.l_floats^^ -= 3.0 + } + + sub f_mul() { + l_floats^^ *= 3.0 + g_floats^^ *= 3.0 + other.g_floats^^ *= 3.0 + other.func.l_floats^^ *= 3.0 + } + + sub f_div() { + l_floats^^ /= 3.0 + g_floats^^ /= 3.0 + other.g_floats^^ /= 3.0 + other.func.l_floats^^ /= 3.0 + } + } +} + +other { + %option force_output + + ^^float g_floats + + sub func() { + ^^float l_floats + } +}""" + compileText(VMTarget(), false, src, outputDir) shouldNotBe null + //compileText(C64Target(), false, src, outputDir) shouldNotBe null + //compileText(Cx16Target(), false, src, outputDir) shouldNotBe null + } + }) \ No newline at end of file diff --git a/compilerAst/src/prog8/compiler/CallGraph.kt b/compilerAst/src/prog8/compiler/CallGraph.kt index 16558a92d..f19f9b2e1 100644 --- a/compilerAst/src/prog8/compiler/CallGraph.kt +++ b/compilerAst/src/prog8/compiler/CallGraph.kt @@ -172,6 +172,9 @@ class CallGraph(private val program: Program) : IAstVisitor { if(variable is VarDecl) { allIdentifiersAndTargets.add(IdentifierReference(listOf(variable.name), variable.position) to variable) } + else if(variable is Subroutine) { + notCalledButReferenced += variable + } chain.removeLastOrNull() } super.visit(deref) diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 09a80e378..6c61ff569 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,6 +1,7 @@ TODO ==== + STRUCTS and TYPED POINTERS -------------------------- diff --git a/examples/test.p8 b/examples/test.p8 index af0a27463..6886d6e9d 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,12 +1,52 @@ +%option enable_floats main { + ^^float g_floats + sub start() { - other.bptr^^ = true - ; other.bptr = 2222 - ;cx16.r0bL = other.bptr^^ + ^^float l_floats + + f_add() + f_sub() + f_mul() + f_div() + + sub f_add() { + l_floats^^ += 3.0 + g_floats^^ += 3.0 + other.g_floats^^ += 3.0 + other.func.l_floats^^ += 3.0 + } + + sub f_sub() { + l_floats^^ -= 3.0 + g_floats^^ -= 3.0 + other.g_floats^^ -= 3.0 + other.func.l_floats^^ -= 3.0 + } + + sub f_mul() { + l_floats^^ *= 3.0 + g_floats^^ *= 3.0 + other.g_floats^^ *= 3.0 + other.func.l_floats^^ *= 3.0 + } + + sub f_div() { + l_floats^^ /= 3.0 + g_floats^^ /= 3.0 + other.g_floats^^ /= 3.0 + other.func.l_floats^^ /= 3.0 + } } } other { - ^^bool bptr + %option force_output + + ^^float g_floats + + sub func() { + ^^float l_floats + } } diff --git a/intermediate/src/prog8/intermediate/IRInstructions.kt b/intermediate/src/prog8/intermediate/IRInstructions.kt index 67cd7f1c5..e43fb4f7e 100644 --- a/intermediate/src/prog8/intermediate/IRInstructions.kt +++ b/intermediate/src/prog8/intermediate/IRInstructions.kt @@ -679,15 +679,15 @@ val instructionFormats = mutableMapOf( Opcode.SUBR to InstructionFormat.from("BW,<>r1,fr1,r1,fr1,a | F,a"), - Opcode.MULR to InstructionFormat.from("BW,<>r1,r1,a"), + Opcode.MULR to InstructionFormat.from("BW,<>r1,fr1,r1,fr1,a | F,a"), Opcode.MULSR to InstructionFormat.from("BW,<>r1,fr1,r1,fr1,a | F,a"), - Opcode.DIVR to InstructionFormat.from("BW,<>r1,r1,a"), + Opcode.DIVR to InstructionFormat.from("BW,<>r1,fr1,r1,fr1,a | F,a"), Opcode.DIVSR to InstructionFormat.from("BW,<>r1,fr1,r1,fr1,a | F,a"),