-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhnl.py
executable file
·244 lines (210 loc) · 7.98 KB
/
hnl.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
#!/usr/bin/env python3
import argparse
import sys
from multiprocessing import cpu_count
from os.path import abspath, isfile, basename
from subprocess import run
from config import vamos_common_PYTHONPATH
from hna.hnl.parser import Parser
sys.path.append(vamos_common_PYTHONPATH)
from hna.codegen.hnl import CodeGenCpp
script_name = basename(sys.argv[0])
def msg(m):
print(f"\033[0;34m[{script_name}]: {m}\033[0m", file=sys.stdout)
def dbg(m):
print(f"\033[0;35m[{script_name}] DBG: {m}\033[0m", file=sys.stderr)
def compile_monitor(args):
run(["cmake", "."] + [f"-D{x}" for x in args.cmake_defs], cwd=args.out_dir)
run(["make", f"-j{int(cpu_count()/2)+1}"], cwd=args.out_dir)
def main(args):
ctx = None
### Parse the event source specification if given and generate the sources
# if args.sources_def:
# msg("generating event sources")
# from config import vamos_sources_PYTHONPATH
# sys.path.append(vamos_sources_PYTHONPATH)
# from vamos_sources.spec.parser.parser import InlineSpecParser as SrcParser
# from vamos_sources.codegen.cpp.codegen import CodeGenCpp as SrcCodeGenCpp
# src_parser = SrcParser()
# src_ast, ctx = src_parser.parse_path(args.sources_def)
# src_args = copy(args)
# src_codegen = SrcCodeGenCpp(src_args, ctx)
# src_codegen.generate(src_ast)
# msg("event sources generated")
# msg(f"ctx: {ctx}")
# ctx.dump()
### Parse formula
msg("Generating monitor code")
parser = Parser(ctx)
formula = args.input_formula
if isfile(formula):
formula = parser.parse_path(formula)
else:
formula = parser.parse_text(formula)
print("Formula: ", formula)
print("Simplified:", formula.simplify())
print("Removed stutter-red:", formula.remove_stutter_reductions())
print("Formula again: ", formula)
print("-----")
print("Quantifiers: ", [str(q) for q in formula.quantifiers()])
print("Trace variables: ", [str(t) for t in formula.trace_variables()])
print("Program variables: ", [str(p) for p in formula.program_variables()])
constants = formula.constants()
print("Constants: ", [str(c) for c in constants])
problems = formula.problems()
if not formula.is_simple():
problems.append("Formula is not simple, we require that for now")
for problem in problems:
print("\033[1;31m", problem, "\033[0m")
if problems:
exit(1)
# if args.sources_def:
# # these files were generated by the source codegen
# #args.add_gen_files.append("src.cpp")
# #args.add_gen_files.append("inputs.cpp")
# # do not overwrite what the source codegen generated
# args.out_dir_overwrite = False
codegen = CodeGenCpp(args, ctx)
codegen.generate(formula)
# msg("generating events")
# assert args.out_dir_overwrite is False
# events_codegen = EventsCodeGen(args, ctx)
# events_codegen.generate(mpt.alphabet)
# msg("DONE generating events")
# msg("generating traces classes")
# assert args.out_dir_overwrite is False
# traces_codegen = TracesCodeGen(args, ctx)
# traces_codegen.generate(ctx.tracetypes, mpt.alphabet)
# msg("DONE generating traces")
# mpt.todot()
# print(ast.pretty())
msg(f"Monitor generated into '{args.out_dir}'")
if not args.gen_only:
msg("-- Compiling the monitor --")
compile_monitor(args)
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument(
"inputs",
nargs="+",
help="Input files or formulas (formula string, .hnl or .vsrc files, additional C++ files",
)
parser.add_argument(
"--out-dir",
action="store",
default="/tmp/hnl",
help="Output directory (default: /tmp/hnl)",
)
parser.add_argument(
"--out-dir-overwrite",
action="store",
default=True,
help="Overwrite the contents of the output dir if it exists (default: True)",
)
parser.add_argument(
"--build-type", action="store", help="Force build _type for the CMake project"
)
parser.add_argument(
"--sanitize", action="store", help="Compile the monitor with sanitizers"
)
parser.add_argument("--debug", action="store_true", help="Compile in debugging mode and produce debugging files")
parser.add_argument("--debug-prints", action="store_true", help="--debug + print debugging messages to stderr")
parser.add_argument(
"--exit-on-error", action="store_true", help="Stop when a violation is found"
)
parser.add_argument(
"--verbose", "-v", action="store_true", help="Print more messages"
)
parser.add_argument("--stats", action="store_true", help="Gather statistics")
parser.add_argument(
"-D", action="append", default=[], help="Additional CMake definitions"
)
parser.add_argument(
"--cflags",
action="append",
default=[],
help="Additional C flags for the compiler",
)
parser.add_argument(
"--alphabet",
action="store",
help="Comma-separated list of letter to use as the alphabet",
)
parser.add_argument(
"--reduction",
action="store",
help="Comma-separated list of 'reflexive','symmetric'",
)
parser.add_argument(
"--overwrite-file",
action="append",
default=[],
help="Do not generate the default version of the given file, its replacement is assumed to be "
"provided as an additional source.",
)
parser.add_argument(
"--gen-only",
action="store_true",
default=False,
help="Do not try to compile the project, just generate the sources",
)
parser.add_argument(
"--gen-csv-reader",
action="store_true",
default=True,
help="Generate code that can read CSV files as input. "
"It is enabled by default even for monitors with other "
"inputs (for testing). See --help of the monitor binary "
"for instructions on how to use the CSV reader if the monitor has also other inputs.",
)
parser.add_argument(
"--csv-header",
action="store",
help="The header for CSV with types, a comma separated list of 'name:type' pairs where name is a valid C name and type is a valid C type",
)
args = parser.parse_args()
if args.debug_prints:
args.debug = True
args.input_formula = None
args.cpp_files = []
args.add_gen_files = []
args.sources_def = None
args.cmake_defs = args.D
for fl in args.inputs + args.overwrite_file:
if not isfile(fl):
if args.input_formula:
raise RuntimeError(
f"Multiple formulas given (previous: {args.input_formula}, now: {fl})"
)
args.input_formula = fl
continue
if fl.endswith(".mpt"):
if args.input_formula:
raise RuntimeError("Multiple .mpt files given")
args.input_formula = fl
elif (
fl.endswith(".cpp")
or fl.endswith(".h")
or fl.endswith(".hpp")
or fl.endswith(".cxx")
or fl.endswith("cc")
):
args.cpp_files.append(abspath(fl))
elif fl.endswith(".vsrc"):
if args.sources_def:
raise RuntimeError("Multiple .vsrc files given")
args.sources_def = fl
args.overwrite_file = [basename(f) for f in args.overwrite_file]
if args.alphabet:
if args.alphabet[-1] =='b' and args.alphabet[:-1].isnumeric():
args.alphabet = [str(n) for n in range(0, 2**int(args.alphabet[:-1]))]
else:
args.alphabet = list(map(lambda s: s.strip(), args.alphabet.split(",")))
if args.reduction:
args.reduction = list(map(lambda s: s.strip(), args.reduction.split(",")))
print(args)
assert args.gen_csv_reader, "Not generating the reader is not implemented yet"
return args
if __name__ == "__main__":
args = parse_arguments()
main(args)