@@ -3,6 +3,82 @@ package deep.decaf.low.amd64
33import deep.decaf.ir.*
44import java.lang.IllegalStateException
55
6+ class AsmProgramInfo {
7+ var loopCount = 0
8+ var ifCount = 0
9+
10+ var stackSize = 0
11+ private set
12+
13+ private val globalVariables = mutableMapOf<String , Type >() // name -> type
14+ private val globalArrays = mutableMapOf<String , Int >() // name -> size
15+ private val variableStacks = mutableListOf<MutableMap <String , MemLoc >>()
16+ private val methodFormalParamsStack = mutableListOf<Map <String , Location >>()
17+
18+ fun addGlobalVariable (name : String , type : Type ) {
19+ globalVariables[name] = type
20+ }
21+
22+ fun addGlobalArray (name : String , size : Int ) {
23+ globalArrays[name] = size
24+ }
25+
26+ fun enterScope () {
27+ variableStacks.add(mutableMapOf ())
28+ }
29+
30+ fun leaveScope (): Int {
31+ val size = variableStacks.last().size
32+ variableStacks.removeAt(variableStacks.size - 1 )
33+ stackSize - = size
34+ return size
35+ }
36+
37+ fun addVariable (name : String ): MemLoc {
38+ stackSize++
39+ val loc = MemLoc (
40+ Register .basePointer(),
41+ NumberOffset (- 8 * stackSize)
42+ )
43+ variableStacks.last()[name] = loc
44+ return loc
45+ }
46+
47+ fun getVariableLocation (name : String ): MemLoc {
48+ for (locMap in variableStacks.reversed()) {
49+ if (name in locMap) {
50+ return locMap.getValue(name)
51+ }
52+ }
53+ if (name in methodFormalParamsStack.last()) {
54+ return methodFormalParamsStack.last()[name]!! as MemLoc
55+ }
56+ if (name in globalVariables) {
57+ return MemLoc (
58+ Register .instructionPointer(),
59+ StringOffset (name)
60+ )
61+ }
62+ throw IllegalStateException (" variable $name not found" )
63+ }
64+
65+ fun pushStack () {
66+ stackSize++
67+ }
68+
69+ fun popStack () {
70+ stackSize--
71+ }
72+
73+ fun pushMethodArgs (args : Map <String , Location >) {
74+ methodFormalParamsStack.add(args)
75+ }
76+
77+ fun popMethodArgs () {
78+ methodFormalParamsStack.removeAt(methodFormalParamsStack.size - 1 )
79+ }
80+ }
81+
682fun irExprToLow (expr : IRExpr , info : AsmProgramInfo ): List <Instruction > {
783 val instructions = mutableListOf<Instruction >()
884
@@ -392,80 +468,4 @@ fun irMethodToLow(method: IRMethodDeclaration, info: AsmProgramInfo): Method {
392468 info.popMethodArgs()
393469
394470 return Method (argMap, blocks)
395- }
396-
397- class AsmProgramInfo {
398- var loopCount = 0
399- var ifCount = 0
400-
401- var stackSize = 0
402- private set
403-
404- private val globalVariables = mutableMapOf<String , Type >() // name -> type
405- private val globalArrays = mutableMapOf<String , Int >() // name -> size
406- private val variableStacks = mutableListOf<MutableMap <String , MemLoc >>()
407- private val methodFormalParamsStack = mutableListOf<Map <String , Location >>()
408-
409- fun addGlobalVariable (name : String , type : Type ) {
410- globalVariables[name] = type
411- }
412-
413- fun addGlobalArray (name : String , size : Int ) {
414- globalArrays[name] = size
415- }
416-
417- fun enterScope () {
418- variableStacks.add(mutableMapOf ())
419- }
420-
421- fun leaveScope (): Int {
422- val size = variableStacks.last().size
423- variableStacks.removeAt(variableStacks.size - 1 )
424- stackSize - = size
425- return size
426- }
427-
428- fun addVariable (name : String ): MemLoc {
429- stackSize++
430- val loc = MemLoc (
431- Register .basePointer(),
432- NumberOffset (- 8 * stackSize)
433- )
434- variableStacks.last()[name] = loc
435- return loc
436- }
437-
438- fun getVariableLocation (name : String ): MemLoc {
439- for (locMap in variableStacks.reversed()) {
440- if (name in locMap) {
441- return locMap.getValue(name)
442- }
443- }
444- if (name in methodFormalParamsStack.last()) {
445- return methodFormalParamsStack.last()[name]!! as MemLoc
446- }
447- if (name in globalVariables) {
448- return MemLoc (
449- Register .instructionPointer(),
450- StringOffset (name)
451- )
452- }
453- throw IllegalStateException (" variable $name not found" )
454- }
455-
456- fun pushStack () {
457- stackSize++
458- }
459-
460- fun popStack () {
461- stackSize--
462- }
463-
464- fun pushMethodArgs (args : Map <String , Location >) {
465- methodFormalParamsStack.add(args)
466- }
467-
468- fun popMethodArgs () {
469- methodFormalParamsStack.removeAt(methodFormalParamsStack.size - 1 )
470- }
471471}
0 commit comments