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 { type Compiler struct {
Output string Output string
EmitIr bool
Optimization int Optimization int
} }
@ -22,7 +21,6 @@ func main () {
compiler := new(Compiler) compiler := new(Compiler)
flag.StringVar(&compiler.Output, "o", "", "Output filename") 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.IntVar(&compiler.Optimization, "O", 0, "Optimization level (0-3)")
flag.Parse() flag.Parse()
@ -33,7 +31,7 @@ func main () {
err := compiler.Compile(flag.Args()) err := compiler.Compile(flag.Args())
if err != nil { if err != nil {
fmt.Println(err) fmt.Fprintf(os.Stderr, "%s: %v\n", os.Args[0], err)
os.Exit(1) os.Exit(1)
} }
} }
@ -56,31 +54,34 @@ func (this *Compiler) Compile (inputs []string) error {
module, err := generator.NativeTarget().Generate(semanticTree) module, err := generator.NativeTarget().Generate(semanticTree)
if err != nil { return err } if err != nil { return err }
if this.EmitIr { if this.Output == "" {
if this.Output == "" { this.Output = strings.TrimSuffix (
this.Output = strings.TrimSuffix ( inputs[0],
inputs[0], filepath.Ext(inputs[0])) + ".o"
filepath.Ext(inputs[0])) + ".ll" }
}
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) file, err := os.Create(this.Output)
if err != nil { return err } if err != nil { return err }
defer file.Close() defer file.Close()
_, err = module.WriteTo(file) _, err = module.WriteTo(file)
return err return err
} else { default:
if this.Output == "" { return errors.New(fmt.Sprintf (
this.Output = strings.TrimSuffix ( "unknown output type %", extension))
inputs[0],
filepath.Ext(inputs[0])) + ".o"
}
return this.CompileModule(module)
} }
} }
func (this *Compiler) CompileModule (module *llvm.Module) error { func (this *Compiler) CompileModule (module *llvm.Module, filetype string) error {
args := []string { args := []string {
"-", "-",
"-filetype=obj", fmt.Sprintf("-filetype=%s", filetype),
"-o", this.Output, "-o", this.Output,
fmt.Sprintf("-O=%d", this.Optimization), fmt.Sprintf("-O=%d", this.Optimization),
} }