JIT 代码生成技术 [英] JIT code generation techniques

查看:36
本文介绍了JIT 代码生成技术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虚拟机如何动态生成本地机器代码并执行它?

How does a virtual machine generate native machine code on the fly and execute it?

假设您可以弄清楚要发出的本机操作码是什么,那么您将如何实际运行它?

Assuming you can figure out what are the native machine op-codes you want to emit, how do you go about actually running it?

是否像将助记符指令映射到二进制代码、将其填充到 char* 指针中并将其转换为函数并执行一样笨拙?

Is it something as hacky as mapping the mnemonic instructions to binary codes, stuffing it into an char* pointer and casting it as a function and executing?

或者您会生成一个临时共享库(.dll 或 .so 或其他)并使用诸如 LoadLibrary 之类的标准函数将其加载到内存中?

Or would you generate a temporary shared library (.dll or .so or whatever) and load it into memory using standard functions like LoadLibrary ?

推荐答案

您只需制作程序计数器 指向要执行的代码.请记住,数据可以是数据或代码.在 x86 上,程序计数器是 EIP 寄存器.EIP的IP部分代表指令指针.调用 JMP 指令跳转到一个地址.跳转后EIP会包含这个地址.

You can just make the program counter point to the code you want to execute. Remember that data can be data or code. On x86 the program counter is the EIP register. The IP part of EIP stands for instruction pointer. The JMP instruction is called to jump to an address. After the jump EIP will contain this address.

是否像将助记符指令映射到二进制代码、将其填充到 char* 指针中并将其转换为函数并执行一样笨拙?

Is it something as hacky as mapping the mnemonic instructions to binary codes, stuffing it into an char* pointer and casting it as a function and executing?

是的.这是一种方法.生成的代码将被转换为 C 中的函数指针.

Yes. This is one way of doing it. The resulting code would be cast to a pointer to function in C.

这篇关于JIT 代码生成技术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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