Statistics
| Branch: | Revision:

root / target-i386 / op.c @ 4136f33c

History | View | Annotate | Download (36.6 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
#include "exec.h"
21

    
22
/* n must be a constant to be efficient */
23
static inline int lshift(int x, int n)
24
{
25
    if (n >= 0)
26
        return x << n;
27
    else
28
        return x >> (-n);
29
}
30

    
31
/* we define the various pieces of code used by the JIT */
32

    
33
#define REG EAX
34
#define REGNAME _EAX
35
#include "opreg_template.h"
36
#undef REG
37
#undef REGNAME
38

    
39
#define REG ECX
40
#define REGNAME _ECX
41
#include "opreg_template.h"
42
#undef REG
43
#undef REGNAME
44

    
45
#define REG EDX
46
#define REGNAME _EDX
47
#include "opreg_template.h"
48
#undef REG
49
#undef REGNAME
50

    
51
#define REG EBX
52
#define REGNAME _EBX
53
#include "opreg_template.h"
54
#undef REG
55
#undef REGNAME
56

    
57
#define REG ESP
58
#define REGNAME _ESP
59
#include "opreg_template.h"
60
#undef REG
61
#undef REGNAME
62

    
63
#define REG EBP
64
#define REGNAME _EBP
65
#include "opreg_template.h"
66
#undef REG
67
#undef REGNAME
68

    
69
#define REG ESI
70
#define REGNAME _ESI
71
#include "opreg_template.h"
72
#undef REG
73
#undef REGNAME
74

    
75
#define REG EDI
76
#define REGNAME _EDI
77
#include "opreg_template.h"
78
#undef REG
79
#undef REGNAME
80

    
81
/* operations with flags */
82

    
83
/* update flags with T0 and T1 (add/sub case) */
84
void OPPROTO op_update2_cc(void)
85
{
86
    CC_SRC = T1;
87
    CC_DST = T0;
88
}
89

    
90
/* update flags with T0 (logic operation case) */
91
void OPPROTO op_update1_cc(void)
92
{
93
    CC_DST = T0;
94
}
95

    
96
void OPPROTO op_update_neg_cc(void)
97
{
98
    CC_SRC = -T0;
99
    CC_DST = T0;
100
}
101

    
102
void OPPROTO op_cmpl_T0_T1_cc(void)
103
{
104
    CC_SRC = T1;
105
    CC_DST = T0 - T1;
106
}
107

    
108
void OPPROTO op_update_inc_cc(void)
109
{
110
    CC_SRC = cc_table[CC_OP].compute_c();
111
    CC_DST = T0;
112
}
113

    
114
void OPPROTO op_testl_T0_T1_cc(void)
115
{
116
    CC_DST = T0 & T1;
117
}
118

    
119
/* operations without flags */
120

    
121
void OPPROTO op_addl_T0_T1(void)
122
{
123
    T0 += T1;
124
}
125

    
126
void OPPROTO op_orl_T0_T1(void)
127
{
128
    T0 |= T1;
129
}
130

    
131
void OPPROTO op_andl_T0_T1(void)
132
{
133
    T0 &= T1;
134
}
135

    
136
void OPPROTO op_subl_T0_T1(void)
137
{
138
    T0 -= T1;
139
}
140

    
141
void OPPROTO op_xorl_T0_T1(void)
142
{
143
    T0 ^= T1;
144
}
145

    
146
void OPPROTO op_negl_T0(void)
147
{
148
    T0 = -T0;
149
}
150

    
151
void OPPROTO op_incl_T0(void)
152
{
153
    T0++;
154
}
155

    
156
void OPPROTO op_decl_T0(void)
157
{
158
    T0--;
159
}
160

    
161
void OPPROTO op_notl_T0(void)
162
{
163
    T0 = ~T0;
164
}
165

    
166
void OPPROTO op_bswapl_T0(void)
167
{
168
    T0 = bswap32(T0);
169
}
170

    
171
/* multiply/divide */
172
void OPPROTO op_mulb_AL_T0(void)
173
{
174
    unsigned int res;
175
    res = (uint8_t)EAX * (uint8_t)T0;
176
    EAX = (EAX & 0xffff0000) | res;
177
    CC_SRC = (res & 0xff00);
178
}
179

    
180
void OPPROTO op_imulb_AL_T0(void)
181
{
182
    int res;
183
    res = (int8_t)EAX * (int8_t)T0;
184
    EAX = (EAX & 0xffff0000) | (res & 0xffff);
185
    CC_SRC = (res != (int8_t)res);
186
}
187

    
188
void OPPROTO op_mulw_AX_T0(void)
189
{
190
    unsigned int res;
191
    res = (uint16_t)EAX * (uint16_t)T0;
192
    EAX = (EAX & 0xffff0000) | (res & 0xffff);
193
    EDX = (EDX & 0xffff0000) | ((res >> 16) & 0xffff);
194
    CC_SRC = res >> 16;
195
}
196

    
197
void OPPROTO op_imulw_AX_T0(void)
198
{
199
    int res;
200
    res = (int16_t)EAX * (int16_t)T0;
201
    EAX = (EAX & 0xffff0000) | (res & 0xffff);
202
    EDX = (EDX & 0xffff0000) | ((res >> 16) & 0xffff);
203
    CC_SRC = (res != (int16_t)res);
204
}
205

    
206
void OPPROTO op_mull_EAX_T0(void)
207
{
208
    uint64_t res;
209
    res = (uint64_t)((uint32_t)EAX) * (uint64_t)((uint32_t)T0);
210
    EAX = res;
211
    EDX = res >> 32;
212
    CC_SRC = res >> 32;
213
}
214

    
215
void OPPROTO op_imull_EAX_T0(void)
216
{
217
    int64_t res;
218
    res = (int64_t)((int32_t)EAX) * (int64_t)((int32_t)T0);
219
    EAX = res;
220
    EDX = res >> 32;
221
    CC_SRC = (res != (int32_t)res);
222
}
223

    
224
void OPPROTO op_imulw_T0_T1(void)
225
{
226
    int res;
227
    res = (int16_t)T0 * (int16_t)T1;
228
    T0 = res;
229
    CC_SRC = (res != (int16_t)res);
230
}
231

    
232
void OPPROTO op_imull_T0_T1(void)
233
{
234
    int64_t res;
235
    res = (int64_t)((int32_t)T0) * (int64_t)((int32_t)T1);
236
    T0 = res;
237
    CC_SRC = (res != (int32_t)res);
238
}
239

    
240
/* division, flags are undefined */
241
/* XXX: add exceptions for overflow */
242

    
243
void OPPROTO op_divb_AL_T0(void)
244
{
245
    unsigned int num, den, q, r;
246

    
247
    num = (EAX & 0xffff);
248
    den = (T0 & 0xff);
249
    if (den == 0) {
250
        EIP = PARAM1;
251
        raise_exception(EXCP00_DIVZ);
252
    }
253
    q = (num / den) & 0xff;
254
    r = (num % den) & 0xff;
255
    EAX = (EAX & 0xffff0000) | (r << 8) | q;
256
}
257

    
258
void OPPROTO op_idivb_AL_T0(void)
259
{
260
    int num, den, q, r;
261

    
262
    num = (int16_t)EAX;
263
    den = (int8_t)T0;
264
    if (den == 0) {
265
        EIP = PARAM1;
266
        raise_exception(EXCP00_DIVZ);
267
    }
268
    q = (num / den) & 0xff;
269
    r = (num % den) & 0xff;
270
    EAX = (EAX & 0xffff0000) | (r << 8) | q;
271
}
272

    
273
void OPPROTO op_divw_AX_T0(void)
274
{
275
    unsigned int num, den, q, r;
276

    
277
    num = (EAX & 0xffff) | ((EDX & 0xffff) << 16);
278
    den = (T0 & 0xffff);
279
    if (den == 0) {
280
        EIP = PARAM1;
281
        raise_exception(EXCP00_DIVZ);
282
    }
283
    q = (num / den) & 0xffff;
284
    r = (num % den) & 0xffff;
285
    EAX = (EAX & 0xffff0000) | q;
286
    EDX = (EDX & 0xffff0000) | r;
287
}
288

    
289
void OPPROTO op_idivw_AX_T0(void)
290
{
291
    int num, den, q, r;
292

    
293
    num = (EAX & 0xffff) | ((EDX & 0xffff) << 16);
294
    den = (int16_t)T0;
295
    if (den == 0) {
296
        EIP = PARAM1;
297
        raise_exception(EXCP00_DIVZ);
298
    }
299
    q = (num / den) & 0xffff;
300
    r = (num % den) & 0xffff;
301
    EAX = (EAX & 0xffff0000) | q;
302
    EDX = (EDX & 0xffff0000) | r;
303
}
304

    
305
void OPPROTO op_divl_EAX_T0(void)
306
{
307
    helper_divl_EAX_T0(PARAM1);
308
}
309

    
310
void OPPROTO op_idivl_EAX_T0(void)
311
{
312
    helper_idivl_EAX_T0(PARAM1);
313
}
314

    
315
/* constant load & misc op */
316

    
317
void OPPROTO op_movl_T0_im(void)
318
{
319
    T0 = PARAM1;
320
}
321

    
322
void OPPROTO op_addl_T0_im(void)
323
{
324
    T0 += PARAM1;
325
}
326

    
327
void OPPROTO op_andl_T0_ffff(void)
328
{
329
    T0 = T0 & 0xffff;
330
}
331

    
332
void OPPROTO op_andl_T0_im(void)
333
{
334
    T0 = T0 & PARAM1;
335
}
336

    
337
void OPPROTO op_movl_T0_T1(void)
338
{
339
    T0 = T1;
340
}
341

    
342
void OPPROTO op_movl_T1_im(void)
343
{
344
    T1 = PARAM1;
345
}
346

    
347
void OPPROTO op_addl_T1_im(void)
348
{
349
    T1 += PARAM1;
350
}
351

    
352
void OPPROTO op_movl_T1_A0(void)
353
{
354
    T1 = A0;
355
}
356

    
357
void OPPROTO op_movl_A0_im(void)
358
{
359
    A0 = PARAM1;
360
}
361

    
362
void OPPROTO op_addl_A0_im(void)
363
{
364
    A0 += PARAM1;
365
}
366

    
367
void OPPROTO op_addl_A0_AL(void)
368
{
369
    A0 += (EAX & 0xff);
370
}
371

    
372
void OPPROTO op_andl_A0_ffff(void)
373
{
374
    A0 = A0 & 0xffff;
375
}
376

    
377
/* memory access */
378

    
379
#define MEMSUFFIX _raw
380
#include "ops_mem.h"
381

    
382
#if !defined(CONFIG_USER_ONLY)
383
#define MEMSUFFIX _user
384
#include "ops_mem.h"
385

    
386
#define MEMSUFFIX _kernel
387
#include "ops_mem.h"
388
#endif
389

    
390
/* used for bit operations */
391

    
392
void OPPROTO op_add_bitw_A0_T1(void)
393
{
394
    A0 += ((int32_t)T1 >> 4) << 1;
395
}
396

    
397
void OPPROTO op_add_bitl_A0_T1(void)
398
{
399
    A0 += ((int32_t)T1 >> 5) << 2;
400
}
401

    
402
/* indirect jump */
403

    
404
void OPPROTO op_jmp_T0(void)
405
{
406
    EIP = T0;
407
}
408

    
409
void OPPROTO op_jmp_im(void)
410
{
411
    EIP = PARAM1;
412
}
413

    
414
void OPPROTO op_hlt(void)
415
{
416
    env->exception_index = EXCP_HLT;
417
    cpu_loop_exit();
418
}
419

    
420
void OPPROTO op_debug(void)
421
{
422
    env->exception_index = EXCP_DEBUG;
423
    cpu_loop_exit();
424
}
425

    
426
void OPPROTO op_raise_interrupt(void)
427
{
428
    int intno;
429
    unsigned int next_eip;
430
    intno = PARAM1;
431
    next_eip = PARAM2;
432
    raise_interrupt(intno, 1, 0, next_eip);
433
}
434

    
435
void OPPROTO op_raise_exception(void)
436
{
437
    int exception_index;
438
    exception_index = PARAM1;
439
    raise_exception(exception_index);
440
}
441

    
442
void OPPROTO op_into(void)
443
{
444
    int eflags;
445
    eflags = cc_table[CC_OP].compute_all();
446
    if (eflags & CC_O) {
447
        raise_interrupt(EXCP04_INTO, 1, 0, PARAM1);
448
    }
449
    FORCE_RET();
450
}
451

    
452
void OPPROTO op_cli(void)
453
{
454
    env->eflags &= ~IF_MASK;
455
}
456

    
457
void OPPROTO op_sti(void)
458
{
459
    env->eflags |= IF_MASK;
460
}
461

    
462
void OPPROTO op_set_inhibit_irq(void)
463
{
464
    env->hflags |= HF_INHIBIT_IRQ_MASK;
465
}
466

    
467
void OPPROTO op_reset_inhibit_irq(void)
468
{
469
    env->hflags &= ~HF_INHIBIT_IRQ_MASK;
470
}
471

    
472
#if 0
473
/* vm86plus instructions */
474
void OPPROTO op_cli_vm(void)
475
{
476
    env->eflags &= ~VIF_MASK;
477
}
478

479
void OPPROTO op_sti_vm(void)
480
{
481
    env->eflags |= VIF_MASK;
482
    if (env->eflags & VIP_MASK) {
483
        EIP = PARAM1;
484
        raise_exception(EXCP0D_GPF);
485
    }
486
    FORCE_RET();
487
}
488
#endif
489

    
490
void OPPROTO op_boundw(void)
491
{
492
    int low, high, v;
493
    low = ldsw((uint8_t *)A0);
494
    high = ldsw((uint8_t *)A0 + 2);
495
    v = (int16_t)T0;
496
    if (v < low || v > high) {
497
        EIP = PARAM1;
498
        raise_exception(EXCP05_BOUND);
499
    }
500
    FORCE_RET();
501
}
502

    
503
void OPPROTO op_boundl(void)
504
{
505
    int low, high, v;
506
    low = ldl((uint8_t *)A0);
507
    high = ldl((uint8_t *)A0 + 4);
508
    v = T0;
509
    if (v < low || v > high) {
510
        EIP = PARAM1;
511
        raise_exception(EXCP05_BOUND);
512
    }
513
    FORCE_RET();
514
}
515

    
516
void OPPROTO op_cmpxchg8b(void)
517
{
518
    helper_cmpxchg8b();
519
}
520

    
521
void OPPROTO op_jmp(void)
522
{
523
    JUMP_TB(op_jmp, PARAM1, 0, PARAM2);
524
}
525

    
526
void OPPROTO op_movl_T0_0(void)
527
{
528
    T0 = 0;
529
}
530

    
531
void OPPROTO op_exit_tb(void)
532
{
533
    EXIT_TB();
534
}
535

    
536
/* multiple size ops */
537

    
538
#define ldul ldl
539

    
540
#define SHIFT 0
541
#include "ops_template.h"
542
#undef SHIFT
543

    
544
#define SHIFT 1
545
#include "ops_template.h"
546
#undef SHIFT
547

    
548
#define SHIFT 2
549
#include "ops_template.h"
550
#undef SHIFT
551

    
552
/* sign extend */
553

    
554
void OPPROTO op_movsbl_T0_T0(void)
555
{
556
    T0 = (int8_t)T0;
557
}
558

    
559
void OPPROTO op_movzbl_T0_T0(void)
560
{
561
    T0 = (uint8_t)T0;
562
}
563

    
564
void OPPROTO op_movswl_T0_T0(void)
565
{
566
    T0 = (int16_t)T0;
567
}
568

    
569
void OPPROTO op_movzwl_T0_T0(void)
570
{
571
    T0 = (uint16_t)T0;
572
}
573

    
574
void OPPROTO op_movswl_EAX_AX(void)
575
{
576
    EAX = (int16_t)EAX;
577
}
578

    
579
void OPPROTO op_movsbw_AX_AL(void)
580
{
581
    EAX = (EAX & 0xffff0000) | ((int8_t)EAX & 0xffff);
582
}
583

    
584
void OPPROTO op_movslq_EDX_EAX(void)
585
{
586
    EDX = (int32_t)EAX >> 31;
587
}
588

    
589
void OPPROTO op_movswl_DX_AX(void)
590
{
591
    EDX = (EDX & 0xffff0000) | (((int16_t)EAX >> 15) & 0xffff);
592
}
593

    
594
/* string ops helpers */
595

    
596
void OPPROTO op_addl_ESI_T0(void)
597
{
598
    ESI += T0;
599
}
600

    
601
void OPPROTO op_addw_ESI_T0(void)
602
{
603
    ESI = (ESI & ~0xffff) | ((ESI + T0) & 0xffff);
604
}
605

    
606
void OPPROTO op_addl_EDI_T0(void)
607
{
608
    EDI += T0;
609
}
610

    
611
void OPPROTO op_addw_EDI_T0(void)
612
{
613
    EDI = (EDI & ~0xffff) | ((EDI + T0) & 0xffff);
614
}
615

    
616
void OPPROTO op_decl_ECX(void)
617
{
618
    ECX--;
619
}
620

    
621
void OPPROTO op_decw_ECX(void)
622
{
623
    ECX = (ECX & ~0xffff) | ((ECX - 1) & 0xffff);
624
}
625

    
626
/* push/pop */
627

    
628
void op_pushl_T0(void)
629
{
630
    uint32_t offset;
631
    offset = ESP - 4;
632
    stl((void *)offset, T0);
633
    /* modify ESP after to handle exceptions correctly */
634
    ESP = offset;
635
}
636

    
637
void op_pushw_T0(void)
638
{
639
    uint32_t offset;
640
    offset = ESP - 2;
641
    stw((void *)offset, T0);
642
    /* modify ESP after to handle exceptions correctly */
643
    ESP = offset;
644
}
645

    
646
void op_pushl_ss32_T0(void)
647
{
648
    uint32_t offset;
649
    offset = ESP - 4;
650
    stl(env->segs[R_SS].base + offset, T0);
651
    /* modify ESP after to handle exceptions correctly */
652
    ESP = offset;
653
}
654

    
655
void op_pushw_ss32_T0(void)
656
{
657
    uint32_t offset;
658
    offset = ESP - 2;
659
    stw(env->segs[R_SS].base + offset, T0);
660
    /* modify ESP after to handle exceptions correctly */
661
    ESP = offset;
662
}
663

    
664
void op_pushl_ss16_T0(void)
665
{
666
    uint32_t offset;
667
    offset = (ESP - 4) & 0xffff;
668
    stl(env->segs[R_SS].base + offset, T0);
669
    /* modify ESP after to handle exceptions correctly */
670
    ESP = (ESP & ~0xffff) | offset;
671
}
672

    
673
void op_pushw_ss16_T0(void)
674
{
675
    uint32_t offset;
676
    offset = (ESP - 2) & 0xffff;
677
    stw(env->segs[R_SS].base + offset, T0);
678
    /* modify ESP after to handle exceptions correctly */
679
    ESP = (ESP & ~0xffff) | offset;
680
}
681

    
682
/* NOTE: ESP update is done after */
683
void op_popl_T0(void)
684
{
685
    T0 = ldl((void *)ESP);
686
}
687

    
688
void op_popw_T0(void)
689
{
690
    T0 = lduw((void *)ESP);
691
}
692

    
693
void op_popl_ss32_T0(void)
694
{
695
    T0 = ldl(env->segs[R_SS].base + ESP);
696
}
697

    
698
void op_popw_ss32_T0(void)
699
{
700
    T0 = lduw(env->segs[R_SS].base + ESP);
701
}
702

    
703
void op_popl_ss16_T0(void)
704
{
705
    T0 = ldl(env->segs[R_SS].base + (ESP & 0xffff));
706
}
707

    
708
void op_popw_ss16_T0(void)
709
{
710
    T0 = lduw(env->segs[R_SS].base + (ESP & 0xffff));
711
}
712

    
713
void op_addl_ESP_4(void)
714
{
715
    ESP += 4;
716
}
717

    
718
void op_addl_ESP_2(void)
719
{
720
    ESP += 2;
721
}
722

    
723
void op_addw_ESP_4(void)
724
{
725
    ESP = (ESP & ~0xffff) | ((ESP + 4) & 0xffff);
726
}
727

    
728
void op_addw_ESP_2(void)
729
{
730
    ESP = (ESP & ~0xffff) | ((ESP + 2) & 0xffff);
731
}
732

    
733
void op_addl_ESP_im(void)
734
{
735
    ESP += PARAM1;
736
}
737

    
738
void op_addw_ESP_im(void)
739
{
740
    ESP = (ESP & ~0xffff) | ((ESP + PARAM1) & 0xffff);
741
}
742

    
743
void OPPROTO op_rdtsc(void)
744
{
745
    helper_rdtsc();
746
}
747

    
748
void OPPROTO op_cpuid(void)
749
{
750
    helper_cpuid();
751
}
752

    
753
void OPPROTO op_rdmsr(void)
754
{
755
    helper_rdmsr();
756
}
757

    
758
void OPPROTO op_wrmsr(void)
759
{
760
    helper_wrmsr();
761
}
762

    
763
/* bcd */
764

    
765
/* XXX: exception */
766
void OPPROTO op_aam(void)
767
{
768
    int base = PARAM1;
769
    int al, ah;
770
    al = EAX & 0xff;
771
    ah = al / base;
772
    al = al % base;
773
    EAX = (EAX & ~0xffff) | al | (ah << 8);
774
    CC_DST = al;
775
}
776

    
777
void OPPROTO op_aad(void)
778
{
779
    int base = PARAM1;
780
    int al, ah;
781
    al = EAX & 0xff;
782
    ah = (EAX >> 8) & 0xff;
783
    al = ((ah * base) + al) & 0xff;
784
    EAX = (EAX & ~0xffff) | al;
785
    CC_DST = al;
786
}
787

    
788
void OPPROTO op_aaa(void)
789
{
790
    int icarry;
791
    int al, ah, af;
792
    int eflags;
793

    
794
    eflags = cc_table[CC_OP].compute_all();
795
    af = eflags & CC_A;
796
    al = EAX & 0xff;
797
    ah = (EAX >> 8) & 0xff;
798

    
799
    icarry = (al > 0xf9);
800
    if (((al & 0x0f) > 9 ) || af) {
801
        al = (al + 6) & 0x0f;
802
        ah = (ah + 1 + icarry) & 0xff;
803
        eflags |= CC_C | CC_A;
804
    } else {
805
        eflags &= ~(CC_C | CC_A);
806
        al &= 0x0f;
807
    }
808
    EAX = (EAX & ~0xffff) | al | (ah << 8);
809
    CC_SRC = eflags;
810
}
811

    
812
void OPPROTO op_aas(void)
813
{
814
    int icarry;
815
    int al, ah, af;
816
    int eflags;
817

    
818
    eflags = cc_table[CC_OP].compute_all();
819
    af = eflags & CC_A;
820
    al = EAX & 0xff;
821
    ah = (EAX >> 8) & 0xff;
822

    
823
    icarry = (al < 6);
824
    if (((al & 0x0f) > 9 ) || af) {
825
        al = (al - 6) & 0x0f;
826
        ah = (ah - 1 - icarry) & 0xff;
827
        eflags |= CC_C | CC_A;
828
    } else {
829
        eflags &= ~(CC_C | CC_A);
830
        al &= 0x0f;
831
    }
832
    EAX = (EAX & ~0xffff) | al | (ah << 8);
833
    CC_SRC = eflags;
834
}
835

    
836
void OPPROTO op_daa(void)
837
{
838
    int al, af, cf;
839
    int eflags;
840

    
841
    eflags = cc_table[CC_OP].compute_all();
842
    cf = eflags & CC_C;
843
    af = eflags & CC_A;
844
    al = EAX & 0xff;
845

    
846
    eflags = 0;
847
    if (((al & 0x0f) > 9 ) || af) {
848
        al = (al + 6) & 0xff;
849
        eflags |= CC_A;
850
    }
851
    if ((al > 0x9f) || cf) {
852
        al = (al + 0x60) & 0xff;
853
        eflags |= CC_C;
854
    }
855
    EAX = (EAX & ~0xff) | al;
856
    /* well, speed is not an issue here, so we compute the flags by hand */
857
    eflags |= (al == 0) << 6; /* zf */
858
    eflags |= parity_table[al]; /* pf */
859
    eflags |= (al & 0x80); /* sf */
860
    CC_SRC = eflags;
861
}
862

    
863
void OPPROTO op_das(void)
864
{
865
    int al, al1, af, cf;
866
    int eflags;
867

    
868
    eflags = cc_table[CC_OP].compute_all();
869
    cf = eflags & CC_C;
870
    af = eflags & CC_A;
871
    al = EAX & 0xff;
872

    
873
    eflags = 0;
874
    al1 = al;
875
    if (((al & 0x0f) > 9 ) || af) {
876
        eflags |= CC_A;
877
        if (al < 6 || cf)
878
            eflags |= CC_C;
879
        al = (al - 6) & 0xff;
880
    }
881
    if ((al1 > 0x99) || cf) {
882
        al = (al - 0x60) & 0xff;
883
        eflags |= CC_C;
884
    }
885
    EAX = (EAX & ~0xff) | al;
886
    /* well, speed is not an issue here, so we compute the flags by hand */
887
    eflags |= (al == 0) << 6; /* zf */
888
    eflags |= parity_table[al]; /* pf */
889
    eflags |= (al & 0x80); /* sf */
890
    CC_SRC = eflags;
891
}
892

    
893
/* segment handling */
894

    
895
/* never use it with R_CS */
896
void OPPROTO op_movl_seg_T0(void)
897
{
898
    load_seg(PARAM1, T0 & 0xffff, PARAM2);
899
}
900

    
901
/* faster VM86 version */
902
void OPPROTO op_movl_seg_T0_vm(void)
903
{
904
    int selector;
905
    SegmentCache *sc;
906
    
907
    selector = T0 & 0xffff;
908
    /* env->segs[] access */
909
    sc = (SegmentCache *)((char *)env + PARAM1);
910
    sc->selector = selector;
911
    sc->base = (void *)(selector << 4);
912
}
913

    
914
void OPPROTO op_movl_T0_seg(void)
915
{
916
    T0 = env->segs[PARAM1].selector;
917
}
918

    
919
void OPPROTO op_movl_A0_seg(void)
920
{
921
    A0 = *(unsigned long *)((char *)env + PARAM1);
922
}
923

    
924
void OPPROTO op_addl_A0_seg(void)
925
{
926
    A0 += *(unsigned long *)((char *)env + PARAM1);
927
}
928

    
929
void OPPROTO op_lsl(void)
930
{
931
    helper_lsl();
932
}
933

    
934
void OPPROTO op_lar(void)
935
{
936
    helper_lar();
937
}
938

    
939
void OPPROTO op_verr(void)
940
{
941
    helper_verr();
942
}
943

    
944
void OPPROTO op_verw(void)
945
{
946
    helper_verw();
947
}
948

    
949
void OPPROTO op_arpl(void)
950
{
951
    if ((T0 & 3) < (T1 & 3)) {
952
        /* XXX: emulate bug or 0xff3f0000 oring as in bochs ? */
953
        T0 = (T0 & ~3) | (T1 & 3);
954
        T1 = CC_Z;
955
   } else {
956
        T1 = 0;
957
    }
958
    FORCE_RET();
959
}
960
            
961
void OPPROTO op_arpl_update(void)
962
{
963
    int eflags;
964
    eflags = cc_table[CC_OP].compute_all();
965
    CC_SRC = (eflags & ~CC_Z) | T1;
966
}
967
    
968
/* T0: segment, T1:eip */
969
void OPPROTO op_ljmp_protected_T0_T1(void)
970
{
971
    helper_ljmp_protected_T0_T1();
972
}
973

    
974
void OPPROTO op_lcall_real_T0_T1(void)
975
{
976
    helper_lcall_real_T0_T1(PARAM1, PARAM2);
977
}
978

    
979
void OPPROTO op_lcall_protected_T0_T1(void)
980
{
981
    helper_lcall_protected_T0_T1(PARAM1, PARAM2);
982
}
983

    
984
void OPPROTO op_iret_real(void)
985
{
986
    helper_iret_real(PARAM1);
987
}
988

    
989
void OPPROTO op_iret_protected(void)
990
{
991
    helper_iret_protected(PARAM1);
992
}
993

    
994
void OPPROTO op_lret_protected(void)
995
{
996
    helper_lret_protected(PARAM1, PARAM2);
997
}
998

    
999
void OPPROTO op_lldt_T0(void)
1000
{
1001
    helper_lldt_T0();
1002
}
1003

    
1004
void OPPROTO op_ltr_T0(void)
1005
{
1006
    helper_ltr_T0();
1007
}
1008

    
1009
/* CR registers access */
1010
void OPPROTO op_movl_crN_T0(void)
1011
{
1012
    helper_movl_crN_T0(PARAM1);
1013
}
1014

    
1015
/* DR registers access */
1016
void OPPROTO op_movl_drN_T0(void)
1017
{
1018
    helper_movl_drN_T0(PARAM1);
1019
}
1020

    
1021
void OPPROTO op_lmsw_T0(void)
1022
{
1023
    /* only 4 lower bits of CR0 are modified */
1024
    T0 = (env->cr[0] & ~0xf) | (T0 & 0xf);
1025
    helper_movl_crN_T0(0);
1026
}
1027

    
1028
void OPPROTO op_invlpg_A0(void)
1029
{
1030
    helper_invlpg(A0);
1031
}
1032

    
1033
void OPPROTO op_movl_T0_env(void)
1034
{
1035
    T0 = *(uint32_t *)((char *)env + PARAM1);
1036
}
1037

    
1038
void OPPROTO op_movl_env_T0(void)
1039
{
1040
    *(uint32_t *)((char *)env + PARAM1) = T0;
1041
}
1042

    
1043
void OPPROTO op_movl_env_T1(void)
1044
{
1045
    *(uint32_t *)((char *)env + PARAM1) = T1;
1046
}
1047

    
1048
void OPPROTO op_clts(void)
1049
{
1050
    env->cr[0] &= ~CR0_TS_MASK;
1051
}
1052

    
1053
/* flags handling */
1054

    
1055
/* slow jumps cases : in order to avoid calling a function with a
1056
   pointer (which can generate a stack frame on PowerPC), we use
1057
   op_setcc to set T0 and then call op_jcc. */
1058
void OPPROTO op_jcc(void)
1059
{
1060
    if (T0)
1061
        JUMP_TB(op_jcc, PARAM1, 0, PARAM2);
1062
    else
1063
        JUMP_TB(op_jcc, PARAM1, 1, PARAM3);
1064
    FORCE_RET();
1065
}
1066

    
1067
void OPPROTO op_jcc_im(void)
1068
{
1069
    if (T0)
1070
        EIP = PARAM1;
1071
    else
1072
        EIP = PARAM2;
1073
    FORCE_RET();
1074
}
1075

    
1076
/* slow set cases (compute x86 flags) */
1077
void OPPROTO op_seto_T0_cc(void)
1078
{
1079
    int eflags;
1080
    eflags = cc_table[CC_OP].compute_all();
1081
    T0 = (eflags >> 11) & 1;
1082
}
1083

    
1084
void OPPROTO op_setb_T0_cc(void)
1085
{
1086
    T0 = cc_table[CC_OP].compute_c();
1087
}
1088

    
1089
void OPPROTO op_setz_T0_cc(void)
1090
{
1091
    int eflags;
1092
    eflags = cc_table[CC_OP].compute_all();
1093
    T0 = (eflags >> 6) & 1;
1094
}
1095

    
1096
void OPPROTO op_setbe_T0_cc(void)
1097
{
1098
    int eflags;
1099
    eflags = cc_table[CC_OP].compute_all();
1100
    T0 = (eflags & (CC_Z | CC_C)) != 0;
1101
}
1102

    
1103
void OPPROTO op_sets_T0_cc(void)
1104
{
1105
    int eflags;
1106
    eflags = cc_table[CC_OP].compute_all();
1107
    T0 = (eflags >> 7) & 1;
1108
}
1109

    
1110
void OPPROTO op_setp_T0_cc(void)
1111
{
1112
    int eflags;
1113
    eflags = cc_table[CC_OP].compute_all();
1114
    T0 = (eflags >> 2) & 1;
1115
}
1116

    
1117
void OPPROTO op_setl_T0_cc(void)
1118
{
1119
    int eflags;
1120
    eflags = cc_table[CC_OP].compute_all();
1121
    T0 = ((eflags ^ (eflags >> 4)) >> 7) & 1;
1122
}
1123

    
1124
void OPPROTO op_setle_T0_cc(void)
1125
{
1126
    int eflags;
1127
    eflags = cc_table[CC_OP].compute_all();
1128
    T0 = (((eflags ^ (eflags >> 4)) & 0x80) || (eflags & CC_Z)) != 0;
1129
}
1130

    
1131
void OPPROTO op_xor_T0_1(void)
1132
{
1133
    T0 ^= 1;
1134
}
1135

    
1136
void OPPROTO op_set_cc_op(void)
1137
{
1138
    CC_OP = PARAM1;
1139
}
1140

    
1141
/* XXX: clear VIF/VIP in all ops ? */
1142

    
1143
void OPPROTO op_movl_eflags_T0(void)
1144
{
1145
    load_eflags(T0, (TF_MASK | AC_MASK | ID_MASK));
1146
}
1147

    
1148
void OPPROTO op_movw_eflags_T0(void)
1149
{
1150
    load_eflags(T0, (TF_MASK | AC_MASK | ID_MASK) & 0xffff);
1151
}
1152

    
1153
void OPPROTO op_movl_eflags_T0_io(void)
1154
{
1155
    load_eflags(T0, (TF_MASK | AC_MASK | ID_MASK | IF_MASK));
1156
}
1157

    
1158
void OPPROTO op_movw_eflags_T0_io(void)
1159
{
1160
    load_eflags(T0, (TF_MASK | AC_MASK | ID_MASK | IF_MASK) & 0xffff);
1161
}
1162

    
1163
void OPPROTO op_movl_eflags_T0_cpl0(void)
1164
{
1165
    load_eflags(T0, (TF_MASK | AC_MASK | ID_MASK | IF_MASK | IOPL_MASK));
1166
}
1167

    
1168
void OPPROTO op_movw_eflags_T0_cpl0(void)
1169
{
1170
    load_eflags(T0, (TF_MASK | AC_MASK | ID_MASK | IF_MASK | IOPL_MASK) & 0xffff);
1171
}
1172

    
1173
#if 0
1174
/* vm86plus version */
1175
void OPPROTO op_movw_eflags_T0_vm(void)
1176
{
1177
    int eflags;
1178
    eflags = T0;
1179
    CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1180
    DF = 1 - (2 * ((eflags >> 10) & 1));
1181
    /* we also update some system flags as in user mode */
1182
    env->eflags = (env->eflags & ~(FL_UPDATE_MASK16 | VIF_MASK)) |
1183
        (eflags & FL_UPDATE_MASK16);
1184
    if (eflags & IF_MASK) {
1185
        env->eflags |= VIF_MASK;
1186
        if (env->eflags & VIP_MASK) {
1187
            EIP = PARAM1;
1188
            raise_exception(EXCP0D_GPF);
1189
        }
1190
    }
1191
    FORCE_RET();
1192
}
1193

1194
void OPPROTO op_movl_eflags_T0_vm(void)
1195
{
1196
    int eflags;
1197
    eflags = T0;
1198
    CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1199
    DF = 1 - (2 * ((eflags >> 10) & 1));
1200
    /* we also update some system flags as in user mode */
1201
    env->eflags = (env->eflags & ~(FL_UPDATE_MASK32 | VIF_MASK)) |
1202
        (eflags & FL_UPDATE_MASK32);
1203
    if (eflags & IF_MASK) {
1204
        env->eflags |= VIF_MASK;
1205
        if (env->eflags & VIP_MASK) {
1206
            EIP = PARAM1;
1207
            raise_exception(EXCP0D_GPF);
1208
        }
1209
    }
1210
    FORCE_RET();
1211
}
1212
#endif
1213

    
1214
/* XXX: compute only O flag */
1215
void OPPROTO op_movb_eflags_T0(void)
1216
{
1217
    int of;
1218
    of = cc_table[CC_OP].compute_all() & CC_O;
1219
    CC_SRC = (T0 & (CC_S | CC_Z | CC_A | CC_P | CC_C)) | of;
1220
}
1221

    
1222
void OPPROTO op_movl_T0_eflags(void)
1223
{
1224
    int eflags;
1225
    eflags = cc_table[CC_OP].compute_all();
1226
    eflags |= (DF & DF_MASK);
1227
    eflags |= env->eflags & ~(VM_MASK | RF_MASK);
1228
    T0 = eflags;
1229
}
1230

    
1231
/* vm86plus version */
1232
#if 0
1233
void OPPROTO op_movl_T0_eflags_vm(void)
1234
{
1235
    int eflags;
1236
    eflags = cc_table[CC_OP].compute_all();
1237
    eflags |= (DF & DF_MASK);
1238
    eflags |= env->eflags & ~(VM_MASK | RF_MASK | IF_MASK);
1239
    if (env->eflags & VIF_MASK)
1240
        eflags |= IF_MASK;
1241
    T0 = eflags;
1242
}
1243
#endif
1244

    
1245
void OPPROTO op_cld(void)
1246
{
1247
    DF = 1;
1248
}
1249

    
1250
void OPPROTO op_std(void)
1251
{
1252
    DF = -1;
1253
}
1254

    
1255
void OPPROTO op_clc(void)
1256
{
1257
    int eflags;
1258
    eflags = cc_table[CC_OP].compute_all();
1259
    eflags &= ~CC_C;
1260
    CC_SRC = eflags;
1261
}
1262

    
1263
void OPPROTO op_stc(void)
1264
{
1265
    int eflags;
1266
    eflags = cc_table[CC_OP].compute_all();
1267
    eflags |= CC_C;
1268
    CC_SRC = eflags;
1269
}
1270

    
1271
void OPPROTO op_cmc(void)
1272
{
1273
    int eflags;
1274
    eflags = cc_table[CC_OP].compute_all();
1275
    eflags ^= CC_C;
1276
    CC_SRC = eflags;
1277
}
1278

    
1279
void OPPROTO op_salc(void)
1280
{
1281
    int cf;
1282
    cf = cc_table[CC_OP].compute_c();
1283
    EAX = (EAX & ~0xff) | ((-cf) & 0xff);
1284
}
1285

    
1286
static int compute_all_eflags(void)
1287
{
1288
    return CC_SRC;
1289
}
1290

    
1291
static int compute_c_eflags(void)
1292
{
1293
    return CC_SRC & CC_C;
1294
}
1295

    
1296
static int compute_c_mul(void)
1297
{
1298
    int cf;
1299
    cf = (CC_SRC != 0);
1300
    return cf;
1301
}
1302

    
1303
static int compute_all_mul(void)
1304
{
1305
    int cf, pf, af, zf, sf, of;
1306
    cf = (CC_SRC != 0);
1307
    pf = 0; /* undefined */
1308
    af = 0; /* undefined */
1309
    zf = 0; /* undefined */
1310
    sf = 0; /* undefined */
1311
    of = cf << 11;
1312
    return cf | pf | af | zf | sf | of;
1313
}
1314
    
1315
CCTable cc_table[CC_OP_NB] = {
1316
    [CC_OP_DYNAMIC] = { /* should never happen */ },
1317

    
1318
    [CC_OP_EFLAGS] = { compute_all_eflags, compute_c_eflags },
1319

    
1320
    [CC_OP_MUL] = { compute_all_mul, compute_c_mul },
1321

    
1322
    [CC_OP_ADDB] = { compute_all_addb, compute_c_addb },
1323
    [CC_OP_ADDW] = { compute_all_addw, compute_c_addw  },
1324
    [CC_OP_ADDL] = { compute_all_addl, compute_c_addl  },
1325

    
1326
    [CC_OP_ADCB] = { compute_all_adcb, compute_c_adcb },
1327
    [CC_OP_ADCW] = { compute_all_adcw, compute_c_adcw  },
1328
    [CC_OP_ADCL] = { compute_all_adcl, compute_c_adcl  },
1329

    
1330
    [CC_OP_SUBB] = { compute_all_subb, compute_c_subb  },
1331
    [CC_OP_SUBW] = { compute_all_subw, compute_c_subw  },
1332
    [CC_OP_SUBL] = { compute_all_subl, compute_c_subl  },
1333
    
1334
    [CC_OP_SBBB] = { compute_all_sbbb, compute_c_sbbb  },
1335
    [CC_OP_SBBW] = { compute_all_sbbw, compute_c_sbbw  },
1336
    [CC_OP_SBBL] = { compute_all_sbbl, compute_c_sbbl  },
1337
    
1338
    [CC_OP_LOGICB] = { compute_all_logicb, compute_c_logicb },
1339
    [CC_OP_LOGICW] = { compute_all_logicw, compute_c_logicw },
1340
    [CC_OP_LOGICL] = { compute_all_logicl, compute_c_logicl },
1341
    
1342
    [CC_OP_INCB] = { compute_all_incb, compute_c_incl },
1343
    [CC_OP_INCW] = { compute_all_incw, compute_c_incl },
1344
    [CC_OP_INCL] = { compute_all_incl, compute_c_incl },
1345
    
1346
    [CC_OP_DECB] = { compute_all_decb, compute_c_incl },
1347
    [CC_OP_DECW] = { compute_all_decw, compute_c_incl },
1348
    [CC_OP_DECL] = { compute_all_decl, compute_c_incl },
1349
    
1350
    [CC_OP_SHLB] = { compute_all_shlb, compute_c_shlb },
1351
    [CC_OP_SHLW] = { compute_all_shlw, compute_c_shlw },
1352
    [CC_OP_SHLL] = { compute_all_shll, compute_c_shll },
1353

    
1354
    [CC_OP_SARB] = { compute_all_sarb, compute_c_sarl },
1355
    [CC_OP_SARW] = { compute_all_sarw, compute_c_sarl },
1356
    [CC_OP_SARL] = { compute_all_sarl, compute_c_sarl },
1357
};
1358

    
1359
/* floating point support. Some of the code for complicated x87
1360
   functions comes from the LGPL'ed x86 emulator found in the Willows
1361
   TWIN windows emulator. */
1362

    
1363
#if defined(__powerpc__)
1364
extern CPU86_LDouble copysign(CPU86_LDouble, CPU86_LDouble);
1365

    
1366
/* correct (but slow) PowerPC rint() (glibc version is incorrect) */
1367
double qemu_rint(double x)
1368
{
1369
    double y = 4503599627370496.0;
1370
    if (fabs(x) >= y)
1371
        return x;
1372
    if (x < 0) 
1373
        y = -y;
1374
    y = (x + y) - y;
1375
    if (y == 0.0)
1376
        y = copysign(y, x);
1377
    return y;
1378
}
1379

    
1380
#define rint qemu_rint
1381
#endif
1382

    
1383
/* fp load FT0 */
1384

    
1385
void OPPROTO op_flds_FT0_A0(void)
1386
{
1387
#ifdef USE_FP_CONVERT
1388
    FP_CONVERT.i32 = ldl((void *)A0);
1389
    FT0 = FP_CONVERT.f;
1390
#else
1391
    FT0 = ldfl((void *)A0);
1392
#endif
1393
}
1394

    
1395
void OPPROTO op_fldl_FT0_A0(void)
1396
{
1397
#ifdef USE_FP_CONVERT
1398
    FP_CONVERT.i64 = ldq((void *)A0);
1399
    FT0 = FP_CONVERT.d;
1400
#else
1401
    FT0 = ldfq((void *)A0);
1402
#endif
1403
}
1404

    
1405
/* helpers are needed to avoid static constant reference. XXX: find a better way */
1406
#ifdef USE_INT_TO_FLOAT_HELPERS
1407

    
1408
void helper_fild_FT0_A0(void)
1409
{
1410
    FT0 = (CPU86_LDouble)ldsw((void *)A0);
1411
}
1412

    
1413
void helper_fildl_FT0_A0(void)
1414
{
1415
    FT0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1416
}
1417

    
1418
void helper_fildll_FT0_A0(void)
1419
{
1420
    FT0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1421
}
1422

    
1423
void OPPROTO op_fild_FT0_A0(void)
1424
{
1425
    helper_fild_FT0_A0();
1426
}
1427

    
1428
void OPPROTO op_fildl_FT0_A0(void)
1429
{
1430
    helper_fildl_FT0_A0();
1431
}
1432

    
1433
void OPPROTO op_fildll_FT0_A0(void)
1434
{
1435
    helper_fildll_FT0_A0();
1436
}
1437

    
1438
#else
1439

    
1440
void OPPROTO op_fild_FT0_A0(void)
1441
{
1442
#ifdef USE_FP_CONVERT
1443
    FP_CONVERT.i32 = ldsw((void *)A0);
1444
    FT0 = (CPU86_LDouble)FP_CONVERT.i32;
1445
#else
1446
    FT0 = (CPU86_LDouble)ldsw((void *)A0);
1447
#endif
1448
}
1449

    
1450
void OPPROTO op_fildl_FT0_A0(void)
1451
{
1452
#ifdef USE_FP_CONVERT
1453
    FP_CONVERT.i32 = (int32_t) ldl((void *)A0);
1454
    FT0 = (CPU86_LDouble)FP_CONVERT.i32;
1455
#else
1456
    FT0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1457
#endif
1458
}
1459

    
1460
void OPPROTO op_fildll_FT0_A0(void)
1461
{
1462
#ifdef USE_FP_CONVERT
1463
    FP_CONVERT.i64 = (int64_t) ldq((void *)A0);
1464
    FT0 = (CPU86_LDouble)FP_CONVERT.i64;
1465
#else
1466
    FT0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1467
#endif
1468
}
1469
#endif
1470

    
1471
/* fp load ST0 */
1472

    
1473
void OPPROTO op_flds_ST0_A0(void)
1474
{
1475
    int new_fpstt;
1476
    new_fpstt = (env->fpstt - 1) & 7;
1477
#ifdef USE_FP_CONVERT
1478
    FP_CONVERT.i32 = ldl((void *)A0);
1479
    env->fpregs[new_fpstt] = FP_CONVERT.f;
1480
#else
1481
    env->fpregs[new_fpstt] = ldfl((void *)A0);
1482
#endif
1483
    env->fpstt = new_fpstt;
1484
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1485
}
1486

    
1487
void OPPROTO op_fldl_ST0_A0(void)
1488
{
1489
    int new_fpstt;
1490
    new_fpstt = (env->fpstt - 1) & 7;
1491
#ifdef USE_FP_CONVERT
1492
    FP_CONVERT.i64 = ldq((void *)A0);
1493
    env->fpregs[new_fpstt] = FP_CONVERT.d;
1494
#else
1495
    env->fpregs[new_fpstt] = ldfq((void *)A0);
1496
#endif
1497
    env->fpstt = new_fpstt;
1498
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1499
}
1500

    
1501
void OPPROTO op_fldt_ST0_A0(void)
1502
{
1503
    helper_fldt_ST0_A0();
1504
}
1505

    
1506
/* helpers are needed to avoid static constant reference. XXX: find a better way */
1507
#ifdef USE_INT_TO_FLOAT_HELPERS
1508

    
1509
void helper_fild_ST0_A0(void)
1510
{
1511
    int new_fpstt;
1512
    new_fpstt = (env->fpstt - 1) & 7;
1513
    env->fpregs[new_fpstt] = (CPU86_LDouble)ldsw((void *)A0);
1514
    env->fpstt = new_fpstt;
1515
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1516
}
1517

    
1518
void helper_fildl_ST0_A0(void)
1519
{
1520
    int new_fpstt;
1521
    new_fpstt = (env->fpstt - 1) & 7;
1522
    env->fpregs[new_fpstt] = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1523
    env->fpstt = new_fpstt;
1524
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1525
}
1526

    
1527
void helper_fildll_ST0_A0(void)
1528
{
1529
    int new_fpstt;
1530
    new_fpstt = (env->fpstt - 1) & 7;
1531
    env->fpregs[new_fpstt] = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1532
    env->fpstt = new_fpstt;
1533
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1534
}
1535

    
1536
void OPPROTO op_fild_ST0_A0(void)
1537
{
1538
    helper_fild_ST0_A0();
1539
}
1540

    
1541
void OPPROTO op_fildl_ST0_A0(void)
1542
{
1543
    helper_fildl_ST0_A0();
1544
}
1545

    
1546
void OPPROTO op_fildll_ST0_A0(void)
1547
{
1548
    helper_fildll_ST0_A0();
1549
}
1550

    
1551
#else
1552

    
1553
void OPPROTO op_fild_ST0_A0(void)
1554
{
1555
    int new_fpstt;
1556
    new_fpstt = (env->fpstt - 1) & 7;
1557
#ifdef USE_FP_CONVERT
1558
    FP_CONVERT.i32 = ldsw((void *)A0);
1559
    env->fpregs[new_fpstt] = (CPU86_LDouble)FP_CONVERT.i32;
1560
#else
1561
    env->fpregs[new_fpstt] = (CPU86_LDouble)ldsw((void *)A0);
1562
#endif
1563
    env->fpstt = new_fpstt;
1564
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1565
}
1566

    
1567
void OPPROTO op_fildl_ST0_A0(void)
1568
{
1569
    int new_fpstt;
1570
    new_fpstt = (env->fpstt - 1) & 7;
1571
#ifdef USE_FP_CONVERT
1572
    FP_CONVERT.i32 = (int32_t) ldl((void *)A0);
1573
    env->fpregs[new_fpstt] = (CPU86_LDouble)FP_CONVERT.i32;
1574
#else
1575
    env->fpregs[new_fpstt] = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1576
#endif
1577
    env->fpstt = new_fpstt;
1578
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1579
}
1580

    
1581
void OPPROTO op_fildll_ST0_A0(void)
1582
{
1583
    int new_fpstt;
1584
    new_fpstt = (env->fpstt - 1) & 7;
1585
#ifdef USE_FP_CONVERT
1586
    FP_CONVERT.i64 = (int64_t) ldq((void *)A0);
1587
    env->fpregs[new_fpstt] = (CPU86_LDouble)FP_CONVERT.i64;
1588
#else
1589
    env->fpregs[new_fpstt] = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1590
#endif
1591
    env->fpstt = new_fpstt;
1592
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1593
}
1594

    
1595
#endif
1596

    
1597
/* fp store */
1598

    
1599
void OPPROTO op_fsts_ST0_A0(void)
1600
{
1601
#ifdef USE_FP_CONVERT
1602
    FP_CONVERT.f = (float)ST0;
1603
    stfl((void *)A0, FP_CONVERT.f);
1604
#else
1605
    stfl((void *)A0, (float)ST0);
1606
#endif
1607
}
1608

    
1609
void OPPROTO op_fstl_ST0_A0(void)
1610
{
1611
    stfq((void *)A0, (double)ST0);
1612
}
1613

    
1614
void OPPROTO op_fstt_ST0_A0(void)
1615
{
1616
    helper_fstt_ST0_A0();
1617
}
1618

    
1619
void OPPROTO op_fist_ST0_A0(void)
1620
{
1621
#if defined(__sparc__) && !defined(__sparc_v9__)
1622
    register CPU86_LDouble d asm("o0");
1623
#else
1624
    CPU86_LDouble d;
1625
#endif
1626
    int val;
1627

    
1628
    d = ST0;
1629
    val = lrint(d);
1630
    if (val != (int16_t)val)
1631
        val = -32768;
1632
    stw((void *)A0, val);
1633
}
1634

    
1635
void OPPROTO op_fistl_ST0_A0(void)
1636
{
1637
#if defined(__sparc__) && !defined(__sparc_v9__)
1638
    register CPU86_LDouble d asm("o0");
1639
#else
1640
    CPU86_LDouble d;
1641
#endif
1642
    int val;
1643

    
1644
    d = ST0;
1645
    val = lrint(d);
1646
    stl((void *)A0, val);
1647
}
1648

    
1649
void OPPROTO op_fistll_ST0_A0(void)
1650
{
1651
#if defined(__sparc__) && !defined(__sparc_v9__)
1652
    register CPU86_LDouble d asm("o0");
1653
#else
1654
    CPU86_LDouble d;
1655
#endif
1656
    int64_t val;
1657

    
1658
    d = ST0;
1659
    val = llrint(d);
1660
    stq((void *)A0, val);
1661
}
1662

    
1663
void OPPROTO op_fbld_ST0_A0(void)
1664
{
1665
    helper_fbld_ST0_A0();
1666
}
1667

    
1668
void OPPROTO op_fbst_ST0_A0(void)
1669
{
1670
    helper_fbst_ST0_A0();
1671
}
1672

    
1673
/* FPU move */
1674

    
1675
void OPPROTO op_fpush(void)
1676
{
1677
    fpush();
1678
}
1679

    
1680
void OPPROTO op_fpop(void)
1681
{
1682
    fpop();
1683
}
1684

    
1685
void OPPROTO op_fdecstp(void)
1686
{
1687
    env->fpstt = (env->fpstt - 1) & 7;
1688
    env->fpus &= (~0x4700);
1689
}
1690

    
1691
void OPPROTO op_fincstp(void)
1692
{
1693
    env->fpstt = (env->fpstt + 1) & 7;
1694
    env->fpus &= (~0x4700);
1695
}
1696

    
1697
void OPPROTO op_fmov_ST0_FT0(void)
1698
{
1699
    ST0 = FT0;
1700
}
1701

    
1702
void OPPROTO op_fmov_FT0_STN(void)
1703
{
1704
    FT0 = ST(PARAM1);
1705
}
1706

    
1707
void OPPROTO op_fmov_ST0_STN(void)
1708
{
1709
    ST0 = ST(PARAM1);
1710
}
1711

    
1712
void OPPROTO op_fmov_STN_ST0(void)
1713
{
1714
    ST(PARAM1) = ST0;
1715
}
1716

    
1717
void OPPROTO op_fxchg_ST0_STN(void)
1718
{
1719
    CPU86_LDouble tmp;
1720
    tmp = ST(PARAM1);
1721
    ST(PARAM1) = ST0;
1722
    ST0 = tmp;
1723
}
1724

    
1725
/* FPU operations */
1726

    
1727
/* XXX: handle nans */
1728
void OPPROTO op_fcom_ST0_FT0(void)
1729
{
1730
    env->fpus &= (~0x4500);        /* (C3,C2,C0) <-- 000 */
1731
    if (ST0 < FT0)
1732
        env->fpus |= 0x100;        /* (C3,C2,C0) <-- 001 */
1733
    else if (ST0 == FT0)
1734
        env->fpus |= 0x4000; /* (C3,C2,C0) <-- 100 */
1735
    FORCE_RET();
1736
}
1737

    
1738
/* XXX: handle nans */
1739
void OPPROTO op_fucom_ST0_FT0(void)
1740
{
1741
    env->fpus &= (~0x4500);        /* (C3,C2,C0) <-- 000 */
1742
    if (ST0 < FT0)
1743
        env->fpus |= 0x100;        /* (C3,C2,C0) <-- 001 */
1744
    else if (ST0 == FT0)
1745
        env->fpus |= 0x4000; /* (C3,C2,C0) <-- 100 */
1746
    FORCE_RET();
1747
}
1748

    
1749
/* XXX: handle nans */
1750
void OPPROTO op_fcomi_ST0_FT0(void)
1751
{
1752
    int eflags;
1753
    eflags = cc_table[CC_OP].compute_all();
1754
    eflags &= ~(CC_Z | CC_P | CC_C);
1755
    if (ST0 < FT0)
1756
        eflags |= CC_C;
1757
    else if (ST0 == FT0)
1758
        eflags |= CC_Z;
1759
    CC_SRC = eflags;
1760
    FORCE_RET();
1761
}
1762

    
1763
/* XXX: handle nans */
1764
void OPPROTO op_fucomi_ST0_FT0(void)
1765
{
1766
    int eflags;
1767
    eflags = cc_table[CC_OP].compute_all();
1768
    eflags &= ~(CC_Z | CC_P | CC_C);
1769
    if (ST0 < FT0)
1770
        eflags |= CC_C;
1771
    else if (ST0 == FT0)
1772
        eflags |= CC_Z;
1773
    CC_SRC = eflags;
1774
    FORCE_RET();
1775
}
1776

    
1777
void OPPROTO op_fcmov_ST0_STN_T0(void)
1778
{
1779
    if (T0) {
1780
        ST0 = ST(PARAM1);
1781
    }
1782
    FORCE_RET();
1783
}
1784

    
1785
void OPPROTO op_fadd_ST0_FT0(void)
1786
{
1787
    ST0 += FT0;
1788
}
1789

    
1790
void OPPROTO op_fmul_ST0_FT0(void)
1791
{
1792
    ST0 *= FT0;
1793
}
1794

    
1795
void OPPROTO op_fsub_ST0_FT0(void)
1796
{
1797
    ST0 -= FT0;
1798
}
1799

    
1800
void OPPROTO op_fsubr_ST0_FT0(void)
1801
{
1802
    ST0 = FT0 - ST0;
1803
}
1804

    
1805
void OPPROTO op_fdiv_ST0_FT0(void)
1806
{
1807
    ST0 /= FT0;
1808
}
1809

    
1810
void OPPROTO op_fdivr_ST0_FT0(void)
1811
{
1812
    ST0 = FT0 / ST0;
1813
}
1814

    
1815
/* fp operations between STN and ST0 */
1816

    
1817
void OPPROTO op_fadd_STN_ST0(void)
1818
{
1819
    ST(PARAM1) += ST0;
1820
}
1821

    
1822
void OPPROTO op_fmul_STN_ST0(void)
1823
{
1824
    ST(PARAM1) *= ST0;
1825
}
1826

    
1827
void OPPROTO op_fsub_STN_ST0(void)
1828
{
1829
    ST(PARAM1) -= ST0;
1830
}
1831

    
1832
void OPPROTO op_fsubr_STN_ST0(void)
1833
{
1834
    CPU86_LDouble *p;
1835
    p = &ST(PARAM1);
1836
    *p = ST0 - *p;
1837
}
1838

    
1839
void OPPROTO op_fdiv_STN_ST0(void)
1840
{
1841
    ST(PARAM1) /= ST0;
1842
}
1843

    
1844
void OPPROTO op_fdivr_STN_ST0(void)
1845
{
1846
    CPU86_LDouble *p;
1847
    p = &ST(PARAM1);
1848
    *p = ST0 / *p;
1849
}
1850

    
1851
/* misc FPU operations */
1852
void OPPROTO op_fchs_ST0(void)
1853
{
1854
    ST0 = -ST0;
1855
}
1856

    
1857
void OPPROTO op_fabs_ST0(void)
1858
{
1859
    ST0 = fabs(ST0);
1860
}
1861

    
1862
void OPPROTO op_fxam_ST0(void)
1863
{
1864
    helper_fxam_ST0();
1865
}
1866

    
1867
void OPPROTO op_fld1_ST0(void)
1868
{
1869
    ST0 = f15rk[1];
1870
}
1871

    
1872
void OPPROTO op_fldl2t_ST0(void)
1873
{
1874
    ST0 = f15rk[6];
1875
}
1876

    
1877
void OPPROTO op_fldl2e_ST0(void)
1878
{
1879
    ST0 = f15rk[5];
1880
}
1881

    
1882
void OPPROTO op_fldpi_ST0(void)
1883
{
1884
    ST0 = f15rk[2];
1885
}
1886

    
1887
void OPPROTO op_fldlg2_ST0(void)
1888
{
1889
    ST0 = f15rk[3];
1890
}
1891

    
1892
void OPPROTO op_fldln2_ST0(void)
1893
{
1894
    ST0 = f15rk[4];
1895
}
1896

    
1897
void OPPROTO op_fldz_ST0(void)
1898
{
1899
    ST0 = f15rk[0];
1900
}
1901

    
1902
void OPPROTO op_fldz_FT0(void)
1903
{
1904
    FT0 = f15rk[0];
1905
}
1906

    
1907
/* associated heplers to reduce generated code length and to simplify
1908
   relocation (FP constants are usually stored in .rodata section) */
1909

    
1910
void OPPROTO op_f2xm1(void)
1911
{
1912
    helper_f2xm1();
1913
}
1914

    
1915
void OPPROTO op_fyl2x(void)
1916
{
1917
    helper_fyl2x();
1918
}
1919

    
1920
void OPPROTO op_fptan(void)
1921
{
1922
    helper_fptan();
1923
}
1924

    
1925
void OPPROTO op_fpatan(void)
1926
{
1927
    helper_fpatan();
1928
}
1929

    
1930
void OPPROTO op_fxtract(void)
1931
{
1932
    helper_fxtract();
1933
}
1934

    
1935
void OPPROTO op_fprem1(void)
1936
{
1937
    helper_fprem1();
1938
}
1939

    
1940

    
1941
void OPPROTO op_fprem(void)
1942
{
1943
    helper_fprem();
1944
}
1945

    
1946
void OPPROTO op_fyl2xp1(void)
1947
{
1948
    helper_fyl2xp1();
1949
}
1950

    
1951
void OPPROTO op_fsqrt(void)
1952
{
1953
    helper_fsqrt();
1954
}
1955

    
1956
void OPPROTO op_fsincos(void)
1957
{
1958
    helper_fsincos();
1959
}
1960

    
1961
void OPPROTO op_frndint(void)
1962
{
1963
    helper_frndint();
1964
}
1965

    
1966
void OPPROTO op_fscale(void)
1967
{
1968
    helper_fscale();
1969
}
1970

    
1971
void OPPROTO op_fsin(void)
1972
{
1973
    helper_fsin();
1974
}
1975

    
1976
void OPPROTO op_fcos(void)
1977
{
1978
    helper_fcos();
1979
}
1980

    
1981
void OPPROTO op_fnstsw_A0(void)
1982
{
1983
    int fpus;
1984
    fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1985
    stw((void *)A0, fpus);
1986
}
1987

    
1988
void OPPROTO op_fnstsw_EAX(void)
1989
{
1990
    int fpus;
1991
    fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1992
    EAX = (EAX & 0xffff0000) | fpus;
1993
}
1994

    
1995
void OPPROTO op_fnstcw_A0(void)
1996
{
1997
    stw((void *)A0, env->fpuc);
1998
}
1999

    
2000
void OPPROTO op_fldcw_A0(void)
2001
{
2002
    int rnd_type;
2003
    env->fpuc = lduw((void *)A0);
2004
    /* set rounding mode */
2005
    switch(env->fpuc & RC_MASK) {
2006
    default:
2007
    case RC_NEAR:
2008
        rnd_type = FE_TONEAREST;
2009
        break;
2010
    case RC_DOWN:
2011
        rnd_type = FE_DOWNWARD;
2012
        break;
2013
    case RC_UP:
2014
        rnd_type = FE_UPWARD;
2015
        break;
2016
    case RC_CHOP:
2017
        rnd_type = FE_TOWARDZERO;
2018
        break;
2019
    }
2020
    fesetround(rnd_type);
2021
}
2022

    
2023
void OPPROTO op_fclex(void)
2024
{
2025
    env->fpus &= 0x7f00;
2026
}
2027

    
2028
void OPPROTO op_fninit(void)
2029
{
2030
    env->fpus = 0;
2031
    env->fpstt = 0;
2032
    env->fpuc = 0x37f;
2033
    env->fptags[0] = 1;
2034
    env->fptags[1] = 1;
2035
    env->fptags[2] = 1;
2036
    env->fptags[3] = 1;
2037
    env->fptags[4] = 1;
2038
    env->fptags[5] = 1;
2039
    env->fptags[6] = 1;
2040
    env->fptags[7] = 1;
2041
}
2042

    
2043
void OPPROTO op_fnstenv_A0(void)
2044
{
2045
    helper_fstenv((uint8_t *)A0, PARAM1);
2046
}
2047

    
2048
void OPPROTO op_fldenv_A0(void)
2049
{
2050
    helper_fldenv((uint8_t *)A0, PARAM1);
2051
}
2052

    
2053
void OPPROTO op_fnsave_A0(void)
2054
{
2055
    helper_fsave((uint8_t *)A0, PARAM1);
2056
}
2057

    
2058
void OPPROTO op_frstor_A0(void)
2059
{
2060
    helper_frstor((uint8_t *)A0, PARAM1);
2061
}
2062

    
2063
/* threading support */
2064
void OPPROTO op_lock(void)
2065
{
2066
    cpu_lock();
2067
}
2068

    
2069
void OPPROTO op_unlock(void)
2070
{
2071
    cpu_unlock();
2072
}
2073