23
23
MacroInstruction ,
24
24
MacroParts ,
25
25
PseudoInstruction ,
26
- StackEffect ,
27
26
OverriddenInstructionPlaceHolder ,
28
27
TIER_ONE ,
29
28
TIER_TWO ,
@@ -183,17 +182,11 @@ def effect_str(effects: list[StackEffect]) -> str:
183
182
assert target_instr
184
183
target_popped = effect_str (target_instr .input_effects )
185
184
target_pushed = effect_str (target_instr .output_effects )
186
- if popped is None and pushed is None :
187
- popped , pushed = target_popped , target_pushed
188
- else :
189
- assert popped == target_popped
190
- assert pushed == target_pushed
191
- case _:
192
- typing .assert_never (thing )
185
+ popped , pushed = target_popped , target_pushed
193
186
return instr , popped , pushed
194
187
195
188
@contextlib .contextmanager
196
- def metadata_item (self , signature , open , close ) :
189
+ def metadata_item (self , signature : str , open : str , close : str ) -> typing . Iterator [ None ] :
197
190
self .out .emit ("" )
198
191
self .out .emit (f"extern { signature } ;" )
199
192
self .out .emit ("#ifdef NEED_OPCODE_METADATA" )
@@ -243,21 +236,21 @@ def from_source_files(self) -> str:
243
236
paths = f"\n { self .out .comment } " .join (filenames )
244
237
return f"{ self .out .comment } from:\n { self .out .comment } { paths } \n "
245
238
246
- def write_provenance_header (self ):
239
+ def write_provenance_header (self ) -> None :
247
240
self .out .write_raw (f"{ self .out .comment } This file is generated by { THIS } \n " )
248
241
self .out .write_raw (self .from_source_files ())
249
242
self .out .write_raw (f"{ self .out .comment } Do not edit!\n " )
250
243
251
- def assign_opcode_ids (self ):
244
+ def assign_opcode_ids (self ) -> None :
252
245
"""Assign IDs to opcodes"""
253
246
254
- ops : list [( bool , str ) ] = [] # (has_arg, name) for each opcode
247
+ ops : list [tuple [ bool , str ] ] = [] # (has_arg, name) for each opcode
255
248
instrumented_ops : list [str ] = []
256
249
257
250
for instr in itertools .chain (
258
251
[instr for instr in self .instrs .values () if instr .kind != "op" ],
259
252
self .macro_instrs .values ()):
260
-
253
+ assert isinstance ( instr , ( Instruction , MacroInstruction , PseudoInstruction ))
261
254
name = instr .name
262
255
if name .startswith ('INSTRUMENTED_' ):
263
256
instrumented_ops .append (name )
@@ -274,11 +267,11 @@ def assign_opcode_ids(self):
274
267
assert len (set (ops )) == len (ops )
275
268
assert len (set (instrumented_ops )) == len (instrumented_ops )
276
269
277
- opname : list [str or None ] = [None ] * 512
278
- opmap : dict = {}
279
- markers : dict = {}
270
+ opname : list [str | None ] = [None ] * 512
271
+ opmap : dict [ str , int ] = {}
272
+ markers : dict [ str , int ] = {}
280
273
281
- def map_op (op , name ) :
274
+ def map_op (op : int , name : str ) -> None :
282
275
assert op < len (opname )
283
276
assert opname [op ] is None
284
277
assert name not in opmap
@@ -320,11 +313,11 @@ def map_op(op, name):
320
313
for i , op in enumerate (sorted (self .pseudos )):
321
314
map_op (256 + i , op )
322
315
323
- assert 255 not in opmap # 255 is reserved
316
+ assert 255 not in opmap . values () # 255 is reserved
324
317
self .opmap = opmap
325
318
self .markers = markers
326
319
327
- def write_opcode_ids (self , opcode_ids_h_filename , opcode_targets_filename ) :
320
+ def write_opcode_ids (self , opcode_ids_h_filename : str , opcode_targets_filename : str ) -> None :
328
321
"""Write header file that defined the opcode IDs"""
329
322
330
323
with open (opcode_ids_h_filename , "w" ) as f :
@@ -342,10 +335,10 @@ def write_opcode_ids(self, opcode_ids_h_filename, opcode_targets_filename):
342
335
self .out .emit ("" )
343
336
self .out .emit ("/* Instruction opcodes for compiled code */" )
344
337
345
- def define (name , opcode ) :
338
+ def define (name : str , opcode : int ) -> None :
346
339
self .out .emit (f"#define { name :<38} { opcode :>3} " )
347
340
348
- all_pairs = []
341
+ all_pairs : list [ tuple [ int , int , str ]] = []
349
342
# the second item in the tuple sorts the markers before the ops
350
343
all_pairs .extend ((i , 1 , name ) for (name , i ) in self .markers .items ())
351
344
all_pairs .extend ((i , 2 , name ) for (name , i ) in self .opmap .items ())
@@ -392,11 +385,7 @@ def write_metadata(self, metadata_filename: str, pymetadata_filename: str) -> No
392
385
assert target_instr
393
386
if format is None :
394
387
format = target_instr .instr_fmt
395
- else :
396
- assert format == target_instr .instr_fmt
397
388
assert format is not None
398
- case _:
399
- typing .assert_never (thing )
400
389
all_formats .add (format )
401
390
402
391
# Turn it into a sorted list of enum values.
@@ -483,8 +472,6 @@ def write_metadata(self, metadata_filename: str, pymetadata_filename: str) -> No
483
472
self .write_metadata_for_macro (self .macro_instrs [thing .name ])
484
473
case parsing .Pseudo ():
485
474
self .write_metadata_for_pseudo (self .pseudo_instrs [thing .name ])
486
- case _:
487
- typing .assert_never (thing )
488
475
489
476
with self .metadata_item (
490
477
"const struct opcode_macro_expansion "
@@ -520,8 +507,6 @@ def write_metadata(self, metadata_filename: str, pymetadata_filename: str) -> No
520
507
)
521
508
case parsing .Pseudo ():
522
509
pass
523
- case _:
524
- typing .assert_never (thing )
525
510
526
511
with self .metadata_item (
527
512
"const char * const _PyOpcode_uop_name[OPCODE_UOP_NAME_SIZE]" , "=" , ";"
@@ -770,8 +755,6 @@ def write_instructions(
770
755
# self.write_macro(self.macro_instrs[thing.name])
771
756
case parsing .Pseudo ():
772
757
pass
773
- case _:
774
- typing .assert_never (thing )
775
758
776
759
print (
777
760
f"Wrote { n_instrs } instructions and { n_macros } macros "
@@ -814,8 +797,6 @@ def write_executor_instructions(
814
797
pass
815
798
case parsing .Pseudo ():
816
799
pass
817
- case _:
818
- typing .assert_never (thing )
819
800
print (
820
801
f"Wrote { n_instrs } instructions and { n_uops } ops to { executor_filename } " ,
821
802
file = sys .stderr ,
@@ -843,8 +824,6 @@ def write_abstract_interpreter_instructions(
843
824
pass
844
825
case parsing .Pseudo ():
845
826
pass
846
- case _:
847
- typing .assert_never (thing )
848
827
print (
849
828
f"Wrote some stuff to { abstract_interpreter_filename } " ,
850
829
file = sys .stderr ,
@@ -878,7 +857,7 @@ def write_instr(self, instr: Instruction) -> None:
878
857
self .out .emit (f"DISPATCH();" )
879
858
880
859
881
- def main ():
860
+ def main () -> None :
882
861
"""Parse command line, parse input, analyze, write output."""
883
862
args = arg_parser .parse_args () # Prints message and sys.exit(2) on error
884
863
if len (args .input ) == 0 :
0 commit comments