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  */
26 #include "config.h"
27 
28 #include "dbginfo_t.h"
29 #include "irnode_t.h"
30 #include "type_t.h"
31 #include "entity_t.h"
32 #include "error.h"
33 
34 merge_pair_func *__dbg_info_merge_pair = default_dbg_info_merge_pair;
35 merge_sets_func *__dbg_info_merge_sets = default_dbg_info_merge_sets;
36 
dbg_init(merge_pair_func * mpf,merge_sets_func * msf)37 void dbg_init(merge_pair_func *mpf, merge_sets_func *msf)
38 {
39 	__dbg_info_merge_pair = mpf ? mpf : default_dbg_info_merge_pair;
40 	__dbg_info_merge_sets = msf ? msf : default_dbg_info_merge_sets;
41 }
42 
43 /*
44  * Converts a debug_action into a string.
45  */
dbg_action_2_str(dbg_action a)46 const char *dbg_action_2_str(dbg_action a)
47 {
48 #define CASE(a) case a: return #a
49 
50 	switch (a) {
51 	CASE(dbg_error);
52 	CASE(dbg_opt_ssa);
53 	CASE(dbg_opt_auxnode);
54 	CASE(dbg_const_eval);
55 	CASE(dbg_opt_cse);
56 	CASE(dbg_straightening);
57 	CASE(dbg_if_simplification);
58 	CASE(dbg_algebraic_simplification);
59 	CASE(dbg_write_after_write);
60 	CASE(dbg_write_after_read);
61 	CASE(dbg_read_after_write);
62 	CASE(dbg_read_after_read);
63 	CASE(dbg_read_a_const);
64 	CASE(dbg_rem_poly_call);
65 	CASE(dbg_dead_code);
66 	CASE(dbg_opt_confirm);
67 	CASE(dbg_gvn_pre);
68 	CASE(dbg_combo);
69 	CASE(dbg_jumpthreading);
70 	CASE(dbg_backend);
71 	default:
72 		if (a <= dbg_max)
73 			return "string conversion not implemented";
74 		else
75 			panic("Missing debug action");
76 	}
77 #undef CASE
78 }
79 
default_dbg_info_merge_pair(ir_node * nw,ir_node * old,dbg_action info)80 void default_dbg_info_merge_pair(ir_node *nw, ir_node *old, dbg_action info)
81 {
82 	dbg_info *new_db = get_irn_dbg_info(nw);
83 	(void) info;
84 	if (new_db == NULL)
85 		set_irn_dbg_info(nw, get_irn_dbg_info(old));
86 }
87 
default_dbg_info_merge_sets(ir_node ** new_nodes,int n_new_nodes,ir_node ** old_nodes,int n_old_nodes,dbg_action info)88 void default_dbg_info_merge_sets(ir_node **new_nodes, int n_new_nodes,
89                                  ir_node **old_nodes, int n_old_nodes,
90                                  dbg_action info)
91 {
92 	(void) info;
93 	if (n_old_nodes == 1) {
94 		dbg_info *old_db = get_irn_dbg_info(old_nodes[0]);
95 		int i;
96 
97 		for (i = 0; i < n_new_nodes; ++i)
98 			if (get_irn_dbg_info(new_nodes[i]) == NULL)
99 				set_irn_dbg_info(new_nodes[i], old_db);
100 	}
101 }
102 
default_retrieve_dbg(dbg_info const * const dbg)103 static src_loc_t default_retrieve_dbg(dbg_info const *const dbg)
104 {
105 	(void)dbg;
106 	src_loc_t const loc = { NULL, 0, 0 };
107 	return loc;
108 }
109 
110 /** The debug info retriever function. */
111 static retrieve_dbg_func      retrieve_dbg      = default_retrieve_dbg;
112 static retrieve_type_dbg_func retrieve_type_dbg = NULL;
113 
ir_set_debug_retrieve(retrieve_dbg_func func)114 void ir_set_debug_retrieve(retrieve_dbg_func func)
115 {
116 	retrieve_dbg = func ? func : default_retrieve_dbg;
117 }
118 
ir_retrieve_dbg_info(dbg_info const * const dbg)119 src_loc_t ir_retrieve_dbg_info(dbg_info const *const dbg)
120 {
121 	return retrieve_dbg(dbg);
122 }
123 
ir_set_type_debug_retrieve(retrieve_type_dbg_func func)124 void ir_set_type_debug_retrieve(retrieve_type_dbg_func func)
125 {
126 	retrieve_type_dbg = func;
127 }
128 
ir_retrieve_type_dbg_info(char * buffer,size_t buffer_size,const type_dbg_info * tdbgi)129 void ir_retrieve_type_dbg_info(char *buffer, size_t buffer_size,
130                                const type_dbg_info *tdbgi)
131 {
132 	buffer[0] = '\0';
133 	if (retrieve_type_dbg)
134 		retrieve_type_dbg(buffer, buffer_size, tdbgi);
135 	assert(buffer_size > 0);
136 	buffer[buffer_size-1] = '\0';
137 }
138 
ir_dbg_info_snprint(char * buf,size_t bufsize,const dbg_info * dbg)139 void ir_dbg_info_snprint(char *buf, size_t bufsize, const dbg_info *dbg)
140 {
141 	src_loc_t const loc = ir_retrieve_dbg_info(dbg);
142 
143 	if (!loc.file) {
144 		assert(bufsize > 0);
145 		buf[0] = 0;
146 		return;
147 	}
148 	if (loc.column == 0) {
149 		snprintf(buf, bufsize, "%s:%u", loc.file, loc.line);
150 	} else {
151 		snprintf(buf, bufsize, "%s:%u:%u", loc.file, loc.line, loc.column);
152 	}
153 }
154