是否有用于加载对象(不是常量)的visitLdcInsn类似物? [英] Is there an analogue of visitLdcInsn for loading objects (not constant)?

查看:471
本文介绍了是否有用于加载对象(不是常量)的visitLdcInsn类似物?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们用Java语言编写了一个简单的PostScript解释器,并希望通过直接为源代码的特定部分生成字节码来对其进行优化。为此,我们需要从Java字节码上下文的上下文中加载对象。在生成的字节码方法的签名中指定这样的对象是不好的,因为在我们的例子中它们可能数量很大。

We wrote a simple PostScript interpreter in Java and want to optimize it by generating bytecode directly for specific parts of source code. For this we need to load the object from the context of the Java bytecode context. Specify such object in the signature of the generated bytecode method is not good, because they may be in a large amount in our case.

在Java Asm中,我们有方法

In Java Asm we have method


public void visitLdcInsn(Object cst)

public void visitLdcInsn(Object cst)

它访问一个LDC指令。参数cst-要在堆栈上加载的常量。

It visits a LDC instruction. Parameter cst - the constant to be loaded on the stack.

有什么方法可以加载非常量对象?

Is there any way to load not constant object?

谢谢

推荐答案

自Java 11以来,就可以加载使用 LDC 指令的任意常量。这些对象可以是任意类型的对象,但要具有常量语义,因此它们最好是不可变的。

Since Java 11, it is possible to load arbitrary constants using the LDC instruction. These may be objects of arbitrary type but meant to bear constant semantics, so they should be preferably immutable.

要使此方法起作用,引用的常量池条目必须为 CONSTANT_Dynamic_info ,其结构与 CONSTANT_InvokeDynamic_info

For this to work, the referenced constant pool entry has to be a CONSTANT_Dynamic_info, which has a similar structure as the CONSTANT_InvokeDynamic_info, likewise describing a bootstrap method.

一个区别是动态信息结构的 name_and_type_index 条目将指向字段描述符。此外,bootstrap方法的签名为(MethodHandles.Lookup,String,Class [,static arguments])具有 Class 参数代表常量的预期类型,而不是 MethodType 对象。 bootstrap方法必须直接返回常量值而不是调用位置。

One difference is that the name_and_type_index entry of the dynamic info structure will point to a field descriptor. Further, the bootstrap method has a signature of (MethodHandles.Lookup,String,Class[,static arguments]) having a Class argument representing the expected type of the constant, rather than a MethodType object. The bootstrap method has to directly return the constant value rather than a call-site.

invokedynamic 指令的公用是第一个引导过程的结果将与 LDC 指令相关联,并在所有后续执行中使用(因为它应该是常量)。

Common to the invokedynamic instruction is that the result of the first bootstrapping process will get associated with the LDC instruction and used in all subsequent executions (as it is supposed to be a constant).

这些动态常量的一个有趣特性是,它们对于另一个动态常量或 invokedynamic 指令来说,是bootstrap方法的有效静态参数。 (只要动态常量之间没有循环依赖性)。

An interesting property of these dynamic constants is that they are valid static arguments to the bootstrap method for another dynamic constant or an invokedynamic instruction (as long as there is no cyclic dependency between the dynamic constants).

请注意,已经有一个便捷类,其中包含一些现成的bootstrap方法动态常数。

Note that there is already a convenience class containing some ready-to-use bootstrap methods for dynamic constants.

这篇关于是否有用于加载对象(不是常量)的visitLdcInsn类似物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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