Statistics
| Branch: | Revision:

root / op-i386.c @ 4c3a88a2

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_protected(void)
957
{
958
    helper_iret_protected(PARAM1);
959
}
960

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

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

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

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

    
983
void OPPROTO op_lmsw_T0(void)
984
{
985
    /* only 4 lower bits of CR0 are modified */
986
    T0 = (env->cr[0] & ~0xf) | (T0 & 0xf);
987
    helper_movl_crN_T0(0);
988
}
989

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

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

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

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

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

    
1015
/* flags handling */
1016

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

    
1029
/* slow set cases (compute x86 flags) */
1030
void OPPROTO op_seto_T0_cc(void)
1031
{
1032
    int eflags;
1033
    eflags = cc_table[CC_OP].compute_all();
1034
    T0 = (eflags >> 11) & 1;
1035
}
1036

    
1037
void OPPROTO op_setb_T0_cc(void)
1038
{
1039
    T0 = cc_table[CC_OP].compute_c();
1040
}
1041

    
1042
void OPPROTO op_setz_T0_cc(void)
1043
{
1044
    int eflags;
1045
    eflags = cc_table[CC_OP].compute_all();
1046
    T0 = (eflags >> 6) & 1;
1047
}
1048

    
1049
void OPPROTO op_setbe_T0_cc(void)
1050
{
1051
    int eflags;
1052
    eflags = cc_table[CC_OP].compute_all();
1053
    T0 = (eflags & (CC_Z | CC_C)) != 0;
1054
}
1055

    
1056
void OPPROTO op_sets_T0_cc(void)
1057
{
1058
    int eflags;
1059
    eflags = cc_table[CC_OP].compute_all();
1060
    T0 = (eflags >> 7) & 1;
1061
}
1062

    
1063
void OPPROTO op_setp_T0_cc(void)
1064
{
1065
    int eflags;
1066
    eflags = cc_table[CC_OP].compute_all();
1067
    T0 = (eflags >> 2) & 1;
1068
}
1069

    
1070
void OPPROTO op_setl_T0_cc(void)
1071
{
1072
    int eflags;
1073
    eflags = cc_table[CC_OP].compute_all();
1074
    T0 = ((eflags ^ (eflags >> 4)) >> 7) & 1;
1075
}
1076

    
1077
void OPPROTO op_setle_T0_cc(void)
1078
{
1079
    int eflags;
1080
    eflags = cc_table[CC_OP].compute_all();
1081
    T0 = (((eflags ^ (eflags >> 4)) & 0x80) || (eflags & CC_Z)) != 0;
1082
}
1083

    
1084
void OPPROTO op_xor_T0_1(void)
1085
{
1086
    T0 ^= 1;
1087
}
1088

    
1089
void OPPROTO op_set_cc_op(void)
1090
{
1091
    CC_OP = PARAM1;
1092
}
1093

    
1094
#define FL_UPDATE_MASK16 (FL_UPDATE_MASK32 & 0xffff)
1095

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

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

    
1118
void OPPROTO op_movl_eflags_T0_cpl0(void)
1119
{
1120
    load_eflags(T0, FL_UPDATE_CPL0_MASK);
1121
}
1122

    
1123
void OPPROTO op_movw_eflags_T0_cpl0(void)
1124
{
1125
    load_eflags(T0, FL_UPDATE_CPL0_MASK & 0xffff);
1126
}
1127

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

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

    
1169
/* XXX: compute only O flag */
1170
void OPPROTO op_movb_eflags_T0(void)
1171
{
1172
    int of;
1173
    of = cc_table[CC_OP].compute_all() & CC_O;
1174
    CC_SRC = (T0 & (CC_S | CC_Z | CC_A | CC_P | CC_C)) | of;
1175
}
1176

    
1177
void OPPROTO op_movl_T0_eflags(void)
1178
{
1179
    int eflags;
1180
    eflags = cc_table[CC_OP].compute_all();
1181
    eflags |= (DF & DF_MASK);
1182
    eflags |= env->eflags & ~(VM_MASK | RF_MASK);
1183
    T0 = eflags;
1184
}
1185

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

    
1200
void OPPROTO op_cld(void)
1201
{
1202
    DF = 1;
1203
}
1204

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

    
1210
void OPPROTO op_clc(void)
1211
{
1212
    int eflags;
1213
    eflags = cc_table[CC_OP].compute_all();
1214
    eflags &= ~CC_C;
1215
    CC_SRC = eflags;
1216
}
1217

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

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

    
1234
void OPPROTO op_salc(void)
1235
{
1236
    int cf;
1237
    cf = cc_table[CC_OP].compute_c();
1238
    EAX = (EAX & ~0xff) | ((-cf) & 0xff);
1239
}
1240

    
1241
static int compute_all_eflags(void)
1242
{
1243
    return CC_SRC;
1244
}
1245

    
1246
static int compute_c_eflags(void)
1247
{
1248
    return CC_SRC & CC_C;
1249
}
1250

    
1251
static int compute_c_mul(void)
1252
{
1253
    int cf;
1254
    cf = (CC_SRC != 0);
1255
    return cf;
1256
}
1257

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

    
1273
    [CC_OP_EFLAGS] = { compute_all_eflags, compute_c_eflags },
