Statistics
| Branch: | Revision:

root / target-i386 / op.c @ 61382a50

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
/* T0: segment, T1:eip */
940
void OPPROTO op_ljmp_protected_T0_T1(void)
941
{
942
    helper_ljmp_protected_T0_T1();
943
}
944

    
945
void OPPROTO op_lcall_real_T0_T1(void)
946
{
947
    helper_lcall_real_T0_T1(PARAM1, PARAM2);
948
}
949

    
950
void OPPROTO op_lcall_protected_T0_T1(void)
951
{
952
    helper_lcall_protected_T0_T1(PARAM1, PARAM2);
953
}
954

    
955
void OPPROTO op_iret_real(void)
956
{
957
    helper_iret_real(PARAM1);
958
}
959

    
960
void OPPROTO op_iret_protected(void)
961
{
962
    helper_iret_protected(PARAM1);
963
}
964

    
965
void OPPROTO op_lret_protected(void)
966
{
967
    helper_lret_protected(PARAM1, PARAM2);
968
}
969

    
970
void OPPROTO op_lldt_T0(void)
971
{
972
    helper_lldt_T0();
973
}
974

    
975
void OPPROTO op_ltr_T0(void)
976
{
977
    helper_ltr_T0();
978
}
979

    
980
/* CR registers access */
981
void OPPROTO op_movl_crN_T0(void)
982
{
983
    helper_movl_crN_T0(PARAM1);
984
}
985

    
986
/* DR registers access */
987
void OPPROTO op_movl_drN_T0(void)
988
{
989
    helper_movl_drN_T0(PARAM1);
990
}
991

    
992
void OPPROTO op_lmsw_T0(void)
993
{
994
    /* only 4 lower bits of CR0 are modified */
995
    T0 = (env->cr[0] & ~0xf) | (T0 & 0xf);
996
    helper_movl_crN_T0(0);
997
}
998

    
999
void OPPROTO op_invlpg_A0(void)
1000
{
1001
    helper_invlpg(A0);
1002
}
1003

    
1004
void OPPROTO op_movl_T0_env(void)
1005
{
1006
    T0 = *(uint32_t *)((char *)env + PARAM1);
1007
}
1008

    
1009
void OPPROTO op_movl_env_T0(void)
1010
{
1011
    *(uint32_t *)((char *)env + PARAM1) = T0;
1012
}
1013

    
1014
void OPPROTO op_movl_env_T1(void)
1015
{
1016
    *(uint32_t *)((char *)env + PARAM1) = T1;
1017
}
1018

    
1019
void OPPROTO op_clts(void)
1020
{
1021
    env->cr[0] &= ~CR0_TS_MASK;
1022
}
1023

    
1024
/* flags handling */
1025

    
1026
/* slow jumps cases : in order to avoid calling a function with a
1027
   pointer (which can generate a stack frame on PowerPC), we use
1028
   op_setcc to set T0 and then call op_jcc. */
