10 #include <libcore/lc_opts.h>
25 #include "belistsched.h"
28 typedef struct _arch_register_class_t arch_register_class_t;
29 typedef struct _arch_register_t arch_register_t;
30 typedef struct _arch_isa_if_t arch_isa_if_t;
31 typedef struct _arch_isa_t arch_isa_t;
32 typedef struct _arch_env_t arch_env_t;
33 typedef struct _arch_irn_ops_if_t arch_irn_ops_if_t;
34 typedef struct _arch_irn_ops_t arch_irn_ops_t;
35 typedef struct _arch_irn_handler_t arch_irn_handler_t;
36 typedef struct _arch_code_generator_t arch_code_generator_t;
37 typedef struct _arch_code_generator_if_t arch_code_generator_if_t;
39 struct _be_node_factory_t;
41 typedef enum _arch_register_type_t {
42 arch_register_type_none = 0,
43 arch_register_type_caller_save = 1, /**< The register must be saved by the caller
44 upon a function call. It thus can be overwritten
45 in the called function. */
46 arch_register_type_callee_save = 2, /**< The register must be saved by the caller
47 upon a function call. It thus can be overwritten
48 in the called function. */
49 arch_register_type_ignore = 4, /**< Do not consider this register when allocating. */
50 arch_register_type_sp = 8, /**< This register is the stack pointer of the architecture. */
51 arch_register_type_bp = 16, /**< The register is the base pointer of the architecture. */
52 } arch_register_type_t;
55 * Convenience macro to check for register type.
56 * @param req A pointer to register.
57 * @param kind The kind of type to check for (see arch_register_type_t).
58 * @return 1, If register is of given kind, 0 if not.
60 #define arch_register_type_is(reg, kind) \
61 ((reg)->type == arch_register_type_ ## kind)
66 struct _arch_register_t {
67 const char *name; /**< The name of the register. */
68 const arch_register_class_t *reg_class; /**< The class the register belongs to. */
69 int index; /**< The index of the register in the class. */
70 arch_register_type_t type; /**< The type of the register. */
71 void *data; /**< Custom data. */
74 static INLINE const arch_register_class_t *
75 _arch_register_get_class(const arch_register_t *reg)
77 return reg->reg_class;
80 static INLINE int _arch_register_get_index(const arch_register_t *reg)
85 #define arch_register_get_class(reg) _arch_register_get_class(reg)
86 #define arch_register_get_index(reg) _arch_register_get_index(reg)
87 #define arch_register_get_name(reg) ((reg)->name)
90 * A class of registers.
91 * Like general purpose or floating point.
93 struct _arch_register_class_t {
94 const char *name; /**< The name of the register class. */
95 int n_regs; /**< Number of registers in this class. */
96 ir_mode *mode; /**< The mode of the register class. */
97 const arch_register_t *regs; /**< The array of registers. */
100 #define arch_register_class_n_regs(cls) ((cls)->n_regs)
103 * Put all registers in a class into a bitset.
104 * @param cls The class.
105 * @param bs The bitset. May be NULL.
106 * @return The number of registers in the class.
108 extern int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs);
110 static INLINE const arch_register_t *
111 _arch_register_for_index(const arch_register_class_t *cls, int idx)
113 assert(0 <= idx && idx < cls->n_regs);
114 return &cls->regs[idx];
117 #define arch_register_for_index(cls, idx) \
118 _arch_register_for_index(cls, idx)
120 typedef enum _arch_operand_type_t {
121 arch_operand_type_invalid,
122 arch_operand_type_memory,
123 arch_operand_type_register,
124 arch_operand_type_immediate,
125 arch_operand_type_symconst,
126 arch_operand_type_last
127 } arch_operand_type_t;
130 * Different types of register allocation requirements.
132 typedef enum _arch_register_req_type_t {
133 arch_register_req_type_none = 0, /**< No register requirement. */
135 arch_register_req_type_normal = 1, /**< All registers in the class
138 arch_register_req_type_limited = 2, /**< Only a real subset of
139 the class is allowed. */
141 arch_register_req_type_should_be_same = 4, /**< The register should be equal
142 another one at the node. */
144 arch_register_req_type_should_be_different = 8, /**< The register must be unequal
145 to some other at the node. */
147 } arch_register_req_type_t;
150 * Convenience macro to check for set constraints.
151 * @param req A pointer to register requirements.
152 * @param kind The kind of constraint to check for (see arch_register_req_type_t).
153 * @return 1, If the kind of constraint is present, 0 if not.
155 #define arch_register_req_is(req, kind) \
156 (((req)->type & (arch_register_req_type_ ## kind)) != 0)
159 * Expresses requirements to register allocation for an operand.
161 typedef struct _arch_register_req_t {
162 arch_register_req_type_t type; /**< The type of the constraint. */
163 const arch_register_class_t *cls; /**< The register class this constraint belongs to. */
165 void (*limited)(void *limited_env, bitset_t *bs);
166 /**< In case of the 'limited'
167 constraint, this function
168 must put all allowable
169 registers in the bitset and
170 return the number of registers
173 void *limited_env; /**< This must passed to limited. */
175 ir_node *other_same; /**< The other which shall have the same reg
176 as this one. (for case should_be_same). */
178 ir_node *other_different; /**< The other node from which this one's register
179 must be different (case must_be_different). */
180 } arch_register_req_t;
183 * Certain node classes which are relevant for the register allocator.
185 typedef enum _arch_irn_class_t {
186 arch_irn_class_normal,
187 arch_irn_class_spill,
188 arch_irn_class_reload,
191 arch_irn_class_branch,
196 * Some flags describing a node in more detail.
198 typedef enum _arch_irn_flags_t {
199 arch_irn_flags_none = 0, /**< Node flags. */
200 arch_irn_flags_dont_spill = 1, /**< This must not be spilled. */
201 arch_irn_flags_rematerializable = 2, /**< This should be replicated instead of spilled/reloaded. */
202 arch_irn_flags_ignore = 4, /**< Do not consider the node during register allocation. */
205 struct _arch_irn_ops_if_t {
208 * Get the register requirements for a given operand.
209 * @param self The self pointer.
210 * @param irn The node.
211 * @param pos The operand's position
212 * (-1 for the result of the node, 0..n for the input
214 * @return The register requirements for the selected operand.
215 * The pointer returned is never NULL.
217 const arch_register_req_t *(*get_irn_reg_req)(const void *self,
218 arch_register_req_t *req, const ir_node *irn, int pos);
221 * Set the register for an output operand.
222 * @param irn The node.
223 * @param reg The register allocated to that operand.
224 * @note If the operand is not a register operand,
225 * the call is ignored.
227 void (*set_irn_reg)(const void *self, ir_node *irn, const arch_register_t *reg);
230 * Get the register allocated for an output operand.
231 * @param irn The node.
232 * @return The register allocated at that operand. NULL, if
233 * the operand was no register operand or
234 * @c arch_register_invalid, if no register has yet been
235 * allocated for this node.
237 const arch_register_t *(*get_irn_reg)(const void *self, const ir_node *irn);
241 * @param irn The node.
242 * @return A classification.
244 arch_irn_class_t (*classify)(const void *self, const ir_node *irn);
247 * Get the flags of a node.
248 * @param self The irn ops themselves.
249 * @param irn The node.
250 * @return A set of flags.
252 arch_irn_flags_t (*get_flags)(const void *self, const ir_node *irn);
255 * Check, if a node modifies the stack pointer by a constant.
256 * @param self The this pointer.
257 * @param irn The node in question.
258 * @return The (constant) amount by which the stack pointer is modifies
259 * by this node. Return 0, if irn does not touch the stack pointer.
260 * <tt>-n</tt> if the node modifies the sp by n against the stack's growing
261 * direction and <tt>n</tt> if irn modifies the stack by n along the stack's
264 int (*modifies_sp)(const void *self, const ir_node *irn);
267 * Set a bias for the stack pointer.
268 * If the node in question uses the stack pointer for indexing, it must
269 * consider the value of <code>bias</code> additionally.
270 * @param self The this pointer.
271 * @param irn The node in question.
272 * @param bias The bias.
274 void (*set_stack_bias)(const void *self, ir_node *irn, int bias);
278 * irn_ops base class.
280 struct _arch_irn_ops_t {
281 const arch_irn_ops_if_t *impl;
285 * Get the register requirements for a node.
286 * @param env The architecture environment.
287 * @param req A pointer to a requirements structure, where the data can
289 * @param irn The node.
290 * @param pos The position of the operand you're interested in.
291 * @return A pointer to the register requirements which may <b>not</b>
292 * neccessarily be equal to @p req. If NULL is returned, the
293 * operand was no register operand.
295 extern const arch_register_req_t *
296 arch_get_register_req(const arch_env_t *env, arch_register_req_t *req,
297 const ir_node *irn, int pos);
300 * Check if an operand is a register operand.
301 * @param env The environment.
302 * @param irn The node.
303 * @param pos The position of the operand.
304 * @return 1, if the operand is significant for register allocation, 0
307 extern int arch_is_register_operand(const arch_env_t *env,
308 const ir_node *irn, int pos);
311 * Get the number of allocatable registers concerning
312 * a register class for an operand of a node.
313 * @param env The environment.
314 * @param irn The node.
315 * @param pos The postition of the node's operand.
316 * @param bs The bitset all allocatable registers shall be put into.
317 * Note, that you can also pass NULL here. If you don't,
318 * make sure, the bitset is as large as the register class
320 * @return The amount of registers allocatable for that operand.
322 extern int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn, int pos, bitset_t *bs);
325 * Put all registers which shall not be ignored by the register
326 * allocator in a bit set.
327 * @param env The arch env.
328 * @param cls The register class to consider.
329 * @param bs The bit set to put the registers to.
331 extern void arch_put_non_ignore_regs(const arch_env_t *env, const arch_register_class_t *cls, bitset_t *bs);
334 * Check, if a register is assignable to an operand of a node.
335 * @param env The architecture environment.
336 * @param irn The node.
337 * @param pos The position of the operand.
338 * @param reg The register.
339 * @return 1, if the register might be allocated to the operand 0 if not.
341 extern int arch_reg_is_allocatable(const arch_env_t *env,
342 const ir_node *irn, int pos, const arch_register_t *reg);
345 * Get the register class of an operand of a node.
346 * @param env The architecture environment.
347 * @param irn The node.
348 * @param pos The position of the operand.
349 * @return The register class of the operand or NULL, if
350 * operand is a non-register operand.
352 extern const arch_register_class_t *
353 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos);
356 * Get the register allocated at a certain output operand of a node.
357 * @param env The arch environment.
358 * @param irn The node.
359 * @return The register allocated for this operand
361 extern const arch_register_t *
362 arch_get_irn_register(const arch_env_t *env, const ir_node *irn);
365 * Set the register for a certain output operand.
366 * @param env The architecture environment.
367 * @param irn The node.
368 * @param idx The index of the output operand.
369 * @param reg The register.
371 extern void arch_set_irn_register(const arch_env_t *env, ir_node *irn,
372 const arch_register_t *reg);
376 * @param env The architecture environment.
377 * @param irn The node.
378 * @return A classification of the node.
380 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn);
383 * Get the flags of a node.
384 * @param env The architecture environment.
385 * @param irn The node.
388 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn);
390 #define arch_irn_is_ignore(env, irn) ((arch_irn_get_flags(env, irn) & arch_irn_flags_ignore) != 0)
392 #define arch_irn_has_reg_class(env, irn, pos, cls) \
393 ((cls) == arch_get_irn_reg_class(env, irn, pos))
395 #define arch_irn_consider_in_reg_alloc(env, cls, irn) \
396 (arch_irn_has_reg_class(env, irn, -1, cls) && !arch_irn_is_ignore(env, irn))
399 * Somebody who can be asked about nodes.
401 struct _arch_irn_handler_t {
404 * Get the operations of an irn.
405 * @param self The handler from which the method is invoked.
406 * @param irn Some node.
407 * @return Operations for that irn.
409 const void *(*get_irn_ops)(const arch_irn_handler_t *handler,
415 * The code generator.
417 struct _arch_code_generator_if_t {
421 * Initialize the code generator.
422 * @param file The file to dump to.
423 * @param irg The function to generate code for.
424 * @param env The architecture environment.
425 * @return A newly created code generator.
427 void *(*init)(FILE *file, ir_graph *irg, const arch_env_t *env);
430 * Called, when the graph is being normalized.
432 void (*prepare_graph)(void *self);
435 * Called before scheduling.
437 void (*before_sched)(void *self);
440 * Called before register allocation.
442 void (*before_ra)(void *self);
445 * Called after register allocation to lower Spills to Stores
446 * @param self The code generator
447 * @param spill The spill node to lower
448 * @return The new backend node which substitutes the spill
450 ir_node *(*lower_spill)(void *self, ir_node *spill);
453 * Called after register allocation to lower Reloads to Loads
454 * @param self The code generator
455 * @param reload The reload node to lower
456 * @return The new backend node which substitutes the reload
458 ir_node *(*lower_reload)(void *self, ir_node *reload);
461 * Called after everything happened.
462 * The code generator must also be de-allocated here.
464 void (*done)(void *self);
468 #define _arch_cg_call(cg, func) \
470 if((cg)->impl->func) \
471 (cg)->impl->func(cg); \
474 #define arch_code_generator_prepare_graph(cg) _arch_cg_call(cg, prepare_graph)
475 #define arch_code_generator_before_sched(cg) _arch_cg_call(cg, before_sched)
476 #define arch_code_generator_before_ra(cg) _arch_cg_call(cg, before_ra)
477 #define arch_code_generator_done(cg) _arch_cg_call(cg, done)
480 * Code generator base class.
482 struct _arch_code_generator_t {
483 const arch_code_generator_if_t *impl;
490 const arch_isa_if_t *impl;
491 const arch_register_t *sp; /** The stack pointer register. */
492 const arch_register_t *bp; /** The base pointer register. */
493 const int stack_dir; /** -1 for decreasing, 1 for increasing. */
496 #define arch_isa_stack_dir(isa) ((isa)->stack_dir)
497 #define arch_isa_sp(isa) ((isa)->sp)
498 #define arch_isa_bp(isa) ((isa)->bp)
501 * Architecture interface.
503 struct _arch_isa_if_t {
506 void (*register_options)(lc_opt_entry_t *grp);
510 * Initialize the isa interface.
515 * Free the isa instance.
517 void (*done)(void *self);
520 * Get the the number of register classes in the isa.
521 * @return The number of register classes.
523 int (*get_n_reg_class)(const void *self);
526 * Get the i-th register class.
527 * @param i The number of the register class.
528 * @return The register class.
530 const arch_register_class_t *(*get_reg_class)(const void *self, int i);
533 * Get the register class which shall be used to store a value of a given mode.
534 * @param self The this pointer.
535 * @param mode The mode in question.
536 * @return A register class which can hold values of the given mode.
538 const arch_register_class_t *(*get_reg_class_for_mode)(const void *self, const ir_mode *mode);
541 * Get the ABI restrictions for procedure calls.
542 * @param self The this pointer.
543 * @param method_type The type of the method (procedure) in question.
544 * @param p The array of parameter locations to be filled.
546 void (*get_call_abi)(const void *self, ir_type *method_type, be_abi_call_t *abi);
549 * The irn handler for this architecture.
550 * The irn handler is registered by the Firm back end
551 * when the architecture is initialized.
554 const arch_irn_handler_t *(*get_irn_handler)(const void *self);
557 * Get the code generator interface.
558 * @param self The this pointer.
559 * @return Some code generator interface.
561 const arch_code_generator_if_t *(*get_code_generator_if)(void *self);
564 * Get the list scheduler to use.
565 * @param self The isa object.
566 * @return The list scheduler selector.
568 const list_sched_selector_t *(*get_list_sched_selector)(const void *self);
571 * Take a proj from a call, set the correct register and projnum for this proj
572 * @param self The isa object.
573 * @param proj The proj
574 * @param is_keep Non-zero if proj is a Keep argument
575 * @return The backend proj number assigned to this proj
577 long (*handle_call_proj)(const void *self, ir_node *proj, int is_keep);
580 #define arch_isa_get_n_reg_class(isa) ((isa)->impl->get_n_reg_class(isa))
581 #define arch_isa_get_reg_class(isa,i) ((isa)->impl->get_reg_class(isa, i))
582 #define arch_isa_get_irn_handler(isa) ((isa)->impl->get_irn_handler(isa))
583 #define arch_isa_get_call_abi(isa,tp,abi) ((isa)->impl->get_call_abi((isa), (tp), (abi)))
584 #define arch_isa_get_reg_class_for_mode(isa,mode) ((isa)->impl->get_reg_class_for_mode((isa), (mode)))
585 #define arch_isa_make_code_generator(isa,irg) ((isa)->impl->make_code_generator(isa, irg))
587 #define ARCH_MAX_HANDLERS 8
590 * Environment for the architecture infrastructure.
591 * Keep this everywhere you're going.
594 const struct _be_node_factory_t *node_factory; /**< The node factory for be nodes. */
595 arch_isa_t *isa; /**< The isa about which everything is. */
597 arch_irn_handler_t const *handlers[ARCH_MAX_HANDLERS]; /**< The handlers are organized as
600 int handlers_tos; /**< The stack pointer of the handler
605 * Get the isa of an arch environment.
606 * @param env The environment.
607 * @return The isa with which the env was initialized with.
609 #define arch_env_get_isa(env) ((env)->isa)
612 * Initialize the architecture environment struct.
613 * @param isa The isa which shall be put into the environment.
614 * @return The environment.
616 extern arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa);
619 * Add a node handler to the environment.
620 * @param env The environment.
621 * @param handler A node handler.
622 * @return The environment itself.
624 extern arch_env_t *arch_env_add_irn_handler(arch_env_t *env, const arch_irn_handler_t *handler);
626 #endif /* _FIRM_BEARCH_H */