Top
Best
New

Posted by TheWiggles 9 hours ago

Solod – A subset of Go that translates to C(github.com)
137 points | 34 comments
ridiculous_fish 6 hours ago|
I was curious how defer is implemented. `defer` in Go is famously function-scoped, not lexically-scoped. This means that the number of actively-deferred statements is unbounded, which implies heap allocation.

The answer is that Solod breaks with Go semantics here: it just makes defer block-scoped (and unavailable in for/if blocks, which I don't quite get).

https://github.com/solod-dev/solod/blob/main/doc/spec.md#def...

hmry 6 hours ago||
What's the point if it's incompatible? The README suggests using go's testing toolchain and type checker, but that's unreliable if the compiled code has different behavior than the tested code. That's like testing and typechecking your code in a C++ compiler but then for production you run it through a C compiler.

Would have been a lot more useful if it tried to match the Go behavior and threw a compiler error if it couldn't, e.g. when you defer in a loop.

Is this just for people who prefer Go syntax over C syntax?

crowdyriver 2 hours ago|||
tbh I'd rather have this behaviour, defer should've been lexically scoped from the beginning.
1718627440 4 hours ago||
> This means that the number of actively-deferred statements is unbounded, which implies heap allocation.

In C you can allocate dynamically on the stack using alloca or a VLA.

kleiba 4 hours ago||
But also: https://stackoverflow.com/a/1018865
Retr0id 8 hours ago||
I don't really "get" the sweet-spot being targeted here. You don't get channels, goroutines, or gc, so aside from syntax and spatial memory safety you're not really inheriting much from Go. There is also no pathway to integrate with existing Go libraries.

Spatial memory safety is nice but it's the temporal safety that worries me most, in nontrivial C codebases.

tidwall 8 hours ago||
Looks to me like having the ability to write Go syntax and interop directly with C is the plus.
Retr0id 7 hours ago|||
I do like Go's syntax but I can't help thinking the best language for C interop is C.
AdieuToLogic 7 hours ago||
> I do like Go's syntax but I can't help thinking the best language for C interop is C.

SWIG[0] is a viable option for incorporating C code as well.

0 - https://swig.org/Doc4.4/Go.html#Go

stevekemp 6 hours ago||
I love how SWIG is still around! I first used it about 30 years ago to integrate with Perl, then later with Java.
whateveracct 5 hours ago|||
Go's syntax is basically C tho lol

what's the benefit? for loops?

cocodill 4 hours ago||
I guess there is no point except Anton is having fun do it.
0xmrpeter 5 hours ago||
The claim that no goroutines makes this pointless isn't quite right. Migrated 50 services off Docker Compose using Nomad and half of them had zero concurrency needs. A safe Go-syntax C target is actually useful for that layer.
tidwall 8 hours ago||
"To keep things simple, there are no channels, goroutines, closures, or generics."

I wonder if it could be integrated with https://github.com/tidwall/neco, which has Go-like coroutines, channels, and synchronization methods.

joshuahart 1 hour ago||
Interesting approach. What was the main motivation for targeting C specifically instead of something like LLVM or WASM as an intermediate?
nulltrace 23 minutes ago|
Biggest reason is usually the toolchain. Debuggers, sanitizers, profilers all just work when your target is C. Go through LLVM and you get similar optimization but now you own the backend. With C, gcc and clang handle that part.
voidUpdate 2 hours ago||
I'm reminded of the tools in programs like Ghidra or IDA which can take assembly code and convert it into a C-like language. If you could create one of those tools that also takes in the source file so that it names things somewhat reasonably, could you create an anything to C translator? As long as the original file compiles to assembly, that is
circuit10 1 hour ago|
I once used Ghidra to decompile a hand-written ARM assembly floating point library and compile the result to a different architecture, and it was significantly faster than GCC’s built in methods…

But in general this kind of thing is very unreliable for any non-trivial code without a lot of manual work, so a better approach could be to compile to WebAssembly which can be translated into C

voidUpdate 1 hour ago||
It may be easier if you also have the original source file (I've not don't much decompilation myself, only seen other people doing it), as more of a custom solution rather than using an existing system
MYEUHD 7 hours ago||
Related and currently on the front page: https://news.ycombinator.com/item?id=47627595
xentripetal 2 hours ago||
Somewhat similar language, https://vlang.io

It’s a mix of go and rust syntax that translates to C

remywang 6 hours ago||
Anton also wrote the fantastic codapi [1] for embedding executable code snippets with wasm

[1]: https://codapi.org/

weitzj 3 hours ago|
Love it. And from my experience the need for Go Routines is not that urgent.

Sure when I started Go there were Go routines plastered everywhere. And now I think harder: “do I really need a go routine here?”

More comments...