383879ab227de0e5d971ff2885d753cf6a51903e
[libfirm] / ir / be / bearch_t.h
1
2 #ifndef _FIRM_BEARCH_T_H
3 #define _FIRM_BEARCH_T_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_IRN_FOURCC         FOURCC('A', 'R', 'C', 'H')
20
21 /**
22  * Flags for registers.
23  */
24 enum {
25
26         /**
27          * The register is invariant concerning writes.
28          * Examples are the 0 registers in RISC architectures.
29          */
30         REG_WRITE_INVARIAT                              = 1
31
32 } arch_register_flags_t;
33
34 typedef enum {
35 #define ARCH_OBJ(x,list)        arch_kind_##x,
36 #include "bearch_obj.def"
37 #undef ARCH_OBJ
38         arch_kind_last
39 } arch_kind_t;
40
41 /**
42  * A header which each of the arch structs should posess.
43  */
44 typedef struct {
45         arch_kind_t kind;
46         arch_isa_t *isa;
47         const char *name;
48         struct list_head list;
49         unsigned is_new : 1;
50 } arch_header_t;
51
52
53 /**
54  * Get the architecture an arch object belongs to.
55  * @param obj The object.
56  * @return The architecture it belongs to.
57  */
58 static INLINE arch_isa_t *arch_obj_get_isa(const void *obj)
59 {
60         return ((const arch_header_t *) obj)->isa;
61 }
62
63 typedef enum _arch_register_flag_t {
64         arch_register_flag_none,
65         arch_register_flag_caller_saved,                                                /**< The register must be saved by the caller
66                                                                                                                                                                                                 upon a function call. It thus can be overwritten
67                                                                                                                                                                                                 in the called function. */
68         arch_register_flag_callee_saved,                                                /**< The register must be saved by the called function,
69                                                                                                                                                                                                 it thus survives a function call. */
70         arch_register_flag_ignore                                                                               /**< Do not consider this register when allocating. */
71 } arch_register_flag_t;
72
73 /**
74  * A register.
75  */
76 struct _arch_register_t {
77         arch_header_t header;
78         int index;                                                                                                                              /**< The index of the register in the class. */
79         const arch_register_class_t *reg_class;         /**< The class the register belongs to. */
80         arch_register_flag_t flags;                                                             /**< Flags describing several properties of
81                                                                                                                                                                                         the register. */
82 };
83
84 /**
85  * A (sub-) set of registers.
86  */
87 struct _arch_register_set_t {
88         arch_header_t header;
89         const struct _arch_register_class_t *reg_class;         /**< The register class for this set. */
90         unsigned comprises_full_class : 1;                                                              /**< True, if all registers of the class
91                                                                                                                                                                                                                         are contained in this set. */
92         int regs[1];                                                                                                                                                    /**< An array containing 0/1 at place i
93                                                                                                                                                                                                                         whether the register with index i is
94                                                                                                                                                                                                                         in the set or not. */
95 };
96
97 static INLINE int _arch_register_in_set(const arch_register_set_t *set, const arch_register_t *reg)
98 {
99         if(reg->reg_class != set->reg_class)
100                 return 0;
101
102         return set->regs[reg->index];
103 }
104
105
106 /**
107  * A class of registers.
108  * Like general purpose or floating point.
109  */
110 struct _arch_register_class_t {
111         arch_header_t header;
112         struct list_head list;          /**< list head to list up in the list of all
113                                                                                                                         register classes in an isa. */
114         arch_register_set_t *set; /**< A register set containing all registers
115                                                                                                                         in this class. */
116         int n_regs;                                                             /**< Number of registers in this class. */
117         arch_register_t *regs[1]; /**< The array of registers. */
118 };
119
120 static INLINE const arch_register_t *_arch_register_for_index(const arch_register_class_t *cls, int idx)
121 {
122         assert(0 <= idx && idx <= cls->n_regs);
123         return cls->regs[idx];
124 }
125
126 /**
127  * Get the register set for a register class.
128  * @param cls The register class.
129  * @return The set containing all registers in the class.
130  */
131 static INLINE arch_register_set_t *_arch_get_register_set_for_class(const arch_register_class_t *cls)
132 {
133         return cls->set;
134 }
135
136 /**
137  * An immediate.
138  */
139 struct _arch_immediate_t {
140         arch_header_t header;
141         ir_mode *mode;                                          /**< The mode of the immediate. */
142 };
143
144 /**
145  * The member of an enum.
146  */
147 struct _arch_enum_member_t {
148         arch_header_t header;                   /**< The omnipresent header. */
149         arch_enum_t *enm;                                       /**< The enum, this member belongs to. */
150 };
151
152 /**
153  * An enumeration operand type.
154  *
155  * Enumeration operand types can be used to describe the variants
156  * of an instruction, like giving the cases for a compare (gt, lt,
157  * eq, ...) some other special attributes of an instruction.
158  */
159 struct _arch_enum_t {
160         arch_header_t header;
161         int n_members;                                                                          /**< The number of members in this enum. */
162         arch_enum_member_t *members[1];         /**< The array of members. */
163 };
164
165 typedef enum _arch_operand_type_t {
166         arch_operand_type_ir = 0,
167         arch_operand_type_variadic,
168         arch_operand_type_symconst,
169         arch_operand_type_register_set,
170         arch_operand_type_immediate
171 } arch_operand_type_t;
172
173
174
175 /**
176  * The data for the different flavours of operand types.
177  */
178 typedef union _arch_operand_data_t {
179         arch_register_callback_t *callback;                             /**< The set of valid registers is determined
180                                                                                                                                                                                         by a callback function. */
181
182         const arch_register_set_t *set;                                         /**< The set of valid registers is directly
183                                                                                                                                                                                         given. Note, that if an insn has no constraints,
184                                                                                                                                                                                         the set comprises all registers in the
185                                                                                                                                                                                         register class. */
186
187         const arch_immediate_t *imm;                                                    /**< If the operand is an immediate
188                                                                                                                                                                                         operand, this describes the kind of
189                                                                                                                                                                                         immediate. */
190
191         const arch_enum_t *enm;                                                                         /**< Some enumeration value. */
192 } arch_operand_data_t;
193
194 /**
195  * An operand to an instruction.
196  */
197 struct _arch_operand_t {
198         int offset_in_irn_data;
199         arch_operand_type_t type;                                                                       /**< The type of the operand. */
200         arch_operand_data_t data;                                                                       /**< The payload. */
201 };
202
203 /**
204  * An instruction format.
205  */
206 struct _arch_insn_format_t {
207         arch_header_t header;
208         int n_in;                                                                                                                                       /**< Number of in operands. */
209         int n_out;                                                                                                                              /**< Number of out operands. */
210         int irn_data_size;
211
212         arch_operand_t operands[1];     /**< Array with operands. */
213 };
214
215 #define arch_get_in_operand(fmt, index)                 (&((fmt)->operands[(fmt)->n_out + (index)]))
216 #define arch_get_out_operand(fmt, index)                (&((fmt)->operands[index]))
217
218 /**
219  * An instruction.
220  */
221 struct _arch_insn_t {
222         arch_header_t header;
223         const arch_insn_format_t *format;                       /**< The format of the instruction. */
224         ir_op *op;                                                                                                              /**< The firm opcode for this insn. */
225 };
226
227 /**
228  * This truct is placed into each ir_node which is made from an arch
229  * insn (If the node is made via arch_new node).
230  */
231 typedef struct _arch_irn_data_t {
232         unsigned magic;                                                                                         /**< A magic number to tell if node is an
233                                                                                                                                                                         arch node. */
234         const arch_insn_t *insn;                                                        /**< The insn this nodes instantiates. */
235 } arch_irn_data_t;
236
237 #define _arch_get_irn_data(irn) ((const arch_irn_data_t *) &((irn)->attr))
238
239 /**
240  * Check, if an ir node is made by the arch module.
241  * @param irn An ir node.
242  * @return 1 if the node was made via arch_new_node() or 0 otherwise.
243  */
244 static INLINE int _arch_is_irn(const ir_node *irn)
245 {
246         return _arch_get_irn_data(irn)->magic == ARCH_IRN_FOURCC;
247 }
248
249 static INLINE const arch_insn_t *_arch_irn_get_insn(const ir_node *irn)
250 {
251         if(!_arch_is_irn(irn))
252                 return NULL;
253
254         return _arch_get_irn_data(irn)->insn;
255 }
256
257
258 /**
259  * An instruction set architecture.
260  */
261 struct _arch_isa_t {
262         arch_header_t header;
263         struct list_head heads[arch_kind_last];         /**< List heads to list objects created in the
264                                                                                                                                                                                         context of this isa. Note: some of the list heads
265                                                                                                                                                                                         remain unused. */
266 };
267
268 struct _arch_implementation_t {
269         const arch_isa_t *isa;
270         const char *name;
271 };
272
273
274 #endif /* _FIRM_BEARCH_T_H */