Go compiler rewritten to Go

    With these words, a comment begins on a fresh commit to the Go master branch:
    This change deletes the C implementations of the Go compiler and assembler from the master branch.
    (“These changes remove the C implementation of the Go compiler and assembler from the main branch.”)

    These words are the work of the Go team for almost a year in rewriting the compiler from C to Go. It was a 90% automatic process. Now Go compiles itself.

    Why it was needed and a little more details under the cut.


    Image: Twitter Russ Cox

    Besides the purely academic interest (“can a language compile itself”), which one way or another arises in all language communities, and almost no one succeeds in, the question of rewriting the Go compiler has exactly the same practical meaning as rewriting any another program. On the one hand, certainly, C, whatever one may say, is faster, but on the other, it is obvious that the development speed and productivity on C are much lower than on Go, and in some places a real show-stopper. Well, as the authors themselves write - “Programming in Go is fun. Programming in C is not. ".

    Changes that Go authors wanted to implement in the compiler / runtime often rested on the practical complexity of their implementation. For example, to speed up the garbage collector and bring its indicators closer to those acceptable in the hard real-time world, it was long planned to implement a true concurrent garbage collector (not making stop-the-world), but it was not possible to implement it in C. On Go, it will be much easier to implement.

    The main question that arose for the team - how, in fact, to carry out this massive move? To rewrite 80,000+ lines of code in C without missing or losing anything is almost impossible. It was decided, taking into account the similarity of the grammar of languages, to write an automatic converter from C to Go and use it. One big gofix.

    The task was set real - the converter had to work with the Go codebase, they did not swing the universal converter for all occasions. 99% of the code is converted automatically, the remaining difficult moments can already be completed by hand.

    Here is an interesting video on this topic from the past GopherCon. See who cares about implementation details:



    Slides here: talks.golang.org/2014/c2go.slide

    There is still a lot of work to optimize the new compiler - all the same, the main excitement of the people was “what if the Go-compiler becomes noticeably slower”, but Go 1.5 will already be without a single lines of C-code, and at the disposal of developers all the tools familiar to Go-programmers to optimize and profile the code — race-detector, various profilers, static code analysis tools, and the like — will automatically appear.

    The release of Go 1.5 is officially scheduled for August 2015.

    You can safely congratulate the whole team and all those involved in this important event in the history of Go!

    UPD The comments rightly noted that rewriting the compiler is not uncommon, and to say that few have succeeded in doing this is wrong.

    Also popular now: