2 * Copyright (C) 1995-2008 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
26 * Intermediate Representation (IR) of a program.
28 * This file defines a construct that keeps all information about a
30 * - A reference point to the method to be executed on program start.
31 * - A list of all procedures.
32 * - A list of all types.
33 * - A global type that contains all global variables and procedures that do
34 * not belong to a class. This type represents the data segment of the
35 * program. It is not the base class of
36 * all classes in a class hierarchy (as, e.g., "object" in java).
37 * - A degenerated graph that contains constant expressions.
38 * - the output file name
40 #ifndef FIRM_IR_IRPROG_H
41 #define FIRM_IR_IRPROG_H
44 #include "firm_types.h"
48 typedef enum ir_segment_t {
50 /** "normal" global data */
51 IR_SEGMENT_GLOBAL = IR_SEGMENT_FIRST,
52 /** thread local storage segment */
53 IR_SEGMENT_THREAD_LOCAL,
55 * the constructors segment. Contains pointers to functions which are
56 * executed on module initialization (program start or when a library is
59 IR_SEGMENT_CONSTRUCTORS,
60 /** like constructors, but functions are executed on module exit */
61 IR_SEGMENT_DESTRUCTORS,
63 IR_SEGMENT_LAST = IR_SEGMENT_DESTRUCTORS
65 ENUM_COUNTABLE(ir_segment_t)
68 * A variable pointing to the current irp (program or module).
69 * This variable should be considered constant. Moreover, one should use get_irp()
70 * to get access the the irp.
72 * @note Think of the irp as the "handle" of a program.
74 FIRM_API ir_prog *irp;
76 typedef enum irp_resources_t {
77 IRP_RESOURCE_NONE = 0,
78 IRP_RESOURCE_IRG_LINK = 1 << 0,
79 IRP_RESOURCE_ENTITY_LINK = 1 << 1,
80 IRP_RESOURCE_TYPE_VISITED = 1 << 2,
81 IRP_RESOURCE_TYPE_LINK = 1 << 3,
83 ENUM_BITSET(irp_resources_t)
86 FIRM_API void irp_reserve_resources(ir_prog *irp, irp_resources_t resources);
87 FIRM_API void irp_free_resources(ir_prog *irp, irp_resources_t resources);
88 FIRM_API irp_resources_t irp_resources_reserved(const ir_prog *irp);
90 #define irp_reserve_resources(irp, resources) (void)0
91 #define irp_free_resources(irp, resources) (void)0
92 #define irp_resources_reserved(irp) 0
96 * Returns the current irp from where everything in the current module
101 FIRM_API ir_prog *get_irp(void);
106 FIRM_API void set_irp(ir_prog *irp);
109 * Creates a new ir_prog (a module or compilation unit).
110 * Note: This does not set irp to the newly created ir_prog
112 * @param name the name of this irp (module)
114 FIRM_API ir_prog *new_ir_prog(const char *name);
116 /** frees all memory used by irp. Types in type list and irgs in irg
117 * list must be freed by hand before. */
118 FIRM_API void free_ir_prog(void);
120 /** Sets the file name / executable name or the like. Initially the
121 ident 'no_name_set'. */
122 FIRM_API void set_irp_prog_name(ident *name);
124 /** Returns true if the user ever set a program name */
125 FIRM_API int irp_prog_name_is_set(void);
127 /** Gets the name of the current irp. */
128 FIRM_API ident *get_irp_ident(void);
130 /** Gets the name of the current irp. */
131 FIRM_API const char *get_irp_name(void);
133 /** Gets the main routine of the compiled program. */
134 FIRM_API ir_graph *get_irp_main_irg(void);
136 /** Sets the main routine of the compiled program. */
137 FIRM_API void set_irp_main_irg(ir_graph *main_irg);
139 /** Adds irg to the list of ir graphs in the current irp. */
140 FIRM_API void add_irp_irg(ir_graph *irg);
142 /** Removes irg from the list of irgs and
143 shrinks the list by one. */
144 FIRM_API void remove_irp_irg_from_list(ir_graph *irg);
145 /** Removes irg from the list of irgs, deallocates it and
146 shrinks the list by one. */
147 FIRM_API void remove_irp_irg(ir_graph *irg);
149 /** returns the biggest not used irg index number */
150 FIRM_API size_t get_irp_last_idx(void);
152 /** Returns the number of ir graphs in the irp. */
153 FIRM_API size_t get_irp_n_irgs(void);
155 /** Returns the ir graph at position pos in the irp. */
156 FIRM_API ir_graph *get_irp_irg(size_t pos);
158 /** Sets the ir graph at position pos. */
159 FIRM_API void set_irp_irg(size_t pos, ir_graph *irg);
162 * Returns the type containing the entities for a segment.
164 * @param segment the segment
166 FIRM_API ir_type *get_segment_type(ir_segment_t segment);
169 * @brief Changes a segment segment type for the program.
172 FIRM_API void set_segment_type(ir_segment_t segment, ir_type *new_type);
175 * Returns the "global" type of the irp.
176 * Upon creation this is an empty class type.
177 * This is a convenience function for get_segment_type(IR_SEGMENT_GLOBAL)
179 FIRM_API ir_type *get_glob_type(void);
182 * Returns the "thread local storage" type of the irp.
183 * Upon creation this is an empty struct type.
184 * This is a convenience function for get_segment_type(IR_SEGMENT_THREAD_LOCAL)
186 FIRM_API ir_type *get_tls_type(void);
188 /** Adds type to the list of types in irp. */
189 FIRM_API void add_irp_type(ir_type *typ);
191 /** Removes type from the list of types, deallocates it and
192 shrinks the list by one. */
193 FIRM_API void remove_irp_type(ir_type *typ);
196 * Returns the number of all types in the irp.
199 FIRM_API size_t get_irp_n_types(void);
202 * Returns the type at position pos in the irp.
205 FIRM_API ir_type *get_irp_type(size_t pos);
208 * Overwrites the type at position pos with another type.
211 FIRM_API void set_irp_type(size_t pos, ir_type *typ);
213 /** Returns the number of all modes in the irp. */
214 FIRM_API size_t get_irp_n_modes(void);
216 /** Returns the mode at position pos in the irp. */
217 FIRM_API ir_mode *get_irp_mode(size_t pos);
219 /** Adds opcode to the list of opcodes in irp. */
220 FIRM_API void add_irp_opcode(ir_op *opcode);
222 /** Removes opcode from the list of opcodes, deallocates it and
223 shrinks the list by one. */
224 FIRM_API void remove_irp_opcode(ir_op *opcode);
226 /** Returns the number of all opcodes in the irp. */
227 FIRM_API size_t get_irp_n_opcodes(void);
229 /** Returns the opcode at position pos in the irp. */
230 FIRM_API ir_op *get_irp_opcode(size_t pos);
232 /** Sets the generic function pointer of all opcodes to NULL */
233 FIRM_API void clear_irp_opcodes_generic_func(void);
236 /** Return the graph for global constants of the current irp.
238 * Returns an irgraph that only contains constant expressions for
239 * constant entities. Do not use any access function for this
240 * graph, do not generate code for this graph. This graph contains
241 * only one block. The constant expressions may not contain control
243 * Walking the graph starting from any node will not reach the block
244 * or any controlflow.
245 * See also copy_const_code() in entity.h.
247 FIRM_API ir_graph *get_const_code_irg(void);
250 /** The phase state for the program.
252 * The phase state of the whole program is
253 * building: if at least one graph is state_building
254 * or one type is incomplete.
255 * high: all graphs are in state high or low, all types are constructed.
256 * low: all graphs are in state low, all types are in state layout fixed.
258 FIRM_API irg_phase_state get_irp_phase_state(void);
259 FIRM_API void set_irp_phase_state(irg_phase_state s);
262 * Creates an ir_prog pass for set_irp_phase_state().
264 * @param name the name of this pass or NULL
265 * @param state the state to set
267 * @return the newly created ir_prog pass
269 FIRM_API ir_prog_pass_t *set_irp_phase_state_pass(const char *name,
270 irg_phase_state state);
272 FIRM_API irg_callee_info_state get_irp_callee_info_state(void);
273 FIRM_API void set_irp_callee_info_state(irg_callee_info_state s);
275 /** Returns a new, unique exception region number. */
276 FIRM_API ir_exc_region_t get_irp_next_region_nr(void);
278 /** Returns a new, unique label number. */
279 FIRM_API ir_label_t get_irp_next_label_nr(void);
281 /** Add a new global asm include. */
282 FIRM_API void add_irp_asm(ident *asm_string);
284 /** Return the number of global asm includes. */
285 FIRM_API size_t get_irp_n_asms(void);
287 /** Return the global asm include at position pos. */
288 FIRM_API ident *get_irp_asm(size_t pos);
290 /** Return whether optimization dump vcg graphs */
291 FIRM_API int get_irp_optimization_dumps(void);
293 /** Enable vcg dumping of optimization */
294 FIRM_API void enable_irp_optimization_dumps(void);