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