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