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