1 /*
   2  * Copyright (c) 2012, 2017, 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(), 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->klass_index_at_put(
  68             idx, 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 
  89   cp->initialize_unresolved_klasses(_orig->pool_holder()->class_loader_data(),
  90                                     CHECK_NULL);
  91   return cp;
  92 }
  93 
  94 void BytecodeAssembler::append(u1 imm_u1) {
  95   _code->append(imm_u1);
  96 }
  97 
  98 void BytecodeAssembler::append(u2 imm_u2) {
  99   _code->append(0);
 100   _code->append(0);
 101   Bytes::put_Java_u2(_code->adr_at(_code->length() - 2), imm_u2);
 102 }
 103 
 104 void BytecodeAssembler::append(u4 imm_u4) {
 105   _code->append(0);
 106   _code->append(0);
 107   _code->append(0);
 108   _code->append(0);
 109   Bytes::put_Java_u4(_code->adr_at(_code->length() - 4), imm_u4);
 110 }
 111 
 112 void BytecodeAssembler::xload(u4 index, u1 onebyteop, u1 twobyteop) {
 113   if (index < 4) {
 114     _code->append(onebyteop + index);
 115   } else {
 116     _code->append(twobyteop);
 117     _code->append((u2)index);
 118   }
 119 }
 120 
 121 void BytecodeAssembler::dup() {
 122   _code->append(Bytecodes::_dup);
 123 }
 124 
 125 void BytecodeAssembler::_new(Symbol* sym) {
 126   u2 cpool_index = _cp->klass(sym);
 127   _code->append(Bytecodes::_new);
 128   append(cpool_index);
 129 }
 130 
 131 void BytecodeAssembler::load_string(Symbol* sym) {
 132   u2 cpool_index = _cp->string(sym);
 133   if (cpool_index < 0x100) {
 134     ldc(cpool_index);
 135   } else {
 136     ldc_w(cpool_index);
 137   }
 138 }
 139 
 140 void BytecodeAssembler::ldc(u1 index) {
 141   _code->append(Bytecodes::_ldc);
 142   append(index);
 143 }
 144 
 145 void BytecodeAssembler::ldc_w(u2 index) {
 146   _code->append(Bytecodes::_ldc_w);
 147   append(index);
 148 }
 149 
 150 void BytecodeAssembler::athrow() {
 151   _code->append(Bytecodes::_athrow);
 152 }
 153 
 154 void BytecodeAssembler::iload(u4 index) {
 155   xload(index, Bytecodes::_iload_0, Bytecodes::_iload);
 156 }
 157 
 158 void BytecodeAssembler::lload(u4 index) {
 159   xload(index, Bytecodes::_lload_0, Bytecodes::_lload);
 160 }
 161 
 162 void BytecodeAssembler::fload(u4 index) {
 163   xload(index, Bytecodes::_fload_0, Bytecodes::_fload);
 164 }
 165 
 166 void BytecodeAssembler::dload(u4 index) {
 167   xload(index, Bytecodes::_dload_0, Bytecodes::_dload);
 168 }
 169 
 170 void BytecodeAssembler::aload(u4 index) {
 171   xload(index, Bytecodes::_aload_0, Bytecodes::_aload);
 172 }
 173 
 174 void BytecodeAssembler::load(BasicType bt, u4 index) {
 175   switch (bt) {
 176     case T_BOOLEAN:
 177     case T_CHAR:
 178     case T_BYTE:
 179     case T_SHORT:
 180     case T_INT:     iload(index); break;
 181     case T_FLOAT:   fload(index); break;
 182     case T_DOUBLE:  dload(index); break;
 183     case T_LONG:    lload(index); break;
 184     case T_OBJECT:
 185     case T_ARRAY:   aload(index); break;
 186     default:
 187       ShouldNotReachHere();
 188   }
 189 }
 190 
 191 void BytecodeAssembler::checkcast(Symbol* sym) {
 192   u2 cpool_index = _cp->klass(sym);
 193   _code->append(Bytecodes::_checkcast);
 194   append(cpool_index);
 195 }
 196 
 197 void BytecodeAssembler::invokespecial(Method* method) {
 198   invokespecial(method->klass_name(), method->name(), method->signature());
 199 }
 200 
 201 void BytecodeAssembler::invokespecial(Symbol* klss, Symbol* name, Symbol* sig) {
 202   u2 methodref_index = _cp->methodref(klss, name, sig);
 203   _code->append(Bytecodes::_invokespecial);
 204   append(methodref_index);
 205 }
 206 
 207 void BytecodeAssembler::invokevirtual(Method* method) {
 208   invokevirtual(method->klass_name(), method->name(), method->signature());
 209 }
 210 
 211 void BytecodeAssembler::invokevirtual(Symbol* klss, Symbol* name, Symbol* sig) {
 212   u2 methodref_index = _cp->methodref(klss, name, sig);
 213   _code->append(Bytecodes::_invokevirtual);
 214   append(methodref_index);
 215 }
 216 
 217 void BytecodeAssembler::ireturn() {
 218   _code->append(Bytecodes::_ireturn);
 219 }
 220 
 221 void BytecodeAssembler::lreturn() {
 222   _code->append(Bytecodes::_lreturn);
 223 }
 224 
 225 void BytecodeAssembler::freturn() {
 226   _code->append(Bytecodes::_freturn);
 227 }
 228 
 229 void BytecodeAssembler::dreturn() {
 230   _code->append(Bytecodes::_dreturn);
 231 }
 232 
 233 void BytecodeAssembler::areturn() {
 234   _code->append(Bytecodes::_areturn);
 235 }
 236 
 237 void BytecodeAssembler::_return() {
 238   _code->append(Bytecodes::_return);
 239 }
 240 
 241 void BytecodeAssembler::_return(BasicType bt) {
 242   switch (bt) {
 243     case T_BOOLEAN:
 244     case T_CHAR:
 245     case T_BYTE:
 246     case T_SHORT:
 247     case T_INT:     ireturn(); break;
 248     case T_FLOAT:   freturn(); break;
 249     case T_DOUBLE:  dreturn(); break;
 250     case T_LONG:    lreturn(); break;
 251     case T_OBJECT:
 252     case T_ARRAY:   areturn(); break;
 253     case T_VOID:    _return(); break;
 254     default:
 255       ShouldNotReachHere();
 256   }
 257 }