1029
void OPPROTO op_jcc(void)
1030
{
1031
    if (T0)
1032
        JUMP_TB(op_jcc, PARAM1, 0, PARAM2);
1033
    else
1034
        JUMP_TB(op_jcc, PARAM1, 1, PARAM3);
1035
    FORCE_RET();
1036
}
1037

    
1038
void OPPROTO op_jcc_im(void)
1039
{
1040
    if (T0)
1041
        EIP = PARAM1;
1042
    else
1043
        EIP = PARAM2;
1044
    FORCE_RET();
1045
}
1046

    
1047
/* slow set cases (compute x86 flags) */
1048
void OPPROTO op_seto_T0_cc(void)
1049
{
1050
    int eflags;
1051
    eflags = cc_table[CC_OP].compute_all();
1052
    T0 = (eflags >> 11) & 1;
1053
}
1054

    
1055
void OPPROTO op_setb_T0_cc(void)
1056
{
1057
    T0 = cc_table[CC_OP].compute_c();
1058
}
1059

    
1060
void OPPROTO op_setz_T0_cc(void)
1061
{
1062
    int eflags;
1063
    eflags = cc_table[CC_OP].compute_all();
1064
    T0 = (eflags >> 6) & 1;
1065
}
1066

    
1067
void OPPROTO op_setbe_T0_cc(void)
1068
{
1069
    int eflags;
1070
    eflags = cc_table[CC_OP].compute_all();
1071
    T0 = (eflags & (CC_Z | CC_C)) != 0;
1072
}
1073

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

    
1081
void OPPROTO op_setp_T0_cc(void)
1082
{
1083
    int eflags;
1084
    eflags = cc_table[CC_OP].compute_all();
1085
    T0 = (eflags >> 2) & 1;
1086
}
1087

    
1088
void OPPROTO op_setl_T0_cc(void)
1089
{
1090
    int eflags;
1091
    eflags = cc_table[CC_OP].compute_all();
1092
    T0 = ((eflags ^ (eflags >> 4)) >> 7) & 1;
1093
}
1094

    
1095
void OPPROTO op_setle_T0_cc(void)
1096
{
1097
    int eflags;
1098
    eflags = cc_table[CC_OP].compute_all();
1099
    T0 = (((eflags ^ (eflags >> 4)) & 0x80) || (eflags & CC_Z)) != 0;
1100
}
1101

    
1102
void OPPROTO op_xor_T0_1(void)
1103
{
1104
    T0 ^= 1;
1105
}
1106

    
1107
void OPPROTO op_set_cc_op(void)
1108
{
1109
    CC_OP = PARAM1;
1110
}
1111

    
1112
#define FL_UPDATE_MASK16 (FL_UPDATE_MASK32 & 0xffff)
1113

    
1114
void OPPROTO op_movl_eflags_T0(void)
1115
{
1116
    int eflags;
1117
    eflags = T0;
1118
    CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1119
    DF = 1 - (2 * ((eflags >> 10) & 1));
1120
    /* we also update some system flags as in user mode */
1121
    env->eflags = (env->eflags & ~FL_UPDATE_MASK32) | 
1122
        (eflags & FL_UPDATE_MASK32);
1123
}
1124

    
1125
void OPPROTO op_movw_eflags_T0(void)
1126
{
1127
    int eflags;
1128
    eflags = T0;
1129
    CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1130
    DF = 1 - (2 * ((eflags >> 10) & 1));
1131
    /* we also update some system flags as in user mode */
1132
    env->eflags = (env->eflags & ~FL_UPDATE_MASK16) | 
1133
        (eflags & FL_UPDATE_MASK16);
1134
}
1135

    
1136
void OPPROTO op_movl_eflags_T0_cpl0(void)
1137
{
1138
    load_eflags(T0, FL_UPDATE_CPL0_MASK);
1139
}
1140

    
1141
void OPPROTO op_movw_eflags_T0_cpl0(void)
1142
{
1143
    load_eflags(T0, FL_UPDATE_CPL0_MASK & 0xffff);
1144
}
1145

    
1146
#if 0
1147
/* vm86plus version */
1148
void OPPROTO op_movw_eflags_T0_vm(void)
1149
{
1150
    int eflags;
1151
    eflags = T0;
1152
    CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1153
    DF = 1 - (2 * ((eflags >> 10) & 1));
1154
    /* we also update some system flags as in user mode */
1155
    env->eflags = (env->eflags & ~(FL_UPDATE_MASK16 | VIF_MASK)) |
1156
        (eflags & FL_UPDATE_MASK16);
1157
    if (eflags & IF_MASK) {
1158
        env->eflags |= VIF_MASK;
1159
        if (env->eflags & VIP_MASK) {
1160
            EIP = PARAM1;
1161
            raise_exception(EXCP0D_GPF);
1162
        }
1163
    }
1164
    FORCE_RET();
1165
}
1166

