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_enum_t arch_enum_t;
25 typedef struct _arch_enum_member_t arch_enum_member_t;
26 typedef struct _arch_isa_if_t arch_isa_if_t;
27 typedef struct _arch_isa_t arch_isa_t;
28 typedef struct _arch_env_t arch_env_t;
29 typedef struct _arch_irn_ops_t arch_irn_ops_t;
30 typedef struct _arch_irn_handler_t arch_irn_handler_t;
31 typedef struct _arch_code_generator_t arch_code_generator_t;
32 typedef struct _arch_code_generator_if_t arch_code_generator_if_t;
34 struct _be_node_factory_t;
36 typedef enum _arch_register_type_t {
37 arch_register_type_none = 0,
38 arch_register_type_write_invariant,
39 arch_register_type_caller_saved, /**< The register must be saved by the caller
40 upon a function call. It thus can be overwritten
41 in the called function. */
42 arch_register_type_callee_saved, /**< The register must be saved by the called function,
43 it thus survives a function call. */
44 arch_register_type_ignore /**< Do not consider this register when allocating. */
45 } arch_register_type_t;
50 struct _arch_register_t {
51 const char *name; /**< The name of the register. */
52 const arch_register_class_t *reg_class; /**< The class the register belongs to. */
53 int index; /**< The index of the register in the class. */
54 arch_register_type_t type; /**< The type of the register. */
55 void *data; /**< Custom data. */
58 static INLINE const arch_register_class_t *
59 _arch_register_get_class(const arch_register_t *reg)
61 return reg->reg_class;
64 static INLINE int _arch_register_get_index(const arch_register_t *reg)
69 #define arch_register_get_class(reg) _arch_register_get_class(reg)
70 #define arch_register_get_index(reg) _arch_register_get_index(reg)
71 #define arch_register_get_name(reg) ((reg)->name)
74 * A class of registers.
75 * Like general purpose or floating point.
77 struct _arch_register_class_t {
78 const char *name; /**< The name of the register class. */
79 int n_regs; /**< Number of registers in this class. */
80 const arch_register_t *regs; /**< The array of registers. */
83 #define arch_register_class_n_regs(cls) ((cls)->n_regs)
86 * Put all registers in a class into a bitset.
87 * @param cls The class.
88 * @param bs The bitset. May be NULL.
89 * @return The number of registers in the class.
91 extern int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs);
93 static INLINE const arch_register_t *
94 _arch_register_for_index(const arch_register_class_t *cls, int idx)
96 assert(0 <= idx && idx < cls->n_regs);
97 return &cls->regs[idx];
100 #define arch_register_for_index(cls, idx) \
101 _arch_register_for_index(cls, idx)
104 * Get the register set for a register class.
105 * @param cls The register class.
106 * @return The set containing all registers in the class.
108 #define arch_get_register_set_for_class(cls) ((cls)->set)
113 struct _arch_immediate_t {
114 const char *name; /**< The name of the immediate. */
115 ir_mode *mode; /**< The mode of the immediate. */
119 * The member of an enum.
121 struct _arch_enum_member_t {
122 arch_enum_t *enm; /**< The enum, this member belongs to. */
126 * An enumeration operand type.
128 * Enumeration operand types can be used to describe the variants
129 * of an instruction, like giving the cases for a compare (gt, lt,
130 * eq, ...) some other special attributes of an instruction.
132 struct _arch_enum_t {
133 int n_members; /**< The number of members in this enum. */
134 arch_enum_member_t *members[1]; /**< The array of members. */
137 typedef enum _arch_operand_type_t {
138 arch_operand_type_invalid,
139 arch_operand_type_memory,
140 arch_operand_type_register,
141 arch_operand_type_immediate,
142 arch_operand_type_symconst,
143 arch_operand_type_last
144 } arch_operand_type_t;
147 * Different types of register allocation requirements.
149 typedef enum _arch_register_req_type_t {
150 arch_register_req_type_none = 0, /**< No register requirement. */
152 arch_register_req_type_normal = 1, /**< All registers in the class
155 arch_register_req_type_limited = 2, /**< Only a real subset of
156 the class is allowed. */
158 arch_register_req_type_should_be_same = 4, /**< The register should be equal
159 another one at the node. */
161 arch_register_req_type_should_be_different = 8, /**< The register must be unequal
162 to some other at the node. */
164 } arch_register_req_type_t;
166 #define arch_register_req_is_constr(x) \
167 ((x)->type & (arch_register_req_type_pair + arch_register_req_type_limited - 1) != 0)
170 * Expresses requirements to register allocation for an operand.
172 typedef struct _arch_register_req_t {
173 arch_register_req_type_t type; /**< The type of the constraint. */
174 const arch_register_class_t *cls; /**< The register class this
175 constraint belongs to. */
177 int (*limited)(const ir_node *irn, int pos, bitset_t *bs);
178 /**< In case of the 'limited'
179 constraint, this function
180 must put all allowable
181 registers in the bitset and
182 return the number of registers
185 int pos; /**< In case of the equal constraint,
186 this gives the position of the
187 operand to which the register of
188 this should be equal to. Same for
191 } arch_register_req_t;
194 * Certain node classes which are relevent for the register allocator.
196 typedef enum _arch_irn_class_t {
197 arch_irn_class_normal,
198 arch_irn_class_spill,
199 arch_irn_class_reload,
202 arch_irn_class_branch
206 * Some flags describing a node in more detail.
208 typedef enum _arch_irn_flags_t {
209 arch_irn_flags_spillable = 1,
210 arch_irn_flags_rematerializable = 2
213 struct _arch_irn_ops_t {
216 * Get the register requirements for a given operand.
217 * @param self The self pointer.
218 * @param irn The node.
219 * @param pos The operand's position
220 * (-1 for the result of the node, 0..n for the input
222 * @return The register requirements for the selected operand.
223 * The pointer returned is never NULL.
225 const arch_register_req_t *(*get_irn_reg_req)(const arch_irn_ops_t *self,
226 arch_register_req_t *req, const ir_node *irn, int pos);
229 * Set the register for an output operand.
230 * @param irn The node.
231 * @param reg The register allocated to that operand.
232 * @note If the operand is not a register operand,
233 * the call is ignored.
235 void (*set_irn_reg)(const arch_irn_ops_t *self, ir_node *irn, const arch_register_t *reg);
238 * Get the register allocated for an output operand.
239 * @param irn The node.
240 * @return The register allocated at that operand. NULL, if
241 * the operand was no register operand or
242 * @c arch_register_invalid, if no register has yet been
243 * allocated for this node.
245 const arch_register_t *(*get_irn_reg)(const arch_irn_ops_t *self, const ir_node *irn);
249 * @param irn The node.
250 * @return A classification.
252 arch_irn_class_t (*classify)(const arch_irn_ops_t *self, const ir_node *irn);
255 * Get the flags of a node.
256 * @param self The irn ops themselves.
257 * @param irn The node.
258 * @return A set of flags.
260 arch_irn_flags_t (*get_flags)(const arch_irn_ops_t *self, const ir_node *irn);
265 * Get the register requirements for a node.
266 * @param env The architecture environment.
267 * @param req A pointer to a requirements structure, where the data can
269 * @param irn The node.
270 * @param pos The position of the operand you're interested in.
271 * @return A pointer to the register requirements which may <b>not</b>
272 * neccessarily be equal to @p req. If NULL is returned, the
273 * operand was no register operand.
275 extern const arch_register_req_t *
276 arch_get_register_req(const arch_env_t *env, arch_register_req_t *req,
277 const ir_node *irn, int pos);
280 * Check if an operand is a register operand.
281 * @param env The environment.
282 * @param irn The node.
283 * @param pos The position of the operand.
284 * @return 1, if the operand is significant for register allocation, 0
287 extern int arch_is_register_operand(const arch_env_t *env,
288 const ir_node *irn, int pos);
291 * Get the number of allocatable registers concerning
292 * a register class for an operand of a node.
293 * @param env The environment.
294 * @param irn The node.
295 * @param pos The postition of the node's operand.
296 * @param cls The register class.
297 * @param bs The bitset all allocatable registers shall be put into.
298 * Note, that you can also pass NULL here. If you don't,
299 * make sure, the bitset is as large as the register class
301 * @return The amount of registers allocatable for that operand.
303 extern int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn,
304 int pos, const arch_register_class_t *cls, bitset_t *bs);
307 * Check, if a register is assignable to an operand of a node.
308 * @param env The architecture environment.
309 * @param irn The node.
310 * @param pos The position of the operand.
311 * @param reg The register.
312 * @return 1, if the register might be allocated to the operand 0 if not.
314 extern int arch_reg_is_allocatable(const arch_env_t *env,
315 const ir_node *irn, int pos, const arch_register_t *reg);
318 * Get the register class of an operand of a node.
319 * @param env The architecture environment.
320 * @param irn The node.
321 * @param pos The position of the operand.
322 * @return The register class of the operand or NULL, if
323 * operand is a non-register operand.
325 extern const arch_register_class_t *
326 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos);
329 * Get the register allocated at a certain output operand of a node.
330 * @param env The arch nvironment.
331 * @param irn The node.
332 * @return The register allocated for this operand
334 extern const arch_register_t *
335 arch_get_irn_register(const arch_env_t *env, const ir_node *irn);
338 * Set the register for a certain output operand.
339 * @param env The architecture environment.
340 * @param irn The node.
341 * @param idx The index of the output operand.
342 * @param reg The register.
344 extern void arch_set_irn_register(const arch_env_t *env, ir_node *irn,
345 const arch_register_t *reg);
349 * @param env The architecture environment.
350 * @param irn The node.
351 * @return A classification of the node.
353 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn);
356 * Get the flags of a node.
357 * @param env The architecture environment.
358 * @param irn The node.
361 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn);
363 #define arch_irn_has_reg_class(env, irn, pos, cls) \
364 ((cls) == arch_get_irn_reg_class(env, irn, pos))
367 * Somebody who can be asked about nodes.
369 struct _arch_irn_handler_t {
372 * Get the operations of an irn.
373 * @param self The handler from which the method is invoked.
374 * @param irn Some node.
375 * @return Operations for that irn.
377 const arch_irn_ops_t *(*get_irn_ops)(const arch_irn_handler_t *handler,
383 * The code generator.
385 struct _arch_code_generator_if_t {
388 * Called, when the graph is being normalized.
390 void (*prepare_graph)(void *self);
393 * Called before scheduling.
395 void (*before_sched)(void *self);
398 * Called before register allocation.
400 void (*before_ra)(void *self);
403 * Called after everything happened.
404 * The code generator must also be de-allocated here.
406 void (*done)(void *self);
410 #define _arch_cg_call(cg, func) \
412 if((cg)->impl->func) \
413 (cg)->impl->func(cg); \
416 #define arch_code_generator_prepare_graph(cg) _arch_cg_call(cg, prepare_graph)
417 #define arch_code_generator_before_sched(cg) _arch_cg_call(cg, before_sched)
418 #define arch_code_generator_before_ra(cg) _arch_cg_call(cg, before_ra)
419 #define arch_code_generator_done(cg) _arch_cg_call(cg, done)
422 * Code generator base class.
424 struct _arch_code_generator_t {
425 const arch_code_generator_if_t *impl;
432 const arch_isa_if_t *impl;
436 * Architecture interface.
438 struct _arch_isa_if_t {
441 void (*register_options)(lc_opt_entry_t *grp);
445 * Initialize the isa interface.
447 void *(*init)(FILE *file_handle);
450 * Free the isa instance.
452 void (*done)(void *self);
455 * Get the the number of register classes in the isa.
456 * @return The number of register classes.
458 int (*get_n_reg_class)(const void *self);
461 * Get the i-th register class.
462 * @param i The number of the register class.
463 * @return The register class.
465 const arch_register_class_t *(*get_reg_class)(const void *self, int i);
468 * The irn handler for this architecture.
469 * The irn handler is registered by the Firm back end
470 * when the architecture is initialized.
473 const arch_irn_handler_t *(*get_irn_handler)(const void *self);
476 * Produce a new code generator.
477 * @param self The this pointer.
478 * @param irg The graph for which code shall be generated.
479 * @return A code generator.
481 arch_code_generator_t *(*make_code_generator)(void *self, ir_graph *irg);
484 * Get the list scheduler to use.
485 * @param self The isa object.
486 * @return The list scheduler selector.
488 const list_sched_selector_t *(*get_list_sched_selector)(const void *self);
491 #define arch_isa_get_n_reg_class(isa) ((isa)->impl->get_n_reg_class(isa))
492 #define arch_isa_get_reg_class(isa,i) ((isa)->impl->get_reg_class(isa, i))
493 #define arch_isa_get_irn_handler(isa) ((isa)->impl->get_irn_handler(isa))
494 #define arch_isa_make_code_generator(isa,irg) ((isa)->impl->make_code_generator(isa, irg))
496 #define ARCH_MAX_HANDLERS 8
499 * Environment for the architecture infrastructure.
500 * Keep this everywhere you're going.
503 const struct _be_node_factory_t *node_factory; /**< The node factory for be nodes. */
504 arch_isa_t *isa; /**< The isa about which everything is. */
506 arch_irn_handler_t const *handlers[ARCH_MAX_HANDLERS]; /**< The handlers are organized as
509 int handlers_tos; /**< The stack pointer of the handler
514 * Get the isa of an arch environment.
515 * @param env The environment.
516 * @return The isa with which the env was initialized with.
518 #define arch_env_get_isa(env) ((env)->isa)
521 * Initialize the architecture environment struct.
522 * @param isa The isa which shall be put into the environment.
523 * @return The environment.
525 extern arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa, FILE *file_handle);
528 * Add a node handler to the environment.
529 * @param env The environment.
530 * @param handler A node handler.
531 * @return The environment itself.
533 extern arch_env_t *arch_env_add_irn_handler(arch_env_t *env,
534 const arch_irn_handler_t *handler);
536 #endif /* _FIRM_BEARCH_H */