Skip to content

Commit e6d99c7

Browse files
committed
Added template to convert if statement
1 parent 6d93d0a commit e6d99c7

2 files changed

Lines changed: 88 additions & 3 deletions

File tree

src/main/kotlin/deep/decaf/low/amd64/IRToLow.kt

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ fun irExprToLow(expr: IRExpr): List<Instruction> {
88

99
fun traverse(expr: IRExpr): Location {
1010
return when (expr) {
11-
is IRIntLiteral -> ImmediateVal(expr.lit)
12-
is IRBoolLiteral -> ImmediateVal(if (expr.lit) 1 else 0)
11+
is IRIntLiteral -> {
12+
instructions.add(MoveInstruction(ImmediateVal(expr.lit), Register.r10()))
13+
Register.r10()
14+
}
15+
is IRBoolLiteral -> {
16+
instructions.add(MoveInstruction(ImmediateVal(if (expr.lit) 1 else 0), Register.r10()))
17+
Register.r10()
18+
}
1319
is IRMethodCallExpr -> {
1420
val argLocations = expr.argList.map { traverse(it) }
1521
argLocations.forEach { instructions.add(PushInstruction(it)) }
@@ -217,4 +223,69 @@ fun irStatementToLow(statement: IRStatement): List<Instruction> {
217223
}
218224

219225
return instructions
226+
}
227+
228+
fun irMethodToLow(method: IRMethodDeclaration): List<Block> {
229+
val blocks = mutableListOf<Block>()
230+
blocks.add(Block(method.name, mutableListOf()))
231+
232+
var falseBlockCount = 0
233+
var ifEndCount = 0
234+
235+
fun convert(ir: IR, block: Block): Block {
236+
return when (ir) {
237+
is IRStatement -> {
238+
when (ir) {
239+
is IRAssignStatement, is IRInvokeStatement -> {
240+
val instructions = irStatementToLow(ir)
241+
block.instructions.addAll(instructions)
242+
block
243+
}
244+
is IRBreakStatement -> TODO()
245+
is IRContinueStatement -> TODO()
246+
is IRIfStatement -> {
247+
val conditionInstructions = irExprToLow(ir.condition)
248+
block.instructions.addAll(conditionInstructions)
249+
val ansLoc = (conditionInstructions.last() as MoveInstruction).dest
250+
block.instructions.add(MoveInstruction(ansLoc, Register.r10()))
251+
block.instructions.add(CmpInstruction(Register.r10(), ImmediateVal(1)))
252+
253+
falseBlockCount++
254+
ifEndCount++
255+
val falseBlock = Block("lab_false_$falseBlockCount", mutableListOf())
256+
val ifEndBlock = Block("lab_if_end_$ifEndCount", mutableListOf())
257+
258+
block.instructions.add(JumpInstruction(AsmJumpOp.JNE, falseBlock.label!!))
259+
val newBlock = convert(ir.ifBlock, block)
260+
newBlock.instructions.add(JumpInstruction(AsmJumpOp.JMP, ifEndBlock.label!!))
261+
262+
blocks.add(falseBlock)
263+
if (ir.elseBlock != null) {
264+
convert(ir.elseBlock, falseBlock)
265+
}
266+
267+
blocks.add(ifEndBlock)
268+
ifEndBlock
269+
}
270+
is IRForStatement -> TODO()
271+
is IRReturnStatement -> TODO()
272+
is IRBlockStatement -> TODO()
273+
}
274+
}
275+
is IRBlock -> {
276+
var currBlock = block
277+
for (memberDeclaration in ir.fieldDeclarations) {
278+
currBlock.instructions.add(PushInstruction(ImmediateVal(0)))
279+
}
280+
for (statement in ir.statements) {
281+
currBlock = convert(statement, currBlock)
282+
}
283+
currBlock
284+
}
285+
else -> throw IllegalStateException("cannot call this method on this IR")
286+
}
287+
}
288+
289+
convert(method.block, blocks[0])
290+
return blocks
220291
}

src/main/kotlin/deep/decaf/low/amd64/Low.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,21 @@ data class EnterInstruction(val size: ImmediateVal) : Instruction() {
222222
override fun toString() = "enter \$($size*8), $0"
223223
}
224224

225-
data class Block(val label: String?, val instructions: MutableList<Instruction>)
225+
data class Block(val label: String?, val instructions: MutableList<Instruction>) {
226+
override fun toString(): String {
227+
val stringBuilder = StringBuilder()
228+
val pref: String = if (label != null) {
229+
stringBuilder.append("$label:").append('\n')
230+
"\t"
231+
} else {
232+
""
233+
}
234+
for (instruction in instructions) {
235+
stringBuilder.append(pref).append(instruction.toString()).append('\n')
236+
}
237+
return stringBuilder.toString()
238+
}
239+
}
226240

227241
data class Method(
228242
var stackSize: Int,

0 commit comments

Comments
 (0)