op.c 20.0 KB
Newer Older
B
bellard 已提交
1 2 3 4
/*
 *  ARM micro operations
 * 
 *  Copyright (c) 2003 Fabrice Bellard
B
bellard 已提交
5
 *  Copyright (c) 2005 CodeSourcery, LLC
B
bellard 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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
 *
 * 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 "exec.h"

#define REGNAME r0
#define REG (env->regs[0])
#include "op_template.h"

#define REGNAME r1
#define REG (env->regs[1])
#include "op_template.h"

#define REGNAME r2
#define REG (env->regs[2])
#include "op_template.h"

#define REGNAME r3
#define REG (env->regs[3])
#include "op_template.h"

#define REGNAME r4
#define REG (env->regs[4])
#include "op_template.h"

#define REGNAME r5
#define REG (env->regs[5])
#include "op_template.h"

#define REGNAME r6
#define REG (env->regs[6])
#include "op_template.h"

#define REGNAME r7
#define REG (env->regs[7])
#include "op_template.h"

#define REGNAME r8
#define REG (env->regs[8])
#include "op_template.h"

#define REGNAME r9
#define REG (env->regs[9])
#include "op_template.h"

#define REGNAME r10
#define REG (env->regs[10])
#include "op_template.h"

#define REGNAME r11
#define REG (env->regs[11])
#include "op_template.h"

#define REGNAME r12
#define REG (env->regs[12])
#include "op_template.h"

#define REGNAME r13
#define REG (env->regs[13])
#include "op_template.h"

#define REGNAME r14
#define REG (env->regs[14])
#include "op_template.h"

#define REGNAME r15
#define REG (env->regs[15])
B
bellard 已提交
85
#define SET_REG(x) REG = x & ~(uint32_t)1
B
bellard 已提交
86 87
#include "op_template.h"

B
bellard 已提交
88 89 90 91 92 93
void OPPROTO op_bx_T0(void)
{
  env->regs[15] = T0 & ~(uint32_t)1;
  env->thumb = (T0 & 1) != 0;
}

B
bellard 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
void OPPROTO op_movl_T0_0(void)
{
    T0 = 0;
}

void OPPROTO op_movl_T0_im(void)
{
    T0 = PARAM1;
}

void OPPROTO op_movl_T1_im(void)
{
    T1 = PARAM1;
}

B
bellard 已提交
109 110 111 112 113
void OPPROTO op_mov_CF_T1(void)
{
    env->CF = ((uint32_t)T1) >> 31;
}

B
bellard 已提交
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 143 144 145 146 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
void OPPROTO op_movl_T2_im(void)
{
    T2 = PARAM1;
}

void OPPROTO op_addl_T1_im(void)
{
    T1 += PARAM1;
}

void OPPROTO op_addl_T1_T2(void)
{
    T1 += T2;
}

void OPPROTO op_subl_T1_T2(void)
{
    T1 -= T2;
}

void OPPROTO op_addl_T0_T1(void)
{
    T0 += T1;
}

void OPPROTO op_addl_T0_T1_cc(void)
{
    unsigned int src1;
    src1 = T0;
    T0 += T1;
    env->NZF = T0;
    env->CF = T0 < src1;
    env->VF = (src1 ^ T1 ^ -1) & (src1 ^ T0);
}

void OPPROTO op_adcl_T0_T1(void)
{
    T0 += T1 + env->CF;
}

void OPPROTO op_adcl_T0_T1_cc(void)
{
    unsigned int src1;
    src1 = T0;
    if (!env->CF) {
        T0 += T1;
        env->CF = T0 < src1;
    } else {
        T0 += T1 + 1;
        env->CF = T0 <= src1;
    }
    env->VF = (src1 ^ T1 ^ -1) & (src1 ^ T0);
    env->NZF = T0;
    FORCE_RET();
}

#define OPSUB(sub, sbc, res, T0, T1)            \
                                                \
void OPPROTO op_ ## sub ## l_T0_T1(void)        \
{                                               \
    res = T0 - T1;                              \
}                                               \
                                                \
void OPPROTO op_ ## sub ## l_T0_T1_cc(void)     \
{                                               \
    unsigned int src1;                          \
    src1 = T0;                                  \
    T0 -= T1;                                   \
    env->NZF = T0;                              \
    env->CF = src1 >= T1;                       \
    env->VF = (src1 ^ T1) & (src1 ^ T0);        \
    res = T0;                                   \
}                                               \
                                                \
void OPPROTO op_ ## sbc ## l_T0_T1(void)        \
{                                               \
    res = T0 - T1 + env->CF - 1;                \
}                                               \
                                                \
void OPPROTO op_ ## sbc ## l_T0_T1_cc(void)     \
{                                               \
    unsigned int src1;                          \
    src1 = T0;                                  \
    if (!env->CF) {                             \
        T0 = T0 - T1 - 1;                       \
B
bellard 已提交
199
        env->CF = src1 > T1;                    \
B
bellard 已提交
200 201
    } else {                                    \
        T0 = T0 - T1;                           \
B
bellard 已提交
202
        env->CF = src1 >= T1;                   \
B
bellard 已提交
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
    }                                           \
    env->VF = (src1 ^ T1) & (src1 ^ T0);        \
    env->NZF = T0;                              \
    res = T0;                                   \
    FORCE_RET();                                \
}

OPSUB(sub, sbc, T0, T0, T1)

OPSUB(rsb, rsc, T0, T1, T0)

void OPPROTO op_andl_T0_T1(void)
{
    T0 &= T1;
}

void OPPROTO op_xorl_T0_T1(void)
{
    T0 ^= T1;
}

void OPPROTO op_orl_T0_T1(void)
{
    T0 |= T1;
}

void OPPROTO op_bicl_T0_T1(void)
{
    T0 &= ~T1;
}

void OPPROTO op_notl_T1(void)
{
    T1 = ~T1;
}

void OPPROTO op_logic_T0_cc(void)
{
    env->NZF = T0;
}

void OPPROTO op_logic_T1_cc(void)
{
    env->NZF = T1;
}

#define EIP (env->regs[15])

void OPPROTO op_test_eq(void)
{
    if (env->NZF == 0)
254
        GOTO_LABEL_PARAM(1);;
B
bellard 已提交
255 256 257 258 259 260
    FORCE_RET();
}

void OPPROTO op_test_ne(void)
{
    if (env->NZF != 0)
261
        GOTO_LABEL_PARAM(1);;
B
bellard 已提交
262 263 264 265 266 267
    FORCE_RET();
}

void OPPROTO op_test_cs(void)
{
    if (env->CF != 0)
268
        GOTO_LABEL_PARAM(1);
B
bellard 已提交
269 270 271 272 273 274
    FORCE_RET();
}

void OPPROTO op_test_cc(void)
{
    if (env->CF == 0)
275
        GOTO_LABEL_PARAM(1);
B
bellard 已提交
276 277 278 279 280 281
    FORCE_RET();
}

void OPPROTO op_test_mi(void)
{
    if ((env->NZF & 0x80000000) != 0)
282
        GOTO_LABEL_PARAM(1);
B
bellard 已提交
283 284 285 286 287 288
    FORCE_RET();
}

void OPPROTO op_test_pl(void)
{
    if ((env->NZF & 0x80000000) == 0)
289
        GOTO_LABEL_PARAM(1);
B
bellard 已提交
290 291 292 293 294 295
    FORCE_RET();
}

void OPPROTO op_test_vs(void)
{
    if ((env->VF & 0x80000000) != 0)
296
        GOTO_LABEL_PARAM(1);
B
bellard 已提交
297 298 299 300 301 302
    FORCE_RET();
}

void OPPROTO op_test_vc(void)
{
    if ((env->VF & 0x80000000) == 0)
303
        GOTO_LABEL_PARAM(1);
B
bellard 已提交
304 305 306 307 308 309
    FORCE_RET();
}

void OPPROTO op_test_hi(void)
{
    if (env->CF != 0 && env->NZF != 0)
310
        GOTO_LABEL_PARAM(1);
B
bellard 已提交
311 312 313 314 315 316
    FORCE_RET();
}

void OPPROTO op_test_ls(void)
{
    if (env->CF == 0 || env->NZF == 0)
317
        GOTO_LABEL_PARAM(1);
B
bellard 已提交
318 319 320 321 322 323
    FORCE_RET();
}

void OPPROTO op_test_ge(void)
{
    if (((env->VF ^ env->NZF) & 0x80000000) == 0)
324
        GOTO_LABEL_PARAM(1);
B
bellard 已提交
325 326 327 328 329 330
    FORCE_RET();
}

void OPPROTO op_test_lt(void)
{
    if (((env->VF ^ env->NZF) & 0x80000000) != 0)
331
        GOTO_LABEL_PARAM(1);
B
bellard 已提交
332 333 334 335 336 337
    FORCE_RET();
}

void OPPROTO op_test_gt(void)
{
    if (env->NZF != 0 && ((env->VF ^ env->NZF) & 0x80000000) == 0)
338
        GOTO_LABEL_PARAM(1);
B
bellard 已提交
339 340 341 342 343 344
    FORCE_RET();
}

void OPPROTO op_test_le(void)
{
    if (env->NZF == 0 || ((env->VF ^ env->NZF) & 0x80000000) != 0)
345
        GOTO_LABEL_PARAM(1);
B
bellard 已提交
346 347 348
    FORCE_RET();
}

349
void OPPROTO op_jmp0(void)
B
bellard 已提交
350
{
351 352 353 354 355 356
    JUMP_TB(op_jmp0, PARAM1, 0, PARAM2);
}

void OPPROTO op_jmp1(void)
{
    JUMP_TB(op_jmp1, PARAM1, 1, PARAM2);
B
bellard 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
}

void OPPROTO op_exit_tb(void)
{
    EXIT_TB();
}

void OPPROTO op_movl_T0_psr(void)
{
    T0 = compute_cpsr();
}

/* NOTE: N = 1 and Z = 1 cannot be stored currently */
void OPPROTO op_movl_psr_T0(void)
{
    unsigned int psr;
    psr = T0;
    env->CF = (psr >> 29) & 1;
    env->NZF = (psr & 0xc0000000) ^ 0x40000000;
    env->VF = (psr << 3) & 0x80000000;
    /* for user mode we do not update other state info */
}

