Statistics
| Branch: | Revision:

root / target-i386 / op.c @ acf5feac

History | View | Annotate | Download (36.4 kB)

1
/*
2
 *  i386 micro operations
3
 * 
4
 *  Copyright (c) 2003 Fabrice Bellard
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
20

    
21
/* XXX: must use this define because the soft mmu macros have huge
22
   register constraints so they cannot be used in any C code. gcc 3.3
23
   does not seem to be able to handle some constraints in rol
24
   operations, so we disable it. */
25
#if !(__GNUC__ == 3 && __GNUC_MINOR__ == 3)
26
#define ASM_SOFTMMU
27
#endif
28
#include "exec.h"
29

    
30
/* n must be a constant to be efficient */
31
static inline int lshift(int x, int n)
32
{
33
    if (n >= 0)
34
        return x << n;
35
    else
36
        return x >> (-n);
37
}
38

    
39
/* we define the various pieces of code used by the JIT */
40

    
41
#define REG EAX
42
#define REGNAME _EAX
43
#include "opreg_template.h"
44
#undef REG
45
#undef REGNAME
46

    
47
#define REG ECX
48
#define REGNAME _ECX
49
#include "opreg_template.h"
50
#undef REG
51
#undef REGNAME
52

    
53
#define REG EDX
54
#define REGNAME _EDX
55
#include "opreg_template.h"
56
#undef REG
57
#undef REGNAME
58

    
59
#define REG EBX
60
#define REGNAME _EBX
61
#include "opreg_template.h"
62
#undef REG
63
#undef REGNAME
64

    
65
#define REG ESP
66
#define REGNAME _ESP
67
#include "opreg_template.h"
68
#undef REG
69
#undef REGNAME
70

    
71
#define REG EBP
72
#define REGNAME _EBP
73
#include "opreg_template.h"
74
#undef REG
75
#undef REGNAME
76

    
77
#define REG ESI
78
#define REGNAME _ESI
79
#include "opreg_template.h"
80
#undef REG
81
#undef REGNAME
82

    
83
#define REG EDI
84
#define REGNAME _EDI
85
#include "opreg_template.h"
86
#undef REG
87
#undef REGNAME
88

    
89
/* operations with flags */
90

    
91
/* update flags with T0 and T1 (add/sub case) */
92
void OPPROTO op_update2_cc(void)
93
{
94
    CC_SRC = T1;
95
    CC_DST = T0;
96
}
97

    
98
/* update flags with T0 (logic operation case) */
99
void OPPROTO op_update1_cc(void)
100
{
101
    CC_DST = T0;
102
}
103

    
104
void OPPROTO op_update_neg_cc(void)
105
{
106
    CC_SRC = -T0;
107
    CC_DST = T0;
108
}
109

    
110
void OPPROTO op_cmpl_T0_T1_cc(void)
111
{
112
    CC_SRC = T1;
113
    CC_DST = T0 - T1;
114
}
115

    
116
void OPPROTO op_update_inc_cc(void)
117
{
118
    CC_SRC = cc_table[CC_OP].compute_c();
119
    CC_DST = T0;
120
}
121

    
122
void OPPROTO op_testl_T0_T1_cc(void)
123
{
124
    CC_DST = T0 & T1;
125
}
126

    
127
/* operations without flags */
128

    
129
void OPPROTO op_addl_T0_T1(void)
130
{
131
    T0 += T1;
132
}
133

    
134
void OPPROTO op_orl_T0_T1(void)
135
{
136
    T0 |= T1;
137
}
138

    
139
void OPPROTO op_andl_T0_T1(void)
140
{
141
    T0 &= T1;
142
}
143

    
144
void OPPROTO op_subl_T0_T1(void)
145
{
146
    T0 -= T1;
147
}
148

    
149
void OPPROTO op_xorl_T0_T1(void)
150
{
151
    T0 ^= T1;
152
}
153

    
154
void OPPROTO op_negl_T0(void)
155
{
156
    T0 = -T0;
157
}
158

    
159
void OPPROTO op_incl_T0(void)
160
{
161
    T0++;
162
}
163

    
164
void OPPROTO op_decl_T0(void)
165
{
166
    T0--;
167
}
168

    
169
void OPPROTO op_notl_T0(void)
170
{
171
    T0 = ~T0;
172
}
173

    
174
void OPPROTO op_bswapl_T0(void)
175
{
176
    T0 = bswap32(T0);
177
}
178

    
179
/* multiply/divide */
180

    
181
/* XXX: add eflags optimizations */
182
/* XXX: add non P4 style flags */
183

    
184
void OPPROTO op_mulb_AL_T0(void)
185
{
186
    unsigned int res;
187
    res = (uint8_t)EAX * (uint8_t)T0;
188
    EAX = (EAX & 0xffff0000) | res;
189
    CC_DST = res;
190
    CC_SRC = (res & 0xff00);
191
}
192

    
193
void OPPROTO op_imulb_AL_T0(void)
194
{
195
    int res;
196
    res = (int8_t)EAX * (int8_t)T0;
197
    EAX = (EAX & 0xffff0000) | (res & 0xffff);
198
    CC_DST = res;
199
    CC_SRC = (res != (int8_t)res);
200
}
201

    
202
void OPPROTO op_mulw_AX_T0(void)
203
{
204
    unsigned int res;
205
    res = (uint16_t)EAX * (uint16_t)T0;
206
    EAX = (EAX & 0xffff0000) | (res & 0xffff);
207
    EDX = (EDX & 0xffff0000) | ((res >> 16) & 0xffff);
208
    CC_DST = res;
209
    CC_SRC = res >> 16;
210
}
211

    
212
void OPPROTO op_imulw_AX_T0(void)
213
{
214
    int res;
215
    res = (int16_t)EAX * (int16_t)T0;
216
    EAX = (EAX & 0xffff0000) | (res & 0xffff);
217
    EDX = (EDX & 0xffff0000) | ((res >> 16) & 0xffff);
218
    CC_DST = res;
219
    CC_SRC = (res != (int16_t)res);
220
}
221

    
222
void OPPROTO op_mull_EAX_T0(void)
223
{
224
    uint64_t res;
225
    res = (uint64_t)((uint32_t)EAX) * (uint64_t)((uint32_t)T0);
226
    EAX = res;
227
    EDX = res >> 32;
228
    CC_DST = res;
229
    CC_SRC = res >> 32;
230
}
231

    
232
void OPPROTO op_imull_EAX_T0(void)
233
{
234
    int64_t res;
235
    res = (int64_t)((int32_t)EAX) * (int64_t)((int32_t)T0);
236
    EAX = res;
237
    EDX = res >> 32;
238
    CC_DST = res;
239
    CC_SRC = (res != (int32_t)res);
240
}
241

    
242
void OPPROTO op_imulw_T0_T1(void)
243
{
244
    int res;
245
    res = (int16_t)T0 * (int16_t)T1;
246
    T0 = res;
247
    CC_DST = res;
248
    CC_SRC = (res != (int16_t)res);
249
}
250

    
251
void OPPROTO op_imull_T0_T1(void)
252
{
253
    int64_t res;
254
    res = (int64_t)((int32_t)T0) * (int64_t)((int32_t)T1);
255
    T0 = res;
256
    CC_DST = res;
257
    CC_SRC = (res != (int32_t)res);
258
}
259

    
260
/* division, flags are undefined */
261
/* XXX: add exceptions for overflow */
262

    
263
void OPPROTO op_divb_AL_T0(void)
264
{
265
    unsigned int num, den, q, r;
266

    
267
    num = (EAX & 0xffff);
268
    den = (T0 & 0xff);
269
    if (den == 0) {
270
        EIP = PARAM1;
271
        raise_exception(EXCP00_DIVZ);
272
    }
273
    q = (num / den) & 0xff;
274
    r = (num % den) & 0xff;
275
    EAX = (EAX & 0xffff0000) | (r << 8) | q;
276
}
277

    
278
void OPPROTO op_idivb_AL_T0(void)
279
{
280
    int num, den, q, r;
281

    
282
    num = (int16_t)EAX;
283
    den = (int8_t)T0;
284
    if (den == 0) {
285
        EIP = PARAM1;
286
        raise_exception(EXCP00_DIVZ);
287
    }
288
    q = (num / den) & 0xff;
289
    r = (num % den) & 0xff;
290
    EAX = (EAX & 0xffff0000) | (r << 8) | q;
291
}
292

    
293
void OPPROTO op_divw_AX_T0(void)
294
{
295
    unsigned int num, den, q, r;
296

    
297
    num = (EAX & 0xffff) | ((EDX & 0xffff) << 16);
298
    den = (T0 & 0xffff);
299
    if (den == 0) {
300
        EIP = PARAM1;
301
        raise_exception(EXCP00_DIVZ);
302
    }
303
    q = (num / den) & 0xffff;
304
    r = (num % den) & 0xffff;
305
    EAX = (EAX & 0xffff0000) | q;
306
    EDX = (EDX & 0xffff0000) | r;
307
}
308

    
309
void OPPROTO op_idivw_AX_T0(void)
310
{
311
    int num, den, q, r;
312

    
313
    num = (EAX & 0xffff) | ((EDX & 0xffff) << 16);
314
    den = (int16_t)T0;
315
    if (den == 0) {
316
        EIP = PARAM1;
317
        raise_exception(EXCP00_DIVZ);
318
    }
319
    q = (num / den) & 0xffff;
320
    r = (num % den) & 0xffff;
321
    EAX = (EAX & 0xffff0000) | q;
322
    EDX = (EDX & 0xffff0000) | r;
323
}
324

    
325
void OPPROTO op_divl_EAX_T0(void)
326
{
327
    helper_divl_EAX_T0(PARAM1);
328
}
329

    
330
void OPPROTO op_idivl_EAX_T0(void)
331
{
332
    helper_idivl_EAX_T0(PARAM1);
333
}
334

    
335
/* constant load & misc op */
336

    
337
void OPPROTO op_movl_T0_im(void)
338
{
339
    T0 = PARAM1;
340
}
341

    
342
void OPPROTO op_addl_T0_im(void)
343
{
344
    T0 += PARAM1;
345
}
346

    
347
void OPPROTO op_andl_T0_ffff(void)
348
{
349
    T0 = T0 & 0xffff;
350
}
351

    
352
void OPPROTO op_andl_T0_im(void)
353
{
354
    T0 = T0 & PARAM1;
355
}
356

    
357
void OPPROTO op_movl_T0_T1(void)
358
{
359
    T0 = T1;
360
}
361

    
362
void OPPROTO op_movl_T1_im(void)
363
{
364
    T1 = PARAM1;
365
}
366

    
367
void OPPROTO op_addl_T1_im(void)
368
{
369
    T1 += PARAM1;
370
}
371

    
372
void OPPROTO op_movl_T1_A0(void)
373
{
374
    T1 = A0;
375
}
376

    
377
void OPPROTO op_movl_A0_im(void)
378
{
379
    A0 = PARAM1;
380
}
381

    
382
void OPPROTO op_addl_A0_im(void)
383
{
384
    A0 += PARAM1;
385
}
386

    
387
void OPPROTO op_addl_A0_AL(void)
388
{
389
    A0 += (EAX & 0xff);
390
}
391

    
392
void OPPROTO op_andl_A0_ffff(void)
393
{
394
    A0 = A0 & 0xffff;
395
}
396

    
397
/* memory access */
398

    
399
#define MEMSUFFIX _raw
400
#include "ops_mem.h"
401

    
402
#if !defined(CONFIG_USER_ONLY)
403
#define MEMSUFFIX _kernel
404
#include "ops_mem.h"
405

    
406
#define MEMSUFFIX _user
407
#include "ops_mem.h"
408
#endif
409

    
410
/* used for bit operations */
411

    
412
void OPPROTO op_add_bitw_A0_T1(void)
413
{
414
    A0 += ((int16_t)T1 >> 4) << 1;
415
}
416

    
417
void OPPROTO op_add_bitl_A0_T1(void)
418
{
419
    A0 += ((int32_t)T1 >> 5) << 2;
420
}
421

    
422
/* indirect jump */
423

    
424
void OPPROTO op_jmp_T0(void)
425
{
426
    EIP = T0;
427
}
428

    
429
void OPPROTO op_jmp_im(void)
430
{
431
    EIP = PARAM1;
432
}
433

    
434
void OPPROTO op_hlt(void)
435
{
436
    env->hflags &= ~HF_INHIBIT_IRQ_MASK; /* needed if sti is just before */
437
    env->exception_index = EXCP_HLT;
438
    cpu_loop_exit();
439
}
440

    
441
void OPPROTO op_debug(void)
442
{
443
    env->exception_index = EXCP_DEBUG;
444
    cpu_loop_exit();
445
}
446

    
447
void OPPROTO op_raise_interrupt(void)
448
{
449
    int intno;
450
    unsigned int next_eip;
451
    intno = PARAM1;
452
    next_eip = PARAM2;
453
    raise_interrupt(intno, 1, 0, next_eip);
454
}
455

    
456
void OPPROTO op_raise_exception(void)
457
{
458
    int exception_index;
459
    exception_index = PARAM1;
460
    raise_exception(exception_index);
461
}
462

    
463
void OPPROTO op_into(void)
464
{
465
    int eflags;
466
    eflags = cc_table[CC_OP].compute_all();
467
    if (eflags & CC_O) {
468
        raise_interrupt(EXCP04_INTO, 1, 0, PARAM1);
469
    }
470
    FORCE_RET();
471
}
472

    
473
void OPPROTO op_cli(void)
474
{
475
    env->eflags &= ~IF_MASK;
476
}
477

    
478
void OPPROTO op_sti(void)
479
{
480
    env->eflags |= IF_MASK;
481
}
482

    
483
void OPPROTO op_set_inhibit_irq(void)
484
{
485
    env->hflags |= HF_INHIBIT_IRQ_MASK;
486
}
487

    
488
void OPPROTO op_reset_inhibit_irq(void)
489
{
490
    env->hflags &= ~HF_INHIBIT_IRQ_MASK;
491
}
492

    
493
#if 0
494
/* vm86plus instructions */
495
void OPPROTO op_cli_vm(void)
496
{
497
    env->eflags &= ~VIF_MASK;
498
}
499

