2 * i386 micro operations (included several times to generate
3 * different operand sizes)
5 * Copyright (c) 2003 Fabrice Bellard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #define DATA_BITS (1 << (3 + SHIFT))
22 #define SHIFT_MASK (DATA_BITS - 1)
23 #define SIGN_MASK (((target_ulong)1) << (DATA_BITS - 1))
25 #define SHIFT1_MASK 0x1f
27 #define SHIFT1_MASK 0x3f
32 #define DATA_TYPE uint8_t
33 #define DATA_STYPE int8_t
34 #define DATA_MASK 0xff
37 #define DATA_TYPE uint16_t
38 #define DATA_STYPE int16_t
39 #define DATA_MASK 0xffff
42 #define DATA_TYPE uint32_t
43 #define DATA_STYPE int32_t
44 #define DATA_MASK 0xffffffff
47 #define DATA_TYPE uint64_t
48 #define DATA_STYPE int64_t
49 #define DATA_MASK 0xffffffffffffffffULL
51 #error unhandled operand size
54 /* dynamic flags computation */
56 static int glue(compute_all_add
, SUFFIX
)(void)
58 int cf
, pf
, af
, zf
, sf
, of
;
59 target_long src1
, src2
;
61 src2
= CC_DST
- CC_SRC
;
62 cf
= (DATA_TYPE
)CC_DST
< (DATA_TYPE
)src1
;
63 pf
= parity_table
[(uint8_t)CC_DST
];
64 af
= (CC_DST
^ src1
^ src2
) & 0x10;
65 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
66 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
67 of
= lshift((src1
^ src2
^ -1) & (src1
^ CC_DST
), 12 - DATA_BITS
) & CC_O
;
68 return cf
| pf
| af
| zf
| sf
| of
;
71 static int glue(compute_c_add
, SUFFIX
)(void)
76 cf
= (DATA_TYPE
)CC_DST
< (DATA_TYPE
)src1
;
80 static int glue(compute_all_adc
, SUFFIX
)(void)
82 int cf
, pf
, af
, zf
, sf
, of
;
83 target_long src1
, src2
;
85 src2
= CC_DST
- CC_SRC
- 1;
86 cf
= (DATA_TYPE
)CC_DST
<= (DATA_TYPE
)src1
;
87 pf
= parity_table
[(uint8_t)CC_DST
];
88 af
= (CC_DST
^ src1
^ src2
) & 0x10;
89 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
90 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
91 of
= lshift((src1
^ src2
^ -1) & (src1
^ CC_DST
), 12 - DATA_BITS
) & CC_O
;
92 return cf
| pf
| af
| zf
| sf
| of
;
95 static int glue(compute_c_adc
, SUFFIX
)(void)
100 cf
= (DATA_TYPE
)CC_DST
<= (DATA_TYPE
)src1
;
104 static int glue(compute_all_sub
, SUFFIX
)(void)
106 int cf
, pf
, af
, zf
, sf
, of
;
107 target_long src1
, src2
;
108 src1
= CC_DST
+ CC_SRC
;
110 cf
= (DATA_TYPE
)src1
< (DATA_TYPE
)src2
;
111 pf
= parity_table
[(uint8_t)CC_DST
];
112 af
= (CC_DST
^ src1
^ src2
) & 0x10;
113 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
114 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
115 of
= lshift((src1
^ src2
) & (src1
^ CC_DST
), 12 - DATA_BITS
) & CC_O
;
116 return cf
| pf
| af
| zf
| sf
| of
;
119 static int glue(compute_c_sub
, SUFFIX
)(void)
122 target_long src1
, src2
;
123 src1
= CC_DST
+ CC_SRC
;
125 cf
= (DATA_TYPE
)src1
< (DATA_TYPE
)src2
;
129 static int glue(compute_all_sbb
, SUFFIX
)(void)
131 int cf
, pf
, af
, zf
, sf
, of
;
132 target_long src1
, src2
;
133 src1
= CC_DST
+ CC_SRC
+ 1;
135 cf
= (DATA_TYPE
)src1
<= (DATA_TYPE
)src2
;
136 pf
= parity_table
[(uint8_t)CC_DST
];
137 af
= (CC_DST
^ src1
^ src2
) & 0x10;
138 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
139 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
140 of
= lshift((src1
^ src2
) & (src1
^ CC_DST
), 12 - DATA_BITS
) & CC_O
;
141 return cf
| pf
| af
| zf
| sf
| of
;
144 static int glue(compute_c_sbb
, SUFFIX
)(void)
147 target_long src1
, src2
;
148 src1
= CC_DST
+ CC_SRC
+ 1;
150 cf
= (DATA_TYPE
)src1
<= (DATA_TYPE
)src2
;
154 static int glue(compute_all_logic
, SUFFIX
)(void)
156 int cf
, pf
, af
, zf
, sf
, of
;
158 pf
= parity_table
[(uint8_t)CC_DST
];
160 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
161 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
163 return cf
| pf
| af
| zf
| sf
| of
;
166 static int glue(compute_c_logic
, SUFFIX
)(void)
171 static int glue(compute_all_inc
, SUFFIX
)(void)
173 int cf
, pf
, af
, zf
, sf
, of
;
174 target_long src1
, src2
;
178 pf
= parity_table
[(uint8_t)CC_DST
];
179 af
= (CC_DST
^ src1
^ src2
) & 0x10;
180 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
181 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
182 of
= ((CC_DST
& DATA_MASK
) == SIGN_MASK
) << 11;
183 return cf
| pf
| af
| zf
| sf
| of
;
187 static int glue(compute_c_inc
, SUFFIX
)(void)
193 static int glue(compute_all_dec
, SUFFIX
)(void)
195 int cf
, pf
, af
, zf
, sf
, of
;
196 target_long src1
, src2
;
200 pf
= parity_table
[(uint8_t)CC_DST
];
201 af
= (CC_DST
^ src1
^ src2
) & 0x10;
202 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
203 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
204 of
= ((CC_DST
& DATA_MASK
) == ((target_ulong
)SIGN_MASK
- 1)) << 11;
205 return cf
| pf
| af
| zf
| sf
| of
;
208 static int glue(compute_all_shl
, SUFFIX
)(void)
210 int cf
, pf
, af
, zf
, sf
, of
;
211 cf
= (CC_SRC
>> (DATA_BITS
- 1)) & CC_C
;
212 pf
= parity_table
[(uint8_t)CC_DST
];
213 af
= 0; /* undefined */
214 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
215 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
216 /* of is defined if shift count == 1 */
217 of
= lshift(CC_SRC
^ CC_DST
, 12 - DATA_BITS
) & CC_O
;
218 return cf
| pf
| af
| zf
| sf
| of
;
221 static int glue(compute_c_shl
, SUFFIX
)(void)
223 return (CC_SRC
>> (DATA_BITS
- 1)) & CC_C
;
227 static int glue(compute_c_sar
, SUFFIX
)(void)
233 static int glue(compute_all_sar
, SUFFIX
)(void)
235 int cf
, pf
, af
, zf
, sf
, of
;
237 pf
= parity_table
[(uint8_t)CC_DST
];
238 af
= 0; /* undefined */
239 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
240 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
241 /* of is defined if shift count == 1 */
242 of
= lshift(CC_SRC
^ CC_DST
, 12 - DATA_BITS
) & CC_O
;
243 return cf
| pf
| af
| zf
| sf
| of
;
247 static int glue(compute_c_mul
, SUFFIX
)(void)
255 /* NOTE: we compute the flags like the P4. On olders CPUs, only OF and
256 CF are modified and it is slower to do that. */
257 static int glue(compute_all_mul
, SUFFIX
)(void)
259 int cf
, pf
, af
, zf
, sf
, of
;
261 pf
= parity_table
[(uint8_t)CC_DST
];
262 af
= 0; /* undefined */
263 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
264 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
266 return cf
| pf
| af
| zf
| sf
| of
;
269 /* various optimized jumps cases */
271 void OPPROTO
glue(op_jb_sub
, SUFFIX
)(void)
273 target_long src1
, src2
;
274 src1
= CC_DST
+ CC_SRC
;
277 if ((DATA_TYPE
)src1
< (DATA_TYPE
)src2
)
282 void OPPROTO
glue(op_jz_sub
, SUFFIX
)(void)
284 if ((DATA_TYPE
)CC_DST
== 0)
289 void OPPROTO
glue(op_jnz_sub
, SUFFIX
)(void)
291 if ((DATA_TYPE
)CC_DST
!= 0)
296 void OPPROTO
glue(op_jbe_sub
, SUFFIX
)(void)
298 target_long src1
, src2
;
299 src1
= CC_DST
+ CC_SRC
;
302 if ((DATA_TYPE
)src1
<= (DATA_TYPE
)src2
)
307 void OPPROTO
glue(op_js_sub
, SUFFIX
)(void)
309 if (CC_DST
& SIGN_MASK
)
314 void OPPROTO
glue(op_jl_sub
, SUFFIX
)(void)
316 target_long src1
, src2
;
317 src1
= CC_DST
+ CC_SRC
;
320 if ((DATA_STYPE
)src1
< (DATA_STYPE
)src2
)
325 void OPPROTO
glue(op_jle_sub
, SUFFIX
)(void)
327 target_long src1
, src2
;
328 src1
= CC_DST
+ CC_SRC
;
331 if ((DATA_STYPE
)src1
<= (DATA_STYPE
)src2
)
340 void OPPROTO
glue(op_loopnz
, SUFFIX
)(void)
342 if ((DATA_TYPE
)ECX
!= 0 && !(T0
& CC_Z
))
347 void OPPROTO
glue(op_loopz
, SUFFIX
)(void)
349 if ((DATA_TYPE
)ECX
!= 0 && (T0
& CC_Z
))
354 void OPPROTO
glue(op_jz_ecx
, SUFFIX
)(void)
356 if ((DATA_TYPE
)ECX
== 0)
361 void OPPROTO
glue(op_jnz_ecx
, SUFFIX
)(void)
363 if ((DATA_TYPE
)ECX
!= 0)
370 /* various optimized set cases */
372 void OPPROTO
glue(op_setb_T0_sub
, SUFFIX
)(void)
374 target_long src1
, src2
;
375 src1
= CC_DST
+ CC_SRC
;
378 T0
= ((DATA_TYPE
)src1
< (DATA_TYPE
)src2
);
381 void OPPROTO
glue(op_setz_T0_sub
, SUFFIX
)(void)
383 T0
= ((DATA_TYPE
)CC_DST
== 0);
386 void OPPROTO
glue(op_setbe_T0_sub
, SUFFIX
)(void)
388 target_long src1
, src2
;
389 src1
= CC_DST
+ CC_SRC
;
392 T0
= ((DATA_TYPE
)src1
<= (DATA_TYPE
)src2
);
395 void OPPROTO
glue(op_sets_T0_sub
, SUFFIX
)(void)
397 T0
= lshift(CC_DST
, -(DATA_BITS
- 1)) & 1;
400 void OPPROTO
glue(op_setl_T0_sub
, SUFFIX
)(void)
402 target_long src1
, src2
;
403 src1
= CC_DST
+ CC_SRC
;
406 T0
= ((DATA_STYPE
)src1
< (DATA_STYPE
)src2
);
409 void OPPROTO
glue(op_setle_T0_sub
, SUFFIX
)(void)
411 target_long src1
, src2
;
412 src1
= CC_DST
+ CC_SRC
;
415 T0
= ((DATA_STYPE
)src1
<= (DATA_STYPE
)src2
);
420 void OPPROTO
glue(glue(op_shl
, SUFFIX
), _T0_T1
)(void)
423 count
= T1
& SHIFT1_MASK
;
428 void OPPROTO
glue(glue(op_shr
, SUFFIX
), _T0_T1
)(void)
431 count
= T1
& SHIFT1_MASK
;
437 void OPPROTO
glue(glue(op_sar
, SUFFIX
), _T0_T1
)(void)
442 count
= T1
& SHIFT1_MASK
;
443 src
= (DATA_STYPE
)T0
;
449 #include "ops_template_mem.h"
452 #include "ops_template_mem.h"
454 #if !defined(CONFIG_USER_ONLY)
456 #include "ops_template_mem.h"
459 #include "ops_template_mem.h"
465 void OPPROTO
glue(glue(op_bt
, SUFFIX
), _T0_T1_cc
)(void)
468 count
= T1
& SHIFT_MASK
;
469 CC_SRC
= T0
>> count
;
472 void OPPROTO
glue(glue(op_bts
, SUFFIX
), _T0_T1_cc
)(void)
475 count
= T1
& SHIFT_MASK
;
477 T0
|= (((target_long
)1) << count
);
480 void OPPROTO
glue(glue(op_btr
, SUFFIX
), _T0_T1_cc
)(void)
483 count
= T1
& SHIFT_MASK
;
485 T0
&= ~(((target_long
)1) << count
);
488 void OPPROTO
glue(glue(op_btc
, SUFFIX
), _T0_T1_cc
)(void)
491 count
= T1
& SHIFT_MASK
;
493 T0
^= (((target_long
)1) << count
);
496 void OPPROTO
glue(glue(op_add_bit
, SUFFIX
), _A0_T1
)(void)
498 A0
+= ((DATA_STYPE
)T1
>> (3 + SHIFT
)) << SHIFT
;
501 void OPPROTO
glue(glue(op_bsf
, SUFFIX
), _T0_cc
)(void)
506 res
= T0
& DATA_MASK
;
509 while ((res
& 1) == 0) {
514 CC_DST
= 1; /* ZF = 0 */
516 CC_DST
= 0; /* ZF = 1 */
521 void OPPROTO
glue(glue(op_bsr
, SUFFIX
), _T0_cc
)(void)
526 res
= T0
& DATA_MASK
;
528 count
= DATA_BITS
- 1;
529 while ((res
& SIGN_MASK
) == 0) {
534 CC_DST
= 1; /* ZF = 0 */
536 CC_DST
= 0; /* ZF = 1 */
544 void OPPROTO
op_update_bt_cc(void)
550 /* string operations */
552 void OPPROTO
glue(op_movl_T0_Dshift
, SUFFIX
)(void)
559 void OPPROTO
glue(glue(op_out
, SUFFIX
), _T0_T1
)(void)
561 glue(cpu_out
, SUFFIX
)(env
, T0
, T1
& DATA_MASK
);
564 void OPPROTO
glue(glue(op_in
, SUFFIX
), _T0_T1
)(void)
566 T1
= glue(cpu_in
, SUFFIX
)(env
, T0
);
569 void OPPROTO
glue(glue(op_in
, SUFFIX
), _DX_T0
)(void)
571 T0
= glue(cpu_in
, SUFFIX
)(env
, EDX
& 0xffff);
574 void OPPROTO
glue(glue(op_out
, SUFFIX
), _DX_T0
)(void)
576 glue(cpu_out
, SUFFIX
)(env
, EDX
& 0xffff, T0
);
579 void OPPROTO
glue(glue(op_check_io
, SUFFIX
), _T0
)(void)
581 glue(glue(check_io
, SUFFIX
), _T0
)();
584 void OPPROTO
glue(glue(op_check_io
, SUFFIX
), _DX
)(void)
586 glue(glue(check_io
, SUFFIX
), _DX
)();