abstractInterpreter.hpp revision 1426:2338d41fbd81
191094Sdes/*
2115619Sdes * Copyright 1997-2010 Sun Microsystems, Inc.  All Rights Reserved.
3228690Sdes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
491094Sdes *
591094Sdes * This code is free software; you can redistribute it and/or modify it
691094Sdes * under the terms of the GNU General Public License version 2 only, as
799158Sdes * published by the Free Software Foundation.
899158Sdes *
999158Sdes * This code is distributed in the hope that it will be useful, but WITHOUT
1091094Sdes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1191094Sdes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1291094Sdes * version 2 for more details (a copy is included in the LICENSE file that
1391094Sdes * accompanied this code).
1491094Sdes *
1591094Sdes * You should have received a copy of the GNU General Public License version
1691094Sdes * 2 along with this work; if not, write to the Free Software Foundation,
1791094Sdes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1891094Sdes *
1991094Sdes * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
2091094Sdes * CA 95054 USA or visit www.sun.com if you need additional information or
2191094Sdes * have any questions.
2291094Sdes *
2391094Sdes */
2491094Sdes
2591094Sdes// This file contains the platform-independent parts
2691094Sdes// of the abstract interpreter and the abstract interpreter generator.
2791094Sdes
2891094Sdes// Organization of the interpreter(s). There exists two different interpreters in hotpot
2991094Sdes// an assembly language version (aka template interpreter) and a high level language version
3091094Sdes// (aka c++ interpreter). Th division of labor is as follows:
3191094Sdes
3291094Sdes// Template Interpreter          C++ Interpreter        Functionality
3391094Sdes//
3491094Sdes// templateTable*                bytecodeInterpreter*   actual interpretation of bytecodes
35271947Sdes//
3691094Sdes// templateInterpreter*          cppInterpreter*        generation of assembly code that creates
3791094Sdes//                                                      and manages interpreter runtime frames.
38228690Sdes//                                                      Also code for populating interpreter
39228690Sdes//                                                      frames created during deoptimization.
40228690Sdes//
41228690Sdes// For both template and c++ interpreter. There are common files for aspects of the interpreter
4291094Sdes// that are generic to both interpreters. This is the layout:
4391094Sdes//
4491094Sdes// abstractInterpreter.hpp: generic description of the interpreter.
4591094Sdes// interpreter*:            generic frame creation and handling.
4691094Sdes//
4791094Sdes
4891094Sdes//------------------------------------------------------------------------------------------------------------------------
49228690Sdes// The C++ interface to the bytecode interpreter(s).
5091094Sdes
51228690Sdesclass AbstractInterpreter: AllStatic {
5291094Sdes  friend class VMStructs;
5391094Sdes  friend class Interpreter;
5491094Sdes  friend class CppInterpreterGenerator;
5591100Sdes public:
5691100Sdes  enum MethodKind {
5791094Sdes    zerolocals,                                                 // method needs locals initialization
5891094Sdes    zerolocals_synchronized,                                    // method needs locals initialization & is synchronized
5991094Sdes    native,                                                     // native method
6091094Sdes    native_synchronized,                                        // native method & is synchronized
6191094Sdes    empty,                                                      // empty method (code: _return)
6291094Sdes    accessor,                                                   // accessor method (code: _aload_0, _getfield, _(a|i)return)
6391094Sdes    abstract,                                                   // abstract method (throws an AbstractMethodException)
6491094Sdes    method_handle,                                              // java.dyn.MethodHandles::invoke
6591094Sdes    java_lang_math_sin,                                         // implementation of java.lang.Math.sin   (x)
66271947Sdes    java_lang_math_cos,                                         // implementation of java.lang.Math.cos   (x)
67114536Sdes    java_lang_math_tan,                                         // implementation of java.lang.Math.tan   (x)
6891094Sdes    java_lang_math_abs,                                         // implementation of java.lang.Math.abs   (x)
69107937Sdes    java_lang_math_sqrt,                                        // implementation of java.lang.Math.sqrt  (x)
7091094Sdes    java_lang_math_log,                                         // implementation of java.lang.Math.log   (x)
71107937Sdes    java_lang_math_log10,                                       // implementation of java.lang.Math.log10 (x)
7291094Sdes    number_of_method_entries,
7391094Sdes    invalid = -1
7491094Sdes  };
75107937Sdes
76107937Sdes  enum SomeConstants {
77228690Sdes    number_of_result_handlers = 10                              // number of result handlers for native calls
78107937Sdes  };
79228690Sdes
80107937Sdes protected:
8191094Sdes  static StubQueue* _code;                                      // the interpreter code (codelets)
8291094Sdes
8391094Sdes  static bool       _notice_safepoints;                         // true if safepoints are activated
8491094Sdes
8591094Sdes  static address    _native_entry_begin;                        // Region for native entry code
8691094Sdes  static address    _native_entry_end;
8791094Sdes
8891094Sdes  // method entry points
8991094Sdes  static address    _entry_table[number_of_method_entries];     // entry points for a given method
9091094Sdes  static address    _native_abi_to_tosca[number_of_result_handlers];  // for native method result handlers
9191094Sdes  static address    _slow_signature_handler;                              // the native method generic (slow) signature handler
9291094Sdes
9391094Sdes  static address    _rethrow_exception_entry;                   // rethrows an activation in previous frame
9491094Sdes
9591094Sdes  friend class      AbstractInterpreterGenerator;
9691094Sdes  friend class              InterpreterGenerator;
9791094Sdes  friend class      InterpreterMacroAssembler;
9891094Sdes
9991094Sdes public:
100107937Sdes  // Initialization/debugging
10191094Sdes  static void       initialize();
10291094Sdes  static StubQueue* code()                                      { return _code; }
10391094Sdes
104271947Sdes
105271947Sdes  // Method activation
106271947Sdes  static MethodKind method_kind(methodHandle m);
10791094Sdes  static address    entry_for_kind(MethodKind k)                { assert(0 <= k && k < number_of_method_entries, "illegal kind"); return _entry_table[k]; }
10891094Sdes  static address    entry_for_method(methodHandle m)            { return _entry_table[method_kind(m)]; }
109228690Sdes
110228690Sdes  static void       print_method_kind(MethodKind kind)          PRODUCT_RETURN;
11191094Sdes
112107937Sdes  static bool       can_be_compiled(methodHandle m);
11391094Sdes
114114536Sdes  // Runtime support
115114536Sdes
116228690Sdes  // length = invoke bytecode length (to advance to next bytecode)
117255376Sdes  static address    deopt_entry   (TosState state, int length) { ShouldNotReachHere(); return NULL; }
118228690Sdes  static address    return_entry  (TosState state, int length) { ShouldNotReachHere(); return NULL; }
11991094Sdes
120294192Sdes  static address    rethrow_exception_entry()                   { return _rethrow_exception_entry; }
12191094Sdes
122255376Sdes  // Activation size in words for a method that is just being called.
123228690Sdes  // Parameters haven't been pushed so count them too.
12491094Sdes  static int        size_top_interpreter_activation(methodOop method);
125114536Sdes
126228690Sdes  // Deoptimization support
12791094Sdes  // Compute the entry address for continuation after
12891094Sdes  static address deopt_continue_after_entry(methodOop method,
12991094Sdes                                            address bcp,
13091094Sdes                                            int callee_parameters,
131271947Sdes                                            bool is_top_frame);
132271947Sdes  // Compute the entry address for reexecution
13391094Sdes  static address deopt_reexecute_entry(methodOop method, address bcp);
13491684Sdes  // Deoptimization should reexecute this bytecode
13591684Sdes  static bool    bytecode_should_reexecute(Bytecodes::Code code);
13691094Sdes
13791094Sdes  // share implementation of size_activation and layout_activation:
13899158Sdes  static int        size_activation(methodOop method,
13999158Sdes                                    int temps,
14091684Sdes                                    int popframe_args,
14199158Sdes                                    int monitors,
14299158Sdes                                    int callee_params,
14391094Sdes                                    int callee_locals,
14491097Sdes                                    bool is_top_frame);
14591094Sdes
14691094Sdes  static int       layout_activation(methodOop method,
147228690Sdes                                      int temps,
14891094Sdes                                      int popframe_args,
14991094Sdes                                      int monitors,
15091094Sdes                                      int callee_params,
15191094Sdes                                      int callee_locals,
15291094Sdes                                      frame* caller,
15391094Sdes                                      frame* interpreter_frame,
154271947Sdes                                      bool is_top_frame);
15591094Sdes
15699158Sdes  // Runtime support
15799158Sdes  static bool       is_not_reached(                       methodHandle method, int bci);
158255376Sdes  // Safepoint support
15991094Sdes  static void       notice_safepoints()                         { ShouldNotReachHere(); } // stops the thread when reaching a safepoint
16091094Sdes  static void       ignore_safepoints()                         { ShouldNotReachHere(); } // ignores safepoints
16191094Sdes
16291094Sdes  // Support for native calls
16391094Sdes  static address    slow_signature_handler()                    { return _slow_signature_handler; }
16491094Sdes  static address    result_handler(BasicType type)              { return _native_abi_to_tosca[BasicType_as_index(type)]; }
16591094Sdes  static int        BasicType_as_index(BasicType type);         // computes index into result_handler_by_index table
16691094Sdes  static bool       in_native_entry(address pc)                 { return _native_entry_begin <= pc && pc < _native_entry_end; }
16791094Sdes  // Debugging/printing
168255376Sdes  static void       print();                                    // prints the interpreter code
16991094Sdes
17091094Sdes public:
17191094Sdes  // Interpreter helpers
17291094Sdes  const static int stackElementWords   = 1;
17391094Sdes  const static int stackElementSize    = stackElementWords * wordSize;
17499158Sdes  const static int logStackElementSize = LogBytesPerWord;
17591097Sdes
176271947Sdes  // Local values relative to locals[n]
177271947Sdes  static int  local_offset_in_bytes(int n) {
178271947Sdes    return ((frame::interpreter_frame_expression_stack_direction() * n) * stackElementSize);
179271947Sdes  }
180271947Sdes
181271947Sdes  // access to stacked values according to type:
182271947Sdes  static oop* oop_addr_in_slot(intptr_t* slot_addr) {
183271947Sdes    return (oop*) slot_addr;
184271947Sdes  }
185271947Sdes  static jint* int_addr_in_slot(intptr_t* slot_addr) {
186271947Sdes    if ((int) sizeof(jint) < wordSize && !Bytes::is_Java_byte_ordering_different())
187271947Sdes      // big-endian LP64
188107937Sdes      return (jint*)(slot_addr + 1) - 1;
18991094Sdes    else
19091094Sdes      return (jint*) slot_addr;
19191094Sdes  }
19291094Sdes  static jlong long_in_slot(intptr_t* slot_addr) {
193228690Sdes    if (sizeof(intptr_t) >= sizeof(jlong)) {
19491094Sdes      return *(jlong*) slot_addr;
19591094Sdes    } else {
19691097Sdes      return Bytes::get_native_u8((address)slot_addr);
197255376Sdes    }
19891097Sdes  }
19991094Sdes  static void set_long_in_slot(intptr_t* slot_addr, jlong value) {
20091094Sdes    if (sizeof(intptr_t) >= sizeof(jlong)) {
20191097Sdes      *(jlong*) slot_addr = value;
20291097Sdes    } else {
20391094Sdes      Bytes::put_native_u8((address)slot_addr, value);
20491094Sdes    }
20591094Sdes  }
20691094Sdes  static void get_jvalue_in_slot(intptr_t* slot_addr, BasicType type, jvalue* value) {
20791094Sdes    switch (type) {
20891094Sdes    case T_BOOLEAN: value->z = *int_addr_in_slot(slot_addr);            break;
20991094Sdes    case T_CHAR:    value->c = *int_addr_in_slot(slot_addr);            break;
21091094Sdes    case T_BYTE:    value->b = *int_addr_in_slot(slot_addr);            break;
21191094Sdes    case T_SHORT:   value->s = *int_addr_in_slot(slot_addr);            break;
21291094Sdes    case T_INT:     value->i = *int_addr_in_slot(slot_addr);            break;
21391094Sdes    case T_LONG:    value->j = long_in_slot(slot_addr);                 break;
21491094Sdes    case T_FLOAT:   value->f = *(jfloat*)int_addr_in_slot(slot_addr);   break;
21591094Sdes    case T_DOUBLE:  value->d = jdouble_cast(long_in_slot(slot_addr));   break;
21691094Sdes    case T_OBJECT:  value->l = (jobject)*oop_addr_in_slot(slot_addr);   break;
21791094Sdes    default:        ShouldNotReachHere();
21891094Sdes    }
21991094Sdes  }
22091094Sdes  static void set_jvalue_in_slot(intptr_t* slot_addr, BasicType type, jvalue* value) {
22191094Sdes    switch (type) {
22291094Sdes    case T_BOOLEAN: *int_addr_in_slot(slot_addr) = (value->z != 0);     break;
22391094Sdes    case T_CHAR:    *int_addr_in_slot(slot_addr) = value->c;            break;
22491094Sdes    case T_BYTE:    *int_addr_in_slot(slot_addr) = value->b;            break;
22591094Sdes    case T_SHORT:   *int_addr_in_slot(slot_addr) = value->s;            break;
22691094Sdes    case T_INT:     *int_addr_in_slot(slot_addr) = value->i;            break;
22791094Sdes    case T_LONG:    set_long_in_slot(slot_addr, value->j);              break;
22891094Sdes    case T_FLOAT:   *(jfloat*)int_addr_in_slot(slot_addr) = value->f;   break;
22991094Sdes    case T_DOUBLE:  set_long_in_slot(slot_addr, jlong_cast(value->d));  break;
23091094Sdes    case T_OBJECT:  *oop_addr_in_slot(slot_addr) = (oop) value->l;      break;
23191094Sdes    default:        ShouldNotReachHere();
23291094Sdes    }
23391094Sdes  }
23491094Sdes};
23591094Sdes
23691094Sdes//------------------------------------------------------------------------------------------------------------------------
23791094Sdes// The interpreter generator.
23891094Sdes
23991684Sdesclass Template;
24091684Sdesclass AbstractInterpreterGenerator: public StackObj {
24191094Sdes protected:
24291094Sdes  InterpreterMacroAssembler* _masm;
24391094Sdes
24491094Sdes  // shared code sequences
24591094Sdes  // Converter for native abi result to tosca result
246228690Sdes  address generate_result_handler_for(BasicType type);
24791094Sdes  address generate_slow_signature_handler();
24891094Sdes
24991100Sdes  // entry point generator
25091100Sdes  address generate_method_entry(AbstractInterpreter::MethodKind kind);
25191100Sdes
25291100Sdes  void bang_stack_shadow_pages(bool native_call);
25391100Sdes
25491100Sdes  void generate_all();
255
256 public:
257  AbstractInterpreterGenerator(StubQueue* _code);
258};
259