clearer no modifications

This commit is contained in:
Irmen de Jong 2020-06-14 02:54:29 +02:00
parent 235610f40c
commit 3e47dad12a
9 changed files with 52 additions and 43 deletions

View File

@ -8,6 +8,7 @@ import prog8.ast.statements.*
internal class AstVariousTransforms(private val program: Program) : AstWalker() {
private val noModifications = emptyList<IAstModification>()
override fun before(functionCall: FunctionCall, parent: Node): Iterable<IAstModification> {
if(functionCall.target.nameInSource.size==1 && functionCall.target.nameInSource[0]=="lsb") {
@ -18,7 +19,7 @@ internal class AstVariousTransforms(private val program: Program) : AstWalker()
))
}
return emptyList()
return noModifications
}
override fun before(decl: VarDecl, parent: Node): Iterable<IAstModification> {
@ -33,7 +34,7 @@ internal class AstVariousTransforms(private val program: Program) : AstWalker()
))
}
return emptyList()
return noModifications
}
override fun after(subroutine: Subroutine, parent: Node): Iterable<IAstModification> {
@ -52,7 +53,7 @@ internal class AstVariousTransforms(private val program: Program) : AstWalker()
}
}
return emptyList()
return noModifications
}
override fun before(expr: BinaryExpression, parent: Node): Iterable<IAstModification> {
@ -71,7 +72,7 @@ internal class AstVariousTransforms(private val program: Program) : AstWalker()
))
}
return emptyList()
return noModifications
}
override fun after(string: StringLiteralValue, parent: Node): Iterable<IAstModification> {
@ -84,7 +85,7 @@ internal class AstVariousTransforms(private val program: Program) : AstWalker()
IAstModification.InsertFirst(vardecl, string.definingScope() as Node)
)
}
return emptyList()
return noModifications
}
override fun after(array: ArrayLiteralValue, parent: Node): Iterable<IAstModification> {
@ -112,7 +113,7 @@ internal class AstVariousTransforms(private val program: Program) : AstWalker()
}
}
}
return emptyList()
return noModifications
}
private fun processBinaryExprWithString(string: StringLiteralValue, operand: Expression, expr: BinaryExpression): Expression {

View File

@ -6,7 +6,7 @@ import prog8.ast.base.FatalAstException
import prog8.ast.expressions.*
import prog8.ast.statements.*
// TODO replace all occurrences of this with AstWalker
// TODO replace all occurrences of this with AstWalker (currently only ConstantFoldingOptimizer remaining)
interface IAstModifyingVisitor {
fun visit(program: Program) {
program.modules.forEach { it.accept(this) }

View File

@ -10,11 +10,12 @@ internal class ImportedModuleDirectiveRemover: AstWalker() {
*/
private val moduleLevelDirectives = listOf("%output", "%launcher", "%zeropage", "%zpreserved", "%address")
private val noModifications = emptyList<IAstModification>()
override fun before(directive: Directive, parent: Node): Iterable<IAstModification> {
if(directive.directive in moduleLevelDirectives) {
return listOf(IAstModification.Remove(directive, parent))
}
return emptyList()
return noModifications
}
}

View File

@ -19,7 +19,7 @@ internal class StatementReorderer(val program: Program) : AstWalker() {
// - sorts the choices in when statement.
// - insert AddressOf (&) expression where required (string params to a UWORD function param etc).
private val noModifications = emptyList<IAstModification>()
private val directivesToMove = setOf("%output", "%launcher", "%zeropage", "%zpreserved", "%address", "%option")
override fun after(module: Module, parent: Node): Iterable<IAstModification> {
@ -33,7 +33,7 @@ internal class StatementReorderer(val program: Program) : AstWalker() {
}
reorderVardeclsAndDirectives(module.statements)
return emptyList()
return noModifications
}
private fun reorderVardeclsAndDirectives(statements: MutableList<Statement>) {
@ -56,7 +56,7 @@ internal class StatementReorderer(val program: Program) : AstWalker() {
}
reorderVardeclsAndDirectives(block.statements)
return emptyList()
return noModifications
}
override fun before(subroutine: Subroutine, parent: Node): Iterable<IAstModification> {
@ -68,7 +68,7 @@ internal class StatementReorderer(val program: Program) : AstWalker() {
)
}
}
return emptyList()
return noModifications
}
override fun after(decl: VarDecl, parent: Node): Iterable<IAstModification> {
@ -86,7 +86,7 @@ internal class StatementReorderer(val program: Program) : AstWalker() {
)
}
}
return emptyList()
return noModifications
}
override fun after(whenStatement: WhenStatement, parent: Node): Iterable<IAstModification> {
@ -95,7 +95,7 @@ internal class StatementReorderer(val program: Program) : AstWalker() {
}
whenStatement.choices.clear()
choices.mapTo(whenStatement.choices) { it.second }
return emptyList()
return noModifications
}
override fun before(assignment: Assignment, parent: Node): Iterable<IAstModification> {
@ -119,7 +119,7 @@ internal class StatementReorderer(val program: Program) : AstWalker() {
}
}
return emptyList()
return noModifications
}
private fun flattenStructAssignmentFromStructLiteral(structAssignment: Assignment, program: Program): List<Assignment> {

View File

@ -16,6 +16,8 @@ class TypecastsAdder(val program: Program, val errors: ErrorReporter) : AstWalke
* (this includes function call arguments)
*/
private val noModifications = emptyList<IAstModification>()
override fun after(expr: BinaryExpression, parent: Node): Iterable<IAstModification> {
val leftDt = expr.left.inferType(program)
val rightDt = expr.right.inferType(program)
@ -32,7 +34,7 @@ class TypecastsAdder(val program: Program, val errors: ErrorReporter) : AstWalke
}
}
}
return emptyList()
return noModifications
}
override fun after(assignment: Assignment, parent: Node): Iterable<IAstModification> {
@ -72,7 +74,7 @@ class TypecastsAdder(val program: Program, val errors: ErrorReporter) : AstWalke
}
}
}
return emptyList()
return noModifications
}
override fun after(functionCallStatement: FunctionCallStatement, parent: Node): Iterable<IAstModification> {
@ -143,7 +145,7 @@ class TypecastsAdder(val program: Program, val errors: ErrorReporter) : AstWalke
if(typecast.implicit && typecast.type in setOf(DataType.FLOAT, DataType.ARRAY_F)) {
errors.warn("byte or word value implicitly converted to float. Suggestion: use explicit cast as float, a float number, or revert to integer arithmetic", typecast.position)
}
return emptyList()
return noModifications
}
override fun after(memread: DirectMemoryRead, parent: Node): Iterable<IAstModification> {
@ -154,7 +156,7 @@ class TypecastsAdder(val program: Program, val errors: ErrorReporter) : AstWalke
?: TypecastExpression(memread.addressExpression, DataType.UWORD, true, memread.addressExpression.position)
return listOf(IAstModification.ReplaceNode(memread.addressExpression, typecast, memread))
}
return emptyList()
return noModifications
}
override fun after(memwrite: DirectMemoryWrite, parent: Node): Iterable<IAstModification> {
@ -165,7 +167,7 @@ class TypecastsAdder(val program: Program, val errors: ErrorReporter) : AstWalke
?: TypecastExpression(memwrite.addressExpression, DataType.UWORD, true, memwrite.addressExpression.position)
return listOf(IAstModification.ReplaceNode(memwrite.addressExpression, typecast, memwrite))
}
return emptyList()
return noModifications
}
override fun after(structLv: StructLiteralValue, parent: Node): Iterable<IAstModification> {
@ -210,7 +212,7 @@ class TypecastsAdder(val program: Program, val errors: ErrorReporter) : AstWalke
}
}
}
return emptyList()
return noModifications
}
override fun after(returnStmt: Return, parent: Node): Iterable<IAstModification> {
@ -221,7 +223,7 @@ class TypecastsAdder(val program: Program, val errors: ErrorReporter) : AstWalke
if(subroutine.returntypes.size==1) {
val subReturnType = subroutine.returntypes.first()
if (returnValue.inferType(program).istype(subReturnType))
return emptyList()
return noModifications
if (returnValue is NumericLiteralValue) {
returnStmt.value = returnValue.cast(subroutine.returntypes.single())
} else {
@ -232,6 +234,6 @@ class TypecastsAdder(val program: Program, val errors: ErrorReporter) : AstWalke
}
}
}
return emptyList()
return noModifications
}
}

