@@ -602,6 +602,9 @@ def write_body(self, out: Formatter) -> None:
602
602
out .assign (var , oeffect )
603
603
604
604
605
+ MacroParts = list [Component | parser .CacheEffect ]
606
+
607
+
605
608
@dataclasses .dataclass
606
609
class MacroInstruction :
607
610
"""A macro instruction."""
@@ -613,7 +616,7 @@ class MacroInstruction:
613
616
instr_fmt : str
614
617
instr_flags : InstructionFlags
615
618
macro : parser .Macro
616
- parts : list [ Component | parser . CacheEffect ]
619
+ parts : MacroParts
617
620
cache_offset : int
618
621
predicted : bool = False
619
622
@@ -906,7 +909,7 @@ def analyze_macro(self, macro: parser.Macro) -> MacroInstruction:
906
909
components = self .check_macro_components (macro )
907
910
stack , initial_sp = self .stack_analysis (components )
908
911
sp = initial_sp
909
- parts : list [ Component | parser . CacheEffect ] = []
912
+ parts : MacroParts = []
910
913
flags = InstructionFlags .newEmpty ()
911
914
offset = 0
912
915
for component in components :
@@ -1253,17 +1256,14 @@ def write_metadata(self) -> None:
1253
1256
pass
1254
1257
case parser .InstDef (name = name ):
1255
1258
instr = self .instrs [name ]
1256
- # Since an 'op' is not a bytecode, it has no expansion
1257
- if instr .kind != "op" and instr .is_viable_uop ():
1258
- # Double check there aren't any used cache effects.
1259
- # If this fails, see write_macro_expansions().
1260
- assert not instr .active_caches , (instr .name , instr .cache_effects )
1261
- self .out .emit (
1262
- f"[{ name } ] = "
1263
- f"{{ .nuops = 1, .uops = {{ {{ { name } , 0, 0 }} }} }},"
1264
- )
1259
+ # Since an 'op' is not a bytecode, it has no expansion; but 'inst' is
1260
+ if instr .kind == "inst" and instr .is_viable_uop ():
1261
+ # Construct a dummy Component -- input/output mappings are not used
1262
+ part = Component (instr , [], [], instr .active_caches )
1263
+ self .write_macro_expansions (instr .name , [part ])
1265
1264
case parser .Macro ():
1266
- self .write_macro_expansions (self .macro_instrs [thing .name ])
1265
+ mac = self .macro_instrs [thing .name ]
1266
+ self .write_macro_expansions (mac .name , mac .parts )
1267
1267
case parser .Pseudo ():
1268
1268
pass
1269
1269
case _:
@@ -1328,29 +1328,29 @@ def add(name: str) -> None:
1328
1328
if instr .kind == "op" and instr .is_viable_uop ():
1329
1329
add (instr .name )
1330
1330
1331
- def write_macro_expansions (self , mac : MacroInstruction ) -> None :
1331
+ def write_macro_expansions (self , name : str , parts : MacroParts ) -> None :
1332
1332
"""Write the macro expansions for a macro-instruction."""
1333
1333
# TODO: Refactor to share code with write_cody(), is_viaible_uop(), etc.
1334
1334
offset = 0 # Cache effect offset
1335
1335
expansions : list [tuple [str , int , int ]] = [] # [(name, size, offset), ...]
1336
- for part in mac . parts :
1336
+ for part in parts :
1337
1337
if isinstance (part , Component ):
1338
1338
# All component instructions must be viable uops
1339
1339
if not part .instr .is_viable_uop ():
1340
- print (f"NOTE: Part { part .instr .name } of { mac . name } is not a viable uop" )
1340
+ print (f"NOTE: Part { part .instr .name } of { name } is not a viable uop" )
1341
1341
return
1342
1342
if part .instr .instr_flags .HAS_ARG_FLAG or not part .active_caches :
1343
1343
size , offset = 0 , 0
1344
1344
else :
1345
1345
# If this assert triggers, is_viable_uops() lied
1346
- assert len (part .active_caches ) == 1 , (mac . name , part .instr .name )
1346
+ assert len (part .active_caches ) == 1 , (name , part .instr .name )
1347
1347
cache = part .active_caches [0 ]
1348
1348
size , offset = cache .effect .size , cache .offset
1349
1349
expansions .append ((part .instr .name , size , offset ))
1350
- assert len (expansions ) > 0 , f"Macro { mac . name } has empty expansion?!"
1350
+ assert len (expansions ) > 0 , f"Macro { name } has empty expansion?!"
1351
1351
pieces = [f"{{ { name } , { size } , { offset } }}" for name , size , offset in expansions ]
1352
1352
self .out .emit (
1353
- f"[{ mac . name } ] = "
1353
+ f"[{ name } ] = "
1354
1354
f"{{ .nuops = { len (expansions )} , .uops = {{ { ', ' .join (pieces )} }} }},"
1355
1355
)
1356
1356
0 commit comments