Skip to content

Commit 1f64348

Browse files
committed
Added instructions for break and continue
1 parent 2e46d24 commit 1f64348

1 file changed

Lines changed: 47 additions & 17 deletions

File tree

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

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,21 @@ fun irExprToLow(expr: IRExpr): List<Instruction> {
147147
}
148148
is IRLocationExpression -> {
149149
when (val location = expr.location) {
150-
is IRIDLocation -> Label(location.name)
150+
is IRIDLocation -> {
151+
val loc = Label(location.name)
152+
instructions.add(MoveInstruction(loc, Register.r10()))
153+
val tmp = Label(getUUID())
154+
instructions.add(MoveInstruction(Register.r10(), tmp))
155+
tmp
156+
}
151157
is IRArrayLocation -> {
152158
val index = traverse(location.indexExpr)
153159
instructions.add(MoveInstruction(index, Register.r10()))
154-
ArrayAsm(location.name, Register.r10())
160+
val loc = ArrayAsm(location.name, Register.r10())
161+
instructions.add(MoveInstruction(loc, Register.r10()))
162+
val tmp = Label(getUUID())
163+
instructions.add(MoveInstruction(Register.r10(), tmp))
164+
tmp
155165
}
156166
}
157167
}
@@ -233,10 +243,11 @@ fun irMethodToLow(method: IRMethodDeclaration): List<Block> {
233243
val blocks = mutableListOf<Block>()
234244
blocks.add(Block(method.name, mutableListOf()))
235245

236-
var falseBlockCount = 0
237-
var ifEndCount = 0
238-
var loopStartCount = 0
239-
var loopEndCount = 0
246+
var loopCount = 0
247+
var ifCount = 0
248+
249+
var loopStartLabel: String? = null
250+
var loopEndLabel: String? = null
240251

241252
fun convert(ir: IR, block: Block): Block {
242253
return when (ir) {
@@ -247,19 +258,24 @@ fun irMethodToLow(method: IRMethodDeclaration): List<Block> {
247258
block.instructions.addAll(instructions)
248259
block
249260
}
250-
is IRBreakStatement -> TODO()
251-
is IRContinueStatement -> TODO()
261+
is IRBreakStatement -> {
262+
block.instructions.add(JumpInstruction(AsmJumpOp.JMP, loopEndLabel!!))
263+
block
264+
}
265+
is IRContinueStatement -> {
266+
block.instructions.add(JumpInstruction(AsmJumpOp.JMP, loopStartLabel!!))
267+
block
268+
}
252269
is IRIfStatement -> {
253270
val conditionInstructions = irExprToLow(ir.condition)
254271
block.instructions.addAll(conditionInstructions)
255272
val ansLoc = (conditionInstructions.last() as MoveInstruction).dest
256273
block.instructions.add(MoveInstruction(ansLoc, Register.r10()))
257274
block.instructions.add(CmpInstruction(Register.r10(), ImmediateVal(1)))
258275

259-
falseBlockCount++
260-
ifEndCount++
261-
val falseBlock = Block("False$falseBlockCount", mutableListOf())
262-
val ifEndBlock = Block("IFend$ifEndCount", mutableListOf())
276+
ifCount++
277+
val falseBlock = Block("False$ifCount", mutableListOf())
278+
val ifEndBlock = Block("IFend$ifCount", mutableListOf())
263279

264280
block.instructions.add(JumpInstruction(AsmJumpOp.JNE, falseBlock.label!!))
265281
val newBlock = convert(ir.ifBlock, block)
@@ -281,17 +297,31 @@ fun irMethodToLow(method: IRMethodDeclaration): List<Block> {
281297
block.instructions.add(MoveInstruction(initLoc, Register.r10()))
282298
block.instructions.add(MoveInstruction(Register.r10(), Label(ir.loopVar)))
283299

284-
loopStartCount++
285-
val loopBlock = Block("LoopStart$loopStartCount", mutableListOf())
300+
loopCount++
301+
val loopBlock = Block("LoopStart$loopCount", mutableListOf())
302+
loopStartLabel = loopBlock.label
286303
blocks.add(loopBlock)
287-
val conditionInstructions = irExprToLow(ir.condition)
304+
val conditionInstructions = irExprToLow(
305+
IRBinOpExpr(
306+
IRLocationExpression(
307+
IRIDLocation(
308+
ir.loopVar,
309+
Position.unknown()
310+
),
311+
Position.unknown()
312+
),
313+
ir.condition,
314+
BinOp.NOT_EQ,
315+
Position.unknown()
316+
)
317+
)
288318
loopBlock.instructions.addAll(conditionInstructions)
289319
val ansLoc = (conditionInstructions.last() as MoveInstruction).dest
290320
loopBlock.instructions.add(MoveInstruction(ansLoc, Register.r10()))
291321
loopBlock.instructions.add(CmpInstruction(Register.r10(), ImmediateVal(1)))
292322

293-
loopEndCount++
294-
val loopEndBlock = Block("LoopEnd$loopEndCount", mutableListOf())
323+
val loopEndBlock = Block("LoopEnd$loopCount", mutableListOf())
324+
loopEndLabel = loopEndBlock.label
295325
loopBlock.instructions.add(JumpInstruction(AsmJumpOp.JNE, loopEndBlock.label!!))
296326
val newBlock = convert(ir.body, loopBlock)
297327
newBlock.instructions.add(JumpInstruction(AsmJumpOp.JMP, loopBlock.label!!))

0 commit comments

Comments
 (0)