-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmain.go
65 lines (56 loc) · 1.3 KB
/
main.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"github.com/bspaans/jit-compiler/ir"
"github.com/bspaans/jit-compiler/ir/encoding/x86_64"
"github.com/bspaans/jit-compiler/ir/shared"
)
func REPL() {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("> ")
text, _ := reader.ReadString('\n')
statements, err := ir.ParseIR(ir.Stdlib + text)
if err != nil {
fmt.Println("Parse error: ", err.Error())
continue
}
debug := true
statements = statements.SSA_Transform(shared.NewSSA_Context())
instr, err := ir.Compile(&x86_64.X86_64{}, x86_64.NewABI_AMDSystemV(), []shared.IR{statements}, debug)
if err != nil {
fmt.Println("Compile error: ", err.Error())
continue
}
fmt.Println(instr.Execute(debug))
}
}
func CompileFiles() {
source := ""
for _, file := range os.Args[1:] {
text, err := ioutil.ReadFile(file)
if err != nil {
panic(err)
}
source += string(text) + "\n"
}
statements, err := ir.ParseIR(ir.Stdlib + source)
if err != nil {
panic(err)
}
debug := true
statements = statements.SSA_Transform(shared.NewSSA_Context())
if err := ir.CompileToBinary(&x86_64.X86_64{}, x86_64.NewABI_AMDSystemV(), []shared.IR{statements}, debug, "test.bin"); err != nil {
panic(err)
}
}
func main() {
if len(os.Args) == 1 {
REPL()
} else {
CompileFiles()
}
}