func output(mod *ir.Module) { target := x86.NewTarget(platform.Linux) err := target.Generate(mod, os.Stdout) if err != nil { log.Fatal(err) } file, _ := os.OpenFile("test.asm", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666) target.Generate(mod, file) cmd := exec.Command("gcc", "-m32", "-x", "assembler", "test.asm", "-o", "test") str, _ := cmd.CombinedOutput() fmt.Println(string(str)) }
func main() { flag.Parse() if flag.NArg() < 1 { println("USAGE: brainfuck <program file>") return } codeFile := flag.Arg(0) source, err := os.Open(codeFile) if err != nil { panic(err) } defer source.Close() parser := new(Parser) parser.Source = bufio.NewReader(source) println("Parsing...") if err := parser.Parse(); err != nil { panic(err) } println("Building...") bundle, err := parser.BuildBundle() if err != nil { panic(err) } println("Generating code...") mod := ir.NewModule("mod") builder := ir.NewBuilder() mainFnType := ir.NewFunctionType([]ir.Type{}, ir.NewIntType(32), false) mainFn := mod.AddFunction("main", mainFnType) entry := mainFn.AddBlockAtStart("entry") builder.SetInsertAtBlockStart(entry) bundle.Codegen(builder) _ = x86.NewTarget(platform.Linux) }