op.c 39.8 KB
Newer Older
bellard's avatar
bellard committed
1
2
3
4
/*
 *  MIPS emulation micro-operations for qemu.
 * 
 *  Copyright (c) 2004-2005 Jocelyn Mayer
bellard's avatar
bellard committed
5
 *  Copyright (c) 2006 Marius Groeger (FPU operations)
bellard's avatar
bellard committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "config.h"
#include "exec.h"

bellard's avatar
bellard committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#ifndef CALL_FROM_TB0
#define CALL_FROM_TB0(func) func();
#endif
#ifndef CALL_FROM_TB1
#define CALL_FROM_TB1(func, arg0) func(arg0);
#endif
#ifndef CALL_FROM_TB1_CONST16
#define CALL_FROM_TB1_CONST16(func, arg0) CALL_FROM_TB1(func, arg0);
#endif
#ifndef CALL_FROM_TB2
#define CALL_FROM_TB2(func, arg0, arg1) func(arg0, arg1);
#endif
#ifndef CALL_FROM_TB2_CONST16
#define CALL_FROM_TB2_CONST16(func, arg0, arg1)     \
CALL_FROM_TB2(func, arg0, arg1);
#endif
#ifndef CALL_FROM_TB3
#define CALL_FROM_TB3(func, arg0, arg1, arg2) func(arg0, arg1, arg2);
#endif
#ifndef CALL_FROM_TB4
#define CALL_FROM_TB4(func, arg0, arg1, arg2, arg3) \
        func(arg0, arg1, arg2, arg3);
#endif

bellard's avatar
bellard committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#define REG 1
#include "op_template.c"
#undef REG
#define REG 2
#include "op_template.c"
#undef REG
#define REG 3
#include "op_template.c"
#undef REG
#define REG 4
#include "op_template.c"
#undef REG
#define REG 5
#include "op_template.c"
#undef REG
#define REG 6
#include "op_template.c"
#undef REG
#define REG 7
#include "op_template.c"
#undef REG
#define REG 8
#include "op_template.c"
#undef REG
#define REG 9
#include "op_template.c"
#undef REG
#define REG 10
#include "op_template.c"
#undef REG
#define REG 11
#include "op_template.c"
#undef REG
#define REG 12
#include "op_template.c"
#undef REG
#define REG 13
#include "op_template.c"
#undef REG
#define REG 14
#include "op_template.c"
#undef REG
#define REG 15
#include "op_template.c"
#undef REG
#define REG 16
#include "op_template.c"
#undef REG
#define REG 17
#include "op_template.c"
#undef REG
#define REG 18
#include "op_template.c"
#undef REG
#define REG 19
#include "op_template.c"
#undef REG
#define REG 20
#include "op_template.c"
#undef REG
#define REG 21
#include "op_template.c"
#undef REG
#define REG 22
#include "op_template.c"
#undef REG
#define REG 23
#include "op_template.c"
#undef REG
#define REG 24
#include "op_template.c"
#undef REG
#define REG 25
#include "op_template.c"
#undef REG
#define REG 26
#include "op_template.c"
#undef REG
#define REG 27
#include "op_template.c"
#undef REG
#define REG 28
#include "op_template.c"
#undef REG
#define REG 29
#include "op_template.c"
#undef REG
#define REG 30
#include "op_template.c"
#undef REG
#define REG 31
#include "op_template.c"
#undef REG

143
#define TN
bellard's avatar
bellard committed
144
145
146
#include "op_template.c"
#undef TN

bellard's avatar
bellard committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#ifdef MIPS_USES_FPU

#define SFREG 0
#define DFREG 0
#include "fop_template.c"
#undef SFREG
#undef DFREG
#define SFREG 1
#include "fop_template.c"
#undef SFREG
#define SFREG 2
#define DFREG 2
#include "fop_template.c"
#undef SFREG
#undef DFREG
#define SFREG 3
#include "fop_template.c"
#undef SFREG
#define SFREG 4
#define DFREG 4
#include "fop_template.c"
#undef SFREG
#undef DFREG
#define SFREG 5
#include "fop_template.c"
#undef SFREG
#define SFREG 6
#define DFREG 6
#include "fop_template.c"
#undef SFREG
#undef DFREG
#define SFREG 7
#include "fop_template.c"
#undef SFREG
#define SFREG 8
#define DFREG 8
#include "fop_template.c"
#undef SFREG
#undef DFREG
#define SFREG 9
#include "fop_template.c"
#undef SFREG
#define SFREG 10
#define DFREG 10
#include "fop_template.c"
#undef SFREG
#undef DFREG
#define SFREG 11
#include "fop_template.c"
#undef SFREG
#define SFREG 12
#define DFREG 12
#include "fop_template.c"
#undef SFREG
#undef DFREG
#define SFREG 13
#include "fop_template.c"
#undef SFREG
#define SFREG 14
#define DFREG 14
#include "fop_template.c"
#undef SFREG
#undef DFREG
#define SFREG 15
#include "fop_template.c"
#undef SFREG
#define SFREG 16
#define DFREG 16
#include "fop_template.c"
#undef SFREG
#undef DFREG
#define SFREG 17
#include "fop_template.c"
#undef SFREG
#define SFREG 18
#define DFREG 18
#include "fop_template.c"
#undef SFREG
#undef DFREG
#define SFREG 19
#include "fop_template.c"
#undef SFREG
#define SFREG 20
#define DFREG 20
#include "fop_template.c"
#undef SFREG
#undef DFREG
#define SFREG 21
#include "fop_template.c"
#undef SFREG
#define SFREG 22
#define DFREG 22
#include "fop_template.c"
#undef SFREG
#undef DFREG
#define SFREG 23
#include "fop_template.c"
#undef SFREG
#define SFREG 24
#define DFREG 24
#include "fop_template.c"
#undef SFREG
#undef DFREG
#define SFREG 25
#include "fop_template.c"
#undef SFREG
#define SFREG 26
#define DFREG 26
#include "fop_template.c"
#undef SFREG
#undef DFREG
#define SFREG 27
#include "fop_template.c"
#undef SFREG
#define SFREG 28
#define DFREG 28
#include "fop_template.c"
#undef SFREG
#undef DFREG
#define SFREG 29
#include "fop_template.c"
#undef SFREG
#define SFREG 30
#define DFREG 30
#include "fop_template.c"
#undef SFREG
#undef DFREG
#define SFREG 31
#include "fop_template.c"
#undef SFREG

#define FTN
#include "fop_template.c"
#undef FTN

#endif

bellard's avatar
bellard committed
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
void op_dup_T0 (void)
{
    T2 = T0;
    RETURN();
}

void op_load_HI (void)
{
    T0 = env->HI;
    RETURN();
}

void op_store_HI (void)
{
    env->HI = T0;
    RETURN();
}

void op_load_LO (void)
{
    T0 = env->LO;
    RETURN();
}

void op_store_LO (void)
{
    env->LO = T0;
    RETURN();
}

/* Load and store */
#define MEMSUFFIX _raw
#include "op_mem.c"
#undef MEMSUFFIX
#if !defined(CONFIG_USER_ONLY)
#define MEMSUFFIX _user
#include "op_mem.c"
#undef MEMSUFFIX

