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