Jasmin使用参数调用方法 [英] Jasmin invoke a method using arguments

查看:97
本文介绍了Jasmin使用参数调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个生成Jasmin代码的编译器,我想使用一个参数来调用方法,如下所示:

I'm writing a compiler that generates Jasmin code and I want to invoke a method using an argument, as follows:

val test(val x) {
    return x;
}

val main (string[] args) {
    test(1);
}

它编译为:

.class public helloworld
.super java/lang/Object

.method public <init>()V
aload_0
invokenonvirtual java/lang/Object/<init>()V
return
.end method

.method public test(I)I
.limit stack 4
.limit locals 3
iload 1
ireturn
.end method

.method public static main([Ljava/lang/String;)V
.limit stack 4
.limit locals 3
aload_0
ldc 1
invokevirtual helloworld/test(I)I
return
.end method

但是,这将导致以下错误.我在这里做什么错了?

However, this results in the following error. What am I doing wrong here?

java.lang.VerifyError: (class: helloworld, method: main signature: ([Ljava/lang/String;)V) Incompatible object argument for function call

推荐答案

首先,您编译"的代码不是Java,并且我不确定它是什么(伪代码?).

To begin with, the code you 'compile' is not Java, and I'm not sure what it is (pseudo-code?).

Jasmin代码的问题在于,似乎您想将test()作为数组的实例方法来调用,而不是,因为它不是实例化的helloworld实例方法,所以不希望将其作为实例.

The problem with your jasmin code is that it seems you want to call test() as an instance method of the array, which it isn't, because its an instance method of helloworld, which you don't instantiate.

因此,您应该实例化helloworld类,或将test()设为静态方法,以便无需创建对象即可对其进行调用.

So either you should instantiate the class helloworld, or make test() a static method, so that it can be called without creating an object.

在修复茉莉花代码时,我还遇到了另一个错误:iload 1应该是iload 0. (程序员喜欢从0开始计数.)

When fixing the jasmin code I also ran into another error: iload 1 should be iload 0. (Programmers like to start counting at 0).

在这段代码中,我假设您是说test()是静态的.

In this code I assume you meant test() to be static.

.class public helloworld
.super java/lang/Object

.method public <init>()V
aload_0
invokenonvirtual java/lang/Object/<init>()V
return
.end method

.method public static test([Ljava/lang/String;)[Ljava/lang/String;
.limit stack 4
.limit locals 3
aload 0
areturn
.end method

.method public static main([Ljava/lang/String;)V
.limit stack 4
.limit locals 3
ldc 1
anewarray java/lang/String
astore 2
aload 2
ldc 0
ldc "bar"
aastore
aload_0
aload 2
invokestatic helloworld/test([Ljava/lang/String;)[Ljava/lang/String;
return
.end method

这篇关于Jasmin使用参数调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