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