make firm compilable with a c++ compiler
[libfirm] / ir / debug / dbginfo.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief    Implements the Firm interface to debug information.
23  * @author   Goetz Lindenmaier, Michael Beck
24  * @date     2001
25  * @version  $Id$
26  */
27 #include "config.h"
28
29 #include "dbginfo_t.h"
30 #include "irnode_t.h"
31 #include "type_t.h"
32 #include "entity_t.h"
33 #include "error.h"
34
35 merge_pair_func *__dbg_info_merge_pair = default_dbg_info_merge_pair;
36 merge_sets_func *__dbg_info_merge_sets = default_dbg_info_merge_sets;
37
38 void dbg_init(merge_pair_func *mpf, merge_sets_func *msf)
39 {
40         __dbg_info_merge_pair = mpf ? mpf : default_dbg_info_merge_pair;
41         __dbg_info_merge_sets = msf ? msf : default_dbg_info_merge_sets;
42 }
43
44 /*
45  * Converts a debug_action into a string.
46  */
47 const char *dbg_action_2_str(dbg_action a)
48 {
49 #define CASE(a) case a: return #a
50
51         switch (a) {
52         CASE(dbg_error);
53         CASE(dbg_opt_ssa);
54         CASE(dbg_opt_auxnode);
55         CASE(dbg_const_eval);
56         CASE(dbg_opt_cse);
57         CASE(dbg_straightening);
58         CASE(dbg_if_simplification);
59         CASE(dbg_algebraic_simplification);
60         CASE(dbg_write_after_write);
61         CASE(dbg_write_after_read);
62         CASE(dbg_read_after_write);
63         CASE(dbg_read_after_read);
64         CASE(dbg_read_a_const);
65         CASE(dbg_rem_poly_call);
66         CASE(dbg_dead_code);
67         CASE(dbg_opt_confirm);
68         CASE(dbg_gvn_pre);
69         CASE(dbg_combo);
70         CASE(dbg_jumpthreading);
71         CASE(dbg_backend);
72         default:
73                 if (a <= dbg_max)
74                         return "string conversion not implemented";
75                 else
76                         panic("Missing debug action in dbg_action_2_str()");
77         }
78 #undef CASE
79 }
80
81 void default_dbg_info_merge_pair(ir_node *nw, ir_node *old, dbg_action info)
82 {
83         dbg_info *new_db = get_irn_dbg_info(nw);
84         (void) info;
85         if (new_db == NULL)
86                 set_irn_dbg_info(nw, get_irn_dbg_info(old));
87 }
88
89 void default_dbg_info_merge_sets(ir_node **new_nodes, int n_new_nodes,
90                                  ir_node **old_nodes, int n_old_nodes,
91                                  dbg_action info)
92 {
93         (void) info;
94         if (n_old_nodes == 1) {
95                 dbg_info *old_db = get_irn_dbg_info(old_nodes[0]);
96                 int i;
97
98                 for (i = 0; i < n_new_nodes; ++i)
99                         if (get_irn_dbg_info(new_nodes[i]) == NULL)
100                                 set_irn_dbg_info(new_nodes[i], old_db);
101         }
102 }
103
104 /** The debug info retriever function. */
105 static retrieve_dbg_func      retrieve_dbg      = NULL;
106 static retrieve_type_dbg_func retrieve_type_dbg = NULL;
107
108 void ir_set_debug_retrieve(retrieve_dbg_func func)
109 {
110         retrieve_dbg = func;
111 }
112
113 const char *ir_retrieve_dbg_info(const dbg_info *dbg, unsigned *line)
114 {
115         if (retrieve_dbg)
116                 return retrieve_dbg(dbg, line);
117
118         *line = 0;
119         return NULL;
120 }
121
122 void ir_set_type_debug_retrieve(retrieve_type_dbg_func func)
123 {
124         retrieve_type_dbg = func;
125 }
126
127 void ir_retrieve_type_dbg_info(char *buffer, size_t buffer_size,
128                                const type_dbg_info *tdbgi)
129 {
130         buffer[0] = '\0';
131         if (retrieve_type_dbg)
132                 retrieve_type_dbg(buffer, buffer_size, tdbgi);
133         assert(buffer_size > 0);
134         buffer[buffer_size-1] = '\0';
135 }
136
137 void ir_dbg_info_snprint(char *buf, size_t bufsize, const dbg_info *dbg)
138 {
139         unsigned    line;
140         const char *source = ir_retrieve_dbg_info(dbg, &line);
141
142         if (source == NULL) {
143                 assert(bufsize > 0);
144                 buf[0] = 0;
145                 return;
146         }
147         snprintf(buf, bufsize, "%s:%u", source, line);
148 }