8dc1f61677f1addb670e7b6d8702356057b9f084
[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 };
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 static INLINE int reg_reqs_equal(const arch_register_req_t *req1,
143                                  const arch_register_req_t *req2)
144 {
145         if (req1 == req2)
146                 return 1;
147
148         if (req1->type != req2->type
149                         || req1->cls != req2->cls
150                         || req1->other_same != req2->other_same
151                         || req1->other_different != req2->other_different)
152                 return 0;
153
154         if (req1->limited != NULL) {
155                 size_t n_regs;
156
157                 if (req2->limited == NULL)
158                         return 0;
159
160                 n_regs = arch_register_class_n_regs(req1->cls);
161                 if (!rbitset_equal(req1->limited, req2->limited, n_regs))
162                         return 0;
163         }
164
165         return 1;
166 }
167
168 /**
169  * An inverse operation returned by the backend
170  */
171 struct arch_inverse_t {
172         int      n;       /**< count of nodes returned in nodes array */
173         int      costs;   /**< costs of this remat */
174
175         /**< nodes for this inverse operation. shall be in
176          *  schedule order. last element is the target value
177          */
178         ir_node  **nodes;
179 };
180
181 struct arch_irn_ops_t {
182
183         /**
184          * Get the register requirements for a given operand.
185          * @param self The self pointer.
186          * @param irn The node.
187          * @param pos The operand's position
188          *        (-1 for the result of the node, 0..n for the input operands).
189          * @return    The register requirements for the selected operand.
190          *            The pointer returned is never NULL.
191          */
192         const arch_register_req_t *(*get_irn_reg_req)(const ir_node *irn, int pos);
193
194         /**
195          * Set the register for an output operand.
196          * @param irn The node.
197          * @param reg The register allocated to that operand.
198          * @note      If the operand is not a register operand,
199          *            the call is ignored.
200          */
201         void (*set_irn_reg)(ir_node *irn, const arch_register_t *reg);
202
203         /**
204          * Get the register allocated for an output operand.
205          * @param irn The node.
206          * @return    The register allocated at that operand. NULL, if
207          *            the operand was no register operand or
208          *            @c arch_register_invalid, if no register has yet been
209          *            allocated for this node.
210          */
211         const arch_register_t *(*get_irn_reg)(const ir_node *irn);
212
213         /**
214          * Classify the node.
215          * @param irn The node.
216          * @return A classification.
217          */
218         arch_irn_class_t (*classify)(const ir_node *irn);
219
220         /**
221          * Get the flags of a node.
222          * @param self The irn ops themselves.
223          * @param irn The node.
224          * @return A set of flags.
225          */
226         arch_irn_flags_t (*get_flags)(const ir_node *irn);
227
228         /**
229          * Get the entity on the stack frame this node depends on.
230          * @param self The this pointer.
231          * @param irn  The node in question.
232          * @return The entity on the stack frame or NULL, if the node does not have a
233          *         stack frame entity.
234          */
235         ir_entity *(*get_frame_entity)(const ir_node *irn);
236
237         /**
238          * Set the entity on the stack frame this node depends on.
239          * @param self The this pointer.
240          * @param irn  The node in question.
241          * @param ent  The entity to set
242          */
243         void (*set_frame_entity)(ir_node *irn, ir_entity *ent);
244
245         /**
246          * Set the offset of a node carrying an entity on the stack frame.
247          * @param self The this pointer.
248          * @param irn  The node.
249          * @param offset The offset of the node's stack frame entity.
250          */
251         void (*set_frame_offset)(ir_node *irn, int offset);
252
253         /**
254          * Returns the delta of the stackpointer for nodes that increment or
255          * decrement the stackpointer with a constant value. (push, pop
256          * nodes on most architectures).
257          * A positive value stands for an expanding stack area, a negative value for
258          * a shrinking one.
259          *
260          * @param self      The this pointer
261          * @param irn       The node
262          * @return          0 if the stackpointer is not modified with a constant
263          *                  value, otherwise the increment/decrement value
264          */
265         int (*get_sp_bias)(const ir_node *irn);
266
267         /**
268          * Returns an inverse operation which yields the i-th argument
269          * of the given node as result.
270          *
271          * @param self      The this pointer.
272          * @param irn       The original operation
273          * @param i         Index of the argument we want the inverse operation to yield
274          * @param inverse   struct to be filled with the resulting inverse op
275          * @param obstack   The obstack to use for allocation of the returned nodes array
276          * @return          The inverse operation or NULL if operation invertible
277          */
278         arch_inverse_t *(*get_inverse)(const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack);
279
280         /**
281          * Get the estimated cycle count for @p irn.
282          *
283          * @param self The this pointer.
284          * @param irn  The node.
285          *
286          * @return     The estimated cycle count for this operation
287          */
288         int (*get_op_estimated_cost)(const ir_node *irn);
289
290         /**
291          * Asks the backend whether operand @p i of @p irn can be loaded form memory internally
292          *
293          * @param self The this pointer.
294          * @param irn  The node.
295          * @param i    Index of the argument we would like to know whether @p irn can load it form memory internally
296          *
297          * @return     nonzero if argument can be loaded or zero otherwise
298          */
299         int (*possible_memory_operand)(const ir_node *irn, unsigned int i);
300
301         /**
302          * Ask the backend to assimilate @p reload of operand @p i into @p irn.
303          *
304          * @param self   The this pointer.
305          * @param irn    The node.
306          * @param spill  The spill.
307          * @param i      The position of the reload.
308          */
309         void (*perform_memory_operand)(ir_node *irn, ir_node *spill, unsigned int i);
310 };
311
312 /**
313  * The code generator interface.
314  */
315 struct arch_code_generator_if_t {
316         /**
317          * Initialize the code generator.
318          * @param birg A backend IRG session.
319          * @return     A newly created code generator.
320          */
321         void *(*init)(be_irg_t *birg);
322
323         /**
324          * return node used as base in pic code addresses
325          */
326         ir_node* (*get_pic_base)(void *self);
327
328         /**
329          * Called before abi introduce.
330          */
331         void (*before_abi)(void *self);
332
333         /**
334          * Called, when the graph is being normalized.
335          */
336         void (*prepare_graph)(void *self);
337
338         /**
339          * Backend may provide an own spiller.
340          * This spiller needs to spill all register classes.
341          */
342         void (*spill)(void *self, be_irg_t *birg);
343
344         /**
345          * Called before scheduling.
346          */
347         void (*before_sched)(void *self);
348
349         /**
350          * Called before register allocation.
351          */
352         void (*before_ra)(void *self);
353
354         /**
355          * Called after register allocation.
356          */
357         void (*after_ra)(void *self);
358
359         /**
360          * Called directly before done is called. This should be the last place
361          * where the irg is modified.
362          */
363         void (*finish)(void *self);
364
365         /**
366          * Called after everything happened. This call should emit the final
367          * assembly code but avoid changing the irg.
368          * The code generator must also be de-allocated here.
369          */
370         void (*done)(void *self);
371 };
372
373 /**
374  * helper macro: call function func from the code generator
375  * if it's implemented.
376  */
377 #define _arch_cg_call(cg, func) \
378 do { \
379         if((cg)->impl->func) \
380                 (cg)->impl->func(cg); \
381 } while(0)
382
383 #define _arch_cg_call_env(cg, env, func) \
384 do { \
385         if((cg)->impl->func) \
386                 (cg)->impl->func(cg, env); \
387 } while(0)
388
389 #define arch_code_generator_before_abi(cg)      _arch_cg_call(cg, before_abi)
390 #define arch_code_generator_prepare_graph(cg)   _arch_cg_call(cg, prepare_graph)
391 #define arch_code_generator_before_sched(cg)    _arch_cg_call(cg, before_sched)
392 #define arch_code_generator_before_ra(cg)       _arch_cg_call(cg, before_ra)
393 #define arch_code_generator_after_ra(cg)        _arch_cg_call(cg, after_ra)
394 #define arch_code_generator_finish(cg)          _arch_cg_call(cg, finish)
395 #define arch_code_generator_done(cg)            _arch_cg_call(cg, done)
396 #define arch_code_generator_spill(cg, birg)     _arch_cg_call_env(cg, birg, spill)
397 #define arch_code_generator_has_spiller(cg)     ((cg)->impl->spill != NULL)
398 #define arch_code_generator_get_pic_base(cg)    \
399         ((cg)->impl->get_pic_base != NULL ? (cg)->impl->get_pic_base(cg) : NULL)
400
401 /**
402  * Code generator base class.
403  */
404 struct arch_code_generator_t {
405         const arch_code_generator_if_t *impl;
406 };
407
408 /**
409  * Architecture interface.
410  */
411 struct arch_isa_if_t {
412         /**
413          * Initialize the isa interface.
414          * @param file_handle  the file handle to write the output to
415          * @param main_env     the be main environment
416          * @return a new isa instance
417          */
418         arch_env_t *(*init)(FILE *file_handle);
419
420         /**
421          * Free the isa instance.
422          */
423         void (*done)(void *self);
424
425         /**
426          * Get the the number of register classes in the isa.
427          * @return The number of register classes.
428          */
429         unsigned (*get_n_reg_class)(const void *self);
430
431         /**
432          * Get the i-th register class.
433          * @param i The number of the register class.
434          * @return The register class.
435          */
436         const arch_register_class_t *(*get_reg_class)(const void *self, unsigned i);
437
438         /**
439          * Get the register class which shall be used to store a value of a given mode.
440          * @param self The this pointer.
441          * @param mode The mode in question.
442          * @return A register class which can hold values of the given mode.
443          */
444         const arch_register_class_t *(*get_reg_class_for_mode)(const void *self, const ir_mode *mode);
445
446         /**
447          * Get the ABI restrictions for procedure calls.
448          * @param self        The this pointer.
449          * @param call_type   The call type of the method (procedure) in question.
450          * @param p           The array of parameter locations to be filled.
451          */
452         void (*get_call_abi)(const void *self, ir_type *call_type, be_abi_call_t *abi);
453
454         /**
455          * Get the code generator interface.
456          * @param self The this pointer.
457          * @return     Some code generator interface.
458          */
459         const arch_code_generator_if_t *(*get_code_generator_if)(void *self);
460
461         /**
462          * Get the list scheduler to use. There is already a selector given, the
463          * backend is free to modify and/or ignore it.
464          *
465          * @param self     The isa object.
466          * @param selector The selector given by options.
467          * @return         The list scheduler selector.
468          */
469         const list_sched_selector_t *(*get_list_sched_selector)(const void *self, list_sched_selector_t *selector);
470
471         /**
472          * Get the ILP scheduler to use.
473          * @param self  The isa object.
474          * @return      The ILP scheduler selector
475          */
476         const ilp_sched_selector_t *(*get_ilp_sched_selector)(const void *self);
477
478         /**
479          * Get the necessary alignment for storing a register of given class.
480          * @param self  The isa object.
481          * @param cls   The register class.
482          * @return      The alignment in bytes.
483          */
484         int (*get_reg_class_alignment)(const void *self, const arch_register_class_t *cls);
485
486         /**
487          * A "static" function, returns the frontend settings
488          * needed for this backend.
489          */
490         const backend_params *(*get_params)(void);
491
492         /**
493          * Returns an 2-dim array of execution units, @p irn can be executed on.
494          * The first dimension is the type, the second the allowed units of this
495          * 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 can 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          * mark node as rematerialized
527          */
528         void (*mark_remat)(const void *self, ir_node *node);
529
530         /**
531          * parse an assembler constraint part and set flags according to its nature
532          * advances the *c pointer to point to the last parsed character (so if you
533          * parse a single character don't advance c)
534          */
535         asm_constraint_flags_t (*parse_asm_constraint)(const void *self, const char **c);
536
537         /**
538          * returns true if the string is a valid clobbered (register) in this
539          * backend
540          */
541         int (*is_valid_clobber)(const void *self, const char *clobber);
542 };
543
544 #define arch_env_done(env)                             ((env)->impl->done(env))
545 #define arch_env_get_n_reg_class(env)                  ((env)->impl->get_n_reg_class(env))
546 #define arch_env_get_reg_class(env,i)                  ((env)->impl->get_reg_class(env, i))
547 #define arch_env_get_reg_class_for_mode(env,mode)      ((env)->impl->get_reg_class_for_mode((env), (mode)))
548 #define arch_env_get_call_abi(env,tp,abi)              ((env)->impl->get_call_abi((env), (tp), (abi)))
549 #define arch_env_get_code_generator_if(env)            ((env)->impl->get_code_generator_if((env)))
550 #define arch_env_get_list_sched_selector(env,selector) ((env)->impl->get_list_sched_selector((env), (selector)))
551 #define arch_env_get_ilp_sched_selector(env)           ((env)->impl->get_ilp_sched_selector(env))
552 #define arch_env_get_reg_class_alignment(env,cls)      ((env)->impl->get_reg_class_alignment((env), (cls)))
553 #define arch_env_get_params(env)                       ((env)->impl->get_params())
554 #define arch_env_get_allowed_execution_units(env,irn)  ((env)->impl->get_allowed_execution_units((env), (irn)))
555 #define arch_env_get_machine(env)                      ((env)->impl->get_machine(env))
556 #define arch_env_get_backend_irg_list(env,irgs)        ((env)->impl->get_backend_irg_list((env), (irgs)))
557 #define arch_env_parse_asm_constraint(env,c)           ((env)->impl->parse_asm_constraint((env), (c))
558 #define arch_env_is_valid_clobber(env,clobber)         ((env)->impl->is_valid_clobber((env), (clobber))
559 #define arch_env_mark_remat(env,node) \
560         do { if ((env)->impl->mark_remat != NULL) (env)->impl->mark_remat((env), (node)); } while(0)
561
562 /**
563  * ISA base class.
564  */
565 struct arch_env_t {
566         const arch_isa_if_t   *impl;
567         const arch_register_t *sp;              /** The stack pointer register. */
568         const arch_register_t *bp;              /** The base pointer register. */
569         int                    stack_dir;       /** -1 for decreasing, 1 for increasing. */
570         int                    stack_alignment; /** power of 2 stack alignment */
571         const be_main_env_t   *main_env;        /** the be main environment */
572         int                    spill_cost;      /** cost for a be_Spill node */
573         int                    reload_cost;     /** cost for a be_Reload node */
574 };
575
576 #define arch_env_stack_dir(env)  ((env)->stack_dir)
577 #define arch_env_sp(env)         ((env)->sp)
578 #define arch_env_bp(env)         ((env)->bp)
579
580 #endif /* FIRM_BE_BEARCH_T_H */