22from src .api .debug import __DEBUG__
33from src .arch .interface .quad import Quad
44from src .arch .z80 .backend import Backend
5+ from src .arch .z80 .backend .common import BOOL_t , F16_t , F_t , I8_t , I16_t , I32_t , U8_t , U16_t , U32_t
56from src .ast import NodeVisitor
67from src .symbols import sym as symbols
78
@@ -10,26 +11,27 @@ class TranslatorInstVisitor(NodeVisitor):
1011 def __init__ (self , backend : Backend ):
1112 self .backend = backend
1213
13- def emit (self , * args ) :
14+ def emit (self , * args : str ) -> None :
1415 """Convert the given args to a Quad (3 address code) instruction"""
1516 quad = Quad (* args )
16- __DEBUG__ ("EMIT " + str ( quad ) )
17+ __DEBUG__ (f "EMIT { quad !s } " )
1718 self .backend .MEMORY .append (quad )
1819
1920 @staticmethod
20- def TSUFFIX (type_ ) :
21+ def TSUFFIX (type_ : TYPE | symbols . TYPEREF | symbols . BASICTYPE ) -> str :
2122 assert isinstance (type_ , symbols .TYPE ) or TYPE .is_valid (type_ )
2223
2324 _TSUFFIX = {
24- TYPE .byte : "i8" ,
25- TYPE .ubyte : "u8" ,
26- TYPE .integer : "i16" ,
27- TYPE .uinteger : "u16" ,
28- TYPE .long : "i32" ,
29- TYPE .ulong : "u32" ,
30- TYPE .fixed : "f16" ,
31- TYPE .float : "f" ,
25+ TYPE .byte : I8_t ,
26+ TYPE .ubyte : U8_t ,
27+ TYPE .integer : I16_t ,
28+ TYPE .uinteger : U16_t ,
29+ TYPE .long : I32_t ,
30+ TYPE .ulong : U32_t ,
31+ TYPE .fixed : F16_t ,
32+ TYPE .float : F_t ,
3233 TYPE .string : "str" ,
34+ TYPE .boolean : BOOL_t ,
3335 }
3436
3537 if isinstance (type_ , symbols .TYPEREF ):
@@ -41,200 +43,205 @@ def TSUFFIX(type_):
4143
4244 return _TSUFFIX [type_ ]
4345
44- def ic_aaddr (self , t1 , t2 ):
45- return self .emit ("aaddr" , t1 , t2 )
46+ @classmethod
47+ def _no_bool (cls , type_ : TYPE | symbols .TYPEREF | symbols .BASICTYPE ) -> str :
48+ """Returns the corresponding type suffix except for bool which maps to U8_t"""
49+ return cls .TSUFFIX (type_ ) if cls .TSUFFIX (type_ ) != BOOL_t else U8_t
4650
47- def ic_abs (self , type_ , t1 , t2 ):
48- return self .emit ("abs" + self . TSUFFIX ( type_ ) , t1 , t2 )
51+ def ic_aaddr (self , t1 , t2 ) -> None :
52+ self .emit ("aaddr" , t1 , t2 )
4953
50- def ic_add (self , type_ , t1 , t2 , t3 ) :
51- return self .emit ("add " + self .TSUFFIX (type_ ), t1 , t2 , t3 )
54+ def ic_abs (self , type_ : TYPE | symbols . BASICTYPE , t1 , t2 ) -> None :
55+ self .emit ("abs " + self .TSUFFIX (type_ ), t1 , t2 )
5256
53- def ic_aload (self , type_ , t1 , mangle : str ) :
54- return self .emit ("aload " + self .TSUFFIX (type_ ), t1 , mangle )
57+ def ic_add (self , type_ : TYPE | symbols . BASICTYPE , t1 , t2 , t3 ) -> None :
58+ self .emit ("add " + self .TSUFFIX (type_ ), t1 , t2 , t3 )
5559
56- def ic_and (self , type_ ) :
57- return self .emit ("and " + self .TSUFFIX (type_ ))
60+ def ic_aload (self , type_ : TYPE | symbols . BASICTYPE , t1 , mangle : str ) -> None :
61+ self .emit ("aload " + self .TSUFFIX (type_ ), t1 , mangle )
5862
59- def ic_astore (self , type_ , addr : str , t ) :
60- return self .emit ("astore" + self .TSUFFIX (type_ ), addr , t )
63+ def ic_and (self , type_ : TYPE | symbols . BASICTYPE , t , t1 , t2 ) -> None :
64+ self .emit (f"and { self ._no_bool (type_ )} " , t , t1 , t2 )
6165
62- def ic_band (self , type_ ) :
63- return self .emit ("band " + self .TSUFFIX (type_ ))
66+ def ic_astore (self , type_ : TYPE | symbols . BASICTYPE , addr : str , t ) -> None :
67+ self .emit ("astore " + self .TSUFFIX (type_ ), addr , t )
6468
65- def ic_bnot (self , type_ , t1 , t2 ):
66- return self .emit ("bnot " + self .TSUFFIX (type_ ), t1 , t2 )
69+ def ic_band (self , type_ : TYPE | symbols . BASICTYPE , t , t1 , t2 ) -> None :
70+ self .emit ("band " + self .TSUFFIX (type_ ), t , t1 , t2 )
6771
68- def ic_bor (self , type_ ) :
69- return self .emit ("bor " + self .TSUFFIX (type_ ))
72+ def ic_bnot (self , type_ : TYPE | symbols . BASICTYPE , t1 , t2 ) -> None :
73+ self .emit ("bnot " + self .TSUFFIX (type_ ), t1 , t2 )
7074
71- def ic_bxor (self , type_ ) :
72- return self .emit ("bxor " + self .TSUFFIX (type_ ))
75+ def ic_bor (self , type_ : TYPE | symbols . BASICTYPE , t , t1 , t2 ) -> None :
76+ self .emit ("bor " + self .TSUFFIX (type_ ), t , t1 , t2 )
7377
74- def ic_call (self , label : str , num : int ) :
75- return self .emit ("call" , label , num )
78+ def ic_bxor (self , type_ : TYPE | symbols . BASICTYPE , t , t1 , t2 ) -> None :
79+ self .emit ("bxor" + self . TSUFFIX ( type_ ), t , t1 , t2 )
7680
77- def ic_cast (self , t1 , type1 , type2 , t2 ):
81+ def ic_call (self , label : str , num : int ) -> None :
82+ self .emit ("call" , label , num )
83+
84+ def ic_cast (self , t1 , type1 , type2 , t2 ) -> None :
7885 self .emit ("cast" , t1 , self .TSUFFIX (type1 ), self .TSUFFIX (type2 ), t2 )
7986
80- def ic_data (self , type_ , data : list ):
81- return self .emit ("data" , self .TSUFFIX (type_ ), data )
87+ def ic_data (self , type_ : TYPE | symbols . BASICTYPE , data : list ) -> None :
88+ self .emit ("data" , self .TSUFFIX (type_ ), data )
8289
83- def ic_deflabel (self , label : str , t ):
84- return self .emit ("deflabel" , label , t )
90+ def ic_deflabel (self , label : str , t ) -> None :
91+ self .emit ("deflabel" , label , t )
8592
86- def ic_div (self , type_ ) :
87- return self .emit ("div" + self .TSUFFIX (type_ ))
93+ def ic_div (self , type_ : TYPE | symbols . BASICTYPE , t , t1 , t2 ) -> None :
94+ self .emit ("div" + self .TSUFFIX (type_ ), t , t1 , t2 )
8895
89- def ic_end (self , t ):
90- return self .emit ("end" , t )
96+ def ic_end (self , t ) -> None :
97+ self .emit ("end" , t )
9198
92- def ic_enter (self , arg ):
93- return self .emit ("enter" , arg )
99+ def ic_enter (self , arg ) -> None :
100+ self .emit ("enter" , arg )
94101
95- def ic_eq (self , type_ , t , t1 , t2 ):
96- return self .emit ("eq" + self .TSUFFIX (type_ ), t , t1 , t2 )
102+ def ic_eq (self , type_ : TYPE | symbols . BASICTYPE , t , t1 , t2 ) -> None :
103+ self .emit ("eq" + self .TSUFFIX (type_ ), t , t1 , t2 )
97104
98- def ic_exchg (self ):
99- return self .emit ("exchg" )
105+ def ic_exchg (self ) -> None :
106+ self .emit ("exchg" )
100107
101- def ic_fparam (self , type_ , t ):
102- return self .emit ("fparam" + self .TSUFFIX (type_ ), t )
108+ def ic_fparam (self , type_ : TYPE | symbols . BASICTYPE , t ) -> None :
109+ self .emit ("fparam" + self .TSUFFIX (type_ ), t )
103110
104- def ic_fpload (self , type_ , t , offset ):
105- return self .emit ("fpload" + self .TSUFFIX (type_ ), t , offset )
111+ def ic_fpload (self , type_ : TYPE | symbols . BASICTYPE , t , offset ) -> None :
112+ self .emit ("fpload" + self .TSUFFIX (type_ ), t , offset )
106113
107- def ic_ge (self , type_ , t , t1 , t2 ):
108- return self .emit ("ge" + self .TSUFFIX (type_ ), t , t1 , t2 )
114+ def ic_ge (self , type_ : TYPE | symbols . BASICTYPE , t , t1 , t2 ) -> None :
115+ self .emit ("ge" + self .TSUFFIX (type_ ), t , t1 , t2 )
109116
110- def ic_gt (self , type_ , t , t1 , t2 ):
111- return self .emit ("gt" + self .TSUFFIX (type_ ), t , t1 , t2 )
117+ def ic_gt (self , type_ : TYPE | symbols . BASICTYPE , t , t1 , t2 ) -> None :
118+ self .emit ("gt" + self .TSUFFIX (type_ ), t , t1 , t2 )
112119
113- def ic_in (self , t ):
114- return self .emit ("in" , t )
120+ def ic_in (self , t ) -> None :
121+ self .emit ("in" , t )
115122
116- def ic_inline (self , asm_code : str ):
117- return self .emit ("inline" , asm_code )
123+ def ic_inline (self , asm_code : str ) -> None :
124+ self .emit ("inline" , asm_code )
118125
119- def ic_jgezero (self , type_ , t , label : str ):
120- return self .emit ("jgezero" + self .TSUFFIX (type_ ), t , label )
126+ def ic_jgezero (self , type_ : TYPE | symbols . BASICTYPE , t , label : str ) -> None :
127+ self .emit (f "jgezero{ self ._no_bool (type_ )} " , t , label )
121128
122- def ic_jnzero (self , type_ , t , label : str ):
123- return self .emit ("jnzero" + self .TSUFFIX (type_ ), t , label )
129+ def ic_jnzero (self , type_ : TYPE | symbols . BASICTYPE , t , label : str ) -> None :
130+ self .emit (f "jnzero{ self ._no_bool (type_ )} " , t , label )
124131
125- def ic_jump (self , label : str ):
126- return self .emit ("jump" , label )
132+ def ic_jump (self , label : str ) -> None :
133+ self .emit ("jump" , label )
127134
128- def ic_jzero (self , type_ , t , label : str ):
129- return self .emit ("jzero" + self .TSUFFIX (type_ ), t , label )
135+ def ic_jzero (self , type_ : TYPE | symbols . BASICTYPE , t , label : str ) -> None :
136+ self .emit (f "jzero{ self ._no_bool (type_ )} " , t , label )
130137
131- def ic_label (self , label : str ):
132- return self .emit ("label" , label )
138+ def ic_label (self , label : str ) -> None :
139+ self .emit ("label" , label )
133140
134- def ic_larrd (self , offset , arg1 , size , arg2 , bound_ptrs ):
135- return self .emit ("larrd" , offset , arg1 , size , arg2 , bound_ptrs )
141+ def ic_larrd (self , offset , arg1 , size , arg2 , bound_ptrs ) -> None :
142+ self .emit ("larrd" , offset , arg1 , size , arg2 , bound_ptrs )
136143
137- def ic_le (self , type_ , t , t1 , t2 ):
138- return self .emit ("le" + self .TSUFFIX (type_ ), t , t1 , t2 )
144+ def ic_le (self , type_ : TYPE | symbols . BASICTYPE , t , t1 , t2 ) -> None :
145+ self .emit ("le" + self .TSUFFIX (type_ ), t , t1 , t2 )
139146
140- def ic_leave (self , convention : str ):
141- return self .emit ("leave" , convention )
147+ def ic_leave (self , convention : str ) -> None :
148+ self .emit ("leave" , convention )
142149
143- def ic_lenstr (self , t1 , t2 ):
144- return self .emit ("lenstr" , t1 , t2 )
150+ def ic_lenstr (self , t1 , t2 ) -> None :
151+ self .emit ("lenstr" , t1 , t2 )
145152
146- def ic_load (self , type_ , t1 , t2 ):
147- return self .emit ("load" + self .TSUFFIX (type_ ), t1 , t2 )
153+ def ic_load (self , type_ : TYPE | symbols . BASICTYPE , t1 , t2 ) -> None :
154+ self .emit ("load" + self .TSUFFIX (type_ ), t1 , t2 )
148155
149- def ic_lt (self , type_ , t , t1 , t2 ):
150- return self .emit ("lt" + self .TSUFFIX (type_ ), t , t1 , t2 )
156+ def ic_lt (self , type_ : TYPE | symbols . BASICTYPE , t , t1 , t2 ) -> None :
157+ self .emit ("lt" + self .TSUFFIX (type_ ), t , t1 , t2 )
151158
152- def ic_lvard (self , offset , default_value : list ):
153- return self .emit ("lvard" , offset , default_value )
159+ def ic_lvard (self , offset , default_value : list ) -> None :
160+ self .emit ("lvard" , offset , default_value )
154161
155- def ic_lvarx (self , type_ , offset , default_value : list ):
162+ def ic_lvarx (self , type_ : TYPE | symbols . BASICTYPE , offset , default_value : list ) -> None :
156163 self .emit ("lvarx" , offset , self .TSUFFIX (type_ ), default_value )
157164
158- def ic_memcopy (self , t1 , t2 , t3 ):
159- return self .emit ("memcopy" , t1 , t2 , t3 )
165+ def ic_memcopy (self , t1 , t2 , t3 ) -> None :
166+ self .emit ("memcopy" , t1 , t2 , t3 )
160167
161- def ic_mod (self , type_ ) :
162- return self .emit ("mod" + self .TSUFFIX (type_ ))
168+ def ic_mod (self , type_ : TYPE | symbols . BASICTYPE , t , t1 , t2 ) -> None :
169+ self .emit ("mod" + self .TSUFFIX (type_ ), t , t1 , t2 )
163170
164- def ic_mul (self , type_ ) :
165- return self .emit ("mul" + self .TSUFFIX (type_ ))
171+ def ic_mul (self , type_ : TYPE | symbols . BASICTYPE , t , t1 , t2 ) -> None :
172+ self .emit ("mul" + self .TSUFFIX (type_ ), t , t1 , t2 )
166173
167- def ic_ne (self , type_ , t , t1 , t2 ):
168- return self .emit ("ne" + self .TSUFFIX (type_ ), t , t1 , t2 )
174+ def ic_ne (self , type_ : TYPE | symbols . BASICTYPE , t , t1 , t2 ) -> None :
175+ self .emit ("ne" + self .TSUFFIX (type_ ), t , t1 , t2 )
169176
170- def ic_neg (self , type_ , t1 , t2 ):
171- return self .emit ("neg" + self .TSUFFIX (type_ ), t1 , t2 )
177+ def ic_neg (self , type_ : TYPE | symbols . BASICTYPE , t1 , t2 ) -> None :
178+ self .emit ("neg" + self .TSUFFIX (type_ ), t1 , t2 )
172179
173- def ic_nop (self ):
174- return self .emit ("nop" )
180+ def ic_nop (self ) -> None :
181+ self .emit ("nop" )
175182
176- def ic_not (self , type_ , t1 , t2 ):
177- return self .emit ("not" + self .TSUFFIX (type_ ), t1 , t2 )
183+ def ic_not (self , type_ : TYPE | symbols . BASICTYPE , t1 , t2 ) -> None :
184+ self .emit ("not" + self .TSUFFIX (type_ ), t1 , t2 )
178185
179- def ic_or (self , type_ ) :
180- return self .emit ("or" + self .TSUFFIX (type_ ))
186+ def ic_or (self , type_ : TYPE | symbols . BASICTYPE , t , t1 , t2 ) -> None :
187+ self .emit (f "or{ self ._no_bool (type_ )} " , t , t1 , t2 )
181188
182- def ic_org (self , type_ ) :
183- return self .emit ("org" + self .TSUFFIX (type_ ))
189+ def ic_org (self , type_ : TYPE | symbols . BASICTYPE ) -> None :
190+ self .emit ("org" + self .TSUFFIX (type_ ))
184191
185- def ic_out (self , t1 , t2 ):
186- return self .emit ("out" , t1 , t2 )
192+ def ic_out (self , t1 , t2 ) -> None :
193+ self .emit ("out" , t1 , t2 )
187194
188- def ic_paaddr (self , t1 , t2 ):
189- return self .emit ("paaddr" , t1 , t2 )
195+ def ic_paaddr (self , t1 , t2 ) -> None :
196+ self .emit ("paaddr" , t1 , t2 )
190197
191- def ic_paddr (self , t1 , t2 ):
192- return self .emit ("paddr" , t1 , t2 )
198+ def ic_paddr (self , t1 , t2 ) -> None :
199+ self .emit ("paddr" , t1 , t2 )
193200
194- def ic_paload (self , type_ , t , offset : int ):
195- return self .emit ("paload" + self .TSUFFIX (type_ ), t , offset )
201+ def ic_paload (self , type_ : TYPE | symbols . BASICTYPE , t , offset : int ) -> None :
202+ self .emit ("paload" + self .TSUFFIX (type_ ), t , offset )
196203
197- def ic_param (self , type_ , t ):
198- return self .emit ("param" + self .TSUFFIX (type_ ), t )
204+ def ic_param (self , type_ : TYPE | symbols . BASICTYPE , t ) -> None :
205+ self .emit ("param" + self .TSUFFIX (type_ ), t )
199206
200- def ic_pastore (self , type_ , offset , t ):
201- return self .emit ("pastore" + self .TSUFFIX (type_ ), offset , t )
207+ def ic_pastore (self , type_ : TYPE | symbols . BASICTYPE , offset , t ) -> None :
208+ self .emit ("pastore" + self .TSUFFIX (type_ ), offset , t )
202209
203- def ic_pload (self , type_ , t1 , offset ):
204- return self .emit ("pload" + self .TSUFFIX (type_ ), t1 , offset )
210+ def ic_pload (self , type_ : TYPE | symbols . BASICTYPE , t1 , offset ) -> None :
211+ self .emit ("pload" + self .TSUFFIX (type_ ), t1 , offset )
205212
206- def ic_pow (self , type_ ) :
207- return self .emit ("pow" + self .TSUFFIX (type_ ))
213+ def ic_pow (self , type_ : TYPE | symbols . BASICTYPE , t , t1 , t2 ) -> None :
214+ self .emit ("pow" + self .TSUFFIX (type_ ), t , t1 , t2 )
208215
209- def ic_pstore (self , type_ , offset , t ):
210- return self .emit ("pstore" + self .TSUFFIX (type_ ), offset , t )
216+ def ic_pstore (self , type_ : TYPE | symbols . BASICTYPE , offset , t ) -> None :
217+ self .emit ("pstore" + self .TSUFFIX (type_ ), offset , t )
211218
212- def ic_ret (self , type_ , t , addr ):
213- return self .emit ("ret" + self .TSUFFIX (type_ ), t , addr )
219+ def ic_ret (self , type_ : TYPE | symbols . BASICTYPE , t , addr ) -> None :
220+ self .emit ("ret" + self .TSUFFIX (type_ ), t , addr )
214221
215- def ic_return (self , addr ):
216- return self .emit ("ret" , addr )
222+ def ic_return (self , addr ) -> None :
223+ self .emit ("ret" , addr )
217224
218- def ic_shl (self , type_ ) :
219- return self .emit ("shl" + self .TSUFFIX (type_ ))
225+ def ic_shl (self , type_ : TYPE | symbols . BASICTYPE , t , t1 , t2 ) -> None :
226+ self .emit ("shl" + self .TSUFFIX (type_ ), t , t1 , t2 )
220227
221- def ic_shr (self , type_ ) :
222- return self .emit ("shr" + self .TSUFFIX (type_ ))
228+ def ic_shr (self , type_ : TYPE | symbols . BASICTYPE , t , t1 , t2 ) -> None :
229+ self .emit ("shr" + self .TSUFFIX (type_ ), t , t1 , t2 )
223230
224- def ic_store (self , type_ , t1 , t2 ):
225- return self .emit ("store" + self .TSUFFIX (type_ ), t1 , t2 )
231+ def ic_store (self , type_ : TYPE | symbols . BASICTYPE , t1 , t2 ) -> None :
232+ self .emit ("store" + self .TSUFFIX (type_ ), t1 , t2 )
226233
227- def ic_sub (self , type_ ) :
228- return self .emit ("sub" + self .TSUFFIX (type_ ))
234+ def ic_sub (self , type_ : TYPE | symbols . BASICTYPE , t , t1 , t2 ) -> None :
235+ self .emit ("sub" + self .TSUFFIX (type_ ), t , t1 , t2 )
229236
230- def ic_var (self , name : str , size_ ):
231- return self .emit ("var" , name , size_ )
237+ def ic_var (self , name : str , size_ ) -> None :
238+ self .emit ("var" , name , size_ )
232239
233- def ic_vard (self , name : str , data : list ):
234- return self .emit ("vard" , name , data )
240+ def ic_vard (self , name : str , data : list ) -> None :
241+ self .emit ("vard" , name , data )
235242
236- def ic_varx (self , name : str , type_ , default_value : list ):
237- return self .emit ("varx" , name , self .TSUFFIX (type_ ), default_value )
243+ def ic_varx (self , name : str , type_ : TYPE | symbols . BASICTYPE , default_value : list ) -> None :
244+ self .emit ("varx" , name , self .TSUFFIX (type_ ), default_value )
238245
239- def ic_xor (self , type_ ) :
240- return self .emit ("xor" + self .TSUFFIX (type_ ))
246+ def ic_xor (self , type_ : TYPE | symbols . BASICTYPE , t , t1 , t2 ) -> None :
247+ self .emit ("xor" + self .TSUFFIX (type_ ), t , t1 , t2 )
0 commit comments