1167
void OPPROTO op_movl_eflags_T0_vm(void)
1168
{
1169
    int eflags;
1170
    eflags = T0;
1171
    CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1172
    DF = 1 - (2 * ((eflags >> 10) & 1));
1173
    /* we also update some system flags as in user mode */
1174
    env->eflags = (env->eflags & ~(FL_UPDATE_MASK32 | VIF_MASK)) |
1175
        (eflags & FL_UPDATE_MASK32);
1176
    if (eflags & IF_MASK) {
1177
        env->eflags |= VIF_MASK;
1178
        if (env->eflags & VIP_MASK) {
1179
            EIP = PARAM1;
1180
            raise_exception(EXCP0D_GPF);
1181
        }
1182
    }
1183
    FORCE_RET();
1184
}
1185
#endif
1186

    
1187
/* XXX: compute only O flag */
1188
void OPPROTO op_movb_eflags_T0(void)
1189
{
1190
    int of;
1191
    of = cc_table[CC_OP].compute_all() & CC_O;
1192
    CC_SRC = (T0 & (CC_S | CC_Z | CC_A | CC_P | CC_C)) | of;
1193
}
1194

    
1195
void OPPROTO op_movl_T0_eflags(void)
1196
{
1197
    int eflags;
1198
    eflags = cc_table[CC_OP].compute_all();
1199
    eflags |= (DF & DF_MASK);
1200
    eflags |= env->eflags & ~(VM_MASK | RF_MASK);
1201
    T0 = eflags;
1202
}
1203

    
1204
/* vm86plus version */
1205
#if 0
1206
void OPPROTO op_movl_T0_eflags_vm(void)
1207
{
1208
    int eflags;
1209
    eflags = cc_table[CC_OP].compute_all();
1210
    eflags |= (DF & DF_MASK);
1211
    eflags |= env->eflags & ~(VM_MASK | RF_MASK | IF_MASK);
1212
    if (env->eflags & VIF_MASK)
1213
        eflags |= IF_MASK;
1214
    T0 = eflags;
1215
}
1216
#endif
1217

    
1218
void OPPROTO op_cld(void)
1219
{
1220
    DF = 1;
1221
}
1222

    
1223
void OPPROTO op_std(void)
1224
{
1225
    DF = -1;
1226
}
1227

    
1228
void OPPROTO op_clc(void)
1229
{
1230
    int eflags;
1231
    eflags = cc_table[CC_OP].compute_all();
1232
    eflags &= ~CC_C;
1233
    CC_SRC = eflags;
1234
}
1235

    
1236
void OPPROTO op_stc(void)
1237
{
1238
    int eflags;
1239
    eflags = cc_table[CC_OP].compute_all();
1240
    eflags |= CC_C;
1241
    CC_SRC = eflags;
1242
}
1243

    
1244
void OPPROTO op_cmc(void)
1245
{
1246
    int eflags;
1247
    eflags = cc_table[CC_OP].compute_all();
1248
    eflags ^= CC_C;
1249
    CC_SRC = eflags;
1250
}
1251

    
1252
void OPPROTO op_salc(void)
1253
{
1254
    int cf;
1255
    cf = cc_table[CC_OP].compute_c();
1256
    EAX = (EAX & ~0xff) | ((-cf) & 0xff);
1257
}
1258

    
1259
static int compute_all_eflags(void)
1260
{
1261
    return CC_SRC;
1262
}
1263

    
1264
static int compute_c_eflags(void)
1265
{
1266
    return CC_SRC & CC_C;
1267
}
1268

    
1269
static int compute_c_mul(void)
1270
{
1271
    int cf;
1272
    cf = (CC_SRC != 0);
1273
    return cf;
1274
}
1275

    
1276
static int compute_all_mul(void)
1277
{
1278
    int cf, pf, af, zf, sf, of;
1279
    cf = (CC_SRC != 0);
1280
    pf = 0; /* undefined */
1281
    af = 0; /* undefined */
1282
    zf = 0; /* undefined */
1283
    sf = 0; /* undefined */
1284
    of = cf << 11;
1285
    return cf | pf | af | zf | sf | of;
1286
}
1287
    
1288
CCTable cc_table[CC_OP_NB] = {
1289
    [CC_OP_DYNAMIC] = { /* should never happen */ },
1290

    
1291
    [CC_OP_EFLAGS] = { compute_all_eflags, compute_c_eflags },
1292

    
1293
    [CC_OP_MUL] = { compute_all_mul, compute_c_mul },
1294

    
1295
    [CC_OP_ADDB] = { compute_all_addb, compute_c_addb },
1296
    [CC_OP_ADDW] = { compute_all_addw, compute_c_addw  },
1297
    [CC_OP_ADDL] = { compute_all_addl, compute_c_addl  },
1298

    
1299
    [CC_OP_ADCB] = { compute_all_adcb, compute_c_adcb },
1300
    [CC_OP_ADCW] = { compute_all_adcw, compute_c_adcw  },
1301
    [CC_OP_ADCL] = { compute_all_adcl, compute_c_adcl  },
1302

    
1303
    [CC_OP_SUBB] = { compute_all_subb, compute_c_subb  },
1304
    [CC_OP_SUBW] = { compute_all_subw, compute_c_subw  },
1305
    [CC_OP_SUBL] = { compute_all_subl, compute_c_subl  },
1306
    
1307
    [CC_OP_SBBB] = { compute_all_sbbb, compute_c_sbbb  },
1308
    [CC_OP_SBBW] = { compute_all_sbbw, compute_c_sbbw  },
1309
    [CC_OP_SBBL] = { compute_all_sbbl, compute_c_sbbl  },
1310
    
1311
    [CC_OP_LOGICB] = { compute_all_logicb, compute_c_logicb },
1312
    [CC_OP_LOGICW] = { compute_all_logicw, compute_c_logicw },
1313
    [CC_OP_LOGICL] = { compute_all_logicl, compute_c_logicl },
1314
    
1315
    [CC_OP_INCB] = { compute_all_incb, compute_c_incl },
1316
    [CC_OP_INCW] = { compute_all_incw, compute_c_incl },
1317
    [CC_OP_INCL] = { compute_all_incl, compute_c_incl },
1318
    
1319
    [CC_OP_DECB] = { compute_all_decb, compute_c_incl },
1320
    [CC_OP_DECW] = { compute_all_decw, compute_c_incl },
1321
    [CC_OP_DECL] = { compute_all_decl, compute_c_incl },
1322
    
1323
    [CC_OP_SHLB] = { compute_all_shlb, compute_c_shlb },
1324
    [CC_OP_SHLW] = { compute_all_shlw, compute_c_shlw },
1325
    [CC_OP_SHLL] = { compute_all_shll, compute_c_shll },
1326

    
1327
    [CC_OP_SARB] = { compute_all_sarb, compute_c_sarl },
1328
    [CC_OP_SARW] = { compute_all_sarw, compute_c_sarl },
1329
    [CC_OP_SARL] = { compute_all_sarl, compute_c_sarl },
1330
};
1331

    
1332
/* floating point support. Some of the code for complicated x87
1333
   functions comes from the LGPL'ed x86 emulator found in the Willows
1334
   TWIN windows emulator. */
