blob: b488133259873cb7e1673a806726af95b964e726 [file] [log] [blame]
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ART_RUNTIME_METHOD_HANDLES_INL_H_
#define ART_RUNTIME_METHOD_HANDLES_INL_H_
#include "method_handles.h"
#include "common_throws.h"
#include "dex_instruction.h"
#include "interpreter/interpreter_common.h"
#include "jvalue.h"
#include "mirror/class.h"
#include "mirror/method_type.h"
#include "mirror/object.h"
#include "reflection.h"
#include "stack.h"
namespace art {
// Assigns |type| to the primitive type associated with |dst_class|. Returns
// true iff. |dst_class| was a boxed type (Integer, Long etc.), false otherwise.
REQUIRES_SHARED(Locks::mutator_lock_)
static inline bool GetPrimitiveType(ObjPtr<mirror::Class> dst_class, Primitive::Type* type) {
if (dst_class->DescriptorEquals("Ljava/lang/Boolean;")) {
(*type) = Primitive::kPrimBoolean;
return true;
} else if (dst_class->DescriptorEquals("Ljava/lang/Byte;")) {
(*type) = Primitive::kPrimByte;
return true;
} else if (dst_class->DescriptorEquals("Ljava/lang/Character;")) {
(*type) = Primitive::kPrimChar;
return true;
} else if (dst_class->DescriptorEquals("Ljava/lang/Float;")) {
(*type) = Primitive::kPrimFloat;
return true;
} else if (dst_class->DescriptorEquals("Ljava/lang/Double;")) {
(*type) = Primitive::kPrimDouble;
return true;
} else if (dst_class->DescriptorEquals("Ljava/lang/Integer;")) {
(*type) = Primitive::kPrimInt;
return true;
} else if (dst_class->DescriptorEquals("Ljava/lang/Long;")) {
(*type) = Primitive::kPrimLong;
return true;
} else if (dst_class->DescriptorEquals("Ljava/lang/Short;")) {
(*type) = Primitive::kPrimShort;
return true;
} else {
return false;
}
}
// A convenience class that allows for iteration through a list of
// input argument registers |arg| for non-range invokes or a list of
// consecutive registers starting with a given based for range
// invokes.
template <bool is_range> class ArgIterator {
public:
ArgIterator(size_t first_src_reg,
const uint32_t (&arg)[Instruction::kMaxVarArgRegs]) :
first_src_reg_(first_src_reg),
arg_(arg),
arg_index_(0) {
}
uint32_t Next() {
const uint32_t next = (is_range ? first_src_reg_ + arg_index_ : arg_[arg_index_]);
++arg_index_;
return next;
}
uint32_t NextPair() {
const uint32_t next = (is_range ? first_src_reg_ + arg_index_ : arg_[arg_index_]);
arg_index_ += 2;
return next;
}
private:
const size_t first_src_reg_;
const uint32_t (&arg_)[Instruction::kMaxVarArgRegs];
size_t arg_index_;
};
REQUIRES_SHARED(Locks::mutator_lock_)
bool ConvertJValue(Handle<mirror::Class> from,
Handle<mirror::Class> to,
const JValue& from_value,
JValue* to_value) {
const Primitive::Type from_type = from->GetPrimitiveType();
const Primitive::Type to_type = to->GetPrimitiveType();
// This method must be called only when the types don't match.
DCHECK(from.Get() != to.Get());
if ((from_type != Primitive::kPrimNot) && (to_type != Primitive::kPrimNot)) {
// Throws a ClassCastException if we're unable to convert a primitive value.
return ConvertPrimitiveValue(false, from_type, to_type, from_value, to_value);
} else if ((from_type == Primitive::kPrimNot) && (to_type == Primitive::kPrimNot)) {
// They're both reference types. If "from" is null, we can pass it
// through unchanged. If not, we must generate a cast exception if
// |to| is not assignable from the dynamic type of |ref|.
mirror::Object* const ref = from_value.GetL();
if (ref == nullptr || to->IsAssignableFrom(ref->GetClass())) {
to_value->SetL(ref);
return true;
} else {
ThrowClassCastException(to.Get(), ref->GetClass());
return false;
}
} else {
// Precisely one of the source or the destination are reference types.
// We must box or unbox.
if (to_type == Primitive::kPrimNot) {
// The target type is a reference, we must box.
Primitive::Type type;
// TODO(narayan): This is a CHECK for now. There might be a few corner cases
// here that we might not have handled yet. For exmple, if |to| is java/lang/Number;,
// we will need to box this "naturally".
CHECK(GetPrimitiveType(to.Get(), &type));
// First perform a primitive conversion to the unboxed equivalent of the target,
// if necessary. This should be for the rarer cases like (int->Long) etc.
if (UNLIKELY(from_type != type)) {
if (!ConvertPrimitiveValue(false, from_type, type, from_value, to_value)) {
return false;
}
} else {
*to_value = from_value;
}
// Then perform the actual boxing, and then set the reference.
ObjPtr<mirror::Object> boxed = BoxPrimitive(type, from_value);
to_value->SetL(boxed.Ptr());
return true;
} else {
// The target type is a primitive, we must unbox.
ObjPtr<mirror::Object> ref(from_value.GetL());
// Note that UnboxPrimitiveForResult already performs all of the type
// conversions that we want, based on |to|.
JValue unboxed_value;
return UnboxPrimitiveForResult(ref, to.Get(), to_value);
}
}
return true;
}
template <bool is_range>
bool ConvertAndCopyArgumentsFromCallerFrame(Thread* self,
Handle<mirror::MethodType> callsite_type,
Handle<mirror::MethodType> callee_type,
const ShadowFrame& caller_frame,
uint32_t first_src_reg,
uint32_t first_dest_reg,
const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
ShadowFrame* callee_frame) {
StackHandleScope<4> hs(self);
Handle<mirror::ObjectArray<mirror::Class>> from_types(hs.NewHandle(callsite_type->GetPTypes()));
Handle<mirror::ObjectArray<mirror::Class>> to_types(hs.NewHandle(callee_type->GetPTypes()));
const int32_t num_method_params = from_types->GetLength();
if (to_types->GetLength() != num_method_params) {
ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
return false;
}
ArgIterator<is_range> input_args(first_src_reg, arg);
size_t to_arg_index = 0;
MutableHandle<mirror::Class> from(hs.NewHandle<mirror::Class>(nullptr));
MutableHandle<mirror::Class> to(hs.NewHandle<mirror::Class>(nullptr));
for (int32_t i = 0; i < num_method_params; ++i) {
from.Assign(from_types->GetWithoutChecks(i));
to.Assign(to_types->GetWithoutChecks(i));
const Primitive::Type from_type = from->GetPrimitiveType();
const Primitive::Type to_type = to->GetPrimitiveType();
// Easy case - the types are identical. Nothing left to do except to pass
// the arguments along verbatim.
if (from.Get() == to.Get()) {
interpreter::AssignRegister(callee_frame,
caller_frame,
first_dest_reg + to_arg_index,
input_args.Next());
++to_arg_index;
// This is a wide argument, we must use the second half of the register
// pair as well.
if (Primitive::Is64BitType(from_type)) {
interpreter::AssignRegister(callee_frame,
caller_frame,
first_dest_reg + to_arg_index,
input_args.Next());
++to_arg_index;
}
continue;
} else {
JValue from_value;
JValue to_value;
if (Primitive::Is64BitType(from_type)) {
from_value.SetJ(caller_frame.GetVRegLong(input_args.NextPair()));
} else if (from_type == Primitive::kPrimNot) {
from_value.SetL(caller_frame.GetVRegReference(input_args.Next()));
} else {
from_value.SetI(caller_frame.GetVReg(input_args.Next()));
}
if (!ConvertJValue(from, to, from_value, &to_value)) {
DCHECK(self->IsExceptionPending());
return false;
}
if (Primitive::Is64BitType(to_type)) {
callee_frame->SetVRegLong(first_dest_reg + to_arg_index, to_value.GetJ());
to_arg_index += 2;
} else if (to_type == Primitive::kPrimNot) {
callee_frame->SetVRegReference(first_dest_reg + to_arg_index, to_value.GetL());
++to_arg_index;
} else {
callee_frame->SetVReg(first_dest_reg + to_arg_index, to_value.GetI());
++to_arg_index;
}
}
}
return true;
}
// Similar to |ConvertAndCopyArgumentsFromCallerFrame|, except that the
// arguments are copied from an |EmulatedStackFrame|.
template <bool is_range>
bool ConvertAndCopyArgumentsFromEmulatedStackFrame(Thread* self,
ObjPtr<mirror::Object> emulated_stack_frame,
Handle<mirror::MethodType> callee_type,
const uint32_t first_dest_reg,
ShadowFrame* callee_frame) {
UNUSED(self);
UNUSED(emulated_stack_frame);
UNUSED(callee_type);
UNUSED(first_dest_reg);
UNUSED(callee_frame);
UNIMPLEMENTED(FATAL) << "ConvertAndCopyArgumentsFromEmulatedStackFrame is unimplemented";
return false;
}
} // namespace art
#endif // ART_RUNTIME_METHOD_HANDLES_INL_H_