mirror of
https://github.com/irmen/prog8.git
synced 2025-01-11 13:29:45 +00:00
fix IR translateIfElseNonZeroComparison for ints + floats
This commit is contained in:
parent
7e69690605
commit
589948c7f4
@ -1047,11 +1047,43 @@ class IRCodeGen(
|
|||||||
val elseBranchOpcode: Opcode
|
val elseBranchOpcode: Opcode
|
||||||
val elseBranchFirstReg: Int
|
val elseBranchFirstReg: Int
|
||||||
val elseBranchSecondReg: Int
|
val elseBranchSecondReg: Int
|
||||||
|
val branchDt: IRDataType
|
||||||
if(irDtLeft==IRDataType.FLOAT) {
|
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 {
|
} else {
|
||||||
// integer comparisons
|
// integer comparisons
|
||||||
|
branchDt = irDtLeft
|
||||||
val leftRegNum = registers.nextFree()
|
val leftRegNum = registers.nextFree()
|
||||||
val rightRegNum = registers.nextFree()
|
val rightRegNum = registers.nextFree()
|
||||||
result += expressionEval.translateExpression(ifElse.condition.left, leftRegNum, -1)
|
result += expressionEval.translateExpression(ifElse.condition.left, leftRegNum, -1)
|
||||||
@ -1097,7 +1129,7 @@ class IRCodeGen(
|
|||||||
// if and else parts
|
// if and else parts
|
||||||
val elseLabel = createLabelName()
|
val elseLabel = createLabelName()
|
||||||
val afterIfLabel = createLabelName()
|
val afterIfLabel = createLabelName()
|
||||||
addInstr(result, IRInstruction(elseBranchOpcode, irDtLeft,
|
addInstr(result, IRInstruction(elseBranchOpcode, branchDt,
|
||||||
reg1=elseBranchFirstReg, reg2=elseBranchSecondReg,
|
reg1=elseBranchFirstReg, reg2=elseBranchSecondReg,
|
||||||
labelSymbol = elseLabel), null)
|
labelSymbol = elseLabel), null)
|
||||||
result += translateNode(ifElse.ifScope)
|
result += translateNode(ifElse.ifScope)
|
||||||
@ -1107,7 +1139,7 @@ class IRCodeGen(
|
|||||||
} else {
|
} else {
|
||||||
// only if part
|
// only if part
|
||||||
val afterIfLabel = createLabelName()
|
val afterIfLabel = createLabelName()
|
||||||
addInstr(result, IRInstruction(elseBranchOpcode, irDtLeft,
|
addInstr(result, IRInstruction(elseBranchOpcode, branchDt,
|
||||||
reg1=elseBranchFirstReg, reg2=elseBranchSecondReg,
|
reg1=elseBranchFirstReg, reg2=elseBranchSecondReg,
|
||||||
labelSymbol = afterIfLabel), null)
|
labelSymbol = afterIfLabel), null)
|
||||||
result += translateNode(ifElse.ifScope)
|
result += translateNode(ifElse.ifScope)
|
||||||
|
@ -3,9 +3,7 @@ TODO
|
|||||||
|
|
||||||
For next minor release
|
For next minor release
|
||||||
^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^^
|
||||||
- fix expericodegen compiler crashes:
|
- fix expericodegen compiler errors:
|
||||||
float comparison <= , >= , == , != all crash with mismatched result register for FLOAT (cube3d-float uses this)
|
|
||||||
maze: thinks draw is unused
|
|
||||||
tehtriz: thinks sound.init routine is unused
|
tehtriz: thinks sound.init routine is unused
|
||||||
textelite: thinks trader.do_local routine is unused , and debug_seed
|
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
|
- 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
|
||||||
|
@ -8,6 +8,7 @@ import java.io.StringReader
|
|||||||
import java.nio.file.Path
|
import java.nio.file.Path
|
||||||
import javax.xml.stream.XMLEventReader
|
import javax.xml.stream.XMLEventReader
|
||||||
import javax.xml.stream.XMLInputFactory
|
import javax.xml.stream.XMLInputFactory
|
||||||
|
import javax.xml.stream.XMLStreamException
|
||||||
import kotlin.io.path.Path
|
import kotlin.io.path.Path
|
||||||
import kotlin.io.path.inputStream
|
import kotlin.io.path.inputStream
|
||||||
|
|
||||||
@ -19,6 +20,9 @@ class IRFileReader {
|
|||||||
val reader = XMLInputFactory.newInstance().createXMLEventReader(stream)
|
val reader = XMLInputFactory.newInstance().createXMLEventReader(stream)
|
||||||
try {
|
try {
|
||||||
return parseProgram(reader)
|
return parseProgram(reader)
|
||||||
|
} catch(x: XMLStreamException) {
|
||||||
|
System.err.println("Error during parsing, this is not a well-formed P8IR source file")
|
||||||
|
throw x
|
||||||
} finally {
|
} finally {
|
||||||
reader.close()
|
reader.close()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user