-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.go
33 lines (29 loc) · 825 Bytes
/
exec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package main
import (
"fmt"
"os"
"github.com/ajsnow/llvm"
)
// Exec JIT-compiles the top level statements in the roots chan and,
// if they are expressions, executes them.
func Exec(roots <-chan node, printLLVMIR bool) {
for n := range roots {
llvmIR := n.codegen()
if llvmIR.IsNil() {
fmt.Fprintln(os.Stderr, "Error: Codegen failed; skipping.")
continue
}
if printLLVMIR {
llvmIR.Dump()
}
if isTopLevelExpr(n) {
returnval := execEngine.RunFunction(llvmIR, []llvm.GenericValue{})
fmt.Println(returnval.Float(llvm.DoubleType()))
}
}
}
// isTopLevelExpr determines if the node is a top level expression.
// Top level expressions are function nodes with no name.
func isTopLevelExpr(n node) bool {
return n.Kind() == nodeFunction && n.(*functionNode).proto.(*fnPrototypeNode).name == ""
}