2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Processor architecture specification - internal data structures.
23 * @author Sebastian Hack
26 #ifndef FIRM_BE_BEARCH_T_H
27 #define FIRM_BE_BEARCH_T_H
31 #include "belistsched.h"
32 #include "beilpsched.h"
33 #include "bemachine.h"
36 #include "raw_bitset.h"
41 struct arch_register_t {
42 const char *name; /**< The name of the register. */
43 const arch_register_class_t *reg_class; /**< The class the register belongs to. */
44 unsigned index; /**< The index of the register in the class. */
45 arch_register_type_t type; /**< The type of the register. */
46 void *data; /**< Custom data. */
49 static INLINE const arch_register_class_t *
50 _arch_register_get_class(const arch_register_t *reg)
52 return reg->reg_class;
56 unsigned _arch_register_get_index(const arch_register_t *reg)
62 const char *_arch_register_get_name(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) _arch_register_get_name(reg)
72 * Convenience macro to check for register type.
73 * @param req A pointer to register.
74 * @param kind The kind of type to check for (see arch_register_type_t).
75 * @return 1, If register is of given kind, 0 if not.
77 #define arch_register_type_is(reg, kind) \
78 (((reg)->type & arch_register_type_ ## kind) != 0)
81 * A class of registers.
82 * Like general purpose or floating point.
84 struct arch_register_class_t {
85 unsigned index; /**< index of this register class */
86 const char *name; /**< The name of the register class.*/
87 unsigned n_regs; /**< Number of registers in this
89 ir_mode *mode; /**< The mode of the register class.*/
90 const arch_register_t *regs; /**< The array of registers. */
91 arch_register_class_flags_t flags; /**< register class flags. */
94 /** return the number of registers in this register class */
95 #define arch_register_class_n_regs(cls) ((cls)->n_regs)
97 /** return the largest mode of this register class */
98 #define arch_register_class_mode(cls) ((cls)->mode)
100 /** return the name of this register class */
101 #define arch_register_class_name(cls) ((cls)->name)
103 /** return the index of this register class */
104 #define arch_register_class_index(cls) ((cls)->index)
106 /** return the register class flags */
107 #define arch_register_class_flags(cls) ((cls)->flags)
109 static INLINE const arch_register_t *
110 _arch_register_for_index(const arch_register_class_t *cls, unsigned idx)
112 assert(idx < cls->n_regs);
113 return &cls->regs[idx];
116 #define arch_register_for_index(cls, idx) _arch_register_for_index(cls, idx)
119 * Convenience macro to check for set constraints.
120 * @param req A pointer to register requirements.
121 * @param kind The kind of constraint to check for (see arch_register_req_type_t).
122 * @return 1, If the kind of constraint is present, 0 if not.
124 #define arch_register_req_is(req, kind) \
125 (((req)->type & (arch_register_req_type_ ## kind)) != 0)
128 * Expresses requirements to register allocation for an operand.
130 struct arch_register_req_t {
131 arch_register_req_type_t type; /**< The type of the constraint. */
132 const arch_register_class_t *cls; /**< The register class this constraint belongs to. */
134 const unsigned *limited; /**< allowed register bitset */
136 unsigned other_same; /**< Bitmask of ins which should use the
137 same register (should_be_same). */
138 unsigned other_different; /**< Bitmask of ins which shall use a
140 (must_be_different) */
143 static INLINE int reg_reqs_equal(const arch_register_req_t *req1,
144 const arch_register_req_t *req2)
149 if (req1->type != req2->type
150 || req1->cls != req2->cls
151 || req1->other_same != req2->other_same
152 || req1->other_different != req2->other_different)
155 if (req1->limited != NULL) {
158 if (req2->limited == NULL)
161 n_regs = arch_register_class_n_regs(req1->cls);
162 if (!rbitset_equal(req1->limited, req2->limited, n_regs))
170 * An inverse operation returned by the backend
172 struct arch_inverse_t {
173 int n; /**< count of nodes returned in nodes array */
174 int costs; /**< costs of this remat */
176 /**< nodes for this inverse operation. shall be in
177 * schedule order. last element is the target value
182 struct arch_irn_ops_if_t {
185 * Get the register requirements for a given operand.
186 * @param self The self pointer.
187 * @param irn The node.
188 * @param pos The operand's position
189 * (-1 for the result of the node, 0..n for the input operands).
190 * @return The register requirements for the selected operand.
191 * The pointer returned is never NULL.
193 const arch_register_req_t *(*get_irn_reg_req)(const void *self,
194 const ir_node *irn, int pos);
197 * Set the register for an output operand.
198 * @param irn The node.
199 * @param reg The register allocated to that operand.
200 * @note If the operand is not a register operand,
201 * the call is ignored.
203 void (*set_irn_reg)(const void *self, ir_node *irn, const arch_register_t *reg);
206 * Get the register allocated for an output operand.
207 * @param irn The node.
208 * @return The register allocated at that operand. NULL, if
209 * the operand was no register operand or
210 * @c arch_register_invalid, if no register has yet been
211 * allocated for this node.
213 const arch_register_t *(*get_irn_reg)(const void *self, const ir_node *irn);
217 * @param irn The node.
218 * @return A classification.
220 arch_irn_class_t (*classify)(const void *self, const ir_node *irn);
223 * Get the flags of a node.
224 * @param self The irn ops themselves.
225 * @param irn The node.
226 * @return A set of flags.
228 arch_irn_flags_t (*get_flags)(const void *self, const ir_node *irn);
231 * Get the entity on the stack frame this node depends on.
232 * @param self The this pointer.
233 * @param irn The node in question.
234 * @return The entity on the stack frame or NULL, if the node does not have a
235 * stack frame entity.
237 ir_entity *(*get_frame_entity)(const void *self, const ir_node *irn);
240 * Set the entity on the stack frame this node depends on.
241 * @param self The this pointer.
242 * @param irn The node in question.
243 * @param ent The entity to set
245 void (*set_frame_entity)(const void *self, ir_node *irn, ir_entity *ent);
248 * Set the offset of a node carrying an entity on the stack frame.
249 * @param self The this pointer.
250 * @param irn The node.
251 * @param offset The offset of the node's stack frame entity.
253 void (*set_frame_offset)(const void *self, ir_node *irn, int offset);
256 * Returns the delta of the stackpointer for nodes that increment or
257 * decrement the stackpointer with a constant value. (push, pop
258 * nodes on most architectures).
259 * A positive value stands for an expanding stack area, a negative value for
262 * @param self The this pointer
263 * @param irn The node
264 * @return 0 if the stackpointer is not modified with a constant
265 * value, otherwise the increment/decrement value
267 int (*get_sp_bias)(const void *self, const ir_node *irn);
270 * Returns an inverse operation which yields the i-th argument
271 * of the given node as result.
273 * @param self The this pointer.
274 * @param irn The original operation
275 * @param i Index of the argument we want the inverse operation to yield
276 * @param inverse struct to be filled with the resulting inverse op
277 * @param obstack The obstack to use for allocation of the returned nodes array
278 * @return The inverse operation or NULL if operation invertible
280 arch_inverse_t *(*get_inverse)(const void *self, const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack);
283 * Get the estimated cycle count for @p irn.
285 * @param self The this pointer.
286 * @param irn The node.
288 * @return The estimated cycle count for this operation
290 int (*get_op_estimated_cost)(const void *self, const ir_node *irn);
293 * Asks the backend whether operand @p i of @p irn can be loaded form memory internally
295 * @param self The this pointer.
296 * @param irn The node.
297 * @param i Index of the argument we would like to know whether @p irn can load it form memory internally
299 * @return nonzero if argument can be loaded or zero otherwise
301 int (*possible_memory_operand)(const void *self, const ir_node *irn, unsigned int i);
304 * Ask the backend to assimilate @p reload of operand @p i into @p irn.
306 * @param self The this pointer.
307 * @param irn The node.
308 * @param spill The spill.
309 * @param i The position of the reload.
311 void (*perform_memory_operand)(const void *self, ir_node *irn, ir_node *spill, unsigned int i);
315 * irn_ops base class.
317 struct arch_irn_ops_t {
318 const arch_irn_ops_if_t *impl;
322 * Somebody who can be asked about IR nodes.
324 struct arch_irn_handler_t {
327 * Get the operations of an irn.
328 * @param self The handler from which the method is invoked.
329 * @param irn Some node.
330 * @return Operations for that irn.
332 const void *(*get_irn_ops)(const arch_irn_handler_t *handler,
337 * The code generator interface.
339 struct arch_code_generator_if_t {
341 * Initialize the code generator.
342 * @param birg A backend IRG session.
343 * @return A newly created code generator.
345 void *(*init)(be_irg_t *birg);
348 * return node used as base in pic code addresses
350 ir_node* (*get_pic_base)(void *self);
353 * Called before abi introduce.
355 void (*before_abi)(void *self);
358 * Called, when the graph is being normalized.
360 void (*prepare_graph)(void *self);
363 * Backend may provide an own spiller.
364 * This spiller needs to spill all register classes.
366 void (*spill)(void *self, be_irg_t *birg);
369 * Called before scheduling.
371 void (*before_sched)(void *self);
374 * Called before register allocation.
376 void (*before_ra)(void *self);
379 * Called after register allocation.
381 void (*after_ra)(void *self);
384 * Called directly before done is called. This should be the last place
385 * where the irg is modified.
387 void (*finish)(void *self);
390 * Called after everything happened. This call should emit the final
391 * assembly code but avoid changing the irg.
392 * The code generator must also be de-allocated here.
394 void (*done)(void *self);
398 * helper macro: call function func from the code generator
399 * if it's implemented.
401 #define _arch_cg_call(cg, func) \
403 if((cg)->impl->func) \
404 (cg)->impl->func(cg); \
407 #define _arch_cg_call_env(cg, env, func) \
409 if((cg)->impl->func) \
410 (cg)->impl->func(cg, env); \
413 #define arch_code_generator_before_abi(cg) _arch_cg_call(cg, before_abi)
414 #define arch_code_generator_prepare_graph(cg) _arch_cg_call(cg, prepare_graph)
415 #define arch_code_generator_before_sched(cg) _arch_cg_call(cg, before_sched)
416 #define arch_code_generator_before_ra(cg) _arch_cg_call(cg, before_ra)
417 #define arch_code_generator_after_ra(cg) _arch_cg_call(cg, after_ra)
418 #define arch_code_generator_finish(cg) _arch_cg_call(cg, finish)
419 #define arch_code_generator_done(cg) _arch_cg_call(cg, done)
420 #define arch_code_generator_spill(cg, birg) _arch_cg_call_env(cg, birg, spill)
421 #define arch_code_generator_has_spiller(cg) ((cg)->impl->spill != NULL)
422 #define arch_code_generator_get_pic_base(cg) \
423 ((cg)->impl->get_pic_base != NULL ? (cg)->impl->get_pic_base(cg) : NULL)
426 * Code generator base class.
428 struct arch_code_generator_t {
429 const arch_code_generator_if_t *impl;
436 const arch_isa_if_t *impl;
437 const arch_register_t *sp; /** The stack pointer register. */
438 const arch_register_t *bp; /** The base pointer register. */
439 int stack_dir; /** -1 for decreasing, 1 for increasing. */
440 int stack_alignment; /** stack alignment */
441 const be_main_env_t *main_env; /** the be main environment */
442 int spill_cost; /** cost for a be_Spill node */
443 int reload_cost; /** cost for a be_Reload node */
446 #define arch_isa_stack_dir(isa) ((isa)->stack_dir)
447 #define arch_isa_sp(isa) ((isa)->sp)
448 #define arch_isa_bp(isa) ((isa)->bp)
451 * Architecture interface.
453 struct arch_isa_if_t {
455 * Initialize the isa interface.
456 * @param file_handle the file handle to write the output to
457 * @param main_env the be main environment
458 * @return a new isa instance
460 void *(*init)(FILE *file_handle);
463 * Free the isa instance.
465 void (*done)(void *self);
468 * Get the the number of register classes in the isa.
469 * @return The number of register classes.
471 unsigned (*get_n_reg_class)(const void *self);
474 * Get the i-th register class.
475 * @param i The number of the register class.
476 * @return The register class.
478 const arch_register_class_t *(*get_reg_class)(const void *self, unsigned i);
481 * Get the register class which shall be used to store a value of a given mode.
482 * @param self The this pointer.
483 * @param mode The mode in question.
484 * @return A register class which can hold values of the given mode.
486 const arch_register_class_t *(*get_reg_class_for_mode)(const void *self, const ir_mode *mode);
489 * Get the ABI restrictions for procedure calls.
490 * @param self The this pointer.
491 * @param call_type The call type of the method (procedure) in question.
492 * @param p The array of parameter locations to be filled.
494 void (*get_call_abi)(const void *self, ir_type *call_type, be_abi_call_t *abi);
497 * The irn handler for this architecture.
498 * The irn handler is registered by the Firm back end
499 * when the architecture is initialized.
502 const arch_irn_handler_t *(*get_irn_handler)(const void *self);
505 * Get the code generator interface.
506 * @param self The this pointer.
507 * @return Some code generator interface.
509 const arch_code_generator_if_t *(*get_code_generator_if)(void *self);
512 * Get the list scheduler to use. There is already a selector given, the
513 * backend is free to modify and/or ignore it.
515 * @param self The isa object.
516 * @param selector The selector given by options.
517 * @return The list scheduler selector.
519 const list_sched_selector_t *(*get_list_sched_selector)(const void *self, list_sched_selector_t *selector);
522 * Get the ILP scheduler to use.
523 * @param self The isa object.
524 * @return The ILP scheduler selector
526 const ilp_sched_selector_t *(*get_ilp_sched_selector)(const void *self);
529 * Get the necessary alignment for storing a register of given class.
530 * @param self The isa object.
531 * @param cls The register class.
532 * @return The alignment in bytes.
534 int (*get_reg_class_alignment)(const void *self, const arch_register_class_t *cls);
537 * A "static" function, returns the frontend settings
538 * needed for this backend.
540 const backend_params *(*get_params)(void);
543 * Returns an 2-dim array of execution units, @p irn can be executed on.
544 * The first dimension is the type, the second the allowed units of this
546 * Each dimension is a NULL terminated list.
547 * @param self The isa object.
548 * @param irn The node.
549 * @return An array of allowed execution units.
551 * { unit1_of_tp1, ..., unitX1_of_tp1, NULL },
553 * { unit1_of_tpY, ..., unitXn_of_tpY, NULL },
557 const be_execution_unit_t ***(*get_allowed_execution_units)(const void *self, const ir_node *irn);
560 * Return the abstract machine for this isa.
561 * @param self The isa object.
563 const be_machine_t *(*get_machine)(const void *self);
566 * Return an ordered list of irgs where code should be generated for.
567 * If NULL is returned, all irg will be taken into account and they will be
568 * generated in an arbitrary order.
569 * @param self The isa object.
570 * @param irgs A flexible array ARR_F of length 0 where the backend can append the desired irgs.
571 * @return A flexible array ARR_F containing all desired irgs in the desired order.
573 ir_graph **(*get_backend_irg_list)(const void *self, ir_graph ***irgs);
576 #define arch_isa_get_n_reg_class(isa) ((isa)->impl->get_n_reg_class(isa))
577 #define arch_isa_get_reg_class(isa,i) ((isa)->impl->get_reg_class(isa, i))
578 #define arch_isa_get_irn_handler(isa) ((isa)->impl->get_irn_handler(isa))
579 #define arch_isa_get_call_abi(isa,tp,abi) ((isa)->impl->get_call_abi((isa), (tp), (abi)))
580 #define arch_isa_get_reg_class_for_mode(isa,mode) ((isa)->impl->get_reg_class_for_mode((isa), (mode)))
581 #define arch_isa_make_code_generator(isa,irg) ((isa)->impl->make_code_generator((isa), (irg)))
582 #define arch_isa_get_reg_class_alignment(isa, cls) ((isa)->impl->get_reg_class_alignment((isa), (cls)))
583 #define arch_isa_get_allowed_execution_units(isa, irn) ((isa)->impl->get_allowed_execution_units((isa), (irn)))
584 #define arch_isa_get_machine(isa) ((isa)->impl->get_machine((isa)))
585 #define arch_isa_get_backend_irg_list(isa, irgs) ((isa)->impl->get_backend_irg_list((isa), (irgs)))
587 #define ARCH_MAX_HANDLERS 8
590 * Environment for the architecture infrastructure.
591 * Keep this everywhere you're going.
594 arch_isa_t *isa; /**< The isa about which everything is. */
596 arch_irn_handler_t const *handlers[ARCH_MAX_HANDLERS]; /**< The handlers are organized as
599 int handlers_tos; /**< The stack pointer of the handler
604 * Get the isa of an arch environment.
605 * @param env The environment.
606 * @return The isa with which the env was initialized with.
608 #define arch_env_get_isa(env) ((env)->isa)
610 #endif /* FIRM_BE_BEARCH_T_H */