I was reviewing some documents on the graalvm and its truffle languages and it occured to me that one could write a go ast to LLVM IR emitter. Go has pretty good support for ast operations, thus it looked feasable at least. Obviously next thing I did was to check and see if someone had already done this. And yes.There is a project doing exactly that.
The support for the language is not all there yet and honestly I don't think this is the most sustainable approach but it was fun to play with it. I got some minimal go(tre) code running through graal's lli pretty fast
- Install GraalVM, and then set your PATH so that you get the GraalVM binaries
- llvm (brew install llvm)
- go install github.com/zegl/tre/cmd/tre
- Create a simple go program eg:
$> cat main.go
package main
import "external"
func fib(num int) int {
if num < 2 {
return num
}
return fib(num-2) + fib(num-1)
}
func main() {
// fib = 5702887
external.Printf("fib = %d\n", fib(34))
}
- Use tre to emmit llvm code and funnel the output into llvm's assembler. This outputs llvm bit code. I did a minor modification to the tre toolchain to output lli ir to the stdout.
$>tre main.go | /usr/local/opt/llvm@6/bin/llvm-as -o out.bc
Now run it with graal's llvm's IR interpreter
$>lli out.bc
fib = 5702887
You just run go code compiled to LLVM IR in the (Graal)JVM!!