500
void OPPROTO op_sti_vm(void)
501
{
502
    env->eflags |= VIF_MASK;
503
    if (env->eflags & VIP_MASK) {
504
        EIP = PARAM1;
505
        raise_exception(EXCP0D_GPF);
506
    }
507
    FORCE_RET();
508
}
509
#endif
510

    
511
void OPPROTO op_boundw(void)
512
{
513
    int low, high, v;
514
    low = ldsw((uint8_t *)A0);
515
    high = ldsw((uint8_t *)A0 + 2);
516
    v = (int16_t)T0;
517
    if (v < low || v > high) {
518
        EIP = PARAM1;
519
        raise_exception(EXCP05_BOUND);
520
    }
521
    FORCE_RET();
522
}
523

    
524
void OPPROTO op_boundl(void)
525
{
526
    int low, high, v;
527
    low = ldl((uint8_t *)A0);
528
    high = ldl((uint8_t *)A0 + 4);
529
    v = T0;
530
    if (v < low || v > high) {
531
        EIP = PARAM1;
532
        raise_exception(EXCP05_BOUND);
533
    }
534
    FORCE_RET();
535
}
536

    
537
void OPPROTO op_cmpxchg8b(void)
538
{
539
    helper_cmpxchg8b();
540
}
541

    
542
void OPPROTO op_jmp(void)
543
{
544
    JUMP_TB(op_jmp, PARAM1, 0, PARAM2);
545
}
546

    
547
void OPPROTO op_movl_T0_0(void)
548
{
549
    T0 = 0;
550
}
551

    
552
void OPPROTO op_exit_tb(void)
553
{
554
    EXIT_TB();
555
}
556

    
557
/* multiple size ops */
558

    
559
#define ldul ldl
560

    
561
#define SHIFT 0
562
#include "ops_template.h"
563
#undef SHIFT
564

    
565
#define SHIFT 1
566
#include "ops_template.h"
567
#undef SHIFT
568

    
569
#define SHIFT 2
570
#include "ops_template.h"
571
#undef SHIFT
572

    
573
/* sign extend */
574

    
575
void OPPROTO op_movsbl_T0_T0(void)
576
{
577
    T0 = (int8_t)T0;
578
}
579

    
580
void OPPROTO op_movzbl_T0_T0(void)
581
{
582
    T0 = (uint8_t)T0;
583
}
584

    
585
void OPPROTO op_movswl_T0_T0(void)
586
{
587
    T0 = (int16_t)T0;
588
}
589

    
590
void OPPROTO op_movzwl_T0_T0(void)
591
{
592
    T0 = (uint16_t)T0;
593
}
594

    
595
void OPPROTO op_movswl_EAX_AX(void)
596
{
597
    EAX = (int16_t)EAX;
598
}
599

    
600
void OPPROTO op_movsbw_AX_AL(void)
601
{
602
    EAX = (EAX & 0xffff0000) | ((int8_t)EAX & 0xffff);
603
}
604

    
605
void OPPROTO op_movslq_EDX_EAX(void)
606
{
607
    EDX = (int32_t)EAX >> 31;
608
}
609

    
610
void OPPROTO op_movswl_DX_AX(void)
611
{
612
    EDX = (EDX & 0xffff0000) | (((int16_t)EAX >> 15) & 0xffff);
613
}
614

    
615
/* string ops helpers */
616

    
617
void OPPROTO op_addl_ESI_T0(void)
618
{
619
    ESI += T0;
620
}
621

    
622
void OPPROTO op_addw_ESI_T0(void)
623
{
624
    ESI = (ESI & ~0xffff) | ((ESI + T0) & 0xffff);
625
}
626

    
627
void OPPROTO op_addl_EDI_T0(void)
628
{
629
    EDI += T0;
630
}
631

    
632
void OPPROTO op_addw_EDI_T0(void)
633
{
634
    EDI = (EDI & ~0xffff) | ((EDI + T0) & 0xffff);
635
}
636

    
637
void OPPROTO op_decl_ECX(void)
638
{
639
    ECX--;
640
}
641

    
642
void OPPROTO op_decw_ECX(void)
643
{
644
    ECX = (ECX & ~0xffff) | ((ECX - 1) & 0xffff);
645
}
646

    
647
/* push/pop utils */
648

    
649
void op_addl_A0_SS(void)
650
{
651
    A0 += (long)env->segs[R_SS].base;
652
}
653

    
654
void op_subl_A0_2(void)
655
{
656
    A0 -= 2;
657
}
658

    
659
void op_subl_A0_4(void)
660
{
661
    A0 -= 4;
662
}
663

    
664
void op_addl_ESP_4(void)
665
{
666
    ESP += 4;
667
}
668

    
669
void op_addl_ESP_2(void)
670
{
671
    ESP += 2;
672
}
673

    
674
void op_addw_ESP_4(void)
675
{
676
    ESP = (ESP & ~0xffff) | ((ESP + 4) & 0xffff);
677
}
678

    
679
void op_addw_ESP_2(void)
680
{
681
    ESP = (ESP & ~0xffff) | ((ESP + 2) & 0xffff);
682
}
683

    
684
void op_addl_ESP_im(void)
685
{
686
    ESP += PARAM1;
687
}
688

    
689
void op_addw_ESP_im(void)
690
{
691
    ESP = (ESP & ~0xffff) | ((ESP + PARAM1) & 0xffff);
692
}
693

    
694
void OPPROTO op_rdtsc(void)
695
{
696
    helper_rdtsc();
697
}
698

    
699
void OPPROTO op_cpuid(void)
700
{
701
    helper_cpuid();
702
}
703

    
704
void OPPROTO op_sysenter(void)
705
{
706
    helper_sysenter();
707
}
708

    
709
void OPPROTO op_sysexit(void)
710
{
711
    helper_sysexit();
712
}
713

    
714
void OPPROTO op_rdmsr(void)
715
{
716
    helper_rdmsr();
717
}
718

    
719
void OPPROTO op_wrmsr(void)
720
{
721
    helper_wrmsr();
722
}
723

    
724
/* bcd */
725

    
726
/* XXX: exception */
727
void OPPROTO op_aam(void)
728
{
729
    int base = PARAM1;
730
    int al, ah;
731
    al = EAX & 0xff;
732
    ah = al / base;
733
    al = al % base;
734
    EAX = (EAX & ~0xffff) | al | (ah << 8);
735
    CC_DST = al;
736
}
737

    
738
void OPPROTO op_aad(void)
739
{
740
    int base = PARAM1;
741
    int al, ah;
742
    al = EAX & 0xff;
743
    ah = (EAX >> 8) & 0xff;
744
    al = ((ah * base) + al) & 0xff;
745
    EAX = (EAX & ~0xffff) | al;
746
    CC_DST = al;
747
}
748

    
749
void OPPROTO op_aaa(void)
750
{
751
    int icarry;
752
    int al, ah, af;
753
    int eflags;
754

    
755
    eflags = cc_table[CC_OP].compute_all();
756
    af = eflags & CC_A;
757
    al = EAX & 0xff;
758
    ah = (EAX >> 8) & 0xff;
759

    
760
    icarry = (al > 0xf9);
761
    if (((al & 0x0f) > 9 ) || af) {
762
        al = (al + 6) & 0x0f;
763
        ah = (ah + 1 + icarry) & 0xff;
764
        eflags |= CC_C | CC_A;
765
    } else {
766
        eflags &= ~(CC_C | CC_A);
767
        al &= 0x0f;
768
    }
769
    EAX = (EAX & ~0xffff) | al | (ah << 8);
770
    CC_SRC = eflags;
771
}
772

    
773
void OPPROTO op_aas(void)
774
{
775
    int icarry;
776
    int al, ah, af;
777
    int eflags;
778

    
779
    eflags = cc_table[CC_OP].compute_all();
780
    af = eflags & CC_A;
781
    al = EAX & 0xff;
782
    ah = (EAX >> 8) & 0xff;
783

    
784
    icarry = (al < 6);
785
    if (((al & 0x0f) > 9 ) || af) {
786
        al = (al - 6) & 0x0f;
787
        ah = (ah - 1 - icarry) & 0xff;
788
        eflags |= CC_C | CC_A;
789
    } else {
790
        eflags &= ~(CC_C | CC_A);
791
        al &= 0x0f;
792
    }
793
    EAX = (EAX & ~0xffff) | al | (ah << 8);
794
    CC_SRC = eflags;
795
}
796

    
797
void OPPROTO op_daa(void)
798
{
799
    int al, af, cf;
800
    int eflags;
801

    
802
    eflags = cc_table[CC_OP].compute_all();
803
    cf = eflags & CC_C;
804
    af = eflags & CC_A;
805
    al = EAX & 0xff;
806

    
807
    eflags = 0;
808
    if (((al & 0x0f) > 9 ) || af) {
809
        al = (al + 6) & 0xff;
810
        eflags |= CC_A;
811
    }
812
    if ((al > 0x9f) || cf) {
813
        al = (al + 0x60) & 0xff;
814
        eflags |= CC_C;
815
    }
816
    EAX = (EAX & ~0xff) | al;
817
    /* well, speed is not an issue here, so we compute the flags by hand */
818
    eflags |= (al == 0) << 6; /* zf */
819
    eflags |= parity_table[al]; /* pf */
820
    eflags |= (al & 0x80); /* sf */
821
    CC_SRC = eflags;
822
}
823

    
824
void OPPROTO op_das(void)
825
{
826
    int al, al1, af, cf;
827
    int eflags;
828

    
829
    eflags = cc_table[CC_OP].compute_all();
830
    cf = eflags & CC_C;
831
    af = eflags & CC_A;
832
    al = EAX & 0xff;
833

    
834
    eflags = 0;
835
    al1 = al;
836
    if (((al & 0x0f) > 9 ) || af) {
837
        eflags |= CC_A;
838
        if (al < 6 || cf)
839
            eflags |= CC_C;
840
        al = (al - 6) & 0xff;
841
    }
842
    if ((al1 > 0x99) || cf) {
843
        al = (al - 0x60) & 0xff;
844
        eflags |= CC_C;
845
    }
846
    EAX = (EAX & ~0xff) | al;
847
    /* well, speed is not an issue here, so we compute the flags by hand */
848
    eflags |= (al == 0) << 6; /* zf */
849
    eflags |= parity_table[al]; /* pf */
850
    eflags |= (al & 0x80); /* sf */
851
    CC_SRC = eflags;
852
}
853

    
854
/* segment handling */
855

    
856
/* never use it with R_CS */
857
void OPPROTO op_movl_seg_T0(void)
858
{
859
    load_seg(PARAM1, T0);
860
}
861

    
862
/* faster VM86 version */
863
void OPPROTO op_movl_seg_T0_vm(void)
864
{
865
    int selector;
866
    SegmentCache *sc;
867
    
868
    selector = T0 & 0xffff;
869
    /* env->segs[] access */
870
    sc = (SegmentCache *)((char *)env + PARAM1);
871
    sc->selector = selector;
872
    sc->base = (void *)(selector << 4);
873
}
874

    
875
void OPPROTO op_movl_T0_seg(void)
876
{
877
    T0 = env->segs[PARAM1].selector;
878
}
879

    
880
void OPPROTO op_movl_A0_seg(void)
881
{
882
    A0 = *(unsigned long *)((char *)env + PARAM1);
883
}
884

    
885
void OPPROTO op_addl_A0_seg(void)
886
{
887
    A0 += *(unsigned long *)((char *)env + PARAM1);
888
}
889

    
890
void OPPROTO op_lsl(void)
891
{
892
    helper_lsl();
893
}
894

    
895
void OPPROTO op_lar(void)
896
{
897
    helper_lar();
898
}
899

    
900
void OPPROTO op_verr(void)
901
{
902
    helper_verr();
903
}
904

    
905
void OPPROTO op_verw(void)
906
{
907
    helper_verw();
908
}
909

    
910
void OPPROTO op_arpl(void)
911
{
912
    if ((T0 & 3) < (T1 & 3)) {
913
        /* XXX: emulate bug or 0xff3f0000 oring as in bochs ? */
914
        T0 = (T0 & ~3) | (T1 & 3);
915
        T1 = CC_Z;
916
   } else {
917
        T1 = 0;
918
    }
919
    FORCE_RET();
920
}
921
            
