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::_invokedynamic) {
 106     LinkResolver::resolve_dynamic_method(m, resolved_klass, constants, index(), CHECK_(methodHandle()));
 107   } else if (adjusted_invoke_code() != Bytecodes::_invokeinterface) {
 108     LinkResolver::resolve_method(m, resolved_klass, constants, index(), CHECK_(methodHandle()));
 109   } else {
 110     LinkResolver::resolve_interface_method(m, resolved_klass, constants, index(), CHECK_(methodHandle()));
 111   }
 112   return m;
 113 }
 114 
 115 
 116 int Bytecode_invoke::index() const {
 117   // Note:  Rewriter::rewrite changes the Java_u2 of an invokedynamic to a native_u4,
 118   // at the same time it allocates per-call-site CP cache entries.
 119   if (has_giant_index())
 120     return Bytes::get_native_u4(bcp() + 1);
 121   else
 122     return Bytes::get_Java_u2(bcp() + 1);
 123 }
 124 
 125 
 126 // Implementation of Bytecode_static
 127 
 128 void Bytecode_static::verify() const {
 129   assert(Bytecodes::java_code(code()) == Bytecodes::_putstatic
 130       || Bytecodes::java_code(code()) == Bytecodes::_getstatic, "check static");
 131 }
 132 
 133 
 134 BasicType Bytecode_static::result_type(methodOop method) const {
 135   int index = java_hwrd_at(1);
 136   constantPoolOop constants = method->constants();
 137   symbolOop field_type = constants->signature_ref_at(index);
 138   BasicType basic_type = FieldType::basic_type(field_type);
 139   return basic_type;
 140 }
 141 
 142 
 143 // Implementation of Bytecode_field
 144 
 145 void Bytecode_field::verify() const {
 146   Bytecodes::Code stdc = Bytecodes::java_code(code());
 147   assert(stdc == Bytecodes::_putstatic || stdc == Bytecodes::_getstatic ||
 148          stdc == Bytecodes::_putfield  || stdc == Bytecodes::_getfield, "check field");
 149 }
 150 
 151 
 152 bool Bytecode_field::is_static() const {
 153   Bytecodes::Code stdc = Bytecodes::java_code(code());
 154   return stdc == Bytecodes::_putstatic || stdc == Bytecodes::_getstatic;
 155 }
 156 
 157 
 158 int Bytecode_field::index() const {
 159   return java_hwrd_at(1);
 160 }
 161 
 162 
 163 // Implementation of Bytecodes loac constant
 164 
 165 int Bytecode_loadconstant::index() const {
 166   Bytecodes::Code stdc = Bytecodes::java_code(code());
 167   return stdc == Bytecodes::_ldc ? java_byte_at(1) : java_hwrd_at(1);
 168 }
 169 
 170 //------------------------------------------------------------------------------
 171 // Non-product code
 172 
 173 #ifndef PRODUCT
 174 
 175 void Bytecode_lookupswitch::verify() const {
 176   switch (Bytecodes::java_code(code())) {
 177     case Bytecodes::_lookupswitch:
 178       { int i = number_of_pairs() - 1;
 179         while (i-- > 0) {
 180           assert(pair_at(i)->match() < pair_at(i+1)->match(), "unsorted table entries");
 181         }
 182       }
 183       break;
 184     default:
 185       fatal("not a lookupswitch bytecode");
 186   }
 187 }
 188 
 189 void Bytecode_tableswitch::verify() const {
 190   switch (Bytecodes::java_code(code())) {
 191     case Bytecodes::_tableswitch:
 192       { int lo = low_key();
 193         int hi = high_key();
 194         assert (hi >= lo, "incorrect hi/lo values in tableswitch");
 195         int i  = hi - lo - 1 ;
 196         while (i-- > 0) {
 197           // no special check needed
 198         }
 199       }
 200       break;
 201     default:
 202       fatal("not a tableswitch bytecode");
 203   }
 204 }
 205 
 206 #endif