interpreter.hpp revision 1472:c18cbe5936b8
1/*
2 * Copyright (c) 1997, 2007, 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// This file contains the platform-independent parts
26// of the interpreter and the interpreter generator.
27
28//------------------------------------------------------------------------------------------------------------------------
29// An InterpreterCodelet is a piece of interpreter code. All
30// interpreter code is generated into little codelets which
31// contain extra information for debugging and printing purposes.
32
33class InterpreterCodelet: public Stub {
34  friend class VMStructs;
35 private:
36  int         _size;                             // the size in bytes
37  const char* _description;                      // a description of the codelet, for debugging & printing
38  Bytecodes::Code _bytecode;                     // associated bytecode if any
39
40 public:
41  // Initialization/finalization
42  void    initialize(int size)                   { _size = size; }
43  void    finalize()                             { ShouldNotCallThis(); }
44
45  // General info/converters
46  int     size() const                           { return _size; }
47  static  int code_size_to_size(int code_size)   { return round_to(sizeof(InterpreterCodelet), CodeEntryAlignment) + code_size; }
48
49  // Code info
50  address code_begin() const                     { return (address)this + round_to(sizeof(InterpreterCodelet), CodeEntryAlignment); }
51  address code_end() const                       { return (address)this + size(); }
52
53  // Debugging
54  void    verify();
55  void    print();
56
57  // Interpreter-specific initialization
58  void    initialize(const char* description, Bytecodes::Code bytecode);
59
60  // Interpreter-specific attributes
61  int         code_size() const                  { return code_end() - code_begin(); }
62  const char* description() const                { return _description; }
63  Bytecodes::Code bytecode() const               { return _bytecode; }
64};
65
66// Define a prototype interface
67DEF_STUB_INTERFACE(InterpreterCodelet);
68
69
70//------------------------------------------------------------------------------------------------------------------------
71// A CodeletMark serves as an automatic creator/initializer for Codelets
72// (As a subclass of ResourceMark it automatically GC's the allocated
73// code buffer and assemblers).
74
75class CodeletMark: ResourceMark {
76 private:
77  InterpreterCodelet*         _clet;
78  InterpreterMacroAssembler** _masm;
79  CodeBuffer                  _cb;
80
81  int codelet_size() {
82    // Request the whole code buffer (minus a little for alignment).
83    // The commit call below trims it back for each codelet.
84    int codelet_size = AbstractInterpreter::code()->available_space() - 2*K;
85
86    // Guarantee there's a little bit of code space left.
87    guarantee (codelet_size > 0 && (size_t)codelet_size >  2*K,
88               "not enough space for interpreter generation");
89
90    return codelet_size;
91  }
92
93 public:
94  CodeletMark(
95    InterpreterMacroAssembler*& masm,
96    const char* description,
97    Bytecodes::Code bytecode = Bytecodes::_illegal):
98    _clet((InterpreterCodelet*)AbstractInterpreter::code()->request(codelet_size())),
99    _cb(_clet->code_begin(), _clet->code_size())
100
101  { // request all space (add some slack for Codelet data)
102    assert (_clet != NULL, "we checked not enough space already");
103
104    // initialize Codelet attributes
105    _clet->initialize(description, bytecode);
106    // create assembler for code generation
107    masm  = new InterpreterMacroAssembler(&_cb);
108    _masm = &masm;
109  }
110
111  ~CodeletMark() {
112    // align so printing shows nop's instead of random code at the end (Codelets are aligned)
113    (*_masm)->align(wordSize);
114    // make sure all code is in code buffer
115    (*_masm)->flush();
116
117
118    // commit Codelet
119    AbstractInterpreter::code()->commit((*_masm)->code()->pure_code_size());
120    // make sure nobody can use _masm outside a CodeletMark lifespan
121    *_masm = NULL;
122  }
123};
124
125// Wrapper classes to produce Interpreter/InterpreterGenerator from either
126// the c++ interpreter or the template interpreter.
127
128class Interpreter: public CC_INTERP_ONLY(CppInterpreter) NOT_CC_INTERP(TemplateInterpreter) {
129
130  public:
131  // Debugging/printing
132  static InterpreterCodelet* codelet_containing(address pc)     { return (InterpreterCodelet*)_code->stub_containing(pc); }
133#include "incls/_interpreter_pd.hpp.incl"
134};
135