2015-01-22 10:48:33 -08:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"os"
|
|
|
|
|
|
|
|
|
|
"cmd/asm/internal/arch"
|
|
|
|
|
"cmd/asm/internal/asm"
|
|
|
|
|
"cmd/asm/internal/flags"
|
|
|
|
|
"cmd/asm/internal/lex"
|
|
|
|
|
|
|
|
|
|
"cmd/internal/obj"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
log.SetFlags(0)
|
|
|
|
|
log.SetPrefix("asm: ")
|
|
|
|
|
|
2015-02-02 10:56:41 -08:00
|
|
|
GOARCH := obj.Getgoarch()
|
2015-01-22 10:48:33 -08:00
|
|
|
|
|
|
|
|
architecture := arch.Set(GOARCH)
|
|
|
|
|
if architecture == nil {
|
|
|
|
|
log.Fatalf("asm: unrecognized architecture %s", GOARCH)
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-21 13:28:17 -04:00
|
|
|
flags.Parse()
|
2015-01-22 10:48:33 -08:00
|
|
|
|
|
|
|
|
// Create object file, write header.
|
|
|
|
|
fd, err := os.Create(*flags.OutputFile)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
ctxt := obj.Linknew(architecture.LinkArch)
|
|
|
|
|
if *flags.PrintOut {
|
|
|
|
|
ctxt.Debugasm = 1
|
|
|
|
|
}
|
2015-04-19 20:57:41 -07:00
|
|
|
ctxt.LineHist.TrimPathPrefix = *flags.TrimPath
|
2015-03-30 00:49:25 +00:00
|
|
|
ctxt.Flag_dynlink = *flags.Dynlink
|
|
|
|
|
if *flags.Shared || *flags.Dynlink {
|
2015-02-10 15:56:32 +13:00
|
|
|
ctxt.Flag_shared = 1
|
|
|
|
|
}
|
2015-01-22 10:48:33 -08:00
|
|
|
ctxt.Bso = obj.Binitw(os.Stdout)
|
2015-05-01 11:51:47 +10:00
|
|
|
defer ctxt.Bso.Flush()
|
2015-01-22 10:48:33 -08:00
|
|
|
ctxt.Diag = log.Fatalf
|
|
|
|
|
output := obj.Binitw(fd)
|
|
|
|
|
fmt.Fprintf(output, "go object %s %s %s\n", obj.Getgoos(), obj.Getgoarch(), obj.Getgoversion())
|
|
|
|
|
fmt.Fprintf(output, "!\n")
|
|
|
|
|
|
|
|
|
|
lexer := lex.NewLexer(flag.Arg(0), ctxt)
|
|
|
|
|
parser := asm.NewParser(ctxt, architecture, lexer)
|
|
|
|
|
pList := obj.Linknewplist(ctxt)
|
|
|
|
|
var ok bool
|
|
|
|
|
pList.Firstpc, ok = parser.Parse()
|
|
|
|
|
if !ok {
|
2015-04-08 11:24:48 -07:00
|
|
|
log.Printf("asm: assembly of %s failed", flag.Arg(0))
|
|
|
|
|
os.Remove(*flags.OutputFile)
|
2015-01-22 10:48:33 -08:00
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
obj.Writeobjdirect(ctxt, output)
|
2015-05-01 11:51:47 +10:00
|
|
|
output.Flush()
|
2015-01-22 10:48:33 -08:00
|
|
|
}
|