JVM 规范中 JSR/RET 的状态

Status of JSR/RET in JVM spec

JVM 规范的某些部分建议操作 JSR (Jump SubRoutine), JSR_W (Jump SubRoutine Wide) and RET (RETurn from subroutine) 只能用于 class 文件版本 50.0 (JDK 1.6):

3.13 Compiling Finally

(This section assumes a compiler generates class files with version number 50.0 or below, so that the jsr instruction may be used. See also §4.10.2.5.)

以后:

4.10.2.5. Exceptions and finally

To implement the try-finally construct, a compiler for the Java programming language that generates class files with version number 50.0 or below may use the exception-handling facilities together with two special instructions: jsr ("jump to subroutine") and ret ("return from subroutine").

另一方面,操作码描述本身并未提及弃用这些功能。引用的文本只说明了 50.0 之前的版本,但没有明确说明之后的情况。

This comment(针对询问此弃用或删除背后动机的问题)表示类似程度的混淆,所以显然我不是唯一一个在寻找这个的人。

在向我的问题添加链接时,我注意到 §4.10.1.9: Type Checking Instructions. So this shows that the new strackframe-based type verification scheme can't handle them, and §4.10: Verification of class Files 写道:

中没有相关操作码

Verification by type checking must be used to verify class files whose version number is greater than or equal to 50.0.

或在 §4.10.1: Verification by Type Checking 中更详细:

A class file whose version number is 50.0 or above (§4.1) must be verified using the type checking rules given in this section.

If, and only if, a class file's version number equals 50.0, then if the type checking fails, a Java Virtual Machine implementation may choose to attempt to perform verification by type inference (§4.10.2).

所以我想说版本 50.0 class 可能 仍然包含 jsrret,但存在 JVM 实现无法验证所述 class 的风险,因此将无法加载它。

但后来我发现了一个更明确的规则,在§4.9.1: Static Constraints:

Only instances of the instructions documented in §6.5 may appear in the code array. Instances of instructions using the reserved opcodes (§6.2) or any opcodes not documented in this specification must not appear in the code array.

If the class file version number is 51.0 or above, then neither the jsr opcode or the jsr_w opcode may appear in the code array.

第一段与这个问题无关,因为说明在§6.5 中列出,并且根据 §6.2 不保留。但第二段明确将它们标记为在 51.0 及更高版本中禁止使用。另一方面,ret 操作码在没有 jsrjsr_w 的情况下是无用的,因为只有这两条指令可以创建类型为 returnAddress 的堆栈元素(并通过一些 astore 该类型的局部变量)供 ret[ 使用=53=].


我仍然认为应该在 §6.5 中包含一些与此相关的通知。不幸的是,如果选择类型:Bug,类别:[=66=,Java bug report webpage 会隐藏 Continue 按钮] 平台标准版,子类别:规范。它指出

This subcategory is for reporting technical errors and ambiguities in the text of the Java Language Specification and the JVM Specification. It is not the venue to propose new features in the Java language or JVM. Ongoing feature development is carried out in OpenJDK; corresponding enhancements to the Java Language Specification and the JVM Specification are managed through the Java Community Process.

但是通过 JCP 只是为了在这三个操作码的描述中添加一些澄清说明感觉就像是大材小用。所以我希望这里 post 可以帮助那些自己无法在规范本身中找到答案的人。