1612N/A/*
1879N/A * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
1612N/A * Copyright 2009 Red Hat, Inc.
1612N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1612N/A *
1612N/A * This code is free software; you can redistribute it and/or modify it
1612N/A * under the terms of the GNU General Public License version 2 only, as
1612N/A * published by the Free Software Foundation.
1612N/A *
1612N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1612N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1612N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1612N/A * version 2 for more details (a copy is included in the LICENSE file that
1612N/A * accompanied this code).
1612N/A *
1612N/A * You should have received a copy of the GNU General Public License version
1612N/A * 2 along with this work; if not, write to the Free Software Foundation,
1612N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1612N/A *
1612N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1612N/A * or visit www.oracle.com if you need additional information or have any
1612N/A * questions.
1612N/A *
1612N/A */
1612N/A
1879N/A#ifndef SHARE_VM_SHARK_SHARKNATIVEWRAPPER_HPP
1879N/A#define SHARE_VM_SHARK_SHARKNATIVEWRAPPER_HPP
1879N/A
1879N/A#include "runtime/handles.hpp"
1879N/A#include "shark/llvmHeaders.hpp"
1879N/A#include "shark/sharkBuilder.hpp"
1879N/A#include "shark/sharkContext.hpp"
1879N/A#include "shark/sharkInvariants.hpp"
1879N/A#include "shark/sharkStack.hpp"
1879N/A
1612N/Aclass SharkNativeWrapper : public SharkCompileInvariants {
1612N/A friend class SharkStackWithNativeFrame;
1612N/A
1612N/A public:
1612N/A static SharkNativeWrapper* build(SharkBuilder* builder,
1612N/A methodHandle target,
1612N/A const char* name,
1612N/A BasicType* arg_types,
1612N/A BasicType return_type) {
1612N/A return new SharkNativeWrapper(builder,
1612N/A target,
1612N/A name,
1612N/A arg_types,
1612N/A return_type);
1612N/A }
1612N/A
1612N/A private:
1612N/A SharkNativeWrapper(SharkBuilder* builder,
1612N/A methodHandle target,
1612N/A const char* name,
1612N/A BasicType* arg_types,
1612N/A BasicType return_type)
1612N/A : SharkCompileInvariants(NULL, builder),
1612N/A _target(target),
1612N/A _arg_types(arg_types),
1612N/A _return_type(return_type),
1612N/A _lock_slot_offset(0) { initialize(name); }
1612N/A
1612N/A private:
1612N/A void initialize(const char* name);
1612N/A
1612N/A private:
1612N/A methodHandle _target;
1612N/A BasicType* _arg_types;
1612N/A BasicType _return_type;
1612N/A llvm::Function* _function;
1612N/A SharkStack* _stack;
1612N/A llvm::Value* _oop_tmp_slot;
1612N/A OopMapSet* _oop_maps;
1612N/A int _receiver_slot_offset;
1612N/A int _lock_slot_offset;
1612N/A
1612N/A // The method being compiled.
1612N/A protected:
1612N/A methodHandle target() const {
1612N/A return _target;
1612N/A }
1612N/A
1612N/A // Properties of the method.
1612N/A protected:
1612N/A int arg_size() const {
1612N/A return target()->size_of_parameters();
1612N/A }
1612N/A BasicType arg_type(int i) const {
1612N/A return _arg_types[i];
1612N/A }
1612N/A BasicType return_type() const {
1612N/A return _return_type;
1612N/A }
1612N/A bool is_static() const {
1612N/A return target()->is_static();
1612N/A }
1612N/A bool is_synchronized() const {
1612N/A return target()->is_synchronized();
1612N/A }
1612N/A bool is_returning_oop() const {
1612N/A return target()->is_returning_oop();
1612N/A }
1612N/A
1612N/A // The LLVM function we are building.
1612N/A public:
1612N/A llvm::Function* function() const {
1612N/A return _function;
1612N/A }
1612N/A
1612N/A // The Zero stack and our frame on it.
1612N/A protected:
1612N/A SharkStack* stack() const {
1612N/A return _stack;
1612N/A }
1612N/A
1612N/A // Temporary oop storage.
1612N/A protected:
1612N/A llvm::Value* oop_tmp_slot() const {
1612N/A assert(is_static() || is_returning_oop(), "should be");
1612N/A return _oop_tmp_slot;
1612N/A }
1612N/A
1612N/A // Information required by nmethod::new_native_nmethod().
1612N/A public:
1612N/A int frame_size() const {
1612N/A return stack()->oopmap_frame_size();
1612N/A }
1612N/A ByteSize receiver_offset() const {
1612N/A return in_ByteSize(_receiver_slot_offset * wordSize);
1612N/A }
1612N/A ByteSize lock_offset() const {
1612N/A return in_ByteSize(_lock_slot_offset * wordSize);
1612N/A }
1612N/A OopMapSet* oop_maps() const {
1612N/A return _oop_maps;
1612N/A }
1612N/A
1612N/A // Helpers.
1612N/A private:
1612N/A llvm::BasicBlock* CreateBlock(const char* name = "") const {
1612N/A return llvm::BasicBlock::Create(SharkContext::current(), name, function());
1612N/A }
1612N/A llvm::Value* thread_state_address() const {
1612N/A return builder()->CreateAddressOfStructEntry(
1612N/A thread(), JavaThread::thread_state_offset(),
1612N/A llvm::PointerType::getUnqual(SharkType::jint_type()),
1612N/A "thread_state_address");
1612N/A }
1612N/A llvm::Value* pending_exception_address() const {
1612N/A return builder()->CreateAddressOfStructEntry(
1612N/A thread(), Thread::pending_exception_offset(),
1612N/A llvm::PointerType::getUnqual(SharkType::oop_type()),
1612N/A "pending_exception_address");
1612N/A }
1612N/A void CreateSetThreadState(JavaThreadState state) const {
1612N/A builder()->CreateStore(
1612N/A LLVMValue::jint_constant(state), thread_state_address());
1612N/A }
1612N/A void CreateWriteMemorySerializePage() const {
1612N/A builder()->CreateStore(
1612N/A LLVMValue::jint_constant(1),
1612N/A builder()->CreateIntToPtr(
1612N/A builder()->CreateAdd(
1612N/A LLVMValue::intptr_constant(
1612N/A (intptr_t) os::get_memory_serialize_page()),
1612N/A builder()->CreateAnd(
1612N/A builder()->CreateLShr(
1612N/A builder()->CreatePtrToInt(thread(), SharkType::intptr_type()),
1612N/A LLVMValue::intptr_constant(os::get_serialize_page_shift_count())),
1612N/A LLVMValue::intptr_constant(os::get_serialize_page_mask()))),
1612N/A llvm::PointerType::getUnqual(SharkType::jint_type())));
1612N/A }
1612N/A void CreateResetHandleBlock() const {
1612N/A llvm::Value *active_handles = builder()->CreateValueOfStructEntry(
1612N/A thread(),
1612N/A JavaThread::active_handles_offset(),
1612N/A SharkType::jniHandleBlock_type(),
1612N/A "active_handles");
1612N/A builder()->CreateStore(
1612N/A LLVMValue::intptr_constant(0),
1612N/A builder()->CreateAddressOfStructEntry(
1612N/A active_handles,
1612N/A in_ByteSize(JNIHandleBlock::top_offset_in_bytes()),
1612N/A llvm::PointerType::getUnqual(SharkType::intptr_type()),
1612N/A "top"));
1612N/A }
1612N/A llvm::LoadInst* CreateLoadPendingException() const {
1612N/A return builder()->CreateLoad(
1612N/A pending_exception_address(), "pending_exception");
1612N/A }
1612N/A};
1879N/A
1879N/A#endif // SHARE_VM_SHARK_SHARKNATIVEWRAPPER_HPP