compiler: move output file type selection to main.go
Этот коммит содержится в:
родитель
9bec479041
коммит
6ab2b30984
2 изменённых файлов: 35 добавлений и 22 удалений
42
compiler.go
42
compiler.go
|
@ -3048,29 +3048,37 @@ func (c *Compiler) Optimize(optLevel, sizeLevel int, inlinerThreshold uint) {
|
||||||
modPasses.Run(c.mod)
|
modPasses.Run(c.mod)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Emit object file (.o).
|
||||||
func (c *Compiler) EmitObject(path string) error {
|
func (c *Compiler) EmitObject(path string) error {
|
||||||
// Generate output
|
llvmBuf, err := c.machine.EmitToMemoryBuffer(c.mod, llvm.ObjectFile)
|
||||||
var buf []byte
|
if err != nil {
|
||||||
if strings.HasSuffix(path, ".o") {
|
return err
|
||||||
llvmBuf, err := c.machine.EmitToMemoryBuffer(c.mod, llvm.ObjectFile)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
buf = llvmBuf.Bytes()
|
|
||||||
} else if strings.HasSuffix(path, ".bc") {
|
|
||||||
buf = llvm.WriteBitcodeToMemoryBuffer(c.mod).Bytes()
|
|
||||||
} else if strings.HasSuffix(path, ".ll") {
|
|
||||||
buf = []byte(c.mod.String())
|
|
||||||
} else {
|
|
||||||
return errors.New("unknown output file extension")
|
|
||||||
}
|
}
|
||||||
|
return c.writeFile(llvmBuf.Bytes(), path)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Emit LLVM bitcode file (.bc).
|
||||||
|
func (c *Compiler) EmitBitcode(path string) error {
|
||||||
|
data := llvm.WriteBitcodeToMemoryBuffer(c.mod).Bytes()
|
||||||
|
return c.writeFile(data, path)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Emit LLVM IR source file (.ll).
|
||||||
|
func (c *Compiler) EmitText(path string) error {
|
||||||
|
data := []byte(c.mod.String())
|
||||||
|
return c.writeFile(data, path)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write the data to the file specified by path.
|
||||||
|
func (c *Compiler) writeFile(data []byte, path string) error {
|
||||||
// Write output to file
|
// Write output to file
|
||||||
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0666)
|
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
f.Write(buf)
|
_, err = f.Write(data)
|
||||||
f.Close()
|
if err != nil {
|
||||||
return nil
|
return err
|
||||||
|
}
|
||||||
|
return f.Close()
|
||||||
}
|
}
|
||||||
|
|
15
main.go
15
main.go
|
@ -6,6 +6,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/aykevl/llvm/bindings/go/llvm"
|
"github.com/aykevl/llvm/bindings/go/llvm"
|
||||||
)
|
)
|
||||||
|
@ -58,12 +59,16 @@ func Compile(pkgName, runtimePath, outpath, target string, printIR, dumpSSA bool
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = c.EmitObject(outpath)
|
// Generate output.
|
||||||
if err != nil {
|
if strings.HasSuffix(outpath, ".o") {
|
||||||
return err
|
return c.EmitObject(outpath)
|
||||||
|
} else if strings.HasSuffix(outpath, ".bc") {
|
||||||
|
return c.EmitBitcode(outpath)
|
||||||
|
} else if strings.HasSuffix(outpath, ".ll") {
|
||||||
|
return c.EmitText(outpath)
|
||||||
|
} else {
|
||||||
|
return errors.New("unknown output file extension")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the specified package directly (using JIT or interpretation).
|
// Run the specified package directly (using JIT or interpretation).
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче