2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Entry point to the representation of a whole program.
9 * @author Goetz Lindenmaier
12 * Intermediate Representation (IR) of a program.
15 #ifndef FIRM_IR_IRPROG_H
16 #define FIRM_IR_IRPROG_H
19 #include "firm_types.h"
24 * @defgroup ir_prog Program
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
43 * A progrom has a number of special segments at the toplevel which modify
44 * the behaviour of the entities in them.
46 typedef enum ir_segment_t {
48 /** "normal" global data */
49 IR_SEGMENT_GLOBAL = IR_SEGMENT_FIRST,
50 /** thread local storage segment */
51 IR_SEGMENT_THREAD_LOCAL,
53 * the constructors segment. Contains pointers to functions which are
54 * executed on module initialization (program start or when a library is
57 IR_SEGMENT_CONSTRUCTORS,
58 /** like constructors, but functions are executed on module exit */
59 IR_SEGMENT_DESTRUCTORS,
61 IR_SEGMENT_LAST = IR_SEGMENT_DESTRUCTORS
63 ENUM_COUNTABLE(ir_segment_t)
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.
70 * @note Think of the irp as the "handle" of a program.
72 FIRM_API ir_prog *irp;
75 * Resources usable by algorithms modifying the program
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,
89 ENUM_BITSET(irp_resources_t)
93 * Reserve resources available for a whole program.
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
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);
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
111 * Returns the current irp from where everything in the current module
116 FIRM_API ir_prog *get_irp(void);
118 /** Sets current irp */
119 FIRM_API void set_irp(ir_prog *irp);
122 * Creates a new ir_prog (a module or compilation unit).
123 * Note: This does not set irp to the newly created ir_prog
125 * @param name the name of this irp (module)
127 FIRM_API ir_prog *new_ir_prog(const char *name);
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);
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);
137 /** Returns true if the user ever set a program name */
138 FIRM_API int irp_prog_name_is_set(void);
140 /** Returns the name of the current irp. */
141 FIRM_API ident *get_irp_ident(void);
143 /** Returns the name of the current irp. */
144 FIRM_API const char *get_irp_name(void);
146 /** Returns the main routine of the compiled program. */
147 FIRM_API ir_graph *get_irp_main_irg(void);
149 /** Sets the main routine of the compiled program. */
150 FIRM_API void set_irp_main_irg(ir_graph *main_irg);
152 /** returns the biggest not used irg index number */
153 FIRM_API size_t get_irp_last_idx(void);
155 /** Returns the number of ir graphs in the irp. */
156 FIRM_API size_t get_irp_n_irgs(void);
158 /** Returns the ir graph at position pos in the irp. */
159 FIRM_API ir_graph *get_irp_irg(size_t pos);
161 /** Sets the ir graph at position pos. */
162 FIRM_API void set_irp_irg(size_t pos, ir_graph *irg);
165 * Returns the type containing the entities for a segment.
167 * @param segment the segment
169 FIRM_API ir_type *get_segment_type(ir_segment_t segment);
172 * @brief Changes a segment segment type for the program.
175 FIRM_API void set_segment_type(ir_segment_t segment, ir_type *new_type);
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)
182 FIRM_API ir_type *get_glob_type(void);
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)
189 FIRM_API ir_type *get_tls_type(void);
192 * Returns the number of all types in the irp.
195 FIRM_API size_t get_irp_n_types(void);
198 * Returns the type at position pos in the irp.
201 FIRM_API ir_type *get_irp_type(size_t pos);
204 * Overwrites the type at position pos with another type.
207 FIRM_API void set_irp_type(size_t pos, ir_type *typ);
209 /** Returns the graph for global constants of the current irp.
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
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.
220 FIRM_API ir_graph *get_const_code_irg(void);
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);
229 /** Returns a new, unique label number. */
230 FIRM_API ir_label_t get_irp_next_label_nr(void);
232 /** Add a new global asm include. */
233 FIRM_API void add_irp_asm(ident *asm_string);
235 /** Returns the number of global asm includes. */
236 FIRM_API size_t get_irp_n_asms(void);
238 /** Returns the global asm include at position pos. */
239 FIRM_API ident *get_irp_asm(size_t pos);