922
void OPPROTO op_arpl_update(void)
923
{
924
    int eflags;
925
    eflags = cc_table[CC_OP].compute_all();
926
    CC_SRC = (eflags & ~CC_Z) | T1;
927
}
928
    
929
/* T0: segment, T1:eip */
930
void OPPROTO op_ljmp_protected_T0_T1(void)
931
{
932
    helper_ljmp_protected_T0_T1(PARAM1);
933
}
934

    
935
void OPPROTO op_lcall_real_T0_T1(void)
936
{
937
    helper_lcall_real_T0_T1(PARAM1, PARAM2);
938
}
939

    
940
void OPPROTO op_lcall_protected_T0_T1(void)
941
{
942
    helper_lcall_protected_T0_T1(PARAM1, PARAM2);
943
}
944

    
945
void OPPROTO op_iret_real(void)
946
{
947
    helper_iret_real(PARAM1);
948
}
949

    
950
void OPPROTO op_iret_protected(void)
951
{
952
    helper_iret_protected(PARAM1, PARAM2);
953
}
954

    
955
void OPPROTO op_lret_protected(void)
956
{
957
    helper_lret_protected(PARAM1, PARAM2);
958
}
959

    
960
void OPPROTO op_lldt_T0(void)
961
{
962
    helper_lldt_T0();
963
}
964

    
965
void OPPROTO op_ltr_T0(void)
966
{
967
    helper_ltr_T0();
968
}
969

    
970
/* CR registers access */
971
void OPPROTO op_movl_crN_T0(void)
972
{
973
    helper_movl_crN_T0(PARAM1);
974
}
975

    
976
/* DR registers access */
977
void OPPROTO op_movl_drN_T0(void)
978
{
979
    helper_movl_drN_T0(PARAM1);
980
}
981

    
982
void OPPROTO op_lmsw_T0(void)
983
{
984
    /* only 4 lower bits of CR0 are modified. PE cannot be set to zero
985
       if already set to one. */
986
    T0 = (env->cr[0] & ~0xe) | (T0 & 0xf);
987
    helper_movl_crN_T0(0);
988
}
989

    
990
void OPPROTO op_invlpg_A0(void)
991
{
992
    helper_invlpg(A0);
993
}
994

    
995
void OPPROTO op_movl_T0_env(void)
996
{
997
    T0 = *(uint32_t *)((char *)env + PARAM1);
998
}
999

    
1000
void OPPROTO op_movl_env_T0(void)
1001
{
1002
    *(uint32_t *)((char *)env + PARAM1) = T0;
1003
}
1004

    
1005
void OPPROTO op_movl_env_T1(void)
1006
{
1007
    *(uint32_t *)((char *)env + PARAM1) = T1;
1008
}
1009

    
1010
void OPPROTO op_clts(void)
1011
{
1012
    env->cr[0] &= ~CR0_TS_MASK;
1013
    env->hflags &= ~HF_TS_MASK;
1014
}
1015

    
1016
/* flags handling */
1017

    
1018
/* slow jumps cases : in order to avoid calling a function with a
1019
   pointer (which can generate a stack frame on PowerPC), we use
1020
   op_setcc to set T0 and then call op_jcc. */
