Split up tgo.go
Этот коммит содержится в:
родитель
0b98ec83ba
коммит
4af2bcb6a9
2 изменённых файлов: 93 добавлений и 85 удалений
|
@ -2,7 +2,6 @@ package main
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"go/build"
|
||||
"go/constant"
|
||||
|
@ -1660,87 +1659,3 @@ func (c *Compiler) EmitObject(path string) error {
|
|||
f.Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Helper function for Compiler object.
|
||||
func Compile(pkgName, runtimePath, outpath, target string, printIR, dumpSSA bool) error {
|
||||
var buildTags []string
|
||||
// TODO: put this somewhere else
|
||||
if target == "pca10040" {
|
||||
buildTags = append(buildTags, "nrf", "nrf52", "nrf52832")
|
||||
target = "armv7m-none-eabi"
|
||||
} else if target == "arduino" {
|
||||
buildTags = append(buildTags, "avr", "avr8", "atmega", "atmega328p")
|
||||
target = "avr--"
|
||||
}
|
||||
|
||||
c, err := NewCompiler(pkgName, target, dumpSSA)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Add C/LLVM runtime.
|
||||
runtime, err := llvm.ParseBitcodeFile(runtimePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = c.LinkModule(runtime)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Compile Go code to IR.
|
||||
parseErr := func() error {
|
||||
if printIR {
|
||||
// Run this even if c.Parse() panics.
|
||||
defer func() {
|
||||
fmt.Println("Generated LLVM IR:")
|
||||
fmt.Println(c.IR())
|
||||
}()
|
||||
}
|
||||
return c.Parse(pkgName, buildTags)
|
||||
}()
|
||||
if parseErr != nil {
|
||||
return parseErr
|
||||
}
|
||||
|
||||
c.ApplyFunctionSections() // -ffunction-sections
|
||||
|
||||
if err := c.Verify(); err != nil {
|
||||
return err
|
||||
}
|
||||
//c.Optimize(2, 1) // -O2 -Os
|
||||
if err := c.Verify(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = c.EmitObject(outpath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
outpath := flag.String("o", "", "output filename")
|
||||
printIR := flag.Bool("printir", false, "print LLVM IR")
|
||||
dumpSSA := flag.Bool("dumpssa", false, "dump internal Go SSA")
|
||||
runtime := flag.String("runtime", "", "runtime LLVM bitcode files (from C sources)")
|
||||
target := flag.String("target", llvm.DefaultTargetTriple(), "LLVM target")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
if *outpath == "" || flag.NArg() != 1 {
|
||||
fmt.Fprintf(os.Stderr, "usage: %s [-printir] -runtime=<runtime.bc> [-target=<target>] -o <output> <input>", os.Args[0])
|
||||
flag.PrintDefaults()
|
||||
return
|
||||
}
|
||||
|
||||
os.Setenv("CC", "clang -target="+*target)
|
||||
|
||||
err := Compile(flag.Args()[0], *runtime, *outpath, *target, *printIR, *dumpSSA)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
93
main.go
Обычный файл
93
main.go
Обычный файл
|
@ -0,0 +1,93 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/aykevl/llvm/bindings/go/llvm"
|
||||
)
|
||||
|
||||
// Helper function for Compiler object.
|
||||
func Compile(pkgName, runtimePath, outpath, target string, printIR, dumpSSA bool) error {
|
||||
var buildTags []string
|
||||
// TODO: put this somewhere else
|
||||
if target == "pca10040" {
|
||||
buildTags = append(buildTags, "nrf", "nrf52", "nrf52832")
|
||||
target = "armv7m-none-eabi"
|
||||
} else if target == "arduino" {
|
||||
buildTags = append(buildTags, "avr", "avr8", "atmega", "atmega328p")
|
||||
target = "avr--"
|
||||
}
|
||||
|
||||
c, err := NewCompiler(pkgName, target, dumpSSA)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Add C/LLVM runtime.
|
||||
runtime, err := llvm.ParseBitcodeFile(runtimePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = c.LinkModule(runtime)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Compile Go code to IR.
|
||||
parseErr := func() error {
|
||||
if printIR {
|
||||
// Run this even if c.Parse() panics.
|
||||
defer func() {
|
||||
fmt.Println("Generated LLVM IR:")
|
||||
fmt.Println(c.IR())
|
||||
}()
|
||||
}
|
||||
return c.Parse(pkgName, buildTags)
|
||||
}()
|
||||
if parseErr != nil {
|
||||
return parseErr
|
||||
}
|
||||
|
||||
c.ApplyFunctionSections() // -ffunction-sections
|
||||
|
||||
if err := c.Verify(); err != nil {
|
||||
return err
|
||||
}
|
||||
//c.Optimize(2, 1) // -O2 -Os
|
||||
if err := c.Verify(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = c.EmitObject(outpath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
outpath := flag.String("o", "", "output filename")
|
||||
printIR := flag.Bool("printir", false, "print LLVM IR")
|
||||
dumpSSA := flag.Bool("dumpssa", false, "dump internal Go SSA")
|
||||
runtime := flag.String("runtime", "", "runtime LLVM bitcode files (from C sources)")
|
||||
target := flag.String("target", llvm.DefaultTargetTriple(), "LLVM target")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
if *outpath == "" || flag.NArg() != 1 {
|
||||
fmt.Fprintf(os.Stderr, "usage: %s [-printir] -runtime=<runtime.bc> [-target=<target>] -o <output> <input>", os.Args[0])
|
||||
flag.PrintDefaults()
|
||||
return
|
||||
}
|
||||
|
||||
os.Setenv("CC", "clang -target="+*target)
|
||||
|
||||
err := Compile(flag.Args()[0], *runtime, *outpath, *target, *printIR, *dumpSSA)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
Загрузка…
Создание таблицы
Сослаться в новой задаче