This commit is contained in:
Irmen de Jong 2022-08-04 18:28:33 +02:00
parent 34aa917ca4
commit 00b717cde8
15 changed files with 35 additions and 31 deletions

View File

@ -12,6 +12,15 @@ sealed class PtExpression(val type: DataType, position: Position) : PtNode(posit
init { init {
if(type==DataType.BOOL) if(type==DataType.BOOL)
throw IllegalArgumentException("bool should have become ubyte @$position") throw IllegalArgumentException("bool should have become ubyte @$position")
if(type==DataType.UNDEFINED) {
@Suppress("LeakingThis")
when(this) {
is PtBuiltinFunctionCall -> {}
is PtFunctionCall -> {}
is PtIdentifier -> {}
else -> throw IllegalArgumentException("type should be known @$position")
}
}
} }
override fun printProperties() { override fun printProperties() {

View File

@ -3,11 +3,6 @@ package prog8.code.core
import java.nio.file.Path import java.nio.file.Path
interface IMachineFloat {
fun toDouble(): Double
fun makeFloatFillAsm(): String
}
enum class CpuType { enum class CpuType {
CPU6502, CPU6502,
CPU65c02, CPU65c02,
@ -27,7 +22,7 @@ interface IMachineDefinition {
val cpu: CpuType val cpu: CpuType
fun initializeZeropage(compilerOptions: CompilationOptions) fun initializeZeropage(compilerOptions: CompilationOptions)
fun getFloat(num: Number): IMachineFloat fun getFloatAsmBytes(num: Number): String
fun importLibs(compilerOptions: CompilationOptions, compilationTargetName: String): List<String> fun importLibs(compilerOptions: CompilationOptions, compilationTargetName: String): List<String>
fun launchEmulator(selectedEmulator: Int, programNameWithPath: Path) fun launchEmulator(selectedEmulator: Int, programNameWithPath: Path)

View File

@ -20,7 +20,7 @@ class AtariMachineDefinition: IMachineDefinition {
override lateinit var zeropage: Zeropage override lateinit var zeropage: Zeropage
override fun getFloat(num: Number) = TODO("float from number") override fun getFloatAsmBytes(num: Number) = TODO("float asm bytes from number")
override fun importLibs(compilerOptions: CompilationOptions, compilationTargetName: String): List<String> { override fun importLibs(compilerOptions: CompilationOptions, compilationTargetName: String): List<String> {
return if (compilerOptions.output == OutputType.XEX) return if (compilerOptions.output == OutputType.XEX)

View File

@ -21,7 +21,7 @@ class C128MachineDefinition: IMachineDefinition {
override lateinit var zeropage: Zeropage override lateinit var zeropage: Zeropage
override fun getFloat(num: Number) = Mflpt5.fromNumber(num) override fun getFloatAsmBytes(num: Number) = Mflpt5.fromNumber(num).makeFloatFillAsm()
override fun importLibs(compilerOptions: CompilationOptions, compilationTargetName: String): List<String> { override fun importLibs(compilerOptions: CompilationOptions, compilationTargetName: String): List<String> {
return if (compilerOptions.launcher == CbmPrgLauncherType.BASIC || compilerOptions.output == OutputType.PRG) return if (compilerOptions.launcher == CbmPrgLauncherType.BASIC || compilerOptions.output == OutputType.PRG)

View File

@ -21,7 +21,7 @@ class C64MachineDefinition: IMachineDefinition {
override lateinit var zeropage: Zeropage override lateinit var zeropage: Zeropage
override fun getFloat(num: Number) = Mflpt5.fromNumber(num) override fun getFloatAsmBytes(num: Number) = Mflpt5.fromNumber(num).makeFloatFillAsm()
override fun importLibs(compilerOptions: CompilationOptions, compilationTargetName: String): List<String> { override fun importLibs(compilerOptions: CompilationOptions, compilationTargetName: String): List<String> {
return if (compilerOptions.launcher == CbmPrgLauncherType.BASIC || compilerOptions.output == OutputType.PRG) return if (compilerOptions.launcher == CbmPrgLauncherType.BASIC || compilerOptions.output == OutputType.PRG)

View File

@ -1,12 +1,11 @@
package prog8.code.target.cbm package prog8.code.target.cbm
import prog8.code.core.IMachineFloat
import prog8.code.core.InternalCompilerException import prog8.code.core.InternalCompilerException
import kotlin.math.absoluteValue import kotlin.math.absoluteValue
import kotlin.math.pow import kotlin.math.pow
data class Mflpt5(val b0: UByte, val b1: UByte, val b2: UByte, val b3: UByte, val b4: UByte): IMachineFloat { data class Mflpt5(val b0: UByte, val b1: UByte, val b2: UByte, val b3: UByte, val b4: UByte) {
companion object { companion object {
const val FLOAT_MAX_POSITIVE = 1.7014118345e+38 // bytes: 255,127,255,255,255 const val FLOAT_MAX_POSITIVE = 1.7014118345e+38 // bytes: 255,127,255,255,255
@ -58,7 +57,7 @@ data class Mflpt5(val b0: UByte, val b1: UByte, val b2: UByte, val b3: UByte, va
} }
} }
override fun toDouble(): Double { fun toDouble(): Double {
if (this == zero) return 0.0 if (this == zero) return 0.0
val exp = b0.toInt() - 128 val exp = b0.toInt() - 128
val sign = (b1.toInt() and 0x80) > 0 val sign = (b1.toInt() and 0x80) > 0
@ -67,7 +66,7 @@ data class Mflpt5(val b0: UByte, val b1: UByte, val b2: UByte, val b3: UByte, va
return if (sign) -result else result return if (sign) -result else result
} }
override fun makeFloatFillAsm(): String { fun makeFloatFillAsm(): String {
val b0 = "$" + b0.toString(16).padStart(2, '0') val b0 = "$" + b0.toString(16).padStart(2, '0')
val b1 = "$" + b1.toString(16).padStart(2, '0') val b1 = "$" + b1.toString(16).padStart(2, '0')
val b2 = "$" + b2.toString(16).padStart(2, '0') val b2 = "$" + b2.toString(16).padStart(2, '0')

View File

@ -20,7 +20,7 @@ class CX16MachineDefinition: IMachineDefinition {
override lateinit var zeropage: Zeropage override lateinit var zeropage: Zeropage
override fun getFloat(num: Number) = Mflpt5.fromNumber(num) override fun getFloatAsmBytes(num: Number) = Mflpt5.fromNumber(num).makeFloatFillAsm()
override fun importLibs(compilerOptions: CompilationOptions, compilationTargetName: String): List<String> { override fun importLibs(compilerOptions: CompilationOptions, compilationTargetName: String): List<String> {
return if (compilerOptions.launcher == CbmPrgLauncherType.BASIC || compilerOptions.output == OutputType.PRG) return if (compilerOptions.launcher == CbmPrgLauncherType.BASIC || compilerOptions.output == OutputType.PRG)
listOf("syslib") listOf("syslib")

View File

@ -22,7 +22,7 @@ class VirtualMachineDefinition: IMachineDefinition {
override lateinit var zeropage: Zeropage // not actually used override lateinit var zeropage: Zeropage // not actually used
override fun getFloat(num: Number) = TODO("float from number") override fun getFloatAsmBytes(num: Number) = TODO("float asm bytes from number")
override fun importLibs(compilerOptions: CompilationOptions, compilationTargetName: String): List<String> { override fun importLibs(compilerOptions: CompilationOptions, compilationTargetName: String): List<String> {
return listOf("syslib") return listOf("syslib")

View File

@ -48,7 +48,7 @@ internal class ProgramAndVarsGen(
// the global list of all floating point constants for the whole program // the global list of all floating point constants for the whole program
asmgen.out("; global float constants") asmgen.out("; global float constants")
for (flt in allocator.globalFloatConsts) { for (flt in allocator.globalFloatConsts) {
val floatFill = compTarget.machine.getFloat(flt.key).makeFloatFillAsm() val floatFill = compTarget.machine.getFloatAsmBytes(flt.key)
val floatvalue = flt.key val floatvalue = flt.key
asmgen.out("${flt.value}\t.byte $floatFill ; float $floatvalue") asmgen.out("${flt.value}\t.byte $floatFill ; float $floatvalue")
} }
@ -497,7 +497,7 @@ internal class ProgramAndVarsGen(
if(initialValue==0) { if(initialValue==0) {
asmgen.out("$name\t.byte 0,0,0,0,0 ; float") asmgen.out("$name\t.byte 0,0,0,0,0 ; float")
} else { } else {
val floatFill = compTarget.machine.getFloat(initialValue).makeFloatFillAsm() val floatFill = compTarget.machine.getFloatAsmBytes(initialValue)
asmgen.out("$name\t.byte $floatFill ; float $initialValue") asmgen.out("$name\t.byte $floatFill ; float $initialValue")
} }
} }
@ -556,7 +556,7 @@ internal class ProgramAndVarsGen(
DataType.ARRAY_F -> { DataType.ARRAY_F -> {
val array = value ?: zeroFilledArray(orNumberOfZeros!!) val array = value ?: zeroFilledArray(orNumberOfZeros!!)
val floatFills = array.map { val floatFills = array.map {
compTarget.machine.getFloat(it.number!!).makeFloatFillAsm() compTarget.machine.getFloatAsmBytes(it.number!!)
} }
asmgen.out(varname) asmgen.out(varname)
for (f in array.zip(floatFills)) for (f in array.zip(floatFills))

View File

@ -45,7 +45,7 @@ internal class BuiltinFuncGen(private val codeGen: CodeGen, private val exprGen:
"ror" -> funcRolRor(Opcode.ROXR, call, resultRegister) "ror" -> funcRolRor(Opcode.ROXR, call, resultRegister)
"rol2" -> funcRolRor(Opcode.ROL, call, resultRegister) "rol2" -> funcRolRor(Opcode.ROL, call, resultRegister)
"ror2" -> funcRolRor(Opcode.ROR, call, resultRegister) "ror2" -> funcRolRor(Opcode.ROR, call, resultRegister)
else -> TODO("builtinfunc ${call.name}") else -> throw AssemblyError("missing builtinfunc for ${call.name}")
} }
} }

View File

@ -279,7 +279,7 @@ class CodeGen(internal val program: PtProgram,
code += addConstMem(loopvarDt, loopvarAddress.toUInt(), step) code += addConstMem(loopvarDt, loopvarAddress.toUInt(), step)
code += VmCodeInstruction(Opcode.LOADM, loopvarDt, reg1 = indexReg, value = loopvarAddress) code += VmCodeInstruction(Opcode.LOADM, loopvarDt, reg1 = indexReg, value = loopvarAddress)
} else { } else {
// TODO WHY THID DISTINCTION? // TODO WHY THIS DISTINCTION?
code += VmCodeInstruction(Opcode.LOADM, loopvarDt, reg1 = indexReg, value = loopvarAddress) code += VmCodeInstruction(Opcode.LOADM, loopvarDt, reg1 = indexReg, value = loopvarAddress)
code += addConstReg(loopvarDt, indexReg, step) code += addConstReg(loopvarDt, indexReg, step)
code += VmCodeInstruction(Opcode.STOREM, loopvarDt, reg1 = indexReg, value = loopvarAddress) code += VmCodeInstruction(Opcode.STOREM, loopvarDt, reg1 = indexReg, value = loopvarAddress)

View File

@ -315,7 +315,7 @@ hline_zero2
; for efficiency of internal algorithms here is the internal plot routine ; for efficiency of internal algorithms here is the internal plot routine
; that takes the plotx coordinate in a separate variable instead of the XY register pair: ; that takes the plotx coordinate in a separate variable instead of the XY register pair:
uword internal_plotx ; 0..319 ; separate 'parameter' for internal_plot() uword @zp internal_plotx ; 0..319 ; separate 'parameter' for internal_plot()
asmsub internal_plot(ubyte ploty @A) clobbers (A, X, Y) { ; internal_plotx is 16 bits 0 to 319... doesn't fit in a register asmsub internal_plot(ubyte ploty @A) clobbers (A, X, Y) { ; internal_plotx is 16 bits 0 to 319... doesn't fit in a register
%asm {{ %asm {{

View File

@ -228,12 +228,12 @@ const ubyte totalNumberOfFaces = 22
const ubyte totalNumberOfPoints = 34 const ubyte totalNumberOfPoints = 34
str shipName = "cobra-mk3" str shipName = "cobra-mk3"
; vertices ; vertices
word[] xcoor = [ 32,-32,0,-120,120,-88,88,128,-128,0,-32,32,-36,-8,8,36,36,8,-8,-36,-1,-1,-80,-80,-88,80,88,80,1,1,1,1,-1,-1 ] word[totalNumberOfPoints] xcoor = [ 32,-32,0,-120,120,-88,88,128,-128,0,-32,32,-36,-8,8,36,36,8,-8,-36,-1,-1,-80,-80,-88,80,88,80,1,1,1,1,-1,-1 ]
word[] ycoor = [ 0,0,26,-3,-3,16,16,-8,-8,26,-24,-24,8,12,12,8,-12,-16,-16,-12,-1,-1,-6,6,0,6,0,-6,-1,-1,1,1,1,1 ] word[totalNumberOfPoints] ycoor = [ 0,0,26,-3,-3,16,16,-8,-8,26,-24,-24,8,12,12,8,-12,-16,-16,-12,-1,-1,-6,6,0,6,0,-6,-1,-1,1,1,1,1 ]
word[] zcoor = [ 76,76,24,-8,-8,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,76,90,-40,-40,-40,-40,-40,-40,76,90,76,90,76,90 ] word[totalNumberOfPoints] zcoor = [ 76,76,24,-8,-8,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,76,90,-40,-40,-40,-40,-40,-40,76,90,76,90,76,90 ]
; edges and faces ; edges and faces
ubyte[] edgesFrom = [ 0,1,0,10,1,0,2,0,4,0,4,7,2,1,1,3,8,3,2,5,6,5,6,16,15,14,14,18,13,12,12,26,25,25,22,23,22,20,28,21,20,28,29,30,31,30,32,20,21,20,20 ] ubyte[totalNumberOfEdges] edgesFrom = [ 0,1,0,10,1,0,2,0,4,0,4,7,2,1,1,3,8,3,2,5,6,5,6,16,15,14,14,18,13,12,12,26,25,25,22,23,22,20,28,21,20,28,29,30,31,30,32,20,21,20,20 ]
ubyte[] edgesTo = [ 1,2,2,11,10,11,6,6,6,4,7,11,5,5,3,5,10,8,9,9,9,8,7,17,16,15,17,19,18,13,19,27,26,27,23,24,24,28,29,29,21,30,31,31,33,32,33,32,33,33,29 ] ubyte[totalNumberOfEdges] edgesTo = [ 1,2,2,11,10,11,6,6,6,4,7,11,5,5,3,5,10,8,9,9,9,8,7,17,16,15,17,19,18,13,19,27,26,27,23,24,24,28,29,29,21,30,31,31,33,32,33,32,33,33,29 ]
ubyte[] facesPoints = [ ubyte[] facesPoints = [
0,1,2 ,255, 0,1,2 ,255,
11,10,1,0 ,255, 11,10,1,0 ,255,

View File

@ -1,5 +1,6 @@
package prog8.vm package prog8.vm
import prog8.code.core.AssemblyError
import kotlin.math.min import kotlin.math.min
/* /*
@ -268,7 +269,7 @@ object SysCalls {
val comparison = first.compareTo(second) val comparison = first.compareTo(second)
vm.registers.setSB(0, comparison.toByte()) vm.registers.setSB(0, comparison.toByte())
} }
else -> TODO("syscall ${call.name}") else -> throw AssemblyError("missing syscall ${call.name}")
} }
} }
} }

View File

@ -1283,7 +1283,7 @@ class VirtualMachine(val memory: Memory, program: List<Instruction>) {
private fun InsEXT(i: Instruction) { private fun InsEXT(i: Instruction) {
when(i.type!!){ when(i.type!!){
VmDataType.BYTE -> registers.setUW(i.reg1!!, registers.getUB(i.reg1).toUShort()) VmDataType.BYTE -> registers.setUW(i.reg1!!, registers.getUB(i.reg1).toUShort())
VmDataType.WORD -> TODO("ext.w not yet supported, requires 32 bits registers") VmDataType.WORD -> throw IllegalArgumentException("ext.w not yet supported, requires 32 bits registers")
VmDataType.FLOAT -> throw IllegalArgumentException("invalid float type for this instruction $i") VmDataType.FLOAT -> throw IllegalArgumentException("invalid float type for this instruction $i")
} }
pc++ pc++
@ -1292,7 +1292,7 @@ class VirtualMachine(val memory: Memory, program: List<Instruction>) {
private fun InsEXTS(i: Instruction) { private fun InsEXTS(i: Instruction) {
when(i.type!!){ when(i.type!!){
VmDataType.BYTE -> registers.setSW(i.reg1!!, registers.getSB(i.reg1).toShort()) VmDataType.BYTE -> registers.setSW(i.reg1!!, registers.getSB(i.reg1).toShort())
VmDataType.WORD -> TODO("exts.w not yet supported, requires 32 bits registers") VmDataType.WORD -> throw IllegalArgumentException("exts.w not yet supported, requires 32 bits registers")
VmDataType.FLOAT -> throw IllegalArgumentException("invalid float type for this instruction $i") VmDataType.FLOAT -> throw IllegalArgumentException("invalid float type for this instruction $i")
} }
pc++ pc++
@ -1761,7 +1761,7 @@ class VirtualMachine(val memory: Memory, program: List<Instruction>) {
val newValue = value.toInt() ushr 8 val newValue = value.toInt() ushr 8
registers.setUB(i.reg1!!, newValue.toUByte()) registers.setUB(i.reg1!!, newValue.toUByte())
} }
VmDataType.WORD -> TODO("msig.w not yet supported, requires 32-bits registers") VmDataType.WORD -> throw IllegalArgumentException("msig.w not yet supported, requires 32-bits registers")
VmDataType.FLOAT -> throw IllegalArgumentException("invalid float type for this instruction $i") VmDataType.FLOAT -> throw IllegalArgumentException("invalid float type for this instruction $i")
} }
pc++ pc++
@ -1774,7 +1774,7 @@ class VirtualMachine(val memory: Memory, program: List<Instruction>) {
val msb = registers.getUB(i.reg2!!) val msb = registers.getUB(i.reg2!!)
registers.setUW(i.reg1, ((msb.toInt() shl 8) or lsb.toInt()).toUShort()) registers.setUW(i.reg1, ((msb.toInt() shl 8) or lsb.toInt()).toUShort())
} }
VmDataType.WORD -> TODO("concat.w not yet supported, requires 32-bits registers") VmDataType.WORD -> throw IllegalArgumentException("concat.w not yet supported, requires 32-bits registers")
VmDataType.FLOAT -> throw IllegalArgumentException("invalid float type for this instruction $i") VmDataType.FLOAT -> throw IllegalArgumentException("invalid float type for this instruction $i")
} }
pc++ pc++