Go Compiler Intermediate Representation

938 views
Skip to first unread message

Arpit Aggarwal

unread,
Feb 21, 2017, 12:23:44 PM2/21/17
to golang-nuts
Hi all,
I am doing a project in which I need Go compiler's intermediate representation(IR) (which is semantics preserving and I can get all all info like line number and data type and other features) (human readable) to convert to another IR of a tool.
I am new to Golang.
Can anybody tell me how can I generate it from the Go compiler.
I also tried this blog but the new versions of go compiler doesn't support this.
I also tried llgo but was able to generate llvm-ir from llgo's docker image available and not able to build llgo myself.
Any help would be highly appreciated.
Thanks in advance.

Thanks,
Arpit
 

Ian Lance Taylor

unread,
Feb 21, 2017, 12:29:41 PM2/21/17
to Arpit Aggarwal, golang-nuts
Unfortunately there is no simple way to do this using the Go compiler.

If you don't actually need the Go compiler itself, then you should use
the go/types package. That is a complete semantics preserving
representation of a Go program with position and type information.

Ian

Arpit Aggarwal

unread,
Feb 21, 2017, 12:32:46 PM2/21/17
to golang-nuts, arpit9...@gmail.com
Thanks a lot Ian.
Will try it and ask if I have any further doubts.

Thanks,
Arpit

adon...@google.com

unread,
Feb 22, 2017, 5:13:25 PM2/22/17
to golang-nuts
On Tuesday, 21 February 2017 12:23:44 UTC-5, Arpit Aggarwal wrote:
I am doing a project in which I need Go compiler's intermediate representation(IR) (which is semantics preserving and I can get all all info like line number and data type and other features) (human readable) to convert to another IR of a tool.

The most convenient way to access the semantics of a Go program is to use the golang.org/x/tools/go/ssa package, which builds a high-level SSA-based intermediate representation.

$ cat fib.go
package fib

func fib(x int) int {
        if x < 2 {
                return x
        }
        return fib(x-2) + fib(x-1)
}

$ ./ssadump -build=F fib.go
# Name: fib.fib
# Package: fib
# Location: fib.go:3:6
func fib(x int) int:
0:                                                                entry P:0 S:2
        t0 = x < 2:int                                                     bool
        if t0 goto 1 else 2
1:                                                              if.then P:1 S:0
        return x
2:                                                              if.done P:1 S:0
        t1 = x - 2:int                                                      int
        t2 = fib(t1)                                                        int
        t3 = x - 1:int                                                      int
        t4 = fib(t3)                                                        int
        t5 = t2 + t4                                                        int
        return t5

# Name: fib.init
# Package: fib
# Synthetic: package initializer
func init():
0:                                                                entry P:0 S:2
        t0 = *init$guard                                                   bool
        if t0 goto 2 else 1
1:                                                           init.start P:1 S:1
        *init$guard = true:bool
        jump 2
2:                                                            init.done P:2 S:0
        return

Arpit Aggarwal

unread,
Feb 24, 2017, 11:25:48 AM2/24/17
to golang-nuts, adon...@google.com
Thank you very much adon...
Its very useful for my project.
Again, thanks a lot

Regards
Arpit
Reply all
Reply to author
Forward
0 new messages