void OPPROTO op_mul_T0_T1(void)
{
    T0 = T0 * T1;
}

/* 64 bit unsigned mul */
void OPPROTO op_mull_T0_T1(void)
{
    uint64_t res;
B
bellard 已提交
389
    res = (uint64_t)T0 * (uint64_t)T1;
B
bellard 已提交
390 391 392 393 394 395 396 397
    T1 = res >> 32;
    T0 = res;
}

/* 64 bit signed mul */
void OPPROTO op_imull_T0_T1(void)
{
    uint64_t res;
398
    res = (int64_t)((int32_t)T0) * (int64_t)((int32_t)T1);
B
bellard 已提交
399 400 401 402
    T1 = res >> 32;
    T0 = res;
}

B
bellard 已提交
403 404 405 406 407 408 409 410
/* 48 bit signed mul, top 32 bits */
void OPPROTO op_imulw_T0_T1(void)
{
  uint64_t res;
  res = (int64_t)((int32_t)T0) * (int64_t)((int32_t)T1);
  T0 = res >> 16;
}

B
bellard 已提交
411 412 413 414 415 416 417 418 419
void OPPROTO op_addq_T0_T1(void)
{
    uint64_t res;
    res = ((uint64_t)T1 << 32) | T0;
    res += ((uint64_t)(env->regs[PARAM2]) << 32) | (env->regs[PARAM1]);
    T1 = res >> 32;
    T0 = res;
}

B
bellard 已提交
420 421 422 423 424 425 426 427 428
void OPPROTO op_addq_lo_T0_T1(void)
{
    uint64_t res;
    res = ((uint64_t)T1 << 32) | T0;
    res += (uint64_t)(env->regs[PARAM1]);
    T1 = res >> 32;
    T0 = res;
}

B
bellard 已提交
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 463 464 465 466 467 468 469 470 471 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 500
void OPPROTO op_logicq_cc(void)
{
    env->NZF = (T1 & 0x80000000) | ((T0 | T1) != 0);
}

/* memory access */

void OPPROTO op_ldub_T0_T1(void)
{
    T0 = ldub((void *)T1);
}

void OPPROTO op_ldsb_T0_T1(void)
{
    T0 = ldsb((void *)T1);
}

void OPPROTO op_lduw_T0_T1(void)
{
    T0 = lduw((void *)T1);
}

void OPPROTO op_ldsw_T0_T1(void)
{
    T0 = ldsw((void *)T1);
}

void OPPROTO op_ldl_T0_T1(void)
{
    T0 = ldl((void *)T1);
}

void OPPROTO op_stb_T0_T1(void)
{
    stb((void *)T1, T0);
}

void OPPROTO op_stw_T0_T1(void)
{
    stw((void *)T1, T0);
}

void OPPROTO op_stl_T0_T1(void)
{
    stl((void *)T1, T0);
}

void OPPROTO op_swpb_T0_T1(void)
{
    int tmp;

    cpu_lock();
    tmp = ldub((void *)T1);
    stb((void *)T1, T0);
    T0 = tmp;
    cpu_unlock();
}

void OPPROTO op_swpl_T0_T1(void)
{
    int tmp;

    cpu_lock();
    tmp = ldl((void *)T1);
    stl((void *)T1, T0);
    T0 = tmp;
    cpu_unlock();
}

/* shifts */

/* T1 based */
B
bellard 已提交
501

B
bellard 已提交
502 503 504 505 506 507 508 509 510 511
void OPPROTO op_shll_T1_im(void)
{
    T1 = T1 << PARAM1;
}

void OPPROTO op_shrl_T1_im(void)
{
    T1 = (uint32_t)T1 >> PARAM1;
}

B
bellard 已提交
512 513 514 515 516
void OPPROTO op_shrl_T1_0(void)
{
    T1 = 0;
}

B
bellard 已提交
517 518 519 520 521
void OPPROTO op_sarl_T1_im(void)
{
    T1 = (int32_t)T1 >> PARAM1;
}

B
bellard 已提交
522 523 524 525 526
void OPPROTO op_sarl_T1_0(void)
{
    T1 = (int32_t)T1 >> 31;
}

B
bellard 已提交
527 528 529 530 531 532 533
void OPPROTO op_rorl_T1_im(void)
{
    int shift;
    shift = PARAM1;
    T1 = ((uint32_t)T1 >> shift) | (T1 << (32 - shift));
}

B
bellard 已提交
534 535 536 537 538
void OPPROTO op_rrxl_T1(void)
{
    T1 = ((uint32_t)T1 >> 1) | ((uint32_t)env->CF << 31);
}

B
bellard 已提交
539 540 541 542 543 544 545 546 547 548 549 550 551
/* T1 based, set C flag */
void OPPROTO op_shll_T1_im_cc(void)
{
    env->CF = (T1 >> (32 - PARAM1)) & 1;
    T1 = T1 << PARAM1;
}

void OPPROTO op_shrl_T1_im_cc(void)
{
    env->CF = (T1 >> (PARAM1 - 1)) & 1;
    T1 = (uint32_t)T1 >> PARAM1;
}

B
bellard 已提交
552 553 554 555 556 557
void OPPROTO op_shrl_T1_0_cc(void)
{
    env->CF = (T1 >> 31) & 1;
    T1 = 0;
}

B
bellard 已提交
558 559 560 561 562 563
void OPPROTO op_sarl_T1_im_cc(void)
{
    env->CF = (T1 >> (PARAM1 - 1)) & 1;
    T1 = (int32_t)T1 >> PARAM1;
}

B
bellard 已提交
564 565 566 567 568 569
void OPPROTO op_sarl_T1_0_cc(void)
{
    env->CF = (T1 >> 31) & 1;
    T1 = (int32_t)T1 >> 31;
}

B
bellard 已提交
570 571 572 573 574 575 576 577
void OPPROTO op_rorl_T1_im_cc(void)
{
    int shift;
    shift = PARAM1;
    env->CF = (T1 >> (shift - 1)) & 1;
    T1 = ((uint32_t)T1 >> shift) | (T1 << (32 - shift));
}

B
bellard 已提交
578 579 580 581 582 583 584 585
void OPPROTO op_rrxl_T1_cc(void)
{
    uint32_t c;
    c = T1 & 1;
    T1 = ((uint32_t)T1 >> 1) | ((uint32_t)env->CF << 31);
    env->CF = c;
}

B
bellard 已提交
586 587 588 589 590 591 592 593 594 595 596
/* T2 based */
void OPPROTO op_shll_T2_im(void)
{
    T2 = T2 << PARAM1;
}

