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?
Because Go is compiled to machine code, it will naturally outperform languages that are interpreted or have virtual runtimes. Go programs also compile extremely fast, and the resulting binary is very small.
Go is a compiled language. This means we must run our source code files through a compiler, which reads source code and generates a binary, or executable, file that is used to run the program. Examples of other popular compiled languages include C, C++, and Swift.
There are three Go compiler implementations supported by the Go team. These are gc , the default compiler, gccgo , which uses the GCC back end, and a somewhat less mature gollvm , which uses the LLVM infrastructure.
In computing, code generation is part of the process chain of a compiler and converts intermediate representation of source code into a form (e.g., machine code) that can be readily executed by the target system. Sophisticated compilers typically perform multiple passes over various intermediate forms.
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?
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With