Thursday, January 3, 2013

Using LLVM based Go compiler - llgo

We are going to use llgo - a compiler for Go, written in Go, and using the LLVM compiler infrastructure. The project is hosted at

1. Install LLVM from source using this guide

2. Download Go 1.0.3 binary packages from The file is named "go1.0.3.linux-amd64.tar.gz" and you have to download it in the "~/Downloads" folder.

3. Extract Go in your home directory
$cd ~
$tar zxvf ~/Downloads/go1.0.3.linux-amd64.tar.gz
4. Add the Go binaries to the path.
$gedit ~/.bashrc
Add the following lines
export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/bin
Reload the shell environment
$source ~/.bashrc
5. Check if Go is available
Go is a tool for managing Go source code.
 go command [arguments]
The commands are:...
Good !

6. Now to build llgo we are going to use "go get" command. These steps are also listed in the llgo's file on their website Type the following in the shell
$export CGO_CFLAGS="`llvm-config --cflags`"
$export CGO_LDFLAGS="`llvm-config --ldflags` -Wl,-L`llvm-config --libdir` -lLLVM-`llvm-config --version`"
$go get
It will automatically build and install the llgo binaries in the "~/go/bin" folder.

7. Check if the "llgo" has been build
$ls ~/go/bin
go  godoc  gofmt  llgo
No Go source files were specified
There it is :)

8. Lets test it
$gedit ~/test.go
Add these lines
package main
import "fmt"
func main() {
  fmt.Println("Hello World")
Now run on the terminal
$llgo -dump test.go

; ModuleID = 'main'
target datalayout = "e-p:64:64:64..."
target triple = "x86_64-unknown-linux"
%0 = type { i8*, i8* }
That's the LLVM assembly code which is then given to LLVM backend to generate the actual code.

No comments:

Post a Comment