简体   繁体   English

OCaml 作为编译目标

[英]OCaml as a compilation target

I am implementing a ISO/IEC 13211-1:1995 compliant Prolog system in OCaml.我正在 OCaml 中实施符合 ISO/IEC 13211-1:1995 的 Prolog 系统。

For the code generation (aka JIT) part I...对于代码生成(又名 JIT)第一部分...

  • ... generate actual ".ml" code, ... ...生成实际的“.ml”代码,...
  • ... compile it with ocamlopt , ... ...用ocamlopt编译它,...
  • ... and use dynamic linking. ...并使用动态链接。

The approach is somewhat crude but good enough as a proof-of-concept.这种方法有些粗糙,但作为概念验证已经足够好了。

Lately, it occurred to me that compiling to OCaml bytecode could be an alternative 1 .最近,我想到编译为 OCaml 字节码可能是一种替代方法1

So my actual question is two-fold:所以我的实际问题有两个:

  1. How can I compile OCaml bytecode to native code?如何将 OCaml 字节码编译为本机代码?

  2. Are there OCaml libraries for emitting/handling bytecode?是否有用于发出/处理字节码的 OCaml 库?


1 And a nice one, too, as this would allow using js_of_ocaml . 1这也是一个不错的选择,因为这将允许使用js_of_ocaml

Well, while technically it is possible to compile bytecode to C and then to native code, eg, there was a project doing this , I would advice against using bytecode as the target.好吧,虽然在技术上可以将字节码编译为 C,然后编译为本地代码,例如,有一个项目正在这样做,但我建议不要使用字节码作为目标。 First of all, the bytecode is less stable and rather undocumented.首先,字节码不太稳定而且没有文档记录。 Next, you would find it harder to emit the bytecode, since you will be doing the work of ocamlc compiler.接下来,您会发现发出字节码变得更加困难,因为您将完成 ocamlc 编译器的工作。 Again, going back to the stability question, your project will bitrot very fast and will be very painful to maintain, as every change to bytecode will break your code, and you will have to either support multiple branches for different versions or drop support for older versions of the compiler.再次回到稳定性问题,您的项目会很快变位并且维护起来非常痛苦,因为对字节码的每次更改都会破坏您的代码,您将不得不支持不同版本的多个分支或放弃对旧版本的支持编译器的版本。

Finally, emitting ml will give you choice to use both bytecode and nativecode by just choosing an appropriate compiler.最后,发出 ml 将让您通过选择适当的编译器来选择使用字节码和本机代码。 You could also use js_of_ocaml or any other backends.您还可以使用 js_of_ocaml 或任何其他后端。 Also, historically, ML and OCaml was specifically designed to be a target of code generation, especially for the needs of proof assistance, see Coq and F* for example, and they use ML not bytecode.此外,从历史上看,ML 和 OCaml 专门设计为代码生成的目标,特别是用于证明辅助的需求,例如,参见 Coq 和 F*,它们使用 ML 而不是字节码。

Are there OCaml libraries for emitting/handling bytecode?是否有用于发出/处理字节码的 OCaml 库?

There is some number of internal to the compiler tools, that I usually use as the reference implementation (and instead of the absent documentation of bytecode), see the tools folder, eg, this is the bytecode disassembler .编译器工具有一些内部工具,我通常将其用作参考实现(而不是缺少的字节码文档),请参阅工具文件夹,例如,这是字节码反汇编器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM