4 #include "firm_config.h"
16 typedef struct _arch_register_class_t arch_register_class_t;
17 typedef struct _arch_register_t arch_register_t;
18 typedef struct _arch_enum_t arch_enum_t;
19 typedef struct _arch_enum_member_t arch_enum_member_t;
20 typedef struct _arch_isa_if_t arch_isa_if_t;
21 typedef struct _arch_env_t arch_env_t;
22 typedef struct _arch_irn_ops_t arch_irn_ops_t;
23 typedef struct _arch_irn_handler_t arch_irn_handler_t;
25 struct _be_node_factory_t;
27 typedef enum _arch_register_type_t {
28 arch_register_type_none = 0,
29 arch_register_type_write_invariant,
30 arch_register_type_caller_saved, /**< The register must be saved by the caller
31 upon a function call. It thus can be overwritten
32 in the called function. */
33 arch_register_type_callee_saved, /**< The register must be saved by the called function,
34 it thus survives a function call. */
35 arch_register_type_ignore /**< Do not consider this register when allocating. */
36 } arch_register_type_t;
41 struct _arch_register_t {
42 const char *name; /**< The name of the register. */
43 const arch_register_class_t *reg_class; /**< The class the register belongs to. */
44 int index; /**< The index of the register in the class. */
45 arch_register_type_t type; /**< The type of the register. */
46 void *data; /**< Custom data. */
49 static INLINE const arch_register_class_t *
50 _arch_register_get_class(const arch_register_t *reg)
52 return reg->reg_class;
55 static INLINE int _arch_register_get_index(const arch_register_t *reg)
60 #define arch_register_get_class(reg) _arch_register_get_class(reg)
61 #define arch_register_get_index(reg) _arch_register_get_index(reg)
62 #define arch_register_get_name(reg) ((reg)->name)
65 * A class of registers.
66 * Like general purpose or floating point.
68 struct _arch_register_class_t {
69 const char *name; /**< The name of the register class. */
70 int n_regs; /**< Number of registers in this class. */
71 const arch_register_t *regs; /**< The array of registers. */
74 #define arch_register_class_n_regs(cls) ((cls)->n_regs)
77 * Put all registers in a class into a bitset.
78 * @param cls The class.
79 * @param bs The bitset. May be NULL.
80 * @return The number of registers in the class.
82 extern int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs);
84 static INLINE const arch_register_t *
85 _arch_register_for_index(const arch_register_class_t *cls, int idx)
87 assert(0 <= idx && idx < cls->n_regs);
88 return &cls->regs[idx];
91 #define arch_register_for_index(cls, idx) \
92 _arch_register_for_index(cls, idx)
95 * Get the register set for a register class.
96 * @param cls The register class.
97 * @return The set containing all registers in the class.
99 #define arch_get_register_set_for_class(cls) ((cls)->set)
104 struct _arch_immediate_t {
105 const char *name; /**< The name of the immediate. */
106 ir_mode *mode; /**< The mode of the immediate. */
110 * The member of an enum.
112 struct _arch_enum_member_t {
113 arch_enum_t *enm; /**< The enum, this member belongs to. */
117 * An enumeration operand type.
119 * Enumeration operand types can be used to describe the variants
120 * of an instruction, like giving the cases for a compare (gt, lt,
121 * eq, ...) some other special attributes of an instruction.
123 struct _arch_enum_t {
124 int n_members; /**< The number of members in this enum. */
125 arch_enum_member_t *members[1]; /**< The array of members. */
128 typedef enum _arch_operand_type_t {
129 arch_operand_type_invalid,
130 arch_operand_type_memory,
131 arch_operand_type_register,
132 arch_operand_type_immediate,
133 arch_operand_type_symconst,
134 arch_operand_type_last
135 } arch_operand_type_t;
138 * Different types of register allocation requirements.
140 typedef enum _arch_register_req_type_t {
141 arch_register_req_type_none = 0, /**< No register requirement. */
143 arch_register_req_type_normal = 1, /**< All registers in the class
146 arch_register_req_type_limited = 2, /**< Only a real subset of
147 the class is allowed. */
149 arch_register_req_type_equal = 4, /**< The register must equal
150 another one at the node. */
152 arch_register_req_type_unequal = 8, /**< The register must be unequal
153 to some other at the node. */
155 arch_register_req_type_pair = 16 /**< The register is part of a
157 } arch_register_req_type_t;
159 #define arch_register_req_is_constr(x) \
160 ((x)->type & (arch_register_req_type_pair + arch_register_req_type_limited - 1) != 0)
163 * Expresses requirements to register allocation for an operand.
165 typedef struct _arch_register_req_t {
166 arch_register_req_type_t type; /**< The type of the constraint. */
167 const arch_register_class_t *cls; /**< The register class this
168 constraint belongs to. */
170 int (*limited)(const ir_node *irn, int pos, bitset_t *bs);
171 /**< In case of the 'limited'
172 constraint, this function
173 must put all allowable
174 registers in the bitset and
175 return the number of registers
178 int pos; /**< In case of the equal constraint,
179 this gives the position of the
180 operand to which the register of
181 this should be equal to. Same for
184 } arch_register_req_t;
187 * Certain node classes which are relevent for the register allocator.
189 typedef enum _arch_irn_class_t {
190 arch_irn_class_normal,
191 arch_irn_class_spill,
192 arch_irn_class_reload,
195 arch_irn_class_branch
199 * Some flags describing a node in more detail.
201 typedef enum _arch_irn_flags_t {
202 arch_irn_flags_spillable = 1,
203 arch_irn_flags_rematerializable = 2
207 * Some words about positions and indices:
209 * Firm has the policy "One node per value", that's why there are
210 * Proj nodes. This view has its advantages, but in a backend
211 * setting where we talk about instructions (which can also have
212 * multiple results and not a single Tuple value) this is sometimes
215 * Each node representing an instruction must provide information
216 * about the kind of its operands (where operands mean both input
217 * and output operands). Such an operand is addressed with a position
218 * which is infact a tuple {in, out} x N. The fact that a position
219 * is an input/output operand is encoded in the sign, so input operands
220 * go from 0..n-1 and output operands from -1..-m if the
221 * instruction has n input and m output operands.
224 #define _BEARCH_TRANSFORM_INDEX(cmp, index) ((index) cmp 0 ? -((index) + 1) : (index))
227 * Make an in position from an index.
228 * @param index The index.
229 * @return The position representing the index as an in operand.
231 #define arch_pos_make_in(index) _BEARCH_TRANSFORM_INDEX(<, index)
234 * Make an out position from an index.
235 * @param index The index.
236 * @return The position representing the index as an out operand.
238 #define arch_pos_make_out(index) _BEARCH_TRANSFORM_INDEX(>=, index)
241 * Check, if a position denotes an input operand.
242 * @param pos The position.
243 * @return 1, if the position denotes an input operand 0 if not.
245 #define arch_pos_is_in(pos) ((pos) >= 0)
248 * Check, if a position denotes an output operand.
249 * @param pos The position.
250 * @return 1, if the position denotes an output operand 0 if not.
252 #define arch_pos_is_out(pos) (!arch_pos_is_in(pos))
255 * Get the index of a position.
256 * @param pos The position.
257 * @return The index of the position.
259 #define arch_pos_get_index(pos) _BEARCH_TRANSFORM_INDEX(<, pos)
261 struct _arch_irn_ops_t {
264 * Get the register requirements for a given operand.
265 * @param self The self pointer.
266 * @param irn The node.
267 * @param pos The operand's position.
268 * @return The register requirements for the selected operand.
269 * The pointer returned is never NULL.
271 const arch_register_req_t *(*get_irn_reg_req)(const arch_irn_ops_t *self,
272 arch_register_req_t *req,
273 const ir_node *irn, int pos);
276 * Get the number of operands of a node.
277 * @param irn The node.
278 * @param in_out Denotes wither input (a number >= 0) or
279 * output (a number < 0).
280 * @return The number of operands for either in, or output.
282 int (*get_n_operands)(const arch_irn_ops_t *self, const ir_node *irn, int in_out);
285 * Set the register for an output operand.
286 * @param irn The node.
287 * @param pos The position of the output operand.
288 * @param reg The register allocated to that operand.
289 * @note If the operand is not a register operand,
290 * the call is ignored.
292 void (*set_irn_reg)(const arch_irn_ops_t *self, ir_node *irn,
293 int idx, const arch_register_t *reg);
296 * Get the register allocated for an output operand.
297 * @param irn The node.
298 * @param pos The index of the output operand.
299 * @return The register allocated at that operand. NULL, if
300 * the operand was no register operand or
301 * @c arch_register_invalid, if no register has yet been
302 * allocated for this node.
304 const arch_register_t *(*get_irn_reg)(const arch_irn_ops_t *self,
305 const ir_node *irn, int idx);
309 * @param irn The node.
310 * @return A classification.
312 arch_irn_class_t (*classify)(const arch_irn_ops_t *self, const ir_node *irn);
315 * Get the flags of a node.
316 * @param self The irn ops themselves.
317 * @param irn The node.
318 * @return A set of flags.
320 arch_irn_flags_t (*get_flags)(const arch_irn_ops_t *self, const ir_node *irn);
325 arch_get_n_operands(const arch_env_t *env, const ir_node *irm, int in_out);
328 * Get the register requirements for a node.
329 * @param env The architecture environment.
330 * @param req A pointer to a requirements structure, where the data can
332 * @param irn The node.
333 * @param pos The position of the operand you're interested in.
334 * @return A pointer to the register requirements which may <b>not</b>
335 * neccessarily be equal to @p req. If NULL is returned, the
336 * operand was no register operand.
338 extern const arch_register_req_t *
339 arch_get_register_req(const arch_env_t *env, arch_register_req_t *req,
340 const ir_node *irn, int pos);
343 * Check if an operand is a register operand.
344 * @param env The environment.
345 * @param irn The node.
346 * @param pos The position of the operand.
347 * @return 1, if the operand is significant for register allocation, 0
350 extern int arch_is_register_operand(const arch_env_t *env,
351 const ir_node *irn, int pos);
354 * Get the number of allocatable registers concerning
355 * a register class for an operand of a node.
356 * @param env The environment.
357 * @param irn The node.
358 * @param pos The postition of the node's operand.
359 * @param cls The register class.
360 * @param bs The bitset all allocatable registers shall be put into.
361 * Note, that you can also pass NULL here. If you don't,
362 * make sure, the bitset is as large as the register class
364 * @return The amount of registers allocatable for that operand.
366 extern int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn,
367 int pos, const arch_register_class_t *cls, bitset_t *bs);
370 * Check, if a register is assignable to an operand of a node.
371 * @param env The architecture environment.
372 * @param irn The node.
373 * @param pos The position of the operand.
374 * @param reg The register.
375 * @return 1, if the register might be allocated to the operand 0 if not.
377 extern int arch_reg_is_allocatable(const arch_env_t *env,
378 const ir_node *irn, int pos, const arch_register_t *reg);
381 * Get the register class of an operand of a node.
382 * @param env The architecture environment.
383 * @param irn The node.
384 * @param pos The position of the operand.
385 * @return The register class of the operand or NULL, if
386 * operand is a non-register operand.
388 extern const arch_register_class_t *
389 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos);
392 * Get the register allocated at a certain output operand of a node.
393 * @param env The arch nvironment.
394 * @param irn The node.
395 * @param idx The index of the output operand.
396 * @return The register allocated for this operand
398 extern const arch_register_t *
399 arch_get_irn_register(const arch_env_t *env, const ir_node *irn, int idx);
402 * Set the register for a certain output operand.
403 * @param env The architecture environment.
404 * @param irn The node.
405 * @param idx The index of the output operand.
406 * @param reg The register.
408 extern void arch_set_irn_register(const arch_env_t *env,
409 ir_node *irn, int idx, const arch_register_t *reg);
413 * @param env The architecture environment.
414 * @param irn The node.
415 * @return A classification of the node.
417 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn);
420 * Get the flags of a node.
421 * @param env The architecture environment.
422 * @param irn The node.
425 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn);
427 #define arch_irn_has_reg_class(env, irn, pos, cls) \
428 ((cls) == arch_get_irn_reg_class(env, irn, pos))
431 * Somebody who can be asked about nodes.
433 struct _arch_irn_handler_t {
436 * Get the operations of an irn.
437 * @param self The handler from which the method is invoked.
438 * @param irn Some node.
439 * @return Operations for that irn.
441 const arch_irn_ops_t *(*get_irn_ops)(const arch_irn_handler_t *handler,
447 * Architecture interface.
449 struct _arch_isa_if_t {
452 * Initialize the isa interface.
457 * Get the the number of register classes in the isa.
458 * @return The number of register classes.
460 int (*get_n_reg_class)(void);
463 * Get the i-th register class.
464 * @param i The number of the register class.
465 * @return The register class.
467 const arch_register_class_t *(*get_reg_class)(int i);
471 * This function is called each time, the back end starts running over
473 * @param irg The graph.
475 void (*prepare_graph)(ir_graph *irg);
478 * The irn handler for this architecture.
479 * The irn handler is registered by the Firm back end
480 * when the architecture is initialized.
483 const arch_irn_handler_t *irn_handler;
486 #define ARCH_MAX_HANDLERS 8
489 * Environment for the architecture infrastructure.
490 * Keep this everywhere you're going.
493 const struct _be_node_factory_t *node_factory; /**< The node factory for be nodes. */
494 const arch_isa_if_t *isa; /**< The isa about which everything is. */
496 arch_irn_handler_t const *handlers[ARCH_MAX_HANDLERS]; /**< The handlers are organized as
499 int handlers_tos; /**< The stack pointer of the handler
504 * Get the isa of an arch environment.
505 * @param env The environment.
506 * @return The isa with which the env was initialized with.
508 #define arch_env_get_isa(env) ((env)->isa)
511 * Initialize the architecture environment struct.
512 * @param isa The isa which shall be put into the environment.
513 * @return The environment.
515 extern arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa);
518 * Add a node handler to the environment.
519 * @param env The environment.
520 * @param handler A node handler.
521 * @return The environment itself.
523 extern arch_env_t *arch_env_add_irn_handler(arch_env_t *env,
524 const arch_irn_handler_t *handler);
526 #endif /* _FIRM_BEARCH_H */