#define MEMSUFFIX _kernel
#include "op_mem.c"
#undef MEMSUFFIX
#endif

/* Arithmetic */
void op_add (void)
{
ths's avatar
ths committed
331
    T0 = (int32_t)((int32_t)T0 + (int32_t)T1);
bellard's avatar
bellard committed
332
333
334
335
336
337
338
    RETURN();
}

void op_addo (void)
{
    target_ulong tmp;

339
340
    tmp = (int32_t)T0;
    T0 = (int32_t)T0 + (int32_t)T1;
341
    if (((tmp ^ T1 ^ (-1)) & (T0 ^ T1)) >> 31) {
342
        /* operands of same sign, result different sign */
bellard's avatar
bellard committed
343
        CALL_FROM_TB1(do_raise_exception_direct, EXCP_OVERFLOW);
bellard's avatar
bellard committed
344
    }
ths's avatar
ths committed
345
    T0 = (int32_t)T0;
bellard's avatar
bellard committed
346
347
348
349
350
    RETURN();
}

void op_sub (void)
{
ths's avatar
ths committed
351
    T0 = (int32_t)((int32_t)T0 - (int32_t)T1);
bellard's avatar
bellard committed
352
353
354
355
356
357
358
    RETURN();
}

void op_subo (void)
{
    target_ulong tmp;

359
    tmp = (int32_t)T0;
bellard's avatar
bellard committed
360
    T0 = (int32_t)T0 - (int32_t)T1;
361
    if (((tmp ^ T1) & (tmp ^ T0)) >> 31) {
362
        /* operands of different sign, first operand and result different sign */
bellard's avatar
bellard committed
363
        CALL_FROM_TB1(do_raise_exception_direct, EXCP_OVERFLOW);
bellard's avatar
bellard committed
364
    }
ths's avatar
ths committed
365
    T0 = (int32_t)T0;
bellard's avatar
bellard committed
366
367
368
369
370
    RETURN();
}

void op_mul (void)
{
ths's avatar
ths committed
371
    T0 = (int32_t)((int32_t)T0 * (int32_t)T1);
bellard's avatar
bellard committed
372
373
374
375
376
377
    RETURN();
}

void op_div (void)
{
    if (T1 != 0) {
ths's avatar
ths committed
378
379
        env->LO = (int32_t)((int32_t)T0 / (int32_t)T1);
        env->HI = (int32_t)((int32_t)T0 % (int32_t)T1);
bellard's avatar
bellard committed
380
381
382
383
384
    }
    RETURN();
}

void op_divu (void)
385
386
{
    if (T1 != 0) {
ths's avatar
ths committed
387
388
        env->LO = (int32_t)((uint32_t)T0 / (uint32_t)T1);
        env->HI = (int32_t)((uint32_t)T0 % (uint32_t)T1);
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
    }
    RETURN();
}

#ifdef MIPS_HAS_MIPS64
/* Arithmetic */
void op_dadd (void)
{
    T0 += T1;
    RETURN();
}

void op_daddo (void)
{
    target_long tmp;

    tmp = T0;
    T0 += T1;
    if (((tmp ^ T1 ^ (-1)) & (T0 ^ T1)) >> 63) {
        /* operands of same sign, result different sign */
        CALL_FROM_TB1(do_raise_exception_direct, EXCP_OVERFLOW);
    }
    RETURN();
}

void op_dsub (void)
{
    T0 -= T1;
    RETURN();
}

void op_dsubo (void)
{
    target_long tmp;

    tmp = T0;
    T0 = (int64_t)T0 - (int64_t)T1;
    if (((tmp ^ T1) & (tmp ^ T0)) >> 63) {
        /* operands of different sign, first operand and result different sign */
        CALL_FROM_TB1(do_raise_exception_direct, EXCP_OVERFLOW);
    }
    RETURN();
}

void op_dmul (void)
{
    T0 = (int64_t)T0 * (int64_t)T1;
    RETURN();
}

#if TARGET_LONG_BITS > HOST_LONG_BITS
/* Those might call libgcc functions.  */
void op_ddiv (void)
{
    do_ddiv();
    RETURN();
}

void op_ddivu (void)
{
    do_ddivu();
    RETURN();
}
#else
void op_ddiv (void)
{
    if (T1 != 0) {
        env->LO = (int64_t)T0 / (int64_t)T1;
        env->HI = (int64_t)T0 % (int64_t)T1;
    }
    RETURN();
}

void op_ddivu (void)
bellard's avatar
bellard committed
463
464
465
466
467
468
469
{
    if (T1 != 0) {
        env->LO = T0 / T1;
        env->HI = T0 % T1;
    }
    RETURN();
}
470
471
#endif
#endif /* MIPS_HAS_MIPS64 */
bellard's avatar
bellard committed
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499

/* Logical */
void op_and (void)
{
    T0 &= T1;
    RETURN();
}

void op_nor (void)
{
    T0 = ~(T0 | T1);
    RETURN();
}

void op_or (void)
{
    T0 |= T1;
    RETURN();
}

void op_xor (void)
{
    T0 ^= T1;
    RETURN();
}

void op_sll (void)
{
ths's avatar
ths committed
500
    T0 = (int32_t)((uint32_t)T0 << (uint32_t)T1);
bellard's avatar
bellard committed
501
502
503
504
505
    RETURN();
}

void op_sra (void)
{
ths's avatar
ths committed
506
    T0 = (int32_t)((int32_t)T0 >> (uint32_t)T1);
bellard's avatar
bellard committed
507
508
509
510
511
    RETURN();
}

void op_srl (void)
{
ths's avatar
ths committed
512
    T0 = (int32_t)((uint32_t)T0 >> (uint32_t)T1);
bellard's avatar
bellard committed
513
514
515
    RETURN();
}

516
517
518
519
520
void op_rotr (void)
{
    target_ulong tmp;

    if (T1) {
ths's avatar
ths committed
521
522
       tmp = (int32_t)((uint32_t)T0 << (0x20 - (uint32_t)T1));
       T0 = (int32_t)((uint32_t)T0 >> (uint32_t)T1) | tmp;
523
524
525
526
527
    } else
       T0 = T1;
    RETURN();
}

bellard's avatar
bellard committed
528
529
void op_sllv (void)
{
ths's avatar
ths committed
530
    T0 = (int32_t)((uint32_t)T1 << ((uint32_t)T0 & 0x1F));
bellard's avatar
bellard committed
531
532
533
534
535
    RETURN();
}

void op_srav (void)
{
ths's avatar
ths committed
536
    T0 = (int32_t)((int32_t)T1 >> (T0 & 0x1F));
bellard's avatar
bellard committed
537
538
539
540
541
    RETURN();
}

void op_srlv (void)
{
ths's avatar
ths committed
542
    T0 = (int32_t)((uint32_t)T1 >> (T0 & 0x1F));
bellard's avatar
bellard committed
543
544
545
    RETURN();
}

546
547
548
549
550
551
void op_rotrv (void)
{
    target_ulong tmp;

    T0 &= 0x1F;
    if (T0) {
ths's avatar
ths committed
552
553
       tmp = (int32_t)((uint32_t)T1 << (0x20 - T0));
       T0 = (int32_t)((uint32_t)T1 >> T0) | tmp;
554
555
556
557
558
    } else
       T0 = T1;
    RETURN();
}

bellard's avatar
bellard committed
559
560
561
562
void op_clo (void)
{
    int n;

563
    if (T0 == ~((target_ulong)0)) {
bellard's avatar
bellard committed
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
        T0 = 32;
    } else {
        for (n = 0; n < 32; n++) {
            if (!(T0 & (1 << 31)))
                break;
            T0 = T0 << 1;
        }
        T0 = n;
    }
    RETURN();
}

void op_clz (void)
{
    int n;

    if (T0 == 0) {
        T0 = 32;
    } else {
        for (n = 0; n < 32; n++) {
            if (T0 & (1 << 31))
                break;
            T0 = T0 << 1;
        }
        T0 = n;
    }
    RETURN();
}

593
594
595
596
597
#ifdef MIPS_HAS_MIPS64

#if TARGET_LONG_BITS > HOST_LONG_BITS
/* Those might call libgcc functions.  */
void op_dsll (void)
bellard's avatar
bellard committed
598
{
599
600
    CALL_FROM_TB0(do_dsll);
    RETURN();
bellard's avatar
bellard committed
601
602
}

603
void op_dsll32 (void)
bellard's avatar
bellard committed
604
{
605
606
    CALL_FROM_TB0(do_dsll32);
    RETURN();
bellard's avatar
bellard committed
607
608
}

609
void op_dsra (void)
bellard's avatar
bellard committed
610
{
611
    CALL_FROM_TB0(do_dsra);
bellard's avatar
bellard committed
612
613
614
    RETURN();
}

615
void op_dsra32 (void)
bellard's avatar
bellard committed
616
{
617
    CALL_FROM_TB0(do_dsra32);
bellard's avatar
bellard committed
618
619
620
    RETURN();
}

621
void op_dsrl (void)
bellard's avatar
bellard committed
622
{
623
624
625
    CALL_FROM_TB0(do_dsrl);
    RETURN();
}
bellard's avatar
bellard committed
626

627
628
629
void op_dsrl32 (void)
{
    CALL_FROM_TB0(do_dsrl32);
bellard's avatar
bellard committed
630
631
632
    RETURN();
}

633
void op_drotr (void)
bellard's avatar
bellard committed
634
{
635
636
637
    CALL_FROM_TB0(do_drotr);
    RETURN();
}
bellard's avatar
bellard committed
638

639
640
641
void op_drotr32 (void)
{
    CALL_FROM_TB0(do_drotr32);
bellard's avatar
bellard committed
642
643
644
    RETURN();
}

645
void op_dsllv (void)
bellard's avatar
bellard committed
646
{
647
648
649
    CALL_FROM_TB0(do_dsllv);
    RETURN();
}
bellard's avatar
bellard committed
650

651
652
653
void op_dsrav (void)
{
    CALL_FROM_TB0(do_dsrav);
bellard's avatar
bellard committed
654
655
656
    RETURN();
}

657
void op_dsrlv (void)
bellard's avatar
bellard committed
658
{
659
660
661
    CALL_FROM_TB0(do_dsrlv);
    RETURN();
}
bellard's avatar
bellard committed
662

663
664
665
void op_drotrv (void)
{
    CALL_FROM_TB0(do_drotrv);
bellard's avatar
bellard committed
666
667
    RETURN();
}
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799

#else /* TARGET_LONG_BITS > HOST_LONG_BITS */

void op_dsll (void)
{
    T0 = T0 << T1;
    RETURN();
}

void op_dsll32 (void)
{
    T0 = T0 << (T1 + 32);
    RETURN();
}

void op_dsra (void)
{
    T0 = (int64_t)T0 >> T1;
    RETURN();
}

void op_dsra32 (void)
{
    T0 = (int64_t)T0 >> (T1 + 32);
    RETURN();
}

void op_dsrl (void)
{
    T0 = T0 >> T1;
    RETURN();
}

void op_dsrl32 (void)
{
    T0 = T0 >> (T1 + 32);
    RETURN();
}

void op_drotr (void)
{
    target_ulong tmp;

    if (T1) {
       tmp = T0 << (0x40 - T1);
       T0 = (T0 >> T1) | tmp;
    } else
       T0 = T1;
    RETURN();
}

void op_drotr32 (void)
{
    target_ulong tmp;

    if (T1) {
       tmp = T0 << (0x40 - (32 + T1));
       T0 = (T0 >> (32 + T1)) | tmp;
    } else
       T0 = T1;
    RETURN();
}

void op_dsllv (void)
{
    T0 = T1 << (T0 & 0x3F);
    RETURN();
}

void op_dsrav (void)
{
    T0 = (int64_t)T1 >> (T0 & 0x3F);
    RETURN();
}

void op_dsrlv (void)
{
    T0 = T1 >> (T0 & 0x3F);
    RETURN();
}

void op_drotrv (void)
{
    target_ulong tmp;

    T0 &= 0x3F;
    if (T0) {
       tmp = T1 << (0x40 - T0);
       T0 = (T1 >> T0) | tmp;
    } else
       T0 = T1;
    RETURN();
}
#endif /* TARGET_LONG_BITS > HOST_LONG_BITS */

void op_dclo (void)
{
    int n;

    if (T0 == ~((target_ulong)0)) {
        T0 = 64;
    } else {
        for (n = 0; n < 64; n++) {
            if (!(T0 & (1ULL << 63)))
                break;
            T0 = T0 << 1;
        }
        T0 = n;
    }
    RETURN();
}

void op_dclz (void)
{
    int n;

    if (T0 == 0) {
        T0 = 64;
    } else {
        for (n = 0; n < 64; n++) {
            if (T0 & (1ULL << 63))
                break;
            T0 = T0 << 1;
        }
        T0 = n;
    }
    RETURN();
}
#endif

/* 64 bits arithmetic */
#if TARGET_LONG_BITS > HOST_LONG_BITS
bellard's avatar
bellard committed
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
void op_mult (void)
{
    CALL_FROM_TB0(do_mult);
    RETURN();
}

void op_multu (void)
{
    CALL_FROM_TB0(do_multu);
    RETURN();
}

void op_madd (void)
{
    CALL_FROM_TB0(do_madd);
    RETURN();
}

void op_maddu (void)
{
    CALL_FROM_TB0(do_maddu);
    RETURN();
}

void op_msub (void)
{
    CALL_FROM_TB0(do_msub);
    RETURN();
}

void op_msubu (void)
{
    CALL_FROM_TB0(do_msubu);
    RETURN();
}
835
836
837
838
839
840
841
842
843
844

#else /* TARGET_LONG_BITS > HOST_LONG_BITS */

static inline uint64_t get_HILO (void)
{
    return ((uint64_t)env->HI << 32) | ((uint64_t)(uint32_t)env->LO);
}

