10 #include <libcore/lc_opts.h>
26 #include "belistsched.h"
29 typedef struct _arch_register_class_t arch_register_class_t;
30 typedef struct _arch_register_t arch_register_t;
31 typedef struct _arch_isa_if_t arch_isa_if_t;
32 typedef struct _arch_isa_t arch_isa_t;
33 typedef struct _arch_env_t arch_env_t;
34 typedef struct _arch_irn_ops_if_t arch_irn_ops_if_t;
35 typedef struct _arch_irn_ops_t arch_irn_ops_t;
36 typedef struct _arch_irn_handler_t arch_irn_handler_t;
37 typedef struct _arch_code_generator_t arch_code_generator_t;
38 typedef struct _arch_code_generator_if_t arch_code_generator_if_t;
40 struct _be_node_factory_t;
42 typedef enum _arch_register_type_t {
43 arch_register_type_none = 0,
44 arch_register_type_caller_save = 1, /**< The register must be saved by the caller
45 upon a function call. It thus can be overwritten
46 in the called function. */
47 arch_register_type_callee_save = 2, /**< The register must be saved by the caller
48 upon a function call. It thus can be overwritten
49 in the called function. */
50 arch_register_type_ignore = 4, /**< Do not consider this register when allocating. */
51 arch_register_type_sp = 8, /**< This register is the stack pointer of the architecture. */
52 arch_register_type_bp = 16, /**< The register is the base pointer of the architecture. */
53 } arch_register_type_t;
56 * Convenience macro to check for register type.
57 * @param req A pointer to register.
58 * @param kind The kind of type to check for (see arch_register_type_t).
59 * @return 1, If register is of given kind, 0 if not.
61 #define arch_register_type_is(reg, kind) \
62 ((reg)->type == arch_register_type_ ## kind)
67 struct _arch_register_t {
68 const char *name; /**< The name of the register. */
69 const arch_register_class_t *reg_class; /**< The class the register belongs to. */
70 int index; /**< The index of the register in the class. */
71 arch_register_type_t type; /**< The type of the register. */
72 void *data; /**< Custom data. */
75 static INLINE const arch_register_class_t *
76 _arch_register_get_class(const arch_register_t *reg)
78 return reg->reg_class;
81 static INLINE int _arch_register_get_index(const arch_register_t *reg)
86 #define arch_register_get_class(reg) _arch_register_get_class(reg)
87 #define arch_register_get_index(reg) _arch_register_get_index(reg)
88 #define arch_register_get_name(reg) ((reg)->name)
91 * A class of registers.
92 * Like general purpose or floating point.
94 struct _arch_register_class_t {
95 const char *name; /**< The name of the register class. */
96 int n_regs; /**< Number of registers in this class. */
97 ir_mode *mode; /**< The mode of the register class. */
98 const arch_register_t *regs; /**< The array of registers. */
101 #define arch_register_class_n_regs(cls) ((cls)->n_regs)
104 * Put all registers in a class into a bitset.
105 * @param cls The class.
106 * @param bs The bitset. May be NULL.
107 * @return The number of registers in the class.
109 extern int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs);
111 static INLINE const arch_register_t *
112 _arch_register_for_index(const arch_register_class_t *cls, int idx)
114 assert(0 <= idx && idx < cls->n_regs);
115 return &cls->regs[idx];
118 #define arch_register_for_index(cls, idx) \
119 _arch_register_for_index(cls, idx)
121 typedef enum _arch_operand_type_t {
122 arch_operand_type_invalid,
123 arch_operand_type_memory,
124 arch_operand_type_register,
125 arch_operand_type_immediate,
126 arch_operand_type_symconst,
127 arch_operand_type_last
128 } arch_operand_type_t;
131 * Different types of register allocation requirements.
133 typedef enum _arch_register_req_type_t {
134 arch_register_req_type_none = 0, /**< No register requirement. */
136 arch_register_req_type_normal = 1, /**< All registers in the class
139 arch_register_req_type_limited = 2, /**< Only a real subset of
140 the class is allowed. */
142 arch_register_req_type_should_be_same = 4, /**< The register should be equal
143 another one at the node. */
145 arch_register_req_type_should_be_different = 8, /**< The register must be unequal
146 to some other at the node. */
148 } arch_register_req_type_t;
151 * Convenience macro to check for set constraints.
152 * @param req A pointer to register requirements.
153 * @param kind The kind of constraint to check for (see arch_register_req_type_t).
154 * @return 1, If the kind of constraint is present, 0 if not.
156 #define arch_register_req_is(req, kind) \
157 (((req)->type & (arch_register_req_type_ ## kind)) != 0)
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 constraint belongs to. */
166 void (*limited)(void *limited_env, bitset_t *bs);
167 /**< In case of the 'limited'
168 constraint, this function
169 must put all allowable
170 registers in the bitset and
171 return the number of registers
174 void *limited_env; /**< This must passed to limited. */
176 ir_node *other_same; /**< The other which shall have the same reg
177 as this one. (for case should_be_same). */
179 ir_node *other_different; /**< The other node from which this one's register
180 must be different (case must_be_different). */
181 } arch_register_req_t;
184 * Certain node classes which are relevant 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,
192 arch_irn_class_branch,
197 * Some flags describing a node in more detail.
199 typedef enum _arch_irn_flags_t {
200 arch_irn_flags_none = 0, /**< Node flags. */
201 arch_irn_flags_dont_spill = 1, /**< This must not be spilled. */
202 arch_irn_flags_rematerializable = 2, /**< This should be replicated instead of spilled/reloaded. */
203 arch_irn_flags_ignore = 4, /**< Do not consider the node during register allocation. */
206 struct _arch_irn_ops_if_t {
209 * Get the register requirements for a given operand.
210 * @param self The self pointer.
211 * @param irn The node.
212 * @param pos The operand's position
213 * (-1 for the result of the node, 0..n for the input
215 * @return The register requirements for the selected operand.
216 * The pointer returned is never NULL.
218 const arch_register_req_t *(*get_irn_reg_req)(const void *self,
219 arch_register_req_t *req, const ir_node *irn, int pos);
222 * Set the register for an output operand.
223 * @param irn The node.
224 * @param reg The register allocated to that operand.
225 * @note If the operand is not a register operand,
226 * the call is ignored.
228 void (*set_irn_reg)(const void *self, ir_node *irn, const arch_register_t *reg);
231 * Get the register allocated for an output operand.
232 * @param irn The node.
233 * @return The register allocated at that operand. NULL, if
234 * the operand was no register operand or
235 * @c arch_register_invalid, if no register has yet been
236 * allocated for this node.
238 const arch_register_t *(*get_irn_reg)(const void *self, const ir_node *irn);
242 * @param irn The node.
243 * @return A classification.
245 arch_irn_class_t (*classify)(const void *self, const ir_node *irn);
248 * Get the flags of a node.
249 * @param self The irn ops themselves.
250 * @param irn The node.
251 * @return A set of flags.
253 arch_irn_flags_t (*get_flags)(const void *self, const ir_node *irn);
256 * Set a bias for the stack pointer.
257 * If the node in question uses the stack pointer for indexing, it must
258 * consider the value of <code>bias</code> additionally.
259 * @param self The this pointer.
260 * @param irn The node in question.
261 * @param bias The bias.
263 void (*set_stack_bias)(const void *self, ir_node *irn, int bias);
267 * irn_ops base class.
269 struct _arch_irn_ops_t {
270 const arch_irn_ops_if_t *impl;
274 arch_set_stack_bias(const arch_env_t *env, ir_node *irn, int bias);
277 * Get the register requirements for a node.
278 * @param env The architecture environment.
279 * @param req A pointer to a requirements structure, where the data can
281 * @param irn The node.
282 * @param pos The position of the operand you're interested in.
283 * @return A pointer to the register requirements which may <b>not</b>
284 * neccessarily be equal to @p req. If NULL is returned, the
285 * operand was no register operand.
287 extern const arch_register_req_t *
288 arch_get_register_req(const arch_env_t *env, arch_register_req_t *req,
289 const ir_node *irn, int pos);
292 * Check if an operand is a register operand.
293 * @param env The environment.
294 * @param irn The node.
295 * @param pos The position of the operand.
296 * @return 1, if the operand is significant for register allocation, 0
299 extern int arch_is_register_operand(const arch_env_t *env,
300 const ir_node *irn, int pos);
303 * Get the number of allocatable registers concerning
304 * a register class for an operand of a node.
305 * @param env The environment.
306 * @param irn The node.
307 * @param pos The postition of the node's operand.
308 * @param bs The bitset all allocatable registers shall be put into.
309 * Note, that you can also pass NULL here. If you don't,
310 * make sure, the bitset is as large as the register class
312 * @return The amount of registers allocatable for that operand.
314 extern int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn, int pos, bitset_t *bs);
317 * Put all registers which shall not be ignored by the register
318 * allocator in a bit set.
319 * @param env The arch env.
320 * @param cls The register class to consider.
321 * @param bs The bit set to put the registers to.
323 extern void arch_put_non_ignore_regs(const arch_env_t *env, const arch_register_class_t *cls, bitset_t *bs);
326 * Check, if a register is assignable to an operand of a node.
327 * @param env The architecture environment.
328 * @param irn The node.
329 * @param pos The position of the operand.
330 * @param reg The register.
331 * @return 1, if the register might be allocated to the operand 0 if not.
333 extern int arch_reg_is_allocatable(const arch_env_t *env,
334 const ir_node *irn, int pos, const arch_register_t *reg);
337 * Get the register class of an operand of a node.
338 * @param env The architecture environment.
339 * @param irn The node.
340 * @param pos The position of the operand.
341 * @return The register class of the operand or NULL, if
342 * operand is a non-register operand.
344 extern const arch_register_class_t *
345 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos);
348 * Get the register allocated at a certain output operand of a node.
349 * @param env The arch environment.
350 * @param irn The node.
351 * @return The register allocated for this operand
353 extern const arch_register_t *
354 arch_get_irn_register(const arch_env_t *env, const ir_node *irn);
357 * Set the register for a certain output operand.
358 * @param env The architecture environment.
359 * @param irn The node.
360 * @param idx The index of the output operand.
361 * @param reg The register.
363 extern void arch_set_irn_register(const arch_env_t *env, ir_node *irn,
364 const arch_register_t *reg);
368 * @param env The architecture environment.
369 * @param irn The node.
370 * @return A classification of the node.
372 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn);
375 * Get the flags of a node.
376 * @param env The architecture environment.
377 * @param irn The node.
380 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn);
382 #define arch_irn_is_ignore(env, irn) ((arch_irn_get_flags(env, irn) & arch_irn_flags_ignore) != 0)
384 #define arch_irn_has_reg_class(env, irn, pos, cls) \
385 ((cls) == arch_get_irn_reg_class(env, irn, pos))
387 #define arch_irn_consider_in_reg_alloc(env, cls, irn) \
388 (arch_irn_has_reg_class(env, irn, -1, cls) && !arch_irn_is_ignore(env, irn))
391 * Somebody who can be asked about nodes.
393 struct _arch_irn_handler_t {
396 * Get the operations of an irn.
397 * @param self The handler from which the method is invoked.
398 * @param irn Some node.
399 * @return Operations for that irn.
401 const void *(*get_irn_ops)(const arch_irn_handler_t *handler,
407 * The code generator.
409 struct _arch_code_generator_if_t {
413 * Initialize the code generator.
414 * @param file The file to dump to.
415 * @param irg The function to generate code for.
416 * @param env The architecture environment.
417 * @return A newly created code generator.
419 void *(*init)(FILE *file, const be_irg_t *birg);
422 * Called, when the graph is being normalized.
424 void (*prepare_graph)(void *self);
427 * Called before scheduling.
429 void (*before_sched)(void *self);
432 * Called before register allocation.
434 void (*before_ra)(void *self);
437 * Called after register allocation to lower Spills to Stores
438 * @param self The code generator
439 * @param spill The spill node to lower
440 * @return The new backend node which substitutes the spill
442 ir_node *(*lower_spill)(void *self, ir_node *spill);
445 * Called after register allocation to lower Reloads to Loads
446 * @param self The code generator
447 * @param reload The reload node to lower
448 * @return The new backend node which substitutes the reload
450 ir_node *(*lower_reload)(void *self, ir_node *reload);
453 * Called after everything happened.
454 * The code generator must also be de-allocated here.
456 void (*done)(void *self);
460 #define _arch_cg_call(cg, func) \
462 if((cg)->impl->func) \
463 (cg)->impl->func(cg); \
466 #define arch_code_generator_prepare_graph(cg) _arch_cg_call(cg, prepare_graph)
467 #define arch_code_generator_before_sched(cg) _arch_cg_call(cg, before_sched)
468 #define arch_code_generator_before_ra(cg) _arch_cg_call(cg, before_ra)
469 #define arch_code_generator_done(cg) _arch_cg_call(cg, done)
472 * Code generator base class.
474 struct _arch_code_generator_t {
475 const arch_code_generator_if_t *impl;
482 const arch_isa_if_t *impl;
483 const arch_register_t *sp; /** The stack pointer register. */
484 const arch_register_t *bp; /** The base pointer register. */
485 const int stack_dir; /** -1 for decreasing, 1 for increasing. */
488 #define arch_isa_stack_dir(isa) ((isa)->stack_dir)
489 #define arch_isa_sp(isa) ((isa)->sp)
490 #define arch_isa_bp(isa) ((isa)->bp)
493 * Architecture interface.
495 struct _arch_isa_if_t {
498 void (*register_options)(lc_opt_entry_t *grp);
502 * Initialize the isa interface.
507 * Free the isa instance.
509 void (*done)(void *self);
512 * Get the the number of register classes in the isa.
513 * @return The number of register classes.
515 int (*get_n_reg_class)(const void *self);
518 * Get the i-th register class.
519 * @param i The number of the register class.
520 * @return The register class.
522 const arch_register_class_t *(*get_reg_class)(const void *self, int i);
525 * Get the register class which shall be used to store a value of a given mode.
526 * @param self The this pointer.
527 * @param mode The mode in question.
528 * @return A register class which can hold values of the given mode.
530 const arch_register_class_t *(*get_reg_class_for_mode)(const void *self, const ir_mode *mode);
533 * Get the ABI restrictions for procedure calls.
534 * @param self The this pointer.
535 * @param method_type The type of the method (procedure) in question.
536 * @param p The array of parameter locations to be filled.
538 void (*get_call_abi)(const void *self, ir_type *method_type, be_abi_call_t *abi);
541 * The irn handler for this architecture.
542 * The irn handler is registered by the Firm back end
543 * when the architecture is initialized.
546 const arch_irn_handler_t *(*get_irn_handler)(const void *self);
549 * Get the code generator interface.
550 * @param self The this pointer.
551 * @return Some code generator interface.
553 const arch_code_generator_if_t *(*get_code_generator_if)(void *self);
556 * Get the list scheduler to use.
557 * @param self The isa object.
558 * @return The list scheduler selector.
560 const list_sched_selector_t *(*get_list_sched_selector)(const void *self);
563 #define arch_isa_get_n_reg_class(isa) ((isa)->impl->get_n_reg_class(isa))
564 #define arch_isa_get_reg_class(isa,i) ((isa)->impl->get_reg_class(isa, i))
565 #define arch_isa_get_irn_handler(isa) ((isa)->impl->get_irn_handler(isa))
566 #define arch_isa_get_call_abi(isa,tp,abi) ((isa)->impl->get_call_abi((isa), (tp), (abi)))
567 #define arch_isa_get_reg_class_for_mode(isa,mode) ((isa)->impl->get_reg_class_for_mode((isa), (mode)))
568 #define arch_isa_make_code_generator(isa,irg) ((isa)->impl->make_code_generator(isa, irg))
570 #define ARCH_MAX_HANDLERS 8
573 * Environment for the architecture infrastructure.
574 * Keep this everywhere you're going.
577 const struct _be_node_factory_t *node_factory; /**< The node factory for be nodes. */
578 arch_isa_t *isa; /**< The isa about which everything is. */
580 arch_irn_handler_t const *handlers[ARCH_MAX_HANDLERS]; /**< The handlers are organized as
583 int handlers_tos; /**< The stack pointer of the handler
588 * Get the isa of an arch environment.
589 * @param env The environment.
590 * @return The isa with which the env was initialized with.
592 #define arch_env_get_isa(env) ((env)->isa)
595 * Initialize the architecture environment struct.
596 * @param isa The isa which shall be put into the environment.
597 * @return The environment.
599 extern arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa);
602 * Add a node handler to the environment.
603 * @param env The environment.
604 * @param handler A node handler.
605 * @return The environment itself.
607 extern arch_env_t *arch_env_add_irn_handler(arch_env_t *env, const arch_irn_handler_t *handler);
609 #endif /* _FIRM_BEARCH_H */