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