1import gdb
2import re
3
4# GDB Pretty Printers for most isl objects
5class IslObjectPrinter:
6	"""Print an isl object"""
7	def __init__ (self, val, type):
8		self.val = val
9		self.type = type
10
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
27
28	def display_hint (self):
29		return 'string'
30
31class IslIntPrinter:
32	"""Print an isl_int """
33	def __init__ (self, val):
34		self.val = val
35
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))
41
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
53
54	def display_hint (self):
55		return 'string'
56
57class 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)
65
66		if printer == None:
67			print("No isl printer for this type")
68			return
69
70		print(printer.to_string())
71
72IslPrintCommand()
73
74def 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
80
81	lookup_tag = val.type.target()
82	regex = re.compile ("^isl_(.*)$")
83
84	if lookup_tag == None:
85		return None
86
87	m = regex.match (str(lookup_tag))
88
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
98
99# Do not register the pretty printer.
100# gdb.current_objfile().pretty_printers.append(str_lookup_function)
101