better names

This commit is contained in:
Irmen de Jong 2020-08-23 16:08:31 +02:00
parent d0674ad688
commit 1be139759c
12 changed files with 59 additions and 65 deletions

View File

@ -545,7 +545,7 @@ internal class AstChecker(private val program: Program,
}
VarDeclType.MEMORY -> {
if(decl.arraysize!=null) {
val arraySize = decl.arraysize!!.size() ?: 1
val arraySize = decl.arraysize!!.constIndex() ?: 1
when(decl.datatype) {
DataType.ARRAY_B, DataType.ARRAY_UB ->
if(arraySize > 256)
@ -587,7 +587,7 @@ internal class AstChecker(private val program: Program,
// array length limits
if(decl.isArray) {
val length = decl.arraysize!!.size() ?: 1
val length = decl.arraysize!!.constIndex() ?: 1
when (decl.datatype) {
DataType.STR, DataType.ARRAY_UB, DataType.ARRAY_B -> {
if(length==0 || length>256)
@ -957,7 +957,7 @@ internal class AstChecker(private val program: Program,
if(target is VarDecl) {
if(target.datatype !in IterableDatatypes)
errors.err("indexing requires an iterable variable", arrayIndexedExpression.position)
val arraysize = target.arraysize?.size()
val arraysize = target.arraysize?.constIndex()
if(arraysize!=null) {
// check out of bounds
val index = (arrayIndexedExpression.arrayspec.index as? NumericLiteralValue)?.number?.toInt()
@ -1078,7 +1078,7 @@ internal class AstChecker(private val program: Program,
if(value.type.istype(targetDt)) {
if(!checkArrayValues(value, targetDt))
return false
val arraySpecSize = arrayspec.size()
val arraySpecSize = arrayspec.constIndex()
val arraySize = value.value.size
if(arraySpecSize!=null && arraySpecSize>0) {
if(arraySpecSize<1 || arraySpecSize>256)
@ -1100,7 +1100,7 @@ internal class AstChecker(private val program: Program,
if(value.type.istype(targetDt)) {
if(!checkArrayValues(value, targetDt))
return false
val arraySpecSize = arrayspec.size()
val arraySpecSize = arrayspec.constIndex()
val arraySize = value.value.size
if(arraySpecSize!=null && arraySpecSize>0) {
if(arraySpecSize<1 || arraySpecSize>128)
@ -1123,7 +1123,7 @@ internal class AstChecker(private val program: Program,
if(!checkArrayValues(value, targetDt))
return false
val arraySize = value.value.size
val arraySpecSize = arrayspec.size()
val arraySpecSize = arrayspec.constIndex()
if(arraySpecSize!=null && arraySpecSize>0) {
if(arraySpecSize < 1 || arraySpecSize>51)
return err("float array length must be 1-51")

View File

@ -261,8 +261,8 @@ open class VarDecl(val type: VarDeclType,
member.isArray,
true,
member.position
) as Statement
}.toMutableList()
)
}.toMutableList<Statement>()
structHasBeenFlattened = true
return result
}
@ -300,7 +300,7 @@ class ArrayIndex(var index: Expression, override val position: Position) : Node
return("ArrayIndex($index, pos=$position)")
}
fun size() = (index as? NumericLiteralValue)?.number?.toInt()
fun constIndex() = (index as? NumericLiteralValue)?.number?.toInt()
}
open class Assignment(var target: AssignTarget, var value: Expression, override val position: Position) : Statement() {
@ -449,9 +449,9 @@ data class AssignTarget(var identifier: IdentifierReference?,
this.identifier!=null -> value is IdentifierReference && value.nameInSource==identifier!!.nameInSource
this.arrayindexed!=null -> value is ArrayIndexedExpression &&
value.identifier.nameInSource==arrayindexed!!.identifier.nameInSource &&
value.arrayspec.size()!=null &&
arrayindexed!!.arrayspec.size()!=null &&
value.arrayspec.size()==arrayindexed!!.arrayspec.size()
value.arrayspec.constIndex()!=null &&
arrayindexed!!.arrayspec.constIndex()!=null &&
value.arrayspec.constIndex()==arrayindexed!!.arrayspec.constIndex()
else -> false
}
}

View File

@ -15,6 +15,7 @@ import prog8.compiler.target.c64.C64MachineDefinition
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_HI_HEX
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_LO_HEX
import prog8.compiler.target.c64.Petscii
import prog8.compiler.target.c64.codegen.assignment.AssignmentAsmGen
import prog8.compiler.target.generatedLabelPrefix
import prog8.functions.BuiltinFunctions
import prog8.functions.FSignature
@ -317,7 +318,7 @@ internal class AsmGen(private val program: Program,
else {
// no init value, use zeros
val zero = decl.zeroElementValue()
Array(decl.arraysize!!.size()!!) { zero }
Array(decl.arraysize!!.constIndex()!!) { zero }
}
val floatFills = array.map {
val number = (it as NumericLiteralValue).number
@ -392,7 +393,7 @@ internal class AsmGen(private val program: Program,
else {
// no array init value specified, use a list of zeros
val zero = decl.zeroElementValue()
Array(decl.arraysize!!.size()!!) { zero }
Array(decl.arraysize!!.constIndex()!!) { zero }
}
return when (decl.datatype) {
DataType.ARRAY_UB ->
@ -419,7 +420,7 @@ internal class AsmGen(private val program: Program,
else {
// no array init value specified, use a list of zeros
val zero = decl.zeroElementValue()
Array(decl.arraysize!!.size()!!) { zero }
Array(decl.arraysize!!.constIndex()!!) { zero }
}
return when (decl.datatype) {
DataType.ARRAY_UB ->

View File

@ -78,7 +78,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
if (variable is IdentifierReference) {
val decl = variable.targetVarDecl(program.namespace)!!
val varName = asmgen.asmIdentifierName(variable)
val numElements = decl.arraysize!!.size()
val numElements = decl.arraysize!!.constIndex()
when (decl.datatype) {
DataType.ARRAY_UB, DataType.ARRAY_B -> {
asmgen.out("""
@ -120,7 +120,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
if (variable is IdentifierReference) {
val decl = variable.targetVarDecl(program.namespace)!!
val varName = asmgen.asmIdentifierName(variable)
val numElements = decl.arraysize!!.size()
val numElements = decl.arraysize!!.constIndex()
when (decl.datatype) {
DataType.ARRAY_UB, DataType.ARRAY_B -> {
asmgen.out("""
@ -487,7 +487,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
private fun outputPushAddressAndLenghtOfArray(arg: Expression) {
arg as IdentifierReference
val identifierName = asmgen.asmIdentifierName(arg)
val size = arg.targetVarDecl(program.namespace)!!.arraysize!!.size()!!
val size = arg.targetVarDecl(program.namespace)!!.arraysize!!.constIndex()!!
asmgen.out("""
lda #<$identifierName
sta $ESTACK_LO_HEX,x

View File

@ -290,7 +290,7 @@ $loopLabel lda ${65535.toHex()} ; modified
$endLabel""")
}
DataType.ARRAY_UB, DataType.ARRAY_B -> {
val length = decl.arraysize!!.size()!!
val length = decl.arraysize!!.constIndex()!!
val indexVar = asmgen.makeLabel("for_index")
asmgen.out("""
ldy #0
@ -324,7 +324,7 @@ $indexVar .byte 0""")
asmgen.out(endLabel)
}
DataType.ARRAY_W, DataType.ARRAY_UW -> {
val length = decl.arraysize!!.size()!! * 2
val length = decl.arraysize!!.constIndex()!! * 2
val indexVar = asmgen.makeLabel("for_index")
val loopvarName = asmgen.asmIdentifierName(stmt.loopVar)
asmgen.out("""

View File

@ -1,4 +1,4 @@
package prog8.compiler.target.c64.codegen
package prog8.compiler.target.c64.codegen.assignment
import prog8.ast.Program
import prog8.ast.base.*
@ -7,9 +7,10 @@ import prog8.ast.statements.AssignTarget
import prog8.ast.statements.Assignment
import prog8.ast.statements.DirectMemoryWrite
import prog8.compiler.AssemblyError
import prog8.compiler.target.c64.codegen.AsmGen
enum class TargetStorageKind {
internal enum class TargetStorageKind {
VARIABLE,
ARRAY,
MEMORY,
@ -17,7 +18,7 @@ enum class TargetStorageKind {
STACK
}
enum class SourceStorageKind {
internal enum class SourceStorageKind {
LITERALNUMBER,
VARIABLE,
ARRAY,
@ -39,7 +40,7 @@ internal class AsmAssignTarget(val kind: TargetStorageKind,
)
{
val constMemoryAddress by lazy { memory?.addressExpression?.constValue(program)?.number?.toInt() ?: 0}
val constArrayIndexValue by lazy { array?.arrayspec?.size() ?: 0 }
val constArrayIndexValue by lazy { array?.arrayspec?.constIndex() }
val vardecl by lazy { variable?.targetVarDecl(program.namespace)!! }
val asmName by lazy {
if(variable!=null)
@ -82,7 +83,7 @@ internal class AsmAssignSource(val kind: SourceStorageKind,
)
{
val constMemoryAddress by lazy { memory?.addressExpression?.constValue(program)?.number?.toInt() ?: 0}
val constArrayIndexValue by lazy { array?.arrayspec?.size() ?: 0 }
val constArrayIndexValue by lazy { array?.arrayspec?.constIndex() }
val vardecl by lazy { variable?.targetVarDecl(program.namespace)!! }
companion object {

View File

@ -1,4 +1,4 @@
package prog8.compiler.target.c64.codegen
package prog8.compiler.target.c64.codegen.assignment
import prog8.ast.Program
import prog8.ast.base.*
@ -9,6 +9,7 @@ import prog8.compiler.target.c64.C64MachineDefinition
import prog8.compiler.target.c64.C64MachineDefinition.C64Zeropage
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_HI_HEX
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_LO_HEX
import prog8.compiler.target.c64.codegen.AsmGen
import prog8.compiler.toHex
// TODO optimize the array indexes where the index is a constant
@ -18,7 +19,7 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
private val augmentableAsmGen = AugmentableAssignmentAsmGen(program, this, asmgen)
internal fun translate(assignment: Assignment) {
fun translate(assignment: Assignment) {
var source = AsmAssignSource.fromAstSource(assignment.value, program)
val target = AsmAssignTarget.fromAstAssignment(assignment, program, asmgen)
@ -33,8 +34,15 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
val assign = AsmAssignment(source, target, assignment.isAugmentable, assignment.position)
when {
source.kind==SourceStorageKind.LITERALNUMBER -> {
if(assign.isAugmentable)
augmentableAsmGen.translate(assign)
else
translateNormalAssignment(assign)
}
fun translateNormalAssignment(assign: AsmAssignment) {
when(assign.source.kind) {
SourceStorageKind.LITERALNUMBER -> {
// simple case: assign a constant number
val num = assign.source.number!!.number
when (assign.source.datatype) {
@ -44,7 +52,7 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
else -> throw AssemblyError("weird numval type")
}
}
source.kind==SourceStorageKind.VARIABLE -> {
SourceStorageKind.VARIABLE -> {
// simple case: assign from another variable
val variable = assign.source.variable!!
when (assign.source.datatype) {
@ -55,22 +63,6 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
else -> throw AssemblyError("unsupported assignment target type ${assign.target.datatype}")
}
}
assign.isAugmentable -> {
// special case: the in-place assignment / modification
augmentableAsmGen.translate(assign)
}
else -> translateOtherAssignment(assign)
}
}
internal fun translateOtherAssignment(assign: AsmAssignment) {
// source kind: expression, register, stack (only expression implemented for now)
when(assign.source.kind) {
SourceStorageKind.LITERALNUMBER,
SourceStorageKind.VARIABLE -> {
throw AssemblyError("assignment value type ${assign.source.kind} should have been handled elsewhere")
}
SourceStorageKind.ARRAY -> {
val value = assign.source.array!!
val elementDt = assign.source.datatype
@ -131,7 +123,7 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
}
}
internal fun assignToRegister(reg: CpuRegister, value: Int?, identifier: IdentifierReference?) {
fun assignToRegister(reg: CpuRegister, value: Int?, identifier: IdentifierReference?) {
if(value!=null) {
asmgen.out(" ld${reg.toString().toLowerCase()} #${value.toHex()}")
} else if(identifier!=null) {

View File

@ -1,4 +1,4 @@
package prog8.compiler.target.c64.codegen
package prog8.compiler.target.c64.codegen.assignment
import prog8.ast.Program
import prog8.ast.base.*
@ -7,6 +7,7 @@ import prog8.compiler.AssemblyError
import prog8.compiler.target.c64.C64MachineDefinition.C64Zeropage
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_HI_PLUS1_HEX
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_LO_PLUS1_HEX
import prog8.compiler.target.c64.codegen.AsmGen
import prog8.compiler.toHex
internal class AugmentableAssignmentAsmGen(private val program: Program,
@ -210,7 +211,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
}
TargetStorageKind.ARRAY -> {
println("*** TODO optimize simple inplace array assignment ${target.array} $operator= $value")
assignmentAsmGen.translateOtherAssignment(target.origAssign) // TODO get rid of this fallback for the most common cases here
assignmentAsmGen.translateNormalAssignment(target.origAssign) // TODO get rid of this fallback for the most common cases here
}
TargetStorageKind.REGISTER -> TODO()
TargetStorageKind.STACK -> TODO()

View File

@ -258,7 +258,7 @@ private fun builtinSizeof(args: List<Expression>, position: Position, program: P
return when {
dt.typeOrElse(DataType.STRUCT) in ArrayDatatypes -> {
val length = (target as VarDecl).arraysize!!.size() ?: throw CannotEvaluateException("sizeof", "unknown array size")
val length = (target as VarDecl).arraysize!!.constIndex() ?: throw CannotEvaluateException("sizeof", "unknown array size")
val elementDt = ArrayElementTypes.getValue(dt.typeOrElse(DataType.STRUCT))
numericLiteral(elementDt.memorySize() * length, position)
}
@ -293,7 +293,7 @@ private fun builtinLen(args: List<Expression>, position: Position, program: Prog
throw SyntaxError("len requires one argument", position)
val directMemVar = ((args[0] as? DirectMemoryRead)?.addressExpression as? IdentifierReference)?.targetVarDecl(program.namespace)
var arraySize = directMemVar?.arraysize?.size()
var arraySize = directMemVar?.arraysize?.constIndex()
if(arraySize != null)
return NumericLiteralValue.optimalInteger(arraySize, position)
if(args[0] is ArrayLiteralValue)
@ -305,7 +305,7 @@ private fun builtinLen(args: List<Expression>, position: Position, program: Prog
return when(target.datatype) {
DataType.ARRAY_UB, DataType.ARRAY_B, DataType.ARRAY_UW, DataType.ARRAY_W, DataType.ARRAY_F -> {
arraySize = target.arraysize?.size()
arraySize = target.arraysize?.constIndex()
if(arraySize==null)
throw CannotEvaluateException("len", "arraysize unknown")
NumericLiteralValue.optimalInteger(arraySize, args[0].position)

View File

@ -56,7 +56,7 @@ internal class ConstantIdentifierReplacer(private val program: Program, private
))
}
}
else if(decl.arraysize?.size()==null) {
else if(decl.arraysize?.constIndex()==null) {
val size = decl.arraysize!!.index.constValue(program)
if(size!=null) {
return listOf(IAstModification.SetExpression(
@ -81,7 +81,7 @@ internal class ConstantIdentifierReplacer(private val program: Program, private
val rangeExpr = decl.value as? RangeExpr
if(rangeExpr!=null) {
// convert the initializer range expression to an actual array
val declArraySize = decl.arraysize?.size()
val declArraySize = decl.arraysize?.constIndex()
if(declArraySize!=null && declArraySize!=rangeExpr.size())
errors.err("range expression size doesn't match declared array size", decl.value?.position!!)
val constRange = rangeExpr.toConstantIntegerRange()
@ -101,7 +101,7 @@ internal class ConstantIdentifierReplacer(private val program: Program, private
}
if(numericLv!=null && numericLv.type==DataType.FLOAT)
errors.err("arraysize requires only integers here", numericLv.position)
val size = decl.arraysize?.size() ?: return noModifications
val size = decl.arraysize?.constIndex() ?: return noModifications
if (rangeExpr==null && numericLv!=null) {
// arraysize initializer is empty or a single int, and we know the size; create the arraysize.
val fillvalue = numericLv.number.toInt()
@ -125,18 +125,18 @@ internal class ConstantIdentifierReplacer(private val program: Program, private
else -> {}
}
// create the array itself, filled with the fillvalue.
val array = Array(size) {fillvalue}.map { NumericLiteralValue(ArrayElementTypes.getValue(decl.datatype), it, numericLv.position) as Expression}.toTypedArray()
val array = Array(size) {fillvalue}.map { NumericLiteralValue(ArrayElementTypes.getValue(decl.datatype), it, numericLv.position) }.toTypedArray<Expression>()
val refValue = ArrayLiteralValue(InferredTypes.InferredType.known(decl.datatype), array, position = numericLv.position)
return listOf(IAstModification.ReplaceNode(decl.value!!, refValue, decl))
}
}
DataType.ARRAY_F -> {
val size = decl.arraysize?.size() ?: return noModifications
val size = decl.arraysize?.constIndex() ?: return noModifications
val litval = decl.value as? NumericLiteralValue
val rangeExpr = decl.value as? RangeExpr
if(rangeExpr!=null) {
// convert the initializer range expression to an actual array of floats
val declArraySize = decl.arraysize?.size()
val declArraySize = decl.arraysize?.constIndex()
if(declArraySize!=null && declArraySize!=rangeExpr.size())
errors.err("range expression size doesn't match declared array size", decl.value?.position!!)
val constRange = rangeExpr.toConstantIntegerRange()
@ -154,7 +154,7 @@ internal class ConstantIdentifierReplacer(private val program: Program, private
errors.err("float value overflow", litval.position)
else {
// create the array itself, filled with the fillvalue.
val array = Array(size) {fillvalue}.map { NumericLiteralValue(DataType.FLOAT, it, litval.position) as Expression}.toTypedArray()
val array = Array(size) {fillvalue}.map { NumericLiteralValue(DataType.FLOAT, it, litval.position) }.toTypedArray<Expression>()
val refValue = ArrayLiteralValue(InferredTypes.InferredType.known(DataType.ARRAY_F), array, position = litval.position)
return listOf(IAstModification.ReplaceNode(decl.value!!, refValue, decl))
}

View File

@ -229,7 +229,7 @@ internal class StatementOptimizer(private val program: Program,
}
}
else if(iterable.datatype in ArrayDatatypes) {
val size = iterable.arraysize!!.size()
val size = iterable.arraysize!!.constIndex()
if(size==1) {
// loop over array of length 1 -> just assign the single value
val av = (iterable.value as ArrayLiteralValue).value[0].constValue(program)?.number

View File

@ -4,7 +4,6 @@ import org.antlr.v4.runtime.*
import prog8.ast.Module
import prog8.ast.Program
import prog8.ast.antlr.toAst
import prog8.ast.base.ErrorReporter
import prog8.ast.base.Position
import prog8.ast.base.SyntaxError
import prog8.ast.base.checkImportedValid
@ -34,7 +33,7 @@ internal class CustomLexer(val modulePath: Path, input: CharStream?) : prog8Lexe
internal fun moduleName(fileName: Path) = fileName.toString().substringBeforeLast('.')
internal class ModuleImporter() {
internal class ModuleImporter {
internal fun importModule(program: Program, filePath: Path): Module {
print("importing '${moduleName(filePath.fileName)}'")