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