1274

    
1275
    [CC_OP_MUL] = { compute_all_mul, compute_c_mul },
1276

    
1277
    [CC_OP_ADDB] = { compute_all_addb, compute_c_addb },
1278
    [CC_OP_ADDW] = { compute_all_addw, compute_c_addw  },
1279
    [CC_OP_ADDL] = { compute_all_addl, compute_c_addl  },
1280

    
1281
    [CC_OP_ADCB] = { compute_all_adcb, compute_c_adcb },
1282
    [CC_OP_ADCW] = { compute_all_adcw, compute_c_adcw  },
1283
    [CC_OP_ADCL] = { compute_all_adcl, compute_c_adcl  },
1284

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

    
1309
    [CC_OP_SARB] = { compute_all_sarb, compute_c_sarl },
1310
    [CC_OP_SARW] = { compute_all_sarw, compute_c_sarl },
1311
    [CC_OP_SARL] = { compute_all_sarl, compute_c_sarl },
1312
};
1313

    
1314
/* floating point support. Some of the code for complicated x87
1315
   functions comes from the LGPL'ed x86 emulator found in the Willows
1316
   TWIN windows emulator. */
1317

    
1318
#if defined(__powerpc__)
1319
extern CPU86_LDouble copysign(CPU86_LDouble, CPU86_LDouble);
1320

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

    
1335
#define rint qemu_rint
1336
#endif
1337

    
1338
/* fp load FT0 */
1339

    
1340
void OPPROTO op_flds_FT0_A0(void)
1341
{
1342
#ifdef USE_FP_CONVERT
1343
    FP_CONVERT.i32 = ldl((void *)A0);
1344
    FT0 = FP_CONVERT.f;
1345
#else
1346
    FT0 = ldfl((void *)A0);
1347
#endif
1348
}
1349

    
1350
void OPPROTO op_fldl_FT0_A0(void)
1351
{
1352
#ifdef USE_FP_CONVERT
1353
    FP_CONVERT.i64 = ldq((void *)A0);
1354
    FT0 = FP_CONVERT.d;
1355
#else
1356
    FT0 = ldfq((void *)A0);
1357
#endif
1358
}
1359

    
1360
/* helpers are needed to avoid static constant reference. XXX: find a better way */
1361
#ifdef USE_INT_TO_FLOAT_HELPERS
1362

    
1363
void helper_fild_FT0_A0(void)
1364
{
1365
    FT0 = (CPU86_LDouble)ldsw((void *)A0);
1366
}
1367

    
1368
void helper_fildl_FT0_A0(void)
1369
{
1370
    FT0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1371
}
1372

    
1373
void helper_fildll_FT0_A0(void)
1374
{
1375
    FT0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1376
}
1377

    
1378
void OPPROTO op_fild_FT0_A0(void)
1379
{
1380
    helper_fild_FT0_A0();
1381
}
1382

    
1383
void OPPROTO op_fildl_FT0_A0(void)
1384
{
1385
    helper_fildl_FT0_A0();
1386
}
1387

    
1388
void OPPROTO op_fildll_FT0_A0(void)
1389
{
1390
    helper_fildll_FT0_A0();
1391
}
1392

    
1393
#else
1394

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

    
1405
void OPPROTO op_fildl_FT0_A0(void)
1406
{
1407
#ifdef USE_FP_CONVERT
1408
    FP_CONVERT.i32 = (int32_t) ldl((void *)A0);
1409
    FT0 = (CPU86_LDouble)FP_CONVERT.i32;
1410
#else
1411
    FT0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1412
#endif
1413
}
1414

    
1415
void OPPROTO op_fildll_FT0_A0(void)
1416
{
1417
#ifdef USE_FP_CONVERT
1418
    FP_CONVERT.i64 = (int64_t) ldq((void *)A0);
1419
    FT0 = (CPU86_LDouble)FP_CONVERT.i64;
1420
#else
1421
    FT0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1422
#endif
1423
}
1424
#endif
1425

    
1426
/* fp load ST0 */
1427

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

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

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

    
1472
/* helpers are needed to avoid static constant reference. XXX: find a better way */
1473
#ifdef USE_INT_TO_FLOAT_HELPERS
1474

    
1475
void helper_fild_ST0_A0(void)
1476
{
1477
    int new_fpstt;
1478
    new_fpstt = (env->fpstt - 1) & 7;
1479
    env->fpregs[new_fpstt] = (CPU86_LDouble)ldsw((void *)A0);
1480
    env->fpstt = new_fpstt;
1481
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1482
}
1483

    
1484
void helper_fildl_ST0_A0(void)
1485
{
1486
    int new_fpstt;
1487
    new_fpstt = (env->fpstt - 1) & 7;
1488
    env->fpregs[new_fpstt] = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1489
    env->fpstt = new_fpstt;
1490
    env->fptags[new_fpstt] = 0; /* validate stack entry */
1491
}
1492

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

    
1502
void OPPROTO op_fild_ST0_A0(void)
1503
{
1504
    helper_fild_ST0_A0();
1505
}
1506

    
1507
void OPPROTO op_fildl_ST0_A0(void)
1508
{
1509
    helper_fildl_ST0_A0();
1510
}
1511

    
1512
void OPPROTO op_fildll_ST0_A0(void)
1513
{
1514
    helper_fildll_ST0_A0();
1515
}
1516

    
1517
#else
1518

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

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

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

    
1561
#endif
1562

    
1563
/* fp store */
1564

    
1565
void OPPROTO op_fsts_ST0_A0(void)
1566
{
1567
#ifdef USE_FP_CONVERT
1568
    FP_CONVERT.f = (float)ST0;
1569
    stfl((void *)A0, FP_CONVERT.f);
1570
#else
1571
    stfl((void *)A0, (float)ST0);
1572
#endif
1573
}
1574

    
1575
void OPPROTO op_fstl_ST0_A0(void)
1576
{
1577
    stfq((void *)A0, (double)ST0);
1578
}
1579

    
1580
#ifdef USE_X86LDOUBLE
1581
void OPPROTO op_fstt_ST0_A0(void)
1582
{
1583
    *(long double *)A0 = ST0;
1584
}
1585
#else
1586
void OPPROTO op_fstt_ST0_A0(void)
1587
{
1588
    helper_fstt_ST0_A0();
1589
}
1590
#endif
1591

    
1592
void OPPROTO op_fist_ST0_A0(void)
1593
{
1594
#if defined(__sparc__) && !defined(__sparc_v9__)
1595
    register CPU86_LDouble d asm("o0");
1596
#else
1597
    CPU86_LDouble d;
1598
#endif
1599
    int val;
1600

    
1601
    d = ST0;
1602
    val = lrint(d);
1603
    if (val != (int16_t)val)
1604
        val = -32768;
1605
    stw((void *)A0, val);
1606
}
1607

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

    
1617
    d = ST0;
