-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_emission.py
24 lines (20 loc) · 961 Bytes
/
code_emission.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from assembely_generation import *
class CodeEmitter :
def __init__(self, file):
self.file = file
def emit_assembly(self , node):
if (isinstance(node , AssemblyProgramNode)):
self.file.write(" .section .note.GNU-stack," + '""' + ",@progbits\n\n\n")
self.file.write(" .text\n")
for function in node.functions :
self.emit_assembly(function)
if (isinstance(node , AssemblyFunctionNode)) :
self.file.write(f" .globl {node.name}\n\n\n")
self.file.write(f"{node.name}:\n")
for instruction in node.instructions :
self.emit_assembly(instruction) ;
if (isinstance(node , AssemblyInstructionNode)) :
if node.dst != None :
self.file.write(f" {node.operation} ${node.src.value}, %{node.dst.register}\n")
else :
self.file.write(f" {node.operation}\n")