for arrays, use the element's datatype more instead of the array decl type

This commit is contained in:
Irmen de Jong 2020-08-23 12:03:52 +02:00
parent d2f0e74879
commit 97ce72521d
4 changed files with 25 additions and 28 deletions

View File

@ -539,14 +539,14 @@ internal class AsmGen(private val program: Program,
}
}
internal fun readAndPushArrayvalueWithIndexA(arrayDt: DataType, variable: IdentifierReference) {
internal fun readAndPushArrayvalueWithIndexA(elementDt: DataType, variable: IdentifierReference) {
val variablename = asmIdentifierName(variable)
when (arrayDt) {
DataType.STR, DataType.ARRAY_UB, DataType.ARRAY_B ->
when (elementDt) {
in ByteDatatypes ->
out(" tay | lda $variablename,y | sta $ESTACK_LO_HEX,x | dex")
DataType.ARRAY_UW, DataType.ARRAY_W ->
in WordDatatypes ->
out(" asl a | tay | lda $variablename,y | sta $ESTACK_LO_HEX,x | lda $variablename+1,y | sta $ESTACK_HI_HEX,x | dex")
DataType.ARRAY_F ->
DataType.FLOAT ->
// index * 5 is done in the subroutine that's called
out("""
sta $ESTACK_LO_HEX,x
@ -556,7 +556,7 @@ internal class AsmGen(private val program: Program,
jsr c64flt.push_float_from_indexed_var
""")
else ->
throw AssemblyError("weird array type")
throw AssemblyError("weird array elt type")
}
}

View File

@ -71,25 +71,25 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
}
AsmSourceStorageType.ARRAY -> {
val value = assign.source.astArray!!
val arrayDt = value.identifier.inferType(program).typeOrElse(DataType.STRUCT)
val elementDt = value.inferType(program).typeOrElse(DataType.STRUCT)
val index = value.arrayspec.index
if (index is NumericLiteralValue) {
// constant array index value
val arrayVarName = asmgen.asmIdentifierName(value.identifier)
val indexValue = index.number.toInt() * ArrayElementTypes.getValue(arrayDt).memorySize()
when (arrayDt) {
DataType.STR, DataType.ARRAY_UB, DataType.ARRAY_B ->
val indexValue = index.number.toInt() * elementDt.memorySize()
when (elementDt) {
in ByteDatatypes ->
asmgen.out(" lda $arrayVarName+$indexValue | sta $ESTACK_LO_HEX,x | dex")
DataType.ARRAY_UW, DataType.ARRAY_W ->
in WordDatatypes ->
asmgen.out(" lda $arrayVarName+$indexValue | sta $ESTACK_LO_HEX,x | lda $arrayVarName+$indexValue+1 | sta $ESTACK_HI_HEX,x | dex")
DataType.ARRAY_F ->
DataType.FLOAT ->
asmgen.out(" lda #<$arrayVarName+$indexValue | ldy #>$arrayVarName+$indexValue | jsr c64flt.push_float")
else ->
throw AssemblyError("weird array type")
}
} else {
asmgen.translateArrayIndexIntoA(value)
asmgen.readAndPushArrayvalueWithIndexA(arrayDt, value.identifier)
asmgen.readAndPushArrayvalueWithIndexA(elementDt, value.identifier)
}
assignFromEvalResult(assign.target)
}

View File

@ -387,10 +387,9 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
private fun translatePushFromArray(arrayExpr: ArrayIndexedExpression) {
// assume *reading* from an array
val index = arrayExpr.arrayspec.index
val arrayDt = arrayExpr.identifier.targetVarDecl(program.namespace)!!.datatype
val elementDt = arrayExpr.inferType(program).typeOrElse(DataType.STRUCT)
val arrayVarName = asmgen.asmIdentifierName(arrayExpr.identifier)
if(index is NumericLiteralValue) {
val elementDt = ArrayElementTypes.getValue(arrayDt)
val indexValue = index.number.toInt() * elementDt.memorySize()
when(elementDt) {
in ByteDatatypes -> {
@ -402,11 +401,11 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
DataType.FLOAT -> {
asmgen.out(" lda #<$arrayVarName+$indexValue | ldy #>$arrayVarName+$indexValue | jsr c64flt.push_float")
}
else -> throw AssemblyError("weird type")
else -> throw AssemblyError("weird element type")
}
} else {
asmgen.translateArrayIndexIntoA(arrayExpr)
asmgen.readAndPushArrayvalueWithIndexA(arrayDt, arrayExpr.identifier)
asmgen.readAndPushArrayvalueWithIndexA(elementDt, arrayExpr.identifier)
}
}

View File

@ -74,8 +74,7 @@ internal class PostIncrDecrAsmGen(private val program: Program, private val asmg
targetArrayIdx!=null -> {
val index = targetArrayIdx.arrayspec.index
val what = asmgen.asmIdentifierName(targetArrayIdx.identifier)
val arrayDt = targetArrayIdx.identifier.inferType(program).typeOrElse(DataType.STRUCT)
val elementDt = ArrayElementTypes.getValue(arrayDt)
val elementDt = targetArrayIdx.inferType(program).typeOrElse(DataType.STRUCT)
when(index) {
is NumericLiteralValue -> {
val indexValue = index.number.toInt() * elementDt.memorySize()
@ -101,11 +100,11 @@ internal class PostIncrDecrAsmGen(private val program: Program, private val asmg
}
is IdentifierReference -> {
asmgen.translateArrayIndexIntoA(targetArrayIdx)
incrDecrArrayvalueWithIndexA(incr, arrayDt, what)
incrDecrArrayvalueWithIndexA(incr, elementDt, what)
}
else -> {
asmgen.translateArrayIndexIntoA(targetArrayIdx)
incrDecrArrayvalueWithIndexA(incr, arrayDt, what)
incrDecrArrayvalueWithIndexA(incr, elementDt, what)
}
}
}
@ -113,14 +112,13 @@ internal class PostIncrDecrAsmGen(private val program: Program, private val asmg
}
}
private fun incrDecrArrayvalueWithIndexA(incr: Boolean, arrayDt: DataType, arrayVarName: String) {
private fun incrDecrArrayvalueWithIndexA(incr: Boolean, elementDt: DataType, arrayVarName: String) {
asmgen.out(" stx ${C64Zeropage.SCRATCH_REG_X} | tax")
when(arrayDt) {
DataType.STR,
DataType.ARRAY_UB, DataType.ARRAY_B -> {
when(elementDt) {
in ByteDatatypes -> {
asmgen.out(if(incr) " inc $arrayVarName,x" else " dec $arrayVarName,x")
}
DataType.ARRAY_UW, DataType.ARRAY_W -> {
in WordDatatypes -> {
if(incr)
asmgen.out(" inc $arrayVarName,x | bne + | inc $arrayVarName+1,x |+")
else
@ -131,11 +129,11 @@ internal class PostIncrDecrAsmGen(private val program: Program, private val asmg
+ dec $arrayVarName
""")
}
DataType.ARRAY_F -> {
DataType.FLOAT -> {
asmgen.out(" lda #<$arrayVarName | ldy #>$arrayVarName")
asmgen.out(if(incr) " jsr c64flt.inc_indexed_var_f" else " jsr c64flt.dec_indexed_var_f")
}
else -> throw AssemblyError("weird array dt")
else -> throw AssemblyError("weird array elt dt")
}
asmgen.out(" ldx ${C64Zeropage.SCRATCH_REG_X}")
}