编程语言
首页 > 编程语言> > 使用jsr指令进行Java递归

使用jsr指令进行Java递归

作者:互联网

我使用Jasmin Java汇编程序编译玩具语言.但是当我使用jsr指令递归回子程序,并使用java运行Jasmin的输出时,我收到错误“递归调用jsr条目”.这是Jasmin代码(它是计算5!(我已经省略了类定义;所有这些都在主方法体中)):

f:
   swap
   istore 2
   iload 2
   ifeq label0
   iload 2
   iload 2
   ldc 1
   isub
   jsr f
   istore 1
   istore 2
   iload 1
   iload 2
   imul
   goto label1
label0:
   ldc 1
label1:
   swap
   astore 0
   ret 0
main:
   ldc 5
   jsr f
   istore 1
   iload 1

解决方法:

§4.8.2 of the JVM spec明确禁止递归jsr:

No jsr or jsr_w instruction may be used to recursively call a subroutine if that subroutine is already present in the subroutine call chain. (Subroutines can be nested when using try-finally constructs from within a finally clause. For more information on Java virtual machine subroutines, see §4.9.6.)

这主要是为了简化字节码验证器的逻辑,以便确保在子例程中保存和恢复适当的状态.

标签:java,assembly,jvm,bytecode,jasmin
来源: https://codeday.me/bug/20190721/1496602.html