From e35ad0cc8fc199d404c2c0a5700eace28c7bd7ad Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 20 Oct 2020 17:52:55 +0200 Subject: [PATCH] code cleanups --- compiler/res/version.txt | 2 +- compiler/src/prog8/ast/AstToplevel.kt | 4 +--- compiler/src/prog8/ast/antlr/Antr2Kotlin.kt | 2 -- .../src/prog8/ast/processing/AstWalker.kt | 14 ++++++------ .../ast/processing/StatementReorderer.kt | 3 ++- .../src/prog8/ast/statements/AstStatements.kt | 11 ---------- .../compiler/target/c64/codegen/AsmGen.kt | 2 -- .../c64/codegen/assignment/AsmAssignment.kt | 7 +++--- .../codegen/assignment/AssignmentAsmGen.kt | 3 +-- .../assignment/AugmentableAssignmentAsmGen.kt | 8 +++---- .../optimizer/ConstantIdentifierReplacer.kt | 2 +- .../prog8/optimizer/ExpressionSimplifier.kt | 22 +++++++++---------- compiler/src/prog8/optimizer/Extensions.kt | 2 +- 13 files changed, 31 insertions(+), 51 deletions(-) diff --git a/compiler/res/version.txt b/compiler/res/version.txt index c14894981..b3d791d75 100644 --- a/compiler/res/version.txt +++ b/compiler/res/version.txt @@ -1 +1 @@ -4.6-SNAPSHOT +4.6 diff --git a/compiler/src/prog8/ast/AstToplevel.kt b/compiler/src/prog8/ast/AstToplevel.kt index 925522ed2..943686585 100644 --- a/compiler/src/prog8/ast/AstToplevel.kt +++ b/compiler/src/prog8/ast/AstToplevel.kt @@ -6,7 +6,6 @@ import prog8.ast.expressions.IdentifierReference import prog8.ast.processing.AstWalker import prog8.ast.processing.IAstVisitor import prog8.ast.statements.* -import prog8.compiler.target.c64.codegen.AsmGen import prog8.functions.BuiltinFunctions import java.nio.file.Path @@ -185,7 +184,6 @@ interface INameScope { } fun containsCodeOrVars() = statements.any { it !is Directive || it.directive == "%asminclude" || it.directive == "%asm"} - fun containsNoVars() = statements.all { it !is VarDecl } fun containsNoCodeNorVars() = !containsCodeOrVars() fun remove(stmt: Statement) { @@ -258,7 +256,7 @@ class Program(val name: String, val modules: MutableList): Node { override val position: Position = Position.DUMMY override var parent: Node get() = throw FatalAstException("program has no parent") - set(value) = throw FatalAstException("can't set parent of program") + set(_) = throw FatalAstException("can't set parent of program") override fun linkParents(parent: Node) { modules.forEach { diff --git a/compiler/src/prog8/ast/antlr/Antr2Kotlin.kt b/compiler/src/prog8/ast/antlr/Antr2Kotlin.kt index 36b728dcd..b0bb6c43d 100644 --- a/compiler/src/prog8/ast/antlr/Antr2Kotlin.kt +++ b/compiler/src/prog8/ast/antlr/Antr2Kotlin.kt @@ -470,7 +470,6 @@ private fun prog8Parser.ExpressionContext.toAst() : Expression { litval.stringliteral()!=null -> litval.stringliteral().toAst() litval.charliteral()!=null -> { try { - val cc=litval.charliteral() NumericLiteralValue(DataType.UBYTE, CompilationTarget.instance.encodeString( unescape(litval.charliteral().SINGLECHAR().text, litval.toPosition()), litval.charliteral().ALT_STRING_ENCODING()!=null)[0], litval.toPosition()) @@ -648,7 +647,6 @@ private fun prog8Parser.VardeclContext.toAst(): VarDecl { } internal fun escape(str: String): String { - val es2 = str.replace("\t", "\\t").replace("\n", "\\n").replace("\r", "\\r") val es = str.map { when(it) { '\t' -> "\\t" diff --git a/compiler/src/prog8/ast/processing/AstWalker.kt b/compiler/src/prog8/ast/processing/AstWalker.kt index 24fdccb78..b8fb2e7fd 100644 --- a/compiler/src/prog8/ast/processing/AstWalker.kt +++ b/compiler/src/prog8/ast/processing/AstWalker.kt @@ -20,14 +20,14 @@ interface IAstModification { } } - class SetExpression(val setter: (newExpr: Expression) -> Unit, val newExpr: Expression, val parent: Node) : IAstModification { + class SetExpression(private val setter: (newExpr: Expression) -> Unit, private val newExpr: Expression, private val parent: Node) : IAstModification { override fun perform() { setter(newExpr) newExpr.linkParents(parent) } } - class InsertFirst(val stmt: Statement, val parent: Node) : IAstModification { + class InsertFirst(private val stmt: Statement, private val parent: Node) : IAstModification { override fun perform() { if(parent is INameScope) { parent.statements.add(0, stmt) @@ -38,7 +38,7 @@ interface IAstModification { } } - class InsertLast(val stmt: Statement, val parent: Node) : IAstModification { + class InsertLast(private val stmt: Statement, private val parent: Node) : IAstModification { override fun perform() { if(parent is INameScope) { parent.statements.add(stmt) @@ -49,7 +49,7 @@ interface IAstModification { } } - class InsertAfter(val after: Statement, val stmt: Statement, val parent: Node) : IAstModification { + class InsertAfter(private val after: Statement, private val stmt: Statement, private val parent: Node) : IAstModification { override fun perform() { if(parent is INameScope) { val idx = parent.statements.indexOfFirst { it===after } + 1 @@ -61,7 +61,7 @@ interface IAstModification { } } - class InsertBefore(val before: Statement, val stmt: Statement, val parent: Node) : IAstModification { + class InsertBefore(private val before: Statement, private val stmt: Statement, private val parent: Node) : IAstModification { override fun perform() { if(parent is INameScope) { val idx = parent.statements.indexOfFirst { it===before } @@ -73,14 +73,14 @@ interface IAstModification { } } - class ReplaceNode(val node: Node, val replacement: Node, val parent: Node) : IAstModification { + class ReplaceNode(private val node: Node, private val replacement: Node, private val parent: Node) : IAstModification { override fun perform() { parent.replaceChildNode(node, replacement) replacement.linkParents(parent) } } - class SwapOperands(val expr: BinaryExpression): IAstModification { + class SwapOperands(private val expr: BinaryExpression): IAstModification { override fun perform() { require(expr.operator in associativeOperators) val tmp = expr.left diff --git a/compiler/src/prog8/ast/processing/StatementReorderer.kt b/compiler/src/prog8/ast/processing/StatementReorderer.kt index 5dad13bdd..b044529f4 100644 --- a/compiler/src/prog8/ast/processing/StatementReorderer.kt +++ b/compiler/src/prog8/ast/processing/StatementReorderer.kt @@ -90,7 +90,8 @@ internal class StatementReorderer(val program: Program, val errors: ErrorReporte val block = expr.definingBlock() if(!block.asmGenInfo.usedAutoArrayIndexer) { // create the indexer var at block level scope - val vardecl = VarDecl(VarDeclType.VAR, DataType.UBYTE, ZeropageWish.PREFER_ZEROPAGE, null, indexerVarName, null, null, false, true, expr.position) + val vardecl = VarDecl(VarDeclType.VAR, DataType.UBYTE, ZeropageWish.PREFER_ZEROPAGE, + null, indexerVarName, null, null, isArray = false, autogeneratedDontRemove = true, position = expr.position) modifications.add(IAstModification.InsertFirst(vardecl, block)) block.asmGenInfo.usedAutoArrayIndexer = true } diff --git a/compiler/src/prog8/ast/statements/AstStatements.kt b/compiler/src/prog8/ast/statements/AstStatements.kt index 85d6b4988..fd531bc3c 100644 --- a/compiler/src/prog8/ast/statements/AstStatements.kt +++ b/compiler/src/prog8/ast/statements/AstStatements.kt @@ -444,17 +444,6 @@ data class AssignTarget(var identifier: IdentifierReference?, fun accept(visitor: IAstVisitor) = visitor.visit(this) fun accept(visitor: AstWalker, parent: Node) = visitor.visit(this, parent) - companion object { - fun fromExpr(expr: Expression): AssignTarget { - return when (expr) { - is IdentifierReference -> AssignTarget(expr, null, null, expr.position) - is ArrayIndexedExpression -> AssignTarget(null, expr, null, expr.position) - is DirectMemoryRead -> AssignTarget(null, null, DirectMemoryWrite(expr.addressExpression, expr.position), expr.position) - else -> throw FatalAstException("invalid expression object $expr") - } - } - } - fun inferType(program: Program, stmt: Statement): InferredTypes.InferredType { // TODO why does this have the extra 'stmt' scope parameter??? if (identifier != null) { val symbol = program.namespace.lookup(identifier!!.nameInSource, stmt) ?: return InferredTypes.unknown() diff --git a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt index 46c99ffa0..361773d80 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt @@ -550,8 +550,6 @@ internal class AsmGen(private val program: Program, private fun fixNameSymbols(name: String) = name.replace("<", "prog8_").replace(">", "") // take care of the autogenerated invalid (anon) label names - private val saveRegisterLabels = Stack() - internal fun saveRegister(register: CpuRegister, dontUseStack: Boolean, scope: Subroutine?) { if(dontUseStack) { when (register) { diff --git a/compiler/src/prog8/compiler/target/c64/codegen/assignment/AsmAssignment.kt b/compiler/src/prog8/compiler/target/c64/codegen/assignment/AsmAssignment.kt index 37d437067..59873de9d 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/assignment/AsmAssignment.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/assignment/AsmAssignment.kt @@ -153,11 +153,12 @@ internal class AsmAssignSource(val kind: SourceStorageKind, } } - fun withAdjustedDt(newType: DataType) = - AsmAssignSource(kind, program, asmgen, newType, variableAsmName, array, memory, register, number, expression) - fun adjustSignedUnsigned(target: AsmAssignTarget): AsmAssignSource { // allow some signed/unsigned relaxations + + fun withAdjustedDt(newType: DataType) = + AsmAssignSource(kind, program, asmgen, newType, variableAsmName, array, memory, register, number, expression) + if(target.datatype!=datatype) { if(target.datatype in ByteDatatypes && datatype in ByteDatatypes) { return withAdjustedDt(target.datatype) diff --git a/compiler/src/prog8/compiler/target/c64/codegen/assignment/AssignmentAsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/assignment/AssignmentAsmGen.kt index 698003be2..7a8f046f9 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/assignment/AssignmentAsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/assignment/AssignmentAsmGen.kt @@ -113,8 +113,7 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen } } SourceStorageKind.EXPRESSION -> { - val value = assign.source.expression!! - when(value) { + when(val value = assign.source.expression!!) { is AddressOf -> { val sourceName = value.identifier.firstStructVarName(program.namespace) ?: asmgen.asmVariableName(value.identifier) assignAddressOf(assign.target, sourceName) diff --git a/compiler/src/prog8/compiler/target/c64/codegen/assignment/AugmentableAssignmentAsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/assignment/AugmentableAssignmentAsmGen.kt index 073cb2939..5c9b28fe5 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/assignment/AugmentableAssignmentAsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/assignment/AugmentableAssignmentAsmGen.kt @@ -10,7 +10,6 @@ import prog8.compiler.target.CpuType import prog8.compiler.target.c64.codegen.AsmGen import prog8.compiler.target.c64.codegen.ExpressionsAsmGen import prog8.compiler.toHex -import kotlin.math.absoluteValue internal class AugmentableAssignmentAsmGen(private val program: Program, private val assignmentAsmGen: AssignmentAsmGen, @@ -20,8 +19,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program, require(assign.isAugmentable) require(assign.source.kind== SourceStorageKind.EXPRESSION) - val value = assign.source.expression!! - when (value) { + when (val value = assign.source.expression!!) { is PrefixExpression -> { // A = -A , A = +A, A = ~A, A = not A val type = value.inferType(program).typeOrElse(DataType.STRUCT) @@ -629,7 +627,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program, // TODO: tuned code for more operators } else -> { - inplaceModification_byte_value_to_variable(name, dt, operator, memread); + inplaceModification_byte_value_to_variable(name, dt, operator, memread) } } } @@ -660,7 +658,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program, // TODO: tuned code for more operators } else -> { - inplaceModification_word_value_to_variable(name, dt, operator, memread); + inplaceModification_word_value_to_variable(name, dt, operator, memread) } } } diff --git a/compiler/src/prog8/optimizer/ConstantIdentifierReplacer.kt b/compiler/src/prog8/optimizer/ConstantIdentifierReplacer.kt index ecbf3c984..fd6edc94b 100644 --- a/compiler/src/prog8/optimizer/ConstantIdentifierReplacer.kt +++ b/compiler/src/prog8/optimizer/ConstantIdentifierReplacer.kt @@ -13,7 +13,7 @@ import prog8.ast.statements.VarDecl import prog8.compiler.target.CompilationTarget // Fix up the literal value's type to match that of the vardecl -internal class VarConstantValueTypeAdjuster(private val program: Program, private val errors: ErrorReporter) : AstWalker() { +internal class VarConstantValueTypeAdjuster(private val program: Program) : AstWalker() { private val noModifications = emptyList() override fun after(decl: VarDecl, parent: Node): Iterable { diff --git a/compiler/src/prog8/optimizer/ExpressionSimplifier.kt b/compiler/src/prog8/optimizer/ExpressionSimplifier.kt index 6f0b6b0d8..fae0af264 100644 --- a/compiler/src/prog8/optimizer/ExpressionSimplifier.kt +++ b/compiler/src/prog8/optimizer/ExpressionSimplifier.kt @@ -372,12 +372,16 @@ internal class ExpressionSimplifier(private val program: Program) : AstWalker() if (rightVal != null) { // right value is a constant, see if we can optimize - val rightConst: NumericLiteralValue = rightVal - when (rightConst.number.toDouble()) { - 0.0 -> { - // left - return expr.left - } + val rnum = rightVal.number.toDouble() + if (rnum == 0.0) { + // left + return expr.left + } + + if(rnum<0.0) { + expr.operator = "+" + expr.right = NumericLiteralValue(rightVal.type, -rnum, rightVal.position) + return expr } } if (leftVal != null) { @@ -390,12 +394,6 @@ internal class ExpressionSimplifier(private val program: Program) : AstWalker() } } - val rnum = rightVal?.number?.toDouble() - if(rnum!=null && rnum<0.0) { - expr.operator = "+" - expr.right = NumericLiteralValue(rightVal.type, -rnum, rightVal.position) - return expr - } return null } diff --git a/compiler/src/prog8/optimizer/Extensions.kt b/compiler/src/prog8/optimizer/Extensions.kt index 307e21370..562fd1b56 100644 --- a/compiler/src/prog8/optimizer/Extensions.kt +++ b/compiler/src/prog8/optimizer/Extensions.kt @@ -5,7 +5,7 @@ import prog8.ast.base.ErrorReporter internal fun Program.constantFold(errors: ErrorReporter) { - val valuetypefixer = VarConstantValueTypeAdjuster(this, errors) + val valuetypefixer = VarConstantValueTypeAdjuster(this) valuetypefixer.visit(this) if(errors.isEmpty()) { valuetypefixer.applyModifications()