90de696178841241711f36e4b268019f9da207c8
[libfirm] / ir / be / beabi.h
1 /**
2  * Backend ABI implementation.
3  */
4 #ifndef FIRM_BEABI_H
5 #define FIRM_BEABI_H
6
7 #include "firm_types.h"
8
9 #include "pset.h"
10 #include "pmap.h"
11 #include "bitset.h"
12
13 #include "be.h"
14 #include "beirg.h"
15 #include "bearch.h"
16 #include "beabi_t.h"
17
18 struct _be_abi_call_flags_bits_t {
19         unsigned left_to_right          : 1;  /**< Arguments are from left to right. */
20         unsigned store_args_sequential  : 1;  /**< Use sequential stores for arguments. */
21         unsigned try_omit_fp            : 1;  /**< Try to omit the frame pointer. */
22         unsigned fp_free                : 1;  /**< The function can use any register as frame pointer. */
23         unsigned call_has_imm           : 1;  /**< A call can take the callee's address as an immediate. */
24         unsigned irg_is_leaf            : 1;  /**< 1, if the IRG is a leaf function. */
25         unsigned frame_is_setup_on_call : 1;  /**< Set to one, if there is already enough room on the stack for call args. */
26 };
27
28 union _be_abi_call_flags_t {
29         be_abi_call_flags_bits_t bits;
30         unsigned val;
31 };
32
33 struct _be_abi_callbacks_t {
34         /**
35          * Initialize the callback object.
36          * @param call The call object.
37          * @param aenv The architecture environment.
38          * @param irg  The graph with the method.
39          * @return     Some pointer. This pointer is passed to all other callback functions as self object.
40          */
41         void *(*init)(const be_abi_call_t *call, const arch_env_t *aenv, ir_graph *irg);
42
43         /**
44          * Destroy the callback object.
45          * @param self The callback object.
46          */
47         void (*done)(void *self);
48
49         /**
50          * Get the between type for that call.
51          * @param self The callback object.
52          * @return The between type of for that call.
53          */
54         ir_type *(*get_between_type)(void *self);
55
56         /**
57          * Put all registers which are saved by the prologue/epilogue in a set.
58          * @param self The callback object.
59          * @param regs A set.
60          */
61         void (*regs_saved_by_me)(void *self, pset *regs);
62
63         /**
64          * Generate the prologue.
65          * @param self    The callback object.
66          * @param mem     A pointer to the mem node. Update this if you define new memory.
67          * @param reg_map A map mapping all callee_save/ignore/parameter registers to their defining nodes.
68          * @return        The register which shall be used as a stack frame base.
69          *
70          * All nodes which define registers in @p reg_map must keep @p reg_map current.
71          */
72         const arch_register_t *(*prologue)(void *self, ir_node **mem, pmap *reg_map);
73
74         /**
75          * Generate the epilogue.
76          * @param self    The callback object.
77          * @param mem     Memory one can attach to.
78          * @param reg_map A mapping mapping all callee_save/ignore/return registers to their defining nodes.
79          *
80      * All nodes which define registers in @p reg_map must keep @p reg_map current.
81          * Also, the @p mem variable must be updated, if memory producing nodes are inserted.
82          */
83         void (*epilogue)(void *self, ir_node *bl, ir_node **mem, pmap *reg_map);
84 };
85
86 /**
87  * Set the flags for a call.
88  * @param call          The call.
89  * @param flags         Some flags to be set.
90  * @param cb            The call callbacks for that call.
91  * @note                The ABI phase might change the flags due to analysis.
92  */
93 void be_abi_call_set_flags(be_abi_call_t *call, be_abi_call_flags_t flags, const be_abi_callbacks_t *cb);
94
95 /**
96  * Set register class for call address.
97  * @param call      The call.
98  * @param cls       The register class for call address.
99  */
100 void be_abi_call_set_call_address_reg_class(be_abi_call_t *call, const arch_register_class_t *cls);
101
102 void be_abi_call_param_stack(be_abi_call_t *call, int pos, unsigned alignment, unsigned space_before, unsigned space_after);
103 void be_abi_call_param_reg(be_abi_call_t *call, int pos, const arch_register_t *reg);
104 void be_abi_call_res_reg(be_abi_call_t *call, int pos, const arch_register_t *reg);
105
106 /**
107  * Get the flags of a ABI call object.
108  * Note that the flags must not be the same as set by be_abi_call_set_flags(). Analysis may have
109  * altered several flags, so getting them from the call object is always a good idea.
110  * @param call The call object.
111  * @return The flags.
112  */
113 be_abi_call_flags_t be_abi_call_get_flags(const be_abi_call_t *call);
114
115 /**
116  * Get the method type of an ABI call object.
117  * @param call The call object.
118  * @return The method type for that call object.
119  */
120 ir_type *be_abi_call_get_method_type(const be_abi_call_t *call);
121
122 be_abi_irg_t *be_abi_introduce(be_irg_t *bi);
123 void be_abi_fix_stack_bias(be_abi_irg_t *env);
124 void be_abi_free(be_abi_irg_t *abi);
125
126 /**
127  * Rewire all stack modifying nodes and their users to assure SSA property.
128  * @param env   The abi
129  */
130 void be_abi_fix_stack_nodes(be_abi_irg_t *env);
131
132 /**
133  * Put the registers which are forbidden specifically for this IRG in a bitset.
134  */
135 void be_abi_put_ignore_regs(be_abi_irg_t *abi, const arch_register_class_t *cls, bitset_t *bs);
136
137 ir_node *be_abi_get_callee_save_irn(be_abi_irg_t *abi, const arch_register_t *reg);
138 ir_node *be_abi_get_ignore_irn(be_abi_irg_t *abi, const arch_register_t *reg);
139 ir_node *be_abi_get_start_barrier(be_abi_irg_t *abi);
140
141 #define be_abi_reg_map_get(map, reg)       pmap_get((map), (void *) (reg))
142 #define be_abi_reg_map_set(map, reg, irn)  pmap_insert((map), (void *) (reg), (irn))
143
144 /** The number of parts of the stack layout. */
145 #define N_FRAME_TYPES 3
146
147 /**
148  * This type describes the stack layout.
149  * The stack is divided into 3 parts:
150  * - arg_type:     A struct type describing the stack arguments and it's order.
151  * - between_type: A struct type describing the stack layout between arguments
152  *                 and frame type. In architectures that put the return address
153  *                 automatically on the stack, the return address is put here.
154  * - frame_type:   A class type describing the frame layout
155  */
156 struct _be_stack_layout_t {
157         ir_type *arg_type;                 /**< A type describing the stack argument layout. */
158         ir_type *between_type;             /**< A type describing the "between" layout. */
159         ir_type *frame_type;               /**< The frame type. */
160
161         ir_type *order[N_FRAME_TYPES];     /**< arg, between and frame types ordered. */
162
163         int initial_offset;
164         int stack_dir;                     /**< -1 for decreasing, 1 for increasing. */
165         ir_entity **param_map;             /**< An array mapping type parameters to arg_type entries */
166 };
167
168 /**
169  * Returns the stack layout from a abi environment.
170  */
171 const be_stack_layout_t *be_abi_get_stack_layout(const be_abi_irg_t *abi);
172
173 /**
174  * Returns non-zero if the ABI has omitted the frame pointer in
175  * the current graph.
176  */
177 int be_abi_omit_fp(const be_abi_irg_t *abi);
178
179 #endif /* _BEABI_H */