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