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