@@ -25,26 +25,35 @@ class FormalVerifier(dataflow_facade):
25
25
26
26
def __init__ (self , code_file_name , topmodule = '' ):
27
27
dataflow_facade .__init__ (self , code_file_name , topmodule = topmodule )
28
+ t_manager = term_manager ()
29
+ t_manager .set_scope_dict (self .binds .scope_dict )
30
+ t_manager .set_terms (self .binds ._terms )
28
31
29
32
DFBranch .tocode_org = MethodType (DFBranch .tocode , None , DFBranch )
30
- DFBranch .tocode = MethodType (DFBranch_tocode_for_sympy , None , DFBranch )
33
+ DFBranch .tocode = MethodType (DFBranch_tocode , None , DFBranch )
31
34
DFOperator .tocode_org = MethodType (DFOperator .tocode , None , DFOperator )
32
- DFOperator .tocode = MethodType (DFOperator_tocode_for_sympy , None , DFOperator )
35
+ DFOperator .tocode = MethodType (DFOperator_tocode , None , DFOperator )
33
36
DFOperator .is_reduction = MethodType (DFOperator_is_reduction , None , DFOperator )
34
- DFTerminal . scope_dict = self . binds . scope_dict
35
- DFTerminal .terms = self . binds . _terms
36
- DFTerminal .get_bind = MethodType (DFTerminal_get_terms , None , DFTerminal )
37
+ DFOperator . is_algebra = MethodType ( DFOperator_is_algebra , None , DFOperator )
38
+ DFTerminal .tocode_org = MethodType ( DFTerminal . tocode , None , DFTerminal )
39
+ DFTerminal .tocode = MethodType (DFTerminal_tocode , None , DFTerminal )
37
40
38
41
def write_back_DFmethods (self ):
42
+ DFTerminal .tocode = MethodType (DFTerminal .tocode_org , None , DFTerminal )
39
43
DFBranch .tocode = MethodType (DFBranch .tocode_org , None , DFBranch )
40
44
DFOperator .tocode = MethodType (DFOperator .tocode_org , None , DFOperator )
41
45
del DFOperator .tocode_org
42
46
del DFOperator .is_reduction
43
- del DFTerminal .scope_dict
44
- del DFTerminal .terms
45
- del DFTerminal .get_bind
47
+ del DFOperator .is_algebra
46
48
47
49
def calc_truth_table (self , var_name ):
50
+ try :
51
+ truth_table = self ._calc_truth_table (var_name )
52
+ finally :
53
+ self .write_back_DFmethods ()
54
+ return truth_table
55
+
56
+ def _calc_truth_table (self , var_name ):
48
57
"""[FUNCTIONS]
49
58
"""
50
59
for tv , tk , bvi , bit , term_lsb in self .binds .walk_reg_each_bit ():
@@ -64,12 +73,22 @@ def calc_truth_table(self, var_name):
64
73
ns [symbol_name ] = Symbol (symbol_name )
65
74
else :
66
75
for i in range (lsb , msb + 1 ):
67
- symbol_name_bit = get_bus_name (symbol_name , i )
76
+ symbol_name_bit = term_manager (). publish_new_name (symbol_name , i )
68
77
ns [symbol_name_bit ] = Symbol (symbol_name_bit )
69
78
79
+ term_manager ().flash_renamed_signals ()
70
80
f = self .to_sympy_expr (target_tree .tocode ())
81
+ for signal in term_manager ().renamed_signals :
82
+ signal = signal .replace ('.' ,'_' )
83
+ ns [signal ] = Symbol (signal )
84
+
85
+ #TODO delete renamed signals
71
86
expr = sympify (f , ns , convert_xor = False )
72
87
88
+ for signal in term_manager ().renamed_signals :
89
+ signal = signal .replace ('.' ,'_' )
90
+ ns .pop (signal )
91
+
73
92
truth_table = {}
74
93
for result , var_state in self .walk_truth_table (ns , expr ):
75
94
truth_table [str (var_state )] = result
@@ -111,7 +130,7 @@ def to_sympy_expr(self, expr):
111
130
expr = expr .replace (comb [0 ], comb [1 ])
112
131
return expr
113
132
114
- def DFBranch_tocode_for_sympy (self , dest = 'dest' , always = '' ):
133
+ def DFBranch_tocode (self , dest = 'dest' , always = '' ):
115
134
ret = '('
116
135
if self .condnode is not None : ret += '(' + self .condnode .tocode (dest ) + ')'
117
136
ret += '& '
@@ -131,35 +150,86 @@ def DFOperator_is_reduction(self):
131
150
else :
132
151
return False
133
152
134
- def DFOperator_tocode_for_sympy (self ):
135
- if not self .is_reduction ():
136
- return self .tocode_org ()
153
+ def DFOperator_is_algebra (self ):
154
+ if self .operator in ('Plus' , 'Minus' , 'LessThan' , 'GreaterThan' , 'LessEq' , 'GreaterEq' ):
155
+ return True
156
+ else :
157
+ return False
158
+
159
+ def DFTerminal_tocode (self , dest = 'dest' ):
160
+ if term_manager ().is_under_algebra :
161
+ return term_manager ().publish_new_name (str (self )).replace ('.' ,'_' )
137
162
else :
138
- if isinstance (self .nextnodes [0 ], DFPartselect ): #TODO
163
+ return self .tocode_org ()
164
+
165
+ def DFOperator_tocode (self ):
166
+ if self .is_algebra (): #if operator is algebra, nextnodes aren't sweeped.
167
+ term_manager ().set_is_under_algebra (True )
168
+ code = self .tocode_org ()
169
+ term_manager ().set_is_under_algebra (False )
170
+ return code
171
+ elif self .is_reduction ():
172
+ if isinstance (self .nextnodes [0 ], DFPartselect ):
139
173
term = self .nextnodes [0 ].var
140
174
msb = eval_value (self .nextnodes [0 ].msb )
141
175
lsb = eval_value (self .nextnodes [0 ].lsb )
142
176
elif isinstance (self .nextnodes [0 ], DFTerminal ):
143
- term = self .nextnodes [0 ]. get_bind ( )
177
+ term = term_manager (). get_term ( str ( self .nextnodes [0 ]) )
144
178
msb = eval_value (term .msb )
145
179
lsb = eval_value (term .lsb )
146
180
else :
147
181
raise Exception ('Unexpected exception.' )
148
182
mark = op2mark .op2mark (self .operator )
149
- return mark .join ([get_bus_name (term , i ).replace ('.' , '_' ) for i in range (lsb , msb + 1 )])
183
+ return mark .join ([term_manager ().publish_new_name (str (term ), i ).replace ('.' , '_' ) for i in range (lsb , msb + 1 )])
184
+ else :
185
+ return self .tocode_org ()
186
+
187
+ class term_manager (object ):
188
+ """ [CLASSES]
189
+ Singleton class for manage terminals for DFxxx.
190
+ """
191
+ _singleton = None
192
+ def __new__ (cls , * argc , ** argv ):
193
+ if cls ._singleton == None :
194
+ cls ._singleton = object .__new__ (cls )
195
+ cls .is_under_algebra = False
196
+ cls .exclude_signals = []
197
+ cls .renamed_signals = []
198
+ return cls ._singleton
199
+
200
+ def set_scope_dict (self , scope_dict ):
201
+ self .scope_dict = scope_dict
202
+
203
+ def set_terms (self , terms ):
204
+ self .terms = terms
205
+
206
+ def get_term (self , signal ):
207
+ scope = self .scope_dict [signal ]
208
+ return self .terms [scope ]
209
+
210
+ def set_is_under_algebra (self , flag = False ):
211
+ self .is_under_algebra = flag
212
+
213
+ def publish_new_name (self , signal , bit = None ):
214
+ if bit is None :
215
+ new_name = signal + '_'
216
+ else :
217
+ new_name = signal + '__' + str (bit ) + '__'
218
+ while (new_name in self .scope_dict .keys ()):
219
+ new_name += '_'
220
+ if bit is None :
221
+ self .renamed_signals .append (new_name )
222
+ return new_name
150
223
151
- def DFTerminal_get_terms (self ):
152
- scope = DFTerminal .scope_dict [str (self )]
153
- return DFTerminal .terms [scope ]
224
+ def flash_renamed_signals (self ):
225
+ self .renamed_signals = []
154
226
155
- def get_bus_name ( signal , bit ):
156
- return str ( signal ) + '__' + str ( bit ) + '__'
227
+ def flash_exclude_signals ( self ):
228
+ self . exclude_signals = []
157
229
158
230
if __name__ == '__main__' :
159
231
fv = FormalVerifier ("../testcode/fv_test.v" )
160
232
#fv.calc_truth_table('TOP.A')
161
233
#fv.calc_truth_table('TOP.C')
162
234
#fv.calc_truth_table('TOP.E')
163
- a = fv .calc_truth_table ('TOP.G' )
164
- fv .write_back_DFmethods ()
165
- pass
235
+ fv .calc_truth_table ('TOP.H' )
0 commit comments