2021-12-16 19:27:22 +00:00
|
|
|
package prog8tests.codegeneration
|
|
|
|
|
|
|
|
import io.kotest.core.spec.style.FunSpec
|
2024-02-04 22:41:01 +00:00
|
|
|
import io.kotest.matchers.shouldBe
|
2022-03-07 20:41:12 +00:00
|
|
|
import io.kotest.matchers.shouldNotBe
|
2024-02-04 22:41:01 +00:00
|
|
|
import io.kotest.matchers.string.shouldContain
|
2024-10-16 00:14:19 +00:00
|
|
|
import prog8.ast.statements.Assignment
|
|
|
|
import prog8.ast.statements.AssignmentOrigin
|
|
|
|
import prog8.code.ast.PtAssignment
|
2022-03-11 19:35:25 +00:00
|
|
|
import prog8.code.target.C64Target
|
2024-02-04 22:41:01 +00:00
|
|
|
import prog8tests.helpers.ErrorReporterForTests
|
2021-12-16 19:27:22 +00:00
|
|
|
import prog8tests.helpers.compileText
|
|
|
|
|
|
|
|
|
|
|
|
class TestVariables: FunSpec({
|
|
|
|
|
|
|
|
test("shared variables without refs not removed for inlined asm") {
|
|
|
|
val text = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
2022-01-02 16:07:04 +00:00
|
|
|
ubyte[] @shared arrayvar = [1,2,3,4]
|
|
|
|
str @shared stringvar = "test"
|
2021-12-16 19:27:22 +00:00
|
|
|
ubyte @shared bytevar = 0
|
|
|
|
|
|
|
|
%asm {{
|
2023-12-20 21:20:12 +00:00
|
|
|
lda p8v_arrayvar
|
|
|
|
lda p8v_stringvar
|
|
|
|
lda p8v_bytevar
|
2021-12-16 19:27:22 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
2022-03-07 20:41:12 +00:00
|
|
|
compileText(C64Target(), true, text, writeAssembly = true) shouldNotBe null
|
2021-12-16 19:27:22 +00:00
|
|
|
}
|
2022-01-23 13:23:34 +00:00
|
|
|
|
|
|
|
test("array initialization with array literal") {
|
|
|
|
val text = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
ubyte[] @shared arrayvar = [1,2,3,4]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
2022-03-07 20:41:12 +00:00
|
|
|
compileText(C64Target(), true, text, writeAssembly = true) shouldNotBe null
|
2022-01-23 13:23:34 +00:00
|
|
|
}
|
|
|
|
|
2022-06-04 20:25:51 +00:00
|
|
|
test("pipe character in string literal") {
|
|
|
|
val text = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
str name = "first|second"
|
|
|
|
str name2 = "first | second"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
compileText(C64Target(), false, text, writeAssembly = true) shouldNotBe null
|
|
|
|
}
|
2022-11-27 15:50:44 +00:00
|
|
|
|
|
|
|
test("negation of unsigned via casts") {
|
|
|
|
val text = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
cx16.r0L = -(cx16.r0L as byte) as ubyte
|
|
|
|
cx16.r0 = -(cx16.r0 as word) as uword
|
|
|
|
ubyte ub
|
|
|
|
uword uw
|
|
|
|
ub = -(ub as byte) as ubyte
|
|
|
|
uw = -(uw as word) as uword
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
compileText(C64Target(), false, text, writeAssembly = true) shouldNotBe null
|
|
|
|
}
|
2022-06-04 20:25:51 +00:00
|
|
|
|
2024-02-04 22:41:01 +00:00
|
|
|
test("initialization of boolean array with array") {
|
|
|
|
val text = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
bool[3] sieve0 = [true, false, true]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
compileText(C64Target(), false, text, writeAssembly = true) shouldNotBe null
|
|
|
|
}
|
|
|
|
|
|
|
|
test("initialization of boolean array with wrong array type should fail") {
|
|
|
|
val text = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
bool[] sieve0 = [true, false, 1]
|
|
|
|
bool[] sieve1 = [true, false, 42]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
val errors = ErrorReporterForTests()
|
|
|
|
compileText(C64Target(), false, text, writeAssembly = true, errors=errors) shouldBe null
|
|
|
|
errors.errors.size shouldBe 2
|
2024-10-12 00:56:36 +00:00
|
|
|
errors.errors[0] shouldContain "value has incompatible type"
|
|
|
|
errors.errors[1] shouldContain "value has incompatible type"
|
2024-02-04 22:41:01 +00:00
|
|
|
}
|
2024-10-16 00:14:19 +00:00
|
|
|
|
|
|
|
test("global var init with array lookup should sometimes be const") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
|
|
|
|
bool[] barray = [true, false, true, false]
|
|
|
|
uword[] warray = [&value1, &barray, &value5, 4242]
|
|
|
|
|
|
|
|
bool @shared value1
|
|
|
|
bool @shared value2 = barray[2] ; should be const!
|
|
|
|
bool @shared value3 = true
|
|
|
|
bool @shared value4 = false
|
|
|
|
bool @shared value5 = barray[cx16.r0L] ; cannot be const
|
|
|
|
uword @shared value6 = warray[3] ; should be const!
|
|
|
|
uword @shared value7 = warray[2] ; cannot be const
|
|
|
|
|
|
|
|
sub start() {
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
|
|
|
|
val result = compileText(C64Target(), true, src, writeAssembly = true)!!.compilerAst
|
|
|
|
val main = result.allBlocks.first { it.name=="main" }
|
|
|
|
main.statements.size shouldBe 17
|
|
|
|
val assigns = main.statements.filterIsInstance<Assignment>()
|
|
|
|
assigns[0].target.identifier?.nameInSource shouldBe listOf("value1")
|
|
|
|
assigns[1].target.identifier?.nameInSource shouldBe listOf("value2")
|
|
|
|
assigns[2].target.identifier?.nameInSource shouldBe listOf("value3")
|
|
|
|
assigns[3].target.identifier?.nameInSource shouldBe listOf("value4")
|
|
|
|
assigns[4].target.identifier?.nameInSource shouldBe listOf("value5")
|
|
|
|
assigns[5].target.identifier?.nameInSource shouldBe listOf("value6")
|
|
|
|
assigns[6].target.identifier?.nameInSource shouldBe listOf("value7")
|
|
|
|
assigns[0].origin shouldBe AssignmentOrigin.VARINIT
|
|
|
|
assigns[1].origin shouldBe AssignmentOrigin.VARINIT
|
|
|
|
assigns[2].origin shouldBe AssignmentOrigin.VARINIT
|
|
|
|
assigns[3].origin shouldBe AssignmentOrigin.VARINIT
|
|
|
|
assigns[4].origin shouldBe AssignmentOrigin.VARINIT
|
|
|
|
assigns[5].origin shouldBe AssignmentOrigin.VARINIT
|
|
|
|
assigns[6].origin shouldBe AssignmentOrigin.VARINIT
|
|
|
|
assigns[0].value.constValue(result)?.number shouldBe 0
|
|
|
|
assigns[1].value.constValue(result)?.number shouldBe 1
|
|
|
|
assigns[2].value.constValue(result)?.number shouldBe 1
|
|
|
|
assigns[3].value.constValue(result)?.number shouldBe 0
|
|
|
|
assigns[4].value.constValue(result) shouldBe null
|
|
|
|
assigns[5].value.constValue(result)?.number shouldBe 4242
|
|
|
|
assigns[6].value.constValue(result) shouldBe null
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
test("scoped var init with array lookup should never be const") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
bool[] barray = [true, false, true, false]
|
|
|
|
uword[] warray = [&value1, &barray, &value5, 4242]
|
|
|
|
|
|
|
|
bool @shared value1
|
|
|
|
bool @shared value2 = barray[2]
|
|
|
|
bool @shared value3 = true
|
|
|
|
bool @shared value4 = false
|
|
|
|
bool @shared value5 = barray[cx16.r0L]
|
|
|
|
uword @shared value6 = warray[3]
|
|
|
|
uword @shared value7 = warray[2]
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
|
|
|
|
val result = compileText(C64Target(), true, src, writeAssembly = true)!!.compilerAst
|
|
|
|
val st = result.entrypoint.statements
|
|
|
|
st.size shouldBe 17
|
|
|
|
val assigns = st.filterIsInstance<Assignment>()
|
|
|
|
assigns[0].target.identifier?.nameInSource shouldBe listOf("value1")
|
|
|
|
assigns[1].target.identifier?.nameInSource shouldBe listOf("value2")
|
|
|
|
assigns[2].target.identifier?.nameInSource shouldBe listOf("value3")
|
|
|
|
assigns[3].target.identifier?.nameInSource shouldBe listOf("value4")
|
|
|
|
assigns[4].target.identifier?.nameInSource shouldBe listOf("value5")
|
|
|
|
assigns[5].target.identifier?.nameInSource shouldBe listOf("value6")
|
|
|
|
assigns[6].target.identifier?.nameInSource shouldBe listOf("value7")
|
|
|
|
assigns[0].origin shouldBe AssignmentOrigin.VARINIT
|
|
|
|
assigns[1].origin shouldBe AssignmentOrigin.VARINIT
|
|
|
|
assigns[2].origin shouldBe AssignmentOrigin.VARINIT
|
|
|
|
assigns[3].origin shouldBe AssignmentOrigin.VARINIT
|
|
|
|
assigns[4].origin shouldBe AssignmentOrigin.VARINIT
|
|
|
|
assigns[5].origin shouldBe AssignmentOrigin.VARINIT
|
|
|
|
assigns[6].origin shouldBe AssignmentOrigin.VARINIT
|
|
|
|
assigns[0].value.constValue(result)?.number shouldBe 0
|
|
|
|
assigns[1].value.constValue(result) shouldBe null
|
|
|
|
assigns[2].value.constValue(result)?.number shouldBe 1
|
|
|
|
assigns[3].value.constValue(result)?.number shouldBe 0
|
|
|
|
assigns[4].value.constValue(result) shouldBe null
|
|
|
|
assigns[5].value.constValue(result) shouldBe null
|
|
|
|
assigns[6].value.constValue(result) shouldBe null
|
|
|
|
}
|
2021-12-16 19:27:22 +00:00
|
|
|
})
|