-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
119 lines (98 loc) · 2.58 KB
/
main.cpp
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
/* What it does:
* 1. Checks reachability by parsing Verilog modules
* 2. Unrolls the transition relation and converts it to a dimacs formatted CNF
* 3. Calls a SAT solver
*/
#include <to_dimacs.h>
#include <iostream>
#include <fstream>
#include <string>
#include <sys/wait.h>
#include <regex>
#include <parser.h>
#include <unistd.h>
using namespace std;
// Create regex for file ending
const regex v_file("^.*\\.(vh|v)$");
int unroll_num;
extern int pvals [2];
extern vector<string> out;
void write_dimacs() {
// Open new dimacs file for writing
ofstream outfile ("out.dimacs");
// Write p line
outfile << "p cnf " << pvals[0] << " " << pvals[1] << endl;
// Write remaining lines in order
for (vector<string>::iterator it = out.begin(), end = out.end(); it != end; ++it) {
outfile << *it << endl;
}
// Close dimacs file
outfile.close();
cout << "Dimacs file written to './out.dimacs'" << endl;
}
// Check and return second argument number
int isNumber(const char str[]) {
string numString;
int i = 0;
while(isdigit(str[i])) {
numString += str[i];
i++;
}
unroll_num = atoi(numString.c_str());
return unroll_num;
}
int main(int argc, char const *argv[]) {
string line;
int line_num = 1;
ifstream f (argv[1]);
// Check for first argument
if (!argv[1]) {
cout << "No file to read!" << endl;
cout << "Usage: simple-vsat <path-to-verilog> [number-of-relation-unrolls]" << endl;
exit(1);
}
// Verify first argument is a verilog file
smatch m;
string str = argv[1];
if (!regex_match(str, m, v_file)) {
cout << "ERROR! Could not open file: " << argv[1] << endl;
exit(1);
}
// Check second argument is a number
if(!argv[2]) {
unroll_num = 1;
}
else if (!isNumber(argv[2])) {
cout << argv[2] << " is not a positive integer" << endl;
exit(1);
}
// Open verilog file
if (!f.is_open()) {
cout << "Error while opening file";
exit(1);
}
// Create Parser instance
Parse::parser file;
// Parse the verilog file line by line
cout << "Parsing..." << endl;
while(getline(f, line)) {
file.parse_line(line, line_num);
line_num ++;
}
if (f.bad()) {
cout << "Error while reading file";
exit(1);
}
// Write our conversions to dimacs out file
write_dimacs();
// Call MiniSAT
int pid, status;
if (pid = fork()) {
waitpid(pid, &status, 0);
}
else {
const char exec[] = "./minisat/minisat";
execl(exec, exec, "./out.dimacs", NULL);
}
return 0;
}