static inline void set_HILO (uint64_t HILO)
{
ths's avatar
ths committed
845
846
    env->LO = (int32_t)(HILO & 0xFFFFFFFF);
    env->HI = (int32_t)(HILO >> 32);
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
}

void op_mult (void)
{
    set_HILO((int64_t)(int32_t)T0 * (int64_t)(int32_t)T1);
    RETURN();
}

void op_multu (void)
{
    set_HILO((uint64_t)(uint32_t)T0 * (uint64_t)(uint32_t)T1);
    RETURN();
}

void op_madd (void)
{
    int64_t tmp;

    tmp = ((int64_t)(int32_t)T0 * (int64_t)(int32_t)T1);
    set_HILO((int64_t)get_HILO() + tmp);
    RETURN();
}

void op_maddu (void)
{
    uint64_t tmp;

    tmp = ((uint64_t)(uint32_t)T0 * (uint64_t)(uint32_t)T1);
    set_HILO(get_HILO() + tmp);
    RETURN();
}

void op_msub (void)
{
    int64_t tmp;

    tmp = ((int64_t)(int32_t)T0 * (int64_t)(int32_t)T1);
    set_HILO((int64_t)get_HILO() - tmp);
    RETURN();
}

void op_msubu (void)
{
    uint64_t tmp;

    tmp = ((uint64_t)(uint32_t)T0 * (uint64_t)(uint32_t)T1);
    set_HILO(get_HILO() - tmp);
    RETURN();
}
#endif /* TARGET_LONG_BITS > HOST_LONG_BITS */

#ifdef MIPS_HAS_MIPS64
void op_dmult (void)
{
    CALL_FROM_TB0(do_dmult);
    RETURN();
}

void op_dmultu (void)
{
    CALL_FROM_TB0(do_dmultu);
    RETURN();
}
bellard's avatar
bellard committed
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
#endif

/* Conditional moves */
void op_movn (void)
{
    if (T1 != 0)
        env->gpr[PARAM1] = T0;
    RETURN();
}

void op_movz (void)
{
    if (T1 == 0)
        env->gpr[PARAM1] = T0;
    RETURN();
}

927
#ifdef MIPS_USES_FPU
928
929
930
931
932
933
934
935
936
937
938
939
940
void op_movf (void)
{
    if (!(env->fcr31 & PARAM1))
        env->gpr[PARAM2] = env->gpr[PARAM3];
    RETURN();
}

void op_movt (void)
{
    if (env->fcr31 & PARAM1)
        env->gpr[PARAM2] = env->gpr[PARAM3];
    RETURN();
}
941
#endif
942

bellard's avatar
bellard committed
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
/* Tests */
#define OP_COND(name, cond) \
void glue(op_, name) (void) \
{                           \
    if (cond) {             \
        T0 = 1;             \
    } else {                \
        T0 = 0;             \
    }                       \
    RETURN();               \
}

OP_COND(eq, T0 == T1);
OP_COND(ne, T0 != T1);
OP_COND(ge, (int32_t)T0 >= (int32_t)T1);
OP_COND(geu, T0 >= T1);
OP_COND(lt, (int32_t)T0 < (int32_t)T1);
OP_COND(ltu, T0 < T1);
OP_COND(gez, (int32_t)T0 >= 0);
OP_COND(gtz, (int32_t)T0 > 0);
OP_COND(lez, (int32_t)T0 <= 0);
OP_COND(ltz, (int32_t)T0 < 0);

966
/* Branches */
bellard's avatar
bellard committed
967
//#undef USE_DIRECT_JUMP
bellard's avatar
bellard committed
968
969
970
971

void OPPROTO op_goto_tb0(void)
{
    GOTO_TB(op_goto_tb0, PARAM1, 0);
972
    RETURN();
bellard's avatar
bellard committed
973
974
975
976
977
}

void OPPROTO op_goto_tb1(void)
{
    GOTO_TB(op_goto_tb1, PARAM1, 1);
978
    RETURN();
bellard's avatar
bellard committed
979
}
bellard's avatar
bellard committed
980
981
982
983
984

/* Branch to register */
void op_save_breg_target (void)
{
    env->btarget = T2;
985
    RETURN();
bellard's avatar
bellard committed
986
987
988
989
990
}

void op_restore_breg_target (void)
{
    T2 = env->btarget;
991
    RETURN();
bellard's avatar
bellard committed
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
}

void op_breg (void)
{
    env->PC = T2;
    RETURN();
}

void op_save_btarget (void)
{
    env->btarget = PARAM1;
    RETURN();
}

/* Conditional branch */
void op_set_bcond (void)
{
    T2 = T0;
    RETURN();
}

void op_save_bcond (void)
{
    env->bcond = T2;
    RETURN();
}

void op_restore_bcond (void)
{
    T2 = env->bcond;
    RETURN();
}

bellard's avatar
bellard committed
1025
void op_jnz_T2 (void)
bellard's avatar
bellard committed
1026
{
bellard's avatar
bellard committed
1027
1028
    if (T2)
        GOTO_LABEL_PARAM(1);
bellard's avatar
bellard committed
1029
1030
1031
1032
    RETURN();
}

/* CP0 functions */
1033
void op_mfc0_index (void)
bellard's avatar
bellard committed
1034
{
ths's avatar
ths committed
1035
    T0 = (int32_t)(env->CP0_index);
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
    RETURN();
}

void op_mfc0_random (void)
{
    CALL_FROM_TB0(do_mfc0_random);
    RETURN();
}

void op_mfc0_entrylo0 (void)
{
    T0 = env->CP0_EntryLo0;
    RETURN();
}

void op_mfc0_entrylo1 (void)
{
    T0 = env->CP0_EntryLo1;
    RETURN();
}

void op_mfc0_context (void)
{
    T0 = env->CP0_Context;
    RETURN();
}

void op_mfc0_pagemask (void)
{
ths's avatar
ths committed
1065
    T0 = (int32_t)env->CP0_PageMask;
1066
1067
1068
    RETURN();
}

1069
1070
void op_mfc0_pagegrain (void)
{
ths's avatar
ths committed
1071
    T0 = (int32_t)env->CP0_PageGrain;
1072
1073
1074
    RETURN();
}

1075
1076
void op_mfc0_wired (void)
{
ths's avatar
ths committed
1077
    T0 = (int32_t)env->CP0_Wired;
1078
1079
1080
    RETURN();
}

1081
1082
void op_mfc0_hwrena (void)
{
ths's avatar
ths committed
1083
    T0 = (int32_t)env->CP0_HWREna;
1084
1085
1086
    RETURN();
}

1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
void op_mfc0_badvaddr (void)
{
    T0 = env->CP0_BadVAddr;
    RETURN();
}

void op_mfc0_count (void)
{
    CALL_FROM_TB0(do_mfc0_count);
    RETURN();
}

void op_mfc0_entryhi (void)
{
    T0 = env->CP0_EntryHi;
    RETURN();
}

void op_mfc0_compare (void)
{
ths's avatar
ths committed
1107
    T0 = (int32_t)env->CP0_Compare;
1108
1109
1110
1111
1112
    RETURN();
}

