Statistics
| Branch: | Revision:

root / op-i386.c @ a412ac57

History | View | Annotate | Download (36.2 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-i386.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
void OPPROTO op_ldub_T0_A0(void)
380
{
381
    T0 = ldub((uint8_t *)A0);
382
}
383

    
384
void OPPROTO op_ldsb_T0_A0(void)
385
{
386
    T0 = ldsb((int8_t *)A0);
387
}
388

    
389
void OPPROTO op_lduw_T0_A0(void)
390
{
391
    T0 = lduw((uint8_t *)A0);
392
}
393

    
394
void OPPROTO op_ldsw_T0_A0(void)
395
{
396
    T0 = ldsw((int8_t *)A0);
397
}
398

    
399
void OPPROTO op_ldl_T0_A0(void)
400
{
401
    T0 = ldl((uint8_t *)A0);
402
}
403

    
404
void OPPROTO op_ldub_T1_A0(void)
405
{
406
    T1 = ldub((uint8_t *)A0);
407
}
408

    
409
void OPPROTO op_ldsb_T1_A0(void)
410
{
411
    T1 = ldsb((int8_t *)A0);
412
}
413

    
414
void OPPROTO op_lduw_T1_A0(void)
415
{
416
    T1 = lduw((uint8_t *)A0);
417
}
418

    
419
void OPPROTO op_ldsw_T1_A0(void)
420
{
421
    T1 = ldsw((int8_t *)A0);
422
}
423

    
424
void OPPROTO op_ldl_T1_A0(void)
425
{
426
    T1 = ldl((uint8_t *)A0);
427
}
428

    
429
void OPPROTO op_stb_T0_A0(void)
430
{
431
    stb((uint8_t *)A0, T0);
432
}
433

    
434
void OPPROTO op_stw_T0_A0(void)
435
{
436
    stw((uint8_t *)A0, T0);
437
}
438

    
439
void OPPROTO op_stl_T0_A0(void)
440
{
441
    stl((uint8_t *)A0, T0);
442
}
443

    
444
/* used for bit operations */
445

    
446
void OPPROTO op_add_bitw_A0_T1(void)
447
{
448
    A0 += ((int32_t)T1 >> 4) << 1;
449
}
450

    
451
void OPPROTO op_add_bitl_A0_T1(void)
452
{
453
    A0 += ((int32_t)T1 >> 5) << 2;
454
}
455

    
456
/* indirect jump */
457

    
458
void OPPROTO op_jmp_T0(void)
459
{
460
    EIP = T0;
461
}
462

    
463
void OPPROTO op_jmp_im(void)
464
{
465
    EIP = PARAM1;
466
}
467

    
468
void OPPROTO op_hlt(void)
469
{
470
    env->exception_index = EXCP_HLT;
471
    cpu_loop_exit();
472
}
473

    
474
void OPPROTO op_debug(void)
475
{
476
    env->exception_index = EXCP_DEBUG;
477
    cpu_loop_exit();
478
}
479

    
480
void OPPROTO op_raise_interrupt(void)
481
{
482
    int intno;
483
    unsigned int next_eip;
484
    intno = PARAM1;
485
    next_eip = PARAM2;
486
    raise_interrupt(intno, 1, 0, next_eip);
487
}
488

    
489
void OPPROTO op_raise_exception(void)
490
{
491
    int exception_index;
492
    exception_index = PARAM1;
493
    raise_exception(exception_index);
494
}
495

    
496
void OPPROTO op_into(void)
497
{
498
    int eflags;
499
    eflags = cc_table[CC_OP].compute_all();
500
    if (eflags & CC_O) {
501
        raise_interrupt(EXCP04_INTO, 1, 0, PARAM1);
502
    }
503
    FORCE_RET();
504
}
505

    
506
void OPPROTO op_cli(void)
507
{
508
    env->eflags &= ~IF_MASK;
509
}
510

    
511
void OPPROTO op_sti(void)
512
{
513
    env->eflags |= IF_MASK;
514
}
515

    
516
#if 0
517
/* vm86plus instructions */
518
void OPPROTO op_cli_vm(void)
519
{
520
    env->eflags &= ~VIF_MASK;
521
}
522

523
void OPPROTO op_sti_vm(void)
524
{
525
    env->eflags |= VIF_MASK;
526
    if (env->eflags & VIP_MASK) {
527
        EIP = PARAM1;
528
        raise_exception(EXCP0D_GPF);
529
    }
530
    FORCE_RET();
531
}
532
#endif
533

    
534
void OPPROTO op_boundw(void)
535
{
536
    int low, high, v;
537
    low = ldsw((uint8_t *)A0);
538
    high = ldsw((uint8_t *)A0 + 2);
539
    v = (int16_t)T0;
540
    if (v < low || v > high) {
541
        EIP = PARAM1;
542
        raise_exception(EXCP05_BOUND);
543
    }
544
    FORCE_RET();
545
}
546

    
547
void OPPROTO op_boundl(void)
548
{
549
    int low, high, v;
550
    low = ldl((uint8_t *)A0);
551
    high = ldl((uint8_t *)A0 + 4);
552
    v = T0;
553
    if (v < low || v > high) {
554
        EIP = PARAM1;
555
        raise_exception(EXCP05_BOUND);
556
    }
557
    FORCE_RET();
558
}
559

    
560
void OPPROTO op_cmpxchg8b(void)
561
{
562
    helper_cmpxchg8b();
563
}
564

    
565
void OPPROTO op_jmp_tb_next(void)
566
{
567
    JUMP_TB(PARAM1, 0, PARAM2);
568
}
569

    
570
void OPPROTO op_movl_T0_0(void)
571
{
572
    T0 = 0;
573
}
574

    
575
void OPPROTO op_exit_tb(void)
576
{
577
    EXIT_TB();
578
}
579

    
580
/* multiple size ops */
581

    
582
#define ldul ldl
583

    
584
#define SHIFT 0
585
#include "ops_template.h"
586
#undef SHIFT
587

    
588
#define SHIFT 1
589
#include "ops_template.h"
590
#undef SHIFT
591

    
592
#define SHIFT 2
593
#include "ops_template.h"
594
#undef SHIFT
595

    
596
/* sign extend */
597

    
598
void OPPROTO op_movsbl_T0_T0(void)
599
{
600
    T0 = (int8_t)T0;
601
}
602

    
603
void OPPROTO op_movzbl_T0_T0(void)
604
{
605
    T0 = (uint8_t)T0;
606
}
607

    
608
void OPPROTO op_movswl_T0_T0(void)
609
{
610
    T0 = (int16_t)T0;
611
}
612

    
613
void OPPROTO op_movzwl_T0_T0(void)
614
{
615
    T0 = (uint16_t)T0;
616
}
617

    
618
void OPPROTO op_movswl_EAX_AX(void)
619
{
620
    EAX = (int16_t)EAX;
621
}
622

    
623
void OPPROTO op_movsbw_AX_AL(void)
624
{
625
    EAX = (EAX & 0xffff0000) | ((int8_t)EAX & 0xffff);
626
}
627

    
628
void OPPROTO op_movslq_EDX_EAX(void)
629
{
630
    EDX = (int32_t)EAX >> 31;
631
}
632

    
633
void OPPROTO op_movswl_DX_AX(void)
634
{
635
    EDX = (EDX & 0xffff0000) | (((int16_t)EAX >> 15) & 0xffff);
636
}
637

    
638
/* push/pop */
639

    
640
void op_pushl_T0(void)
641
{
642
    uint32_t offset;
643
    offset = ESP - 4;
644
    stl((void *)offset, T0);
645
    /* modify ESP after to handle exceptions correctly */
646
    ESP = offset;
647
}
648

    
649
void op_pushw_T0(void)
650
{
651
    uint32_t offset;
652
    offset = ESP - 2;
653
    stw((void *)offset, T0);
654
    /* modify ESP after to handle exceptions correctly */
655
    ESP = offset;
656
}
657

    
658
void op_pushl_ss32_T0(void)
659
{
660
    uint32_t offset;
661
    offset = ESP - 4;
662
    stl(env->segs[R_SS].base + offset, T0);
663
    /* modify ESP after to handle exceptions correctly */
664
    ESP = offset;
665
}
666

    
667
void op_pushw_ss32_T0(void)
668
{
669
    uint32_t offset;
670
    offset = ESP - 2;
671
    stw(env->segs[R_SS].base + offset, T0);
672
    /* modify ESP after to handle exceptions correctly */
673
    ESP = offset;
674
}
675

    
676
void op_pushl_ss16_T0(void)
677
{
678
    uint32_t offset;
679
    offset = (ESP - 4) & 0xffff;
680
    stl(env->segs[R_SS].base + offset, T0);
681
    /* modify ESP after to handle exceptions correctly */
682
    ESP = (ESP & ~0xffff) | offset;
683
}
684

    
685
void op_pushw_ss16_T0(void)
686
{
687
    uint32_t offset;
688
    offset = (ESP - 2) & 0xffff;
689
    stw(env->segs[R_SS].base + offset, T0);
690
    /* modify ESP after to handle exceptions correctly */
691
    ESP = (ESP & ~0xffff) | offset;
692
}
693

    
694
/* NOTE: ESP update is done after */
695
void op_popl_T0(void)
696
{
697
    T0 = ldl((void *)ESP);
698
}
699

    
700
void op_popw_T0(void)
701
{
702
    T0 = lduw((void *)ESP);
703
}
704

    
705
void op_popl_ss32_T0(void)
706
{
707
    T0 = ldl(env->segs[R_SS].base + ESP);
708
}
709

    
710
void op_popw_ss32_T0(void)
711
{
712
    T0 = lduw(env->segs[R_SS].base + ESP);
713
}
714

    
715
void op_popl_ss16_T0(void)
716
{
717
    T0 = ldl(env->segs[R_SS].base + (ESP & 0xffff));
718
}
719

    
720
void op_popw_ss16_T0(void)
721
{
722
    T0 = lduw(env->segs[R_SS].base + (ESP & 0xffff));
723
}
724

    
725
void op_addl_ESP_4(void)
726
{
727
    ESP += 4;
728
}
729

    
730
void op_addl_ESP_2(void)
731
{
732
    ESP += 2;
733
}
734

    
735
void op_addw_ESP_4(void)
736
{
737
    ESP = (ESP & ~0xffff) | ((ESP + 4) & 0xffff);
738
}
739

    
740
void op_addw_ESP_2(void)
741
{
742
    ESP = (ESP & ~0xffff) | ((ESP + 2) & 0xffff);
743
}
744

    
745
void op_addl_ESP_im(void)
746
{
747
    ESP += PARAM1;
748
}
749

    
750
void op_addw_ESP_im(void)
751
{
752
    ESP = (ESP & ~0xffff) | ((ESP + PARAM1) & 0xffff);
753
}
754

    
755
void OPPROTO op_rdtsc(void)
756
{
757
    helper_rdtsc();
758
}
759

    
760
void OPPROTO op_cpuid(void)
761
{
762
    helper_cpuid();
763
}
764

    
765
void OPPROTO op_rdmsr(void)
766
{
767
    helper_rdmsr();
768
}
769

    
770
void OPPROTO op_wrmsr(void)
771
{
772
    helper_wrmsr();
773
}
774

    
775
/* bcd */
776

    
777
/* XXX: exception */
778
void OPPROTO op_aam(void)
779
{
780
    int base = PARAM1;
781
    int al, ah;
782
    al = EAX & 0xff;
783
    ah = al / base;
784
    al = al % base;
785
    EAX = (EAX & ~0xffff) | al | (ah << 8);
786
    CC_DST = al;
787
}
788

    
789
void OPPROTO op_aad(void)
790
{
791
    int base = PARAM1;
792
    int al, ah;
793
    al = EAX & 0xff;
794
    ah = (EAX >> 8) & 0xff;
795
    al = ((ah * base) + al) & 0xff;
796
    EAX = (EAX & ~0xffff) | al;
797
    CC_DST = al;
798
}
799

    
800
void OPPROTO op_aaa(void)
801
{
802
    int icarry;
803
    int al, ah, af;
804
    int eflags;
805

    
806
    eflags = cc_table[CC_OP].compute_all();
807
    af = eflags & CC_A;
808
    al = EAX & 0xff;
809
    ah = (EAX >> 8) & 0xff;
810

    
811
    icarry = (al > 0xf9);
812
    if (((al & 0x0f) > 9 ) || af) {
813
        al = (al + 6) & 0x0f;
814
        ah = (ah + 1 + icarry) & 0xff;
815
        eflags |= CC_C | CC_A;
816
    } else {
817
        eflags &= ~(CC_C | CC_A);
818
        al &= 0x0f;
819
    }
820
    EAX = (EAX & ~0xffff) | al | (ah << 8);
821
    CC_SRC = eflags;
822
}
823

    
824
void OPPROTO op_aas(void)
825
{
826
    int icarry;
827
    int al, ah, af;
828
    int eflags;
829

    
830
    eflags = cc_table[CC_OP].compute_all();
831
    af = eflags & CC_A;
832
    al = EAX & 0xff;
833
    ah = (EAX >> 8) & 0xff;
834

    
835
    icarry = (al < 6);
836
    if (((al & 0x0f) > 9 ) || af) {
837
        al = (al - 6) & 0x0f;
838
        ah = (ah - 1 - icarry) & 0xff;
839
        eflags |= CC_C | CC_A;
840
    } else {
841
        eflags &= ~(CC_C | CC_A);
842
        al &= 0x0f;
843
    }
844
    EAX = (EAX & ~0xffff) | al | (ah << 8);
845
    CC_SRC = eflags;
846
}
847

    
848
void OPPROTO op_daa(void)
849
{
850
    int al, af, cf;
851
    int eflags;
852

    
853
    eflags = cc_table[CC_OP].compute_all();
854
    cf = eflags & CC_C;
855
    af = eflags & CC_A;
856
    al = EAX & 0xff;
857

    
858
    eflags = 0;
859
    if (((al & 0x0f) > 9 ) || af) {
860
        al = (al + 6) & 0xff;
861
        eflags |= CC_A;
862
    }
863
    if ((al > 0x9f) || cf) {
864
        al = (al + 0x60) & 0xff;
865
        eflags |= CC_C;
866
    }
867
    EAX = (EAX & ~0xff) | al;
868
    /* well, speed is not an issue here, so we compute the flags by hand */
869
    eflags |= (al == 0) << 6; /* zf */
870
    eflags |= parity_table[al]; /* pf */
871
    eflags |= (al & 0x80); /* sf */
872
    CC_SRC = eflags;
873
}
874

    
875
void OPPROTO op_das(void)
876
{
877
    int al, al1, af, cf;
878
    int eflags;
879

    
880
    eflags = cc_table[CC_OP].compute_all();
881
    cf = eflags & CC_C;
882
    af = eflags & CC_A;
883
    al = EAX & 0xff;
884

    
885
    eflags = 0;
886
    al1 = al;
887
    if (((al & 0x0f) > 9 ) || af) {
888
        eflags |= CC_A;
889
        if (al < 6 || cf)
890
            eflags |= CC_C;
891
        al = (al - 6) & 0xff;
892
    }
893
    if ((al1 > 0x99) || cf) {
894
        al = (al - 0x60) & 0xff;
895
        eflags |= CC_C;
896
    }
897
    EAX = (EAX & ~0xff) | al;
898
    /* well, speed is not an issue here, so we compute the flags by hand */
899
    eflags |= (al == 0) << 6; /* zf */
900
    eflags |= parity_table[al]; /* pf */
901
    eflags |= (al & 0x80); /* sf */
902
    CC_SRC = eflags;
903
}
904

    
905
/* segment handling */
906

    
907
void OPPROTO op_movl_seg_T0(void)
908
{
909
    load_seg(PARAM1, T0 & 0xffff, PARAM2);
910
}
911

    
912
/* faster VM86 version */
913
void OPPROTO op_movl_seg_T0_vm(void)
914
{
915
    int selector;
916
    SegmentCache *sc;
917
    
918
    selector = T0 & 0xffff;
919
    /* env->segs[] access */
920
    sc = (SegmentCache *)((char *)env + PARAM1);
921
    sc->selector = selector;
922
    sc->base = (void *)(selector << 4);
923
}
924

    
925
void OPPROTO op_movl_T0_seg(void)
926
{
927
    T0 = env->segs[PARAM1].selector;
928
}
929

    
930
void OPPROTO op_movl_A0_seg(void)
931
{
932
    A0 = *(unsigned long *)((char *)env + PARAM1);
933
}
934

    
935
void OPPROTO op_addl_A0_seg(void)
936
{
937
    A0 += *(unsigned long *)((char *)env + PARAM1);
938
}
939

    
940
void OPPROTO op_lsl(void)
941
{
942
    helper_lsl();
943
}
944

    
945
void OPPROTO op_lar(void)
946
{
947
    helper_lar();
948
}
949

    
950
/* T0: segment, T1:eip */
951
void OPPROTO op_ljmp_T0_T1(void)
952
{
953
    jmp_seg(T0 & 0xffff, T1);
954
}
955

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

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

    
966
void OPPROTO op_lldt_T0(void)
967
{
968
    helper_lldt_T0();
969
}
970

    
971
void OPPROTO op_ltr_T0(void)
972
{
973
    helper_ltr_T0();
974
}
975

    
976
/* CR registers access */
977
void OPPROTO op_movl_crN_T0(void)
978
{
979
    helper_movl_crN_T0(PARAM1);
980
}
981

    
982
/* DR registers access */
983
void OPPROTO op_movl_drN_T0(void)
984
{
985
    helper_movl_drN_T0(PARAM1);
986
}
987

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

    
995
void OPPROTO op_invlpg_A0(void)
996
{
997
    helper_invlpg(A0);
998
}
999

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

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

    
1010
void OPPROTO op_movl_env_T1(void)
1011
{
1012
    *(uint32_t *)((char *)env + PARAM1) = T1;
1013
}
1014

    
1015
void OPPROTO op_clts(void)
1016
{
1017
    env->cr[0] &= ~CR0_TS_MASK;
1018
}
1019

    
1020
/* flags handling */
1021

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

    
1034
/* slow set cases (compute x86 flags) */
1035
void OPPROTO op_seto_T0_cc(void)
1036
{
1037
    int eflags;
1038
    eflags = cc_table[CC_OP].compute_all();
1039
    T0 = (eflags >> 11) & 1;
1040
}
1041

    
1042
void OPPROTO op_setb_T0_cc(void)
1043
{
1044
    T0 = cc_table[CC_OP].compute_c();
1045
}
1046

    
1047
void OPPROTO op_setz_T0_cc(void)
1048
{
1049
    int eflags;
1050
    eflags = cc_table[CC_OP].compute_all();
1051
    T0 = (eflags >> 6) & 1;
1052
}
1053

    
1054
void OPPROTO op_setbe_T0_cc(void)
1055
{
1056
    int eflags;
1057
    eflags = cc_table[CC_OP].compute_all();
1058
    T0 = (eflags & (CC_Z | CC_C)) != 0;
1059
}
1060

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

    
1068
void OPPROTO op_setp_T0_cc(void)
1069
{
1070
    int eflags;
1071
    eflags = cc_table[CC_OP].compute_all();
1072
    T0 = (eflags >> 2) & 1;
1073
}
1074

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

    
1082
void OPPROTO op_setle_T0_cc(void)
1083
{
1084
    int eflags;
1085
    eflags = cc_table[CC_OP].compute_all();
1086
    T0 = (((eflags ^ (eflags >> 4)) & 0x80) || (eflags & CC_Z)) != 0;
1087
}
1088

    
1089
void OPPROTO op_xor_T0_1(void)
1090
{
1091
    T0 ^= 1;
1092
}
1093

    
1094
void OPPROTO op_set_cc_op(void)
1095
{
1096
    CC_OP = PARAM1;
1097
}
1098

    
1099
#define FL_UPDATE_MASK16 (FL_UPDATE_MASK32 & 0xffff)
1100

    
1101
void OPPROTO op_movl_eflags_T0(void)
1102
{
1103
    int eflags;
1104
    eflags = T0;
1105
    CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1106
    DF = 1 - (2 * ((eflags >> 10) & 1));
1107
    /* we also update some system flags as in user mode */
1108
    env->eflags = (env->eflags & ~FL_UPDATE_MASK32) | 
1109
        (eflags & FL_UPDATE_MASK32);
1110
}
1111

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

    
1123
void OPPROTO op_movl_eflags_T0_cpl0(void)
1124
{
1125
    load_eflags(T0, FL_UPDATE_CPL0_MASK);
1126
}
1127

    
1128
void OPPROTO op_movw_eflags_T0_cpl0(void)
1129
{
1130
    load_eflags(T0, FL_UPDATE_CPL0_MASK & 0xffff);
1131
}
1132

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

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

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

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

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

    
1205
void OPPROTO op_cld(void)
1206
{
1207
    DF = 1;
1208
}
1209

    
1210
void OPPROTO op_std(void)
1211
{
1212
    DF = -1;
1213
}
1214

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

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

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

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

    
1246
static int compute_all_eflags(void)
1247
{
1248
    return CC_SRC;
1249
}
1250

    
1251
static int compute_c_eflags(void)
1252
{
1253
    return CC_SRC & CC_C;
1254
}
1255

    
1256
static int compute_c_mul(void)
1257
{
1258
    int cf;
1259
    cf = (CC_SRC != 0);
1260
    return cf;
1261
}
1262

    
1263
static int compute_all_mul(void)
1264
{
1265
    int cf, pf, af, zf, sf, of;
1266
    cf = (CC_SRC != 0);
1267
    pf = 0; /* undefined */
1268
    af = 0; /* undefined */
1269
    zf = 0; /* undefined */
1270
    sf = 0; /* undefined */
1271
    of = cf << 11;
1272
    return cf | pf | af | zf | sf | of;
1273
}
1274
    
1275
CCTable cc_table[CC_OP_NB] = {
1276
    [CC_OP_DYNAMIC] = { /* should never happen */ },
1277

    
1278
    [CC_OP_EFLAGS] = { compute_all_eflags, compute_c_eflags },
1279

    
1280
    [CC_OP_MUL] = { compute_all_mul, compute_c_mul },
1281

    
1282
    [CC_OP_ADDB] = { compute_all_addb, compute_c_addb },
1283
    [CC_OP_ADDW] = { compute_all_addw, compute_c_addw  },
1284
    [CC_OP_ADDL] = { compute_all_addl, compute_c_addl  },
1285

    
1286
    [CC_OP_ADCB] = { compute_all_adcb, compute_c_adcb },
1287
    [CC_OP_ADCW] = { compute_all_adcw, compute_c_adcw  },
1288
    [CC_OP_ADCL] = { compute_all_adcl, compute_c_adcl  },
1289

    
1290
    [CC_OP_SUBB] = { compute_all_subb, compute_c_subb  },
1291
    [CC_OP_SUBW] = { compute_all_subw, compute_c_subw  },
1292
    [CC_OP_SUBL] = { compute_all_subl, compute_c_subl  },
1293
    
1294
    [CC_OP_SBBB] = { compute_all_sbbb, compute_c_sbbb  },
1295
    [CC_OP_SBBW] = { compute_all_sbbw, compute_c_sbbw  },
1296
    [CC_OP_SBBL] = { compute_all_sbbl, compute_c_sbbl  },
1297
    
1298
    [CC_OP_LOGICB] = { compute_all_logicb, compute_c_logicb },
1299
    [CC_OP_LOGICW] = { compute_all_logicw, compute_c_logicw },
1300
    [CC_OP_LOGICL] = { compute_all_logicl, compute_c_logicl },
1301
    
1302
    [CC_OP_INCB] = { compute_all_incb, compute_c_incl },
1303
    [CC_OP_INCW] = { compute_all_incw, compute_c_incl },
1304
    [CC_OP_INCL] = { compute_all_incl, compute_c_incl },
1305
    
1306
    [CC_OP_DECB] = { compute_all_decb, compute_c_incl },
1307
    [CC_OP_DECW] = { compute_all_decw, compute_c_incl },
1308
    [CC_OP_DECL] = { compute_all_decl, compute_c_incl },
1309
    
1310
    [CC_OP_SHLB] = { compute_all_shlb, compute_c_shlb },
1311
    [CC_OP_SHLW] = { compute_all_shlw, compute_c_shlw },
1312
    [CC_OP_SHLL] = { compute_all_shll, compute_c_shll },
1313

    
1314
    [CC_OP_SARB] = { compute_all_sarb, compute_c_sarl },
1315
    [CC_OP_SARW] = { compute_all_sarw, compute_c_sarl },
1316
    [CC_OP_SARL] = { compute_all_sarl, compute_c_sarl },
1317
};
1318

    
1319
/* floating point support. Some of the code for complicated x87
1320
   functions comes from the LGPL'ed x86 emulator found in the Willows
1321
   TWIN windows emulator. */
1322

    
1323
#if defined(__powerpc__)
1324
extern CPU86_LDouble copysign(CPU86_LDouble, CPU86_LDouble);
1325

    
1326
/* correct (but slow) PowerPC rint() (glibc version is incorrect) */
1327
double qemu_rint(double x)
1328
{
1329
    double y = 4503599627370496.0;
1330
    if (fabs(x) >= y)
1331
        return x;
1332
    if (x < 0) 
1333
        y = -y;
1334
    y = (x + y) - y;
1335
    if (y == 0.0)
1336
        y = copysign(y, x);
1337
    return y;
1338
}
1339

    
1340
#define rint qemu_rint
1341
#endif
1342

    
1343
/* fp load FT0 */
1344

    
1345
void OPPROTO op_flds_FT0_A0(void)
1346
{
1347
#ifdef USE_FP_CONVERT
1348
    FP_CONVERT.i32 = ldl((void *)A0);
1349
    FT0 = FP_CONVERT.f;
1350
#else
1351
    FT0 = ldfl((void *)A0);
1352
#endif
1353
}
1354

    
1355
void OPPROTO op_fldl_FT0_A0(void)
1356
{
1357
#ifdef USE_FP_CONVERT
1358
    FP_CONVERT.i64 = ldq((void *)A0);
1359
    FT0 = FP_CONVERT.d;
1360
#else
1361
    FT0 = ldfq((void *)A0);
1362
#endif
1363
}
1364

    
1365
/* helpers are needed to avoid static constant reference. XXX: find a better way */
1366
#ifdef USE_INT_TO_FLOAT_HELPERS
1367

    
1368
void helper_fild_FT0_A0(void)
1369
{
1370
    FT0 = (CPU86_LDouble)ldsw((void *)A0);
1371
}
1372

    
1373
void helper_fildl_FT0_A0(void)
1374
{
1375
    FT0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1376
}
1377

    
1378
void helper_fildll_FT0_A0(void)
1379
{
1380
    FT0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1381
}
1382

    
1383
void OPPROTO op_fild_FT0_A0(void)
1384
{
1385
    helper_fild_FT0_A0();
1386
}
1387

    
1388
void OPPROTO op_fildl_FT0_A0(void)
1389
{
1390
    helper_fildl_FT0_A0();
1391
}
1392

    
1393
void OPPROTO op_fildll_FT0_A0(void)
1394
{
1395
    helper_fildll_FT0_A0();
1396
}
1397

    
1398
#else
1399

    
1400
void OPPROTO op_fild_FT0_A0(void)
1401
{
1402
#ifdef USE_FP_CONVERT
1403
    FP_CONVERT.i32 = ldsw((void *)A0);
1404
    FT0 = (CPU86_LDouble)FP_CONVERT.i32;
1405
#else
1406
    FT0 = (CPU86_LDouble)ldsw((void *)A0);
1407
#endif
1408
}
1409

    
1410
void OPPROTO op_fildl_FT0_A0(void)
1411
{
1412
#ifdef USE_FP_CONVERT
1413
    FP_CONVERT.i32 = (int32_t) ldl((void *)A0);
1414
    FT0 = (CPU86_LDouble)FP_CONVERT.i32;
1415
#else
1416
    FT0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1417
#endif
1418
}
1419

    
1420
void OPPROTO op_fildll_FT0_A0(void)
1421
{
1422
#ifdef USE_FP_CONVERT
1423
    FP_CONVERT.i64 = (int64_t) ldq((void *)A0);
1424
    FT0 = (CPU86_LDouble)FP_CONVERT.i64;
1425
#else
1426
    FT0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1427
#endif
1428
}
1429
#endif
1430

    
1431
/* fp load ST0 */
1432

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

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

    
1461
#ifdef USE_X86LDOUBLE
1462
void OPPROTO op_fldt_ST0_A0(void)
1463
{
1464
    int new_fpstt;
1465
    new_fpstt = (env->fpstt - 1) & 7;
1466
    env->fpregs[new_fpstt] = *(long double *)A0;
1467
    env->fpstt = new_fpstt;
1468
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1469
}
1470
#else
1471
void OPPROTO op_fldt_ST0_A0(void)
1472
{
1473
    helper_fldt_ST0_A0();
1474
}
1475
#endif
1476

    
1477
/* helpers are needed to avoid static constant reference. XXX: find a better way */
1478
#ifdef USE_INT_TO_FLOAT_HELPERS
1479

    
1480
void helper_fild_ST0_A0(void)
1481
{
1482
    int new_fpstt;
1483
    new_fpstt = (env->fpstt - 1) & 7;
1484
    env->fpregs[new_fpstt] = (CPU86_LDouble)ldsw((void *)A0);
1485
    env->fpstt = new_fpstt;
1486
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1487
}
1488

    
1489
void helper_fildl_ST0_A0(void)
1490
{
1491
    int new_fpstt;
1492
    new_fpstt = (env->fpstt - 1) & 7;
1493
    env->fpregs[new_fpstt] = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1494
    env->fpstt = new_fpstt;
1495
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1496
}
1497

    
1498
void helper_fildll_ST0_A0(void)
1499
{
1500
    int new_fpstt;
1501
    new_fpstt = (env->fpstt - 1) & 7;
1502
    env->fpregs[new_fpstt] = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1503
    env->fpstt = new_fpstt;
1504
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1505
}
1506

    
1507
void OPPROTO op_fild_ST0_A0(void)
1508
{
1509
    helper_fild_ST0_A0();
1510
}
1511

    
1512
void OPPROTO op_fildl_ST0_A0(void)
1513
{
1514
    helper_fildl_ST0_A0();
1515
}
1516

    
1517
void OPPROTO op_fildll_ST0_A0(void)
1518
{
1519
    helper_fildll_ST0_A0();
1520
}
1521

    
1522
#else
1523

    
1524
void OPPROTO op_fild_ST0_A0(void)
1525
{
1526
    int new_fpstt;
1527
    new_fpstt = (env->fpstt - 1) & 7;
1528
#ifdef USE_FP_CONVERT
1529
    FP_CONVERT.i32 = ldsw((void *)A0);
1530
    env->fpregs[new_fpstt] = (CPU86_LDouble)FP_CONVERT.i32;
1531
#else
1532
    env->fpregs[new_fpstt] = (CPU86_LDouble)ldsw((void *)A0);
1533
#endif
1534
    env->fpstt = new_fpstt;
1535
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1536
}
1537

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

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

    
1566
#endif
1567

    
1568
/* fp store */
1569

    
1570
void OPPROTO op_fsts_ST0_A0(void)
1571
{
1572
#ifdef USE_FP_CONVERT
1573
    FP_CONVERT.f = (float)ST0;
1574
    stfl((void *)A0, FP_CONVERT.f);
1575
#else
1576
    stfl((void *)A0, (float)ST0);
1577
#endif
1578
}
1579

    
1580
void OPPROTO op_fstl_ST0_A0(void)
1581
{
1582
    stfq((void *)A0, (double)ST0);
1583
}
1584

    
1585
#ifdef USE_X86LDOUBLE
1586
void OPPROTO op_fstt_ST0_A0(void)
1587
{
1588
    *(long double *)A0 = ST0;
1589
}
1590
#else
1591
void OPPROTO op_fstt_ST0_A0(void)
1592
{
1593
    helper_fstt_ST0_A0();
1594
}
1595
#endif
1596

    
1597
void OPPROTO op_fist_ST0_A0(void)
1598
{
1599
#if defined(__sparc__) && !defined(__sparc_v9__)
1600
    register CPU86_LDouble d asm("o0");
1601
#else
1602
    CPU86_LDouble d;
1603
#endif
1604
    int val;
1605

    
1606
    d = ST0;
1607
    val = lrint(d);
1608
    if (val != (int16_t)val)
1609
        val = -32768;
1610
    stw((void *)A0, val);
1611
}
1612

    
1613
void OPPROTO op_fistl_ST0_A0(void)
1614
{
1615
#if defined(__sparc__) && !defined(__sparc_v9__)
1616
    register CPU86_LDouble d asm("o0");
1617
#else
1618
    CPU86_LDouble d;
1619
#endif
1620
    int val;
1621

    
1622
    d = ST0;
1623
    val = lrint(d);
1624
    stl((void *)A0, val);
1625
}
1626

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

    
1636
    d = ST0;
1637
    val = llrint(d);
1638
    stq((void *)A0, val);
1639
}
1640

    
1641
void OPPROTO op_fbld_ST0_A0(void)
1642
{
1643
    helper_fbld_ST0_A0();
1644
}
1645

    
1646
void OPPROTO op_fbst_ST0_A0(void)
1647
{
1648
    helper_fbst_ST0_A0();
1649
}
1650

    
1651
/* FPU move */
1652

    
1653
void OPPROTO op_fpush(void)
1654
{
1655
    fpush();
1656
}
1657

    
1658
void OPPROTO op_fpop(void)
1659
{
1660
    fpop();
1661
}
1662

    
1663
void OPPROTO op_fdecstp(void)
1664
{
1665
    env->fpstt = (env->fpstt - 1) & 7;
1666
    env->fpus &= (~0x4700);
1667
}
1668

    
1669
void OPPROTO op_fincstp(void)
1670
{
1671
    env->fpstt = (env->fpstt + 1) & 7;
1672
    env->fpus &= (~0x4700);
1673
}
1674

    
1675
void OPPROTO op_fmov_ST0_FT0(void)
1676
{
1677
    ST0 = FT0;
1678
}
1679

    
1680
void OPPROTO op_fmov_FT0_STN(void)
1681
{
1682
    FT0 = ST(PARAM1);
1683
}
1684

    
1685
void OPPROTO op_fmov_ST0_STN(void)
1686
{
1687
    ST0 = ST(PARAM1);
1688
}
1689

    
1690
void OPPROTO op_fmov_STN_ST0(void)
1691
{
1692
    ST(PARAM1) = ST0;
1693
}
1694

    
1695
void OPPROTO op_fxchg_ST0_STN(void)
1696
{
1697
    CPU86_LDouble tmp;
1698
    tmp = ST(PARAM1);
1699
    ST(PARAM1) = ST0;
1700
    ST0 = tmp;
1701
}
1702

    
1703
/* FPU operations */
1704

    
1705
/* XXX: handle nans */
1706
void OPPROTO op_fcom_ST0_FT0(void)
1707
{
1708
    env->fpus &= (~0x4500);        /* (C3,C2,C0) <-- 000 */
1709
    if (ST0 < FT0)
1710
        env->fpus |= 0x100;        /* (C3,C2,C0) <-- 001 */
1711
    else if (ST0 == FT0)
1712
        env->fpus |= 0x4000; /* (C3,C2,C0) <-- 100 */
1713
    FORCE_RET();
1714
}
1715

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

    
1727
/* XXX: handle nans */
1728
void OPPROTO op_fcomi_ST0_FT0(void)
1729
{
1730
    int eflags;
1731
    eflags = cc_table[CC_OP].compute_all();
1732
    eflags &= ~(CC_Z | CC_P | CC_C);
1733
    if (ST0 < FT0)
1734
        eflags |= CC_C;
1735
    else if (ST0 == FT0)
1736
        eflags |= CC_Z;
1737
    CC_SRC = eflags;
1738
    FORCE_RET();
1739
}
1740

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

    
1755
void OPPROTO op_fadd_ST0_FT0(void)
1756
{
1757
    ST0 += FT0;
1758
}
1759

    
1760
void OPPROTO op_fmul_ST0_FT0(void)
1761
{
1762
    ST0 *= FT0;
1763
}
1764

    
1765
void OPPROTO op_fsub_ST0_FT0(void)
1766
{
1767
    ST0 -= FT0;
1768
}
1769

    
1770
void OPPROTO op_fsubr_ST0_FT0(void)
1771
{
1772
    ST0 = FT0 - ST0;
1773
}
1774

    
1775
void OPPROTO op_fdiv_ST0_FT0(void)
1776
{
1777
    ST0 /= FT0;
1778
}
1779

    
1780
void OPPROTO op_fdivr_ST0_FT0(void)
1781
{
1782
    ST0 = FT0 / ST0;
1783
}
1784

    
1785
/* fp operations between STN and ST0 */
1786

    
1787
void OPPROTO op_fadd_STN_ST0(void)
1788
{
1789
    ST(PARAM1) += ST0;
1790
}
1791

    
1792
void OPPROTO op_fmul_STN_ST0(void)
1793
{
1794
    ST(PARAM1) *= ST0;
1795
}
1796

    
1797
void OPPROTO op_fsub_STN_ST0(void)
1798
{
1799
    ST(PARAM1) -= ST0;
1800
}
1801

    
1802
void OPPROTO op_fsubr_STN_ST0(void)
1803
{
1804
    CPU86_LDouble *p;
1805
    p = &ST(PARAM1);
1806
    *p = ST0 - *p;
1807
}
1808

    
1809
void OPPROTO op_fdiv_STN_ST0(void)
1810
{
1811
    ST(PARAM1) /= ST0;
1812
}
1813

    
1814
void OPPROTO op_fdivr_STN_ST0(void)
1815
{
1816
    CPU86_LDouble *p;
1817
    p = &ST(PARAM1);
1818
    *p = ST0 / *p;
1819
}
1820

    
1821
/* misc FPU operations */
1822
void OPPROTO op_fchs_ST0(void)
1823
{
1824
    ST0 = -ST0;
1825
}
1826

    
1827
void OPPROTO op_fabs_ST0(void)
1828
{
1829
    ST0 = fabs(ST0);
1830
}
1831

    
1832
void OPPROTO op_fxam_ST0(void)
1833
{
1834
    helper_fxam_ST0();
1835
}
1836

    
1837
void OPPROTO op_fld1_ST0(void)
1838
{
1839
    ST0 = f15rk[1];
1840
}
1841

    
1842
void OPPROTO op_fldl2t_ST0(void)
1843
{
1844
    ST0 = f15rk[6];
1845
}
1846

    
1847
void OPPROTO op_fldl2e_ST0(void)
1848
{
1849
    ST0 = f15rk[5];
1850
}
1851

    
1852
void OPPROTO op_fldpi_ST0(void)
1853
{
1854
    ST0 = f15rk[2];
1855
}
1856

    
1857
void OPPROTO op_fldlg2_ST0(void)
1858
{
1859
    ST0 = f15rk[3];
1860
}
1861

    
1862
void OPPROTO op_fldln2_ST0(void)
1863
{
1864
    ST0 = f15rk[4];
1865
}
1866

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

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

    
1877
/* associated heplers to reduce generated code length and to simplify
1878
   relocation (FP constants are usually stored in .rodata section) */
