made the ZP and compilation target more generic

This commit is contained in:
Irmen de Jong 2020-08-25 19:32:31 +02:00
parent 5da9379c37
commit 8d8c066447
13 changed files with 333 additions and 352 deletions

View File

@ -7,6 +7,7 @@
c64 {
; TODO get rid of those:
const uword ESTACK_LO = $ce00 ; evaluation stack (lsb)
const uword ESTACK_HI = $cf00 ; evaluation stack (msb)
&ubyte SCRATCH_ZPB1 = $02 ; scratch byte 1 in ZP

View File

@ -4,14 +4,25 @@ import prog8.compiler.CompilationOptions
import prog8.compiler.Zeropage
interface IMachineFloat {
fun toDouble(): Double
fun makeFloatFillAsm(): String
}
interface IMachineDefinition {
val FLOAT_MAX_NEGATIVE: Double
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 opcodeNames: Set<String>
var zeropage: Zeropage
fun initializeZeropage(compilerOptions: CompilationOptions)
fun getFloat(num: Number): IMachineFloat
fun getFloatRomConst(number: Double): String?
}

View File

@ -5,31 +5,62 @@ import prog8.compiler.CompilerException
import prog8.compiler.Zeropage
import prog8.compiler.ZeropageType
import prog8.compiler.target.IMachineDefinition
import prog8.compiler.target.IMachineFloat
import java.math.RoundingMode
import kotlin.math.absoluteValue
import kotlin.math.pow
object C64MachineDefinition: IMachineDefinition {
internal object C64MachineDefinition: IMachineDefinition {
// 5-byte cbm MFLPT format limitations:
override val FLOAT_MAX_POSITIVE = 1.7014118345e+38 // bytes: 255,127,255,255,255
override val FLOAT_MAX_NEGATIVE = -1.7014118345e+38 // bytes: 255,255,255,255,255
override val FLOAT_MEM_SIZE = 5
override val POINTER_MEM_SIZE = 2
const val BASIC_LOAD_ADDRESS = 0x0801
const val RAW_LOAD_ADDRESS = 0xc000
override val BASIC_LOAD_ADDRESS = 0x0801
override val RAW_LOAD_ADDRESS = 0xc000
// the 2*256 byte evaluation stack (on which bytes, words, and even floats are stored during calculations)
// and some heavily used string constants derived from the two values above
const val ESTACK_LO_VALUE = 0xce00 // $ce00-$ceff inclusive
const val ESTACK_HI_VALUE = 0xcf00 // $cf00-$cfff inclusive
const val ESTACK_LO_HEX = "\$ce00"
const val ESTACK_LO_PLUS1_HEX = "\$ce01"
const val ESTACK_LO_PLUS2_HEX = "\$ce02"
const val ESTACK_HI_HEX = "\$cf00"
const val ESTACK_HI_PLUS1_HEX = "\$cf01"
const val ESTACK_HI_PLUS2_HEX = "\$cf02"
override val ESTACK_LO = 0xce00 // $ce00-$ceff inclusive
override val ESTACK_HI = 0xcf00 // $ce00-$ceff inclusive
override lateinit var zeropage: Zeropage
override fun getFloat(num: Number) = Mflpt5.fromNumber(num)
override fun getFloatRomConst(number: Double): String? {
// try to match the ROM float constants to save memory
val mflpt5 = Mflpt5.fromNumber(number)
val floatbytes = shortArrayOf(mflpt5.b0, mflpt5.b1, mflpt5.b2, mflpt5.b3, mflpt5.b4)
when {
floatbytes.contentEquals(shortArrayOf(0x00, 0x00, 0x00, 0x00, 0x00)) -> return "c64flt.FL_ZERO"
floatbytes.contentEquals(shortArrayOf(0x82, 0x49, 0x0f, 0xda, 0xa1)) -> return "c64flt.FL_PIVAL"
floatbytes.contentEquals(shortArrayOf(0x90, 0x80, 0x00, 0x00, 0x00)) -> return "c64flt.FL_N32768"
floatbytes.contentEquals(shortArrayOf(0x81, 0x00, 0x00, 0x00, 0x00)) -> return "c64flt.FL_FONE"
floatbytes.contentEquals(shortArrayOf(0x80, 0x35, 0x04, 0xf3, 0x34)) -> return "c64flt.FL_SQRHLF"
floatbytes.contentEquals(shortArrayOf(0x81, 0x35, 0x04, 0xf3, 0x34)) -> return "c64flt.FL_SQRTWO"
floatbytes.contentEquals(shortArrayOf(0x80, 0x80, 0x00, 0x00, 0x00)) -> return "c64flt.FL_NEGHLF"
floatbytes.contentEquals(shortArrayOf(0x80, 0x31, 0x72, 0x17, 0xf8)) -> return "c64flt.FL_LOG2"
floatbytes.contentEquals(shortArrayOf(0x84, 0x20, 0x00, 0x00, 0x00)) -> return "c64flt.FL_TENC"
floatbytes.contentEquals(shortArrayOf(0x9e, 0x6e, 0x6b, 0x28, 0x00)) -> return "c64flt.FL_NZMIL"
floatbytes.contentEquals(shortArrayOf(0x80, 0x00, 0x00, 0x00, 0x00)) -> return "c64flt.FL_FHALF"
floatbytes.contentEquals(shortArrayOf(0x81, 0x38, 0xaa, 0x3b, 0x29)) -> return "c64flt.FL_LOGEB2"
floatbytes.contentEquals(shortArrayOf(0x81, 0x49, 0x0f, 0xda, 0xa2)) -> return "c64flt.FL_PIHALF"
floatbytes.contentEquals(shortArrayOf(0x83, 0x49, 0x0f, 0xda, 0xa2)) -> return "c64flt.FL_TWOPI"
floatbytes.contentEquals(shortArrayOf(0x7f, 0x00, 0x00, 0x00, 0x00)) -> return "c64flt.FL_FR4"
else -> {
// attempt to correct for a few rounding issues
when (number.toBigDecimal().setScale(10, RoundingMode.HALF_DOWN).toDouble()) {
3.1415926536 -> return "c64flt.FL_PIVAL"
1.4142135624 -> return "c64flt.FL_SQRTWO"
0.7071067812 -> return "c64flt.FL_SQRHLF"
0.6931471806 -> return "c64flt.FL_LOG2"
else -> {}
}
}
}
return null
}
override fun initializeZeropage(compilerOptions: CompilationOptions) {
zeropage = C64Zeropage(compilerOptions)
@ -47,16 +78,7 @@ object C64MachineDefinition: IMachineDefinition {
"sta", "stx", "sty", "tas", "tax", "tay", "tsx", "txa", "txs", "tya", "xaa")
class C64Zeropage(options: CompilationOptions) : Zeropage(options) {
companion object {
// TODO get rid of these static constants, use the properties from the Zeropage base class instead
const val SCRATCH_B1 = 0x02
const val SCRATCH_REG = 0x03 // temp storage for a register
const val SCRATCH_REG_X = 0xfa // temp storage for register X (the evaluation stack pointer)
const val SCRATCH_W1 = 0xfb // $fb+$fc
const val SCRATCH_W2 = 0xfd // $fd+$fe
}
internal class C64Zeropage(options: CompilationOptions) : Zeropage(options) {
override val SCRATCH_B1 = 0x02 // temp storage for a single byte
override val SCRATCH_REG = 0x03 // temp storage for a register
@ -130,8 +152,7 @@ object C64MachineDefinition: IMachineDefinition {
}
}
data class Mflpt5(val b0: Short, val b1: Short, val b2: Short, val b3: Short, val b4: Short) {
internal data class Mflpt5(val b0: Short, val b1: Short, val b2: Short, val b3: Short, val b4: Short): IMachineFloat {
companion object {
val zero = Mflpt5(0, 0, 0, 0, 0)
@ -178,7 +199,7 @@ object C64MachineDefinition: IMachineDefinition {
}
}
fun toDouble(): Double {
override fun toDouble(): Double {
if (this == zero) return 0.0
val exp = b0 - 128
val sign = (b1.toInt() and 0x80) > 0
@ -186,5 +207,14 @@ object C64MachineDefinition: IMachineDefinition {
val result = number.toDouble() * (2.0).pow(exp) / 0x100000000
return if (sign) -result else result
}
override fun makeFloatFillAsm(): String {
val b0 = "$" + b0.toString(16).padStart(2, '0')
val b1 = "$" + b1.toString(16).padStart(2, '0')
val b2 = "$" + b2.toString(16).padStart(2, '0')
val b3 = "$" + b3.toString(16).padStart(2, '0')
val b4 = "$" + b4.toString(16).padStart(2, '0')
return "$b0, $b1, $b2, $b3, $b4"
}
}
}

View File

@ -8,13 +8,11 @@ import prog8.ast.base.*
import prog8.ast.expressions.*
import prog8.ast.statements.*
import prog8.compiler.*
import prog8.compiler.target.CompilationTarget
import prog8.compiler.target.IAssemblyGenerator
import prog8.compiler.target.IAssemblyProgram
import prog8.compiler.target.c64.AssemblyProgram
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.Petscii
import prog8.compiler.target.c64.codegen.assignment.AsmAssignSource
import prog8.compiler.target.c64.codegen.assignment.AsmAssignTarget
@ -91,7 +89,17 @@ internal class AsmGen(private val program: Program,
program.actualLoadAddress = program.definedLoadAddress
if (program.actualLoadAddress == 0) // fix load address
program.actualLoadAddress = if (options.launcher == LauncherType.BASIC)
C64MachineDefinition.BASIC_LOAD_ADDRESS else C64MachineDefinition.RAW_LOAD_ADDRESS
CompilationTarget.machine.BASIC_LOAD_ADDRESS else CompilationTarget.machine.RAW_LOAD_ADDRESS
// the global prog8 variables needed
val zp = CompilationTarget.machine.zeropage
out("P8ZP_SCRATCH_B1 = ${zp.SCRATCH_B1}")
out("P8ZP_SCRATCH_REG = ${zp.SCRATCH_REG}")
out("P8ZP_SCRATCH_REG_X = ${zp.SCRATCH_REG_X}")
out("P8ZP_SCRATCH_W1 = ${zp.SCRATCH_W1} ; word")
out("P8ZP_SCRATCH_W2 = ${zp.SCRATCH_W2} ; word")
out("P8ESTACK_LO = ${CompilationTarget.machine.ESTACK_LO.toHex()}")
out("P8ESTACK_HI = ${CompilationTarget.machine.ESTACK_HI.toHex()}")
when {
options.launcher == LauncherType.BASIC -> {
@ -154,8 +162,7 @@ internal class AsmGen(private val program: Program,
// the global list of all floating point constants for the whole program
out("; global float constants")
for (flt in globalFloatConsts) {
val mflpt5 = C64MachineDefinition.Mflpt5.fromNumber(flt.key)
val floatFill = makeFloatFill(mflpt5)
val floatFill = CompilationTarget.machine.getFloat(flt.key).makeFloatFillAsm()
val floatvalue = flt.key
out("${flt.value}\t.byte $floatFill ; float $floatvalue")
}
@ -229,15 +236,6 @@ internal class AsmGen(private val program: Program,
} else assemblyLines.add(fragment)
}
private fun makeFloatFill(flt: C64MachineDefinition.Mflpt5): String {
val b0 = "$" + flt.b0.toString(16).padStart(2, '0')
val b1 = "$" + flt.b1.toString(16).padStart(2, '0')
val b2 = "$" + flt.b2.toString(16).padStart(2, '0')
val b3 = "$" + flt.b3.toString(16).padStart(2, '0')
val b4 = "$" + flt.b4.toString(16).padStart(2, '0')
return "$b0, $b1, $b2, $b3, $b4"
}
private fun encode(str: String, altEncoding: Boolean): List<Short> {
val bytes = if(altEncoding) Petscii.encodeScreencode(str, true) else Petscii.encodePetscii(str, true)
return bytes.plus(0)
@ -333,7 +331,7 @@ internal class AsmGen(private val program: Program,
}
val floatFills = array.map {
val number = (it as NumericLiteralValue).number
makeFloatFill(C64MachineDefinition.Mflpt5.fromNumber(number))
CompilationTarget.machine.getFloat(number).makeFloatFillAsm()
}
out(decl.name)
for (f in array.zip(floatFills))
@ -466,45 +464,17 @@ internal class AsmGen(private val program: Program,
}
}
internal fun getFloatConst(number: Double): String {
// try to match the ROM float constants to save memory
val mflpt5 = C64MachineDefinition.Mflpt5.fromNumber(number)
val floatbytes = shortArrayOf(mflpt5.b0, mflpt5.b1, mflpt5.b2, mflpt5.b3, mflpt5.b4)
when {
floatbytes.contentEquals(shortArrayOf(0x00, 0x00, 0x00, 0x00, 0x00)) -> return "c64flt.FL_ZERO"
floatbytes.contentEquals(shortArrayOf(0x82, 0x49, 0x0f, 0xda, 0xa1)) -> return "c64flt.FL_PIVAL"
floatbytes.contentEquals(shortArrayOf(0x90, 0x80, 0x00, 0x00, 0x00)) -> return "c64flt.FL_N32768"
floatbytes.contentEquals(shortArrayOf(0x81, 0x00, 0x00, 0x00, 0x00)) -> return "c64flt.FL_FONE"
floatbytes.contentEquals(shortArrayOf(0x80, 0x35, 0x04, 0xf3, 0x34)) -> return "c64flt.FL_SQRHLF"
floatbytes.contentEquals(shortArrayOf(0x81, 0x35, 0x04, 0xf3, 0x34)) -> return "c64flt.FL_SQRTWO"
floatbytes.contentEquals(shortArrayOf(0x80, 0x80, 0x00, 0x00, 0x00)) -> return "c64flt.FL_NEGHLF"
floatbytes.contentEquals(shortArrayOf(0x80, 0x31, 0x72, 0x17, 0xf8)) -> return "c64flt.FL_LOG2"
floatbytes.contentEquals(shortArrayOf(0x84, 0x20, 0x00, 0x00, 0x00)) -> return "c64flt.FL_TENC"
floatbytes.contentEquals(shortArrayOf(0x9e, 0x6e, 0x6b, 0x28, 0x00)) -> return "c64flt.FL_NZMIL"
floatbytes.contentEquals(shortArrayOf(0x80, 0x00, 0x00, 0x00, 0x00)) -> return "c64flt.FL_FHALF"
floatbytes.contentEquals(shortArrayOf(0x81, 0x38, 0xaa, 0x3b, 0x29)) -> return "c64flt.FL_LOGEB2"
floatbytes.contentEquals(shortArrayOf(0x81, 0x49, 0x0f, 0xda, 0xa2)) -> return "c64flt.FL_PIHALF"
floatbytes.contentEquals(shortArrayOf(0x83, 0x49, 0x0f, 0xda, 0xa2)) -> return "c64flt.FL_TWOPI"
floatbytes.contentEquals(shortArrayOf(0x7f, 0x00, 0x00, 0x00, 0x00)) -> return "c64flt.FL_FR4"
else -> {
// attempt to correct for a few rounding issues
when (number.toBigDecimal().setScale(10, RoundingMode.HALF_DOWN).toDouble()) {
3.1415926536 -> return "c64flt.FL_PIVAL"
1.4142135624 -> return "c64flt.FL_SQRTWO"
0.7071067812 -> return "c64flt.FL_SQRHLF"
0.6931471806 -> return "c64flt.FL_LOG2"
else -> {}
}
// no ROM float const for this value, create our own
val name = globalFloatConsts[number]
if(name!=null)
return name
val newName = "prog8_float_const_${globalFloatConsts.size}"
globalFloatConsts[number] = newName
return newName
internal fun getFloatAsmConst(number: Double): String {
var asmName = CompilationTarget.machine.getFloatRomConst(number)
if(asmName.isNullOrEmpty()) {
// no ROM float const for this value, create our own
asmName = globalFloatConsts[number]
if(asmName==null) {
asmName = "prog8_float_const_${globalFloatConsts.size}"
globalFloatConsts[number] = asmName
}
}
return asmName
}
internal fun asmIdentifierName(identifier: IdentifierReference): String {
@ -529,10 +499,10 @@ internal class AsmGen(private val program: Program,
out("""
lda $sourceName
ldy $sourceName+1
sta ${C64Zeropage.SCRATCH_W1}
sty ${C64Zeropage.SCRATCH_W1 + 1}
sta P8ZP_SCRATCH_W1
sty P8ZP_SCRATCH_W1+1
ldy #0
lda (${C64Zeropage.SCRATCH_W1}),y""")
lda (P8ZP_SCRATCH_W1),y""")
return Pair(false, sourceName)
}
}
@ -549,12 +519,12 @@ internal class AsmGen(private val program: Program,
} else {
out("""
ldy $sourceName
sty ${C64Zeropage.SCRATCH_W2}
sty P8ZP_SCRATCH_W2
ldy $sourceName+1
sty ${C64Zeropage.SCRATCH_W2 + 1}
sty P8ZP_SCRATCH_W2+1
${if(ldaInstructionArg==null) "" else "lda $ldaInstructionArg"}
ldy #0
sta (${C64Zeropage.SCRATCH_W2}),y""")
sta (P8ZP_SCRATCH_W2),y""")
}
}
@ -680,14 +650,14 @@ internal class AsmGen(private val program: Program,
else {
expressionsAsmGen.translateExpression(index)
out("""
inc $ESTACK_LO_HEX,x
inc P8ESTACK_LO,x
bne +
inc $ESTACK_HI_HEX,x
inc P8ESTACK_HI,x
+""")
when(register) {
CpuRegister.A -> out(" inx | lda $ESTACK_LO_HEX,x")
CpuRegister.A -> out(" inx | lda P8ESTACK_LO,x")
CpuRegister.X -> throw AssemblyError("can't use X here")
CpuRegister.Y -> out(" inx | ldy $ESTACK_LO_HEX,x")
CpuRegister.Y -> out(" inx | ldy P8ESTACK_LO,x")
}
}
} else {
@ -723,9 +693,9 @@ internal class AsmGen(private val program: Program,
else {
expressionsAsmGen.translateExpression(index)
when(register) {
CpuRegister.A -> out(" inx | lda $ESTACK_LO_HEX,x")
CpuRegister.A -> out(" inx | lda P8ESTACK_LO,x")
CpuRegister.X -> throw AssemblyError("can't use X here")
CpuRegister.Y -> out(" inx | ldy $ESTACK_LO_HEX,x")
CpuRegister.Y -> out(" inx | ldy P8ESTACK_LO,x")
}
}
}
@ -808,8 +778,8 @@ internal class AsmGen(private val program: Program,
private fun translateTestStack(dataType: DataType) {
when(dataType) {
in ByteDatatypes -> out(" inx | lda $ESTACK_LO_HEX,x")
in WordDatatypes -> out(" inx | lda $ESTACK_LO_HEX,x | ora $ESTACK_HI_HEX,x")
in ByteDatatypes -> out(" inx | lda P8ESTACK_LO,x")
in WordDatatypes -> out(" inx | lda P8ESTACK_LO,x | ora P8ESTACK_HI,x")
DataType.FLOAT -> throw AssemblyError("conditional value should be an integer (boolean)")
else -> throw AssemblyError("non-numerical dt")
}
@ -864,11 +834,11 @@ internal class AsmGen(private val program: Program,
val dt = stmt.iterations!!.inferType(program).typeOrElse(DataType.STRUCT)
when (dt) {
in ByteDatatypes -> {
out(" inx | lda ${ESTACK_LO_HEX},x")
out(" inx | lda P8ESTACK_LO,x")
repeatByteCountInA(null, repeatLabel, endLabel, stmt.body)
}
in WordDatatypes -> {
out(" inx | lda ${ESTACK_LO_HEX},x | ldy ${ESTACK_HI_HEX},x")
out(" inx | lda P8ESTACK_LO,x | ldy P8ESTACK_HI,x")
repeatWordCountInAY(null, repeatLabel, endLabel, stmt.body)
}
else -> throw AssemblyError("invalid loop expression datatype $dt")
@ -940,13 +910,13 @@ $counterVar .byte 0""")
if(!conditionDt.isKnown)
throw AssemblyError("unknown condition dt")
if(conditionDt.typeOrElse(DataType.BYTE) in ByteDatatypes) {
out(" inx | lda $ESTACK_LO_HEX,x | beq $endLabel")
out(" inx | lda P8ESTACK_LO,x | beq $endLabel")
} else {
out("""
inx
lda $ESTACK_LO_HEX,x
lda P8ESTACK_LO,x
bne +
lda $ESTACK_HI_HEX,x
lda P8ESTACK_HI,x
beq $endLabel
+ """)
}
@ -967,13 +937,13 @@ $counterVar .byte 0""")
if(!conditionDt.isKnown)
throw AssemblyError("unknown condition dt")
if(conditionDt.typeOrElse(DataType.BYTE) in ByteDatatypes) {
out(" inx | lda $ESTACK_LO_HEX,x | beq $repeatLabel")
out(" inx | lda P8ESTACK_LO,x | beq $repeatLabel")
} else {
out("""
inx
lda $ESTACK_LO_HEX,x
lda P8ESTACK_LO,x
bne +
lda $ESTACK_HI_HEX,x
lda P8ESTACK_HI,x
beq $repeatLabel
+ """)
}
@ -989,9 +959,9 @@ $counterVar .byte 0""")
if(!conditionDt.isKnown)
throw AssemblyError("unknown condition dt")
if(conditionDt.typeOrElse(DataType.BYTE) in ByteDatatypes)
out(" inx | lda $ESTACK_LO_HEX,x")
out(" inx | lda P8ESTACK_LO,x")
else
out(" inx | lda $ESTACK_LO_HEX,x | ldy $ESTACK_HI_HEX,x")
out(" inx | lda P8ESTACK_LO,x | ldy P8ESTACK_HI,x")
for(choice in stmt.choices) {
val choiceLabel = makeLabel("choice")
if(choice.values==null) {

View File

@ -1,8 +1,5 @@
package prog8.compiler.target.c64.codegen
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_LO_HEX
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_LO_PLUS1_HEX
// note: see https://wiki.nesdev.com/w/index.php/6502_assembly_optimisations
@ -87,10 +84,10 @@ private fun optimizeCmpSequence(linesByFour: List<List<IndexedValue<String>>>):
// the repeated lda can be removed
val mods = mutableListOf<Modification>()
for(lines in linesByFour) {
if(lines[0].value.trim()=="lda $ESTACK_LO_PLUS1_HEX,x" &&
if(lines[0].value.trim()=="lda P8ESTACK_LO+1,x" &&
lines[1].value.trim().startsWith("cmp ") &&
lines[2].value.trim().startsWith("beq ") &&
lines[3].value.trim()=="lda $ESTACK_LO_PLUS1_HEX,x") {
lines[3].value.trim()=="lda P8ESTACK_LO+1,x") {
mods.add(Modification(lines[3].index, true, null)) // remove the second lda
}
}
@ -102,10 +99,10 @@ private fun optimizeUselessStackByteWrites(linesByFour: List<List<IndexedValue<S
// this is a lot harder for word values because the instruction sequence varies.
val mods = mutableListOf<Modification>()
for(lines in linesByFour) {
if(lines[0].value.trim()=="sta $ESTACK_LO_HEX,x" &&
if(lines[0].value.trim()=="sta P8ESTACK_LO,x" &&
lines[1].value.trim()=="dex" &&
lines[2].value.trim()=="inx" &&
lines[3].value.trim()=="lda $ESTACK_LO_HEX,x") {
lines[3].value.trim()=="lda P8ESTACK_LO,x") {
mods.add(Modification(lines[1].index, true, null))
mods.add(Modification(lines[2].index, true, null))
mods.add(Modification(lines[3].index, true, null))

View File

@ -6,15 +6,6 @@ import prog8.ast.base.*
import prog8.ast.expressions.*
import prog8.ast.statements.FunctionCallStatement
import prog8.compiler.AssemblyError
import prog8.compiler.target.c64.C64MachineDefinition.C64Zeropage
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_HI_HEX
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_HI_PLUS1_HEX
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_LO_HEX
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_LO_PLUS1_HEX
import prog8.compiler.target.c64.codegen.assignment.AsmAssignSource
import prog8.compiler.target.c64.codegen.assignment.AsmAssignTarget
import prog8.compiler.target.c64.codegen.assignment.AsmAssignment
import prog8.compiler.target.c64.codegen.assignment.TargetStorageKind
import prog8.compiler.toHex
import prog8.functions.FSignature
@ -60,7 +51,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
"rsave" -> {
// save cpu status flag and all registers A, X, Y.
// see http://6502.org/tutorials/register_preservation.html
asmgen.out(" php | sta ${C64Zeropage.SCRATCH_REG} | pha | txa | pha | tya | pha | lda ${C64Zeropage.SCRATCH_REG}")
asmgen.out(" php | sta P8ZP_SCRATCH_REG | pha | txa | pha | tya | pha | lda P8ZP_SCRATCH_REG")
}
"rrestore" -> {
// restore all registers and cpu status flag
@ -88,8 +79,8 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
asmgen.out("""
lda #<$varName
ldy #>$varName
sta ${C64Zeropage.SCRATCH_W1}
sty ${C64Zeropage.SCRATCH_W1 + 1}
sta P8ZP_SCRATCH_W1
sty P8ZP_SCRATCH_W1+1
lda #$numElements
jsr prog8_lib.reverse_b
""")
@ -98,8 +89,8 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
asmgen.out("""
lda #<$varName
ldy #>$varName
sta ${C64Zeropage.SCRATCH_W1}
sty ${C64Zeropage.SCRATCH_W1 + 1}
sta P8ZP_SCRATCH_W1
sty P8ZP_SCRATCH_W1+1
lda #$numElements
jsr prog8_lib.reverse_w
""")
@ -108,8 +99,8 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
asmgen.out("""
lda #<$varName
ldy #>$varName
sta ${C64Zeropage.SCRATCH_W1}
sty ${C64Zeropage.SCRATCH_W1 + 1}
sta P8ZP_SCRATCH_W1
sty P8ZP_SCRATCH_W1+1
lda #$numElements
jsr prog8_lib.reverse_f
""")
@ -130,10 +121,10 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
asmgen.out("""
lda #<$varName
ldy #>$varName
sta ${C64Zeropage.SCRATCH_W1}
sty ${C64Zeropage.SCRATCH_W1 + 1}
sta P8ZP_SCRATCH_W1
sty P8ZP_SCRATCH_W1+1
lda #$numElements
sta ${C64Zeropage.SCRATCH_B1}
sta P8ZP_SCRATCH_B1
""")
asmgen.out(if (decl.datatype == DataType.ARRAY_UB) " jsr prog8_lib.sort_ub" else " jsr prog8_lib.sort_b")
}
@ -141,10 +132,10 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
asmgen.out("""
lda #<$varName
ldy #>$varName
sta ${C64Zeropage.SCRATCH_W1}
sty ${C64Zeropage.SCRATCH_W1 + 1}
sta P8ZP_SCRATCH_W1
sty P8ZP_SCRATCH_W1+1
lda #$numElements
sta ${C64Zeropage.SCRATCH_B1}
sta P8ZP_SCRATCH_B1
""")
asmgen.out(if (decl.datatype == DataType.ARRAY_UW) " jsr prog8_lib.sort_uw" else " jsr prog8_lib.sort_w")
}
@ -219,9 +210,9 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
asmgen.translateExpression(what.addressExpression)
asmgen.out("""
inx
lda $ESTACK_LO_HEX,x
lda P8ESTACK_LO,x
sta (+) + 1
lda $ESTACK_HI_HEX,x
lda P8ESTACK_HI,x
sta (+) + 2
+ ror ${'$'}ffff ; modified
""")
@ -316,9 +307,9 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
asmgen.translateExpression(what.addressExpression)
asmgen.out("""
inx
lda $ESTACK_LO_HEX,x
lda P8ESTACK_LO,x
sta (+) + 1
lda $ESTACK_HI_HEX,x
lda P8ESTACK_HI,x
sta (+) + 2
+ rol ${'$'}ffff ; modified
""")
@ -397,7 +388,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
lda #<$name
ldy #>$name
jsr prog8_lib.strlen
sta $ESTACK_LO_HEX,x
sta P8ESTACK_LO,x
dex""")
}
@ -428,13 +419,13 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
if(dt.istype(DataType.FLOAT)) {
asmgen.out("""
lda #<$firstName
sta ${C64Zeropage.SCRATCH_W1}
sta P8ZP_SCRATCH_W1
lda #>$firstName
sta ${C64Zeropage.SCRATCH_W1+1}
sta P8ZP_SCRATCH_W1+1
lda #<$secondName
sta ${C64Zeropage.SCRATCH_W2}
sta P8ZP_SCRATCH_W2
lda #>$secondName
sta ${C64Zeropage.SCRATCH_W2+1}
sta P8ZP_SCRATCH_W2+1
jsr c64flt.swap_floats
""")
return
@ -460,7 +451,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
// trick: push the args in reverse order (msb first, lsb second) this saves some instructions
asmgen.translateExpression(fcall.args[1])
asmgen.translateExpression(fcall.args[0])
asmgen.out(" inx | lda $ESTACK_LO_HEX,x | sta $ESTACK_HI_PLUS1_HEX,x")
asmgen.out(" inx | lda P8ESTACK_LO,x | sta P8ESTACK_HI+1,x")
}
private fun funcMsb(fcall: IFunctionCall) {
@ -471,10 +462,10 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
throw AssemblyError("msb(const) should have been const-folded away")
if (arg is IdentifierReference) {
val sourceName = asmgen.asmIdentifierName(arg)
asmgen.out(" lda $sourceName+1 | sta $ESTACK_LO_HEX,x | dex")
asmgen.out(" lda $sourceName+1 | sta P8ESTACK_LO,x | dex")
} else {
asmgen.translateExpression(arg)
asmgen.out(" lda $ESTACK_HI_PLUS1_HEX,x | sta $ESTACK_LO_PLUS1_HEX,x")
asmgen.out(" lda P8ESTACK_HI+1,x | sta P8ESTACK_LO+1,x")
}
}
@ -486,7 +477,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
throw AssemblyError("lsb(const) should have been const-folded away")
if (arg is IdentifierReference) {
val sourceName = asmgen.asmIdentifierName(arg)
asmgen.out(" lda $sourceName | sta $ESTACK_LO_HEX,x | dex")
asmgen.out(" lda $sourceName | sta P8ESTACK_LO,x | dex")
} else {
asmgen.translateExpression(arg)
// just ignore any high-byte
@ -499,12 +490,12 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
val size = arg.targetVarDecl(program.namespace)!!.arraysize!!.constIndex()!!
asmgen.out("""
lda #<$identifierName
sta $ESTACK_LO_HEX,x
sta P8ESTACK_LO,x
lda #>$identifierName
sta $ESTACK_HI_HEX,x
sta P8ESTACK_HI,x
dex
lda #$size
sta $ESTACK_LO_HEX,x
sta P8ESTACK_LO,x
dex
""")
}

View File

@ -4,12 +4,6 @@ import prog8.ast.Program
import prog8.ast.base.*
import prog8.ast.expressions.*
import prog8.compiler.AssemblyError
import prog8.compiler.target.c64.C64MachineDefinition.C64Zeropage
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_HI_HEX
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_HI_PLUS1_HEX
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_LO_HEX
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_LO_PLUS1_HEX
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_LO_PLUS2_HEX
import prog8.compiler.toHex
import prog8.functions.BuiltinFunctions
import kotlin.math.absoluteValue
@ -46,20 +40,20 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
// result value in cpu or status registers, put it on the stack
if (reg.registerOrPair != null) {
when (reg.registerOrPair) {
RegisterOrPair.A -> asmgen.out(" sta $ESTACK_LO_HEX,x | dex")
RegisterOrPair.Y -> asmgen.out(" tya | sta $ESTACK_LO_HEX,x | dex")
RegisterOrPair.AY -> asmgen.out(" sta $ESTACK_LO_HEX,x | tya | sta $ESTACK_HI_HEX,x | dex")
RegisterOrPair.A -> asmgen.out(" sta P8ESTACK_LO,x | dex")
RegisterOrPair.Y -> asmgen.out(" tya | sta P8ESTACK_LO,x | dex")
RegisterOrPair.AY -> asmgen.out(" sta P8ESTACK_LO,x | tya | sta P8ESTACK_HI,x | dex")
RegisterOrPair.X -> {
// return value in X register has been discarded, just push a zero
asmgen.out(" lda #0 | sta $ESTACK_LO_HEX,x | dex")
asmgen.out(" lda #0 | sta P8ESTACK_LO,x | dex")
}
RegisterOrPair.AX -> {
// return value in X register has been discarded, just push a zero in this place
asmgen.out(" sta $ESTACK_LO_HEX,x | lda #0 | sta $ESTACK_HI_HEX,x | dex")
asmgen.out(" sta P8ESTACK_LO,x | lda #0 | sta P8ESTACK_HI,x | dex")
}
RegisterOrPair.XY -> {
// return value in X register has been discarded, just push a zero in this place
asmgen.out(" lda #0 | sta $ESTACK_LO_HEX,x | tya | sta $ESTACK_HI_HEX,x | dex")
asmgen.out(" lda #0 | sta P8ESTACK_LO,x | tya | sta P8ESTACK_HI,x | dex")
}
}
}
@ -75,7 +69,7 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
DataType.UBYTE -> {
when(expr.type) {
DataType.UBYTE, DataType.BYTE -> {}
DataType.UWORD, DataType.WORD -> asmgen.out(" lda #0 | sta $ESTACK_HI_PLUS1_HEX,x")
DataType.UWORD, DataType.WORD -> asmgen.out(" lda #0 | sta P8ESTACK_HI+1,x")
DataType.FLOAT -> asmgen.out(" jsr c64flt.stack_ub2float")
in PassByReferenceDatatypes -> throw AssemblyError("cannot cast to a pass-by-reference datatype")
else -> throw AssemblyError("weird type")
@ -87,11 +81,11 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
DataType.UWORD, DataType.WORD -> {
// sign extend
asmgen.out("""
lda $ESTACK_LO_PLUS1_HEX,x
lda P8ESTACK_LO+1,x
ora #$7f
bmi +
lda #0
+ sta $ESTACK_HI_PLUS1_HEX,x""")
+ sta P8ESTACK_HI+1,x""")
}
DataType.FLOAT -> asmgen.out(" jsr c64flt.stack_b2float")
in PassByReferenceDatatypes -> throw AssemblyError("cannot cast to a pass-by-reference datatype")
@ -134,40 +128,40 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
private fun translateExpression(expr: AddressOf) {
val name = asmgen.asmIdentifierName(expr.identifier)
asmgen.out(" lda #<$name | sta $ESTACK_LO_HEX,x | lda #>$name | sta $ESTACK_HI_HEX,x | dex")
asmgen.out(" lda #<$name | sta P8ESTACK_LO,x | lda #>$name | sta P8ESTACK_HI,x | dex")
}
private fun translateExpression(expr: DirectMemoryRead) {
when(expr.addressExpression) {
is NumericLiteralValue -> {
val address = (expr.addressExpression as NumericLiteralValue).number.toInt()
asmgen.out(" lda ${address.toHex()} | sta $ESTACK_LO_HEX,x | dex")
asmgen.out(" lda ${address.toHex()} | sta P8ESTACK_LO,x | dex")
}
is IdentifierReference -> {
// the identifier is a pointer variable, so read the value from the address in it
asmgen.loadByteFromPointerIntoA(expr.addressExpression as IdentifierReference)
asmgen.out(" sta $ESTACK_LO_HEX,x | dex")
asmgen.out(" sta P8ESTACK_LO,x | dex")
}
else -> {
translateExpression(expr.addressExpression)
asmgen.out(" jsr prog8_lib.read_byte_from_address_on_stack")
asmgen.out(" sta $ESTACK_LO_PLUS1_HEX,x")
asmgen.out(" sta P8ESTACK_LO+1,x")
}
}
}
private fun translateExpression(expr: NumericLiteralValue) {
when(expr.type) {
DataType.UBYTE, DataType.BYTE -> asmgen.out(" lda #${expr.number.toHex()} | sta $ESTACK_LO_HEX,x | dex")
DataType.UBYTE, DataType.BYTE -> asmgen.out(" lda #${expr.number.toHex()} | sta P8ESTACK_LO,x | dex")
DataType.UWORD, DataType.WORD -> asmgen.out("""
lda #<${expr.number.toHex()}
sta $ESTACK_LO_HEX,x
sta P8ESTACK_LO,x
lda #>${expr.number.toHex()}
sta $ESTACK_HI_HEX,x
sta P8ESTACK_HI,x
dex
""")
DataType.FLOAT -> {
val floatConst = asmgen.getFloatConst(expr.number.toDouble())
val floatConst = asmgen.getFloatAsmConst(expr.number.toDouble())
asmgen.out(" lda #<$floatConst | ldy #>$floatConst | jsr c64flt.push_float")
}
else -> throw AssemblyError("weird type")
@ -178,16 +172,16 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
val varname = asmgen.asmIdentifierName(expr)
when(expr.inferType(program).typeOrElse(DataType.STRUCT)) {
DataType.UBYTE, DataType.BYTE -> {
asmgen.out(" lda $varname | sta $ESTACK_LO_HEX,x | dex")
asmgen.out(" lda $varname | sta P8ESTACK_LO,x | dex")
}
DataType.UWORD, DataType.WORD -> {
asmgen.out(" lda $varname | sta $ESTACK_LO_HEX,x | lda $varname+1 | sta $ESTACK_HI_HEX,x | dex")
asmgen.out(" lda $varname | sta P8ESTACK_LO,x | lda $varname+1 | sta P8ESTACK_HI,x | dex")
}
DataType.FLOAT -> {
asmgen.out(" lda #<$varname | ldy #>$varname| jsr c64flt.push_float")
}
in IterableDatatypes -> {
asmgen.out(" lda #<$varname | sta $ESTACK_LO_HEX,x | lda #>$varname | sta $ESTACK_HI_HEX,x | dex")
asmgen.out(" lda #<$varname | sta P8ESTACK_LO,x | lda #>$varname | sta P8ESTACK_HI,x | dex")
}
else -> throw AssemblyError("stack push weird variable type $expr")
}
@ -213,20 +207,20 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
when (leftDt) {
DataType.UBYTE -> {
if (amount <= 2)
repeat(amount) { asmgen.out(" lsr $ESTACK_LO_PLUS1_HEX,x") }
repeat(amount) { asmgen.out(" lsr P8ESTACK_LO+1,x") }
else {
asmgen.out(" lda $ESTACK_LO_PLUS1_HEX,x")
asmgen.out(" lda P8ESTACK_LO+1,x")
repeat(amount) { asmgen.out(" lsr a") }
asmgen.out(" sta $ESTACK_LO_PLUS1_HEX,x")
asmgen.out(" sta P8ESTACK_LO+1,x")
}
}
DataType.BYTE -> {
if (amount <= 2)
repeat(amount) { asmgen.out(" lda $ESTACK_LO_PLUS1_HEX,x | asl a | ror $ESTACK_LO_PLUS1_HEX,x") }
repeat(amount) { asmgen.out(" lda P8ESTACK_LO+1,x | asl a | ror P8ESTACK_LO+1,x") }
else {
asmgen.out(" lda $ESTACK_LO_PLUS1_HEX,x | sta ${C64Zeropage.SCRATCH_B1}")
repeat(amount) { asmgen.out(" asl a | ror ${C64Zeropage.SCRATCH_B1} | lda ${C64Zeropage.SCRATCH_B1}") }
asmgen.out(" sta $ESTACK_LO_PLUS1_HEX,x")
asmgen.out(" lda P8ESTACK_LO+1,x | sta P8ZP_SCRATCH_B1")
repeat(amount) { asmgen.out(" asl a | ror P8ZP_SCRATCH_B1 | lda P8ZP_SCRATCH_B1") }
asmgen.out(" sta P8ESTACK_LO+1,x")
}
}
DataType.UWORD -> {
@ -236,7 +230,7 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
left -= 7
}
if (left in 0..2)
repeat(left) { asmgen.out(" lsr $ESTACK_HI_PLUS1_HEX,x | ror $ESTACK_LO_PLUS1_HEX,x") }
repeat(left) { asmgen.out(" lsr P8ESTACK_HI+1,x | ror P8ESTACK_LO+1,x") }
else
asmgen.out(" jsr math.shift_right_uw_$left")
}
@ -247,7 +241,7 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
left -= 7
}
if (left in 0..2)
repeat(left) { asmgen.out(" lda $ESTACK_HI_PLUS1_HEX,x | asl a | ror $ESTACK_HI_PLUS1_HEX,x | ror $ESTACK_LO_PLUS1_HEX,x") }
repeat(left) { asmgen.out(" lda P8ESTACK_HI+1,x | asl a | ror P8ESTACK_HI+1,x | ror P8ESTACK_LO+1,x") }
else
asmgen.out(" jsr math.shift_right_w_$left")
}
@ -262,11 +256,11 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
if(amount!=null) {
if (leftDt in ByteDatatypes) {
if (amount <= 2)
repeat(amount) { asmgen.out(" asl $ESTACK_LO_PLUS1_HEX,x") }
repeat(amount) { asmgen.out(" asl P8ESTACK_LO+1,x") }
else {
asmgen.out(" lda $ESTACK_LO_PLUS1_HEX,x")
asmgen.out(" lda P8ESTACK_LO+1,x")
repeat(amount) { asmgen.out(" asl a") }
asmgen.out(" sta $ESTACK_LO_PLUS1_HEX,x")
asmgen.out(" sta P8ESTACK_LO+1,x")
}
} else {
var left = amount
@ -275,7 +269,7 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
left -= 7
}
if (left in 0..2)
repeat(left) { asmgen.out(" asl $ESTACK_LO_PLUS1_HEX,x | rol $ESTACK_HI_PLUS1_HEX,x") }
repeat(left) { asmgen.out(" asl P8ESTACK_LO+1,x | rol P8ESTACK_HI+1,x") }
else
asmgen.out(" jsr math.shift_left_w_$left")
}
@ -365,9 +359,9 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
when(type) {
in ByteDatatypes ->
asmgen.out("""
lda $ESTACK_LO_PLUS1_HEX,x
lda P8ESTACK_LO+1,x
eor #255
sta $ESTACK_LO_PLUS1_HEX,x
sta P8ESTACK_LO+1,x
""")
in WordDatatypes -> asmgen.out(" jsr prog8_lib.inv_word")
else -> throw AssemblyError("weird type")
@ -392,10 +386,10 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
val indexValue = index.number.toInt() * elementDt.memorySize()
when(elementDt) {
in ByteDatatypes -> {
asmgen.out(" lda $arrayVarName+$indexValue | sta $ESTACK_LO_HEX,x | dex")
asmgen.out(" lda $arrayVarName+$indexValue | sta P8ESTACK_LO,x | dex")
}
in WordDatatypes -> {
asmgen.out(" lda $arrayVarName+$indexValue | sta $ESTACK_LO_HEX,x | lda $arrayVarName+$indexValue+1 | sta $ESTACK_HI_HEX,x | dex")
asmgen.out(" lda $arrayVarName+$indexValue | sta P8ESTACK_LO,x | lda $arrayVarName+$indexValue+1 | sta P8ESTACK_HI,x | dex")
}
DataType.FLOAT -> {
asmgen.out(" lda #<$arrayVarName+$indexValue | ldy #>$arrayVarName+$indexValue | jsr c64flt.push_float")
@ -406,11 +400,11 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
when(elementDt) {
in ByteDatatypes -> {
asmgen.loadScaledArrayIndexIntoRegister(arrayExpr, elementDt, CpuRegister.Y)
asmgen.out(" lda $arrayVarName,y | sta $ESTACK_LO_HEX,x | dex")
asmgen.out(" lda $arrayVarName,y | sta P8ESTACK_LO,x | dex")
}
in WordDatatypes -> {
asmgen.loadScaledArrayIndexIntoRegister(arrayExpr, elementDt, CpuRegister.Y)
asmgen.out(" lda $arrayVarName,y | sta $ESTACK_LO_HEX,x | lda $arrayVarName+1,y | sta $ESTACK_HI_HEX,x | dex")
asmgen.out(" lda $arrayVarName,y | sta P8ESTACK_LO,x | lda $arrayVarName+1,y | sta P8ESTACK_HI,x | dex")
}
DataType.FLOAT -> {
asmgen.loadScaledArrayIndexIntoRegister(arrayExpr, elementDt, CpuRegister.A)
@ -439,18 +433,18 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
asmgen.out(" jsr prog8_lib.remainder_ub")
}
"+" -> asmgen.out("""
lda $ESTACK_LO_PLUS2_HEX,x
lda P8ESTACK_LO+2,x
clc
adc $ESTACK_LO_PLUS1_HEX,x
adc P8ESTACK_LO+1,x
inx
sta $ESTACK_LO_PLUS1_HEX,x
sta P8ESTACK_LO+1,x
""")
"-" -> asmgen.out("""
lda $ESTACK_LO_PLUS2_HEX,x
lda P8ESTACK_LO+2,x
sec
sbc $ESTACK_LO_PLUS1_HEX,x
sbc P8ESTACK_LO+1,x
inx
sta $ESTACK_LO_PLUS1_HEX,x
sta P8ESTACK_LO+1,x
""")
"<<" -> asmgen.out(" jsr prog8_lib.shiftleft_b")
">>" -> asmgen.out(" jsr prog8_lib.shiftright_b")

View File

@ -4,13 +4,8 @@ import prog8.ast.Program
import prog8.ast.base.DataType
import prog8.ast.expressions.IdentifierReference
import prog8.ast.expressions.RangeExpr
import prog8.ast.statements.AssignTarget
import prog8.ast.statements.Assignment
import prog8.ast.statements.ForLoop
import prog8.compiler.AssemblyError
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_HI_PLUS1_HEX
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_LO_HEX
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_LO_PLUS1_HEX
import prog8.compiler.target.c64.codegen.assignment.AsmAssignSource
import prog8.compiler.target.c64.codegen.assignment.AsmAssignTarget
import prog8.compiler.target.c64.codegen.assignment.AsmAssignment
@ -60,9 +55,9 @@ internal class ForLoopsAsmGen(private val program: Program, private val asmgen:
asmgen.translateExpression(range.from)
asmgen.out("""
inx
lda $ESTACK_LO_HEX,x
lda P8ESTACK_LO,x
sta $varname
lda $ESTACK_LO_PLUS1_HEX,x
lda P8ESTACK_LO+1,x
sta $modifiedLabel+1
$loopLabel""")
asmgen.translate(stmt.body)
@ -84,9 +79,9 @@ $endLabel inx""")
asmgen.translateExpression(range.from)
asmgen.out("""
inx
lda $ESTACK_LO_HEX,x
lda P8ESTACK_LO,x
sta $varname
lda $ESTACK_LO_PLUS1_HEX,x
lda P8ESTACK_LO+1,x
sta $modifiedLabel+1
$loopLabel""")
asmgen.translate(stmt.body)
@ -122,9 +117,9 @@ $endLabel inx""")
assignLoopvar(stmt, range)
val varname = asmgen.asmIdentifierName(stmt.loopVar)
asmgen.out("""
lda $ESTACK_HI_PLUS1_HEX,x
lda P8ESTACK_HI+1,x
sta $modifiedLabel+1
lda $ESTACK_LO_PLUS1_HEX,x
lda P8ESTACK_LO+1,x
sta $modifiedLabel2+1
$loopLabel""")
asmgen.translate(stmt.body)
@ -158,9 +153,9 @@ $modifiedLabel2 cmp #0 ; modified
// (u)words, step >= 2
asmgen.translateExpression(range.to)
asmgen.out("""
lda $ESTACK_HI_PLUS1_HEX,x
lda P8ESTACK_HI+1,x
sta $modifiedLabel+1
lda $ESTACK_LO_PLUS1_HEX,x
lda P8ESTACK_LO+1,x
sta $modifiedLabel2+1
""")
assignLoopvar(stmt, range)
@ -209,9 +204,9 @@ $endLabel inx""")
// (u)words, step <= -2
asmgen.translateExpression(range.to)
asmgen.out("""
lda $ESTACK_HI_PLUS1_HEX,x
lda P8ESTACK_HI+1,x
sta $modifiedLabel+1
lda $ESTACK_LO_PLUS1_HEX,x
lda P8ESTACK_LO+1,x
sta $modifiedLabel2+1
""")
assignLoopvar(stmt, range)

View File

@ -7,7 +7,6 @@ import prog8.ast.expressions.*
import prog8.ast.statements.Subroutine
import prog8.ast.statements.SubroutineParameter
import prog8.compiler.AssemblyError
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_LO_HEX
import prog8.compiler.target.c64.codegen.assignment.*
@ -72,7 +71,7 @@ internal class FunctionCallAsmGen(private val program: Program, private val asmg
asmgen.out("""
inx
pha
lda $ESTACK_LO_HEX,x
lda P8ESTACK_LO,x
beq +
sec
bcs ++
@ -156,7 +155,7 @@ internal class FunctionCallAsmGen(private val program: Program, private val asmg
asmgen.out("""
inx
pha
lda $ESTACK_LO_HEX,x
lda P8ESTACK_LO,x
beq +
sec
bcs ++

View File

@ -6,9 +6,6 @@ import prog8.ast.expressions.IdentifierReference
import prog8.ast.expressions.NumericLiteralValue
import prog8.ast.statements.PostIncrDecr
import prog8.compiler.AssemblyError
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.toHex
@ -59,9 +56,9 @@ internal class PostIncrDecrAsmGen(private val program: Program, private val asmg
asmgen.translateExpression(addressExpr)
asmgen.out("""
inx
lda $ESTACK_LO_HEX,x
lda P8ESTACK_LO,x
sta (+) + 1
lda $ESTACK_HI_HEX,x
lda P8ESTACK_HI,x
sta (+) + 2
""")
if(incr)
@ -100,7 +97,7 @@ internal class PostIncrDecrAsmGen(private val program: Program, private val asmg
}
else -> {
asmgen.loadScaledArrayIndexIntoRegister(targetArrayIdx, elementDt, CpuRegister.A)
asmgen.out(" stx ${C64Zeropage.SCRATCH_REG_X} | tax")
asmgen.out(" stx P8ZP_SCRATCH_REG_X | tax")
when(elementDt) {
in ByteDatatypes -> {
asmgen.out(if(incr) " inc $asmArrayvarname,x" else " dec $asmArrayvarname,x")
@ -127,7 +124,7 @@ internal class PostIncrDecrAsmGen(private val program: Program, private val asmg
}
else -> throw AssemblyError("weird array elt dt")
}
asmgen.out(" ldx ${C64Zeropage.SCRATCH_REG_X}")
asmgen.out(" ldx P8ZP_SCRATCH_REG_X")
}
}
}

View File

@ -5,9 +5,6 @@ import prog8.ast.base.*
import prog8.ast.expressions.*
import prog8.ast.statements.*
import prog8.compiler.AssemblyError
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
@ -62,9 +59,9 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
val indexValue = index.number.toInt() * elementDt.memorySize()
when (elementDt) {
in ByteDatatypes ->
asmgen.out(" lda $arrayVarName+$indexValue | sta $ESTACK_LO_HEX,x | dex")
asmgen.out(" lda $arrayVarName+$indexValue | sta P8ESTACK_LO,x | dex")
in WordDatatypes ->
asmgen.out(" lda $arrayVarName+$indexValue | sta $ESTACK_LO_HEX,x | lda $arrayVarName+$indexValue+1 | sta $ESTACK_HI_HEX,x | dex")
asmgen.out(" lda $arrayVarName+$indexValue | sta P8ESTACK_LO,x | lda $arrayVarName+$indexValue+1 | sta P8ESTACK_HI,x | dex")
DataType.FLOAT ->
asmgen.out(" lda #<$arrayVarName+$indexValue | ldy #>$arrayVarName+$indexValue | jsr c64flt.push_float")
else ->
@ -74,11 +71,11 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
when (elementDt) {
in ByteDatatypes -> {
asmgen.loadScaledArrayIndexIntoRegister(value, elementDt, CpuRegister.Y)
asmgen.out(" lda $arrayVarName,y | sta $ESTACK_LO_HEX,x | dex")
asmgen.out(" lda $arrayVarName,y | sta P8ESTACK_LO,x | dex")
}
in WordDatatypes -> {
asmgen.loadScaledArrayIndexIntoRegister(value, elementDt, CpuRegister.Y)
asmgen.out(" lda $arrayVarName,y | sta $ESTACK_LO_HEX,x | lda $arrayVarName+1,y | sta $ESTACK_HI_HEX,x | dex")
asmgen.out(" lda $arrayVarName,y | sta P8ESTACK_LO,x | lda $arrayVarName+1,y | sta P8ESTACK_HI,x | dex")
}
DataType.FLOAT -> {
asmgen.loadScaledArrayIndexIntoRegister(value, elementDt, CpuRegister.A)
@ -167,14 +164,14 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
TargetStorageKind.VARIABLE -> {
when (target.datatype) {
DataType.UBYTE, DataType.BYTE -> {
asmgen.out(" inx | lda $ESTACK_LO_HEX,x | sta ${target.asmVarname}")
asmgen.out(" inx | lda P8ESTACK_LO,x | sta ${target.asmVarname}")
}
DataType.UWORD, DataType.WORD -> {
asmgen.out("""
inx
lda $ESTACK_LO_HEX,x
lda P8ESTACK_LO,x
sta ${target.asmVarname}
lda $ESTACK_HI_HEX,x
lda P8ESTACK_HI,x
sta ${target.asmVarname}+1
""")
}
@ -190,7 +187,7 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
}
TargetStorageKind.MEMORY -> {
asmgen.out(" inx")
storeByteViaRegisterAInMemoryAddress("$ESTACK_LO_HEX,x", target.memory!!)
storeByteViaRegisterAInMemoryAddress("P8ESTACK_LO,x", target.memory!!)
}
TargetStorageKind.ARRAY -> {
val index = target.array!!.arrayspec.index
@ -199,14 +196,14 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
val scaledIdx = target.constArrayIndexValue!! * target.datatype.memorySize()
when(target.datatype) {
in ByteDatatypes -> {
asmgen.out(" inx | lda $ESTACK_LO_HEX,x | sta ${target.asmVarname}+$scaledIdx")
asmgen.out(" inx | lda P8ESTACK_LO,x | sta ${target.asmVarname}+$scaledIdx")
}
in WordDatatypes -> {
asmgen.out("""
inx
lda $ESTACK_LO_HEX,x
lda P8ESTACK_LO,x
sta ${target.asmVarname}+$scaledIdx
lda $ESTACK_HI_HEX,x
lda P8ESTACK_HI,x
sta ${target.asmVarname}+$scaledIdx+1
""")
}
@ -224,15 +221,15 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
when(target.datatype) {
DataType.UBYTE, DataType.BYTE -> {
asmgen.loadScaledArrayIndexIntoRegister(target.array, target.datatype, CpuRegister.Y)
asmgen.out(" inx | lda $ESTACK_LO_HEX,x | sta ${target.asmVarname},y")
asmgen.out(" inx | lda P8ESTACK_LO,x | sta ${target.asmVarname},y")
}
DataType.UWORD, DataType.WORD -> {
asmgen.loadScaledArrayIndexIntoRegister(target.array, target.datatype, CpuRegister.Y)
asmgen.out("""
inx
lda $ESTACK_LO_HEX,x
lda P8ESTACK_LO,x
sta ${target.asmVarname},y
lda $ESTACK_HI_HEX,x
lda P8ESTACK_HI,x
sta ${target.asmVarname}+1,y
""")
}
@ -251,7 +248,7 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
}
else -> {
asmgen.translateExpression(index)
asmgen.out(" inx | lda $ESTACK_LO_HEX,x")
asmgen.out(" inx | lda P8ESTACK_LO,x")
popAndWriteArrayvalueWithUnscaledIndexA(target.datatype, target.asmVarname)
}
}
@ -260,16 +257,16 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
when (target.datatype) {
DataType.UBYTE, DataType.BYTE -> {
when(target.register!!) {
RegisterOrPair.A -> asmgen.out(" inx | lda $ESTACK_LO_HEX,x")
RegisterOrPair.A -> asmgen.out(" inx | lda P8ESTACK_LO,x")
RegisterOrPair.X -> throw AssemblyError("can't use X here")
RegisterOrPair.Y -> asmgen.out(" inx | ldy $ESTACK_LO_HEX,x")
RegisterOrPair.Y -> asmgen.out(" inx | ldy P8ESTACK_LO,x")
else -> throw AssemblyError("can't assign byte to register pair word")
}
}
DataType.UWORD, DataType.WORD, in PassByReferenceDatatypes -> {
when(target.register!!) {
RegisterOrPair.AX -> throw AssemblyError("can't use X here")
RegisterOrPair.AY-> asmgen.out(" inx | lda $ESTACK_LO_HEX,x | ldy $ESTACK_HI_HEX,x")
RegisterOrPair.AY-> asmgen.out(" inx | lda P8ESTACK_LO,x | ldy P8ESTACK_HI,x")
RegisterOrPair.XY-> throw AssemblyError("can't use X here")
else -> throw AssemblyError("can't assign word to single byte register")
}
@ -322,9 +319,9 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
val srcname = asmgen.asmIdentifierName(name)
asmgen.out("""
lda #<$srcname
sta $ESTACK_LO_HEX,x
sta P8ESTACK_LO,x
lda #>$srcname
sta $ESTACK_HI_HEX,x
sta P8ESTACK_HI,x
dex""")
}
}
@ -365,8 +362,8 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
asmgen.out("""
lda #<$sourceName
ldy #>$sourceName
sta ${C64Zeropage.SCRATCH_W1}
sty ${C64Zeropage.SCRATCH_W1+1}
sta P8ZP_SCRATCH_W1
sty P8ZP_SCRATCH_W1+1
lda #<${target.asmVarname}+$scaledIdx
ldy #>${target.asmVarname}+$scaledIdx
jsr c64flt.copy_float
@ -394,9 +391,9 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
asmgen.loadScaledArrayIndexIntoRegister(target.array, target.datatype, CpuRegister.A)
asmgen.out("""
ldy #<$sourceName
sty ${C64Zeropage.SCRATCH_W1}
sty P8ZP_SCRATCH_W1
ldy #>$sourceName
sty ${C64Zeropage.SCRATCH_W1+1}
sty P8ZP_SCRATCH_W1+1
ldy #>${target.asmVarname}
clc
adc #<${target.asmVarname}
@ -408,9 +405,9 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
}
}
else -> {
asmgen.out(" lda $sourceName | sta $ESTACK_LO_HEX,x | lda $sourceName+1 | sta $ESTACK_HI_HEX,x | dex")
asmgen.out(" lda $sourceName | sta P8ESTACK_LO,x | lda $sourceName+1 | sta P8ESTACK_HI,x | dex")
asmgen.translateExpression(index)
asmgen.out(" inx | lda $ESTACK_LO_HEX,x")
asmgen.out(" inx | lda P8ESTACK_LO,x")
popAndWriteArrayvalueWithUnscaledIndexA(target.datatype, target.asmVarname)
}
}
@ -426,9 +423,9 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
TargetStorageKind.STACK -> {
asmgen.out("""
lda #$sourceName
sta $ESTACK_LO_HEX,x
sta P8ESTACK_LO,x
lda #$sourceName+1
sta $ESTACK_HI_HEX,x
sta P8ESTACK_HI,x
dex""")
}
}
@ -493,9 +490,9 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
asmgen.out(" lda $sourceName | sta ${target.asmVarname},y")
}
else -> {
asmgen.out(" lda $sourceName | sta $ESTACK_LO_HEX,x | dex")
asmgen.out(" lda $sourceName | sta P8ESTACK_LO,x | dex")
asmgen.translateExpression(index)
asmgen.out(" inx | lda $ESTACK_LO_HEX,x")
asmgen.out(" inx | lda P8ESTACK_LO,x")
popAndWriteArrayvalueWithUnscaledIndexA(target.datatype, target.asmVarname)
}
}
@ -513,7 +510,7 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
TargetStorageKind.STACK -> {
asmgen.out("""
lda #$sourceName
sta $ESTACK_LO_HEX,x
sta P8ESTACK_LO,x
dex""")
}
}
@ -552,15 +549,15 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
asmgen.translateExpression(index)
asmgen.restoreRegister(register)
when (register) {
CpuRegister.A -> asmgen.out(" sta ${C64Zeropage.SCRATCH_B1}")
CpuRegister.X -> asmgen.out(" stx ${C64Zeropage.SCRATCH_B1}")
CpuRegister.Y -> asmgen.out(" sty ${C64Zeropage.SCRATCH_B1}")
CpuRegister.A -> asmgen.out(" sta P8ZP_SCRATCH_B1")
CpuRegister.X -> asmgen.out(" stx P8ZP_SCRATCH_B1")
CpuRegister.Y -> asmgen.out(" sty P8ZP_SCRATCH_B1")
}
asmgen.out("""
inx
lda $ESTACK_LO_HEX,x
lda P8ESTACK_LO,x
tay
lda ${C64Zeropage.SCRATCH_B1}
lda P8ZP_SCRATCH_B1
sta ${target.asmVarname},y
""")
}
@ -590,9 +587,9 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
}
TargetStorageKind.STACK -> {
when(register) {
CpuRegister.A -> asmgen.out(" sta $ESTACK_LO_HEX,x | dex")
CpuRegister.A -> asmgen.out(" sta P8ESTACK_LO,x | dex")
CpuRegister.X -> throw AssemblyError("can't use X here")
CpuRegister.Y -> asmgen.out(" tya | sta $ESTACK_LO_HEX,x | dex")
CpuRegister.Y -> asmgen.out(" tya | sta P8ESTACK_LO,x | dex")
}
}
}
@ -631,7 +628,7 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
asmgen.translateExpression(index)
asmgen.out("""
inx
lda $ESTACK_LO_HEX,x
lda P8ESTACK_LO,x
asl a
tay
lda #<${word.toHex()}
@ -651,9 +648,9 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
TargetStorageKind.STACK -> {
asmgen.out("""
lda #<${word.toHex()}
sta $ESTACK_LO_HEX,x
sta P8ESTACK_LO,x
lda #>${word.toHex()}
sta $ESTACK_HI_HEX,x
sta P8ESTACK_HI,x
dex""")
}
}
@ -682,7 +679,7 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
asmgen.translateExpression(index)
asmgen.out("""
inx
ldy $ESTACK_LO_HEX,x
ldy P8ESTACK_LO,x
lda #${byte.toHex()}
sta ${target.asmVarname},y
""")
@ -698,7 +695,7 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
TargetStorageKind.STACK -> {
asmgen.out("""
lda #${byte.toHex()}
sta $ESTACK_LO_HEX,x
sta P8ESTACK_LO,x
dex""")
}
}
@ -740,9 +737,9 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
asmgen.translateExpression(index)
asmgen.out("""
lda #<${target.asmVarname}
sta ${C64Zeropage.SCRATCH_W1}
sta P8ZP_SCRATCH_W1
lda #>${target.asmVarname}
sta ${C64Zeropage.SCRATCH_W1 + 1}
sta P8ZP_SCRATCH_W1+1
jsr c64flt.set_0_array_float
""")
}
@ -750,13 +747,13 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
TargetStorageKind.MEMORY -> throw AssemblyError("can't assign float to memory byte")
TargetStorageKind.REGISTER -> throw AssemblyError("can't assign float to register")
TargetStorageKind.STACK -> {
val floatConst = asmgen.getFloatConst(float)
val floatConst = asmgen.getFloatAsmConst(float)
asmgen.out(" lda #<$floatConst | ldy #>$floatConst | jsr c64flt.push_float")
}
}
} else {
// non-zero value
val constFloat = asmgen.getFloatConst(float)
val constFloat = asmgen.getFloatAsmConst(float)
when(target.kind) {
TargetStorageKind.VARIABLE -> {
asmgen.out("""
@ -799,13 +796,13 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
asmgen.translateExpression(index)
asmgen.out("""
lda #<${constFloat}
sta ${C64Zeropage.SCRATCH_W1}
sta P8ZP_SCRATCH_W1
lda #>${constFloat}
sta ${C64Zeropage.SCRATCH_W1 + 1}
sta P8ZP_SCRATCH_W1+1
lda #<${arrayVarName}
sta ${C64Zeropage.SCRATCH_W2}
sta P8ZP_SCRATCH_W2
lda #>${arrayVarName}
sta ${C64Zeropage.SCRATCH_W2 + 1}
sta P8ZP_SCRATCH_W2+1
jsr c64flt.set_array_float
""")
}
@ -813,7 +810,7 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
TargetStorageKind.MEMORY -> throw AssemblyError("can't assign float to memory byte")
TargetStorageKind.REGISTER -> throw AssemblyError("can't assign float to register")
TargetStorageKind.STACK -> {
val floatConst = asmgen.getFloatConst(float)
val floatConst = asmgen.getFloatAsmConst(float)
asmgen.out(" lda #<$floatConst | ldy #>$floatConst | jsr c64flt.push_float")
}
}
@ -844,7 +841,7 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
TargetStorageKind.STACK -> {
asmgen.out("""
lda ${address.toHex()}
sta $ESTACK_LO_HEX,x
sta P8ESTACK_LO,x
dex""")
}
}
@ -872,7 +869,7 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
}
TargetStorageKind.STACK -> {
asmgen.loadByteFromPointerIntoA(identifier)
asmgen.out(" sta $ESTACK_LO_HEX,x | dex")
asmgen.out(" sta P8ESTACK_LO,x | dex")
}
}
}
@ -892,13 +889,13 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
asmgen.translateExpression(addressExpr)
asmgen.out("""
inx
lda $ESTACK_LO_HEX,x
sta ${C64Zeropage.SCRATCH_W2}
lda $ESTACK_HI_HEX,x
sta ${C64Zeropage.SCRATCH_W2+1}
lda P8ESTACK_LO,x
sta P8ZP_SCRATCH_W2
lda P8ESTACK_HI,x
sta P8ZP_SCRATCH_W2+1
lda $ldaInstructionArg
ldy #0
sta (${C64Zeropage.SCRATCH_W2}),y""")
sta (P8ZP_SCRATCH_W2),y""")
}
}
}
@ -926,12 +923,12 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
asmgen.restoreRegister(CpuRegister.A)
asmgen.out("""
inx
ldy $ESTACK_LO_HEX,x
sty ${C64Zeropage.SCRATCH_W2}
ldy $ESTACK_HI_HEX,x
sty ${C64Zeropage.SCRATCH_W2+1}
ldy P8ESTACK_LO,x
sty P8ZP_SCRATCH_W2
ldy P8ESTACK_HI,x
sty P8ZP_SCRATCH_W2+1
ldy #0
sta (${C64Zeropage.SCRATCH_W2}),y""")
sta (P8ZP_SCRATCH_W2),y""")
}
}
}
@ -939,13 +936,13 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
private fun popAndWriteArrayvalueWithUnscaledIndexA(elementDt: DataType, asmArrayvarname: String) {
when (elementDt) {
in ByteDatatypes ->
asmgen.out(" tay | inx | lda $ESTACK_LO_HEX,x | sta $asmArrayvarname,y")
asmgen.out(" tay | inx | lda P8ESTACK_LO,x | sta $asmArrayvarname,y")
in WordDatatypes ->
asmgen.out(" asl a | tay | inx | lda $ESTACK_LO_HEX,x | sta $asmArrayvarname,y | lda $ESTACK_HI_HEX,x | sta $asmArrayvarname+1,y")
asmgen.out(" asl a | tay | inx | lda P8ESTACK_LO,x | sta $asmArrayvarname,y | lda P8ESTACK_HI,x | sta $asmArrayvarname+1,y")
DataType.FLOAT ->
// scaling * 5 is done in the subroutine that's called
asmgen.out("""
sta $ESTACK_LO_HEX,x
sta P8ESTACK_LO,x
dex
lda #<$asmArrayvarname
ldy #>$asmArrayvarname

View File

@ -4,9 +4,7 @@ import prog8.ast.Program
import prog8.ast.base.*
import prog8.ast.expressions.*
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.CompilationTarget
import prog8.compiler.target.c64.codegen.AsmGen
import prog8.compiler.toHex
@ -194,18 +192,19 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
println("warning: slow stack evaluation used (1): ${memory.addressExpression::class.simpleName} at ${memory.addressExpression.position}") // TODO optimize...
asmgen.translateExpression(memory.addressExpression)
// TODO buggy?:
asmgen.out(" jsr prog8_lib.read_byte_from_address_on_stack | sta ${C64Zeropage.SCRATCH_B1}")
asmgen.out(" jsr prog8_lib.read_byte_from_address_on_stack | sta P8ZP_SCRATCH_B1")
val zp = CompilationTarget.machine.zeropage
when {
valueLv != null -> inplaceModification_byte_litval_to_variable(C64Zeropage.SCRATCH_B1.toHex(), DataType.UBYTE, operator, valueLv.toInt())
ident != null -> inplaceModification_byte_variable_to_variable(C64Zeropage.SCRATCH_B1.toHex(), DataType.UBYTE, operator, ident)
valueLv != null -> inplaceModification_byte_litval_to_variable(zp.SCRATCH_B1.toHex(), DataType.UBYTE, operator, valueLv.toInt())
ident != null -> inplaceModification_byte_variable_to_variable(zp.SCRATCH_B1.toHex(), DataType.UBYTE, operator, ident)
// TODO more specialized code for types such as memory read etc.
value is TypecastExpression -> {
if (tryRemoveRedundantCast(value, target, operator)) return
inplaceModification_byte_value_to_variable(C64Zeropage.SCRATCH_B1.toHex(), DataType.UBYTE, operator, value)
inplaceModification_byte_value_to_variable(zp.SCRATCH_B1.toHex(), DataType.UBYTE, operator, value)
}
else -> inplaceModification_byte_value_to_variable(C64Zeropage.SCRATCH_B1.toHex(), DataType.UBYTE, operator, value)
else -> inplaceModification_byte_value_to_variable(zp.SCRATCH_B1.toHex(), DataType.UBYTE, operator, value)
}
asmgen.out(" lda ${C64Zeropage.SCRATCH_B1} | jsr prog8_lib.write_byte_to_address_on_stack | inx")
asmgen.out(" lda P8ZP_SCRATCH_B1 | jsr prog8_lib.write_byte_to_address_on_stack | inx")
}
}
}
@ -240,27 +239,27 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
"+" -> {
asmgen.out("""
jsr c64flt.pop_float_fac1
stx ${C64Zeropage.SCRATCH_REG_X}
stx P8ZP_SCRATCH_REG_X
lda #<$name
ldy #>$name
jsr c64flt.FADD
ldx #<$name
ldy #>$name
jsr c64flt.MOVMF
ldx ${C64Zeropage.SCRATCH_REG_X}
ldx P8ZP_SCRATCH_REG_X
""")
}
"-" -> {
asmgen.out("""
jsr c64flt.pop_float_fac1
stx ${C64Zeropage.SCRATCH_REG_X}
stx P8ZP_SCRATCH_REG_X
lda #<$name
ldy #>$name
jsr c64flt.FSUB
ldx #<$name
ldy #>$name
jsr c64flt.MOVMF
ldx ${C64Zeropage.SCRATCH_REG_X}
ldx P8ZP_SCRATCH_REG_X
""")
}
"*" -> TODO("mul")// asmgen.out(" jsr prog8_lib.mul_byte") // the optimized routines should have been checked earlier
@ -290,7 +289,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
"-" -> TODO("-")
"*" -> {
asmgen.out("""
stx ${C64Zeropage.SCRATCH_REG_X}
stx P8ZP_SCRATCH_REG_X
lda #<$name
ldy #>$name
jsr c64flt.MOVFM
@ -300,7 +299,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
ldx #<$name
ldy #>$name
jsr c64flt.MOVMF
ldx ${C64Zeropage.SCRATCH_REG_X}
ldx P8ZP_SCRATCH_REG_X
""")
}
"/" -> TODO("div")
@ -310,14 +309,14 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
}
private fun inplaceModification_float_litval_to_variable(name: String, operator: String, value: Double) {
val constValueName = asmgen.getFloatConst(value)
val constValueName = asmgen.getFloatAsmConst(value)
when (operator) {
"**" -> TODO("pow")
"+" -> {
if (value == 0.0)
return
asmgen.out("""
stx ${C64Zeropage.SCRATCH_REG_X}
stx P8ZP_SCRATCH_REG_X
lda #<$name
ldy #>$name
jsr c64flt.MOVFM
@ -327,14 +326,14 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
ldx #<$name
ldy #>$name
jsr c64flt.MOVMF
ldx ${C64Zeropage.SCRATCH_REG_X}
ldx P8ZP_SCRATCH_REG_X
""")
}
"-" -> {
if (value == 0.0)
return
asmgen.out("""
stx ${C64Zeropage.SCRATCH_REG_X}
stx P8ZP_SCRATCH_REG_X
lda #<$constValueName
ldy #>$constValueName
jsr c64flt.MOVFM
@ -344,7 +343,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
ldx #<$name
ldy #>$name
jsr c64flt.MOVMF
ldx ${C64Zeropage.SCRATCH_REG_X}
ldx P8ZP_SCRATCH_REG_X
""")
}
"*" -> TODO("mul")
@ -369,19 +368,19 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
// note: ** (power) operator requires floats.
"+" -> {
val (ptrOnZp, sourceName) = asmgen.loadByteFromPointerIntoA(pointervar)
asmgen.out(" clc | adc $ESTACK_LO_PLUS1_HEX,x")
asmgen.out(" clc | adc P8ESTACK_LO+1,x")
if(ptrOnZp)
asmgen.out(" sta ($sourceName),y")
else
asmgen.out(" sta (${C64Zeropage.SCRATCH_W1}),y")
asmgen.out(" sta (P8ZP_SCRATCH_W1),y")
}
"-" -> {
val (ptrOnZp, sourceName) = asmgen.loadByteFromPointerIntoA(pointervar)
asmgen.out(" sec | sbc $ESTACK_LO_PLUS1_HEX,x")
asmgen.out(" sec | sbc P8ESTACK_LO+1,x")
if(ptrOnZp)
asmgen.out(" sta ($sourceName),y")
else
asmgen.out(" sta (${C64Zeropage.SCRATCH_W1}),y")
asmgen.out(" sta (P8ZP_SCRATCH_W1),y")
}
"*" -> TODO("mul")// asmgen.out(" jsr prog8_lib.mul_byte") // the optimized routines should have been checked earlier
"/" -> TODO("div")// asmgen.out(if(types==DataType.UBYTE) " jsr prog8_lib.idiv_ub" else " jsr prog8_lib.idiv_b")
@ -395,27 +394,27 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
">>" -> TODO("ubyte lsr")
"&" -> {
val (ptrOnZp, sourceName) = asmgen.loadByteFromPointerIntoA(pointervar)
asmgen.out(" and $ESTACK_LO_PLUS1_HEX,x")
asmgen.out(" and P8ESTACK_LO+1,x")
if(ptrOnZp)
asmgen.out(" sta ($sourceName),y")
else
asmgen.out(" sta (${C64Zeropage.SCRATCH_W1}),y")
asmgen.out(" sta (P8ZP_SCRATCH_W1),y")
}
"^" -> {
val (ptrOnZp, sourceName) = asmgen.loadByteFromPointerIntoA(pointervar)
asmgen.out(" xor $ESTACK_LO_PLUS1_HEX,x")
asmgen.out(" xor P8ESTACK_LO+1,x")
if(ptrOnZp)
asmgen.out(" sta ($sourceName),y")
else
asmgen.out(" sta (${C64Zeropage.SCRATCH_W1}),y")
asmgen.out(" sta (P8ZP_SCRATCH_W1),y")
}
"|" -> {
val (ptrOnZp, sourceName) = asmgen.loadByteFromPointerIntoA(pointervar)
asmgen.out(" ora $ESTACK_LO_PLUS1_HEX,x")
asmgen.out(" ora P8ESTACK_LO+1,x")
if(ptrOnZp)
asmgen.out(" sta ($sourceName),y")
else
asmgen.out(" sta (${C64Zeropage.SCRATCH_W1}),y")
asmgen.out(" sta (P8ZP_SCRATCH_W1),y")
}
else -> throw AssemblyError("invalid operator for in-place modification $operator")
}
@ -432,7 +431,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
if(ptrOnZp)
asmgen.out(" sta ($sourceName),y")
else
asmgen.out(" sta (${C64Zeropage.SCRATCH_W1}),y")
asmgen.out(" sta (P8ZP_SCRATCH_W1),y")
}
"-" -> {
val (ptrOnZp, sourceName) = asmgen.loadByteFromPointerIntoA(pointervar)
@ -440,7 +439,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
if(ptrOnZp)
asmgen.out(" sta ($sourceName),y")
else
asmgen.out(" sta (${C64Zeropage.SCRATCH_W1}),y")
asmgen.out(" sta (P8ZP_SCRATCH_W1),y")
}
"*" -> TODO("mul")// asmgen.out(" jsr prog8_lib.mul_byte") // the optimized routines should have been checked earlier
"/" -> TODO("div")// asmgen.out(if(types==DataType.UBYTE) " jsr prog8_lib.idiv_ub" else " jsr prog8_lib.idiv_b")
@ -458,7 +457,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
if(ptrOnZp)
asmgen.out(" sta ($sourceName),y")
else
asmgen.out(" sta (${C64Zeropage.SCRATCH_W1}),y")
asmgen.out(" sta (P8ZP_SCRATCH_W1),y")
}
"^" -> {
val (ptrOnZp, sourceName) = asmgen.loadByteFromPointerIntoA(pointervar)
@ -466,7 +465,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
if(ptrOnZp)
asmgen.out(" sta ($sourceName),y")
else
asmgen.out(" sta (${C64Zeropage.SCRATCH_W1}),y")
asmgen.out(" sta (P8ZP_SCRATCH_W1),y")
}
"|" -> {
val (ptrOnZp, sourceName) = asmgen.loadByteFromPointerIntoA(pointervar)
@ -474,7 +473,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
if(ptrOnZp)
asmgen.out(" sta ($sourceName),y")
else
asmgen.out(" sta (${C64Zeropage.SCRATCH_W1}),y")
asmgen.out(" sta (P8ZP_SCRATCH_W1),y")
}
else -> throw AssemblyError("invalid operator for in-place modification $operator")
}
@ -489,7 +488,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
if(ptrOnZp)
asmgen.out(" sta ($sourceName),y")
else
asmgen.out(" sta (${C64Zeropage.SCRATCH_W1}),y")
asmgen.out(" sta (P8ZP_SCRATCH_W1),y")
}
"-" -> {
val (ptrOnZp, sourceName) = asmgen.loadByteFromPointerIntoA(pointervar)
@ -497,7 +496,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
if(ptrOnZp)
asmgen.out(" sta ($sourceName),y")
else
asmgen.out(" sta (${C64Zeropage.SCRATCH_W1}),y")
asmgen.out(" sta (P8ZP_SCRATCH_W1),y")
}
"*" -> TODO("mul")// asmgen.out(" jsr prog8_lib.mul_byte") // the optimized routines should have been checked earlier
"/" -> {
@ -521,7 +520,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
if(ptrOnZp)
asmgen.out(" sta ($sourceName),y")
else
asmgen.out(" sta (${C64Zeropage.SCRATCH_W1}),y")
asmgen.out(" sta (P8ZP_SCRATCH_W1),y")
}
}
">>" -> {
@ -531,7 +530,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
if(ptrOnZp)
asmgen.out(" sta ($sourceName),y")
else
asmgen.out(" sta (${C64Zeropage.SCRATCH_W1}),y")
asmgen.out(" sta (P8ZP_SCRATCH_W1),y")
}
}
"&" -> {
@ -540,7 +539,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
if(ptrOnZp)
asmgen.out(" sta ($sourceName),y")
else
asmgen.out(" sta (${C64Zeropage.SCRATCH_W1}),y")
asmgen.out(" sta (P8ZP_SCRATCH_W1),y")
}
"^" -> {
val (ptrOnZp, sourceName) = asmgen.loadByteFromPointerIntoA(pointervar)
@ -548,7 +547,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
if(ptrOnZp)
asmgen.out(" sta ($sourceName),y")
else
asmgen.out(" sta (${C64Zeropage.SCRATCH_W1}),y")
asmgen.out(" sta (P8ZP_SCRATCH_W1),y")
}
"|" -> {
val (ptrOnZp, sourceName) = asmgen.loadByteFromPointerIntoA(pointervar)
@ -556,7 +555,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
if(ptrOnZp)
asmgen.out(" sta ($sourceName),y")
else
asmgen.out(" sta (${C64Zeropage.SCRATCH_W1}),y")
asmgen.out(" sta (P8ZP_SCRATCH_W1),y")
}
else -> throw AssemblyError("invalid operator for in-place modification $operator")
}
@ -782,7 +781,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
"+" -> asmgen.out("""
lda $name
clc
adc $ESTACK_LO_PLUS1_HEX,x
adc P8ESTACK_LO+1,x
sta $name
bcc +
inc $name+1
@ -790,7 +789,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
"-" -> asmgen.out("""
lda $name
sec
sbc $ESTACK_LO_PLUS1_HEX,x
sbc P8ESTACK_LO+1,x
sta $name
bcs +
dec $name+1
@ -846,8 +845,8 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
// the value is a proper 16-bit word, so use both bytes of it.
when (operator) {
// note: ** (power) operator requires floats.
"+" -> asmgen.out(" lda $name | clc | adc $ESTACK_LO_PLUS1_HEX,x | sta $name | lda $name+1 | adc $ESTACK_HI_PLUS1_HEX,x | sta $name+1")
"-" -> asmgen.out(" lda $name | sec | sbc $ESTACK_LO_PLUS1_HEX,x | sta $name | lda $name+1 | sbc $ESTACK_HI_PLUS1_HEX,x | sta $name+1")
"+" -> asmgen.out(" lda $name | clc | adc P8ESTACK_LO+1,x | sta $name | lda $name+1 | adc P8ESTACK_HI+1,x | sta $name+1")
"-" -> asmgen.out(" lda $name | sec | sbc P8ESTACK_LO+1,x | sta $name | lda $name+1 | sbc P8ESTACK_HI+1,x | sta $name+1")
"*" -> TODO("mul")// asmgen.out(" jsr prog8_lib.mul_byte") // the optimized routines should have been checked earlier
"/" -> TODO("div")// asmgen.out(if(types==DataType.UBYTE) " jsr prog8_lib.idiv_ub" else " jsr prog8_lib.idiv_b")
"%" -> {
@ -857,9 +856,9 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
// asmgen.out(" jsr prog8_lib.remainder_ub")
}
"<<", ">>" -> throw AssemblyError("shift by a word value not supported, max is a byte")
"&" -> asmgen.out(" lda $name | and $ESTACK_LO_PLUS1_HEX,x | sta $name | lda $name+1 | and $ESTACK_HI_PLUS1_HEX,x | sta $name+1")
"^" -> asmgen.out(" lda $name | xor $ESTACK_LO_PLUS1_HEX,x | sta $name | lda $name+1 | xor $ESTACK_HI_PLUS1_HEX,x | sta $name+1")
"|" -> asmgen.out(" lda $name | ora $ESTACK_LO_PLUS1_HEX,x | sta $name | lda $name+1 | ora $ESTACK_HI_PLUS1_HEX,x | sta $name+1")
"&" -> asmgen.out(" lda $name | and P8ESTACK_LO+1,x | sta $name | lda $name+1 | and P8ESTACK_HI+1,x | sta $name+1")
"^" -> asmgen.out(" lda $name | xor P8ESTACK_LO+1,x | sta $name | lda $name+1 | xor P8ESTACK_HI+1,x | sta $name+1")
"|" -> asmgen.out(" lda $name | ora P8ESTACK_LO+1,x | sta $name | lda $name+1 | ora P8ESTACK_HI+1,x | sta $name+1")
else -> throw AssemblyError("invalid operator for in-place modification $operator")
}
}
@ -878,8 +877,8 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
asmgen.translateExpression(value)
when (operator) {
// note: ** (power) operator requires floats.
"+" -> asmgen.out(" lda $name | clc | adc $ESTACK_LO_PLUS1_HEX,x | sta $name")
"-" -> asmgen.out(" lda $name | sec | sbc $ESTACK_LO_PLUS1_HEX,x | sta $name")
"+" -> asmgen.out(" lda $name | clc | adc P8ESTACK_LO+1,x | sta $name")
"-" -> asmgen.out(" lda $name | sec | sbc P8ESTACK_LO+1,x | sta $name")
"*" -> TODO("mul")// asmgen.out(" jsr prog8_lib.mul_byte") // the optimized routines should have been checked earlier
"/" -> TODO("div")// asmgen.out(if(types==DataType.UBYTE) " jsr prog8_lib.idiv_ub" else " jsr prog8_lib.idiv_b")
"%" -> {
@ -923,9 +922,9 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
+""")
}
}
"&" -> asmgen.out(" lda $name | and $ESTACK_LO_PLUS1_HEX,x | sta $name")
"^" -> asmgen.out(" lda $name | xor $ESTACK_LO_PLUS1_HEX,x | sta $name")
"|" -> asmgen.out(" lda $name | ora $ESTACK_LO_PLUS1_HEX,x | sta $name")
"&" -> asmgen.out(" lda $name | and P8ESTACK_LO+1,x | sta $name")
"^" -> asmgen.out(" lda $name | xor P8ESTACK_LO+1,x | sta $name")
"|" -> asmgen.out(" lda $name | ora P8ESTACK_LO+1,x | sta $name")
else -> throw AssemblyError("invalid operator for in-place modification $operator")
}
asmgen.out(" inx")
@ -1063,7 +1062,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
asmgen.loadScaledArrayIndexIntoRegister(target.array!!, target.datatype, CpuRegister.Y, true)
asmgen.out(" lda #0 | sta ${target.asmVarname},y")
}
TargetStorageKind.STACK -> asmgen.out(" lda #0 | sta $ESTACK_HI_PLUS1_HEX,x")
TargetStorageKind.STACK -> asmgen.out(" lda #0 | sta P8ESTACK_HI+1,x")
else -> throw AssemblyError("weird target")
}
}
@ -1121,7 +1120,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
if(ptrOnZp)
asmgen.out(" sta ($sourceName),y")
else
asmgen.out(" sta (${C64Zeropage.SCRATCH_W1}),y")
asmgen.out(" sta (P8ZP_SCRATCH_W1),y")
}
else -> {
println("warning: slow stack evaluation used (6): ${mem.addressExpression::class.simpleName} at ${mem.addressExpression.position}") // TODO
@ -1192,7 +1191,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
if(ptrOnZp)
asmgen.out(" sta ($sourceName),y")
else
asmgen.out(" sta (${C64Zeropage.SCRATCH_W1}),y")
asmgen.out(" sta (P8ZP_SCRATCH_W1),y")
}
else -> {
println("warning: slow stack evaluation used (7): ${memory.addressExpression::class.simpleName} at ${memory.addressExpression.position}") // TODO
@ -1272,7 +1271,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
when(target.kind) {
TargetStorageKind.VARIABLE -> {
asmgen.out("""
stx ${C64Zeropage.SCRATCH_REG_X}
stx P8ZP_SCRATCH_REG_X
lda #<${target.asmVarname}
ldy #>${target.asmVarname}
jsr c64flt.MOVFM
@ -1280,7 +1279,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
ldx #<${target.asmVarname}
ldy #>${target.asmVarname}
jsr c64flt.MOVMF
ldx ${C64Zeropage.SCRATCH_REG_X}
ldx P8ZP_SCRATCH_REG_X
""")
}
TargetStorageKind.ARRAY -> TODO("in-place negate float array")

View File

@ -123,7 +123,7 @@ class TestCompiler {
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class TestZeropage {
class TestC64Zeropage {
private val errors = ErrorReporter()