forked from tardisgo/tardisgo
/
tardisgo.go
587 lines (534 loc) · 19.1 KB
/
tardisgo.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
// Copyright 2013 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.
// Modifications:
// Copyright 2014 Elliott Stoneham and The TARDIS Go Authors
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
// TARDIS Go is a Go->Haxe transpiler.
// However the tool is written with a "language" interface type separating the generic from the language specific parts of the code, which will allow other languages to be targeted in future.
// To see example code working in your browser please visit http://tardisgo.github.io .
// For simplicity, the current command line tool is simply a modified version of ssadump: a tool for displaying and interpreting the SSA form of Go programs.
package main
import (
"flag"
"fmt"
"go/build"
"log"
"os"
"runtime"
"runtime/pprof"
"strings"
"golang.org/x/tools/go/loader"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/interp"
"golang.org/x/tools/go/ssa/ssautil"
"golang.org/x/tools/go/types"
// TARDIS Go additions
"os/exec"
_ "github.com/tardisgo/tardisgo/haxe" // TARDIS Go addition
"github.com/tardisgo/tardisgo/pogo"
)
/*
var buildFlag = flag.String("build", "", `Options controlling the SSA builder.
The value is a sequence of zero or more of these letters:
C perform sanity [C]hecking of the SSA form.
D include [D]ebug info for every function.
P log [P]ackage inventory.
F log [F]unction SSA code.
S log [S]ource locations as SSA builder progresses.
G use binary object files from gc to provide imports (no code).
L build distinct packages seria[L]ly instead of in parallel.
N build [N]aive SSA form: don't replace local loads/stores with registers.
`)
*/
var testFlag = flag.Bool("test", false, "Loads test code (*_test.go) for imported packages.")
var LoadTestZipFS = false
const TestFS = "tgotestfs.zip"
var runFlag = flag.Bool("run", false, "Invokes the SSA interpreter on the program.")
var interpFlag = flag.String("interp", "", `Options controlling the SSA test interpreter.
The value is a sequence of zero or more more of these letters:
R disable [R]ecover() from panic; show interpreter crash instead.
T [T]race execution of the program. Best for single-threaded programs!
`)
// TARDIS Go addition
var allFlag = flag.String("haxe", "", "invokes the Haxe compiler (output ignored) and then runs the compiled program on the command line (OSX only): all=all targets, math=math-safe targets (cpp & js -D fullunsafe), interp=haxe interpreter")
var debugFlag = flag.Bool("debug", false, "Instrument the code to enable debugging, add comments, and give more meaningful information during a stack dump (warning: increased code size)")
var traceFlag = flag.Bool("trace", false, "Output trace information for every block visited (warning: huge output)")
var buidTags = flag.String("tags", "", "build tags separated by spaces")
var tgoroot = flag.String("tgoroot", "", "set goroot to the given value")
var modeFlag = ssa.BuilderModeFlag(flag.CommandLine, "build", 0)
// TODO
//var traceFlag = flag.Bool("v", false, "Verbose compiler mode (including files written)")
//var hxPackFlag = flag.String("hxpack", "tardis", "Sets the Haxe package name to use")
//var hxDirFlag = flag.String("hxdir", "tardis", "Sets the directory in which to output generated Haxe code")
//var hxLibFlag = flag.Bool("hxlib", false, "Generates code suitable for use as a Haxe library (no Dead Code Elimination)")
// TARDIS Go modification TODO review words here
const usage = `SSA builder and TARDIS Go transpiler (experimental).
Usage: tardisgo [<flag> ...] <args> ...
A shameless copy of the ssadump utility, but also writes a 'Go.hx' Haxe file into the 'tardis' sub-directory of the current location (which you must create by hand).
Example:
% tardisgo hello.go
Then to compile the tardis/Go.hx file generated, type the command line: "haxe -main tardis.Go -cp tardis -js tardis/go.js", or whatever Haxe compilation options you want to use.
Use -help to display other options.
`
const ignore = `
Use -help flag to display options.
Examples:
% ssadump -build=FPG hello.go # quickly dump SSA form of a single package
% ssadump -run -interp=T hello.go # interpret a program, with tracing
% ssadump -run unicode -- -test.v # interpret the unicode package's tests, verbosely
` + loader.FromArgsUsage +
`
When -run is specified, ssadump will find the first package that
defines a main function and run it in the interpreter.
If none is found, the tests of each package will be run instead.
`
// end TARDIS Go modification
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
func init() {
// If $GOMAXPROCS isn't set, use the full capacity of the machine.
// For small machines, use at least 4 threads.
if os.Getenv("GOMAXPROCS") == "" {
n := runtime.NumCPU()
if n < 4 {
n = 4
}
runtime.GOMAXPROCS(n)
}
}
func main() {
if err := doMain(); err != nil {
fmt.Fprintf(os.Stderr, "TARDISgo: %s\n", err) // TARDISgo alteration
os.Exit(1)
}
os.Exit(0)
}
func doMain() error {
flag.Parse()
args := flag.Args()
return doTestable(args)
}
func doTestable(args []string) error {
conf := loader.Config{
Build: &build.Default,
}
// TODO(adonovan): make go/types choose its default Sizes from
// build.Default or a specified *build.Context.
var wordSize int64 = 8
switch conf.Build.GOARCH {
case "386", "arm":
wordSize = 4
}
//if !(*runFlag) {
wordSize = 4 // TARDIS Go addition to force default int size to 32 bits
conf.Build.GOARCH = "haxe" // TARDIS Go addition - 32-bit int
conf.Build.GOOS = "nacl" // or haxe? TARDIS Go addition - simplest OS-specific code to emulate?
//}
conf.Build.BuildTags = strings.Split(*buidTags, " ")
conf.TypeChecker.Sizes = &types.StdSizes{ // must equal haxe.haxeStdSizes when (!*runFlag)
MaxAlign: 8,
WordSize: wordSize,
}
var mode ssa.BuilderMode
/*
for _, c := range *buildFlag {
switch c {
case 'D':
mode |= ssa.GlobalDebug
case 'P':
mode |= ssa.PrintPackages
case 'F':
mode |= ssa.PrintFunctions
case 'S':
mode |= ssa.LogSource | ssa.BuildSerially
case 'C':
mode |= ssa.SanityCheckFunctions
case 'N':
mode |= ssa.NaiveForm
case 'L':
mode |= ssa.BuildSerially
case 'I':
mode |= ssa.BareInits
default:
return fmt.Errorf("unknown -build option: '%c'", c)
}
}
*/
// TARDIS go addition
if *debugFlag {
mode |= ssa.GlobalDebug
}
var interpMode interp.Mode
for _, c := range *interpFlag {
switch c {
case 'T':
interpMode |= interp.EnableTracing
case 'R':
interpMode |= interp.DisableRecover
default:
log.Fatalf("Unknown -interp option: '%c'.", c)
}
}
if len(args) == 0 {
//fmt.Fprint(os.Stderr, usage)
return fmt.Errorf("%v", usage)
}
// Profiling support.
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
return err
}
err = pprof.StartCPUProfile(f)
if err != nil {
return err
}
defer pprof.StopCPUProfile()
}
// TODO Eventually this might be better as an environment variable
//if !(*runFlag) {
if *tgoroot == "" {
if conf.Build.GOPATH == "" {
return fmt.Errorf("GOPATH must be set")
}
conf.Build.GOROOT = strings.Split(conf.Build.GOPATH, ":")[0] + "/src/github.com/tardisgo/tardisgo/goroot/haxe/go1.4"
} else {
conf.Build.GOROOT = *tgoroot
}
//}
//fmt.Println("DEBUG GOPATH", conf.Build.GOPATH)
//fmt.Println("DEBUG GOROOT", conf.Build.GOROOT)
if *testFlag {
conf.ImportWithTests(args[0]) // assumes you give the full cannonical name of the package to test
args = args[1:]
}
// Use the initial packages from the command line.
_, err := conf.FromArgs(args, *testFlag)
if err != nil {
return err
}
// The interpreter needs the runtime package.
//if *runFlag {
conf.Import("runtime")
//} else {
// TARDIS GO additional line to add the language specific go runtime code
conf.Import(pogo.LanguageList[pogo.TargetLang].Goruntime) // TODO add code to set pogo.TargetLang when more than one of them
//}
// Load, parse and type-check the whole program, including the type definitions.
iprog, err := conf.Load()
if err != nil {
return err
}
// Create and build SSA-form program representation.
*modeFlag |= mode | ssa.SanityCheckFunctions
prog := ssautil.CreateProgram(iprog, *modeFlag)
prog.BuildAll()
// Run the interpreter.
if *runFlag {
var main *ssa.Package
pkgs := prog.AllPackages()
if *testFlag {
// If -test, run all packages' tests.
if len(pkgs) > 0 {
main = prog.CreateTestMainPackage(pkgs...)
}
if main == nil {
return fmt.Errorf("no tests")
}
} else {
// Otherwise, run main.main.
for _, pkg := range pkgs {
if pkg.Object.Name() == "main" {
main = pkg
if main.Func("main") == nil {
return fmt.Errorf("no func main() in main package")
}
break
}
}
if main == nil {
return fmt.Errorf("no main package")
}
}
// NOTE TARDIS Go removal of this test required if we alter the GOARCH to stop architecture-specific code
//if runtime.GOARCH != build.Default.GOARCH {
// return fmt.Errorf("cross-interpretation is not yet supported (target has GOARCH %s, interpreter has %s)",
// build.Default.GOARCH, runtime.GOARCH)
//}
interp.Interpret(main, interpMode, conf.TypeChecker.Sizes, main.Object.Path(), args)
} else {
// if not interpreting...
// TARDIS Go additions: copy run interpreter code above, but call pogo class
var main *ssa.Package
pkgs := prog.AllPackages()
//fmt.Println("DEBUG pkgs:", pkgs)
if *testFlag {
// If -test, run all packages' tests.
if len(pkgs) > 0 {
main = prog.CreateTestMainPackage(pkgs...)
}
if main == nil {
return fmt.Errorf("no tests")
}
fd, err := os.Open(TestFS)
fd.Close()
if err == nil {
LoadTestZipFS = true
for l := range pogo.LanguageList {
pogo.LanguageList[l].TestFS = TestFS
}
}
} else {
// Otherwise, run main.main.
for _, pkg := range pkgs {
if pkg.Object.Name() == "main" {
main = pkg
if main.Func("main") == nil {
return fmt.Errorf("no func main() in main package")
}
break
}
}
if main == nil {
return fmt.Errorf("no main package")
}
}
/*
if runtime.GOARCH != build.Default.GOARCH {
return fmt.Errorf("cross-interpretation is not yet supported (target has GOARCH %s, interpreter has %s)",
build.Default.GOARCH, runtime.GOARCH)
}
interp.Interpret(main, interpMode, conf.TypeChecker.Sizes, main.Object.Path(), args)
*/
pogo.DebugFlag = *debugFlag
pogo.TraceFlag = *traceFlag
err = pogo.EntryPoint(main) // TARDIS Go entry point, returns an error
if err != nil {
return err
}
results := make(chan resChan)
switch *allFlag {
case "": // NoOp
case "all", "bench":
//for _, dir := range dirs {
// err := os.RemoveAll(dir)
// if err != nil {
// fmt.Println("Error deleting existing '" + dir + "' directory: " + err.Error())
// }
//}
var targets [][][]string
if *allFlag == "bench" {
targets = allBenchmark // fast execution time
} else {
targets = allCompile // fast compile time
}
for _, cmd := range targets {
go doTarget(cmd, results)
}
for _ = range targets {
r := <-results
fmt.Println(r.output)
if (r.err != nil || len(strings.TrimSpace(r.output)) == 0) && *allFlag != "bench" {
os.Exit(1) // exit with an error if the test fails, but not for benchmarking
}
r.backChan <- true
}
case "math": // which is faster for the test with correct math processing, cpp or js?
//err := os.RemoveAll("tardis/cpp")
//if err != nil {
// fmt.Println("Error deleting existing '" + "tardis/cpp" + "' directory: " + err.Error())
//}
mathCmds := [][][]string{
[][]string{
[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-D", "inlinepointers", "-cpp", "tardis/cpp"},
[]string{"echo", `"CPP:"`},
[]string{"time", "./tardis/cpp/Go"},
},
[][]string{
[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-D", "inlinepointers", "-D", "fullunsafe", "-js", "tardis/go-fu.js"},
[]string{"echo", `"Node/JS using fullunsafe memory mode (js dataview):"`},
[]string{"time", "node", "tardis/go-fu.js"},
},
}
for _, cmd := range mathCmds {
go doTarget(cmd, results)
}
for _ = range mathCmds {
r := <-results
fmt.Println(r.output)
if r.err != nil {
os.Exit(1) // exit with an error if the test fails
}
r.backChan <- true
}
case "interp", "cpp", "cs", "js", "jsfu", "java": // for running tests
switch *allFlag {
case "interp":
go doTarget([][]string{
[]string{"echo", ``}, // Output from this line is ignored
[]string{"echo", `"Neko (haxe --interp):"`},
[]string{"time", "haxe", "-main", "tardis.Go", "-cp", "tardis", "--interp"},
}, results)
case "cpp":
go doTarget([][]string{
[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-D", "inlinepointers", "-cpp", "tardis/cpp"},
[]string{"echo", `"CPP:"`},
[]string{"time", "./tardis/cpp/Go"},
}, results)
case "cs":
go doTarget([][]string{
[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-D", "inlinepointers", "-cs", "tardis/cs"},
[]string{"echo", `"CS:"`},
[]string{"time", "mono", "./tardis/cs/bin/Go.exe"},
}, results)
case "js":
go doTarget([][]string{
[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-D", "inlinepointers", "-D", "uselocalfunctions", "-js", "tardis/go.js"},
[]string{"echo", `"Node/JS:"`},
[]string{"time", "node", "tardis/go.js"},
}, results)
case "jsfu":
go doTarget([][]string{
[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-D", "inlinepointers", "-D", "uselocalfunctions", "-D", "fullunsafe", "-js", "tardis/go-fu.js"},
[]string{"echo", `"Node/JS using fullunsafe memory mode (js dataview):"`},
[]string{"time", "node", "tardis/go-fu.js"},
}, results)
case "java":
go doTarget([][]string{
[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-D", "inlinepointers", "-java", "tardis/java"},
[]string{"echo", `"Java:"`},
[]string{"time", "java", "-jar", "tardis/java/Go.jar"},
}, results)
}
r := <-results
fmt.Println(r.output)
if r.err != nil {
os.Exit(1) // exit with an error if the test fails
}
r.backChan <- true
default:
panic("invalid value for -haxe flag: " + *allFlag)
}
}
return nil
}
//var dirs = []string{"tardis/cpp", "tardis/java", "tardis/cs" /*, "tardis/php"*/}
var allCompile = [][][]string{
[][]string{
[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-D", "inlinepointers", "-cpp", "tardis/cpp"},
[]string{"echo", `"CPP:"`},
[]string{"time", "./tardis/cpp/Go"},
},
[][]string{
[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-D", "inlinepointers", "-java", "tardis/java"},
[]string{"echo", `"Java:"`},
[]string{"time", "java", "-jar", "tardis/java/Go.jar"},
},
[][]string{
[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-D", "inlinepointers", "-cs", "tardis/cs"},
[]string{"echo", `"CS:"`},
[]string{"time", "mono", "./tardis/cs/bin/Go.exe"},
},
[][]string{
[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-D", "inlinepointers", "-D", "uselocalfunctions", "-js", "tardis/go.js"},
[]string{"echo", `"Node/JS:"`},
[]string{"time", "node", "tardis/go.js"},
},
}
var allBenchmark = [][][]string{
[][]string{
[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full" /*, "-D", "nulltempvars"*/, "-D", "inlinepointers" /*, "-D", "abstractobjects"*/, "-cpp", "tardis/cpp-bench"},
[]string{"echo", `"CPP (bench):"`},
[]string{"time", "./tardis/cpp-bench/Go"},
},
[][]string{
[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full" /*, "-D", "nulltempvars"*/, "-D", "inlinepointers" /*, "-D", "abstractobjects"*/, "-java", "tardis/java-bench"},
[]string{"echo", `"Java (bench):"`},
[]string{"time", "java", "-jar", "tardis/java-bench/Go.jar"},
},
[][]string{
[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full" /*, "-D", "nulltempvars"*/, "-D", "inlinepointers" /*, "-D", "abstractobjects"*/, "-cs", "tardis/cs-bench"},
[]string{"echo", `"CS (bench):"`},
[]string{"time", "mono", "./tardis/cs-bench/bin/Go.exe"},
},
[][]string{
[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full" /*, "-D", "nulltempvars"*/, "-D", "inlinepointers" /*, "-D", "abstractobjects" */, "-D", "jsinit", "-D", "uselocalfunctions", "-js", "tardis/go-bench.js"},
[]string{"echo", `"Node/JS (bench):"`},
[]string{"time", "node", "tardis/go-bench.js"},
},
// as this mode is no longer used for testing, remove it from the "all" tests
//[][]string{
// []string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-D", "fullunsafe", "-js", "tardis/go-fu.js"},
// []string{"echo", `"Node/JS using fullunsafe memory mode (js dataview):"`},
// []string{"time", "node", "tardis/go-fu.js"},
//},
// Cannot automate testing for SWF so removed
//[][]string{
// []string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-swf", "tardis/go.swf"},
// []string{"echo", `"Opening swf file (Chrome as a file association for swf works to test on OSX):"` + "\n"},
// []string{"open", "tardis/go.swf"},
//},
// PHP will never be a reliable target, so removed
//[][]string{
// []string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-php", "tardis/php", "--php-prefix", "tgo"},
// []string{"echo", `"PHP:"`},
// []string{"time", "php", "tardis/php/index.php"},
//},
// Seldom works, so removed
//[][]string{
// []string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-neko", "tardis/go.n"},
// []string{"echo", `"Neko (does not work for large code):"`},
// []string{"time", "neko", "tardis/go.n"},
//},
// only really useful for testing, so can be run from the command line
//[][]string{
// []string{"echo", ``}, // Output from this line is ignored
// []string{"echo", `"Neko (haxe --interp):"`},
// []string{"time", "haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "--interp"},
//},
}
type resChan struct {
output string
err error
backChan chan bool
}
func doTarget(cl [][]string, results chan resChan) {
res := ""
var lastErr error
for j, c := range cl {
if lastErr != nil {
break
}
exe := c[0]
if exe == "echo" {
res += c[1] + "\n"
} else {
_, err := exec.LookPath(exe)
if err != nil {
res += "TARDISgo error - executable not found: " + exe + "\n"
exe = "" // nothing to execute
}
if exe == "time" && c[1] == "node" && runtime.GOOS == "linux" {
c[1] = "nodejs" // for Ubuntu
}
if (exe == "haxe" || (exe == "time" && c[1] == "haxe")) && LoadTestZipFS {
c = append(c, "-resource")
c = append(c, TestFS)
}
if exe != "" {
out := []byte{}
out, lastErr = exec.Command(exe, c[1:]...).CombinedOutput()
if lastErr != nil {
out = append(out, []byte(lastErr.Error())...)
}
if j > 0 { // ignore the output from the compile phase
res += string(out)
}
}
}
}
bc := make(chan bool)
results <- resChan{res, lastErr, bc}
<-bc
}