4 ms·
Hacking the Go compiler to add a new keyword
- eatonphil 5y agoOn the topic, there was another good post recently on hacking in a new operator to the Go compiler. Yes they rebuild the ^ operator but it's still very illustrative of hacking on the Go project! https://medium.com/trendyol-tech/contributing-the-go-compiler-adding-new-tilde-operator-f66d0c6cff7 https://medium.com/trendyol-tech/contributing-the-go-compile...
- Maksadbek 5y agoThere is a similar article here https://eli.thegreenplace.net/2019/go-compiler-internals-adding-a-new-statement-to-go-part-1 https://eli.thegreenplace.net/2019/go-compiler-internals-add..., where the author adds the `until` keyword to Go compiler.
- avinassh 5y agohey! author of the submitted post here. I did refer to Eli's articles and they were incredibly helpful. I have mentioned them in my post too. This was the only post I could find on internet which talked about Go compiler internals.
- didip 5y agoI wonder if the author has heard of https://github.com/goplus/gop https://github.com/goplus/gop He'd have fun reverse engineering it.
- avinassh 5y agoI have seen it on the HN, but I hadn't looked closely. Thanks for linking again!
- not-my-account 5y agoA great video with a similar topic is George Hotz adding “fore” loops to clang, which runs the body 4 times per loop. https://m.youtube.com/watch?v=ee1bXLDN60U https://m.youtube.com/watch?v=ee1bXLDN60U
- VWWHFSfQ 5y agoI find it disconcerting that the go compiler is such a mess that it took this much effort just to alias a new keyword. I know the Ruby internals are famously very nasty but I'm surprised go is this bad.
- philosopher1234 5y agoWhy does this mean its a mess? Why should the codebase be optimized for adding new keywords, when that happens maybe once a decade? Your comment seems overly negative.
- benhoyt 5y agoIndeed. In fact, even with the release of generics in Go 1.18, which is coming out in early 2022 exactly a decade after Go 1.0, there will be no new keywords. So it won't even have happened once in a decade. :-)
- throwaway894345 5y agoWhat's a language that allows for easily adding new keywords? What tradeoffs were involved in facilitating that property? Is this property compatible with Go's goals?
- wk_end 5y agoIn this context (I see you Lispers) the difficulty of adding a new keyword to the compiler is less a property of the language than a property of how the compiler's implemented. Specifically, the weird stuff the author encountered like: * Generating the token list by parsing the comments of a source file * only parsing up to a hard-coded token instead of all of the known tokens (?!) * using a hacky token hashing mechanism that only looks at the first two characters of the token have nothing to do with Go-the-language.
- preseinger 5y agoA language and its principal compiler are I think not so decoupled as you're implying.
- johnisgood 5y ago> Other than Eli’s post, there are no documentation or articles on Go compiler internals. How does someone get started working on them? How do they navigate and find all these intricacies without spending hours? Maybe Google has some internal documentation on the Go compiler. It would be nice to have more information out there on the internals of the Go compiler. Perhaps there is. I found stuff like: - https://github.com/emluque/golang-internals-resources https://github.com/emluque/golang-internals-resources - https://www.altoros.com/blog/golang-internals-part-1-main-concepts-and-project-structure/ https://www.altoros.com/blog/golang-internals-part-1-main-co... - https://github.com/teh-cmc/go-internals https://github.com/teh-cmc/go-internals But yeah, Eli's articles[1] are pretty good. [1] https://eli.thegreenplace.net/2019/go-compiler-internals-adding-a-new-statement-to-go-part-1/ https://eli.thegreenplace.net/2019/go-compiler-internals-add...
- _wldu 5y agoThis is a great demo. The old C compiler backdoor, but in Go: https://github.com/yeokm1/reflections-on-trusting-trust-go https://github.com/yeokm1/reflections-on-trusting-trust-go The Gopher Con Singapore (2018) video is a really great summary (20 mins). He modifies the compiler and inserts the backdoor during the presentation: https://www.youtube.com/watch?v=T82JttlJf60&list=PLq2Nv-Sh8EbbIjQgDzapOFeVfv5bGOoPE&index=14 https://www.youtube.com/watch?v=T82JttlJf60&list=PLq2Nv-Sh8E...
- melony 5y agoThe Go compiler is about as straightforward as it can get. Just read the source: https://github.com/golang/go https://github.com/golang/go
- avinassh 5y agoI don't think I would have figured out how one adds a new token if not for Eli's post. This comment [0] perfectly explains the quirks I ran into. As an exercise, can you help me figure out how to add a token just from the source and discover these quirks? On second thought, reading from the source and figuring it out could have been possible if you spent hours. But don't you think it should also have some comments to navigate? [0] - https://news.ycombinator.com/item?id=29489113 https://news.ycombinator.com/item?id=29489113
- rodmena 5y ago> How do they navigate and find all these intricacies without spending hours? Technical documentation of internals is not important for corporate built languages -- which is a shame.
- quotemstr 5y agoIf you do perfect hashing, you should make it infallible --- retry map creation with tweaked hash functions until it works.
- lxe 5y ago> The hash method considers the token’s first and second characters and the length. Quite a kludgey optimization for the token hash
- tptacek 5y agoThe language isn't extensible, so these are changes that happen very rarely; the compiler is simply optimized for the actual task it has. What would be weird would be expending any real effort --- or, worse, runtime cost --- for an engineering case we know is never going to happen.
- benhoyt 5y agoYes, and specifically in this case no keywords have been added to the Go language at all since its 1.0 release (I just checked the 1.0.1 spec and there are still 25 keywords). Even the addition of generics coming soon in 1.18 will add no new keywords (though it will add a new token, "~").
- Leszek 5y agoV8 developer here (who happened to also implement perfect hashing in V8's tokenizer) - perfect hashing is a very common compiler optimisation, and as the sibling comment says, is worth it for the runtime scanner speed improvement. If you do end up adding new keywords (which is ~never for anything that wants to preserve backwards compat), then you just recalculate your perfect hash with gperf or by hand or however.
- fefe23 5y agoWhy do we link to some dude applying a HOWTO instead of the HOWTO?
- SilasX 5y agoSemi-related: in the nand2tetris course, they teach you enough so they can add a keyword in their (admittedly toy) system -- the course involves implementing the entire toolchain, including the high-level language[1] compiler, which emits VM code, and the VM code's translator into assembly. By the time you've implemented a compiler that "just works" for the language, you notice that you have really inefficient code for those times when all you need to do is increment a variable by 1, given that the hardware has an opcode for that! In order to have nice, general compilation across all use cases, you've programmed the compiler to implement any case of "add X to variable" via the VM commands for "push X's value onto stack, push variable's value onto stack, call add, pop stack into X". So, I figured I could add "inc" as a keyword. You have the compiler recognize that keyword and translate "inc <var>" into a VM instruction, and then tell the translator how to turn that VM instruction into something that makes use of the opcode. (Alternatively, you can have it just recognize when it's doing something of the form "<variable> = <variable> + 1", but that's trickier once you've written the whole VM emission step as a single-pass operation.) I know, pretty basic stuff from the standpoint of a professional compiler programmer, but pretty neat to be able to make an addition to the language like that! [1] It uses "Jack", a syntactic-sugar-free Java-like language
- chx 5y ago> Due to hash calculations, you can’t add single-character tokens, tokens that are of the same length but differ after 3rd characters onwards People to this day laugh at Rasmus choosing the length of function names in PHP decades ago and go does, well, not the same, but let's just say a variant of it. Oh the irony.
- Thaxll 5y agoIt's not a variant of it, it's an optimization because Go "never" add new keywords.
- throwaway19937 5y ago> People to this day laugh at Rasmus choosing the length of function names in PHP decades ago and go does, well, not the same, but let's just say a variant of it. Oh the irony. This is a difference in kind, not degree. PHP has wonky function names because Rasmus didn't know any better. The Go token hash is a private implementation detail; they can change it and only the compiler internals are affected.
- deleted 5y ago[deleted]
- thamer 5y agoThis is a great exercise for anyone who wants to start exploring how a particular compiler works. I went through something similar in 2005, when during an internship I was working on a program reading and displaying generated Python code in which the code comments provided important metadata about the statements on the same line. I hacked the compiler to make the AST generate a new token for comments (they're otherwise dropped), and this was an incredibly useful experience that gave me knowledge about CPython that I still rely on to this day – 16 years later.
- ir193 5y agoI was trying to write some function similar to `make` but in vein. Maybe I need to open the compiler like the author did. make() takes a type as argument but you can't define such functions in go. And `make` itself is not a keyword, how does golang implement it? is it a special case?
- philosopher1234 5y agoIts a special case, yes. You could probably find something like `makeFn` in `runtime` internal code.