adding setlsb() and setmsb() builtin functions to 6502 codegen

This commit is contained in:
Irmen de Jong 2023-09-15 23:05:42 +02:00
parent 95e4490a8a
commit a1874f6f00
4 changed files with 51 additions and 2 deletions

View File

@ -612,7 +612,51 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram,
}
private fun funcSetLsbMsb(fcall: PtBuiltinFunctionCall, msb: Boolean) {
TODO("setlsb/setmsb for $fcall")
asmgen.assignExpressionToRegister(fcall.args[1], RegisterOrPair.A, false)
val address: PtExpression
when(fcall.args[0]) {
is PtIdentifier -> {
if(msb) {
address = PtBinaryExpression("+", DataType.UWORD, fcall.args[0].position)
val addressOf = PtAddressOf(fcall.position)
addressOf.add(fcall.args[0])
address.add(addressOf)
address.add(PtNumber(address.type, 1.0, fcall.args[0].position))
} else {
address = PtAddressOf(fcall.position)
address.add(fcall.args[0])
}
}
is PtNumber -> {
val num = (fcall.args[0] as PtNumber).number + if(msb) 1 else 0
address = PtNumber(fcall.args[0].type, num, fcall.args[0].position)
}
is PtAddressOf -> {
if(msb) {
address = PtBinaryExpression("+", DataType.UWORD, fcall.args[0].position)
address.add(fcall.args[0])
address.add(PtNumber(address.type, 1.0, fcall.args[0].position))
} else {
address = fcall.args[0]
}
}
is PtArrayIndexer -> {
val indexer = fcall.args[0] as PtArrayIndexer
require(!indexer.usesPointerVariable)
if(indexer.splitWords) {
// lsb/msb in split arrays, element index 'size' is always 1
TODO("setlsb/setmsb on split array element ${fcall.position}")
} else {
TODO("setlsb/setmsb on array element ${fcall.position}")
}
}
else -> throw AssemblyError("setlsb/setmsb on weird target ${fcall.args[0]}")
}
val mem = PtMemoryByte(fcall.position)
mem.add(address)
mem.parent = fcall
assignAsmGen.storeRegisterAInMemoryAddress(mem) // TODO use assignRegisterByte()???, and assignConstantByte() if the value is contstant zero
}
private fun funcSgn(fcall: PtBuiltinFunctionCall, resultRegister: RegisterOrPair?, scope: IPtSubroutine?) {

View File

@ -3589,7 +3589,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
}
}
private fun storeRegisterAInMemoryAddress(memoryAddress: PtMemoryByte) {
internal fun storeRegisterAInMemoryAddress(memoryAddress: PtMemoryByte) {
val addressExpr = memoryAddress.address
val addressLv = addressExpr as? PtNumber
val addressOf = addressExpr as? PtAddressOf

View File

@ -597,6 +597,7 @@ internal class BuiltinFuncGen(private val codeGen: IRCodeGen, private val exprGe
is PtArrayIndexer -> {
require(!target.usesPointerVariable)
if(target.splitWords) {
// lsb/msb in split arrays, element index 'size' is always 1
val varName = target.variable.name + if(msb) "_msb" else "_lsb"
val valueTr = exprGen.translateExpression(call.args[1])
addToResult(result, valueTr, valueTr.resultReg, -1)

View File

@ -7,9 +7,11 @@ main {
txt.print_uwhex(zz, true)
txt.nl()
;@(&zz) = $11
setlsb(zz, $11)
txt.print_uwhex(zz, true)
txt.nl()
;@(&zz+1) = $22
setmsb(zz, $22)
txt.print_uwhex(zz, true)
txt.nl()
@ -23,6 +25,8 @@ main {
txt.nl()
txt.print_uwhex(array[2], true)
txt.nl()
;@(&array+one*2) = $ff
;@(&array+two*2+1) = $ff
setlsb(array[one],$ff)
setmsb(array[two],$00)
txt.print_uwhex(array[1], true)