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