From 0e3d75cfeb2cd72bb865ac3b97e75dc9ee9704fb Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 14 May 2023 20:44:32 +0200 Subject: [PATCH] move irType() to intermediate module --- .../codegen/intermediate/AssignmentGen.kt | 8 +++--- .../codegen/intermediate/BuiltinFuncGen.kt | 6 ++--- .../codegen/intermediate/ExpressionGen.kt | 26 +++++++++---------- .../prog8/codegen/intermediate/IRCodeGen.kt | 14 ---------- docs/source/todo.rst | 2 -- intermediate/src/prog8/intermediate/Utils.kt | 14 ++++++++++ 6 files changed, 34 insertions(+), 36 deletions(-) diff --git a/codeGenIntermediate/src/prog8/codegen/intermediate/AssignmentGen.kt b/codeGenIntermediate/src/prog8/codegen/intermediate/AssignmentGen.kt index 4b1be02c4..29ad7f341 100644 --- a/codeGenIntermediate/src/prog8/codegen/intermediate/AssignmentGen.kt +++ b/codeGenIntermediate/src/prog8/codegen/intermediate/AssignmentGen.kt @@ -46,7 +46,7 @@ internal class AssignmentGen(private val codeGen: IRCodeGen, private val express assignment: PtAugmentedAssign ): IRCodeChunks { val value = assignment.value - val vmDt = codeGen.irType(value.type) + val vmDt = irType(value.type) return when(assignment.operator) { "+" -> expressionEval.operatorPlusInplace(address, null, vmDt, value) "-" -> expressionEval.operatorMinusInplace(address, null, vmDt, value) @@ -72,7 +72,7 @@ internal class AssignmentGen(private val codeGen: IRCodeGen, private val express private fun assignVarAugmented(symbol: String, assignment: PtAugmentedAssign): IRCodeChunks { val value = assignment.value - val targetDt = codeGen.irType(assignment.target.type) + val targetDt = irType(assignment.target.type) return when (assignment.operator) { "+=" -> expressionEval.operatorPlusInplace(null, symbol, targetDt, value) "-=" -> expressionEval.operatorMinusInplace(null, symbol, targetDt, value) @@ -161,8 +161,8 @@ internal class AssignmentGen(private val codeGen: IRCodeGen, private val express val targetIdent = assignment.target.identifier val targetMemory = assignment.target.memory val targetArray = assignment.target.array - val valueDt = codeGen.irType(assignment.value.type) - val targetDt = codeGen.irType(assignment.target.type) + val valueDt = irType(assignment.value.type) + val targetDt = irType(assignment.target.type) val result = mutableListOf() var valueRegister = -1 diff --git a/codeGenIntermediate/src/prog8/codegen/intermediate/BuiltinFuncGen.kt b/codeGenIntermediate/src/prog8/codegen/intermediate/BuiltinFuncGen.kt index e4c81a5a5..4fede9675 100644 --- a/codeGenIntermediate/src/prog8/codegen/intermediate/BuiltinFuncGen.kt +++ b/codeGenIntermediate/src/prog8/codegen/intermediate/BuiltinFuncGen.kt @@ -93,7 +93,7 @@ internal class BuiltinFuncGen(private val codeGen: IRCodeGen, private val exprGe addToResult(result, leftTr, leftTr.resultReg, -1) val rightTr = exprGen.translateExpression(call.args[1]) addToResult(result, rightTr, rightTr.resultReg, -1) - val dt = codeGen.irType(call.args[0].type) + val dt = irType(call.args[0].type) result += IRCodeChunk(null, null).also { it += IRInstruction(Opcode.CMP, dt, reg1=leftTr.resultReg, reg2=rightTr.resultReg) } @@ -188,7 +188,7 @@ internal class BuiltinFuncGen(private val codeGen: IRCodeGen, private val exprGe private fun funcSgn(call: PtBuiltinFunctionCall): ExpressionCodeResult { val result = mutableListOf() - val vmDt = codeGen.irType(call.type) + val vmDt = irType(call.type) val tr = exprGen.translateExpression(call.args.single()) addToResult(result, tr, tr.resultReg, -1) val resultReg = codeGen.registers.nextFree() @@ -438,7 +438,7 @@ internal class BuiltinFuncGen(private val codeGen: IRCodeGen, private val exprGe } private fun funcRolRor(opcode: Opcode, call: PtBuiltinFunctionCall): ExpressionCodeResult { - val vmDt = codeGen.irType(call.args[0].type) + val vmDt = irType(call.args[0].type) val result = mutableListOf() val tr = exprGen.translateExpression(call.args[0]) addToResult(result, tr, tr.resultReg, -1) diff --git a/codeGenIntermediate/src/prog8/codegen/intermediate/ExpressionGen.kt b/codeGenIntermediate/src/prog8/codegen/intermediate/ExpressionGen.kt index bc77efa9d..d270f1786 100644 --- a/codeGenIntermediate/src/prog8/codegen/intermediate/ExpressionGen.kt +++ b/codeGenIntermediate/src/prog8/codegen/intermediate/ExpressionGen.kt @@ -28,10 +28,10 @@ internal class ExpressionGen(private val codeGen: IRCodeGen) { fun translateExpression(expr: PtExpression): ExpressionCodeResult { return when (expr) { is PtMachineRegister -> { - ExpressionCodeResult(emptyList(), codeGen.irType(expr.type), expr.register, -1) + ExpressionCodeResult(emptyList(), irType(expr.type), expr.register, -1) } is PtNumber -> { - val vmDt = codeGen.irType(expr.type) + val vmDt = irType(expr.type) val code = IRCodeChunk(null, null) if(vmDt==IRDataType.FLOAT) { val resultFpRegister = codeGen.registers.nextFreeFloat() @@ -45,7 +45,7 @@ internal class ExpressionGen(private val codeGen: IRCodeGen) { } } is PtIdentifier -> { - val vmDt = codeGen.irType(expr.type) + val vmDt = irType(expr.type) val code = IRCodeChunk(null, null) if (expr.type in PassByValueDatatypes) { if(vmDt==IRDataType.FLOAT) { @@ -66,7 +66,7 @@ internal class ExpressionGen(private val codeGen: IRCodeGen) { } } is PtAddressOf -> { - val vmDt = codeGen.irType(expr.type) + val vmDt = irType(expr.type) val symbol = expr.identifier.name // note: LOAD gets you the address of the symbol, whereas LOADM would get you the value stored at that location val code = IRCodeChunk(null, null) @@ -142,7 +142,7 @@ internal class ExpressionGen(private val codeGen: IRCodeGen) { private fun translate(arrayIx: PtArrayIndexer): ExpressionCodeResult { val eltSize = codeGen.program.memsizer.memorySize(arrayIx.type) - val vmDt = codeGen.irType(arrayIx.type) + val vmDt = irType(arrayIx.type) val result = mutableListOf() val arrayVarSymbol = arrayIx.variable.name @@ -192,7 +192,7 @@ internal class ExpressionGen(private val codeGen: IRCodeGen) { val result = mutableListOf() val tr = translateExpression(expr.value) addToResult(result, tr, tr.resultReg, tr.resultFpReg) - val vmDt = codeGen.irType(expr.type) + val vmDt = irType(expr.type) when(expr.operator) { "+" -> { } "-" -> { @@ -308,12 +308,12 @@ internal class ExpressionGen(private val codeGen: IRCodeGen) { else -> throw AssemblyError("weird cast type") } - return ExpressionCodeResult(result, codeGen.irType(cast.type), actualResultReg2, actualResultFpReg2) + return ExpressionCodeResult(result, irType(cast.type), actualResultReg2, actualResultFpReg2) } private fun translate(binExpr: PtBinaryExpression): ExpressionCodeResult { require(!codeGen.options.useNewExprCode) - val vmDt = codeGen.irType(binExpr.left.type) + val vmDt = irType(binExpr.left.type) val signed = binExpr.left.type in SignedDatatypes return when(binExpr.operator) { "+" -> operatorPlus(binExpr, vmDt) @@ -343,7 +343,7 @@ internal class ExpressionGen(private val codeGen: IRCodeGen) { // assign the arguments val argRegisters = mutableListOf() for ((arg, parameter) in fcall.args.zip(callTarget.parameters)) { - val paramDt = codeGen.irType(parameter.type) + val paramDt = irType(parameter.type) val tr = translateExpression(arg) if(paramDt==IRDataType.FLOAT) argRegisters.add(FunctionCallArgs.ArgumentSpec(parameter.name, null, FunctionCallArgs.RegSpec(IRDataType.FLOAT, tr.resultFpReg, null))) @@ -353,7 +353,7 @@ internal class ExpressionGen(private val codeGen: IRCodeGen) { } // return value val returnRegSpec = if(fcall.void) null else { - val returnIrType = codeGen.irType(callTarget.returnType!!) + val returnIrType = irType(callTarget.returnType!!) if(returnIrType==IRDataType.FLOAT) FunctionCallArgs.RegSpec(returnIrType, codeGen.registers.nextFreeFloat(), null) else @@ -374,7 +374,7 @@ internal class ExpressionGen(private val codeGen: IRCodeGen) { // assign the arguments val argRegisters = mutableListOf() for ((arg, parameter) in fcall.args.zip(callTarget.parameters)) { - val paramDt = codeGen.irType(parameter.type) + val paramDt = irType(parameter.type) val tr = translateExpression(arg) if(paramDt==IRDataType.FLOAT) argRegisters.add(FunctionCallArgs.ArgumentSpec("", null, FunctionCallArgs.RegSpec(IRDataType.FLOAT, tr.resultFpReg, parameter.register))) @@ -388,7 +388,7 @@ internal class ExpressionGen(private val codeGen: IRCodeGen) { null else if(callTarget.returns.size==1) { val returns = callTarget.returns[0] - val returnIrType = codeGen.irType(returns.type) + val returnIrType = irType(returns.type) if(returnIrType==IRDataType.FLOAT) FunctionCallArgs.RegSpec(returnIrType, codeGen.registers.nextFreeFloat(), returns.register) else @@ -396,7 +396,7 @@ internal class ExpressionGen(private val codeGen: IRCodeGen) { } else { // multiple return values: take the first *register* (not status flag) return value and ignore the rest. val returns = callTarget.returns.first { it.register.registerOrPair!=null } - val returnIrType = codeGen.irType(returns.type) + val returnIrType = irType(returns.type) if(returnIrType==IRDataType.FLOAT) FunctionCallArgs.RegSpec(returnIrType, codeGen.registers.nextFreeFloat(), returns.register) else diff --git a/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt b/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt index 0f2cabcb7..d3f8a816b 100644 --- a/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt +++ b/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt @@ -1545,20 +1545,6 @@ class IRCodeGen( } } - - internal fun irType(type: DataType): IRDataType { - return when(type) { - DataType.BOOL, - DataType.UBYTE, - DataType.BYTE -> IRDataType.BYTE - DataType.UWORD, - DataType.WORD -> IRDataType.WORD - DataType.FLOAT -> IRDataType.FLOAT - in PassByReferenceDatatypes -> IRDataType.WORD - else -> throw AssemblyError("no IR datatype for $type") - } - } - private var labelSequenceNumber = 0 internal fun createLabelName(): String { labelSequenceNumber++ diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 4ffefd935..2a54c6c2e 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,8 +3,6 @@ TODO For next minor release ^^^^^^^^^^^^^^^^^^^^^^ -- fix romsub encoding into IR - ... diff --git a/intermediate/src/prog8/intermediate/Utils.kt b/intermediate/src/prog8/intermediate/Utils.kt index 5f101fc39..4355b9a6c 100644 --- a/intermediate/src/prog8/intermediate/Utils.kt +++ b/intermediate/src/prog8/intermediate/Utils.kt @@ -1,8 +1,10 @@ package prog8.intermediate import prog8.code.* +import prog8.code.core.AssemblyError import prog8.code.core.DataType import prog8.code.core.InternalCompilerException +import prog8.code.core.PassByReferenceDatatypes fun getTypeString(dt : DataType): String = when(dt) { @@ -262,3 +264,15 @@ private fun parseCall(rest: String): ParsedCall { } +fun irType(type: DataType): IRDataType { + return when(type) { + DataType.BOOL, + DataType.UBYTE, + DataType.BYTE -> IRDataType.BYTE + DataType.UWORD, + DataType.WORD -> IRDataType.WORD + DataType.FLOAT -> IRDataType.FLOAT + in PassByReferenceDatatypes -> IRDataType.WORD + else -> throw AssemblyError("no IR datatype for $type") + } +}