fea91c56d5e7db03d9e7d5d735b41f294fdfb428
[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  * @summary
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_COUNT
65 } ir_segment_t;
66
67 /**
68  * Data structure that holds central information about a program
69  * or a module.
70  * One irp is created by libFirm on construction, so irp should never be NULL.
71  *
72  * - main_irg:  The ir graph that is the entry point to the program.
73  *              (Anything not reachable from here may be optimized away
74  *              if this irp represents a whole program.
75  * - irg:       List of all ir graphs in the program or module.
76  * - type:      A list containing all types known to the translated program.
77  *              Some types can have several entries in this list (as a result of
78  *              using exchange_types()).
79  * - glob_type: The unique global type that is owner of all global entities
80  *              of this module.
81  */
82 typedef struct ir_prog ir_prog;
83
84 /**
85  * A variable pointing to the current irp (program or module).
86  * This variable should be considered constant. Moreover, one should use get_irp()
87  * to get access the the irp.
88  *
89  * @note
90  *      Think of the irp as the "handle" of a program.
91  */
92 extern ir_prog *irp;
93
94 #ifndef NDEBUG
95 void irp_reserve_resources(ir_prog *irp, ir_resources_t resources);
96 void irp_free_resources(ir_prog *irp, ir_resources_t resources);
97 ir_resources_t irp_resources_reserved(const ir_prog *irp);
98 #else
99 #define irp_reserve_resources(irp, resources)
100 #define irp_free_resources(irp, resources)
101 #define irp_resources_reserved(irp)   0
102 #endif
103
104 /**
105  * Returns the current irp from where everything in the current module
106  * can be accessed.
107  *
108  * @see irp
109  */
110 ir_prog *get_irp(void);
111
112 /**
113  * Creates a new ir_prog (a module or compilation unit),
114  * returns it and sets irp with it.
115  */
116 ir_prog *new_ir_prog(void);
117
118 /** frees all memory used by irp.  Types in type list and irgs in irg
119  *  list must be freed by hand before. */
120 void free_ir_prog(void);
121
122 /** Sets the file name / executable name or the like. Initially the
123     ident 'no_name_set'. */
124 void set_irp_prog_name(ident *name);
125
126 /** Returns true if the user ever set a program name */
127 int irp_prog_name_is_set(void);
128
129 /** Gets the name of the current irp. */
130 ident *get_irp_ident(void);
131
132 /** Gets the name of the current irp. */
133 const char *get_irp_name(void);
134
135 /** Gets the main routine of the compiled program. */
136 ir_graph *get_irp_main_irg(void);
137
138 /** Sets the main routine of the compiled program. */
139 void set_irp_main_irg(ir_graph *main_irg);
140
141 /** Adds irg to the list of ir graphs in the current irp. */
142 void add_irp_irg(ir_graph *irg);
143
144 /** Removes irg from the list of irgs and
145     shrinks the list by one. */
146 void remove_irp_irg_from_list(ir_graph *irg);
147 /** Removes irg from the list of irgs, deallocates it and
148     shrinks the list by one. */
149 void remove_irp_irg(ir_graph *irg);
150
151 /** returns the biggest not used irg index number */
152 int get_irp_last_idx(void);
153
154 /** Returns the number of ir graphs in the irp. */
155 int get_irp_n_irgs(void);
156
157 /** Returns the ir graph at position pos in the irp. */
158 ir_graph *get_irp_irg(int pos);
159
160 /** Sets the ir graph at position pos. */
161 void set_irp_irg(int pos, ir_graph *irg);
162
163 /** Gets the number of graphs _and_ pseudo graphs. */
164 int get_irp_n_allirgs(void);
165
166 /** Returns the ir graph at position pos of all graphs (including
167  pseudo graphs).  Visits first graphs, then pseudo graphs. */
168 ir_graph *get_irp_allirg(int pos);
169
170 /**
171  * Returns the type containing the entities for a segment.
172  *
173  * @param segment  the segment
174  */
175 ir_type *get_segment_type(ir_segment_t segment);
176
177 /**
178  * Returns the "global" type of the irp.
179  * Upon creation this is an empty class type.
180  * This is a convenience function for get_segment_type(IR_SEGMENT_GLOBAL)
181  */
182 ir_type *get_glob_type(void);
183
184 /**
185  * Returns the "thread local storage" type of the irp.
186  * Upon creation this is an empty struct type.
187  */
188 ir_type *get_tls_type(void);
189
190 /** Adds type to the list of types in irp. */
191 void add_irp_type(ir_type *typ);
192
193 /** Removes type from the list of types, deallocates it and
194     shrinks the list by one. */
195 void remove_irp_type(ir_type *typ);
196
197 /** Returns the number of all types in the irp. */
198 int get_irp_n_types(void);
199
200 /** Returns the type at position pos in the irp. */
201 ir_type *get_irp_type(int pos);
202
203 /** Overwrites the type at position pos with another type. */
204 void set_irp_type(int pos, ir_type *typ);
205
206 /** Returns the number of all modes in the irp. */
207 int get_irp_n_modes(void);
208
209 /** Returns the mode at position pos in the irp. */
210 ir_mode *get_irp_mode(int pos);
211
212 /** Adds opcode to the list of opcodes in irp. */
213 void add_irp_opcode(ir_op *opcode);
214
215 /** Removes opcode from the list of opcodes, deallocates it and
216     shrinks the list by one. */
217 void remove_irp_opcode(ir_op *opcode);
218
219 /** Returns the number of all opcodes in the irp. */
220 int get_irp_n_opcodes(void);
221
222 /** Returns the opcode at position pos in the irp. */
223 ir_op *get_irp_opcode(int pos);
224
225 /** Sets the generic function pointer of all opcodes to NULL */
226 void clear_irp_opcodes_generic_func(void);
227
228
229 /**  Return the graph for global constants of the current irp.
230  *
231  *   Returns an irgraph that only contains constant expressions for
232  *   constant entities.  Do not use any access function for this
233  *   graph, do not generate code for this graph.  This graph contains
234  *   only one block.  The constant expressions may not contain control
235  *   flow.
236  *   Walking the graph starting from any node will not reach the block
237  *   or any controlflow.
238  *   See also copy_const_code() in entity.h.
239  */
240 ir_graph *get_const_code_irg(void);
241
242
243 /** The phase state for the program.
244  *
245  *  The phase state of the whole program is
246  *   building:  if at least one graph is state_building
247  *              or one type is incomplete.
248  *   high:      all graphs are in state high or low, all types are constructed.
249  *   low:       all graphs are in state low, all types are in state layout fixed.
250  */
251 irg_phase_state get_irp_phase_state(void);
252 void            set_irp_phase_state(irg_phase_state s);
253
254 irg_outs_state get_irp_ip_outs_state(void);
255 void           set_irp_ip_outs_inconsistent(void);
256
257
258 irg_callee_info_state get_irp_callee_info_state(void);
259 void                  set_irp_callee_info_state(irg_callee_info_state s);
260
261 /** Returns a new, unique exception region number. */
262 ir_exc_region_t get_irp_next_region_nr(void);
263
264 /** Returns a new, unique label number. */
265 ir_label_t get_irp_next_label_nr(void);
266
267 /** Add a new global asm include. */
268 void add_irp_asm(ident *asm_string);
269
270 /** Return the number of global asm includes. */
271 int get_irp_n_asms(void);
272
273 /** Return the global asm include at position pos. */
274 ident *get_irp_asm(int pos);
275
276 #endif