summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/back/generator.py17
1 files changed, 11 insertions, 6 deletions
diff --git a/src/back/generator.py b/src/back/generator.py
index 1ae403f..84ab639 100644
--- a/src/back/generator.py
+++ b/src/back/generator.py
@@ -33,6 +33,8 @@ class Generator(object):
else:
self.__op_list.append(op)
+ # this cde will appear before every function
+ # sets bp, pushes used registers and variables
def generate_prologue(self, name):
Scope().set_function(name)
self.emit(Label(name))
@@ -43,6 +45,8 @@ class Generator(object):
for register in Register().get_registers(name):
self.emit("PUSH r%d" % register)
+ # this code will appear after every function
+ # restores registers, sp, bp
def generate_epilogue(self, name):
for variable in Scope().get_variables()[1]:
self.emit("POP r0")
@@ -55,7 +59,7 @@ class Generator(object):
self.emit("RET")
def generate(self):
- # pass 1 - generate non-label ops
+ # pass 1 - generate ops, but not labels yet
for tac in self.tac_list:
if isinstance(tac, TAC):
if tac.op in [Op.ADD, Op.SUB, Op.MUL, Op.DIV, Op.MOD, Op.AND, Op.OR]:
@@ -106,7 +110,7 @@ class Generator(object):
else:
raise Exception("%s is not a valid TACList element" % repr(tac))
- # pass 2 - generate label ops
+ # pass 2 - generate label ops (ignores still present labels)
for op in self.__op_list:
if isinstance(op, TAC):
if op.op == Op.BEZ:
@@ -125,12 +129,12 @@ class Generator(object):
# pass 3 - remove labels
self.__op_list = filter(lambda x: not isinstance(x, Label), self.__op_list)
- # pass 4 - replace pseudo registers and insert number of registers
+ # pass 4 - replace the pseudo registers with their register numbers
def replace_pseudo_regs(op):
regs = Register().count
- bp = "r%d" % (regs + 1)
- sp = "r%d" % (regs + 2)
- rv = "r%d" % (regs + 3)
+ bp = "r%d" % (regs + 1) # base pointer
+ sp = "r%d" % (regs + 2) # stack pointer
+ rv = "r%d" % (regs + 3) # return value
return op.replace("bp", bp).replace("sp", sp).replace("rv", rv)
self.__op_list = map(replace_pseudo_regs, self.__op_list)
@@ -139,6 +143,7 @@ class Generator(object):
# finally, print the generated code
print "\n".join(self.__op_list)
+ # find the line number to which the given label leads
def get_label_offset(self, name, tac):
tac_index = 0
for op in self.__op_list: