-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
286 lines (247 loc) · 12.4 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"""This file is the entrypoint of the execution."""
from __future__ import absolute_import
from __future__ import print_function
import pyverilog
import z3
from z3 import Solver, Int, BitVec, Context, BitVecSort, ExprRef, BitVecRef, If, BitVecVal, And
from pyverilog.vparser.parser import parse
from pyverilog.vparser.ast import Description, ModuleDef, Node, IfStatement, SingleStatement, And, Constant, Rvalue, Plus, Input, Output
from pyverilog.vparser.ast import WhileStatement, ForStatement, CaseStatement, Block, SystemCall, Land, InstanceList, IntConst, Partselect, Ioport
from pyverilog.vparser.ast import Value, Reg, Initial, Eq, Identifier, Initial, NonblockingSubstitution, Decl, Always, Assign, NotEql, Case
from pyverilog.vparser.ast import Concat, BlockingSubstitution, Parameter, StringConst, Wire, PortArg
import sys
import os
from optparse import OptionParser
from typing import Optional
import random, string
import time
from itertools import product
import logging
import gc
from pyverilog.vparser.preprocessor import preprocess
from engine.execution_manager import ExecutionManager
from engine.symbolic_state import SymbolicState
from helpers.rvalue_parser import tokenize, parse_tokens, evaluate
from strategies.dfs import DepthFirst
from engine.execution_engine import ExecutionEngine
from pyverilog.dataflow.dataflow_analyzer import VerilogDataflowAnalyzer
from pyverilog.dataflow.optimizer import VerilogDataflowOptimizer
from pyverilog.dataflow.graphgen import VerilogGraphGenerator
import pygraphviz as pgv
gc.collect()
with open('errors.log', 'w'):
pass
logging.basicConfig(filename='errors.log', level=logging.DEBUG)
logging.debug("Starting over")
INFO = "Verilog Symbolic Execution Engine"
VERSION = pyverilog.__version__
USAGE = "Usage: python3 -m main <num_cycles> <verilog_file>.v > out.txt"
def showVersion():
print(INFO)
print(VERSION)
print(USAGE)
sys.exit()
def main():
"""Entrypoint of the program."""
engine: ExecutionEngine = ExecutionEngine()
search_strategy: DepthFirst = DepthFirst()
optparser = OptionParser()
optparser.add_option("-v", "--version", action="store_true", dest="showversion",
default=False, help="Show the version")
optparser.add_option("-I", "--include", dest="include", action="append",
default=["designs/or1200/", "darkriscv/", "designs"], help="Include path")
optparser.add_option("-D", dest="define", action="append",
default=[], help="Macro Definition")
optparser.add_option("-B", "--debug", action="store_true", dest="showdebug", help="Debug Mode")
optparser.add_option("-t", "--top", dest="topmodule",
default="top", help="Top module, Default=top")
optparser.add_option("--nobind", action="store_true", dest="nobind",
default=False, help="No binding traversal, Default=False")
optparser.add_option("--noreorder", action="store_true", dest="noreorder",
default=False, help="No reordering of binding dataflow, Default=False")
optparser.add_option("-o", "--output", dest="outputfile",
default="out.png", help="Graph file name, Default=out.png")
optparser.add_option("-s", "--search", dest="searchtarget", action="append",
default=[], help="Search Target Signal")
optparser.add_option("--walk", action="store_true", dest="walk",
default=False, help="Walk contineous signals, Default=False")
optparser.add_option("--identical", action="store_true", dest="identical",
default=False, help="# Identical Laef, Default=False")
optparser.add_option("--step", dest="step", type='int',
default=1, help="# Search Steps, Default=1")
optparser.add_option("--reorder", action="store_true", dest="reorder",
default=False, help="Reorder the contineous tree, Default=False")
optparser.add_option("--delay", action="store_true", dest="delay",
default=False, help="Inset Delay Node to walk Regs, Default=False")
(options, args) = optparser.parse_args()
num_cycles = args[0]
filelist = args[1:]
if options.showversion:
showVersion()
if options.showdebug:
engine.debug = True
for f in filelist:
if not os.path.exists(f):
raise IOError("file not found: " + f)
if len(filelist) == 0:
showVersion()
text = preprocess(filelist, include=options.include, define=options.define)
#print(text)
ast, directives = parse(filelist,
preprocess_include=options.include,
preprocess_define=options.define)
# analyzer = VerilogDataflowAnalyzer(filelist, options.topmodule,
# noreorder=options.noreorder,
# nobind=options.nobind,
# preprocess_include=options.include,
# preprocess_define=options.define)
# analyzer.generate()
# directives = analyzer.get_directives()
# terms = analyzer.getTerms()
# binddict = analyzer.getBinddict()
# optimizer = VerilogDataflowOptimizer(terms, binddict)
# optimizer.resolveConstant()
# resolved_terms = optimizer.getResolvedTerms()
# resolved_binddict = optimizer.getResolvedBinddict()
# constlist = optimizer.getConstlist()
# graphgen = VerilogGraphGenerator(options.topmodule, terms, binddict,
# resolved_terms, resolved_binddict, constlist, options.outputfile)
# for target in options.searchtarget:
# graphgen.generate(target, walk=options.walk, identical=options.identical,
# step=options.step)
# graphgen.draw()
#ast.show()
#print(ast.children()[0].definitions)
description: Description = ast.children()[0]
top_level_module: ModuleDef = description.children()[0]
modules = description.definitions
start = time.process_time()
engine.execute(top_level_module, modules, None, directives, num_cycles)
end = time.process_time()
print(f"Elapsed time {end - start}")
if __name__ == '__main__':
main()
# def seen_all_cases(self, m: ExecutionManager, bit_index: int, nested_ifs: int) -> bool:
# """Checks if we've seen all the cases for this index in the bit string.
# We know there are no more nested conditionals within the block, just want to check
# that we have seen the path where this bit was turned on but the thing to the left of it
# could vary."""
# # first check if things less than me have been added.
# # so index 29 shouldnt be completed before 30
# for i in range(bit_index + 1, 32):
# if not i in m.completed:
# return False
# count = 0
# seen = m.seen
# #print(seen)
# for path in seen[m.curr_module]:
# if path[bit_index] == '1':
# count += 1
# if count > 2 * nested_ifs:
# return True
# return False
# def module_count(self, m: ExecutionManager, items) -> None:
# """Traverse a top level module and count up the instances of each type of module."""
# if isinstance(items, Block):
# items = items.statements
# if hasattr(items, '__iter__'):
# for item in items:
# if isinstance(item, InstanceList):
# if item.module in m.instance_count:
# m.instance_count[item.module] += 1
# else:
# m.instance_count[item.module] = 1
# self.module_count(m, item.instances)
# if isinstance(item, Block):
# self.module_count(m, item.items)
# elif isinstance(item, Always):
# self.module_count(m, item.statement)
# elif isinstance(item, Initial):
# self.module_count(m, item.statement)
# elif items != None:
# if isinstance(items, InstanceList):
# if items.module in m.instance_count:
# m.instance_count[items.module] += 1
# else:
# m.instance_count[items.module] = 1
# self.module_count(m, items.instances)
# def populate_child_paths(self, manager: ExecutionManager) -> None:
# """Populates child path codes based on number of paths."""
# for child in manager.child_num_paths:
# manager.child_path_codes[child] = []
# if manager.piece_wise:
# manager.child_path_codes[child] = []
# for i in manager.child_range:
# manager.child_path_codes[child].append(to_binary(i))
# else:
# for i in range(manager.child_num_paths[child]):
# manager.child_path_codes[child].append(to_binary(i))
# def populate_seen_mod(self, manager: ExecutionManager) -> None:
# """Populates child path codes but in a format to keep track of corresponding states that we've seen."""
# for child in manager.child_num_paths:
# manager.seen_mod[child] = {}
# for i in range(manager.child_num_paths[child]):
# manager.seen_mod[child][(to_binary(i))] = {}
# def piece_wise_execute(self, ast: ModuleDef, manager: Optional[ExecutionManager], modules) -> None:
# """Drives symbolic execution piecewise when number of paths is too large not to breakup.
# We break it up to avoid the memory blow up."""
# self.module_depth += 1
# manager.piece_wise = True
# state: SymbolicState = SymbolicState()
# if manager is None:
# manager: ExecutionManager = ExecutionManager()
# manager.debugging = False
# modules_dict = {}
# for module in modules:
# modules_dict[module.name] = module
# manager.child_path_codes[module.name] = to_binary(0)
# manager.seen_mod[module.name] = {}
# sub_manager = ExecutionManager()
# manager.names_list.append(module.name)
# self.init_run(sub_manager, module)
# self.module_count(manager, module.items)
# manager.child_num_paths[module.name] = sub_manager.num_paths
# manager.config[module.name] = to_binary(0)
# state.store[module.name] = {}
# total_paths = sum(manager.child_num_paths.values())
# print(total_paths)
# manager.piece_wise = True
# #TODO: things piecewise, say 10,000 at a time.
# for i in range(0, total_paths, 10000):
# manager.child_range = range(i*10000, i*10000+10000)
# self.populate_child_paths(manager)
# if len(modules) > 1:
# self.populate_seen_mod(manager)
# manager.opt_1 = True
# else:
# manager.opt_1 = False
# manager.modules = modules_dict
# paths = list(product(*manager.child_path_codes.values()))
# #print(f" Upper bound on num paths {len(paths)}")
# self.init_run(manager, ast)
# manager.seen = {}
# for name in manager.names_list:
# manager.seen[name] = []
# manager.curr_module = manager.names_list[0]
# for i in range(len(paths)):
# for j in range(len(paths[i])):
# manager.config[manager.names_list[j]] = paths[i][j]
# manager.path_code = manager.config[manager.names_list[0]]
# if self.check_dup(manager):
# # #if False:
# continue
# else:
# print("------------------------")
# #print(f"{ast.name} Path {i}")
# self.visit_module(manager, state, ast, modules_dict)
# manager.seen[ast.name].append(manager.path_code)
# if (manager.assertion_violation):
# print("Assertion violation")
# manager.assertion_violation = False
# self.solve_pc(state.pc)
# manager.curr_level = 0
# manager.dependencies = {}
# state.pc.reset()
# #manager.path_code = to_binary(0)
# #print(f" finishing {ast.name}")
# self.module_depth -= 1