Skip to content

Instantly share code, notes, and snippets.

@eginez
Last active March 3, 2022 20:58
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eginez/d69e3e1c4045eb692a274bd2bd5d31d6 to your computer and use it in GitHub Desktop.
Save eginez/d69e3e1c4045eb692a274bd2bd5d31d6 to your computer and use it in GitHub Desktop.
Graal and PseudoGo

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

Ingredients

Steps

  • 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!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment