1 /*
   2  * Copyright (c) 2012, 2014, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 
  27 #include "classfile/bytecodeAssembler.hpp"
  28 #include "interpreter/bytecodes.hpp"
  29 #include "memory/oopFactory.hpp"
  30 #include "oops/constantPool.hpp"
  31 #include "utilities/bytes.hpp"
  32 
  33 u2 BytecodeConstantPool::find_or_add(BytecodeCPEntry const& bcpe) {
  34   u2 index;
  35   u2* probe = _indices.get(bcpe);
  36   if (probe == NULL) {
  37     index = _entries.length();
  38     _entries.append(bcpe);
  39     _indices.put(bcpe, index);
  40   } else {
  41     index = *probe;
  42   }
  43   return index + _orig->length();
  44 }
  45 
  46 ConstantPool* BytecodeConstantPool::create_constant_pool(TRAPS) const {
  47   if (_entries.length() == 0) {
  48     return _orig;
  49   }
  50 
  51   ConstantPool* cp = ConstantPool::allocate(
  52       _orig->pool_holder()->class_loader_data(),
  53       _orig->length() + _entries.length(), _orig->patched(), CHECK_NULL);
  54 
  55   cp->set_pool_holder(_orig->pool_holder());
  56   _orig->copy_cp_to(1, _orig->length() - 1, cp, 1, CHECK_NULL);
  57 
  58   for (int i = 0; i < _entries.length(); ++i) {
  59     BytecodeCPEntry entry = _entries.at(i);
  60     int idx = i + _orig->length();
  61     switch (entry._tag) {
  62       case BytecodeCPEntry::UTF8:
  63         entry._u.utf8->increment_refcount();
  64         cp->symbol_at_put(idx, entry._u.utf8);
  65         break;
  66       case BytecodeCPEntry::KLASS:
  67         cp->unresolved_klass_at_put(
  68             idx, cp->symbol_at(entry._u.klass));
  69         break;
  70       case BytecodeCPEntry::STRING:
  71         cp->unresolved_string_at_put(
  72             idx, cp->symbol_at(entry._u.string));
  73         break;
  74       case BytecodeCPEntry::NAME_AND_TYPE:
  75         cp->name_and_type_at_put(idx,
  76             entry._u.name_and_type.name_index,
  77             entry._u.name_and_type.type_index);
  78         break;
  79       case BytecodeCPEntry::METHODREF:
  80         cp->method_at_put(idx,
  81             entry._u.methodref.class_index,
  82             entry._u.methodref.name_and_type_index);
  83         break;
  84       default:
  85         ShouldNotReachHere();
  86     }
  87   }
  88   return cp;
  89 }
  90 
  91 void BytecodeAssembler::append(u1 imm_u1) {
  92   _code->append(imm_u1);
  93 }
  94 
  95 void BytecodeAssembler::append(u2 imm_u2) {
  96   _code->append(0);
  97   _code->append(0);
  98   Bytes::put_Java_u2(_code->adr_at(_code->length() - 2), imm_u2);
  99 }
 100 
 101 void BytecodeAssembler::append(u4 imm_u4) {
 102   _code->append(0);
 103   _code->append(0);
 104   _code->append(0);
 105   _code->append(0);
 106   Bytes::put_Java_u4(_code->adr_at(_code->length() - 4), imm_u4);
 107 }
 108 
 109 void BytecodeAssembler::xload(u4 index, u1 onebyteop, u1 twobyteop) {
 110   if (index < 4) {
 111     _code->append(onebyteop + index);
 112   } else {
 113     _code->append(twobyteop);
 114     _code->append((u2)index);
 115   }
 116 }
 117 
 118 void BytecodeAssembler::dup() {
 119   _code->append(Bytecodes::_dup);
 120 }
 121 
 122 void BytecodeAssembler::_new(Symbol* sym) {
 123   u2 cpool_index = _cp->klass(sym);
 124   _code->append(Bytecodes::_new);
 125   append(cpool_index);
 126 }
 127 
 128 void BytecodeAssembler::load_string(Symbol* sym) {
 129   u2 cpool_index = _cp->string(sym);
 130   if (cpool_index < 0x100) {
 131     ldc(cpool_index);
 132   } else {
 133     ldc_w(cpool_index);
 134   }
 135 }
 136 
 137 void BytecodeAssembler::ldc(u1 index) {
 138   _code->append(Bytecodes::_ldc);
 139   append(index);
 140 }
 141 
 142 void BytecodeAssembler::ldc_w(u2 index) {
 143   _code->append(Bytecodes::_ldc_w);
 144   append(index);
 145 }
 146 
 147 void BytecodeAssembler::athrow() {
 148   _code->append(Bytecodes::_athrow);
 149 }
 150 
 151 void BytecodeAssembler::iload(u4 index) {
 152   xload(index, Bytecodes::_iload_0, Bytecodes::_iload);
 153 }
 154 
 155 void BytecodeAssembler::lload(u4 index) {
 156   xload(index, Bytecodes::_lload_0, Bytecodes::_lload);
 157 }
 158 
 159 void BytecodeAssembler::fload(u4 index) {
 160   xload(index, Bytecodes::_fload_0, Bytecodes::_fload);
 161 }
 162 
 163 void BytecodeAssembler::dload(u4 index) {
 164   xload(index, Bytecodes::_dload_0, Bytecodes::_dload);
 165 }
 166 
 167 void BytecodeAssembler::aload(u4 index) {
 168   xload(index, Bytecodes::_aload_0, Bytecodes::_aload);
 169 }
 170 
 171 void BytecodeAssembler::load(BasicType bt, u4 index) {
 172   switch (bt) {
 173     case T_BOOLEAN:
 174     case T_CHAR:
 175     case T_BYTE:
 176     case T_SHORT:
 177     case T_INT:     iload(index); break;
 178     case T_FLOAT:   fload(index); break;
 179     case T_DOUBLE:  dload(index); break;
 180     case T_LONG:    lload(index); break;
 181     case T_OBJECT:
 182     case T_ARRAY:   aload(index); break;
 183     default:
 184       ShouldNotReachHere();
 185   }
 186 }
 187 
 188 void BytecodeAssembler::checkcast(Symbol* sym) {
 189   u2 cpool_index = _cp->klass(sym);
 190   _code->append(Bytecodes::_checkcast);
 191   append(cpool_index);
 192 }
 193 
 194 void BytecodeAssembler::invokespecial(Method* method) {
 195   invokespecial(method->klass_name(), method->name(), method->signature());
 196 }
 197 
 198 void BytecodeAssembler::invokespecial(Symbol* klss, Symbol* name, Symbol* sig) {
 199   u2 methodref_index = _cp->methodref(klss, name, sig);
 200   _code->append(Bytecodes::_invokespecial);
 201   append(methodref_index);
 202 }
 203 
 204 void BytecodeAssembler::invokevirtual(Method* method) {
 205   invokevirtual(method->klass_name(), method->name(), method->signature());
 206 }
 207 
 208 void BytecodeAssembler::invokevirtual(Symbol* klss, Symbol* name, Symbol* sig) {
 209   u2 methodref_index = _cp->methodref(klss, name, sig);
 210   _code->append(Bytecodes::_invokevirtual);
 211   append(methodref_index);
 212 }
 213 
 214 void BytecodeAssembler::ireturn() {
 215   _code->append(Bytecodes::_ireturn);
 216 }
 217 
 218 void BytecodeAssembler::lreturn() {
 219   _code->append(Bytecodes::_lreturn);
 220 }
 221 
 222 void BytecodeAssembler::freturn() {
 223   _code->append(Bytecodes::_freturn);
 224 }
 225 
 226 void BytecodeAssembler::dreturn() {
 227   _code->append(Bytecodes::_dreturn);
 228 }
 229 
 230 void BytecodeAssembler::areturn() {
 231   _code->append(Bytecodes::_areturn);
 232 }
 233 
 234 void BytecodeAssembler::_return() {
 235   _code->append(Bytecodes::_return);
 236 }
 237 
 238 void BytecodeAssembler::_return(BasicType bt) {
 239   switch (bt) {
 240     case T_BOOLEAN:
 241     case T_CHAR:
 242     case T_BYTE:
 243     case T_SHORT:
 244     case T_INT:     ireturn(); break;
 245     case T_FLOAT:   freturn(); break;
 246     case T_DOUBLE:  dreturn(); break;
 247     case T_LONG:    lreturn(); break;
 248     case T_OBJECT:
 249     case T_ARRAY:   areturn(); break;
 250     case T_VOID:    _return(); break;
 251     default:
 252       ShouldNotReachHere();
 253   }
 254 }