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