Skip to content
Browse files
8263776: [JVMCI] add helper to perform Java upcalls
Reviewed-by: kvn
  • Loading branch information
Tom Rodriguez committed Mar 23, 2021
1 parent b23228d commit 6b4c654186e49528eeae7249fdcd0f2d1a98b3ad
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
@@ -456,6 +456,113 @@ JRT_BLOCK_ENTRY(int, JVMCIRuntime::throw_class_cast_exception(JavaThread* thread
return caller_is_deopted();

class ArgumentPusher : public SignatureIterator {
JavaCallArguments* _jca;
jlong _argument;
bool _pushed;

jlong next_arg() {
guarantee(!_pushed, "one argument");
_pushed = true;
return _argument;

float next_float() {
guarantee(!_pushed, "one argument");
_pushed = true;
jvalue v;
v.i = (jint) _argument;
return v.f;

double next_double() {
guarantee(!_pushed, "one argument");
_pushed = true;
jvalue v;
v.j = _argument;
return v.d;

Handle next_object() {
guarantee(!_pushed, "one argument");
_pushed = true;
return Handle(Thread::current(), (oop) (address) _argument);

ArgumentPusher(Symbol* signature, JavaCallArguments* jca, jlong argument) : SignatureIterator(signature) {
this->_return_type = T_ILLEGAL;
_jca = jca;
_argument = argument;
_pushed = false;

void do_type(BasicType type) {
switch (type) {
case T_OBJECT:
case T_ARRAY: _jca->push_oop(next_object()); break;
case T_BOOLEAN: _jca->push_int((jboolean) next_arg()); break;
case T_CHAR: _jca->push_int((jchar) next_arg()); break;
case T_SHORT: _jca->push_int((jint) next_arg()); break;
case T_BYTE: _jca->push_int((jbyte) next_arg()); break;
case T_INT: _jca->push_int((jint) next_arg()); break;
case T_LONG: _jca->push_long((jlong) next_arg()); break;
case T_FLOAT: _jca->push_float(next_float()); break;
case T_DOUBLE: _jca->push_double(next_double()); break;
default: fatal("Unexpected type %s", type2name(type));

JRT_ENTRY(jlong, JVMCIRuntime::invoke_static_method_one_arg(JavaThread* thread, Method* method, jlong argument))
ResourceMark rm;
HandleMark hm(THREAD);

methodHandle mh(thread, method);
if (mh->size_of_parameters() > 1 && !mh->is_static()) {
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Invoked method must be static and take at most one argument");

Symbol* signature = mh->signature();
JavaCallArguments jca(mh->size_of_parameters());
ArgumentPusher jap(signature, &jca, argument);
BasicType return_type = jap.return_type();
JavaValue result(return_type);
JavaCalls::call(&result, mh, &jca, CHECK_0);

if (return_type == T_VOID) {
return 0;
} else if (return_type == T_OBJECT || return_type == T_ARRAY) {
thread->set_vm_result((oop) result.get_jobject());
return 0;
} else {
jvalue *value = (jvalue *) result.get_value_addr();
// Narrow the value down if required (Important on big endian machines)
switch (return_type) {
return (jboolean) value->i;
case T_BYTE:
return (jbyte) value->i;
case T_CHAR:
return (jchar) value->i;
case T_SHORT:
return (jshort) value->i;
case T_INT:
case T_FLOAT:
return value->i;
case T_LONG:
case T_DOUBLE:
return value->j;
fatal("Unexpected type %s", type2name(return_type));
return 0;

JRT_LEAF(void, JVMCIRuntime::log_object(JavaThread* thread, oopDesc* obj, bool as_string, bool newline))
ttyLocker ttyl;

@@ -412,6 +412,11 @@ class JVMCIRuntime: public CHeapObj<mtJVMCI> {
static int throw_klass_external_name_exception(JavaThread* thread, const char* exception, Klass* klass);
static int throw_class_cast_exception(JavaThread* thread, const char* exception, Klass* caster_klass, Klass* target_klass);

// A helper to allow invocation of an arbitrary Java method. For simplicity the method is
// restricted to a static method that takes at most one argument. For calling convention
// simplicty all types are passed by being converted into a jlong
static jlong invoke_static_method_one_arg(JavaThread* thread, Method* method, jlong argument);

// Test only function
static jint test_deoptimize_call_int(JavaThread* thread, int value);
@@ -684,6 +684,8 @@
declare_function(JVMCIRuntime::dynamic_new_array_or_null) \
declare_function(JVMCIRuntime::dynamic_new_instance_or_null) \
declare_function(JVMCIRuntime::invoke_static_method_one_arg) \
declare_function(JVMCIRuntime::vm_message) \
declare_function(JVMCIRuntime::identity_hash_code) \
declare_function(JVMCIRuntime::exception_handler_for_pc) \

0 comments on commit 6b4c654

Please sign in to comment.