49517eb3a000f3a0b9005f3a388c38ab54ae7f5d
[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  *   - the output file name
40  */
41 #ifndef FIRM_IR_IRPROG_H
42 #define FIRM_IR_IRPROG_H
43
44 #include <stddef.h>
45 #include "firm_types.h"
46 #include "irgraph.h"
47 #include "begin.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 ENUM_COUNTABLE(ir_segment_t)
67
68 /**
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.
72  *
73  * @note Think of the irp as the "handle" of a program.
74  */
75 FIRM_API ir_prog *irp;
76
77 typedef enum irp_resources_t {
78         IRP_RESOURCE_NONE         = 0,
79         IRP_RESOURCE_IRG_LINK     = 1 << 0,
80         IRP_RESOURCE_ENTITY_LINK  = 1 << 1,
81         IRP_RESOURCE_TYPE_VISITED = 1 << 2,
82         IRP_RESOURCE_TYPE_LINK    = 1 << 3,
83 } irp_resources_t;
84 ENUM_BITSET(irp_resources_t)
85
86 #ifndef NDEBUG
87 FIRM_API void irp_reserve_resources(ir_prog *irp, irp_resources_t resources);
88 FIRM_API void irp_free_resources(ir_prog *irp, irp_resources_t resources);
89 FIRM_API irp_resources_t irp_resources_reserved(const ir_prog *irp);
90 #else
91 #define irp_reserve_resources(irp, resources) (void)0
92 #define irp_free_resources(irp, resources)    (void)0
93 #define irp_resources_reserved(irp)           0
94 #endif
95
96 /**
97  * Returns the current irp from where everything in the current module
98  * can be accessed.
99  *
100  * @see irp
101  */
102 FIRM_API ir_prog *get_irp(void);
103
104 /**
105  * Set current irp
106  */
107 FIRM_API void set_irp(ir_prog *irp);
108
109 /**
110  * Creates a new ir_prog (a module or compilation unit).
111  * Note: This does not set irp to the newly created ir_prog
112  *
113  * @param name  the name of this irp (module)
114  */
115 FIRM_API ir_prog *new_ir_prog(const char *name);
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 FIRM_API void free_ir_prog(void);
120
121 /** Sets the file name / executable name or the like. Initially the
122     ident 'no_name_set'. */
123 FIRM_API void set_irp_prog_name(ident *name);
124
125 /** Returns true if the user ever set a program name */
126 FIRM_API int irp_prog_name_is_set(void);
127
128 /** Gets the name of the current irp. */
129 FIRM_API ident *get_irp_ident(void);
130
131 /** Gets the name of the current irp. */
132 FIRM_API const char *get_irp_name(void);
133
134 /** Gets the main routine of the compiled program. */
135 FIRM_API ir_graph *get_irp_main_irg(void);
136
137 /** Sets the main routine of the compiled program. */
138 FIRM_API void set_irp_main_irg(ir_graph *main_irg);
139
140 /** Adds irg to the list of ir graphs in the current irp. */
141 FIRM_API void add_irp_irg(ir_graph *irg);
142
143 /** Removes irg from the list of irgs and
144     shrinks the list by one. */
145 FIRM_API void remove_irp_irg_from_list(ir_graph *irg);
146 /** Removes irg from the list of irgs, deallocates it and
147     shrinks the list by one. */
148 FIRM_API void remove_irp_irg(ir_graph *irg);
149
150 /** returns the biggest not used irg index number */
151 FIRM_API size_t get_irp_last_idx(void);
152
153 /** Returns the number of ir graphs in the irp. */
154 FIRM_API size_t get_irp_n_irgs(void);
155
156 /** Returns the ir graph at position pos in the irp. */
157 FIRM_API ir_graph *get_irp_irg(size_t pos);
158
159 /** Sets the ir graph at position pos. */
160 FIRM_API void set_irp_irg(size_t pos, ir_graph *irg);
161
162 /**
163  * Returns the type containing the entities for a segment.
164  *
165  * @param segment  the segment
166  */
167 FIRM_API ir_type *get_segment_type(ir_segment_t segment);
168
169 /**
170  * @brief Changes a segment segment type for the program.
171  * (use with care)
172  */
173 FIRM_API void set_segment_type(ir_segment_t segment, ir_type *new_type);
174
175 /**
176  * Returns the "global" type of the irp.
177  * Upon creation this is an empty class type.
178  * This is a convenience function for get_segment_type(IR_SEGMENT_GLOBAL)
179  */
180 FIRM_API ir_type *get_glob_type(void);
181
182 /**
183  * Returns the "thread local storage" type of the irp.
184  * Upon creation this is an empty struct type.
185  * This is a convenience function for get_segment_type(IR_SEGMENT_THREAD_LOCAL)
186  */
187 FIRM_API ir_type *get_tls_type(void);
188
189 /** Adds type to the list of types in irp. */
190 FIRM_API 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 FIRM_API void remove_irp_type(ir_type *typ);
195
196 /**
197  * Returns the number of all types in the irp.
198  * @deprecated
199  */
200 FIRM_API size_t get_irp_n_types(void);
201
202 /**
203  * Returns the type at position pos in the irp.
204  * @deprecated
205  */
206 FIRM_API ir_type *get_irp_type(size_t pos);
207
208 /**
209  * Overwrites the type at position pos with another type.
210  * @deprecated
211  */
212 FIRM_API void set_irp_type(size_t pos, ir_type *typ);
213
214 /** Returns the number of all modes in the irp. */
215 FIRM_API size_t get_irp_n_modes(void);
216
217 /** Returns the mode at position pos in the irp. */
218 FIRM_API ir_mode *get_irp_mode(size_t pos);
219
220 /** Adds opcode to the list of opcodes in irp. */
221 FIRM_API void add_irp_opcode(ir_op *opcode);
222
223 /** Removes opcode from the list of opcodes, deallocates it and
224     shrinks the list by one. */
225 FIRM_API void remove_irp_opcode(ir_op *opcode);
226
227 /** Returns the number of all opcodes in the irp. */
228 FIRM_API size_t get_irp_n_opcodes(void);
229
230 /** Returns the opcode at position pos in the irp. */
231 FIRM_API ir_op *get_irp_opcode(size_t pos);
232
233 /** Sets the generic function pointer of all opcodes to NULL */
234 FIRM_API void clear_irp_opcodes_generic_func(void);
235
236
237 /**  Return the graph for global constants of the current irp.
238  *
239  *   Returns an irgraph that only contains constant expressions for
240  *   constant entities.  Do not use any access function for this
241  *   graph, do not generate code for this graph.  This graph contains
242  *   only one block.  The constant expressions may not contain control
243  *   flow.
244  *   Walking the graph starting from any node will not reach the block
245  *   or any controlflow.
246  *   See also copy_const_code() in entity.h.
247  */
248 FIRM_API ir_graph *get_const_code_irg(void);
249
250
251 /** The phase state for the program.
252  *
253  *  The phase state of the whole program is
254  *   building:  if at least one graph is state_building
255  *              or one type is incomplete.
256  *   high:      all graphs are in state high or low, all types are constructed.
257  *   low:       all graphs are in state low, all types are in state layout fixed.
258  */
259 FIRM_API irg_phase_state get_irp_phase_state(void);
260 FIRM_API void            set_irp_phase_state(irg_phase_state s);
261
262 FIRM_API irg_outs_state get_irp_ip_outs_state(void);
263 FIRM_API void           set_irp_ip_outs_inconsistent(void);
264
265 /**
266  * Creates an ir_prog pass for set_irp_phase_state().
267  *
268  * @param name   the name of this pass or NULL
269  * @param state  the state to set
270  *
271  * @return  the newly created ir_prog pass
272  */
273 FIRM_API ir_prog_pass_t *set_irp_phase_state_pass(const char *name,
274                                                   irg_phase_state state);
275
276 FIRM_API irg_callee_info_state get_irp_callee_info_state(void);
277 FIRM_API void                  set_irp_callee_info_state(irg_callee_info_state s);
278
279 /** Returns a new, unique exception region number. */
280 FIRM_API ir_exc_region_t get_irp_next_region_nr(void);
281
282 /** Returns a new, unique label number. */
283 FIRM_API ir_label_t get_irp_next_label_nr(void);
284
285 /** Add a new global asm include. */
286 FIRM_API void add_irp_asm(ident *asm_string);
287
288 /** Return the number of global asm includes. */
289 FIRM_API size_t get_irp_n_asms(void);
290
291 /** Return the global asm include at position pos. */
292 FIRM_API ident *get_irp_asm(size_t pos);
293
294 /** Return whether optimization dump vcg graphs */
295 FIRM_API int get_irp_optimization_dumps(void);
296
297 /** Enable vcg dumping of optimization */
298 FIRM_API void enable_irp_optimization_dumps(void);
299
300 #include "end.h"
301
302 #endif