10 #include <libcore/lc_opts.h>
23 #include "belistsched.h"
25 typedef struct _arch_register_class_t arch_register_class_t;
26 typedef struct _arch_register_t arch_register_t;
27 typedef struct _arch_isa_if_t arch_isa_if_t;
28 typedef struct _arch_isa_t arch_isa_t;
29 typedef struct _arch_env_t arch_env_t;
30 typedef struct _arch_irn_ops_t arch_irn_ops_t;
31 typedef struct _arch_irn_handler_t arch_irn_handler_t;
32 typedef struct _arch_code_generator_t arch_code_generator_t;
33 typedef struct _arch_code_generator_if_t arch_code_generator_if_t;
35 struct _be_node_factory_t;
37 typedef enum _arch_register_type_t {
38 arch_register_type_none = 0,
39 arch_register_type_write_invariant,
40 arch_register_type_caller_saved, /**< The register must be saved by the caller
41 upon a function call. It thus can be overwritten
42 in the called function. */
43 arch_register_type_callee_saved, /**< The register must be saved by the called function,
44 it thus survives a function call. */
45 arch_register_type_ignore /**< Do not consider this register when allocating. */
46 } arch_register_type_t;
49 * Convenience macro to check for register type.
50 * @param req A pointer to register.
51 * @param kind The kind of type to check for (see arch_register_type_t).
52 * @return 1, If register is of given kind, 0 if not.
54 #define arch_register_type_is(reg, kind) \
55 ((reg)->type == arch_register_type_ ## kind)
60 struct _arch_register_t {
61 const char *name; /**< The name of the register. */
62 const arch_register_class_t *reg_class; /**< The class the register belongs to. */
63 int index; /**< The index of the register in the class. */
64 arch_register_type_t type; /**< The type of the register. */
65 void *data; /**< Custom data. */
68 static INLINE const arch_register_class_t *
69 _arch_register_get_class(const arch_register_t *reg)
71 return reg->reg_class;
74 static INLINE int _arch_register_get_index(const arch_register_t *reg)
79 #define arch_register_get_class(reg) _arch_register_get_class(reg)
80 #define arch_register_get_index(reg) _arch_register_get_index(reg)
81 #define arch_register_get_name(reg) ((reg)->name)
84 * A class of registers.
85 * Like general purpose or floating point.
87 struct _arch_register_class_t {
88 const char *name; /**< The name of the register class. */
89 int n_regs; /**< Number of registers in this class. */
90 const arch_register_t *regs; /**< The array of registers. */
93 #define arch_register_class_n_regs(cls) ((cls)->n_regs)
96 * Put all registers in a class into a bitset.
97 * @param cls The class.
98 * @param bs The bitset. May be NULL.
99 * @return The number of registers in the class.
101 extern int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs);
103 static INLINE const arch_register_t *
104 _arch_register_for_index(const arch_register_class_t *cls, int idx)
106 assert(0 <= idx && idx < cls->n_regs);
107 return &cls->regs[idx];
110 #define arch_register_for_index(cls, idx) \
111 _arch_register_for_index(cls, idx)
114 * Get the register set for a register class.
115 * @param cls The register class.
116 * @return The set containing all registers in the class.
118 #define arch_get_register_set_for_class(cls) ((cls)->set)
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 int (*limited)(const ir_node *irn, int pos, 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 ir_node *other; /**< In case of "should be equal"
174 or should be different, this gives
175 the node to whose register this
176 one's should be the same/different. */
177 } arch_register_req_t;
180 * Certain node classes which are relevant for the register allocator.
182 typedef enum _arch_irn_class_t {
183 arch_irn_class_normal,
184 arch_irn_class_spill,
185 arch_irn_class_reload,
188 arch_irn_class_branch,
193 * Some flags describing a node in more detail.
195 typedef enum _arch_irn_flags_t {
196 arch_irn_flags_spillable = 1,
197 arch_irn_flags_rematerializable = 2
200 struct _arch_irn_ops_t {
203 * Get the register requirements for a given operand.
204 * @param self The self pointer.
205 * @param irn The node.
206 * @param pos The operand's position
207 * (-1 for the result of the node, 0..n for the input
209 * @return The register requirements for the selected operand.
210 * The pointer returned is never NULL.
212 const arch_register_req_t *(*get_irn_reg_req)(const arch_irn_ops_t *self,
213 arch_register_req_t *req, const ir_node *irn, int pos);
216 * Set the register for an output operand.
217 * @param irn The node.
218 * @param reg The register allocated to that operand.
219 * @note If the operand is not a register operand,
220 * the call is ignored.
222 void (*set_irn_reg)(const arch_irn_ops_t *self, ir_node *irn, const arch_register_t *reg);
225 * Get the register allocated for an output operand.
226 * @param irn The node.
227 * @return The register allocated at that operand. NULL, if
228 * the operand was no register operand or
229 * @c arch_register_invalid, if no register has yet been
230 * allocated for this node.
232 const arch_register_t *(*get_irn_reg)(const arch_irn_ops_t *self, const ir_node *irn);
236 * @param irn The node.
237 * @return A classification.
239 arch_irn_class_t (*classify)(const arch_irn_ops_t *self, const ir_node *irn);
242 * Get the flags of a node.
243 * @param self The irn ops themselves.
244 * @param irn The node.
245 * @return A set of flags.
247 arch_irn_flags_t (*get_flags)(const arch_irn_ops_t *self, const ir_node *irn);
252 * Get the register requirements for a node.
253 * @param env The architecture environment.
254 * @param req A pointer to a requirements structure, where the data can
256 * @param irn The node.
257 * @param pos The position of the operand you're interested in.
258 * @return A pointer to the register requirements which may <b>not</b>
259 * neccessarily be equal to @p req. If NULL is returned, the
260 * operand was no register operand.
262 extern const arch_register_req_t *
263 arch_get_register_req(const arch_env_t *env, arch_register_req_t *req,
264 const ir_node *irn, int pos);
267 * Check if an operand is a register operand.
268 * @param env The environment.
269 * @param irn The node.
270 * @param pos The position of the operand.
271 * @return 1, if the operand is significant for register allocation, 0
274 extern int arch_is_register_operand(const arch_env_t *env,
275 const ir_node *irn, int pos);
278 * Get the number of allocatable registers concerning
279 * a register class for an operand of a node.
280 * @param env The environment.
281 * @param irn The node.
282 * @param pos The postition of the node's operand.
283 * @param cls The register class.
284 * @param bs The bitset all allocatable registers shall be put into.
285 * Note, that you can also pass NULL here. If you don't,
286 * make sure, the bitset is as large as the register class
288 * @return The amount of registers allocatable for that operand.
290 extern int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn,
291 int pos, const arch_register_class_t *cls, bitset_t *bs);
294 * Check, if a register is assignable to an operand of a node.
295 * @param env The architecture environment.
296 * @param irn The node.
297 * @param pos The position of the operand.
298 * @param reg The register.
299 * @return 1, if the register might be allocated to the operand 0 if not.
301 extern int arch_reg_is_allocatable(const arch_env_t *env,
302 const ir_node *irn, int pos, const arch_register_t *reg);
305 * Get the register class of an operand of a node.
306 * @param env The architecture environment.
307 * @param irn The node.
308 * @param pos The position of the operand.
309 * @return The register class of the operand or NULL, if
310 * operand is a non-register operand.
312 extern const arch_register_class_t *
313 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos);
316 * Get the register allocated at a certain output operand of a node.
317 * @param env The arch environment.
318 * @param irn The node.
319 * @return The register allocated for this operand
321 extern const arch_register_t *
322 arch_get_irn_register(const arch_env_t *env, const ir_node *irn);
325 * Set the register for a certain output operand.
326 * @param env The architecture environment.
327 * @param irn The node.
328 * @param idx The index of the output operand.
329 * @param reg The register.
331 extern void arch_set_irn_register(const arch_env_t *env, ir_node *irn,
332 const arch_register_t *reg);
336 * @param env The architecture environment.
337 * @param irn The node.
338 * @return A classification of the node.
340 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn);
343 * Get the flags of a node.
344 * @param env The architecture environment.
345 * @param irn The node.
348 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn);
350 #define arch_irn_has_reg_class(env, irn, pos, cls) \
351 ((cls) == arch_get_irn_reg_class(env, irn, pos))
354 * Somebody who can be asked about nodes.
356 struct _arch_irn_handler_t {
359 * Get the operations of an irn.
360 * @param self The handler from which the method is invoked.
361 * @param irn Some node.
362 * @return Operations for that irn.
364 const arch_irn_ops_t *(*get_irn_ops)(const arch_irn_handler_t *handler,
370 * The code generator.
372 struct _arch_code_generator_if_t {
376 * Initialize the code generator.
377 * @param file The file to dump to.
378 * @param irg The function to generate code for.
379 * @param env The architecture environment.
380 * @return A newly created code generator.
382 void *(*init)(FILE *file, ir_graph *irg, const arch_env_t *env);
385 * Called, when the graph is being normalized.
387 void (*prepare_graph)(void *self);
390 * Called before scheduling.
392 void (*before_sched)(void *self);
395 * Called before register allocation.
397 void (*before_ra)(void *self);
400 * Called after everything happened.
401 * The code generator must also be de-allocated here.
403 void (*done)(void *self);
407 #define _arch_cg_call(cg, func) \
409 if((cg)->impl->func) \
410 (cg)->impl->func(cg); \
413 #define arch_code_generator_prepare_graph(cg) _arch_cg_call(cg, prepare_graph)
414 #define arch_code_generator_before_sched(cg) _arch_cg_call(cg, before_sched)
415 #define arch_code_generator_before_ra(cg) _arch_cg_call(cg, before_ra)
416 #define arch_code_generator_done(cg) _arch_cg_call(cg, done)
419 * Code generator base class.
421 struct _arch_code_generator_t {
422 const arch_code_generator_if_t *impl;
429 const arch_isa_if_t *impl;
433 * Architecture interface.
435 struct _arch_isa_if_t {
438 void (*register_options)(lc_opt_entry_t *grp);
442 * Initialize the isa interface.
447 * Free the isa instance.
449 void (*done)(void *self);
452 * Get the the number of register classes in the isa.
453 * @return The number of register classes.
455 int (*get_n_reg_class)(const void *self);
458 * Get the i-th register class.
459 * @param i The number of the register class.
460 * @return The register class.
462 const arch_register_class_t *(*get_reg_class)(const void *self, int i);
465 * The irn handler for this architecture.
466 * The irn handler is registered by the Firm back end
467 * when the architecture is initialized.
470 const arch_irn_handler_t *(*get_irn_handler)(const void *self);
473 * Get the code generator interface.
474 * @param self The this pointer.
475 * @return Some code generator interface.
477 const arch_code_generator_if_t *(*get_code_generator)(void *self);
480 * Get the list scheduler to use.
481 * @param self The isa object.
482 * @return The list scheduler selector.
484 const list_sched_selector_t *(*get_list_sched_selector)(const void *self);
487 * Get the proj number assigned to the register.
488 * @param self The isa object.
489 * @param reg The register
490 * @return The proj number assigned to this register
492 long (*get_projnum_for_register)(const void *self, const arch_register_t *reg);
495 #define arch_isa_get_n_reg_class(isa) ((isa)->impl->get_n_reg_class(isa))
496 #define arch_isa_get_reg_class(isa,i) ((isa)->impl->get_reg_class(isa, i))
497 #define arch_isa_get_irn_handler(isa) ((isa)->impl->get_irn_handler(isa))
498 #define arch_isa_make_code_generator(isa,irg) ((isa)->impl->make_code_generator(isa, irg))
500 #define ARCH_MAX_HANDLERS 8
503 * Environment for the architecture infrastructure.
504 * Keep this everywhere you're going.
507 const struct _be_node_factory_t *node_factory; /**< The node factory for be nodes. */
508 arch_isa_t *isa; /**< The isa about which everything is. */
510 arch_irn_handler_t const *handlers[ARCH_MAX_HANDLERS]; /**< The handlers are organized as
513 int handlers_tos; /**< The stack pointer of the handler
518 * Get the isa of an arch environment.
519 * @param env The environment.
520 * @return The isa with which the env was initialized with.
522 #define arch_env_get_isa(env) ((env)->isa)
525 * Initialize the architecture environment struct.
526 * @param isa The isa which shall be put into the environment.
527 * @return The environment.
529 extern arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa);
532 * Add a node handler to the environment.
533 * @param env The environment.
534 * @param handler A node handler.
535 * @return The environment itself.
537 extern arch_env_t *arch_env_add_irn_handler(arch_env_t *env,
538 const arch_irn_handler_t *handler);
540 #endif /* _FIRM_BEARCH_H */