-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtac_generator.py
More file actions
158 lines (119 loc) · 5.53 KB
/
tac_generator.py
File metadata and controls
158 lines (119 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from ast_tree import *
from tac_instruction import *
class TempVar:
def __init__(self):
self.counter = 0
def new_temp(self):
name = f"t{self.counter}"
self.counter += 1
return name
class TACGenerator:
def __init__(self, symbol_table):
self.instructions = []
self.temps = TempVar()
self.symbol_table = symbol_table;
def visit(self, node):
method = 'visit_' + node.__class__.__name__
visitor = getattr(self, method, self.generic_visit)
return visitor(node)
def generic_visit(self, node):
raise Exception(f"Nenhum visitador TAC definido para {node.__class__.__name__}")
def visit_Program(self, node):
for stmt in node.statements:
self.visit(stmt)
return self.instructions
def visit_Decl(self, node):
self.instructions.append(TACInstruction("alloc", 1, None, node.name))
def visit_FunctionDecl(self, node):
self.instructions.append(TACInstruction("label", None, None, node.name))
for param_name, _ in node.params:
# assume que cada parâmetro já está em uma variável correspondente
self.instructions.append(TACInstruction("param", None, None, param_name))
self.visit(node.body)
def visit_AutoDecl(self, node):
self.instructions.append(TACInstruction("alloc", 1, None, node.name)) # string
value = self.visit(node.expr)
self.instructions.append(TACInstruction("=", value, None, node.name))
def visit_NamespaceDecl(self, node):
for decl in node.declarations:
self.visit(decl)
def visit_ArrayDecl(self, node):
size = self.visit(node.size)
self.instructions.append(TACInstruction("alloc", size, None, node.name))
def visit_ArrayAccess(self, node):
index = self.visit(node.index)
temp = self.temps.new_temp()
self.instructions.append(TACInstruction("load", f"{node.name}", index, temp))
return temp
def visit_ExprStmt(self, node):
self.visit(node.expr)
def visit_Assign(self, node):
value = self.visit(node.expr)
if isinstance(node.name, VarRef):
self.instructions.append(TACInstruction("=", value, None, node.name.name))
elif isinstance(node.name, ArrayAccess):
index = self.visit(node.name.index)
self.instructions.append(TACInstruction("store", value, index, node.name.name))
else:
raise Exception(f"Tipo de destino inválido em Assign: {type(node.name)}")
def visit_If(self, node):
cond = self.visit(node.condition)
label_else = f"L{self.temps.new_temp()}"
label_end = f"L{self.temps.new_temp()}"
self.instructions.append(TACInstruction("ifz", cond, None, label_else))
self.visit(node.then_branch)
self.instructions.append(TACInstruction("goto", None, None, label_end))
self.instructions.append(TACInstruction("label", None, None, label_else))
if node.else_branch:
self.visit(node.else_branch)
self.instructions.append(TACInstruction("label", None, None, label_end))
def visit_Block(self, node):
for stmt in node.statements:
self.visit(stmt)
def visit_BinaryOp(self, node):
left = self.visit(node.left)
right = self.visit(node.right)
temp = self.temps.new_temp()
self.instructions.append(TACInstruction(node.op, left, right, temp))
return temp
def visit_VarRef(self, node):
return node.name
def visit_QualifiedRef(self, node):
return f"{node.namespace}.{node.name}"
def visit_Literal(self, node):
# label = self.symbol_table.register_literal(node)
# self.instructions.append(TACInstruction("alloc", None, None, label))
# self.instructions.append(TACInstruction('literal_init', node.value, None, label))
return node.value
def visit_TypeCast(self, node):
value = self.visit(node.expr)
temp = self.temps.new_temp()
self.instructions.append(TACInstruction("cast_" + node.target_type, value, None, temp))
return temp
def visit_Call(self, node):
for arg in node.args:
if isinstance(arg, Literal):
self.instructions.append(TACInstruction("arg", arg)) # passa o nó Literal
elif isinstance(arg, VarRef):
self.instructions.append(TACInstruction("arg", arg)) # passa o nó VarRef
else:
temp = self.visit(arg) # para BinaryOp, Call aninhada, etc.
self.instructions.append(TACInstruction("arg", temp))
temp = self.temps.new_temp()
self.instructions.append(TACInstruction("call", node.name, len(node.args), temp))
return temp
def visit_Print(self, node):
for arg in node.args:
if isinstance(arg, Literal):
self.instructions.append(TACInstruction("arg", arg)) # passa o nó Literal
elif isinstance(arg, VarRef):
self.instructions.append(TACInstruction("arg", arg)) # passa o nó VarRef
else:
temp = self.visit(arg) # para BinaryOp, Call aninhada, etc.
self.instructions.append(TACInstruction("arg", temp))
self.instructions.append(TACInstruction("PRINT"))
def visit_Halt(self, node):
self.instructions.append(TACInstruction("HALT"))
def visit_Return(self, node):
value = self.visit(node.expr)
self.instructions.append(TACInstruction("ret", value))