0bac0461c6b0bef10ac90c17f93803a34a93f97c
[libfirm] / ir / be / bearch.h
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Processor architecture specification.
23  * @author      Sebastian Hack
24  * @version     $Id$
25  */
26 #ifndef FIRM_BE_BEARCH_H
27 #define FIRM_BE_BEARCH_H
28
29 #include "firm_types.h"
30 #include "bitset.h"
31 #include "be.h"
32 #include "obst.h"
33
34 typedef struct arch_register_class_t     arch_register_class_t;
35 typedef struct arch_register_req_t       arch_register_req_t;
36 typedef struct arch_register_t           arch_register_t;
37 typedef struct arch_flag_t               arch_flag_t;
38 typedef struct arch_inverse_t            arch_inverse_t;
39 typedef struct arch_isa_if_t             arch_isa_if_t;
40 typedef struct arch_isa_t                arch_isa_t;
41 typedef struct arch_env_t                arch_env_t;
42 typedef struct arch_code_generator_t     arch_code_generator_t;
43 typedef struct arch_code_generator_if_t  arch_code_generator_if_t;
44
45 typedef enum arch_register_class_flags_t {
46         arch_register_class_flag_none      = 0,
47         arch_register_class_flag_manual_ra = 1,  /**< don't do automatic register allocation for this class */
48         arch_register_class_flag_state     = 2
49 } arch_register_class_flags_t;
50
51 typedef enum arch_register_type_t {
52         arch_register_type_none         = 0,
53         arch_register_type_caller_save  = 1,  /**< The register must be saved by the caller
54                                              upon a function call. It thus can be overwritten
55                                              in the called function. */
56         arch_register_type_callee_save  = 2,  /**< The register must be saved by the caller
57                                              upon a function call. It thus can be overwritten
58                                              in the called function. */
59         arch_register_type_ignore       = 4,  /**< Do not consider this register when allocating. */
60         arch_register_type_joker        = 8,  /**< The emitter can choose an arbitrary register */
61         arch_register_type_virtual      = 16, /**< This is just a virtual register.Virtual registers have
62                                                nearly no constraints, it is a allowed to have multiple
63                                                                                            definition for the same register at a point) */
64         arch_register_type_state        = 32, /**< The register represents a state that should be handled by
65                                                    bestate code */
66 } arch_register_type_t;
67
68 /**
69  * Put all registers in a class into a bitset.
70  * @param cls The class.
71  * @param bs The bitset. May be NULL.
72  * @return The number of registers in the class.
73  */
74 extern int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs);
75
76 typedef enum arch_operand_type_t {
77         arch_operand_type_invalid,
78         arch_operand_type_memory,
79         arch_operand_type_register,
80         arch_operand_type_immediate,
81         arch_operand_type_symconst,
82         arch_operand_type_last
83 } arch_operand_type_t;
84
85 /**
86  * Different types of register allocation requirements.
87  */
88 typedef enum arch_register_req_type_t {
89         arch_register_req_type_none                = 0,  /**< No register requirement. */
90         arch_register_req_type_normal              = 1,  /**< All registers in the class are allowed. */
91         arch_register_req_type_limited             = 2,  /**< Only a real subset of the class is allowed. */
92         arch_register_req_type_should_be_same      = 4,  /**< The register should be equal to another one at the node. */
93         arch_register_req_type_should_be_different = 8,  /**< The register must be unequal from some other at the node. */
94 } arch_register_req_type_t;
95
96 extern const arch_register_req_t *arch_no_register_req;
97
98 /**
99  * Format a register requirements information into a string.
100  * @param buf The string where to put it to.
101  * @param len The size of @p buf.
102  * @param req The requirements structure to format.
103  * @return    A pointer to buf.
104  */
105 extern char *arch_register_req_format(char *buf, size_t len, const arch_register_req_t *req, const ir_node *node);
106
107 /**
108  * Certain node classes which are relevant for the register allocator.
109  */
110 typedef enum arch_irn_class_t {
111         arch_irn_class_normal     = 1 << 0,
112         arch_irn_class_spill      = 1 << 1,
113         arch_irn_class_reload     = 1 << 2,
114         arch_irn_class_copy       = 1 << 3,
115         arch_irn_class_perm       = 1 << 4,
116         arch_irn_class_branch     = 1 << 5,
117         arch_irn_class_call       = 1 << 6,
118         arch_irn_class_load       = 1 << 7,
119         arch_irn_class_store      = 1 << 8,
120         arch_irn_class_stackparam = 1 << 9,
121 } arch_irn_class_t;
122
123 /**
124  * Some flags describing a node in more detail.
125  */
126 typedef enum arch_irn_flags_t {
127         arch_irn_flags_none             = 0, /**< Node flags. */
128         arch_irn_flags_dont_spill       = 1, /**< This must not be spilled. */
129         arch_irn_flags_rematerializable = 2, /**< This can be replicated instead of spilled/reloaded. */
130         arch_irn_flags_ignore           = 4, /**< Ignore node during register allocation. */
131         arch_irn_flags_modify_sp        = 8, /**< I modify the stack pointer. */
132         arch_irn_flags_modify_flags     = 16, /**< I modify flags. */
133         arch_irn_flags_last             = arch_irn_flags_modify_flags
134 } arch_irn_flags_t;
135
136 /**
137  * Get the string representation of a flag.
138  * This functions does not handle or'ed bitmasks of flags.
139  * @param flag The flag.
140  * @return The flag as a string.
141  */
142 extern const char *arch_irn_flag_str(arch_irn_flags_t flag);
143
144 extern const arch_irn_ops_t *arch_get_irn_ops(const arch_env_t *env,
145                                               const ir_node *irn);
146
147 extern void arch_set_frame_offset(const arch_env_t *env, ir_node *irn, int bias);
148
149 extern ir_entity *arch_get_frame_entity(const arch_env_t *env, const ir_node *irn);
150 extern void arch_set_frame_entity(const arch_env_t *env, ir_node *irn, ir_entity *ent);
151 extern int arch_get_sp_bias(const arch_env_t *env, ir_node *irn);
152
153 extern int arch_get_op_estimated_cost(const arch_env_t *env, const ir_node *irn);
154 extern arch_inverse_t *arch_get_inverse(const arch_env_t *env, const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack);
155 extern int arch_possible_memory_operand(const arch_env_t *env, const ir_node *irn, unsigned int i);
156 extern void arch_perform_memory_operand(const arch_env_t *env, ir_node *irn, ir_node *spill, unsigned int i);
157
158 /**
159  * Get the register requirements for a node.
160  * @param env The architecture environment.
161  * @param req A pointer to a requirements structure, where the data can
162  *            be put into.
163  * @param irn The node.
164  * @param pos The position of the operand you're interested in.
165  * @return    A pointer to the register requirements which may <b>not</b>
166  *            neccessarily be equal to @p req. If NULL is returned, the
167  *            operand was no register operand.
168  */
169 extern const arch_register_req_t *
170 arch_get_register_req(const arch_env_t *env, const ir_node *irn, int pos);
171
172 /**
173  * Check if an operand is a register operand.
174  * @param env The environment.
175  * @param irn The node.
176  * @param pos The position of the operand.
177  * @return 1, if the operand is significant for register allocation, 0
178  * if not.
179  */
180 extern int arch_is_register_operand(const arch_env_t *env,
181     const ir_node *irn, int pos);
182
183 /**
184  * Get the number of allocatable registers concerning
185  * a register class for an operand of a node.
186  * @param env The environment.
187  * @param irn The node.
188  * @param pos The postition of the node's operand.
189  * @param bs  The bitset all allocatable registers shall be put into.
190  *            Note, that you can also pass NULL here. If you don't,
191  *            make sure, the bitset is as large as the register class
192  *            has registers.
193  * @return    The amount of registers allocatable for that operand.
194  */
195 extern int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn, int pos, bitset_t *bs);
196
197 /**
198  * Put all registers which shall not be ignored by the register
199  * allocator in a bit set.
200  * @param env The arch env.
201  * @param cls The register class to consider.
202  * @param bs  The bit set to put the registers to.
203  */
204 extern void arch_put_non_ignore_regs(const arch_env_t *env, const arch_register_class_t *cls, bitset_t *bs);
205
206 /**
207  * Check, if a register is assignable to an operand of a node.
208  * @param env The architecture environment.
209  * @param irn The node.
210  * @param pos The position of the operand.
211  * @param reg The register.
212  * @return    1, if the register might be allocated to the operand 0 if not.
213  */
214 extern int arch_reg_is_allocatable(const arch_env_t *env,
215     const ir_node *irn, int pos, const arch_register_t *reg);
216
217 /**
218  * Get the register class of an operand of a node.
219  * @param env The architecture environment.
220  * @param irn The node.
221  * @param pos The position of the operand, -1 for the output.
222  * @return    The register class of the operand or NULL, if
223  *            operand is a non-register operand.
224  */
225 extern const arch_register_class_t *
226 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos);
227
228 /**
229  * Get the register allocated at a certain output operand of a node.
230  * @param env The arch environment.
231  * @param irn The node.
232  * @return    The register allocated for this operand
233  */
234 extern const arch_register_t *
235 arch_get_irn_register(const arch_env_t *env, const ir_node *irn);
236
237 /**
238  * Set the register for a certain output operand.
239  * @param env The architecture environment.
240  * @param irn The node.
241  * @param idx The index of the output operand.
242  * @param reg The register.
243  */
244 extern void arch_set_irn_register(const arch_env_t *env, ir_node *irn,
245                 const arch_register_t *reg);
246
247 /**
248  * Classify a node.
249  * @param env The architecture environment.
250  * @param irn The node.
251  * @return A classification of the node.
252  */
253 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn);
254
255 #define arch_irn_class_is(env, irn, irn_class) ((arch_irn_classify(env, irn) & arch_irn_class_ ## irn_class) != 0)
256
257 /**
258  * Get the flags of a node.
259  * @param env The architecture environment.
260  * @param irn The node.
261  * @return The flags.
262  */
263 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn);
264
265 #define arch_irn_is(env, irn, flag) ((arch_irn_get_flags(env, irn) & arch_irn_flags_ ## flag) != 0)
266
267 #define arch_irn_has_reg_class(env, irn, pos, cls) \
268         ((cls) == arch_get_irn_reg_class(env, irn, pos))
269
270 #define arch_irn_consider_in_reg_alloc(env, cls, irn) \
271         (arch_irn_has_reg_class(env, irn, -1, cls) && !arch_irn_is(env, irn, ignore))
272
273 /**
274  * Get the operations of an irn.
275  * @param self The handler from which the method is invoked.
276  * @param irn Some node.
277  * @return Operations for that irn.
278  */
279 typedef const void *(arch_get_irn_ops_t)(const ir_node *irn);
280
281 /**
282  * Initialize the architecture environment struct.
283  * @param isa           The isa which shall be put into the environment.
284  * @param file_handle   The file handle
285  * @return The environment.
286  */
287 extern arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa,
288                                  FILE *file_handle, be_main_env_t *main_env);
289
290 /**
291  * Register an instruction set architecture
292  */
293 void be_register_isa_if(const char *name, const arch_isa_if_t *isa);
294
295 #endif /* FIRM_BE_BEARCH_H */