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       Type definitions for ia32 node attributes.
23  * @author      Christian Wuerdig
24  */
25 #ifndef FIRM_BE_IA32_IA32_NODES_ATTR_H
26 #define FIRM_BE_IA32_IA32_NODES_ATTR_H
27 
28 #include "firm_types.h"
29 #include "bearch.h"
30 #include "irnode_t.h"
31 
32 /** ia32 condition codes (the numbers correspond to the real encoding order) */
33 typedef enum ia32_condition_code_t {
34 	ia32_cc_negated       = 0x01, /**< negates condition */
35 
36 	ia32_cc_overflow      = 0x00,                                /**< OF=1 */
37 	ia32_cc_below         = 0x02,                                /**< CF=1 */
38 	ia32_cc_equal         = 0x04,                                /**< ZF=1 */
39 	ia32_cc_below_equal   = 0x06,                                /**< ZF=1 or CF=1 */
40 	ia32_cc_sign          = 0x08,                                /**< SF=1 */
41 	ia32_cc_parity        = 0x0A,                                /**< PF=1 */
42 	ia32_cc_less          = 0x0C,                                /**< SF!=OF */
43 	ia32_cc_less_equal    = 0x0E,                                /**< ZF=1 or SF!=OF */
44 	ia32_cc_not_overflow  = ia32_cc_negated|ia32_cc_overflow,    /**< OF=0 */
45 	ia32_cc_above_equal   = ia32_cc_negated|ia32_cc_below,       /**< CF=0 */
46 	ia32_cc_not_equal     = ia32_cc_negated|ia32_cc_equal,       /**< ZF=0 */
47 	ia32_cc_above         = ia32_cc_negated|ia32_cc_below_equal, /**< ZF=0 and CF=0 */
48 	ia32_cc_not_sign      = ia32_cc_negated|ia32_cc_sign,        /**< SF=0 */
49 	ia32_cc_not_parity    = ia32_cc_negated|ia32_cc_parity,      /**< PF=0 */
50 	ia32_cc_greater_equal = ia32_cc_negated|ia32_cc_less,        /**< SF=OF */
51 	ia32_cc_greater       = ia32_cc_negated|ia32_cc_less_equal,  /**< ZF=0 and SF=OF */
52 
53 	/* the following codes are (unfortunately) NOT real hardware codes but
54 	 * simplify our backend as you need these combinations for some
55 	 * floatingpoint compares (the emitter will split them into multiple
56 	 * instructions) */
57 	ia32_cc_float_parity_cases = 0x20,
58 	/* we need even more cases as inversing the cc is different for float
59 	 * comparisons (though for the following we need no special
60 	 * parity+x combinations) */
61 	ia32_cc_additional_float_cases = 0x10,
62 
63 	/* make sure that the lower 4 bit correspond to the real encoding
64 	 * (of the comparison not involving the parity special) */
65 	ia32_cc_float_equal        = 0x34,                                /**< PF=0 and ZF=1 */
66 	ia32_cc_float_below        = 0x32,                                /**< PF=0 and CF=1 */
67 	ia32_cc_float_below_equal  = 0x36,                                /**< PF=0 and (ZF=1 or CF=1) */
68 	ia32_cc_float_not_equal    = ia32_cc_negated|ia32_cc_float_equal, /**< PF=1 or ZF=0 */
69 	ia32_cc_float_unordered_above_equal
70 		= ia32_cc_negated|ia32_cc_float_below,                        /**< PF=1 or CF=0 */
71 	ia32_cc_float_unordered_above
72 		= ia32_cc_negated|ia32_cc_float_below_equal,                  /**< PF=1 or (ZF=0 and CF=0) */
73 
74 	ia32_cc_float_unordered_below_equal = 0x16,                       /**< ZF=1 or CF=1 */
75 	ia32_cc_float_unordered_below       = 0x12,                       /**< CF=1 */
76 	ia32_cc_float_above        =
77 		ia32_cc_negated|ia32_cc_float_unordered_below_equal,          /**< ZF=0 and CF=0 */
78 	ia32_cc_float_above_equal
79 		= ia32_cc_negated|ia32_cc_float_unordered_below,              /**< CF=0 */
80 } ia32_condition_code_t;
ENUM_BITSET(ia32_condition_code_t)81 ENUM_BITSET(ia32_condition_code_t)
82 
83 static inline ia32_condition_code_t ia32_negate_condition_code(
84 		ia32_condition_code_t code)
85 {
86 	return code ^ ia32_cc_negated;
87 }
88 
ia32_invert_condition_code(ia32_condition_code_t code)89 static inline ia32_condition_code_t ia32_invert_condition_code(
90 		ia32_condition_code_t code)
91 {
92 	/* doesn't appear to have any systematic, so use a table */
93 	switch (code) {
94 	case ia32_cc_below:              return ia32_cc_above;
95 	case ia32_cc_below_equal:        return ia32_cc_above_equal;
96 	case ia32_cc_above:              return ia32_cc_below;
97 	case ia32_cc_above_equal:        return ia32_cc_below_equal;
98 	case ia32_cc_less:               return ia32_cc_greater;
99 	case ia32_cc_less_equal:         return ia32_cc_greater_equal;
100 	case ia32_cc_greater:            return ia32_cc_less;
101 	case ia32_cc_greater_equal:      return ia32_cc_less_equal;
102 	case ia32_cc_float_below:        return ia32_cc_float_above;
103 	case ia32_cc_float_below_equal:  return ia32_cc_float_above_equal;
104 	case ia32_cc_float_above:        return ia32_cc_float_below;
105 	case ia32_cc_float_above_equal:  return ia32_cc_float_below_equal;
106 	case ia32_cc_float_unordered_below:       return ia32_cc_float_unordered_above;
107 	case ia32_cc_float_unordered_below_equal: return ia32_cc_float_unordered_above_equal;
108 	case ia32_cc_float_unordered_above:       return ia32_cc_float_unordered_below;
109 	case ia32_cc_float_unordered_above_equal: return ia32_cc_float_unordered_below_equal;
110 	default:                         return code;
111 	}
112 }
113 
114 typedef enum {
115 	ia32_Normal,
116 	ia32_AddrModeD,
117 	ia32_AddrModeS
118 } ia32_op_type_t;
119 
120 typedef enum {
121 	ia32_am_none   = 0,
122 	ia32_am_unary  = 1,
123 	ia32_am_binary = 2
124 } ia32_am_type_t;
125 
126 typedef enum {
127 	match_commutative       = 1 << 0, /**< inputs are commutative */
128 	match_am_and_immediates = 1 << 1, /**< node supports AM and immediate at
129 	                                       the same time */
130 	match_am                = 1 << 2, /**< node supports (32bit) source AM */
131 	match_8bit_am           = 1 << 3, /**< node supports 8bit source AM */
132 	match_16bit_am          = 1 << 4, /**< node supports 16bit source AM */
133 	match_immediate         = 1 << 5, /**< node supports immediates */
134 	/** for 8/16 bit modes, mode_neutral operations can be emulated by their
135 	 * 32bit equivalents, they just don't care about the upper bits (they can be
136 	 * arbitrary before the insn and are unknown after the instruction). */
137 	match_mode_neutral      = 1 << 6,
138 	/** for 8/16 bit modes, zero_ext operations can be emulated by their
139 	 * 32bit equivalents, however the upper bits must be zero extended. */
140 	match_zero_ext          = 1 << 7,
141 	/** for 8/16 bit modes, upconv operations can be emulated by their
142 	 * 32bit equivalents, however the upper bits have to sign/zero extended
143 	 * based on the operations mode. */
144 	match_upconv            = 1 << 8,
145 	match_try_am            = 1 << 9, /**< only try to produce AM node, don't
146 	                                       do anything if AM isn't possible */
147 	match_two_users         = 1 << 10,/**< the instruction uses a load two times ... */
148 } match_flags_t;
149 ENUM_BITSET(match_flags_t)
150 
151 typedef struct ia32_op_attr_t ia32_op_attr_t;
152 struct ia32_op_attr_t {
153 	//match_flags_t  flags;
154 	unsigned       latency;
155 };
156 
157 #ifndef NDEBUG
158 typedef enum {
159 	IA32_ATTR_INVALID                = 0,
160 	IA32_ATTR_ia32_attr_t            = 1 << 0,
161 	IA32_ATTR_ia32_x87_attr_t        = 1 << 1,
162 	IA32_ATTR_ia32_asm_attr_t        = 1 << 2,
163 	IA32_ATTR_ia32_immediate_attr_t  = 1 << 3,
164 	IA32_ATTR_ia32_condcode_attr_t   = 1 << 4,
165 	IA32_ATTR_ia32_copyb_attr_t      = 1 << 5,
166 	IA32_ATTR_ia32_call_attr_t       = 1 << 6,
167 	IA32_ATTR_ia32_climbframe_attr_t = 1 << 7,
168 	IA32_ATTR_ia32_switch_attr_t     = 1 << 8,
169 } ia32_attr_type_t;
170 #endif
171 
172 /**
173  * The generic ia32 attributes. Every node has them.
174  */
175 typedef struct ia32_attr_t ia32_attr_t;
176 struct ia32_attr_t {
177 	except_attr  exc;               /**< the exception attribute. MUST be the first one. */
178 	struct ia32_attr_data_bitfield {
179 		unsigned tp:3;                  /**< ia32 node type. */
180 		unsigned am_arity:2;            /**< Indicates the address mode type supported by this node. */
181 		unsigned am_scale:2;            /**< The address mode scale for index register. */
182 		unsigned am_sc_sign:1;          /**< The sign bit of the address mode symconst. */
183 
184 		unsigned am_sc_no_pic_adjust : 1;/**< AM symconst can be relative to EIP */
185 		unsigned am_tls_segment:1;       /**< addresses are relative to TLS */
186 		unsigned use_frame:1;           /**< Indicates whether the operation uses the frame pointer or not. */
187 		unsigned has_except_label:1;        /**< Set if this node needs a label because of possible exception. */
188 
189 		unsigned is_commutative:1;      /**< Indicates whether op is commutative or not. */
190 
191 		unsigned need_stackent:1;       /**< Set to 1 if node need space on stack. */
192 		unsigned need_64bit_stackent:1; /**< needs a 64bit stack entity (see double->unsigned int conv) */
193 		unsigned need_32bit_stackent:1; /**< needs a 32bit stack entity */
194 		unsigned ins_permuted : 1;      /**< inputs of node have been permuted
195 		                                     (for commutative nodes) */
196 		unsigned is_reload : 1;         /**< node performs a reload */
197 		unsigned is_spill : 1;
198 		unsigned is_remat : 1;
199 	} data;
200 
201 	int        am_offs;       /**< offsets for AddrMode */
202 	ir_entity *am_sc;         /**< SymConst for AddrMode */
203 
204 	ir_mode   *ls_mode;       /**< Load/Store mode: This is the mode of the
205 	                               value that is manipulated by this node. */
206 
207 	ir_entity *frame_ent; /**< the frame entity attached to this node */
208 
209 	ir_label_t        exc_label;       /**< the exception label iff this instruction can throw an exception */
210 
211 #ifndef NDEBUG
212 	const char       *orig_node;      /**< holds the name of the original ir node */
213 	unsigned          attr_type;      /**< bitfield indicating the attribute type */
214 #endif
215 };
216 
217 /**
218  * The attributes for a Call node.
219  */
220 typedef struct ia32_call_attr_t ia32_call_attr_t;
221 struct ia32_call_attr_t {
222 	ia32_attr_t  attr;    /**< generic attribute */
223 	unsigned     pop;     /**< number of bytes that get popped by the callee */
224 	ir_type     *call_tp; /**< The call type, copied from the original Call node. */
225 };
226 
227 /**
228  * The attributes for nodes with condition code.
229  */
230 typedef struct ia32_condcode_attr_t ia32_condcode_attr_t;
231 struct ia32_condcode_attr_t {
232 	ia32_attr_t           attr;           /**< generic attribute */
233 	ia32_condition_code_t condition_code; /**< condition code*/
234 };
235 
236 /**
237  * The attributes for Switches
238  */
239 typedef struct ia32_switch_attr_t ia32_switch_attr_t;
240 struct ia32_switch_attr_t {
241 	ia32_attr_t            attr;        /**< generic attribute */
242 	const ir_switch_table *table;
243 	ir_entity             *jump_table;
244 };
245 
246 /**
247  * The attributes for CopyB code.
248  */
249 typedef struct ia32_copyb_attr_t ia32_copyb_attr_t;
250 struct ia32_copyb_attr_t {
251 	ia32_attr_t  attr;      /**< generic attribute */
252 	unsigned     size;      /**< size of copied block */
253 };
254 
255 /**
256  * The attributes for immediates.
257  */
258 typedef struct ia32_immediate_attr_t ia32_immediate_attr_t;
259 struct ia32_immediate_attr_t {
260 	ia32_attr_t  attr;              /**< generic attribute */
261 	ir_entity   *symconst;          /**< An entity if any. */
262 	long         offset;            /**< An offset if any. */
263 	unsigned     sc_sign : 1;       /**< The sign bit of the symconst. */
264 	unsigned     no_pic_adjust : 1; /**< constant can be relative to EIP */
265 };
266 
267 /**
268  * The attributes for x87 nodes.
269  */
270 typedef struct ia32_x87_attr_t ia32_x87_attr_t;
271 struct ia32_x87_attr_t {
272 	ia32_attr_t            attr;       /**< the generic attribute */
273 	arch_register_t const *reg;        /**< The explicit register operand. */
274 	bool                   res_in_reg; /**< True if the result is in the explicit register operand, %st0 otherwise. */
275 	bool                   pop;        /**< Emit a pop suffix. */
276 };
277 
278 typedef struct ia32_asm_reg_t ia32_asm_reg_t;
279 struct ia32_asm_reg_t {
280 	unsigned                   use_input  : 1; /* use input or output pos */
281 	unsigned                   valid      : 1;
282 	unsigned                   memory     : 1;
283 	unsigned                   dummy_fill : 13;
284 	unsigned                   inout_pos  : 16; /* in/out pos where the
285 	                                               register is assigned */
286 	const ir_mode             *mode;
287 };
288 
289 /**
290  * The attributes for ASM nodes.
291  */
292 typedef struct ia32_asm_attr_t ia32_asm_attr_t;
293 struct ia32_asm_attr_t {
294 	ia32_x87_attr_t       x87_attr;
295 	ident                *asm_text;
296 	const ia32_asm_reg_t *register_map;
297 };
298 
299 /**
300  * The attributes for the ClimbFrame node.
301  */
302 typedef struct ia32_climbframe_attr_t ia32_climbframe_attr_t;
303 struct ia32_climbframe_attr_t {
304 	ia32_attr_t attr;      /**< generic attribute */
305 	unsigned    count;     /**< number of frames to climb up */
306 };
307 
308 /* the following union is necessary to indicate to the compiler that we might want to cast
309  * the structs (we use them to simulate OO-inheritance) */
310 union allow_casts_attr_t_ {
311 	ia32_attr_t            attr;
312 	ia32_call_attr_t       call_attr;
313 	ia32_condcode_attr_t   cc_attr;
314 	ia32_copyb_attr_t      cpy_attr;
315 	ia32_x87_attr_t        x87_attr;
316 	ia32_asm_attr_t        asm_attr;
317 	ia32_immediate_attr_t  immediate_attr;
318 	ia32_climbframe_attr_t climbframe_attr;
319 	ia32_switch_attr_t     switch_attr;
320 };
321 
322 #ifndef NDEBUG
323 #define CAST_IA32_ATTR(type,ptr)        (assert( ((const ia32_attr_t*)(ptr))->attr_type & IA32_ATTR_ ## type ), (type*) (ptr))
324 #define CONST_CAST_IA32_ATTR(type,ptr)  (assert( ((const ia32_attr_t*)(ptr))->attr_type & IA32_ATTR_ ## type ), (const type*) (ptr))
325 #else
326 #define CAST_IA32_ATTR(type,ptr)        ((type*) (ptr))
327 #define CONST_CAST_IA32_ATTR(type,ptr)  ((const type*) (ptr))
328 #endif
329 
330 #endif
331