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