Skip to content

Commit e99b029

Browse files
committed
Added template for For statement
1 parent e5eb703 commit e99b029

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ fun irMethodToLow(method: IRMethodDeclaration): List<Block> {
231231

232232
var falseBlockCount = 0
233233
var ifEndCount = 0
234+
var loopStartCount = 0
235+
var loopEndCount = 0
234236

235237
fun convert(ir: IR, block: Block): Block {
236238
return when (ir) {
@@ -252,8 +254,8 @@ fun irMethodToLow(method: IRMethodDeclaration): List<Block> {
252254

253255
falseBlockCount++
254256
ifEndCount++
255-
val falseBlock = Block("lab_false_$falseBlockCount", mutableListOf())
256-
val ifEndBlock = Block("lab_if_end_$ifEndCount", mutableListOf())
257+
val falseBlock = Block("False$falseBlockCount", mutableListOf())
258+
val ifEndBlock = Block("IFend$ifEndCount", mutableListOf())
257259

258260
block.instructions.add(JumpInstruction(AsmJumpOp.JNE, falseBlock.label!!))
259261
val newBlock = convert(ir.ifBlock, block)
@@ -267,7 +269,31 @@ fun irMethodToLow(method: IRMethodDeclaration): List<Block> {
267269
blocks.add(ifEndBlock)
268270
ifEndBlock
269271
}
270-
is IRForStatement -> TODO()
272+
is IRForStatement -> {
273+
block.instructions.add(PushInstruction(ImmediateVal(0)))
274+
val initInstructions = irExprToLow(ir.initExpr)
275+
block.instructions.addAll(initInstructions)
276+
val initLoc = (initInstructions.last() as MoveInstruction).dest
277+
block.instructions.add(MoveInstruction(initLoc, Register.r10()))
278+
block.instructions.add(MoveInstruction(Register.r10(), Label(ir.loopVar)))
279+
280+
loopStartCount++
281+
val loopBlock = Block("LoopStart$loopStartCount", mutableListOf())
282+
blocks.add(loopBlock)
283+
val conditionInstructions = irExprToLow(ir.condition)
284+
loopBlock.instructions.addAll(conditionInstructions)
285+
val ansLoc = (conditionInstructions.last() as MoveInstruction).dest
286+
loopBlock.instructions.add(MoveInstruction(ansLoc, Register.r10()))
287+
loopBlock.instructions.add(CmpInstruction(Register.r10(), ImmediateVal(1)))
288+
289+
loopEndCount++
290+
val loopEndBlock = Block("LoopEnd$loopEndCount", mutableListOf())
291+
loopBlock.instructions.add(JumpInstruction(AsmJumpOp.JNE, loopEndBlock.label!!))
292+
val newBlock = convert(ir.body, loopBlock)
293+
newBlock.instructions.add(JumpInstruction(AsmJumpOp.JMP, loopBlock.label!!))
294+
blocks.add(loopEndBlock)
295+
loopEndBlock
296+
}
271297
is IRReturnStatement -> TODO()
272298
is IRBlockStatement -> TODO()
273299
}
@@ -286,6 +312,10 @@ fun irMethodToLow(method: IRMethodDeclaration): List<Block> {
286312
}
287313
}
288314

315+
blocks[0].instructions.add(EnterInstruction(0))
289316
convert(method.block, blocks[0])
317+
blocks.last().instructions.add(LeaveInstruction)
318+
blocks.last().instructions.add(ReturnInstruction)
319+
290320
return blocks
291321
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ object LeaveInstruction : Instruction() {
218218
override fun toString() = "leave"
219219
}
220220

221-
data class EnterInstruction(val size: ImmediateVal) : Instruction() {
221+
data class EnterInstruction(val size: Int) : Instruction() {
222222
override fun toString() = "enter \$($size*8), $0"
223223
}
224224

0 commit comments

Comments
 (0)