removed broken include
[libfirm] / ir / be / bearch.h
1 #ifndef _FIRM_BEARCH_H
2 #define _FIRM_BEARCH_H
3
4 #include "firm_config.h"
5
6 #include "irnode.h"
7 #include "irmode.h"
8
9 #include "bitset.h"
10 #include "hashptr.h"
11 #include "fourcc.h"
12 #include "set.h"
13 #include "list.h"
14 #include "ident.h"
15
16 typedef struct _arch_register_class_t   arch_register_class_t;
17 typedef struct _arch_register_t         arch_register_t;
18 typedef struct _arch_enum_t             arch_enum_t;
19 typedef struct _arch_enum_member_t      arch_enum_member_t;
20 typedef struct _arch_isa_if_t           arch_isa_if_t;
21 typedef struct _arch_env_t              arch_env_t;
22 typedef struct _arch_irn_ops_t          arch_irn_ops_t;
23 typedef struct _arch_irn_handler_t      arch_irn_handler_t;
24
25 struct _be_node_factory_t;
26
27 typedef enum _arch_register_type_t {
28   arch_register_type_none = 0,
29   arch_register_type_write_invariant,
30   arch_register_type_caller_saved,    /**< The register must be saved by the caller
31                                            upon a function call. It thus can be overwritten
32                                            in the called function. */
33   arch_register_type_callee_saved,    /**< The register must be saved by the called function,
34                                            it thus survives a function call. */
35   arch_register_type_ignore           /**< Do not consider this register when allocating. */
36 } arch_register_type_t;
37
38 /**
39  * A register.
40  */
41 struct _arch_register_t {
42   const char *name;                         /**< The name of the register. */
43   const arch_register_class_t *reg_class;   /**< The class the register belongs to. */
44   int index;                                /**< The index of the register in the class. */
45   arch_register_type_t type;                /**< The type of the register. */
46   void *data;                               /**< Custom data. */
47 };
48
49 static INLINE const arch_register_class_t *
50 _arch_register_get_class(const arch_register_t *reg)
51 {
52   return reg->reg_class;
53 }
54
55 static INLINE int _arch_register_get_index(const arch_register_t *reg)
56 {
57   return reg->index;
58 }
59
60 #define arch_register_get_class(reg)      _arch_register_get_class(reg)
61 #define arch_register_get_index(reg)      _arch_register_get_index(reg)
62 #define arch_register_get_name(reg)       ((reg)->name)
63
64 /**
65  * A class of registers.
66  * Like general purpose or floating point.
67  */
68 struct _arch_register_class_t {
69   const char *name;               /**< The name of the register class. */
70   int n_regs;                     /**< Number of registers in this class. */
71   const arch_register_t *regs;    /**< The array of registers. */
72 };
73
74 #define arch_register_class_n_regs(cls) ((cls)->n_regs)
75
76 /**
77  * Put all registers in a class into a bitset.
78  * @param cls The class.
79  * @param bs The bitset. May be NULL.
80  * @return The number of registers in the class.
81  */
82 extern int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs);
83
84 static INLINE const arch_register_t *
85 _arch_register_for_index(const arch_register_class_t *cls, int idx)
86 {
87   assert(0 <= idx && idx < cls->n_regs);
88   return &cls->regs[idx];
89 }
90
91 #define arch_register_for_index(cls, idx) \
92   _arch_register_for_index(cls, idx)
93
94 /**
95  * Get the register set for a register class.
96  * @param cls The register class.
97  * @return The set containing all registers in the class.
98  */
99 #define arch_get_register_set_for_class(cls) ((cls)->set)
100
101 /**
102  * An immediate.
103  */
104 struct _arch_immediate_t {
105   const char *name;         /**< The name of the immediate. */
106   ir_mode *mode;            /**< The mode of the immediate. */
107 };
108
109 /**
110  * The member of an enum.
111  */
112 struct _arch_enum_member_t {
113   arch_enum_t *enm;         /**< The enum, this member belongs to. */
114 };
115
116 /**
117  * An enumeration operand type.
118  *
119  * Enumeration operand types can be used to describe the variants
120  * of an instruction, like giving the cases for a compare (gt, lt,
121  * eq, ...) some other special attributes of an instruction.
122  */
123 struct _arch_enum_t {
124   int n_members;                    /**< The number of members in this enum. */
125   arch_enum_member_t *members[1];   /**< The array of members. */
126 };
127
128 typedef enum _arch_operand_type_t {
129   arch_operand_type_invalid,
130   arch_operand_type_memory,
131   arch_operand_type_register,
132   arch_operand_type_immediate,
133   arch_operand_type_symconst,
134   arch_operand_type_last
135 } arch_operand_type_t;
136
137 /**
138  * Different types of register allocation requirements.
139  */
140 typedef enum _arch_register_req_type_t {
141   arch_register_req_type_none = 0,        /**< No register requirement. */
142
143   arch_register_req_type_normal = 1,      /**< All registers in the class
144                                                are allowed. */
145
146   arch_register_req_type_limited = 2,     /**< Only a real subset of
147                                                the class is allowed. */
148
149   arch_register_req_type_equal = 4,       /**< The register must equal
150                                                another one at the node. */
151
152   arch_register_req_type_unequal = 8,     /**< The register must be unequal
153                                                to some other at the node. */
154
155   arch_register_req_type_pair = 16        /**< The register is part of a
156                                                register pair. */
157 } arch_register_req_type_t;
158
159 #define arch_register_req_is_constr(x) \
160   ((x)->type & (arch_register_req_type_pair + arch_register_req_type_limited - 1) != 0)
161
162 /**
163  * Expresses requirements to register allocation for an operand.
164  */
165 typedef struct _arch_register_req_t {
166   arch_register_req_type_t type;          /**< The type of the constraint. */
167   const arch_register_class_t *cls;       /**< The register class this
168                                                constraint belongs to. */
169   union {
170     int (*limited)(const ir_node *irn, int pos, bitset_t *bs);
171                                           /**< In case of the 'limited'
172                                             constraint, this function
173                                             must put all allowable
174                                             registers in the bitset and
175                                             return the number of registers
176                                             in the bitset. */
177
178     int pos;                             /**< In case of the equal constraint,
179                                             this gives the position of the
180                                             operand to which the register of
181                                             this should be equal to. Same for
182                                             unequal. */
183   } data;
184 } arch_register_req_t;
185
186 /**
187  * Certain node classes which are relevent for the register allocator.
188  */
189 typedef enum _arch_irn_class_t {
190   arch_irn_class_normal,
191   arch_irn_class_spill,
192   arch_irn_class_reload,
193   arch_irn_class_copy,
194   arch_irn_class_perm,
195   arch_irn_class_branch
196 } arch_irn_class_t;
197
198 /**
199  * Some flags describing a node in more detail.
200  */
201 typedef enum _arch_irn_flags_t {
202   arch_irn_flags_spillable = 1,
203   arch_irn_flags_rematerializable = 2
204 } arch_irn_flags_t;
205
206 /*
207  * Some words about positions and indices:
208  *
209  * Firm has the policy "One node per value", that's why there are
210  * Proj nodes. This view has its advantages, but in a backend
211  * setting where we talk about instructions (which can also have
212  * multiple results and not a single Tuple value) this is sometimes
213  * hard.
214  *
215  * Each node representing an instruction must provide information
216  * about the kind of its operands (where operands mean both input
217  * and output operands). Such an operand is addressed with a position
218  * which is infact a tuple {in, out} x N. The fact that a position
219  * is an input/output operand is encoded in the sign, so input operands
220  * go from 0..n-1 and output operands from -1..-m if the
221  * instruction has n input and m output operands.
222  */
223
224 #define _BEARCH_TRANSFORM_INDEX(cmp, index) ((index) cmp 0 ? -((index) + 1) : (index))
225
226 /**
227  * Make an in position from an index.
228  * @param index The index.
229  * @return The position representing the index as an in operand.
230  */
231 #define arch_pos_make_in(index)   _BEARCH_TRANSFORM_INDEX(<, index)
232
233 /**
234  * Make an out position from an index.
235  * @param index The index.
236  * @return The position representing the index as an out operand.
237  */
238 #define arch_pos_make_out(index)  _BEARCH_TRANSFORM_INDEX(>=, index)
239
240 /**
241  * Check, if a position denotes an input operand.
242  * @param pos The position.
243  * @return 1, if the position denotes an input operand 0 if not.
244  */
245 #define arch_pos_is_in(pos)       ((pos) >= 0)
246
247 /**
248  * Check, if a position denotes an output operand.
249  * @param pos The position.
250  * @return 1, if the position denotes an output operand 0 if not.
251  */
252 #define arch_pos_is_out(pos)      (!arch_pos_is_in(pos))
253
254 /**
255  * Get the index of a position.
256  * @param pos The position.
257  * @return The index of the position.
258  */
259 #define arch_pos_get_index(pos)   _BEARCH_TRANSFORM_INDEX(<, pos)
260
261 struct _arch_irn_ops_t {
262
263   /**
264    * Get the register requirements for a given operand.
265    * @param self The self pointer.
266    * @param irn The node.
267    * @param pos The operand's position.
268    * @return    The register requirements for the selected operand.
269    *            The pointer returned is never NULL.
270    */
271   const arch_register_req_t *(*get_irn_reg_req)(const arch_irn_ops_t *self,
272       arch_register_req_t *req,
273       const ir_node *irn, int pos);
274
275   /**
276    * Get the number of operands of a node.
277    * @param irn     The node.
278    * @param in_out  Denotes wither input (a number >= 0) or
279    *                output (a number < 0).
280    * @return        The number of operands for either in, or output.
281    */
282   int (*get_n_operands)(const arch_irn_ops_t *self, const ir_node *irn, int in_out);
283
284   /**
285    * Set the register for an output operand.
286    * @param irn The node.
287    * @param pos The position of the output operand.
288    * @param reg The register allocated to that operand.
289    * @note      If the operand is not a register operand,
290    *            the call is ignored.
291    */
292   void (*set_irn_reg)(const arch_irn_ops_t *self, ir_node *irn,
293       int idx, const arch_register_t *reg);
294
295   /**
296    * Get the register allocated for an output operand.
297    * @param irn The node.
298    * @param pos The index of the output operand.
299    * @return    The register allocated at that operand. NULL, if
300    *            the operand was no register operand or
301    *            @c arch_register_invalid, if no register has yet been
302    *            allocated for this node.
303    */
304   const arch_register_t *(*get_irn_reg)(const arch_irn_ops_t *self,
305       const ir_node *irn, int idx);
306
307   /**
308    * Classify the node.
309    * @param irn The node.
310    * @return A classification.
311    */
312   arch_irn_class_t (*classify)(const arch_irn_ops_t *self, const ir_node *irn);
313
314   /**
315    * Get the flags of a node.
316    * @param self The irn ops themselves.
317    * @param irn The node.
318    * @return A set of flags.
319    */
320   arch_irn_flags_t (*get_flags)(const arch_irn_ops_t *self, const ir_node *irn);
321
322 };
323
324 extern int
325 arch_get_n_operands(const arch_env_t *env, const ir_node *irm, int in_out);
326
327 /**
328  * Get the register requirements for a node.
329  * @param env The architecture environment.
330  * @param req A pointer to a requirements structure, where the data can
331  *            be put into.
332  * @param irn The node.
333  * @param pos The position of the operand you're interested in.
334  * @return    A pointer to the register requirements which may <b>not</b>
335  *            neccessarily be equal to @p req. If NULL is returned, the
336  *            operand was no register operand.
337  */
338 extern const arch_register_req_t *
339 arch_get_register_req(const arch_env_t *env, arch_register_req_t *req,
340     const ir_node *irn, int pos);
341
342 /**
343  * Check if an operand is a register operand.
344  * @param env The environment.
345  * @param irn The node.
346  * @param pos The position of the operand.
347  * @return 1, if the operand is significant for register allocation, 0
348  * if not.
349  */
350 extern int arch_is_register_operand(const arch_env_t *env,
351     const ir_node *irn, int pos);
352
353 /**
354  * Get the number of allocatable registers concerning
355  * a register class for an operand of a node.
356  * @param env The environment.
357  * @param irn The node.
358  * @param pos The postition of the node's operand.
359  * @param cls The register class.
360  * @param bs  The bitset all allocatable registers shall be put into.
361  *            Note, that you can also pass NULL here. If you don't,
362  *            make sure, the bitset is as large as the register class
363  *            has registers.
364  * @return    The amount of registers allocatable for that operand.
365  */
366 extern int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn,
367     int pos, const arch_register_class_t *cls, bitset_t *bs);
368
369 /**
370  * Check, if a register is assignable to an operand of a node.
371  * @param env The architecture environment.
372  * @param irn The node.
373  * @param pos The position of the operand.
374  * @param reg The register.
375  * @return    1, if the register might be allocated to the operand 0 if not.
376  */
377 extern int arch_reg_is_allocatable(const arch_env_t *env,
378     const ir_node *irn, int pos, const arch_register_t *reg);
379
380 /**
381  * Get the register class of an operand of a node.
382  * @param env The architecture environment.
383  * @param irn The node.
384  * @param pos The position of the operand.
385  * @return    The register class of the operand or NULL, if
386  *            operand is a non-register operand.
387  */
388 extern const arch_register_class_t *
389 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos);
390
391 /**
392  * Get the register allocated at a certain output operand of a node.
393  * @param env The arch nvironment.
394  * @param irn The node.
395  * @param idx The index of the output operand.
396  * @return    The register allocated for this operand
397  */
398 extern const arch_register_t *
399 arch_get_irn_register(const arch_env_t *env, const ir_node *irn, int idx);
400
401 /**
402  * Set the register for a certain output operand.
403  * @param env The architecture environment.
404  * @param irn The node.
405  * @param idx The index of the output operand.
406  * @param reg The register.
407  */
408 extern void arch_set_irn_register(const arch_env_t *env,
409     ir_node *irn, int idx, const arch_register_t *reg);
410
411 /**
412  * Classify a node.
413  * @param env The architecture environment.
414  * @param irn The node.
415  * @return A classification of the node.
416  */
417 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn);
418
419 /**
420  * Get the flags of a node.
421  * @param env The architecture environment.
422  * @param irn The node.
423  * @return The flags.
424  */
425 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn);
426
427 #define arch_irn_has_reg_class(env, irn, pos, cls) \
428   ((cls) == arch_get_irn_reg_class(env, irn, pos))
429
430 /**
431  * Somebody who can be asked about nodes.
432  */
433 struct _arch_irn_handler_t {
434
435   /**
436     * Get the operations of an irn.
437     * @param self The handler from which the method is invoked.
438     * @param irn Some node.
439     * @return Operations for that irn.
440     */
441   const arch_irn_ops_t *(*get_irn_ops)(const arch_irn_handler_t *handler,
442       const ir_node *irn);
443
444 };
445
446 /**
447  * Architecture interface.
448  */
449 struct _arch_isa_if_t {
450
451   /**
452    * Initialize the isa interface.
453    */
454   void (*init)(void);
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)(void);
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)(int i);
468
469   /**
470    * Prepare a graph.
471    * This function is called each time, the back end starts running over
472    * a graph.
473    * @param irg The graph.
474    */
475   void (*prepare_graph)(ir_graph *irg);
476
477   /**
478    * The irn handler for this architecture.
479    * The irn handler is registered by the Firm back end
480    * when the architecture is initialized.
481    * (May be NULL).
482    */
483   const arch_irn_handler_t *irn_handler;
484
485   /**
486    * The code generation function for this architecture.
487    * (May be NULL)
488    * @param out The output file
489    */
490   void (*codegen)(FILE *out);
491 };
492
493 #define ARCH_MAX_HANDLERS         8
494
495 /**
496  * Environment for the architecture infrastructure.
497  * Keep this everywhere you're going.
498  */
499 struct _arch_env_t {
500   const struct _be_node_factory_t *node_factory;  /**< The node factory for be nodes. */
501   const arch_isa_if_t *isa;               /**< The isa about which everything is. */
502
503   arch_irn_handler_t const *handlers[ARCH_MAX_HANDLERS]; /**< The handlers are organized as
504                                                            a stack. */
505
506   int handlers_tos;                                   /**< The stack pointer of the handler
507                                                         stack. */
508 };
509
510 /**
511  * Get the isa of an arch environment.
512  * @param env The environment.
513  * @return The isa with which the env was initialized with.
514  */
515 #define arch_env_get_isa(env)   ((env)->isa)
516
517 /**
518  * Initialize the architecture environment struct.
519  * @param isa The isa which shall be put into the environment.
520  * @return The environment.
521  */
522 extern arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa);
523
524 /**
525  * Add a node handler to the environment.
526  * @param env The environment.
527  * @param handler A node handler.
528  * @return The environment itself.
529  */
530 extern arch_env_t *arch_env_add_irn_handler(arch_env_t *env,
531     const arch_irn_handler_t *handler);
532
533 #endif /* _FIRM_BEARCH_H */