ginger/main.go

71 lines
1.7 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"github.com/mediocregopher/ginger/expr"
"llvm.org/llvm/bindings/go/llvm"
)
func main() {
//ee, err := expr.Parse(os.Stdin)
//if err != nil {
// panic(err)
//}
//for _, e := range ee {
// fmt.Println(e)
//}
llvm.LinkInMCJIT()
llvm.InitializeNativeTarget()
llvm.InitializeNativeAsmPrinter()
// setup our builder and module
2016-08-02 00:08:51 +00:00
lctx := expr.LLVMCtx{
B: llvm.NewBuilder(),
M: llvm.NewModule("my_module"),
}
2016-08-02 00:08:51 +00:00
// do the work in the function
a := expr.Expr{Actual: expr.Int(1)}
b := expr.Expr{Actual: expr.Int(2)}
c := expr.Expr{Actual: expr.Int(3)}
2016-08-02 00:08:51 +00:00
tup := expr.Expr{Actual: expr.Tuple([]expr.Expr{a, b, c})}
2016-07-28 19:57:17 +00:00
addMacro := expr.Expr{Actual: expr.Macro("add")}
stmt := expr.Expr{Actual: expr.Statement{In: tup, To: addMacro}}
2016-08-02 00:08:51 +00:00
block := expr.Block([]expr.Expr{stmt})
fn := block.LLVMVal(expr.RootCtx, lctx)
// create main and call our function
mainFn := llvm.AddFunction(lctx.M, "main", llvm.FunctionType(llvm.Int64Type(), []llvm.Type{}, false))
mainBlock := llvm.AddBasicBlock(mainFn, "entry")
lctx.B.SetInsertPoint(mainBlock, mainBlock.FirstInstruction())
2016-07-28 19:57:17 +00:00
2016-08-02 00:08:51 +00:00
ret := lctx.B.CreateCall(fn, []llvm.Value{}, "")
lctx.B.CreateRet(ret)
// verify it's all good
2016-08-02 00:08:51 +00:00
if err := llvm.VerifyModule(lctx.M, llvm.ReturnStatusAction); err != nil {
panic(err)
}
fmt.Println("# verified")
// Dump the IR
fmt.Println("# dumping IR")
2016-08-02 00:08:51 +00:00
lctx.M.Dump()
fmt.Println("# done dumping IR")
// create our exe engine
fmt.Println("# creating new execution engine")
2016-08-02 00:08:51 +00:00
engine, err := llvm.NewExecutionEngine(lctx.M)
if err != nil {
panic(err)
}
// run the function!
fmt.Println("# running the function main")
2016-08-02 00:08:51 +00:00
funcResult := engine.RunFunction(lctx.M.NamedFunction("main"), []llvm.GenericValue{})
fmt.Printf("%d\n", funcResult.Int(false))
}