2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Entry point to the representation of a whole program.
23 * @author Goetz Lindenmaier, Michael Beck
31 #include "irgraph_t.h"
40 /** The initial name of the irp program. */
41 #define INITAL_PROG_NAME "no_name_set"
44 ir_prog *get_irp(void) { return irp; }
45 void set_irp(ir_prog *new_irp)
51 * Create a new incomplete ir_prog.
53 static ir_prog *new_incomplete_ir_prog(void)
55 ir_prog *res = XMALLOCZ(ir_prog);
57 res->kind = k_ir_prog;
58 res->graphs = NEW_ARR_F(ir_graph *, 0);
59 res->types = NEW_ARR_F(ir_type *, 0);
60 res->global_asms = NEW_ARR_F(ident *, 0);
61 res->last_label_nr = 1; /* 0 is reserved as non-label */
65 res->reserved_resources = IRP_RESOURCE_NONE;
72 * Completes an incomplete irprog.
74 * @param irp the (yet incomplete) irp
75 * @param module_name the (module) name for this irp
77 static void complete_ir_prog(ir_prog *irp, const char *module_name)
79 #define IDENT(x) new_id_from_chars(x, sizeof(x) - 1)
81 irp->name = new_id_from_str(module_name);
82 irp->segment_types[IR_SEGMENT_GLOBAL]
83 = new_type_class(IDENT("GlobalType"));
84 irp->segment_types[IR_SEGMENT_THREAD_LOCAL]
85 = new_type_struct(IDENT("ThreadLocal"));
86 irp->segment_types[IR_SEGMENT_CONSTRUCTORS]
87 = new_type_class(IDENT("Constructors"));
88 irp->segment_types[IR_SEGMENT_DESTRUCTORS]
89 = new_type_class(IDENT("Destructors"));
91 /* Set these flags for debugging. */
92 irp->segment_types[IR_SEGMENT_GLOBAL]->flags |= tf_segment|tf_global_type;
93 irp->segment_types[IR_SEGMENT_THREAD_LOCAL]->flags |= tf_segment|tf_tls_type;
94 irp->segment_types[IR_SEGMENT_CONSTRUCTORS]->flags |= tf_segment|tf_constructors;
95 irp->segment_types[IR_SEGMENT_DESTRUCTORS]->flags |= tf_segment|tf_destructors;
97 /* The global type is a class, but we cannot derive from it, so set
98 the final property to assist optimizations that checks for it. */
99 set_class_final(irp->segment_types[IR_SEGMENT_GLOBAL], 1);
101 irp->const_code_irg = new_const_code_irg();
102 irp->class_cast_state = ir_class_casts_transitive;
103 irp->globals_entity_usage_state = ir_entity_usage_not_computed;
107 void init_irprog_1(void)
109 irp = new_incomplete_ir_prog();
112 void init_irprog_2(void)
114 complete_ir_prog(irp, INITAL_PROG_NAME);
119 ir_prog *new_ir_prog(const char *name)
121 ir_prog *irp = new_incomplete_ir_prog();
122 complete_ir_prog(irp, name);
126 void free_ir_prog(void)
129 /* must iterate backwards here */
130 for (i = get_irp_n_irgs(); i > 0;)
131 free_ir_graph(get_irp_irg(--i));
133 /* free entities first to avoid entity types being destroyed before
134 * the entities using them */
135 for (i = get_irp_n_types(); i > 0;)
136 free_type_entities(get_irp_type(--i));
138 ir_finish_entity(irp);
140 for (i = get_irp_n_types(); i > 0;)
141 free_type(get_irp_type(--i));
143 free_ir_graph(irp->const_code_irg);
147 DEL_ARR_F(irp->graphs);
148 DEL_ARR_F(irp->types);
150 DEL_ARR_F(irp->global_asms);
153 irp->const_code_irg = NULL;
159 ir_graph *get_irp_main_irg(void)
162 return irp->main_irg;
165 void set_irp_main_irg(ir_graph *main_irg)
168 irp->main_irg = main_irg;
171 ir_type *(get_segment_type)(ir_segment_t segment)
173 return get_segment_type_(segment);
176 void set_segment_type(ir_segment_t segment, ir_type *new_type)
178 assert(segment <= IR_SEGMENT_LAST);
179 irp->segment_types[segment] = new_type;
182 ir_type *(get_glob_type)(void)
184 return get_glob_type_();
187 ir_type *(get_tls_type)(void)
189 return get_tls_type_();
192 void add_irp_irg(ir_graph *irg)
195 assert(irp && irp->graphs);
196 ARR_APP1(ir_graph *, irp->graphs, irg);
199 void remove_irp_irg_from_list(ir_graph *irg)
204 l = ARR_LEN(irp->graphs);
205 for (i = 0; i < l; ++i) {
206 if (irp->graphs[i] == irg) {
207 for (; i < l - 1; ++i) {
208 irp->graphs[i] = irp->graphs[i+1];
210 ARR_SETLEN(ir_graph*, irp->graphs, l - 1);
216 void remove_irp_irg(ir_graph *irg)
219 remove_irp_irg_from_list(irg);
222 size_t (get_irp_n_irgs)(void)
224 return get_irp_n_irgs_();
227 ir_graph *(get_irp_irg)(size_t pos)
229 return get_irp_irg_(pos);
232 size_t get_irp_last_idx(void)
234 return irp->max_irg_idx;
237 void set_irp_irg(size_t pos, ir_graph *irg)
240 assert(pos < ARR_LEN(irp->graphs));
241 irp->graphs[pos] = irg;
244 void add_irp_type(ir_type *typ)
248 ARR_APP1(ir_type *, irp->types, typ);
251 void remove_irp_type(ir_type *typ)
256 l = ARR_LEN(irp->types);
257 for (i = 0; i < l; ++i) {
258 if (irp->types[i] == typ) {
259 for (; i < l - 1; ++i) {
260 irp->types[i] = irp->types[i+1];
262 ARR_SETLEN(ir_type *, irp->types, l - 1);
268 size_t (get_irp_n_types) (void)
270 return get_irp_n_types_();
273 ir_type *(get_irp_type) (size_t pos)
275 return get_irp_type_(pos);
278 void set_irp_type(size_t pos, ir_type *typ)
281 assert(pos < ARR_LEN((irp)->types));
282 irp->types[pos] = typ;
285 void set_irp_prog_name(ident *name)
289 int irp_prog_name_is_set(void)
291 return irp->name != new_id_from_str(INITAL_PROG_NAME);
293 ident *get_irp_ident(void)
297 const char *get_irp_name(void)
299 return get_id_str(irp->name);
303 ir_graph *(get_const_code_irg)(void)
305 return get_const_code_irg_();
308 void set_irp_ip_outedges(ir_node ** ip_outedges)
310 irp->ip_outedges = ip_outedges;
313 ir_node** get_irp_ip_outedges(void)
315 return irp->ip_outedges;
318 irg_callee_info_state get_irp_callee_info_state(void)
320 return irp->callee_info_state;
323 void set_irp_callee_info_state(irg_callee_info_state s)
325 irp->callee_info_state = s;
328 ir_label_t (get_irp_next_label_nr)(void)
330 return get_irp_next_label_nr_();
333 void add_irp_asm(ident *asm_string)
335 ARR_APP1(ident *, irp->global_asms, asm_string);
338 size_t get_irp_n_asms(void)
340 return ARR_LEN(irp->global_asms);
343 ident *get_irp_asm(size_t pos)
345 assert(pos < get_irp_n_asms());
346 return irp->global_asms[pos];
350 void irp_reserve_resources(ir_prog *irp, irp_resources_t resources)
352 assert((irp->reserved_resources & resources) == 0);
353 irp->reserved_resources |= resources;
356 void irp_free_resources(ir_prog *irp, irp_resources_t resources)
358 assert((irp->reserved_resources & resources) == resources);
359 irp->reserved_resources &= ~resources;
362 irp_resources_t irp_resources_reserved(const ir_prog *irp)
364 return irp->reserved_resources;