Hacker News new | ask | show | jobs
by smeg 5185 days ago
Does anyone know why the Go runtime is written in C? The FAQ says it is to get around bootstrapping, but that does not seem obvious, as the Go compiler (6g) is written in C, so why cant the runtime be written in Go and compiled with the rest of the Go library?

Also, is the incompatibility between Go and C just due to the segmented stacks in Go? What about function call conventions? Anything else?

1 comments

If the runtime were written in Go, what would the runtime's runtime be?
Well given that the current runtime (written in C) doesn't have a runtime, I would assume there wouldn't be one, as Go compiles down to native code just like C does, using the same toolchain (6c/g -> 6a -> 6l).

My current guesses are: 1) Performance 2) The need to call assembly code easily

C is capable of running without a runtime, but Go isn't. This has nothing to do with compiling down to native code and everything to do with the language semantics.
Right, so native code generated by Go probably needs some kind of runtime initialization before it can even start executing.
Among other things which aren't expressible in Go, that's part of it. You could do those bits in Assembler and the rest in Go if you really wanted to stay out of C, but the net effect is just that the language runtime becomes harder to port to new platforms.
Please note that you can call assembly code from Go easily.

The runtime needs to break the abstractions Go provide, it needs to break Go's memory model, it needs to be aware of the garbage collector, it needs to be aware of stacks, stack growths and switch them, and it needs to do other things: http://research.swtch.com/goabstract.

C was chosen as the pragmatical choice, they could have done it all in assembly (various bits are written in assembly), but C is a portable assembler.