1618
    val = lrint(d);
1619
    stl((void *)A0, val);
1620
}
1621

    
1622
void OPPROTO op_fistll_ST0_A0(void)
1623
{
1624
#if defined(__sparc__) && !defined(__sparc_v9__)
1625
    register CPU86_LDouble d asm("o0");
1626
#else
1627
    CPU86_LDouble d;
1628
#endif
1629
    int64_t val;
1630

    
1631
    d = ST0;
1632
    val = llrint(d);
1633
    stq((void *)A0, val);
1634
}
1635

    
1636
void OPPROTO op_fbld_ST0_A0(void)
1637
{
1638
    helper_fbld_ST0_A0();
1639
}
1640

    
1641
void OPPROTO op_fbst_ST0_A0(void)
1642
{
1643
    helper_fbst_ST0_A0();
1644
}
1645

    
1646
/* FPU move */
1647

    
1648
void OPPROTO op_fpush(void)
1649
{
1650
    fpush();
1651
}
1652

    
1653
void OPPROTO op_fpop(void)
1654
{
1655
    fpop();
1656
}
1657

    
1658
void OPPROTO op_fdecstp(void)
1659
{
1660
    env->fpstt = (env->fpstt - 1) & 7;
1661
    env->fpus &= (~0x4700);
1662
}
1663

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

    
1670
void OPPROTO op_fmov_ST0_FT0(void)
1671
{
1672
    ST0 = FT0;
1673
}
1674

    
1675
void OPPROTO op_fmov_FT0_STN(void)
1676
{
1677
    FT0 = ST(PARAM1);
1678
}
1679

    
1680
void OPPROTO op_fmov_ST0_STN(void)
1681
{
1682
    ST0 = ST(PARAM1);
1683
}
1684

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

    
1690
void OPPROTO op_fxchg_ST0_STN(void)
1691
{
1692
    CPU86_LDouble tmp;
1693
    tmp = ST(PARAM1);
1694
    ST(PARAM1) = ST0;
1695
    ST0 = tmp;
1696
}
1697

    
1698
/* FPU operations */
1699

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

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

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

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

    
1750
void OPPROTO op_fadd_ST0_FT0(void)
1751
{
1752
    ST0 += FT0;
1753
}
1754

    
1755
void OPPROTO op_fmul_ST0_FT0(void)
1756
{
1757
    ST0 *= FT0;
1758
}
1759

    
1760
void OPPROTO op_fsub_ST0_FT0(void)
1761
{
1762
    ST0 -= FT0;
1763
}
1764

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

    
1770
void OPPROTO op_fdiv_ST0_FT0(void)
1771
{
1772
    ST0 /= FT0;
1773
}
1774

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

    
1780
/* fp operations between STN and ST0 */
1781

    
1782
void OPPROTO op_fadd_STN_ST0(void)
1783
{
1784
    ST(PARAM1) += ST0;
1785
}
1786

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

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

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

    
1804
void OPPROTO op_fdiv_STN_ST0(void)
1805
{
1806
    ST(PARAM1) /= ST0;
1807
}
1808

    
1809
void OPPROTO op_fdivr_STN_ST0(void)
1810
{
1811
    CPU86_LDouble *p;
1812
    p = &ST(PARAM1);
1813
    *p = ST0 / *p;
1814
}
1815

    
1816
/* misc FPU operations */
1817
void OPPROTO op_fchs_ST0(void)
1818
{
1819
    ST0 = -ST0;
1820
}
1821

    
1822
void OPPROTO op_fabs_ST0(void)
1823
{
1824
    ST0 = fabs(ST0);
1825
}
1826

    
1827
void OPPROTO op_fxam_ST0(void)
1828
{
1829
    helper_fxam_ST0();
1830
}
1831

    
1832
void OPPROTO op_fld1_ST0(void)
1833
{
1834
    ST0 = f15rk[1];
1835
}
1836

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

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

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

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

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

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

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

    
1872
/* associated heplers to reduce generated code length and to simplify
1873
   relocation (FP constants are usually stored in .rodata section) */
