fix IR translateIfElseNonZeroComparison for ints + floats

This commit is contained in:
Irmen de Jong 2023-03-07 22:44:23 +01:00
parent 7e69690605
commit 589948c7f4
3 changed files with 41 additions and 7 deletions

View File

@ -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)

View File

@ -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

View File

@ -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()
}