5 #include "firm_config.h"
21 typedef struct _arch_register_class_t arch_register_class_t;
22 typedef struct _arch_register_t arch_register_t;
23 typedef struct _arch_enum_t arch_enum_t;
24 typedef struct _arch_enum_member_t arch_enum_member_t;
25 typedef struct _arch_isa_if_t arch_isa_if_t;
26 typedef struct _arch_env_t arch_env_t;
28 typedef enum _arch_register_type_t {
29 arch_register_type_none = 0,
30 arch_register_type_write_invariant,
31 arch_register_type_caller_saved, /**< The register must be saved by the caller
32 upon a function call. It thus can be overwritten
33 in the called function. */
34 arch_register_type_callee_saved, /**< The register must be saved by the called function,
35 it thus survives a function call. */
36 arch_register_type_ignore /**< Do not consider this register when allocating. */
37 } arch_register_type_t;
42 struct _arch_register_t {
43 const char *name; /**< The name of the register. */
44 const arch_register_class_t *reg_class; /**< The class the register belongs to. */
45 int index; /**< The index of the register in the class. */
46 arch_register_type_t type; /**< The type of the register. */
47 void *data; /**< Custom data. */
50 static INLINE const arch_register_class_t *
51 _arch_register_get_class(const arch_register_t *reg)
53 return reg->reg_class;
56 static INLINE int _arch_register_get_index(const arch_register_t *reg)
61 #define arch_register_get_class(reg) _arch_register_get_class(reg)
62 #define arch_register_get_index(reg) _arch_register_get_index(reg)
63 #define arch_register_get_name(reg) ((reg)->name)
66 * A class of registers.
67 * Like general purpose or floating point.
69 struct _arch_register_class_t {
70 const char *name; /**< The name of the register. */
71 int n_regs; /**< Number of registers in this class. */
72 const arch_register_t *regs; /**< The array of registers. */
75 #define arch_register_class_n_regs(cls) ((cls)->n_regs)
78 * Put all registers in a class into a bitset.
79 * @param cls The class.
80 * @param bs The bitset. May be NULL.
81 * @return The number of registers in the class.
83 extern int arch_register_class_put(const arch_register_class_t *cls,
84 struct _bitset_t *bs);
86 static INLINE const arch_register_t *
87 _arch_register_for_index(const arch_register_class_t *cls, int idx)
89 assert(0 <= idx && idx < cls->n_regs);
90 return &cls->regs[idx];
93 #define arch_register_for_index(cls, idx) \
94 _arch_register_for_index(cls, idx)
97 * Get the register set for a register class.
98 * @param cls The register class.
99 * @return The set containing all registers in the class.
101 #define arch_get_register_set_for_class(cls) ((cls)->set)
106 struct _arch_immediate_t {
107 const char *name; /**< The name of the immediate. */
108 ir_mode *mode; /**< The mode of the immediate. */
112 * The member of an enum.
114 struct _arch_enum_member_t {
115 arch_enum_t *enm; /**< The enum, this member belongs to. */
119 * An enumeration operand type.
121 * Enumeration operand types can be used to describe the variants
122 * of an instruction, like giving the cases for a compare (gt, lt,
123 * eq, ...) some other special attributes of an instruction.
125 struct _arch_enum_t {
126 int n_members; /**< The number of members in this enum. */
127 arch_enum_member_t *members[1]; /**< The array of members. */
130 typedef enum _arch_operand_type_t {
131 arch_operand_type_invalid,
132 arch_operand_type_memory,
133 arch_operand_type_register,
134 arch_operand_type_immediate,
135 arch_operand_type_symconst,
136 arch_operand_type_last
137 } arch_operand_type_t;
140 * Different types of register allocation requirements.
142 typedef enum _arch_register_req_type_t {
143 arch_register_req_type_normal, /** All registers in the class
146 arch_register_req_type_limited, /** Only a real subset of
147 the class is allowed. */
149 arch_register_req_type_equal, /** The register must equal
150 another one at the node. */
152 arch_register_req_type_unequal, /** The register must be unequal
153 to some other at the node. */
155 arch_register_req_type_pair /** The register is part of a
157 } arch_register_req_type_t;
160 * Expresses requirements to register allocation for an operand.
162 typedef struct _arch_register_req_t {
163 arch_register_req_type_t type; /** The type of the constraint. */
164 const arch_register_class_t *cls; /** The register class this
165 constraint belongs to. */
167 int (*limited)(const ir_node *irn, int pos, struct _bitset_t *bs);
168 /** In case of the 'limited'
169 constraint, this function
170 must put all allowable
171 registers in the bitset and
172 return the number of registers
175 int pos; /** In case of the equal constraint,
176 this gives the position of the
177 operand to which the register of
178 this should be equal to. Same for
181 } arch_register_req_t;
184 * Certain node classes which are relevent for the register allocator.
186 typedef enum _arch_irn_class_t {
187 arch_irn_class_normal,
188 arch_irn_class_spill,
189 arch_irn_class_reload,
194 * Some words about positions and indices:
196 * Firm has the policy "One node per value", that's why there are
197 * Proj nodes. This view has its advantages, but in a backend
198 * setting where we talk about instructions (which can also have
199 * multiple results and not a single Tuple value) this is sometimes
202 * Each node representing an instruction must provide information
203 * about the kind of its operands (where operands mean both input
204 * and output operands). Such an operand is addressed with a position
205 * which is infact a tuple {in, out} x N. The fact that a position
206 * is an input/output operand is encoded in the sign, so input operands
207 * go from 0..n-1 and output operands from -1..-m if the
208 * instruction has n input and m output operands.
212 * Make an in position from an index.
213 * @param index The index.
214 * @return The position representing the index as an in operand.
216 #define arch_pos_make_in(index) (index)
219 * Make an out position from an index.
220 * @param index The index.
221 * @return The position representing the index as an out operand.
223 #define arch_pos_make_out(index) (-((index) + 1))
226 * Check, if a position denotes an input operand.
227 * @param pos The position.
228 * @return 1, if the position denotes an input operand 0 if not.
230 #define arch_pos_is_in(pos) ((pos) >= 0)
233 * Check, if a position denotes an output operand.
234 * @param pos The position.
235 * @return 1, if the position denotes an output operand 0 if not.
237 #define arch_pos_is_out(pos) (!arch_pos_is_in(pos))
240 * Get the index of a position.
241 * @param pos The position.
242 * @return The index of the position.
244 #define arch_pos_get_index(pos) ((pos) < 0 ? -(pos) - 1 : (pos))
246 typedef struct _arch_irn_ops_t {
249 * Get the register requirements for a given operand.
250 * @param irn The node.
251 * @param pos The operand's position.
252 * @return The register requirements for the selected operand,
253 * or NULL, if the operand is no register.
255 const arch_register_req_t *(*get_irn_reg_req)(const ir_node *irn, int pos);
258 * Get the number of operands of a node.
259 * @param irn The node.
260 * @param in_out Denotes wither input (a number >= 0) or
261 * output (a number < 0).
262 * @return The number of operands for either in, or output.
264 int (*get_n_operands)(const ir_node *irn, int in_out);
267 * Set the register for an output operand.
268 * @param irn The node.
269 * @param pos The position of the output operand.
270 * @param reg The register allocated to that operand.
271 * @note If the operand is not a register operand,
272 * the call is ignored.
274 void (*set_irn_reg)(ir_node *irn, int idx, const arch_register_t *reg);
277 * Get the register allocated for an output operand.
278 * @param irn The node.
279 * @param pos The index of the output operand.
280 * @return The register allocated at that operand. NULL, if
281 * the operand was no register operand or
282 * @c arch_register_invalid, if no register has yet been
283 * allocated for this node.
285 const arch_register_t *(*get_irn_reg)(const ir_node *irn, int idx);
289 * @param irn The node.
290 * @return A classification.
292 arch_irn_class_t (*classify)(const ir_node *irn);
297 * Check if an operand is a register operand.
298 * @param env The environment.
299 * @param irn The node.
300 * @param pos The position of the operand.
301 * @return 1, if the operand is significant for register allocation, 0
304 extern int arch_is_register_operand(const arch_env_t *env,
305 const ir_node *irn, int pos);
308 * Get the number of allocatable registers concerning
309 * a register class for an operand of a node.
310 * @param env The environment.
311 * @param irn The node.
312 * @param pos The postition of the node's operand.
313 * @param cls The register class.
314 * @param bs The bitset all allocatable registers shall be put into.
315 * Note, that you can also pass NULL here. If you don't,
316 * make sure, the bitset is as large as the register class
318 * @return The amount of registers allocatable for that operand.
320 extern int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn,
321 int pos, const arch_register_class_t *cls, struct _bitset_t *bs);
324 * Check, if a register is assignable to an operand of a node.
325 * @param env The architecture environment.
326 * @param irn The node.
327 * @param pos The position of the operand.
328 * @param reg The register.
329 * @return 1, if the register might be allocated to the operand 0 if not.
331 extern int arch_reg_is_allocatable(const arch_env_t *env,
332 const ir_node *irn, int pos, const arch_register_t *reg);
335 * Get the register class of an operand of a node.
336 * @param env The architecture environment.
337 * @param irn The node.
338 * @param idx The position of the operand.
339 * @return The register class of the operand or NULL, if
340 * operand is a non-register operand.
342 extern const arch_register_class_t *
343 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos);
346 * Get the register allocated at a certain output operand of a node.
347 * @param env The arch nvironment.
348 * @param irn The node.
349 * @param idx The index of the output operand.
350 * @return The register allocated for this operand
352 extern const arch_register_t *
353 arch_get_irn_register(const arch_env_t *env, const ir_node *irn, int idx);
356 * Set the register for a certain output operand.
357 * @param env The architecture environment.
358 * @param irn The node.
359 * @param idx The index of the output operand.
360 * @param reg The register.
362 extern void arch_set_irn_register(const arch_env_t *env,
363 ir_node *irn, int idx, const arch_register_t *reg);
365 #define arch_irn_has_reg_class(env, irn, pos, cls) \
366 ((cls) == arch_get_irn_reg_class(env, irn, pos))
369 * Somebody who can be asked about nodes.
371 typedef struct _arch_irn_handler_t {
374 * Get the operations of an irn.
375 * @param irn Some node.
376 * @return Operations for that irn.
378 const arch_irn_ops_t *(*get_irn_ops)(const ir_node *irn);
380 } arch_irn_handler_t;
383 * Architecture interface.
385 struct _arch_isa_if_t {
388 * Initialize the isa interface.
393 * Get the the number of register classes in the isa.
394 * @return The number of register classes.
396 int (*get_n_reg_class)(void);
399 * Get the i-th register class.
400 * @param i The number of the register class.
401 * @return The register class.
403 const arch_register_class_t *(*get_reg_class)(int i);
407 #define ARCH_MAX_HANDLERS 8
410 * Environment for the architecture infrastructure.
411 * Keep this everywhere you're going.
414 const arch_isa_if_t *isa; /** The isa about which everything is. */
416 arch_irn_handler_t const *handlers[ARCH_MAX_HANDLERS]; /** The handlers are organized as
419 int handlers_tos; /** The stack pointer of the handler
424 * Get the isa of an arch environment.
425 * @param env The environment.
426 * @return The isa with which the env was initialized with.
428 #define arch_env_get_isa(env) ((env)->isa)
431 * Initialize the architecture environment struct.
432 * @param isa The isa which shall be put into the environment.
433 * @return The environment.
435 extern arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa);
438 * Add a node handler to the environment.
439 * @param env The environment.
440 * @param handler A node handler.
441 * @return The environment itself.
443 extern arch_env_t *arch_env_add_irn_handler(arch_env_t *env,
444 const arch_irn_handler_t *handler);
446 #endif /* _FIRM_BEARCH_H */