1 /*
2  * Copyright (c) 1997, 2016, 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/bytecodeHistogram.hpp"
30 #include "interpreter/bytecodeInterpreter.hpp"
31 #include "interpreter/interpreter.hpp"
32 #include "interpreter/interpreterRuntime.hpp"
33 #include "interpreter/interp_masm.hpp"
34 #include "interpreter/templateTable.hpp"
35 #include "memory/allocation.inline.hpp"
36 #include "memory/resourceArea.hpp"
37 #include "oops/arrayOop.hpp"
38 #include "oops/methodData.hpp"
39 #include "oops/method.hpp"
40 #include "oops/oop.inline.hpp"
41 #include "prims/forte.hpp"
42 #include "prims/jvmtiExport.hpp"
43 #include "prims/methodHandles.hpp"
44 #include "runtime/handles.inline.hpp"
45 #include "runtime/sharedRuntime.hpp"
46 #include "runtime/stubRoutines.hpp"
47 #include "runtime/timer.hpp"
48 
49 # define __ _masm->
50 
51 
52 //------------------------------------------------------------------------------------------------------------------------
53 // Implementation of InterpreterCodelet
54 
initialize(const char * description,Bytecodes::Code bytecode)55 void InterpreterCodelet::initialize(const char* description, Bytecodes::Code bytecode) {
56   _description       = description;
57   _bytecode          = bytecode;
58 }
59 
60 
verify()61 void InterpreterCodelet::verify() {
62 }
63 
64 
print_on(outputStream * st) const65 void InterpreterCodelet::print_on(outputStream* st) const {
66   ttyLocker ttyl;
67 
68   if (PrintInterpreter) {
69     st->cr();
70     st->print_cr("----------------------------------------------------------------------");
71   }
72 
73   if (description() != NULL) st->print("%s  ", description());
74   if (bytecode()    >= 0   ) st->print("%d %s  ", bytecode(), Bytecodes::name(bytecode()));
75   st->print_cr("[" INTPTR_FORMAT ", " INTPTR_FORMAT "]  %d bytes",
76                 p2i(code_begin()), p2i(code_end()), code_size());
77 
78   if (PrintInterpreter) {
79     st->cr();
80     Disassembler::decode(code_begin(), code_end(), st, DEBUG_ONLY(_strings) NOT_DEBUG(CodeStrings()));
81   }
82 }
83 
CodeletMark(InterpreterMacroAssembler * & masm,const char * description,Bytecodes::Code bytecode)84 CodeletMark::CodeletMark(InterpreterMacroAssembler*& masm,
85                          const char* description,
86                          Bytecodes::Code bytecode) :
87   _clet((InterpreterCodelet*)AbstractInterpreter::code()->request(codelet_size())),
88   _cb(_clet->code_begin(), _clet->code_size()) {
89   // Request all space (add some slack for Codelet data).
90   assert(_clet != NULL, "we checked not enough space already");
91 
92   // Initialize Codelet attributes.
93   _clet->initialize(description, bytecode);
94   // Create assembler for code generation.
95   masm = new InterpreterMacroAssembler(&_cb);
96   _masm = &masm;
97 }
98 
~CodeletMark()99 CodeletMark::~CodeletMark() {
100   // Align so printing shows nop's instead of random code at the end (Codelets are aligned).
101   (*_masm)->align(wordSize);
102   // Make sure all code is in code buffer.
103   (*_masm)->flush();
104 
105   // Commit Codelet.
106   int committed_code_size = (*_masm)->code()->pure_insts_size();
107   if (committed_code_size) {
108     AbstractInterpreter::code()->commit(committed_code_size, (*_masm)->code()->strings());
109   }
110   // Make sure nobody can use _masm outside a CodeletMark lifespan.
111   *_masm = NULL;
112 }
113 
114 
interpreter_init()115 void interpreter_init() {
116   Interpreter::initialize();
117 #ifndef PRODUCT
118   if (TraceBytecodes) BytecodeTracer::set_closure(BytecodeTracer::std_closure());
119 #endif // PRODUCT
120   // need to hit every safepoint in order to call zapping routine
121   // register the interpreter
122   Forte::register_stub(
123     "Interpreter",
124     AbstractInterpreter::code()->code_start(),
125     AbstractInterpreter::code()->code_end()
126   );
127 
128   // notify JVMTI profiler
129   if (JvmtiExport::should_post_dynamic_code_generated()) {
130     JvmtiExport::post_dynamic_code_generated("Interpreter",
131                                              AbstractInterpreter::code()->code_start(),
132                                              AbstractInterpreter::code()->code_end());
133   }
134 }
135