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