8 #include "firm_config.h"
11 #include <libcore/lc_opts.h>
24 #include "belistsched.h"
26 typedef struct _arch_register_class_t arch_register_class_t;
27 typedef struct _arch_register_t arch_register_t;
28 typedef struct _arch_isa_if_t arch_isa_if_t;
29 typedef struct _arch_isa_t arch_isa_t;
30 typedef struct _arch_env_t arch_env_t;
31 typedef struct _arch_irn_ops_t arch_irn_ops_t;
32 typedef struct _arch_irn_handler_t arch_irn_handler_t;
33 typedef struct _arch_code_generator_t arch_code_generator_t;
34 typedef struct _arch_code_generator_if_t arch_code_generator_if_t;
36 struct _be_node_factory_t;
38 typedef enum _arch_register_type_t {
39 arch_register_type_none = 0,
40 arch_register_type_write_invariant,
41 arch_register_type_caller_saved, /**< The register must be saved by the caller
42 upon a function call. It thus can be overwritten
43 in the called function. */
44 arch_register_type_callee_saved, /**< The register must be saved by the called function,
45 it thus survives a function call. */
46 arch_register_type_ignore /**< Do not consider this register when allocating. */
47 } arch_register_type_t;
52 struct _arch_register_t {
53 const char *name; /**< The name of the register. */
54 const arch_register_class_t *reg_class; /**< The class the register belongs to. */
55 int index; /**< The index of the register in the class. */
56 arch_register_type_t type; /**< The type of the register. */
57 void *data; /**< Custom data. */
60 static INLINE const arch_register_class_t *
61 _arch_register_get_class(const arch_register_t *reg)
63 return reg->reg_class;
66 static INLINE int _arch_register_get_index(const arch_register_t *reg)
71 #define arch_register_get_class(reg) _arch_register_get_class(reg)
72 #define arch_register_get_index(reg) _arch_register_get_index(reg)
73 #define arch_register_get_name(reg) ((reg)->name)
76 * A class of registers.
77 * Like general purpose or floating point.
79 struct _arch_register_class_t {
80 const char *name; /**< The name of the register class. */
81 int n_regs; /**< Number of registers in this class. */
82 const arch_register_t *regs; /**< The array of registers. */
85 #define arch_register_class_n_regs(cls) ((cls)->n_regs)
88 * Put all registers in a class into a bitset.
89 * @param cls The class.
90 * @param bs The bitset. May be NULL.
91 * @return The number of registers in the class.
93 extern int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs);
95 static INLINE const arch_register_t *
96 _arch_register_for_index(const arch_register_class_t *cls, int idx)
98 assert(0 <= idx && idx < cls->n_regs);
99 return &cls->regs[idx];
102 #define arch_register_for_index(cls, idx) \
103 _arch_register_for_index(cls, idx)
106 * Get the register set for a register class.
107 * @param cls The register class.
108 * @return The set containing all registers in the class.
110 #define arch_get_register_set_for_class(cls) ((cls)->set)
112 typedef enum _arch_operand_type_t {
113 arch_operand_type_invalid,
114 arch_operand_type_memory,
115 arch_operand_type_register,
116 arch_operand_type_immediate,
117 arch_operand_type_symconst,
118 arch_operand_type_last
119 } arch_operand_type_t;
122 * Different types of register allocation requirements.
124 typedef enum _arch_register_req_type_t {
125 arch_register_req_type_none = 0, /**< No register requirement. */
127 arch_register_req_type_normal = 1, /**< All registers in the class
130 arch_register_req_type_limited = 2, /**< Only a real subset of
131 the class is allowed. */
133 arch_register_req_type_should_be_same = 4, /**< The register should be equal
134 another one at the node. */
136 arch_register_req_type_should_be_different = 8, /**< The register must be unequal
137 to some other at the node. */
139 } arch_register_req_type_t;
142 * Convenience macro to check for set constraints.
143 * @param req A pointer to register requirements.
144 * @param kind The kind of constraint to check for (see arch_register_req_type_t).
145 * @return 1, If the kind of constraint is present, 0 if not.
147 #define arch_register_req_is(req, kind) \
148 (((req)->type & (arch_register_req_type_ ## kind)) != 0)
151 * Expresses requirements to register allocation for an operand.
153 typedef struct _arch_register_req_t {
154 arch_register_req_type_t type; /**< The type of the constraint. */
155 const arch_register_class_t *cls; /**< The register class this constraint belongs to. */
157 int (*limited)(const ir_node *irn, int pos, bitset_t *bs);
158 /**< In case of the 'limited'
159 constraint, this function
160 must put all allowable
161 registers in the bitset and
162 return the number of registers
165 ir_node *other; /**< In case of "should be equal"
166 or should be different, this gives
167 the node to whose register this
168 one's should be the same/different. */
169 } arch_register_req_t;
172 * Certain node classes which are relevant for the register allocator.
174 typedef enum _arch_irn_class_t {
175 arch_irn_class_normal,
176 arch_irn_class_spill,
177 arch_irn_class_reload,
180 arch_irn_class_branch
184 * Some flags describing a node in more detail.
186 typedef enum _arch_irn_flags_t {
187 arch_irn_flags_spillable = 1,
188 arch_irn_flags_rematerializable = 2
191 struct _arch_irn_ops_t {
194 * Get the register requirements for a given operand.
195 * @param self The self pointer.
196 * @param irn The node.
197 * @param pos The operand's position
198 * (-1 for the result of the node, 0..n for the input
200 * @return The register requirements for the selected operand.
201 * The pointer returned is never NULL.
203 const arch_register_req_t *(*get_irn_reg_req)(const arch_irn_ops_t *self,
204 arch_register_req_t *req, const ir_node *irn, int pos);
207 * Set the register for an output operand.
208 * @param irn The node.
209 * @param reg The register allocated to that operand.
210 * @note If the operand is not a register operand,
211 * the call is ignored.
213 void (*set_irn_reg)(const arch_irn_ops_t *self, ir_node *irn, const arch_register_t *reg);
216 * Get the register allocated for an output operand.
217 * @param irn The node.
218 * @return The register allocated at that operand. NULL, if
219 * the operand was no register operand or
220 * @c arch_register_invalid, if no register has yet been
221 * allocated for this node.
223 const arch_register_t *(*get_irn_reg)(const arch_irn_ops_t *self, const ir_node *irn);
227 * @param irn The node.
228 * @return A classification.
230 arch_irn_class_t (*classify)(const arch_irn_ops_t *self, const ir_node *irn);
233 * Get the flags of a node.
234 * @param self The irn ops themselves.
235 * @param irn The node.
236 * @return A set of flags.
238 arch_irn_flags_t (*get_flags)(const arch_irn_ops_t *self, const ir_node *irn);
243 * Get the register requirements for a node.
244 * @param env The architecture environment.
245 * @param req A pointer to a requirements structure, where the data can
247 * @param irn The node.
248 * @param pos The position of the operand you're interested in.
249 * @return A pointer to the register requirements which may <b>not</b>
250 * neccessarily be equal to @p req. If NULL is returned, the
251 * operand was no register operand.
253 extern const arch_register_req_t *
254 arch_get_register_req(const arch_env_t *env, arch_register_req_t *req,
255 const ir_node *irn, int pos);
258 * Check if an operand is a register operand.
259 * @param env The environment.
260 * @param irn The node.
261 * @param pos The position of the operand.
262 * @return 1, if the operand is significant for register allocation, 0
265 extern int arch_is_register_operand(const arch_env_t *env,
266 const ir_node *irn, int pos);
269 * Get the number of allocatable registers concerning
270 * a register class for an operand of a node.
271 * @param env The environment.
272 * @param irn The node.
273 * @param pos The postition of the node's operand.
274 * @param cls The register class.
275 * @param bs The bitset all allocatable registers shall be put into.
276 * Note, that you can also pass NULL here. If you don't,
277 * make sure, the bitset is as large as the register class
279 * @return The amount of registers allocatable for that operand.
281 extern int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn,
282 int pos, const arch_register_class_t *cls, bitset_t *bs);
285 * Check, if a register is assignable to an operand of a node.
286 * @param env The architecture environment.
287 * @param irn The node.
288 * @param pos The position of the operand.
289 * @param reg The register.
290 * @return 1, if the register might be allocated to the operand 0 if not.
292 extern int arch_reg_is_allocatable(const arch_env_t *env,
293 const ir_node *irn, int pos, const arch_register_t *reg);
296 * Get the register class of an operand of a node.
297 * @param env The architecture environment.
298 * @param irn The node.
299 * @param pos The position of the operand.
300 * @return The register class of the operand or NULL, if
301 * operand is a non-register operand.
303 extern const arch_register_class_t *
304 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos);
307 * Get the register allocated at a certain output operand of a node.
308 * @param env The arch environment.
309 * @param irn The node.
310 * @return The register allocated for this operand
312 extern const arch_register_t *
313 arch_get_irn_register(const arch_env_t *env, const ir_node *irn);
316 * Set the register for a certain output operand.
317 * @param env The architecture environment.
318 * @param irn The node.
319 * @param idx The index of the output operand.
320 * @param reg The register.
322 extern void arch_set_irn_register(const arch_env_t *env, ir_node *irn,
323 const arch_register_t *reg);
327 * @param env The architecture environment.
328 * @param irn The node.
329 * @return A classification of the node.
331 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn);
334 * Get the flags of a node.
335 * @param env The architecture environment.
336 * @param irn The node.
339 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn);
341 #define arch_irn_has_reg_class(env, irn, pos, cls) \
342 ((cls) == arch_get_irn_reg_class(env, irn, pos))
345 * Somebody who can be asked about nodes.
347 struct _arch_irn_handler_t {
350 * Get the operations of an irn.
351 * @param self The handler from which the method is invoked.
352 * @param irn Some node.
353 * @return Operations for that irn.
355 const arch_irn_ops_t *(*get_irn_ops)(const arch_irn_handler_t *handler,
361 * The code generator.
363 struct _arch_code_generator_if_t {
367 * Initialize the code generator.
368 * @param file The file to dump to.
369 * @param irg The function to generate code for.
370 * @param env The architecture environment.
371 * @return A newly created code generator.
373 void *(*init)(FILE *file, ir_graph *irg, const arch_env_t *env);
376 * Called, when the graph is being normalized.
378 void (*prepare_graph)(void *self);
381 * Called before scheduling.
383 void (*before_sched)(void *self);
386 * Called before register allocation.
388 void (*before_ra)(void *self);
391 * Called after everything happened.
392 * The code generator must also be de-allocated here.
394 void (*done)(void *self);
398 #define _arch_cg_call(cg, func) \
400 if((cg)->impl->func) \
401 (cg)->impl->func(cg); \
404 #define arch_code_generator_prepare_graph(cg) _arch_cg_call(cg, prepare_graph)
405 #define arch_code_generator_before_sched(cg) _arch_cg_call(cg, before_sched)
406 #define arch_code_generator_before_ra(cg) _arch_cg_call(cg, before_ra)
407 #define arch_code_generator_done(cg) _arch_cg_call(cg, done)
410 * Code generator base class.
412 struct _arch_code_generator_t {
413 const arch_code_generator_if_t *impl;
420 const arch_isa_if_t *impl;
424 * Architecture interface.
426 struct _arch_isa_if_t {
429 void (*register_options)(lc_opt_entry_t *grp);
433 * Initialize the isa interface.
438 * Free the isa instance.
440 void (*done)(void *self);
443 * Get the the number of register classes in the isa.
444 * @return The number of register classes.
446 int (*get_n_reg_class)(const void *self);
449 * Get the i-th register class.
450 * @param i The number of the register class.
451 * @return The register class.
453 const arch_register_class_t *(*get_reg_class)(const void *self, int i);
456 * The irn handler for this architecture.
457 * The irn handler is registered by the Firm back end
458 * when the architecture is initialized.
461 const arch_irn_handler_t *(*get_irn_handler)(const void *self);
464 * Get the code generator interface.
465 * @param self The this pointer.
466 * @return Some code generator interface.
468 const arch_code_generator_if_t *(*get_code_generator)(void *self);
471 * Get the list scheduler to use.
472 * @param self The isa object.
473 * @return The list scheduler selector.
475 const list_sched_selector_t *(*get_list_sched_selector)(const void *self);
478 #define arch_isa_get_n_reg_class(isa) ((isa)->impl->get_n_reg_class(isa))
479 #define arch_isa_get_reg_class(isa,i) ((isa)->impl->get_reg_class(isa, i))
480 #define arch_isa_get_irn_handler(isa) ((isa)->impl->get_irn_handler(isa))
481 #define arch_isa_make_code_generator(isa,irg) ((isa)->impl->make_code_generator(isa, irg))
483 #define ARCH_MAX_HANDLERS 8
486 * Environment for the architecture infrastructure.
487 * Keep this everywhere you're going.
490 const struct _be_node_factory_t *node_factory; /**< The node factory for be nodes. */
491 arch_isa_t *isa; /**< The isa about which everything is. */
493 arch_irn_handler_t const *handlers[ARCH_MAX_HANDLERS]; /**< The handlers are organized as
496 int handlers_tos; /**< The stack pointer of the handler
501 * Get the isa of an arch environment.
502 * @param env The environment.
503 * @return The isa with which the env was initialized with.
505 #define arch_env_get_isa(env) ((env)->isa)
508 * Initialize the architecture environment struct.
509 * @param isa The isa which shall be put into the environment.
510 * @return The environment.
512 extern arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa);
515 * Add a node handler to the environment.
516 * @param env The environment.
517 * @param handler A node handler.
518 * @return The environment itself.
520 extern arch_env_t *arch_env_add_irn_handler(arch_env_t *env,
521 const arch_irn_handler_t *handler);
523 #endif /* _FIRM_BEARCH_H */