Debian Codegraph @

scripts/mk_genfile_common.py

raw
1 # This file contains code that is common to
2 # both the Python build system and the CMake
3 # build system.
4 #
5 # The code here generally is involved in
6 # generating files needed by Z3 at build time.
7 #
8 # You should **not** import ``mk_util`` here
9 # to avoid having this code depend on the
10 # of the Python build system.
11 import io
12 import os
13 import pprint
14 import logging
15 import re
16 import sys
17
18 # Logger for this module
19 _logger = logging.getLogger(__name__)
20
21
22 ###############################################################################
23 # Utility functions
24 ###############################################################################
25 def check_dir_exists(output_dir):
26 """
27 Returns ``True`` if ``output_dir`` exists, otherwise
28 returns ``False``.
29 """
30 if not os.path.isdir(output_dir):
31 _logger.error('"{}" is not an existing directory'.format(output_dir))
32 return False
33 return True
34
35 def check_files_exist(files):
36 assert isinstance(files, list)
37 for f in files:
38 if not os.path.exists(f):
39 _logger.error('"{}" does not exist'.format(f))
40 return False
41 return True
42
43 def sorted_headers_by_component(l):
44 """
45 Take a list of header files and sort them by the
46 path after ``src/``. E.g. for ``src/ast/fpa/fpa2bv_converter.h`` the sorting
47 key is ``ast/fpa/fpa2bv_converter.h``.
48
49 The sort is done this way because for the CMake build
50 there are two directories for every component (e.g.
51 ``<src_dir>/src/ast/fpa`` and ``<build_dir>/src/ast/fpa``).
52 We don't want to sort based on different ``<src_dir>``
53 and ``<build_dir>`` prefixes so that we can match the Python build
54 system's behaviour.
55 """
56 assert isinstance(l, list)
57 def get_key(path):
58 _logger.debug("get_key({})".format(path))
59 path_components = []
60 stripped_path = path
61 assert 'src' in stripped_path.split(os.path.sep) or 'src' in stripped_path.split('/')
62 # Keep stripping off directory components until we hit ``src``
63 while os.path.basename(stripped_path) != 'src':
64 path_components.append(os.path.basename(stripped_path))
65 stripped_path = os.path.dirname(stripped_path)
66 assert len(path_components) > 0
67 path_components.reverse()
68 # For consistency across platforms use ``/`` rather than ``os.sep``.
69 # This is a sorting key so it doesn't need to a platform suitable
70 # path
71 r = '/'.join(path_components)
72 _logger.debug("return key:'{}'".format(r))
73 return r
74 sorted_headers = sorted(l, key=get_key)
75 _logger.debug('sorted headers:{}'.format(pprint.pformat(sorted_headers)))
76 return sorted_headers
77
78
79
80 ###############################################################################
81 # Functions for generating constant declarations for language bindings
82 ###############################################################################
83
84 def mk_z3consts_py_internal(api_files, output_dir):
85 """
86 Generate ``z3consts.py`` from the list of API header files
87 in ``api_files`` and write the output file into
88 the ``output_dir`` directory
89
90 Returns the path to the generated file.
91 """
92 assert os.path.isdir(output_dir)
93 assert isinstance(api_files, list)
94
95 blank_pat = re.compile("^ *\r?$")
96 comment_pat = re.compile("^ *//.*$")
97 typedef_pat = re.compile("typedef enum *")
98 typedef2_pat = re.compile("typedef enum { *")
99 openbrace_pat = re.compile("{ *")
100 closebrace_pat = re.compile("}.*;")
101
102 z3consts = open(os.path.join(output_dir, 'z3', 'z3consts.py'), 'w')
103 z3consts_output_path = z3consts.name
104 z3consts.write('# Automatically generated file\n\n')
105 for api_file in api_files:
106 api = open(api_file, 'r')
107
108 SEARCHING = 0
109 FOUND_ENUM = 1
110 IN_ENUM = 2
111
112 mode = SEARCHING
113 decls = {}
114 idx = 0
115
116 linenum = 1
117 for line in api:
118 m1 = blank_pat.match(line)
119 m2 = comment_pat.match(line)
120 if m1 or m2:
121 # skip blank lines and comments
122 linenum = linenum + 1
123 elif mode == SEARCHING:
124 m = typedef_pat.match(line)
125 if m:
126 mode = FOUND_ENUM
127 m = typedef2_pat.match(line)
128 if m:
129 mode = IN_ENUM
130 decls = {}
131 idx = 0
132 elif mode == FOUND_ENUM:
133 m = openbrace_pat.match(line)
134 if m:
135 mode = IN_ENUM
136 decls = {}
137 idx = 0
138 else:
139 assert False, "Invalid %s, line: %s" % (api_file, linenum)
140 else:
141 assert mode == IN_ENUM
142 words = re.split('[^-a-zA-Z0-9_]+', line)
143 m = closebrace_pat.match(line)
144 if m:
145 name = words[1]
146 z3consts.write('# enum %s\n' % name)
147 # Iterate over key-value pairs ordered by value
148 for k, v in sorted(decls.items(), key=lambda pair: pair[1]):
149 z3consts.write('%s = %s\n' % (k, v))
150 z3consts.write('\n')
151 mode = SEARCHING
152 elif len(words) <= 2:
153 assert False, "Invalid %s, line: %s" % (api_file, linenum)
154 else:
155 if words[2] != '':
156 if len(words[2]) > 1 and words[2][1] == 'x':
157 idx = int(words[2], 16)
158 else:
159 idx = int(words[2])
160 decls[words[1]] = idx
161 idx = idx + 1
162 linenum = linenum + 1
163 api.close()
164 z3consts.close()
165 return z3consts_output_path
166
167 def mk_z3consts_dotnet_internal(api_files, output_dir):
168 """
169 Generate ``Enumerations.cs`` from the list of API header files
170 in ``api_files`` and write the output file into
171 the ``output_dir`` directory
172
173 Returns the path to the generated file.
174 """
175 assert os.path.isdir(output_dir)
176 assert isinstance(api_files, list)
177 blank_pat = re.compile("^ *\r?$")
178 comment_pat = re.compile("^ *//.*$")
179 typedef_pat = re.compile("typedef enum *")
180 typedef2_pat = re.compile("typedef enum { *")
181 openbrace_pat = re.compile("{ *")
182 closebrace_pat = re.compile("}.*;")
183
184 DeprecatedEnums = [ 'Z3_search_failure' ]
185 z3consts = open(os.path.join(output_dir, 'Enumerations.cs'), 'w')
186 z3consts_output_path = z3consts.name
187 z3consts.write('// Automatically generated file\n\n')
188 z3consts.write('using System;\n\n'
189 '#pragma warning disable 1591\n\n'
190 'namespace Microsoft.Z3\n'
191 '{\n')
192
193 for api_file in api_files:
194 api = open(api_file, 'r')
195
196 SEARCHING = 0
197 FOUND_ENUM = 1
198 IN_ENUM = 2
199
200 mode = SEARCHING
201 decls = {}
202 idx = 0
203
204 linenum = 1
205 for line in api:
206 m1 = blank_pat.match(line)
207 m2 = comment_pat.match(line)
208 if m1 or m2:
209 # skip blank lines and comments
210 linenum = linenum + 1
211 elif mode == SEARCHING:
212 m = typedef_pat.match(line)
213 if m:
214 mode = FOUND_ENUM
215 m = typedef2_pat.match(line)
216 if m:
217 mode = IN_ENUM
218 decls = {}
219 idx = 0
220 elif mode == FOUND_ENUM:
221 m = openbrace_pat.match(line)
222 if m:
223 mode = IN_ENUM
224 decls = {}
225 idx = 0
226 else:
227 assert False, "Invalid %s, line: %s" % (api_file, linenum)
228 else:
229 assert mode == IN_ENUM
230 words = re.split('[^-a-zA-Z0-9_]+', line)
231 m = closebrace_pat.match(line)
232 if m:
233 name = words[1]
234 if name not in DeprecatedEnums:
235 z3consts.write(' /// <summary>%s</summary>\n' % name)
236 z3consts.write(' public enum %s {\n' % name)
237 z3consts.write
238 # Iterate over key-value pairs ordered by value
239 for k, v in sorted(decls.items(), key=lambda pair: pair[1]):
240 z3consts.write(' %s = %s,\n' % (k, v))
241 z3consts.write(' }\n\n')
242 mode = SEARCHING
243 elif len(words) <= 2:
244 assert False, "Invalid %s, line: %s" % (api_file, linenum)
245 else:
246 if words[2] != '':
247 if len(words[2]) > 1 and words[2][1] == 'x':
248 idx = int(words[2], 16)
249 else:
250 idx = int(words[2])
251 decls[words[1]] = idx
252 idx = idx + 1
253 linenum = linenum + 1
254 api.close()
255 z3consts.write('}\n');
256 z3consts.close()
257 return z3consts_output_path
258
259
260 def mk_z3consts_java_internal(api_files, package_name, output_dir):
261 """
262 Generate "com.microsoft.z3.enumerations" package from the list of API
263 header files in ``api_files`` and write the package directory into
264 the ``output_dir`` directory
265
266 Returns a list of the generated java source files.
267 """
268 blank_pat = re.compile("^ *$")
269 comment_pat = re.compile("^ *//.*$")
270 typedef_pat = re.compile("typedef enum *")
271 typedef2_pat = re.compile("typedef enum { *")
272 openbrace_pat = re.compile("{ *")
273 closebrace_pat = re.compile("}.*;")
274
275 DeprecatedEnums = [ 'Z3_search_failure' ]
276 gendir = os.path.join(output_dir, "enumerations")
277 if not os.path.exists(gendir):
278 os.mkdir(gendir)
279
280 generated_enumeration_files = []
281 for api_file in api_files:
282 api = open(api_file, 'r')
283
284 SEARCHING = 0
285 FOUND_ENUM = 1
286 IN_ENUM = 2
287
288 mode = SEARCHING
289 decls = {}
290 idx = 0
291
292 linenum = 1
293 for line in api:
294 m1 = blank_pat.match(line)
295 m2 = comment_pat.match(line)
296 if m1 or m2:
297 # skip blank lines and comments
298 linenum = linenum + 1
299 elif mode == SEARCHING:
300 m = typedef_pat.match(line)
301 if m:
302 mode = FOUND_ENUM
303 m = typedef2_pat.match(line)
304 if m:
305 mode = IN_ENUM
306 decls = {}
307 idx = 0
308 elif mode == FOUND_ENUM:
309 m = openbrace_pat.match(line)
310 if m:
311 mode = IN_ENUM
312 decls = {}
313 idx = 0
314 else:
315 assert False, "Invalid %s, line: %s" % (api_file, linenum)
316 else:
317 assert mode == IN_ENUM
318 words = re.split('[^-a-zA-Z0-9_]+', line)
319 m = closebrace_pat.match(line)
320 if m:
321 name = words[1]
322 if name not in DeprecatedEnums:
323 efile = open('%s.java' % os.path.join(gendir, name), 'w')
324 generated_enumeration_files.append(efile.name)
325 efile.write('/**\n * Automatically generated file\n **/\n\n')
326 efile.write('package %s.enumerations;\n\n' % package_name)
327 efile.write('import java.util.HashMap;\n')
328 efile.write('import java.util.Map;\n')
329 efile.write('\n')
330
331 efile.write('/**\n')
332 efile.write(' * %s\n' % name)
333 efile.write(' **/\n')
334 efile.write('public enum %s {\n' % name)
335 efile.write
336 first = True
337 # Iterate over key-value pairs ordered by value
338 for k, v in sorted(decls.items(), key=lambda pair: pair[1]):
339 if first:
340 first = False
341 else:
342 efile.write(',\n')
343 efile.write(' %s (%s)' % (k, v))
344 efile.write(";\n")
345 efile.write('\n private final int intValue;\n\n')
346 efile.write(' %s(int v) {\n' % name)
347 efile.write(' this.intValue = v;\n')
348 efile.write(' }\n\n')
349 efile.write(' // Cannot initialize map in constructor, so need to do it lazily.\n')
350 efile.write(' // Easiest thread-safe way is the initialization-on-demand holder pattern.\n')
351 efile.write(' private static class %s_MappingHolder {\n' % name)
352 efile.write(' private static final Map<Integer, %s> intMapping = new HashMap<>();\n' % name)
353 efile.write(' static {\n')
354 efile.write(' for (%s k : %s.values())\n' % (name, name))
355 efile.write(' intMapping.put(k.toInt(), k);\n')
356 efile.write(' }\n')
357 efile.write(' }\n\n')
358 efile.write(' public static final %s fromInt(int v) {\n' % name)
359 efile.write(' %s k = %s_MappingHolder.intMapping.get(v);\n' % (name, name))
360 efile.write(' if (k != null) return k;\n')
361 efile.write(' throw new IllegalArgumentException("Illegal value " + v + " for %s");\n' % name)
362 efile.write(' }\n\n')
363 efile.write(' public final int toInt() { return this.intValue; }\n')
364 # efile.write(';\n %s(int v) {}\n' % name)
365 efile.write('}\n\n')
366 efile.close()
367 mode = SEARCHING
368 else:
369 if words[2] != '':
370 if len(words[2]) > 1 and words[2][1] == 'x':
371 idx = int(words[2], 16)
372 else:
373 idx = int(words[2])
374 decls[words[1]] = idx
375 idx = idx + 1
376 linenum = linenum + 1
377 api.close()
378 return generated_enumeration_files
379
380 # Extract enumeration types from z3_api.h, and add ML definitions
381 def mk_z3consts_ml_internal(api_files, output_dir):
382 """
383 Generate ``z3enums.ml`` from the list of API header files
384 in ``api_files`` and write the output file into
385 the ``output_dir`` directory
386
387 Returns the path to the generated file.
388 """
389 assert os.path.isdir(output_dir)
390 assert isinstance(api_files, list)
391 blank_pat = re.compile("^ *$")
392 comment_pat = re.compile("^ *//.*$")
393 typedef_pat = re.compile("typedef enum *")
394 typedef2_pat = re.compile("typedef enum { *")
395 openbrace_pat = re.compile("{ *")
396 closebrace_pat = re.compile("}.*;")
397
398
399 DeprecatedEnums = [ 'Z3_search_failure' ]
400 if not os.path.exists(output_dir):
401 os.mkdir(output_dir)
402
403 efile = open('%s.ml' % os.path.join(output_dir, "z3enums"), 'w')
404 z3consts_output_path = efile.name
405 efile.write('(* Automatically generated file *)\n\n')
406 efile.write('(** The enumeration types of Z3. *)\n\n')
407 for api_file in api_files:
408 api = open(api_file, 'r')
409
410 SEARCHING = 0
411 FOUND_ENUM = 1
412 IN_ENUM = 2
413
414 mode = SEARCHING
415 decls = {}
416 idx = 0
417
418 linenum = 1
419 for line in api:
420 m1 = blank_pat.match(line)
421 m2 = comment_pat.match(line)
422 if m1 or m2:
423 # skip blank lines and comments
424 linenum = linenum + 1
425 elif mode == SEARCHING:
426 m = typedef_pat.match(line)
427 if m:
428 mode = FOUND_ENUM
429 m = typedef2_pat.match(line)
430 if m:
431 mode = IN_ENUM
432 decls = {}
433 idx = 0
434 elif mode == FOUND_ENUM:
435 m = openbrace_pat.match(line)
436 if m:
437 mode = IN_ENUM
438 decls = {}
439 idx = 0
440 else:
441 assert False, "Invalid %s, line: %s" % (api_file, linenum)
442 else:
443 assert mode == IN_ENUM
444 words = re.split('[^-a-zA-Z0-9_]+', line)
445 m = closebrace_pat.match(line)
446 if m:
447 name = words[1]
448 if name not in DeprecatedEnums:
449 sorted_decls = sorted(decls.items(), key=lambda pair: pair[1])
450 efile.write('(** %s *)\n' % name[3:])
451 efile.write('type %s =\n' % name[3:]) # strip Z3_
452 for k, i in sorted_decls:
453 efile.write(' | %s \n' % k[3:]) # strip Z3_
454 efile.write('\n')
455 efile.write('(** Convert %s to int*)\n' % name[3:])
456 efile.write('let int_of_%s x : int =\n' % (name[3:])) # strip Z3_
457 efile.write(' match x with\n')
458 for k, i in sorted_decls:
459 efile.write(' | %s -> %d\n' % (k[3:], i))
460 efile.write('\n')
461 efile.write('(** Convert int to %s*)\n' % name[3:])
462 efile.write('let %s_of_int x : %s =\n' % (name[3:],name[3:])) # strip Z3_
463 efile.write(' match x with\n')
464 for k, i in sorted_decls:
465 efile.write(' | %d -> %s\n' % (i, k[3:]))
466 # use Z3.Exception?
467 efile.write(' | _ -> raise (Failure "undefined enum value")\n\n')
468 mode = SEARCHING
469 else:
470 if words[2] != '':
471 if len(words[2]) > 1 and words[2][1] == 'x':
472 idx = int(words[2], 16)
473 else:
474 idx = int(words[2])
475 decls[words[1]] = idx
476 idx = idx + 1
477 linenum = linenum + 1
478 api.close()
479 efile.close()
480 return z3consts_output_path
481 # efile = open('%s.mli' % os.path.join(gendir, "z3enums"), 'w')
482 # efile.write('(* Automatically generated file *)\n\n')
483 # efile.write('(** The enumeration types of Z3. *)\n\n')
484 # for api_file in api_files:
485 # api_file_c = ml.find_file(api_file, ml.name)
486 # api_file = os.path.join(api_file_c.src_dir, api_file)
487
488 # api = open(api_file, 'r')
489
490 # SEARCHING = 0
491 # FOUND_ENUM = 1
492 # IN_ENUM = 2
493
494 # mode = SEARCHING
495 # decls = {}
496 # idx = 0
497
498 # linenum = 1
499 # for line in api:
500 # m1 = blank_pat.match(line)
501 # m2 = comment_pat.match(line)
502 # if m1 or m2:
503 # # skip blank lines and comments
504 # linenum = linenum + 1
505 # elif mode == SEARCHING:
506 # m = typedef_pat.match(line)
507 # if m:
508 # mode = FOUND_ENUM
509 # m = typedef2_pat.match(line)
510 # if m:
511 # mode = IN_ENUM
512 # decls = {}
513 # idx = 0
514 # elif mode == FOUND_ENUM:
515 # m = openbrace_pat.match(line)
516 # if m:
517 # mode = IN_ENUM
518 # decls = {}
519 # idx = 0
520 # else:
521 # assert False, "Invalid %s, line: %s" % (api_file, linenum)
522 # else:
523 # assert mode == IN_ENUM
524 # words = re.split('[^\-a-zA-Z0-9_]+', line)
525 # m = closebrace_pat.match(line)
526 # if m:
527 # name = words[1]
528 # if name not in DeprecatedEnums:
529 # efile.write('(** %s *)\n' % name[3:])
530 # efile.write('type %s =\n' % name[3:]) # strip Z3_
531 # for k, i in sorted(decls.items(), key=lambda pair: pair[1]):
532 # efile.write(' | %s \n' % k[3:]) # strip Z3_
533 # efile.write('\n')
534 # efile.write('(** Convert %s to int*)\n' % name[3:])
535 # efile.write('val int_of_%s : %s -> int\n' % (name[3:], name[3:])) # strip Z3_
536 # efile.write('(** Convert int to %s*)\n' % name[3:])
537 # efile.write('val %s_of_int : int -> %s\n' % (name[3:],name[3:])) # strip Z3_
538 # efile.write('\n')
539 # mode = SEARCHING
540 # else:
541 # if words[2] != '':
542 # if len(words[2]) > 1 and words[2][1] == 'x':
543 # idx = int(words[2], 16)
544 # else:
545 # idx = int(words[2])
546 # decls[words[1]] = idx
547 # idx = idx + 1
548 # linenum = linenum + 1
549 # api.close()
550 # efile.close()
551 # if VERBOSE:
552 # print ('Generated "%s/z3enums.mli"' % ('%s' % gendir))
553
554
555 ###############################################################################
556 # Functions for generating a "module definition file" for MSVC
557 ###############################################################################
558
559 def mk_def_file_internal(defname, dll_name, export_header_files):
560 """
561 Writes to a module definition file to a file named ``defname``.
562
563 ``dll_name`` is the name of the dll (without the ``.dll`` suffix).
564 ``export_header_file`` is a list of header files to scan for symbols
565 to include in the module definition file.
566 """
567 assert isinstance(export_header_files, list)
568 pat1 = re.compile(".*Z3_API.*")
569 fout = open(defname, 'w')
570 fout.write('LIBRARY "%s"\nEXPORTS\n' % dll_name)
571 num = 1
572 for export_header_file in export_header_files:
573 api = open(export_header_file, 'r')
574 for line in api:
575 m = pat1.match(line)
576 if m:
577 words = re.split(r'\W+', line)
578 i = 0
579 for w in words:
580 if w == 'Z3_API':
581 f = words[i+1]
582 fout.write('\t%s @%s\n' % (f, num))
583 i = i + 1
584 num = num + 1
585 api.close()
586 fout.close()
587
588 ###############################################################################
589 # Functions for generating ``gparams_register_modules.cpp``
590 ###############################################################################
591
592 def path_after_src(h_file):
593 h_file = h_file.replace("\\","/")
594 idx = h_file.rfind("src/")
595 if idx == -1:
596 return h_file
597 return h_file[idx + 4:]
598
599 def mk_gparams_register_modules_internal(h_files_full_path, path):
600 """
601 Generate a ``gparams_register_modules.cpp`` file in the directory ``path``.
602 Returns the path to the generated file.
603
604 This file implements the procedure
605
606 ```
607 void gparams_register_modules()
608 ```
609
610 This procedure is invoked by gparams::init()
611 """
612 assert isinstance(h_files_full_path, list)
613 assert check_dir_exists(path)
614 cmds = []
615 mod_cmds = []
616 mod_descrs = []
617 fullname = os.path.join(path, 'gparams_register_modules.cpp')
618 fout = open(fullname, 'w')
619 fout.write('// Automatically generated file.\n')
620 fout.write('#include "util/gparams.h"\n')
621 reg_pat = re.compile(r'[ \t]*REG_PARAMS\(\'([^\']*)\'\)')
622 reg_mod_pat = re.compile(r'[ \t]*REG_MODULE_PARAMS\(\'([^\']*)\', *\'([^\']*)\'\)')
623 reg_mod_descr_pat = re.compile(r'[ \t]*REG_MODULE_DESCRIPTION\(\'([^\']*)\', *\'([^\']*)\'\)')
624 for h_file in sorted_headers_by_component(h_files_full_path):
625 added_include = False
626 with io.open(h_file, encoding='utf-8', mode='r') as fin:
627 for line in fin:
628 m = reg_pat.match(line)
629 if m:
630 if not added_include:
631 added_include = True
632 fout.write('#include "%s"\n' % path_after_src(h_file))
633 cmds.append((m.group(1)))
634 m = reg_mod_pat.match(line)
635 if m:
636 if not added_include:
637 added_include = True
638 fout.write('#include "%s"\n' % path_after_src(h_file))
639 mod_cmds.append((m.group(1), m.group(2)))
640 m = reg_mod_descr_pat.match(line)
641 if m:
642 mod_descrs.append((m.group(1), m.group(2)))
643 fout.write('void gparams_register_modules() {\n')
644 for code in cmds:
645 fout.write('{ param_descrs d; %s(d); gparams::register_global(d); }\n' % code)
646 for (mod, code) in mod_cmds:
647 fout.write('{ auto f = []() { auto* d = alloc(param_descrs); %s(*d); return d; }; gparams::register_module("%s", f); }\n' % (code, mod))
648 for (mod, descr) in mod_descrs:
649 fout.write('gparams::register_module_descr("%s", "%s");\n' % (mod, descr))
650 fout.write('}\n')
651 fout.close()
652 return fullname
653
654 ###############################################################################
655 # Functions/data structures for generating ``install_tactics.cpp``
656 ###############################################################################
657
658 def mk_install_tactic_cpp_internal(h_files_full_path, path):
659 """
660 Generate a ``install_tactics.cpp`` file in the directory ``path``.
661 Returns the path the generated file.
662
663 This file implements the procedure
664
665 ```
666 void install_tactics(tactic_manager & ctx)
667 ```
668
669 It installs all tactics declared in the given header files
670 ``h_files_full_path`` The procedure looks for ``ADD_TACTIC`` and
671 ``ADD_PROBE``commands in the ``.h`` and ``.hpp`` files of these
672 components.
673 """
674 ADD_TACTIC_DATA = []
675 ADD_SIMPLIFIER_DATA = []
676 ADD_PROBE_DATA = []
677 def ADD_TACTIC(name, descr, cmd):
678 ADD_TACTIC_DATA.append((name, descr, cmd))
679
680 def ADD_PROBE(name, descr, cmd):
681 ADD_PROBE_DATA.append((name, descr, cmd))
682
683 def ADD_SIMPLIFIER(name, descr, cmd):
684 ADD_SIMPLIFIER_DATA.append((name, descr, cmd))
685
686 eval_globals = {
687 'ADD_TACTIC': ADD_TACTIC,
688 'ADD_PROBE': ADD_PROBE,
689 'ADD_SIMPLIFIER': ADD_SIMPLIFIER
690 }
691
692 assert isinstance(h_files_full_path, list)
693 assert check_dir_exists(path)
694 fullname = os.path.join(path, 'install_tactic.cpp')
695 fout = open(fullname, 'w')
696 fout.write('// Automatically generated file.\n')
697 fout.write('#include "tactic/tactic.h"\n')
698 fout.write('#include "cmd_context/tactic_cmds.h"\n')
699 fout.write('#include "cmd_context/simplifier_cmds.h"\n')
700 fout.write('#include "cmd_context/cmd_context.h"\n')
701 tactic_pat = re.compile(r'[ \t]*ADD_TACTIC\(.*\)')
702 probe_pat = re.compile(r'[ \t]*ADD_PROBE\(.*\)')
703 simplifier_pat = re.compile(r'[ \t]*ADD_SIMPLIFIER\(.*\)')
704 for h_file in sorted_headers_by_component(h_files_full_path):
705 added_include = False
706 try:
707 with io.open(h_file, encoding='utf-8', mode='r') as fin:
708 for line in fin:
709 if tactic_pat.match(line):
710 if not added_include:
711 added_include = True
712 fout.write('#include "%s"\n' % path_after_src(h_file))
713 try:
714 eval(line.strip('\n '), eval_globals, None)
715 except Exception as e:
716 _logger.error("Failed processing ADD_TACTIC command at '{}'\n{}".format(
717 fullname, line))
718 raise e
719 if probe_pat.match(line):
720 if not added_include:
721 added_include = True
722 fout.write('#include "%s"\n' % path_after_src(h_file))
723 try:
724 eval(line.strip('\n '), eval_globals, None)
725 except Exception as e:
726 _logger.error("Failed processing ADD_PROBE command at '{}'\n{}".format(
727 fullname, line))
728 raise e
729 if simplifier_pat.match(line):
730 if not added_include:
731 added_include = True
732 fout.write('#include "%s"\n' % path_after_src(h_file))
733 try:
734 eval(line.strip('\n '), eval_globals, None)
735 except Exception as e:
736 _logger.error("Failed processing ADD_SIMPLIFIER command at '{}'\n{}".format(
737 fullname, line))
738 raise e
739
740 except Exception as e:
741 _logger.error("Failed to read file {}\n".format(h_file))
742 raise e
743 # First pass will just generate the tactic factories
744 fout.write('#define ADD_TACTIC_CMD(NAME, DESCR, CODE) ctx.insert(alloc(tactic_cmd, symbol(NAME), DESCR, [](ast_manager &m, const params_ref &p) { return CODE; }))\n')
745 fout.write('#define ADD_PROBE(NAME, DESCR, PROBE) ctx.insert(alloc(probe_info, symbol(NAME), DESCR, PROBE))\n')
746 fout.write('#define ADD_SIMPLIFIER_CMD(NAME, DESCR, CODE) ctx.insert(alloc(simplifier_cmd, symbol(NAME), DESCR, [](auto& m, auto& p, auto &s) -> dependent_expr_simplifier* { return CODE; }))\n')
747 fout.write('void install_tactics(tactic_manager & ctx) {\n')
748 for data in ADD_TACTIC_DATA:
749 fout.write(' ADD_TACTIC_CMD("%s", "%s", %s);\n' % data)
750 for data in ADD_PROBE_DATA:
751 fout.write(' ADD_PROBE("%s", "%s", %s);\n' % data)
752 for data in ADD_SIMPLIFIER_DATA:
753 fout.write(' ADD_SIMPLIFIER_CMD("%s", "%s", %s);\n' % data)
754 fout.write('}\n')
755 fout.close()
756 return fullname
757
758 ###############################################################################
759 # Functions for generating ``mem_initializer.cpp``
760 ###############################################################################
761
762 def mk_mem_initializer_cpp_internal(h_files_full_path, path):
763 """
764 Generate a ``mem_initializer.cpp`` file in the directory ``path``.
765 Returns the path to the generated file.
766
767 This file implements the procedures
768
769 ```
770 void mem_initialize()
771 void mem_finalize()
772 ```
773
774 These procedures are invoked by the Z3 memory_manager
775 """
776 assert isinstance(h_files_full_path, list)
777 assert check_dir_exists(path)
778 initializer_cmds = []
779 finalizer_cmds = []
780 fullname = os.path.join(path, 'mem_initializer.cpp')
781 fout = open(fullname, 'w')
782 fout.write('// Automatically generated file.\n')
783 initializer_pat = re.compile(r'[ \t]*ADD_INITIALIZER\(\'([^\']*)\'\)')
784 # ADD_INITIALIZER with priority
785 initializer_prio_pat = re.compile(r'[ \t]*ADD_INITIALIZER\(\'([^\']*)\',[ \t]*(-?[0-9]*)\)')
786 finalizer_pat = re.compile(r'[ \t]*ADD_FINALIZER\(\'([^\']*)\'\)')
787 for h_file in sorted_headers_by_component(h_files_full_path):
788 added_include = False
789 with io.open(h_file, encoding='utf-8', mode='r') as fin:
790 for line in fin:
791 m = initializer_pat.match(line)
792 if m:
793 if not added_include:
794 added_include = True
795 fout.write('#include "%s"\n' % path_after_src(h_file))
796 initializer_cmds.append((m.group(1), 0))
797 m = initializer_prio_pat.match(line)
798 if m:
799 if not added_include:
800 added_include = True
801 fout.write('#include "%s"\n' % path_after_src(h_file))
802 initializer_cmds.append((m.group(1), int(m.group(2))))
803 m = finalizer_pat.match(line)
804 if m:
805 if not added_include:
806 added_include = True
807 fout.write('#include "%s"\n' % path_after_src(h_file))
808 finalizer_cmds.append(m.group(1))
809 initializer_cmds.sort(key=lambda tup: tup[1])
810 fout.write('void mem_initialize() {\n')
811 for (cmd, prio) in initializer_cmds:
812 fout.write(cmd)
813 fout.write('\n')
814 fout.write('}\n')
815 fout.write('void mem_finalize() {\n')
816 for cmd in finalizer_cmds:
817 fout.write(cmd)
818 fout.write('\n')
819 fout.write('}\n')
820 fout.close()
821 return fullname
822
823 ###############################################################################
824 # Functions for generating ``database.h``
825 ###############################################################################
826
827 def mk_pat_db_internal(inputFilePath, outputFilePath):
828 """
829 Generate ``g_pattern_database[]`` declaration header file.
830 """
831 with open(inputFilePath, 'r') as fin:
832 with open(outputFilePath, 'w') as fout:
833 fout.write('static char const g_pattern_database[] =\n')
834 for line in fin:
835 fout.write('"%s\\n"\n' % line.strip('\n'))
836 fout.write(';\n')
837
838 ###############################################################################
839 # Functions and data structures for generating ``*_params.hpp`` files from
840 # ``*.pyg`` files
841 ###############################################################################
842
843 UINT = 0
844 BOOL = 1
845 DOUBLE = 2
846 STRING = 3
847 SYMBOL = 4
848 UINT_MAX = 4294967295
849
850 TYPE2CPK = { UINT : 'CPK_UINT', BOOL : 'CPK_BOOL', DOUBLE : 'CPK_DOUBLE', STRING : 'CPK_STRING', SYMBOL : 'CPK_SYMBOL' }
851 TYPE2CTYPE = { UINT : 'unsigned', BOOL : 'bool', DOUBLE : 'double', STRING : 'char const *', SYMBOL : 'symbol' }
852 TYPE2GETTER = { UINT : 'get_uint', BOOL : 'get_bool', DOUBLE : 'get_double', STRING : 'get_str', SYMBOL : 'get_sym' }
853
854 def pyg_default(p):
855 if p[1] == BOOL:
856 if p[2]:
857 return "true"
858 else:
859 return "false"
860 return p[2]
861
862 def pyg_default_as_c_literal(p):
863 if p[1] == BOOL:
864 if p[2]:
865 return "true"
866 else:
867 return "false"
868 elif p[1] == STRING:
869 return '"%s"' % p[2]
870 elif p[1] == SYMBOL:
871 return 'symbol("%s")' % p[2]
872 elif p[1] == UINT:
873 return '%su' % p[2]
874 else:
875 return p[2]
876
877 def to_c_method(s):
878 return s.replace('.', '_')
879
880
881 def max_memory_param():
882 return ('max_memory', UINT, UINT_MAX, 'maximum amount of memory in megabytes')
883
884 def max_steps_param():
885 return ('max_steps', UINT, UINT_MAX, 'maximum number of steps')
886
887 def mk_hpp_from_pyg(pyg_file, output_dir):
888 """
889 Generates the corresponding header file for the input pyg file
890 at the path ``pyg_file``. The file is emitted into the directory
891 ``output_dir``.
892
893 Returns the path to the generated file
894 """
895 CURRENT_PYG_HPP_DEST_DIR = output_dir
896 # Note OUTPUT_HPP_FILE cannot be a string as we need a mutable variable
897 # for the nested function to modify
898 OUTPUT_HPP_FILE = [ ]
899 # The function below has been nested so that it can use closure to capture
900 # the above variables that aren't global but instead local to this
901 # function. This avoids use of global state which makes this function safer.
902 def def_module_params(module_name, export, params, class_name=None, description=None):
903 dirname = CURRENT_PYG_HPP_DEST_DIR
904 assert(os.path.exists(dirname))
905 if class_name is None:
906 class_name = '%s_params' % module_name
907 hpp = os.path.join(dirname, '%s.hpp' % class_name)
908 out = open(hpp, 'w')
909 out.write('// Automatically generated file\n')
910 out.write('#pragma once\n')
911 out.write('#include "util/params.h"\n')
912 if export:
913 out.write('#include "util/gparams.h"\n')
914 out.write('struct %s {\n' % class_name)
915 out.write(' params_ref const & p;\n')
916 if export:
917 out.write(' params_ref g;\n')
918 out.write(' %s(params_ref const & _p = params_ref::get_empty()):\n' % class_name)
919 out.write(' p(_p)')
920 if export:
921 out.write(', g(gparams::get_module("%s"))' % module_name)
922 out.write(' {}\n')
923 out.write(' static void collect_param_descrs(param_descrs & d) {\n')
924 for param in params:
925 out.write(' d.insert("%s", %s, "%s", "%s","%s");\n' % (param[0], TYPE2CPK[param[1]], param[3], pyg_default(param), module_name))
926 out.write(' }\n')
927 if export:
928 out.write(' /*\n')
929 out.write(" REG_MODULE_PARAMS('%s', '%s::collect_param_descrs')\n" % (module_name, class_name))
930 if description is not None:
931 out.write(" REG_MODULE_DESCRIPTION('%s', '%s')\n" % (module_name, description))
932 out.write(' */\n')
933 # Generated accessors
934 for param in params:
935 if export:
936 out.write(' %s %s() const { return p.%s("%s", g, %s); }\n' %
937 (TYPE2CTYPE[param[1]], to_c_method(param[0]), TYPE2GETTER[param[1]], param[0], pyg_default_as_c_literal(param)))
938 else:
939 out.write(' %s %s() const { return p.%s("%s", %s); }\n' %
940 (TYPE2CTYPE[param[1]], to_c_method(param[0]), TYPE2GETTER[param[1]], param[0], pyg_default_as_c_literal(param)))
941 out.write('};\n')
942 out.close()
943 OUTPUT_HPP_FILE.append(hpp)
944
945 # Globals to use when executing the ``.pyg`` file.
946 pyg_globals = {
947 'UINT' : UINT,
948 'BOOL' : BOOL,
949 'DOUBLE' : DOUBLE,
950 'STRING' : STRING,
951 'SYMBOL' : SYMBOL,
952 'UINT_MAX' : UINT_MAX,
953 'max_memory_param' : max_memory_param,
954 'max_steps_param' : max_steps_param,
955 # Note that once this function is entered that function
956 # executes with respect to the globals of this module and
957 # not the globals defined here
958 'def_module_params' : def_module_params,
959 }
960 with open(pyg_file, 'r') as fh:
961 eval(fh.read() + "\n", pyg_globals, None)
962 assert len(OUTPUT_HPP_FILE) == 1
963 return OUTPUT_HPP_FILE[0]