2021-10-21 22:41:34 +00:00
|
|
|
package prog8tests
|
|
|
|
|
2021-11-08 14:50:29 +00:00
|
|
|
import io.kotest.assertions.withClue
|
2021-11-07 23:16:58 +00:00
|
|
|
import io.kotest.core.spec.style.FunSpec
|
2021-11-08 14:50:29 +00:00
|
|
|
import io.kotest.matchers.shouldBe
|
|
|
|
import io.kotest.matchers.shouldNotBe
|
|
|
|
import io.kotest.matchers.string.shouldContain
|
|
|
|
import io.kotest.matchers.types.instanceOf
|
2021-10-21 22:41:34 +00:00
|
|
|
import prog8.ast.base.DataType
|
2021-11-02 23:47:22 +00:00
|
|
|
import prog8.ast.expressions.*
|
2021-10-24 18:57:10 +00:00
|
|
|
import prog8.ast.statements.*
|
2021-10-21 22:41:34 +00:00
|
|
|
import prog8.compiler.target.C64Target
|
2021-10-21 23:25:26 +00:00
|
|
|
import prog8tests.helpers.ErrorReporterForTests
|
2021-10-21 22:41:34 +00:00
|
|
|
import prog8tests.helpers.assertFailure
|
|
|
|
import prog8tests.helpers.assertSuccess
|
|
|
|
import prog8tests.helpers.compileText
|
|
|
|
|
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
class TestSubroutines: FunSpec({
|
2021-10-21 22:41:34 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
test("stringParameter") {
|
2021-10-21 22:41:34 +00:00
|
|
|
val text = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
2021-10-24 18:57:10 +00:00
|
|
|
str text = "test"
|
|
|
|
|
|
|
|
asmfunc("text")
|
|
|
|
asmfunc(text)
|
|
|
|
asmfunc($2000)
|
|
|
|
func("text")
|
|
|
|
func(text)
|
|
|
|
func($2000)
|
2021-10-21 22:41:34 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 18:57:10 +00:00
|
|
|
asmsub asmfunc(str thing @AY) {
|
2021-10-21 22:41:34 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 18:57:10 +00:00
|
|
|
sub func(str thing) {
|
|
|
|
uword t2 = thing as uword
|
|
|
|
asmfunc(thing)
|
2021-10-21 22:41:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
2021-10-24 18:57:10 +00:00
|
|
|
val result = compileText(C64Target, false, text, writeAssembly = false).assertSuccess()
|
2021-10-29 22:25:34 +00:00
|
|
|
val module = result.program.toplevelModule
|
2021-10-24 18:57:10 +00:00
|
|
|
val mainBlock = module.statements.single() as Block
|
|
|
|
val asmfunc = mainBlock.statements.filterIsInstance<Subroutine>().single { it.name=="asmfunc"}
|
|
|
|
val func = mainBlock.statements.filterIsInstance<Subroutine>().single { it.name=="func"}
|
2021-11-08 14:50:29 +00:00
|
|
|
asmfunc.isAsmSubroutine shouldBe true
|
|
|
|
asmfunc.parameters.single().type shouldBe DataType.STR
|
|
|
|
asmfunc.statements.isEmpty() shouldBe true
|
|
|
|
func.isAsmSubroutine shouldBe false
|
|
|
|
func.parameters.single().type shouldBe DataType.STR
|
|
|
|
func.statements.size shouldBe 4
|
2021-10-24 18:57:10 +00:00
|
|
|
val paramvar = func.statements[0] as VarDecl
|
2021-11-08 14:50:29 +00:00
|
|
|
paramvar.name shouldBe "thing"
|
|
|
|
paramvar.datatype shouldBe DataType.STR
|
2021-10-31 23:24:15 +00:00
|
|
|
val assign = func.statements[2] as Assignment
|
2021-11-08 14:50:29 +00:00
|
|
|
assign.target.identifier!!.nameInSource shouldBe listOf("t2")
|
|
|
|
withClue("str param in function body should not be transformed by normal compiler steps") {
|
|
|
|
assign.value shouldBe instanceOf<TypecastExpression>()
|
|
|
|
}
|
|
|
|
(assign.value as TypecastExpression).type shouldBe DataType.UWORD
|
2021-10-31 23:24:15 +00:00
|
|
|
val call = func.statements[3] as FunctionCallStatement
|
2021-11-08 14:50:29 +00:00
|
|
|
call.target.nameInSource.single() shouldBe "asmfunc"
|
|
|
|
withClue("str param in function body should not be transformed by normal compiler steps") {
|
|
|
|
call.args.single() shouldBe instanceOf<IdentifierReference>()
|
|
|
|
}
|
|
|
|
(call.args.single() as IdentifierReference).nameInSource.single() shouldBe "thing"
|
2021-10-21 22:41:34 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
test("stringParameterAsmGen") {
|
2021-10-21 22:41:34 +00:00
|
|
|
val text = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
str text = "test"
|
|
|
|
|
|
|
|
asmfunc("text")
|
|
|
|
asmfunc(text)
|
|
|
|
asmfunc($2000)
|
|
|
|
func("text")
|
|
|
|
func(text)
|
|
|
|
func($2000)
|
|
|
|
}
|
|
|
|
|
|
|
|
asmsub asmfunc(str thing @AY) {
|
|
|
|
}
|
|
|
|
|
|
|
|
sub func(str thing) {
|
2021-10-24 18:57:10 +00:00
|
|
|
uword t2 = thing as uword
|
|
|
|
asmfunc(thing)
|
2021-10-21 22:41:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
2021-10-24 18:57:10 +00:00
|
|
|
val result = compileText(C64Target, false, text, writeAssembly = true).assertSuccess()
|
2021-10-29 22:25:34 +00:00
|
|
|
val module = result.program.toplevelModule
|
2021-10-21 22:41:34 +00:00
|
|
|
val mainBlock = module.statements.single() as Block
|
|
|
|
val asmfunc = mainBlock.statements.filterIsInstance<Subroutine>().single { it.name=="asmfunc"}
|
|
|
|
val func = mainBlock.statements.filterIsInstance<Subroutine>().single { it.name=="func"}
|
2021-11-08 14:50:29 +00:00
|
|
|
asmfunc.isAsmSubroutine shouldBe true
|
|
|
|
asmfunc.parameters.single().type shouldBe DataType.STR
|
|
|
|
asmfunc.statements.single() shouldBe instanceOf<Return>()
|
|
|
|
func.isAsmSubroutine shouldBe false
|
|
|
|
withClue("asmgen should have changed str to uword type") {
|
|
|
|
func.parameters.single().type shouldBe DataType.UWORD
|
|
|
|
}
|
|
|
|
asmfunc.statements.last() shouldBe instanceOf<Return>()
|
|
|
|
|
|
|
|
func.statements.size shouldBe 5
|
|
|
|
func.statements[4] shouldBe instanceOf<Return>()
|
2021-10-24 18:57:10 +00:00
|
|
|
val paramvar = func.statements[0] as VarDecl
|
2021-11-08 14:50:29 +00:00
|
|
|
paramvar.name shouldBe "thing"
|
|
|
|
withClue("pre-asmgen should have changed str to uword type") {
|
|
|
|
paramvar.datatype shouldBe DataType.UWORD
|
|
|
|
}
|
2021-10-31 23:24:15 +00:00
|
|
|
val assign = func.statements[2] as Assignment
|
2021-11-08 14:50:29 +00:00
|
|
|
assign.target.identifier!!.nameInSource shouldBe listOf("t2")
|
|
|
|
withClue("str param in function body should be treated as plain uword before asmgen") {
|
|
|
|
assign.value shouldBe instanceOf<IdentifierReference>()
|
|
|
|
}
|
|
|
|
(assign.value as IdentifierReference).nameInSource.single() shouldBe "thing"
|
2021-10-31 23:24:15 +00:00
|
|
|
val call = func.statements[3] as FunctionCallStatement
|
2021-11-08 14:50:29 +00:00
|
|
|
call.target.nameInSource.single() shouldBe "asmfunc"
|
|
|
|
withClue("str param in function body should be treated as plain uword and not been transformed") {
|
|
|
|
call.args.single() shouldBe instanceOf<IdentifierReference>()
|
|
|
|
}
|
|
|
|
(call.args.single() as IdentifierReference).nameInSource.single() shouldBe "thing"
|
2021-10-21 22:41:34 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
test("arrayParameterNotYetAllowed_ButShouldPerhapsBe") {
|
2021-10-25 21:01:07 +00:00
|
|
|
// note: the *parser* accepts this as it is valid *syntax*,
|
|
|
|
// however, it's not (yet) valid for the compiler
|
|
|
|
val text = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
}
|
|
|
|
|
|
|
|
asmsub asmfunc(ubyte[] thing @AY) {
|
|
|
|
}
|
|
|
|
|
|
|
|
sub func(ubyte[22] thing) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
val errors = ErrorReporterForTests()
|
|
|
|
compileText(C64Target, false, text, errors, false).assertFailure("currently array dt in signature is invalid") // TODO should not be invalid?
|
2021-11-08 14:50:29 +00:00
|
|
|
errors.warnings.size shouldBe 0
|
|
|
|
errors.errors.single() shouldContain ".p8:9:16: Non-string pass-by-reference types cannot occur as a parameter type directly"
|
2021-10-25 21:01:07 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
// TODO allow this?
|
|
|
|
xtest("arrayParameter") {
|
2021-10-21 22:41:34 +00:00
|
|
|
val text = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
ubyte[] array = [1,2,3]
|
|
|
|
|
|
|
|
asmfunc(array)
|
|
|
|
asmfunc([4,5,6])
|
|
|
|
asmfunc($2000)
|
|
|
|
asmfunc(12.345)
|
|
|
|
func(array)
|
|
|
|
func([4,5,6])
|
|
|
|
func($2000)
|
|
|
|
func(12.345)
|
|
|
|
}
|
|
|
|
|
|
|
|
asmsub asmfunc(ubyte[] thing @AY) {
|
|
|
|
}
|
|
|
|
|
|
|
|
sub func(ubyte[22] thing) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2021-10-21 23:25:26 +00:00
|
|
|
val result = compileText(C64Target, false, text, writeAssembly = false).assertSuccess()
|
2021-10-29 22:25:34 +00:00
|
|
|
val module = result.program.toplevelModule
|
2021-10-21 22:41:34 +00:00
|
|
|
val mainBlock = module.statements.single() as Block
|
|
|
|
val asmfunc = mainBlock.statements.filterIsInstance<Subroutine>().single { it.name=="asmfunc"}
|
|
|
|
val func = mainBlock.statements.filterIsInstance<Subroutine>().single { it.name=="func"}
|
2021-11-08 14:50:29 +00:00
|
|
|
asmfunc.isAsmSubroutine shouldBe true
|
|
|
|
asmfunc.parameters.single().type shouldBe DataType.ARRAY_UB
|
|
|
|
asmfunc.statements.isEmpty() shouldBe true
|
|
|
|
func.isAsmSubroutine shouldBe false
|
|
|
|
func.parameters.single().type shouldBe DataType.ARRAY_UB
|
|
|
|
func.statements.isEmpty() shouldBe true
|
2021-10-21 22:41:34 +00:00
|
|
|
}
|
2021-11-02 23:47:22 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
test("testUwordParameterAndNormalVarIndexedAsArrayWorkAsDirectMemoryRead") {
|
2021-11-02 23:47:22 +00:00
|
|
|
val text="""
|
|
|
|
main {
|
|
|
|
sub thing(uword rr) {
|
2021-11-17 23:17:22 +00:00
|
|
|
ubyte @shared xx = rr[1] ; should still work as var initializer that will be rewritten
|
|
|
|
ubyte @shared yy
|
2021-11-02 23:47:22 +00:00
|
|
|
yy = rr[2]
|
2021-11-17 23:17:22 +00:00
|
|
|
uword @shared other
|
2021-11-02 23:47:22 +00:00
|
|
|
ubyte zz = other[3]
|
|
|
|
}
|
|
|
|
|
|
|
|
sub start() {
|
|
|
|
ubyte[] array=[1,2,3]
|
|
|
|
thing(array)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
val result = compileText(C64Target, false, text, writeAssembly = true).assertSuccess()
|
|
|
|
val module = result.program.toplevelModule
|
|
|
|
val block = module.statements.single() as Block
|
|
|
|
val thing = block.statements.filterIsInstance<Subroutine>().single {it.name=="thing"}
|
2021-11-08 14:50:29 +00:00
|
|
|
block.name shouldBe "main"
|
2021-11-17 23:17:22 +00:00
|
|
|
thing.statements.size shouldBe 11 // rr paramdecl, xx, xx assign, yy decl, yy init 0, yy assign, other, other assign 0, zz, zz assign, return
|
2021-11-02 23:47:22 +00:00
|
|
|
val xx = thing.statements[1] as VarDecl
|
2021-11-08 14:50:29 +00:00
|
|
|
withClue("vardecl init values must have been moved to separate assignments") {
|
|
|
|
xx.value shouldBe null
|
|
|
|
}
|
2021-11-02 23:47:22 +00:00
|
|
|
val assignXX = thing.statements[2] as Assignment
|
2021-11-17 23:17:22 +00:00
|
|
|
val assignYY = thing.statements[5] as Assignment
|
|
|
|
val assignZZ = thing.statements[9] as Assignment
|
2021-11-08 14:50:29 +00:00
|
|
|
assignXX.target.identifier!!.nameInSource shouldBe listOf("xx")
|
|
|
|
assignYY.target.identifier!!.nameInSource shouldBe listOf("yy")
|
|
|
|
assignZZ.target.identifier!!.nameInSource shouldBe listOf("zz")
|
2021-11-02 23:47:22 +00:00
|
|
|
val valueXXexpr = (assignXX.value as DirectMemoryRead).addressExpression as BinaryExpression
|
|
|
|
val valueYYexpr = (assignYY.value as DirectMemoryRead).addressExpression as BinaryExpression
|
|
|
|
val valueZZexpr = (assignZZ.value as DirectMemoryRead).addressExpression as BinaryExpression
|
2021-11-08 14:50:29 +00:00
|
|
|
(valueXXexpr.left as IdentifierReference).nameInSource shouldBe listOf("rr")
|
|
|
|
(valueYYexpr.left as IdentifierReference).nameInSource shouldBe listOf("rr")
|
|
|
|
(valueZZexpr.left as IdentifierReference).nameInSource shouldBe listOf("other")
|
|
|
|
(valueXXexpr.right as NumericLiteralValue).number.toInt() shouldBe 1
|
|
|
|
(valueYYexpr.right as NumericLiteralValue).number.toInt() shouldBe 2
|
|
|
|
(valueZZexpr.right as NumericLiteralValue).number.toInt() shouldBe 3
|
2021-11-02 23:47:22 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
test("testUwordParameterAndNormalVarIndexedAsArrayWorkAsMemoryWrite") {
|
2021-11-02 23:47:22 +00:00
|
|
|
val text="""
|
|
|
|
main {
|
|
|
|
sub thing(uword rr) {
|
|
|
|
rr[10] = 42
|
|
|
|
}
|
|
|
|
|
|
|
|
sub start() {
|
|
|
|
ubyte[] array=[1,2,3]
|
|
|
|
thing(array)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
val result = compileText(C64Target, false, text, writeAssembly = true).assertSuccess()
|
|
|
|
val module = result.program.toplevelModule
|
|
|
|
val block = module.statements.single() as Block
|
|
|
|
val thing = block.statements.filterIsInstance<Subroutine>().single {it.name=="thing"}
|
2021-11-08 14:50:29 +00:00
|
|
|
block.name shouldBe "main"
|
|
|
|
thing.statements.size shouldBe 3 // "rr, rr assign, return void"
|
2021-11-02 23:47:22 +00:00
|
|
|
val assignRR = thing.statements[1] as Assignment
|
2021-11-08 14:50:29 +00:00
|
|
|
(assignRR.value as NumericLiteralValue).number.toInt() shouldBe 42
|
2021-11-02 23:47:22 +00:00
|
|
|
val memwrite = assignRR.target.memoryAddress
|
2021-11-08 14:50:29 +00:00
|
|
|
memwrite shouldNotBe null
|
|
|
|
val addressExpr = memwrite!!.addressExpression as BinaryExpression
|
|
|
|
(addressExpr.left as IdentifierReference).nameInSource shouldBe listOf("rr")
|
|
|
|
addressExpr.operator shouldBe "+"
|
|
|
|
(addressExpr.right as NumericLiteralValue).number.toInt() shouldBe 10
|
2021-11-02 23:47:22 +00:00
|
|
|
}
|
2021-11-07 23:16:58 +00:00
|
|
|
})
|