Path: blob/master/src/hotspot/share/classfile/bytecodeAssembler.hpp
40949 views
/*1* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_CLASSFILE_BYTECODEASSEMBLER_HPP25#define SHARE_CLASSFILE_BYTECODEASSEMBLER_HPP2627#include "memory/allocation.hpp"28#include "oops/method.hpp"29#include "oops/symbol.hpp"30#include "utilities/globalDefinitions.hpp"31#include "utilities/growableArray.hpp"32#include "utilities/resourceHash.hpp"333435/**36* Bytecode Assembler37*38* These classes are used to synthesize code for creating new methods from39* within the VM. This is only a partial implementation of an assembler;40* only the bytecodes that are needed by clients are implemented at this time.41* This is used during default method analysis to create overpass methods42* and add them to a call during parsing. Other uses (such as creating43* bridges) may come later. Any missing bytecodes can be implemented on an44* as-need basis.45*/4647class BytecodeBuffer : public GrowableArray<u1> {48public:49BytecodeBuffer() : GrowableArray<u1>(20) {}50};5152// Entries in a yet-to-be-created constant pool. Limited types for now.53class BytecodeCPEntry {54public:55enum tag {56ERROR_TAG,57UTF8,58KLASS,59STRING,60NAME_AND_TYPE,61METHODREF62};6364u1 _tag;65union {66Symbol* utf8;67u2 klass;68u2 string;69struct {70u2 name_index;71u2 type_index;72} name_and_type;73struct {74u2 class_index;75u2 name_and_type_index;76} methodref;77uintptr_t hash;78} _u;7980BytecodeCPEntry() : _tag(ERROR_TAG) { _u.hash = 0; }81BytecodeCPEntry(u1 tag) : _tag(tag) { _u.hash = 0; }8283static BytecodeCPEntry utf8(Symbol* symbol) {84BytecodeCPEntry bcpe(UTF8);85bcpe._u.utf8 = symbol;86return bcpe;87}8889static BytecodeCPEntry klass(u2 index) {90BytecodeCPEntry bcpe(KLASS);91bcpe._u.klass = index;92return bcpe;93}9495static BytecodeCPEntry string(u2 index) {96BytecodeCPEntry bcpe(STRING);97bcpe._u.string = index;98return bcpe;99}100101static BytecodeCPEntry name_and_type(u2 name, u2 type) {102BytecodeCPEntry bcpe(NAME_AND_TYPE);103bcpe._u.name_and_type.name_index = name;104bcpe._u.name_and_type.type_index = type;105return bcpe;106}107108static BytecodeCPEntry methodref(u2 class_index, u2 nat) {109BytecodeCPEntry bcpe(METHODREF);110bcpe._u.methodref.class_index = class_index;111bcpe._u.methodref.name_and_type_index = nat;112return bcpe;113}114115static bool equals(BytecodeCPEntry const& e0, BytecodeCPEntry const& e1) {116return e0._tag == e1._tag && e0._u.hash == e1._u.hash;117}118119static unsigned hash(BytecodeCPEntry const& e0) {120return (unsigned)(e0._tag ^ e0._u.hash);121}122};123124class BytecodeConstantPool : ResourceObj {125private:126typedef ResourceHashtable<BytecodeCPEntry, u2,127&BytecodeCPEntry::hash, &BytecodeCPEntry::equals> IndexHash;128129ConstantPool* _orig;130GrowableArray<BytecodeCPEntry> _entries;131IndexHash _indices;132133u2 find_or_add(BytecodeCPEntry const& bcpe);134135public:136137BytecodeConstantPool(ConstantPool* orig) : _orig(orig) {}138139BytecodeCPEntry const& at(u2 index) const { return _entries.at(index); }140141InstanceKlass* pool_holder() const {142return _orig->pool_holder();143}144145u2 utf8(Symbol* sym) {146return find_or_add(BytecodeCPEntry::utf8(sym));147}148149u2 klass(Symbol* class_name) {150return find_or_add(BytecodeCPEntry::klass(utf8(class_name)));151}152153u2 string(Symbol* str) {154return find_or_add(BytecodeCPEntry::string(utf8(str)));155}156157u2 name_and_type(Symbol* name, Symbol* sig) {158return find_or_add(BytecodeCPEntry::name_and_type(utf8(name), utf8(sig)));159}160161u2 methodref(Symbol* class_name, Symbol* name, Symbol* sig) {162return find_or_add(BytecodeCPEntry::methodref(163klass(class_name), name_and_type(name, sig)));164}165166ConstantPool* create_constant_pool(TRAPS) const;167};168169// Partial bytecode assembler - only what we need for creating170// overpass methods for default methods is implemented171class BytecodeAssembler : StackObj {172private:173BytecodeBuffer* _code;174BytecodeConstantPool* _cp;175176void append(u1 imm_u1);177void append(u2 imm_u2);178void append(u4 imm_u4);179180void xload(u4 index, u1 quick, u1 twobyte);181182public:183BytecodeAssembler(BytecodeBuffer* buffer, BytecodeConstantPool* cp)184: _code(buffer), _cp(cp) {}185186void aload(u4 index);187void areturn();188void athrow();189void checkcast(Symbol* sym);190void dload(u4 index);191void dreturn();192void dup();193void fload(u4 index);194void freturn();195void iload(u4 index);196void invokespecial(Method* method);197void invokespecial(Symbol* cls, Symbol* name, Symbol* sig);198void invokevirtual(Method* method);199void invokevirtual(Symbol* cls, Symbol* name, Symbol* sig);200void ireturn();201void ldc(u1 index);202void ldc_w(u2 index);203void lload(u4 index);204void lreturn();205void _new(Symbol* sym);206void _return();207208void load_string(Symbol* sym);209void load(BasicType bt, u4 index);210void _return(BasicType bt);211};212213#endif // SHARE_CLASSFILE_BYTECODEASSEMBLER_HPP214215216