move asmgen test to codeGeneration module

This commit is contained in:
Irmen de Jong 2021-10-29 16:20:53 +02:00
parent a226b82d0b
commit 495a18805c
17 changed files with 101 additions and 78 deletions

View File

@ -545,16 +545,16 @@ class AsmGen(private val program: Program,
return scopedName return scopedName
} }
fun asmSymbolName(regs: RegisterOrPair): String = internal fun asmSymbolName(regs: RegisterOrPair): String =
if (regs in Cx16VirtualRegisters) if (regs in Cx16VirtualRegisters)
"cx16." + regs.toString().lowercase() "cx16." + regs.toString().lowercase()
else else
throw AssemblyError("no symbol name for register $regs") throw AssemblyError("no symbol name for register $regs")
fun asmSymbolName(name: String) = fixNameSymbols(name) internal fun asmSymbolName(name: String) = fixNameSymbols(name)
fun asmVariableName(name: String) = fixNameSymbols(name) internal fun asmVariableName(name: String) = fixNameSymbols(name)
fun asmSymbolName(name: Iterable<String>) = fixNameSymbols(name.joinToString(".")) internal fun asmSymbolName(name: Iterable<String>) = fixNameSymbols(name.joinToString("."))
fun asmVariableName(name: Iterable<String>) = fixNameSymbols(name.joinToString(".")) internal fun asmVariableName(name: Iterable<String>) = fixNameSymbols(name.joinToString("."))
internal fun loadByteFromPointerIntoA(pointervar: IdentifierReference): String { internal fun loadByteFromPointerIntoA(pointervar: IdentifierReference): String {

View File

@ -20,7 +20,7 @@ internal class ForLoopsAsmGen(private val program: Program, private val asmgen:
throw AssemblyError("unknown dt") throw AssemblyError("unknown dt")
when(stmt.iterable) { when(stmt.iterable) {
is RangeExpr -> { is RangeExpr -> {
val range = (stmt.iterable as RangeExpr).toConstantIntegerRange(asmgen.options.compTarget) val range = (stmt.iterable as RangeExpr).toConstantIntegerRange()
if(range==null) { if(range==null) {
translateForOverNonconstRange(stmt, iterableDt.getOr(DataType.UNDEFINED), stmt.iterable as RangeExpr) translateForOverNonconstRange(stmt, iterableDt.getOr(DataType.UNDEFINED), stmt.iterable as RangeExpr)
} else { } else {

View File

@ -1,4 +1,4 @@
package prog8tests package prog8tests.asmgen
import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo import org.hamcrest.Matchers.equalTo
@ -19,12 +19,10 @@ import prog8.compiler.target.c64.C64MachineDefinition
import prog8.compiler.target.cpu6502.codegen.AsmGen import prog8.compiler.target.cpu6502.codegen.AsmGen
import prog8.compilerinterface.* import prog8.compilerinterface.*
import prog8.parser.SourceCode import prog8.parser.SourceCode
import prog8tests.ast.helpers.DummyFunctions import prog8tests.asmgen.helpers.DummyFunctions
import prog8tests.ast.helpers.DummyMemsizer import prog8tests.asmgen.helpers.DummyMemsizer
import java.nio.file.Path import java.nio.file.Path
// TODO move to codegen project, readjust symbol protection levels
@TestInstance(TestInstance.Lifecycle.PER_CLASS) @TestInstance(TestInstance.Lifecycle.PER_CLASS)
class TestAsmGen6502 { class TestAsmGen6502 {

View File

@ -1,4 +1,4 @@
package prog8tests package prog8tests.asmgen
import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test

View File

@ -0,0 +1,27 @@
package prog8tests.asmgen.helpers
import prog8.ast.IBuiltinFunctions
import prog8.ast.base.Position
import prog8.ast.expressions.Expression
import prog8.ast.expressions.InferredTypes
import prog8.ast.expressions.NumericLiteralValue
import prog8.compiler.IMemSizer
import prog8.ast.base.DataType
internal val DummyFunctions = object : IBuiltinFunctions {
override val names: Set<String> = emptySet()
override val purefunctionNames: Set<String> = emptySet()
override fun constValue(
name: String,
args: List<Expression>,
position: Position,
memsizer: IMemSizer
): NumericLiteralValue? = null
override fun returnType(name: String, args: MutableList<Expression>) = InferredTypes.InferredType.unknown()
}
internal val DummyMemsizer = object : IMemSizer {
override fun memorySize(dt: DataType): Int = 0
}

View File

@ -141,9 +141,9 @@ internal class ConstantIdentifierReplacer(private val program: Program, private
if(rangeExpr!=null) { if(rangeExpr!=null) {
// convert the initializer range expression to an actual array // convert the initializer range expression to an actual array
val declArraySize = decl.arraysize?.constIndex() val declArraySize = decl.arraysize?.constIndex()
if(declArraySize!=null && declArraySize!=rangeExpr.size(compTarget)) if(declArraySize!=null && declArraySize!=rangeExpr.size())
errors.err("range expression size doesn't match declared array size", decl.value?.position!!) errors.err("range expression size doesn't match declared array size", decl.value?.position!!)
val constRange = rangeExpr.toConstantIntegerRange(compTarget) val constRange = rangeExpr.toConstantIntegerRange()
if(constRange!=null) { if(constRange!=null) {
val eltType = rangeExpr.inferType(program).getOr(DataType.UBYTE) val eltType = rangeExpr.inferType(program).getOr(DataType.UBYTE)
val newValue = if(eltType in ByteDatatypes) { val newValue = if(eltType in ByteDatatypes) {
@ -195,9 +195,9 @@ internal class ConstantIdentifierReplacer(private val program: Program, private
if(rangeExpr!=null) { if(rangeExpr!=null) {
// convert the initializer range expression to an actual array of floats // convert the initializer range expression to an actual array of floats
val declArraySize = decl.arraysize?.constIndex() val declArraySize = decl.arraysize?.constIndex()
if(declArraySize!=null && declArraySize!=rangeExpr.size(compTarget)) if(declArraySize!=null && declArraySize!=rangeExpr.size())
errors.err("range expression size (${rangeExpr.size(compTarget)}) doesn't match declared array size ($declArraySize)", decl.value?.position!!) errors.err("range expression size (${rangeExpr.size()}) doesn't match declared array size ($declArraySize)", decl.value?.position!!)
val constRange = rangeExpr.toConstantIntegerRange(compTarget) val constRange = rangeExpr.toConstantIntegerRange()
if(constRange!=null) { if(constRange!=null) {
val newValue = ArrayLiteralValue(InferredTypes.InferredType.known(DataType.ARRAY_F), val newValue = ArrayLiteralValue(InferredTypes.InferredType.known(DataType.ARRAY_F),
constRange.map { NumericLiteralValue(DataType.FLOAT, it.toDouble(), decl.value!!.position) }.toTypedArray(), constRange.map { NumericLiteralValue(DataType.FLOAT, it.toDouble(), decl.value!!.position) }.toTypedArray(),

View File

@ -198,7 +198,7 @@ class StatementOptimizer(private val program: Program,
val range = forLoop.iterable as? RangeExpr val range = forLoop.iterable as? RangeExpr
if(range!=null) { if(range!=null) {
if (range.size(compTarget) == 1) { if (range.size() == 1) {
// for loop over a (constant) range of just a single value-- optimize the loop away // for loop over a (constant) range of just a single value-- optimize the loop away
// loopvar/reg = range value , follow by block // loopvar/reg = range value , follow by block
val scope = AnonymousScope(mutableListOf(), forLoop.position) val scope = AnonymousScope(mutableListOf(), forLoop.position)

View File

@ -63,8 +63,7 @@ fun compileProgram(filepath: Path,
programAst, programAst,
errors, errors,
BuiltinFunctionsFacade(BuiltinFunctions), BuiltinFunctionsFacade(BuiltinFunctions),
compTarget, compTarget
compilationOptions
) )
postprocessAst(programAst, errors, compilationOptions) postprocessAst(programAst, errors, compilationOptions)
@ -266,7 +265,7 @@ private fun processAst(programAst: Program, errors: IErrorReporter, compilerOpti
errors.report() errors.report()
} }
private fun optimizeAst(programAst: Program, errors: IErrorReporter, functions: IBuiltinFunctions, compTarget: ICompilationTarget, options: CompilationOptions) { private fun optimizeAst(programAst: Program, errors: IErrorReporter, functions: IBuiltinFunctions, compTarget: ICompilationTarget) {
// optimize the parse tree // optimize the parse tree
println("Optimizing...") println("Optimizing...")

View File

@ -15,6 +15,8 @@ import prog8.parser.ParseError
import prog8.parser.SourceCode import prog8.parser.SourceCode
import prog8tests.ast.helpers.* import prog8tests.ast.helpers.*
import prog8tests.helpers.ErrorReporterForTests import prog8tests.helpers.ErrorReporterForTests
import prog8tests.helpers.DummyFunctions
import prog8tests.helpers.DummyMemsizer
import kotlin.io.path.* import kotlin.io.path.*
import kotlin.test.assertContains import kotlin.test.assertContains
import kotlin.test.assertFailsWith import kotlin.test.assertFailsWith

View File

@ -164,10 +164,10 @@ class TestCompilerOnRanges {
val expectedEnd = platform.encodeString("f", false)[0].toInt() val expectedEnd = platform.encodeString("f", false)[0].toInt()
val expectedStr = "$expectedStart .. $expectedEnd" val expectedStr = "$expectedStart .. $expectedEnd"
val intProgression = rangeExpr.toConstantIntegerRange(platform) val intProgression = rangeExpr.toConstantIntegerRange()
val actualStr = "${intProgression?.first} .. ${intProgression?.last}" val actualStr = "${intProgression?.first} .. ${intProgression?.last}"
assertEquals(expectedStr, actualStr,".first .. .last") assertEquals(expectedStr, actualStr,".first .. .last")
assertEquals(expectedEnd - expectedStart + 1, rangeExpr.size(platform), "rangeExpr.size()") assertEquals(expectedEnd - expectedStart + 1, rangeExpr.size(), "rangeExpr.size()")
} }
@Test @Test
@ -191,8 +191,8 @@ class TestCompilerOnRanges {
.map { it.iterable } .map { it.iterable }
.filterIsInstance<RangeExpr>()[0] .filterIsInstance<RangeExpr>()[0]
assertEquals(2, rangeExpr.size(platform)) assertEquals(2, rangeExpr.size())
val intProgression = rangeExpr.toConstantIntegerRange(platform) val intProgression = rangeExpr.toConstantIntegerRange()
assertEquals(0, intProgression?.first) assertEquals(0, intProgression?.first)
assertEquals(1, intProgression?.last) assertEquals(1, intProgression?.last)
} }
@ -218,8 +218,8 @@ class TestCompilerOnRanges {
.map { it.iterable } .map { it.iterable }
.filterIsInstance<RangeExpr>()[0] .filterIsInstance<RangeExpr>()[0]
assertEquals(9, rangeExpr.size(platform)) assertEquals(9, rangeExpr.size())
val intProgression = rangeExpr.toConstantIntegerRange(platform) val intProgression = rangeExpr.toConstantIntegerRange()
assertEquals(1, intProgression?.first) assertEquals(1, intProgression?.first)
assertEquals(9, intProgression?.last) assertEquals(9, intProgression?.last)
} }

View File

@ -15,8 +15,8 @@ import prog8.ast.statements.*
import prog8.compiler.target.C64Target import prog8.compiler.target.C64Target
import prog8.compilerinterface.isInRegularRAMof import prog8.compilerinterface.isInRegularRAMof
import prog8.parser.SourceCode import prog8.parser.SourceCode
import prog8tests.ast.helpers.DummyFunctions import prog8tests.helpers.DummyFunctions
import prog8tests.ast.helpers.DummyMemsizer import prog8tests.helpers.DummyMemsizer
import kotlin.test.assertFalse import kotlin.test.assertFalse
import kotlin.test.assertTrue import kotlin.test.assertTrue

View File

@ -0,0 +1,26 @@
package prog8tests.helpers
import prog8.ast.IBuiltinFunctions
import prog8.ast.base.DataType
import prog8.ast.base.Position
import prog8.ast.expressions.Expression
import prog8.ast.expressions.InferredTypes
import prog8.ast.expressions.NumericLiteralValue
import prog8.compiler.IMemSizer
internal val DummyFunctions = object : IBuiltinFunctions {
override val names: Set<String> = emptySet()
override val purefunctionNames: Set<String> = emptySet()
override fun constValue(
name: String,
args: List<Expression>,
position: Position,
memsizer: IMemSizer
): NumericLiteralValue? = null
override fun returnType(name: String, args: MutableList<Expression>) = InferredTypes.InferredType.unknown()
}
internal val DummyMemsizer = object : IMemSizer {
override fun memorySize(dt: DataType): Int = 0
}

View File

@ -1,13 +1,14 @@
package prog8tests.ast.helpers package prog8tests.ast.helpers
import prog8.ast.IBuiltinFunctions import prog8.ast.IBuiltinFunctions
import prog8.ast.base.DataType
import prog8.ast.base.Position import prog8.ast.base.Position
import prog8.ast.expressions.Expression import prog8.ast.expressions.Expression
import prog8.ast.expressions.InferredTypes import prog8.ast.expressions.InferredTypes
import prog8.ast.expressions.NumericLiteralValue import prog8.ast.expressions.NumericLiteralValue
import prog8.compiler.IMemSizer import prog8.compiler.IMemSizer
val DummyFunctions = object : IBuiltinFunctions { internal val DummyFunctions = object : IBuiltinFunctions {
override val names: Set<String> = emptySet() override val names: Set<String> = emptySet()
override val purefunctionNames: Set<String> = emptySet() override val purefunctionNames: Set<String> = emptySet()
override fun constValue( override fun constValue(
@ -19,3 +20,7 @@ val DummyFunctions = object : IBuiltinFunctions {
override fun returnType(name: String, args: MutableList<Expression>) = InferredTypes.InferredType.unknown() override fun returnType(name: String, args: MutableList<Expression>) = InferredTypes.InferredType.unknown()
} }
internal val DummyMemsizer = object : IMemSizer {
override fun memorySize(dt: DataType): Int = 0
}

View File

@ -1,8 +0,0 @@
package prog8tests.ast.helpers
import prog8.ast.base.DataType
import prog8.compiler.IMemSizer
val DummyMemsizer = object : IMemSizer {
override fun memorySize(dt: DataType): Int = 0
}

View File

@ -4,7 +4,6 @@ import prog8.ast.base.VarDeclType
import prog8.ast.expressions.IdentifierReference import prog8.ast.expressions.IdentifierReference
import prog8.ast.expressions.NumericLiteralValue import prog8.ast.expressions.NumericLiteralValue
import prog8.ast.expressions.RangeExpr import prog8.ast.expressions.RangeExpr
import prog8.ast.expressions.StringLiteralValue
import prog8.ast.statements.AssignTarget import prog8.ast.statements.AssignTarget
import kotlin.math.abs import kotlin.math.abs
@ -52,7 +51,7 @@ fun AssignTarget.isInRegularRAMof(machine: IMachineDefinition): Boolean {
} }
} }
fun RangeExpr.toConstantIntegerRange(encoding: IStringEncoding): IntProgression? { fun RangeExpr.toConstantIntegerRange(): IntProgression? {
fun makeRange(fromVal: Int, toVal: Int, stepVal: Int): IntProgression { fun makeRange(fromVal: Int, toVal: Int, stepVal: Int): IntProgression {
return when { return when {
@ -69,32 +68,20 @@ fun RangeExpr.toConstantIntegerRange(encoding: IStringEncoding): IntProgression?
} }
} }
val fromVal: Int val fromLv = from as? NumericLiteralValue
val toVal: Int val toLv = to as? NumericLiteralValue
val fromString = from as? StringLiteralValue if(fromLv==null || toLv==null)
val toString = to as? StringLiteralValue return null
if(fromString!=null && toString!=null ) { val fromVal = fromLv.number.toInt()
// TODO WHAT IS A STRING RANGE?????? val toVal = toLv.number.toInt()
// string range -> int range over character values
fromVal = encoding.encodeString(fromString.value, fromString.altEncoding)[0].toInt()
toVal = encoding.encodeString(toString.value, fromString.altEncoding)[0].toInt()
} else {
val fromLv = from as? NumericLiteralValue
val toLv = to as? NumericLiteralValue
if(fromLv==null || toLv==null)
return null // non-constant range
// integer range
fromVal = fromLv.number.toInt()
toVal = toLv.number.toInt()
}
val stepVal = (step as? NumericLiteralValue)?.number?.toInt() ?: 1 val stepVal = (step as? NumericLiteralValue)?.number?.toInt() ?: 1
return makeRange(fromVal, toVal, stepVal) return makeRange(fromVal, toVal, stepVal)
} }
fun RangeExpr.size(encoding: IStringEncoding): Int? { fun RangeExpr.size(): Int? {
val fromLv = (from as? NumericLiteralValue) val fromLv = (from as? NumericLiteralValue)
val toLv = (to as? NumericLiteralValue) val toLv = (to as? NumericLiteralValue)
if(fromLv==null || toLv==null) if(fromLv==null || toLv==null)
return null return null
return toConstantIntegerRange(encoding)?.count() return toConstantIntegerRange()?.count()
} }

View File

@ -5,7 +5,6 @@ import org.junit.jupiter.api.TestInstance
import prog8.ast.base.Position import prog8.ast.base.Position
import prog8.ast.expressions.NumericLiteralValue import prog8.ast.expressions.NumericLiteralValue
import prog8.ast.expressions.RangeExpr import prog8.ast.expressions.RangeExpr
import prog8.compilerinterface.IStringEncoding
import prog8.compilerinterface.size import prog8.compilerinterface.size
import kotlin.test.assertEquals import kotlin.test.assertEquals
@ -20,18 +19,6 @@ class TestAstExtensions {
NumericLiteralValue.optimalInteger(20, Position.DUMMY), NumericLiteralValue.optimalInteger(20, Position.DUMMY),
NumericLiteralValue.optimalInteger(2, Position.DUMMY), NumericLiteralValue.optimalInteger(2, Position.DUMMY),
Position.DUMMY) Position.DUMMY)
val encoding = DummyStringEncoding() assertEquals(6, expr.size())
assertEquals(6, expr.size(encoding))
}
class DummyStringEncoding : IStringEncoding {
override fun encodeString(str: String, altEncoding: Boolean): List<Short> {
TODO("Not yet implemented")
}
override fun decodeString(bytes: List<Short>, altEncoding: Boolean): String {
TODO("Not yet implemented")
}
} }
} }

View File

@ -1,9 +1,9 @@
main { main {
sub start() { sub start() {
fubar(1,2,3,4) ubyte xx
} for xx in "abcdef" to "zzz" {
xx++
sub fubar(ubyte aa, ubyte bb, ubyte cc, ubyte dd) { }
} }
} }