1 /*
   2  * Copyright 1997-2009 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 #include "incls/_precompiled.incl"
  26 #include "incls/_bytecode.cpp.incl"
  27 
  28 // Implementation of Bytecode
  29 // Should eventually get rid of these functions and use ThisRelativeObj methods instead
  30 
  31 void Bytecode::set_code(Bytecodes::Code code) {
  32   Bytecodes::check(code);
  33   *addr_at(0) = u_char(code);
  34 }
  35 
  36 
  37 bool Bytecode::check_must_rewrite() const {
  38   assert(Bytecodes::can_rewrite(code()), "post-check only");
  39 
  40   // Some codes are conditionally rewriting.  Look closely at them.
  41   switch (code()) {
  42   case Bytecodes::_aload_0:
  43     // Even if RewriteFrequentPairs is turned on,
  44     // the _aload_0 code might delay its rewrite until
  45     // a following _getfield rewrites itself.
  46     return false;
  47 
  48   case Bytecodes::_lookupswitch:
  49     return false;  // the rewrite is not done by the interpreter
  50 
  51   case Bytecodes::_new:
  52     // (Could actually look at the class here, but the profit would be small.)
  53     return false;  // the rewrite is not always done
  54   }
  55 
  56   // No other special cases.
  57   return true;
  58 }
  59 
  60 
  61 
  62 // Implementation of Bytecode_tableupswitch
  63 
  64 int Bytecode_tableswitch::dest_offset_at(int i) const {
  65   address x = aligned_addr_at(1);
  66   int x2 = aligned_offset(1 + (3 + i)*jintSize);
  67   int val = java_signed_word_at(x2);
  68   return java_signed_word_at(aligned_offset(1 + (3 + i)*jintSize));
  69 }
  70 
  71 
  72 // Implementation of Bytecode_invoke
  73 
  74 void Bytecode_invoke::verify() const {
  75   Bytecodes::Code bc = adjusted_invoke_code();
  76   assert(is_valid(), "check invoke");
  77 }
  78 
  79 
  80 symbolOop Bytecode_invoke::signature() const {
  81   constantPoolOop constants = method()->constants();
  82   return constants->signature_ref_at(index());
  83 }
  84 
  85 
  86 symbolOop Bytecode_invoke::name() const {
  87   constantPoolOop constants = method()->constants();
  88   return constants->name_ref_at(index());
  89 }
  90 
  91 
  92 BasicType Bytecode_invoke::result_type(Thread *thread) const {
  93   symbolHandle sh(thread, signature());
  94   ResultTypeFinder rts(sh);
  95   rts.iterate();
  96   return rts.type();
  97 }
  98 
  99 
 100 methodHandle Bytecode_invoke::static_target(TRAPS) {
 101   methodHandle m;
 102   KlassHandle resolved_klass;
 103   constantPoolHandle constants(THREAD, _method->constants());
 104 
 105   if (adjusted_invoke_code() != Bytecodes::_invokeinterface) {
 106     LinkResolver::resolve_method(m, resolved_klass, constants, index(), CHECK_(methodHandle()));
 107   } else {
 108     LinkResolver::resolve_interface_method(m, resolved_klass, constants, index(), CHECK_(methodHandle()));
 109   }
 110   return m;
 111 }
 112 
 113 
 114 int Bytecode_invoke::index() const {
 115   // Note:  Rewriter::rewrite changes the Java_u2 of an invokedynamic to a native_u4,
 116   // at the same time it allocates per-call-site CP cache entries.
 117   if (has_giant_index())
 118     return Bytes::get_native_u4(bcp() + 1);
 119   else
 120     return Bytes::get_Java_u2(bcp() + 1);
 121 }
 122 
 123 
 124 // Implementation of Bytecode_static
 125 
 126 void Bytecode_static::verify() const {
 127   assert(Bytecodes::java_code(code()) == Bytecodes::_putstatic
 128       || Bytecodes::java_code(code()) == Bytecodes::_getstatic, "check static");
 129 }
 130 
 131 
 132 BasicType Bytecode_static::result_type(methodOop method) const {
 133   int index = java_hwrd_at(1);
 134   constantPoolOop constants = method->constants();
 135   symbolOop field_type = constants->signature_ref_at(index);
 136   BasicType basic_type = FieldType::basic_type(field_type);
 137   return basic_type;
 138 }
 139 
 140 
 141 // Implementation of Bytecode_field
 142 
 143 void Bytecode_field::verify() const {
 144   Bytecodes::Code stdc = Bytecodes::java_code(code());
 145   assert(stdc == Bytecodes::_putstatic || stdc == Bytecodes::_getstatic ||
 146          stdc == Bytecodes::_putfield  || stdc == Bytecodes::_getfield, "check field");
 147 }
 148 
 149 
 150 bool Bytecode_field::is_static() const {
 151   Bytecodes::Code stdc = Bytecodes::java_code(code());
 152   return stdc == Bytecodes::_putstatic || stdc == Bytecodes::_getstatic;
 153 }
 154 
 155 
 156 int Bytecode_field::index() const {
 157   return java_hwrd_at(1);
 158 }
 159 
 160 
 161 // Implementation of Bytecodes loac constant
 162 
 163 int Bytecode_loadconstant::index() const {
 164   Bytecodes::Code stdc = Bytecodes::java_code(code());
 165   return stdc == Bytecodes::_ldc ? java_byte_at(1) : java_hwrd_at(1);
 166 }
 167 
 168 //------------------------------------------------------------------------------
 169 // Non-product code
 170 
 171 #ifndef PRODUCT
 172 
 173 void Bytecode_lookupswitch::verify() const {
 174   switch (Bytecodes::java_code(code())) {
 175     case Bytecodes::_lookupswitch:
 176       { int i = number_of_pairs() - 1;
 177         while (i-- > 0) {
 178           assert(pair_at(i)->match() < pair_at(i+1)->match(), "unsorted table entries");
 179         }
 180       }
 181       break;
 182     default:
 183       fatal("not a lookupswitch bytecode");
 184   }
 185 }
 186 
 187 void Bytecode_tableswitch::verify() const {
 188   switch (Bytecodes::java_code(code())) {
 189     case Bytecodes::_tableswitch:
 190       { int lo = low_key();
 191         int hi = high_key();
 192         assert (hi >= lo, "incorrect hi/lo values in tableswitch");
 193         int i  = hi - lo - 1 ;
 194         while (i-- > 0) {
 195           // no special check needed
 196         }
 197       }
 198       break;
 199     default:
 200       fatal("not a tableswitch bytecode");
 201   }
 202 }
 203 
 204 #endif