为什么Go使用自己的代码生成器?

huangapple go评论104阅读模式
英文:

Why does Go use its own Code generator?

问题

Go语言目前使用的官方编译器(http://code.google.com/p/go/)目前使用的是一个手工制作的、可以说是晦涩难懂的代码生成器,其中包括将自定义部分注入到ELF二进制文件中。

这种方法导致了与直接读取和/或写入ELF信息的实用程序(如lddobjdumpstrip)相关的许多错误。

我认为,通过使用经过充分测试的跨平台代码生成器(如LLVM),然后只使用操作系统附带的链接工具(如Unix/Linux上的ld,或带有MinGW的Windows上的ld.exe,或Windows上的Visual Studio上的link.exe),这些问题本可以避免。

那么为什么Go要使用自己的代码生成器呢?它真的只是在重新发明轮子吗?还是背后有更重要的原因?

英文:

The current, official compiler for Go (http://code.google.com/p/go/) currently uses a handcrafted, arguably arcane code generator, which includes injecting custom sections into the ELF Binary.

This approach has spawned quite a few bugs related to utilities that directly read and/or write ELF Information, such as ldd, objdump or strip.

I believe that this could have been prevented by using a welltested crossplatform code generator, such as LLVM, and then just use linking facilities shipped with the OS, such as ld on Unix/Linux (or ld.exe on windows w/ MinGW), or link.exe on Windows with Visual Studio.

So why does Go use its very own code generator? Is it really just reinventing the wheel? Or are there more important reasons behind it?

答案1

得分: 6

有关如何使用gccgo的信息,请参阅设置和使用gccgo

Go语言规范与编译器无关。您可以从可用的编译器中选择,自己编写一个,或者为LLVM Go前端项目做出贡献。

要了解Go编译器技术的历史背景,请阅读这个问题的答案:使用什么编译器技术构建编译器?

英文:

For information on how to use gccgo, a more traditional compiler using the GCC back end, see Setting up and using gccgo.

The Go Language Specification is compiler agnostic. You can choose from the available compilers, write one yourself, or contribute to the LLVM Go frontend project.

For an historical perspective on the Go compiler technology, read the answer to this question: What compiler technology is used to build the compilers?

答案2

得分: 3

参考编译器(5g、6g和8g,统称为gc)是由Ken Thompson基于他为Plan 9操作系统编写的C编译器编写的。他这样做有几个原因:

  • 他已经熟悉自己的C编译器的工作原理,所以对他来说,改编现有的工作比学习一个全新的框架更容易。
  • Go的一个目标是快速编译。gc编译器可能比基于LLVM的编译器更快,因为它不需要做太多的工作。然而,它的优化程度也不如后者。

正如Peter提到的,还有gccgo,它使用gcc作为后端。我猜想,将来可能会有其他Go编译器的选择,特别是考虑到Go库中包含了一个完整的Go解析器和一个(部分完成的)类型检查器。

英文:

The reference compiler (5g, 6g, and 8g, collectively referred to as gc) was written by Ken Thompson based on the C compiler he wrote for the Plan 9 operating system. There are a couple reasons he did this:

  • He was already familiar with how his C compiler worked, so it was easier for him to adapt his existing work rather than learning an entirely new framework.
  • One of Go's goals is to compile fast. The gc compiler is probably faster than an LLVM-based compiler could be because it's just not doing as much work. Then again, it's also not optimizing as well.

As Peter mentioned, there's also gccgo, which uses gcc as a backend. I suspect there will eventually be other options for Go compilers, especially given that the Go libraries include a complete Go parser and a (partially complete) type checker.

huangapple
  • 本文由 发表于 2011年7月1日 02:53:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/6539676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定