implement-modules #43

Closed
sashakoshka wants to merge 502 commits from implement-modules into main
Showing only changes of commit f08a09cee2 - Show all commits

View File

@ -14,7 +14,6 @@ import "git.tebibyte.media/sashakoshka/fspl/generator"
type Compiler struct {
Output string
EmitIr bool
Optimization int
}
@ -22,7 +21,6 @@ func main () {
compiler := new(Compiler)
flag.StringVar(&compiler.Output, "o", "", "Output filename")
flag.BoolVar(&compiler.EmitIr, "emit-llvm", false, "Output LLVM IR instead of an object file")
flag.IntVar(&compiler.Optimization, "O", 0, "Optimization level (0-3)")
flag.Parse()
@ -33,7 +31,7 @@ func main () {
err := compiler.Compile(flag.Args())
if err != nil {
fmt.Println(err)
fmt.Fprintf(os.Stderr, "%s: %v\n", os.Args[0], err)
os.Exit(1)
}
}
@ -56,31 +54,34 @@ func (this *Compiler) Compile (inputs []string) error {
module, err := generator.NativeTarget().Generate(semanticTree)
if err != nil { return err }
if this.EmitIr {
if this.Output == "" {
this.Output = strings.TrimSuffix (
inputs[0],
filepath.Ext(inputs[0])) + ".ll"
}
if this.Output == "" {
this.Output = strings.TrimSuffix (
inputs[0],
filepath.Ext(inputs[0])) + ".o"
}
extension := filepath.Ext(this.Output)
switch extension {
case ".s":
return this.CompileModule(module, "asm")
case ".o":
return this.CompileModule(module, "obj")
case ".ll":
file, err := os.Create(this.Output)
if err != nil { return err }
defer file.Close()
_, err = module.WriteTo(file)
return err
} else {
if this.Output == "" {
this.Output = strings.TrimSuffix (
inputs[0],
filepath.Ext(inputs[0])) + ".o"
}
return this.CompileModule(module)
default:
return errors.New(fmt.Sprintf (
"unknown output type %", extension))
}
}
func (this *Compiler) CompileModule (module *llvm.Module) error {
func (this *Compiler) CompileModule (module *llvm.Module, filetype string) error {
args := []string {
"-",
"-filetype=obj",
fmt.Sprintf("-filetype=%s", filetype),
"-o", this.Output,
fmt.Sprintf("-O=%d", this.Optimization),
}