void OPPROTO op_shrl_T2_im(void)
{
    T2 = (uint32_t)T2 >> PARAM1;
}

B
bellard 已提交
597 598 599 600 601
void OPPROTO op_shrl_T2_0(void)
{
    T2 = 0;
}

B
bellard 已提交
602 603 604 605 606
void OPPROTO op_sarl_T2_im(void)
{
    T2 = (int32_t)T2 >> PARAM1;
}

B
bellard 已提交
607 608 609 610 611
void OPPROTO op_sarl_T2_0(void)
{
    T2 = (int32_t)T2 >> 31;
}

B
bellard 已提交
612 613 614 615 616 617 618
void OPPROTO op_rorl_T2_im(void)
{
    int shift;
    shift = PARAM1;
    T2 = ((uint32_t)T2 >> shift) | (T2 << (32 - shift));
}

B
bellard 已提交
619 620 621 622 623
void OPPROTO op_rrxl_T2(void)
{
    T2 = ((uint32_t)T2 >> 1) | ((uint32_t)env->CF << 31);
}

B
bellard 已提交
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 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
/* T1 based, use T0 as shift count */

void OPPROTO op_shll_T1_T0(void)
{
    int shift;
    shift = T0 & 0xff;
    if (shift >= 32)
        T1 = 0;
    else
        T1 = T1 << shift;
    FORCE_RET();
}

void OPPROTO op_shrl_T1_T0(void)
{
    int shift;
    shift = T0 & 0xff;
    if (shift >= 32)
        T1 = 0;
    else
        T1 = (uint32_t)T1 >> shift;
    FORCE_RET();
}

void OPPROTO op_sarl_T1_T0(void)
{
    int shift;
    shift = T0 & 0xff;
    if (shift >= 32)
        shift = 31;
    T1 = (int32_t)T1 >> shift;
}

void OPPROTO op_rorl_T1_T0(void)
{
    int shift;
    shift = T0 & 0x1f;
    if (shift) {
        T1 = ((uint32_t)T1 >> shift) | (T1 << (32 - shift));
    }
    FORCE_RET();
}

/* T1 based, use T0 as shift count and compute CF */

void OPPROTO op_shll_T1_T0_cc(void)
{
    int shift;
    shift = T0 & 0xff;
    if (shift >= 32) {
        if (shift == 32)
            env->CF = T1 & 1;
        else
            env->CF = 0;
        T1 = 0;
    } else if (shift != 0) {
        env->CF = (T1 >> (32 - shift)) & 1;
        T1 = T1 << shift;
    }
    FORCE_RET();
}

void OPPROTO op_shrl_T1_T0_cc(void)
{
    int shift;
    shift = T0 & 0xff;
    if (shift >= 32) {
        if (shift == 32)
            env->CF = (T1 >> 31) & 1;
        else
            env->CF = 0;
        T1 = 0;
    } else if (shift != 0) {
        env->CF = (T1 >> (shift - 1)) & 1;
        T1 = (uint32_t)T1 >> shift;
    }
    FORCE_RET();
}

void OPPROTO op_sarl_T1_T0_cc(void)
{
    int shift;
    shift = T0 & 0xff;
    if (shift >= 32) {
        env->CF = (T1 >> 31) & 1;
        T1 = (int32_t)T1 >> 31;
    } else {
        env->CF = (T1 >> (shift - 1)) & 1;
        T1 = (int32_t)T1 >> shift;
    }
    FORCE_RET();
}

void OPPROTO op_rorl_T1_T0_cc(void)
{
    int shift1, shift;
    shift1 = T0 & 0xff;
    shift = shift1 & 0x1f;
    if (shift == 0) {
        if (shift1 != 0)
            env->CF = (T1 >> 31) & 1;
    } else {
        env->CF = (T1 >> (shift - 1)) & 1;
        T1 = ((uint32_t)T1 >> shift) | (T1 << (32 - shift));
    }
    FORCE_RET();
}

B
bellard 已提交
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 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
/* misc */
void OPPROTO op_clz_T0(void)
{
    int count;
    for (count = 32; T0 > 0; count--)
        T0 = T0 >> 1;
    T0 = count;
    FORCE_RET();
}

void OPPROTO op_sarl_T0_im(void)
{
    T0 = (int32_t)T0 >> PARAM1;
}

/* 16->32 Sign extend */
void OPPROTO op_sxl_T0(void)
{
  T0 = (int16_t)T0;
}

void OPPROTO op_sxl_T1(void)
{
  T1 = (int16_t)T1;
}

#define SIGNBIT (uint32_t)0x80000000
/* saturating arithmetic  */
void OPPROTO op_addl_T0_T1_setq(void)
{
  uint32_t res;

  res = T0 + T1;
  if (((res ^ T0) & SIGNBIT) && !((T0 ^ T1) & SIGNBIT))
      env->QF = 1;

  T0 = res;
  FORCE_RET();
}

void OPPROTO op_addl_T0_T1_saturate(void)
{
  uint32_t res;

  res = T0 + T1;
  if (((res ^ T0) & SIGNBIT) && !((T0 ^ T1) & SIGNBIT)) {
      env->QF = 1;
      if (T0 & SIGNBIT)
          T0 = 0x80000000;
      else
          T0 = 0x7fffffff;
  }
  else
    T0 = res;
  
  FORCE_RET();
}

void OPPROTO op_subl_T0_T1_saturate(void)
{
  uint32_t res;

  res = T0 - T1;
  if (((res ^ T0) & SIGNBIT) && ((T0 ^ T1) & SIGNBIT)) {
      env->QF = 1;
      if (T0 & SIGNBIT)
          T0 = 0x8000000;
      else
          T0 = 0x7fffffff;
  }
  else
    T0 = res;
  
  FORCE_RET();
}

/* thumb shift by immediate */
void OPPROTO op_shll_T0_im_thumb(void)
{
    int shift;
    shift = PARAM1;
    if (shift != 0) {
	env->CF = (T1 >> (32 - shift)) & 1;
	T0 = T0 << shift;
    }
    env->NZF = T0;
    FORCE_RET();
}

void OPPROTO op_shrl_T0_im_thumb(void)
{
    int shift;

    shift = PARAM1;
    if (shift == 0) {
B
bellard 已提交
827
	env->CF = ((uint32_t)shift) >> 31;
B
bellard 已提交
828 829 830 831 832
	T0 = 0;
    } else {
	env->CF = (T0 >> (shift - 1)) & 1;
	T0 = T0 >> shift;
    }
B
bellard 已提交
833
    env->NZF = T0;
B
bellard 已提交
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
    FORCE_RET();
}

void OPPROTO op_sarl_T0_im_thumb(void)
{
    int shift;

    shift = PARAM1;
    if (shift == 0) {
	T0 = ((int32_t)T0) >> 31;
	env->CF = T0 & 1;
    } else {
	env->CF = (T0 >> (shift - 1)) & 1;
	T0 = ((int32_t)T0) >> shift;
    }
    env->NZF = T0;
    FORCE_RET();
}

B
bellard 已提交
853 854 855 856 857 858 859 860 861 862 863 864 865 866
/* exceptions */

void OPPROTO op_swi(void)
{
    env->exception_index = EXCP_SWI;
    cpu_loop_exit();
}

void OPPROTO op_undef_insn(void)
{
    env->exception_index = EXCP_UDEF;
    cpu_loop_exit();
}

B
bellard 已提交
867 868 869 870 871 872
void OPPROTO op_debug(void)
{
    env->exception_index = EXCP_DEBUG;
    cpu_loop_exit();
}

B
bellard 已提交
873 874 875
/* VFP support.  We follow the convention used for VFP instrunctions:
   Single precition routines have a "s" suffix, double precision a
   "d" suffix.  */
B
bellard 已提交
876

B
bellard 已提交
877
#define VFP_OP(name, p) void OPPROTO op_vfp_##name##p(void)
B
bellard 已提交
878

B
bellard 已提交
879
#define VFP_BINOP(name) \
B
bellard 已提交
880 881
VFP_OP(name, s)             \
{                           \
B
bellard 已提交
882
    FT0s = float32_ ## name (FT0s, FT1s, &env->vfp.fp_status);    \
B
bellard 已提交
883 884 885
}                           \
VFP_OP(name, d)             \
{                           \
B
bellard 已提交
886
    FT0d = float64_ ## name (FT0d, FT1d, &env->vfp.fp_status);    \
B
bellard 已提交
887
}
B
bellard 已提交
888 889 890 891
VFP_BINOP(add)
VFP_BINOP(sub)
VFP_BINOP(mul)
VFP_BINOP(div)
B
bellard 已提交
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
#undef VFP_BINOP

#define VFP_HELPER(name)  \
VFP_OP(name, s)           \
{                         \
    do_vfp_##name##s();    \
}                         \
VFP_OP(name, d)           \
{                         \
    do_vfp_##name##d();    \
}
VFP_HELPER(abs)
VFP_HELPER(sqrt)
VFP_HELPER(cmp)
VFP_HELPER(cmpe)
#undef VFP_HELPER

/* XXX: Will this do the right thing for NANs.  Should invert the signbit
   without looking at the rest of the value.  */
VFP_OP(neg, s)
{
B
bellard 已提交
913
    FT0s = float32_chs(FT0s);
B
bellard 已提交
914 915 916 917
}

VFP_OP(neg, d)
{
B
bellard 已提交
918
    FT0d = float64_chs(FT0d);
B
bellard 已提交
919 920 921 922
}

VFP_OP(F1_ld0, s)
{
B
bellard 已提交
923 924 925 926 927 928
    union {
        uint32_t i;
        float32 s;
    } v;
    v.i = 0;
    FT1s = v.s;
B
bellard 已提交
929 930 931 932
}

VFP_OP(F1_ld0, d)
{
B
bellard 已提交
933 934 935 936 937 938
    union {
        uint64_t i;
        float64 d;
    } v;
    v.i = 0;
    FT1d = v.d;
B
bellard 已提交
939 940 941
}

/* Helper routines to perform bitwise copies between float and int.  */
B
bellard 已提交
942
static inline float32 vfp_itos(uint32_t i)
B
bellard 已提交
943 944 945
{
    union {
        uint32_t i;
B
bellard 已提交
946
        float32 s;
B
bellard 已提交
947 948 949 950 951 952
    } v;

    v.i = i;
    return v.s;
}

B
bellard 已提交
953
static inline uint32_t vfp_stoi(float32 s)
B
bellard 已提交
954 955 956
{
    union {
        uint32_t i;
B
bellard 已提交
957
        float32 s;
B
bellard 已提交
958 959 960 961 962 963 964 965 966
    } v;

    v.s = s;
    return v.i;
}

/* Integer to float conversion.  */
VFP_OP(uito, s)
{
B
bellard 已提交
967
    FT0s = uint32_to_float32(vfp_stoi(FT0s), &env->vfp.fp_status);
B
bellard 已提交
968 969 970 971
}

VFP_OP(uito, d)
{
B
bellard 已提交
972
    FT0d = uint32_to_float64(vfp_stoi(FT0s), &env->vfp.fp_status);
B
bellard 已提交
973 974 975 976
}

VFP_OP(sito, s)
{
B
bellard 已提交
977
    FT0s = int32_to_float32(vfp_stoi(FT0s), &env->vfp.fp_status);
B
bellard 已提交
978 979 980 981
}

VFP_OP(sito, d)
{
B
bellard 已提交
982
    FT0d = int32_to_float64(vfp_stoi(FT0s), &env->vfp.fp_status);
B
bellard 已提交
983 984 985 986 987
}

/* Float to integer conversion.  */
VFP_OP(toui, s)
{
B
bellard 已提交
988
    FT0s = vfp_itos(float32_to_uint32(FT0s, &env->vfp.fp_status));
B
bellard 已提交
989 990 991 992
}

VFP_OP(toui, d)
{
B
bellard 已提交
993
    FT0s = vfp_itos(float64_to_uint32(FT0d, &env->vfp.fp_status));
B
bellard 已提交
994 995 996 997
}

VFP_OP(tosi, s)
{
B
bellard 已提交
998
    FT0s = vfp_itos(float32_to_int32(FT0s, &env->vfp.fp_status));
B
bellard 已提交
999 1000 1001 1002
}

VFP_OP(tosi, d)
{
B
bellard 已提交
1003
    FT0s = vfp_itos(float64_to_int32(FT0d, &env->vfp.fp_status));
B
bellard 已提交
1004 1005 1006 1007 1008
}

/* TODO: Set rounding mode properly.  */
VFP_OP(touiz, s)
{
B
bellard 已提交
1009
    FT0s = vfp_itos(float32_to_uint32_round_to_zero(FT0s, &env->vfp.fp_status));
B
bellard 已提交
1010 1011 1012 1013
}

VFP_OP(touiz, d)
{
B
bellard 已提交
1014
    FT0s = vfp_itos(float64_to_uint32_round_to_zero(FT0d, &env->vfp.fp_status));
B
bellard 已提交
1015 1016 1017 1018
}

VFP_OP(tosiz, s)
{
B
bellard 已提交
1019
    FT0s = vfp_itos(float32_to_int32_round_to_zero(FT0s, &env->vfp.fp_status));
B
bellard 已提交
1020 1021 1022
}

VFP_OP(tosiz, d)
B
bellard 已提交
1023
{
B
bellard 已提交
1024
    FT0s = vfp_itos(float64_to_int32_round_to_zero(FT0d, &env->vfp.fp_status));
B
bellard 已提交
1025 1026
}

B
bellard 已提交
1027 1028
/* floating point conversion */
VFP_OP(fcvtd, s)
B
bellard 已提交
1029
{
B
bellard 已提交
1030
    FT0d = float32_to_float64(FT0s, &env->vfp.fp_status);
B
bellard 已提交
1031 1032
}

B
bellard 已提交
1033 1034
VFP_OP(fcvts, d)
{
B
bellard 已提交
1035
    FT0s = float64_to_float32(FT0d, &env->vfp.fp_status);
B
bellard 已提交
1036 1037 1038 1039 1040
}

/* Get and Put values from registers.  */
VFP_OP(getreg_F0, d)
{
B
bellard 已提交
1041
  FT0d = *(float64 *)((char *) env + PARAM1);
B
bellard 已提交
1042 1043 1044 1045
}

VFP_OP(getreg_F0, s)
{
B
bellard 已提交
1046
  FT0s = *(float32 *)((char *) env + PARAM1);
B
bellard 已提交
1047 1048 1049 1050
}

VFP_OP(getreg_F1, d)
{
B
bellard 已提交
1051
  FT1d = *(float64 *)((char *) env + PARAM1);
B
bellard 已提交
1052 1053 1054 1055
}

VFP_OP(getreg_F1, s)
{
B
bellard 已提交
1056
  FT1s = *(float32 *)((char *) env + PARAM1);
B
bellard 已提交
1057 1058 1059 1060
}

VFP_OP(setreg_F0, d)
{
B
bellard 已提交
1061
  *(float64 *)((char *) env + PARAM1) = FT0d;
B
bellard 已提交
1062 1063 1064 1065
}

VFP_OP(setreg_F0, s)
{
B
bellard 已提交
1066
  *(float32 *)((char *) env + PARAM1) = FT0s;
B
bellard 已提交
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
}

void OPPROTO op_vfp_movl_T0_fpscr(void)
{
    do_vfp_get_fpscr ();
}

void OPPROTO op_vfp_movl_T0_fpscr_flags(void)
{
    T0 = env->vfp.fpscr & (0xf << 28);
}

void OPPROTO op_vfp_movl_fpscr_T0(void)
{
    do_vfp_set_fpscr();
}

/* Move between FT0s to T0  */
void OPPROTO op_vfp_mrs(void)
{
    T0 = vfp_stoi(FT0s);
}

void OPPROTO op_vfp_msr(void)
{
    FT0s = vfp_itos(T0);
}

/* Move between FT0d and {T0,T1} */
void OPPROTO op_vfp_mrrd(void)
{
    CPU_DoubleU u;
    
    u.d = FT0d;
    T0 = u.l.lower;
    T1 = u.l.upper;
}

void OPPROTO op_vfp_mdrr(void)
{
    CPU_DoubleU u;
    
    u.l.lower = T0;
    u.l.upper = T1;
    FT0d = u.d;
}

/* Floating point load/store.  Address is in T1 */
void OPPROTO op_vfp_lds(void)
{
    FT0s = ldfl((void *)T1);
}

void OPPROTO op_vfp_ldd(void)
{
    FT0d = ldfq((void *)T1);
}

void OPPROTO op_vfp_sts(void)
{
    stfl((void *)T1, FT0s);
}

void OPPROTO op_vfp_std(void)
{
    stfq((void *)T1, FT0d);
}