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