void op_mfc0_status (void)
{
ths's avatar
ths committed
1113
    T0 = (int32_t)env->CP0_Status;
1114
1115
1116
1117
1118
1119
1120
1121
1122
    if (env->hflags & MIPS_HFLAG_UM)
        T0 |= (1 << CP0St_UM);
    if (env->hflags & MIPS_HFLAG_ERL)
        T0 |= (1 << CP0St_ERL);
    if (env->hflags & MIPS_HFLAG_EXL)
        T0 |= (1 << CP0St_EXL);
    RETURN();
}

1123
1124
void op_mfc0_intctl (void)
{
ths's avatar
ths committed
1125
    T0 = (int32_t)env->CP0_IntCtl;
1126
1127
1128
1129
1130
    RETURN();
}

void op_mfc0_srsctl (void)
{
ths's avatar
ths committed
1131
    T0 = (int32_t)env->CP0_SRSCtl;
1132
1133
1134
    RETURN();
}

1135
1136
void op_mfc0_cause (void)
{
ths's avatar
ths committed
1137
    T0 = (int32_t)env->CP0_Cause;
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
    RETURN();
}

void op_mfc0_epc (void)
{
    T0 = env->CP0_EPC;
    RETURN();
}

void op_mfc0_prid (void)
{
ths's avatar
ths committed
1149
    T0 = (int32_t)env->CP0_PRid;
1150
1151
1152
    RETURN();
}

1153
1154
1155
1156
1157
1158
void op_mfc0_ebase (void)
{
    T0 = env->CP0_EBase;
    RETURN();
}

1159
1160
void op_mfc0_config0 (void)
{
ths's avatar
ths committed
1161
    T0 = (int32_t)env->CP0_Config0;
1162
1163
1164
1165
1166
    RETURN();
}

void op_mfc0_config1 (void)
{
ths's avatar
ths committed
1167
    T0 = (int32_t)env->CP0_Config1;
1168
1169
1170
    RETURN();
}

1171
1172
void op_mfc0_config2 (void)
{
ths's avatar
ths committed
1173
    T0 = (int32_t)env->CP0_Config2;
1174
1175
1176
1177
1178
    RETURN();
}

void op_mfc0_config3 (void)
{
ths's avatar
ths committed
1179
    T0 = (int32_t)env->CP0_Config3;
1180
1181
1182
    RETURN();
}

1183
1184
1185
1186
1187
1188
void op_mfc0_lladdr (void)
{
    T0 = env->CP0_LLAddr >> 4;
    RETURN();
}

1189
void op_mfc0_watchlo0 (void)
1190
{
ths's avatar
ths committed
1191
    T0 = (int32_t)env->CP0_WatchLo;
1192
1193
1194
    RETURN();
}

1195
void op_mfc0_watchhi0 (void)
1196
{
ths's avatar
ths committed
1197
    T0 = (int32_t)env->CP0_WatchHi;
1198
1199
1200
    RETURN();
}

1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
void op_mfc0_xcontext (void)
{
    T0 = env->CP0_XContext;
    RETURN();
}

void op_mfc0_framemask (void)
{
    T0 = env->CP0_Framemask;
    RETURN();
}

1213
1214
void op_mfc0_debug (void)
{
ths's avatar
ths committed
1215
    T0 = (int32_t)env->CP0_Debug;
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
    if (env->hflags & MIPS_HFLAG_DM)
        T0 |= 1 << CP0DB_DM;
    RETURN();
}

void op_mfc0_depc (void)
{
    T0 = env->CP0_DEPC;
    RETURN();
}

1227
1228
void op_mfc0_performance0 (void)
{
ths's avatar
ths committed
1229
    T0 = (int32_t)env->CP0_Performance0;
1230
1231
1232
    RETURN();
}

1233
1234
void op_mfc0_taglo (void)
{
ths's avatar
ths committed
1235
    T0 = (int32_t)env->CP0_TagLo;
1236
1237
1238
1239
1240
    RETURN();
}

void op_mfc0_datalo (void)
{
ths's avatar
ths committed
1241
    T0 = (int32_t)env->CP0_DataLo;
1242
1243
1244
    RETURN();
}

1245
1246
void op_mfc0_taghi (void)
{
ths's avatar
ths committed
1247
    T0 = (int32_t)env->CP0_TagHi;
1248
1249
1250
1251
1252
    RETURN();
}

void op_mfc0_datahi (void)
{
ths's avatar
ths committed
1253
    T0 = (int32_t)env->CP0_DataHi;
1254
1255
1256
    RETURN();
}

1257
1258
1259
1260
1261
1262
1263
1264
void op_mfc0_errorepc (void)
{
    T0 = env->CP0_ErrorEPC;
    RETURN();
}

void op_mfc0_desave (void)
{
ths's avatar
ths committed
1265
    T0 = (int32_t)env->CP0_DESAVE;
bellard's avatar
bellard committed
1266
1267
1268
    RETURN();
}

1269
void op_mtc0_index (void)
bellard's avatar
bellard committed
1270
{
1271
    env->CP0_index = (env->CP0_index & 0x80000000) | (T0 & (MIPS_TLB_NB - 1));
1272
1273
1274
1275
1276
    RETURN();
}

void op_mtc0_entrylo0 (void)
{
1277
1278
    /* Large physaddr not implemented */
    /* 1k pages not implemented */
ths's avatar
ths committed
1279
    env->CP0_EntryLo0 = T0 & (int32_t)0x3FFFFFFF;
1280
1281
1282
1283
1284
    RETURN();
}

void op_mtc0_entrylo1 (void)
{
1285
1286
    /* Large physaddr not implemented */
    /* 1k pages not implemented */
ths's avatar
ths committed
1287
    env->CP0_EntryLo1 = T0 & (int32_t)0x3FFFFFFF;
1288
1289
1290
1291
1292
    RETURN();
}

void op_mtc0_context (void)
{
1293
    env->CP0_Context = (env->CP0_Context & ~0x007FFFFF) | (T0 & 0x007FFFF0);
1294
1295
1296
1297
1298
    RETURN();
}

void op_mtc0_pagemask (void)
{
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
    /* 1k pages not implemented */
    env->CP0_PageMask = T0 & 0x1FFFE000;
    RETURN();
}

void op_mtc0_pagegrain (void)
{
    /* SmartMIPS not implemented */
    /* Large physaddr not implemented */
    /* 1k pages not implemented */
    env->CP0_PageGrain = 0;
1310
1311
1312
1313
1314
    RETURN();
}

void op_mtc0_wired (void)
{
1315
1316
1317
1318
1319
1320
1321
    env->CP0_Wired = T0 & (MIPS_TLB_NB - 1);
    RETURN();
}

void op_mtc0_hwrena (void)
{
    env->CP0_HWREna = T0 & 0x0000000F;
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
    RETURN();
}

void op_mtc0_count (void)
{
    CALL_FROM_TB2(cpu_mips_store_count, env, T0);
    RETURN();
}

