2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Processor architecture specification.
23 * @author Sebastian Hack
26 #ifndef FIRM_BE_BEARCH_H
27 #define FIRM_BE_BEARCH_H
29 #include "firm_types.h"
33 typedef struct arch_register_class_t arch_register_class_t;
34 typedef struct arch_register_req_t arch_register_req_t;
35 typedef struct arch_register_t arch_register_t;
36 typedef struct arch_flag_t arch_flag_t;
37 typedef struct arch_inverse_t arch_inverse_t;
38 typedef struct arch_isa_if_t arch_isa_if_t;
39 typedef struct arch_isa_t arch_isa_t;
40 typedef struct arch_env_t arch_env_t;
41 typedef struct arch_irn_ops_if_t arch_irn_ops_if_t;
42 typedef struct arch_irn_ops_t arch_irn_ops_t;
43 typedef struct arch_irn_handler_t arch_irn_handler_t;
44 typedef struct arch_code_generator_t arch_code_generator_t;
45 typedef struct arch_code_generator_if_t arch_code_generator_if_t;
47 typedef enum arch_register_class_flags_t {
48 arch_register_class_flag_none = 0,
49 arch_register_class_flag_manual_ra = 1, /**< don't do automatic register allocation for this class */
50 arch_register_class_flag_state = 2
51 } arch_register_class_flags_t;
53 typedef enum arch_register_type_t {
54 arch_register_type_none = 0,
55 arch_register_type_caller_save = 1, /**< The register must be saved by the caller
56 upon a function call. It thus can be overwritten
57 in the called function. */
58 arch_register_type_callee_save = 2, /**< The register must be saved by the caller
59 upon a function call. It thus can be overwritten
60 in the called function. */
61 arch_register_type_ignore = 4, /**< Do not consider this register when allocating. */
62 arch_register_type_joker = 8, /**< The emitter can choose an arbitrary register */
63 arch_register_type_virtual = 16, /**< This is just a virtual register.Virtual registers have
64 nearly no constraints, it is a allowed to have multiple
65 definition for the same register at a point) */
66 arch_register_type_state = 32, /**< The register represents a state that should be handled by
68 } arch_register_type_t;
71 * Put all registers in a class into a bitset.
72 * @param cls The class.
73 * @param bs The bitset. May be NULL.
74 * @return The number of registers in the class.
76 extern int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs);
78 typedef enum arch_operand_type_t {
79 arch_operand_type_invalid,
80 arch_operand_type_memory,
81 arch_operand_type_register,
82 arch_operand_type_immediate,
83 arch_operand_type_symconst,
84 arch_operand_type_last
85 } arch_operand_type_t;
88 * Different types of register allocation requirements.
90 typedef enum arch_register_req_type_t {
91 arch_register_req_type_none = 0, /**< No register requirement. */
92 arch_register_req_type_normal = 1, /**< All registers in the class are allowed. */
93 arch_register_req_type_limited = 2, /**< Only a real subset of the class is allowed. */
94 arch_register_req_type_should_be_same = 4, /**< The register should be equal to another one at the node. */
95 arch_register_req_type_should_be_different = 8, /**< The register must be unequal from some other at the node. */
96 } arch_register_req_type_t;
98 extern const arch_register_req_t *arch_no_register_req;
101 * Format a register requirements information into a string.
102 * @param buf The string where to put it to.
103 * @param len The size of @p buf.
104 * @param req The requirements structure to format.
105 * @return A pointer to buf.
107 extern char *arch_register_req_format(char *buf, size_t len, const arch_register_req_t *req, const ir_node *node);
110 * Certain node classes which are relevant for the register allocator.
112 typedef enum arch_irn_class_t {
113 arch_irn_class_normal = 1 << 0,
114 arch_irn_class_spill = 1 << 1,
115 arch_irn_class_reload = 1 << 2,
116 arch_irn_class_copy = 1 << 3,
117 arch_irn_class_perm = 1 << 4,
118 arch_irn_class_branch = 1 << 5,
119 arch_irn_class_call = 1 << 6,
120 arch_irn_class_load = 1 << 7,
121 arch_irn_class_store = 1 << 8,
122 arch_irn_class_stackparam = 1 << 9,
126 * Some flags describing a node in more detail.
128 typedef enum arch_irn_flags_t {
129 arch_irn_flags_none = 0, /**< Node flags. */
130 arch_irn_flags_dont_spill = 1, /**< This must not be spilled. */
131 arch_irn_flags_rematerializable = 2, /**< This can be replicated instead of spilled/reloaded. */
132 arch_irn_flags_ignore = 4, /**< Ignore node during register allocation. */
133 arch_irn_flags_modify_sp = 8, /**< I modify the stack pointer. */
134 arch_irn_flags_modify_flags = 16, /**< I modify flags. */
135 arch_irn_flags_last = arch_irn_flags_modify_flags
139 * Get the string representation of a flag.
140 * This functions does not handle or'ed bitmasks of flags.
141 * @param flag The flag.
142 * @return The flag as a string.
144 extern const char *arch_irn_flag_str(arch_irn_flags_t flag);
147 extern const arch_irn_ops_t *arch_get_irn_ops(const arch_env_t *env,
151 extern void arch_set_frame_offset(const arch_env_t *env, ir_node *irn, int bias);
153 extern ir_entity *arch_get_frame_entity(const arch_env_t *env, const ir_node *irn);
154 extern void arch_set_frame_entity(const arch_env_t *env, ir_node *irn, ir_entity *ent);
155 extern int arch_get_sp_bias(const arch_env_t *env, ir_node *irn);
157 extern int arch_get_op_estimated_cost(const arch_env_t *env, const ir_node *irn);
158 extern arch_inverse_t *arch_get_inverse(const arch_env_t *env, const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack);
159 extern int arch_possible_memory_operand(const arch_env_t *env, const ir_node *irn, unsigned int i);
160 extern void arch_perform_memory_operand(const arch_env_t *env, ir_node *irn, ir_node *spill, unsigned int i);
163 * Get the register requirements for a node.
164 * @param env The architecture environment.
165 * @param req A pointer to a requirements structure, where the data can
167 * @param irn The node.
168 * @param pos The position of the operand you're interested in.
169 * @return A pointer to the register requirements which may <b>not</b>
170 * neccessarily be equal to @p req. If NULL is returned, the
171 * operand was no register operand.
173 extern const arch_register_req_t *
174 arch_get_register_req(const arch_env_t *env, const ir_node *irn, int pos);
177 * Check if an operand is a register operand.
178 * @param env The environment.
179 * @param irn The node.
180 * @param pos The position of the operand.
181 * @return 1, if the operand is significant for register allocation, 0
184 extern int arch_is_register_operand(const arch_env_t *env,
185 const ir_node *irn, int pos);
188 * Get the number of allocatable registers concerning
189 * a register class for an operand of a node.
190 * @param env The environment.
191 * @param irn The node.
192 * @param pos The postition of the node's operand.
193 * @param bs The bitset all allocatable registers shall be put into.
194 * Note, that you can also pass NULL here. If you don't,
195 * make sure, the bitset is as large as the register class
197 * @return The amount of registers allocatable for that operand.
199 extern int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn, int pos, bitset_t *bs);
202 * Put all registers which shall not be ignored by the register
203 * allocator in a bit set.
204 * @param env The arch env.
205 * @param cls The register class to consider.
206 * @param bs The bit set to put the registers to.
208 extern void arch_put_non_ignore_regs(const arch_env_t *env, const arch_register_class_t *cls, bitset_t *bs);
211 * Check, if a register is assignable to an operand of a node.
212 * @param env The architecture environment.
213 * @param irn The node.
214 * @param pos The position of the operand.
215 * @param reg The register.
216 * @return 1, if the register might be allocated to the operand 0 if not.
218 extern int arch_reg_is_allocatable(const arch_env_t *env,
219 const ir_node *irn, int pos, const arch_register_t *reg);
222 * Get the register class of an operand of a node.
223 * @param env The architecture environment.
224 * @param irn The node.
225 * @param pos The position of the operand, -1 for the output.
226 * @return The register class of the operand or NULL, if
227 * operand is a non-register operand.
229 extern const arch_register_class_t *
230 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos);
233 * Get the register allocated at a certain output operand of a node.
234 * @param env The arch environment.
235 * @param irn The node.
236 * @return The register allocated for this operand
238 extern const arch_register_t *
239 arch_get_irn_register(const arch_env_t *env, const ir_node *irn);
242 * Set the register for a certain output operand.
243 * @param env The architecture environment.
244 * @param irn The node.
245 * @param idx The index of the output operand.
246 * @param reg The register.
248 extern void arch_set_irn_register(const arch_env_t *env, ir_node *irn,
249 const arch_register_t *reg);
253 * @param env The architecture environment.
254 * @param irn The node.
255 * @return A classification of the node.
257 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn);
259 #define arch_irn_class_is(env, irn, irn_class) ((arch_irn_classify(env, irn) & arch_irn_class_ ## irn_class) != 0)
262 * Get the flags of a node.
263 * @param env The architecture environment.
264 * @param irn The node.
267 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn);
269 #define arch_irn_is(env, irn, flag) ((arch_irn_get_flags(env, irn) & arch_irn_flags_ ## flag) != 0)
271 #define arch_irn_has_reg_class(env, irn, pos, cls) \
272 ((cls) == arch_get_irn_reg_class(env, irn, pos))
274 #define arch_irn_consider_in_reg_alloc(env, cls, irn) \
275 (arch_irn_has_reg_class(env, irn, -1, cls) && !arch_irn_is(env, irn, ignore))
278 * Initialize the architecture environment struct.
279 * @param isa The isa which shall be put into the environment.
280 * @param file_handle The file handle
281 * @return The environment.
283 extern arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa,
284 FILE *file_handle, be_main_env_t *main_env);
287 * Add a node handler to the environment.
288 * @param env The environment.
289 * @param handler A node handler.
290 * @return The environment itself.
292 extern arch_env_t *arch_env_push_irn_handler(arch_env_t *env, const arch_irn_handler_t *handler);
295 * Remove a node handler from the handler stack.
296 * @param env The architecture environment.
297 * @return The popped handler.
299 extern const arch_irn_handler_t *arch_env_pop_irn_handler(arch_env_t *env);
302 * Register an instruction set architecture
304 void be_register_isa_if(const char *name, const arch_isa_if_t *isa);
306 #endif /* FIRM_BE_BEARCH_H */