作者:mobiledu2502912277 | 来源:互联网 | 2023-05-19 04:45
IvewrittenavirtualmachineinCwhichhasdecentperformanceforanon-JITVM,butIwanttolea
I've written a virtual machine in C which has decent performance for a non-JIT VM, but I want to learn something new, and improve performance. My current implementation simply uses a switch to translate from VM bytecode to instructions, which is compiled to a jump table. Like I said, decent performance for what it is, but I've hit a barrier that can only be overcome with a JIT compiler.
我用C语言编写了一个虚拟机,它对于非jit VM有很好的性能,但是我想学习一些新的东西,并提高性能。我当前的实现只是使用一个开关将VM字节码转换为指令,指令被编译为跳转表。就像我说的,这是一种不错的性能,但是我遇到了一个障碍,只能用JIT编译器来克服。
I've already asked a similar question not long ago about self-modifying code, but I came to realize that I wasn't asking the right question.
不久前我已经问过一个类似的关于自我修改代码的问题,但我开始意识到我问的不是正确的问题。
So my goal is to write a JIT compiler for this C virtual machine, and I want to do it in x86 assembly. (I'm using NASM as my assembler) I'm not quite sure how to go about doing this. I'm comfortable with assembly, and I've looked over some self-modifying code examples, but I haven't come to figure out how to do code generation just yet.
因此,我的目标是为这个C虚拟机编写一个JIT编译器,我想在x86汇编语言中实现它。(我使用NASM作为我的汇编程序)我不太确定该怎么做。我对汇编很熟悉,我已经看过一些自我修改的代码示例,但是我还没有找到如何生成代码的方法。
My main block so far is copying instructions to an executable piece of memory, with my arguments. I'm aware that I can label a certain line in NASM, and copy the entire line from that address with the static arguments, but that's not very dynamic, and doesn't work for a JIT compiler. I need to be able to interpret the instruction from bytecode, copy it to executable memory, interpret the first argument, copy it to memory, then interpret the second argument, and copy it to memory.
到目前为止,我的主要模块是使用我的参数将指令复制到可执行内存中。我知道,我可以在NASM中标记某一行,并使用静态参数从该地址复制整行,但这不是动态的,对JIT编译器不起作用。我需要能够解释来自字节码的指令,将它复制到可执行内存中,解释第一个参数,将它复制到内存中,然后解释第二个参数,并将其复制到内存中。
I've been informed about several libraries that would make this task easier, such as GNU lightning, and even LLVM. However, I'd like to write this by hand first, to understand how it works, before using external resources.
我被告知有几个库可以使这个任务更容易,比如GNU lightning,甚至LLVM。但是,我想先用手来写,在使用外部资源之前先了解它是如何工作的。
Are there any resources or examples this community could provide to help me get started on this task? A simple example showing two or three instructions like "add" and "mov" being used to generate executable code, with arguments, dynamically, in memory, would do wonders.
这个社区有什么资源或例子可以帮助我开始这项任务吗?一个简单的示例显示使用“添加”和“mov”之类的指令来生成可执行代码,并在内存中动态地使用参数,这将带来奇迹。
2 个解决方案