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_if_t arch_irn_ops_if_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;
50 * Convenience macro to check for register type.
51 * @param req A pointer to register.
52 * @param kind The kind of type to check for (see arch_register_type_t).
53 * @return 1, If register is of given kind, 0 if not.
55 #define arch_register_type_is(reg, kind) \
56 ((reg)->type == arch_register_type_ ## kind)
61 struct _arch_register_t {
62 const char *name; /**< The name of the register. */
63 const arch_register_class_t *reg_class; /**< The class the register belongs to. */
64 int index; /**< The index of the register in the class. */
65 arch_register_type_t type; /**< The type of the register. */
66 void *data; /**< Custom data. */
69 static INLINE const arch_register_class_t *
70 _arch_register_get_class(const arch_register_t *reg)
72 return reg->reg_class;
75 static INLINE int _arch_register_get_index(const arch_register_t *reg)
80 #define arch_register_get_class(reg) _arch_register_get_class(reg)
81 #define arch_register_get_index(reg) _arch_register_get_index(reg)
82 #define arch_register_get_name(reg) ((reg)->name)
85 * A class of registers.
86 * Like general purpose or floating point.
88 struct _arch_register_class_t {
89 const char *name; /**< The name of the register class. */
90 int n_regs; /**< Number of registers in this class. */
91 const arch_register_t *regs; /**< The array of registers. */
94 #define arch_register_class_n_regs(cls) ((cls)->n_regs)
97 * Put all registers in a class into a bitset.
98 * @param cls The class.
99 * @param bs The bitset. May be NULL.
100 * @return The number of registers in the class.
102 extern int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs);
104 static INLINE const arch_register_t *
105 _arch_register_for_index(const arch_register_class_t *cls, int idx)
107 assert(0 <= idx && idx < cls->n_regs);
108 return &cls->regs[idx];
111 #define arch_register_for_index(cls, idx) \
112 _arch_register_for_index(cls, idx)
115 * Get the register set for a register class.
116 * @param cls The register class.
117 * @return The set containing all registers in the class.
119 #define arch_get_register_set_for_class(cls) ((cls)->set)
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 int (*limited)(const ir_node *irn, int pos, 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 ir_node *other; /**< In case of "should be equal"
175 or should be different, this gives
176 the node to whose register this
177 one's should be the same/different. */
178 } arch_register_req_t;
181 * Certain node classes which are relevant for the register allocator.
183 typedef enum _arch_irn_class_t {
184 arch_irn_class_normal,
185 arch_irn_class_spill,
186 arch_irn_class_reload,
189 arch_irn_class_branch,
194 * Some flags describing a node in more detail.
196 typedef enum _arch_irn_flags_t {
197 arch_irn_flags_spillable = 1,
198 arch_irn_flags_rematerializable = 2
201 struct _arch_irn_ops_if_t {
204 * Get the register requirements for a given operand.
205 * @param self The self pointer.
206 * @param irn The node.
207 * @param pos The operand's position
208 * (-1 for the result of the node, 0..n for the input
210 * @return The register requirements for the selected operand.
211 * The pointer returned is never NULL.
213 const arch_register_req_t *(*get_irn_reg_req)(const void *self,
214 arch_register_req_t *req, const ir_node *irn, int pos);
217 * Set the register for an output operand.
218 * @param irn The node.
219 * @param reg The register allocated to that operand.
220 * @note If the operand is not a register operand,
221 * the call is ignored.
223 void (*set_irn_reg)(const void *self, ir_node *irn, const arch_register_t *reg);
226 * Get the register allocated for an output operand.
227 * @param irn The node.
228 * @return The register allocated at that operand. NULL, if
229 * the operand was no register operand or
230 * @c arch_register_invalid, if no register has yet been
231 * allocated for this node.
233 const arch_register_t *(*get_irn_reg)(const void *self, const ir_node *irn);
237 * @param irn The node.
238 * @return A classification.
240 arch_irn_class_t (*classify)(const void *self, const ir_node *irn);
243 * Get the flags of a node.
244 * @param self The irn ops themselves.
245 * @param irn The node.
246 * @return A set of flags.
248 arch_irn_flags_t (*get_flags)(const void *self, const ir_node *irn);
253 * irn_ops base class.
255 struct _arch_irn_ops_t {
256 const arch_irn_ops_if_t *impl;
260 * Get the register requirements for a node.
261 * @param env The architecture environment.
262 * @param req A pointer to a requirements structure, where the data can
264 * @param irn The node.
265 * @param pos The position of the operand you're interested in.
266 * @return A pointer to the register requirements which may <b>not</b>
267 * neccessarily be equal to @p req. If NULL is returned, the
268 * operand was no register operand.
270 extern const arch_register_req_t *
271 arch_get_register_req(const arch_env_t *env, arch_register_req_t *req,
272 const ir_node *irn, int pos);
275 * Check if an operand is a register operand.
276 * @param env The environment.
277 * @param irn The node.
278 * @param pos The position of the operand.
279 * @return 1, if the operand is significant for register allocation, 0
282 extern int arch_is_register_operand(const arch_env_t *env,
283 const ir_node *irn, int pos);
286 * Get the number of allocatable registers concerning
287 * a register class for an operand of a node.
288 * @param env The environment.
289 * @param irn The node.
290 * @param pos The postition of the node's operand.
291 * @param cls The register class.
292 * @param bs The bitset all allocatable registers shall be put into.
293 * Note, that you can also pass NULL here. If you don't,
294 * make sure, the bitset is as large as the register class
296 * @return The amount of registers allocatable for that operand.
298 extern int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn,
299 int pos, const arch_register_class_t *cls, bitset_t *bs);
302 * Check, if a register is assignable to an operand of a node.
303 * @param env The architecture environment.
304 * @param irn The node.
305 * @param pos The position of the operand.
306 * @param reg The register.
307 * @return 1, if the register might be allocated to the operand 0 if not.
309 extern int arch_reg_is_allocatable(const arch_env_t *env,
310 const ir_node *irn, int pos, const arch_register_t *reg);
313 * Get the register class of an operand of a node.
314 * @param env The architecture environment.
315 * @param irn The node.
316 * @param pos The position of the operand.
317 * @return The register class of the operand or NULL, if
318 * operand is a non-register operand.
320 extern const arch_register_class_t *
321 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos);
324 * Get the register allocated at a certain output operand of a node.
325 * @param env The arch environment.
326 * @param irn The node.
327 * @return The register allocated for this operand
329 extern const arch_register_t *
330 arch_get_irn_register(const arch_env_t *env, const ir_node *irn);
333 * Set the register for a certain output operand.
334 * @param env The architecture environment.
335 * @param irn The node.
336 * @param idx The index of the output operand.
337 * @param reg The register.
339 extern void arch_set_irn_register(const arch_env_t *env, ir_node *irn,
340 const arch_register_t *reg);
344 * @param env The architecture environment.
345 * @param irn The node.
346 * @return A classification of the node.
348 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn);
351 * Get the flags of a node.
352 * @param env The architecture environment.
353 * @param irn The node.
356 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn);
358 #define arch_irn_has_reg_class(env, irn, pos, cls) \
359 ((cls) == arch_get_irn_reg_class(env, irn, pos))
362 * Somebody who can be asked about nodes.
364 struct _arch_irn_handler_t {
367 * Get the operations of an irn.
368 * @param self The handler from which the method is invoked.
369 * @param irn Some node.
370 * @return Operations for that irn.
372 const void *(*get_irn_ops)(const arch_irn_handler_t *handler,
378 * The code generator.
380 struct _arch_code_generator_if_t {
384 * Initialize the code generator.
385 * @param file The file to dump to.
386 * @param irg The function to generate code for.
387 * @param env The architecture environment.
388 * @return A newly created code generator.
390 void *(*init)(FILE *file, ir_graph *irg, const arch_env_t *env);
393 * Called, when the graph is being normalized.
395 void (*prepare_graph)(void *self);
398 * Called before scheduling.
400 void (*before_sched)(void *self);
403 * Called before register allocation.
405 void (*before_ra)(void *self);
408 * Called after register allocation to lower Spills to Stores
410 ir_node *(*lower_spill)(void *self, ir_node *spill);
413 * Called after register allocation to lower Reloads to Loads
415 ir_node *(*lower_reload)(void *self, ir_node *reload);
418 * Called after everything happened.
419 * The code generator must also be de-allocated here.
421 void (*done)(void *self);
425 #define _arch_cg_call(cg, func) \
427 if((cg)->impl->func) \
428 (cg)->impl->func(cg); \
431 #define arch_code_generator_prepare_graph(cg) _arch_cg_call(cg, prepare_graph)
432 #define arch_code_generator_before_sched(cg) _arch_cg_call(cg, before_sched)
433 #define arch_code_generator_before_ra(cg) _arch_cg_call(cg, before_ra)
434 #define arch_code_generator_done(cg) _arch_cg_call(cg, done)
437 * Code generator base class.
439 struct _arch_code_generator_t {
440 const arch_code_generator_if_t *impl;
447 const arch_isa_if_t *impl;
451 * Architecture interface.
453 struct _arch_isa_if_t {
456 void (*register_options)(lc_opt_entry_t *grp);
460 * Initialize the isa interface.
465 * Free the isa instance.
467 void (*done)(void *self);
470 * Get the the number of register classes in the isa.
471 * @return The number of register classes.
473 int (*get_n_reg_class)(const void *self);
476 * Get the i-th register class.
477 * @param i The number of the register class.
478 * @return The register class.
480 const arch_register_class_t *(*get_reg_class)(const void *self, int i);
483 * The irn handler for this architecture.
484 * The irn handler is registered by the Firm back end
485 * when the architecture is initialized.
488 const arch_irn_handler_t *(*get_irn_handler)(const void *self);
491 * Get the code generator interface.
492 * @param self The this pointer.
493 * @return Some code generator interface.
495 const arch_code_generator_if_t *(*get_code_generator_if)(void *self);
498 * Get the list scheduler to use.
499 * @param self The isa object.
500 * @return The list scheduler selector.
502 const list_sched_selector_t *(*get_list_sched_selector)(const void *self);
505 * Get the proj number assigned to the register.
506 * @param self The isa object.
507 * @param reg The register
508 * @return The proj number assigned to this register
510 long (*get_projnum_for_register)(const void *self, const arch_register_t *reg);
513 #define arch_isa_get_n_reg_class(isa) ((isa)->impl->get_n_reg_class(isa))
514 #define arch_isa_get_reg_class(isa,i) ((isa)->impl->get_reg_class(isa, i))
515 #define arch_isa_get_irn_handler(isa) ((isa)->impl->get_irn_handler(isa))
516 #define arch_isa_make_code_generator(isa,irg) ((isa)->impl->make_code_generator(isa, irg))
518 #define ARCH_MAX_HANDLERS 8
521 * Environment for the architecture infrastructure.
522 * Keep this everywhere you're going.
525 const struct _be_node_factory_t *node_factory; /**< The node factory for be nodes. */
526 arch_isa_t *isa; /**< The isa about which everything is. */
528 arch_irn_handler_t const *handlers[ARCH_MAX_HANDLERS]; /**< The handlers are organized as
531 int handlers_tos; /**< The stack pointer of the handler
536 * Get the isa of an arch environment.
537 * @param env The environment.
538 * @return The isa with which the env was initialized with.
540 #define arch_env_get_isa(env) ((env)->isa)
543 * Initialize the architecture environment struct.
544 * @param isa The isa which shall be put into the environment.
545 * @return The environment.
547 extern arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa);
550 * Add a node handler to the environment.
551 * @param env The environment.
552 * @param handler A node handler.
553 * @return The environment itself.
555 extern arch_env_t *arch_env_add_irn_handler(arch_env_t *env,
556 const arch_irn_handler_t *handler);
558 #endif /* _FIRM_BEARCH_H */