Is it possible to write a JIT compiler (to native code) entirely in a managed .NET language

后端 未结 4 572
余生分开走
余生分开走 2020-12-12 09:33

I\'m toying with the idea of writing a JIT compiler and am just wondering if it is even theoretically possible to write the whole thing in managed code. In particular, once

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-12 10:13

    Using unsafe code, you can "hack" a delegate and make it point to an arbitrary assembly code that you generated and stored in an array. The idea is that delegate has a _methodPtr field, which can be set using Reflection. Here is some sample code:

    • Inline x86 ASM in C#

    This is, of course, a dirty hack that may stop working at any time when the .NET runtime changes.

    I guess that, in principle, fully managed safe code cannot be allowed to implement JIT, because that would break any security assumptions that the runtime relies on. (Unless, the generated assembly code came with a machine-checkable proof that it does not violate the assumptions...)

提交回复
热议问题