isl_ast_build_get_space: improve error handling
[isl.git] / isl.py
blob6382f6384e722ec992141da3d25101c59e12cea0
1 import gdb
2 import re
4 # GDB Pretty Printers for most isl objects
5 class IslObjectPrinter:
6 """Print an isl object"""
7 def __init__ (self, val, type):
8 self.val = val
9 self.type = type
11 def to_string (self):
12 # Cast val to a void pointer to stop gdb using this pretty
13 # printer for the pointer which would lead to an infinite loop.
14 void_ptr = gdb.lookup_type('void').pointer()
15 value = str(self.val.cast(void_ptr))
16 printer = gdb.parse_and_eval("isl_printer_to_str(isl_"
17 + str(self.type)
18 + "_get_ctx(" + value + "))")
19 printer = gdb.parse_and_eval("isl_printer_print_"
20 + str(self.type) + "("
21 + str(printer) + ", "
22 + value + ")")
23 string = gdb.parse_and_eval("(char*)isl_printer_get_str("
24 + str(printer) + ")")
25 gdb.parse_and_eval("isl_printer_free(" + str(printer) + ")")
26 return string
28 def display_hint (self):
29 return 'string'
31 class IslIntPrinter:
32 """Print an isl_int """
33 def __init__ (self, val):
34 self.val = val
36 def to_string (self):
37 # Cast val to a void pointer to stop gdb using this pretty
38 # printer for the pointer which would lead to an infinite loop.
39 void_ptr = gdb.lookup_type('void').pointer()
40 value = str(self.val.cast(void_ptr))
42 context = gdb.parse_and_eval("isl_ctx_alloc()")
43 printer = gdb.parse_and_eval("isl_printer_to_str("
44 + str(context) + ")")
45 printer = gdb.parse_and_eval("isl_printer_print_isl_int("
46 + str(printer) + ", "
47 + value + ")")
48 string = gdb.parse_and_eval("(char*)isl_printer_get_str("
49 + str(printer) + ")")
50 gdb.parse_and_eval("isl_printer_free(" + str(printer) + ")")
51 gdb.parse_and_eval("isl_ctx_free(" + str(context) + ")")
52 return string
54 def display_hint (self):
55 return 'string'
57 class IslPrintCommand (gdb.Command):
58 """Print an isl value."""
59 def __init__ (self):
60 super (IslPrintCommand, self).__init__ ("islprint",
61 gdb.COMMAND_OBSCURE)
62 def invoke (self, arg, from_tty):
63 arg = gdb.parse_and_eval(arg);
64 printer = str_lookup_function(arg)
66 if printer == None:
67 print "No isl printer for this type"
68 return
70 print printer.to_string()
72 IslPrintCommand()
74 def str_lookup_function (val):
75 if val.type.code != gdb.TYPE_CODE_PTR:
76 if str(val.type) == "isl_int":
77 return IslIntPrinter(val)
78 else:
79 return None
81 lookup_tag = val.type.target()
82 regex = re.compile ("^isl_(.*)$")
84 if lookup_tag == None:
85 return None
87 m = regex.match (str(lookup_tag))
89 if m:
90 # Those types of printers defined in isl.
91 if m.group(1) in ["basic_set", "set", "union_set", "basic_map",
92 "map", "union_map", "qpolynomial",
93 "pw_qpolynomial", "pw_qpolynomial_fold",
94 "union_pw_qpolynomial",
95 "union_pw_qpolynomial_fold"]:
96 return IslObjectPrinter(val, m.group(1))
97 return None
99 # Do not register the pretty printer.
100 # gdb.current_objfile().pretty_printers.append(str_lookup_function)