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