1021
void OPPROTO op_jcc(void)
1022
{
1023
    if (T0)
1024
        JUMP_TB(op_jcc, PARAM1, 0, PARAM2);
1025
    else
1026
        JUMP_TB(op_jcc, PARAM1, 1, PARAM3);
1027
    FORCE_RET();
1028
}
1029

    
1030
void OPPROTO op_jcc_im(void)
1031
{
1032
    if (T0)
1033
        EIP = PARAM1;
1034
    else
1035
        EIP = PARAM2;
1036
    FORCE_RET();
1037
}
1038

    
1039
/* slow set cases (compute x86 flags) */
1040
void OPPROTO op_seto_T0_cc(void)
1041
{
1042
    int eflags;
1043
    eflags = cc_table[CC_OP].compute_all();
1044
    T0 = (eflags >> 11) & 1;
1045
}
1046

    
1047
void OPPROTO op_setb_T0_cc(void)
1048
{
1049
    T0 = cc_table[CC_OP].compute_c();
1050
}
1051

    
1052
void OPPROTO op_setz_T0_cc(void)
1053
{
1054
    int eflags;
1055
    eflags = cc_table[CC_OP].compute_all();
1056
    T0 = (eflags >> 6) & 1;
1057
}
1058

    
1059
void OPPROTO op_setbe_T0_cc(void)
1060
{
1061
    int eflags;
1062
    eflags = cc_table[CC_OP].compute_all();
1063
    T0 = (eflags & (CC_Z | CC_C)) != 0;
1064
}
1065

    
1066
void OPPROTO op_sets_T0_cc(void)
1067
{
1068
    int eflags;
1069
    eflags = cc_table[CC_OP].compute_all();
1070
    T0 = (eflags >> 7) & 1;
1071
}
1072

    
1073
void OPPROTO op_setp_T0_cc(void)
1074
{
1075
    int eflags;
1076
    eflags = cc_table[CC_OP].compute_all();
1077
    T0 = (eflags >> 2) & 1;
1078
}
1079

    
1080
void OPPROTO op_setl_T0_cc(void)
1081
{
1082
    int eflags;
1083
    eflags = cc_table[CC_OP].compute_all();
1084
    T0 = ((eflags ^ (eflags >> 4)) >> 7) & 1;
1085
}
1086

    
1087
void OPPROTO op_setle_T0_cc(void)
1088
{
1089
    int eflags;
1090
    eflags = cc_table[CC_OP].compute_all();
1091
    T0 = (((eflags ^ (eflags >> 4)) & 0x80) || (eflags & CC_Z)) != 0;
1092
}
1093

    
1094
void OPPROTO op_xor_T0_1(void)
1095
{
1096
    T0 ^= 1;
1097
}
1098

    
1099
void OPPROTO op_set_cc_op(void)
1100
{
1101
    CC_OP = PARAM1;
1102
}
1103

    
1104
/* XXX: clear VIF/VIP in all ops ? */
1105

    
1106
void OPPROTO op_movl_eflags_T0(void)
1107
{
1108
    load_eflags(T0, (TF_MASK | AC_MASK | ID_MASK | NT_MASK));
1109
}
1110

    
1111
void OPPROTO op_movw_eflags_T0(void)
1112
{
1113
    load_eflags(T0, (TF_MASK | AC_MASK | ID_MASK | NT_MASK) & 0xffff);
1114
}
1115

    
1116
void OPPROTO op_movl_eflags_T0_io(void)
1117
{
1118
    load_eflags(T0, (TF_MASK | AC_MASK | ID_MASK | NT_MASK | IF_MASK));
1119
}
1120

    
1121
void OPPROTO op_movw_eflags_T0_io(void)
1122
{
1123
    load_eflags(T0, (TF_MASK | AC_MASK | ID_MASK | NT_MASK | IF_MASK) & 0xffff);
1124
}
1125

    
1126
void OPPROTO op_movl_eflags_T0_cpl0(void)
1127
{
1128
    load_eflags(T0, (TF_MASK | AC_MASK | ID_MASK | NT_MASK | IF_MASK | IOPL_MASK));
1129
}
1130

    
1131
void OPPROTO op_movw_eflags_T0_cpl0(void)
1132
{
1133
    load_eflags(T0, (TF_MASK | AC_MASK | ID_MASK | NT_MASK | IF_MASK | IOPL_MASK) & 0xffff);
1134
}
1135

    
1136
#if 0
1137
/* vm86plus version */
1138
void OPPROTO op_movw_eflags_T0_vm(void)
1139
{
1140
    int eflags;
1141
    eflags = T0;
1142
    CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1143
    DF = 1 - (2 * ((eflags >> 10) & 1));
1144
    /* we also update some system flags as in user mode */
1145
    env->eflags = (env->eflags & ~(FL_UPDATE_MASK16 | VIF_MASK)) |
1146
        (eflags & FL_UPDATE_MASK16);
1147
    if (eflags & IF_MASK) {
1148
        env->eflags |= VIF_MASK;
1149
        if (env->eflags & VIP_MASK) {
1150
            EIP = PARAM1;
1151
            raise_exception(EXCP0D_GPF);
1152
        }
1153
    }
1154
    FORCE_RET();
1155
}
1156

1157
void OPPROTO op_movl_eflags_T0_vm(void)
1158
{
1159
    int eflags;
1160
    eflags = T0;
1161
    CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1162
    DF = 1 - (2 * ((eflags >> 10) & 1));
1163
    /* we also update some system flags as in user mode */
1164
    env->eflags = (env->eflags & ~(FL_UPDATE_MASK32 | VIF_MASK)) |
1165
        (eflags & FL_UPDATE_MASK32);
1166
    if (eflags & IF_MASK) {
1167
        env->eflags |= VIF_MASK;
1168
        if (env->eflags & VIP_MASK) {
1169
            EIP = PARAM1;
1170
            raise_exception(EXCP0D_GPF);
1171
        }
1172
    }
1173
    FORCE_RET();
1174
}
1175
#endif
1176

    
1177
/* XXX: compute only O flag */
1178
void OPPROTO op_movb_eflags_T0(void)
1179
{
1180
    int of;
1181
    of = cc_table[CC_OP].compute_all() & CC_O;
1182
    CC_SRC = (T0 & (CC_S | CC_Z | CC_A | CC_P | CC_C)) | of;
1183
}
1184

    
1185
void OPPROTO op_movl_T0_eflags(void)
1186
{
1187
    int eflags;
1188
    eflags = cc_table[CC_OP].compute_all();
1189
    eflags |= (DF & DF_MASK);
1190
    eflags |= env->eflags & ~(VM_MASK | RF_MASK);
1191
    T0 = eflags;
1192
}
1193

    
1194
/* vm86plus version */
1195
#if 0
1196
void OPPROTO op_movl_T0_eflags_vm(void)
1197
{
1198
    int eflags;
1199
    eflags = cc_table[CC_OP].compute_all();
1200
    eflags |= (DF & DF_MASK);
1201
    eflags |= env->eflags & ~(VM_MASK | RF_MASK | IF_MASK);
1202
    if (env->eflags & VIF_MASK)
1203
        eflags |= IF_MASK;
1204
    T0 = eflags;
1205
}
1206
#endif
1207

    
1208
void OPPROTO op_cld(void)
1209
{
1210
    DF = 1;
1211
}
1212

    
1213
void OPPROTO op_std(void)
1214
{
1215
    DF = -1;
1216
}
1217

    
1218
void OPPROTO op_clc(void)
1219
{
1220
    int eflags;
1221
    eflags = cc_table[CC_OP].compute_all();
1222
    eflags &= ~CC_C;
1223
    CC_SRC = eflags;
1224
}
1225

    
1226
void OPPROTO op_stc(void)
1227
{
1228
    int eflags;
1229
    eflags = cc_table[CC_OP].compute_all();
1230
    eflags |= CC_C;
1231
    CC_SRC = eflags;
1232
}
1233

    
1234
void OPPROTO op_cmc(void)
1235
{
1236
    int eflags;
1237
    eflags = cc_table[CC_OP].compute_all();
1238
    eflags ^= CC_C;
1239
    CC_SRC = eflags;
1240
}
1241

    
1242
void OPPROTO op_salc(void)
1243
{
1244
    int cf;
1245
    cf = cc_table[CC_OP].compute_c();
1246
    EAX = (EAX & ~0xff) | ((-cf) & 0xff);
1247
}
1248

    
1249
static int compute_all_eflags(void)
1250
{
1251
    return CC_SRC;
1252
}
1253

    
1254
static int compute_c_eflags(void)
1255
{
1256
    return CC_SRC & CC_C;
1257
}
1258

    
1259
CCTable cc_table[CC_OP_NB] = {
1260
    [CC_OP_DYNAMIC] = { /* should never happen */ },
1261

    
1262
    [CC_OP_EFLAGS] = { compute_all_eflags, compute_c_eflags },
1263

    
1264
    [CC_OP_MULB] = { compute_all_mulb, compute_c_mull },
1265
    [CC_OP_MULW] = { compute_all_mulw, compute_c_mull },
1266
    [CC_OP_MULL] = { compute_all_mull, compute_c_mull },
1267

    
1268
    [CC_OP_ADDB] = { compute_all_addb, compute_c_addb },
1269
    [CC_OP_ADDW] = { compute_all_addw, compute_c_addw  },
1270
    [CC_OP_ADDL] = { compute_all_addl, compute_c_addl  },
1271

    
1272
    [CC_OP_ADCB] = { compute_all_adcb, compute_c_adcb },
1273
    [CC_OP_ADCW] = { compute_all_adcw, compute_c_adcw  },
1274
    [CC_OP_ADCL] = { compute_all_adcl, compute_c_adcl  },
1275

    
1276
    [CC_OP_SUBB] = { compute_all_subb, compute_c_subb  },
1277
    [CC_OP_SUBW] = { compute_all_subw, compute_c_subw  },
1278
    [CC_OP_SUBL] = { compute_all_subl, compute_c_subl  },
1279
    
1280
    [CC_OP_SBBB] = { compute_all_sbbb, compute_c_sbbb  },
1281
    [CC_OP_SBBW] = { compute_all_sbbw, compute_c_sbbw  },
1282
    [CC_OP_SBBL] = { compute_all_sbbl, compute_c_sbbl  },
1283
    
1284
    [CC_OP_LOGICB] = { compute_all_logicb, compute_c_logicb },
1285
    [CC_OP_LOGICW] = { compute_all_logicw, compute_c_logicw },
1286
    [CC_OP_LOGICL] = { compute_all_logicl, compute_c_logicl },
1287
    
1288
    [CC_OP_INCB] = { compute_all_incb, compute_c_incl },
1289
    [CC_OP_INCW] = { compute_all_incw, compute_c_incl },
1290
    [CC_OP_INCL] = { compute_all_incl, compute_c_incl },
1291
    
1292
    [CC_OP_DECB] = { compute_all_decb, compute_c_incl },
1293
    [CC_OP_DECW] = { compute_all_decw, compute_c_incl },
1294
    [CC_OP_DECL] = { compute_all_decl, compute_c_incl },
1295
    
1296
    [CC_OP_SHLB] = { compute_all_shlb, compute_c_shlb },
1297
    [CC_OP_SHLW] = { compute_all_shlw, compute_c_shlw },
1298
    [CC_OP_SHLL] = { compute_all_shll, compute_c_shll },
1299

    
1300
    [CC_OP_SARB] = { compute_all_sarb, compute_c_sarl },
1301
    [CC_OP_SARW] = { compute_all_sarw, compute_c_sarl },
1302
    [CC_OP_SARL] = { compute_all_sarl, compute_c_sarl },
1303
};
1304

    
1305
/* floating point support. Some of the code for complicated x87
1306
   functions comes from the LGPL'ed x86 emulator found in the Willows
1307
   TWIN windows emulator. */
1308

    
1309
#if defined(__powerpc__)
1310
extern CPU86_LDouble copysign(CPU86_LDouble, CPU86_LDouble);
1311

    
1312
/* correct (but slow) PowerPC rint() (glibc version is incorrect) */
1313
double qemu_rint(double x)
1314
{
1315
    double y = 4503599627370496.0;
1316
    if (fabs(x) >= y)
1317
        return x;
1318
    if (x < 0) 
1319
        y = -y;
1320
    y = (x + y) - y;
1321
    if (y == 0.0)
1322
        y = copysign(y, x);
1323
    return y;
1324
}
1325

    
1326
#define rint qemu_rint
1327
#endif
1328

    
1329
/* fp load FT0 */
1330

    
1331
void OPPROTO op_flds_FT0_A0(void)
1332
{
1333
#ifdef USE_FP_CONVERT
1334
    FP_CONVERT.i32 = ldl((void *)A0);
1335
    FT0 = FP_CONVERT.f;
1336
#else
1337
    FT0 = ldfl((void *)A0);
1338
#endif
1339
}
1340

    
1341
void OPPROTO op_fldl_FT0_A0(void)
1342
{
1343
#ifdef USE_FP_CONVERT
1344
    FP_CONVERT.i64 = ldq((void *)A0);
1345
    FT0 = FP_CONVERT.d;
1346
#else
1347
    FT0 = ldfq((void *)A0);
1348
#endif
1349
}
1350

    
1351
/* helpers are needed to avoid static constant reference. XXX: find a better way */
1352
#ifdef USE_INT_TO_FLOAT_HELPERS
1353

    
1354
void helper_fild_FT0_A0(void)
1355
{
1356
    FT0 = (CPU86_LDouble)ldsw((void *)A0);
1357
}
1358

    
1359
void helper_fildl_FT0_A0(void)
1360
{
1361
    FT0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1362
}
1363

    
1364
void helper_fildll_FT0_A0(void)
1365
{
1366
    FT0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1367
}
1368

    
1369
void OPPROTO op_fild_FT0_A0(void)
1370
{
1371
    helper_fild_FT0_A0();
1372
}
1373

    
1374
void OPPROTO op_fildl_FT0_A0(void)
1375
{
1376
    helper_fildl_FT0_A0();
1377
}
1378

    
1379
void OPPROTO op_fildll_FT0_A0(void)
1380
{
1381
    helper_fildll_FT0_A0();
1382
}
1383

    
1384
#else
1385

    
1386
void OPPROTO op_fild_FT0_A0(void)
1387
{
1388
#ifdef USE_FP_CONVERT
1389
    FP_CONVERT.i32 = ldsw((void *)A0);
1390
    FT0 = (CPU86_LDouble)FP_CONVERT.i32;
1391
#else
1392
    FT0 = (CPU86_LDouble)ldsw((void *)A0);
1393
#endif
1394
}
1395

    
1396
void OPPROTO op_fildl_FT0_A0(void)
1397
{
1398
#ifdef USE_FP_CONVERT
1399
    FP_CONVERT.i32 = (int32_t) ldl((void *)A0);
1400
    FT0 = (CPU86_LDouble)FP_CONVERT.i32;
1401
#else
1402
    FT0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1403
#endif
1404
}
1405

    
1406
void OPPROTO op_fildll_FT0_A0(void)
1407
{
1408
#ifdef USE_FP_CONVERT
1409
    FP_CONVERT.i64 = (int64_t) ldq((void *)A0);
1410
    FT0 = (CPU86_LDouble)FP_CONVERT.i64;
1411
#else
1412
    FT0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1413
#endif
1414
}
1415
#endif
1416

    
1417
/* fp load ST0 */
1418

    
1419
void OPPROTO op_flds_ST0_A0(void)
1420
{
1421
    int new_fpstt;
1422
    new_fpstt = (env->fpstt - 1) & 7;
1423
#ifdef USE_FP_CONVERT
1424
    FP_CONVERT.i32 = ldl((void *)A0);
1425
    env->fpregs[new_fpstt] = FP_CONVERT.f;
1426
#else
1427
    env->fpregs[new_fpstt] = ldfl((void *)A0);
1428
#endif
1429
    env->fpstt = new_fpstt;
1430
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1431
}
1432

    
1433
void OPPROTO op_fldl_ST0_A0(void)
1434
{
1435
    int new_fpstt;
1436
    new_fpstt = (env->fpstt - 1) & 7;
1437
#ifdef USE_FP_CONVERT
1438
    FP_CONVERT.i64 = ldq((void *)A0);
1439
    env->fpregs[new_fpstt] = FP_CONVERT.d;
1440
#else
1441
    env->fpregs[new_fpstt] = ldfq((void *)A0);
1442
#endif
1443
    env->fpstt = new_fpstt;
1444
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1445
}
1446

    
1447
void OPPROTO op_fldt_ST0_A0(void)
1448
{
1449
    helper_fldt_ST0_A0();
1450
}
1451

    
1452
/* helpers are needed to avoid static constant reference. XXX: find a better way */
1453
#ifdef USE_INT_TO_FLOAT_HELPERS
1454

    
1455
void helper_fild_ST0_A0(void)
1456
{
1457
    int new_fpstt;
1458
    new_fpstt = (env->fpstt - 1) & 7;
1459
    env->fpregs[new_fpstt] = (CPU86_LDouble)ldsw((void *)A0);
1460
    env->fpstt = new_fpstt;
1461
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1462
}
1463

    
1464
void helper_fildl_ST0_A0(void)
1465
{
1466
    int new_fpstt;
1467
    new_fpstt = (env->fpstt - 1) & 7;
1468
    env->fpregs[new_fpstt] = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1469
    env->fpstt = new_fpstt;
1470
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1471
}
1472

    
1473
void helper_fildll_ST0_A0(void)
1474
{
1475
    int new_fpstt;
1476
    new_fpstt = (env->fpstt - 1) & 7;
1477
    env->fpregs[new_fpstt] = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1478
    env->fpstt = new_fpstt;
1479
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1480
}
1481

    
1482
void OPPROTO op_fild_ST0_A0(void)
1483
{
1484
    helper_fild_ST0_A0();
1485
}
1486

    
1487
void OPPROTO op_fildl_ST0_A0(void)
1488
{
1489
    helper_fildl_ST0_A0();
1490
}
1491

    
1492
void OPPROTO op_fildll_ST0_A0(void)
1493
{
1494
    helper_fildll_ST0_A0();
1495
}
1496

    
1497
#else
1498

    
1499
void OPPROTO op_fild_ST0_A0(void)
1500
{
1501
    int new_fpstt;
1502
    new_fpstt = (env->fpstt - 1) & 7;
1503
#ifdef USE_FP_CONVERT
1504
    FP_CONVERT.i32 = ldsw((void *)A0);
1505
    env->fpregs[new_fpstt] = (CPU86_LDouble)FP_CONVERT.i32;
1506
#else
1507
    env->fpregs[new_fpstt] = (CPU86_LDouble)ldsw((void *)A0);
1508
#endif
1509
    env->fpstt = new_fpstt;
1510
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1511
}
1512

    
1513
void OPPROTO op_fildl_ST0_A0(void)
1514
{
1515
    int new_fpstt;
1516
    new_fpstt = (env->fpstt - 1) & 7;
1517
#ifdef USE_FP_CONVERT
1518
    FP_CONVERT.i32 = (int32_t) ldl((void *)A0);
1519
    env->fpregs[new_fpstt] = (CPU86_LDouble)FP_CONVERT.i32;
1520
#else
1521
    env->fpregs[new_fpstt] = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1522
#endif
1523
    env->fpstt = new_fpstt;
1524
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1525
}
1526

    
1527
void OPPROTO op_fildll_ST0_A0(void)
1528
{
1529
    int new_fpstt;
1530
    new_fpstt = (env->fpstt - 1) & 7;
1531
#ifdef USE_FP_CONVERT
1532
    FP_CONVERT.i64 = (int64_t) ldq((void *)A0);
1533
    env->fpregs[new_fpstt] = (CPU86_LDouble)FP_CONVERT.i64;
1534
#else
1535
    env->fpregs[new_fpstt] = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1536
#endif
1537
    env->fpstt = new_fpstt;
1538
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1539
}
1540

    
1541
#endif
1542

    
1543
/* fp store */
1544

    
1545
void OPPROTO op_fsts_ST0_A0(void)
1546
{
1547
#ifdef USE_FP_CONVERT
1548
    FP_CONVERT.f = (float)ST0;
1549
    stfl((void *)A0, FP_CONVERT.f);
1550
#else
1551
    stfl((void *)A0, (float)ST0);
1552
#endif
1553
}
1554

    
1555
void OPPROTO op_fstl_ST0_A0(void)
1556
{
1557
    stfq((void *)A0, (double)ST0);
1558
}
1559

    
1560
void OPPROTO op_fstt_ST0_A0(void)
1561
{
1562
    helper_fstt_ST0_A0();
1563
}
1564

    
1565
void OPPROTO op_fist_ST0_A0(void)
1566
{
1567
#if defined(__sparc__) && !defined(__sparc_v9__)
1568
    register CPU86_LDouble d asm("o0");
1569
#else
1570
    CPU86_LDouble d;
1571
#endif
1572
    int val;
1573

    
1574
    d = ST0;
1575
    val = lrint(d);
1576
    if (val != (int16_t)val)
1577
        val = -32768;
1578
    stw((void *)A0, val);
1579
}
1580

    
1581
void OPPROTO op_fistl_ST0_A0(void)
1582
{
1583
#if defined(__sparc__) && !defined(__sparc_v9__)
1584
    register CPU86_LDouble d asm("o0");
1585
#else
1586
    CPU86_LDouble d;
1587
#endif
1588
    int val;
1589

    
1590
    d = ST0;
1591
    val = lrint(d);
1592
    stl((void *)A0, val);
1593
}
1594

    
1595
void OPPROTO op_fistll_ST0_A0(void)
1596
{
1597
#if defined(__sparc__) && !defined(__sparc_v9__)
1598
    register CPU86_LDouble d asm("o0");
1599
#else
1600
    CPU86_LDouble d;
1601
#endif
1602
    int64_t val;
1603

    
1604
    d = ST0;
1605
    val = llrint(d);
1606
    stq((void *)A0, val);
1607
}
1608

    
1609
void OPPROTO op_fbld_ST0_A0(void)
1610
{
1611
    helper_fbld_ST0_A0();
1612
}
1613

    
1614
void OPPROTO op_fbst_ST0_A0(void)
1615
{
1616
    helper_fbst_ST0_A0();
1617
}
1618

    
1619
/* FPU move */
1620

    
1621
void OPPROTO op_fpush(void)
1622
{
1623
    fpush();
1624
}
1625

    
1626
void OPPROTO op_fpop(void)
1627
{
1628
    fpop();
1629
}
1630

    
1631
void OPPROTO op_fdecstp(void)
1632
{
1633
    env->fpstt = (env->fpstt - 1) & 7;
1634
    env->fpus &= (~0x4700);
1635
}
1636

    
1637
void OPPROTO op_fincstp(void)
1638
{
1639
    env->fpstt = (env->fpstt + 1) & 7;
1640
    env->fpus &= (~0x4700);
1641
}
1642

    
1643
void OPPROTO op_ffree_STN(void)
1644
{
1645
    env->fptags[(env->fpstt + PARAM1) & 7] = 1;
1646
}
1647

    
1648
void OPPROTO op_fmov_ST0_FT0(void)
1649
{
1650
    ST0 = FT0;
1651
}
1652

    
1653
void OPPROTO op_fmov_FT0_STN(void)
1654
{
1655
    FT0 = ST(PARAM1);
1656
}
1657

    
1658
void OPPROTO op_fmov_ST0_STN(void)
1659
{
1660
    ST0 = ST(PARAM1);
1661
}
1662

    
1663
void OPPROTO op_fmov_STN_ST0(void)
1664
{
1665
    ST(PARAM1) = ST0;
1666
}
1667

    
1668
void OPPROTO op_fxchg_ST0_STN(void)
1669
{
1670
    CPU86_LDouble tmp;
1671
    tmp = ST(PARAM1);
1672
    ST(PARAM1) = ST0;
1673
    ST0 = tmp;
1674
}
1675

    
1676
/* FPU operations */
1677

    
1678
/* XXX: handle nans */
1679
void OPPROTO op_fcom_ST0_FT0(void)
1680
{
1681
    env->fpus &= (~0x4500);        /* (C3,C2,C0) <-- 000 */
1682
    if (ST0 < FT0)
1683
        env->fpus |= 0x100;        /* (C3,C2,C0) <-- 001 */
1684
    else if (ST0 == FT0)
1685
        env->fpus |= 0x4000; /* (C3,C2,C0) <-- 100 */
1686
    FORCE_RET();
1687
}
1688

    
1689
/* XXX: handle nans */
1690
void OPPROTO op_fucom_ST0_FT0(void)
1691
{
1692
    env->fpus &= (~0x4500);        /* (C3,C2,C0) <-- 000 */
1693
    if (ST0 < FT0)
1694
        env->fpus |= 0x100;        /* (C3,C2,C0) <-- 001 */
1695
    else if (ST0 == FT0)
1696
        env->fpus |= 0x4000; /* (C3,C2,C0) <-- 100 */
1697
    FORCE_RET();
1698
}
1699

    
1700
/* XXX: handle nans */
1701
void OPPROTO op_fcomi_ST0_FT0(void)
1702
{
1703
    int eflags;
1704
    eflags = cc_table[CC_OP].compute_all();
1705
    eflags &= ~(CC_Z | CC_P | CC_C);
1706
    if (ST0 < FT0)
1707
        eflags |= CC_C;
1708
    else if (ST0 == FT0)
1709
        eflags |= CC_Z;
1710
    CC_SRC = eflags;
1711
    FORCE_RET();
1712
}
1713

    
1714
/* XXX: handle nans */
1715
void OPPROTO op_fucomi_ST0_FT0(void)
1716
{
1717
    int eflags;
1718
    eflags = cc_table[CC_OP].compute_all();
1719
    eflags &= ~(CC_Z | CC_P | CC_C);
1720
    if (ST0 < FT0)
1721
        eflags |= CC_C;
1722
    else if (ST0 == FT0)
1723
        eflags |= CC_Z;
1724
    CC_SRC = eflags;
1725
    FORCE_RET();
1726
}
1727

    
1728
void OPPROTO op_fcmov_ST0_STN_T0(void)
1729
{
1730
    if (T0) {
1731
        ST0 = ST(PARAM1);
1732
    }
1733
    FORCE_RET();
1734
}
1735

    
1736
void OPPROTO op_fadd_ST0_FT0(void)
1737
{
1738
    ST0 += FT0;
1739
}
1740

    
1741
void OPPROTO op_fmul_ST0_FT0(void)
1742
{
1743
    ST0 *= FT0;
1744
}
1745

    
1746
void OPPROTO op_fsub_ST0_FT0(void)
1747
{
1748
    ST0 -= FT0;
1749
}
1750

    
1751
void OPPROTO op_fsubr_ST0_FT0(void)
1752
{
1753
    ST0 = FT0 - ST0;
1754
}
1755

    
1756
void OPPROTO op_fdiv_ST0_FT0(void)
1757
{
1758
    ST0 = helper_fdiv(ST0, FT0);
1759
}
1760

    
1761
void OPPROTO op_fdivr_ST0_FT0(void)
1762
{
1763
    ST0 = helper_fdiv(FT0, ST0);
1764
}
1765

    
1766
/* fp operations between STN and ST0 */
1767

    
1768
void OPPROTO op_fadd_STN_ST0(void)
1769
{
1770
    ST(PARAM1) += ST0;
1771
}
1772

    
1773
void OPPROTO op_fmul_STN_ST0(void)
1774
{
1775
    ST(PARAM1) *= ST0;
1776
}
1777

    
1778
void OPPROTO op_fsub_STN_ST0(void)
1779
{
1780
    ST(PARAM1) -= ST0;
1781
}
1782

    
1783
void OPPROTO op_fsubr_STN_ST0(void)
1784
{
1785
    CPU86_LDouble *p;
1786
    p = &ST(PARAM1);
1787
    *p = ST0 - *p;
1788
}
1789

    
1790
void OPPROTO op_fdiv_STN_ST0(void)
1791
{
1792
    CPU86_LDouble *p;
1793
    p = &ST(PARAM1);
1794
    *p = helper_fdiv(*p, ST0);
1795
}
1796

    
1797
void OPPROTO op_fdivr_STN_ST0(void)
1798
{
1799
    CPU86_LDouble *p;
1800
    p = &ST(PARAM1);
1801
    *p = helper_fdiv(ST0, *p);
1802
}
1803

    
1804
/* misc FPU operations */
1805
void OPPROTO op_fchs_ST0(void)
1806
{
1807
    ST0 = -ST0;
1808
}
1809

    
1810
void OPPROTO op_fabs_ST0(void)
1811
{
1812
    ST0 = fabs(ST0);
1813
}
1814

    
1815
void OPPROTO op_fxam_ST0(void)
1816
{
1817
    helper_fxam_ST0();
1818
}
1819

    
1820
void OPPROTO op_fld1_ST0(void)
1821
{
1822
    ST0 = f15rk[1];
1823
}
1824

    
1825
void OPPROTO op_fldl2t_ST0(void)
1826
{
1827
    ST0 = f15rk[6];
1828
}
1829

    
1830
void OPPROTO op_fldl2e_ST0(void)
1831
{
1832
    ST0 = f15rk[5];
1833
}
1834

    
1835
void OPPROTO op_fldpi_ST0(void)
1836
{
1837
    ST0 = f15rk[2];
1838
}
1839

    
1840
void OPPROTO op_fldlg2_ST0(void)
1841
{
1842
    ST0 = f15rk[3];
1843
}
1844

    
1845
void OPPROTO op_fldln2_ST0(void)
1846
{
1847
    ST0 = f15rk[4];
1848
}
1849

    
1850
void OPPROTO op_fldz_ST0(void)
1851
{
1852
    ST0 = f15rk[0];
1853
}
1854

    
1855
void OPPROTO op_fldz_FT0(void)
1856
{
1857
    FT0 = f15rk[0];
1858
}
1859

    
1860
/* associated heplers to reduce generated code length and to simplify
1861
   relocation (FP constants are usually stored in .rodata section) */
