aoqi@0: /* aoqi@0: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_CLASSFILE_BYTECODEASSEMBLER_HPP aoqi@0: #define SHARE_VM_CLASSFILE_BYTECODEASSEMBLER_HPP aoqi@0: aoqi@0: #include "memory/allocation.hpp" aoqi@0: #include "oops/method.hpp" aoqi@0: #include "oops/symbol.hpp" aoqi@0: #include "utilities/globalDefinitions.hpp" aoqi@0: #include "utilities/growableArray.hpp" aoqi@0: #include "utilities/resourceHash.hpp" aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Bytecode Assembler aoqi@0: * aoqi@0: * These classes are used to synthesize code for creating new methods from aoqi@0: * within the VM. This is only a partial implementation of an assembler; aoqi@0: * only the bytecodes that are needed by clients are implemented at this time. aoqi@0: * This is used during default method analysis to create overpass methods aoqi@0: * and add them to a call during parsing. Other uses (such as creating aoqi@0: * bridges) may come later. Any missing bytecodes can be implemented on an aoqi@0: * as-need basis. aoqi@0: */ aoqi@0: aoqi@0: class BytecodeBuffer : public GrowableArray { aoqi@0: public: aoqi@0: BytecodeBuffer() : GrowableArray(20) {} aoqi@0: }; aoqi@0: aoqi@0: // Entries in a yet-to-be-created constant pool. Limited types for now. aoqi@0: class BytecodeCPEntry VALUE_OBJ_CLASS_SPEC { aoqi@0: public: aoqi@0: enum tag { aoqi@0: ERROR_TAG, aoqi@0: UTF8, aoqi@0: KLASS, aoqi@0: STRING, aoqi@0: NAME_AND_TYPE, aoqi@0: METHODREF aoqi@0: }; aoqi@0: aoqi@0: u1 _tag; aoqi@0: union { aoqi@0: Symbol* utf8; aoqi@0: u2 klass; aoqi@0: u2 string; aoqi@0: struct { aoqi@0: u2 name_index; aoqi@0: u2 type_index; aoqi@0: } name_and_type; aoqi@0: struct { aoqi@0: u2 class_index; aoqi@0: u2 name_and_type_index; aoqi@0: } methodref; aoqi@0: uintptr_t hash; aoqi@0: } _u; aoqi@0: aoqi@0: BytecodeCPEntry() : _tag(ERROR_TAG) { _u.hash = 0; } aoqi@0: BytecodeCPEntry(u1 tag) : _tag(tag) { _u.hash = 0; } aoqi@0: aoqi@0: static BytecodeCPEntry utf8(Symbol* symbol) { aoqi@0: BytecodeCPEntry bcpe(UTF8); aoqi@0: bcpe._u.utf8 = symbol; aoqi@0: return bcpe; aoqi@0: } aoqi@0: aoqi@0: static BytecodeCPEntry klass(u2 index) { aoqi@0: BytecodeCPEntry bcpe(KLASS); aoqi@0: bcpe._u.klass = index; aoqi@0: return bcpe; aoqi@0: } aoqi@0: aoqi@0: static BytecodeCPEntry string(u2 index) { aoqi@0: BytecodeCPEntry bcpe(STRING); aoqi@0: bcpe._u.string = index; aoqi@0: return bcpe; aoqi@0: } aoqi@0: aoqi@0: static BytecodeCPEntry name_and_type(u2 name, u2 type) { aoqi@0: BytecodeCPEntry bcpe(NAME_AND_TYPE); aoqi@0: bcpe._u.name_and_type.name_index = name; aoqi@0: bcpe._u.name_and_type.type_index = type; aoqi@0: return bcpe; aoqi@0: } aoqi@0: aoqi@0: static BytecodeCPEntry methodref(u2 class_index, u2 nat) { aoqi@0: BytecodeCPEntry bcpe(METHODREF); aoqi@0: bcpe._u.methodref.class_index = class_index; aoqi@0: bcpe._u.methodref.name_and_type_index = nat; aoqi@0: return bcpe; aoqi@0: } aoqi@0: aoqi@0: static bool equals(BytecodeCPEntry const& e0, BytecodeCPEntry const& e1) { aoqi@0: return e0._tag == e1._tag && e0._u.hash == e1._u.hash; aoqi@0: } aoqi@0: aoqi@0: static unsigned hash(BytecodeCPEntry const& e0) { aoqi@0: return (unsigned)(e0._tag ^ e0._u.hash); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: class BytecodeConstantPool : ResourceObj { aoqi@0: private: aoqi@0: typedef ResourceHashtable IndexHash; aoqi@0: aoqi@0: ConstantPool* _orig; aoqi@0: GrowableArray _entries; aoqi@0: IndexHash _indices; aoqi@0: aoqi@0: u2 find_or_add(BytecodeCPEntry const& bcpe); aoqi@0: aoqi@0: public: aoqi@0: aoqi@0: BytecodeConstantPool(ConstantPool* orig) : _orig(orig) {} aoqi@0: aoqi@0: BytecodeCPEntry const& at(u2 index) const { return _entries.at(index); } aoqi@0: aoqi@0: InstanceKlass* pool_holder() const { aoqi@0: return InstanceKlass::cast(_orig->pool_holder()); aoqi@0: } aoqi@0: aoqi@0: u2 utf8(Symbol* sym) { aoqi@0: return find_or_add(BytecodeCPEntry::utf8(sym)); aoqi@0: } aoqi@0: aoqi@0: u2 klass(Symbol* class_name) { aoqi@0: return find_or_add(BytecodeCPEntry::klass(utf8(class_name))); aoqi@0: } aoqi@0: aoqi@0: u2 string(Symbol* str) { aoqi@0: return find_or_add(BytecodeCPEntry::string(utf8(str))); aoqi@0: } aoqi@0: aoqi@0: u2 name_and_type(Symbol* name, Symbol* sig) { aoqi@0: return find_or_add(BytecodeCPEntry::name_and_type(utf8(name), utf8(sig))); aoqi@0: } aoqi@0: aoqi@0: u2 methodref(Symbol* class_name, Symbol* name, Symbol* sig) { aoqi@0: return find_or_add(BytecodeCPEntry::methodref( aoqi@0: klass(class_name), name_and_type(name, sig))); aoqi@0: } aoqi@0: aoqi@0: ConstantPool* create_constant_pool(TRAPS) const; aoqi@0: }; aoqi@0: aoqi@0: // Partial bytecode assembler - only what we need for creating aoqi@0: // overpass methods for default methods is implemented aoqi@0: class BytecodeAssembler : StackObj { aoqi@0: private: aoqi@0: BytecodeBuffer* _code; aoqi@0: BytecodeConstantPool* _cp; aoqi@0: aoqi@0: void append(u1 imm_u1); aoqi@0: void append(u2 imm_u2); aoqi@0: void append(u4 imm_u4); aoqi@0: aoqi@0: void xload(u4 index, u1 quick, u1 twobyte); aoqi@0: aoqi@0: public: aoqi@0: BytecodeAssembler(BytecodeBuffer* buffer, BytecodeConstantPool* cp) aoqi@0: : _code(buffer), _cp(cp) {} aoqi@0: aoqi@0: void aload(u4 index); aoqi@0: void areturn(); aoqi@0: void athrow(); aoqi@0: void checkcast(Symbol* sym); aoqi@0: void dload(u4 index); aoqi@0: void dreturn(); aoqi@0: void dup(); aoqi@0: void fload(u4 index); aoqi@0: void freturn(); aoqi@0: void iload(u4 index); aoqi@0: void invokespecial(Method* method); aoqi@0: void invokespecial(Symbol* cls, Symbol* name, Symbol* sig); aoqi@0: void invokevirtual(Method* method); aoqi@0: void invokevirtual(Symbol* cls, Symbol* name, Symbol* sig); aoqi@0: void ireturn(); aoqi@0: void ldc(u1 index); aoqi@0: void ldc_w(u2 index); aoqi@0: void lload(u4 index); aoqi@0: void lreturn(); aoqi@0: void _new(Symbol* sym); aoqi@0: void _return(); aoqi@0: aoqi@0: void load_string(Symbol* sym); aoqi@0: void load(BasicType bt, u4 index); aoqi@0: void _return(BasicType bt); aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_CLASSFILE_BYTECODEASSEMBLER_HPP