4 #include "firm_config.h"
7 #include <libcore/lc_opts.h>
20 #include "belistsched.h"
22 typedef struct _arch_register_class_t arch_register_class_t;
23 typedef struct _arch_register_t arch_register_t;
24 typedef struct _arch_isa_if_t arch_isa_if_t;
25 typedef struct _arch_isa_t arch_isa_t;
26 typedef struct _arch_env_t arch_env_t;
27 typedef struct _arch_irn_ops_t arch_irn_ops_t;
28 typedef struct _arch_irn_handler_t arch_irn_handler_t;
29 typedef struct _arch_code_generator_t arch_code_generator_t;
30 typedef struct _arch_code_generator_if_t arch_code_generator_if_t;
32 struct _be_node_factory_t;
34 typedef enum _arch_register_type_t {
35 arch_register_type_none = 0,
36 arch_register_type_write_invariant,
37 arch_register_type_caller_saved, /**< The register must be saved by the caller
38 upon a function call. It thus can be overwritten
39 in the called function. */
40 arch_register_type_callee_saved, /**< The register must be saved by the called function,
41 it thus survives a function call. */
42 arch_register_type_ignore /**< Do not consider this register when allocating. */
43 } arch_register_type_t;
48 struct _arch_register_t {
49 const char *name; /**< The name of the register. */
50 const arch_register_class_t *reg_class; /**< The class the register belongs to. */
51 int index; /**< The index of the register in the class. */
52 arch_register_type_t type; /**< The type of the register. */
53 void *data; /**< Custom data. */
56 static INLINE const arch_register_class_t *
57 _arch_register_get_class(const arch_register_t *reg)
59 return reg->reg_class;
62 static INLINE int _arch_register_get_index(const arch_register_t *reg)
67 #define arch_register_get_class(reg) _arch_register_get_class(reg)
68 #define arch_register_get_index(reg) _arch_register_get_index(reg)
69 #define arch_register_get_name(reg) ((reg)->name)
72 * A class of registers.
73 * Like general purpose or floating point.
75 struct _arch_register_class_t {
76 const char *name; /**< The name of the register class. */
77 int n_regs; /**< Number of registers in this class. */
78 const arch_register_t *regs; /**< The array of registers. */
81 #define arch_register_class_n_regs(cls) ((cls)->n_regs)
84 * Put all registers in a class into a bitset.
85 * @param cls The class.
86 * @param bs The bitset. May be NULL.
87 * @return The number of registers in the class.
89 extern int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs);
91 static INLINE const arch_register_t *
92 _arch_register_for_index(const arch_register_class_t *cls, int idx)
94 assert(0 <= idx && idx < cls->n_regs);
95 return &cls->regs[idx];
98 #define arch_register_for_index(cls, idx) \
99 _arch_register_for_index(cls, idx)
102 * Get the register set for a register class.
103 * @param cls The register class.
104 * @return The set containing all registers in the class.
106 #define arch_get_register_set_for_class(cls) ((cls)->set)
108 typedef enum _arch_operand_type_t {
109 arch_operand_type_invalid,
110 arch_operand_type_memory,
111 arch_operand_type_register,
112 arch_operand_type_immediate,
113 arch_operand_type_symconst,
114 arch_operand_type_last
115 } arch_operand_type_t;
118 * Different types of register allocation requirements.
120 typedef enum _arch_register_req_type_t {
121 arch_register_req_type_none = 0, /**< No register requirement. */
123 arch_register_req_type_normal = 1, /**< All registers in the class
126 arch_register_req_type_limited = 2, /**< Only a real subset of
127 the class is allowed. */
129 arch_register_req_type_should_be_same = 4, /**< The register should be equal
130 another one at the node. */
132 arch_register_req_type_should_be_different = 8, /**< The register must be unequal
133 to some other at the node. */
135 } arch_register_req_type_t;
138 * Convenience macro to check for set constraints.
139 * @param req A pointer to register requirements.
140 * @param kind The kind of constraint to check for (see arch_register_req_type_t).
141 * @return 1, If the kind of constraint is present, 0 if not.
143 #define arch_register_req_is(req, kind) \
144 (((req)->type & (arch_register_req_type_ ## kind)) != 0)
147 * Expresses requirements to register allocation for an operand.
149 typedef struct _arch_register_req_t {
150 arch_register_req_type_t type; /**< The type of the constraint. */
151 const arch_register_class_t *cls; /**< The register class this constraint belongs to. */
153 int (*limited)(const ir_node *irn, int pos, bitset_t *bs);
154 /**< In case of the 'limited'
155 constraint, this function
156 must put all allowable
157 registers in the bitset and
158 return the number of registers
161 ir_node *other; /**< In case of "should be equal"
162 or should be different, this gives
163 the node to whose register this
164 one's should be the same/different. */
165 } arch_register_req_t;
168 * Certain node classes which are relevant for the register allocator.
170 typedef enum _arch_irn_class_t {
171 arch_irn_class_normal,
172 arch_irn_class_spill,
173 arch_irn_class_reload,
176 arch_irn_class_branch
180 * Some flags describing a node in more detail.
182 typedef enum _arch_irn_flags_t {
183 arch_irn_flags_spillable = 1,
184 arch_irn_flags_rematerializable = 2
187 struct _arch_irn_ops_t {
190 * Get the register requirements for a given operand.
191 * @param self The self pointer.
192 * @param irn The node.
193 * @param pos The operand's position
194 * (-1 for the result of the node, 0..n for the input
196 * @return The register requirements for the selected operand.
197 * The pointer returned is never NULL.
199 const arch_register_req_t *(*get_irn_reg_req)(const arch_irn_ops_t *self,
200 arch_register_req_t *req, const ir_node *irn, int pos);
203 * Set the register for an output operand.
204 * @param irn The node.
205 * @param reg The register allocated to that operand.
206 * @note If the operand is not a register operand,
207 * the call is ignored.
209 void (*set_irn_reg)(const arch_irn_ops_t *self, ir_node *irn, const arch_register_t *reg);
212 * Get the register allocated for an output operand.
213 * @param irn The node.
214 * @return The register allocated at that operand. NULL, if
215 * the operand was no register operand or
216 * @c arch_register_invalid, if no register has yet been
217 * allocated for this node.
219 const arch_register_t *(*get_irn_reg)(const arch_irn_ops_t *self, const ir_node *irn);
223 * @param irn The node.
224 * @return A classification.
226 arch_irn_class_t (*classify)(const arch_irn_ops_t *self, const ir_node *irn);
229 * Get the flags of a node.
230 * @param self The irn ops themselves.
231 * @param irn The node.
232 * @return A set of flags.
234 arch_irn_flags_t (*get_flags)(const arch_irn_ops_t *self, const ir_node *irn);
239 * Get the register requirements for a node.
240 * @param env The architecture environment.
241 * @param req A pointer to a requirements structure, where the data can
243 * @param irn The node.
244 * @param pos The position of the operand you're interested in.
245 * @return A pointer to the register requirements which may <b>not</b>
246 * neccessarily be equal to @p req. If NULL is returned, the
247 * operand was no register operand.
249 extern const arch_register_req_t *
250 arch_get_register_req(const arch_env_t *env, arch_register_req_t *req,
251 const ir_node *irn, int pos);
254 * Check if an operand is a register operand.
255 * @param env The environment.
256 * @param irn The node.
257 * @param pos The position of the operand.
258 * @return 1, if the operand is significant for register allocation, 0
261 extern int arch_is_register_operand(const arch_env_t *env,
262 const ir_node *irn, int pos);
265 * Get the number of allocatable registers concerning
266 * a register class for an operand of a node.
267 * @param env The environment.
268 * @param irn The node.
269 * @param pos The postition of the node's operand.
270 * @param cls The register class.
271 * @param bs The bitset all allocatable registers shall be put into.
272 * Note, that you can also pass NULL here. If you don't,
273 * make sure, the bitset is as large as the register class
275 * @return The amount of registers allocatable for that operand.
277 extern int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn,
278 int pos, const arch_register_class_t *cls, bitset_t *bs);
281 * Check, if a register is assignable to an operand of a node.
282 * @param env The architecture environment.
283 * @param irn The node.
284 * @param pos The position of the operand.
285 * @param reg The register.
286 * @return 1, if the register might be allocated to the operand 0 if not.
288 extern int arch_reg_is_allocatable(const arch_env_t *env,
289 const ir_node *irn, int pos, const arch_register_t *reg);
292 * Get the register class of an operand of a node.
293 * @param env The architecture environment.
294 * @param irn The node.
295 * @param pos The position of the operand.
296 * @return The register class of the operand or NULL, if
297 * operand is a non-register operand.
299 extern const arch_register_class_t *
300 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos);
303 * Get the register allocated at a certain output operand of a node.
304 * @param env The arch environment.
305 * @param irn The node.
306 * @return The register allocated for this operand
308 extern const arch_register_t *
309 arch_get_irn_register(const arch_env_t *env, const ir_node *irn);
312 * Set the register for a certain output operand.
313 * @param env The architecture environment.
314 * @param irn The node.
315 * @param idx The index of the output operand.
316 * @param reg The register.
318 extern void arch_set_irn_register(const arch_env_t *env, ir_node *irn,
319 const arch_register_t *reg);
323 * @param env The architecture environment.
324 * @param irn The node.
325 * @return A classification of the node.
327 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn);
330 * Get the flags of a node.
331 * @param env The architecture environment.
332 * @param irn The node.
335 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn);
337 #define arch_irn_has_reg_class(env, irn, pos, cls) \
338 ((cls) == arch_get_irn_reg_class(env, irn, pos))
341 * Somebody who can be asked about nodes.
343 struct _arch_irn_handler_t {
346 * Get the operations of an irn.
347 * @param self The handler from which the method is invoked.
348 * @param irn Some node.
349 * @return Operations for that irn.
351 const arch_irn_ops_t *(*get_irn_ops)(const arch_irn_handler_t *handler,
357 * The code generator.
359 struct _arch_code_generator_if_t {
363 * Initialize the code generator.
364 * @param file The file to dump to.
365 * @param irg The function to generate code for.
366 * @param env The architecture environment.
367 * @return A newly created code generator.
369 void *(*init)(FILE *file, ir_graph *irg, const arch_env_t *env);
372 * Called, when the graph is being normalized.
374 void (*prepare_graph)(void *self);
377 * Called before scheduling.
379 void (*before_sched)(void *self);
382 * Called before register allocation.
384 void (*before_ra)(void *self);
387 * Called after everything happened.
388 * The code generator must also be de-allocated here.
390 void (*done)(void *self);
394 #define _arch_cg_call(cg, func) \
396 if((cg)->impl->func) \
397 (cg)->impl->func(cg); \
400 #define arch_code_generator_prepare_graph(cg) _arch_cg_call(cg, prepare_graph)
401 #define arch_code_generator_before_sched(cg) _arch_cg_call(cg, before_sched)
402 #define arch_code_generator_before_ra(cg) _arch_cg_call(cg, before_ra)
403 #define arch_code_generator_done(cg) _arch_cg_call(cg, done)
406 * Code generator base class.
408 struct _arch_code_generator_t {
409 const arch_code_generator_if_t *impl;
416 const arch_isa_if_t *impl;
420 * Architecture interface.
422 struct _arch_isa_if_t {
425 void (*register_options)(lc_opt_entry_t *grp);
429 * Initialize the isa interface.
434 * Free the isa instance.
436 void (*done)(void *self);
439 * Get the the number of register classes in the isa.
440 * @return The number of register classes.
442 int (*get_n_reg_class)(const void *self);
445 * Get the i-th register class.
446 * @param i The number of the register class.
447 * @return The register class.
449 const arch_register_class_t *(*get_reg_class)(const void *self, int i);
452 * The irn handler for this architecture.
453 * The irn handler is registered by the Firm back end
454 * when the architecture is initialized.
457 const arch_irn_handler_t *(*get_irn_handler)(const void *self);
460 * Get the code generator interface.
461 * @param self The this pointer.
462 * @return Some code generator interface.
464 const arch_code_generator_if_t *(*get_code_generator)(void *self);
467 * Get the list scheduler to use.
468 * @param self The isa object.
469 * @return The list scheduler selector.
471 const list_sched_selector_t *(*get_list_sched_selector)(const void *self);
474 #define arch_isa_get_n_reg_class(isa) ((isa)->impl->get_n_reg_class(isa))
475 #define arch_isa_get_reg_class(isa,i) ((isa)->impl->get_reg_class(isa, i))
476 #define arch_isa_get_irn_handler(isa) ((isa)->impl->get_irn_handler(isa))
477 #define arch_isa_make_code_generator(isa,irg) ((isa)->impl->make_code_generator(isa, irg))
479 #define ARCH_MAX_HANDLERS 8
482 * Environment for the architecture infrastructure.
483 * Keep this everywhere you're going.
486 const struct _be_node_factory_t *node_factory; /**< The node factory for be nodes. */
487 arch_isa_t *isa; /**< The isa about which everything is. */
489 arch_irn_handler_t const *handlers[ARCH_MAX_HANDLERS]; /**< The handlers are organized as
492 int handlers_tos; /**< The stack pointer of the handler
497 * Get the isa of an arch environment.
498 * @param env The environment.
499 * @return The isa with which the env was initialized with.
501 #define arch_env_get_isa(env) ((env)->isa)
504 * Initialize the architecture environment struct.
505 * @param isa The isa which shall be put into the environment.
506 * @return The environment.
508 extern arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa);
511 * Add a node handler to the environment.
512 * @param env The environment.
513 * @param handler A node handler.
514 * @return The environment itself.
516 extern arch_env_t *arch_env_add_irn_handler(arch_env_t *env,
517 const arch_irn_handler_t *handler);
519 #endif /* _FIRM_BEARCH_H */