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