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