2022-10-27 19:58:37 +00:00
|
|
|
package prog8tests.codegeneration
|
|
|
|
|
|
|
|
import io.kotest.core.spec.style.FunSpec
|
2023-05-28 21:19:01 +00:00
|
|
|
import io.kotest.matchers.shouldBe
|
2022-10-27 19:58:37 +00:00
|
|
|
import io.kotest.matchers.shouldNotBe
|
2023-05-28 21:19:01 +00:00
|
|
|
import io.kotest.matchers.string.shouldContain
|
|
|
|
import prog8.code.ast.printAst
|
2022-10-27 19:58:37 +00:00
|
|
|
import prog8.code.target.C64Target
|
2022-10-27 21:31:57 +00:00
|
|
|
import prog8.code.target.VMTarget
|
2023-05-28 21:19:01 +00:00
|
|
|
import prog8tests.helpers.ErrorReporterForTests
|
2022-10-27 21:08:40 +00:00
|
|
|
import prog8tests.helpers.compileText
|
2022-10-27 19:58:37 +00:00
|
|
|
|
2023-05-28 21:19:01 +00:00
|
|
|
class TestArrayThings: FunSpec({
|
2022-10-27 19:58:37 +00:00
|
|
|
test("assign prefix var to array should compile fine and is not split into inplace array modification") {
|
|
|
|
val text = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
byte[5] array
|
|
|
|
byte bb
|
|
|
|
array[1] = -bb
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
compileText(C64Target(), false, text, writeAssembly = true) shouldNotBe null
|
2022-10-27 21:31:57 +00:00
|
|
|
compileText(VMTarget(), false, text, writeAssembly = true) shouldNotBe null
|
2022-10-27 19:58:37 +00:00
|
|
|
}
|
2022-10-27 21:08:40 +00:00
|
|
|
|
|
|
|
test("array in-place negation (integer types)") {
|
|
|
|
val text = """
|
|
|
|
main {
|
|
|
|
byte[10] foo
|
|
|
|
ubyte[10] foou
|
|
|
|
word[10] foow
|
|
|
|
uword[10] foowu
|
|
|
|
|
|
|
|
sub start() {
|
|
|
|
foo[1] = 42
|
|
|
|
foo[1] = -foo[1]
|
|
|
|
|
|
|
|
foow[1] = 4242
|
|
|
|
foow[1] = -foow[1]
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
compileText(C64Target(), false, text, writeAssembly = true) shouldNotBe null
|
2022-10-27 21:31:57 +00:00
|
|
|
compileText(VMTarget(), false, text, writeAssembly = true) shouldNotBe null
|
2022-10-27 21:08:40 +00:00
|
|
|
}
|
|
|
|
|
2022-10-29 15:04:39 +00:00
|
|
|
test("array in-place negation (float type) vm target") {
|
2022-10-27 21:08:40 +00:00
|
|
|
val text = """
|
|
|
|
%import floats
|
|
|
|
|
|
|
|
main {
|
|
|
|
float[10] flt
|
|
|
|
|
|
|
|
sub start() {
|
|
|
|
flt[1] = 42.42
|
|
|
|
flt[1] = -flt[1]
|
|
|
|
}
|
|
|
|
}"""
|
2022-10-27 21:31:57 +00:00
|
|
|
compileText(VMTarget(), false, text, writeAssembly = true) shouldNotBe null
|
2022-10-29 15:04:39 +00:00
|
|
|
}
|
|
|
|
|
2023-02-03 23:02:50 +00:00
|
|
|
test("array in-place negation (float type) 6502 target") {
|
2022-10-29 15:04:39 +00:00
|
|
|
val text = """
|
|
|
|
%import floats
|
|
|
|
|
|
|
|
main {
|
|
|
|
float[10] flt
|
|
|
|
|
|
|
|
sub start() {
|
|
|
|
flt[1] = 42.42
|
|
|
|
flt[1] = -flt[1]
|
|
|
|
}
|
|
|
|
}"""
|
2022-10-27 21:08:40 +00:00
|
|
|
compileText(C64Target(), false, text, writeAssembly = true) shouldNotBe null
|
|
|
|
}
|
|
|
|
|
|
|
|
test("array in-place invert") {
|
|
|
|
val text = """
|
|
|
|
main {
|
|
|
|
ubyte[10] foo
|
|
|
|
uword[10] foow
|
|
|
|
|
|
|
|
sub start() {
|
|
|
|
foo[1] = 42
|
|
|
|
foo[1] = ~foo[1]
|
|
|
|
|
|
|
|
foow[1] = 4242
|
|
|
|
foow[1] = ~foow[1]
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
compileText(C64Target(), false, text, writeAssembly = true) shouldNotBe null
|
2022-10-27 21:31:57 +00:00
|
|
|
compileText(VMTarget(), false, text, writeAssembly = true) shouldNotBe null
|
2022-10-27 21:08:40 +00:00
|
|
|
}
|
2023-05-28 21:19:01 +00:00
|
|
|
|
|
|
|
test("split only for word arrays") {
|
|
|
|
val text = """
|
|
|
|
main {
|
|
|
|
ubyte[10] @split sb
|
|
|
|
uword[10] @split sw
|
|
|
|
word[10] @split sw2
|
|
|
|
float[10] @split sf
|
|
|
|
|
|
|
|
sub start() {
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val errors = ErrorReporterForTests()
|
|
|
|
compileText(C64Target(), false, text, writeAssembly = false, errors = errors) shouldBe null
|
|
|
|
errors.errors.size shouldBe 2
|
|
|
|
errors.errors.forEach {
|
|
|
|
it shouldContain "split"
|
|
|
|
it shouldContain "word arrays"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
test("split word arrays in asm as lsb/msb") {
|
|
|
|
val text = """
|
|
|
|
main {
|
|
|
|
uword[10] @split @shared uw
|
|
|
|
word[10] @split @shared sw
|
|
|
|
uword[10] @shared normal
|
|
|
|
|
|
|
|
sub start() {
|
|
|
|
%asm {{
|
|
|
|
lda normal
|
|
|
|
lda uw_lsb
|
|
|
|
lda uw_msb
|
|
|
|
lda sw_lsb
|
|
|
|
lda sw_msb
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val result6502 = compileText(C64Target(), false, text, writeAssembly = true)!!.codegenAst!!
|
|
|
|
val resultVm = compileText(VMTarget(), false, text, writeAssembly = true)!!.codegenAst!!
|
|
|
|
printAst(result6502, true, ::println)
|
|
|
|
printAst(resultVm, true, ::println)
|
|
|
|
}
|
2022-10-27 19:58:37 +00:00
|
|
|
})
|
|
|
|
|