哪个是好的且简单的中间代码?

发布于 2024-09-10 15:24:23 字数 364 浏览 1 评论 0原文

假设作业的目的是编写一个适用于 C 语言子集的编译器(您可以假设任何语言的子集,仅支持基本的脚本表达能力,而无需将复杂的事物作为对象)。

什么样的中间代码可以用来验证编译器的正确性?我正在与一位教授交谈,他谈到了这样一个事实:他不知道应该为他的学生提供什么作为用于“编译代码”的虚拟机,所以我想知道这可能是一个好的解决方案。

Subset of C -> Compiler -> Code? -> VM

其中代码可以是二进制格式,也可以是更好的 ASCII 格式(类似于伪 asm)。

我正在寻找已经制作好的东西,而不是如何构建这个中间代码和VM,只是一个简单的准备好用于测试一些编译程序的东西。

Suppose that the purpose of an assignment is to write a compiler that works on a subset of C language (you can assume a subset of whatever language, just supporting basic scripting expressiveness without having complex things as objects).

What kind of intermediate code could be used to verify the correctness of the compiler? I was talking with a professor and he spoke about the fact that he didn't know what to give to his students as the VM to be used for the "compiled code" so I wondered which could be a good solution.

Subset of C -> Compiler -> Code? -> VM

in which code could be either in binary format or better in an ASCII format (something like pseudo-asm).

I'm looking for something already made, not how to structure this intermediate code and the VM, just an easy and simple one ready to be used to test some compiled programs..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

稳稳的幸福 2024-09-17 15:24:23

您可以描述一些抽象的机器设计,然后为其提供列表格式的指令集。我的小型 LISP 解析器是解析器中的轻而易举的事。

(label add-two)
(init-stack-frame 2)
(load r1 0)
(load r2 1)
(add val r1 r2)
(goto cont)

另外,编写一个 lisp 解释器来读取此内容也是轻而易举的事情。

load_labels (index, expr, env)
    if expr.first == 'label'
        env.set(expr.second, index)

interpret (machine, expr, env)
    return env.lookup(expr.first).eval(machine, expr.tail)

You could describe some abstract machine design and then provide it an instruction set in list-format. I small LISP parser is a nobrainer in parsers.

(label add-two)
(init-stack-frame 2)
(load r1 0)
(load r2 1)
(add val r1 r2)
(goto cont)

Also, writing a lisp interpreter to read this in is a nobrainer.

load_labels (index, expr, env)
    if expr.first == 'label'
        env.set(expr.second, index)

interpret (machine, expr, env)
    return env.lookup(expr.first).eval(machine, expr.tail)
暮年 2024-09-17 15:24:23

您可以在现有虚拟机中找到许多中间代码/字节码示例。根据您的定义,它们可能很简单,也可能不简单。示例:

You can find many examples of intermediate code/bytecode in existing VMs. Depending on your definition, they may or may not be simple. Examples:

眼中杀气 2024-09-17 15:24:23

编译为脚本语言(例如 JavaScript)怎么样?它是人类可读的并且已经制作完成。

How about compiling to a scripting language (e.g. JavaScript)? It's human-readable and already made.

与君绝 2024-09-17 15:24:23

针对 Java 虚拟机怎么样?不确定它有多简单,但它有很好的文档记录,因此如果学生感到好奇,他们可以前往 amazon.com 并获取一本有关中间代码的实际含义以及虚拟机如何工作的书。

您还可以创建真正的 80x86 或 68000 程序集,使用汇编器获取机器代码,然后使用模拟器运行它。如果你已经编写了一个编译器并且它已经有大量的调试器和其他可用的实用程序,那么真正的硬件并不会让我觉得比一些虚构的虚拟机更复杂。

但我确实喜欢 LISP 建议:-)

How about targeting the Java Virtual Machine? Not sure how simple it is but it is very well documented, so if the students where curious, they could head over to amazon.com and get a book about what the intermediate code actually means and how the vm works.

You could also just create real 80x86 or 68000 assembly, use an assembler to get machine code and then use an emulator to run it. Real hardware doesn't strike me as more complicated than some made-up VM if you're already gone through writing a compiler and it has tons of debuggers and other utilities available already.

But I do like the LISP suggestion :-)

爺獨霸怡葒院 2024-09-17 15:24:23

llvm 怎么样?

How about llvm?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文