0c0cf0d87675d517fb9b42405f1d949d19c88b2f
[libfirm] / ir / be / bearch.h
1 #ifndef _FIRM_BEARCH_H
2 #define _FIRM_BEARCH_H
3
4 #ifdef HAVE_CONFIG_H
5 #include "config.h"
6 #endif
7
8
9 #ifdef WITH_LIBCORE
10 #include <libcore/lc_opts.h>
11 #endif
12
13 #include "type.h"
14
15 #include "irnode.h"
16 #include "irmode.h"
17
18 #include "bitset.h"
19 #include "hashptr.h"
20 #include "fourcc.h"
21 #include "set.h"
22 #include "list.h"
23 #include "ident.h"
24
25 #include "belistsched.h"
26 #include "beabi_t.h"
27
28 typedef struct _arch_register_class_t     arch_register_class_t;
29 typedef struct _arch_register_t           arch_register_t;
30 typedef struct _arch_isa_if_t             arch_isa_if_t;
31 typedef struct _arch_isa_t                arch_isa_t;
32 typedef struct _arch_env_t                arch_env_t;
33 typedef struct _arch_irn_ops_if_t         arch_irn_ops_if_t;
34 typedef struct _arch_irn_ops_t            arch_irn_ops_t;
35 typedef struct _arch_irn_handler_t        arch_irn_handler_t;
36 typedef struct _arch_code_generator_t     arch_code_generator_t;
37 typedef struct _arch_code_generator_if_t  arch_code_generator_if_t;
38
39 struct _be_node_factory_t;
40
41 typedef enum _arch_register_type_t {
42   arch_register_type_none = 0,
43   arch_register_type_caller_save  = 1, /**< The register must be saved by the caller
44                                             upon a function call. It thus can be overwritten
45                                             in the called function. */
46   arch_register_type_callee_save  = 2, /**< The register must be saved by the caller
47                                             upon a function call. It thus can be overwritten
48                                             in the called function. */
49   arch_register_type_ignore = 4,       /**< Do not consider this register when allocating. */
50   arch_register_type_sp = 8,           /**< This register is the stack pointer of the architecture. */
51   arch_register_type_bp = 16,          /**< The register is the base pointer of the architecture. */
52 } arch_register_type_t;
53
54 /**
55  * Convenience macro to check for register type.
56  * @param req   A pointer to register.
57  * @param kind  The kind of type to check for (see arch_register_type_t).
58  * @return      1, If register is of given kind, 0 if not.
59  */
60 #define arch_register_type_is(reg, kind) \
61         ((reg)->type == arch_register_type_ ## kind)
62
63 /**
64  * A register.
65  */
66 struct _arch_register_t {
67   const char *name;                         /**< The name of the register. */
68   const arch_register_class_t *reg_class;   /**< The class the register belongs to. */
69   int index;                                /**< The index of the register in the class. */
70   arch_register_type_t type;                /**< The type of the register. */
71   void *data;                               /**< Custom data. */
72 };
73
74 static INLINE const arch_register_class_t *
75 _arch_register_get_class(const arch_register_t *reg)
76 {
77   return reg->reg_class;
78 }
79
80 static INLINE int _arch_register_get_index(const arch_register_t *reg)
81 {
82   return reg->index;
83 }
84
85 #define arch_register_get_class(reg)      _arch_register_get_class(reg)
86 #define arch_register_get_index(reg)      _arch_register_get_index(reg)
87 #define arch_register_get_name(reg)       ((reg)->name)
88
89 /**
90  * A class of registers.
91  * Like general purpose or floating point.
92  */
93 struct _arch_register_class_t {
94   const char *name;               /**< The name of the register class. */
95   int n_regs;                     /**< Number of registers in this class. */
96   ir_mode *mode;                  /**< The mode of the register class. */
97   const arch_register_t *regs;    /**< The array of registers. */
98 };
99
100 #define arch_register_class_n_regs(cls) ((cls)->n_regs)
101
102 /**
103  * Put all registers in a class into a bitset.
104  * @param cls The class.
105  * @param bs The bitset. May be NULL.
106  * @return The number of registers in the class.
107  */
108 extern int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs);
109
110 static INLINE const arch_register_t *
111 _arch_register_for_index(const arch_register_class_t *cls, int idx)
112 {
113   assert(0 <= idx && idx < cls->n_regs);
114   return &cls->regs[idx];
115 }
116
117 #define arch_register_for_index(cls, idx) \
118   _arch_register_for_index(cls, idx)
119
120 typedef enum _arch_operand_type_t {
121   arch_operand_type_invalid,
122   arch_operand_type_memory,
123   arch_operand_type_register,
124   arch_operand_type_immediate,
125   arch_operand_type_symconst,
126   arch_operand_type_last
127 } arch_operand_type_t;
128
129 /**
130  * Different types of register allocation requirements.
131  */
132 typedef enum _arch_register_req_type_t {
133   arch_register_req_type_none = 0,        /**< No register requirement. */
134
135   arch_register_req_type_normal = 1,      /**< All registers in the class
136                                                are allowed. */
137
138   arch_register_req_type_limited = 2,     /**< Only a real subset of
139                                                the class is allowed. */
140
141   arch_register_req_type_should_be_same = 4,       /**< The register should be equal
142                                                         another one at the node. */
143
144   arch_register_req_type_should_be_different = 8,  /**< The register must be unequal
145                                                         to some other at the node. */
146
147 } arch_register_req_type_t;
148
149 /**
150  * Convenience macro to check for set constraints.
151  * @param req   A pointer to register requirements.
152  * @param kind  The kind of constraint to check for (see arch_register_req_type_t).
153  * @return      1, If the kind of constraint is present, 0 if not.
154  */
155 #define arch_register_req_is(req, kind) \
156         (((req)->type & (arch_register_req_type_ ## kind)) != 0)
157
158 /**
159  * Expresses requirements to register allocation for an operand.
160  */
161 typedef struct _arch_register_req_t {
162         arch_register_req_type_t type;          /**< The type of the constraint. */
163         const arch_register_class_t *cls;       /**< The register class this constraint belongs to. */
164
165         void (*limited)(void *limited_env, bitset_t *bs);
166                                           /**< In case of the 'limited'
167                                             constraint, this function
168                                             must put all allowable
169                                             registers in the bitset and
170                                             return the number of registers
171                                             in the bitset. */
172
173         void *limited_env;                    /**< This must passed to limited. */
174
175         ir_node *other_same;                      /**< The other which shall have the same reg
176                                                                                     as this one. (for case should_be_same). */
177
178         ir_node *other_different;             /**< The other node from which this one's register
179                                                                                     must be different (case must_be_different). */
180 } arch_register_req_t;
181
182 /**
183  * Certain node classes which are relevant for the register allocator.
184  */
185 typedef enum _arch_irn_class_t {
186   arch_irn_class_normal,
187   arch_irn_class_spill,
188   arch_irn_class_reload,
189   arch_irn_class_copy,
190   arch_irn_class_perm,
191   arch_irn_class_branch,
192   arch_irn_class_call
193 } arch_irn_class_t;
194
195 /**
196  * Some flags describing a node in more detail.
197  */
198 typedef enum _arch_irn_flags_t {
199         arch_irn_flags_none             = 0, /**< Node flags. */
200         arch_irn_flags_dont_spill       = 1, /**< This must not be spilled. */
201         arch_irn_flags_rematerializable = 2, /**< This should be replicated instead of spilled/reloaded. */
202         arch_irn_flags_ignore           = 4, /**< Do not consider the node during register allocation. */
203 } arch_irn_flags_t;
204
205 struct _arch_irn_ops_if_t {
206
207   /**
208    * Get the register requirements for a given operand.
209    * @param self The self pointer.
210    * @param irn The node.
211    * @param pos The operand's position
212          *                                              (-1 for the result of the node, 0..n for the input
213          *                                              operands).
214    * @return    The register requirements for the selected operand.
215    *            The pointer returned is never NULL.
216    */
217   const arch_register_req_t *(*get_irn_reg_req)(const void *self,
218       arch_register_req_t *req, const ir_node *irn, int pos);
219
220   /**
221    * Set the register for an output operand.
222    * @param irn The node.
223    * @param reg The register allocated to that operand.
224    * @note      If the operand is not a register operand,
225    *            the call is ignored.
226    */
227   void (*set_irn_reg)(const void *self, ir_node *irn, const arch_register_t *reg);
228
229   /**
230    * Get the register allocated for an output operand.
231    * @param irn The node.
232    * @return    The register allocated at that operand. NULL, if
233    *            the operand was no register operand or
234    *            @c arch_register_invalid, if no register has yet been
235    *            allocated for this node.
236    */
237   const arch_register_t *(*get_irn_reg)(const void *self, const ir_node *irn);
238
239   /**
240    * Classify the node.
241    * @param irn The node.
242    * @return A classification.
243    */
244   arch_irn_class_t (*classify)(const void *self, const ir_node *irn);
245
246   /**
247    * Get the flags of a node.
248    * @param self The irn ops themselves.
249    * @param irn The node.
250    * @return A set of flags.
251    */
252   arch_irn_flags_t (*get_flags)(const void *self, const ir_node *irn);
253
254   /**
255    * Set a bias for the stack pointer.
256    * If the node in question uses the stack pointer for indexing, it must
257    * consider the value of <code>bias</code> additionally.
258    * @param self The this pointer.
259    * @param irn  The node in question.
260    * @param bias The bias.
261    */
262   void (*set_stack_bias)(const void *self, ir_node *irn, int bias);
263 };
264
265 /**
266  * irn_ops base class.
267  */
268 struct _arch_irn_ops_t {
269         const arch_irn_ops_if_t *impl;
270 };
271
272 extern void
273 arch_set_stack_bias(const arch_env_t *env, ir_node *irn, int bias);
274
275 /**
276  * Get the register requirements for a node.
277  * @param env The architecture environment.
278  * @param req A pointer to a requirements structure, where the data can
279  *            be put into.
280  * @param irn The node.
281  * @param pos The position of the operand you're interested in.
282  * @return    A pointer to the register requirements which may <b>not</b>
283  *            neccessarily be equal to @p req. If NULL is returned, the
284  *            operand was no register operand.
285  */
286 extern const arch_register_req_t *
287 arch_get_register_req(const arch_env_t *env, arch_register_req_t *req,
288     const ir_node *irn, int pos);
289
290 /**
291  * Check if an operand is a register operand.
292  * @param env The environment.
293  * @param irn The node.
294  * @param pos The position of the operand.
295  * @return 1, if the operand is significant for register allocation, 0
296  * if not.
297  */
298 extern int arch_is_register_operand(const arch_env_t *env,
299     const ir_node *irn, int pos);
300
301 /**
302  * Get the number of allocatable registers concerning
303  * a register class for an operand of a node.
304  * @param env The environment.
305  * @param irn The node.
306  * @param pos The postition of the node's operand.
307  * @param bs  The bitset all allocatable registers shall be put into.
308  *            Note, that you can also pass NULL here. If you don't,
309  *            make sure, the bitset is as large as the register class
310  *            has registers.
311  * @return    The amount of registers allocatable for that operand.
312  */
313 extern int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn, int pos, bitset_t *bs);
314
315 /**
316  * Put all registers which shall not be ignored by the register
317  * allocator in a bit set.
318  * @param env The arch env.
319  * @param cls The register class to consider.
320  * @param bs  The bit set to put the registers to.
321  */
322 extern void arch_put_non_ignore_regs(const arch_env_t *env, const arch_register_class_t *cls, bitset_t *bs);
323
324 /**
325  * Check, if a register is assignable to an operand of a node.
326  * @param env The architecture environment.
327  * @param irn The node.
328  * @param pos The position of the operand.
329  * @param reg The register.
330  * @return    1, if the register might be allocated to the operand 0 if not.
331  */
332 extern int arch_reg_is_allocatable(const arch_env_t *env,
333     const ir_node *irn, int pos, const arch_register_t *reg);
334
335 /**
336  * Get the register class of an operand of a node.
337  * @param env The architecture environment.
338  * @param irn The node.
339  * @param pos The position of the operand.
340  * @return    The register class of the operand or NULL, if
341  *            operand is a non-register operand.
342  */
343 extern const arch_register_class_t *
344 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos);
345
346 /**
347  * Get the register allocated at a certain output operand of a node.
348  * @param env The arch environment.
349  * @param irn The node.
350  * @return    The register allocated for this operand
351  */
352 extern const arch_register_t *
353 arch_get_irn_register(const arch_env_t *env, const ir_node *irn);
354
355 /**
356  * Set the register for a certain output operand.
357  * @param env The architecture environment.
358  * @param irn The node.
359  * @param idx The index of the output operand.
360  * @param reg The register.
361  */
362 extern void arch_set_irn_register(const arch_env_t *env, ir_node *irn,
363                 const arch_register_t *reg);
364
365 /**
366  * Classify a node.
367  * @param env The architecture environment.
368  * @param irn The node.
369  * @return A classification of the node.
370  */
371 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn);
372
373 /**
374  * Get the flags of a node.
375  * @param env The architecture environment.
376  * @param irn The node.
377  * @return The flags.
378  */
379 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn);
380
381 #define arch_irn_is_ignore(env, irn) ((arch_irn_get_flags(env, irn) & arch_irn_flags_ignore) != 0)
382
383 #define arch_irn_has_reg_class(env, irn, pos, cls) \
384   ((cls) == arch_get_irn_reg_class(env, irn, pos))
385
386 #define arch_irn_consider_in_reg_alloc(env, cls, irn) \
387         (arch_irn_has_reg_class(env, irn, -1, cls) && !arch_irn_is_ignore(env, irn))
388
389 /**
390  * Somebody who can be asked about nodes.
391  */
392 struct _arch_irn_handler_t {
393
394   /**
395     * Get the operations of an irn.
396     * @param self The handler from which the method is invoked.
397     * @param irn Some node.
398     * @return Operations for that irn.
399     */
400   const void *(*get_irn_ops)(const arch_irn_handler_t *handler,
401       const ir_node *irn);
402
403 };
404
405 /**
406  * The code generator.
407  */
408 struct _arch_code_generator_if_t {
409
410
411         /**
412          * Initialize the code generator.
413          * @param file The file to dump to.
414          * @param irg  The function to generate code for.
415          * @param env  The architecture environment.
416          * @return     A newly created code generator.
417          */
418         void *(*init)(FILE *file, ir_graph *irg, const arch_env_t *env);
419
420         /**
421          * Called, when the graph is being normalized.
422          */
423         void (*prepare_graph)(void *self);
424
425         /**
426          * Called before scheduling.
427          */
428         void (*before_sched)(void *self);
429
430         /**
431          * Called before register allocation.
432          */
433         void (*before_ra)(void *self);
434
435         /**
436          * Called after register allocation to lower Spills to Stores
437          * @param self  The code generator
438          * @param spill The spill node to lower
439          * @return      The new backend node which substitutes the spill
440          */
441         ir_node *(*lower_spill)(void *self, ir_node *spill);
442
443         /**
444          * Called after register allocation to lower Reloads to Loads
445          * @param self   The code generator
446          * @param reload The reload node to lower
447          * @return       The new backend node which substitutes the reload
448          */
449         ir_node *(*lower_reload)(void *self, ir_node *reload);
450
451         /**
452          * Called after everything happened.
453          * The code generator must also be de-allocated here.
454          */
455         void (*done)(void *self);
456
457 };
458
459 #define _arch_cg_call(cg, func) \
460 do { \
461         if((cg)->impl->func) \
462                 (cg)->impl->func(cg); \
463 } while(0)
464
465 #define arch_code_generator_prepare_graph(cg)   _arch_cg_call(cg, prepare_graph)
466 #define arch_code_generator_before_sched(cg)    _arch_cg_call(cg, before_sched)
467 #define arch_code_generator_before_ra(cg)       _arch_cg_call(cg, before_ra)
468 #define arch_code_generator_done(cg)            _arch_cg_call(cg, done)
469
470 /**
471  * Code generator base class.
472  */
473 struct _arch_code_generator_t {
474         const arch_code_generator_if_t *impl;
475 };
476
477 /**
478  * ISA base class.
479  */
480 struct _arch_isa_t {
481         const arch_isa_if_t *impl;
482         const arch_register_t *sp;  /** The stack pointer register. */
483         const arch_register_t *bp;  /** The base pointer register. */
484         const int stack_dir;        /** -1 for decreasing, 1 for increasing. */
485 };
486
487 #define arch_isa_stack_dir(isa)  ((isa)->stack_dir)
488 #define arch_isa_sp(isa)         ((isa)->sp)
489 #define arch_isa_bp(isa)         ((isa)->bp)
490
491 /**
492  * Architecture interface.
493  */
494 struct _arch_isa_if_t {
495
496 #ifdef WITH_LIBCORE
497   void (*register_options)(lc_opt_entry_t *grp);
498 #endif
499
500   /**
501    * Initialize the isa interface.
502    */
503   void *(*init)(void);
504
505   /**
506    * Free the isa instance.
507    */
508   void (*done)(void *self);
509
510   /**
511    * Get the the number of register classes in the isa.
512    * @return The number of register classes.
513    */
514   int (*get_n_reg_class)(const void *self);
515
516   /**
517    * Get the i-th register class.
518    * @param i The number of the register class.
519    * @return The register class.
520    */
521   const arch_register_class_t *(*get_reg_class)(const void *self, int i);
522
523   /**
524    * Get the register class which shall be used to store a value of a given mode.
525    * @param self The this pointer.
526    * @param mode The mode in question.
527    * @return A register class which can hold values of the given mode.
528    */
529   const arch_register_class_t *(*get_reg_class_for_mode)(const void *self, const ir_mode *mode);
530
531   /**
532    * Get the ABI restrictions for procedure calls.
533    * @param self        The this pointer.
534    * @param method_type The type of the method (procedure) in question.
535    * @param p           The array of parameter locations to be filled.
536    */
537   void (*get_call_abi)(const void *self, ir_type *method_type, be_abi_call_t *abi);
538
539   /**
540    * The irn handler for this architecture.
541    * The irn handler is registered by the Firm back end
542    * when the architecture is initialized.
543    * (May be NULL).
544    */
545   const arch_irn_handler_t *(*get_irn_handler)(const void *self);
546
547   /**
548    * Get the code generator interface.
549    * @param self The this pointer.
550    * @return     Some code generator interface.
551    */
552   const arch_code_generator_if_t *(*get_code_generator_if)(void *self);
553
554   /**
555    * Get the list scheduler to use.
556    * @param self  The isa object.
557    * @return      The list scheduler selector.
558    */
559   const list_sched_selector_t *(*get_list_sched_selector)(const void *self);
560
561   /**
562    * Take a proj from a call, set the correct register and projnum for this proj
563    * @param self    The isa object.
564    * @param proj    The proj
565    * @param is_keep Non-zero if proj is a Keep argument
566    * @return        The backend proj number assigned to this proj
567    */
568   long (*handle_call_proj)(const void *self, ir_node *proj, int is_keep);
569 };
570
571 #define arch_isa_get_n_reg_class(isa)                       ((isa)->impl->get_n_reg_class(isa))
572 #define arch_isa_get_reg_class(isa,i)                       ((isa)->impl->get_reg_class(isa, i))
573 #define arch_isa_get_irn_handler(isa)                               ((isa)->impl->get_irn_handler(isa))
574 #define arch_isa_get_call_abi(isa,tp,abi)                   ((isa)->impl->get_call_abi((isa), (tp), (abi)))
575 #define arch_isa_get_reg_class_for_mode(isa,mode)           ((isa)->impl->get_reg_class_for_mode((isa), (mode)))
576 #define arch_isa_make_code_generator(isa,irg)               ((isa)->impl->make_code_generator(isa, irg))
577
578 #define ARCH_MAX_HANDLERS         8
579
580 /**
581  * Environment for the architecture infrastructure.
582  * Keep this everywhere you're going.
583  */
584 struct _arch_env_t {
585   const struct _be_node_factory_t *node_factory;  /**< The node factory for be nodes. */
586   arch_isa_t *isa;                                /**< The isa about which everything is. */
587
588   arch_irn_handler_t const *handlers[ARCH_MAX_HANDLERS]; /**< The handlers are organized as
589                                                            a stack. */
590
591   int handlers_tos;                                   /**< The stack pointer of the handler
592                                                         stack. */
593 };
594
595 /**
596  * Get the isa of an arch environment.
597  * @param env The environment.
598  * @return The isa with which the env was initialized with.
599  */
600 #define arch_env_get_isa(env)   ((env)->isa)
601
602 /**
603  * Initialize the architecture environment struct.
604  * @param isa The isa which shall be put into the environment.
605  * @return The environment.
606  */
607 extern arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa);
608
609 /**
610  * Add a node handler to the environment.
611  * @param env The environment.
612  * @param handler A node handler.
613  * @return The environment itself.
614  */
615 extern arch_env_t *arch_env_add_irn_handler(arch_env_t *env, const arch_irn_handler_t *handler);
616
617 #endif /* _FIRM_BEARCH_H */