Add wrapper macros for pset_first() and pset_next(), which have the return type as...
[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 /**
55  * Segment
56  *
57  * A progrom has a number of special segments at the toplevel which modify
58  * the behaviour of the entities in them.
59  */
60 typedef enum ir_segment_t {
61         IR_SEGMENT_FIRST,
62         /** "normal" global data */
63         IR_SEGMENT_GLOBAL = IR_SEGMENT_FIRST,
64         /** thread local storage segment */
65         IR_SEGMENT_THREAD_LOCAL,
66         /**
67          * the constructors segment. Contains pointers to functions which are
68          * executed on module initialization (program start or when a library is
69          * dynamically loaded)
70          */
71         IR_SEGMENT_CONSTRUCTORS,
72         /** like constructors, but functions are executed on module exit */
73         IR_SEGMENT_DESTRUCTORS,
74
75         IR_SEGMENT_LAST = IR_SEGMENT_DESTRUCTORS
76 } ir_segment_t;
77 ENUM_COUNTABLE(ir_segment_t)
78
79 /**
80  * A variable pointing to the current irp (program or module).
81  * This variable should be considered constant. Moreover, one should use get_irp()
82  * to get access the the irp.
83  *
84  * @note Think of the irp as the "handle" of a program.
85  */
86 FIRM_API ir_prog *irp;
87
88 /**
89  * Resources usable by algorithms modifying the program
90  */
91 typedef enum irp_resources_t {
92         IRP_RESOURCE_NONE         = 0,      /**< no resource */
93         /** irg link field @see set_irg_link(), get_irg_link() */
94         IRP_RESOURCE_IRG_LINK     = 1 << 0,
95         /** entity link field @see set_entity_link(), get_entity_link() */
96         IRP_RESOURCE_ENTITY_LINK  = 1 << 1,
97         /** type visited field @see type_visited(), mark_type_visited(),
98          *  inc_master_type_visited() */
99         IRP_RESOURCE_TYPE_VISITED = 1 << 2,
100         /** type link field @see set_type_link(), get_type_link() */
101         IRP_RESOURCE_TYPE_LINK    = 1 << 3,
102 } irp_resources_t;
103 ENUM_BITSET(irp_resources_t)
104
105 #ifndef NDEBUG
106 /**
107  * Reserve resources available for a whole program.
108  *
109  * This is a debug tool: All code should properly allocate the resources it uses
110  * so if two interlocked algorithms use the same resources that bug will get
111  * detected.
112  */
113 FIRM_API void irp_reserve_resources(ir_prog *irp, irp_resources_t resources);
114 /** Frees resources availabel for a whole program. */
115 FIRM_API void irp_free_resources(ir_prog *irp, irp_resources_t resources);
116 /** Returns currently reserved whole program resources. */
117 FIRM_API irp_resources_t irp_resources_reserved(const ir_prog *irp);
118 #else
119 #define irp_reserve_resources(irp, resources) (void)0
120 #define irp_free_resources(irp, resources)    (void)0
121 #define irp_resources_reserved(irp)           0
122 #endif
123
124 /**
125  * Returns the current irp from where everything in the current module
126  * can be accessed.
127  *
128  * @see irp
129  */
130 FIRM_API ir_prog *get_irp(void);
131
132 /** Sets current irp */
133 FIRM_API void set_irp(ir_prog *irp);
134
135 /**
136  * Creates a new ir_prog (a module or compilation unit).
137  * Note: This does not set irp to the newly created ir_prog
138  *
139  * @param name  the name of this irp (module)
140  */
141 FIRM_API ir_prog *new_ir_prog(const char *name);
142
143 /** Frees all memory used by irp.  Types in type list and irgs in irg
144  *  list must be freed by hand before. */
145 FIRM_API void free_ir_prog(void);
146
147 /** Sets the file name / executable name or the like. Initially the
148     ident 'no_name_set'. */
149 FIRM_API void set_irp_prog_name(ident *name);
150
151 /** Returns true if the user ever set a program name */
152 FIRM_API int irp_prog_name_is_set(void);
153
154 /** Returns the name of the current irp. */
155 FIRM_API ident *get_irp_ident(void);
156
157 /** Returns the name of the current irp. */
158 FIRM_API const char *get_irp_name(void);
159
160 /** Returns the main routine of the compiled program. */
161 FIRM_API ir_graph *get_irp_main_irg(void);
162
163 /** Sets the main routine of the compiled program. */
164 FIRM_API void set_irp_main_irg(ir_graph *main_irg);
165
166 /** Adds irg to the list of ir graphs in the current irp. */
167 FIRM_API void add_irp_irg(ir_graph *irg);
168
169 /** Removes irg from the list of irgs and
170     shrinks the list by one. */
171 FIRM_API void remove_irp_irg_from_list(ir_graph *irg);
172 /** Removes irg from the list of irgs, deallocates it and
173     shrinks the list by one. */
174 FIRM_API void remove_irp_irg(ir_graph *irg);
175
176 /** returns the biggest not used irg index number */
177 FIRM_API size_t get_irp_last_idx(void);
178
179 /** Returns the number of ir graphs in the irp. */
180 FIRM_API size_t get_irp_n_irgs(void);
181
182 /** Returns the ir graph at position pos in the irp. */
183 FIRM_API ir_graph *get_irp_irg(size_t pos);
184
185 /** Sets the ir graph at position pos. */
186 FIRM_API void set_irp_irg(size_t pos, ir_graph *irg);
187
188 /**
189  * Returns the type containing the entities for a segment.
190  *
191  * @param segment  the segment
192  */
193 FIRM_API ir_type *get_segment_type(ir_segment_t segment);
194
195 /**
196  * @brief Changes a segment segment type for the program.
197  * (use with care)
198  */
199 FIRM_API void set_segment_type(ir_segment_t segment, ir_type *new_type);
200
201 /**
202  * Returns the "global" type of the irp.
203  * Upon creation this is an empty class type.
204  * This is a convenience function for get_segment_type(IR_SEGMENT_GLOBAL)
205  */
206 FIRM_API ir_type *get_glob_type(void);
207
208 /**
209  * Returns the "thread local storage" type of the irp.
210  * Upon creation this is an empty struct type.
211  * This is a convenience function for get_segment_type(IR_SEGMENT_THREAD_LOCAL)
212  */
213 FIRM_API ir_type *get_tls_type(void);
214
215 /**
216  * Returns the number of all types in the irp.
217  * @deprecated
218  */
219 FIRM_API size_t get_irp_n_types(void);
220
221 /**
222  * Returns the type at position pos in the irp.
223  * @deprecated
224  */
225 FIRM_API ir_type *get_irp_type(size_t pos);
226
227 /**
228  * Overwrites the type at position pos with another type.
229  * @deprecated
230  */
231 FIRM_API void set_irp_type(size_t pos, ir_type *typ);
232
233 /**  Returns the graph for global constants of the current irp.
234  *
235  *   Returns an irgraph that only contains constant expressions for
236  *   constant entities.  Do not use any access function for this
237  *   graph, do not generate code for this graph.  This graph contains
238  *   only one block.  The constant expressions may not contain control
239  *   flow.
240  *   Walking the graph starting from any node will not reach the block
241  *   or any controlflow.
242  *   See also copy_const_code() in entity.h.
243  */
244 FIRM_API ir_graph *get_const_code_irg(void);
245
246 /** The phase state for the program.
247  *
248  *  The phase state of the whole program is
249  *   building:  if at least one graph is state_building
250  *              or one type is incomplete.
251  *   high:      all graphs are in state high or low, all types are constructed.
252  *   low:       all graphs are in state low, all types are in state layout fixed.
253  */
254 FIRM_API irg_phase_state get_irp_phase_state(void);
255 /** Sets the phase state of the program */
256 FIRM_API void set_irp_phase_state(irg_phase_state s);
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 FIRM_API ir_prog_pass_t *set_irp_phase_state_pass(const char *name,
267                                                   irg_phase_state state);
268
269 /** Returns callee info state for the whole program.
270  * @see get_irg_callee_info_state() */
271 FIRM_API irg_callee_info_state get_irp_callee_info_state(void);
272 /** Sets callee info state for the whole program.
273  * @see set_irg_callee_info_state() */
274 FIRM_API void set_irp_callee_info_state(irg_callee_info_state s);
275
276 /** Returns a new, unique label number. */
277 FIRM_API ir_label_t get_irp_next_label_nr(void);
278
279 /** Add a new global asm include. */
280 FIRM_API void add_irp_asm(ident *asm_string);
281
282 /** Returns the number of global asm includes. */
283 FIRM_API size_t get_irp_n_asms(void);
284
285 /** Returns the global asm include at position pos. */
286 FIRM_API ident *get_irp_asm(size_t pos);
287
288 /** @} */
289
290 #include "end.h"
291
292 #endif