void op_mtc0_entryhi (void)
{
    uint32_t old, val;

1335
1336
    /* 1k pages not implemented */
    /* Ignore MIPS64 TLB for now */
ths's avatar
ths committed
1337
    val = T0 & (int32_t)0xFFFFE0FF;
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
    old = env->CP0_EntryHi;
    env->CP0_EntryHi = val;
    /* If the ASID changes, flush qemu's TLB.  */
    if ((old & 0xFF) != (val & 0xFF))
        CALL_FROM_TB2(cpu_mips_tlb_flush, env, 1);
    RETURN();
}

void op_mtc0_compare (void)
{
    CALL_FROM_TB2(cpu_mips_store_compare, env, T0);
    RETURN();
}

void op_mtc0_status (void)
{
    uint32_t val, old, mask;

ths's avatar
ths committed
1356
    val = T0 & (int32_t)0xFA78FF01;
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
    old = env->CP0_Status;
    if (T0 & (1 << CP0St_UM))
        env->hflags |= MIPS_HFLAG_UM;
    else
        env->hflags &= ~MIPS_HFLAG_UM;
    if (T0 & (1 << CP0St_ERL))
        env->hflags |= MIPS_HFLAG_ERL;
    else
        env->hflags &= ~MIPS_HFLAG_ERL;
    if (T0 & (1 << CP0St_EXL))
        env->hflags |= MIPS_HFLAG_EXL;
    else
        env->hflags &= ~MIPS_HFLAG_EXL;
    env->CP0_Status = val;
    /* If we unmasked an asserted IRQ, raise it */
    mask = 0x0000FF00;
    if (loglevel & CPU_LOG_TB_IN_ASM)
       CALL_FROM_TB2(do_mtc0_status_debug, old, val);
    if ((val & (1 << CP0St_IE)) && !(old & (1 << CP0St_IE)) &&
        !(env->hflags & MIPS_HFLAG_EXL) &&
        !(env->hflags & MIPS_HFLAG_ERL) &&
        !(env->hflags & MIPS_HFLAG_DM) &&
        (env->CP0_Status & env->CP0_Cause & mask)) {
        env->interrupt_request |= CPU_INTERRUPT_HARD;
       if (logfile)
           CALL_FROM_TB0(do_mtc0_status_irqraise_debug);
    } else if (!(val & (1 << CP0St_IE)) && (old & (1 << CP0St_IE))) {
        env->interrupt_request &= ~CPU_INTERRUPT_HARD;
    }
    RETURN();
}

1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
void op_mtc0_intctl (void)
{
    /* vectored interrupts not implemented */
    env->CP0_IntCtl = 0;
    RETURN();
}

void op_mtc0_srsctl (void)
{
    /* shadow registers not implemented */
    env->CP0_SRSCtl = 0;
    RETURN();
}

1403
1404
1405
1406
void op_mtc0_cause (void)
{
    uint32_t val, old;

1407
    val = (env->CP0_Cause & 0xB000F87C) | (T0 & 0x00C00300);
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
    old = env->CP0_Cause;
    env->CP0_Cause = val;
#if 0
    {
        int i, mask;
       /* Check if we ever asserted a software IRQ */
        for (i = 0; i < 2; i++) {
            mask = 0x100 << i;
            if ((val & mask) & !(old & mask))
                CALL_FROM_TB1(mips_set_irq, i);
        }
    }
#endif
    RETURN();
}

void op_mtc0_epc (void)
{
    env->CP0_EPC = T0;
    RETURN();
}

1430
1431
1432
1433
void op_mtc0_ebase (void)
{
    /* vectored interrupts not implemented */
    /* Multi-CPU not implemented */
ths's avatar
ths committed
1434
    env->CP0_EBase = (int32_t)0x80000000 | (T0 & 0x3FFFF000);
1435
1436
1437
    RETURN();
}

1438
1439
1440
void op_mtc0_config0 (void)
{
#if defined(MIPS_USES_R4K_TLB)
1441
1442
     /* Fixed mapping MMU not implemented */
    env->CP0_Config0 = (env->CP0_Config0 & 0x8017FF88) | (T0 & 0x00000001);
1443
#else
1444
    env->CP0_Config0 = (env->CP0_Config0 & 0xFE17FF88) | (T0 & 0x00000001);
1445
1446
1447
1448
#endif
    RETURN();
}

1449
1450
1451
1452
1453
1454
1455
1456
void op_mtc0_config2 (void)
{
    /* tertiary/secondary caches not implemented */
    env->CP0_Config2 = (env->CP0_Config2 & 0x8FFF0FFF);
    RETURN();
}

void op_mtc0_watchlo0 (void)
1457
1458
1459
1460
1461
{
    env->CP0_WatchLo = T0;
    RETURN();
}

1462
void op_mtc0_watchhi0 (void)
1463
1464
1465
1466
1467
{
    env->CP0_WatchHi = T0 & 0x40FF0FF8;
    RETURN();
}

1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
void op_mtc0_xcontext (void)
{
    env->CP0_XContext = T0; /* XXX */
    RETURN();
}

void op_mtc0_framemask (void)
{
    env->CP0_Framemask = T0; /* XXX */
    RETURN();
}

1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
void op_mtc0_debug (void)
{
    env->CP0_Debug = (env->CP0_Debug & 0x8C03FC1F) | (T0 & 0x13300120);
    if (T0 & (1 << CP0DB_DM))
        env->hflags |= MIPS_HFLAG_DM;
    else
        env->hflags &= ~MIPS_HFLAG_DM;
    RETURN();
}

void op_mtc0_depc (void)
{
    env->CP0_DEPC = T0;
    RETURN();
}

1496
1497
1498
1499
1500
1501
void op_mtc0_performance0 (void)
{
    env->CP0_Performance0 = T0; /* XXX */
    RETURN();
}

1502
1503
void op_mtc0_taglo (void)
{
ths's avatar
ths committed
1504
    env->CP0_TagLo = T0 & (int32_t)0xFFFFFCF6;
1505
1506
1507
    RETURN();
}

1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
void op_mtc0_datalo (void)
{
    env->CP0_DataLo = T0; /* XXX */
    RETURN();
}

void op_mtc0_taghi (void)
{
    env->CP0_TagHi = T0; /* XXX */
    RETURN();
}

void op_mtc0_datahi (void)
{
    env->CP0_DataHi = T0; /* XXX */
    RETURN();
}

1526
1527
1528
1529
1530
1531
1532
1533
1534
void op_mtc0_errorepc (void)
{
    env->CP0_ErrorEPC = T0;
    RETURN();
}

void op_mtc0_desave (void)
{
    env->CP0_DESAVE = T0;
bellard's avatar
bellard committed
1535
1536
1537
    RETURN();
}

bellard's avatar
bellard committed
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
#ifdef MIPS_USES_FPU

#if 0
# define DEBUG_FPU_STATE() CALL_FROM_TB1(dump_fpu, env)
#else
# define DEBUG_FPU_STATE() do { } while(0)
#endif

void op_cp1_enabled(void)
{
    if (!(env->CP0_Status & (1 << CP0St_CU1))) {
        CALL_FROM_TB2(do_raise_exception_err, EXCP_CpU, 1);
    }
    RETURN();
}

/* CP1 functions */
void op_cfc1 (void)
{
    if (T1 == 0) {
        T0 = env->fcr0;
    }
    else {
        /* fetch fcr31, masking unused bits */
        T0 = env->fcr31 & 0x0183FFFF;
    }
    DEBUG_FPU_STATE();
    RETURN();
}

/* convert MIPS rounding mode in FCR31 to IEEE library */
unsigned int ieee_rm[] = { 
    float_round_nearest_even,
    float_round_to_zero,
    float_round_up,
    float_round_down
};

#define RESTORE_ROUNDING_MODE \
    set_float_rounding_mode(ieee_rm[env->fcr31 & 3], &env->fp_status)

void op_ctc1 (void)
{
    if (T1 == 0) {
        /* XXX should this throw an exception?
         * don't write to FCR0.
         * env->fcr0 = T0; 
         */
    }
    else {
        /* store new fcr31, masking unused bits */  
        env->fcr31 = T0 & 0x0183FFFF;

        /* set rounding mode */
        RESTORE_ROUNDING_MODE;

#ifndef CONFIG_SOFTFLOAT
        /* no floating point exception for native float */
        SET_FP_ENABLE(env->fcr31, 0);
#endif
    }
    DEBUG_FPU_STATE();
    RETURN();
}

void op_mfc1 (void)
{
    T0 = WT0;
    DEBUG_FPU_STATE();
    RETURN();
}

void op_mtc1 (void)
{
    WT0 = T0;
    DEBUG_FPU_STATE();
    RETURN();
}

/* Float support.
   Single precition routines have a "s" suffix, double precision a
   "d" suffix.  */

#define FLOAT_OP(name, p) void OPPROTO op_float_##name##_##p(void)

1623
1624
1625
1626
1627
1628
FLOAT_OP(cvtd, s)
{
    FDT2 = float32_to_float64(WT0, &env->fp_status);
    DEBUG_FPU_STATE();
    RETURN();
}
bellard's avatar
bellard committed
1629
1630
1631
1632
1633
1634
FLOAT_OP(cvtd, w)
{
    FDT2 = int32_to_float64(WT0, &env->fp_status);
    DEBUG_FPU_STATE();
    RETURN();
}
1635
1636
FLOAT_OP(cvts, d)
{
pbrook's avatar
pbrook committed
1637
    FST2 = float64_to_float32(FDT0, &env->fp_status);
1638
1639
1640
    DEBUG_FPU_STATE();
    RETURN();
}
bellard's avatar
bellard committed
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
FLOAT_OP(cvts, w)
{
    FST2 = int32_to_float32(WT0, &env->fp_status);
    DEBUG_FPU_STATE();
    RETURN();
}
FLOAT_OP(cvtw, s)
{
    WT2 = float32_to_int32(FST0, &env->fp_status);
    DEBUG_FPU_STATE();
    RETURN();
}
FLOAT_OP(cvtw, d)
{
    WT2 = float64_to_int32(FDT0, &env->fp_status);
    DEBUG_FPU_STATE();
    RETURN();
}

FLOAT_OP(roundw, d)
{
    set_float_rounding_mode(float_round_nearest_even, &env->fp_status);
    WT2 = float64_round_to_int(FDT0, &env->fp_status);
    RESTORE_ROUNDING_MODE;

    DEBUG_FPU_STATE();
    RETURN();
}
FLOAT_OP(roundw, s)
{
    set_float_rounding_mode(float_round_nearest_even, &env->fp_status);
    WT2 = float32_round_to_int(FST0, &env->fp_status);
    RESTORE_ROUNDING_MODE;
    DEBUG_FPU_STATE();
    RETURN();
}

FLOAT_OP(truncw, d)
{
    WT2 = float64_to_int32_round_to_zero(FDT0, &env->fp_status);
    DEBUG_FPU_STATE();
    RETURN();
}
FLOAT_OP(truncw, s)
{
    WT2 = float32_to_int32_round_to_zero(FST0, &env->fp_status);
    DEBUG_FPU_STATE();
    RETURN();
}

FLOAT_OP(ceilw, d)
{
    set_float_rounding_mode(float_round_up, &env->fp_status);
    WT2 = float64_round_to_int(FDT0, &env->fp_status);
    RESTORE_ROUNDING_MODE;

    DEBUG_FPU_STATE();
    RETURN();
}
FLOAT_OP(ceilw, s)
{
    set_float_rounding_mode(float_round_up, &env->fp_status);
    WT2 = float32_round_to_int(FST0, &env->fp_status);
    RESTORE_ROUNDING_MODE;
    DEBUG_FPU_STATE();
    RETURN();
}

FLOAT_OP(floorw, d)
{
    set_float_rounding_mode(float_round_down, &env->fp_status);
    WT2 = float64_round_to_int(FDT0, &env->fp_status);
    RESTORE_ROUNDING_MODE;

    DEBUG_FPU_STATE();
    RETURN();
}
FLOAT_OP(floorw, s)
{
    set_float_rounding_mode(float_round_down, &env->fp_status);
    WT2 = float32_round_to_int(FST0, &env->fp_status);
    RESTORE_ROUNDING_MODE;
    DEBUG_FPU_STATE();
    RETURN();
}

/* binary operations */
#define FLOAT_BINOP(name) \
FLOAT_OP(name, d)         \
{                         \
    FDT2 = float64_ ## name (FDT0, FDT1, &env->fp_status);    \
    DEBUG_FPU_STATE();    \
}                         \
FLOAT_OP(name, s)         \
{                         \
    FST2 = float32_ ## name (FST0, FST1, &env->fp_status);    \
    DEBUG_FPU_STATE();    \
}
FLOAT_BINOP(add)
FLOAT_BINOP(sub)
FLOAT_BINOP(mul)
FLOAT_BINOP(div)
#undef FLOAT_BINOP

/* unary operations, modifying fp status  */
#define FLOAT_UNOP(name)  \
FLOAT_OP(name, d)         \
{                         \
    FDT2 = float64_ ## name(FDT0, &env->fp_status);   \
    DEBUG_FPU_STATE();    \
}                         \
FLOAT_OP(name, s)         \
{                         \
    FST2 = float32_ ## name(FST0, &env->fp_status);   \
    DEBUG_FPU_STATE();    \
}
FLOAT_UNOP(sqrt)
#undef FLOAT_UNOP

/* unary operations, not modifying fp status  */
#define FLOAT_UNOP(name)  \
FLOAT_OP(name, d)         \
{                         \
    FDT2 = float64_ ## name(FDT0);   \
    DEBUG_FPU_STATE();    \
}                         \
FLOAT_OP(name, s)         \
{                         \
    FST2 = float32_ ## name(FST0);   \
    DEBUG_FPU_STATE();    \
}
FLOAT_UNOP(abs)
FLOAT_UNOP(chs)
#undef FLOAT_UNOP

FLOAT_OP(mov, d)
{
    FDT2 = FDT0;
    DEBUG_FPU_STATE();
    RETURN();
}
FLOAT_OP(mov, s)
{
    FST2 = FST0;
    DEBUG_FPU_STATE();
    RETURN();
}

#ifdef CONFIG_SOFTFLOAT
#define clear_invalid() do {                                \
    int flags = get_float_exception_flags(&env->fp_status); \
    flags &= ~float_flag_invalid;                           \
    set_float_exception_flags(flags, &env->fp_status);      \
} while(0)
#else
#define clear_invalid() do { } while(0)
#endif

extern void dump_fpu_s(CPUState *env);

#define FOP_COND(fmt, op, sig, cond)           \
void op_cmp_ ## fmt ## _ ## op (void)          \
{                                              \
    if (cond)                                  \
        SET_FP_COND(env->fcr31);               \
    else                                       \
        CLEAR_FP_COND(env->fcr31);             \
    if (!sig)                                  \
        clear_invalid();                       \
    /*CALL_FROM_TB1(dump_fpu_s, env);*/ \
    DEBUG_FPU_STATE();                         \
    RETURN();                                  \
}

bellard's avatar
bellard committed
1815
int float64_is_unordered(float64 a, float64 b STATUS_PARAM)
bellard's avatar
bellard committed
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
{
    if (float64_is_nan(a) || float64_is_nan(b)) {
        float_raise(float_flag_invalid, status);
        return 1;
    }
    else {
        return 0;
    }
}

FOP_COND(d, f,   0,                                                      0) 
FOP_COND(d, un,  0, float64_is_unordered(FDT1, FDT0, &env->fp_status))
FOP_COND(d, eq,  0,                                                      float64_eq(FDT0, FDT1, &env->fp_status))
FOP_COND(d, ueq, 0, float64_is_unordered(FDT1, FDT0, &env->fp_status) || float64_eq(FDT0, FDT1, &env->fp_status))
FOP_COND(d, olt, 0,                                                      float64_lt(FDT0, FDT1, &env->fp_status))
FOP_COND(d, ult, 0, float64_is_unordered(FDT1, FDT0, &env->fp_status) || float64_lt(FDT0, FDT1, &env->fp_status))
FOP_COND(d, ole, 0,                                                      float64_le(FDT0, FDT1, &env->fp_status))
FOP_COND(d, ule, 0, float64_is_unordered(FDT1, FDT0, &env->fp_status) || float64_le(FDT0, FDT1, &env->fp_status))
/* NOTE: the comma operator will make "cond" to eval to false,
 * but float*_is_unordered() is still called
 */
FOP_COND(d, sf,  1,                                                      (float64_is_unordered(FDT0, FDT1, &env->fp_status), 0))
FOP_COND(d, ngle,1, float64_is_unordered(FDT1, FDT0, &env->fp_status))
FOP_COND(d, seq, 1,                                                      float64_eq(FDT0, FDT1, &env->fp_status))
FOP_COND(d, ngl, 1, float64_is_unordered(FDT1, FDT0, &env->fp_status) || float64_eq(FDT0, FDT1, &env->fp_status))
FOP_COND(d, lt,  1,                                                      float64_lt(FDT0, FDT1, &env->fp_status))
FOP_COND(d, nge, 1, float64_is_unordered(FDT1, FDT0, &env->fp_status) || float64_lt(FDT0, FDT1, &env->fp_status))
FOP_COND(d, le,  1,                                                      float64_le(FDT0, FDT1, &env->fp_status))
FOP_COND(d, ngt, 1, float64_is_unordered(FDT1, FDT0, &env->fp_status) || float64_le(FDT0, FDT1, &env->fp_status))

flag float32_is_unordered(float32 a, float32 b STATUS_PARAM)
{
    extern flag float32_is_nan( float32 a );
    if (float32_is_nan(a) || float32_is_nan(b)) {
        float_raise(float_flag_invalid, status);
        return 1;
    }
    else {
        return 0;
    }
}

/* NOTE: the comma operator will make "cond" to eval to false,
 * but float*_is_unordered() is still called
 */
FOP_COND(s, f,   0,                                                      0) 
FOP_COND(s, un,  0, float32_is_unordered(FST1, FST0, &env->fp_status))
FOP_COND(s, eq,  0,                                                      float32_eq(FST0, FST1, &env->fp_status))
FOP_COND(s, ueq, 0, float32_is_unordered(FST1, FST0, &env->fp_status) || float32_eq(FST0, FST1, &env->fp_status))
FOP_COND(s, olt, 0,                                                      float32_lt(FST0, FST1, &env->fp_status))
FOP_COND(s, ult, 0, float32_is_unordered(FST1, FST0, &env->fp_status) || float32_lt(FST0, FST1, &env->fp_status))
FOP_COND(s, ole, 0,                                                      float32_le(FST0, FST1, &env->fp_status))
FOP_COND(s, ule, 0, float32_is_unordered(FST1, FST0, &env->fp_status) || float32_le(FST0, FST1, &env->fp_status))
/* NOTE: the comma operator will make "cond" to eval to false,
 * but float*_is_unordered() is still called
 */
FOP_COND(s, sf,  1,                                                      (float32_is_unordered(FST0, FST1, &env->fp_status), 0))
FOP_COND(s, ngle,1, float32_is_unordered(FST1, FST0, &env->fp_status))
FOP_COND(s, seq, 1,                                                      float32_eq(FST0, FST1, &env->fp_status))
FOP_COND(s, ngl, 1, float32_is_unordered(FST1, FST0, &env->fp_status) || float32_eq(FST0, FST1, &env->fp_status))
FOP_COND(s, lt,  1,                                                      float32_lt(FST0, FST1, &env->fp_status))
FOP_COND(s, nge, 1, float32_is_unordered(FST1, FST0, &env->fp_status) || float32_lt(FST0, FST1, &env->fp_status))
FOP_COND(s, le,  1,                                                      float32_le(FST0, FST1, &env->fp_status))
FOP_COND(s, ngt, 1, float32_is_unordered(FST1, FST0, &env->fp_status) || float32_le(FST0, FST1, &env->fp_status))

void op_bc1f (void)
{
    T0 = ! IS_FP_COND_SET(env->fcr31);
    DEBUG_FPU_STATE();
    RETURN();
}

void op_bc1t (void)
{
    T0 = IS_FP_COND_SET(env->fcr31);
    DEBUG_FPU_STATE();
    RETURN();
}
#endif /* MIPS_USES_FPU */

bellard's avatar
bellard committed
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
#if defined(MIPS_USES_R4K_TLB)
void op_tlbwi (void)
{
    CALL_FROM_TB0(do_tlbwi);
    RETURN();
}

void op_tlbwr (void)
{
    CALL_FROM_TB0(do_tlbwr);
    RETURN();
}

void op_tlbp (void)
{
    CALL_FROM_TB0(do_tlbp);
    RETURN();
}

void op_tlbr (void)
{
    CALL_FROM_TB0(do_tlbr);
    RETURN();
}
#endif

/* Specials */
void op_pmon (void)
{
    CALL_FROM_TB1(do_pmon, PARAM1);