Commit 7555f7f2 authored by Matthew Dempsky's avatar Matthew Dempsky

cmd/compile: cleanup mkbuiltin.go

Changes largely in preparation for eventually switching the builtin
export data to use the new binary format.

Replace fancy incremental line-by-line scanning with simply reading
the entire object file into memory, finding the export data section,
and processing it that way.

Just use "package runtime" and "package unsafe" in the builtin Go
source files so we don't need to rewrite references to "PACKAGE".

Stop looking for init_PACKAGE_function; it doesn't exist anyway.

Compile package runtime with -u so that its export data marks it as a
"safe" package.

Eliminate requirement to pass "runtime" and "unsafe" as command-line
arguments so that just "go run mkbuiltin.go" works.

Only rewrite builtin.go when successful.

Change-Id: I4addfde9e0cfb30607c7a83de686bde0ad1f035a
Reviewed-on: https://go-review.googlesource.com/19624Reviewed-by: default avatarRobert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 7d80291c
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
package gc package gc
const runtimeimport = "" + const runtimeimport = "" +
"package runtime\n" + "package runtime safe\n" +
"func @\"\".newobject (@\"\".typ·2 *byte) (? *any)\n" + "func @\"\".newobject (@\"\".typ·2 *byte) (? *any)\n" +
"func @\"\".panicindex ()\n" + "func @\"\".panicindex ()\n" +
"func @\"\".panicslice ()\n" + "func @\"\".panicslice ()\n" +
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
// +build ignore // +build ignore
package PACKAGE package runtime
// emitted by compiler, not referred to by go programs // emitted by compiler, not referred to by go programs
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
// +build ignore // +build ignore
package PACKAGE package unsafe
type Pointer uintptr // not really; filled in by compiler type Pointer uintptr // not really; filled in by compiler
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
//go:generate go run mkbuiltin.go runtime unsafe //go:generate go run mkbuiltin.go
package gc package gc
......
...@@ -4,95 +4,79 @@ ...@@ -4,95 +4,79 @@
// +build ignore // +build ignore
// Generate builtin.go from builtin/runtime.go and builtin/unsafe.go // Generate builtin.go from builtin/runtime.go and builtin/unsafe.go.
// (passed as arguments on the command line by a go:generate comment).
// Run this after changing builtin/runtime.go and builtin/unsafe.go // Run this after changing builtin/runtime.go and builtin/unsafe.go
// or after changing the export metadata format in the compiler. // or after changing the export metadata format in the compiler.
// Either way, you need to have a working compiler binary first. // Either way, you need to have a working compiler binary first.
package main package main
import ( import (
"bufio" "bytes"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log" "log"
"os" "os"
"os/exec" "os/exec"
"strings"
) )
func main() { func main() {
f, err := os.Create("builtin.go") var b bytes.Buffer
if err != nil { fmt.Fprintln(&b, "// AUTO-GENERATED by mkbuiltin.go; DO NOT EDIT")
log.Fatal(err) fmt.Fprintln(&b, "")
} fmt.Fprintln(&b, "package gc")
defer f.Close()
w := bufio.NewWriter(f)
fmt.Fprintln(w, "// AUTO-GENERATED by mkbuiltin.go; DO NOT EDIT")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "package gc")
for _, name := range os.Args[1:] { mkbuiltin(&b, "runtime")
mkbuiltin(w, name) mkbuiltin(&b, "unsafe")
}
if err := w.Flush(); err != nil { if err := ioutil.WriteFile("builtin.go", b.Bytes(), 0666); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
// Compile .go file, import data from .6 file, and write Go string version. // Compile .go file, import data from .o file, and write Go string version.
func mkbuiltin(w io.Writer, name string) { func mkbuiltin(w io.Writer, name string) {
if err := exec.Command("go", "tool", "compile", "-A", "builtin/"+name+".go").Run(); err != nil { args := []string{"tool", "compile", "-A"}
if name == "runtime" {
args = append(args, "-u")
}
args = append(args, "builtin/"+name+".go")
if err := exec.Command("go", args...).Run(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
obj := name + ".o" obj := name + ".o"
defer os.Remove(obj) defer os.Remove(obj)
r, err := os.Open(obj) b, err := ioutil.ReadFile(obj)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
defer r.Close()
scanner := bufio.NewScanner(r)
// Look for $$ that introduces imports. // Look for $$ that introduces imports.
for scanner.Scan() { i := bytes.Index(b, []byte("\n$$\n"))
if strings.Contains(scanner.Text(), "$$") { if i < 0 {
goto Begin log.Fatal("did not find beginning of imports")
}
} }
log.Fatal("did not find beginning of imports") i += 4
Begin: // Look for $$ that closes imports.
initfunc := fmt.Sprintf("init_%s_function", name) j := bytes.Index(b[i:], []byte("\n$$\n"))
if j < 0 {
fmt.Fprintf(w, "\nconst %simport = \"\" +\n", name) log.Fatal("did not find end of imports")
}
// sys.go claims to be in package PACKAGE to avoid j += i + 4
// conflicts during "go tool compile sys.go". Rename PACKAGE to $2.
replacer := strings.NewReplacer("PACKAGE", name)
// Process imports, stopping at $$ that closes them.
for scanner.Scan() {
p := scanner.Text()
if strings.Contains(p, "$$") {
goto End
}
// Process and reformat imports.
fmt.Fprintf(w, "\nconst %simport = \"\"", name)
for _, p := range bytes.SplitAfter(b[i:j], []byte("\n")) {
// Chop leading white space. // Chop leading white space.
p = strings.TrimLeft(p, " \t") p = bytes.TrimLeft(p, " \t")
if len(p) == 0 {
// Cut out decl of init_$1_function - it doesn't exist.
if strings.Contains(p, initfunc) {
continue continue
} }
fmt.Fprintf(w, "\t%q +\n", replacer.Replace(p)+"\n") fmt.Fprintf(w, " +\n\t%q", p)
} }
log.Fatal("did not find end of imports") fmt.Fprintf(w, "\n")
End:
fmt.Fprintf(w, "\t\"$$\\n\"\n")
} }
...@@ -67,7 +67,7 @@ func (p *parser) loadsys() { ...@@ -67,7 +67,7 @@ func (p *parser) loadsys() {
importpkg = Runtimepkg importpkg = Runtimepkg
if Debug['A'] != 0 { if Debug['A'] != 0 {
cannedimports("runtime.Builtin", "package runtime\n\n$$\n\n") cannedimports("runtime.Builtin", "package runtime safe\n\n$$\n\n")
} else { } else {
cannedimports("runtime.Builtin", runtimeimport) cannedimports("runtime.Builtin", runtimeimport)
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment