1 /*
   2  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2008, 2009, 2010 Red Hat, Inc.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "interpreter/bytecodes.hpp"
  28 #include "shark/llvmHeaders.hpp"
  29 #include "shark/llvmValue.hpp"
  30 #include "shark/sharkBlock.hpp"
  31 #include "shark/sharkBuilder.hpp"
  32 #include "shark/sharkConstant.hpp"
  33 #include "shark/sharkState.hpp"
  34 #include "shark/sharkValue.hpp"
  35 #include "shark/shark_globals.hpp"
  36 #include "utilities/debug.hpp"
  37 
  38 using namespace llvm;
  39 
  40 void SharkBlock::parse_bytecode(int start, int limit) {
  41   SharkValue *a, *b, *c, *d;
  42   int i;
  43 
  44   // Ensure the current state is initialized before we emit any code,
  45   // so that any setup code for the state is at the start of the block
  46   current_state();
  47 
  48   // Parse the bytecodes
  49   iter()->reset_to_bci(start);
  50   while (iter()->next_bci() < limit) {
  51     NOT_PRODUCT(a = b = c = d = NULL);
  52     iter()->next();
  53 
  54     if (SharkTraceBytecodes)
  55       tty->print_cr("%4d: %s", bci(), Bytecodes::name(bc()));
  56 
  57     if (has_trap() && trap_bci() == bci()) {
  58       do_trap(trap_request());
  59       return;
  60     }
  61 
  62     if (UseLoopSafepoints) {
  63       // XXX if a lcmp is followed by an if_?? then C2 maybe-inserts
  64       // the safepoint before the lcmp rather than before the if.
  65       // Maybe we should do this too.  See parse2.cpp for details.
  66       switch (bc()) {
  67       case Bytecodes::_goto:
  68       case Bytecodes::_ifnull:
  69       case Bytecodes::_ifnonnull:
  70       case Bytecodes::_if_acmpeq:
  71       case Bytecodes::_if_acmpne:
  72       case Bytecodes::_ifeq:
  73       case Bytecodes::_ifne:
  74       case Bytecodes::_iflt:
  75       case Bytecodes::_ifle:
  76       case Bytecodes::_ifgt:
  77       case Bytecodes::_ifge:
  78       case Bytecodes::_if_icmpeq:
  79       case Bytecodes::_if_icmpne:
  80       case Bytecodes::_if_icmplt:
  81       case Bytecodes::_if_icmple:
  82       case Bytecodes::_if_icmpgt:
  83       case Bytecodes::_if_icmpge:
  84         if (iter()->get_dest() <= bci())
  85           maybe_add_backedge_safepoint();
  86         break;
  87 
  88       case Bytecodes::_goto_w:
  89         if (iter()->get_far_dest() <= bci())
  90           maybe_add_backedge_safepoint();
  91         break;
  92 
  93       case Bytecodes::_tableswitch:
  94       case Bytecodes::_lookupswitch:
  95         if (switch_default_dest() <= bci()) {
  96           maybe_add_backedge_safepoint();
  97           break;
  98         }
  99         int len = switch_table_length();
 100         for (int i = 0; i < len; i++) {
 101           if (switch_dest(i) <= bci()) {
 102             maybe_add_backedge_safepoint();
 103             break;
 104           }
 105         }
 106         break;
 107       }
 108     }
 109 
 110     switch (bc()) {
 111     case Bytecodes::_nop:
 112       break;
 113 
 114     case Bytecodes::_aconst_null:
 115       push(SharkValue::null());
 116       break;
 117 
 118     case Bytecodes::_iconst_m1:
 119       push(SharkValue::jint_constant(-1));
 120       break;
 121     case Bytecodes::_iconst_0:
 122       push(SharkValue::jint_constant(0));
 123       break;
 124     case Bytecodes::_iconst_1:
 125       push(SharkValue::jint_constant(1));
 126       break;
 127     case Bytecodes::_iconst_2:
 128       push(SharkValue::jint_constant(2));
 129       break;
 130     case Bytecodes::_iconst_3:
 131       push(SharkValue::jint_constant(3));
 132       break;
 133     case Bytecodes::_iconst_4:
 134       push(SharkValue::jint_constant(4));
 135       break;
 136     case Bytecodes::_iconst_5:
 137       push(SharkValue::jint_constant(5));
 138       break;
 139 
 140     case Bytecodes::_lconst_0:
 141       push(SharkValue::jlong_constant(0));
 142       break;
 143     case Bytecodes::_lconst_1:
 144       push(SharkValue::jlong_constant(1));
 145       break;
 146 
 147     case Bytecodes::_fconst_0:
 148       push(SharkValue::jfloat_constant(0));
 149       break;
 150     case Bytecodes::_fconst_1:
 151       push(SharkValue::jfloat_constant(1));
 152       break;
 153     case Bytecodes::_fconst_2:
 154       push(SharkValue::jfloat_constant(2));
 155       break;
 156 
 157     case Bytecodes::_dconst_0:
 158       push(SharkValue::jdouble_constant(0));
 159       break;
 160     case Bytecodes::_dconst_1:
 161       push(SharkValue::jdouble_constant(1));
 162       break;
 163 
 164     case Bytecodes::_bipush:
 165       push(SharkValue::jint_constant(iter()->get_constant_u1()));
 166       break;
 167     case Bytecodes::_sipush:
 168       push(SharkValue::jint_constant(iter()->get_constant_u2()));
 169       break;
 170 
 171     case Bytecodes::_ldc:
 172     case Bytecodes::_ldc_w:
 173     case Bytecodes::_ldc2_w:
 174       push(SharkConstant::for_ldc(iter())->value(builder()));
 175       break;
 176 
 177     case Bytecodes::_iload_0:
 178     case Bytecodes::_lload_0:
 179     case Bytecodes::_fload_0:
 180     case Bytecodes::_dload_0:
 181     case Bytecodes::_aload_0:
 182       push(local(0));
 183       break;
 184     case Bytecodes::_iload_1:
 185     case Bytecodes::_lload_1:
 186     case Bytecodes::_fload_1:
 187     case Bytecodes::_dload_1:
 188     case Bytecodes::_aload_1:
 189       push(local(1));
 190       break;
 191     case Bytecodes::_iload_2:
 192     case Bytecodes::_lload_2:
 193     case Bytecodes::_fload_2:
 194     case Bytecodes::_dload_2:
 195     case Bytecodes::_aload_2:
 196       push(local(2));
 197       break;
 198     case Bytecodes::_iload_3:
 199     case Bytecodes::_lload_3:
 200     case Bytecodes::_fload_3:
 201     case Bytecodes::_dload_3:
 202     case Bytecodes::_aload_3:
 203       push(local(3));
 204       break;
 205     case Bytecodes::_iload:
 206     case Bytecodes::_lload:
 207     case Bytecodes::_fload:
 208     case Bytecodes::_dload:
 209     case Bytecodes::_aload:
 210       push(local(iter()->get_index()));
 211       break;
 212 
 213     case Bytecodes::_baload:
 214       do_aload(T_BYTE);
 215       break;
 216     case Bytecodes::_caload:
 217       do_aload(T_CHAR);
 218       break;
 219     case Bytecodes::_saload:
 220       do_aload(T_SHORT);
 221       break;
 222     case Bytecodes::_iaload:
 223       do_aload(T_INT);
 224       break;
 225     case Bytecodes::_laload:
 226       do_aload(T_LONG);
 227       break;
 228     case Bytecodes::_faload:
 229       do_aload(T_FLOAT);
 230       break;
 231     case Bytecodes::_daload:
 232       do_aload(T_DOUBLE);
 233       break;
 234     case Bytecodes::_aaload:
 235       do_aload(T_OBJECT);
 236       break;
 237 
 238     case Bytecodes::_istore_0:
 239     case Bytecodes::_lstore_0:
 240     case Bytecodes::_fstore_0:
 241     case Bytecodes::_dstore_0:
 242     case Bytecodes::_astore_0:
 243       set_local(0, pop());
 244       break;
 245     case Bytecodes::_istore_1:
 246     case Bytecodes::_lstore_1:
 247     case Bytecodes::_fstore_1:
 248     case Bytecodes::_dstore_1:
 249     case Bytecodes::_astore_1:
 250       set_local(1, pop());
 251       break;
 252     case Bytecodes::_istore_2:
 253     case Bytecodes::_lstore_2:
 254     case Bytecodes::_fstore_2:
 255     case Bytecodes::_dstore_2:
 256     case Bytecodes::_astore_2:
 257       set_local(2, pop());
 258       break;
 259     case Bytecodes::_istore_3:
 260     case Bytecodes::_lstore_3:
 261     case Bytecodes::_fstore_3:
 262     case Bytecodes::_dstore_3:
 263     case Bytecodes::_astore_3:
 264       set_local(3, pop());
 265       break;
 266     case Bytecodes::_istore:
 267     case Bytecodes::_lstore:
 268     case Bytecodes::_fstore:
 269     case Bytecodes::_dstore:
 270     case Bytecodes::_astore:
 271       set_local(iter()->get_index(), pop());
 272       break;
 273 
 274     case Bytecodes::_bastore:
 275       do_astore(T_BYTE);
 276       break;
 277     case Bytecodes::_castore:
 278       do_astore(T_CHAR);
 279       break;
 280     case Bytecodes::_sastore:
 281       do_astore(T_SHORT);
 282       break;
 283     case Bytecodes::_iastore:
 284       do_astore(T_INT);
 285       break;
 286     case Bytecodes::_lastore:
 287       do_astore(T_LONG);
 288       break;
 289     case Bytecodes::_fastore:
 290       do_astore(T_FLOAT);
 291       break;
 292     case Bytecodes::_dastore:
 293       do_astore(T_DOUBLE);
 294       break;
 295     case Bytecodes::_aastore:
 296       do_astore(T_OBJECT);
 297       break;
 298 
 299     case Bytecodes::_pop:
 300       xpop();
 301       break;
 302     case Bytecodes::_pop2:
 303       xpop();
 304       xpop();
 305       break;
 306     case Bytecodes::_swap:
 307       a = xpop();
 308       b = xpop();
 309       xpush(a);
 310       xpush(b);
 311       break;
 312     case Bytecodes::_dup:
 313       a = xpop();
 314       xpush(a);
 315       xpush(a);
 316       break;
 317     case Bytecodes::_dup_x1:
 318       a = xpop();
 319       b = xpop();
 320       xpush(a);
 321       xpush(b);
 322       xpush(a);
 323       break;
 324     case Bytecodes::_dup_x2:
 325       a = xpop();
 326       b = xpop();
 327       c = xpop();
 328       xpush(a);
 329       xpush(c);
 330       xpush(b);
 331       xpush(a);
 332       break;
 333     case Bytecodes::_dup2:
 334       a = xpop();
 335       b = xpop();
 336       xpush(b);
 337       xpush(a);
 338       xpush(b);
 339       xpush(a);
 340       break;
 341     case Bytecodes::_dup2_x1:
 342       a = xpop();
 343       b = xpop();
 344       c = xpop();
 345       xpush(b);
 346       xpush(a);
 347       xpush(c);
 348       xpush(b);
 349       xpush(a);
 350       break;
 351     case Bytecodes::_dup2_x2:
 352       a = xpop();
 353       b = xpop();
 354       c = xpop();
 355       d = xpop();
 356       xpush(b);
 357       xpush(a);
 358       xpush(d);
 359       xpush(c);
 360       xpush(b);
 361       xpush(a);
 362       break;
 363 
 364     case Bytecodes::_arraylength:
 365       do_arraylength();
 366       break;
 367 
 368     case Bytecodes::_getfield:
 369       do_getfield();
 370       break;
 371     case Bytecodes::_getstatic:
 372       do_getstatic();
 373       break;
 374     case Bytecodes::_putfield:
 375       do_putfield();
 376       break;
 377     case Bytecodes::_putstatic:
 378       do_putstatic();
 379       break;
 380 
 381     case Bytecodes::_iadd:
 382       b = pop();
 383       a = pop();
 384       push(SharkValue::create_jint(
 385         builder()->CreateAdd(a->jint_value(), b->jint_value()), false));
 386       break;
 387     case Bytecodes::_isub:
 388       b = pop();
 389       a = pop();
 390       push(SharkValue::create_jint(
 391         builder()->CreateSub(a->jint_value(), b->jint_value()), false));
 392       break;
 393     case Bytecodes::_imul:
 394       b = pop();
 395       a = pop();
 396       push(SharkValue::create_jint(
 397         builder()->CreateMul(a->jint_value(), b->jint_value()), false));
 398       break;
 399     case Bytecodes::_idiv:
 400       do_idiv();
 401       break;
 402     case Bytecodes::_irem:
 403       do_irem();
 404       break;
 405     case Bytecodes::_ineg:
 406       a = pop();
 407       push(SharkValue::create_jint(
 408         builder()->CreateNeg(a->jint_value()), a->zero_checked()));
 409       break;
 410     case Bytecodes::_ishl:
 411       b = pop();
 412       a = pop();
 413       push(SharkValue::create_jint(
 414         builder()->CreateShl(
 415           a->jint_value(),
 416           builder()->CreateAnd(
 417             b->jint_value(), LLVMValue::jint_constant(0x1f))), false));
 418       break;
 419     case Bytecodes::_ishr:
 420       b = pop();
 421       a = pop();
 422       push(SharkValue::create_jint(
 423         builder()->CreateAShr(
 424           a->jint_value(),
 425           builder()->CreateAnd(
 426             b->jint_value(), LLVMValue::jint_constant(0x1f))), false));
 427       break;
 428     case Bytecodes::_iushr:
 429       b = pop();
 430       a = pop();
 431       push(SharkValue::create_jint(
 432         builder()->CreateLShr(
 433           a->jint_value(),
 434           builder()->CreateAnd(
 435             b->jint_value(), LLVMValue::jint_constant(0x1f))), false));
 436       break;
 437     case Bytecodes::_iand:
 438       b = pop();
 439       a = pop();
 440       push(SharkValue::create_jint(
 441         builder()->CreateAnd(a->jint_value(), b->jint_value()), false));
 442       break;
 443     case Bytecodes::_ior:
 444       b = pop();
 445       a = pop();
 446       push(SharkValue::create_jint(
 447         builder()->CreateOr(a->jint_value(), b->jint_value()),
 448         a->zero_checked() && b->zero_checked()));
 449       break;
 450     case Bytecodes::_ixor:
 451       b = pop();
 452       a = pop();
 453       push(SharkValue::create_jint(
 454         builder()->CreateXor(a->jint_value(), b->jint_value()), false));
 455       break;
 456 
 457     case Bytecodes::_ladd:
 458       b = pop();
 459       a = pop();
 460       push(SharkValue::create_jlong(
 461         builder()->CreateAdd(a->jlong_value(), b->jlong_value()), false));
 462       break;
 463     case Bytecodes::_lsub:
 464       b = pop();
 465       a = pop();
 466       push(SharkValue::create_jlong(
 467         builder()->CreateSub(a->jlong_value(), b->jlong_value()), false));
 468       break;
 469     case Bytecodes::_lmul:
 470       b = pop();
 471       a = pop();
 472       push(SharkValue::create_jlong(
 473         builder()->CreateMul(a->jlong_value(), b->jlong_value()), false));
 474       break;
 475     case Bytecodes::_ldiv:
 476       do_ldiv();
 477       break;
 478     case Bytecodes::_lrem:
 479       do_lrem();
 480       break;
 481     case Bytecodes::_lneg:
 482       a = pop();
 483       push(SharkValue::create_jlong(
 484         builder()->CreateNeg(a->jlong_value()), a->zero_checked()));
 485       break;
 486     case Bytecodes::_lshl:
 487       b = pop();
 488       a = pop();
 489       push(SharkValue::create_jlong(
 490         builder()->CreateShl(
 491           a->jlong_value(),
 492           builder()->CreateIntCast(
 493             builder()->CreateAnd(
 494               b->jint_value(), LLVMValue::jint_constant(0x3f)),
 495             SharkType::jlong_type(), true)), false));
 496       break;
 497     case Bytecodes::_lshr:
 498       b = pop();
 499       a = pop();
 500       push(SharkValue::create_jlong(
 501         builder()->CreateAShr(
 502           a->jlong_value(),
 503           builder()->CreateIntCast(
 504             builder()->CreateAnd(
 505               b->jint_value(), LLVMValue::jint_constant(0x3f)),
 506             SharkType::jlong_type(), true)), false));
 507       break;
 508     case Bytecodes::_lushr:
 509       b = pop();
 510       a = pop();
 511       push(SharkValue::create_jlong(
 512         builder()->CreateLShr(
 513           a->jlong_value(),
 514           builder()->CreateIntCast(
 515             builder()->CreateAnd(
 516               b->jint_value(), LLVMValue::jint_constant(0x3f)),
 517             SharkType::jlong_type(), true)), false));
 518       break;
 519     case Bytecodes::_land:
 520       b = pop();
 521       a = pop();
 522       push(SharkValue::create_jlong(
 523         builder()->CreateAnd(a->jlong_value(), b->jlong_value()), false));
 524       break;
 525     case Bytecodes::_lor:
 526       b = pop();
 527       a = pop();
 528       push(SharkValue::create_jlong(
 529         builder()->CreateOr(a->jlong_value(), b->jlong_value()),
 530         a->zero_checked() && b->zero_checked()));
 531       break;
 532     case Bytecodes::_lxor:
 533       b = pop();
 534       a = pop();
 535       push(SharkValue::create_jlong(
 536         builder()->CreateXor(a->jlong_value(), b->jlong_value()), false));
 537       break;
 538 
 539     case Bytecodes::_fadd:
 540       b = pop();
 541       a = pop();
 542       push(SharkValue::create_jfloat(
 543         builder()->CreateFAdd(a->jfloat_value(), b->jfloat_value())));
 544       break;
 545     case Bytecodes::_fsub:
 546       b = pop();
 547       a = pop();
 548       push(SharkValue::create_jfloat(
 549         builder()->CreateFSub(a->jfloat_value(), b->jfloat_value())));
 550       break;
 551     case Bytecodes::_fmul:
 552       b = pop();
 553       a = pop();
 554       push(SharkValue::create_jfloat(
 555         builder()->CreateFMul(a->jfloat_value(), b->jfloat_value())));
 556       break;
 557     case Bytecodes::_fdiv:
 558       b = pop();
 559       a = pop();
 560       push(SharkValue::create_jfloat(
 561         builder()->CreateFDiv(a->jfloat_value(), b->jfloat_value())));
 562       break;
 563     case Bytecodes::_frem:
 564       b = pop();
 565       a = pop();
 566       push(SharkValue::create_jfloat(
 567         builder()->CreateFRem(a->jfloat_value(), b->jfloat_value())));
 568       break;
 569     case Bytecodes::_fneg:
 570       a = pop();
 571       push(SharkValue::create_jfloat(
 572         builder()->CreateFNeg(a->jfloat_value())));
 573       break;
 574 
 575     case Bytecodes::_dadd:
 576       b = pop();
 577       a = pop();
 578       push(SharkValue::create_jdouble(
 579         builder()->CreateFAdd(a->jdouble_value(), b->jdouble_value())));
 580       break;
 581     case Bytecodes::_dsub:
 582       b = pop();
 583       a = pop();
 584       push(SharkValue::create_jdouble(
 585         builder()->CreateFSub(a->jdouble_value(), b->jdouble_value())));
 586       break;
 587     case Bytecodes::_dmul:
 588       b = pop();
 589       a = pop();
 590       push(SharkValue::create_jdouble(
 591         builder()->CreateFMul(a->jdouble_value(), b->jdouble_value())));
 592       break;
 593     case Bytecodes::_ddiv:
 594       b = pop();
 595       a = pop();
 596       push(SharkValue::create_jdouble(
 597         builder()->CreateFDiv(a->jdouble_value(), b->jdouble_value())));
 598       break;
 599     case Bytecodes::_drem:
 600       b = pop();
 601       a = pop();
 602       push(SharkValue::create_jdouble(
 603         builder()->CreateFRem(a->jdouble_value(), b->jdouble_value())));
 604       break;
 605     case Bytecodes::_dneg:
 606       a = pop();
 607       push(SharkValue::create_jdouble(
 608         builder()->CreateFNeg(a->jdouble_value())));
 609       break;
 610 
 611     case Bytecodes::_iinc:
 612       i = iter()->get_index();
 613       set_local(
 614         i,
 615         SharkValue::create_jint(
 616           builder()->CreateAdd(
 617             LLVMValue::jint_constant(iter()->get_iinc_con()),
 618             local(i)->jint_value()), false));
 619       break;
 620 
 621     case Bytecodes::_lcmp:
 622       do_lcmp();
 623       break;
 624 
 625     case Bytecodes::_fcmpl:
 626       do_fcmp(false, false);
 627       break;
 628     case Bytecodes::_fcmpg:
 629       do_fcmp(false, true);
 630       break;
 631     case Bytecodes::_dcmpl:
 632       do_fcmp(true, false);
 633       break;
 634     case Bytecodes::_dcmpg:
 635       do_fcmp(true, true);
 636       break;
 637 
 638     case Bytecodes::_i2l:
 639       a = pop();
 640       push(SharkValue::create_jlong(
 641         builder()->CreateIntCast(
 642           a->jint_value(), SharkType::jlong_type(), true), a->zero_checked()));
 643       break;
 644     case Bytecodes::_i2f:
 645       push(SharkValue::create_jfloat(
 646         builder()->CreateSIToFP(
 647           pop()->jint_value(), SharkType::jfloat_type())));
 648       break;
 649     case Bytecodes::_i2d:
 650       push(SharkValue::create_jdouble(
 651         builder()->CreateSIToFP(
 652           pop()->jint_value(), SharkType::jdouble_type())));
 653       break;
 654 
 655     case Bytecodes::_l2i:
 656       push(SharkValue::create_jint(
 657         builder()->CreateIntCast(
 658           pop()->jlong_value(), SharkType::jint_type(), true), false));
 659       break;
 660     case Bytecodes::_l2f:
 661       push(SharkValue::create_jfloat(
 662         builder()->CreateSIToFP(
 663           pop()->jlong_value(), SharkType::jfloat_type())));
 664       break;
 665     case Bytecodes::_l2d:
 666       push(SharkValue::create_jdouble(
 667         builder()->CreateSIToFP(
 668           pop()->jlong_value(), SharkType::jdouble_type())));
 669       break;
 670 
 671     case Bytecodes::_f2i:
 672       push(SharkValue::create_jint(
 673         builder()->CreateCall(
 674           builder()->f2i(), pop()->jfloat_value()), false));
 675       break;
 676     case Bytecodes::_f2l:
 677       push(SharkValue::create_jlong(
 678         builder()->CreateCall(
 679           builder()->f2l(), pop()->jfloat_value()), false));
 680       break;
 681     case Bytecodes::_f2d:
 682       push(SharkValue::create_jdouble(
 683         builder()->CreateFPExt(
 684           pop()->jfloat_value(), SharkType::jdouble_type())));
 685       break;
 686 
 687     case Bytecodes::_d2i:
 688       push(SharkValue::create_jint(
 689         builder()->CreateCall(
 690           builder()->d2i(), pop()->jdouble_value()), false));
 691       break;
 692     case Bytecodes::_d2l:
 693       push(SharkValue::create_jlong(
 694         builder()->CreateCall(
 695           builder()->d2l(), pop()->jdouble_value()), false));
 696       break;
 697     case Bytecodes::_d2f:
 698       push(SharkValue::create_jfloat(
 699         builder()->CreateFPTrunc(
 700           pop()->jdouble_value(), SharkType::jfloat_type())));
 701       break;
 702 
 703     case Bytecodes::_i2b:
 704       push(SharkValue::create_jint(
 705         builder()->CreateAShr(
 706           builder()->CreateShl(
 707             pop()->jint_value(),
 708             LLVMValue::jint_constant(24)),
 709           LLVMValue::jint_constant(24)), false));
 710       break;
 711     case Bytecodes::_i2c:
 712       push(SharkValue::create_jint(
 713         builder()->CreateAnd(
 714             pop()->jint_value(),
 715             LLVMValue::jint_constant(0xffff)), false));
 716       break;
 717     case Bytecodes::_i2s:
 718       push(SharkValue::create_jint(
 719         builder()->CreateAShr(
 720           builder()->CreateShl(
 721             pop()->jint_value(),
 722             LLVMValue::jint_constant(16)),
 723           LLVMValue::jint_constant(16)), false));
 724       break;
 725 
 726     case Bytecodes::_return:
 727       do_return(T_VOID);
 728       break;
 729     case Bytecodes::_ireturn:
 730       do_return(T_INT);
 731       break;
 732     case Bytecodes::_lreturn:
 733       do_return(T_LONG);
 734       break;
 735     case Bytecodes::_freturn:
 736       do_return(T_FLOAT);
 737       break;
 738     case Bytecodes::_dreturn:
 739       do_return(T_DOUBLE);
 740       break;
 741     case Bytecodes::_areturn:
 742       do_return(T_OBJECT);
 743       break;
 744 
 745     case Bytecodes::_athrow:
 746       do_athrow();
 747       break;
 748 
 749     case Bytecodes::_goto:
 750     case Bytecodes::_goto_w:
 751       do_goto();
 752       break;
 753 
 754     case Bytecodes::_jsr:
 755     case Bytecodes::_jsr_w:
 756       do_jsr();
 757       break;
 758 
 759     case Bytecodes::_ret:
 760       do_ret();
 761       break;
 762 
 763     case Bytecodes::_ifnull:
 764       do_if(ICmpInst::ICMP_EQ, SharkValue::null(), pop());
 765       break;
 766     case Bytecodes::_ifnonnull:
 767       do_if(ICmpInst::ICMP_NE, SharkValue::null(), pop());
 768       break;
 769     case Bytecodes::_if_acmpeq:
 770       b = pop();
 771       a = pop();
 772       do_if(ICmpInst::ICMP_EQ, b, a);
 773       break;
 774     case Bytecodes::_if_acmpne:
 775       b = pop();
 776       a = pop();
 777       do_if(ICmpInst::ICMP_NE, b, a);
 778       break;
 779     case Bytecodes::_ifeq:
 780       do_if(ICmpInst::ICMP_EQ, SharkValue::jint_constant(0), pop());
 781       break;
 782     case Bytecodes::_ifne:
 783       do_if(ICmpInst::ICMP_NE, SharkValue::jint_constant(0), pop());
 784       break;
 785     case Bytecodes::_iflt:
 786       do_if(ICmpInst::ICMP_SLT, SharkValue::jint_constant(0), pop());
 787       break;
 788     case Bytecodes::_ifle:
 789       do_if(ICmpInst::ICMP_SLE, SharkValue::jint_constant(0), pop());
 790       break;
 791     case Bytecodes::_ifgt:
 792       do_if(ICmpInst::ICMP_SGT, SharkValue::jint_constant(0), pop());
 793       break;
 794     case Bytecodes::_ifge:
 795       do_if(ICmpInst::ICMP_SGE, SharkValue::jint_constant(0), pop());
 796       break;
 797     case Bytecodes::_if_icmpeq:
 798       b = pop();
 799       a = pop();
 800       do_if(ICmpInst::ICMP_EQ, b, a);
 801       break;
 802     case Bytecodes::_if_icmpne:
 803       b = pop();
 804       a = pop();
 805       do_if(ICmpInst::ICMP_NE, b, a);
 806       break;
 807     case Bytecodes::_if_icmplt:
 808       b = pop();
 809       a = pop();
 810       do_if(ICmpInst::ICMP_SLT, b, a);
 811       break;
 812     case Bytecodes::_if_icmple:
 813       b = pop();
 814       a = pop();
 815       do_if(ICmpInst::ICMP_SLE, b, a);
 816       break;
 817     case Bytecodes::_if_icmpgt:
 818       b = pop();
 819       a = pop();
 820       do_if(ICmpInst::ICMP_SGT, b, a);
 821       break;
 822     case Bytecodes::_if_icmpge:
 823       b = pop();
 824       a = pop();
 825       do_if(ICmpInst::ICMP_SGE, b, a);
 826       break;
 827 
 828     case Bytecodes::_tableswitch:
 829     case Bytecodes::_lookupswitch:
 830       do_switch();
 831       break;
 832 
 833     case Bytecodes::_invokestatic:
 834     case Bytecodes::_invokespecial:
 835     case Bytecodes::_invokevirtual:
 836     case Bytecodes::_invokeinterface:
 837       do_call();
 838       break;
 839 
 840     case Bytecodes::_instanceof:
 841       // This is a very common construct:
 842       //
 843       //  if (object instanceof Klass) {
 844       //    something = (Klass) object;
 845       //    ...
 846       //  }
 847       //
 848       // which gets compiled to something like this:
 849       //
 850       //  28: aload 9
 851       //  30: instanceof <Class Klass>
 852       //  33: ifeq 52
 853       //  36: aload 9
 854       //  38: checkcast <Class Klass>
 855       //
 856       // Handling both bytecodes at once allows us
 857       // to eliminate the checkcast.
 858       if (iter()->next_bci() < limit &&
 859           (iter()->next_bc() == Bytecodes::_ifeq ||
 860            iter()->next_bc() == Bytecodes::_ifne) &&
 861           (!UseLoopSafepoints ||
 862            iter()->next_get_dest() > iter()->next_bci())) {
 863         if (maybe_do_instanceof_if()) {
 864           iter()->next();
 865           if (SharkTraceBytecodes)
 866             tty->print_cr("%4d: %s", bci(), Bytecodes::name(bc()));
 867           break;
 868         }
 869       }
 870       // fall through
 871     case Bytecodes::_checkcast:
 872       do_instance_check();
 873       break;
 874 
 875     case Bytecodes::_new:
 876       do_new();
 877       break;
 878     case Bytecodes::_newarray:
 879       do_newarray();
 880       break;
 881     case Bytecodes::_anewarray:
 882       do_anewarray();
 883       break;
 884     case Bytecodes::_multianewarray:
 885       do_multianewarray();
 886       break;
 887 
 888     case Bytecodes::_monitorenter:
 889       do_monitorenter();
 890       break;
 891     case Bytecodes::_monitorexit:
 892       do_monitorexit();
 893       break;
 894 
 895     default:
 896       ShouldNotReachHere();
 897     }
 898   }
 899 }
 900 
 901 SharkState* SharkBlock::initial_current_state() {
 902   return entry_state()->copy();
 903 }
 904 
 905 int SharkBlock::switch_default_dest() {
 906   return iter()->get_dest_table(0);
 907 }
 908 
 909 int SharkBlock::switch_table_length() {
 910   switch(bc()) {
 911   case Bytecodes::_tableswitch:
 912     return iter()->get_int_table(2) - iter()->get_int_table(1) + 1;
 913 
 914   case Bytecodes::_lookupswitch:
 915     return iter()->get_int_table(1);
 916 
 917   default:
 918     ShouldNotReachHere();
 919   }
 920 }
 921 
 922 int SharkBlock::switch_key(int i) {
 923   switch(bc()) {
 924   case Bytecodes::_tableswitch:
 925     return iter()->get_int_table(1) + i;
 926 
 927   case Bytecodes::_lookupswitch:
 928     return iter()->get_int_table(2 + 2 * i);
 929 
 930   default:
 931     ShouldNotReachHere();
 932   }
 933 }
 934 
 935 int SharkBlock::switch_dest(int i) {
 936   switch(bc()) {
 937   case Bytecodes::_tableswitch:
 938     return iter()->get_dest_table(i + 3);
 939 
 940   case Bytecodes::_lookupswitch:
 941     return iter()->get_dest_table(2 + 2 * i + 1);
 942 
 943   default:
 944     ShouldNotReachHere();
 945   }
 946 }
 947 
 948 void SharkBlock::do_div_or_rem(bool is_long, bool is_rem) {
 949   SharkValue *sb = pop();
 950   SharkValue *sa = pop();
 951 
 952   check_divide_by_zero(sb);
 953 
 954   Value *a, *b, *p, *q;
 955   if (is_long) {
 956     a = sa->jlong_value();
 957     b = sb->jlong_value();
 958     p = LLVMValue::jlong_constant(0x8000000000000000LL);
 959     q = LLVMValue::jlong_constant(-1);
 960   }
 961   else {
 962     a = sa->jint_value();
 963     b = sb->jint_value();
 964     p = LLVMValue::jint_constant(0x80000000);
 965     q = LLVMValue::jint_constant(-1);
 966   }
 967 
 968   BasicBlock *ip           = builder()->GetBlockInsertionPoint();
 969   BasicBlock *special_case = builder()->CreateBlock(ip, "special_case");
 970   BasicBlock *general_case = builder()->CreateBlock(ip, "general_case");
 971   BasicBlock *done         = builder()->CreateBlock(ip, "done");
 972 
 973   builder()->CreateCondBr(
 974     builder()->CreateAnd(
 975       builder()->CreateICmpEQ(a, p),
 976       builder()->CreateICmpEQ(b, q)),
 977     special_case, general_case);
 978 
 979   builder()->SetInsertPoint(special_case);
 980   Value *special_result;
 981   if (is_rem) {
 982     if (is_long)
 983       special_result = LLVMValue::jlong_constant(0);
 984     else
 985       special_result = LLVMValue::jint_constant(0);
 986   }
 987   else {
 988     special_result = a;
 989   }
 990   builder()->CreateBr(done);
 991 
 992   builder()->SetInsertPoint(general_case);
 993   Value *general_result;
 994   if (is_rem)
 995     general_result = builder()->CreateSRem(a, b);
 996   else
 997     general_result = builder()->CreateSDiv(a, b);
 998   builder()->CreateBr(done);
 999 
1000   builder()->SetInsertPoint(done);
1001   PHINode *result;
1002   if (is_long)
1003     result = builder()->CreatePHI(SharkType::jlong_type(), "result");
1004   else
1005     result = builder()->CreatePHI(SharkType::jint_type(), "result");
1006   result->addIncoming(special_result, special_case);
1007   result->addIncoming(general_result, general_case);
1008 
1009   if (is_long)
1010     push(SharkValue::create_jlong(result, false));
1011   else
1012     push(SharkValue::create_jint(result, false));
1013 }
1014 
1015 void SharkBlock::do_field_access(bool is_get, bool is_field) {
1016   bool will_link;
1017   ciField *field = iter()->get_field(will_link);
1018   assert(will_link, "typeflow responsibility");
1019   assert(is_field != field->is_static(), "mismatch");
1020 
1021   // Pop the value off the stack where necessary
1022   SharkValue *value = NULL;
1023   if (!is_get)
1024     value = pop();
1025 
1026   // Find the object we're accessing, if necessary
1027   Value *object = NULL;
1028   if (is_field) {
1029     SharkValue *value = pop();
1030     check_null(value);
1031     object = value->generic_value();
1032   }
1033   if (is_get && field->is_constant()) {
1034     SharkConstant *constant = SharkConstant::for_field(iter());
1035     if (constant->is_loaded())
1036       value = constant->value(builder());
1037   }
1038   if (!is_get || value == NULL) {
1039     if (!is_field)
1040       object = builder()->CreateInlineOop(field->holder());
1041 
1042     BasicType   basic_type = field->type()->basic_type();
1043     const Type *stack_type = SharkType::to_stackType(basic_type);
1044     const Type *field_type = SharkType::to_arrayType(basic_type);
1045 
1046     Value *addr = builder()->CreateAddressOfStructEntry(
1047       object, in_ByteSize(field->offset_in_bytes()),
1048       PointerType::getUnqual(field_type),
1049       "addr");
1050 
1051     // Do the access
1052     if (is_get) {
1053       Value *field_value = builder()->CreateLoad(addr);
1054 
1055       if (field_type != stack_type) {
1056         field_value = builder()->CreateIntCast(
1057           field_value, stack_type, basic_type != T_CHAR);
1058       }
1059 
1060       value = SharkValue::create_generic(field->type(), field_value, false);
1061     }
1062     else {
1063       Value *field_value = value->generic_value();
1064 
1065       if (field_type != stack_type) {
1066         field_value = builder()->CreateIntCast(
1067           field_value, field_type, basic_type != T_CHAR);
1068       }
1069 
1070       builder()->CreateStore(field_value, addr);
1071 
1072       if (!field->type()->is_primitive_type())
1073         builder()->CreateUpdateBarrierSet(oopDesc::bs(), addr);
1074 
1075       if (field->is_volatile())
1076         builder()->CreateMemoryBarrier(SharkBuilder::BARRIER_STORELOAD);
1077     }
1078   }
1079 
1080   // Push the value onto the stack where necessary
1081   if (is_get)
1082     push(value);
1083 }
1084 
1085 void SharkBlock::do_lcmp() {
1086   Value *b = pop()->jlong_value();
1087   Value *a = pop()->jlong_value();
1088 
1089   BasicBlock *ip   = builder()->GetBlockInsertionPoint();
1090   BasicBlock *ne   = builder()->CreateBlock(ip, "lcmp_ne");
1091   BasicBlock *lt   = builder()->CreateBlock(ip, "lcmp_lt");
1092   BasicBlock *gt   = builder()->CreateBlock(ip, "lcmp_gt");
1093   BasicBlock *done = builder()->CreateBlock(ip, "done");
1094 
1095   BasicBlock *eq = builder()->GetInsertBlock();
1096   builder()->CreateCondBr(builder()->CreateICmpEQ(a, b), done, ne);
1097 
1098   builder()->SetInsertPoint(ne);
1099   builder()->CreateCondBr(builder()->CreateICmpSLT(a, b), lt, gt);
1100 
1101   builder()->SetInsertPoint(lt);
1102   builder()->CreateBr(done);
1103 
1104   builder()->SetInsertPoint(gt);
1105   builder()->CreateBr(done);
1106 
1107   builder()->SetInsertPoint(done);
1108   PHINode *result = builder()->CreatePHI(SharkType::jint_type(), "result");
1109   result->addIncoming(LLVMValue::jint_constant(-1), lt);
1110   result->addIncoming(LLVMValue::jint_constant(0),  eq);
1111   result->addIncoming(LLVMValue::jint_constant(1),  gt);
1112 
1113   push(SharkValue::create_jint(result, false));
1114 }
1115 
1116 void SharkBlock::do_fcmp(bool is_double, bool unordered_is_greater) {
1117   Value *a, *b;
1118   if (is_double) {
1119     b = pop()->jdouble_value();
1120     a = pop()->jdouble_value();
1121   }
1122   else {
1123     b = pop()->jfloat_value();
1124     a = pop()->jfloat_value();
1125   }
1126 
1127   BasicBlock *ip      = builder()->GetBlockInsertionPoint();
1128   BasicBlock *ordered = builder()->CreateBlock(ip, "ordered");
1129   BasicBlock *ge      = builder()->CreateBlock(ip, "fcmp_ge");
1130   BasicBlock *lt      = builder()->CreateBlock(ip, "fcmp_lt");
1131   BasicBlock *eq      = builder()->CreateBlock(ip, "fcmp_eq");
1132   BasicBlock *gt      = builder()->CreateBlock(ip, "fcmp_gt");
1133   BasicBlock *done    = builder()->CreateBlock(ip, "done");
1134 
1135   builder()->CreateCondBr(
1136     builder()->CreateFCmpUNO(a, b),
1137     unordered_is_greater ? gt : lt, ordered);
1138 
1139   builder()->SetInsertPoint(ordered);
1140   builder()->CreateCondBr(builder()->CreateFCmpULT(a, b), lt, ge);
1141 
1142   builder()->SetInsertPoint(ge);
1143   builder()->CreateCondBr(builder()->CreateFCmpUGT(a, b), gt, eq);
1144 
1145   builder()->SetInsertPoint(lt);
1146   builder()->CreateBr(done);
1147 
1148   builder()->SetInsertPoint(gt);
1149   builder()->CreateBr(done);
1150 
1151   builder()->SetInsertPoint(eq);
1152   builder()->CreateBr(done);
1153 
1154   builder()->SetInsertPoint(done);
1155   PHINode *result = builder()->CreatePHI(SharkType::jint_type(), "result");
1156   result->addIncoming(LLVMValue::jint_constant(-1), lt);
1157   result->addIncoming(LLVMValue::jint_constant(0),  eq);
1158   result->addIncoming(LLVMValue::jint_constant(1),  gt);
1159 
1160   push(SharkValue::create_jint(result, false));
1161 }
1162 
1163 void SharkBlock::emit_IR() {
1164   ShouldNotCallThis();
1165 }
1166 
1167 SharkState* SharkBlock::entry_state() {
1168   ShouldNotCallThis();
1169 }
1170 
1171 void SharkBlock::do_zero_check(SharkValue* value) {
1172   ShouldNotCallThis();
1173 }
1174 
1175 void SharkBlock::maybe_add_backedge_safepoint() {
1176   ShouldNotCallThis();
1177 }
1178 
1179 bool SharkBlock::has_trap() {
1180   return false;
1181 }
1182 
1183 int SharkBlock::trap_request() {
1184   ShouldNotCallThis();
1185 }
1186 
1187 int SharkBlock::trap_bci() {
1188   ShouldNotCallThis();
1189 }
1190 
1191 void SharkBlock::do_trap(int trap_request) {
1192   ShouldNotCallThis();
1193 }
1194 
1195 void SharkBlock::do_arraylength() {
1196   ShouldNotCallThis();
1197 }
1198 
1199 void SharkBlock::do_aload(BasicType basic_type) {
1200   ShouldNotCallThis();
1201 }
1202 
1203 void SharkBlock::do_astore(BasicType basic_type) {
1204   ShouldNotCallThis();
1205 }
1206 
1207 void SharkBlock::do_return(BasicType type) {
1208   ShouldNotCallThis();
1209 }
1210 
1211 void SharkBlock::do_athrow() {
1212   ShouldNotCallThis();
1213 }
1214 
1215 void SharkBlock::do_goto() {
1216   ShouldNotCallThis();
1217 }
1218 
1219 void SharkBlock::do_jsr() {
1220   ShouldNotCallThis();
1221 }
1222 
1223 void SharkBlock::do_ret() {
1224   ShouldNotCallThis();
1225 }
1226 
1227 void SharkBlock::do_if(ICmpInst::Predicate p, SharkValue* b, SharkValue* a) {
1228   ShouldNotCallThis();
1229 }
1230 
1231 void SharkBlock::do_switch() {
1232   ShouldNotCallThis();
1233 }
1234 
1235 void SharkBlock::do_call() {
1236   ShouldNotCallThis();
1237 }
1238 
1239 void SharkBlock::do_instance_check() {
1240   ShouldNotCallThis();
1241 }
1242 
1243 bool SharkBlock::maybe_do_instanceof_if() {
1244   ShouldNotCallThis();
1245 }
1246 
1247 void SharkBlock::do_new() {
1248   ShouldNotCallThis();
1249 }
1250 
1251 void SharkBlock::do_newarray() {
1252   ShouldNotCallThis();
1253 }
1254 
1255 void SharkBlock::do_anewarray() {
1256   ShouldNotCallThis();
1257 }
1258 
1259 void SharkBlock::do_multianewarray() {
1260   ShouldNotCallThis();
1261 }
1262 
1263 void SharkBlock::do_monitorenter() {
1264   ShouldNotCallThis();
1265 }
1266 
1267 void SharkBlock::do_monitorexit() {
1268   ShouldNotCallThis();
1269 }