我们可以构造一个“OpCode”的实例吗?
.NET Framework 4.0 向 Reflection API 引入了多个项目,这些项目对我的工作从极其有用到至关重要。其中包括 Assembly
、Module
、MethodBody
和 LocalVariableInfo
的受保护构造函数以及新的 CustomAttributeData< /代码> 类。有几件我仍然需要的东西解决起来相当麻烦。我相信它们很容易适用于需要扩展我刚刚列出的类型的同一[小]群体。
这一次:我正在寻找一种方法来构造System.Reflection.Emit.OpCode
结构体,带有我自己的参数。我当前调用内部构造函数来创建实例。这不会对性能造成损害,因为我将构造的项目公开为类的 public static readonly
成员以供重用,但正如您可以想象的那样,这是一个极其次优的场景。
是否有任何原因无法通过说明用户构造的 OpCode
不能与 ILGenerator
一起使用的文档来公开当前的内部 OpCode
构造函数>。
编辑:这是一个例子。通过创建以下自定义操作码,我可以在一些中间指令列表之间的字节码转换中使用它,而无需创建临时局部变量。如果我发出 IL,我会将剩余的 swap
指令转换为有效的 IL 表示,但就我而言,下一步是理解自定义 swap
指令的 JIT 。我使用的是 Prefix2
前缀 0xFD
,它被任何有效的 IL 操作码保留和使用。
/// <summary>
/// Swaps adjacent elements on the evaluation stack. The supplied inline int32 argument gives the
/// index of the topmost item in the pair.
/// </summary>
public static readonly OpCode Swap;
我还将将此用于没有简单/通用托管代码表示但在各种本机代码生成器中可用的简单的依赖于平台的表示的 JIT 内在函数。其中之一是 ldthread(加载对当前托管线程的 RuntimeThread 表示形式的引用)。
The .NET Framework 4.0 introduces several items to the Reflection API that range from extremely useful to vital for my work. Among these are protected constructors for Assembly
, Module
, MethodBody
, and LocalVariableInfo
and the new CustomAttributeData
class. There are a couple items I still need that are quite troublesome to work around. I believe they easily apply to the same [small] group of people would need to extend the types I just listed.
This time: I'm looking for a way to construct an instance of the System.Reflection.Emit.OpCode
struct with my own parameters. I currently invoke the internal constructor to create the instances. It's not detrimental to performance because I expose the constructed items as public static readonly
members of a class for reuse, but as you can imagine this is an extremely sub-optimal scenario.
Is there any reason it is not possible to make the current internal OpCode
constructor public with documentation that states user-constructed OpCode
s cannot be used with ILGenerator
.
Edit: Here's an example. By creating the following custom opcode, I'm able to use it in byte code transformations between some intermediate lists of instructions without resorting to creating temporary local variables. If I were emitting IL, I'd convert the remaining swap
instructions to a valid IL representation, but in my case the next step is a JIT that understands the custom swap
instruction. I'm using the Prefix2
prefix 0xFD
, which is reserved and unused by any valid IL opcodes.
/// <summary>
/// Swaps adjacent elements on the evaluation stack. The supplied inline int32 argument gives the
/// index of the topmost item in the pair.
/// </summary>
public static readonly OpCode Swap;
I'll also be using this for JIT intrinsics that don't have a simple/common managed code representation but have a simple platform-dependent representation available in the various native code generators. One of these is ldthread
(loads a reference to the current managed thread's RuntimeThread
representation).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为不可能创建自定义 OpCode 实例,因为 OpCode 实例严格源自 公共语言基础设施 (CLI) 文档。因此,即使您的情况有意义,OpCode 似乎也不是正确的选择。
I don't think it is possible to create custom OpCode instance, as OpCode instances are strictly derived from the Common Language Infrastructure (CLI) documentation. So even if your case makes sense, OpCode does not seems to be the way to go.
为什么不使用我们自己的 IL-Opcodes 作为中间结果,然后在最后一步将它们转换为真正的操作码。
Why don't you use our own IL-Opcodes for the intermediate results and then convert them to real opcodes in the last step.