1335

    
1336
#if defined(__powerpc__)
1337
extern CPU86_LDouble copysign(CPU86_LDouble, CPU86_LDouble);
1338

    
1339
/* correct (but slow) PowerPC rint() (glibc version is incorrect) */
1340
double qemu_rint(double x)
1341
{
1342
    double y = 4503599627370496.0;
1343
    if (fabs(x) >= y)
1344
        return x;
1345
    if (x < 0) 
1346
        y = -y;
1347
    y = (x + y) - y;
1348
    if (y == 0.0)
1349
        y = copysign(y, x);
1350
    return y;
1351
}
1352

    
1353
#define rint qemu_rint
1354
#endif
1355

    
1356
/* fp load FT0 */
1357

    
1358
void OPPROTO op_flds_FT0_A0(void)
1359
{
1360
#ifdef USE_FP_CONVERT
1361
    FP_CONVERT.i32 = ldl((void *)A0);
1362
    FT0 = FP_CONVERT.f;
1363
#else
1364
    FT0 = ldfl((void *)A0);
1365
#endif
1366
}
1367

    
1368
void OPPROTO op_fldl_FT0_A0(void)
1369
{
1370
#ifdef USE_FP_CONVERT
1371
    FP_CONVERT.i64 = ldq((void *)A0);
1372
    FT0 = FP_CONVERT.d;
1373
#else
1374
    FT0 = ldfq((void *)A0);
1375
#endif
1376
}
1377

    
1378
/* helpers are needed to avoid static constant reference. XXX: find a better way */
1379
#ifdef USE_INT_TO_FLOAT_HELPERS
1380

    
1381
void helper_fild_FT0_A0(void)
1382
{
1383
    FT0 = (CPU86_LDouble)ldsw((void *)A0);
1384
}
1385

    
1386
void helper_fildl_FT0_A0(void)
1387
{
1388
    FT0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1389
}
1390

    
1391
void helper_fildll_FT0_A0(void)
1392
{
1393
    FT0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1394
}
1395

    
1396
void OPPROTO op_fild_FT0_A0(void)
1397
{
1398
    helper_fild_FT0_A0();
1399
}
1400

    
1401
void OPPROTO op_fildl_FT0_A0(void)
1402
{
1403
    helper_fildl_FT0_A0();
1404
}
1405

    
1406
void OPPROTO op_fildll_FT0_A0(void)
1407
{
1408
    helper_fildll_FT0_A0();
1409
}
1410

    
1411
#else
1412

    
1413
void OPPROTO op_fild_FT0_A0(void)
1414
{
1415
#ifdef USE_FP_CONVERT
1416
    FP_CONVERT.i32 = ldsw((void *)A0);
1417
    FT0 = (CPU86_LDouble)FP_CONVERT.i32;
1418
#else
1419
    FT0 = (CPU86_LDouble)ldsw((void *)A0);
1420
#endif
1421
}
1422

    
1423
void OPPROTO op_fildl_FT0_A0(void)
1424
{
1425
#ifdef USE_FP_CONVERT
1426
    FP_CONVERT.i32 = (int32_t) ldl((void *)A0);
1427
    FT0 = (CPU86_LDouble)FP_CONVERT.i32;
1428
#else
1429
    FT0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1430
#endif
1431
}
1432

    
1433
void OPPROTO op_fildll_FT0_A0(void)
1434
{
1435
#ifdef USE_FP_CONVERT
1436
    FP_CONVERT.i64 = (int64_t) ldq((void *)A0);
1437
    FT0 = (CPU86_LDouble)FP_CONVERT.i64;
1438
#else
1439
    FT0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1440
#endif
1441
}
1442
#endif
1443

    
1444
/* fp load ST0 */
1445

    
1446
void OPPROTO op_flds_ST0_A0(void)
1447
{
1448
    int new_fpstt;
1449
    new_fpstt = (env->fpstt - 1) & 7;
1450
#ifdef USE_FP_CONVERT
1451
    FP_CONVERT.i32 = ldl((void *)A0);
1452
    env->fpregs[new_fpstt] = FP_CONVERT.f;
1453
#else
1454
    env->fpregs[new_fpstt] = ldfl((void *)A0);
1455
#endif
1456
    env->fpstt = new_fpstt;
1457
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1458
}
1459

    
1460
void OPPROTO op_fldl_ST0_A0(void)
1461
{
1462
    int new_fpstt;
1463
    new_fpstt = (env->fpstt - 1) & 7;
1464
#ifdef USE_FP_CONVERT
1465
    FP_CONVERT.i64 = ldq((void *)A0);
1466
    env->fpregs[new_fpstt] = FP_CONVERT.d;
1467
#else
1468
    env->fpregs[new_fpstt] = ldfq((void *)A0);
1469
#endif
1470
    env->fpstt = new_fpstt;
1471
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1472
}
1473

    
1474
#ifdef USE_X86LDOUBLE
1475
void OPPROTO op_fldt_ST0_A0(void)
1476
{
1477
    int new_fpstt;
1478
    new_fpstt = (env->fpstt - 1) & 7;
1479
    env->fpregs[new_fpstt] = *(long double *)A0;
1480
    env->fpstt = new_fpstt;
1481
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1482
}
1483
#else
1484
void OPPROTO op_fldt_ST0_A0(void)
1485
{
1486
    helper_fldt_ST0_A0();
1487
}
1488
#endif
1489

    
1490
/* helpers are needed to avoid static constant reference. XXX: find a better way */
1491
#ifdef USE_INT_TO_FLOAT_HELPERS
1492

    
1493
void helper_fild_ST0_A0(void)
1494
{
1495
    int new_fpstt;
1496
    new_fpstt = (env->fpstt - 1) & 7;
1497
    env->fpregs[new_fpstt] = (CPU86_LDouble)ldsw((void *)A0);
1498
    env->fpstt = new_fpstt;
1499
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1500
}
1501

    
1502
void helper_fildl_ST0_A0(void)
1503
{
1504
    int new_fpstt;
1505
    new_fpstt = (env->fpstt - 1) & 7;
1506
    env->fpregs[new_fpstt] = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1507
    env->fpstt = new_fpstt;
1508
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1509
}
1510

    
1511
void helper_fildll_ST0_A0(void)
1512
{
1513
    int new_fpstt;
1514
    new_fpstt = (env->fpstt - 1) & 7;
1515
    env->fpregs[new_fpstt] = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1516
    env->fpstt = new_fpstt;
1517
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1518
}
1519

    
1520
void OPPROTO op_fild_ST0_A0(void)
1521
{
1522
    helper_fild_ST0_A0();
1523
}
1524

    
1525
void OPPROTO op_fildl_ST0_A0(void)
1526
{
1527
    helper_fildl_ST0_A0();
1528
}
1529

    
1530
void OPPROTO op_fildll_ST0_A0(void)
1531
{
1532
    helper_fildll_ST0_A0();
1533
}
1534

    
1535
#else
1536

    
1537
void OPPROTO op_fild_ST0_A0(void)
1538
{
1539
    int new_fpstt;
1540
    new_fpstt = (env->fpstt - 1) & 7;
1541
#ifdef USE_FP_CONVERT
1542
    FP_CONVERT.i32 = ldsw((void *)A0);
1543
    env->fpregs[new_fpstt] = (CPU86_LDouble)FP_CONVERT.i32;
1544
#else
1545
    env->fpregs[new_fpstt] = (CPU86_LDouble)ldsw((void *)A0);
1546
#endif
1547
    env->fpstt = new_fpstt;
1548
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1549
}
1550

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

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

    
1579
#endif
1580

    
1581
/* fp store */
1582

    
1583
void OPPROTO op_fsts_ST0_A0(void)
1584
{
1585
#ifdef USE_FP_CONVERT
1586
    FP_CONVERT.f = (float)ST0;
1587
    stfl((void *)A0, FP_CONVERT.f);
1588
#else
1589
    stfl((void *)A0, (float)ST0);
1590
#endif
1591
}
1592

    
1593
void OPPROTO op_fstl_ST0_A0(void)
1594
{
1595
    stfq((void *)A0, (double)ST0);
1596
}
1597

    
1598
#ifdef USE_X86LDOUBLE
1599
void OPPROTO op_fstt_ST0_A0(void)
1600
{
1601
    *(long double *)A0 = ST0;
1602
}
1603
#else
1604
void OPPROTO op_fstt_ST0_A0(void)
1605
{
1606
    helper_fstt_ST0_A0();
1607
}
1608
#endif
1609

    
1610
void OPPROTO op_fist_ST0_A0(void)
1611
{
1612
#if defined(__sparc__) && !defined(__sparc_v9__)
1613
    register CPU86_LDouble d asm("o0");
1614
#else
1615
    CPU86_LDouble d;
1616
#endif
1617
    int val;
1618

    
1619
    d = ST0;
1620
    val = lrint(d);
1621
    if (val != (int16_t)val)
1622
        val = -32768;
1623
    stw((void *)A0, val);
1624
}
1625

    
1626
void OPPROTO op_fistl_ST0_A0(void)
1627
{
1628
#if defined(__sparc__) && !defined(__sparc_v9__)
1629
    register CPU86_LDouble d asm("o0");
1630
#else
1631
    CPU86_LDouble d;
1632
#endif
1633
    int val;
1634

    
1635
    d = ST0;
1636
    val = lrint(d);
1637
    stl((void *)A0, val);
1638
}
1639

    
1640
void OPPROTO op_fistll_ST0_A0(void)
1641
{
1642
#if defined(__sparc__) && !defined(__sparc_v9__)
1643
    register CPU86_LDouble d asm("o0");
1644
#else
1645
    CPU86_LDouble d;
1646
#endif
1647
    int64_t val;
1648

    
1649
    d = ST0;
1650
    val = llrint(d);
1651
    stq((void *)A0, val);
1652
}
1653

    
1654
void OPPROTO op_fbld_ST0_A0(void)
1655
{
1656
    helper_fbld_ST0_A0();
1657
}
1658

    
1659
void OPPROTO op_fbst_ST0_A0(void)
1660
{
1661
    helper_fbst_ST0_A0();
1662
}
1663

    
1664
/* FPU move */
1665

    
1666
void OPPROTO op_fpush(void)
1667
{
1668
    fpush();
1669
}
1670

    
1671
void OPPROTO op_fpop(void)
1672
{
1673
    fpop();
1674
}
1675

    
1676
void OPPROTO op_fdecstp(void)
1677
{
1678
    env->fpstt = (env->fpstt - 1) & 7;
1679
    env->fpus &= (~0x4700);
1680
}
1681

    
1682
void OPPROTO op_fincstp(void)
1683
{
1684
    env->fpstt = (env->fpstt + 1) & 7;
1685
    env->fpus &= (~0x4700);
1686
}
1687

    
1688
void OPPROTO op_fmov_ST0_FT0(void)
1689
{
1690
    ST0 = FT0;
1691
}
1692

    
1693
void OPPROTO op_fmov_FT0_STN(void)
1694
{
1695
    FT0 = ST(PARAM1);
1696
}
1697

    
1698
void OPPROTO op_fmov_ST0_STN(void)
1699
{
1700
    ST0 = ST(PARAM1);
1701
}
1702

    
1703
void OPPROTO op_fmov_STN_ST0(void)
1704
{
1705
    ST(PARAM1) = ST0;
1706
}
1707

    
1708
void OPPROTO op_fxchg_ST0_STN(void)
1709
{
1710
    CPU86_LDouble tmp;
1711
    tmp = ST(PARAM1);
1712
    ST(PARAM1) = ST0;
1713
    ST0 = tmp;
1714
}
1715

    
1716
/* FPU operations */
1717

    
1718
/* XXX: handle nans */
1719
void OPPROTO op_fcom_ST0_FT0(void)
1720
{
1721
    env->fpus &= (~0x4500);        /* (C3,C2,C0) <-- 000 */
1722
    if (ST0 < FT0)
1723
        env->fpus |= 0x100;        /* (C3,C2,C0) <-- 001 */
1724
    else if (ST0 == FT0)
1725
        env->fpus |= 0x4000; /* (C3,C2,C0) <-- 100 */
1726
    FORCE_RET();
1727
}
1728

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

    
1740
/* XXX: handle nans */
1741
void OPPROTO op_fcomi_ST0_FT0(void)
1742
{
1743
    int eflags;
1744
    eflags = cc_table[CC_OP].compute_all();
1745
    eflags &= ~(CC_Z | CC_P | CC_C);
1746
    if (ST0 < FT0)
1747
        eflags |= CC_C;
1748
    else if (ST0 == FT0)
1749
        eflags |= CC_Z;
1750
    CC_SRC = eflags;
1751
    FORCE_RET();
1752
}
1753

    
1754
/* XXX: handle nans */
1755
void OPPROTO op_fucomi_ST0_FT0(void)
1756
{
1757
    int eflags;
1758
    eflags = cc_table[CC_OP].compute_all();
1759
    eflags &= ~(CC_Z | CC_P | CC_C);
1760
    if (ST0 < FT0)
1761
        eflags |= CC_C;
1762
    else if (ST0 == FT0)
1763
        eflags |= CC_Z;
1764
    CC_SRC = eflags;
1765
    FORCE_RET();
1766
}
1767

    
1768
void OPPROTO op_fadd_ST0_FT0(void)
1769
{
1770
    ST0 += FT0;
1771
}
1772

    
1773
void OPPROTO op_fmul_ST0_FT0(void)
1774
{
1775
    ST0 *= FT0;
1776
}
1777

    
1778
void OPPROTO op_fsub_ST0_FT0(void)
1779
{
1780
    ST0 -= FT0;
1781
}
1782

    
1783
void OPPROTO op_fsubr_ST0_FT0(void)
1784
{
1785
    ST0 = FT0 - ST0;
1786
}
1787

    
1788
void OPPROTO op_fdiv_ST0_FT0(void)
1789
{
1790
    ST0 /= FT0;
1791
}
1792

    
1793
void OPPROTO op_fdivr_ST0_FT0(void)
1794
{
1795
    ST0 = FT0 / ST0;
1796
}
1797

    
1798
/* fp operations between STN and ST0 */
1799

    
1800
void OPPROTO op_fadd_STN_ST0(void)
1801
{
1802
    ST(PARAM1) += ST0;
1803
}
1804

    
1805
void OPPROTO op_fmul_STN_ST0(void)
1806
{
1807
    ST(PARAM1) *= ST0;
1808
}
1809

    
1810
void OPPROTO op_fsub_STN_ST0(void)
1811
{
1812
    ST(PARAM1) -= ST0;
1813
}
1814

    
1815
void OPPROTO op_fsubr_STN_ST0(void)
1816
{
1817
    CPU86_LDouble *p;
1818
    p = &ST(PARAM1);
1819
    *p = ST0 - *p;
1820
}
1821

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

    
1827
void OPPROTO op_fdivr_STN_ST0(void)
1828
{
1829
    CPU86_LDouble *p;
1830
    p = &ST(PARAM1);
1831
    *p = ST0 / *p;
1832
}
1833

    
1834
/* misc FPU operations */
1835
void OPPROTO op_fchs_ST0(void)
1836
{
1837
    ST0 = -ST0;
1838
}
1839

    
1840
void OPPROTO op_fabs_ST0(void)
1841
{
1842
    ST0 = fabs(ST0);
1843
}
1844

    
1845
void OPPROTO op_fxam_ST0(void)
1846
{
1847
    helper_fxam_ST0();
1848
}
1849

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

    
1855
void OPPROTO op_fldl2t_ST0(void)
1856
{
1857
    ST0 = f15rk[6];
1858
}
1859

    
1860
void OPPROTO op_fldl2e_ST0(void)
1861
{
1862
    ST0 = f15rk[5];
1863
}
1864

    
1865
void OPPROTO op_fldpi_ST0(void)
1866
{
1867
    ST0 = f15rk[2];
1868
}
1869

    
1870
void OPPROTO op_fldlg2_ST0(void)
1871
{
1872
    ST0 = f15rk[3];
1873
}
1874

    
1875
void OPPROTO op_fldln2_ST0(void)
1876
{
1877
    ST0 = f15rk[4];
1878
}
1879

    
1880
void OPPROTO op_fldz_ST0(void)
1881
{
1882
    ST0 = f15rk[0];
1883
}
1884

    
1885
void OPPROTO op_fldz_FT0(void)
1886
{
1887
    ST0 = f15rk[0];
1888
}
1889

    
1890
/* associated heplers to reduce generated code length and to simplify
1891
   relocation (FP constants are usually stored in .rodata section) */
