7dc1ce063938c579b25fe95fb4fdd60a97d4328e
[libfirm] / ir / be / bearch_t.h
1 #ifndef FIRM_BEARCH_T_H
2 #define FIRM_BEARCH_T_H
3
4 #include "bearch.h"
5
6 #include "belistsched.h"
7 #include "beilpsched.h"
8 #include "bemachine.h"
9 #include "beirg.h"
10 #include "beabi.h"
11
12 /**
13  * A register.
14  */
15 struct arch_register_t {
16         const char                  *name;        /**< The name of the register. */
17         const arch_register_class_t *reg_class;   /**< The class the register belongs to. */
18         int                         index;        /**< The index of the register in the class. */
19         arch_register_type_t        type;         /**< The type of the register. */
20         void                        *data;        /**< Custom data. */
21 };
22
23 static INLINE const arch_register_class_t *
24 _arch_register_get_class(const arch_register_t *reg)
25 {
26         return reg->reg_class;
27 }
28
29 static INLINE int _arch_register_get_index(const arch_register_t *reg)
30 {
31         return reg->index;
32 }
33
34 static INLINE const char *_arch_register_get_name(const arch_register_t *reg)
35 {
36         return reg->name;
37 }
38
39 #define arch_register_get_class(reg)      _arch_register_get_class(reg)
40 #define arch_register_get_index(reg)      _arch_register_get_index(reg)
41 #define arch_register_get_name(reg)       _arch_register_get_name(reg)
42
43 /**
44  * Convenience macro to check for register type.
45  * @param req   A pointer to register.
46  * @param kind  The kind of type to check for (see arch_register_type_t).
47  * @return      1, If register is of given kind, 0 if not.
48  */
49 #define arch_register_type_is(reg, kind) \
50   (((reg)->type & arch_register_type_ ## kind) != 0)
51
52 /**
53  * A class of registers.
54  * Like general purpose or floating point.
55  */
56 struct arch_register_class_t {
57         const char *name;               /**< The name of the register class. */
58         int n_regs;                     /**< Number of registers in this class. */
59         ir_mode *mode;                  /**< The mode of the register class. */
60         const arch_register_t *regs;    /**< The array of registers. */
61 };
62
63 /** return the number of registers in this register class */
64 #define arch_register_class_n_regs(cls) ((cls)->n_regs)
65
66 /** return the largest mode of this register class */
67 #define arch_register_class_mode(cls) ((cls)->mode)
68
69 /** return the name of this register class */
70 #define arch_register_class_name(cls) ((cls)->name)
71
72 static INLINE const arch_register_t *
73 _arch_register_for_index(const arch_register_class_t *cls, int idx)
74 {
75         assert(0 <= idx && idx < cls->n_regs);
76         return &cls->regs[idx];
77 }
78
79 #define arch_register_for_index(cls, idx)   _arch_register_for_index(cls, idx)
80
81 /**
82  * Convenience macro to check for set constraints.
83  * @param req   A pointer to register requirements.
84  * @param kind  The kind of constraint to check for (see arch_register_req_type_t).
85  * @return      1, If the kind of constraint is present, 0 if not.
86  */
87 #define arch_register_req_is(req, kind) \
88         (((req)->type & (arch_register_req_type_ ## kind)) != 0)
89
90 /**
91  * Expresses requirements to register allocation for an operand.
92  */
93 struct arch_register_req_t {
94         arch_register_req_type_t type;      /**< The type of the constraint. */
95         const arch_register_class_t *cls;   /**< The register class this constraint belongs to. */
96
97         const unsigned *limited;            /**< allowed register bitset */
98
99         int other_same;                     /**< The in number which shall have
100                                                                                      the same res (should_be_same)*/
101         int other_different;                /**< The other node from which this
102                                                                                      one's register must be different
103                                                                                          (case must_be_different). */
104 };
105
106 struct arch_flag_t {
107         const char *name;
108         unsigned    index;
109 };
110
111 /**
112  * An inverse operation returned by the backend
113  */
114 struct arch_inverse_t {
115         int      n;       /**< count of nodes returned in nodes array */
116         int      costs;   /**< costs of this remat */
117
118         /**< nodes for this inverse operation. shall be in
119          *  schedule order. last element is the target value
120          */
121         ir_node  **nodes;
122 };
123
124 struct arch_irn_ops_if_t {
125
126         /**
127          * Get the register requirements for a given operand.
128          * @param self The self pointer.
129          * @param irn The node.
130          * @param pos The operand's position
131          *        (-1 for the result of the node, 0..n for the input operands).
132          * @return    The register requirements for the selected operand.
133          *            The pointer returned is never NULL.
134          */
135         const arch_register_req_t *(*get_irn_reg_req)(const void *self,
136                                                       const ir_node *irn, int pos);
137
138         /**
139          * Set the register for an output operand.
140          * @param irn The node.
141          * @param reg The register allocated to that operand.
142          * @note      If the operand is not a register operand,
143          *            the call is ignored.
144          */
145         void (*set_irn_reg)(const void *self, ir_node *irn, const arch_register_t *reg);
146
147         /**
148          * Get the register allocated for an output operand.
149          * @param irn The node.
150          * @return    The register allocated at that operand. NULL, if
151          *            the operand was no register operand or
152          *            @c arch_register_invalid, if no register has yet been
153          *            allocated for this node.
154          */
155         const arch_register_t *(*get_irn_reg)(const void *self, const ir_node *irn);
156
157         /**
158          * Classify the node.
159          * @param irn The node.
160          * @return A classification.
161          */
162         arch_irn_class_t (*classify)(const void *self, const ir_node *irn);
163
164         /**
165          * Get the flags of a node.
166          * @param self The irn ops themselves.
167          * @param irn The node.
168          * @return A set of flags.
169          */
170         arch_irn_flags_t (*get_flags)(const void *self, const ir_node *irn);
171
172         /**
173          * Get the entity on the stack frame this node depends on.
174          * @param self The this pointer.
175          * @param irn  The node in question.
176          * @return The entity on the stack frame or NULL, if the node does not have a
177          *         stack frame entity.
178          */
179         ir_entity *(*get_frame_entity)(const void *self, const ir_node *irn);
180
181         /**
182          * Set the entity on the stack frame this node depends on.
183          * @param self The this pointer.
184          * @param irn  The node in question.
185          * @param ent  The entity to set
186          */
187         void (*set_frame_entity)(const void *self, ir_node *irn, ir_entity *ent);
188
189         /**
190          * Set the offset of a node carrying an entity on the stack frame.
191          * @param self The this pointer.
192          * @param irn  The node.
193          * @param offset The offset of the node's stack frame entity.
194          */
195         void (*set_frame_offset)(const void *self, ir_node *irn, int offset);
196
197         /**
198          * Returns the delta of the stackpointer for nodes that increment or
199          * decrement the stackpointer with a constant value. (push, pop
200          * nodes on most architectures).
201          * A positive value stands for an expanding stack area, a negative value for
202          * a shrinking one.
203          *
204          * @param self      The this pointer
205          * @param irn       The node
206          * @return          0 if the stackpointer is not modified with a constant
207          *                  value, otherwise the increment/decrement value
208          */
209         int (*get_sp_bias)(const void *self, const ir_node *irn);
210
211         /**
212          * Returns an inverse operation which yields the i-th argument
213          * of the given node as result.
214          *
215          * @param self      The this pointer.
216          * @param irn       The original operation
217          * @param i         Index of the argument we want the inverse operation to yield
218          * @param inverse   struct to be filled with the resulting inverse op
219          * @param obstack   The obstack to use for allocation of the returned nodes array
220          * @return          The inverse operation or NULL if operation invertible
221          */
222         arch_inverse_t *(*get_inverse)(const void *self, const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack);
223
224         /**
225          * Get the estimated cycle count for @p irn.
226          *
227          * @param self The this pointer.
228          * @param irn  The node.
229          *
230          * @return     The estimated cycle count for this operation
231          */
232         int (*get_op_estimated_cost)(const void *self, const ir_node *irn);
233
234         /**
235          * Asks the backend whether operand @p i of @p irn can be loaded form memory internally
236          *
237          * @param self The this pointer.
238          * @param irn  The node.
239          * @param i    Index of the argument we would like to know whether @p irn can load it form memory internally
240          *
241          * @return     nonzero if argument can be loaded or zero otherwise
242          */
243         int (*possible_memory_operand)(const void *self, const ir_node *irn, unsigned int i);
244
245         /**
246          * Ask the backend to assimilate @p reload of operand @p i into @p irn.
247          *
248          * @param self   The this pointer.
249          * @param irn    The node.
250          * @param spill  The spill.
251          * @param i      The position of the reload.
252          */
253         void (*perform_memory_operand)(const void *self, ir_node *irn, ir_node *spill, unsigned int i);
254 };
255
256 /**
257  * irn_ops base class.
258  */
259 struct arch_irn_ops_t {
260         const arch_irn_ops_if_t *impl;
261 };
262
263 /**
264  * Somebody who can be asked about IR nodes.
265  */
266 struct arch_irn_handler_t {
267
268   /**
269     * Get the operations of an irn.
270     * @param self The handler from which the method is invoked.
271     * @param irn Some node.
272     * @return Operations for that irn.
273     */
274   const void *(*get_irn_ops)(const arch_irn_handler_t *handler,
275       const ir_node *irn);
276 };
277
278 /**
279  * The code generator interface.
280  */
281 struct arch_code_generator_if_t {
282         /**
283          * Initialize the code generator.
284          * @param birg A backend IRG session.
285          * @return     A newly created code generator.
286          */
287         void *(*init)(be_irg_t *birg);
288
289         /**
290          * Called before abi introduce.
291          */
292         void (*before_abi)(void *self);
293
294         /**
295          * Called, when the graph is being normalized.
296          */
297         void (*prepare_graph)(void *self);
298
299         /**
300          * Backend may provide an own spiller.
301          * This spiller needs to spill all register classes.
302          */
303         void (*spill)(void *self, be_irg_t *birg);
304
305         /**
306          * Called before scheduling.
307          */
308         void (*before_sched)(void *self);
309
310         /**
311          * Called before register allocation.
312          */
313         void (*before_ra)(void *self);
314
315         /**
316          * Called after register allocation.
317          */
318         void (*after_ra)(void *self);
319
320         /**
321          * Called directly before done is called. This should be the last place
322          * where the irg is modified.
323          */
324         void (*finish)(void *self);
325
326         /**
327          * Called after everything happened. This call should emit the final
328          * assembly code but avoid changing the irg.
329          * The code generator must also be de-allocated here.
330          */
331         void (*done)(void *self);
332 };
333
334 /**
335  * helper macro: call function func from the code generator
336  * if it's implemented.
337  */
338 #define _arch_cg_call(cg, func) \
339 do { \
340         if((cg)->impl->func) \
341                 (cg)->impl->func(cg); \
342 } while(0)
343
344 #define _arch_cg_call_env(cg, env, func) \
345 do { \
346         if((cg)->impl->func) \
347                 (cg)->impl->func(cg, env); \
348 } while(0)
349
350 #define arch_code_generator_before_abi(cg)      _arch_cg_call(cg, before_abi)
351 #define arch_code_generator_prepare_graph(cg)   _arch_cg_call(cg, prepare_graph)
352 #define arch_code_generator_before_sched(cg)    _arch_cg_call(cg, before_sched)
353 #define arch_code_generator_before_ra(cg)       _arch_cg_call(cg, before_ra)
354 #define arch_code_generator_after_ra(cg)        _arch_cg_call(cg, after_ra)
355 #define arch_code_generator_finish(cg)          _arch_cg_call(cg, finish)
356 #define arch_code_generator_done(cg)            _arch_cg_call(cg, done)
357 #define arch_code_generator_spill(cg, birg)     _arch_cg_call_env(cg, birg, spill)
358 #define arch_code_generator_has_spiller(cg)     ((cg)->impl->spill != NULL)
359
360 /**
361  * Code generator base class.
362  */
363 struct arch_code_generator_t {
364         const arch_code_generator_if_t *impl;
365 };
366
367 /**
368  * ISA base class.
369  */
370 struct arch_isa_t {
371         const arch_isa_if_t   *impl;
372         const arch_register_t *sp;        /** The stack pointer register. */
373         const arch_register_t *bp;        /** The base pointer register. */
374         const int             stack_dir;  /** -1 for decreasing, 1 for increasing. */
375         const be_main_env_t   *main_env;  /** the be main environment */
376 };
377
378 #define arch_isa_stack_dir(isa)  ((isa)->stack_dir)
379 #define arch_isa_sp(isa)         ((isa)->sp)
380 #define arch_isa_bp(isa)         ((isa)->bp)
381
382 /**
383  * Architecture interface.
384  */
385 struct arch_isa_if_t {
386         /**
387          * Initialize the isa interface.
388          * @param file_handle  the file handle to write the output to
389          * @param main_env     the be main environment
390          * @return a new isa instance
391          */
392         void *(*init)(FILE *file_handle);
393
394         /**
395          * Free the isa instance.
396          */
397         void (*done)(void *self);
398
399         /**
400          * Get the the number of register classes in the isa.
401          * @return The number of register classes.
402          */
403         int (*get_n_reg_class)(const void *self);
404
405         /**
406          * Get the i-th register class.
407          * @param i The number of the register class.
408          * @return The register class.
409          */
410         const arch_register_class_t *(*get_reg_class)(const void *self, int i);
411
412         /**
413          * Get the register class which shall be used to store a value of a given mode.
414          * @param self The this pointer.
415          * @param mode The mode in question.
416          * @return A register class which can hold values of the given mode.
417          */
418         const arch_register_class_t *(*get_reg_class_for_mode)(const void *self, const ir_mode *mode);
419
420         /**
421          * Get the ABI restrictions for procedure calls.
422          * @param self        The this pointer.
423          * @param method_type The type of the method (procedure) in question.
424          * @param p           The array of parameter locations to be filled.
425          */
426         void (*get_call_abi)(const void *self, ir_type *method_type, be_abi_call_t *abi);
427
428         /**
429          * The irn handler for this architecture.
430          * The irn handler is registered by the Firm back end
431          * when the architecture is initialized.
432          * (May be NULL).
433          */
434         const arch_irn_handler_t *(*get_irn_handler)(const void *self);
435
436         /**
437          * Get the code generator interface.
438          * @param self The this pointer.
439          * @return     Some code generator interface.
440          */
441         const arch_code_generator_if_t *(*get_code_generator_if)(void *self);
442
443         /**
444          * Get the list scheduler to use. There is already a selector given, the
445          * backend is free to modify and/or ignore it.
446          *
447          * @param self     The isa object.
448          * @param selector The selector given by options.
449          * @return         The list scheduler selector.
450          */
451         const list_sched_selector_t *(*get_list_sched_selector)(const void *self, list_sched_selector_t *selector);
452
453         /**
454          * Get the ILP scheduler to use.
455          * @param self  The isa object.
456          * @return      The ILP scheduler selector
457          */
458         const ilp_sched_selector_t *(*get_ilp_sched_selector)(const void *self);
459
460         /**
461          * Get the necessary alignment for storing a register of given class.
462          * @param self  The isa object.
463          * @param cls   The register class.
464          * @return      The alignment in bytes.
465          */
466         int (*get_reg_class_alignment)(const void *self, const arch_register_class_t *cls);
467
468         /**
469          * A "static" function, returns the frontend settings
470          * needed for this backend.
471          */
472         const backend_params *(*get_params)(void);
473
474         /**
475          * Returns an 2-dim array of execution units, @p irn can be executed on.
476          * The first dimension is the type, the second the allowed units of this type.
477          * Each dimension is a NULL terminated list.
478          * @param self  The isa object.
479          * @param irn   The node.
480          * @return An array of allowed execution units.
481          *         exec_unit = {
482          *                       { unit1_of_tp1, ..., unitX1_of_tp1, NULL },
483          *                       ...,
484          *                       { unit1_of_tpY, ..., unitXn_of_tpY, NULL },
485          *                       NULL
486          *                     };
487          */
488         const be_execution_unit_t ***(*get_allowed_execution_units)(const void *self, const ir_node *irn);
489
490         /**
491          * Return the abstract machine for this isa.
492          * @param self  The isa object.
493          */
494         const be_machine_t *(*get_machine)(const void *self);
495
496         /**
497          * Return an ordered list of irgs where code should be generated for.
498          * If NULL is returned, all irg will be taken into account and they will be
499          * generated in an arbitrary order.
500          * @param self   The isa object.
501          * @param irgs   A flexible array ARR_F of length 0 where the backend cann append the desired irgs.
502          * @return A flexible array ARR_F containing all desired irgs in the desired order.
503          */
504         ir_graph **(*get_backend_irg_list)(const void *self, ir_graph ***irgs);
505 };
506
507 #define arch_isa_get_n_reg_class(isa)                  ((isa)->impl->get_n_reg_class(isa))
508 #define arch_isa_get_reg_class(isa,i)                  ((isa)->impl->get_reg_class(isa, i))
509 #define arch_isa_get_irn_handler(isa)                  ((isa)->impl->get_irn_handler(isa))
510 #define arch_isa_get_call_abi(isa,tp,abi)              ((isa)->impl->get_call_abi((isa), (tp), (abi)))
511 #define arch_isa_get_reg_class_for_mode(isa,mode)      ((isa)->impl->get_reg_class_for_mode((isa), (mode)))
512 #define arch_isa_make_code_generator(isa,irg)          ((isa)->impl->make_code_generator((isa), (irg)))
513 #define arch_isa_get_reg_class_alignment(isa, cls)     ((isa)->impl->get_reg_class_alignment((isa), (cls)))
514 #define arch_isa_get_allowed_execution_units(isa, irn) ((isa)->impl->get_allowed_execution_units((isa), (irn)))
515 #define arch_isa_get_machine(isa)                      ((isa)->impl->get_machine((isa)))
516 #define arch_isa_get_backend_irg_list(isa, irgs)       ((isa)->impl->get_backend_irg_list((isa), (irgs)))
517
518 #define ARCH_MAX_HANDLERS         8
519
520 /**
521  * Environment for the architecture infrastructure.
522  * Keep this everywhere you're going.
523  */
524 struct arch_env_t {
525         arch_isa_t *isa;                                /**< The isa about which everything is. */
526
527         arch_irn_handler_t const *handlers[ARCH_MAX_HANDLERS]; /**< The handlers are organized as
528                                                            a stack. */
529
530         int handlers_tos;                                   /**< The stack pointer of the handler
531                                                         stack. */
532 };
533
534 /**
535  * Get the isa of an arch environment.
536  * @param env The environment.
537  * @return The isa with which the env was initialized with.
538  */
539 #define arch_env_get_isa(env)   ((env)->isa)
540
541 #endif