2024-09-08 14:03:57 +00:00
|
|
|
package prog8tests.compiler
|
2021-10-20 20:16:26 +00:00
|
|
|
|
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
|
2021-11-19 23:15:04 +00:00
|
|
|
import io.kotest.matchers.string.shouldContain
|
2022-05-08 11:21:34 +00:00
|
|
|
import io.kotest.matchers.string.shouldStartWith
|
2021-11-08 14:50:29 +00:00
|
|
|
import io.kotest.matchers.types.instanceOf
|
|
|
|
import io.kotest.matchers.types.shouldBeSameInstanceAs
|
2022-03-10 00:41:42 +00:00
|
|
|
import prog8.ast.ParentSentinel
|
2021-11-01 23:47:01 +00:00
|
|
|
import prog8.ast.Program
|
2021-11-09 00:13:23 +00:00
|
|
|
import prog8.ast.expressions.*
|
2021-11-01 23:47:01 +00:00
|
|
|
import prog8.ast.statements.*
|
2024-04-06 00:16:21 +00:00
|
|
|
import prog8.code.ast.PtAssignTarget
|
|
|
|
import prog8.code.ast.PtAssignment
|
|
|
|
import prog8.code.ast.PtFunctionCall
|
2024-09-01 18:19:24 +00:00
|
|
|
import prog8.code.core.BaseDataType
|
2022-06-29 20:19:44 +00:00
|
|
|
import prog8.code.core.Position
|
2022-03-11 19:35:25 +00:00
|
|
|
import prog8.code.target.C64Target
|
2022-12-24 17:01:54 +00:00
|
|
|
import prog8.code.target.Cx16Target
|
2023-11-17 18:34:19 +00:00
|
|
|
import prog8.code.target.VMTarget
|
2021-11-19 23:15:04 +00:00
|
|
|
import prog8tests.helpers.*
|
2022-03-10 21:38:16 +00:00
|
|
|
|
2021-10-20 20:16:26 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
class TestOptimization: FunSpec({
|
2021-11-19 21:49:35 +00:00
|
|
|
test("remove empty subroutine except start") {
|
2021-10-20 20:16:26 +00:00
|
|
|
val sourcecode = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
}
|
|
|
|
sub empty() {
|
|
|
|
; going to be removed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
2022-03-07 20:41:12 +00:00
|
|
|
val result = compileText(C64Target(), true, sourcecode)!!
|
2023-02-09 00:46:23 +00:00
|
|
|
val toplevelModule = result.compilerAst.toplevelModule
|
2021-10-20 20:16:26 +00:00
|
|
|
val mainBlock = toplevelModule.statements.single() as Block
|
2021-10-20 20:50:18 +00:00
|
|
|
val startSub = mainBlock.statements.single() as Subroutine
|
2023-02-09 00:46:23 +00:00
|
|
|
result.compilerAst.entrypoint shouldBeSameInstanceAs startSub
|
2021-11-08 14:50:29 +00:00
|
|
|
withClue("only start sub should remain") {
|
|
|
|
startSub.name shouldBe "start"
|
|
|
|
}
|
|
|
|
withClue("compiler has inserted return in empty subroutines") {
|
|
|
|
startSub.statements.single() shouldBe instanceOf<Return>()
|
|
|
|
}
|
2021-10-20 20:16:26 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 21:49:35 +00:00
|
|
|
test("don't remove empty subroutine if it's referenced") {
|
2021-10-20 20:16:26 +00:00
|
|
|
val sourcecode = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
uword xx = &empty
|
|
|
|
xx++
|
|
|
|
}
|
|
|
|
sub empty() {
|
|
|
|
; should not be removed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
2022-03-07 20:41:12 +00:00
|
|
|
val result = compileText(C64Target(), true, sourcecode)!!
|
2023-02-09 00:46:23 +00:00
|
|
|
val toplevelModule = result.compilerAst.toplevelModule
|
2021-10-20 20:16:26 +00:00
|
|
|
val mainBlock = toplevelModule.statements.single() as Block
|
|
|
|
val startSub = mainBlock.statements[0] as Subroutine
|
|
|
|
val emptySub = mainBlock.statements[1] as Subroutine
|
2023-02-09 00:46:23 +00:00
|
|
|
result.compilerAst.entrypoint shouldBeSameInstanceAs startSub
|
2021-11-08 14:50:29 +00:00
|
|
|
startSub.name shouldBe "start"
|
|
|
|
emptySub.name shouldBe "empty"
|
|
|
|
withClue("compiler has inserted return in empty subroutines") {
|
|
|
|
emptySub.statements.single() shouldBe instanceOf<Return>()
|
|
|
|
}
|
2021-10-20 20:16:26 +00:00
|
|
|
}
|
2021-11-01 23:47:01 +00:00
|
|
|
|
2024-03-16 23:35:36 +00:00
|
|
|
test("don't remove empty subroutine if it's referenced in vardecl") {
|
|
|
|
val sourcecode = """
|
|
|
|
main {
|
|
|
|
ubyte tw = other.width()
|
|
|
|
sub start() {
|
|
|
|
tw++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
other {
|
|
|
|
sub width() -> ubyte {
|
|
|
|
cx16.r0++
|
|
|
|
return 80
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
compileText(C64Target(), true, sourcecode, writeAssembly = true) shouldNotBe null
|
|
|
|
compileText(VMTarget(), true, sourcecode, writeAssembly = true) shouldNotBe null
|
|
|
|
}
|
|
|
|
|
2021-11-19 23:15:04 +00:00
|
|
|
test("generated constvalue from typecast inherits proper parent linkage") {
|
2024-09-01 18:19:24 +00:00
|
|
|
val number = NumericLiteral(BaseDataType.UBYTE, 11.0, Position.DUMMY)
|
2022-03-10 21:38:16 +00:00
|
|
|
val tc = TypecastExpression(number, DataType.BYTE, false, Position.DUMMY)
|
2021-11-01 23:47:01 +00:00
|
|
|
val program = Program("test", DummyFunctions, DummyMemsizer, DummyStringEncoder)
|
|
|
|
tc.linkParents(ParentSentinel)
|
2021-11-08 14:50:29 +00:00
|
|
|
tc.parent shouldNotBe null
|
|
|
|
number.parent shouldNotBe null
|
|
|
|
tc shouldBeSameInstanceAs number.parent
|
2021-11-01 23:47:01 +00:00
|
|
|
val constvalue = tc.constValue(program)!!
|
2022-02-10 23:21:40 +00:00
|
|
|
constvalue shouldBe instanceOf<NumericLiteral>()
|
2021-11-19 23:15:04 +00:00
|
|
|
constvalue.number shouldBe 11.0
|
2021-11-08 14:50:29 +00:00
|
|
|
constvalue.type shouldBe DataType.BYTE
|
2021-11-19 23:15:04 +00:00
|
|
|
constvalue.parent shouldBeSameInstanceAs tc.parent
|
|
|
|
}
|
|
|
|
|
|
|
|
test("generated constvalue from prefixexpr inherits proper parent linkage") {
|
2024-09-01 18:19:24 +00:00
|
|
|
val number = NumericLiteral(BaseDataType.UBYTE, 11.0, Position.DUMMY)
|
2022-03-10 21:38:16 +00:00
|
|
|
val pfx = PrefixExpression("-", number, Position.DUMMY)
|
2021-11-19 23:15:04 +00:00
|
|
|
val program = Program("test", DummyFunctions, DummyMemsizer, DummyStringEncoder)
|
|
|
|
pfx.linkParents(ParentSentinel)
|
|
|
|
pfx.parent shouldNotBe null
|
|
|
|
number.parent shouldNotBe null
|
|
|
|
pfx shouldBeSameInstanceAs number.parent
|
|
|
|
val constvalue = pfx.constValue(program)!!
|
2022-02-10 23:21:40 +00:00
|
|
|
constvalue shouldBe instanceOf<NumericLiteral>()
|
2021-11-19 23:15:04 +00:00
|
|
|
constvalue.number shouldBe -11.0
|
|
|
|
constvalue.type shouldBe DataType.BYTE
|
|
|
|
constvalue.parent shouldBeSameInstanceAs pfx.parent
|
2021-11-01 23:47:01 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 14:02:21 +00:00
|
|
|
test("various 'not' operator rewrites even without optimizations") {
|
2021-11-12 22:23:51 +00:00
|
|
|
val src = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
2024-01-04 14:02:21 +00:00
|
|
|
bool @shared a1
|
|
|
|
bool @shared a2
|
2024-02-04 22:41:01 +00:00
|
|
|
a1 = not a1 ; a1 = not a1
|
2024-01-04 14:02:21 +00:00
|
|
|
a1 = not not a1 ; a1 = a1, so removed totally
|
2024-02-04 22:41:01 +00:00
|
|
|
a1 = not not not a1 ; a1 = not a1
|
|
|
|
a1 = not a1 or not a2 ; a1 = not (a1 and a2)
|
|
|
|
a1 = not a1 and not a2 ; a1 = not (a1 or a2)
|
2021-11-12 22:23:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
2022-07-11 10:22:14 +00:00
|
|
|
val result = compileText(C64Target(), false, src, writeAssembly = true)!!
|
2023-02-09 00:46:23 +00:00
|
|
|
val stmts = result.compilerAst.entrypoint.statements
|
2024-01-04 14:02:21 +00:00
|
|
|
stmts.size shouldBe 9
|
|
|
|
|
2024-02-04 22:41:01 +00:00
|
|
|
val value1 = (stmts[4] as Assignment).value as PrefixExpression
|
|
|
|
val value2 = (stmts[5] as Assignment).value as PrefixExpression
|
|
|
|
val value3 = (stmts[6] as Assignment).value as PrefixExpression
|
|
|
|
val value4 = (stmts[7] as Assignment).value as PrefixExpression
|
|
|
|
value1.operator shouldBe "not"
|
|
|
|
value2.operator shouldBe "not"
|
|
|
|
value3.operator shouldBe "not"
|
|
|
|
value4.operator shouldBe "not"
|
|
|
|
value1.expression shouldBe instanceOf<IdentifierReference>()
|
|
|
|
value2.expression shouldBe instanceOf<IdentifierReference>()
|
|
|
|
(value3.expression as BinaryExpression).operator shouldBe "and"
|
|
|
|
(value4.expression as BinaryExpression).operator shouldBe "or"
|
2024-01-04 14:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test("various 'not' operator rewrites with optimizations") {
|
|
|
|
val src = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
bool @shared a1
|
|
|
|
bool @shared a2
|
2024-02-04 22:41:01 +00:00
|
|
|
a1 = not a1 ; a1 = not a1
|
|
|
|
a1 = not not a1 ; a1 = a1, so removed totally
|
|
|
|
a1 = not not not a1 ; a1 = not a1
|
|
|
|
a1 = not a1 or not a2 ; a1 = not (a1 and a2)
|
|
|
|
a1 = not a1 and not a2 ; a1 = not (a1 or a2)
|
2024-01-04 14:02:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
val result = compileText(C64Target(), true, src, writeAssembly = true)!!
|
|
|
|
val stmts = result.compilerAst.entrypoint.statements
|
|
|
|
stmts.size shouldBe 9
|
2021-11-12 22:23:51 +00:00
|
|
|
|
2024-02-04 22:41:01 +00:00
|
|
|
val value1 = (stmts[4] as Assignment).value as PrefixExpression
|
|
|
|
val value2 = (stmts[5] as Assignment).value as PrefixExpression
|
|
|
|
val value3 = (stmts[6] as Assignment).value as PrefixExpression
|
|
|
|
val value4 = (stmts[7] as Assignment).value as PrefixExpression
|
|
|
|
value1.operator shouldBe "not"
|
|
|
|
value2.operator shouldBe "not"
|
|
|
|
value3.operator shouldBe "not"
|
|
|
|
value4.operator shouldBe "not"
|
|
|
|
value1.expression shouldBe instanceOf<IdentifierReference>()
|
|
|
|
value2.expression shouldBe instanceOf<IdentifierReference>()
|
|
|
|
(value3.expression as BinaryExpression).operator shouldBe "and"
|
|
|
|
(value4.expression as BinaryExpression).operator shouldBe "or"
|
2021-11-09 02:45:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test("asmgen correctly deals with float typecasting in augmented assignment") {
|
|
|
|
val src="""
|
2022-02-06 21:56:17 +00:00
|
|
|
%import floats
|
2021-11-09 02:45:07 +00:00
|
|
|
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
ubyte ub
|
2021-11-17 23:17:22 +00:00
|
|
|
float ff = 1.0
|
2021-11-09 02:45:07 +00:00
|
|
|
ff += (ub as float) ; operator doesn't matter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
2022-03-07 20:41:12 +00:00
|
|
|
val result = compileText(C64Target(), optimize=false, src, writeAssembly = false)!!
|
2023-02-09 00:46:23 +00:00
|
|
|
val assignFF = result.compilerAst.entrypoint.statements.last() as Assignment
|
2021-11-17 23:17:22 +00:00
|
|
|
assignFF.isAugmentable shouldBe true
|
|
|
|
assignFF.target.identifier!!.nameInSource shouldBe listOf("ff")
|
|
|
|
val value = assignFF.value as BinaryExpression
|
2021-11-09 02:45:07 +00:00
|
|
|
value.operator shouldBe "+"
|
2022-01-27 22:32:55 +00:00
|
|
|
(value.left as? IdentifierReference)?.nameInSource shouldBe listOf("ff")
|
2021-11-09 02:45:07 +00:00
|
|
|
value.right shouldBe instanceOf<TypecastExpression>()
|
|
|
|
|
2022-03-07 20:41:12 +00:00
|
|
|
compileText(C64Target(), optimize=false, src, writeAssembly = true) shouldNotBe null
|
2021-11-09 00:13:23 +00:00
|
|
|
}
|
2021-11-11 02:03:21 +00:00
|
|
|
|
|
|
|
test("unused variable removal") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
2022-01-09 16:08:50 +00:00
|
|
|
ubyte unused ; removed
|
2021-11-11 02:03:21 +00:00
|
|
|
ubyte @shared unused_but_shared ; this one should remain
|
2022-01-09 16:08:50 +00:00
|
|
|
ubyte usedvar_only_written ; not removed because has multiple assignments
|
2021-11-11 02:03:21 +00:00
|
|
|
usedvar_only_written=2
|
|
|
|
usedvar_only_written++
|
2022-01-09 16:08:50 +00:00
|
|
|
ubyte usedvar ; and this one remains too
|
2021-11-11 02:03:21 +00:00
|
|
|
usedvar = msb(usedvar)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
2022-03-07 20:41:12 +00:00
|
|
|
val result = compileText(C64Target(), optimize=true, src, writeAssembly=false)!!
|
2023-02-09 00:46:23 +00:00
|
|
|
result.compilerAst.entrypoint.statements.size shouldBe 7
|
|
|
|
val alldecls = result.compilerAst.entrypoint.allDefinedSymbols.toList()
|
2022-01-09 16:08:50 +00:00
|
|
|
alldecls.map { it.first } shouldBe listOf("unused_but_shared", "usedvar_only_written", "usedvar")
|
2021-11-11 02:03:21 +00:00
|
|
|
}
|
2021-11-13 13:22:37 +00:00
|
|
|
|
|
|
|
test("unused variable removal from subscope") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
2024-02-04 22:41:01 +00:00
|
|
|
if cx16.r0!=0 {
|
2022-01-09 16:08:50 +00:00
|
|
|
uword xx ; to be removed
|
2021-11-13 13:22:37 +00:00
|
|
|
cx16.r0 = 0
|
|
|
|
}
|
|
|
|
func2()
|
|
|
|
|
|
|
|
sub func2() {
|
2022-01-09 16:08:50 +00:00
|
|
|
uword yy ; to be removed
|
2021-11-13 13:22:37 +00:00
|
|
|
yy=99 ; to be removed
|
|
|
|
cx16.r0 = 0
|
2024-02-06 17:50:08 +00:00
|
|
|
rol(cx16.r0)
|
2021-11-13 13:22:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}"""
|
2022-03-07 20:41:12 +00:00
|
|
|
val result = compileText(C64Target(), optimize=true, src, writeAssembly=false)!!
|
2023-02-09 00:46:23 +00:00
|
|
|
result.compilerAst.entrypoint.statements.size shouldBe 3
|
|
|
|
val ifstmt = result.compilerAst.entrypoint.statements[0] as IfElse
|
2021-11-13 13:22:37 +00:00
|
|
|
ifstmt.truepart.statements.size shouldBe 1
|
|
|
|
(ifstmt.truepart.statements[0] as Assignment).target.identifier!!.nameInSource shouldBe listOf("cx16", "r0")
|
2023-02-09 00:46:23 +00:00
|
|
|
val func2 = result.compilerAst.entrypoint.statements[2] as Subroutine
|
2023-03-07 22:47:14 +00:00
|
|
|
func2.statements.size shouldBe 2
|
2021-11-13 13:22:37 +00:00
|
|
|
(func2.statements[0] as Assignment).target.identifier!!.nameInSource shouldBe listOf("cx16", "r0")
|
|
|
|
}
|
2021-11-17 21:34:34 +00:00
|
|
|
|
2024-02-10 20:17:36 +00:00
|
|
|
test("unused subroutine removal") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub strip() {
|
|
|
|
lstrip()
|
|
|
|
}
|
|
|
|
|
|
|
|
sub lstrip() {
|
|
|
|
lstripped()
|
|
|
|
}
|
|
|
|
|
|
|
|
sub lstripped() {
|
|
|
|
cx16.r0++
|
|
|
|
evenmore()
|
|
|
|
cx16.r1++
|
|
|
|
}
|
|
|
|
|
|
|
|
sub evenmore() {
|
|
|
|
cx16.r1++
|
|
|
|
cx16.r2++
|
|
|
|
}
|
|
|
|
|
|
|
|
sub start() {
|
|
|
|
; nothing
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val result = compileText(C64Target(), optimize=true, src, writeAssembly=false)!!
|
|
|
|
result.compilerAst.entrypoint.statements.size shouldBe 0
|
|
|
|
result.compilerAst.entrypoint.definingScope.statements.size shouldBe 1
|
|
|
|
}
|
|
|
|
|
2021-11-17 21:34:34 +00:00
|
|
|
test("test simple augmented assignment optimization correctly initializes all variables") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
2021-11-17 23:17:22 +00:00
|
|
|
ubyte @shared z1
|
2021-11-17 21:34:34 +00:00
|
|
|
z1 = 10
|
2021-11-17 23:17:22 +00:00
|
|
|
ubyte @shared z2
|
2021-11-20 16:33:02 +00:00
|
|
|
z2 = ~z2
|
2024-02-04 22:41:01 +00:00
|
|
|
bool @shared z3
|
2021-11-20 16:33:02 +00:00
|
|
|
z3 = not z3
|
|
|
|
uword @shared z4
|
|
|
|
z4 = (z4 as ubyte)
|
|
|
|
ubyte @shared z5
|
|
|
|
z5 = z1+z5+5
|
|
|
|
ubyte @shared z6
|
|
|
|
z6 = z1+z6-5
|
2021-11-17 21:34:34 +00:00
|
|
|
}
|
|
|
|
}"""
|
2022-03-07 20:41:12 +00:00
|
|
|
val result = compileText(C64Target(), optimize=true, src, writeAssembly=false)!!
|
2021-11-17 21:34:34 +00:00
|
|
|
/* expected:
|
|
|
|
ubyte z1
|
|
|
|
z1 = 10
|
|
|
|
ubyte z2
|
2021-11-20 16:33:02 +00:00
|
|
|
z2 = 255
|
2024-02-04 22:41:01 +00:00
|
|
|
bool z3
|
|
|
|
z3 = true
|
2021-11-20 16:33:02 +00:00
|
|
|
uword z4
|
|
|
|
z4 = 0
|
|
|
|
ubyte z5
|
2023-07-19 23:36:43 +00:00
|
|
|
z5 = z1 + 5
|
2021-11-20 16:33:02 +00:00
|
|
|
ubyte z6
|
2023-07-19 23:36:43 +00:00
|
|
|
z6 = z1 - 5
|
2021-11-20 16:33:02 +00:00
|
|
|
*/
|
2023-02-09 00:46:23 +00:00
|
|
|
val statements = result.compilerAst.entrypoint.statements
|
2023-07-19 23:36:43 +00:00
|
|
|
statements.size shouldBe 12
|
2021-11-17 21:34:34 +00:00
|
|
|
val z1decl = statements[0] as VarDecl
|
|
|
|
val z1init = statements[1] as Assignment
|
|
|
|
val z2decl = statements[2] as VarDecl
|
|
|
|
val z2init = statements[3] as Assignment
|
2021-11-20 16:33:02 +00:00
|
|
|
val z3decl = statements[4] as VarDecl
|
|
|
|
val z3init = statements[5] as Assignment
|
2022-06-28 01:38:13 +00:00
|
|
|
val z4decl = statements[6] as VarDecl
|
|
|
|
val z4init = statements[7] as Assignment
|
|
|
|
val z5decl = statements[8] as VarDecl
|
|
|
|
val z5init = statements[9] as Assignment
|
2023-07-19 23:36:43 +00:00
|
|
|
val z6decl = statements[10] as VarDecl
|
|
|
|
val z6init = statements[11] as Assignment
|
2021-11-17 21:34:34 +00:00
|
|
|
|
|
|
|
z1decl.name shouldBe "z1"
|
2024-09-01 18:19:24 +00:00
|
|
|
z1init.value shouldBe NumericLiteral(BaseDataType.UBYTE, 10.0, Position.DUMMY)
|
2021-11-17 21:34:34 +00:00
|
|
|
z2decl.name shouldBe "z2"
|
2024-09-01 18:19:24 +00:00
|
|
|
z2init.value shouldBe NumericLiteral(BaseDataType.UBYTE, 255.0, Position.DUMMY)
|
2021-11-17 23:17:22 +00:00
|
|
|
z3decl.name shouldBe "z3"
|
2024-09-01 18:19:24 +00:00
|
|
|
z3init.value shouldBe NumericLiteral(BaseDataType.BOOL, 1.0, Position.DUMMY)
|
2021-11-20 16:33:02 +00:00
|
|
|
z4decl.name shouldBe "z4"
|
2024-09-01 18:19:24 +00:00
|
|
|
z4init.value shouldBe NumericLiteral(BaseDataType.UWORD, 0.0, Position.DUMMY)
|
2021-11-20 16:33:02 +00:00
|
|
|
z5decl.name shouldBe "z5"
|
2023-07-19 23:36:43 +00:00
|
|
|
(z5init.value as BinaryExpression).operator shouldBe "+"
|
2024-09-01 18:19:24 +00:00
|
|
|
(z5init.value as BinaryExpression).right shouldBe NumericLiteral(BaseDataType.UBYTE, 5.0, Position.DUMMY)
|
2021-11-20 16:33:02 +00:00
|
|
|
z6decl.name shouldBe "z6"
|
2023-07-19 23:36:43 +00:00
|
|
|
(z6init.value as BinaryExpression).operator shouldBe "-"
|
2024-09-01 18:19:24 +00:00
|
|
|
(z6init.value as BinaryExpression).right shouldBe NumericLiteral(BaseDataType.UBYTE, 5.0, Position.DUMMY)
|
2021-11-17 21:34:34 +00:00
|
|
|
}
|
2021-11-18 22:55:20 +00:00
|
|
|
|
|
|
|
test("force_output option should work with optimizing memwrite assignment") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
%option force_output
|
|
|
|
|
|
|
|
sub start() {
|
2023-12-29 18:48:40 +00:00
|
|
|
uword @shared aa
|
|
|
|
ubyte @shared zz
|
2021-11-18 22:55:20 +00:00
|
|
|
@(aa) = zz + 32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2022-03-07 20:41:12 +00:00
|
|
|
val result = compileText(C64Target(), optimize=true, src, writeAssembly=false)!!
|
2023-02-09 00:46:23 +00:00
|
|
|
val stmts = result.compilerAst.entrypoint.statements
|
2023-07-19 23:36:43 +00:00
|
|
|
stmts.size shouldBe 5
|
2021-11-19 21:49:35 +00:00
|
|
|
val assign=stmts.last() as Assignment
|
|
|
|
(assign.target.memoryAddress?.addressExpression as IdentifierReference).nameInSource shouldBe listOf("aa")
|
2021-11-18 22:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test("don't optimize memory writes away") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
2023-12-28 04:17:15 +00:00
|
|
|
uword @shared aa
|
|
|
|
ubyte @shared zz
|
2021-11-18 22:55:20 +00:00
|
|
|
@(aa) = zz + 32 ; do not optimize this away!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
2022-03-07 20:41:12 +00:00
|
|
|
val result = compileText(C64Target(), optimize=true, src, writeAssembly=false)!!
|
2023-02-09 00:46:23 +00:00
|
|
|
val stmts = result.compilerAst.entrypoint.statements
|
2023-07-19 23:36:43 +00:00
|
|
|
stmts.size shouldBe 5
|
2021-11-19 21:49:35 +00:00
|
|
|
val assign=stmts.last() as Assignment
|
|
|
|
(assign.target.memoryAddress?.addressExpression as IdentifierReference).nameInSource shouldBe listOf("aa")
|
2021-11-18 22:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test("correctly process constant prefix numbers") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
2021-11-19 21:49:35 +00:00
|
|
|
ubyte @shared z1 = 1
|
2021-11-19 23:15:04 +00:00
|
|
|
ubyte @shared z2 = + 1
|
|
|
|
ubyte @shared z3 = ~ 1
|
2024-02-04 22:41:01 +00:00
|
|
|
bool @shared z4 = not 1
|
2021-11-19 21:49:35 +00:00
|
|
|
byte @shared z5 = - 1
|
2021-11-18 22:55:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
2022-03-07 20:41:12 +00:00
|
|
|
val result = compileText(C64Target(), optimize=true, src, writeAssembly=false)!!
|
2023-02-09 00:46:23 +00:00
|
|
|
val stmts = result.compilerAst.entrypoint.statements
|
2021-11-19 21:49:35 +00:00
|
|
|
stmts.size shouldBe 10
|
|
|
|
stmts.filterIsInstance<VarDecl>().size shouldBe 5
|
|
|
|
stmts.filterIsInstance<Assignment>().size shouldBe 5
|
2021-11-18 22:55:20 +00:00
|
|
|
}
|
2021-11-19 23:15:04 +00:00
|
|
|
|
|
|
|
test("correctly process constant prefix numbers with type mismatch and give error") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
2022-06-05 12:21:10 +00:00
|
|
|
ubyte @shared z1 = - 200
|
2021-11-19 23:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
val errors = ErrorReporterForTests()
|
2022-06-05 12:21:10 +00:00
|
|
|
compileText(C64Target(), optimize=false, src, writeAssembly=false, errors = errors) shouldBe null
|
2024-02-04 22:41:01 +00:00
|
|
|
errors.errors.size shouldBe 2
|
2023-12-26 17:49:01 +00:00
|
|
|
errors.errors[0] shouldContain "out of range"
|
2024-02-04 22:41:01 +00:00
|
|
|
errors.errors[1] shouldContain "cannot assign word to byte"
|
2021-11-19 23:15:04 +00:00
|
|
|
}
|
2021-11-20 00:21:33 +00:00
|
|
|
|
2022-06-05 12:21:10 +00:00
|
|
|
test("out of range cast should give error") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
ubyte @shared z1 = - 200 as ubyte
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
val errors = ErrorReporterForTests()
|
|
|
|
compileText(C64Target(), optimize=false, src, writeAssembly=false, errors = errors) shouldBe null
|
|
|
|
errors.errors.size shouldBe 1
|
2023-12-15 21:05:57 +00:00
|
|
|
errors.errors[0] shouldContain "no cast"
|
2022-06-05 12:21:10 +00:00
|
|
|
}
|
|
|
|
|
2021-11-20 00:21:33 +00:00
|
|
|
test("test augmented expression asmgen") {
|
|
|
|
val src = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
ubyte c
|
|
|
|
ubyte r
|
|
|
|
ubyte q
|
|
|
|
r = (q+r)-c
|
|
|
|
q=r
|
|
|
|
r = q+(r-c)
|
|
|
|
q=r
|
|
|
|
}
|
|
|
|
}"""
|
2022-03-07 20:41:12 +00:00
|
|
|
val result = compileText(C64Target(), optimize=false, src, writeAssembly=true)!!
|
2023-02-09 00:46:23 +00:00
|
|
|
result.compilerAst.entrypoint.statements.size shouldBe 11
|
|
|
|
result.compilerAst.entrypoint.statements.last() shouldBe instanceOf<Return>()
|
2021-11-20 00:21:33 +00:00
|
|
|
}
|
2021-11-20 16:33:02 +00:00
|
|
|
|
|
|
|
test("keep the value initializer assignment if the next one depends on it") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
uword @shared yy
|
|
|
|
yy = 20 ; ok to remove =0 initializer before this
|
|
|
|
uword @shared zz
|
|
|
|
zz += 60 ; NOT ok to remove initializer, should evaluate to 60
|
|
|
|
ubyte @shared xx
|
2023-04-29 20:11:26 +00:00
|
|
|
xx = 6+lsb(mkword(xx,22)) ; is not an initializer because it references xx
|
2021-11-20 16:33:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
2022-03-07 20:41:12 +00:00
|
|
|
val result = compileText(C64Target(), optimize=true, src, writeAssembly=false)!!
|
2021-11-20 16:33:02 +00:00
|
|
|
/* expected result:
|
|
|
|
uword yy
|
|
|
|
yy = 20
|
|
|
|
uword zz
|
|
|
|
zz = 60
|
|
|
|
ubyte xx
|
2022-01-10 23:34:44 +00:00
|
|
|
xx = 0
|
2022-04-12 21:58:19 +00:00
|
|
|
xx = abs(xx)
|
2021-11-20 16:33:02 +00:00
|
|
|
xx += 6
|
|
|
|
*/
|
2023-02-09 00:46:23 +00:00
|
|
|
val stmts = result.compilerAst.entrypoint.statements
|
2023-07-19 23:36:43 +00:00
|
|
|
stmts.size shouldBe 7
|
2021-11-20 16:33:02 +00:00
|
|
|
stmts.filterIsInstance<VarDecl>().size shouldBe 3
|
2023-07-19 23:36:43 +00:00
|
|
|
stmts.filterIsInstance<Assignment>().size shouldBe 4
|
2021-11-20 16:33:02 +00:00
|
|
|
}
|
2021-11-21 11:34:57 +00:00
|
|
|
|
|
|
|
test("only substitue assignments with 0 after a =0 initializer if it is the same variable") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
uword @shared xx
|
|
|
|
xx = xx + 20 ; is same var so can be changed just fine into xx=20
|
|
|
|
uword @shared yy
|
|
|
|
xx = 20
|
|
|
|
yy = 0 ; is other var..
|
|
|
|
xx = xx+10 ; so this should not be changed into xx=10
|
|
|
|
}
|
|
|
|
}"""
|
2022-03-07 20:41:12 +00:00
|
|
|
val result = compileText(C64Target(), optimize=true, src, writeAssembly=false)!!
|
2021-11-21 11:34:57 +00:00
|
|
|
/*
|
|
|
|
expected result:
|
|
|
|
uword xx
|
|
|
|
xx = 20
|
|
|
|
uword yy
|
|
|
|
yy = 0
|
|
|
|
xx = 20
|
|
|
|
yy = 0
|
|
|
|
xx += 10
|
|
|
|
*/
|
2023-02-09 00:46:23 +00:00
|
|
|
val stmts = result.compilerAst.entrypoint.statements
|
2021-11-21 11:34:57 +00:00
|
|
|
stmts.size shouldBe 7
|
|
|
|
stmts.filterIsInstance<VarDecl>().size shouldBe 2
|
|
|
|
stmts.filterIsInstance<Assignment>().size shouldBe 5
|
|
|
|
val assignXX1 = stmts[1] as Assignment
|
|
|
|
assignXX1.target.identifier!!.nameInSource shouldBe listOf("xx")
|
2024-09-01 18:19:24 +00:00
|
|
|
assignXX1.value shouldBe NumericLiteral(BaseDataType.UWORD, 20.0, Position.DUMMY)
|
2021-11-21 11:34:57 +00:00
|
|
|
val assignXX2 = stmts.last() as Assignment
|
|
|
|
assignXX2.target.identifier!!.nameInSource shouldBe listOf("xx")
|
|
|
|
val xxValue = assignXX2.value as BinaryExpression
|
|
|
|
xxValue.operator shouldBe "+"
|
2022-01-27 22:32:55 +00:00
|
|
|
(xxValue.left as? IdentifierReference)?.nameInSource shouldBe listOf("xx")
|
2024-09-01 18:19:24 +00:00
|
|
|
xxValue.right shouldBe NumericLiteral(BaseDataType.UWORD, 10.0, Position.DUMMY)
|
2021-11-21 11:34:57 +00:00
|
|
|
}
|
2022-05-08 11:21:34 +00:00
|
|
|
|
|
|
|
test("multi-comparison replaced by containment check") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
2023-12-28 04:17:15 +00:00
|
|
|
ubyte @shared source=99
|
|
|
|
ubyte @shared thingy=42
|
2022-05-08 11:21:34 +00:00
|
|
|
|
|
|
|
if source==3 or source==4 or source==99 or source==1
|
|
|
|
thingy++
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val result = compileText(C64Target(), optimize=true, src, writeAssembly=false)!!
|
|
|
|
/*
|
|
|
|
expected result:
|
|
|
|
ubyte[] auto_heap_var = [1,4,99,3]
|
|
|
|
ubyte source
|
|
|
|
source = 99
|
|
|
|
ubyte thingy
|
|
|
|
thingy = 42
|
|
|
|
if source in auto_heap_var
|
|
|
|
thingy++
|
|
|
|
*/
|
2023-02-09 00:46:23 +00:00
|
|
|
val stmts = result.compilerAst.entrypoint.statements
|
2022-05-08 11:21:34 +00:00
|
|
|
stmts.size shouldBe 6
|
|
|
|
val ifStmt = stmts[5] as IfElse
|
|
|
|
val containment = ifStmt.condition as ContainmentCheck
|
|
|
|
(containment.element as IdentifierReference).nameInSource shouldBe listOf("source")
|
|
|
|
(containment.iterable as IdentifierReference).nameInSource.single() shouldStartWith "auto_heap_value"
|
|
|
|
val arrayDecl = stmts[0] as VarDecl
|
|
|
|
arrayDecl.isArray shouldBe true
|
|
|
|
arrayDecl.arraysize?.constIndex() shouldBe 4
|
|
|
|
val arrayValue = arrayDecl.value as ArrayLiteral
|
|
|
|
arrayValue.type shouldBe InferredTypes.InferredType.known(DataType.ARRAY_UB)
|
|
|
|
arrayValue.value shouldBe listOf(
|
|
|
|
NumericLiteral.optimalInteger(1, Position.DUMMY),
|
|
|
|
NumericLiteral.optimalInteger(3, Position.DUMMY),
|
|
|
|
NumericLiteral.optimalInteger(4, Position.DUMMY),
|
|
|
|
NumericLiteral.optimalInteger(99, Position.DUMMY))
|
|
|
|
}
|
|
|
|
|
|
|
|
test("invalid multi-comparison (not all equals) not replaced") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
2023-12-28 04:17:15 +00:00
|
|
|
ubyte @shared source=99
|
|
|
|
ubyte @shared thingy=42
|
2022-05-08 11:21:34 +00:00
|
|
|
|
|
|
|
if source==3 or source==4 or source!=99 or source==1
|
|
|
|
thingy++
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val result = compileText(C64Target(), optimize=true, src, writeAssembly=false)!!
|
2023-02-09 00:46:23 +00:00
|
|
|
val stmts = result.compilerAst.entrypoint.statements
|
2022-05-08 11:21:34 +00:00
|
|
|
stmts.size shouldBe 5
|
|
|
|
val ifStmt = stmts[4] as IfElse
|
|
|
|
ifStmt.condition shouldBe instanceOf<BinaryExpression>()
|
|
|
|
}
|
|
|
|
|
|
|
|
test("invalid multi-comparison (not all same needle) not replaced") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
2023-12-28 04:17:15 +00:00
|
|
|
ubyte @shared source=99
|
|
|
|
ubyte @shared thingy=42
|
2022-05-08 11:21:34 +00:00
|
|
|
|
|
|
|
if source==3 or source==4 or thingy==99 or source==1
|
|
|
|
thingy++
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val result = compileText(C64Target(), optimize=true, src, writeAssembly=false)!!
|
2023-02-09 00:46:23 +00:00
|
|
|
val stmts = result.compilerAst.entrypoint.statements
|
2022-05-08 11:21:34 +00:00
|
|
|
stmts.size shouldBe 5
|
|
|
|
val ifStmt = stmts[4] as IfElse
|
|
|
|
ifStmt.condition shouldBe instanceOf<BinaryExpression>()
|
|
|
|
}
|
|
|
|
|
|
|
|
test("invalid multi-comparison (not all or) not replaced") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
2023-12-28 04:17:15 +00:00
|
|
|
ubyte @shared source=99
|
|
|
|
ubyte @shared thingy=42
|
2022-05-08 11:21:34 +00:00
|
|
|
|
|
|
|
if source==3 or source==4 and source==99 or source==1
|
|
|
|
thingy++
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val result = compileText(C64Target(), optimize=true, src, writeAssembly=false)!!
|
2023-02-09 00:46:23 +00:00
|
|
|
val stmts = result.compilerAst.entrypoint.statements
|
2022-05-08 11:21:34 +00:00
|
|
|
stmts.size shouldBe 5
|
|
|
|
val ifStmt = stmts[4] as IfElse
|
|
|
|
ifStmt.condition shouldBe instanceOf<BinaryExpression>()
|
|
|
|
}
|
2022-06-04 10:46:32 +00:00
|
|
|
|
|
|
|
test("pointer indexing inside other expression ok") {
|
|
|
|
val src="""
|
|
|
|
main{
|
|
|
|
sub start () {
|
2023-12-28 04:17:15 +00:00
|
|
|
uword @shared eRef
|
2024-02-04 22:41:01 +00:00
|
|
|
if eRef[3] & 10 ==0 {
|
2022-06-04 10:46:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val result = compileText(C64Target(), optimize=true, src, writeAssembly=false)!!
|
2023-02-09 00:46:23 +00:00
|
|
|
val stmts = result.compilerAst.entrypoint.statements
|
2022-06-04 10:46:32 +00:00
|
|
|
stmts.size shouldBe 3
|
|
|
|
}
|
2022-12-24 17:01:54 +00:00
|
|
|
|
|
|
|
test("repeated assignments to IO register should remain") {
|
|
|
|
val srcX16="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
ubyte @shared xx
|
|
|
|
xx = 42
|
|
|
|
xx = 42 ; removed
|
|
|
|
xx = 42 ; removed
|
|
|
|
cx16.VERA_DATA0 = 0
|
|
|
|
cx16.VERA_DATA0 = 0
|
|
|
|
cx16.VERA_DATA0 = 0
|
|
|
|
@($9fff) = 0
|
|
|
|
@($9fff) = 0
|
|
|
|
@($9fff) = 0
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
var result = compileText(Cx16Target(), true, srcX16, writeAssembly = true)!!
|
2023-02-09 00:46:23 +00:00
|
|
|
var statements = result.compilerAst.entrypoint.statements
|
2022-12-24 17:01:54 +00:00
|
|
|
statements.size shouldBe 9
|
|
|
|
(statements[1] as Assignment).target.identifier!!.nameInSource shouldBe listOf("xx")
|
|
|
|
(statements[2] as Assignment).target.identifier!!.nameInSource shouldBe listOf("cx16", "VERA_DATA0")
|
|
|
|
(statements[3] as Assignment).target.identifier!!.nameInSource shouldBe listOf("cx16", "VERA_DATA0")
|
|
|
|
(statements[4] as Assignment).target.identifier!!.nameInSource shouldBe listOf("cx16", "VERA_DATA0")
|
2023-02-09 00:46:23 +00:00
|
|
|
(statements[5] as Assignment).target.memoryAddress!!.addressExpression.constValue(result.compilerAst)!!.number shouldBe 0x9fff
|
|
|
|
(statements[6] as Assignment).target.memoryAddress!!.addressExpression.constValue(result.compilerAst)!!.number shouldBe 0x9fff
|
|
|
|
(statements[7] as Assignment).target.memoryAddress!!.addressExpression.constValue(result.compilerAst)!!.number shouldBe 0x9fff
|
2022-12-24 17:01:54 +00:00
|
|
|
|
|
|
|
val srcC64="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
ubyte @shared xx
|
|
|
|
xx = 42
|
|
|
|
xx = 42 ;removed
|
|
|
|
xx = 42 ;removed
|
|
|
|
c64.EXTCOL = 0
|
|
|
|
c64.EXTCOL = 0
|
|
|
|
c64.EXTCOL = 0
|
|
|
|
@(53281) = 0
|
|
|
|
@(53281) = 0
|
|
|
|
@(53281) = 0
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
result = compileText(C64Target(), true, srcC64, writeAssembly = true)!!
|
2023-02-09 00:46:23 +00:00
|
|
|
statements = result.compilerAst.entrypoint.statements
|
2022-12-24 17:01:54 +00:00
|
|
|
statements.size shouldBe 9
|
|
|
|
(statements[1] as Assignment).target.identifier!!.nameInSource shouldBe listOf("xx")
|
|
|
|
(statements[2] as Assignment).target.identifier!!.nameInSource shouldBe listOf("c64", "EXTCOL")
|
|
|
|
(statements[3] as Assignment).target.identifier!!.nameInSource shouldBe listOf("c64", "EXTCOL")
|
|
|
|
(statements[4] as Assignment).target.identifier!!.nameInSource shouldBe listOf("c64", "EXTCOL")
|
2023-02-09 00:46:23 +00:00
|
|
|
(statements[5] as Assignment).target.memoryAddress!!.addressExpression.constValue(result.compilerAst)!!.number shouldBe 53281.0
|
|
|
|
(statements[6] as Assignment).target.memoryAddress!!.addressExpression.constValue(result.compilerAst)!!.number shouldBe 53281.0
|
|
|
|
(statements[7] as Assignment).target.memoryAddress!!.addressExpression.constValue(result.compilerAst)!!.number shouldBe 53281.0
|
2022-12-24 17:01:54 +00:00
|
|
|
}
|
2023-02-13 23:26:29 +00:00
|
|
|
|
2023-03-12 17:16:48 +00:00
|
|
|
test("no string error when inlining") {
|
|
|
|
val text="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
test()
|
|
|
|
}
|
|
|
|
|
|
|
|
sub test() {
|
|
|
|
cx16.r0 = "abc"
|
|
|
|
}
|
2023-02-13 23:26:29 +00:00
|
|
|
}"""
|
|
|
|
compileText(C64Target(), true, text, writeAssembly = false) shouldNotBe null
|
|
|
|
}
|
2023-11-17 18:34:19 +00:00
|
|
|
|
|
|
|
test("replacing string print by chrout with referenced string elsewhere shouldn't give string symbol error") {
|
|
|
|
val text="""
|
|
|
|
%import textio
|
|
|
|
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
str key = "test"
|
|
|
|
txt.print(":")
|
|
|
|
if key != ":" {
|
|
|
|
cx16.r0++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
compileText(VMTarget(), true, text, writeAssembly = false) shouldNotBe null
|
|
|
|
}
|
2023-11-21 00:26:32 +00:00
|
|
|
|
|
|
|
test("sub only called by asm should not be optimized away") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
%asm{{
|
2023-12-20 21:20:12 +00:00
|
|
|
jsr p8s_test
|
2023-11-21 00:26:32 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub test() {
|
|
|
|
cx16.r0++
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
compileText(Cx16Target(), true, src, writeAssembly = true) shouldNotBe null
|
|
|
|
}
|
2023-12-04 21:18:37 +00:00
|
|
|
|
|
|
|
test("no crash for making var in removed/inlined subroutine fully scoped") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
test()
|
|
|
|
}
|
|
|
|
|
|
|
|
sub test() {
|
|
|
|
sub nested() {
|
|
|
|
ubyte counter
|
|
|
|
counter++
|
|
|
|
}
|
2024-01-11 21:12:01 +00:00
|
|
|
test2(main.test.nested.counter)
|
2023-12-04 21:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub test2(ubyte value) {
|
|
|
|
value++
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val errors = ErrorReporterForTests()
|
2024-01-11 21:12:01 +00:00
|
|
|
compileText(Cx16Target(), true, src, writeAssembly = false, errors = errors) shouldNotBe null
|
2023-12-04 21:18:37 +00:00
|
|
|
}
|
2023-12-28 12:30:07 +00:00
|
|
|
|
|
|
|
test("var to const") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
ubyte xx=10 ; to const
|
|
|
|
ubyte @shared yy=20 ; remain var
|
|
|
|
cx16.r0L = xx+yy
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val errors = ErrorReporterForTests()
|
|
|
|
val result = compileText(Cx16Target(), true, src, writeAssembly = false, errors = errors)!!
|
|
|
|
val st = result.compilerAst.entrypoint.statements
|
|
|
|
st.size shouldBe 4
|
|
|
|
val xxConst = st[0] as VarDecl
|
|
|
|
xxConst.type shouldBe VarDeclType.CONST
|
|
|
|
xxConst.name shouldBe "xx"
|
|
|
|
(xxConst.value as? NumericLiteral)?.number shouldBe 10.0
|
|
|
|
(st[1] as VarDecl).type shouldBe VarDeclType.VAR
|
|
|
|
val expr = (st[3] as Assignment).value as BinaryExpression
|
|
|
|
(expr.left as? IdentifierReference)?.nameInSource shouldBe listOf("yy")
|
|
|
|
(expr.right as? NumericLiteral)?.number shouldBe 10.0
|
|
|
|
}
|
2023-12-28 18:58:45 +00:00
|
|
|
|
2024-04-16 21:27:22 +00:00
|
|
|
test("var to const inside typecast") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
uword variable ; will be made a const
|
|
|
|
|
|
|
|
sub start() {
|
|
|
|
cx16.r0 = msb(variable) as uword
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
compileText(VMTarget(), true, src, writeAssembly = false) shouldNotBe null
|
|
|
|
}
|
|
|
|
|
2024-01-04 23:26:56 +00:00
|
|
|
test("De Morgan's laws") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
bool @shared a1
|
|
|
|
bool @shared a2
|
|
|
|
|
|
|
|
if not a1 or not a2
|
|
|
|
cx16.r0++
|
|
|
|
if not a1 and not a2
|
|
|
|
cx16.r0++
|
2024-02-20 22:34:28 +00:00
|
|
|
|
|
|
|
if cx16.r0L==42 or not a2
|
|
|
|
cx16.r0L++
|
|
|
|
if not a2 or cx16.r0L==42
|
|
|
|
cx16.r0L++
|
|
|
|
|
2024-01-04 23:26:56 +00:00
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val result = compileText(Cx16Target(), true, src, writeAssembly = false)!!
|
|
|
|
val st = result.compilerAst.entrypoint.statements
|
2024-02-20 22:34:28 +00:00
|
|
|
st.size shouldBe 8
|
2024-01-05 12:32:16 +00:00
|
|
|
val if1c = (st[4] as IfElse).condition as PrefixExpression
|
|
|
|
val if2c = (st[5] as IfElse).condition as PrefixExpression
|
2024-02-20 22:34:28 +00:00
|
|
|
val if3c = (st[6] as IfElse).condition as PrefixExpression
|
|
|
|
val if4c = (st[7] as IfElse).condition as PrefixExpression
|
2024-01-05 12:32:16 +00:00
|
|
|
if1c.operator shouldBe "not"
|
|
|
|
if2c.operator shouldBe "not"
|
2024-02-20 22:34:28 +00:00
|
|
|
if3c.operator shouldBe "not"
|
|
|
|
if4c.operator shouldBe "not"
|
2024-02-04 22:41:01 +00:00
|
|
|
(if1c.expression as BinaryExpression).operator shouldBe "and"
|
|
|
|
(if2c.expression as BinaryExpression).operator shouldBe "or"
|
2024-02-20 22:34:28 +00:00
|
|
|
(if3c.expression as BinaryExpression).operator shouldBe "and"
|
|
|
|
(if4c.expression as BinaryExpression).operator shouldBe "and"
|
2024-01-04 23:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test("absorption laws") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
bool @shared a
|
|
|
|
bool @shared b
|
|
|
|
|
|
|
|
if a or (a and b)
|
|
|
|
cx16.r0 ++
|
|
|
|
if a or (b and a)
|
|
|
|
cx16.r0 ++
|
|
|
|
if a and (a or b)
|
|
|
|
cx16.r0 ++
|
|
|
|
if a and (b or a)
|
|
|
|
cx16.r0 ++
|
|
|
|
|
|
|
|
if a and (b and a)
|
|
|
|
cx16.r0 ++
|
|
|
|
if a or (b or a)
|
|
|
|
cx16.r0 ++
|
2024-02-14 19:58:28 +00:00
|
|
|
if (b and a) and b
|
|
|
|
cx16.r0 ++
|
|
|
|
if (b or a) or b
|
|
|
|
cx16.r0 ++
|
2024-01-04 23:26:56 +00:00
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val result = compileText(Cx16Target(), true, src, writeAssembly = false)!!
|
|
|
|
val st = result.compilerAst.entrypoint.statements
|
2024-02-14 19:58:28 +00:00
|
|
|
st.size shouldBe 12
|
2024-01-04 23:26:56 +00:00
|
|
|
val if1 = st[4] as IfElse
|
|
|
|
val if2 = st[5] as IfElse
|
|
|
|
val if3 = st[6] as IfElse
|
|
|
|
val if4 = st[7] as IfElse
|
|
|
|
(if1.condition as IdentifierReference).nameInSource shouldBe listOf("a")
|
|
|
|
(if2.condition as IdentifierReference).nameInSource shouldBe listOf("a")
|
|
|
|
(if3.condition as IdentifierReference).nameInSource shouldBe listOf("a")
|
|
|
|
(if4.condition as IdentifierReference).nameInSource shouldBe listOf("a")
|
|
|
|
val if5 = st[8] as IfElse
|
|
|
|
val if6 = st[9] as IfElse
|
2024-02-14 19:58:28 +00:00
|
|
|
val if7 = st[10] as IfElse
|
|
|
|
val if8 = st[11] as IfElse
|
|
|
|
val if5bc = if5.condition as BinaryExpression
|
|
|
|
val if6bc = if6.condition as BinaryExpression
|
|
|
|
val if7bc = if7.condition as BinaryExpression
|
|
|
|
val if8bc = if8.condition as BinaryExpression
|
|
|
|
if5bc.left shouldBe instanceOf<IdentifierReference>()
|
|
|
|
if5bc.right shouldBe instanceOf<IdentifierReference>()
|
|
|
|
if6bc.left shouldBe instanceOf<IdentifierReference>()
|
|
|
|
if6bc.right shouldBe instanceOf<IdentifierReference>()
|
|
|
|
if7bc.left shouldBe instanceOf<IdentifierReference>()
|
|
|
|
if7bc.right shouldBe instanceOf<IdentifierReference>()
|
|
|
|
if8bc.left shouldBe instanceOf<IdentifierReference>()
|
|
|
|
if8bc.right shouldBe instanceOf<IdentifierReference>()
|
2024-01-04 23:26:56 +00:00
|
|
|
}
|
2024-01-21 22:05:51 +00:00
|
|
|
|
|
|
|
test("funky bitshifts") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
const uword one = 1
|
|
|
|
const uword two = 2
|
|
|
|
uword @shared answer = one * two >> 8
|
|
|
|
funcw(one * two >> 8)
|
|
|
|
|
|
|
|
const uword uw1 = 99
|
|
|
|
const uword uw2 = 22
|
|
|
|
uword @shared answer2 = uw1 * uw2 >> 8 ; optimized into msb(uw1*uw2) as uword
|
|
|
|
funcw(uw1 * uw2 >> 8)
|
|
|
|
|
|
|
|
uword @shared uw3 = 99
|
|
|
|
uword @shared uw4 = 22
|
|
|
|
uword @shared answer3 = uw3 * uw4 >> 8 ; optimized into msb(uw1*uw2) as uword
|
|
|
|
funcw(uw3 * uw4 >> 8)
|
|
|
|
}
|
|
|
|
|
|
|
|
sub funcw(uword ww) {
|
|
|
|
cx16.r0++
|
|
|
|
}
|
|
|
|
|
|
|
|
}"""
|
|
|
|
|
|
|
|
val result = compileText(Cx16Target(), true, src, writeAssembly = false)!!
|
|
|
|
val st = result.compilerAst.entrypoint.statements
|
|
|
|
st.size shouldBe 17
|
|
|
|
|
|
|
|
val answerValue = (st[3] as Assignment).value
|
2024-09-01 18:19:24 +00:00
|
|
|
answerValue shouldBe NumericLiteral(BaseDataType.UWORD, 0.0, Position.DUMMY)
|
2024-01-21 22:05:51 +00:00
|
|
|
|
|
|
|
val funcarg1 = (st[4] as FunctionCallStatement).args.single()
|
2024-09-01 18:19:24 +00:00
|
|
|
funcarg1 shouldBe NumericLiteral(BaseDataType.UWORD, 0.0, Position.DUMMY)
|
2024-01-21 22:05:51 +00:00
|
|
|
|
|
|
|
val answer2Value = (st[8] as Assignment).value
|
2024-09-01 18:19:24 +00:00
|
|
|
answer2Value shouldBe NumericLiteral(BaseDataType.UWORD, 8.0, Position.DUMMY)
|
2024-01-21 22:05:51 +00:00
|
|
|
|
|
|
|
val funcarg2 = (st[9] as FunctionCallStatement).args.single()
|
2024-09-01 18:19:24 +00:00
|
|
|
funcarg2 shouldBe NumericLiteral(BaseDataType.UWORD, 8.0, Position.DUMMY)
|
2024-01-21 22:05:51 +00:00
|
|
|
|
|
|
|
val answer3ValueTc = (st[15] as Assignment).value as TypecastExpression
|
|
|
|
answer3ValueTc.type shouldBe DataType.UWORD
|
|
|
|
val answer3Value = answer3ValueTc.expression as FunctionCallExpression
|
|
|
|
answer3Value.target.nameInSource shouldBe listOf("msb")
|
|
|
|
answer3Value.args.single() shouldBe instanceOf<BinaryExpression>()
|
|
|
|
|
|
|
|
val funcarg3tc = (st[16] as FunctionCallStatement).args.single() as TypecastExpression
|
|
|
|
funcarg3tc.type shouldBe DataType.UWORD
|
|
|
|
val funcarg3 = funcarg3tc.expression as FunctionCallExpression
|
|
|
|
funcarg3.target.nameInSource shouldBe listOf("msb")
|
|
|
|
funcarg3.args.single() shouldBe instanceOf<BinaryExpression>()
|
|
|
|
}
|
2024-03-20 21:34:39 +00:00
|
|
|
|
|
|
|
test("no operand swap on logical expressions with shortcircuit evaluation") {
|
|
|
|
val src="""
|
|
|
|
%import diskio
|
|
|
|
%zeropage basicsafe
|
|
|
|
%option no_sysinit
|
|
|
|
|
|
|
|
main {
|
|
|
|
str scanline_buf = "?"* 20
|
|
|
|
|
|
|
|
sub start() {
|
|
|
|
if diskio.f_open("test.prg") and diskio.f_read(scanline_buf, 2)==2
|
|
|
|
cx16.r0++
|
|
|
|
|
|
|
|
if diskio.f_open("test.prg") or diskio.f_read(scanline_buf, 2)==2
|
|
|
|
cx16.r0++
|
|
|
|
|
|
|
|
if diskio.f_open("test.prg") xor diskio.f_read(scanline_buf, 2)==2
|
|
|
|
cx16.r0++
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val result = compileText(Cx16Target(), true, src, writeAssembly = false)!!
|
|
|
|
val st = result.compilerAst.entrypoint.statements
|
|
|
|
st.size shouldBe 3
|
|
|
|
val ifCond1 = (st[0] as IfElse).condition as BinaryExpression
|
|
|
|
val ifCond2 = (st[1] as IfElse).condition as BinaryExpression
|
|
|
|
val ifCond3 = (st[2] as IfElse).condition as BinaryExpression
|
|
|
|
(ifCond1.left as FunctionCallExpression).target.nameInSource shouldBe listOf("diskio", "f_open")
|
|
|
|
(ifCond2.left as FunctionCallExpression).target.nameInSource shouldBe listOf("diskio", "f_open")
|
|
|
|
(ifCond3.left as FunctionCallExpression).target.nameInSource shouldBe listOf("diskio", "f_open")
|
|
|
|
val right1 = ifCond1.right as BinaryExpression
|
|
|
|
val right2 = ifCond2.right as BinaryExpression
|
|
|
|
val right3 = ifCond3.right as BinaryExpression
|
|
|
|
(right1.left as FunctionCallExpression).target.nameInSource shouldBe listOf("diskio", "f_read")
|
|
|
|
(right2.left as FunctionCallExpression).target.nameInSource shouldBe listOf("diskio", "f_read")
|
|
|
|
(right3.left as FunctionCallExpression).target.nameInSource shouldBe listOf("diskio", "f_read")
|
|
|
|
}
|
2024-04-06 00:16:21 +00:00
|
|
|
|
|
|
|
test("eliminate same target register assignments") {
|
|
|
|
val src="""
|
|
|
|
%zeropage basicsafe
|
|
|
|
%option no_sysinit
|
|
|
|
|
|
|
|
main {
|
|
|
|
romsub ${'$'}2000 = func1() clobbers(X) -> ubyte @A, word @R0, byte @R1
|
|
|
|
romsub ${'$'}3000 = func2() clobbers(X) -> ubyte @A, uword @R0, uword @R1
|
|
|
|
romsub ${'$'}4000 = func3() clobbers(X) -> ubyte @R0
|
|
|
|
|
|
|
|
sub start() {
|
|
|
|
bool flag
|
|
|
|
void cbm.GETIN()
|
|
|
|
flag, cx16.r1L = cbm.GETIN()
|
|
|
|
void, cx16.r0s, cx16.r1sL = func1()
|
|
|
|
void, cx16.r2, cx16.r1 = func2()
|
|
|
|
cx16.r0L = func3()
|
|
|
|
cx16.r0H = func3()
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val result = compileText(C64Target(), true, src, writeAssembly = true)!!
|
|
|
|
val st = result.codegenAst!!.entrypoint()!!.children
|
|
|
|
st.size shouldBe 9
|
|
|
|
(st[2] as PtFunctionCall).name shouldBe "cbm.GETIN"
|
|
|
|
(st[2] as PtFunctionCall).void shouldBe true
|
|
|
|
val a1 = st[3] as PtAssignment
|
|
|
|
(a1.value as PtFunctionCall).name shouldBe "cbm.GETIN"
|
|
|
|
a1.multiTarget shouldBe true
|
|
|
|
a1.children.size shouldBe 3
|
|
|
|
(a1.children[0] as PtAssignTarget).void shouldBe false
|
|
|
|
(a1.children[0] as PtAssignTarget).identifier!!.name shouldBe "p8b_main.p8s_start.p8v_flag"
|
|
|
|
(a1.children[1] as PtAssignTarget).void shouldBe false
|
|
|
|
(a1.children[1] as PtAssignTarget).identifier!!.name shouldBe "cx16.r1L"
|
|
|
|
|
|
|
|
(st[4] as PtFunctionCall).name shouldBe "p8b_main.p8s_func1"
|
|
|
|
(st[4] as PtFunctionCall).void shouldBe true
|
|
|
|
val a2 = st[5] as PtAssignment
|
|
|
|
(a2.value as PtFunctionCall).name shouldBe "p8b_main.p8s_func2"
|
|
|
|
a2.multiTarget shouldBe true
|
|
|
|
a2.children.size shouldBe 4
|
|
|
|
(a2.children[0] as PtAssignTarget).void shouldBe true
|
|
|
|
(a2.children[1] as PtAssignTarget).void shouldBe false
|
|
|
|
(a2.children[1] as PtAssignTarget).identifier!!.name shouldBe "cx16.r2"
|
|
|
|
(a2.children[2] as PtAssignTarget).void shouldBe true
|
|
|
|
|
|
|
|
(st[6] as PtFunctionCall).name shouldBe "p8b_main.p8s_func3"
|
|
|
|
(st[6] as PtFunctionCall).void shouldBe true
|
|
|
|
val a3 = st[7] as PtAssignment
|
|
|
|
(a3.value as PtFunctionCall).name shouldBe "p8b_main.p8s_func3"
|
|
|
|
a3.multiTarget shouldBe false
|
|
|
|
a3.children.size shouldBe 2
|
|
|
|
(a3.children[0] as PtAssignTarget).void shouldBe false
|
|
|
|
(a3.children[0] as PtAssignTarget).identifier!!.name shouldBe "cx16.r0H"
|
|
|
|
}
|
2024-04-06 10:47:04 +00:00
|
|
|
|
|
|
|
test("symbol table correct in asmgen after earlier optimization steps") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
uword @shared bulletRef, enemyRef
|
|
|
|
const ubyte BD_Y = 10
|
|
|
|
const ubyte EN_Y = 11
|
|
|
|
|
|
|
|
if bulletRef[BD_Y] == enemyRef[EN_Y] or bulletRef[BD_Y] == enemyRef[EN_Y] + 1 {
|
|
|
|
cx16.r0++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
compileText(VMTarget(), true, src, writeAssembly = true) shouldNotBe null
|
|
|
|
compileText(C64Target(), true, src, writeAssembly = true) shouldNotBe null
|
|
|
|
}
|
2024-06-25 21:07:35 +00:00
|
|
|
|
2024-06-29 15:47:13 +00:00
|
|
|
test("optimizing inlined functions must reference proper scopes") {
|
2024-06-25 21:07:35 +00:00
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
2024-06-29 15:47:13 +00:00
|
|
|
void other.sub1()
|
|
|
|
cx16.r0L = other.sub1()+other.sub1()
|
2024-06-25 21:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
other {
|
2024-06-29 15:47:13 +00:00
|
|
|
sub sub2() -> ubyte{
|
2024-06-25 21:07:35 +00:00
|
|
|
cx16.r0++
|
|
|
|
cx16.r1++
|
2024-06-29 15:47:13 +00:00
|
|
|
return cx16.r0L
|
2024-06-25 21:07:35 +00:00
|
|
|
}
|
|
|
|
|
2024-06-29 15:47:13 +00:00
|
|
|
sub sub1() -> ubyte {
|
|
|
|
return sub2()
|
2024-06-25 21:07:35 +00:00
|
|
|
}
|
|
|
|
}"""
|
|
|
|
|
|
|
|
compileText(VMTarget(), true, src, writeAssembly = true) shouldNotBe null
|
|
|
|
compileText(C64Target(), true, src, writeAssembly = true) shouldNotBe null
|
|
|
|
}
|
2021-11-07 23:16:58 +00:00
|
|
|
})
|