code cleanups

This commit is contained in:
Irmen de Jong 2020-10-20 17:52:55 +02:00
parent 1a36302cf1
commit e35ad0cc8f
13 changed files with 31 additions and 51 deletions

View File

@ -1 +1 @@
4.6-SNAPSHOT
4.6

View File

@ -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<Module>): 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 {

View File

@ -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"

View File

@ -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

View File

@ -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
}

View File

@ -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()

View File

@ -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<String>()
internal fun saveRegister(register: CpuRegister, dontUseStack: Boolean, scope: Subroutine?) {
if(dontUseStack) {
when (register) {

View File

@ -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)

View File

@ -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)

View File

@ -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)
}
}
}

View File

@ -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<IAstModification>()
override fun after(decl: VarDecl, parent: Node): Iterable<IAstModification> {

View File

@ -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
}

View File

@ -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()