fix optimization ast parent linkage problem

This commit is contained in:
Irmen de Jong 2024-04-16 23:27:22 +02:00
parent 94263c43d0
commit 2675623aea
3 changed files with 15 additions and 1 deletions

View File

@ -77,8 +77,9 @@ class VarConstantValueTypeAdjuster(
// variable is never written to, so it can be replaced with a constant, IF the value is a constant
errors.info("variable '${decl.name}' is never written to and was replaced by a constant", decl.position)
val const = VarDecl(VarDeclType.CONST, decl.origin, decl.datatype, decl.zeropage, decl.arraysize, decl.name, decl.names, declValue, decl.sharedWithAsm, decl.splitArray, decl.position)
decl.value = null
return listOf(
IAstModification.ReplaceNode(decl, const, parent),
IAstModification.ReplaceNode(decl, const, parent)
)
}
}

View File

@ -793,6 +793,18 @@ main {
(expr.right as? NumericLiteral)?.number shouldBe 10.0
}
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
}
test("De Morgan's laws") {
val src="""
main {

View File

@ -375,6 +375,7 @@ class TypecastExpression(var expression: Expression, var type: DataType, val imp
override fun inferType(program: Program) = InferredTypes.knownFor(type)
override fun constValue(program: Program): NumericLiteral? {
val cv = expression.constValue(program) ?: return null
cv.linkParents(parent)
val cast = cv.cast(type, implicit)
return if(cast.isValid) {
val newval = cast.valueOrZero()