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