5 #include "firm_config.h"
17 typedef struct _arch_register_class_t arch_register_class_t;
18 typedef struct _arch_register_t arch_register_t;
19 typedef struct _arch_enum_t arch_enum_t;
20 typedef struct _arch_enum_member_t arch_enum_member_t;
21 typedef struct _arch_isa_if_t arch_isa_if_t;
22 typedef struct _arch_env_t arch_env_t;
23 typedef struct _arch_irn_ops_t arch_irn_ops_t;
24 typedef struct _arch_irn_handler_t arch_irn_handler_t;
26 struct _be_node_factory_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, bitset_t *bs);
85 static INLINE const arch_register_t *
86 _arch_register_for_index(const arch_register_class_t *cls, int idx)
88 assert(0 <= idx && idx < cls->n_regs);
89 return &cls->regs[idx];
92 #define arch_register_for_index(cls, idx) \
93 _arch_register_for_index(cls, idx)
96 * Get the register set for a register class.
97 * @param cls The register class.
98 * @return The set containing all registers in the class.
100 #define arch_get_register_set_for_class(cls) ((cls)->set)
105 struct _arch_immediate_t {
106 const char *name; /**< The name of the immediate. */
107 ir_mode *mode; /**< The mode of the immediate. */
111 * The member of an enum.
113 struct _arch_enum_member_t {
114 arch_enum_t *enm; /**< The enum, this member belongs to. */
118 * An enumeration operand type.
120 * Enumeration operand types can be used to describe the variants
121 * of an instruction, like giving the cases for a compare (gt, lt,
122 * eq, ...) some other special attributes of an instruction.
124 struct _arch_enum_t {
125 int n_members; /**< The number of members in this enum. */
126 arch_enum_member_t *members[1]; /**< The array of members. */
129 typedef enum _arch_operand_type_t {
130 arch_operand_type_invalid,
131 arch_operand_type_memory,
132 arch_operand_type_register,
133 arch_operand_type_immediate,
134 arch_operand_type_symconst,
135 arch_operand_type_last
136 } arch_operand_type_t;
139 * Different types of register allocation requirements.
141 typedef enum _arch_register_req_type_t {
142 arch_register_req_type_none = 0, /** No register requirement. */
144 arch_register_req_type_normal = 1, /** All registers in the class
147 arch_register_req_type_limited = 2, /** Only a real subset of
148 the class is allowed. */
150 arch_register_req_type_equal = 4, /** The register must equal
151 another one at the node. */
153 arch_register_req_type_unequal = 8, /** The register must be unequal
154 to some other at the node. */
156 arch_register_req_type_pair = 16 /** The register is part of a
158 } arch_register_req_type_t;
160 #define arch_register_req_is_constr(x) \
161 ((x)->type & (arch_register_req_type_pair + arch_register_req_type_limited - 1) != 0)
164 * Expresses requirements to register allocation for an operand.
166 typedef struct _arch_register_req_t {
167 arch_register_req_type_t type; /** The type of the constraint. */
168 const arch_register_class_t *cls; /** The register class this
169 constraint belongs to. */
171 int (*limited)(const ir_node *irn, int pos, bitset_t *bs);
172 /** In case of the 'limited'
173 constraint, this function
174 must put all allowable
175 registers in the bitset and
176 return the number of registers
179 int pos; /** In case of the equal constraint,
180 this gives the position of the
181 operand to which the register of
182 this should be equal to. Same for
185 } arch_register_req_t;
188 * Certain node classes which are relevent for the register allocator.
190 typedef enum _arch_irn_class_t {
191 arch_irn_class_normal,
192 arch_irn_class_spill,
193 arch_irn_class_reload,
196 arch_irn_class_branch
200 * Some flags describing a node in more detail.
202 typedef enum _arch_irn_flags_t {
203 arch_irn_flags_spillable = 1,
204 arch_irn_flags_rematerializable = 2
208 * Some words about positions and indices:
210 * Firm has the policy "One node per value", that's why there are
211 * Proj nodes. This view has its advantages, but in a backend
212 * setting where we talk about instructions (which can also have
213 * multiple results and not a single Tuple value) this is sometimes
216 * Each node representing an instruction must provide information
217 * about the kind of its operands (where operands mean both input
218 * and output operands). Such an operand is addressed with a position
219 * which is infact a tuple {in, out} x N. The fact that a position
220 * is an input/output operand is encoded in the sign, so input operands
221 * go from 0..n-1 and output operands from -1..-m if the
222 * instruction has n input and m output operands.
225 #define _BEARCH_TRANSFORM_INDEX(cmp, index) ((index) cmp 0 ? -((index) + 1) : (index))
228 * Make an in position from an index.
229 * @param index The index.
230 * @return The position representing the index as an in operand.
232 #define arch_pos_make_in(index) _BEARCH_TRANSFORM_INDEX(<, index)
235 * Make an out position from an index.
236 * @param index The index.
237 * @return The position representing the index as an out operand.
239 #define arch_pos_make_out(index) _BEARCH_TRANSFORM_INDEX(>=, index)
242 * Check, if a position denotes an input operand.
243 * @param pos The position.
244 * @return 1, if the position denotes an input operand 0 if not.
246 #define arch_pos_is_in(pos) ((pos) >= 0)
249 * Check, if a position denotes an output operand.
250 * @param pos The position.
251 * @return 1, if the position denotes an output operand 0 if not.
253 #define arch_pos_is_out(pos) (!arch_pos_is_in(pos))
256 * Get the index of a position.
257 * @param pos The position.
258 * @return The index of the position.
260 #define arch_pos_get_index(pos) _BEARCH_TRANSFORM_INDEX(<, pos)
262 struct _arch_irn_ops_t {
265 * Get the register requirements for a given operand.
266 * @param self The self pointer.
267 * @param irn The node.
268 * @param pos The operand's position.
269 * @return The register requirements for the selected operand.
270 * The pointer returned is never NULL.
272 const arch_register_req_t *(*get_irn_reg_req)(const arch_irn_ops_t *self,
273 arch_register_req_t *req,
274 const ir_node *irn, int pos);
277 * Get the number of operands of a node.
278 * @param irn The node.
279 * @param in_out Denotes wither input (a number >= 0) or
280 * output (a number < 0).
281 * @return The number of operands for either in, or output.
283 int (*get_n_operands)(const arch_irn_ops_t *self, const ir_node *irn, int in_out);
286 * Set the register for an output operand.
287 * @param irn The node.
288 * @param pos The position of the output operand.
289 * @param reg The register allocated to that operand.
290 * @note If the operand is not a register operand,
291 * the call is ignored.
293 void (*set_irn_reg)(const arch_irn_ops_t *self, ir_node *irn,
294 int idx, const arch_register_t *reg);
297 * Get the register allocated for an output operand.
298 * @param irn The node.
299 * @param pos The index of the output operand.
300 * @return The register allocated at that operand. NULL, if
301 * the operand was no register operand or
302 * @c arch_register_invalid, if no register has yet been
303 * allocated for this node.
305 const arch_register_t *(*get_irn_reg)(const arch_irn_ops_t *self,
306 const ir_node *irn, int idx);
310 * @param irn The node.
311 * @return A classification.
313 arch_irn_class_t (*classify)(const arch_irn_ops_t *self, const ir_node *irn);
316 * Get the flags of a node.
317 * @param self The irn ops themselves.
318 * @param irn The node.
319 * @return A set of flags.
321 arch_irn_flags_t (*get_flags)(const arch_irn_ops_t *self, const ir_node *irn);
326 arch_get_n_operands(const arch_env_t *env, const ir_node *irm, int in_out);
329 * Get the register requirements for a node.
330 * @param env The architecture environment.
331 * @param req A pointer to a requirements structure, where the data can
333 * @param irn The node.
334 * @param pos The position of the operand you're interested in.
335 * @return A pointer to the register requirements which may <b>not</b>
336 * neccessarily be equal to @p req. If NULL is returned, the
337 * operand was no register operand.
339 extern const arch_register_req_t *
340 arch_get_register_req(const arch_env_t *env, arch_register_req_t *req,
341 const ir_node *irn, int pos);
344 * Check if an operand is a register operand.
345 * @param env The environment.
346 * @param irn The node.
347 * @param pos The position of the operand.
348 * @return 1, if the operand is significant for register allocation, 0
351 extern int arch_is_register_operand(const arch_env_t *env,
352 const ir_node *irn, int pos);
355 * Get the number of allocatable registers concerning
356 * a register class for an operand of a node.
357 * @param env The environment.
358 * @param irn The node.
359 * @param pos The postition of the node's operand.
360 * @param cls The register class.
361 * @param bs The bitset all allocatable registers shall be put into.
362 * Note, that you can also pass NULL here. If you don't,
363 * make sure, the bitset is as large as the register class
365 * @return The amount of registers allocatable for that operand.
367 extern int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn,
368 int pos, const arch_register_class_t *cls, bitset_t *bs);
371 * Check, if a register is assignable to an operand of a node.
372 * @param env The architecture environment.
373 * @param irn The node.
374 * @param pos The position of the operand.
375 * @param reg The register.
376 * @return 1, if the register might be allocated to the operand 0 if not.
378 extern int arch_reg_is_allocatable(const arch_env_t *env,
379 const ir_node *irn, int pos, const arch_register_t *reg);
382 * Get the register class of an operand of a node.
383 * @param env The architecture environment.
384 * @param irn The node.
385 * @param pos The position of the operand.
386 * @return The register class of the operand or NULL, if
387 * operand is a non-register operand.
389 extern const arch_register_class_t *
390 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos);
393 * Get the register allocated at a certain output operand of a node.
394 * @param env The arch nvironment.
395 * @param irn The node.
396 * @param idx The index of the output operand.
397 * @return The register allocated for this operand
399 extern const arch_register_t *
400 arch_get_irn_register(const arch_env_t *env, const ir_node *irn, int idx);
403 * Set the register for a certain output operand.
404 * @param env The architecture environment.
405 * @param irn The node.
406 * @param idx The index of the output operand.
407 * @param reg The register.
409 extern void arch_set_irn_register(const arch_env_t *env,
410 ir_node *irn, int idx, const arch_register_t *reg);
414 * @param env The architecture environment.
415 * @param irn The node.
416 * @return A classification of the node.
418 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn);
421 * Get the flags of a node.
422 * @param env The architecture environment.
423 * @param irn The node.
426 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn);
428 #define arch_irn_has_reg_class(env, irn, pos, cls) \
429 ((cls) == arch_get_irn_reg_class(env, irn, pos))
432 * Somebody who can be asked about nodes.
434 struct _arch_irn_handler_t {
437 * Get the operations of an irn.
438 * @param self The handler from which the method is invoked.
439 * @param irn Some node.
440 * @return Operations for that irn.
442 const arch_irn_ops_t *(*get_irn_ops)(const arch_irn_handler_t *handler,
448 * Architecture interface.
450 struct _arch_isa_if_t {
453 * Initialize the isa interface.
458 * Get the the number of register classes in the isa.
459 * @return The number of register classes.
461 int (*get_n_reg_class)(void);
464 * Get the i-th register class.
465 * @param i The number of the register class.
466 * @return The register class.
468 const arch_register_class_t *(*get_reg_class)(int i);
472 * This function is called each time, the backend starts running over
474 * @param irg The graph.
476 void (*prepare_graph)(ir_graph *irg);
479 #define ARCH_MAX_HANDLERS 8
482 * Environment for the architecture infrastructure.
483 * Keep this everywhere you're going.
486 const struct _be_node_factory_t *node_factory; /**< The node factory for be nodes. */
487 const arch_isa_if_t *isa; /**< The isa about which everything is. */
489 arch_irn_handler_t const *handlers[ARCH_MAX_HANDLERS]; /**< The handlers are organized as
492 int handlers_tos; /**< The stack pointer of the handler
497 * Get the isa of an arch environment.
498 * @param env The environment.
499 * @return The isa with which the env was initialized with.
501 #define arch_env_get_isa(env) ((env)->isa)
504 * Initialize the architecture environment struct.
505 * @param isa The isa which shall be put into the environment.
506 * @return The environment.
508 extern arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa);
511 * Add a node handler to the environment.
512 * @param env The environment.
513 * @param handler A node handler.
514 * @return The environment itself.
516 extern arch_env_t *arch_env_add_irn_handler(arch_env_t *env,
517 const arch_irn_handler_t *handler);
519 #endif /* _FIRM_BEARCH_H */