]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/gc: separate builtin and real runtime packages
authorMatthew Dempsky <mdempsky@google.com>
Tue, 28 Feb 2017 23:51:29 +0000 (15:51 -0800)
committerMatthew Dempsky <mdempsky@google.com>
Wed, 1 Mar 2017 01:06:32 +0000 (01:06 +0000)
The builtin runtime package definitions intentionally diverge from the
actual runtime package's, but this only works as long as they never
overlap.

To make it easier to expand the builtin runtime package, this CL now
loads their definitions into a logically separate "go.runtime"
package.  By resetting the package's Prefix field to "runtime", any
references to builtin definitions will still resolve against the real
package runtime.

Fixes #14482.

Passes toolstash -cmp.

Change-Id: I539c0994deaed4506a331f38c5b4d6bc8c95433f
Reviewed-on: https://go-review.googlesource.com/37538
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/cmd/compile/internal/gc/go.go
src/cmd/compile/internal/gc/main.go
src/cmd/compile/internal/gc/typecheck.go
src/cmd/compile/internal/gc/walk.go
test/runtime.go

index 95c1124f9e0bc4d13e70e51a7fc4439485dec0f6..9bf4b493951985bbb6490d44482b9926519004a7 100644 (file)
@@ -27,6 +27,14 @@ type Pkg struct {
        Syms     map[string]*Sym
 }
 
+// isRuntime reports whether p is package runtime.
+func (p *Pkg) isRuntime() bool {
+       if compiling_runtime && p == localpkg {
+               return true
+       }
+       return p.Path == "runtime"
+}
+
 // Sym represents an object name. Most commonly, this is a Go identifier naming
 // an object declared within a package, but Syms are also used to name internal
 // synthesized objects.
@@ -153,7 +161,7 @@ var itabpkg *Pkg // fake pkg for itab entries
 
 var itablinkpkg *Pkg // fake package for runtime itab entries
 
-var Runtimepkg *Pkg // package runtime
+var Runtimepkg *Pkg // fake package runtime
 
 var racepkg *Pkg // package runtime/race
 
index 2b1ae860d69cf343382ce32e2bd0f2cd825292a1..b9350d33e0ad806f23cd1b264098a11b14dab31b 100644 (file)
@@ -124,9 +124,14 @@ func Main() {
        unsafepkg = mkpkg("unsafe")
        unsafepkg.Name = "unsafe"
 
-       // real package, referred to by generated runtime calls
-       Runtimepkg = mkpkg("runtime")
+       // Pseudo-package that contains the compiler's builtin
+       // declarations for package runtime. These are declared in a
+       // separate package to avoid conflicts with package runtime's
+       // actual declarations, which may differ intentionally but
+       // insignificantly.
+       Runtimepkg = mkpkg("go.runtime")
        Runtimepkg.Name = "runtime"
+       Runtimepkg.Prefix = "runtime"
 
        // pseudo-packages used in symbol tables
        itabpkg = mkpkg("go.itab")
index 1aca44bce5cce80537723b8c9425538fc9581640..7b26b870c5082901fe452d428df04158468bc05b 100644 (file)
@@ -1289,7 +1289,7 @@ OpSwitch:
                if t.Results().NumFields() == 1 {
                        n.Type = l.Type.Results().Field(0).Type
 
-                       if n.Op == OCALLFUNC && n.Left.Op == ONAME && (compiling_runtime || n.Left.Sym.Pkg == Runtimepkg) && n.Left.Sym.Name == "getg" {
+                       if n.Op == OCALLFUNC && n.Left.Op == ONAME && n.Left.Sym.Pkg.isRuntime() && n.Left.Sym.Name == "getg" {
                                // Emit code for runtime.getg() directly instead of calling function.
                                // Most such rewrites (for example the similar one for math.Sqrt) should be done in walk,
                                // so that the ordering pass can make sure to preserve the semantics of the original code
index 96f66148a534f460c42a64b2f1864352c08568c9..2ba994991bdfa48ff019fcbd6a5c8e0fd83ff637 100644 (file)
@@ -1914,7 +1914,7 @@ func walkprint(nn *Node, init *Nodes) *Node {
                        on = substArgTypes(on, n.Type) // any-1
                } else if isInt[et] {
                        if et == TUINT64 {
-                               if (t.Sym.Pkg == Runtimepkg || compiling_runtime) && t.Sym.Name == "hex" {
+                               if t.Sym.Pkg.isRuntime() && t.Sym.Name == "hex" {
                                        on = syslook("printhex")
                                } else {
                                        on = syslook("printuint")
index bccc9b53afc021950284370cc95834b9aae147d2..0cf781b814bdad5711681d671520dc70fb81b8de 100644 (file)
@@ -17,5 +17,5 @@ package main
 import "runtime"
 
 func main() {
-       runtime.printbool(true) // ERROR "unexported"
+       runtime.printbool(true) // ERROR "unexported" "undefined"
 }