1d6f6bd60ca0bfaf7e5fa2ba2c488fe270768cad
[libfirm] / include / libfirm / irprog.h
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   Entry point to the representation of a whole program.
23  * @author  Goetz Lindenmaier
24  * @date    2000
25  * @version $Id$
26  * @brief
27  *  Intermediate Representation (IR) of a program.
28  *
29  *  This file defines a construct that keeps all information about a
30  *  program:
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
42  */
43 #ifndef FIRM_IR_IRPROG_H
44 #define FIRM_IR_IRPROG_H
45
46 #include "firm_types.h"
47 #include "irgraph.h"
48
49 typedef enum ir_segment_t {
50         IR_SEGMENT_FIRST,
51         /** "normal" global data */
52         IR_SEGMENT_GLOBAL = IR_SEGMENT_FIRST,
53         /** thread local storage segment */
54         IR_SEGMENT_THREAD_LOCAL,
55         /**
56          * the constructors segment. Contains pointers to functions which are
57          * executed on module initialization (program start or when a library is
58          * dynamically loaded)
59          */
60         IR_SEGMENT_CONSTRUCTORS,
61         /** like constructors, but functions are executed on module exit */
62         IR_SEGMENT_DESTRUCTORS,
63
64         IR_SEGMENT_LAST = IR_SEGMENT_DESTRUCTORS
65 } ir_segment_t;
66
67 /**
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.
71  *
72  * @note
73  *      Think of the irp as the "handle" of a program.
74  */
75 extern ir_prog *irp;
76
77 #ifndef NDEBUG
78 void irp_reserve_resources(ir_prog *irp, ir_resources_t resources);
79 void irp_free_resources(ir_prog *irp, ir_resources_t resources);
80 ir_resources_t irp_resources_reserved(const ir_prog *irp);
81 #else
82 #define irp_reserve_resources(irp, resources)
83 #define irp_free_resources(irp, resources)
84 #define irp_resources_reserved(irp)   0
85 #endif
86
87 /**
88  * Returns the current irp from where everything in the current module
89  * can be accessed.
90  *
91  * @see irp
92  */
93 ir_prog *get_irp(void);
94
95 /**
96  * Creates a new ir_prog (a module or compilation unit),
97  * returns it and sets irp with it.
98  *
99  * @param name  the name of this irp (module)
100  */
101 ir_prog *new_ir_prog(const char *name);
102
103 /** frees all memory used by irp.  Types in type list and irgs in irg
104  *  list must be freed by hand before. */
105 void free_ir_prog(void);
106
107 /** Sets the file name / executable name or the like. Initially the
108     ident 'no_name_set'. */
109 void set_irp_prog_name(ident *name);
110
111 /** Returns true if the user ever set a program name */
112 int irp_prog_name_is_set(void);
113
114 /** Gets the name of the current irp. */
115 ident *get_irp_ident(void);
116
117 /** Gets the name of the current irp. */
118 const char *get_irp_name(void);
119
120 /** Gets the main routine of the compiled program. */
121 ir_graph *get_irp_main_irg(void);
122
123 /** Sets the main routine of the compiled program. */
124 void set_irp_main_irg(ir_graph *main_irg);
125
126 /** Adds irg to the list of ir graphs in the current irp. */
127 void add_irp_irg(ir_graph *irg);
128
129 /** Removes irg from the list of irgs and
130     shrinks the list by one. */
131 void remove_irp_irg_from_list(ir_graph *irg);
132 /** Removes irg from the list of irgs, deallocates it and
133     shrinks the list by one. */
134 void remove_irp_irg(ir_graph *irg);
135
136 /** returns the biggest not used irg index number */
137 int get_irp_last_idx(void);
138
139 /** Returns the number of ir graphs in the irp. */
140 int get_irp_n_irgs(void);
141
142 /** Returns the ir graph at position pos in the irp. */
143 ir_graph *get_irp_irg(int pos);
144
145 /** Sets the ir graph at position pos. */
146 void set_irp_irg(int pos, ir_graph *irg);
147
148 /** Gets the number of graphs _and_ pseudo graphs. */
149 int get_irp_n_allirgs(void);
150
151 /** Returns the ir graph at position pos of all graphs (including
152  pseudo graphs).  Visits first graphs, then pseudo graphs. */
153 ir_graph *get_irp_allirg(int pos);
154
155 /**
156  * Returns the type containing the entities for a segment.
157  *
158  * @param segment  the segment
159  */
160 ir_type *get_segment_type(ir_segment_t segment);
161
162 /**
163  * @brief Changes a segment segment type for the program.
164  * (use with care)
165  */
166 void set_segment_type(ir_segment_t segment, ir_type *new_type);
167
168 /**
169  * Returns the "global" type of the irp.
170  * Upon creation this is an empty class type.
171  * This is a convenience function for get_segment_type(IR_SEGMENT_GLOBAL)
172  */
173 ir_type *get_glob_type(void);
174
175 /**
176  * Returns the "thread local storage" type of the irp.
177  * Upon creation this is an empty struct type.
178  * This is a convenience function for get_segment_type(IR_SEGMENT_THREAD_LOCAL)
179  */
180 ir_type *get_tls_type(void);
181
182 /** Adds type to the list of types in irp. */
183 void add_irp_type(ir_type *typ);
184
185 /** Removes type from the list of types, deallocates it and
186     shrinks the list by one. */
187 void remove_irp_type(ir_type *typ);
188
189 /**
190  * Returns the number of all types in the irp.
191  * @deprecated
192  */
193 int get_irp_n_types(void);
194
195 /**
196  * Returns the type at position pos in the irp.
197  * @deprecated
198  */
199 ir_type *get_irp_type(int pos);
200
201 /**
202  * Overwrites the type at position pos with another type.
203  * @deprecated
204  */
205 void set_irp_type(int pos, ir_type *typ);
206
207 /** Returns the number of all modes in the irp. */
208 int get_irp_n_modes(void);
209
210 /** Returns the mode at position pos in the irp. */
211 ir_mode *get_irp_mode(int pos);
212
213 /** Adds opcode to the list of opcodes in irp. */
214 void add_irp_opcode(ir_op *opcode);
215
216 /** Removes opcode from the list of opcodes, deallocates it and
217     shrinks the list by one. */
218 void remove_irp_opcode(ir_op *opcode);
219
220 /** Returns the number of all opcodes in the irp. */
221 int get_irp_n_opcodes(void);
222
223 /** Returns the opcode at position pos in the irp. */
224 ir_op *get_irp_opcode(int pos);
225
226 /** Sets the generic function pointer of all opcodes to NULL */
227 void clear_irp_opcodes_generic_func(void);
228
229
230 /**  Return the graph for global constants of the current irp.
231  *
232  *   Returns an irgraph that only contains constant expressions for
233  *   constant entities.  Do not use any access function for this
234  *   graph, do not generate code for this graph.  This graph contains
235  *   only one block.  The constant expressions may not contain control
236  *   flow.
237  *   Walking the graph starting from any node will not reach the block
238  *   or any controlflow.
239  *   See also copy_const_code() in entity.h.
240  */
241 ir_graph *get_const_code_irg(void);
242
243
244 /** The phase state for the program.
245  *
246  *  The phase state of the whole program is
247  *   building:  if at least one graph is state_building
248  *              or one type is incomplete.
249  *   high:      all graphs are in state high or low, all types are constructed.
250  *   low:       all graphs are in state low, all types are in state layout fixed.
251  */
252 irg_phase_state get_irp_phase_state(void);
253 void            set_irp_phase_state(irg_phase_state s);
254
255 irg_outs_state get_irp_ip_outs_state(void);
256 void           set_irp_ip_outs_inconsistent(void);
257
258 /**
259  * Creates an ir_prog pass for set_irp_phase_state().
260  *
261  * @param name   the name of this pass or NULL
262  * @param state  the state to set
263  *
264  * @return  the newly created ir_prog pass
265  */
266 ir_prog_pass_t *set_irp_phase_state_pass(const char *name, irg_phase_state state);
267
268 irg_callee_info_state get_irp_callee_info_state(void);
269 void                  set_irp_callee_info_state(irg_callee_info_state s);
270
271 /** Returns a new, unique exception region number. */
272 ir_exc_region_t get_irp_next_region_nr(void);
273
274 /** Returns a new, unique label number. */
275 ir_label_t get_irp_next_label_nr(void);
276
277 /** Add a new global asm include. */
278 void add_irp_asm(ident *asm_string);
279
280 /** Return the number of global asm includes. */
281 int get_irp_n_asms(void);
282
283 /** Return the global asm include at position pos. */
284 ident *get_irp_asm(int pos);
285
286 #endif