[dev.cc] cmd/asm: the assembler proper

Add main.go, the simple driver for the assembler, and the
subdirectory internal/asm, which contains the parser and
instruction generator.

It's likely that much of the implementation is superstition,
or at best experimental phenomenology, but it does generate
working binaries.

Change-Id: I322a9ae8a20174b6693153f30e39217ba68f8032
Reviewed-on: https://go-review.googlesource.com/3196
Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
Rob Pike 2015-01-22 10:48:33 -08:00
parent 8642639575
commit fdeee3a538
4 changed files with 1217 additions and 0 deletions

63
src/cmd/asm/main.go Normal file
View file

@ -0,0 +1,63 @@
// 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"
"go/build"
"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: ")
GOARCH := build.Default.GOARCH
architecture := arch.Set(GOARCH)
if architecture == nil {
log.Fatalf("asm: unrecognized architecture %s", GOARCH)
}
// Is this right?
flags.Parse(build.Default.GOROOT, build.Default.GOOS, GOARCH, architecture.Thechar)
// 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
}
ctxt.Bso = obj.Binitw(os.Stdout)
defer obj.Bflush(ctxt.Bso)
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 {
log.Print("FAIL TODO")
os.Exit(1)
}
obj.Writeobjdirect(ctxt, output)
obj.Bflush(output)
}