-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
186 lines (161 loc) · 3.68 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
_ "embed"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"github.com/alecthomas/kong"
"github.com/jvmakine/shine/compiler"
"github.com/jvmakine/shine/grammar"
"github.com/jvmakine/shine/passes/callresolver"
"github.com/jvmakine/shine/passes/closureresolver"
"github.com/jvmakine/shine/passes/optimisation"
"github.com/jvmakine/shine/passes/typeinference"
"github.com/llir/llvm/ir"
)
//go:embed lib/runtime.ll
var runtime string
type Build struct {
File string `arg:"" name:"file" help:"Source code file"`
Output string `name:"output" short:"o" optional:"" help:"Output executable name"`
}
type Compile struct {
File string `arg:"" name:"file" help:"Source code file"`
}
type Run struct {
File string `arg:"" name:"file" help:"Source code file"`
}
type CLI struct {
Build Build `cmd:"" help:"Build executable"`
Compile Compile `cmd:"" help:"Compile to LLVM IR"`
Run Run `cmd:"" help:"Compile to LLVM IR"`
}
func main() {
cli := &CLI{}
ctx := kong.Parse(cli)
err := ctx.Run()
if err != nil {
fmt.Fprintln(os.Stderr, "ERROR: "+err.Error())
os.Exit(1)
}
}
func (cmd *Compile) Run() error {
text, err := ioutil.ReadFile(cmd.File)
if err != nil {
return err
}
module, err := compileModule(string(text))
if err != nil {
return err
}
fmt.Println(module)
return nil
}
func (cmd *Run) Run() error {
if err := verifyLLVMBinaries("lli", "llvm-link"); err != nil {
return err
}
dir, err := ioutil.TempDir(".", "run")
if err != nil {
return err
}
defer os.RemoveAll(dir)
all, err := compileIRTo(cmd.File, dir)
if err != nil {
return err
}
c := exec.Command("lli", all)
c.Stderr = os.Stderr
c.Stdout = os.Stdout
err = c.Run()
if err != nil {
return err
}
return nil
}
func (cmd *Build) Run() error {
if err := verifyLLVMBinaries("llvm-link", "clang"); err != nil {
return err
}
dir, err := ioutil.TempDir(".", "run")
if err != nil {
return err
}
defer os.RemoveAll(dir)
all, err := compileIRTo(cmd.File, dir)
if err != nil {
return err
}
c := exec.Command("clang", all)
c.Stderr = os.Stderr
c.Stdout = os.Stdout
if cmd.Output != "" {
c.Args = append(c.Args, "-o", cmd.Output)
}
err = c.Run()
if err != nil {
return err
}
return nil
}
func compileIRTo(file, dir string) (string, error) {
text, err := ioutil.ReadFile(file)
if err != nil {
return "", err
}
module, err := compileModule(string(text))
if err != nil {
return "", err
}
input := filepath.Join(dir, "input.ll")
rt := filepath.Join(dir, "runtime.ll")
all := filepath.Join(dir, "all.ll")
if err := ioutil.WriteFile(input, []byte(module.String()), 0600); err != nil {
return "", err
}
if err := ioutil.WriteFile(rt, []byte(runtime), 0600); err != nil {
return "", err
}
combined, err := exec.Command("llvm-link", "-S", input, rt).Output()
if err != nil {
return "", err
}
if err := ioutil.WriteFile(all, combined, 0600); err != nil {
return "", err
}
return all, nil
}
func verifyLLVMBinaries(bins ...string) error {
for _, b := range bins {
_, err := exec.LookPath(b)
if err != nil {
return errors.New(b + " could not be found. Is LLVM installed?")
}
}
return nil
}
func compileModule(text string) (*ir.Module, error) {
parsed, err := grammar.Parse(text)
if err != nil {
return nil, err
}
ast, err := parsed.ToAst()
if err != nil {
return nil, err
}
err = typeinference.Infer(ast)
if err != nil {
return nil, err
}
optimisation.SequentialFunctionPass(ast)
callresolver.ResolveFunctions(ast)
closureresolver.CollectClosures(ast)
optimisation.ClosureRemoval(ast)
optimisation.DeadCodeElimination(ast)
fcat := callresolver.Collect(ast)
module := compiler.Compile(ast, &fcat)
return module, nil
}