From 00b717cde8ca8f7ceb7d0616abab8db9ada5fe02 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Thu, 4 Aug 2022 18:28:33 +0200 Subject: [PATCH] tweak --- codeAst/src/prog8/code/ast/AstExpressions.kt | 9 +++++++++ codeCore/src/prog8/code/core/IMachineDefinition.kt | 7 +------ .../prog8/code/target/atari/AtariMachineDefinition.kt | 2 +- .../prog8/code/target/c128/C128MachineDefinition.kt | 2 +- .../src/prog8/code/target/c64/C64MachineDefinition.kt | 2 +- codeCore/src/prog8/code/target/cbm/Mflpt5.kt | 7 +++---- .../prog8/code/target/cx16/CX16MachineDefinition.kt | 2 +- .../code/target/virtual/VirtualMachineDefinition.kt | 2 +- .../src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt | 6 +++--- .../src/prog8/codegen/virtual/BuiltinFuncGen.kt | 2 +- codeGenVirtual/src/prog8/codegen/virtual/CodeGen.kt | 2 +- compiler/res/prog8lib/c64/graphics.p8 | 2 +- examples/cx16/cobramk3-gfx.p8 | 10 +++++----- virtualmachine/src/prog8/vm/SysCalls.kt | 3 ++- virtualmachine/src/prog8/vm/VirtualMachine.kt | 8 ++++---- 15 files changed, 35 insertions(+), 31 deletions(-) diff --git a/codeAst/src/prog8/code/ast/AstExpressions.kt b/codeAst/src/prog8/code/ast/AstExpressions.kt index 24c8e5e80..8308c2d17 100644 --- a/codeAst/src/prog8/code/ast/AstExpressions.kt +++ b/codeAst/src/prog8/code/ast/AstExpressions.kt @@ -12,6 +12,15 @@ sealed class PtExpression(val type: DataType, position: Position) : PtNode(posit init { if(type==DataType.BOOL) 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() { diff --git a/codeCore/src/prog8/code/core/IMachineDefinition.kt b/codeCore/src/prog8/code/core/IMachineDefinition.kt index f58da62bd..d3b89fd56 100644 --- a/codeCore/src/prog8/code/core/IMachineDefinition.kt +++ b/codeCore/src/prog8/code/core/IMachineDefinition.kt @@ -3,11 +3,6 @@ package prog8.code.core import java.nio.file.Path -interface IMachineFloat { - fun toDouble(): Double - fun makeFloatFillAsm(): String -} - enum class CpuType { CPU6502, CPU65c02, @@ -27,7 +22,7 @@ interface IMachineDefinition { val cpu: CpuType fun initializeZeropage(compilerOptions: CompilationOptions) - fun getFloat(num: Number): IMachineFloat + fun getFloatAsmBytes(num: Number): String fun importLibs(compilerOptions: CompilationOptions, compilationTargetName: String): List fun launchEmulator(selectedEmulator: Int, programNameWithPath: Path) diff --git a/codeCore/src/prog8/code/target/atari/AtariMachineDefinition.kt b/codeCore/src/prog8/code/target/atari/AtariMachineDefinition.kt index 4998e0bd7..5ad3cca39 100644 --- a/codeCore/src/prog8/code/target/atari/AtariMachineDefinition.kt +++ b/codeCore/src/prog8/code/target/atari/AtariMachineDefinition.kt @@ -20,7 +20,7 @@ class AtariMachineDefinition: IMachineDefinition { 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 { return if (compilerOptions.output == OutputType.XEX) diff --git a/codeCore/src/prog8/code/target/c128/C128MachineDefinition.kt b/codeCore/src/prog8/code/target/c128/C128MachineDefinition.kt index 28a8e6a57..63567260a 100644 --- a/codeCore/src/prog8/code/target/c128/C128MachineDefinition.kt +++ b/codeCore/src/prog8/code/target/c128/C128MachineDefinition.kt @@ -21,7 +21,7 @@ class C128MachineDefinition: IMachineDefinition { 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 { return if (compilerOptions.launcher == CbmPrgLauncherType.BASIC || compilerOptions.output == OutputType.PRG) diff --git a/codeCore/src/prog8/code/target/c64/C64MachineDefinition.kt b/codeCore/src/prog8/code/target/c64/C64MachineDefinition.kt index 28d078ae3..e3141b531 100644 --- a/codeCore/src/prog8/code/target/c64/C64MachineDefinition.kt +++ b/codeCore/src/prog8/code/target/c64/C64MachineDefinition.kt @@ -21,7 +21,7 @@ class C64MachineDefinition: IMachineDefinition { 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 { return if (compilerOptions.launcher == CbmPrgLauncherType.BASIC || compilerOptions.output == OutputType.PRG) diff --git a/codeCore/src/prog8/code/target/cbm/Mflpt5.kt b/codeCore/src/prog8/code/target/cbm/Mflpt5.kt index 8c12b50ca..d636620ea 100644 --- a/codeCore/src/prog8/code/target/cbm/Mflpt5.kt +++ b/codeCore/src/prog8/code/target/cbm/Mflpt5.kt @@ -1,12 +1,11 @@ package prog8.code.target.cbm -import prog8.code.core.IMachineFloat import prog8.code.core.InternalCompilerException import kotlin.math.absoluteValue 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 { 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 val exp = b0.toInt() - 128 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 } - override fun makeFloatFillAsm(): String { + 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') diff --git a/codeCore/src/prog8/code/target/cx16/CX16MachineDefinition.kt b/codeCore/src/prog8/code/target/cx16/CX16MachineDefinition.kt index 08da1afce..c57ee1162 100644 --- a/codeCore/src/prog8/code/target/cx16/CX16MachineDefinition.kt +++ b/codeCore/src/prog8/code/target/cx16/CX16MachineDefinition.kt @@ -20,7 +20,7 @@ class CX16MachineDefinition: IMachineDefinition { 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 { return if (compilerOptions.launcher == CbmPrgLauncherType.BASIC || compilerOptions.output == OutputType.PRG) listOf("syslib") diff --git a/codeCore/src/prog8/code/target/virtual/VirtualMachineDefinition.kt b/codeCore/src/prog8/code/target/virtual/VirtualMachineDefinition.kt index 0359c4bc8..e16ffc4e1 100644 --- a/codeCore/src/prog8/code/target/virtual/VirtualMachineDefinition.kt +++ b/codeCore/src/prog8/code/target/virtual/VirtualMachineDefinition.kt @@ -22,7 +22,7 @@ class VirtualMachineDefinition: IMachineDefinition { 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 { return listOf("syslib") diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt index 8bf4fe579..7da356855 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt @@ -48,7 +48,7 @@ internal class ProgramAndVarsGen( // the global list of all floating point constants for the whole program asmgen.out("; global float constants") for (flt in allocator.globalFloatConsts) { - val floatFill = compTarget.machine.getFloat(flt.key).makeFloatFillAsm() + val floatFill = compTarget.machine.getFloatAsmBytes(flt.key) val floatvalue = flt.key asmgen.out("${flt.value}\t.byte $floatFill ; float $floatvalue") } @@ -497,7 +497,7 @@ internal class ProgramAndVarsGen( if(initialValue==0) { asmgen.out("$name\t.byte 0,0,0,0,0 ; float") } else { - val floatFill = compTarget.machine.getFloat(initialValue).makeFloatFillAsm() + val floatFill = compTarget.machine.getFloatAsmBytes(initialValue) asmgen.out("$name\t.byte $floatFill ; float $initialValue") } } @@ -556,7 +556,7 @@ internal class ProgramAndVarsGen( DataType.ARRAY_F -> { val array = value ?: zeroFilledArray(orNumberOfZeros!!) val floatFills = array.map { - compTarget.machine.getFloat(it.number!!).makeFloatFillAsm() + compTarget.machine.getFloatAsmBytes(it.number!!) } asmgen.out(varname) for (f in array.zip(floatFills)) diff --git a/codeGenVirtual/src/prog8/codegen/virtual/BuiltinFuncGen.kt b/codeGenVirtual/src/prog8/codegen/virtual/BuiltinFuncGen.kt index 6b0d846bd..e88353f6c 100644 --- a/codeGenVirtual/src/prog8/codegen/virtual/BuiltinFuncGen.kt +++ b/codeGenVirtual/src/prog8/codegen/virtual/BuiltinFuncGen.kt @@ -45,7 +45,7 @@ internal class BuiltinFuncGen(private val codeGen: CodeGen, private val exprGen: "ror" -> funcRolRor(Opcode.ROXR, call, resultRegister) "rol2" -> funcRolRor(Opcode.ROL, call, resultRegister) "ror2" -> funcRolRor(Opcode.ROR, call, resultRegister) - else -> TODO("builtinfunc ${call.name}") + else -> throw AssemblyError("missing builtinfunc for ${call.name}") } } diff --git a/codeGenVirtual/src/prog8/codegen/virtual/CodeGen.kt b/codeGenVirtual/src/prog8/codegen/virtual/CodeGen.kt index 52bc42c68..1d0d33604 100644 --- a/codeGenVirtual/src/prog8/codegen/virtual/CodeGen.kt +++ b/codeGenVirtual/src/prog8/codegen/virtual/CodeGen.kt @@ -279,7 +279,7 @@ class CodeGen(internal val program: PtProgram, code += addConstMem(loopvarDt, loopvarAddress.toUInt(), step) code += VmCodeInstruction(Opcode.LOADM, loopvarDt, reg1 = indexReg, value = loopvarAddress) } else { - // TODO WHY THID DISTINCTION? + // TODO WHY THIS DISTINCTION? code += VmCodeInstruction(Opcode.LOADM, loopvarDt, reg1 = indexReg, value = loopvarAddress) code += addConstReg(loopvarDt, indexReg, step) code += VmCodeInstruction(Opcode.STOREM, loopvarDt, reg1 = indexReg, value = loopvarAddress) diff --git a/compiler/res/prog8lib/c64/graphics.p8 b/compiler/res/prog8lib/c64/graphics.p8 index 58ddc5fd1..0780b3124 100644 --- a/compiler/res/prog8lib/c64/graphics.p8 +++ b/compiler/res/prog8lib/c64/graphics.p8 @@ -315,7 +315,7 @@ hline_zero2 ; 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: - 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 %asm {{ diff --git a/examples/cx16/cobramk3-gfx.p8 b/examples/cx16/cobramk3-gfx.p8 index 55440d628..77322dd2e 100644 --- a/examples/cx16/cobramk3-gfx.p8 +++ b/examples/cx16/cobramk3-gfx.p8 @@ -228,12 +228,12 @@ const ubyte totalNumberOfFaces = 22 const ubyte totalNumberOfPoints = 34 str shipName = "cobra-mk3" ; 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[] 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] 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] 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] 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 -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[] 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] 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] 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 = [ 0,1,2 ,255, 11,10,1,0 ,255, diff --git a/virtualmachine/src/prog8/vm/SysCalls.kt b/virtualmachine/src/prog8/vm/SysCalls.kt index 39d1db9b9..c0cf2b567 100644 --- a/virtualmachine/src/prog8/vm/SysCalls.kt +++ b/virtualmachine/src/prog8/vm/SysCalls.kt @@ -1,5 +1,6 @@ package prog8.vm +import prog8.code.core.AssemblyError import kotlin.math.min /* @@ -268,7 +269,7 @@ object SysCalls { val comparison = first.compareTo(second) vm.registers.setSB(0, comparison.toByte()) } - else -> TODO("syscall ${call.name}") + else -> throw AssemblyError("missing syscall ${call.name}") } } } diff --git a/virtualmachine/src/prog8/vm/VirtualMachine.kt b/virtualmachine/src/prog8/vm/VirtualMachine.kt index 00a6bc2e4..7f6d64044 100644 --- a/virtualmachine/src/prog8/vm/VirtualMachine.kt +++ b/virtualmachine/src/prog8/vm/VirtualMachine.kt @@ -1283,7 +1283,7 @@ class VirtualMachine(val memory: Memory, program: List) { private fun InsEXT(i: Instruction) { when(i.type!!){ 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") } pc++ @@ -1292,7 +1292,7 @@ class VirtualMachine(val memory: Memory, program: List) { private fun InsEXTS(i: Instruction) { when(i.type!!){ 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") } pc++ @@ -1761,7 +1761,7 @@ class VirtualMachine(val memory: Memory, program: List) { val newValue = value.toInt() ushr 8 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") } pc++ @@ -1774,7 +1774,7 @@ class VirtualMachine(val memory: Memory, program: List) { val msb = registers.getUB(i.reg2!!) 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") } pc++