1874

    
1875
void OPPROTO op_f2xm1(void)
1876
{
1877
    helper_f2xm1();
1878
}
1879

    
1880
void OPPROTO op_fyl2x(void)
1881
{
1882
    helper_fyl2x();
1883
}
1884

    
1885
void OPPROTO op_fptan(void)
1886
{
1887
    helper_fptan();
1888
}
1889

    
1890
void OPPROTO op_fpatan(void)
1891
{
1892
    helper_fpatan();
1893
}
1894

    
1895
void OPPROTO op_fxtract(void)
1896
{
1897
    helper_fxtract();
1898
}
1899

    
1900
void OPPROTO op_fprem1(void)
1901
{
1902
    helper_fprem1();
1903
}
1904

    
1905

    
1906
void OPPROTO op_fprem(void)
1907
{
1908
    helper_fprem();
1909
}
1910

    
1911
void OPPROTO op_fyl2xp1(void)
1912
{
1913
    helper_fyl2xp1();
1914
}
1915

    
1916
void OPPROTO op_fsqrt(void)
1917
{
1918
    helper_fsqrt();
1919
}
1920

    
1921
void OPPROTO op_fsincos(void)
1922
{
1923
    helper_fsincos();
1924
}
1925

    
1926
void OPPROTO op_frndint(void)
1927
{
1928
    helper_frndint();
1929
}
1930

    
1931
void OPPROTO op_fscale(void)
1932
{
1933
    helper_fscale();
1934
}
1935

    
1936
void OPPROTO op_fsin(void)
1937
{
1938
    helper_fsin();
1939
}
1940

    
1941
void OPPROTO op_fcos(void)
1942
{
1943
    helper_fcos();
1944
}
1945

    
1946
void OPPROTO op_fnstsw_A0(void)
1947
{
1948
    int fpus;
1949
    fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1950
    stw((void *)A0, fpus);
1951
}
1952

    
1953
void OPPROTO op_fnstsw_EAX(void)
1954
{
1955
    int fpus;
1956
    fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1957
    EAX = (EAX & 0xffff0000) | fpus;
1958
}
1959

    
1960
void OPPROTO op_fnstcw_A0(void)
1961
{
1962
    stw((void *)A0, env->fpuc);
1963
}
1964

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

    
1988
void OPPROTO op_fclex(void)
1989
{
1990
    env->fpus &= 0x7f00;
1991
}
1992

    
1993
void OPPROTO op_fninit(void)
1994
{
1995
    env->fpus = 0;
1996
    env->fpstt = 0;
1997
    env->fpuc = 0x37f;
1998
    env->fptags[0] = 1;
1999
    env->fptags[1] = 1;
2000
    env->fptags[2] = 1;
2001
    env->fptags[3] = 1;
2002
    env->fptags[4] = 1;
2003
    env->fptags[5] = 1;
2004
    env->fptags[6] = 1;
2005
    env->fptags[7] = 1;
2006
}
2007

    
2008
void OPPROTO op_fnstenv_A0(void)
2009
{
2010
    helper_fstenv((uint8_t *)A0, PARAM1);
2011
}
2012

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

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

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

    
2028
/* threading support */
2029
void OPPROTO op_lock(void)
2030
{
2031
    cpu_lock();
2032
}
2033

    
2034
void OPPROTO op_unlock(void)
2035
{
2036
    cpu_unlock();
2037
}
2038