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
27 * Intermediate Representation (IR) of a program.
29 * This file defines a construct that keeps all information about a
31 * - A reference point to the method to be executed on program start.
32 * - A list of all procedures.
33 * - A list of all types.
34 * - A global type that contains all global variables and procedures that do
35 * not belong to a class. This type represents the data segment of the
36 * program. It is not the base class of
37 * all classes in a class hierarchy (as, e.g., "object" in java).
38 * - A degenerated graph that contains constant expressions.
39 * - interprocedural outs state.
40 * - a flag indicating validity of the interprocedural representation.
41 * - the output file name
43 #ifndef FIRM_IR_IRPROG_H
44 #define FIRM_IR_IRPROG_H
46 #include "firm_types.h"
50 typedef enum ir_segment_t {
52 /** "normal" global data */
53 IR_SEGMENT_GLOBAL = IR_SEGMENT_FIRST,
54 /** thread local storage segment */
55 IR_SEGMENT_THREAD_LOCAL,
57 * the constructors segment. Contains pointers to functions which are
58 * executed on module initialization (program start or when a library is
61 IR_SEGMENT_CONSTRUCTORS,
62 /** like constructors, but functions are executed on module exit */
63 IR_SEGMENT_DESTRUCTORS,
65 IR_SEGMENT_LAST = IR_SEGMENT_DESTRUCTORS
69 * A variable pointing to the current irp (program or module).
70 * This variable should be considered constant. Moreover, one should use get_irp()
71 * to get access the the irp.
74 * Think of the irp as the "handle" of a program.
76 FIRM_API ir_prog *irp;
79 FIRM_API void irp_reserve_resources(ir_prog *irp, ir_resources_t resources);
80 FIRM_API void irp_free_resources(ir_prog *irp, ir_resources_t resources);
81 FIRM_API ir_resources_t irp_resources_reserved(const ir_prog *irp);
83 #define irp_reserve_resources(irp, resources)
84 #define irp_free_resources(irp, resources)
85 #define irp_resources_reserved(irp) 0
89 * Returns the current irp from where everything in the current module
94 FIRM_API ir_prog *get_irp(void);
97 * Creates a new ir_prog (a module or compilation unit),
98 * returns it and sets irp with it.
100 * @param name the name of this irp (module)
102 FIRM_API ir_prog *new_ir_prog(const char *name);
104 /** frees all memory used by irp. Types in type list and irgs in irg
105 * list must be freed by hand before. */
106 FIRM_API void free_ir_prog(void);
108 /** Sets the file name / executable name or the like. Initially the
109 ident 'no_name_set'. */
110 FIRM_API void set_irp_prog_name(ident *name);
112 /** Returns true if the user ever set a program name */
113 FIRM_API int irp_prog_name_is_set(void);
115 /** Gets the name of the current irp. */
116 FIRM_API ident *get_irp_ident(void);
118 /** Gets the name of the current irp. */
119 FIRM_API const char *get_irp_name(void);
121 /** Gets the main routine of the compiled program. */
122 FIRM_API ir_graph *get_irp_main_irg(void);
124 /** Sets the main routine of the compiled program. */
125 FIRM_API void set_irp_main_irg(ir_graph *main_irg);
127 /** Adds irg to the list of ir graphs in the current irp. */
128 FIRM_API void add_irp_irg(ir_graph *irg);
130 /** Removes irg from the list of irgs and
131 shrinks the list by one. */
132 FIRM_API void remove_irp_irg_from_list(ir_graph *irg);
133 /** Removes irg from the list of irgs, deallocates it and
134 shrinks the list by one. */
135 FIRM_API void remove_irp_irg(ir_graph *irg);
137 /** returns the biggest not used irg index number */
138 FIRM_API int get_irp_last_idx(void);
140 /** Returns the number of ir graphs in the irp. */
141 FIRM_API int get_irp_n_irgs(void);
143 /** Returns the ir graph at position pos in the irp. */
144 FIRM_API ir_graph *get_irp_irg(int pos);
146 /** Sets the ir graph at position pos. */
147 FIRM_API void set_irp_irg(int pos, ir_graph *irg);
149 /** Gets the number of graphs _and_ pseudo graphs. */
150 FIRM_API int get_irp_n_allirgs(void);
152 /** Returns the ir graph at position pos of all graphs (including
153 pseudo graphs). Visits first graphs, then pseudo graphs. */
154 FIRM_API ir_graph *get_irp_allirg(int pos);
157 * Returns the type containing the entities for a segment.
159 * @param segment the segment
161 FIRM_API ir_type *get_segment_type(ir_segment_t segment);
164 * @brief Changes a segment segment type for the program.
167 FIRM_API void set_segment_type(ir_segment_t segment, ir_type *new_type);
170 * Returns the "global" type of the irp.
171 * Upon creation this is an empty class type.
172 * This is a convenience function for get_segment_type(IR_SEGMENT_GLOBAL)
174 FIRM_API ir_type *get_glob_type(void);
177 * Returns the "thread local storage" type of the irp.
178 * Upon creation this is an empty struct type.
179 * This is a convenience function for get_segment_type(IR_SEGMENT_THREAD_LOCAL)
181 FIRM_API ir_type *get_tls_type(void);
183 /** Adds type to the list of types in irp. */
184 FIRM_API void add_irp_type(ir_type *typ);
186 /** Removes type from the list of types, deallocates it and
187 shrinks the list by one. */
188 FIRM_API void remove_irp_type(ir_type *typ);
191 * Returns the number of all types in the irp.
194 FIRM_API int get_irp_n_types(void);
197 * Returns the type at position pos in the irp.
200 FIRM_API ir_type *get_irp_type(int pos);
203 * Overwrites the type at position pos with another type.
206 FIRM_API void set_irp_type(int pos, ir_type *typ);
208 /** Returns the number of all modes in the irp. */
209 FIRM_API int get_irp_n_modes(void);
211 /** Returns the mode at position pos in the irp. */
212 FIRM_API ir_mode *get_irp_mode(int pos);
214 /** Adds opcode to the list of opcodes in irp. */
215 FIRM_API void add_irp_opcode(ir_op *opcode);
217 /** Removes opcode from the list of opcodes, deallocates it and
218 shrinks the list by one. */
219 FIRM_API void remove_irp_opcode(ir_op *opcode);
221 /** Returns the number of all opcodes in the irp. */
222 FIRM_API int get_irp_n_opcodes(void);
224 /** Returns the opcode at position pos in the irp. */
225 FIRM_API ir_op *get_irp_opcode(int pos);
227 /** Sets the generic function pointer of all opcodes to NULL */
228 FIRM_API void clear_irp_opcodes_generic_func(void);
231 /** Return the graph for global constants of the current irp.
233 * Returns an irgraph that only contains constant expressions for
234 * constant entities. Do not use any access function for this
235 * graph, do not generate code for this graph. This graph contains
236 * only one block. The constant expressions may not contain control
238 * Walking the graph starting from any node will not reach the block
239 * or any controlflow.
240 * See also copy_const_code() in entity.h.
242 FIRM_API ir_graph *get_const_code_irg(void);
245 /** The phase state for the program.
247 * The phase state of the whole program is
248 * building: if at least one graph is state_building
249 * or one type is incomplete.
250 * high: all graphs are in state high or low, all types are constructed.
251 * low: all graphs are in state low, all types are in state layout fixed.
253 FIRM_API irg_phase_state get_irp_phase_state(void);
254 FIRM_API void set_irp_phase_state(irg_phase_state s);
256 FIRM_API irg_outs_state get_irp_ip_outs_state(void);
257 FIRM_API void set_irp_ip_outs_inconsistent(void);
260 * Creates an ir_prog pass for set_irp_phase_state().
262 * @param name the name of this pass or NULL
263 * @param state the state to set
265 * @return the newly created ir_prog pass
267 FIRM_API ir_prog_pass_t *set_irp_phase_state_pass(const char *name,
268 irg_phase_state state);
270 FIRM_API irg_callee_info_state get_irp_callee_info_state(void);
271 FIRM_API void set_irp_callee_info_state(irg_callee_info_state s);
273 /** Returns a new, unique exception region number. */
274 FIRM_API ir_exc_region_t get_irp_next_region_nr(void);
276 /** Returns a new, unique label number. */
277 FIRM_API ir_label_t get_irp_next_label_nr(void);
279 /** Add a new global asm include. */
280 FIRM_API void add_irp_asm(ident *asm_string);
282 /** Return the number of global asm includes. */
283 FIRM_API int get_irp_n_asms(void);
285 /** Return the global asm include at position pos. */
286 FIRM_API ident *get_irp_asm(int pos);