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 https://github.com/axw/llgo



1. Install LLVM from source using this guide http://linuxdeveloper.blogspot.in/2012/12/building-llvm-32-from-source.html

2. Download Go 1.0.3 binary packages from http://code.google.com/p/go/downloads/list. 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
Go is a tool for managing Go source code.
Usage:
 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 README.md file on their website https://github.com/axw/llgo. 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 github.com/axw/llgo/llgo
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
$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: