mirror of
https://github.com/irmen/prog8.git
synced 2024-09-07 19:54:26 +00:00
zp test added, some cleanups
This commit is contained in:
parent
b5c5560af8
commit
2665618fa6
@ -497,7 +497,11 @@ class ReferenceLiteralValue(val type: DataType, // only reference types allo
|
|||||||
throw FatalAstException("weird array element $it")
|
throw FatalAstException("weird array element $it")
|
||||||
it
|
it
|
||||||
} else {
|
} else {
|
||||||
num.cast(elementType) // TODO this can throw an exception
|
try {
|
||||||
|
num.cast(elementType)
|
||||||
|
} catch(x: ExpressionError) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}.toTypedArray()
|
}.toTypedArray()
|
||||||
return ReferenceLiteralValue(targettype, null, array=castArray, position = position)
|
return ReferenceLiteralValue(targettype, null, array=castArray, position = position)
|
||||||
@ -668,7 +672,7 @@ data class IdentifierReference(val nameInSource: List<String>, override val posi
|
|||||||
|
|
||||||
override fun accept(visitor: IAstModifyingVisitor) = visitor.visit(this)
|
override fun accept(visitor: IAstModifyingVisitor) = visitor.visit(this)
|
||||||
override fun accept(visitor: IAstVisitor) = visitor.visit(this)
|
override fun accept(visitor: IAstVisitor) = visitor.visit(this)
|
||||||
override fun referencesIdentifiers(vararg name: String): Boolean = nameInSource.last() in name // @todo is this correct all the time?
|
override fun referencesIdentifiers(vararg name: String): Boolean = nameInSource.last() in name
|
||||||
|
|
||||||
override fun inferType(program: Program): DataType? {
|
override fun inferType(program: Program): DataType? {
|
||||||
val targetStmt = targetStatement(program.namespace)
|
val targetStmt = targetStatement(program.namespace)
|
||||||
|
@ -390,7 +390,7 @@ internal fun fixupArrayDatatype(array: ReferenceLiteralValue, vardecl: VarDecl,
|
|||||||
}
|
}
|
||||||
vardecl.value = litval2
|
vardecl.value = litval2
|
||||||
litval2.linkParents(vardecl)
|
litval2.linkParents(vardecl)
|
||||||
litval2.addToHeap(heap) // TODO is the previous array discarded from the resulting asm code?
|
litval2.addToHeap(heap)
|
||||||
return litval2
|
return litval2
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -13,7 +13,7 @@ abstract class Zeropage(protected val options: CompilationOptions) {
|
|||||||
|
|
||||||
val allowedDatatypes = NumericDatatypes
|
val allowedDatatypes = NumericDatatypes
|
||||||
|
|
||||||
fun available() = free.size
|
fun available() = if(options.zeropage==ZeropageType.DONTUSE) 0 else free.size
|
||||||
|
|
||||||
fun allocate(scopedname: String, datatype: DataType, position: Position?): Int {
|
fun allocate(scopedname: String, datatype: DataType, position: Position?): Int {
|
||||||
assert(scopedname.isEmpty() || !allocations.values.any { it.first==scopedname } ) {"isSameAs scopedname can't be allocated twice"}
|
assert(scopedname.isEmpty() || !allocations.values.any { it.first==scopedname } ) {"isSameAs scopedname can't be allocated twice"}
|
||||||
|
@ -210,7 +210,7 @@ class ConstantFolding(private val program: Program) : IAstModifyingVisitor {
|
|||||||
val possibleDts = arg.second.possibleDatatypes
|
val possibleDts = arg.second.possibleDatatypes
|
||||||
val argConst = arg.first.value.constValue(program)
|
val argConst = arg.first.value.constValue(program)
|
||||||
if(argConst!=null && argConst.type !in possibleDts) {
|
if(argConst!=null && argConst.type !in possibleDts) {
|
||||||
val convertedValue = argConst.cast(possibleDts.first()) // TODO can throw exception
|
val convertedValue = argConst.cast(possibleDts.first())
|
||||||
functionCall.arglist[arg.first.index] = convertedValue
|
functionCall.arglist[arg.first.index] = convertedValue
|
||||||
optimizationsDone++
|
optimizationsDone++
|
||||||
}
|
}
|
||||||
@ -226,7 +226,7 @@ class ConstantFolding(private val program: Program) : IAstModifyingVisitor {
|
|||||||
val expectedDt = arg.second.type
|
val expectedDt = arg.second.type
|
||||||
val argConst = arg.first.value.constValue(program)
|
val argConst = arg.first.value.constValue(program)
|
||||||
if(argConst!=null && argConst.type!=expectedDt) {
|
if(argConst!=null && argConst.type!=expectedDt) {
|
||||||
val convertedValue = argConst.cast(expectedDt) // TODO can throw exception
|
val convertedValue = argConst.cast(expectedDt)
|
||||||
functionCall.arglist[arg.first.index] = convertedValue
|
functionCall.arglist[arg.first.index] = convertedValue
|
||||||
optimizationsDone++
|
optimizationsDone++
|
||||||
}
|
}
|
||||||
@ -356,7 +356,7 @@ class ConstantFolding(private val program: Program) : IAstModifyingVisitor {
|
|||||||
subleftIsConst: Boolean,
|
subleftIsConst: Boolean,
|
||||||
subrightIsConst: Boolean): Expression
|
subrightIsConst: Boolean): Expression
|
||||||
{
|
{
|
||||||
// @todo this implements only a small set of possible reorderings for now
|
// todo: this implements only a small set of possible reorderings at this time
|
||||||
if(expr.operator==subExpr.operator) {
|
if(expr.operator==subExpr.operator) {
|
||||||
// both operators are the isSameAs.
|
// both operators are the isSameAs.
|
||||||
// If + or *, we can simply swap the const of expr and Var in subexpr.
|
// If + or *, we can simply swap the const of expr and Var in subexpr.
|
||||||
@ -544,11 +544,15 @@ class ConstantFolding(private val program: Program) : IAstModifyingVisitor {
|
|||||||
override fun visit(forLoop: ForLoop): Statement {
|
override fun visit(forLoop: ForLoop): Statement {
|
||||||
|
|
||||||
fun adjustRangeDt(rangeFrom: NumericLiteralValue, targetDt: DataType, rangeTo: NumericLiteralValue, stepLiteral: NumericLiteralValue?, range: RangeExpr): RangeExpr {
|
fun adjustRangeDt(rangeFrom: NumericLiteralValue, targetDt: DataType, rangeTo: NumericLiteralValue, stepLiteral: NumericLiteralValue?, range: RangeExpr): RangeExpr {
|
||||||
// TODO casts can throw exception
|
val newFrom: NumericLiteralValue
|
||||||
val newFrom = rangeFrom.cast(targetDt)
|
val newTo: NumericLiteralValue
|
||||||
val newTo = rangeTo.cast(targetDt)
|
try {
|
||||||
val newStep: Expression =
|
newFrom = rangeFrom.cast(targetDt)
|
||||||
stepLiteral?.cast(targetDt) ?: range.step
|
newTo = rangeTo.cast(targetDt)
|
||||||
|
} catch (x: ExpressionError) {
|
||||||
|
return range
|
||||||
|
}
|
||||||
|
val newStep: Expression = stepLiteral?.cast(targetDt) ?: range.step
|
||||||
return RangeExpr(newFrom, newTo, newStep, range.position)
|
return RangeExpr(newFrom, newTo, newStep, range.position)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -169,7 +169,15 @@ class TestZeropage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO test dontuse option
|
@Test
|
||||||
|
fun testZpDontuse() {
|
||||||
|
val zp = C64Zeropage(CompilationOptions(OutputType.RAW, LauncherType.NONE, ZeropageType.DONTUSE, emptyList(), false))
|
||||||
|
println(zp.free)
|
||||||
|
assertEquals(0, zp.available())
|
||||||
|
assertFailsWith<CompilerException> {
|
||||||
|
zp.allocate("", DataType.BYTE, null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testFreeSpaces() {
|
fun testFreeSpaces() {
|
||||||
|
Loading…
Reference in New Issue
Block a user