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