1879

    
1880
void OPPROTO op_f2xm1(void)
1881
{
1882
    helper_f2xm1();
1883
}
1884

    
1885
void OPPROTO op_fyl2x(void)
1886
{
1887
    helper_fyl2x();
1888
}
1889

    
1890
void OPPROTO op_fptan(void)
1891
{
1892
    helper_fptan();
1893
}
1894

    
1895
void OPPROTO op_fpatan(void)
1896
{
1897
    helper_fpatan();
1898
}
1899

    
1900
void OPPROTO op_fxtract(void)
1901
{
1902
    helper_fxtract();
1903
}
1904

    
1905
void OPPROTO op_fprem1(void)
1906
{
1907
    helper_fprem1();
1908
}
1909

    
1910

    
1911
void OPPROTO op_fprem(void)
1912
{
1913
    helper_fprem();
1914
}
1915

    
1916
void OPPROTO op_fyl2xp1(void)
1917
{
1918
    helper_fyl2xp1();
1919
}
1920

    
1921
void OPPROTO op_fsqrt(void)
1922
{
1923
    helper_fsqrt();
1924
}
1925

    
1926
void OPPROTO op_fsincos(void)
1927
{
1928
    helper_fsincos();
1929
}
1930

    
1931
void OPPROTO op_frndint(void)
1932
{
1933
    helper_frndint();
1934
}
1935

    
1936
void OPPROTO op_fscale(void)
1937
{
1938
    helper_fscale();
1939
}
1940

    
1941
void OPPROTO op_fsin(void)
1942
{
1943
    helper_fsin();
1944
}
1945

    
1946
void OPPROTO op_fcos(void)
1947
{
1948
    helper_fcos();
1949
}
1950

    
1951
void OPPROTO op_fnstsw_A0(void)
1952
{
1953
    int fpus;
1954
    fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1955
    stw((void *)A0, fpus);
1956
}
1957

    
1958
void OPPROTO op_fnstsw_EAX(void)
1959
{
1960
    int fpus;
1961
    fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1962
    EAX = (EAX & 0xffff0000) | fpus;
1963
}
1964

    
1965
void OPPROTO op_fnstcw_A0(void)
1966
{
1967
    stw((void *)A0, env->fpuc);
1968
}
1969

    
1970
void OPPROTO op_fldcw_A0(void)
1971
{
1972
    int rnd_type;
1973
    env->fpuc = lduw((void *)A0);
1974
    /* set rounding mode */
1975
    switch(env->fpuc & RC_MASK) {
1976
    default:
1977
    case RC_NEAR:
1978
        rnd_type = FE_TONEAREST;
1979
        break;
1980
    case RC_DOWN:
1981
        rnd_type = FE_DOWNWARD;
1982
        break;
1983
    case RC_UP:
1984
        rnd_type = FE_UPWARD;
1985
        break;
1986
    case RC_CHOP:
1987
        rnd_type = FE_TOWARDZERO;
1988
        break;
1989
    }
1990
    fesetround(rnd_type);
1991
}
1992

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

    
1998
void OPPROTO op_fninit(void)
1999
{
2000
    env->fpus = 0;
2001
    env->fpstt = 0;
2002
    env->fpuc = 0x37f;
2003
    env->fptags[0] = 1;
2004
    env->fptags[1] = 1;
2005
    env->fptags[2] = 1;
2006
    env->fptags[3] = 1;
2007
    env->fptags[4] = 1;
2008
    env->fptags[5] = 1;
2009
    env->fptags[6] = 1;
2010
    env->fptags[7] = 1;
2011
}
2012

    
2013
void OPPROTO op_fnstenv_A0(void)
2014
{
2015
    helper_fstenv((uint8_t *)A0, PARAM1);
2016
}
2017

    
2018
void OPPROTO op_fldenv_A0(void)
2019
{
2020
    helper_fldenv((uint8_t *)A0, PARAM1);
2021
}
2022

    
2023
void OPPROTO op_fnsave_A0(void)
2024
{
2025
    helper_fsave((uint8_t *)A0, PARAM1);
2026
}
2027

    
2028
void OPPROTO op_frstor_A0(void)
2029
{
2030
    helper_frstor((uint8_t *)A0, PARAM1);
2031
}
2032

    
2033
/* threading support */
2034
void OPPROTO op_lock(void)
2035
{
2036
    cpu_lock();
2037
}
2038

    
2039
void OPPROTO op_unlock(void)
2040
{
2041
    cpu_unlock();
2042
}
2043