View File

@ -11,12 +11,14 @@ import prog8.ast.statements.*
internal class BeforeAsmGenerationAstChanger(val program: Program, val errors: ErrorReporter) : AstWalker() {
private val noModifications = emptyList<IAstModification>()
override fun after(decl: VarDecl, parent: Node): Iterable<IAstModification> {
if (decl.value == null && decl.type == VarDeclType.VAR && decl.datatype in NumericDatatypes) {
// a numeric vardecl without an initial value is initialized with zero.
decl.value = decl.zeroElementValue()
}
return emptyList()
return noModifications
}
override fun after(scope: AnonymousScope, parent: Node): Iterable<IAstModification> {
@ -45,7 +47,7 @@ internal class BeforeAsmGenerationAstChanger(val program: Program, val errors: E
decls.map { IAstModification.InsertFirst(it, sub) } // move it up to the subroutine
}
}
return emptyList()
return noModifications
}
override fun after(subroutine: Subroutine, parent: Node): Iterable<IAstModification> {
@ -99,6 +101,6 @@ internal class BeforeAsmGenerationAstChanger(val program: Program, val errors: E
}
}
return emptyList()
return noModifications
}
}

View File

@ -14,6 +14,7 @@ import prog8.ast.statements.PostIncrDecr
internal class AssignmentTransformer(val program: Program, val errors: ErrorReporter) : AstWalker() {
var optimizationsDone: Int = 0
private val noModifications = emptyList<IAstModification>()
override fun before(assignment: Assignment, parent: Node): Iterable<IAstModification> {
// modify A = A + 5 back into augmented form A += 5 for easier code generation for optimized in-place assignments
@ -27,7 +28,7 @@ internal class AssignmentTransformer(val program: Program, val errors: ErrorRepo
assignment.aug_op = binExpr.operator + "="
assignment.value.parent = assignment
optimizationsDone++
return emptyList()
return noModifications
}
}
assignment.aug_op = "setvalue"
@ -151,6 +152,6 @@ internal class AssignmentTransformer(val program: Program, val errors: ErrorRepo
}
}
}
return emptyList()
return noModifications
}
}

