interpreter.hpp revision 1472:c18cbe5936b8
1219820Sjeff/*
2219820Sjeff * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
3219820Sjeff * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4219820Sjeff *
5219820Sjeff * This code is free software; you can redistribute it and/or modify it
6219820Sjeff * under the terms of the GNU General Public License version 2 only, as
7219820Sjeff * published by the Free Software Foundation.
8219820Sjeff *
9219820Sjeff * This code is distributed in the hope that it will be useful, but WITHOUT
10219820Sjeff * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11219820Sjeff * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12219820Sjeff * version 2 for more details (a copy is included in the LICENSE file that
13219820Sjeff * accompanied this code).
14219820Sjeff *
15219820Sjeff * You should have received a copy of the GNU General Public License version
16219820Sjeff * 2 along with this work; if not, write to the Free Software Foundation,
17219820Sjeff * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18219820Sjeff *
19219820Sjeff * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20219820Sjeff * or visit www.oracle.com if you need additional information or have any
21219820Sjeff * questions.
22219820Sjeff *
23219820Sjeff */
24219820Sjeff
25219820Sjeff// This file contains the platform-independent parts
26219820Sjeff// of the interpreter and the interpreter generator.
27219820Sjeff
28219820Sjeff//------------------------------------------------------------------------------------------------------------------------
29219820Sjeff// An InterpreterCodelet is a piece of interpreter code. All
30219820Sjeff// interpreter code is generated into little codelets which
31219820Sjeff// contain extra information for debugging and printing purposes.
32331772Shselasky
33331772Shselaskyclass InterpreterCodelet: public Stub {
34219820Sjeff  friend class VMStructs;
35219820Sjeff private:
36219820Sjeff  int         _size;                             // the size in bytes
37219820Sjeff  const char* _description;                      // a description of the codelet, for debugging & printing
38219820Sjeff  Bytecodes::Code _bytecode;                     // associated bytecode if any
39219820Sjeff
40219820Sjeff public:
41219820Sjeff  // Initialization/finalization
42219820Sjeff  void    initialize(int size)                   { _size = size; }
43219820Sjeff  void    finalize()                             { ShouldNotCallThis(); }
44219820Sjeff
45219820Sjeff  // General info/converters
46219820Sjeff  int     size() const                           { return _size; }
47219820Sjeff  static  int code_size_to_size(int code_size)   { return round_to(sizeof(InterpreterCodelet), CodeEntryAlignment) + code_size; }
48219820Sjeff
49219820Sjeff  // Code info
50219820Sjeff  address code_begin() const                     { return (address)this + round_to(sizeof(InterpreterCodelet), CodeEntryAlignment); }
51219820Sjeff  address code_end() const                       { return (address)this + size(); }
52219820Sjeff
53219820Sjeff  // Debugging
54219820Sjeff  void    verify();
55219820Sjeff  void    print();
56219820Sjeff
57219820Sjeff  // Interpreter-specific initialization
58219820Sjeff  void    initialize(const char* description, Bytecodes::Code bytecode);
59219820Sjeff
60219820Sjeff  // Interpreter-specific attributes
61219820Sjeff  int         code_size() const                  { return code_end() - code_begin(); }
62219820Sjeff  const char* description() const                { return _description; }
63219820Sjeff  Bytecodes::Code bytecode() const               { return _bytecode; }
64331769Shselasky};
65219820Sjeff
66219820Sjeff// Define a prototype interface
67331769ShselaskyDEF_STUB_INTERFACE(InterpreterCodelet);
68331769Shselasky
69219820Sjeff
70219820Sjeff//------------------------------------------------------------------------------------------------------------------------
71219820Sjeff// A CodeletMark serves as an automatic creator/initializer for Codelets
72278886Shselasky// (As a subclass of ResourceMark it automatically GC's the allocated
73219820Sjeff// code buffer and assemblers).
74219820Sjeff
75219820Sjeffclass CodeletMark: ResourceMark {
76219820Sjeff private:
77331769Shselasky  InterpreterCodelet*         _clet;
78331769Shselasky  InterpreterMacroAssembler** _masm;
79331769Shselasky  CodeBuffer                  _cb;
80331769Shselasky
81278886Shselasky  int codelet_size() {
82219820Sjeff    // Request the whole code buffer (minus a little for alignment).
83219820Sjeff    // The commit call below trims it back for each codelet.
84219820Sjeff    int codelet_size = AbstractInterpreter::code()->available_space() - 2*K;
85219820Sjeff
86219820Sjeff    // Guarantee there's a little bit of code space left.
87219820Sjeff    guarantee (codelet_size > 0 && (size_t)codelet_size >  2*K,
88219820Sjeff               "not enough space for interpreter generation");
89219820Sjeff
90219820Sjeff    return codelet_size;
91219820Sjeff  }
92219820Sjeff
93219820Sjeff public:
94219820Sjeff  CodeletMark(
95219820Sjeff    InterpreterMacroAssembler*& masm,
96219820Sjeff    const char* description,
97219820Sjeff    Bytecodes::Code bytecode = Bytecodes::_illegal):
98219820Sjeff    _clet((InterpreterCodelet*)AbstractInterpreter::code()->request(codelet_size())),
99219820Sjeff    _cb(_clet->code_begin(), _clet->code_size())
100219820Sjeff
101219820Sjeff  { // request all space (add some slack for Codelet data)
102219820Sjeff    assert (_clet != NULL, "we checked not enough space already");
103219820Sjeff
104219820Sjeff    // initialize Codelet attributes
105331769Shselasky    _clet->initialize(description, bytecode);
106219820Sjeff    // create assembler for code generation
107219820Sjeff    masm  = new InterpreterMacroAssembler(&_cb);
108219820Sjeff    _masm = &masm;
109219820Sjeff  }
110219820Sjeff
111219820Sjeff  ~CodeletMark() {
112219820Sjeff    // align so printing shows nop's instead of random code at the end (Codelets are aligned)
113219820Sjeff    (*_masm)->align(wordSize);
114219820Sjeff    // make sure all code is in code buffer
115219820Sjeff    (*_masm)->flush();
116219820Sjeff
117219820Sjeff
118219820Sjeff    // commit Codelet
119219820Sjeff    AbstractInterpreter::code()->commit((*_masm)->code()->pure_code_size());
120219820Sjeff    // make sure nobody can use _masm outside a CodeletMark lifespan
121219820Sjeff    *_masm = NULL;
122219820Sjeff  }
123219820Sjeff};
124219820Sjeff
125278886Shselasky// Wrapper classes to produce Interpreter/InterpreterGenerator from either
126278886Shselasky// the c++ interpreter or the template interpreter.
127278886Shselasky
128278886Shselaskyclass Interpreter: public CC_INTERP_ONLY(CppInterpreter) NOT_CC_INTERP(TemplateInterpreter) {
129278886Shselasky
130278886Shselasky  public:
131278886Shselasky  // Debugging/printing
132278886Shselasky  static InterpreterCodelet* codelet_containing(address pc)     { return (InterpreterCodelet*)_code->stub_containing(pc); }
133278886Shselasky#include "incls/_interpreter_pd.hpp.incl"
134278886Shselasky};
135278886Shselasky