Debian Codegraph @

scripts/update_api.py

raw
1 #!/usr/bin/env python
2 ############################################
3 # Copyright (c) 2012 Microsoft Corporation
4 #
5 # Scripts for generating Makefiles and Visual
6 # Studio project files.
7 #
8 # Author: Leonardo de Moura (leonardo)
9 ############################################
10 """
11 This script generates the ``api_log_macros.h``,
12 ``api_log_macros.cpp`` and ``api_commands.cpp``
13 files for the "api" module based on parsing
14 several API header files. It can also optionally
15 emit some of the files required for Z3's different
16 language bindings.
17 """
18
19 import argparse
20 import logging
21 import re
22 import os
23 import sys
24
25 VERBOSE = True
26 def is_verbose():
27 return VERBOSE
28
29 ##########################################################
30 # TODO: rewrite this file without using global variables.
31 # This file is a big HACK.
32 # It started as small simple script.
33 # Now, it is too big, and is invoked from mk_make.py
34 #
35 ##########################################################
36
37 IN = 0
38 OUT = 1
39 INOUT = 2
40 IN_ARRAY = 3
41 OUT_ARRAY = 4
42 INOUT_ARRAY = 5
43 OUT_MANAGED_ARRAY = 6
44 FN_PTR = 7
45
46 # Primitive Types
47 VOID = 0
48 VOID_PTR = 1
49 INT = 2
50 UINT = 3
51 INT64 = 4
52 UINT64 = 5
53 STRING = 6
54 STRING_PTR = 7
55 BOOL = 8
56 SYMBOL = 9
57 PRINT_MODE = 10
58 ERROR_CODE = 11
59 DOUBLE = 12
60 FLOAT = 13
61 CHAR = 14
62 CHAR_PTR = 15
63 LBOOL = 16
64
65 FIRST_FN_ID = 50
66
67 FIRST_OBJ_ID = 100
68
69 def is_obj(ty):
70 return ty >= FIRST_OBJ_ID
71
72 def is_fn(ty):
73 return FIRST_FN_ID <= ty and ty < FIRST_OBJ_ID
74
75 Type2Str = { VOID : 'void', VOID_PTR : 'void*', INT : 'int', UINT : 'unsigned', INT64 : 'int64_t', UINT64 : 'uint64_t', DOUBLE : 'double',
76 FLOAT : 'float', STRING : 'Z3_string', STRING_PTR : 'Z3_string_ptr', BOOL : 'bool', SYMBOL : 'Z3_symbol',
77 PRINT_MODE : 'Z3_ast_print_mode', ERROR_CODE : 'Z3_error_code', CHAR: 'char', CHAR_PTR: 'Z3_char_ptr', LBOOL : 'Z3_lbool'
78 }
79
80 Type2PyStr = { VOID_PTR : 'ctypes.c_void_p', INT : 'ctypes.c_int', UINT : 'ctypes.c_uint', INT64 : 'ctypes.c_longlong',
81 UINT64 : 'ctypes.c_ulonglong', DOUBLE : 'ctypes.c_double', FLOAT : 'ctypes.c_float',
82 STRING : 'ctypes.c_char_p', STRING_PTR : 'ctypes.POINTER(ctypes.c_char_p)', BOOL : 'ctypes.c_bool', SYMBOL : 'Symbol',
83 PRINT_MODE : 'ctypes.c_uint', ERROR_CODE : 'ctypes.c_uint', CHAR : 'ctypes.c_char', CHAR_PTR: 'ctypes.POINTER(ctypes.c_char)', LBOOL : 'ctypes.c_int'
84 }
85
86 # Mapping to .NET types
87 Type2Dotnet = { VOID : 'void', VOID_PTR : 'IntPtr', INT : 'int', UINT : 'uint', INT64 : 'Int64', UINT64 : 'UInt64', DOUBLE : 'double',
88 FLOAT : 'float', STRING : 'string', STRING_PTR : 'byte**', BOOL : 'byte', SYMBOL : 'IntPtr',
89 PRINT_MODE : 'uint', ERROR_CODE : 'uint', CHAR : 'char', CHAR_PTR : 'IntPtr', LBOOL : 'int' }
90
91
92 # Mapping to ML types
93 Type2ML = { VOID : 'unit', VOID_PTR : 'ptr', INT : 'int', UINT : 'int', INT64 : 'int64', UINT64 : 'int64', DOUBLE : 'float',
94 FLOAT : 'float', STRING : 'string', STRING_PTR : 'char**',
95 BOOL : 'bool', SYMBOL : 'z3_symbol', PRINT_MODE : 'int', ERROR_CODE : 'int', CHAR : 'char', CHAR_PTR : 'string', LBOOL : 'int' }
96
97 Closures = []
98
99 class APITypes:
100 def __init__(self):
101 self.next_type_id = FIRST_OBJ_ID
102 self.next_fntype_id = FIRST_FN_ID
103
104 def def_Type(self, var, c_type, py_type):
105 """Process type definitions of the form def_Type(var, c_type, py_type)
106 The variable 'var' is set to a unique number and recorded globally using exec
107 It is used by 'def_APIs' to that uses the unique numbers to access the
108 corresponding C and Python types.
109 """
110 id = self.next_type_id
111 exec('%s = %s' % (var, id), globals())
112 Type2Str[id] = c_type
113 Type2PyStr[id] = py_type
114 self.next_type_id += 1
115
116
117 def def_Types(self, api_files):
118 global Closures
119 pat1 = re.compile(r" *def_Type\(\'(.*)\',[^\']*\'(.*)\',[^\']*\'(.*)\'\)[ \t]*")
120 pat2 = re.compile(r"Z3_DECLARE_CLOSURE\((.*),(.*), \((.*)\)\)")
121 for api_file in api_files:
122 with open(api_file, 'r') as api:
123 for line in api:
124 m = pat1.match(line)
125 if m:
126 self.def_Type(m.group(1), m.group(2), m.group(3))
127 continue
128 m = pat2.match(line)
129 if m:
130 self.fun_Type(m.group(1))
131 Closures += [(m.group(1), m.group(2), m.group(3))]
132 continue
133 #
134 # Populate object type entries in dotnet and ML bindings.
135 #
136 for k in Type2Str:
137 v = Type2Str[k]
138 if is_obj(k) or is_fn(k):
139 Type2Dotnet[k] = v
140 Type2ML[k] = v.lower()
141
142 def fun_Type(self, var):
143 """Process function type definitions"""
144 id = self.next_fntype_id
145 exec('%s = %s' % (var, id), globals())
146 Type2Str[id] = var
147 Type2PyStr[id] = var
148 self.next_fntype_id += 1
149
150
151 def type2str(ty):
152 global Type2Str
153 return Type2Str[ty]
154
155 def type2pystr(ty):
156 global Type2PyStr
157 return Type2PyStr[ty]
158
159 def type2dotnet(ty):
160 global Type2Dotnet
161 return Type2Dotnet[ty]
162
163 def type2ml(ty):
164 global Type2ML
165 q = Type2ML[ty]
166 if q[0:3] == 'z3_':
167 return q[3:]
168 else:
169 return q;
170
171 def _in(ty):
172 return (IN, ty)
173
174 def _in_array(sz, ty):
175 return (IN_ARRAY, ty, sz)
176
177 def _fnptr(ty):
178 return (FN_PTR, ty)
179
180 def _out(ty):
181 return (OUT, ty)
182
183 def _out_array(sz, ty):
184 return (OUT_ARRAY, ty, sz, sz)
185
186 # cap contains the position of the argument that stores the capacity of the array
187 # sz contains the position of the output argument that stores the (real) size of the array
188 def _out_array2(cap, sz, ty):
189 return (OUT_ARRAY, ty, cap, sz)
190
191 def _inout_array(sz, ty):
192 return (INOUT_ARRAY, ty, sz, sz)
193
194 def _out_managed_array(sz,ty):
195 return (OUT_MANAGED_ARRAY, ty, 0, sz)
196
197
198 def param_kind(p):
199 return p[0]
200
201 def param_type(p):
202 return p[1]
203
204 def param_array_capacity_pos(p):
205 return p[2]
206
207 def param_array_size_pos(p):
208 return p[3]
209
210 def param2str(p):
211 if param_kind(p) == IN_ARRAY:
212 return "%s const *" % (type2str(param_type(p)))
213 elif param_kind(p) == OUT_ARRAY or param_kind(p) == IN_ARRAY or param_kind(p) == INOUT_ARRAY:
214 return "%s*" % (type2str(param_type(p)))
215 elif param_kind(p) == OUT:
216 return "%s*" % (type2str(param_type(p)))
217 elif param_kind(p) == FN_PTR:
218 return "%s*" % (type2str(param_type(p)))
219 else:
220 return type2str(param_type(p))
221
222
223 def param2dotnet(p):
224 k = param_kind(p)
225 if k == OUT:
226 if param_type(p) == STRING:
227 return "out IntPtr"
228 else:
229 return "[In, Out] ref %s" % type2dotnet(param_type(p))
230 elif k == IN_ARRAY:
231 return "[In] %s[]" % type2dotnet(param_type(p))
232 elif k == INOUT_ARRAY:
233 return "[In, Out] %s[]" % type2dotnet(param_type(p))
234 elif k == OUT_ARRAY:
235 return "[Out] %s[]" % type2dotnet(param_type(p))
236 elif k == OUT_MANAGED_ARRAY:
237 return "[Out] out %s[]" % type2dotnet(param_type(p))
238 else:
239 return type2dotnet(param_type(p))
240
241
242 # --------------
243
244 def param2pystr(p):
245 if param_kind(p) == IN_ARRAY or param_kind(p) == OUT_ARRAY or param_kind(p) == IN_ARRAY or param_kind(p) == INOUT_ARRAY or param_kind(p) == OUT:
246 return "ctypes.POINTER(%s)" % type2pystr(param_type(p))
247 else:
248 return type2pystr(param_type(p))
249
250 # --------------
251 # ML
252
253 def param2ml(p):
254 k = param_kind(p)
255 if k == OUT:
256 if param_type(p) == INT or param_type(p) == UINT or param_type(p) == BOOL:
257 return "int"
258 elif param_type(p) == INT64 or param_type(p) == UINT64:
259 return "int64"
260 elif param_type(p) == STRING:
261 return "string"
262 else:
263 return "ptr"
264 elif k == IN_ARRAY or k == INOUT_ARRAY or k == OUT_ARRAY:
265 return "%s list" % type2ml(param_type(p))
266 elif k == OUT_MANAGED_ARRAY:
267 return "%s list" % type2ml(param_type(p))
268 else:
269 return type2ml(param_type(p))
270
271 # Save name, result, params to generate wrapper
272 _API2PY = []
273
274 def mk_py_binding(name, result, params):
275 global core_py
276 global _API2PY
277 _API2PY.append((name, result, params))
278 if result != VOID:
279 core_py.write("_lib.%s.restype = %s\n" % (name, type2pystr(result)))
280 core_py.write("_lib.%s.argtypes = [" % name)
281 first = True
282 for p in params:
283 if first:
284 first = False
285 else:
286 core_py.write(", ")
287 core_py.write(param2pystr(p))
288 core_py.write("]\n")
289
290 def extra_API(name, result, params):
291 mk_py_binding(name, result, params)
292 reg_dotnet(name, result, params)
293
294 def display_args(num):
295 for i in range(num):
296 if i > 0:
297 core_py.write(", ")
298 core_py.write("a%s" % i)
299
300 def display_args_to_z3(params):
301 i = 0
302 for p in params:
303 if i > 0:
304 core_py.write(", ")
305 if param_type(p) == STRING:
306 core_py.write("_str_to_bytes(a%s)" % i)
307 else:
308 core_py.write("a%s" % i)
309 i = i + 1
310
311 NULLWrapped = [ 'Z3_mk_context', 'Z3_mk_context_rc' ]
312 Unwrapped = [ 'Z3_del_context', 'Z3_get_error_code' ]
313 Unchecked = frozenset([ 'Z3_dec_ref', 'Z3_params_dec_ref', 'Z3_model_dec_ref',
314 'Z3_func_interp_dec_ref', 'Z3_func_entry_dec_ref',
315 'Z3_goal_dec_ref', 'Z3_tactic_dec_ref', 'Z3_simplifier_dec_ref', 'Z3_probe_dec_ref',
316 'Z3_fixedpoint_dec_ref', 'Z3_param_descrs_dec_ref',
317 'Z3_ast_vector_dec_ref', 'Z3_ast_map_dec_ref',
318 'Z3_apply_result_dec_ref', 'Z3_solver_dec_ref',
319 'Z3_stats_dec_ref', 'Z3_optimize_dec_ref'])
320
321 def mk_py_wrappers():
322 core_py.write("""
323 class Elementaries:
324 def __init__(self, f):
325 self.f = f
326 self.get_error_code = _lib.Z3_get_error_code
327 self.get_error_message = _lib.Z3_get_error_msg
328 self.OK = Z3_OK
329 self.Exception = Z3Exception
330
331 def Check(self, ctx):
332 err = self.get_error_code(ctx)
333 if err != self.OK:
334 raise self.Exception(self.get_error_message(ctx, err))
335
336 def Z3_set_error_handler(ctx, hndlr, _elems=Elementaries(_lib.Z3_set_error_handler)):
337 ceh = _error_handler_type(hndlr)
338 _elems.f(ctx, ceh)
339 _elems.Check(ctx)
340 return ceh
341
342 def Z3_solver_register_on_clause(ctx, s, user_ctx, on_clause_eh, _elems = Elementaries(_lib.Z3_solver_register_on_clause)):
343 _elems.f(ctx, s, user_ctx, on_clause_eh)
344 _elems.Check(ctx)
345
346 def Z3_solver_propagate_init(ctx, s, user_ctx, push_eh, pop_eh, fresh_eh, _elems = Elementaries(_lib.Z3_solver_propagate_init)):
347 _elems.f(ctx, s, user_ctx, push_eh, pop_eh, fresh_eh)
348 _elems.Check(ctx)
349
350 def Z3_solver_propagate_final(ctx, s, final_eh, _elems = Elementaries(_lib.Z3_solver_propagate_final)):
351 _elems.f(ctx, s, final_eh)
352 _elems.Check(ctx)
353
354 def Z3_solver_propagate_fixed(ctx, s, fixed_eh, _elems = Elementaries(_lib.Z3_solver_propagate_fixed)):
355 _elems.f(ctx, s, fixed_eh)
356 _elems.Check(ctx)
357
358 def Z3_solver_propagate_eq(ctx, s, eq_eh, _elems = Elementaries(_lib.Z3_solver_propagate_eq)):
359 _elems.f(ctx, s, eq_eh)
360 _elems.Check(ctx)
361
362 def Z3_solver_propagate_diseq(ctx, s, diseq_eh, _elems = Elementaries(_lib.Z3_solver_propagate_diseq)):
363 _elems.f(ctx, s, diseq_eh)
364 _elems.Check(ctx)
365
366 def Z3_optimize_register_model_eh(ctx, o, m, user_ctx, on_model_eh, _elems = Elementaries(_lib.Z3_optimize_register_model_eh)):
367 _elems.f(ctx, o, m, user_ctx, on_model_eh)
368 _elems.Check(ctx)
369
370 """)
371
372 for sig in _API2PY:
373 mk_py_wrapper_single(sig)
374 if sig[1] == STRING:
375 mk_py_wrapper_single(sig, decode_string=False)
376
377 def mk_py_wrapper_single(sig, decode_string=True):
378 name = sig[0]
379 result = sig[1]
380 params = sig[2]
381 num = len(params)
382 def_name = name
383 if not decode_string:
384 def_name += '_bytes'
385 core_py.write("def %s(" % def_name)
386 display_args(num)
387 comma = ", " if num != 0 else ""
388 core_py.write("%s_elems=Elementaries(_lib.%s)):\n" % (comma, name))
389 lval = "r = " if result != VOID else ""
390 core_py.write(" %s_elems.f(" % lval)
391 display_args_to_z3(params)
392 core_py.write(")\n")
393 if len(params) > 0 and param_type(params[0]) == CONTEXT and not name in Unwrapped and not name in Unchecked:
394 core_py.write(" _elems.Check(a0)\n")
395 if result == STRING and decode_string:
396 core_py.write(" return _to_pystr(r)\n")
397 elif result != VOID:
398 core_py.write(" return r\n")
399 core_py.write("\n")
400
401
402 ## .NET API native interface
403 _dotnet_decls = []
404 def reg_dotnet(name, result, params):
405 global _dotnet_decls
406 _dotnet_decls.append((name, result, params))
407
408 def mk_dotnet(dotnet):
409 global Type2Str
410 dotnet.write('// Automatically generated file\n')
411 dotnet.write('using System;\n')
412 dotnet.write('using System.Collections.Generic;\n')
413 dotnet.write('using System.Text;\n')
414 dotnet.write('using System.Runtime.InteropServices;\n\n')
415 dotnet.write('#pragma warning disable 1591\n\n')
416 dotnet.write('namespace Microsoft.Z3\n')
417 dotnet.write('{\n')
418 for k in Type2Str:
419 v = Type2Str[k]
420 if is_obj(k):
421 dotnet.write(' using %s = System.IntPtr;\n' % v)
422
423 dotnet.write(' using voidp = System.IntPtr;\n')
424 dotnet.write('\n')
425 dotnet.write(' public class Native\n')
426 dotnet.write(' {\n\n')
427
428 for name, ret, sig in Closures:
429 sig = sig.replace("unsigned const*","uint[]")
430 sig = sig.replace("void*","voidp").replace("unsigned","uint")
431 sig = sig.replace("Z3_ast*","ref IntPtr").replace("uint*","ref uint").replace("Z3_lbool*","ref int")
432 ret = ret.replace("void*","voidp").replace("unsigned","uint")
433 if "*" in sig or "*" in ret:
434 continue
435 dotnet.write(' [UnmanagedFunctionPointer(CallingConvention.Cdecl)]\n')
436 dotnet.write(' public delegate %s %s(%s);\n' % (ret,name,sig))
437
438 dotnet.write(' public class LIB\n')
439 dotnet.write(' {\n')
440 dotnet.write(' const string Z3_DLL_NAME = \"libz3\";\n'
441 ' \n')
442 dotnet.write(' [DllImport(Z3_DLL_NAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]\n')
443 dotnet.write(' public extern static void Z3_set_error_handler(Z3_context a0, Z3_error_handler a1);\n\n')
444 for name, result, params in _dotnet_decls:
445 dotnet.write(' [DllImport(Z3_DLL_NAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]\n')
446 dotnet.write(' ')
447 if result == STRING:
448 dotnet.write('public extern static IntPtr %s(' % (name))
449 else:
450 dotnet.write('public extern static %s %s(' % (type2dotnet(result), name))
451 first = True
452 i = 0
453 for param in params:
454 if first:
455 first = False
456 else:
457 dotnet.write(', ')
458 dotnet.write('%s a%d' % (param2dotnet(param), i))
459 i = i + 1
460 dotnet.write(');\n\n')
461 dotnet.write(' }\n')
462
463 def mk_dotnet_wrappers(dotnet):
464 global Type2Str
465 dotnet.write("\n")
466 dotnet.write(" public static void Z3_set_error_handler(Z3_context a0, Z3_error_handler a1) {\n")
467 dotnet.write(" LIB.Z3_set_error_handler(a0, a1);\n")
468 dotnet.write(" Z3_error_code err = (Z3_error_code)LIB.Z3_get_error_code(a0);\n")
469 dotnet.write(" if (err != Z3_error_code.Z3_OK)\n")
470 dotnet.write(" throw new Z3Exception(Marshal.PtrToStringAnsi(LIB.Z3_get_error_msg(a0, (uint)err)));\n")
471 dotnet.write(" }\n\n")
472 for name, result, params in _dotnet_decls:
473 if result == STRING:
474 dotnet.write(' public static string %s(' % (name))
475 else:
476 dotnet.write(' public static %s %s(' % (type2dotnet(result), name))
477 first = True
478 i = 0
479 for param in params:
480 if first:
481 first = False
482 else:
483 dotnet.write(', ')
484 dotnet.write('%s a%d' % (param2dotnet(param), i))
485 i = i + 1
486 dotnet.write(') {\n')
487 dotnet.write(' ')
488 if result == STRING:
489 dotnet.write('IntPtr r = ')
490 elif result != VOID:
491 dotnet.write('%s r = ' % type2dotnet(result))
492 dotnet.write('LIB.%s(' % (name))
493 first = True
494 i = 0
495 for param in params:
496 if first:
497 first = False
498 else:
499 dotnet.write(', ')
500 if param_kind(param) == OUT:
501 if param_type(param) == STRING:
502 dotnet.write('out ')
503 else:
504 dotnet.write('ref ')
505 elif param_kind(param) == OUT_MANAGED_ARRAY:
506 dotnet.write('out ')
507 dotnet.write('a%d' % i)
508 i = i + 1
509 dotnet.write(');\n')
510 if name not in Unwrapped:
511 if name in NULLWrapped:
512 dotnet.write(" if (r == IntPtr.Zero)\n")
513 dotnet.write(" throw new Z3Exception(\"Object allocation failed.\");\n")
514 else:
515 if len(params) > 0 and param_type(params[0]) == CONTEXT and name not in Unchecked:
516 dotnet.write(" Z3_error_code err = (Z3_error_code)LIB.Z3_get_error_code(a0);\n")
517 dotnet.write(" if (err != Z3_error_code.Z3_OK)\n")
518 dotnet.write(" throw new Z3Exception(Marshal.PtrToStringAnsi(LIB.Z3_get_error_msg(a0, (uint)err)));\n")
519 if result == STRING:
520 dotnet.write(" return Marshal.PtrToStringAnsi(r);\n")
521 elif result != VOID:
522 dotnet.write(" return r;\n")
523 dotnet.write(" }\n\n")
524 dotnet.write(" }\n\n")
525 dotnet.write("}\n\n")
526
527 # ----------------------
528 # Java
529
530 Type2Java = { VOID : 'void', VOID_PTR : 'long', INT : 'int', UINT : 'int', INT64 : 'long', UINT64 : 'long', DOUBLE : 'double',
531 FLOAT : 'float', STRING : 'String', STRING_PTR : 'StringPtr',
532 BOOL : 'boolean', SYMBOL : 'long', PRINT_MODE : 'int', ERROR_CODE : 'int', CHAR : 'char', CHAR_PTR : 'long', LBOOL : 'int' }
533
534 Type2JavaW = { VOID : 'void', VOID_PTR : 'jlong', INT : 'jint', UINT : 'jint', INT64 : 'jlong', UINT64 : 'jlong', DOUBLE : 'jdouble',
535 FLOAT : 'jfloat', STRING : 'jstring', STRING_PTR : 'jobject',
536 BOOL : 'jboolean', SYMBOL : 'jlong', PRINT_MODE : 'jint', ERROR_CODE : 'jint', CHAR : 'jchar', CHAR_PTR : 'jlong', LBOOL : 'jint'}
537
538 def type2java(ty):
539 global Type2Java
540 if (ty >= FIRST_FN_ID):
541 return 'long'
542 else:
543 return Type2Java[ty]
544
545 def type2javaw(ty):
546 global Type2JavaW
547 if (ty >= FIRST_FN_ID):
548 return 'jlong'
549 else:
550 return Type2JavaW[ty]
551
552 def param2java(p):
553 k = param_kind(p)
554 if k == OUT:
555 if param_type(p) == INT or param_type(p) == UINT:
556 return "IntPtr"
557 elif param_type(p) == INT64 or param_type(p) == UINT64 or param_type(p) == VOID_PTR or param_type(p) >= FIRST_OBJ_ID:
558 return "LongPtr"
559 elif param_type(p) == STRING:
560 return "StringPtr"
561 else:
562 print("ERROR: unreachable code")
563 assert(False)
564 exit(1)
565 elif k == IN_ARRAY or k == INOUT_ARRAY or k == OUT_ARRAY:
566 return "%s[]" % type2java(param_type(p))
567 elif k == OUT_MANAGED_ARRAY:
568 if param_type(p) == UINT:
569 return "UIntArrayPtr"
570 else:
571 return "ObjArrayPtr"
572 elif k == FN_PTR:
573 return "LongPtr"
574 else:
575 return type2java(param_type(p))
576
577 def param2javaw(p):
578 k = param_kind(p)
579 if k == OUT:
580 return "jobject"
581 elif k == IN_ARRAY or k == INOUT_ARRAY or k == OUT_ARRAY:
582 if param_type(p) == INT or param_type(p) == UINT or param_type(p) == BOOL:
583 return "jintArray"
584 else:
585 return "jlongArray"
586 elif k == OUT_MANAGED_ARRAY:
587 return "jlong"
588 else:
589 return type2javaw(param_type(p))
590
591 def java_method_name(name):
592 result = ''
593 name = name[3:] # Remove Z3_
594 n = len(name)
595 i = 0
596 while i < n:
597 if name[i] == '_':
598 i = i + 1
599 if i < n:
600 result += name[i].upper()
601 else:
602 result += name[i]
603 i = i + 1
604 return result
605
606 # Return the type of the java array elements
607 def java_array_element_type(p):
608 if param_type(p) == INT or param_type(p) == UINT or param_type(p) == BOOL:
609 return 'jint'
610 else:
611 return 'jlong'
612
613 def mk_java(java_src, java_dir, package_name):
614 java_nativef = os.path.join(java_dir, 'Native.java')
615 java_wrapperf = os.path.join(java_dir, 'Native.cpp')
616 java_native = open(java_nativef, 'w')
617 java_native.write('// Automatically generated file\n')
618 java_native.write('package %s;\n' % package_name)
619 java_native.write('import %s.enumerations.*;\n' % package_name)
620 java_native.write('public final class Native {\n')
621 java_native.write(' public static class IntPtr { public int value; }\n')
622 java_native.write(' public static class LongPtr { public long value; }\n')
623 java_native.write(' public static class StringPtr { public String value; }\n')
624 java_native.write(' public static class ObjArrayPtr { public long[] value; }\n')
625 java_native.write(' public static class UIntArrayPtr { public int[] value; }\n')
626 java_native.write(' public static native void setInternalErrorHandler(long ctx);\n\n')
627
628 java_native.write(' static {\n')
629 java_native.write(' if (!Boolean.parseBoolean(System.getProperty("z3.skipLibraryLoad"))) {\n')
630 java_native.write(' try {\n')
631 java_native.write(' System.loadLibrary("z3java");\n')
632 java_native.write(' } catch (UnsatisfiedLinkError ex) {\n')
633 java_native.write(' System.loadLibrary("libz3java");\n')
634 java_native.write(' }\n')
635 java_native.write(' }\n')
636 java_native.write(' }\n')
637 java_native.write("""
638 public static native long propagateInit(Object o, long ctx, long solver);
639 public static native void propagateRegisterCreated(Object o, long ctx, long solver);
640 public static native void propagateRegisterFixed(Object o, long ctx, long solver);
641 public static native void propagateRegisterEq(Object o, long ctx, long solver);
642 public static native void propagateRegisterDecide(Object o, long ctx, long solver);
643 public static native void propagateRegisterFinal(Object o, long ctx, long solver);
644 public static native void propagateAdd(Object o, long ctx, long solver, long javainfo, long e);
645 public static native boolean propagateConsequence(Object o, long ctx, long solver, long javainfo, int num_fixed, long[] fixed, long num_eqs, long[] eq_lhs, long[] eq_rhs, long conseq);
646 public static native boolean propagateNextSplit(Object o, long ctx, long solver, long javainfo, long e, long idx, int phase);
647 public static native void propagateDestroy(Object o, long ctx, long solver, long javainfo);
648
649 public static abstract class UserPropagatorBase implements AutoCloseable {
650 protected long ctx;
651 protected long solver;
652 protected long javainfo;
653
654 public UserPropagatorBase(long _ctx, long _solver) {
655 ctx = _ctx;
656 solver = _solver;
657 javainfo = propagateInit(this, ctx, solver);
658 }
659
660 @Override
661 public void close() {
662 Native.propagateDestroy(this, ctx, solver, javainfo);
663 javainfo = 0;
664 solver = 0;
665 ctx = 0;
666 }
667
668 protected final void registerCreated() {
669 Native.propagateRegisterCreated(this, ctx, solver);
670 }
671
672 protected final void registerFixed() {
673 Native.propagateRegisterFixed(this, ctx, solver);
674 }
675
676 protected final void registerEq() {
677 Native.propagateRegisterEq(this, ctx, solver);
678 }
679
680 protected final void registerDecide() {
681 Native.propagateRegisterDecide(this, ctx, solver);
682 }
683
684 protected final void registerFinal() {
685 Native.propagateRegisterFinal(this, ctx, solver);
686 }
687
688 protected abstract void pushWrapper();
689
690 protected abstract void popWrapper(int number);
691
692 protected abstract void finWrapper();
693
694 protected abstract void eqWrapper(long lx, long ly);
695
696 protected abstract UserPropagatorBase freshWrapper(long lctx);
697
698 protected abstract void createdWrapper(long le);
699
700 protected abstract void fixedWrapper(long lvar, long lvalue);
701
702 protected abstract void decideWrapper(long lvar, int bit, boolean is_pos);
703 }
704 """)
705 java_native.write('\n')
706 for name, result, params in _dotnet_decls:
707 java_native.write(' protected static native %s INTERNAL%s(' % (type2java(result), java_method_name(name)))
708 first = True
709 i = 0
710 for param in params:
711 if first:
712 first = False
713 else:
714 java_native.write(', ')
715 java_native.write('%s a%d' % (param2java(param), i))
716 i = i + 1
717 java_native.write(');\n')
718 java_native.write('\n\n')
719 # Exception wrappers
720 for name, result, params in _dotnet_decls:
721 java_native.write(' public static %s %s(' % (type2java(result), java_method_name(name)))
722 first = True
723 i = 0
724 for param in params:
725 if first:
726 first = False
727 else:
728 java_native.write(', ')
729 java_native.write('%s a%d' % (param2java(param), i))
730 i = i + 1
731 java_native.write(')')
732 if (len(params) > 0 and param_type(params[0]) == CONTEXT) or name in NULLWrapped:
733 java_native.write(' throws Z3Exception')
734 java_native.write('\n')
735 java_native.write(' {\n')
736 java_native.write(' ')
737 if result != VOID:
738 java_native.write('%s res = ' % type2java(result))
739 java_native.write('INTERNAL%s(' % (java_method_name(name)))
740 first = True
741 i = 0
742 for param in params:
743 if first:
744 first = False
745 else:
746 java_native.write(', ')
747 java_native.write('a%d' % i)
748 i = i + 1
749 java_native.write(');\n')
750 if name not in Unwrapped:
751 if name in NULLWrapped:
752 java_native.write(" if (res == 0)\n")
753 java_native.write(" throw new Z3Exception(\"Object allocation failed.\");\n")
754 else:
755 if len(params) > 0 and param_type(params[0]) == CONTEXT and name not in Unchecked:
756 java_native.write(' Z3_error_code err = Z3_error_code.fromInt(INTERNALgetErrorCode(a0));\n')
757 java_native.write(' if (err != Z3_error_code.Z3_OK)\n')
758 java_native.write(' throw new Z3Exception(INTERNALgetErrorMsg(a0, err.toInt()));\n')
759 if result != VOID:
760 java_native.write(' return res;\n')
761 java_native.write(' }\n\n')
762 java_native.write('}\n')
763 java_wrapper = open(java_wrapperf, 'w')
764 pkg_str = package_name.replace('.', '_')
765 java_wrapper.write("// Automatically generated file\n")
766 with open(java_src + "/NativeStatic.txt") as ins:
767 for line in ins:
768 java_wrapper.write(line)
769 for name, result, params in _dotnet_decls:
770 java_wrapper.write('DLL_VIS JNIEXPORT %s JNICALL Java_%s_Native_INTERNAL%s(JNIEnv * jenv, jclass cls' % (type2javaw(result), pkg_str, java_method_name(name)))
771 i = 0
772 for param in params:
773 java_wrapper.write(', ')
774 java_wrapper.write('%s a%d' % (param2javaw(param), i))
775 i = i + 1
776 java_wrapper.write(') {\n')
777 # preprocess arrays, strings, in/out arguments
778 i = 0
779 for param in params:
780 k = param_kind(param)
781 if k == OUT or k == INOUT:
782 java_wrapper.write(' %s _a%s;\n' % (type2str(param_type(param)), i))
783 elif k == IN_ARRAY or k == INOUT_ARRAY:
784 if param_type(param) == INT or param_type(param) == UINT or param_type(param) == BOOL:
785 java_wrapper.write(' %s * _a%s = (%s*) jenv->GetIntArrayElements(a%s, NULL);\n' % (type2str(param_type(param)), i, type2str(param_type(param)), i))
786 else:
787 java_wrapper.write(' GETLONGAELEMS(%s, a%s, _a%s);\n' % (type2str(param_type(param)), i, i))
788 elif k == OUT_ARRAY:
789 java_wrapper.write(' %s * _a%s = (%s *) malloc(((unsigned)a%s) * sizeof(%s));\n' % (type2str(param_type(param)),
790 i,
791 type2str(param_type(param)),
792 param_array_capacity_pos(param),
793 type2str(param_type(param))))
794 if param_type(param) == INT or param_type(param) == UINT or param_type(param) == BOOL:
795 java_wrapper.write(' jenv->GetIntArrayRegion(a%s, 0, (jsize)a%s, (jint*)_a%s);\n' % (i, param_array_capacity_pos(param), i))
796 else:
797 java_wrapper.write(' GETLONGAREGION(%s, a%s, 0, a%s, _a%s);\n' % (type2str(param_type(param)), i, param_array_capacity_pos(param), i))
798 elif k == IN and param_type(param) == STRING:
799 java_wrapper.write(' Z3_string _a%s = (Z3_string) jenv->GetStringUTFChars(a%s, NULL);\n' % (i, i))
800 elif k == OUT_MANAGED_ARRAY:
801 java_wrapper.write(' %s * _a%s = 0;\n' % (type2str(param_type(param)), i))
802 i = i + 1
803 # invoke procedure
804 java_wrapper.write(' ')
805 if result != VOID:
806 java_wrapper.write('%s result = ' % type2str(result))
807 java_wrapper.write('%s(' % name)
808 i = 0
809 first = True
810 for param in params:
811 if first:
812 first = False
813 else:
814 java_wrapper.write(', ')
815 k = param_kind(param)
816 if k == OUT or k == INOUT:
817 java_wrapper.write('&_a%s' % i)
818 elif k == OUT_ARRAY or k == IN_ARRAY or k == INOUT_ARRAY:
819 java_wrapper.write('_a%s' % i)
820 elif k == OUT_MANAGED_ARRAY:
821 java_wrapper.write('&_a%s' % i)
822 elif k == IN and param_type(param) == STRING:
823 java_wrapper.write('_a%s' % i)
824 else:
825 java_wrapper.write('(%s)a%i' % (param2str(param), i))
826 i = i + 1
827 java_wrapper.write(');\n')
828 # cleanup
829 i = 0
830 for param in params:
831 k = param_kind(param)
832 if k == OUT_ARRAY:
833 if param_type(param) == INT or param_type(param) == UINT or param_type(param) == BOOL:
834 java_wrapper.write(' jenv->SetIntArrayRegion(a%s, 0, (jsize)a%s, (jint*)_a%s);\n' % (i, param_array_capacity_pos(param), i))
835 else:
836 java_wrapper.write(' SETLONGAREGION(a%s, 0, a%s, _a%s);\n' % (i, param_array_capacity_pos(param), i))
837 java_wrapper.write(' free(_a%s);\n' % i)
838 elif k == IN_ARRAY or k == OUT_ARRAY:
839 if param_type(param) == INT or param_type(param) == UINT or param_type(param) == BOOL:
840 java_wrapper.write(' jenv->ReleaseIntArrayElements(a%s, (jint*)_a%s, JNI_ABORT);\n' % (i, i))
841 else:
842 java_wrapper.write(' RELEASELONGAELEMS(a%s, _a%s);\n' % (i, i))
843
844 elif k == OUT or k == INOUT:
845 if param_type(param) == INT or param_type(param) == UINT or param_type(param) == BOOL:
846 java_wrapper.write(' {\n')
847 java_wrapper.write(' jclass mc = jenv->GetObjectClass(a%s);\n' % i)
848 java_wrapper.write(' jfieldID fid = jenv->GetFieldID(mc, "value", "I");\n')
849 java_wrapper.write(' jenv->SetIntField(a%s, fid, (jint) _a%s);\n' % (i, i))
850 java_wrapper.write(' }\n')
851 elif param_type(param) == STRING:
852 java_wrapper.write(' {\n')
853 java_wrapper.write(' jclass mc = jenv->GetObjectClass(a%s);\n' % i)
854 java_wrapper.write(' jfieldID fid = jenv->GetFieldID(mc, "value", "Ljava/lang/String;");')
855 java_wrapper.write(' jstring fval = jenv->NewStringUTF(_a%s);\n' % i)
856 java_wrapper.write(' jenv->SetObjectField(a%s, fid, fval);\n' % i)
857 java_wrapper.write(' }\n')
858 else:
859 java_wrapper.write(' {\n')
860 java_wrapper.write(' jclass mc = jenv->GetObjectClass(a%s);\n' % i)
861 java_wrapper.write(' jfieldID fid = jenv->GetFieldID(mc, "value", "J");\n')
862 java_wrapper.write(' jenv->SetLongField(a%s, fid, (jlong) _a%s);\n' % (i, i))
863 java_wrapper.write(' }\n')
864 elif k == OUT_MANAGED_ARRAY:
865 java_wrapper.write(' *(jlong**)a%s = (jlong*)_a%s;\n' % (i, i))
866
867 elif k == IN and param_type(param) == STRING:
868 java_wrapper.write(' jenv->ReleaseStringUTFChars(a%s, _a%s);\n' % (i, i));
869 i = i + 1
870 # return
871 if result == STRING:
872 java_wrapper.write(' return jenv->NewStringUTF(result);\n')
873 elif result != VOID:
874 java_wrapper.write(' return (%s) result;\n' % type2javaw(result))
875 java_wrapper.write('}\n')
876 java_wrapper.write('#ifdef __cplusplus\n')
877 java_wrapper.write('}\n')
878 java_wrapper.write('#endif\n')
879 if is_verbose():
880 print("Generated '%s'" % java_nativef)
881
882 def mk_log_header(file, name, params):
883 file.write("void log_%s(" % name)
884 i = 0
885 for p in params:
886 if i > 0:
887 file.write(", ")
888 file.write("%s a%s" % (param2str(p), i))
889 i = i + 1
890 file.write(")")
891
892 # ---------------------------------
893 # Logging
894
895
896 def log_param(p):
897 kind = param_kind(p)
898 ty = param_type(p)
899 return is_obj(ty) and (kind == OUT or kind == INOUT or kind == OUT_ARRAY or kind == INOUT_ARRAY)
900
901 def log_result(result, params):
902 for p in params:
903 if log_param(p):
904 return True
905 return False
906
907 def mk_log_macro(file, name, params):
908 file.write("#define LOG_%s(" % name)
909 i = 0
910 for p in params:
911 if i > 0:
912 file.write(", ")
913 file.write("_ARG%s" % i)
914 i = i + 1
915 file.write(") z3_log_ctx _LOG_CTX; ")
916 auxs = set()
917 i = 0
918 for p in params:
919 if log_param(p):
920 kind = param_kind(p)
921 if kind == OUT_ARRAY or kind == INOUT_ARRAY:
922 cap = param_array_capacity_pos(p)
923 if cap not in auxs:
924 auxs.add(cap)
925 file.write("unsigned _Z3_UNUSED Z3ARG%s = 0; " % cap)
926 sz = param_array_size_pos(p)
927 if sz not in auxs:
928 auxs.add(sz)
929 file.write("unsigned * _Z3_UNUSED Z3ARG%s = 0; " % sz)
930 file.write("%s _Z3_UNUSED Z3ARG%s = 0; " % (param2str(p), i))
931 i = i + 1
932 file.write("if (_LOG_CTX.enabled()) { log_%s(" % name)
933 i = 0
934 for p in params:
935 if (i > 0):
936 file.write(', ')
937 file.write("_ARG%s" %i)
938 i = i + 1
939 file.write("); ")
940 auxs = set()
941 i = 0
942 for p in params:
943 if log_param(p):
944 kind = param_kind(p)
945 if kind == OUT_ARRAY or kind == INOUT_ARRAY:
946 cap = param_array_capacity_pos(p)
947 if cap not in auxs:
948 auxs.add(cap)
949 file.write("Z3ARG%s = _ARG%s; " % (cap, cap))
950 sz = param_array_size_pos(p)
951 if sz not in auxs:
952 auxs.add(sz)
953 file.write("Z3ARG%s = _ARG%s; " % (sz, sz))
954 file.write("Z3ARG%s = _ARG%s; " % (i, i))
955 i = i + 1
956 file.write("}\n")
957
958 def mk_log_result_macro(file, name, result, params):
959 file.write("#define RETURN_%s" % name)
960 if is_obj(result):
961 file.write("(Z3RES)")
962 file.write(" ")
963 file.write("if (_LOG_CTX.enabled()) { ")
964 if is_obj(result):
965 file.write("SetR(Z3RES); ")
966 i = 0
967 for p in params:
968 if log_param(p):
969 kind = param_kind(p)
970 if kind == OUT_ARRAY or kind == INOUT_ARRAY:
971 cap = param_array_capacity_pos(p)
972 sz = param_array_size_pos(p)
973 if cap == sz:
974 file.write("for (unsigned i = 0; i < Z3ARG%s; i++) { SetAO(Z3ARG%s[i], %s, i); } " % (sz, i, i))
975 else:
976 file.write("for (unsigned i = 0; Z3ARG%s && i < *Z3ARG%s; i++) { SetAO(Z3ARG%s[i], %s, i); } " % (sz, sz, i, i))
977 if kind == OUT or kind == INOUT:
978 file.write("SetO((Z3ARG%s == 0 ? 0 : *Z3ARG%s), %s); " % (i, i, i))
979 i = i + 1
980 file.write("} ")
981 if is_obj(result):
982 file.write("return Z3RES\n")
983 else:
984 file.write("return\n")
985
986 def mk_exec_header(file, name):
987 file.write("void exec_%s(z3_replayer & in)" % name)
988
989 def error(msg):
990 sys.stderr.write(msg)
991 exit(-1)
992
993 next_id = 0
994 API2Id = {}
995
996 def def_API(name, result, params):
997 global API2Id, next_id
998 global log_h, log_c
999 mk_py_binding(name, result, params)
1000 reg_dotnet(name, result, params)
1001 API2Id[next_id] = name
1002 mk_log_header(log_h, name, params)
1003 log_h.write(';\n')
1004 mk_log_header(log_c, name, params)
1005 log_c.write(' {\n R();\n')
1006 mk_exec_header(exe_c, name)
1007 exe_c.write(' {\n')
1008 # Create Log function & Function call
1009 i = 0
1010 exe_c.write(" ")
1011 if is_obj(result):
1012 exe_c.write("%s result = " % type2str(result))
1013 exe_c.write("%s(\n " % name)
1014 for p in params:
1015 kind = param_kind(p)
1016 ty = param_type(p)
1017 if (i > 0):
1018 exe_c.write(",\n ")
1019 if kind == IN:
1020 if is_obj(ty):
1021 log_c.write(" P(a%s);\n" % i)
1022 exe_c.write("reinterpret_cast<%s>(in.get_obj(%s))" % (param2str(p), i))
1023 elif ty == STRING:
1024 log_c.write(" S(a%s);\n" % i)
1025 exe_c.write("in.get_str(%s)" % i)
1026 elif ty == SYMBOL:
1027 log_c.write(" Sy(a%s);\n" % i)
1028 exe_c.write("in.get_symbol(%s)" % i)
1029 elif ty == UINT:
1030 log_c.write(" U(a%s);\n" % i)
1031 exe_c.write("in.get_uint(%s)" % i)
1032 elif ty == UINT64:
1033 log_c.write(" U(a%s);\n" % i)
1034 exe_c.write("in.get_uint64(%s)" % i)
1035 elif ty == INT:
1036 log_c.write(" I(a%s);\n" % i)
1037 exe_c.write("in.get_int(%s)" % i)
1038 elif ty == INT64:
1039 log_c.write(" I(a%s);\n" % i)
1040 exe_c.write("in.get_int64(%s)" % i)
1041 elif ty == DOUBLE:
1042 log_c.write(" D(a%s);\n" % i)
1043 exe_c.write("in.get_double(%s)" % i)
1044 elif ty == FLOAT:
1045 log_c.write(" D(a%s);\n" % i)
1046 exe_c.write("in.get_float(%s)" % i)
1047 elif ty == BOOL:
1048 log_c.write(" I(a%s);\n" % i)
1049 exe_c.write("in.get_bool(%s)" % i)
1050 elif ty == VOID_PTR:
1051 log_c.write(" P(0);\n")
1052 exe_c.write("in.get_obj_addr(%s)" % i)
1053 elif ty == LBOOL:
1054 log_c.write(" I(static_cast<signed>(a%s));\n" % i)
1055 exe_c.write("static_cast<%s>(in.get_int(%s))" % (type2str(ty), i))
1056 elif ty == PRINT_MODE or ty == ERROR_CODE:
1057 log_c.write(" U(static_cast<unsigned>(a%s));\n" % i)
1058 exe_c.write("static_cast<%s>(in.get_uint(%s))" % (type2str(ty), i))
1059 else:
1060 error("unsupported parameter for %s, %s" % (name, p))
1061 elif kind == INOUT:
1062 error("unsupported parameter for %s, %s" % (name, p))
1063 elif kind == OUT:
1064 if is_obj(ty):
1065 log_c.write(" P(0);\n")
1066 exe_c.write("reinterpret_cast<%s>(in.get_obj_addr(%s))" % (param2str(p), i))
1067 elif ty == STRING:
1068 log_c.write(" S(\"\");\n")
1069 exe_c.write("in.get_str_addr(%s)" % i)
1070 elif ty == UINT:
1071 log_c.write(" U(0);\n")
1072 exe_c.write("in.get_uint_addr(%s)" % i)
1073 elif ty == UINT64:
1074 log_c.write(" U(0);\n")
1075 exe_c.write("in.get_uint64_addr(%s)" % i)
1076 elif ty == INT:
1077 log_c.write(" I(0);\n")
1078 exe_c.write("in.get_int_addr(%s)" % i)
1079 elif ty == INT64:
1080 log_c.write(" I(0);\n")
1081 exe_c.write("in.get_int64_addr(%s)" % i)
1082 elif ty == VOID_PTR:
1083 log_c.write(" P(0);\n")
1084 exe_c.write("in.get_obj_addr(%s)" % i)
1085 else:
1086 error("unsupported parameter for %s, %s" % (name, p))
1087 elif kind == IN_ARRAY or kind == INOUT_ARRAY:
1088 sz = param_array_capacity_pos(p)
1089 log_c.write(" for (unsigned i = 0; i < a%s; i++) { " % sz)
1090 if is_obj(ty):
1091 log_c.write("P(a%s[i]);" % i)
1092 log_c.write(" }\n")
1093 log_c.write(" Ap(a%s);\n" % sz)
1094 exe_c.write("reinterpret_cast<%s*>(in.get_obj_array(%s))" % (type2str(ty), i))
1095 elif ty == SYMBOL:
1096 log_c.write("Sy(a%s[i]);" % i)
1097 log_c.write(" }\n")
1098 log_c.write(" Asy(a%s);\n" % sz)
1099 exe_c.write("in.get_symbol_array(%s)" % i)
1100 elif ty == UINT:
1101 log_c.write("U(a%s[i]);" % i)
1102 log_c.write(" }\n")
1103 log_c.write(" Au(a%s);\n" % sz)
1104 exe_c.write("in.get_uint_array(%s)" % i)
1105 elif ty == INT:
1106 log_c.write("I(a%s[i]);" % i)
1107 log_c.write(" }\n")
1108 log_c.write(" Ai(a%s);\n" % sz)
1109 exe_c.write("in.get_int_array(%s)" % i)
1110 elif ty == BOOL:
1111 log_c.write("U(a%s[i]);" % i)
1112 log_c.write(" }\n")
1113 log_c.write(" Au(a%s);\n" % sz)
1114 exe_c.write("in.get_bool_array(%s)" % i)
1115 else:
1116 error ("unsupported parameter for %s, %s, %s" % (ty, name, p))
1117 elif kind == OUT_ARRAY:
1118 sz = param_array_capacity_pos(p)
1119 sz_p = params[sz]
1120 sz_p_k = param_kind(sz_p)
1121 tstr = type2str(ty)
1122 if sz_p_k == OUT or sz_p_k == INOUT:
1123 sz_e = ("(*a%s)" % sz)
1124 else:
1125 sz_e = ("a%s" % sz)
1126 log_c.write(" for (unsigned i = 0; i < %s; i++) { " % sz_e)
1127 if is_obj(ty):
1128 log_c.write("P(0);")
1129 log_c.write(" }\n")
1130 log_c.write(" Ap(%s);\n" % sz_e)
1131 exe_c.write("reinterpret_cast<%s*>(in.get_obj_array(%s))" % (tstr, i))
1132 elif ty == UINT:
1133 log_c.write("U(0);")
1134 log_c.write(" }\n")
1135 log_c.write(" Au(%s);\n" % sz_e)
1136 exe_c.write("in.get_uint_array(%s)" % i)
1137 else:
1138 error ("unsupported parameter for %s, %s" % (name, p))
1139 elif kind == OUT_MANAGED_ARRAY:
1140 sz = param_array_size_pos(p)
1141 sz_p = params[sz]
1142 sz_p_k = param_kind(sz_p)
1143 tstr = type2str(ty)
1144 if sz_p_k == OUT or sz_p_k == INOUT:
1145 sz_e = ("(*a%s)" % sz)
1146 else:
1147 sz_e = ("a%s" % sz)
1148 log_c.write(" for (unsigned i = 0; i < %s; i++) { " % sz_e)
1149 log_c.write("P(0);")
1150 log_c.write(" }\n")
1151 log_c.write(" Ap(%s);\n" % sz_e)
1152 exe_c.write("reinterpret_cast<%s**>(in.get_obj_array(%s))" % (tstr, i))
1153 elif kind == FN_PTR:
1154 log_c.write("// P(a%s);\n" % i)
1155 exe_c.write("reinterpret_cast<%s>(in.get_obj(%s))" % (param2str(p), i))
1156 else:
1157 error ("unsupported parameter for %s, %s" % (name, p))
1158 i = i + 1
1159 log_c.write(" C(%s);\n" % next_id)
1160 exe_c.write(");\n")
1161 if is_obj(result):
1162 exe_c.write(" in.store_result(result);\n")
1163 if name == 'Z3_mk_context' or name == 'Z3_mk_context_rc':
1164 exe_c.write(" Z3_set_error_handler(result, Z3_replayer_error_handler);")
1165 log_c.write('}\n')
1166 exe_c.write('}\n')
1167 mk_log_macro(log_h, name, params)
1168 if log_result(result, params):
1169 mk_log_result_macro(log_h, name, result, params)
1170 next_id = next_id + 1
1171
1172 def mk_bindings(exe_c):
1173 exe_c.write("void register_z3_replayer_cmds(z3_replayer & in) {\n")
1174 for key, val in API2Id.items():
1175 exe_c.write(" in.register_cmd(%s, exec_%s, \"%s\");\n" % (key, val, val))
1176 exe_c.write("}\n")
1177
1178 def ml_method_name(name):
1179 return name[3:] # Remove Z3_
1180
1181 def is_out_param(p):
1182 if param_kind(p) == OUT or param_kind(p) == INOUT or param_kind(p) == OUT_ARRAY or param_kind(p) == INOUT_ARRAY or param_kind(p) == OUT_MANAGED_ARRAY:
1183 return True
1184 else:
1185 return False
1186
1187 def outparams(params):
1188 op = []
1189 for param in params:
1190 if is_out_param(param):
1191 op.append(param)
1192 return op
1193
1194 def is_in_param(p):
1195 if param_kind(p) == IN or param_kind(p) == INOUT or param_kind(p) == IN_ARRAY or param_kind(p) == INOUT_ARRAY:
1196 return True
1197 else:
1198 return False
1199
1200 def inparams(params):
1201 ip = []
1202 for param in params:
1203 if is_in_param(param):
1204 ip.append(param)
1205 return ip
1206
1207 def is_array_param(p):
1208 if param_kind(p) == IN_ARRAY or param_kind(p) == INOUT_ARRAY or param_kind(p) == OUT_ARRAY:
1209 return True
1210 else:
1211 return False
1212
1213 def arrayparams(params):
1214 op = []
1215 for param in params:
1216 if is_array_param(param):
1217 op.append(param)
1218 return op
1219
1220 def ml_plus_type(ts):
1221 if ts == 'Z3_context':
1222 return 'Z3_context_plus'
1223 elif ts == 'Z3_ast' or ts == 'Z3_sort' or ts == 'Z3_func_decl' or ts == 'Z3_app' or ts == 'Z3_pattern':
1224 return 'Z3_ast_plus'
1225 elif ts == 'Z3_symbol':
1226 return 'Z3_symbol_plus'
1227 elif ts == 'Z3_constructor':
1228 return 'Z3_constructor_plus'
1229 elif ts == 'Z3_constructor_list':
1230 return 'Z3_constructor_list_plus'
1231 elif ts == 'Z3_rcf_num':
1232 return 'Z3_rcf_num_plus'
1233 elif ts == 'Z3_params':
1234 return 'Z3_params_plus'
1235 elif ts == 'Z3_param_descrs':
1236 return 'Z3_param_descrs_plus'
1237 elif ts == 'Z3_model':
1238 return 'Z3_model_plus'
1239 elif ts == 'Z3_func_interp':
1240 return 'Z3_func_interp_plus'
1241 elif ts == 'Z3_func_entry':
1242 return 'Z3_func_entry_plus'
1243 elif ts == 'Z3_goal':
1244 return 'Z3_goal_plus'
1245 elif ts == 'Z3_tactic':
1246 return 'Z3_tactic_plus'
1247 elif ts == 'Z3_simplifier':
1248 return 'Z3_simplifier_plus'
1249 elif ts == 'Z3_probe':
1250 return 'Z3_probe_plus'
1251 elif ts == 'Z3_apply_result':
1252 return 'Z3_apply_result_plus'
1253 elif ts == 'Z3_solver':
1254 return 'Z3_solver_plus'
1255 elif ts == 'Z3_stats':
1256 return 'Z3_stats_plus'
1257 elif ts == 'Z3_ast_vector':
1258 return 'Z3_ast_vector_plus'
1259 elif ts == 'Z3_ast_map':
1260 return 'Z3_ast_map_plus'
1261 elif ts == 'Z3_fixedpoint':
1262 return 'Z3_fixedpoint_plus'
1263 elif ts == 'Z3_optimize':
1264 return 'Z3_optimize_plus'
1265 else:
1266 return ts
1267
1268 def ml_minus_type(ts):
1269 if ts == 'Z3_ast' or ts == 'Z3_sort' or ts == 'Z3_func_decl' or ts == 'Z3_app' or ts == 'Z3_pattern':
1270 return 'Z3_ast'
1271 if ts == 'Z3_ast_plus' or ts == 'Z3_sort_plus' or ts == 'Z3_func_decl_plus' or ts == 'Z3_app_plus' or ts == 'Z3_pattern_plus':
1272 return 'Z3_ast'
1273 elif ts == 'Z3_constructor_plus':
1274 return 'Z3_constructor'
1275 elif ts == 'Z3_constructor_list_plus':
1276 return 'Z3_constructor_list'
1277 elif ts == 'Z3_rcf_num_plus':
1278 return 'Z3_rcf_num'
1279 elif ts == 'Z3_params_plus':
1280 return 'Z3_params'
1281 elif ts == 'Z3_param_descrs_plus':
1282 return 'Z3_param_descrs'
1283 elif ts == 'Z3_model_plus':
1284 return 'Z3_model'
1285 elif ts == 'Z3_func_interp_plus':
1286 return 'Z3_func_interp'
1287 elif ts == 'Z3_func_entry_plus':
1288 return 'Z3_func_entry'
1289 elif ts == 'Z3_goal_plus':
1290 return 'Z3_goal'
1291 elif ts == 'Z3_tactic_plus':
1292 return 'Z3_tactic'
1293 elif ts == 'Z3_simplifier_plus':
1294 return 'Z3_simplifier'
1295 elif ts == 'Z3_probe_plus':
1296 return 'Z3_probe'
1297 elif ts == 'Z3_apply_result_plus':
1298 return 'Z3_apply_result'
1299 elif ts == 'Z3_solver_plus':
1300 return 'Z3_solver'
1301 elif ts == 'Z3_stats_plus':
1302 return 'Z3_stats'
1303 elif ts == 'Z3_ast_vector_plus':
1304 return 'Z3_ast_vector'
1305 elif ts == 'Z3_ast_map_plus':
1306 return 'Z3_ast_map'
1307 elif ts == 'Z3_fixedpoint_plus':
1308 return 'Z3_fixedpoint'
1309 elif ts == 'Z3_optimize_plus':
1310 return 'Z3_optimize'
1311 else:
1312 return ts
1313
1314 def ml_plus_type_raw(ts):
1315 if ml_has_plus_type(ts):
1316 return ml_plus_type(ts) + '_raw';
1317 else:
1318 return ts
1319
1320 def ml_plus_ops_type(ts):
1321 if ml_has_plus_type(ts):
1322 return ml_plus_type(ts) + '_custom_ops'
1323 else:
1324 return 'default_custom_ops'
1325
1326 def ml_has_plus_type(ts):
1327 return ts != ml_plus_type(ts)
1328
1329 def ml_unwrap(t, ts, s):
1330 if t == STRING:
1331 return '(' + ts + ') String_val(' + s + ')'
1332 elif t == BOOL or (type2str(t) == 'bool'):
1333 return '(' + ts + ') Bool_val(' + s + ')'
1334 elif t == INT or t == PRINT_MODE or t == ERROR_CODE or t == LBOOL:
1335 return '(' + ts + ') Int_val(' + s + ')'
1336 elif t == UINT:
1337 return '(' + ts + ') Unsigned_int_val(' + s + ')'
1338 elif t == INT64:
1339 return '(' + ts + ') Int64_val(' + s + ')'
1340 elif t == UINT64:
1341 return '(' + ts + ') Int64_val(' + s + ')'
1342 elif t == DOUBLE:
1343 return '(' + ts + ') Double_val(' + s + ')'
1344 elif ml_has_plus_type(ts):
1345 pts = ml_plus_type(ts)
1346 return '(' + ts + ') ' + ml_plus_type_raw(ts) + '((' + pts + '*) Data_custom_val(' + s + '))'
1347 else:
1348 return '* ((' + ts + '*) Data_custom_val(' + s + '))'
1349
1350 def ml_set_wrap(t, d, n):
1351 if t == VOID:
1352 return d + ' = Val_unit;'
1353 elif t == BOOL or (type2str(t) == 'bool'):
1354 return d + ' = Val_bool(' + n + ');'
1355 elif t == INT or t == UINT or t == PRINT_MODE or t == ERROR_CODE or t == LBOOL:
1356 return d + ' = Val_int(' + n + ');'
1357 elif t == INT64 or t == UINT64:
1358 return d + ' = caml_copy_int64(' + n + ');'
1359 elif t == DOUBLE:
1360 return d + '= caml_copy_double(' + n + ');'
1361 elif t == STRING:
1362 return d + ' = caml_copy_string((const char*) ' + n + ');'
1363 else:
1364 pts = ml_plus_type(type2str(t))
1365 return '*(' + pts + '*)Data_custom_val(' + d + ') = ' + n + ';'
1366
1367 def ml_alloc_and_store(t, lhs, rhs):
1368 if t == VOID or t == BOOL or t == INT or t == UINT or t == PRINT_MODE or t == ERROR_CODE or t == INT64 or t == UINT64 or t == DOUBLE or t == STRING or t == LBOOL or (type2str(t) == 'bool'):
1369 return ml_set_wrap(t, lhs, rhs)
1370 else:
1371 pts = ml_plus_type(type2str(t))
1372 pops = ml_plus_ops_type(type2str(t))
1373 alloc_str = '%s = caml_alloc_custom(&%s, sizeof(%s), 0, 1); ' % (lhs, pops, pts)
1374 return alloc_str + ml_set_wrap(t, lhs, rhs)
1375
1376
1377 z3_long_funs = frozenset([
1378 'Z3_solver_check',
1379 'Z3_solver_check_assumptions',
1380 'Z3_simplify',
1381 'Z3_simplify_ex',
1382 ])
1383
1384 z3_ml_overrides = frozenset([
1385 'Z3_mk_config'])
1386
1387 z3_ml_callbacks = frozenset([
1388 'Z3_solver_propagate_init',
1389 'Z3_solver_propagate_fixed',
1390 'Z3_solver_propagate_final',
1391 'Z3_solver_propagate_eq',
1392 'Z3_solver_propagate_diseq',
1393 'Z3_solver_propagate_created',
1394 'Z3_solver_propagate_decide',
1395 'Z3_solver_register_on_clause'
1396 ])
1397
1398 def mk_ml(ml_src_dir, ml_output_dir):
1399 global Type2Str
1400 ml_nativef = os.path.join(ml_output_dir, 'z3native.ml')
1401 ml_native = open(ml_nativef, 'w')
1402 ml_native.write('(* Automatically generated file *)\n\n')
1403
1404 ml_pref = open(os.path.join(ml_src_dir, 'z3native.ml.pre'), 'r')
1405 for s in ml_pref:
1406 ml_native.write(s);
1407 ml_pref.close()
1408
1409 ml_native.write('\n')
1410 for name, result, params in _dotnet_decls:
1411 if name in z3_ml_callbacks:
1412 continue
1413 ml_native.write('external %s : ' % ml_method_name(name))
1414 ip = inparams(params)
1415 op = outparams(params)
1416 if len(ip) == 0:
1417 ml_native.write(' unit -> ')
1418 for p in ip:
1419 ml_native.write('%s -> ' % param2ml(p))
1420 if len(op) > 0:
1421 ml_native.write('(')
1422 first = True
1423 if result != VOID or len(op) == 0:
1424 ml_native.write('%s' % type2ml(result))
1425 first = False
1426 for p in op:
1427 if first:
1428 first = False
1429 else:
1430 ml_native.write(' * ')
1431 ml_native.write('%s' % param2ml(p))
1432 if len(op) > 0:
1433 ml_native.write(')')
1434 if len(ip) > 5:
1435 ml_native.write(' = "n_%s_bytecode" "n_%s"\n' % (ml_method_name(name), ml_method_name(name)))
1436 else:
1437 ml_native.write(' = "n_%s"\n' % ml_method_name(name))
1438 ml_native.write('\n')
1439
1440 # null pointer helpers
1441 for type_id in Type2Str:
1442 type_name = Type2Str[type_id]
1443 if ml_has_plus_type(type_name) and not type_name in ['Z3_context', 'Z3_sort', 'Z3_func_decl', 'Z3_app', 'Z3_pattern']:
1444 ml_name = type2ml(type_id)
1445 ml_native.write('external context_of_%s : %s -> context = "n_context_of_%s"\n' % (ml_name, ml_name, ml_name))
1446 ml_native.write('external is_null_%s : %s -> bool = "n_is_null_%s"\n' % (ml_name, ml_name, ml_name))
1447 ml_native.write('external mk_null_%s : context -> %s = "n_mk_null_%s"\n\n' % (ml_name, ml_name, ml_name))
1448
1449 ml_native.write('(**/**)\n')
1450 ml_native.close()
1451
1452 if is_verbose():
1453 print ('Generated "%s"' % ml_nativef)
1454
1455 mk_z3native_stubs_c(ml_src_dir, ml_output_dir)
1456
1457 def mk_z3native_stubs_c(ml_src_dir, ml_output_dir): # C interface
1458 ml_wrapperf = os.path.join(ml_output_dir, 'z3native_stubs.c')
1459 ml_wrapper = open(ml_wrapperf, 'w')
1460 ml_wrapper.write('// Automatically generated file\n\n')
1461
1462 ml_pref = open(os.path.join(ml_src_dir, 'z3native_stubs.c.pre'), 'r')
1463 for s in ml_pref:
1464 ml_wrapper.write(s);
1465 ml_pref.close()
1466
1467 for name, result, params in _dotnet_decls:
1468
1469 if name in z3_ml_overrides:
1470 continue
1471 if name in z3_ml_callbacks:
1472 continue
1473
1474 ip = inparams(params)
1475 op = outparams(params)
1476 ap = arrayparams(params)
1477 ret_size = len(op)
1478 if result != VOID:
1479 ret_size = ret_size + 1
1480
1481 # Setup frame
1482 ml_wrapper.write('CAMLprim DLL_PUBLIC value n_%s(' % ml_method_name(name))
1483 first = True
1484 i = 0
1485 for p in params:
1486 if is_in_param(p):
1487 if first:
1488 first = False
1489 else:
1490 ml_wrapper.write(', ')
1491 ml_wrapper.write('value a%d' % i)
1492 i = i + 1
1493 ml_wrapper.write(') {\n')
1494 ml_wrapper.write(' CAMLparam%d(' % len(ip))
1495 i = 0
1496 first = True
1497 for p in params:
1498 if is_in_param(p):
1499 if first:
1500 first = False
1501 else:
1502 ml_wrapper.write(', ')
1503 ml_wrapper.write('a%d' % i)
1504 i = i + 1
1505 ml_wrapper.write(');\n')
1506 i = 0
1507 if len(op) + len(ap) == 0:
1508 ml_wrapper.write(' CAMLlocal1(result);\n')
1509 else:
1510 c = 0
1511 needs_tmp_value = False
1512 for p in params:
1513 if is_out_param(p) or is_array_param(p):
1514 c = c + 1
1515 needs_tmp_value = needs_tmp_value or param_kind(p) == OUT_ARRAY or param_kind(p) == INOUT_ARRAY
1516 if needs_tmp_value:
1517 c = c + 1
1518 if len(ap) > 0:
1519 c = c + 1
1520 ml_wrapper.write(' CAMLlocal%s(result, z3rv_val' % (c+2))
1521 for p in params:
1522 if is_out_param(p) or is_array_param(p):
1523 ml_wrapper.write(', _a%s_val' % i)
1524 i = i + 1
1525 if needs_tmp_value:
1526 ml_wrapper.write(', tmp_val')
1527 if len(ap) != 0:
1528 ml_wrapper.write(', _iter');
1529
1530 ml_wrapper.write(');\n')
1531
1532 if len(ap) > 0:
1533 ml_wrapper.write(' unsigned _i;\n')
1534
1535 # determine if the function has a context as parameter.
1536 have_context = (len(params) > 0) and (param_type(params[0]) == CONTEXT)
1537
1538 if have_context and name not in Unwrapped and name not in Unchecked:
1539 ml_wrapper.write(' Z3_error_code ec;\n')
1540
1541 if result != VOID:
1542 ts = type2str(result)
1543 if ml_has_plus_type(ts):
1544 pts = ml_plus_type(ts)
1545 ml_wrapper.write(' %s z3rv_m;\n' % ts)
1546 ml_wrapper.write(' %s z3rv;\n' % pts)
1547 else:
1548 ml_wrapper.write(' %s z3rv;\n' % ts)
1549
1550 # declare all required local variables
1551 # To comply with C89, we need to first declare the variables and initialize them
1552 # only afterwards.
1553 i = 0
1554 for param in params:
1555 if param_type(param) == CONTEXT and i == 0:
1556 ml_wrapper.write(' Z3_context_plus ctx_p;\n')
1557 ml_wrapper.write(' Z3_context _a0;\n')
1558 else:
1559 k = param_kind(param)
1560 if k == OUT_ARRAY:
1561 ml_wrapper.write(' %s * _a%s;\n' % (type2str(param_type(param)), i))
1562 elif k == OUT_MANAGED_ARRAY:
1563 ml_wrapper.write(' %s * _a%s;\n' % (type2str(param_type(param)), i))
1564 elif k == IN_ARRAY or k == INOUT_ARRAY:
1565 t = param_type(param)
1566 ts = type2str(t)
1567 ml_wrapper.write(' %s * _a%s;\n' % (ts, i))
1568 elif k == IN:
1569 t = param_type(param)
1570 ml_wrapper.write(' %s _a%s;\n' % (type2str(t), i))
1571 elif k == OUT or k == INOUT:
1572 t = param_type(param)
1573 ml_wrapper.write(' %s _a%s;\n' % (type2str(t), i))
1574 ts = type2str(t)
1575 if ml_has_plus_type(ts):
1576 pts = ml_plus_type(ts)
1577 ml_wrapper.write(' %s _a%dp;\n' % (pts, i))
1578 i = i + 1
1579
1580
1581 # End of variable declarations in outermost block:
1582 # To comply with C89, no variable declarations may occur in the outermost block
1583 # from that point onwards (breaks builds with at least VC 2012 and prior)
1584 ml_wrapper.write('\n')
1585
1586 # Declare locals, preprocess arrays, strings, in/out arguments
1587 i = 0
1588 for param in params:
1589 if param_type(param) == CONTEXT and i == 0:
1590 ml_wrapper.write(' ctx_p = *(Z3_context_plus*) Data_custom_val(a' + str(i) + ');\n')
1591 ml_wrapper.write(' _a0 = ctx_p->ctx;\n')
1592 else:
1593 k = param_kind(param)
1594 if k == OUT_ARRAY:
1595 ml_wrapper.write(' _a%s = (%s*) malloc(sizeof(%s) * (_a%s));\n' % (
1596 i,
1597 type2str(param_type(param)),
1598 type2str(param_type(param)),
1599 param_array_capacity_pos(param)))
1600 elif k == OUT_MANAGED_ARRAY:
1601 ml_wrapper.write(' _a%s = 0;\n' % i)
1602 elif k == IN_ARRAY or k == INOUT_ARRAY:
1603 t = param_type(param)
1604 ts = type2str(t)
1605 ml_wrapper.write(' _a%s = (%s*) malloc(sizeof(%s) * _a%s);\n' % (i, ts, ts, param_array_capacity_pos(param)))
1606 elif k == IN:
1607 t = param_type(param)
1608 ml_wrapper.write(' _a%s = %s;\n' % (i, ml_unwrap(t, type2str(t), 'a' + str(i))))
1609 i = i + 1
1610
1611 i = 0
1612 for param in params:
1613 k = param_kind(param)
1614 if k == IN_ARRAY or k == INOUT_ARRAY:
1615 t = param_type(param)
1616 ts = type2str(t)
1617 ml_wrapper.write(' _iter = a' + str(i) + ';\n')
1618 ml_wrapper.write(' for (_i = 0; _i < _a%s; _i++) {\n' % param_array_capacity_pos(param))
1619 ml_wrapper.write(' assert(_iter != Val_emptylist);\n')
1620 ml_wrapper.write(' _a%s[_i] = %s;\n' % (i, ml_unwrap(t, ts, 'Field(_iter, 0)')))
1621 ml_wrapper.write(' _iter = Field(_iter, 1);\n')
1622 ml_wrapper.write(' }\n')
1623 ml_wrapper.write(' assert(_iter == Val_emptylist);\n\n')
1624 i = i + 1
1625
1626 release_caml_gc= name in z3_long_funs
1627 if release_caml_gc:
1628 ml_wrapper.write('\n caml_release_runtime_system();\n')
1629
1630 ml_wrapper.write('\n /* invoke Z3 function */\n ')
1631 if result != VOID:
1632 ts = type2str(result)
1633 if ml_has_plus_type(ts):
1634 ml_wrapper.write('z3rv_m = ')
1635 else:
1636 ml_wrapper.write('z3rv = ')
1637
1638
1639 # invoke procedure
1640 ml_wrapper.write('%s(' % name)
1641 i = 0
1642 first = True
1643 for param in params:
1644 if first:
1645 first = False
1646 else:
1647 ml_wrapper.write(', ')
1648 k = param_kind(param)
1649 if k == OUT or k == INOUT or k == OUT_MANAGED_ARRAY:
1650 ml_wrapper.write('&_a%s' % i)
1651 else:
1652 ml_wrapper.write('_a%i' % i)
1653 i = i + 1
1654 ml_wrapper.write(');\n')
1655
1656 if name in NULLWrapped:
1657 ml_wrapper.write(' if (z3rv_m == NULL) {\n')
1658 ml_wrapper.write(' caml_raise_with_string(*caml_named_value("Z3EXCEPTION"), "Object allocation failed");\n')
1659 ml_wrapper.write(' }\n')
1660
1661 if release_caml_gc:
1662 ml_wrapper.write('\n caml_acquire_runtime_system();\n')
1663
1664 if have_context and name not in Unwrapped and name not in Unchecked:
1665 ml_wrapper.write(' ec = Z3_get_error_code(ctx_p->ctx);\n')
1666 ml_wrapper.write(' if (ec != Z3_OK) {\n')
1667 ml_wrapper.write(' const char * msg = Z3_get_error_msg(ctx_p->ctx, ec);\n')
1668 ml_wrapper.write(' caml_raise_with_string(*caml_named_value("Z3EXCEPTION"), msg);\n')
1669 ml_wrapper.write(' }\n')
1670
1671 if result != VOID:
1672 ts = type2str(result)
1673 if ml_has_plus_type(ts):
1674 pts = ml_plus_type(ts)
1675 if name in NULLWrapped:
1676 ml_wrapper.write(' z3rv = %s_mk(z3rv_m);\n' % pts)
1677 else:
1678 ml_wrapper.write(' z3rv = %s_mk(ctx_p, (%s) z3rv_m);\n' % (pts, ml_minus_type(ts)))
1679
1680 # convert output params
1681 if len(op) > 0:
1682 # we have output parameters (i.e. call-by-reference arguments to the Z3 native
1683 # code function). Hence, the value returned by the OCaml native wrapper is a tuple
1684 # which contains the Z3 native function's return value (if it is non-void) in its
1685 # first and the output parameters in the following components.
1686
1687 ml_wrapper.write('\n /* construct return tuple */\n')
1688 ml_wrapper.write(' result = caml_alloc(%s, 0);\n' % ret_size)
1689
1690 i = 0
1691 for p in params:
1692 pt = param_type(p)
1693 ts = type2str(pt)
1694 if param_kind(p) == OUT_ARRAY or param_kind(p) == INOUT_ARRAY:
1695 # convert a C-array into an OCaml list and return it
1696 ml_wrapper.write('\n _a%s_val = Val_emptylist;\n' % i)
1697 ml_wrapper.write(' for (_i = _a%s; _i > 0; _i--) {\n' % param_array_capacity_pos(p))
1698 pts = ml_plus_type(ts)
1699 pops = ml_plus_ops_type(ts)
1700 if ml_has_plus_type(ts):
1701 ml_wrapper.write(' %s _a%dp = %s_mk(ctx_p, (%s) _a%d[_i - 1]);\n' % (pts, i, pts, ml_minus_type(ts), i))
1702 ml_wrapper.write(' %s\n' % ml_alloc_and_store(pt, 'tmp_val', '_a%dp' % i))
1703 else:
1704 ml_wrapper.write(' %s\n' % ml_alloc_and_store(pt, 'tmp_val', '_a%d[_i - 1]' % i))
1705 ml_wrapper.write(' _iter = caml_alloc(2,0);\n')
1706 ml_wrapper.write(' Store_field(_iter, 0, tmp_val);\n')
1707 ml_wrapper.write(' Store_field(_iter, 1, _a%s_val);\n' % i)
1708 ml_wrapper.write(' _a%s_val = _iter;\n' % i)
1709 ml_wrapper.write(' }\n\n')
1710 elif param_kind(p) == OUT_MANAGED_ARRAY:
1711 wrp = ml_set_wrap(pt, '_a%d_val' % i, '_a%d' % i)
1712 wrp = wrp.replace('*)', '**)')
1713 wrp = wrp.replace('_plus', '')
1714 ml_wrapper.write(' %s\n' % wrp)
1715 elif is_out_param(p):
1716 if ml_has_plus_type(ts):
1717 pts = ml_plus_type(ts)
1718 ml_wrapper.write(' _a%dp = %s_mk(ctx_p, (%s) _a%d);\n' % (i, pts, ml_minus_type(ts), i))
1719 ml_wrapper.write(' %s\n' % ml_alloc_and_store(pt, '_a%d_val' % i, '_a%dp' % i))
1720 else:
1721 ml_wrapper.write(' %s\n' % ml_alloc_and_store(pt, '_a%d_val' % i, '_a%d' % i))
1722 i = i + 1
1723
1724 # return tuples
1725 i = j = 0
1726 if result != VOID:
1727 ml_wrapper.write(' %s' % ml_alloc_and_store(result, 'z3rv_val', 'z3rv'))
1728 ml_wrapper.write(' Store_field(result, 0, z3rv_val);\n')
1729 j = j + 1
1730 for p in params:
1731 if is_out_param(p):
1732 ml_wrapper.write(' Store_field(result, %s, _a%s_val);\n' % (j, i))
1733 j = j + 1
1734 i = i + 1
1735 else:
1736 # As we have no output parameters, we simply return the result
1737 ml_wrapper.write('\n /* construct simple return value */\n')
1738 ml_wrapper.write(' %s' % ml_alloc_and_store(result, "result", "z3rv"))
1739
1740 # local array cleanup
1741 ml_wrapper.write('\n /* cleanup and return */\n')
1742 i = 0
1743 for p in params:
1744 k = param_kind(p)
1745 if k == OUT_ARRAY or k == IN_ARRAY or k == INOUT_ARRAY:
1746 ml_wrapper.write(' free(_a%s);\n' % i)
1747 i = i + 1
1748
1749 # return
1750 ml_wrapper.write(' CAMLreturn(result);\n')
1751 ml_wrapper.write('}\n\n')
1752 if len(ip) > 5:
1753 ml_wrapper.write('CAMLprim DLL_PUBLIC value n_%s_bytecode(value * argv, int argn) {\n' % ml_method_name(name))
1754 ml_wrapper.write(' return n_%s(' % ml_method_name(name))
1755 i = 0
1756 while i < len(ip):
1757 if i == 0:
1758 ml_wrapper.write('argv[0]')
1759 else:
1760 ml_wrapper.write(', argv[%s]' % i)
1761 i = i + 1
1762 ml_wrapper.write(');\n}\n')
1763 ml_wrapper.write('\n\n')
1764 ml_wrapper.write('#ifdef __cplusplus\n')
1765 ml_wrapper.write('}\n')
1766 ml_wrapper.write('#endif\n')
1767
1768 if is_verbose():
1769 print ('Generated "%s"' % ml_wrapperf)
1770
1771 # Collect API(...) commands from
1772 def def_APIs(api_files):
1773 pat1 = re.compile(" *def_API.*")
1774 pat2 = re.compile(" *extra_API.*")
1775 for api_file in api_files:
1776 api = open(api_file, 'r')
1777 for line in api:
1778 line = line.strip('\r\n\t ')
1779 try:
1780 m = pat1.match(line)
1781 if m:
1782 eval(line)
1783 m = pat2.match(line)
1784 if m:
1785 eval(line)
1786 except Exception as e:
1787 error('ERROR: While processing: %s: %s\n' % (e, line))
1788
1789 def write_log_h_preamble(log_h):
1790 log_h.write('// Automatically generated file\n')
1791 log_h.write('#include\"api/z3.h\"\n')
1792 log_h.write('#ifdef __GNUC__\n')
1793 log_h.write('#define _Z3_UNUSED __attribute__((unused))\n')
1794 log_h.write('#else\n')
1795 log_h.write('#define _Z3_UNUSED\n')
1796 log_h.write('#endif\n')
1797 #
1798 log_h.write('#include "util/mutex.h"\n')
1799 log_h.write('extern atomic<bool> g_z3_log_enabled;\n')
1800 log_h.write('void ctx_enable_logging();\n')
1801 log_h.write('class z3_log_ctx { bool m_prev; public: z3_log_ctx() { ATOMIC_EXCHANGE(m_prev, g_z3_log_enabled, false); } ~z3_log_ctx() { if (m_prev) g_z3_log_enabled = true; } bool enabled() const { return m_prev; } };\n')
1802 log_h.write('void SetR(void * obj);\nvoid SetO(void * obj, unsigned pos);\nvoid SetAO(void * obj, unsigned pos, unsigned idx);\n')
1803 log_h.write('#define RETURN_Z3(Z3RES) do { auto tmp_ret = Z3RES; if (_LOG_CTX.enabled()) { SetR(tmp_ret); } return tmp_ret; } while (0)\n')
1804
1805
1806 def write_log_c_preamble(log_c):
1807 log_c.write('// Automatically generated file\n')
1808 log_c.write('#include\"api/z3.h\"\n')
1809 log_c.write('#include\"api/api_log_macros.h\"\n')
1810 log_c.write('#include\"api/z3_logger.h\"\n')
1811
1812 def write_exe_c_preamble(exe_c):
1813 exe_c.write('// Automatically generated file\n')
1814 exe_c.write('#include\"api/z3.h\"\n')
1815 exe_c.write('#include\"api/z3_replayer.h\"\n')
1816 #
1817 exe_c.write('void Z3_replayer_error_handler(Z3_context ctx, Z3_error_code c) { printf("[REPLAYER ERROR HANDLER]: %s\\n", Z3_get_error_msg(ctx, c)); }\n')
1818
1819 def write_core_py_post(core_py):
1820 core_py.write("""
1821 # Clean up
1822 del _lib
1823 del _default_dirs
1824 del _all_dirs
1825 del _ext
1826 """)
1827
1828 def write_core_py_preamble(core_py):
1829 core_py.write(
1830 """
1831 # Automatically generated file
1832 import atexit
1833 import sys, os
1834 import contextlib
1835 import ctypes
1836 if sys.version_info >= (3, 9):
1837 import importlib.resources as importlib_resources
1838 else:
1839 import importlib_resources
1840 from .z3types import *
1841 from .z3consts import *
1842
1843 _file_manager = contextlib.ExitStack()
1844 atexit.register(_file_manager.close)
1845 _ext = 'dll' if sys.platform in ('win32', 'cygwin') else 'dylib' if sys.platform == 'darwin' else 'so'
1846 _lib = None
1847 _z3_lib_resource = importlib_resources.files('z3').joinpath('lib')
1848 _z3_lib_resource_path = _file_manager.enter_context(
1849 importlib_resources.as_file(_z3_lib_resource)
1850 )
1851 _default_dirs = ['.',
1852 os.path.dirname(os.path.abspath(__file__)),
1853 _z3_lib_resource_path,
1854 os.path.join(sys.prefix, 'lib'),
1855 None]
1856 _all_dirs = []
1857 # search the default dirs first
1858 _all_dirs.extend(_default_dirs)
1859
1860 if sys.version < '3':
1861 import __builtin__
1862 if hasattr(__builtin__, "Z3_LIB_DIRS"):
1863 _all_dirs = __builtin__.Z3_LIB_DIRS
1864 else:
1865 import builtins
1866 if hasattr(builtins, "Z3_LIB_DIRS"):
1867 _all_dirs = builtins.Z3_LIB_DIRS
1868
1869 for v in ('Z3_LIBRARY_PATH', 'PATH', 'PYTHONPATH'):
1870 if v in os.environ:
1871 lp = os.environ[v];
1872 lds = lp.split(';') if sys.platform in ('win32') else lp.split(':')
1873 _all_dirs.extend(lds)
1874
1875 _failures = []
1876 for d in _all_dirs:
1877 try:
1878 d = os.path.realpath(d)
1879 if os.path.isdir(d):
1880 d = os.path.join(d, 'libz3.%s' % _ext)
1881 if os.path.isfile(d):
1882 _lib = ctypes.CDLL(d)
1883 break
1884 except Exception as e:
1885 _failures += [e]
1886 pass
1887
1888 if _lib is None:
1889 # If all else failed, ask the system to find it.
1890 try:
1891 _lib = ctypes.CDLL('libz3.%s' % _ext)
1892 except Exception as e:
1893 _failures += [e]
1894 pass
1895
1896 if _lib is None:
1897 print("Could not find libz3.%s; consider adding the directory containing it to" % _ext)
1898 print(" - your system's PATH environment variable,")
1899 print(" - the Z3_LIBRARY_PATH environment variable, or ")
1900 print(" - to the custom Z3_LIB_DIRS Python-builtin before importing the z3 module, e.g. via")
1901 if sys.version < '3':
1902 print(" import __builtin__")
1903 print(" __builtin__.Z3_LIB_DIRS = [ '/path/to/z3/lib/dir' ] # directory containing libz3.%s" % _ext)
1904 else:
1905 print(" import builtins")
1906 print(" builtins.Z3_LIB_DIRS = [ '/path/to/z3/lib/dir' ] # directory containing libz3.%s" % _ext)
1907 print(_failures)
1908 raise Z3Exception("libz3.%s not found." % _ext)
1909
1910
1911 if sys.version < '3':
1912 def _str_to_bytes(s):
1913 return s
1914 def _to_pystr(s):
1915 return s
1916 else:
1917 def _str_to_bytes(s):
1918 if isinstance(s, str):
1919 enc = sys.getdefaultencoding()
1920 return s.encode(enc if enc != None else 'latin-1')
1921 else:
1922 return s
1923
1924 def _to_pystr(s):
1925 if s != None:
1926 enc = sys.getdefaultencoding()
1927 return s.decode(enc if enc != None else 'latin-1')
1928 else:
1929 return ""
1930
1931 _error_handler_type = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_uint)
1932
1933 _lib.Z3_set_error_handler.restype = None
1934 _lib.Z3_set_error_handler.argtypes = [ContextObj, _error_handler_type]
1935
1936 Z3_on_clause_eh = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_uint, ctypes.POINTER(ctypes.c_uint), ctypes.c_void_p)
1937 Z3_push_eh = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_void_p)
1938 Z3_pop_eh = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_uint)
1939 Z3_fresh_eh = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p)
1940
1941 Z3_fixed_eh = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p)
1942 Z3_final_eh = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_void_p)
1943 Z3_eq_eh = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p)
1944
1945 Z3_created_eh = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p)
1946 Z3_decide_eh = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_uint, ctypes.c_int)
1947
1948 _lib.Z3_solver_register_on_clause.restype = None
1949 _lib.Z3_solver_propagate_init.restype = None
1950 _lib.Z3_solver_propagate_final.restype = None
1951 _lib.Z3_solver_propagate_fixed.restype = None
1952 _lib.Z3_solver_propagate_eq.restype = None
1953 _lib.Z3_solver_propagate_diseq.restype = None
1954 _lib.Z3_solver_propagate_decide.restype = None
1955
1956 on_model_eh_type = ctypes.CFUNCTYPE(None, ctypes.c_void_p)
1957 _lib.Z3_optimize_register_model_eh.restype = None
1958 _lib.Z3_optimize_register_model_eh.argtypes = [ContextObj, OptimizeObj, ModelObj, ctypes.c_void_p, on_model_eh_type]
1959
1960 """
1961 )
1962
1963 log_h = None
1964 log_c = None
1965 exe_c = None
1966 core_py = None
1967
1968 # FIXME: This can only be called once from this module
1969 # due to its use of global state!
1970 def generate_files(api_files,
1971 api_output_dir=None,
1972 z3py_output_dir=None,
1973 dotnet_output_dir=None,
1974 java_input_dir=None,
1975 java_output_dir=None,
1976 java_package_name=None,
1977 ml_output_dir=None,
1978 ml_src_dir=None):
1979 """
1980 Scan the api files in ``api_files`` and emit the relevant API files into
1981 the output directories specified. If an output directory is set to ``None``
1982 then the files for that language binding or module are not emitted.
1983
1984 The reason for this function interface is:
1985
1986 * The CMake build system needs to control where
1987 files are emitted.
1988 * The CMake build system needs to be able to choose
1989 which API files are emitted.
1990 * This function should be as decoupled from the Python
1991 build system as much as possible but it must be possible
1992 for the Python build system code to use this function.
1993
1994 Therefore we:
1995
1996 * Do not use the ``mk_util.is_*_enabled()`` functions
1997 to determine if certain files should be or should not be emitted.
1998
1999 * Do not use the components declared in the Python build system
2000 to determine the output directory paths.
2001 """
2002 # FIXME: These should not be global
2003 global log_h, log_c, exe_c, core_py
2004 assert isinstance(api_files, list)
2005
2006 # Hack: Avoid emitting files when we don't want them
2007 # by writing to temporary files that get deleted when
2008 # closed. This allows us to work around the fact that
2009 # existing code is designed to always emit these files.
2010 def mk_file_or_temp(output_dir, file_name, mode='w'):
2011 if output_dir != None:
2012 assert os.path.exists(output_dir) and os.path.isdir(output_dir)
2013 return open(os.path.join(output_dir, file_name), mode)
2014 else:
2015 # Return a file that we can write to without caring
2016 print("Faking emission of '{}'".format(file_name))
2017 import tempfile
2018 return tempfile.TemporaryFile(mode=mode)
2019
2020 apiTypes = APITypes()
2021 with mk_file_or_temp(api_output_dir, 'api_log_macros.h') as log_h:
2022 with mk_file_or_temp(api_output_dir, 'api_log_macros.cpp') as log_c:
2023 with mk_file_or_temp(api_output_dir, 'api_commands.cpp') as exe_c:
2024 with mk_file_or_temp(z3py_output_dir, os.path.join('z3', 'z3core.py')) as core_py:
2025 # Write preambles
2026 write_log_h_preamble(log_h)
2027 write_log_c_preamble(log_c)
2028 write_exe_c_preamble(exe_c)
2029 write_core_py_preamble(core_py)
2030
2031 # FIXME: these functions are awful
2032 apiTypes.def_Types(api_files)
2033 def_APIs(api_files)
2034 mk_bindings(exe_c)
2035 mk_py_wrappers()
2036 write_core_py_post(core_py)
2037
2038 if is_verbose():
2039 print("Generated '{}'".format(log_h.name))
2040 print("Generated '{}'".format(log_c.name))
2041 print("Generated '{}'".format(exe_c.name))
2042 print("Generated '{}'".format(core_py.name))
2043
2044 if dotnet_output_dir:
2045 with open(os.path.join(dotnet_output_dir, 'Native.cs'), 'w') as dotnet_file:
2046 mk_dotnet(dotnet_file)
2047 mk_dotnet_wrappers(dotnet_file)
2048 if is_verbose():
2049 print("Generated '{}'".format(dotnet_file.name))
2050
2051 if java_output_dir:
2052 mk_java(java_input_dir, java_output_dir, java_package_name)
2053
2054 if ml_output_dir:
2055 assert not ml_src_dir is None
2056 mk_ml(ml_src_dir, ml_output_dir)
2057
2058
2059 def main(args):
2060 logging.basicConfig(level=logging.INFO)
2061 parser = argparse.ArgumentParser(description=__doc__)
2062 parser.add_argument("api_files",
2063 nargs="+",
2064 help="API header files to generate files from")
2065 parser.add_argument("--api_output_dir",
2066 default=None,
2067 help="Directory to emit files for api module. If not specified no files are emitted.")
2068 parser.add_argument("--z3py-output-dir",
2069 dest="z3py_output_dir",
2070 default=None,
2071 help="Directory to emit z3py files. If not specified no files are emitted.")
2072 parser.add_argument("--dotnet-output-dir",
2073 dest="dotnet_output_dir",
2074 default=None,
2075 help="Directory to emit dotnet files. If not specified no files are emitted.")
2076 parser.add_argument("--java-input-dir",
2077 dest="java_input_dir",
2078 default=None,
2079 help="Directory where Java sources reside.")
2080 parser.add_argument("--java-output-dir",
2081 dest="java_output_dir",
2082 default=None,
2083 help="Directory to emit Java files. If not specified no files are emitted.")
2084 parser.add_argument("--java-package-name",
2085 dest="java_package_name",
2086 default=None,
2087 help="Name to give the Java package (e.g. ``com.microsoft.z3``).")
2088 parser.add_argument("--ml-src-dir",
2089 dest="ml_src_dir",
2090 default=None,
2091 help="Directory containing OCaml source files. If not specified no files are emitted")
2092 parser.add_argument("--ml-output-dir",
2093 dest="ml_output_dir",
2094 default=None,
2095 help="Directory to emit OCaml files. If not specified no files are emitted.")
2096 pargs = parser.parse_args(args)
2097
2098 if pargs.java_output_dir:
2099 if pargs.java_package_name == None:
2100 logging.error('--java-package-name must be specified')
2101 return 1
2102 if pargs.java_input_dir is None:
2103 logging.error('--java-input-dir must be specified')
2104 return 1
2105
2106 if pargs.ml_output_dir:
2107 if pargs.ml_src_dir is None:
2108 logging.error('--ml-src-dir must be specified')
2109 return 1
2110
2111 for api_file in pargs.api_files:
2112 if not os.path.exists(api_file):
2113 logging.error('"{}" does not exist'.format(api_file))
2114 return 1
2115
2116 generate_files(api_files=pargs.api_files,
2117 api_output_dir=pargs.api_output_dir,
2118 z3py_output_dir=pargs.z3py_output_dir,
2119 dotnet_output_dir=pargs.dotnet_output_dir,
2120 java_input_dir=pargs.java_input_dir,
2121 java_output_dir=pargs.java_output_dir,
2122 java_package_name=pargs.java_package_name,
2123 ml_output_dir=pargs.ml_output_dir,
2124 ml_src_dir=pargs.ml_src_dir)
2125 return 0
2126
2127 if __name__ == '__main__':
2128 sys.exit(main(sys.argv[1:]))