1 /*
  2  * Copyright (c) 1997, 2020, 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 #include "asm/macroAssembler.hpp"
 27 #include "asm/macroAssembler.inline.hpp"
 28 #include "compiler/disassembler.hpp"
 29 #include "interpreter/interpreter.hpp"
 30 #include "interpreter/interpreterRuntime.hpp"
 31 #include "interpreter/interp_masm.hpp"
 32 #include "interpreter/templateTable.hpp"
 33 #include "memory/allocation.inline.hpp"
 34 #include "memory/resourceArea.hpp"
 35 #include "oops/arrayOop.hpp"
 36 #include "oops/methodData.hpp"
 37 #include "oops/method.hpp"
 38 #include "oops/oop.inline.hpp"
 39 #include "prims/forte.hpp"
 40 #include "prims/jvmtiExport.hpp"
 41 #include "prims/methodHandles.hpp"
 42 #include "runtime/handles.inline.hpp"
 43 #include "runtime/sharedRuntime.hpp"
 44 #include "runtime/stubRoutines.hpp"
 45 #include "runtime/timer.hpp"
 46 
 47 # define __ _masm->
 48 
 49 
 50 //------------------------------------------------------------------------------
 51 // Implementation of InterpreterCodelet
 52 
 53 void InterpreterCodelet::initialize(const char* description, Kind kind, Bytecodes::Code bytecode) {
 54   _description = description;
 55   _kind        = kind;
 56   _bytecode    = bytecode;
 57 #ifndef PRODUCT
 58   AsmRemarks* arp = new(&_asm_remarks) AsmRemarks();
 59   DbgStrings* dsp = new(&_dbg_strings) DbgStrings();
 60   postcond(arp == &_asm_remarks);
 61   postcond(dsp == &_dbg_strings);
 62 #endif
 63 }
 64 
 65 void InterpreterCodelet::verify() {}
 66 
 67 void InterpreterCodelet::print_on(outputStream* st) const {
 68   ttyLocker ttyl;
 69 
 70   if (PrintInterpreter) {
 71     st->cr();
 72     st->print_cr("----------------------------------------------------------------------");
 73   }
 74 
 75   if (description() != NULL) st->print("%s  ", description());
 76   if (bytecode()    >= 0   ) st->print("%d %s  ", bytecode(), Bytecodes::name(bytecode()));
 77   st->print_cr("[" INTPTR_FORMAT ", " INTPTR_FORMAT "]  %d bytes",
 78                 p2i(code_begin()), p2i(code_end()), code_size());
 79 
 80   if (PrintInterpreter) {
 81     st->cr();
 82     Disassembler::decode(code_begin(), code_end(), st NOT_PRODUCT(COMMA &_asm_remarks));
 83   }
 84 }
 85 
 86 void InterpreterCodelet::print() const { print_on(tty); }
 87 
 88 CodeletMark::CodeletMark(InterpreterMacroAssembler*& masm,
 89                          const char* description,
 90                          InterpreterCodelet::Kind kind,
 91                          Bytecodes::Code bytecode) :
 92   _clet((InterpreterCodelet*)AbstractInterpreter::code()->request(codelet_size())),
 93   _cb(_clet->code_begin(), _clet->code_size()) {
 94   // Request all space (add some slack for Codelet data).
 95   assert(_clet != NULL, "we checked not enough space already");
 96 
 97   // Initialize Codelet attributes.
 98   _clet->initialize(description, kind, bytecode);
 99   // Create assembler for code generation.
100   masm = new InterpreterMacroAssembler(&_cb);
101   _masm = &masm;
102 }
103 
104 CodeletMark::~CodeletMark() {
105   // Align so printing shows nop's instead of random code at the end (Codelets are aligned).
106   (*_masm)->align(wordSize);
107   // Make sure all code is in code buffer.
108   (*_masm)->flush();
109 
110   // Commit Codelet.
111   int committed_code_size = (*_masm)->code()->pure_insts_size();
112   if (committed_code_size > 0) {
113     // This is  the ONE place where  we pickup any assembly  remarks and debug
114     // strings, and propagate these to the codelet.
115     NOT_PRODUCT(_clet->use_remarks((*_masm)->code()->asm_remarks()));
116     NOT_PRODUCT(_clet->use_strings((*_masm)->code()->dbg_strings()));
117 
118     AbstractInterpreter::code()->commit(committed_code_size);
119   }
120   // Make sure nobody can use _masm outside a CodeletMark lifespan.
121   *_masm = NULL;
122 }
123 
124 // The reason that interpreter initialization is split into two parts is that the first part
125 // needs to run before methods are loaded (which with CDS implies linked also), and the other
126 // part needs to run after. The reason is that when methods are loaded (with CDS) or linked
127 // (without CDS), the i2c adapters are generated that assert we are currently in the interpreter.
128 // Asserting that requires knowledge about where the interpreter is in memory. Therefore,
129 // establishing the interpreter address must be done before methods are loaded. However,
130 // we would like to actually generate the interpreter after methods are loaded. That allows
131 // us to remove otherwise hardcoded offsets regarding fields that are needed in the interpreter
132 // code. This leads to a split if 1. reserving the memory for the interpreter, 2. loading methods
133 // and 3. generating the interpreter.
134 void interpreter_init_stub() {
135   Interpreter::initialize_stub();
136 }
137 
138 void interpreter_init_code() {
139   Interpreter::initialize_code();
140 #ifndef PRODUCT
141   if (TraceBytecodes) BytecodeTracer::set_closure(BytecodeTracer::std_closure());
142 #endif // PRODUCT
143   // need to hit every safepoint in order to call zapping routine
144   // register the interpreter
145   Forte::register_stub(
146     "Interpreter",
147     AbstractInterpreter::code()->code_start(),
148     AbstractInterpreter::code()->code_end()
149   );
150 
151   // notify JVMTI profiler
152   if (JvmtiExport::should_post_dynamic_code_generated()) {
153     JvmtiExport::post_dynamic_code_generated("Interpreter",
154                                              AbstractInterpreter::code()->code_start(),
155                                              AbstractInterpreter::code()->code_end());
156   }
157 }