diff --git a/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt b/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt index bf60ba8d8..8d493d323 100644 --- a/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt +++ b/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt @@ -1047,11 +1047,43 @@ class IRCodeGen( val elseBranchOpcode: Opcode val elseBranchFirstReg: Int val elseBranchSecondReg: Int - + val branchDt: IRDataType if(irDtLeft==IRDataType.FLOAT) { - TODO("float non-zero compare via fcomp instruction") + branchDt = IRDataType.BYTE + val leftFpRegNum = registers.nextFreeFloat() + val rightFpRegNum = registers.nextFreeFloat() + val compResultReg = registers.nextFree() + result += expressionEval.translateExpression(ifElse.condition.left, -1, leftFpRegNum) + result += expressionEval.translateExpression(ifElse.condition.right, -1, rightFpRegNum) + addInstr(result, IRInstruction(Opcode.FCOMP, IRDataType.FLOAT, reg1=compResultReg, fpReg1 = leftFpRegNum, fpReg2 = rightFpRegNum), null) + val elseBranch = when (ifElse.condition.operator) { + "==" -> Opcode.BNZ + "!=" -> Opcode.BZ + "<" -> Opcode.BGEZS + ">" -> Opcode.BLEZS + "<=" -> Opcode.BGZS + ">=" -> Opcode.BLZS + else -> throw AssemblyError("weird operator") + } + if(ifElse.elseScope.children.isNotEmpty()) { + // if and else parts + val elseLabel = createLabelName() + val afterIfLabel = createLabelName() + addInstr(result, IRInstruction(elseBranch, IRDataType.BYTE, reg1=compResultReg, labelSymbol = elseLabel), null) + result += translateNode(ifElse.ifScope) + addInstr(result, IRInstruction(Opcode.JUMP, labelSymbol = afterIfLabel), null) + result += labelFirstChunk(translateNode(ifElse.elseScope), elseLabel) + result += IRCodeChunk(afterIfLabel, null) + } else { + // only if part + val afterIfLabel = createLabelName() + addInstr(result, IRInstruction(elseBranch, IRDataType.BYTE, reg1=compResultReg, labelSymbol = afterIfLabel), null) + result += translateNode(ifElse.ifScope) + result += IRCodeChunk(afterIfLabel, null) + } } else { // integer comparisons + branchDt = irDtLeft val leftRegNum = registers.nextFree() val rightRegNum = registers.nextFree() result += expressionEval.translateExpression(ifElse.condition.left, leftRegNum, -1) @@ -1097,7 +1129,7 @@ class IRCodeGen( // if and else parts val elseLabel = createLabelName() val afterIfLabel = createLabelName() - addInstr(result, IRInstruction(elseBranchOpcode, irDtLeft, + addInstr(result, IRInstruction(elseBranchOpcode, branchDt, reg1=elseBranchFirstReg, reg2=elseBranchSecondReg, labelSymbol = elseLabel), null) result += translateNode(ifElse.ifScope) @@ -1107,7 +1139,7 @@ class IRCodeGen( } else { // only if part val afterIfLabel = createLabelName() - addInstr(result, IRInstruction(elseBranchOpcode, irDtLeft, + addInstr(result, IRInstruction(elseBranchOpcode, branchDt, reg1=elseBranchFirstReg, reg2=elseBranchSecondReg, labelSymbol = afterIfLabel), null) result += translateNode(ifElse.ifScope) diff --git a/docs/source/todo.rst b/docs/source/todo.rst index fb0c59d30..aa84f7bdd 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,9 +3,7 @@ TODO For next minor release ^^^^^^^^^^^^^^^^^^^^^^ -- fix expericodegen compiler crashes: - float comparison <= , >= , == , != all crash with mismatched result register for FLOAT (cube3d-float uses this) - maze: thinks draw is unused +- fix expericodegen compiler errors: tehtriz: thinks sound.init routine is unused textelite: thinks trader.do_local routine is unused , and debug_seed - fix IR/VM: animals.p8 example somehow doesn't print the animal name in the first question, and exits after 1 animal instead of looping diff --git a/intermediate/src/prog8/intermediate/IRFileReader.kt b/intermediate/src/prog8/intermediate/IRFileReader.kt index 1a8a7a523..46bdbc0d2 100644 --- a/intermediate/src/prog8/intermediate/IRFileReader.kt +++ b/intermediate/src/prog8/intermediate/IRFileReader.kt @@ -8,6 +8,7 @@ import java.io.StringReader import java.nio.file.Path import javax.xml.stream.XMLEventReader import javax.xml.stream.XMLInputFactory +import javax.xml.stream.XMLStreamException import kotlin.io.path.Path import kotlin.io.path.inputStream @@ -19,6 +20,9 @@ class IRFileReader { val reader = XMLInputFactory.newInstance().createXMLEventReader(stream) try { return parseProgram(reader) + } catch(x: XMLStreamException) { + System.err.println("Error during parsing, this is not a well-formed P8IR source file") + throw x } finally { reader.close() }