becopyheur2: Remove unnecessary indirection.
[libfirm] / include / libfirm / irprog.h
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief   Entry point to the representation of a whole program.
9  * @author  Goetz Lindenmaier
10  * @date    2000
11  * @brief
12  *  Intermediate Representation (IR) of a program.
13  *
14  */
15 #ifndef FIRM_IR_IRPROG_H
16 #define FIRM_IR_IRPROG_H
17
18 #include <stddef.h>
19 #include "firm_types.h"
20 #include "irgraph.h"
21 #include "begin.h"
22
23 /**
24  * @defgroup ir_prog Program
25  *
26  *  ir_prog keeps information about a program:
27  *   - A reference point to the method to be executed on program start.
28  *   - A list of all procedures.
29  *   - A list of all types.
30  *   - A global type that contains all global variables and procedures that do
31  *     not belong to a class.  This type represents the data segment of the
32  *     program.  It is not the base class of
33  *     all classes in a class hierarchy (as, e.g., "object" in java).
34  *   - A degenerated graph that contains constant expressions.
35  *   - the output file name
36  *
37  * @{
38  */
39
40 /**
41  * Segment
42  *
43  * A progrom has a number of special segments at the toplevel which modify
44  * the behaviour of the entities in them.
45  */
46 typedef enum ir_segment_t {
47         IR_SEGMENT_FIRST,
48         /** "normal" global data */
49         IR_SEGMENT_GLOBAL = IR_SEGMENT_FIRST,
50         /** thread local storage segment */
51         IR_SEGMENT_THREAD_LOCAL,
52         /**
53          * the constructors segment. Contains pointers to functions which are
54          * executed on module initialization (program start or when a library is
55          * dynamically loaded)
56          */
57         IR_SEGMENT_CONSTRUCTORS,
58         /** like constructors, but functions are executed on module exit */
59         IR_SEGMENT_DESTRUCTORS,
60
61         IR_SEGMENT_LAST = IR_SEGMENT_DESTRUCTORS
62 } ir_segment_t;
63 ENUM_COUNTABLE(ir_segment_t)
64
65 /**
66  * A variable pointing to the current irp (program or module).
67  * This variable should be considered constant. Moreover, one should use get_irp()
68  * to get access the the irp.
69  *
70  * @note Think of the irp as the "handle" of a program.
71  */
72 FIRM_API ir_prog *irp;
73
74 /**
75  * Resources usable by algorithms modifying the program
76  */
77 typedef enum irp_resources_t {
78         IRP_RESOURCE_NONE         = 0,      /**< no resource */
79         /** irg link field @see set_irg_link(), get_irg_link() */
80         IRP_RESOURCE_IRG_LINK     = 1 << 0,
81         /** entity link field @see set_entity_link(), get_entity_link() */
82         IRP_RESOURCE_ENTITY_LINK  = 1 << 1,
83         /** type visited field @see type_visited(), mark_type_visited(),
84          *  inc_master_type_visited() */
85         IRP_RESOURCE_TYPE_VISITED = 1 << 2,
86         /** type link field @see set_type_link(), get_type_link() */
87         IRP_RESOURCE_TYPE_LINK    = 1 << 3,
88 } irp_resources_t;
89 ENUM_BITSET(irp_resources_t)
90
91 #ifndef NDEBUG
92 /**
93  * Reserve resources available for a whole program.
94  *
95  * This is a debug tool: All code should properly allocate the resources it uses
96  * so if two interlocked algorithms use the same resources that bug will get
97  * detected.
98  */
99 FIRM_API void irp_reserve_resources(ir_prog *irp, irp_resources_t resources);
100 /** Frees resources availabel for a whole program. */
101 FIRM_API void irp_free_resources(ir_prog *irp, irp_resources_t resources);
102 /** Returns currently reserved whole program resources. */
103 FIRM_API irp_resources_t irp_resources_reserved(const ir_prog *irp);
104 #else
105 #define irp_reserve_resources(irp, resources) (void)0
106 #define irp_free_resources(irp, resources)    (void)0
107 #define irp_resources_reserved(irp)           0
108 #endif
109
110 /**
111  * Returns the current irp from where everything in the current module
112  * can be accessed.
113  *
114  * @see irp
115  */
116 FIRM_API ir_prog *get_irp(void);
117
118 /** Sets current irp */
119 FIRM_API void set_irp(ir_prog *irp);
120
121 /**
122  * Creates a new ir_prog (a module or compilation unit).
123  * Note: This does not set irp to the newly created ir_prog
124  *
125  * @param name  the name of this irp (module)
126  */
127 FIRM_API ir_prog *new_ir_prog(const char *name);
128
129 /** Frees all memory used by irp.  Types in type list and irgs in irg
130  *  list must be freed by hand before. */
131 FIRM_API void free_ir_prog(void);
132
133 /** Sets the file name / executable name or the like. Initially the
134     ident 'no_name_set'. */
135 FIRM_API void set_irp_prog_name(ident *name);
136
137 /** Returns true if the user ever set a program name */
138 FIRM_API int irp_prog_name_is_set(void);
139
140 /** Returns the name of the current irp. */
141 FIRM_API ident *get_irp_ident(void);
142
143 /** Returns the name of the current irp. */
144 FIRM_API const char *get_irp_name(void);
145
146 /** Returns the main routine of the compiled program. */
147 FIRM_API ir_graph *get_irp_main_irg(void);
148
149 /** Sets the main routine of the compiled program. */
150 FIRM_API void set_irp_main_irg(ir_graph *main_irg);
151
152 /** returns the biggest not used irg index number */
153 FIRM_API size_t get_irp_last_idx(void);
154
155 /** Returns the number of ir graphs in the irp. */
156 FIRM_API size_t get_irp_n_irgs(void);
157
158 /** Returns the ir graph at position pos in the irp. */
159 FIRM_API ir_graph *get_irp_irg(size_t pos);
160
161 /** Sets the ir graph at position pos. */
162 FIRM_API void set_irp_irg(size_t pos, ir_graph *irg);
163
164 /**
165  * Returns the type containing the entities for a segment.
166  *
167  * @param segment  the segment
168  */
169 FIRM_API ir_type *get_segment_type(ir_segment_t segment);
170
171 /**
172  * @brief Changes a segment segment type for the program.
173  * (use with care)
174  */
175 FIRM_API void set_segment_type(ir_segment_t segment, ir_type *new_type);
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 FIRM_API 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  * This is a convenience function for get_segment_type(IR_SEGMENT_THREAD_LOCAL)
188  */
189 FIRM_API ir_type *get_tls_type(void);
190
191 /**
192  * Returns the number of all types in the irp.
193  * @deprecated
194  */
195 FIRM_API size_t get_irp_n_types(void);
196
197 /**
198  * Returns the type at position pos in the irp.
199  * @deprecated
200  */
201 FIRM_API ir_type *get_irp_type(size_t pos);
202
203 /**
204  * Overwrites the type at position pos with another type.
205  * @deprecated
206  */
207 FIRM_API void set_irp_type(size_t pos, ir_type *typ);
208
209 /**  Returns the graph for global constants of the current irp.
210  *
211  *   Returns an irgraph that only contains constant expressions for
212  *   constant entities.  Do not use any access function for this
213  *   graph, do not generate code for this graph.  This graph contains
214  *   only one block.  The constant expressions may not contain control
215  *   flow.
216  *   Walking the graph starting from any node will not reach the block
217  *   or any controlflow.
218  *   See also copy_const_code() in entity.h.
219  */
220 FIRM_API ir_graph *get_const_code_irg(void);
221
222 /** Returns callee info state for the whole program.
223  * @see get_irg_callee_info_state() */
224 FIRM_API irg_callee_info_state get_irp_callee_info_state(void);
225 /** Sets callee info state for the whole program.
226  * @see set_irg_callee_info_state() */
227 FIRM_API void set_irp_callee_info_state(irg_callee_info_state s);
228
229 /** Returns a new, unique label number. */
230 FIRM_API ir_label_t get_irp_next_label_nr(void);
231
232 /** Add a new global asm include. */
233 FIRM_API void add_irp_asm(ident *asm_string);
234
235 /** Returns the number of global asm includes. */
236 FIRM_API size_t get_irp_n_asms(void);
237
238 /** Returns the global asm include at position pos. */
239 FIRM_API ident *get_irp_asm(size_t pos);
240
241 /** @} */
242
243 #include "end.h"
244
245 #endif