replaced many short/int values by unsigned types if appropriate

This commit is contained in:
Irmen de Jong
2021-11-21 00:48:23 +01:00
parent c0035ba1a2
commit b292124f3c
29 changed files with 238 additions and 212 deletions

View File

@@ -16,12 +16,12 @@ fun AssignTarget.isIOAddress(machine: IMachineDefinition): Boolean {
memAddr != null -> {
val addr = memAddr.addressExpression.constValue(definingModule.program)
if(addr!=null)
return machine.isIOAddress(addr.number.toInt())
return machine.isIOAddress(addr.number.toUInt())
return when (memAddr.addressExpression) {
is IdentifierReference -> {
val decl = (memAddr.addressExpression as IdentifierReference).targetVarDecl(definingModule.program)
val result = if ((decl?.type == VarDeclType.MEMORY || decl?.type == VarDeclType.CONST) && decl.value is NumericLiteralValue)
machine.isIOAddress((decl.value as NumericLiteralValue).number.toInt())
machine.isIOAddress((decl.value as NumericLiteralValue).number.toUInt())
else
false
result
@@ -34,7 +34,7 @@ fun AssignTarget.isIOAddress(machine: IMachineDefinition): Boolean {
return if (targetStmt?.type == VarDeclType.MEMORY) {
val addr = targetStmt.value as? NumericLiteralValue
if (addr != null)
machine.isIOAddress(addr.number.toInt())
machine.isIOAddress(addr.number.toUInt())
else
false
} else false
@@ -42,7 +42,7 @@ fun AssignTarget.isIOAddress(machine: IMachineDefinition): Boolean {
ident != null -> {
val decl = ident.targetVarDecl(definingModule.program) ?: throw FatalAstException("invalid identifier ${ident.nameInSource}")
return if (decl.type == VarDeclType.MEMORY && decl.value is NumericLiteralValue)
machine.isIOAddress((decl.value as NumericLiteralValue).number.toInt())
machine.isIOAddress((decl.value as NumericLiteralValue).number.toUInt())
else
false
}

View File

@@ -513,6 +513,8 @@ private fun builtinSgn(args: List<Expression>, position: Position, program: Prog
return NumericLiteralValue(DataType.BYTE, constval.number.sign, position)
}
private fun numericLiteral(value: UInt, position: Position): NumericLiteralValue = numericLiteral(value.toInt(), position)
private fun numericLiteral(value: Number, position: Position): NumericLiteralValue {
val floatNum=value.toDouble()
val tweakedValue: Number =

View File

@@ -21,7 +21,7 @@ enum class ZeropageType {
class CompilationOptions(val output: OutputType,
val launcher: LauncherType,
val zeropage: ZeropageType,
val zpReserved: List<IntRange>,
val zpReserved: List<UIntRange>,
val floats: Boolean,
val noSysInit: Boolean,
val compTarget: ICompilationTarget,

View File

@@ -18,10 +18,10 @@ interface IMachineDefinition {
val FLOAT_MAX_POSITIVE: Double
val FLOAT_MEM_SIZE: Int
val POINTER_MEM_SIZE: Int
val ESTACK_LO: Int
val ESTACK_HI: Int
val BASIC_LOAD_ADDRESS : Int
val RAW_LOAD_ADDRESS : Int
val ESTACK_LO: UInt
val ESTACK_HI: UInt
val BASIC_LOAD_ADDRESS : UInt
val RAW_LOAD_ADDRESS : UInt
val opcodeNames: Set<String>
var zeropage: Zeropage
@@ -32,5 +32,5 @@ interface IMachineDefinition {
fun importLibs(compilerOptions: CompilationOptions, compilationTargetName: String): List<String>
fun launchEmulator(selectedEmulator: Int, programNameWithPath: Path)
fun isIOAddress(address: Int): Boolean
fun isIOAddress(address: UInt): Boolean
}

View File

@@ -8,14 +8,14 @@ class ZeropageDepletedError(message: String) : Exception(message)
abstract class Zeropage(protected val options: CompilationOptions) {
abstract val SCRATCH_B1 : Int // temp storage for a single byte
abstract val SCRATCH_REG : Int // temp storage for a register
abstract val SCRATCH_W1 : Int // temp storage 1 for a word $fb+$fc
abstract val SCRATCH_W2 : Int // temp storage 2 for a word $fb+$fc
abstract val SCRATCH_B1 : UInt // temp storage for a single byte
abstract val SCRATCH_REG : UInt // temp storage for a register
abstract val SCRATCH_W1 : UInt // temp storage 1 for a word $fb+$fc
abstract val SCRATCH_W2 : UInt // temp storage 2 for a word $fb+$fc
private val allocations = mutableMapOf<Int, Pair<String, DataType>>()
val free = mutableListOf<Int>() // subclasses must set this to the appropriate free locations.
private val allocations = mutableMapOf<UInt, Pair<String, DataType>>()
val free = mutableListOf<UInt>() // subclasses must set this to the appropriate free locations.
val allowedDatatypes = NumericDatatypes
@@ -23,7 +23,7 @@ abstract class Zeropage(protected val options: CompilationOptions) {
for (reserved in options.zpReserved)
reserve(reserved)
free.removeAll(setOf(SCRATCH_B1, SCRATCH_REG, SCRATCH_W1, SCRATCH_W1 + 1, SCRATCH_W2, SCRATCH_W2 + 1))
free.removeAll(setOf(SCRATCH_B1, SCRATCH_REG, SCRATCH_W1, SCRATCH_W1 + 1u, SCRATCH_W2, SCRATCH_W2 + 1u))
}
fun availableBytes() = if(options.zeropage== ZeropageType.DONTUSE) 0 else free.size
@@ -32,9 +32,9 @@ abstract class Zeropage(protected val options: CompilationOptions) {
if(options.zeropage== ZeropageType.DONTUSE)
return 0
val words = free.windowed(2).filter { it[0] == it[1]-1 }
val words = free.windowed(2).filter { it[0] == it[1]-1u }
var nonOverlappingWordsCount = 0
var prevMsbLoc = -1
var prevMsbLoc = UInt.MAX_VALUE
for(w in words) {
if(w[0]!=prevMsbLoc) {
nonOverlappingWordsCount++
@@ -47,16 +47,16 @@ abstract class Zeropage(protected val options: CompilationOptions) {
if(options.zeropage== ZeropageType.DONTUSE)
return false
return free.windowed(2).any { it[0] == it[1] - 1 }
return free.windowed(2).any { it[0] == it[1] - 1u }
}
fun allocate(scopedname: String, datatype: DataType, position: Position?, errors: IErrorReporter): Int {
fun allocate(scopedname: String, datatype: DataType, position: Position?, errors: IErrorReporter): UInt {
require(scopedname.isEmpty() || !allocations.values.any { it.first==scopedname } ) {"scopedname can't be allocated twice"}
if(options.zeropage== ZeropageType.DONTUSE)
throw InternalCompilerException("zero page usage has been disabled")
val size =
val size: Int =
when (datatype) {
in ByteDatatypes -> 1
in WordDatatypes -> 2
@@ -66,7 +66,7 @@ abstract class Zeropage(protected val options: CompilationOptions) {
errors.warn("allocated a large value (float) in zeropage", position)
else
errors.warn("$scopedname: allocated a large value (float) in zeropage", position ?: Position.DUMMY)
5
options.compTarget.machine.FLOAT_MEM_SIZE
} else throw InternalCompilerException("floating point option not enabled")
}
else -> throw InternalCompilerException("cannot put datatype $datatype in zeropage")
@@ -74,13 +74,13 @@ abstract class Zeropage(protected val options: CompilationOptions) {
if(free.size > 0) {
if(size==1) {
for(candidate in free.minOrNull()!! .. free.maxOrNull()!!+1) {
for(candidate in free.minOrNull()!! .. free.maxOrNull()!!+1u) {
if(loneByte(candidate))
return makeAllocation(candidate, 1, datatype, scopedname)
}
return makeAllocation(free[0], 1, datatype, scopedname)
}
for(candidate in free.minOrNull()!! .. free.maxOrNull()!!+1) {
for(candidate in free.minOrNull()!! .. free.maxOrNull()!!+1u) {
if (sequentialFree(candidate, size))
return makeAllocation(candidate, size, datatype, scopedname)
}
@@ -89,14 +89,18 @@ abstract class Zeropage(protected val options: CompilationOptions) {
throw ZeropageDepletedError("ERROR: no free space in ZP to allocate $size sequential bytes")
}
protected fun reserve(range: IntRange) = free.removeAll(range)
protected fun reserve(range: UIntRange) = free.removeAll(range)
private fun makeAllocation(address: Int, size: Int, datatype: DataType, name: String?): Int {
free.removeAll(address until address+size)
private fun makeAllocation(address: UInt, size: Int, datatype: DataType, name: String?): UInt {
require(size>=0)
free.removeAll(address until address+size.toUInt())
allocations[address] = (name ?: "<unnamed>") to datatype
return address
}
private fun loneByte(address: Int) = address in free && address-1 !in free && address+1 !in free
private fun sequentialFree(address: Int, size: Int) = free.containsAll((address until address+size).toList())
private fun loneByte(address: UInt) = address in free && address-1u !in free && address+1u !in free
private fun sequentialFree(address: UInt, size: Int): Boolean {
require(size>0)
return free.containsAll((address until address+size.toUInt()).toList())
}
}