22
33import ast
44from dataclasses import dataclass
5- from typing import List
5+ from typing import Iterable
66
77from utils import dedent , indent , indent_stmt , label_name
88
99
10- @dataclass ( frozen = True )
10+ @dataclass
1111class X86Program :
1212 body : dict [str , list [instr ]] | list [instr ]
1313
@@ -31,7 +31,7 @@ def __str__(self):
3131 result += '\n '
3232 return result
3333
34- @dataclass ( frozen = True )
34+ @dataclass
3535class X86ProgramDefs :
3636 defs : list [ast .FunctionDef ]
3737
@@ -42,10 +42,15 @@ class instr: ...
4242class arg : ...
4343class location (arg ): ...
4444
45- @dataclass (frozen = True )
45+ @dataclass (frozen = True , eq = False )
4646class Instr (instr ):
4747 instr : str
48- args : List [arg ]
48+ args : tuple [arg , ...]
49+
50+ def __init__ (self , name : str , args : Iterable [arg ]):
51+ # https://docs.python.org/3/library/dataclasses.html#frozen-instances
52+ object .__setattr__ (self , 'instr' , name )
53+ object .__setattr__ (self , 'args' , tuple (args ))
4954
5055 def source (self ):
5156 return self .args [0 ]
@@ -54,45 +59,45 @@ def target(self):
5459 def __str__ (self ):
5560 return indent_stmt () + self .instr + ' ' + ', ' .join (str (a ) for a in self .args ) + '\n '
5661
57- @dataclass (frozen = True )
62+ @dataclass (frozen = True , eq = False )
5863class Callq (instr ):
5964 func : str
6065 num_args : int
6166
6267 def __str__ (self ):
6368 return indent_stmt () + 'callq' + ' ' + self .func + '\n '
6469
65- @dataclass (frozen = True )
70+ @dataclass (frozen = True , eq = False )
6671class IndirectCallq (instr ):
6772 func : arg
6873 num_args : int
6974
7075 def __str__ (self ):
7176 return indent_stmt () + 'callq' + ' *' + str (self .func ) + '\n '
7277
73- @dataclass (frozen = True )
78+ @dataclass (frozen = True , eq = False )
7479class JumpIf (instr ):
7580 cc : str
7681 label : str
7782
7883 def __str__ (self ):
7984 return indent_stmt () + 'j' + self .cc + ' ' + self .label + '\n '
8085
81- @dataclass (frozen = True )
86+ @dataclass (frozen = True , eq = False )
8287class Jump (instr ):
8388 label : str
8489
8590 def __str__ (self ):
8691 return indent_stmt () + 'jmp ' + self .label + '\n '
8792
88- @dataclass (frozen = True )
93+ @dataclass (frozen = True , eq = False )
8994class IndirectJump (instr ):
9095 target : location
9196
9297 def __str__ (self ):
9398 return indent_stmt () + 'jmp *' + str (self .target ) + '\n '
9499
95- @dataclass (frozen = True )
100+ @dataclass (frozen = True , eq = False )
96101class TailJump (instr ):
97102 func : arg
98103 arity : int
0 commit comments