Skip to content

Commit 9ce9997

Browse files
committed
irMethodToLow actually returns a Method
1 parent 4f19885 commit 9ce9997

3 files changed

Lines changed: 42 additions & 16 deletions

File tree

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ fun irStatementToLow(statement: IRStatement, info: AsmProgramInfo): List<Instruc
264264
return instructions
265265
}
266266

267-
fun irMethodToLow(method: IRMethodDeclaration, info: AsmProgramInfo): List<Block> {
267+
fun irMethodToLow(method: IRMethodDeclaration, info: AsmProgramInfo): Method {
268268
val blocks = mutableListOf<Block>()
269269
blocks.add(Block(method.name, mutableListOf()))
270270

@@ -380,12 +380,18 @@ fun irMethodToLow(method: IRMethodDeclaration, info: AsmProgramInfo): List<Block
380380
}
381381
}
382382

383-
blocks[0].instructions.add(EnterInstruction(0))
383+
val argMap = mutableMapOf<String, Location>()
384+
for ((i, arg) in method.argList.withIndex()) {
385+
argMap[arg.name] = MemLoc(
386+
Register.basePointer(),
387+
NumberOffset((i + 2) * 8)
388+
)
389+
}
390+
info.pushMethodArgs(argMap)
384391
convert(method.block, blocks[0])
385-
blocks.last().instructions.add(LeaveInstruction)
386-
blocks.last().instructions.add(ReturnInstruction)
392+
info.popMethodArgs()
387393

388-
return blocks
394+
return Method(argMap, blocks)
389395
}
390396

391397
class AsmProgramInfo {
@@ -398,6 +404,7 @@ class AsmProgramInfo {
398404
private val globalVariables = mutableMapOf<String, Type>() // name -> type
399405
private val globalArrays = mutableMapOf<String, Int>() // name -> size
400406
private val variableStacks = mutableListOf<MutableMap<String, MemLoc>>()
407+
private val methodFormalParamsStack = mutableListOf<Map<String, Location>>()
401408

402409
fun addGlobalVariable(name: String, type: Type) {
403410
globalVariables[name] = type
@@ -434,6 +441,9 @@ class AsmProgramInfo {
434441
return locMap.getValue(name)
435442
}
436443
}
444+
if (name in methodFormalParamsStack.last()) {
445+
return methodFormalParamsStack.last()[name]!! as MemLoc
446+
}
437447
if (name in globalVariables) {
438448
return MemLoc(
439449
Register.instructionPointer(),
@@ -450,4 +460,12 @@ class AsmProgramInfo {
450460
fun popStack() {
451461
stackSize--
452462
}
463+
464+
fun pushMethodArgs(args: Map<String, Location>) {
465+
methodFormalParamsStack.add(args)
466+
}
467+
468+
fun popMethodArgs() {
469+
methodFormalParamsStack.removeAt(methodFormalParamsStack.size - 1)
470+
}
453471
}

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ data class PushInstruction(val src: Location) : Instruction() {
211211
}
212212

213213
data class PopInstruction(val src: Location) : Instruction() {
214-
override fun toString() = "pop ${src ?: ""}"
214+
override fun toString() = "pop $src"
215215
}
216216

217217
object LeaveInstruction : Instruction() {
@@ -239,12 +239,22 @@ data class Block(val label: String?, val instructions: MutableList<Instruction>)
239239
}
240240

241241
data class Method(
242-
var stackSize: Int,
243-
var formalParams: Map<String, Location>,
244-
var localVars: Map<String, Location>,
245-
var blocks: List<Block>,
246-
var program: Program
247-
)
242+
val formalParams: Map<String, Location>,
243+
val blocks: List<Block>,
244+
) {
245+
init {
246+
blocks[0].instructions.add(0, EnterInstruction(0))
247+
val lastBlock = blocks.last()
248+
lastBlock.instructions.add(LeaveInstruction)
249+
lastBlock.instructions.add(ReturnInstruction)
250+
}
251+
252+
override fun toString(): String {
253+
val stringBuilder = StringBuilder()
254+
for (block in blocks) stringBuilder.append(block.toString())
255+
return stringBuilder.toString()
256+
}
257+
}
248258

249259
data class Program(
250260
var globalVars: List<String>,

src/main/kotlin/deep/decaf/tester/LowMethodTester.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ fun main() {
1919
for (method in irTree.methodDeclarations) {
2020
if (method.name == "main") {
2121
val info = AsmProgramInfo()
22-
val blocks = irMethodToLow(method, info)
22+
val methodAsm = irMethodToLow(method, info)
2323
println(file.absolutePath)
24-
for (block in blocks) {
25-
print(block)
26-
}
24+
println(methodAsm)
2725
println()
2826
}
2927
}

0 commit comments

Comments
 (0)