编译为字节码的动机是什么? [英] What are motivations behind compiling to byte-code?

查看:23
本文介绍了编译为字节码的动机是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究我自己的玩具编程语言.现在我正在解释来自 AST 的源语言,我想知道编译为字节码然后解释它可以为我提供什么优势.

I'm working on my own toy programming language. For now I'm interpreting the source language from AST and I'm wondering what advantages compiling to a byte-code and then interpreting it could provide me.

现在我想到了三件事:

  • 数百次遍历语法树可能比运行数组中的指令慢,特别是如果数组支持 O(1) 随机访问(即向上和向下跳转 10 条指令).
  • 在类型化执行环境中,我有一些运行时成本,因为我的 AST 是类型化的,而且我一直在遍历它(即,我有 10 种类型的节点,我需要检查我现在使用的是什么类型)执行).也许编译为无类型字节码可以帮助改善这一点,因为在类型检查和编译之后,我会有一个无类型的值和代码.
  • 编译为字节码可能会提供更好的可移植性.

我的观点正确吗?编译为字节码的其他动机是什么?

Are my points correct? What are some other motivations behind compiling to bytecode?

推荐答案

速度是主要原因;在实践中解释 AST 太慢了.

Speed is the main reason; interpreting ASTs is just too slow in practice.

使用字节码的另一个原因是它可以简单地序列化(存储在磁盘上),以便您可以分发它.这就是 Java 所做的.

Another reason to use bytecode is that it can be trivially serialized (stored on disk), so that you can distribute it. This is what Java does.

这篇关于编译为字节码的动机是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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