11from typing import List
2+ from utils import label_name
23
34class X86Program :
45 __match_args__ = ("body" ,)
@@ -8,10 +9,15 @@ def __repr__(self):
89 result = ''
910 if type (self .body ) == dict :
1011 for (l ,ss ) in self .body .items ():
12+ if l == label_name ('main' ):
13+ result += '\t .globl ' + label_name ('main' ) + '\n '
1114 result += l + ':\n '
1215 result += '\n ' .join ([repr (s ) for s in ss ]) + '\n \n '
1316 else :
14- result = '\n ' .join ([repr (s ) for s in self .body ])
17+ result += '\t .globl ' + label_name ('main' ) + '\n ' + \
18+ label_name ('main' ) + ':\n '
19+ result += '\n ' .join ([repr (s ) for s in self .body ])
20+ result += '\n '
1521 return result
1622
1723class instr : ...
@@ -31,16 +37,36 @@ def source(self):
3137 def target (self ):
3238 return self .args [- 1 ]
3339 def __repr__ (self ):
34- return self .instr + ' ' + ', ' .join (repr (a ) for a in self .args )
40+ return ' \t ' + self .instr + ' ' + ', ' .join (repr (a ) for a in self .args )
3541
3642class Callq (instr ):
3743 __match_args__ = ("func" , "num_args" )
3844 def __init__ (self , func , num_args ):
3945 self .func = func
4046 self .num_args = num_args
4147 def __repr__ (self ):
42- return 'callq ' + ' ' + self .func
48+ return '\t callq ' + ' ' + self .func
4349
50+ class JumpIf (instr ):
51+ cc : str
52+ label : str
53+
54+ __match_args__ = ("cc" , "label" )
55+ def __init__ (self , cc , label ):
56+ self .cc = cc
57+ self .label = label
58+ def __repr__ (self ):
59+ return '\t j' + self .cc + ' ' + self .label
60+
61+ class Jump (instr ):
62+ label : str
63+
64+ __match_args__ = ("label" ,)
65+ def __init__ (self , label ):
66+ self .label = label
67+ def __repr__ (self ):
68+ return '\t jmp ' + self .label
69+
4470class Variable (location ):
4571 __match_args__ = ("id" ,)
4672 def __init__ (self , id ):
@@ -60,7 +86,7 @@ class Immediate(arg):
6086 def __init__ (self , value ):
6187 self .value = value
6288 def __repr__ (self ):
63- return repr (self .value )
89+ return '$' + repr (self .value )
6490 def __eq__ (self , other ):
6591 if isinstance (other , Immediate ):
6692 return self .value == other .value
@@ -112,22 +138,3 @@ def __eq__(self, other):
112138 def __hash__ (self ):
113139 return hash ((self .reg , self .offset ))
114140
115- class JumpIf (instr ):
116- cc : str
117- label : str
118-
119- __match_args__ = ("cc" , "label" )
120- def __init__ (self , cc , label ):
121- self .cc = cc
122- self .label = label
123- def __repr__ (self ):
124- return 'j' + self .cc + ' ' + self .label
125-
126- class Jump (instr ):
127- label : str
128-
129- __match_args__ = ("label" ,)
130- def __init__ (self , label ):
131- self .label = label
132- def __repr__ (self ):
133- return 'jmp ' + self .label
0 commit comments