2021-10-20 20:16:26 +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
|
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.*
|
2022-06-29 20:19:44 +00:00
|
|
|
import prog8.code.core.DataType
|
|
|
|
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
|
2022-01-08 17:02:38 +00:00
|
|
|
import prog8.compiler.printProgram
|
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)!!
|
2021-10-29 22:25:34 +00:00
|
|
|
val toplevelModule = result.program.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
|
2021-11-08 14:50:29 +00:00
|
|
|
result.program.entrypoint shouldBeSameInstanceAs startSub
|
|
|
|
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)!!
|
2021-10-29 22:25:34 +00:00
|
|
|
val toplevelModule = result.program.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
|
2021-11-08 14:50:29 +00:00
|
|
|
result.program.entrypoint shouldBeSameInstanceAs startSub
|
|
|
|
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
|
|
|
|
2021-11-19 23:15:04 +00:00
|
|
|
test("generated constvalue from typecast inherits proper parent linkage") {
|
2022-03-10 21:38:16 +00:00
|
|
|
val number = NumericLiteral(DataType.UBYTE, 11.0, Position.DUMMY)
|
|
|
|
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") {
|
2022-03-10 21:38:16 +00:00
|
|
|
val number = NumericLiteral(DataType.UBYTE, 11.0, Position.DUMMY)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-12-09 20:13:13 +00:00
|
|
|
test("const folding multiple scenarios +/-") {
|
|
|
|
val source = """
|
|
|
|
main {
|
|
|
|
const ubyte boardHeightC = 20
|
|
|
|
const ubyte boardOffsetC = 3
|
|
|
|
|
|
|
|
sub start() {
|
|
|
|
uword load_location = 12345
|
2021-12-09 21:12:31 +00:00
|
|
|
word llw = 12345
|
2021-12-09 20:13:13 +00:00
|
|
|
cx16.r0 = load_location + 8000 + 1000 + 1000
|
|
|
|
cx16.r2 = 8000 + 1000 + 1000 + load_location
|
|
|
|
cx16.r4 = load_location + boardOffsetC + boardHeightC - 1
|
2021-12-09 21:12:31 +00:00
|
|
|
cx16.r5s = llw - 900 - 999
|
|
|
|
cx16.r7s = llw - 900 + 999
|
2021-12-09 20:13:13 +00:00
|
|
|
}
|
|
|
|
}"""
|
2022-03-07 20:41:12 +00:00
|
|
|
val result = compileText(C64Target(), true, source, writeAssembly = false)!!
|
2021-12-09 20:13:13 +00:00
|
|
|
// expected:
|
|
|
|
// uword load_location
|
|
|
|
// load_location = 12345
|
2021-12-09 21:12:31 +00:00
|
|
|
// word llw
|
|
|
|
// llw = 12345
|
2021-12-09 20:13:13 +00:00
|
|
|
// cx16.r0 = load_location
|
|
|
|
// cx16.r0 += 10000
|
|
|
|
// cx16.r2 = load_location
|
|
|
|
// cx16.r2 += 10000
|
|
|
|
// cx16.r4 = load_location
|
|
|
|
// cx16.r4 += 22
|
2021-12-09 21:12:31 +00:00
|
|
|
// cx16.r5s = llw
|
|
|
|
// cx16.r5s -= 1899
|
|
|
|
// cx16.r7s = llw
|
|
|
|
// cx16.r7s += 99
|
2021-12-09 20:13:13 +00:00
|
|
|
val stmts = result.program.entrypoint.statements
|
2021-12-09 21:12:31 +00:00
|
|
|
stmts.size shouldBe 14
|
2021-12-09 20:13:13 +00:00
|
|
|
|
2021-12-09 21:12:31 +00:00
|
|
|
val addR0value = (stmts[5] as Assignment).value
|
2021-12-09 20:13:13 +00:00
|
|
|
val binexpr0 = addR0value as BinaryExpression
|
2021-12-09 21:12:31 +00:00
|
|
|
binexpr0.operator shouldBe "+"
|
2022-03-10 21:38:16 +00:00
|
|
|
binexpr0.right shouldBe NumericLiteral(DataType.UWORD, 10000.0, Position.DUMMY)
|
2021-12-09 21:12:31 +00:00
|
|
|
val addR2value = (stmts[7] as Assignment).value
|
|
|
|
val binexpr2 = addR2value as BinaryExpression
|
|
|
|
binexpr2.operator shouldBe "+"
|
2022-03-10 21:38:16 +00:00
|
|
|
binexpr2.right shouldBe NumericLiteral(DataType.UWORD, 10000.0, Position.DUMMY)
|
2021-12-09 20:13:13 +00:00
|
|
|
val addR4value = (stmts[9] as Assignment).value
|
|
|
|
val binexpr4 = addR4value as BinaryExpression
|
2021-12-09 21:12:31 +00:00
|
|
|
binexpr4.operator shouldBe "+"
|
2022-03-10 21:38:16 +00:00
|
|
|
binexpr4.right shouldBe NumericLiteral(DataType.UWORD, 22.0, Position.DUMMY)
|
2021-12-09 21:12:31 +00:00
|
|
|
val subR5value = (stmts[11] as Assignment).value
|
|
|
|
val binexpr5 = subR5value as BinaryExpression
|
|
|
|
binexpr5.operator shouldBe "-"
|
2022-03-10 21:38:16 +00:00
|
|
|
binexpr5.right shouldBe NumericLiteral(DataType.UWORD, 1899.0, Position.DUMMY)
|
2021-12-09 21:12:31 +00:00
|
|
|
val subR7value = (stmts[13] as Assignment).value
|
|
|
|
val binexpr7 = subR7value as BinaryExpression
|
|
|
|
binexpr7.operator shouldBe "+"
|
2022-03-10 21:38:16 +00:00
|
|
|
binexpr7.right shouldBe NumericLiteral(DataType.UWORD, 99.0, Position.DUMMY)
|
2021-12-09 20:13:13 +00:00
|
|
|
}
|
|
|
|
|
2021-12-09 22:12:12 +00:00
|
|
|
test("const folding multiple scenarios * and /") {
|
|
|
|
val source = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
word llw = 300
|
|
|
|
cx16.r0s = 9 * 2 * 10 * llw
|
|
|
|
cx16.r1s = llw * 9 * 2 * 10
|
2021-12-09 22:32:48 +00:00
|
|
|
cx16.r2s = llw / 30 / 3
|
|
|
|
cx16.r3s = llw / 2 * 10
|
|
|
|
cx16.r4s = llw * 90 / 5 ; not optimized because of loss of integer division precision
|
2021-12-09 22:12:12 +00:00
|
|
|
}
|
|
|
|
}"""
|
2022-03-07 20:41:12 +00:00
|
|
|
val result = compileText(C64Target(), true, source, writeAssembly = false)!!
|
2021-12-09 22:12:12 +00:00
|
|
|
// expected:
|
|
|
|
// word llw
|
|
|
|
// llw = 300
|
|
|
|
// cx16.r0s = llw
|
|
|
|
// cx16.r0s *= 180
|
|
|
|
// cx16.r1s = llw
|
|
|
|
// cx16.r1s *= 180
|
|
|
|
// cx16.r2s = llw
|
2021-12-09 22:32:48 +00:00
|
|
|
// cx16.r2s /= 90
|
|
|
|
// cx16.r3s = llw
|
|
|
|
// cx16.r3s *= 5
|
|
|
|
// cx16.r4s = llw
|
|
|
|
// cx16.r4s *= 90
|
|
|
|
// cx16.r4s /= 5
|
2021-12-09 22:12:12 +00:00
|
|
|
val stmts = result.program.entrypoint.statements
|
2021-12-09 22:32:48 +00:00
|
|
|
stmts.size shouldBe 13
|
2021-12-09 22:12:12 +00:00
|
|
|
|
|
|
|
val mulR0Value = (stmts[3] as Assignment).value
|
|
|
|
val binexpr0 = mulR0Value as BinaryExpression
|
|
|
|
binexpr0.operator shouldBe "*"
|
2022-03-10 21:38:16 +00:00
|
|
|
binexpr0.right shouldBe NumericLiteral(DataType.UWORD, 180.0, Position.DUMMY)
|
2021-12-09 22:12:12 +00:00
|
|
|
val mulR1Value = (stmts[5] as Assignment).value
|
|
|
|
val binexpr1 = mulR1Value as BinaryExpression
|
|
|
|
binexpr1.operator shouldBe "*"
|
2022-03-10 21:38:16 +00:00
|
|
|
binexpr1.right shouldBe NumericLiteral(DataType.UWORD, 180.0, Position.DUMMY)
|
2021-12-09 22:12:12 +00:00
|
|
|
val divR2Value = (stmts[7] as Assignment).value
|
|
|
|
val binexpr2 = divR2Value as BinaryExpression
|
|
|
|
binexpr2.operator shouldBe "/"
|
2022-03-10 21:38:16 +00:00
|
|
|
binexpr2.right shouldBe NumericLiteral(DataType.UWORD, 90.0, Position.DUMMY)
|
2021-12-09 22:32:48 +00:00
|
|
|
val mulR3Value = (stmts[9] as Assignment).value
|
|
|
|
val binexpr3 = mulR3Value as BinaryExpression
|
|
|
|
binexpr3.operator shouldBe "*"
|
2022-03-10 21:38:16 +00:00
|
|
|
binexpr3.right shouldBe NumericLiteral(DataType.UWORD, 5.0, Position.DUMMY)
|
2021-12-09 22:32:48 +00:00
|
|
|
val mulR4Value = (stmts[11] as Assignment).value
|
|
|
|
val binexpr4 = mulR4Value as BinaryExpression
|
|
|
|
binexpr4.operator shouldBe "*"
|
2022-03-10 21:38:16 +00:00
|
|
|
binexpr4.right shouldBe NumericLiteral(DataType.UWORD, 90.0, Position.DUMMY)
|
2021-12-09 22:32:48 +00:00
|
|
|
val divR4Value = (stmts[12] as Assignment).value
|
|
|
|
val binexpr4b = divR4Value as BinaryExpression
|
|
|
|
binexpr4b.operator shouldBe "/"
|
2022-03-10 21:38:16 +00:00
|
|
|
binexpr4b.right shouldBe NumericLiteral(DataType.UWORD, 5.0, Position.DUMMY)
|
2021-12-09 22:12:12 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 21:49:35 +00:00
|
|
|
test("constantfolded and silently typecasted for initializervalues") {
|
2021-11-01 23:47:01 +00:00
|
|
|
val sourcecode = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
const ubyte TEST = 10
|
2021-11-11 02:03:21 +00:00
|
|
|
byte @shared x1 = TEST as byte + 1
|
|
|
|
byte @shared x2 = 1 + TEST as byte
|
|
|
|
ubyte @shared y1 = TEST + 1 as byte
|
|
|
|
ubyte @shared y2 = 1 as byte + TEST
|
2021-11-01 23:47:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
2022-03-07 20:41:12 +00:00
|
|
|
val result = compileText(C64Target(), false, sourcecode)!!
|
2021-11-01 23:47:01 +00:00
|
|
|
val mainsub = result.program.entrypoint
|
2021-11-08 14:50:29 +00:00
|
|
|
mainsub.statements.size shouldBe 10
|
2021-11-01 23:47:01 +00:00
|
|
|
val declTest = mainsub.statements[0] as VarDecl
|
|
|
|
val declX1 = mainsub.statements[1] as VarDecl
|
|
|
|
val initX1 = mainsub.statements[2] as Assignment
|
|
|
|
val declX2 = mainsub.statements[3] as VarDecl
|
|
|
|
val initX2 = mainsub.statements[4] as Assignment
|
|
|
|
val declY1 = mainsub.statements[5] as VarDecl
|
|
|
|
val initY1 = mainsub.statements[6] as Assignment
|
|
|
|
val declY2 = mainsub.statements[7] as VarDecl
|
|
|
|
val initY2 = mainsub.statements[8] as Assignment
|
2021-11-08 14:50:29 +00:00
|
|
|
mainsub.statements[9] shouldBe instanceOf<Return>()
|
2022-02-10 23:21:40 +00:00
|
|
|
(declTest.value as NumericLiteral).number shouldBe 10.0
|
2021-11-08 14:50:29 +00:00
|
|
|
declX1.value shouldBe null
|
|
|
|
declX2.value shouldBe null
|
|
|
|
declY1.value shouldBe null
|
|
|
|
declY2.value shouldBe null
|
2022-02-10 23:21:40 +00:00
|
|
|
(initX1.value as NumericLiteral).type shouldBe DataType.BYTE
|
|
|
|
(initX1.value as NumericLiteral).number shouldBe 11.0
|
|
|
|
(initX2.value as NumericLiteral).type shouldBe DataType.BYTE
|
|
|
|
(initX2.value as NumericLiteral).number shouldBe 11.0
|
|
|
|
(initY1.value as NumericLiteral).type shouldBe DataType.UBYTE
|
|
|
|
(initY1.value as NumericLiteral).number shouldBe 11.0
|
|
|
|
(initY2.value as NumericLiteral).type shouldBe DataType.UBYTE
|
|
|
|
(initY2.value as NumericLiteral).number shouldBe 11.0
|
2021-11-01 23:47:01 +00:00
|
|
|
}
|
2021-11-09 00:13:23 +00:00
|
|
|
|
2022-07-01 22:54:38 +00:00
|
|
|
test("various 'not' operator rewrites even without optimizations on") {
|
2021-11-12 22:23:51 +00:00
|
|
|
val src = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
2022-06-29 20:19:44 +00:00
|
|
|
ubyte a1
|
|
|
|
ubyte a2
|
|
|
|
a1 = not not a1 ; a1 = a1==0
|
2022-07-01 22:54:38 +00:00
|
|
|
a1 = not a1 or not a2 ; a1 = a1==0 | a2==0
|
|
|
|
a1 = not a1 and not a2 ; a1 = a1==0 & a2==0
|
2021-11-12 22:23:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
2022-07-11 10:22:14 +00:00
|
|
|
val result = compileText(C64Target(), false, src, writeAssembly = true)!!
|
2022-06-30 22:38:19 +00:00
|
|
|
printProgram(result.program)
|
2022-06-29 20:19:44 +00:00
|
|
|
val stmts = result.program.entrypoint.statements
|
2022-07-11 10:22:14 +00:00
|
|
|
stmts.size shouldBe 8
|
2021-11-12 22:23:51 +00:00
|
|
|
|
2022-06-29 20:19:44 +00:00
|
|
|
val value1 = (stmts[4] as Assignment).value as BinaryExpression
|
|
|
|
val value2 = (stmts[5] as Assignment).value as BinaryExpression
|
|
|
|
val value3 = (stmts[6] as Assignment).value as BinaryExpression
|
|
|
|
value1.operator shouldBe "=="
|
|
|
|
value1.right shouldBe NumericLiteral(DataType.UBYTE, 0.0, Position.DUMMY)
|
2022-07-01 22:54:38 +00:00
|
|
|
value2.operator shouldBe "|"
|
|
|
|
value3.operator shouldBe "&"
|
2021-11-09 02:45:07 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 13:22:37 +00:00
|
|
|
test("intermediate assignment steps generated for typecasted expression") {
|
2021-11-12 01:17:37 +00:00
|
|
|
val src = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
ubyte r
|
2022-04-12 21:58:19 +00:00
|
|
|
ubyte @shared bb = (sgn(r)*2 + 100) as ubyte
|
2021-11-12 01:17:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
2022-03-07 20:41:12 +00:00
|
|
|
val result = compileText(C64Target(), true, src, writeAssembly = true)!!
|
2021-11-13 13:22:37 +00:00
|
|
|
/* turned into:
|
|
|
|
ubyte r
|
|
|
|
r = 0
|
|
|
|
ubyte bb
|
2022-04-12 21:58:19 +00:00
|
|
|
prog8_lib.retval_interm_b = sgn(r)
|
|
|
|
prog8_lib.retval_interm_b <<= 1
|
2021-11-14 01:38:59 +00:00
|
|
|
prog8_lib.retval_interm_b += 100
|
|
|
|
bb = prog8_lib.retval_interm_b
|
2021-11-13 13:22:37 +00:00
|
|
|
return
|
|
|
|
*/
|
|
|
|
val st = result.program.entrypoint.statements
|
|
|
|
st.size shouldBe 8
|
|
|
|
st.last() shouldBe instanceOf<Return>()
|
|
|
|
var assign = st[3] as Assignment
|
2022-02-10 01:47:46 +00:00
|
|
|
assign.target.identifier!!.nameInSource shouldBe listOf("prog8_lib","tempvar_b")
|
2021-11-13 13:22:37 +00:00
|
|
|
assign = st[4] as Assignment
|
2022-02-10 01:47:46 +00:00
|
|
|
assign.target.identifier!!.nameInSource shouldBe listOf("prog8_lib","tempvar_b")
|
2021-11-13 13:22:37 +00:00
|
|
|
assign = st[5] as Assignment
|
2022-02-10 01:47:46 +00:00
|
|
|
assign.target.identifier!!.nameInSource shouldBe listOf("prog8_lib","tempvar_b")
|
2021-11-13 13:22:37 +00:00
|
|
|
assign = st[6] as Assignment
|
|
|
|
assign.target.identifier!!.nameInSource shouldBe listOf("bb")
|
2021-11-12 01:17:37 +00:00
|
|
|
}
|
|
|
|
|
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)!!
|
2021-11-17 23:17:22 +00:00
|
|
|
val assignFF = result.program.entrypoint.statements.last() as Assignment
|
|
|
|
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)!!
|
2022-01-09 16:08:50 +00:00
|
|
|
result.program.entrypoint.statements.size shouldBe 7
|
2022-01-10 22:15:24 +00:00
|
|
|
val alldecls = result.program.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() {
|
|
|
|
if cx16.r0 {
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"""
|
2022-03-07 20:41:12 +00:00
|
|
|
val result = compileText(C64Target(), optimize=true, src, writeAssembly=false)!!
|
2021-11-13 13:22:37 +00:00
|
|
|
result.program.entrypoint.statements.size shouldBe 3
|
2021-12-28 12:56:47 +00:00
|
|
|
val ifstmt = result.program.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")
|
|
|
|
val func2 = result.program.entrypoint.statements[2] as Subroutine
|
|
|
|
func2.statements.size shouldBe 1
|
|
|
|
(func2.statements[0] as Assignment).target.identifier!!.nameInSource shouldBe listOf("cx16", "r0")
|
|
|
|
}
|
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
|
2021-11-17 23:17:22 +00:00
|
|
|
ubyte @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)!!
|
2022-06-28 00:38:19 +00:00
|
|
|
printProgram(result.program)
|
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
|
2021-11-17 23:17:22 +00:00
|
|
|
ubyte z3
|
2022-06-28 01:38:13 +00:00
|
|
|
z3 = 1
|
2021-11-20 16:33:02 +00:00
|
|
|
uword z4
|
|
|
|
z4 = 0
|
|
|
|
ubyte z5
|
|
|
|
z5 = z1
|
|
|
|
z5 += 5
|
|
|
|
ubyte z6
|
|
|
|
z6 = z1
|
|
|
|
z6 -= 5
|
|
|
|
*/
|
2021-11-17 21:34:34 +00:00
|
|
|
val statements = result.program.entrypoint.statements
|
2022-06-28 01:38:13 +00:00
|
|
|
statements.size shouldBe 14
|
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
|
|
|
|
val z5plus = statements[10] as Assignment
|
|
|
|
val z6decl = statements[11] as VarDecl
|
|
|
|
val z6init = statements[12] as Assignment
|
|
|
|
val z6plus = statements[13] as Assignment
|
2021-11-17 21:34:34 +00:00
|
|
|
|
|
|
|
z1decl.name shouldBe "z1"
|
2022-03-10 21:38:16 +00:00
|
|
|
z1init.value shouldBe NumericLiteral(DataType.UBYTE, 10.0, Position.DUMMY)
|
2021-11-17 21:34:34 +00:00
|
|
|
z2decl.name shouldBe "z2"
|
2022-03-10 21:38:16 +00:00
|
|
|
z2init.value shouldBe NumericLiteral(DataType.UBYTE, 255.0, Position.DUMMY)
|
2021-11-17 23:17:22 +00:00
|
|
|
z3decl.name shouldBe "z3"
|
2022-06-28 01:38:13 +00:00
|
|
|
z3init.value shouldBe NumericLiteral(DataType.UBYTE, 1.0, Position.DUMMY)
|
2021-11-20 16:33:02 +00:00
|
|
|
z4decl.name shouldBe "z4"
|
2022-03-10 21:38:16 +00:00
|
|
|
z4init.value shouldBe NumericLiteral(DataType.UBYTE, 0.0, Position.DUMMY)
|
2021-11-20 16:33:02 +00:00
|
|
|
z5decl.name shouldBe "z5"
|
2022-01-27 22:32:55 +00:00
|
|
|
(z5init.value as? IdentifierReference)?.nameInSource shouldBe listOf("z1")
|
2021-11-20 16:33:02 +00:00
|
|
|
z5plus.isAugmentable shouldBe true
|
|
|
|
(z5plus.value as BinaryExpression).operator shouldBe "+"
|
2022-03-10 21:38:16 +00:00
|
|
|
(z5plus.value as BinaryExpression).right shouldBe NumericLiteral(DataType.UBYTE, 5.0, Position.DUMMY)
|
2021-11-20 16:33:02 +00:00
|
|
|
z6decl.name shouldBe "z6"
|
2022-01-27 22:32:55 +00:00
|
|
|
(z6init.value as? IdentifierReference)?.nameInSource shouldBe listOf("z1")
|
2021-11-20 16:33:02 +00:00
|
|
|
z6plus.isAugmentable shouldBe true
|
|
|
|
(z6plus.value as BinaryExpression).operator shouldBe "-"
|
2022-03-10 21:38:16 +00:00
|
|
|
(z6plus.value as BinaryExpression).right shouldBe NumericLiteral(DataType.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() {
|
|
|
|
uword aa
|
|
|
|
ubyte zz
|
|
|
|
@(aa) = zz + 32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2022-03-07 20:41:12 +00:00
|
|
|
val result = compileText(C64Target(), optimize=true, src, writeAssembly=false)!!
|
2021-11-18 22:55:20 +00:00
|
|
|
val stmts = result.program.entrypoint.statements
|
2021-11-19 21:49:35 +00:00
|
|
|
stmts.size shouldBe 6
|
|
|
|
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() {
|
|
|
|
uword aa
|
|
|
|
ubyte zz
|
|
|
|
@(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)!!
|
2021-11-18 22:55:20 +00:00
|
|
|
val stmts = result.program.entrypoint.statements
|
2021-11-19 21:49:35 +00:00
|
|
|
stmts.size shouldBe 6
|
|
|
|
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
|
2021-11-19 21:49:35 +00:00
|
|
|
ubyte @shared z4 = not 1
|
|
|
|
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)!!
|
2021-11-18 22:55:20 +00:00
|
|
|
val stmts = result.program.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
|
2022-01-23 00:44:16 +00:00
|
|
|
errors.errors.size shouldBe 2
|
2022-06-05 12:21:10 +00:00
|
|
|
errors.errors[0] shouldContain "type of value WORD doesn't match target UBYTE"
|
2022-01-23 00:44:16 +00:00
|
|
|
errors.errors[1] shouldContain "out of range"
|
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
|
|
|
|
errors.errors[0] shouldContain "can't cast"
|
|
|
|
}
|
|
|
|
|
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)!!
|
2021-11-20 17:30:55 +00:00
|
|
|
result.program.entrypoint.statements.size shouldBe 11
|
|
|
|
result.program.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
|
2022-04-13 21:23:59 +00:00
|
|
|
xx = 6+lsb(abs(xx)) ; 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
|
|
|
|
*/
|
|
|
|
val stmts = result.program.entrypoint.statements
|
2022-01-10 23:34:44 +00:00
|
|
|
stmts.size shouldBe 8
|
2021-11-20 16:33:02 +00:00
|
|
|
stmts.filterIsInstance<VarDecl>().size shouldBe 3
|
2022-01-10 23:34:44 +00:00
|
|
|
stmts.filterIsInstance<Assignment>().size shouldBe 5
|
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
|
|
|
|
*/
|
|
|
|
val stmts = result.program.entrypoint.statements
|
|
|
|
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")
|
2022-03-10 21:38:16 +00:00
|
|
|
assignXX1.value shouldBe NumericLiteral(DataType.UBYTE, 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")
|
2022-03-10 21:38:16 +00:00
|
|
|
xxValue.right shouldBe NumericLiteral(DataType.UBYTE, 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() {
|
|
|
|
ubyte source=99
|
|
|
|
ubyte thingy=42
|
|
|
|
|
|
|
|
if source==3 or source==4 or source==99 or source==1
|
|
|
|
thingy++
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val result = compileText(C64Target(), optimize=true, src, writeAssembly=false)!!
|
2022-07-01 22:21:18 +00:00
|
|
|
printProgram(result.program)
|
2022-05-08 11:21:34 +00:00
|
|
|
/*
|
|
|
|
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++
|
|
|
|
*/
|
|
|
|
val stmts = result.program.entrypoint.statements
|
|
|
|
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() {
|
|
|
|
ubyte source=99
|
|
|
|
ubyte thingy=42
|
|
|
|
|
|
|
|
if source==3 or source==4 or source!=99 or source==1
|
|
|
|
thingy++
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val result = compileText(C64Target(), optimize=true, src, writeAssembly=false)!!
|
|
|
|
printProgram(result.program)
|
|
|
|
val stmts = result.program.entrypoint.statements
|
|
|
|
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() {
|
|
|
|
ubyte source=99
|
|
|
|
ubyte thingy=42
|
|
|
|
|
|
|
|
if source==3 or source==4 or thingy==99 or source==1
|
|
|
|
thingy++
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val result = compileText(C64Target(), optimize=true, src, writeAssembly=false)!!
|
|
|
|
printProgram(result.program)
|
|
|
|
val stmts = result.program.entrypoint.statements
|
|
|
|
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() {
|
|
|
|
ubyte source=99
|
|
|
|
ubyte thingy=42
|
|
|
|
|
|
|
|
if source==3 or source==4 and source==99 or source==1
|
|
|
|
thingy++
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val result = compileText(C64Target(), optimize=true, src, writeAssembly=false)!!
|
|
|
|
val stmts = result.program.entrypoint.statements
|
|
|
|
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 () {
|
|
|
|
uword eRef
|
|
|
|
if eRef[3] and 10 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val result = compileText(C64Target(), optimize=true, src, writeAssembly=false)!!
|
|
|
|
val stmts = result.program.entrypoint.statements
|
|
|
|
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)!!
|
|
|
|
var statements = result.program.entrypoint.statements
|
|
|
|
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")
|
|
|
|
(statements[5] as Assignment).target.memoryAddress!!.addressExpression.constValue(result.program)!!.number shouldBe 0x9fff
|
|
|
|
(statements[6] as Assignment).target.memoryAddress!!.addressExpression.constValue(result.program)!!.number shouldBe 0x9fff
|
|
|
|
(statements[7] as Assignment).target.memoryAddress!!.addressExpression.constValue(result.program)!!.number shouldBe 0x9fff
|
|
|
|
|
|
|
|
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)!!
|
|
|
|
statements = result.program.entrypoint.statements
|
|
|
|
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")
|
|
|
|
(statements[5] as Assignment).target.memoryAddress!!.addressExpression.constValue(result.program)!!.number shouldBe 53281.0
|
|
|
|
(statements[6] as Assignment).target.memoryAddress!!.addressExpression.constValue(result.program)!!.number shouldBe 53281.0
|
|
|
|
(statements[7] as Assignment).target.memoryAddress!!.addressExpression.constValue(result.program)!!.number shouldBe 53281.0
|
|
|
|
}
|
2021-11-07 23:16:58 +00:00
|
|
|
})
|