config.h include added
[libfirm] / ir / be / bearch.h
1
2 #ifndef _FIRM_BEARCH_H
3 #define _FIRM_BEARCH_H
4
5 #include "firm_config.h"
6
7 #include "irop_t.h"
8 #include "irnode_t.h"
9 #include "irmode_t.h"
10
11 #include "hashptr.h"
12 #include "fourcc.h"
13 #include "set.h"
14 #include "list.h"
15 #include "ident.h"
16
17 #include "bearch.h"
18
19 #define ARCH_OBJ(name,x) typedef struct _arch_ ## name ## _t arch_ ## name ## _t;
20 #include "bearch_obj.def"
21 #undef ARCH_OBJ
22
23 struct _bitset_t;
24
25 typedef enum _arch_register_type_t {
26         arch_register_type_none = 0,
27   arch_register_type_write_invariant,
28         arch_register_type_caller_saved,    /**< The register must be saved by the caller
29                                         upon a function call. It thus can be overwritten
30                                                                                                                                                                                                 in the called function. */
31         arch_register_type_callee_saved,    /**< The register must be saved by the called function,
32                                         it thus survives a function call. */
33         arch_register_type_ignore           /**< Do not consider this register when allocating. */
34 } arch_register_type_t;
35
36 /**
37  * A register.
38  */
39 struct _arch_register_t {
40   const char *name;                         /**< The name of the register. */
41         const arch_register_class_t *reg_class;   /**< The class the register belongs to. */
42         int index;                                                                                                                              /**< The index of the register in the class. */
43         arch_register_type_t type;                /**< The type of the register. */
44 };
45
46 static INLINE const arch_register_class_t *
47 _arch_register_get_class(const arch_register_t *reg)
48 {
49   return reg->reg_class;
50 }
51
52 static INLINE int _arch_register_get_index(const arch_register_t *reg)
53 {
54   return reg->index;
55 }
56
57 #define arch_register_get_class(reg)      _arch_register_get_class(reg)
58 #define arch_register_get_index(reg)      _arch_register_get_index(reg)
59
60 /**
61  * A (sub-) set of registers.
62  */
63 struct _arch_register_set_t {
64         const struct _arch_register_class_t *reg_class;   /**< The register class for this set. */
65         int *regs;                                        /**< An array containing 0/1 at place i
66                                                                                                                                                                                                                         whether the register with index i is
67                                                                                                                                                                                                                         in the set or not. */
68 };
69
70 static INLINE int _arch_register_in_set(const arch_register_set_t *set,
71     const arch_register_t *reg)
72 {
73         if(reg->reg_class != set->reg_class)
74                 return 0;
75
76         return set->regs[reg->index];
77 }
78
79
80 /**
81  * A class of registers.
82  * Like general purpose or floating point.
83  */
84 struct _arch_register_class_t {
85   const char *name;         /**< The name of the register. */
86         arch_register_set_t *set; /**< A register set containing all registers
87                                                                                                                         in this class. */
88         int n_regs;                                                             /**< Number of registers in this class. */
89         const arch_register_t *regs; /**< The array of registers. */
90 };
91
92 #define arch_register_class_n_regs(cls) ((cls)->n_regs)
93
94 static INLINE const arch_register_t *
95 _arch_register_for_index(const arch_register_class_t *cls, int idx)
96 {
97         assert(0 <= idx && idx < cls->n_regs);
98         return &cls->regs[idx];
99 }
100
101 #define arch_register_for_index(cls, idx) \
102   _arch_register_for_index(cls, idx)
103
104 /**
105  * Get the register set for a register class.
106  * @param cls The register class.
107  * @return The set containing all registers in the class.
108  */
109 #define arch_get_register_set_for_class(cls) ((cls)->set)
110
111 /**
112  * An immediate.
113  */
114 struct _arch_immediate_t {
115   const char *name;         /**< The name of the immediate. */
116         ir_mode *mode;                                          /**< The mode of the immediate. */
117 };
118
119 /**
120  * The member of an enum.
121  */
122 struct _arch_enum_member_t {
123         arch_enum_t *enm;                                       /**< The enum, this member belongs to. */
124 };
125
126 /**
127  * An enumeration operand type.
128  *
129  * Enumeration operand types can be used to describe the variants
130  * of an instruction, like giving the cases for a compare (gt, lt,
131  * eq, ...) some other special attributes of an instruction.
132  */
133 struct _arch_enum_t {
134         int n_members;                                                                          /**< The number of members in this enum. */
135         arch_enum_member_t *members[1];         /**< The array of members. */
136 };
137
138 typedef enum _arch_operand_type_t {
139 #define ARCH_OPERAND_TYPE(name,size_in_irn) arch_operand_type_ ## name,
140 #include "bearch_operand_types.def"
141 #undef ARCH_OPERAND_TYPE
142         arch_operand_type_last
143 } arch_operand_type_t;
144
145
146
147 /**
148  * The data for the different flavours of operand types.
149  */
150 typedef union _arch_operand_data_t {
151         const arch_register_set_t *set;       /**< The set of valid registers is directly
152                                           given. Note, that if an insn has no constraints,
153                                           the set comprises all registers in the
154                                           register class. */
155
156         const arch_immediate_t *imm;          /**< If the operand is an immediate
157                                           operand, this describes the kind of
158                                           immediate. */
159
160         const arch_enum_t *enm;               /**< Some enumeration value. */
161
162         int same_as_pos;                      /**< 'Same as' position for equals. */
163 } arch_operand_data_t;
164
165 /**
166  * An operand to an instruction.
167  */
168 struct _arch_operand_t {
169         arch_operand_type_t type;                                                                       /**< The type of the operand. */
170         arch_operand_data_t data;                                                                       /**< The payload. */
171 };
172
173 /**
174  * An instruction format.
175  */
176 struct _arch_insn_format_t {
177         int n_in;                       /**< Number of in operands. */
178         int n_out;                      /**< Number of out operands. */
179
180         arch_operand_t * const *in_operands;    /**< In operands. */
181         arch_operand_t * const *out_operands;   /**< Out operands. */
182 };
183
184 /**
185  * An instruction.
186  */
187 struct _arch_insn_t {
188         const arch_insn_format_t *format;                       /**< The format of the instruction. */
189         ir_op *op;                                                                                                              /**< The firm opcode for this insn. */
190 };
191
192 #define arch_insn_is_allocatable(insn,reg) \
193   ((irn)->get_allocatable_regs(irn, cls, NULL) != 0)
194
195 /**
196  * Architecture interface.
197  */
198 struct _arch_isa_if_t {
199
200   /**
201    * Initialize the isa interface.
202    */
203   void (*init)(void);
204
205   /**
206    * Get the the number of register classes in the isa.
207    * @return The number of register classes.
208    */
209   int (*get_n_reg_class)(void);
210
211   /**
212    * Get the i-th register class.
213    * @param i The number of the register class.
214    * @return The register class.
215    */
216   const arch_register_class_t *(*get_reg_class)(int i);
217
218   /**
219    * Put all registers of a given class which are allocatable to a
220    * certain node into a bitset.
221    * The bitset contains the indices of the registers concerning
222    * the register class @p cls.
223    * @param irn The node.
224    * @param cls The register class.
225    * @param bs The bitset. Can be NULL.
226    * @return The number of registers which are allocatable at this node.
227    */
228   int (*get_allocatable_regs)(const ir_node *irn,
229       const arch_register_class_t *cls, struct _bitset_t *bs);
230
231   int (*is_reg_allocatable)(const ir_node *irn, const arch_register_t *reg);
232
233   /**
234    * Get the register class, the value of a node belongs to.
235    * @param irn The node.
236    * @return The register class of the registers which can hold the
237    * value of irn. If the node does not return a value, or possesses no
238    * suitable register class, NULL is returned.
239    */
240   const arch_register_class_t *(*get_irn_reg_class)(const ir_node *irn);
241
242
243
244   /**
245    * Get an op for a name.
246    * @note This method may not be implemented.
247    * @param name The name of the op.
248    * @return The op with that name.
249    */
250   ir_op *(*get_op_by_name)(const char *name);
251 };
252
253 /**
254  * Check, if the value of a node can be stored in a
255  * specific register class.
256  * @param isa The isa.
257  * @param irn The node.
258  * @param cls The register class.
259  * @return 1, if the value can be stored in the register class, 0 if
260  * not.
261  */
262 #define arch_isa_irn_has_reg_class(isa, irn, cls) \
263   ((isa)->get_irn_reg_class(irn) == (cls))
264
265 /**
266  * Check, if a register is allocatable for an irn.
267  * @param irn The node.
268  * @param reg The register.
269  * @return 1, if the register can be allocated to this node, 0 if not.
270  */
271 #define arch_isa_reg_is_allocatable(irn, reg) \
272   ((isa)->is_reg_allocatable(irn, reg))
273
274 #endif /* _FIRM_BEARCH_H */