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