forked from go-llvm/llgo
/
llgo.go
281 lines (250 loc) · 6.5 KB
/
llgo.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright 2011 The llgo Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
// Portions (from gotype):
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"errors"
"flag"
"fmt"
"github.com/greggoryhz/gollvm/llvm"
"github.com/greggoryhz/llgo"
"go/ast"
"go/parser"
"go/scanner"
"go/token"
"log"
"os"
"runtime"
"sort"
"strings"
)
var dump = flag.Bool(
"dump", false,
"Dump the LLVM assembly to stderr and exit")
var trace = flag.Bool(
"trace", false,
"Trace the compilation process")
var importpath = flag.String(
"importpath", "",
"Package import path of the source being compiled (empty means the same as package name)")
var version = flag.Bool(
"version", false,
"Display version information and exit")
var os_ = flag.String("os", runtime.GOOS, "Set the target OS")
var arch = flag.String("arch", runtime.GOARCH, "Set the target architecture")
var triple = flag.String("triple", "", "Set the target triple")
var printTriple = flag.Bool("print-triple", false, "Print out target triple and exit")
var compileOnly = flag.Bool("c", false, "Compile only, don't link")
var outputFile = flag.String("o", "-", "Output filename")
var exitCode = 0
func report(err error) {
scanner.PrintError(os.Stderr, err)
exitCode = 2
}
func parseFile(fset *token.FileSet, filename string) *ast.File {
// parse entire file
mode := parser.DeclarationErrors | parser.ParseComments
//if *allErrors {
// mode |= parser.SpuriousErrors
//}
//if *printTrace {
// mode |= parser.Trace
//}
file, err := parser.ParseFile(fset, filename, nil, mode)
if err != nil {
report(err)
return nil
}
return file
}
func parseFiles(fset *token.FileSet, filenames []string) (files []*ast.File) {
sort.Strings(filenames)
for i, filename := range filenames {
if i > 0 && filenames[i-1] == filename {
report(errors.New(fmt.Sprintf("%q: duplicate file", filename)))
} else {
file := parseFile(fset, filename)
if file != nil {
files = append(files, file)
}
}
}
return
}
func isGoFilename(filename string) bool {
// ignore non-Go files
return !strings.HasPrefix(filename, ".") &&
strings.HasSuffix(filename, ".go")
}
func compileFiles(compiler llgo.Compiler, filenames []string, importpath string) (*llgo.Module, error) {
i := 0
for _, filename := range filenames {
switch _, err := os.Stat(filename); {
case err != nil:
report(err)
default:
filenames[i] = filename
i++
}
}
if i == 0 {
return nil, errors.New("No Go source files were specified")
}
fset := token.NewFileSet()
files := parseFiles(fset, filenames[0:i])
return compiler.Compile(fset, files, importpath)
}
func writeObjectFile(m *llgo.Module) error {
var outfile *os.File
switch *outputFile {
case "-":
outfile = os.Stdout
default:
var err error
outfile, err = os.Create(*outputFile)
if err != nil {
return err
}
}
err := llvm.VerifyModule(m.Module, llvm.ReturnStatusAction)
if err != nil {
return fmt.Errorf("Verification failed: %v", err)
}
return llvm.WriteBitcodeToFile(m.Module, outfile)
}
func displayVersion() {
fmt.Printf("llgo version %s (Go %s)\n", llgo.LLGOVersion, runtime.Version())
fmt.Println()
fmt.Println(" Available targets:")
longestTargetName := 0
targetDescriptions := make(map[string]string)
targetNames := make([]string, 0)
for target := llvm.FirstTarget(); target.C != nil; target = target.NextTarget() {
targetName := target.Name()
targetNames = append(targetNames, targetName)
targetDescriptions[targetName] = target.Description()
if len(targetName) > longestTargetName {
longestTargetName = len(targetName)
}
}
sort.Strings(targetNames)
for _, targetName := range targetNames {
var paddingLen int = longestTargetName - len(targetName)
fmt.Printf(" %s %*s %s\n", targetName, paddingLen+1, "-",
targetDescriptions[targetName])
}
fmt.Println()
os.Exit(0)
}
// Convert the architecture name to the string used in LLVM triples.
// See: llvm::Triple::getArchTypeName.
//
// TODO move this into the LLVM C API.
func getTripleArchName(llvmArch string) string {
switch llvmArch {
case "x86":
return "i386"
case "x86-64":
return "x86_64"
case "ppc32":
return "powerpc"
case "ppc64":
return "powerpc64"
}
return llvmArch
}
var tripleArchOsError = errors.New("-triple must not be specified as well as -os/-arch")
func computeTriple() string {
if *triple != "" {
// Ensure os/arch aren't specified if triple/ is specified.
//
// This is an ugly way of telling whether or not -os or -arch were
// specified. We can't just check the value, as it will have a default.
archFlag := flag.Lookup("arch")
osFlag := flag.Lookup("os")
flag.Visit(func(f *flag.Flag) {
switch f {
case archFlag, osFlag:
fmt.Fprintln(os.Stderr, tripleArchOsError)
os.Exit(1)
}
})
return *triple
}
// -arch is either an architecture name recognised by
// the gc compiler, or an LLVM architecture name.
targetArch := *arch
if targetArch == "" {
targetArch = runtime.GOARCH
}
switch targetArch {
case "386":
targetArch = "x86"
case "amd64", "x86_64":
targetArch = "x86-64"
}
// -os is either an OS name recognised by the gc
// compiler, or an LLVM OS name.
targetOS := *os_
if targetOS == "" {
targetOS = runtime.GOOS
}
switch targetOS {
case "windows":
targetOS = "win32"
}
tripleArch := getTripleArchName(targetArch)
return fmt.Sprintf("%s-unknown-%s", tripleArch, targetOS)
}
func initCompiler() (llgo.Compiler, error) {
opts := llgo.CompilerOptions{TargetTriple: computeTriple()}
if *trace {
opts.Logger = log.New(os.Stderr, "", 0)
}
return llgo.NewCompiler(opts)
}
func main() {
llvm.InitializeAllTargets()
llvm.InitializeAllTargetMCs()
llvm.InitializeAllTargetInfos()
flag.Parse()
if *version {
displayVersion()
}
if *printTriple {
fmt.Println(computeTriple())
os.Exit(0)
}
opts := llgo.CompilerOptions{}
opts.TargetTriple = computeTriple()
if *trace {
opts.Logger = log.New(os.Stderr, "", 0)
}
compiler, err := initCompiler()
if err != nil {
fmt.Fprintf(os.Stderr, "initCompiler failed: %s\n", err)
os.Exit(1)
}
defer compiler.Dispose()
module, err := compileFiles(compiler, flag.Args(), *importpath)
if err == nil {
defer module.Dispose()
if exitCode == 0 {
if *dump {
module.Dump()
} else {
err := writeObjectFile(module)
if err != nil {
fmt.Println(err)
}
}
}
} else {
report(err)
}
os.Exit(exitCode)
}