@@ -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
391397class 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}
0 commit comments