1892

    
1893
void OPPROTO op_f2xm1(void)
1894
{
1895
    helper_f2xm1();
1896
}
1897

    
1898
void OPPROTO op_fyl2x(void)
1899
{
1900
    helper_fyl2x();
1901
}
1902

    
1903
void OPPROTO op_fptan(void)
1904
{
1905
    helper_fptan();
1906
}
1907

    
1908
void OPPROTO op_fpatan(void)
1909
{
1910
    helper_fpatan();
1911
}
1912

    
1913
void OPPROTO op_fxtract(void)
1914
{
1915
    helper_fxtract();
1916
}
1917

    
1918
void OPPROTO op_fprem1(void)
1919
{
1920
    helper_fprem1();
1921
}
1922

    
1923

    
1924
void OPPROTO op_fprem(void)
1925
{
1926
    helper_fprem();
1927
}
1928

    
1929
void OPPROTO op_fyl2xp1(void)
1930
{
1931
    helper_fyl2xp1();
1932
}
1933

    
1934
void OPPROTO op_fsqrt(void)
1935
{
1936
    helper_fsqrt();
1937
}
1938

    
1939
void OPPROTO op_fsincos(void)
1940
{
1941
    helper_fsincos();
1942
}
1943

    
1944
void OPPROTO op_frndint(void)
1945
{
1946
    helper_frndint();
1947
}
1948

    
1949
void OPPROTO op_fscale(void)
1950
{
1951
    helper_fscale();
1952
}
1953

    
1954
void OPPROTO op_fsin(void)
1955
{
1956
    helper_fsin();
1957
}
1958

    
1959
void OPPROTO op_fcos(void)
1960
{
1961
    helper_fcos();
1962
}
1963

    
1964
void OPPROTO op_fnstsw_A0(void)
1965
{
1966
    int fpus;
1967
    fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1968
    stw((void *)A0, fpus);
1969
}
1970

    
1971
void OPPROTO op_fnstsw_EAX(void)
1972
{
1973
    int fpus;
1974
    fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1975
    EAX = (EAX & 0xffff0000) | fpus;
1976
}
1977

    
1978
void OPPROTO op_fnstcw_A0(void)
1979
{
1980
    stw((void *)A0, env->fpuc);
1981
}
1982

    
1983
void OPPROTO op_fldcw_A0(void)
1984
{
1985
    int rnd_type;
1986
    env->fpuc = lduw((void *)A0);
1987
    /* set rounding mode */
1988
    switch(env->fpuc & RC_MASK) {
1989
    default:
1990
    case RC_NEAR:
1991
        rnd_type = FE_TONEAREST;
1992
        break;
1993
    case RC_DOWN:
1994
        rnd_type = FE_DOWNWARD;
1995
        break;
1996
    case RC_UP:
1997
        rnd_type = FE_UPWARD;
1998
        break;
1999
    case RC_CHOP:
2000
        rnd_type = FE_TOWARDZERO;
2001
        break;
2002
    }
2003
    fesetround(rnd_type);
2004
}
2005

    
2006
void OPPROTO op_fclex(void)
2007
{
2008
    env->fpus &= 0x7f00;
2009
}
2010

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

    
2026
void OPPROTO op_fnstenv_A0(void)
2027
{
2028
    helper_fstenv((uint8_t *)A0, PARAM1);
2029
}
2030

    
2031
void OPPROTO op_fldenv_A0(void)
2032
{
2033
    helper_fldenv((uint8_t *)A0, PARAM1);
2034
}
2035

    
2036
void OPPROTO op_fnsave_A0(void)
2037
{
2038
    helper_fsave((uint8_t *)A0, PARAM1);
2039
}
2040

    
2041
void OPPROTO op_frstor_A0(void)
2042
{
2043
    helper_frstor((uint8_t *)A0, PARAM1);
2044
}
2045

    
2046
/* threading support */
2047
void OPPROTO op_lock(void)
2048
{
2049
    cpu_lock();
2050
}
2051

    
2052
void OPPROTO op_unlock(void)
2053
{
2054
    cpu_unlock();
2055
}
2056