View File

@ -22,11 +22,12 @@ import kotlin.math.pow
internal class ExpressionSimplifier(private val program: Program) : AstWalker() {
private val powersOfTwo = (1..16).map { (2.0).pow(it) }.toSet()
private val negativePowersOfTwo = powersOfTwo.map { -it }.toSet()
private val noModifications = emptyList<IAstModification>()
override fun after(assignment: Assignment, parent: Node): Iterable<IAstModification> {
if (assignment.aug_op != null)
throw FatalAstException("augmented assignments should have been converted to normal assignments before this optimizer: $assignment")
return emptyList()
return noModifications
}
override fun after(typecast: TypecastExpression, parent: Node): Iterable<IAstModification> {
@ -82,10 +83,10 @@ internal class ExpressionSimplifier(private val program: Program) : AstWalker()
if (newExpr != null)
return listOf(IAstModification.ReplaceNode(expr, newExpr, parent))
}
else -> return emptyList()
else -> return noModifications
}
}
return emptyList()
return noModifications
}
override fun after(expr: BinaryExpression, parent: Node): Iterable<IAstModification> {
@ -297,7 +298,7 @@ internal class ExpressionSimplifier(private val program: Program) : AstWalker()
if(newExpr != null)
return listOf(IAstModification.ReplaceNode(expr, newExpr, parent))
return emptyList()
return noModifications
}
private fun determineY(x: Expression, subBinExpr: BinaryExpression): Expression? {

View File

@ -23,6 +23,7 @@ import kotlin.math.floor
internal class StatementOptimizer(private val program: Program,
private val errors: ErrorReporter) : AstWalker() {
private val noModifications = emptyList<IAstModification>()
private val callgraph = CallGraph(program)
private val pureBuiltinFunctions = BuiltinFunctions.filter { it.value.pure }
@ -38,7 +39,7 @@ internal class StatementOptimizer(private val program: Program,
return listOf(IAstModification.Remove(block, parent))
}
}
return emptyList()
return noModifications
}
override fun after(subroutine: Subroutine, parent: Node): Iterable<IAstModification> {
@ -60,7 +61,7 @@ internal class StatementOptimizer(private val program: Program,
return listOf(IAstModification.Remove(subroutine, parent))
}
return emptyList()
return noModifications
}
override fun after(scope: AnonymousScope, parent: Node): Iterable<IAstModification> {
@ -77,7 +78,7 @@ internal class StatementOptimizer(private val program: Program,
return listOf(IAstModification.Remove(decl, parent))
}
return emptyList()
return noModifications
}
override fun after(functionCallStatement: FunctionCallStatement, parent: Node): Iterable<IAstModification> {
@ -139,7 +140,7 @@ internal class StatementOptimizer(private val program: Program,
return listOf(IAstModification.Remove(functionCallStatement, parent))
}
return emptyList()
return noModifications
}
override fun before(functionCall: FunctionCall, parent: Node): Iterable<IAstModification> {
@ -153,7 +154,7 @@ internal class StatementOptimizer(private val program: Program,
return listOf(IAstModification.ReplaceNode(functionCall, constval, parent))
}
}
return emptyList()
return noModifications
}
override fun after(ifStatement: IfStatement, parent: Node): Iterable<IAstModification> {
@ -186,7 +187,7 @@ internal class StatementOptimizer(private val program: Program,
}
}
return emptyList()
return noModifications
}
override fun after(forLoop: ForLoop, parent: Node): Iterable<IAstModification> {
@ -244,7 +245,7 @@ internal class StatementOptimizer(private val program: Program,
}
}
return emptyList()
return noModifications
}
override fun before(repeatLoop: RepeatLoop, parent: Node): Iterable<IAstModification> {
@ -261,7 +262,7 @@ internal class StatementOptimizer(private val program: Program,
return listOf(IAstModification.ReplaceNode(repeatLoop, forever, parent))
}
}
return emptyList()
return noModifications
}
override fun before(whileLoop: WhileLoop, parent: Node): Iterable<IAstModification> {
@ -277,7 +278,7 @@ internal class StatementOptimizer(private val program: Program,
listOf(IAstModification.Remove(whileLoop, parent))
}
}
return emptyList()
return noModifications
}
override fun after(whenStatement: WhenStatement, parent: Node): Iterable<IAstModification> {
@ -299,7 +300,7 @@ internal class StatementOptimizer(private val program: Program,
if(label!=null && scope.statements.indexOf(label) == scope.statements.indexOf(jump)+1)
return listOf(IAstModification.Remove(jump, parent))
return emptyList()
return noModifications
}
override fun after(assignment: Assignment, parent: Node): Iterable<IAstModification> {
@ -391,7 +392,7 @@ internal class StatementOptimizer(private val program: Program,
}
}
return emptyList()
return noModifications
}
private fun deduplicateAssignments(statements: List<Statement>): MutableList<Int> {