1862

    
1863
void OPPROTO op_f2xm1(void)
1864
{
1865
    helper_f2xm1();
1866
}
1867

    
1868
void OPPROTO op_fyl2x(void)
1869
{
1870
    helper_fyl2x();
1871
}
1872

    
1873
void OPPROTO op_fptan(void)
1874
{
1875
    helper_fptan();
1876
}
1877

    
1878
void OPPROTO op_fpatan(void)
1879
{
1880
    helper_fpatan();
1881
}
1882

    
1883
void OPPROTO op_fxtract(void)
1884
{
1885
    helper_fxtract();
1886
}
1887

    
1888
void OPPROTO op_fprem1(void)
1889
{
1890
    helper_fprem1();
1891
}
1892

    
1893

    
1894
void OPPROTO op_fprem(void)
1895
{
1896
    helper_fprem();
1897
}
1898

    
1899
void OPPROTO op_fyl2xp1(void)
1900
{
1901
    helper_fyl2xp1();
1902
}
1903

    
1904
void OPPROTO op_fsqrt(void)
1905
{
1906
    helper_fsqrt();
1907
}
1908

    
1909
void OPPROTO op_fsincos(void)
1910
{
1911
    helper_fsincos();
1912
}
1913

    
1914
void OPPROTO op_frndint(void)
1915
{
1916
    helper_frndint();
1917
}
1918

    
1919
void OPPROTO op_fscale(void)
1920
{
1921
    helper_fscale();
1922
}
1923

    
1924
void OPPROTO op_fsin(void)
1925
{
1926
    helper_fsin();
1927
}
1928

    
1929
void OPPROTO op_fcos(void)
1930
{
1931
    helper_fcos();
1932
}
1933

    
1934
void OPPROTO op_fnstsw_A0(void)
1935
{
1936
    int fpus;
1937
    fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1938
    stw((void *)A0, fpus);
1939
}
1940

    
1941
void OPPROTO op_fnstsw_EAX(void)
1942
{
1943
    int fpus;
1944
    fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1945
    EAX = (EAX & 0xffff0000) | fpus;
1946
}
1947

    
1948
void OPPROTO op_fnstcw_A0(void)
1949
{
1950
    stw((void *)A0, env->fpuc);
1951
}
1952

    
1953
void OPPROTO op_fldcw_A0(void)
1954
{
1955
    int rnd_type;
1956
    env->fpuc = lduw((void *)A0);
1957
    /* set rounding mode */
1958
#ifdef _BSD
1959
    switch(env->fpuc & RC_MASK) {
1960
    default:
1961
    case RC_NEAR:
1962
        rnd_type = FP_RN;
1963
        break;
1964
    case RC_DOWN:
1965
        rnd_type = FP_RM;
1966
        break;
1967
    case RC_UP:
1968
        rnd_type = FP_RP;
1969
        break;
1970
    case RC_CHOP:
1971
        rnd_type = FP_RZ;
1972
        break;
1973
    }
1974
    fpsetround(rnd_type);
1975
#else
1976
    switch(env->fpuc & RC_MASK) {
1977
    default:
1978
    case RC_NEAR:
1979
        rnd_type = FE_TONEAREST;
1980
        break;
1981
    case RC_DOWN:
1982
        rnd_type = FE_DOWNWARD;
1983
        break;
1984
    case RC_UP:
1985
        rnd_type = FE_UPWARD;
1986
        break;
1987
    case RC_CHOP:
1988
        rnd_type = FE_TOWARDZERO;
1989
        break;
1990
    }
1991
    fesetround(rnd_type);
1992
#endif
1993
}
1994

    
1995
void OPPROTO op_fclex(void)
1996
{
1997
    env->fpus &= 0x7f00;
1998
}
1999

    
2000
void OPPROTO op_fwait(void)
2001
{
2002
    if (env->fpus & FPUS_SE)
2003
        fpu_raise_exception();
2004
    FORCE_RET();
2005
}
2006

    
2007
void OPPROTO op_fninit(void)
2008
{
2009
    env->fpus = 0;
2010
    env->fpstt = 0;
2011
    env->fpuc = 0x37f;
2012
    env->fptags[0] = 1;
2013
    env->fptags[1] = 1;
2014
    env->fptags[2] = 1;
2015
    env->fptags[3] = 1;
2016
    env->fptags[4] = 1;
2017
    env->fptags[5] = 1;
2018
    env->fptags[6] = 1;
2019
    env->fptags[7] = 1;
2020
}
2021

    
2022
void OPPROTO op_fnstenv_A0(void)
2023
{
2024
    helper_fstenv((uint8_t *)A0, PARAM1);
2025
}
2026

    
2027
void OPPROTO op_fldenv_A0(void)
2028
{
2029
    helper_fldenv((uint8_t *)A0, PARAM1);
2030
}
2031

    
2032
void OPPROTO op_fnsave_A0(void)
2033
{
2034
    helper_fsave((uint8_t *)A0, PARAM1);
2035
}
2036

    
2037
void OPPROTO op_frstor_A0(void)
2038
{
2039
    helper_frstor((uint8_t *)A0, PARAM1);
2040
}
2041

    
2042
/* threading support */
2043
void OPPROTO op_lock(void)
2044
{
2045
    cpu_lock();
2046
}
2047

    
2048
void OPPROTO op_unlock(void)
2049
{
2050
    cpu_unlock();
2051
}
2052