]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/cmd/compile/internal/reflectdata/reflect.go
[dev.boringcrypto] all: merge master (2f0da6d) into dev.boringcrypto
[gostls13.git] / src / cmd / compile / internal / reflectdata / reflect.go
similarity index 54%
rename from src/cmd/compile/internal/gc/reflect.go
rename to src/cmd/compile/internal/reflectdata/reflect.go
index c12e99e71ee8174093e833d409e07a72b7126608..df92a9f3498fdce1b20289f356545606697dabfe 100644 (file)
@@ -2,19 +2,28 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package gc
+package reflectdata
 
 import (
-       "cmd/compile/internal/types"
-       "cmd/internal/gcprog"
-       "cmd/internal/obj"
-       "cmd/internal/objabi"
-       "cmd/internal/src"
        "fmt"
        "os"
        "sort"
        "strings"
        "sync"
+
+       "cmd/compile/internal/base"
+       "cmd/compile/internal/bitvec"
+       "cmd/compile/internal/escape"
+       "cmd/compile/internal/inline"
+       "cmd/compile/internal/ir"
+       "cmd/compile/internal/objw"
+       "cmd/compile/internal/typebits"
+       "cmd/compile/internal/typecheck"
+       "cmd/compile/internal/types"
+       "cmd/internal/gcprog"
+       "cmd/internal/obj"
+       "cmd/internal/objabi"
+       "cmd/internal/src"
 )
 
 type itabEntry struct {
@@ -23,7 +32,7 @@ type itabEntry struct {
 
        // symbols of each method in
        // the itab, sorted by byte offset;
-       // filled in by peekitabs
+       // filled in by CompileITabs
        entries []*obj.LSym
 }
 
@@ -32,6 +41,10 @@ type ptabEntry struct {
        t *types.Type
 }
 
+func CountTabs() (numPTabs, numITabs int) {
+       return len(ptabs), len(itabs)
+}
+
 // runtime interface and reflection data structures
 var (
        signatmu    sync.Mutex // protects signatset and signatslice
@@ -39,13 +52,13 @@ var (
        signatslice []*types.Type
 
        itabs []itabEntry
-       ptabs []ptabEntry
+       ptabs []*ir.Name
 )
 
-type Sig struct {
+type typeSig struct {
        name  *types.Sym
-       isym  *types.Sym
-       tsym  *types.Sym
+       isym  *obj.LSym
+       tsym  *obj.LSym
        type_ *types.Type
        mtype *types.Type
 }
@@ -61,35 +74,32 @@ const (
        MAXELEMSIZE = 128
 )
 
-func structfieldSize() int { return 3 * Widthptr }       // Sizeof(runtime.structfield{})
-func imethodSize() int     { return 4 + 4 }              // Sizeof(runtime.imethod{})
-func commonSize() int      { return 4*Widthptr + 8 + 8 } // Sizeof(runtime._type{})
+func structfieldSize() int { return 3 * types.PtrSize }       // Sizeof(runtime.structfield{})
+func imethodSize() int     { return 4 + 4 }                   // Sizeof(runtime.imethod{})
+func commonSize() int      { return 4*types.PtrSize + 8 + 8 } // Sizeof(runtime._type{})
 
 func uncommonSize(t *types.Type) int { // Sizeof(runtime.uncommontype{})
-       if t.Sym == nil && len(methods(t)) == 0 {
+       if t.Sym() == nil && len(methods(t)) == 0 {
                return 0
        }
        return 4 + 2 + 2 + 4 + 4
 }
 
 func makefield(name string, t *types.Type) *types.Field {
-       f := types.NewField()
-       f.Type = t
-       f.Sym = (*types.Pkg)(nil).Lookup(name)
-       return f
+       sym := (*types.Pkg)(nil).Lookup(name)
+       return types.NewField(src.NoXPos, sym, t)
 }
 
-// bmap makes the map bucket type given the type of the map.
-func bmap(t *types.Type) *types.Type {
+// MapBucketType makes the map bucket type given the type of the map.
+func MapBucketType(t *types.Type) *types.Type {
        if t.MapType().Bucket != nil {
                return t.MapType().Bucket
        }
 
-       bucket := types.New(TSTRUCT)
        keytype := t.Key()
        elemtype := t.Elem()
-       dowidth(keytype)
-       dowidth(elemtype)
+       types.CalcSize(keytype)
+       types.CalcSize(elemtype)
        if keytype.Width > MAXKEYSIZE {
                keytype = types.NewPtr(keytype)
        }
@@ -100,7 +110,7 @@ func bmap(t *types.Type) *types.Type {
        field := make([]*types.Field, 0, 5)
 
        // The first field is: uint8 topbits[BUCKETSIZE].
-       arr := types.NewArray(types.Types[TUINT8], BUCKETSIZE)
+       arr := types.NewArray(types.Types[types.TUINT8], BUCKETSIZE)
        field = append(field, makefield("topbits", arr))
 
        arr = types.NewArray(keytype, BUCKETSIZE)
@@ -119,66 +129,66 @@ func bmap(t *types.Type) *types.Type {
        // Arrange for the bucket to have no pointers by changing
        // the type of the overflow field to uintptr in this case.
        // See comment on hmap.overflow in runtime/map.go.
-       otyp := types.NewPtr(bucket)
+       otyp := types.Types[types.TUNSAFEPTR]
        if !elemtype.HasPointers() && !keytype.HasPointers() {
-               otyp = types.Types[TUINTPTR]
+               otyp = types.Types[types.TUINTPTR]
        }
        overflow := makefield("overflow", otyp)
        field = append(field, overflow)
 
        // link up fields
+       bucket := types.NewStruct(types.NoPkg, field[:])
        bucket.SetNoalg(true)
-       bucket.SetFields(field[:])
-       dowidth(bucket)
+       types.CalcSize(bucket)
 
        // Check invariants that map code depends on.
-       if !IsComparable(t.Key()) {
-               Fatalf("unsupported map key type for %v", t)
+       if !types.IsComparable(t.Key()) {
+               base.Fatalf("unsupported map key type for %v", t)
        }
        if BUCKETSIZE < 8 {
-               Fatalf("bucket size too small for proper alignment")
+               base.Fatalf("bucket size too small for proper alignment")
        }
        if keytype.Align > BUCKETSIZE {
-               Fatalf("key align too big for %v", t)
+               base.Fatalf("key align too big for %v", t)
        }
        if elemtype.Align > BUCKETSIZE {
-               Fatalf("elem align too big for %v", t)
+               base.Fatalf("elem align too big for %v", t)
        }
        if keytype.Width > MAXKEYSIZE {
-               Fatalf("key size to large for %v", t)
+               base.Fatalf("key size to large for %v", t)
        }
        if elemtype.Width > MAXELEMSIZE {
-               Fatalf("elem size to large for %v", t)
+               base.Fatalf("elem size to large for %v", t)
        }
        if t.Key().Width > MAXKEYSIZE && !keytype.IsPtr() {
-               Fatalf("key indirect incorrect for %v", t)
+               base.Fatalf("key indirect incorrect for %v", t)
        }
        if t.Elem().Width > MAXELEMSIZE && !elemtype.IsPtr() {
-               Fatalf("elem indirect incorrect for %v", t)
+               base.Fatalf("elem indirect incorrect for %v", t)
        }
        if keytype.Width%int64(keytype.Align) != 0 {
-               Fatalf("key size not a multiple of key align for %v", t)
+               base.Fatalf("key size not a multiple of key align for %v", t)
        }
        if elemtype.Width%int64(elemtype.Align) != 0 {
-               Fatalf("elem size not a multiple of elem align for %v", t)
+               base.Fatalf("elem size not a multiple of elem align for %v", t)
        }
        if bucket.Align%keytype.Align != 0 {
-               Fatalf("bucket align not multiple of key align %v", t)
+               base.Fatalf("bucket align not multiple of key align %v", t)
        }
        if bucket.Align%elemtype.Align != 0 {
-               Fatalf("bucket align not multiple of elem align %v", t)
+               base.Fatalf("bucket align not multiple of elem align %v", t)
        }
        if keys.Offset%int64(keytype.Align) != 0 {
-               Fatalf("bad alignment of keys in bmap for %v", t)
+               base.Fatalf("bad alignment of keys in bmap for %v", t)
        }
        if elems.Offset%int64(elemtype.Align) != 0 {
-               Fatalf("bad alignment of elems in bmap for %v", t)
+               base.Fatalf("bad alignment of elems in bmap for %v", t)
        }
 
        // Double-check that overflow field is final memory in struct,
        // with no padding at end.
-       if overflow.Offset != bucket.Width-int64(Widthptr) {
-               Fatalf("bad offset of overflow in bmap for %v", t)
+       if overflow.Offset != bucket.Width-int64(types.PtrSize) {
+               base.Fatalf("bad offset of overflow in bmap for %v", t)
        }
 
        t.MapType().Bucket = bucket
@@ -187,14 +197,14 @@ func bmap(t *types.Type) *types.Type {
        return bucket
 }
 
-// hmap builds a type representing a Hmap structure for the given map type.
+// MapType builds a type representing a Hmap structure for the given map type.
 // Make sure this stays in sync with runtime/map.go.
-func hmap(t *types.Type) *types.Type {
+func MapType(t *types.Type) *types.Type {
        if t.MapType().Hmap != nil {
                return t.MapType().Hmap
        }
 
-       bmap := bmap(t)
+       bmap := MapBucketType(t)
 
        // build a struct:
        // type hmap struct {
@@ -210,26 +220,25 @@ func hmap(t *types.Type) *types.Type {
        // }
        // must match runtime/map.go:hmap.
        fields := []*types.Field{
-               makefield("count", types.Types[TINT]),
-               makefield("flags", types.Types[TUINT8]),
-               makefield("B", types.Types[TUINT8]),
-               makefield("noverflow", types.Types[TUINT16]),
-               makefield("hash0", types.Types[TUINT32]), // Used in walk.go for OMAKEMAP.
-               makefield("buckets", types.NewPtr(bmap)), // Used in walk.go for OMAKEMAP.
+               makefield("count", types.Types[types.TINT]),
+               makefield("flags", types.Types[types.TUINT8]),
+               makefield("B", types.Types[types.TUINT8]),
+               makefield("noverflow", types.Types[types.TUINT16]),
+               makefield("hash0", types.Types[types.TUINT32]), // Used in walk.go for OMAKEMAP.
+               makefield("buckets", types.NewPtr(bmap)),       // Used in walk.go for OMAKEMAP.
                makefield("oldbuckets", types.NewPtr(bmap)),
-               makefield("nevacuate", types.Types[TUINTPTR]),
-               makefield("extra", types.Types[TUNSAFEPTR]),
+               makefield("nevacuate", types.Types[types.TUINTPTR]),
+               makefield("extra", types.Types[types.TUNSAFEPTR]),
        }
 
-       hmap := types.New(TSTRUCT)
+       hmap := types.NewStruct(types.NoPkg, fields)
        hmap.SetNoalg(true)
-       hmap.SetFields(fields)
-       dowidth(hmap)
+       types.CalcSize(hmap)
 
        // The size of hmap should be 48 bytes on 64 bit
        // and 28 bytes on 32 bit platforms.
-       if size := int64(8 + 5*Widthptr); hmap.Width != size {
-               Fatalf("hmap size not correct: got %d, want %d", hmap.Width, size)
+       if size := int64(8 + 5*types.PtrSize); hmap.Width != size {
+               base.Fatalf("hmap size not correct: got %d, want %d", hmap.Width, size)
        }
 
        t.MapType().Hmap = hmap
@@ -237,15 +246,15 @@ func hmap(t *types.Type) *types.Type {
        return hmap
 }
 
-// hiter builds a type representing an Hiter structure for the given map type.
+// MapIterType builds a type representing an Hiter structure for the given map type.
 // Make sure this stays in sync with runtime/map.go.
-func hiter(t *types.Type) *types.Type {
+func MapIterType(t *types.Type) *types.Type {
        if t.MapType().Hiter != nil {
                return t.MapType().Hiter
        }
 
-       hmap := hmap(t)
-       bmap := bmap(t)
+       hmap := MapType(t)
+       bmap := MapBucketType(t)
 
        // build a struct:
        // type hiter struct {
@@ -269,210 +278,110 @@ func hiter(t *types.Type) *types.Type {
        fields := []*types.Field{
                makefield("key", types.NewPtr(t.Key())),   // Used in range.go for TMAP.
                makefield("elem", types.NewPtr(t.Elem())), // Used in range.go for TMAP.
-               makefield("t", types.Types[TUNSAFEPTR]),
+               makefield("t", types.Types[types.TUNSAFEPTR]),
                makefield("h", types.NewPtr(hmap)),
                makefield("buckets", types.NewPtr(bmap)),
                makefield("bptr", types.NewPtr(bmap)),
-               makefield("overflow", types.Types[TUNSAFEPTR]),
-               makefield("oldoverflow", types.Types[TUNSAFEPTR]),
-               makefield("startBucket", types.Types[TUINTPTR]),
-               makefield("offset", types.Types[TUINT8]),
-               makefield("wrapped", types.Types[TBOOL]),
-               makefield("B", types.Types[TUINT8]),
-               makefield("i", types.Types[TUINT8]),
-               makefield("bucket", types.Types[TUINTPTR]),
-               makefield("checkBucket", types.Types[TUINTPTR]),
+               makefield("overflow", types.Types[types.TUNSAFEPTR]),
+               makefield("oldoverflow", types.Types[types.TUNSAFEPTR]),
+               makefield("startBucket", types.Types[types.TUINTPTR]),
+               makefield("offset", types.Types[types.TUINT8]),
+               makefield("wrapped", types.Types[types.TBOOL]),
+               makefield("B", types.Types[types.TUINT8]),
+               makefield("i", types.Types[types.TUINT8]),
+               makefield("bucket", types.Types[types.TUINTPTR]),
+               makefield("checkBucket", types.Types[types.TUINTPTR]),
        }
 
        // build iterator struct holding the above fields
-       hiter := types.New(TSTRUCT)
+       hiter := types.NewStruct(types.NoPkg, fields)
        hiter.SetNoalg(true)
-       hiter.SetFields(fields)
-       dowidth(hiter)
-       if hiter.Width != int64(12*Widthptr) {
-               Fatalf("hash_iter size not correct %d %d", hiter.Width, 12*Widthptr)
+       types.CalcSize(hiter)
+       if hiter.Width != int64(12*types.PtrSize) {
+               base.Fatalf("hash_iter size not correct %d %d", hiter.Width, 12*types.PtrSize)
        }
        t.MapType().Hiter = hiter
        hiter.StructType().Map = t
        return hiter
 }
 
-// deferstruct makes a runtime._defer structure, with additional space for
-// stksize bytes of args.
-func deferstruct(stksize int64) *types.Type {
-       makefield := func(name string, typ *types.Type) *types.Field {
-               f := types.NewField()
-               f.Type = typ
-               // Unlike the global makefield function, this one needs to set Pkg
-               // because these types might be compared (in SSA CSE sorting).
-               // TODO: unify this makefield and the global one above.
-               f.Sym = &types.Sym{Name: name, Pkg: localpkg}
-               return f
-       }
-       argtype := types.NewArray(types.Types[TUINT8], stksize)
-       argtype.Width = stksize
-       argtype.Align = 1
-       // These fields must match the ones in runtime/runtime2.go:_defer and
-       // cmd/compile/internal/gc/ssa.go:(*state).call.
-       fields := []*types.Field{
-               makefield("siz", types.Types[TUINT32]),
-               makefield("started", types.Types[TBOOL]),
-               makefield("heap", types.Types[TBOOL]),
-               makefield("openDefer", types.Types[TBOOL]),
-               makefield("sp", types.Types[TUINTPTR]),
-               makefield("pc", types.Types[TUINTPTR]),
-               // Note: the types here don't really matter. Defer structures
-               // are always scanned explicitly during stack copying and GC,
-               // so we make them uintptr type even though they are real pointers.
-               makefield("fn", types.Types[TUINTPTR]),
-               makefield("_panic", types.Types[TUINTPTR]),
-               makefield("link", types.Types[TUINTPTR]),
-               makefield("framepc", types.Types[TUINTPTR]),
-               makefield("varp", types.Types[TUINTPTR]),
-               makefield("fd", types.Types[TUINTPTR]),
-               makefield("args", argtype),
-       }
-
-       // build struct holding the above fields
-       s := types.New(TSTRUCT)
-       s.SetNoalg(true)
-       s.SetFields(fields)
-       s.Width = widstruct(s, s, 0, 1)
-       s.Align = uint8(Widthptr)
-       return s
-}
-
-// f is method type, with receiver.
-// return function type, receiver as first argument (or not).
-func methodfunc(f *types.Type, receiver *types.Type) *types.Type {
-       inLen := f.Params().Fields().Len()
-       if receiver != nil {
-               inLen++
-       }
-       in := make([]*Node, 0, inLen)
-
-       if receiver != nil {
-               d := anonfield(receiver)
-               in = append(in, d)
-       }
-
-       for _, t := range f.Params().Fields().Slice() {
-               d := anonfield(t.Type)
-               d.SetIsDDD(t.IsDDD())
-               in = append(in, d)
-       }
-
-       outLen := f.Results().Fields().Len()
-       out := make([]*Node, 0, outLen)
-       for _, t := range f.Results().Fields().Slice() {
-               d := anonfield(t.Type)
-               out = append(out, d)
-       }
-
-       t := functype(nil, in, out)
-       if f.Nname() != nil {
-               // Link to name of original method function.
-               t.SetNname(f.Nname())
-       }
-
-       return t
-}
-
 // methods returns the methods of the non-interface type t, sorted by name.
 // Generates stub functions as needed.
-func methods(t *types.Type) []*Sig {
+func methods(t *types.Type) []*typeSig {
        // method type
-       mt := methtype(t)
+       mt := types.ReceiverBaseType(t)
 
        if mt == nil {
                return nil
        }
-       expandmeth(mt)
+       typecheck.CalcMethods(mt)
 
        // type stored in interface word
        it := t
 
-       if !isdirectiface(it) {
+       if !types.IsDirectIface(it) {
                it = types.NewPtr(t)
        }
 
        // make list of methods for t,
        // generating code if necessary.
-       var ms []*Sig
+       var ms []*typeSig
        for _, f := range mt.AllMethods().Slice() {
+               if f.Sym == nil {
+                       base.Fatalf("method with no sym on %v", mt)
+               }
                if !f.IsMethod() {
-                       Fatalf("non-method on %v method %v %v\n", mt, f.Sym, f)
+                       base.Fatalf("non-method on %v method %v %v", mt, f.Sym, f)
                }
                if f.Type.Recv() == nil {
-                       Fatalf("receiver with no type on %v method %v %v\n", mt, f.Sym, f)
+                       base.Fatalf("receiver with no type on %v method %v %v", mt, f.Sym, f)
                }
                if f.Nointerface() {
                        continue
                }
 
-               method := f.Sym
-               if method == nil {
-                       break
-               }
-
                // get receiver type for this particular method.
                // if pointer receiver but non-pointer t and
                // this is not an embedded pointer inside a struct,
                // method does not apply.
-               if !isMethodApplicable(t, f) {
+               if !types.IsMethodApplicable(t, f) {
                        continue
                }
 
-               sig := &Sig{
-                       name:  method,
-                       isym:  methodSym(it, method),
-                       tsym:  methodSym(t, method),
-                       type_: methodfunc(f.Type, t),
-                       mtype: methodfunc(f.Type, nil),
+               sig := &typeSig{
+                       name:  f.Sym,
+                       isym:  methodWrapper(it, f),
+                       tsym:  methodWrapper(t, f),
+                       type_: typecheck.NewMethodType(f.Type, t),
+                       mtype: typecheck.NewMethodType(f.Type, nil),
                }
                ms = append(ms, sig)
-
-               this := f.Type.Recv().Type
-
-               if !sig.isym.Siggen() {
-                       sig.isym.SetSiggen(true)
-                       if !types.Identical(this, it) {
-                               genwrapper(it, f, sig.isym)
-                       }
-               }
-
-               if !sig.tsym.Siggen() {
-                       sig.tsym.SetSiggen(true)
-                       if !types.Identical(this, t) {
-                               genwrapper(t, f, sig.tsym)
-                       }
-               }
        }
 
        return ms
 }
 
 // imethods returns the methods of the interface type t, sorted by name.
-func imethods(t *types.Type) []*Sig {
-       var methods []*Sig
+func imethods(t *types.Type) []*typeSig {
+       var methods []*typeSig
        for _, f := range t.Fields().Slice() {
-               if f.Type.Etype != TFUNC || f.Sym == nil {
+               if f.Type.Kind() != types.TFUNC || f.Sym == nil {
                        continue
                }
                if f.Sym.IsBlank() {
-                       Fatalf("unexpected blank symbol in interface method set")
+                       base.Fatalf("unexpected blank symbol in interface method set")
                }
                if n := len(methods); n > 0 {
                        last := methods[n-1]
                        if !last.name.Less(f.Sym) {
-                               Fatalf("sigcmp vs sortinter %v %v", last.name, f.Sym)
+                               base.Fatalf("sigcmp vs sortinter %v %v", last.name, f.Sym)
                        }
                }
 
-               sig := &Sig{
+               sig := &typeSig{
                        name:  f.Sym,
                        mtype: f.Type,
-                       type_: methodfunc(f.Type, nil),
+                       type_: typecheck.NewMethodType(f.Type, nil),
                }
                methods = append(methods, sig)
 
@@ -480,11 +389,7 @@ func imethods(t *types.Type) []*Sig {
                // IfaceType.Method is not in the reflect data.
                // Generate the method body, so that compiled
                // code can refer to it.
-               isym := methodSym(t, f.Sym)
-               if !isym.Siggen() {
-                       isym.SetSiggen(true)
-                       genwrapper(t, f, isym)
-               }
+               methodWrapper(t, f)
        }
 
        return methods
@@ -496,79 +401,79 @@ func dimportpath(p *types.Pkg) {
        }
 
        // If we are compiling the runtime package, there are two runtime packages around
-       // -- localpkg and Runtimepkg. We don't want to produce import path symbols for
+       // -- localpkg and Pkgs.Runtime. We don't want to produce import path symbols for
        // both of them, so just produce one for localpkg.
-       if myimportpath == "runtime" && p == Runtimepkg {
+       if base.Ctxt.Pkgpath == "runtime" && p == ir.Pkgs.Runtime {
                return
        }
 
        str := p.Path
-       if p == localpkg {
+       if p == types.LocalPkg {
                // Note: myimportpath != "", or else dgopkgpath won't call dimportpath.
-               str = myimportpath
+               str = base.Ctxt.Pkgpath
        }
 
-       s := Ctxt.Lookup("type..importpath." + p.Prefix + ".")
+       s := base.Ctxt.Lookup("type..importpath." + p.Prefix + ".")
        ot := dnameData(s, 0, str, "", nil, false)
-       ggloblsym(s, int32(ot), obj.DUPOK|obj.RODATA)
+       objw.Global(s, int32(ot), obj.DUPOK|obj.RODATA)
        s.Set(obj.AttrContentAddressable, true)
        p.Pathsym = s
 }
 
 func dgopkgpath(s *obj.LSym, ot int, pkg *types.Pkg) int {
        if pkg == nil {
-               return duintptr(s, ot, 0)
+               return objw.Uintptr(s, ot, 0)
        }
 
-       if pkg == localpkg && myimportpath == "" {
+       if pkg == types.LocalPkg && base.Ctxt.Pkgpath == "" {
                // If we don't know the full import path of the package being compiled
                // (i.e. -p was not passed on the compiler command line), emit a reference to
                // type..importpath.""., which the linker will rewrite using the correct import path.
                // Every package that imports this one directly defines the symbol.
                // See also https://groups.google.com/forum/#!topic/golang-dev/myb9s53HxGQ.
-               ns := Ctxt.Lookup(`type..importpath."".`)
-               return dsymptr(s, ot, ns, 0)
+               ns := base.Ctxt.Lookup(`type..importpath."".`)
+               return objw.SymPtr(s, ot, ns, 0)
        }
 
        dimportpath(pkg)
-       return dsymptr(s, ot, pkg.Pathsym, 0)
+       return objw.SymPtr(s, ot, pkg.Pathsym, 0)
 }
 
 // dgopkgpathOff writes an offset relocation in s at offset ot to the pkg path symbol.
 func dgopkgpathOff(s *obj.LSym, ot int, pkg *types.Pkg) int {
        if pkg == nil {
-               return duint32(s, ot, 0)
+               return objw.Uint32(s, ot, 0)
        }
-       if pkg == localpkg && myimportpath == "" {
+       if pkg == types.LocalPkg && base.Ctxt.Pkgpath == "" {
                // If we don't know the full import path of the package being compiled
                // (i.e. -p was not passed on the compiler command line), emit a reference to
                // type..importpath.""., which the linker will rewrite using the correct import path.
                // Every package that imports this one directly defines the symbol.
                // See also https://groups.google.com/forum/#!topic/golang-dev/myb9s53HxGQ.
-               ns := Ctxt.Lookup(`type..importpath."".`)
-               return dsymptrOff(s, ot, ns)
+               ns := base.Ctxt.Lookup(`type..importpath."".`)
+               return objw.SymPtrOff(s, ot, ns)
        }
 
        dimportpath(pkg)
-       return dsymptrOff(s, ot, pkg.Pathsym)
+       return objw.SymPtrOff(s, ot, pkg.Pathsym)
 }
 
 // dnameField dumps a reflect.name for a struct field.
 func dnameField(lsym *obj.LSym, ot int, spkg *types.Pkg, ft *types.Field) int {
        if !types.IsExported(ft.Sym.Name) && ft.Sym.Pkg != spkg {
-               Fatalf("package mismatch for %v", ft.Sym)
+               base.Fatalf("package mismatch for %v", ft.Sym)
        }
        nsym := dname(ft.Sym.Name, ft.Note, nil, types.IsExported(ft.Sym.Name))
-       return dsymptr(lsym, ot, nsym, 0)
+       return objw.SymPtr(lsym, ot, nsym, 0)
 }
 
 // dnameData writes the contents of a reflect.name into s at offset ot.
 func dnameData(s *obj.LSym, ot int, name, tag string, pkg *types.Pkg, exported bool) int {
        if len(name) > 1<<16-1 {
-               Fatalf("name too long: %s", name)
+               base.Fatalf("name too long: %s", name)
        }
        if len(tag) > 1<<16-1 {
-               Fatalf("tag too long: %s", tag)
+               base.Fatalf("tag too long: %s", tag)
        }
 
        // Encode name and tag. See reflect/type.go for details.
@@ -596,7 +501,7 @@ func dnameData(s *obj.LSym, ot int, name, tag string, pkg *types.Pkg, exported b
                copy(tb[2:], tag)
        }
 
-       ot = int(s.WriteBytes(Ctxt, int64(ot), b))
+       ot = int(s.WriteBytes(base.Ctxt, int64(ot), b))
 
        if pkg != nil {
                ot = dgopkgpathOff(s, ot, pkg)
@@ -633,12 +538,12 @@ func dname(name, tag string, pkg *types.Pkg, exported bool) *obj.LSym {
                sname = fmt.Sprintf(`%s"".%d`, sname, dnameCount)
                dnameCount++
        }
-       s := Ctxt.Lookup(sname)
+       s := base.Ctxt.Lookup(sname)
        if len(s.P) > 0 {
                return s
        }
        ot := dnameData(s, 0, name, tag, pkg, exported)
-       ggloblsym(s, int32(ot), obj.DUPOK|obj.RODATA)
+       objw.Global(s, int32(ot), obj.DUPOK|obj.RODATA)
        s.Set(obj.AttrContentAddressable, true)
        return s
 }
@@ -648,16 +553,16 @@ func dname(name, tag string, pkg *types.Pkg, exported bool) *obj.LSym {
 // backing array of the []method field is written (by dextratypeData).
 func dextratype(lsym *obj.LSym, ot int, t *types.Type, dataAdd int) int {
        m := methods(t)
-       if t.Sym == nil && len(m) == 0 {
+       if t.Sym() == nil && len(m) == 0 {
                return ot
        }
-       noff := int(Rnd(int64(ot), int64(Widthptr)))
+       noff := int(types.Rnd(int64(ot), int64(types.PtrSize)))
        if noff != ot {
-               Fatalf("unexpected alignment in dextratype for %v", t)
+               base.Fatalf("unexpected alignment in dextratype for %v", t)
        }
 
        for _, a := range m {
-               dtypesym(a.type_)
+               writeType(a.type_)
        }
 
        ot = dgopkgpathOff(lsym, ot, typePkg(t))
@@ -665,31 +570,31 @@ func dextratype(lsym *obj.LSym, ot int, t *types.Type, dataAdd int) int {
        dataAdd += uncommonSize(t)
        mcount := len(m)
        if mcount != int(uint16(mcount)) {
-               Fatalf("too many methods on %v: %d", t, mcount)
+               base.Fatalf("too many methods on %v: %d", t, mcount)
        }
        xcount := sort.Search(mcount, func(i int) bool { return !types.IsExported(m[i].name.Name) })
        if dataAdd != int(uint32(dataAdd)) {
-               Fatalf("methods are too far away on %v: %d", t, dataAdd)
+               base.Fatalf("methods are too far away on %v: %d", t, dataAdd)
        }
 
-       ot = duint16(lsym, ot, uint16(mcount))
-       ot = duint16(lsym, ot, uint16(xcount))
-       ot = duint32(lsym, ot, uint32(dataAdd))
-       ot = duint32(lsym, ot, 0)
+       ot = objw.Uint16(lsym, ot, uint16(mcount))
+       ot = objw.Uint16(lsym, ot, uint16(xcount))
+       ot = objw.Uint32(lsym, ot, uint32(dataAdd))
+       ot = objw.Uint32(lsym, ot, 0)
        return ot
 }
 
 func typePkg(t *types.Type) *types.Pkg {
-       tsym := t.Sym
+       tsym := t.Sym()
        if tsym == nil {
-               switch t.Etype {
-               case TARRAY, TSLICE, TPTR, TCHAN:
+               switch t.Kind() {
+               case types.TARRAY, types.TSLICE, types.TPTR, types.TCHAN:
                        if t.Elem() != nil {
-                               tsym = t.Elem().Sym
+                               tsym = t.Elem().Sym()
                        }
                }
        }
-       if tsym != nil && t != types.Types[t.Etype] && t != types.Errortype {
+       if tsym != nil && t != types.Types[t.Kind()] && t != types.ErrorType {
                return tsym.Pkg
        }
        return nil
@@ -707,16 +612,16 @@ func dextratypeData(lsym *obj.LSym, ot int, t *types.Type) int {
                }
                nsym := dname(a.name.Name, "", pkg, exported)
 
-               ot = dsymptrOff(lsym, ot, nsym)
-               ot = dmethodptrOff(lsym, ot, dtypesym(a.mtype))
-               ot = dmethodptrOff(lsym, ot, a.isym.Linksym())
-               ot = dmethodptrOff(lsym, ot, a.tsym.Linksym())
+               ot = objw.SymPtrOff(lsym, ot, nsym)
+               ot = dmethodptrOff(lsym, ot, writeType(a.mtype))
+               ot = dmethodptrOff(lsym, ot, a.isym)
+               ot = dmethodptrOff(lsym, ot, a.tsym)
        }
        return ot
 }
 
 func dmethodptrOff(s *obj.LSym, ot int, x *obj.LSym) int {
-       duint32(s, ot, 0)
+       objw.Uint32(s, ot, 0)
        r := obj.Addrel(s)
        r.Off = int32(ot)
        r.Siz = 4
@@ -726,81 +631,32 @@ func dmethodptrOff(s *obj.LSym, ot int, x *obj.LSym) int {
 }
 
 var kinds = []int{
-       TINT:        objabi.KindInt,
-       TUINT:       objabi.KindUint,
-       TINT8:       objabi.KindInt8,
-       TUINT8:      objabi.KindUint8,
-       TINT16:      objabi.KindInt16,
-       TUINT16:     objabi.KindUint16,
-       TINT32:      objabi.KindInt32,
-       TUINT32:     objabi.KindUint32,
-       TINT64:      objabi.KindInt64,
-       TUINT64:     objabi.KindUint64,
-       TUINTPTR:    objabi.KindUintptr,
-       TFLOAT32:    objabi.KindFloat32,
-       TFLOAT64:    objabi.KindFloat64,
-       TBOOL:       objabi.KindBool,
-       TSTRING:     objabi.KindString,
-       TPTR:        objabi.KindPtr,
-       TSTRUCT:     objabi.KindStruct,
-       TINTER:      objabi.KindInterface,
-       TCHAN:       objabi.KindChan,
-       TMAP:        objabi.KindMap,
-       TARRAY:      objabi.KindArray,
-       TSLICE:      objabi.KindSlice,
-       TFUNC:       objabi.KindFunc,
-       TCOMPLEX64:  objabi.KindComplex64,
-       TCOMPLEX128: objabi.KindComplex128,
-       TUNSAFEPTR:  objabi.KindUnsafePointer,
-}
-
-// typeptrdata returns the length in bytes of the prefix of t
-// containing pointer data. Anything after this offset is scalar data.
-func typeptrdata(t *types.Type) int64 {
-       if !t.HasPointers() {
-               return 0
-       }
-
-       switch t.Etype {
-       case TPTR,
-               TUNSAFEPTR,
-               TFUNC,
-               TCHAN,
-               TMAP:
-               return int64(Widthptr)
-
-       case TSTRING:
-               // struct { byte *str; intgo len; }
-               return int64(Widthptr)
-
-       case TINTER:
-               // struct { Itab *tab;  void *data; } or
-               // struct { Type *type; void *data; }
-               // Note: see comment in plive.go:onebitwalktype1.
-               return 2 * int64(Widthptr)
-
-       case TSLICE:
-               // struct { byte *array; uintgo len; uintgo cap; }
-               return int64(Widthptr)
-
-       case TARRAY:
-               // haspointers already eliminated t.NumElem() == 0.
-               return (t.NumElem()-1)*t.Elem().Width + typeptrdata(t.Elem())
-
-       case TSTRUCT:
-               // Find the last field that has pointers.
-               var lastPtrField *types.Field
-               for _, t1 := range t.Fields().Slice() {
-                       if t1.Type.HasPointers() {
-                               lastPtrField = t1
-                       }
-               }
-               return lastPtrField.Offset + typeptrdata(lastPtrField.Type)
-
-       default:
-               Fatalf("typeptrdata: unexpected type, %v", t)
-               return 0
-       }
+       types.TINT:        objabi.KindInt,
+       types.TUINT:       objabi.KindUint,
+       types.TINT8:       objabi.KindInt8,
+       types.TUINT8:      objabi.KindUint8,
+       types.TINT16:      objabi.KindInt16,
+       types.TUINT16:     objabi.KindUint16,
+       types.TINT32:      objabi.KindInt32,
+       types.TUINT32:     objabi.KindUint32,
+       types.TINT64:      objabi.KindInt64,
+       types.TUINT64:     objabi.KindUint64,
+       types.TUINTPTR:    objabi.KindUintptr,
+       types.TFLOAT32:    objabi.KindFloat32,
+       types.TFLOAT64:    objabi.KindFloat64,
+       types.TBOOL:       objabi.KindBool,
+       types.TSTRING:     objabi.KindString,
+       types.TPTR:        objabi.KindPtr,
+       types.TSTRUCT:     objabi.KindStruct,
+       types.TINTER:      objabi.KindInterface,
+       types.TCHAN:       objabi.KindChan,
+       types.TMAP:        objabi.KindMap,
+       types.TARRAY:      objabi.KindArray,
+       types.TSLICE:      objabi.KindSlice,
+       types.TFUNC:       objabi.KindFunc,
+       types.TCOMPLEX64:  objabi.KindComplex64,
+       types.TCOMPLEX128: objabi.KindComplex128,
+       types.TUNSAFEPTR:  objabi.KindUnsafePointer,
 }
 
 // tflag is documented in reflect/type.go.
@@ -824,17 +680,17 @@ var (
 
 // dcommontype dumps the contents of a reflect.rtype (runtime._type).
 func dcommontype(lsym *obj.LSym, t *types.Type) int {
-       dowidth(t)
+       types.CalcSize(t)
        eqfunc := geneq(t)
 
        sptrWeak := true
        var sptr *obj.LSym
        if !t.IsPtr() || t.IsPtrElem() {
                tptr := types.NewPtr(t)
-               if t.Sym != nil || methods(tptr) != nil {
+               if t.Sym() != nil || methods(tptr) != nil {
                        sptrWeak = false
                }
-               sptr = dtypesym(tptr)
+               sptr = writeType(tptr)
        }
 
        gcsym, useGCProg, ptrdata := dgcsym(t)
@@ -855,18 +711,18 @@ func dcommontype(lsym *obj.LSym, t *types.Type) int {
        //              ptrToThis     typeOff
        //      }
        ot := 0
-       ot = duintptr(lsym, ot, uint64(t.Width))
-       ot = duintptr(lsym, ot, uint64(ptrdata))
-       ot = duint32(lsym, ot, typehash(t))
+       ot = objw.Uintptr(lsym, ot, uint64(t.Width))
+       ot = objw.Uintptr(lsym, ot, uint64(ptrdata))
+       ot = objw.Uint32(lsym, ot, types.TypeHash(t))
 
        var tflag uint8
        if uncommonSize(t) != 0 {
                tflag |= tflagUncommon
        }
-       if t.Sym != nil && t.Sym.Name != "" {
+       if t.Sym() != nil && t.Sym().Name != "" {
                tflag |= tflagNamed
        }
-       if IsRegularMemory(t) {
+       if isRegularMemory(t) {
                tflag |= tflagRegularMemory
        }
 
@@ -880,16 +736,16 @@ func dcommontype(lsym *obj.LSym, t *types.Type) int {
        if !strings.HasPrefix(p, "*") {
                p = "*" + p
                tflag |= tflagExtraStar
-               if t.Sym != nil {
-                       exported = types.IsExported(t.Sym.Name)
+               if t.Sym() != nil {
+                       exported = types.IsExported(t.Sym().Name)
                }
        } else {
-               if t.Elem() != nil && t.Elem().Sym != nil {
-                       exported = types.IsExported(t.Elem().Sym.Name)
+               if t.Elem() != nil && t.Elem().Sym() != nil {
+                       exported = types.IsExported(t.Elem().Sym().Name)
                }
        }
 
-       ot = duint8(lsym, ot, tflag)
+       ot = objw.Uint8(lsym, ot, tflag)
 
        // runtime (and common sense) expects alignment to be a power of two.
        i := int(t.Align)
@@ -898,88 +754,54 @@ func dcommontype(lsym *obj.LSym, t *types.Type) int {
                i = 1
        }
        if i&(i-1) != 0 {
-               Fatalf("invalid alignment %d for %v", t.Align, t)
+               base.Fatalf("invalid alignment %d for %v", t.Align, t)
        }
-       ot = duint8(lsym, ot, t.Align) // align
-       ot = duint8(lsym, ot, t.Align) // fieldAlign
+       ot = objw.Uint8(lsym, ot, t.Align) // align
+       ot = objw.Uint8(lsym, ot, t.Align) // fieldAlign
 
-       i = kinds[t.Etype]
-       if isdirectiface(t) {
+       i = kinds[t.Kind()]
+       if types.IsDirectIface(t) {
                i |= objabi.KindDirectIface
        }
        if useGCProg {
                i |= objabi.KindGCProg
        }
-       ot = duint8(lsym, ot, uint8(i)) // kind
+       ot = objw.Uint8(lsym, ot, uint8(i)) // kind
        if eqfunc != nil {
-               ot = dsymptr(lsym, ot, eqfunc, 0) // equality function
+               ot = objw.SymPtr(lsym, ot, eqfunc, 0) // equality function
        } else {
-               ot = duintptr(lsym, ot, 0) // type we can't do == with
+               ot = objw.Uintptr(lsym, ot, 0) // type we can't do == with
        }
-       ot = dsymptr(lsym, ot, gcsym, 0) // gcdata
+       ot = objw.SymPtr(lsym, ot, gcsym, 0) // gcdata
 
        nsym := dname(p, "", nil, exported)
-       ot = dsymptrOff(lsym, ot, nsym) // str
+       ot = objw.SymPtrOff(lsym, ot, nsym) // str
        // ptrToThis
        if sptr == nil {
-               ot = duint32(lsym, ot, 0)
+               ot = objw.Uint32(lsym, ot, 0)
        } else if sptrWeak {
-               ot = dsymptrWeakOff(lsym, ot, sptr)
+               ot = objw.SymPtrWeakOff(lsym, ot, sptr)
        } else {
-               ot = dsymptrOff(lsym, ot, sptr)
+               ot = objw.SymPtrOff(lsym, ot, sptr)
        }
 
        return ot
 }
 
-// typeHasNoAlg reports whether t does not have any associated hash/eq
-// algorithms because t, or some component of t, is marked Noalg.
-func typeHasNoAlg(t *types.Type) bool {
-       a, bad := algtype1(t)
-       return a == ANOEQ && bad.Noalg()
-}
-
-func typesymname(t *types.Type) string {
-       name := t.ShortString()
-       // Use a separate symbol name for Noalg types for #17752.
-       if typeHasNoAlg(t) {
-               name = "noalg." + name
-       }
-       return name
-}
-
-// Fake package for runtime type info (headers)
-// Don't access directly, use typeLookup below.
-var (
-       typepkgmu sync.Mutex // protects typepkg lookups
-       typepkg   = types.NewPkg("type", "type")
-)
-
-func typeLookup(name string) *types.Sym {
-       typepkgmu.Lock()
-       s := typepkg.Lookup(name)
-       typepkgmu.Unlock()
-       return s
-}
-
-func typesym(t *types.Type) *types.Sym {
-       return typeLookup(typesymname(t))
-}
-
-// tracksym returns the symbol for tracking use of field/method f, assumed
+// TrackSym returns the symbol for tracking use of field/method f, assumed
 // to be a member of struct/interface type t.
-func tracksym(t *types.Type, f *types.Field) *types.Sym {
-       return trackpkg.Lookup(t.ShortString() + "." + f.Sym.Name)
+func TrackSym(t *types.Type, f *types.Field) *obj.LSym {
+       return base.PkgLinksym("go.track", t.ShortString()+"."+f.Sym.Name, obj.ABI0)
 }
 
-func typesymprefix(prefix string, t *types.Type) *types.Sym {
+func TypeSymPrefix(prefix string, t *types.Type) *types.Sym {
        p := prefix + "." + t.ShortString()
-       s := typeLookup(p)
+       s := types.TypeSymLookup(p)
 
        // This function is for looking up type-related generated functions
        // (e.g. eq and hash). Make sure they are indeed generated.
        signatmu.Lock()
-       addsignat(t)
+       NeedRuntimeType(t)
        signatmu.Unlock()
 
        //print("algsym: %s -> %+S\n", p, s);
@@ -987,116 +809,68 @@ func typesymprefix(prefix string, t *types.Type) *types.Sym {
        return s
 }
 
-func typenamesym(t *types.Type) *types.Sym {
+func TypeSym(t *types.Type) *types.Sym {
        if t == nil || (t.IsPtr() && t.Elem() == nil) || t.IsUntyped() {
-               Fatalf("typenamesym %v", t)
+               base.Fatalf("TypeSym %v", t)
+       }
+       if t.Kind() == types.TFUNC && t.Recv() != nil {
+               base.Fatalf("misuse of method type: %v", t)
        }
-       s := typesym(t)
+       s := types.TypeSym(t)
        signatmu.Lock()
-       addsignat(t)
+       NeedRuntimeType(t)
        signatmu.Unlock()
        return s
 }
 
-func typename(t *types.Type) *Node {
-       s := typenamesym(t)
-       if s.Def == nil {
-               n := newnamel(src.NoXPos, s)
-               n.Type = types.Types[TUINT8]
-               n.SetClass(PEXTERN)
-               n.SetTypecheck(1)
-               s.Def = asTypesNode(n)
-       }
-
-       n := nod(OADDR, asNode(s.Def), nil)
-       n.Type = types.NewPtr(asNode(s.Def).Type)
-       n.SetTypecheck(1)
-       return n
+func TypeLinksymPrefix(prefix string, t *types.Type) *obj.LSym {
+       return TypeSymPrefix(prefix, t).Linksym()
 }
 
-func itabname(t, itype *types.Type) *Node {
-       if t == nil || (t.IsPtr() && t.Elem() == nil) || t.IsUntyped() || !itype.IsInterface() || itype.IsEmptyInterface() {
-               Fatalf("itabname(%v, %v)", t, itype)
-       }
-       s := itabpkg.Lookup(t.ShortString() + "," + itype.ShortString())
-       if s.Def == nil {
-               n := newname(s)
-               n.Type = types.Types[TUINT8]
-               n.SetClass(PEXTERN)
-               n.SetTypecheck(1)
-               s.Def = asTypesNode(n)
-               itabs = append(itabs, itabEntry{t: t, itype: itype, lsym: s.Linksym()})
-       }
-
-       n := nod(OADDR, asNode(s.Def), nil)
-       n.Type = types.NewPtr(asNode(s.Def).Type)
-       n.SetTypecheck(1)
-       return n
+func TypeLinksymLookup(name string) *obj.LSym {
+       return types.TypeSymLookup(name).Linksym()
 }
 
-// isreflexive reports whether t has a reflexive equality operator.
-// That is, if x==x for all x of type t.
-func isreflexive(t *types.Type) bool {
-       switch t.Etype {
-       case TBOOL,
-               TINT,
-               TUINT,
-               TINT8,
-               TUINT8,
-               TINT16,
-               TUINT16,
-               TINT32,
-               TUINT32,
-               TINT64,
-               TUINT64,
-               TUINTPTR,
-               TPTR,
-               TUNSAFEPTR,
-               TSTRING,
-               TCHAN:
-               return true
-
-       case TFLOAT32,
-               TFLOAT64,
-               TCOMPLEX64,
-               TCOMPLEX128,
-               TINTER:
-               return false
-
-       case TARRAY:
-               return isreflexive(t.Elem())
+func TypeLinksym(t *types.Type) *obj.LSym {
+       return TypeSym(t).Linksym()
+}
 
-       case TSTRUCT:
-               for _, t1 := range t.Fields().Slice() {
-                       if !isreflexive(t1.Type) {
-                               return false
-                       }
-               }
-               return true
+func TypePtr(t *types.Type) *ir.AddrExpr {
+       n := ir.NewLinksymExpr(base.Pos, TypeLinksym(t), types.Types[types.TUINT8])
+       return typecheck.Expr(typecheck.NodAddr(n)).(*ir.AddrExpr)
+}
 
-       default:
-               Fatalf("bad type for map key: %v", t)
-               return false
+func ITabAddr(t, itype *types.Type) *ir.AddrExpr {
+       if t == nil || (t.IsPtr() && t.Elem() == nil) || t.IsUntyped() || !itype.IsInterface() || itype.IsEmptyInterface() {
+               base.Fatalf("ITabAddr(%v, %v)", t, itype)
+       }
+       s, existed := ir.Pkgs.Itab.LookupOK(t.ShortString() + "," + itype.ShortString())
+       if !existed {
+               itabs = append(itabs, itabEntry{t: t, itype: itype, lsym: s.Linksym()})
        }
+
+       lsym := s.Linksym()
+       n := ir.NewLinksymExpr(base.Pos, lsym, types.Types[types.TUINT8])
+       return typecheck.Expr(typecheck.NodAddr(n)).(*ir.AddrExpr)
 }
 
 // needkeyupdate reports whether map updates with t as a key
 // need the key to be updated.
 func needkeyupdate(t *types.Type) bool {
-       switch t.Etype {
-       case TBOOL, TINT, TUINT, TINT8, TUINT8, TINT16, TUINT16, TINT32, TUINT32,
-               TINT64, TUINT64, TUINTPTR, TPTR, TUNSAFEPTR, TCHAN:
+       switch t.Kind() {
+       case types.TBOOL, types.TINT, types.TUINT, types.TINT8, types.TUINT8, types.TINT16, types.TUINT16, types.TINT32, types.TUINT32,
+               types.TINT64, types.TUINT64, types.TUINTPTR, types.TPTR, types.TUNSAFEPTR, types.TCHAN:
                return false
 
-       case TFLOAT32, TFLOAT64, TCOMPLEX64, TCOMPLEX128, // floats and complex can be +0/-0
-               TINTER,
-               TSTRING: // strings might have smaller backing stores
+       case types.TFLOAT32, types.TFLOAT64, types.TCOMPLEX64, types.TCOMPLEX128, // floats and complex can be +0/-0
+               types.TINTER,
+               types.TSTRING: // strings might have smaller backing stores
                return true
 
-       case TARRAY:
+       case types.TARRAY:
                return needkeyupdate(t.Elem())
 
-       case TSTRUCT:
+       case types.TSTRUCT:
                for _, t1 := range t.Fields().Slice() {
                        if needkeyupdate(t1.Type) {
                                return true
@@ -1105,21 +879,21 @@ func needkeyupdate(t *types.Type) bool {
                return false
 
        default:
-               Fatalf("bad type for map key: %v", t)
+               base.Fatalf("bad type for map key: %v", t)
                return true
        }
 }
 
 // hashMightPanic reports whether the hash of a map key of type t might panic.
 func hashMightPanic(t *types.Type) bool {
-       switch t.Etype {
-       case TINTER:
+       switch t.Kind() {
+       case types.TINTER:
                return true
 
-       case TARRAY:
+       case types.TARRAY:
                return hashMightPanic(t.Elem())
 
-       case TSTRUCT:
+       case types.TSTRUCT:
                for _, t1 := range t.Fields().Slice() {
                        if hashMightPanic(t1.Type) {
                                return true
@@ -1136,19 +910,19 @@ func hashMightPanic(t *types.Type) bool {
 // They've been separate internally to make error messages
 // better, but we have to merge them in the reflect tables.
 func formalType(t *types.Type) *types.Type {
-       if t == types.Bytetype || t == types.Runetype {
-               return types.Types[t.Etype]
+       if t == types.ByteType || t == types.RuneType {
+               return types.Types[t.Kind()]
        }
        return t
 }
 
-func dtypesym(t *types.Type) *obj.LSym {
+func writeType(t *types.Type) *obj.LSym {
        t = formalType(t)
        if t.IsUntyped() {
-               Fatalf("dtypesym %v", t)
+               base.Fatalf("writeType %v", t)
        }
 
-       s := typesym(t)
+       s := types.TypeSym(t)
        lsym := s.Linksym()
        if s.Siggen() {
                return lsym
@@ -1160,77 +934,73 @@ func dtypesym(t *types.Type) *obj.LSym {
        // emit the type structures for int, float, etc.
        tbase := t
 
-       if t.IsPtr() && t.Sym == nil && t.Elem().Sym != nil {
+       if t.IsPtr() && t.Sym() == nil && t.Elem().Sym() != nil {
                tbase = t.Elem()
        }
        dupok := 0
-       if tbase.Sym == nil {
+       if tbase.Sym() == nil {
                dupok = obj.DUPOK
        }
 
-       if myimportpath != "runtime" || (tbase != types.Types[tbase.Etype] && tbase != types.Bytetype && tbase != types.Runetype && tbase != types.Errortype) { // int, float, etc
+       if base.Ctxt.Pkgpath != "runtime" || (tbase != types.Types[tbase.Kind()] && tbase != types.ByteType && tbase != types.RuneType && tbase != types.ErrorType) { // int, float, etc
                // named types from other files are defined only by those files
-               if tbase.Sym != nil && tbase.Sym.Pkg != localpkg {
-                       if i, ok := typeSymIdx[tbase]; ok {
-                               lsym.Pkg = tbase.Sym.Pkg.Prefix
-                               if t != tbase {
-                                       lsym.SymIdx = int32(i[1])
-                               } else {
-                                       lsym.SymIdx = int32(i[0])
-                               }
+               if tbase.Sym() != nil && tbase.Sym().Pkg != types.LocalPkg {
+                       if i := typecheck.BaseTypeIndex(t); i >= 0 {
+                               lsym.Pkg = tbase.Sym().Pkg.Prefix
+                               lsym.SymIdx = int32(i)
                                lsym.Set(obj.AttrIndexed, true)
                        }
                        return lsym
                }
                // TODO(mdempsky): Investigate whether this can happen.
-               if tbase.Etype == TFORW {
+               if tbase.Kind() == types.TFORW {
                        return lsym
                }
        }
 
        ot := 0
-       switch t.Etype {
+       switch t.Kind() {
        default:
                ot = dcommontype(lsym, t)
                ot = dextratype(lsym, ot, t, 0)
 
-       case TARRAY:
+       case types.TARRAY:
                // ../../../../runtime/type.go:/arrayType
-               s1 := dtypesym(t.Elem())
+               s1 := writeType(t.Elem())
                t2 := types.NewSlice(t.Elem())
-               s2 := dtypesym(t2)
+               s2 := writeType(t2)
                ot = dcommontype(lsym, t)
-               ot = dsymptr(lsym, ot, s1, 0)
-               ot = dsymptr(lsym, ot, s2, 0)
-               ot = duintptr(lsym, ot, uint64(t.NumElem()))
+               ot = objw.SymPtr(lsym, ot, s1, 0)
+               ot = objw.SymPtr(lsym, ot, s2, 0)
+               ot = objw.Uintptr(lsym, ot, uint64(t.NumElem()))
                ot = dextratype(lsym, ot, t, 0)
 
-       case TSLICE:
+       case types.TSLICE:
                // ../../../../runtime/type.go:/sliceType
-               s1 := dtypesym(t.Elem())
+               s1 := writeType(t.Elem())
                ot = dcommontype(lsym, t)
-               ot = dsymptr(lsym, ot, s1, 0)
+               ot = objw.SymPtr(lsym, ot, s1, 0)
                ot = dextratype(lsym, ot, t, 0)
 
-       case TCHAN:
+       case types.TCHAN:
                // ../../../../runtime/type.go:/chanType
-               s1 := dtypesym(t.Elem())
+               s1 := writeType(t.Elem())
                ot = dcommontype(lsym, t)
-               ot = dsymptr(lsym, ot, s1, 0)
-               ot = duintptr(lsym, ot, uint64(t.ChanDir()))
+               ot = objw.SymPtr(lsym, ot, s1, 0)
+               ot = objw.Uintptr(lsym, ot, uint64(t.ChanDir()))
                ot = dextratype(lsym, ot, t, 0)
 
-       case TFUNC:
+       case types.TFUNC:
                for _, t1 := range t.Recvs().Fields().Slice() {
-                       dtypesym(t1.Type)
+                       writeType(t1.Type)
                }
                isddd := false
                for _, t1 := range t.Params().Fields().Slice() {
                        isddd = t1.IsDDD()
-                       dtypesym(t1.Type)
+                       writeType(t1.Type)
                }
                for _, t1 := range t.Results().Fields().Slice() {
-                       dtypesym(t1.Type)
+                       writeType(t1.Type)
                }
 
                ot = dcommontype(lsym, t)
@@ -1239,45 +1009,45 @@ func dtypesym(t *types.Type) *obj.LSym {
                if isddd {
                        outCount |= 1 << 15
                }
-               ot = duint16(lsym, ot, uint16(inCount))
-               ot = duint16(lsym, ot, uint16(outCount))
-               if Widthptr == 8 {
+               ot = objw.Uint16(lsym, ot, uint16(inCount))
+               ot = objw.Uint16(lsym, ot, uint16(outCount))
+               if types.PtrSize == 8 {
                        ot += 4 // align for *rtype
                }
 
-               dataAdd := (inCount + t.NumResults()) * Widthptr
+               dataAdd := (inCount + t.NumResults()) * types.PtrSize
                ot = dextratype(lsym, ot, t, dataAdd)
 
                // Array of rtype pointers follows funcType.
                for _, t1 := range t.Recvs().Fields().Slice() {
-                       ot = dsymptr(lsym, ot, dtypesym(t1.Type), 0)
+                       ot = objw.SymPtr(lsym, ot, writeType(t1.Type), 0)
                }
                for _, t1 := range t.Params().Fields().Slice() {
-                       ot = dsymptr(lsym, ot, dtypesym(t1.Type), 0)
+                       ot = objw.SymPtr(lsym, ot, writeType(t1.Type), 0)
                }
                for _, t1 := range t.Results().Fields().Slice() {
-                       ot = dsymptr(lsym, ot, dtypesym(t1.Type), 0)
+                       ot = objw.SymPtr(lsym, ot, writeType(t1.Type), 0)
                }
 
-       case TINTER:
+       case types.TINTER:
                m := imethods(t)
                n := len(m)
                for _, a := range m {
-                       dtypesym(a.type_)
+                       writeType(a.type_)
                }
 
                // ../../../../runtime/type.go:/interfaceType
                ot = dcommontype(lsym, t)
 
                var tpkg *types.Pkg
-               if t.Sym != nil && t != types.Types[t.Etype] && t != types.Errortype {
-                       tpkg = t.Sym.Pkg
+               if t.Sym() != nil && t != types.Types[t.Kind()] && t != types.ErrorType {
+                       tpkg = t.Sym().Pkg
                }
                ot = dgopkgpath(lsym, ot, tpkg)
 
-               ot = dsymptr(lsym, ot, lsym, ot+3*Widthptr+uncommonSize(t))
-               ot = duintptr(lsym, ot, uint64(n))
-               ot = duintptr(lsym, ot, uint64(n))
+               ot = objw.SymPtr(lsym, ot, lsym, ot+3*types.PtrSize+uncommonSize(t))
+               ot = objw.Uintptr(lsym, ot, uint64(n))
+               ot = objw.Uintptr(lsym, ot, uint64(n))
                dataAdd := imethodSize() * n
                ot = dextratype(lsym, ot, t, dataAdd)
 
@@ -1290,40 +1060,40 @@ func dtypesym(t *types.Type) *obj.LSym {
                        }
                        nsym := dname(a.name.Name, "", pkg, exported)
 
-                       ot = dsymptrOff(lsym, ot, nsym)
-                       ot = dsymptrOff(lsym, ot, dtypesym(a.type_))
+                       ot = objw.SymPtrOff(lsym, ot, nsym)
+                       ot = objw.SymPtrOff(lsym, ot, writeType(a.type_))
                }
 
        // ../../../../runtime/type.go:/mapType
-       case TMAP:
-               s1 := dtypesym(t.Key())
-               s2 := dtypesym(t.Elem())
-               s3 := dtypesym(bmap(t))
+       case types.TMAP:
+               s1 := writeType(t.Key())
+               s2 := writeType(t.Elem())
+               s3 := writeType(MapBucketType(t))
                hasher := genhash(t.Key())
 
                ot = dcommontype(lsym, t)
-               ot = dsymptr(lsym, ot, s1, 0)
-               ot = dsymptr(lsym, ot, s2, 0)
-               ot = dsymptr(lsym, ot, s3, 0)
-               ot = dsymptr(lsym, ot, hasher, 0)
+               ot = objw.SymPtr(lsym, ot, s1, 0)
+               ot = objw.SymPtr(lsym, ot, s2, 0)
+               ot = objw.SymPtr(lsym, ot, s3, 0)
+               ot = objw.SymPtr(lsym, ot, hasher, 0)
                var flags uint32
                // Note: flags must match maptype accessors in ../../../../runtime/type.go
                // and maptype builder in ../../../../reflect/type.go:MapOf.
                if t.Key().Width > MAXKEYSIZE {
-                       ot = duint8(lsym, ot, uint8(Widthptr))
+                       ot = objw.Uint8(lsym, ot, uint8(types.PtrSize))
                        flags |= 1 // indirect key
                } else {
-                       ot = duint8(lsym, ot, uint8(t.Key().Width))
+                       ot = objw.Uint8(lsym, ot, uint8(t.Key().Width))
                }
 
                if t.Elem().Width > MAXELEMSIZE {
-                       ot = duint8(lsym, ot, uint8(Widthptr))
+                       ot = objw.Uint8(lsym, ot, uint8(types.PtrSize))
                        flags |= 2 // indirect value
                } else {
-                       ot = duint8(lsym, ot, uint8(t.Elem().Width))
+                       ot = objw.Uint8(lsym, ot, uint8(t.Elem().Width))
                }
-               ot = duint16(lsym, ot, uint16(bmap(t).Width))
-               if isreflexive(t.Key()) {
+               ot = objw.Uint16(lsym, ot, uint16(MapBucketType(t).Width))
+               if types.IsReflexive(t.Key()) {
                        flags |= 4 // reflexive key
                }
                if needkeyupdate(t.Key()) {
@@ -1332,11 +1102,11 @@ func dtypesym(t *types.Type) *obj.LSym {
                if hashMightPanic(t.Key()) {
                        flags |= 16 // hash might panic
                }
-               ot = duint32(lsym, ot, flags)
+               ot = objw.Uint32(lsym, ot, flags)
                ot = dextratype(lsym, ot, t, 0)
 
-       case TPTR:
-               if t.Elem().Etype == TANY {
+       case types.TPTR:
+               if t.Elem().Kind() == types.TANY {
                        // ../../../../runtime/type.go:/UnsafePointerType
                        ot = dcommontype(lsym, t)
                        ot = dextratype(lsym, ot, t, 0)
@@ -1345,15 +1115,15 @@ func dtypesym(t *types.Type) *obj.LSym {
                }
 
                // ../../../../runtime/type.go:/ptrType
-               s1 := dtypesym(t.Elem())
+               s1 := writeType(t.Elem())
 
                ot = dcommontype(lsym, t)
-               ot = dsymptr(lsym, ot, s1, 0)
+               ot = objw.SymPtr(lsym, ot, s1, 0)
                ot = dextratype(lsym, ot, t, 0)
 
        // ../../../../runtime/type.go:/structType
        // for security, only the exported fields.
-       case TSTRUCT:
+       case types.TSTRUCT:
                fields := t.Fields().Slice()
 
                // omitFieldForAwfulBoringCryptoKludge reports whether
@@ -1369,8 +1139,8 @@ func dtypesym(t *types.Type) *obj.LSym {
                                return false
                        }
                        path := t.Sym.Pkg.Path
-                       if t.Sym.Pkg == localpkg {
-                               path = myimportpath
+                       if t.Sym.Pkg == types.LocalPkg {
+                               path = base.Ctxt.Pkgpath
                        }
                        return strings.HasPrefix(path, "crypto/")
                }
@@ -1383,7 +1153,7 @@ func dtypesym(t *types.Type) *obj.LSym {
                fields = newFields
 
                for _, t1 := range fields {
-                       dtypesym(t1.Type)
+                       writeType(t1.Type)
                }
 
                // All non-exported struct field names within a struct
@@ -1401,9 +1171,9 @@ func dtypesym(t *types.Type) *obj.LSym {
 
                ot = dcommontype(lsym, t)
                ot = dgopkgpath(lsym, ot, spkg)
-               ot = dsymptr(lsym, ot, lsym, ot+3*Widthptr+uncommonSize(t))
-               ot = duintptr(lsym, ot, uint64(len(fields)))
-               ot = duintptr(lsym, ot, uint64(len(fields)))
+               ot = objw.SymPtr(lsym, ot, lsym, ot+3*types.PtrSize+uncommonSize(t))
+               ot = objw.Uintptr(lsym, ot, uint64(len(fields)))
+               ot = objw.Uintptr(lsym, ot, uint64(len(fields)))
 
                dataAdd := len(fields) * structfieldSize()
                ot = dextratype(lsym, ot, t, dataAdd)
@@ -1411,40 +1181,40 @@ func dtypesym(t *types.Type) *obj.LSym {
                for _, f := range fields {
                        // ../../../../runtime/type.go:/structField
                        ot = dnameField(lsym, ot, spkg, f)
-                       ot = dsymptr(lsym, ot, dtypesym(f.Type), 0)
+                       ot = objw.SymPtr(lsym, ot, writeType(f.Type), 0)
                        offsetAnon := uint64(f.Offset) << 1
                        if offsetAnon>>1 != uint64(f.Offset) {
-                               Fatalf("%v: bad field offset for %s", t, f.Sym.Name)
+                               base.Fatalf("%v: bad field offset for %s", t, f.Sym.Name)
                        }
                        if f.Embedded != 0 {
                                offsetAnon |= 1
                        }
-                       ot = duintptr(lsym, ot, offsetAnon)
+                       ot = objw.Uintptr(lsym, ot, offsetAnon)
                }
        }
 
        ot = dextratypeData(lsym, ot, t)
-       ggloblsym(lsym, int32(ot), int16(dupok|obj.RODATA))
+       objw.Global(lsym, int32(ot), int16(dupok|obj.RODATA))
 
        // The linker will leave a table of all the typelinks for
        // types in the binary, so the runtime can find them.
        //
        // When buildmode=shared, all types are in typelinks so the
        // runtime can deduplicate type pointers.
-       keep := Ctxt.Flag_dynlink
-       if !keep && t.Sym == nil {
+       keep := base.Ctxt.Flag_dynlink
+       if !keep && t.Sym() == nil {
                // For an unnamed type, we only need the link if the type can
                // be created at run time by reflect.PtrTo and similar
                // functions. If the type exists in the program, those
                // functions must return the existing type structure rather
                // than creating a new one.
-               switch t.Etype {
-               case TPTR, TARRAY, TCHAN, TFUNC, TMAP, TSLICE, TSTRUCT:
+               switch t.Kind() {
+               case types.TPTR, types.TARRAY, types.TCHAN, types.TFUNC, types.TMAP, types.TSLICE, types.TSTRUCT:
                        keep = true
                }
        }
        // Do not put Noalg types in typelinks.  See issue #22605.
-       if typeHasNoAlg(t) {
+       if types.TypeHasNoAlg(t) {
                keep = false
        }
        lsym.Set(obj.AttrMakeTypelink, keep)
@@ -1452,9 +1222,9 @@ func dtypesym(t *types.Type) *obj.LSym {
        return lsym
 }
 
-// ifaceMethodOffset returns the offset of the i-th method in the interface
+// InterfaceMethodOffset returns the offset of the i-th method in the interface
 // type descriptor, ityp.
-func ifaceMethodOffset(ityp *types.Type, i int64) int64 {
+func InterfaceMethodOffset(ityp *types.Type, i int64) int64 {
        // interface type descriptor layout is struct {
        //   _type        // commonSize
        //   pkgpath      // 1 word
@@ -1463,12 +1233,12 @@ func ifaceMethodOffset(ityp *types.Type, i int64) int64 {
        //   [...]imethod
        // }
        // The size of imethod is 8.
-       return int64(commonSize()+4*Widthptr+uncommonSize(ityp)) + i*8
+       return int64(commonSize()+4*types.PtrSize+uncommonSize(ityp)) + i*8
 }
 
 // for each itabEntry, gather the methods on
 // the concrete type that implement the interface
-func peekitabs() {
+func CompileITabs() {
        for i := range itabs {
                tab := &itabs[i]
                methods := genfun(tab.t, tab.itype)
@@ -1499,7 +1269,7 @@ func genfun(t, it *types.Type) []*obj.LSym {
        // so we can find the intersect in a single pass
        for _, m := range methods {
                if m.name == sigs[0].name {
-                       out = append(out, m.isym.Linksym())
+                       out = append(out, m.isym)
                        sigs = sigs[1:]
                        if len(sigs) == 0 {
                                break
@@ -1508,17 +1278,17 @@ func genfun(t, it *types.Type) []*obj.LSym {
        }
 
        if len(sigs) != 0 {
-               Fatalf("incomplete itab")
+               base.Fatalf("incomplete itab")
        }
 
        return out
 }
 
-// itabsym uses the information gathered in
-// peekitabs to de-virtualize interface methods.
+// ITabSym uses the information gathered in
+// CompileITabs to de-virtualize interface methods.
 // Since this is called by the SSA backend, it shouldn't
 // generate additional Nodes, Syms, etc.
-func itabsym(it *obj.LSym, offset int64) *obj.LSym {
+func ITabSym(it *obj.LSym, offset int64) *obj.LSym {
        var syms []*obj.LSym
        if it == nil {
                return nil
@@ -1536,54 +1306,45 @@ func itabsym(it *obj.LSym, offset int64) *obj.LSym {
        }
 
        // keep this arithmetic in sync with *itab layout
-       methodnum := int((offset - 2*int64(Widthptr) - 8) / int64(Widthptr))
+       methodnum := int((offset - 2*int64(types.PtrSize) - 8) / int64(types.PtrSize))
        if methodnum >= len(syms) {
                return nil
        }
        return syms[methodnum]
 }
 
-// addsignat ensures that a runtime type descriptor is emitted for t.
-func addsignat(t *types.Type) {
+// NeedRuntimeType ensures that a runtime type descriptor is emitted for t.
+func NeedRuntimeType(t *types.Type) {
        if _, ok := signatset[t]; !ok {
                signatset[t] = struct{}{}
                signatslice = append(signatslice, t)
        }
 }
 
-func addsignats(dcls []*Node) {
-       // copy types from dcl list to signatset
-       for _, n := range dcls {
-               if n.Op == OTYPE {
-                       addsignat(n.Type)
-               }
-       }
-}
-
-func dumpsignats() {
-       // Process signatset. Use a loop, as dtypesym adds
+func WriteRuntimeTypes() {
+       // Process signatset. Use a loop, as writeType adds
        // entries to signatset while it is being processed.
        signats := make([]typeAndStr, len(signatslice))
        for len(signatslice) > 0 {
                signats = signats[:0]
                // Transfer entries to a slice and sort, for reproducible builds.
                for _, t := range signatslice {
-                       signats = append(signats, typeAndStr{t: t, short: typesymname(t), regular: t.String()})
+                       signats = append(signats, typeAndStr{t: t, short: types.TypeSymName(t), regular: t.String()})
                        delete(signatset, t)
                }
                signatslice = signatslice[:0]
                sort.Sort(typesByString(signats))
                for _, ts := range signats {
                        t := ts.t
-                       dtypesym(t)
-                       if t.Sym != nil {
-                               dtypesym(types.NewPtr(t))
+                       writeType(t)
+                       if t.Sym() != nil {
+                               writeType(types.NewPtr(t))
                        }
                }
        }
 }
 
-func dumptabs() {
+func WriteTabs() {
        // process itabs
        for _, i := range itabs {
                // dump empty itab symbol into i.sym
@@ -1594,22 +1355,22 @@ func dumptabs() {
                //   _      [4]byte
                //   fun    [1]uintptr // variable sized
                // }
-               o := dsymptr(i.lsym, 0, dtypesym(i.itype), 0)
-               o = dsymptr(i.lsym, o, dtypesym(i.t), 0)
-               o = duint32(i.lsym, o, typehash(i.t)) // copy of type hash
-               o += 4                                // skip unused field
+               o := objw.SymPtr(i.lsym, 0, writeType(i.itype), 0)
+               o = objw.SymPtr(i.lsym, o, writeType(i.t), 0)
+               o = objw.Uint32(i.lsym, o, types.TypeHash(i.t)) // copy of type hash
+               o += 4                                          // skip unused field
                for _, fn := range genfun(i.t, i.itype) {
-                       o = dsymptr(i.lsym, o, fn, 0) // method pointer for each method
+                       o = objw.SymPtr(i.lsym, o, fn, 0) // method pointer for each method
                }
                // Nothing writes static itabs, so they are read only.
-               ggloblsym(i.lsym, int32(o), int16(obj.DUPOK|obj.RODATA))
+               objw.Global(i.lsym, int32(o), int16(obj.DUPOK|obj.RODATA))
                i.lsym.Set(obj.AttrContentAddressable, true)
        }
 
        // process ptabs
-       if localpkg.Name == "main" && len(ptabs) > 0 {
+       if types.LocalPkg.Name == "main" && len(ptabs) > 0 {
                ot := 0
-               s := Ctxt.Lookup("go.plugin.tabs")
+               s := base.Ctxt.Lookup("go.plugin.tabs")
                for _, p := range ptabs {
                        // Dump ptab symbol into go.pluginsym package.
                        //
@@ -1617,57 +1378,70 @@ func dumptabs() {
                        //      name nameOff
                        //      typ  typeOff // pointer to symbol
                        // }
-                       nsym := dname(p.s.Name, "", nil, true)
-                       ot = dsymptrOff(s, ot, nsym)
-                       ot = dsymptrOff(s, ot, dtypesym(p.t))
+                       nsym := dname(p.Sym().Name, "", nil, true)
+                       t := p.Type()
+                       if p.Class != ir.PFUNC {
+                               t = types.NewPtr(t)
+                       }
+                       tsym := writeType(t)
+                       ot = objw.SymPtrOff(s, ot, nsym)
+                       ot = objw.SymPtrOff(s, ot, tsym)
+                       // Plugin exports symbols as interfaces. Mark their types
+                       // as UsedInIface.
+                       tsym.Set(obj.AttrUsedInIface, true)
                }
-               ggloblsym(s, int32(ot), int16(obj.RODATA))
+               objw.Global(s, int32(ot), int16(obj.RODATA))
 
                ot = 0
-               s = Ctxt.Lookup("go.plugin.exports")
+               s = base.Ctxt.Lookup("go.plugin.exports")
                for _, p := range ptabs {
-                       ot = dsymptr(s, ot, p.s.Linksym(), 0)
+                       ot = objw.SymPtr(s, ot, p.Linksym(), 0)
                }
-               ggloblsym(s, int32(ot), int16(obj.RODATA))
+               objw.Global(s, int32(ot), int16(obj.RODATA))
        }
 }
 
-func dumpimportstrings() {
+func WriteImportStrings() {
        // generate import strings for imported packages
        for _, p := range types.ImportedPkgList() {
                dimportpath(p)
        }
 }
 
-func dumpbasictypes() {
+func WriteBasicTypes() {
        // do basic types if compiling package runtime.
        // they have to be in at least one package,
        // and runtime is always loaded implicitly,
        // so this is as good as any.
        // another possible choice would be package main,
        // but using runtime means fewer copies in object files.
-       if myimportpath == "runtime" {
-               for i := types.EType(1); i <= TBOOL; i++ {
-                       dtypesym(types.NewPtr(types.Types[i]))
+       if base.Ctxt.Pkgpath == "runtime" {
+               for i := types.Kind(1); i <= types.TBOOL; i++ {
+                       writeType(types.NewPtr(types.Types[i]))
                }
-               dtypesym(types.NewPtr(types.Types[TSTRING]))
-               dtypesym(types.NewPtr(types.Types[TUNSAFEPTR]))
+               writeType(types.NewPtr(types.Types[types.TSTRING]))
+               writeType(types.NewPtr(types.Types[types.TUNSAFEPTR]))
 
                // emit type structs for error and func(error) string.
                // The latter is the type of an auto-generated wrapper.
-               dtypesym(types.NewPtr(types.Errortype))
+               writeType(types.NewPtr(types.ErrorType))
 
-               dtypesym(functype(nil, []*Node{anonfield(types.Errortype)}, []*Node{anonfield(types.Types[TSTRING])}))
+               writeType(types.NewSignature(types.NoPkg, nil, []*types.Field{
+                       types.NewField(base.Pos, nil, types.ErrorType),
+               }, []*types.Field{
+                       types.NewField(base.Pos, nil, types.Types[types.TSTRING]),
+               }))
 
                // add paths for runtime and main, which 6l imports implicitly.
-               dimportpath(Runtimepkg)
+               dimportpath(ir.Pkgs.Runtime)
 
-               if flag_race {
-                       dimportpath(racepkg)
+               if base.Flag.Race {
+                       dimportpath(types.NewPkg("runtime/race", ""))
                }
-               if flag_msan {
-                       dimportpath(msanpkg)
+               if base.Flag.MSan {
+                       dimportpath(types.NewPkg("runtime/msan", ""))
                }
+
                dimportpath(types.NewPkg("main", ""))
        }
 }
@@ -1696,7 +1470,7 @@ func (a typesByString) Less(i, j int) bool {
        // will be equal for the above checks, but different in DWARF output.
        // Sort by source position to ensure deterministic order.
        // See issues 27013 and 30202.
-       if a[i].t.Etype == types.TINTER && a[i].t.Methods().Len() > 0 {
+       if a[i].t.Kind() == types.TINTER && a[i].t.Methods().Len() > 0 {
                return a[i].t.Methods().Index(0).Pos.Before(a[j].t.Methods().Index(0).Pos)
        }
        return false
@@ -1741,8 +1515,8 @@ const maxPtrmaskBytes = 2048
 // along with a boolean reporting whether the UseGCProg bit should be set in
 // the type kind, and the ptrdata field to record in the reflect type information.
 func dgcsym(t *types.Type) (lsym *obj.LSym, useGCProg bool, ptrdata int64) {
-       ptrdata = typeptrdata(t)
-       if ptrdata/int64(Widthptr) <= maxPtrmaskBytes*8 {
+       ptrdata = types.PtrDataSize(t)
+       if ptrdata/int64(types.PtrSize) <= maxPtrmaskBytes*8 {
                lsym = dgcptrmask(t)
                return
        }
@@ -1754,18 +1528,18 @@ func dgcsym(t *types.Type) (lsym *obj.LSym, useGCProg bool, ptrdata int64) {
 
 // dgcptrmask emits and returns the symbol containing a pointer mask for type t.
 func dgcptrmask(t *types.Type) *obj.LSym {
-       ptrmask := make([]byte, (typeptrdata(t)/int64(Widthptr)+7)/8)
+       ptrmask := make([]byte, (types.PtrDataSize(t)/int64(types.PtrSize)+7)/8)
        fillptrmask(t, ptrmask)
        p := fmt.Sprintf("gcbits.%x", ptrmask)
 
-       sym := Runtimepkg.Lookup(p)
+       sym := ir.Pkgs.Runtime.Lookup(p)
        lsym := sym.Linksym()
        if !sym.Uniq() {
                sym.SetUniq(true)
                for i, x := range ptrmask {
-                       duint8(lsym, i, x)
+                       objw.Uint8(lsym, i, x)
                }
-               ggloblsym(lsym, int32(len(ptrmask)), obj.DUPOK|obj.RODATA|obj.LOCAL)
+               objw.Global(lsym, int32(len(ptrmask)), obj.DUPOK|obj.RODATA|obj.LOCAL)
                lsym.Set(obj.AttrContentAddressable, true)
        }
        return lsym
@@ -1782,10 +1556,10 @@ func fillptrmask(t *types.Type, ptrmask []byte) {
                return
        }
 
-       vec := bvalloc(8 * int32(len(ptrmask)))
-       onebitwalktype1(t, 0, vec)
+       vec := bitvec.New(8 * int32(len(ptrmask)))
+       typebits.Set(t, 0, vec)
 
-       nptr := typeptrdata(t) / int64(Widthptr)
+       nptr := types.PtrDataSize(t) / int64(types.PtrSize)
        for i := int64(0); i < nptr; i++ {
                if vec.Get(int32(i)) {
                        ptrmask[i/8] |= 1 << (uint(i) % 8)
@@ -1798,80 +1572,78 @@ func fillptrmask(t *types.Type, ptrmask []byte) {
 // In practice, the size is typeptrdata(t) except for non-trivial arrays.
 // For non-trivial arrays, the program describes the full t.Width size.
 func dgcprog(t *types.Type) (*obj.LSym, int64) {
-       dowidth(t)
-       if t.Width == BADWIDTH {
-               Fatalf("dgcprog: %v badwidth", t)
+       types.CalcSize(t)
+       if t.Width == types.BADWIDTH {
+               base.Fatalf("dgcprog: %v badwidth", t)
        }
-       lsym := typesymprefix(".gcprog", t).Linksym()
-       var p GCProg
+       lsym := TypeLinksymPrefix(".gcprog", t)
+       var p gcProg
        p.init(lsym)
        p.emit(t, 0)
-       offset := p.w.BitIndex() * int64(Widthptr)
+       offset := p.w.BitIndex() * int64(types.PtrSize)
        p.end()
-       if ptrdata := typeptrdata(t); offset < ptrdata || offset > t.Width {
-               Fatalf("dgcprog: %v: offset=%d but ptrdata=%d size=%d", t, offset, ptrdata, t.Width)
+       if ptrdata := types.PtrDataSize(t); offset < ptrdata || offset > t.Width {
+               base.Fatalf("dgcprog: %v: offset=%d but ptrdata=%d size=%d", t, offset, ptrdata, t.Width)
        }
        return lsym, offset
 }
 
-type GCProg struct {
+type gcProg struct {
        lsym   *obj.LSym
        symoff int
        w      gcprog.Writer
 }
 
-var Debug_gcprog int // set by -d gcprog
-
-func (p *GCProg) init(lsym *obj.LSym) {
+func (p *gcProg) init(lsym *obj.LSym) {
        p.lsym = lsym
        p.symoff = 4 // first 4 bytes hold program length
        p.w.Init(p.writeByte)
-       if Debug_gcprog > 0 {
+       if base.Debug.GCProg > 0 {
                fmt.Fprintf(os.Stderr, "compile: start GCProg for %v\n", lsym)
                p.w.Debug(os.Stderr)
        }
 }
 
-func (p *GCProg) writeByte(x byte) {
-       p.symoff = duint8(p.lsym, p.symoff, x)
+func (p *gcProg) writeByte(x byte) {
+       p.symoff = objw.Uint8(p.lsym, p.symoff, x)
 }
 
-func (p *GCProg) end() {
+func (p *gcProg) end() {
        p.w.End()
-       duint32(p.lsym, 0, uint32(p.symoff-4))
-       ggloblsym(p.lsym, int32(p.symoff), obj.DUPOK|obj.RODATA|obj.LOCAL)
-       if Debug_gcprog > 0 {
+       objw.Uint32(p.lsym, 0, uint32(p.symoff-4))
+       objw.Global(p.lsym, int32(p.symoff), obj.DUPOK|obj.RODATA|obj.LOCAL)
+       if base.Debug.GCProg > 0 {
                fmt.Fprintf(os.Stderr, "compile: end GCProg for %v\n", p.lsym)
        }
 }
 
-func (p *GCProg) emit(t *types.Type, offset int64) {
-       dowidth(t)
+func (p *gcProg) emit(t *types.Type, offset int64) {
+       types.CalcSize(t)
        if !t.HasPointers() {
                return
        }
-       if t.Width == int64(Widthptr) {
-               p.w.Ptr(offset / int64(Widthptr))
+       if t.Width == int64(types.PtrSize) {
+               p.w.Ptr(offset / int64(types.PtrSize))
                return
        }
-       switch t.Etype {
+       switch t.Kind() {
        default:
-               Fatalf("GCProg.emit: unexpected type %v", t)
+               base.Fatalf("gcProg.emit: unexpected type %v", t)
 
-       case TSTRING:
-               p.w.Ptr(offset / int64(Widthptr))
+       case types.TSTRING:
+               p.w.Ptr(offset / int64(types.PtrSize))
 
-       case TINTER:
-               // Note: the first word isn't a pointer. See comment in plive.go:onebitwalktype1.
-               p.w.Ptr(offset/int64(Widthptr) + 1)
+       case types.TINTER:
+               // Note: the first word isn't a pointer. See comment in typebits.Set
+               p.w.Ptr(offset/int64(types.PtrSize) + 1)
 
-       case TSLICE:
-               p.w.Ptr(offset / int64(Widthptr))
+       case types.TSLICE:
+               p.w.Ptr(offset / int64(types.PtrSize))
 
-       case TARRAY:
+       case types.TARRAY:
                if t.NumElem() == 0 {
                        // should have been handled by haspointers check above
-                       Fatalf("GCProg.emit: empty array")
+                       base.Fatalf("gcProg.emit: empty array")
                }
 
                // Flatten array-of-array-of-array to just a big array by multiplying counts.
@@ -1882,7 +1654,7 @@ func (p *GCProg) emit(t *types.Type, offset int64) {
                        elem = elem.Elem()
                }
 
-               if !p.w.ShouldRepeat(elem.Width/int64(Widthptr), count) {
+               if !p.w.ShouldRepeat(elem.Width/int64(types.PtrSize), count) {
                        // Cheaper to just emit the bits.
                        for i := int64(0); i < count; i++ {
                                p.emit(elem, offset+i*elem.Width)
@@ -1890,35 +1662,202 @@ func (p *GCProg) emit(t *types.Type, offset int64) {
                        return
                }
                p.emit(elem, offset)
-               p.w.ZeroUntil((offset + elem.Width) / int64(Widthptr))
-               p.w.Repeat(elem.Width/int64(Widthptr), count-1)
+               p.w.ZeroUntil((offset + elem.Width) / int64(types.PtrSize))
+               p.w.Repeat(elem.Width/int64(types.PtrSize), count-1)
 
-       case TSTRUCT:
+       case types.TSTRUCT:
                for _, t1 := range t.Fields().Slice() {
                        p.emit(t1.Type, offset+t1.Offset)
                }
        }
 }
 
-// zeroaddr returns the address of a symbol with at least
+// ZeroAddr returns the address of a symbol with at least
 // size bytes of zeros.
-func zeroaddr(size int64) *Node {
+func ZeroAddr(size int64) ir.Node {
        if size >= 1<<31 {
-               Fatalf("map elem too big %d", size)
-       }
-       if zerosize < size {
-               zerosize = size
-       }
-       s := mappkg.Lookup("zero")
-       if s.Def == nil {
-               x := newname(s)
-               x.Type = types.Types[TUINT8]
-               x.SetClass(PEXTERN)
-               x.SetTypecheck(1)
-               s.Def = asTypesNode(x)
-       }
-       z := nod(OADDR, asNode(s.Def), nil)
-       z.Type = types.NewPtr(types.Types[TUINT8])
-       z.SetTypecheck(1)
-       return z
+               base.Fatalf("map elem too big %d", size)
+       }
+       if ZeroSize < size {
+               ZeroSize = size
+       }
+       lsym := base.PkgLinksym("go.map", "zero", obj.ABI0)
+       x := ir.NewLinksymExpr(base.Pos, lsym, types.Types[types.TUINT8])
+       return typecheck.Expr(typecheck.NodAddr(x))
+}
+
+func CollectPTabs() {
+       if !base.Ctxt.Flag_dynlink || types.LocalPkg.Name != "main" {
+               return
+       }
+       for _, exportn := range typecheck.Target.Exports {
+               s := exportn.Sym()
+               nn := ir.AsNode(s.Def)
+               if nn == nil {
+                       continue
+               }
+               if nn.Op() != ir.ONAME {
+                       continue
+               }
+               n := nn.(*ir.Name)
+               if !types.IsExported(s.Name) {
+                       continue
+               }
+               if s.Pkg.Name != "main" {
+                       continue
+               }
+               ptabs = append(ptabs, n)
+       }
+}
+
+// Generate a wrapper function to convert from
+// a receiver of type T to a receiver of type U.
+// That is,
+//
+//     func (t T) M() {
+//             ...
+//     }
+//
+// already exists; this function generates
+//
+//     func (u U) M() {
+//             u.M()
+//     }
+//
+// where the types T and U are such that u.M() is valid
+// and calls the T.M method.
+// The resulting function is for use in method tables.
+//
+//     rcvr - U
+//     method - M func (t T)(), a TFIELD type struct
+func methodWrapper(rcvr *types.Type, method *types.Field) *obj.LSym {
+       newnam := ir.MethodSym(rcvr, method.Sym)
+       lsym := newnam.Linksym()
+       if newnam.Siggen() {
+               return lsym
+       }
+       newnam.SetSiggen(true)
+
+       if types.Identical(rcvr, method.Type.Recv().Type) {
+               return lsym
+       }
+
+       // Only generate (*T).M wrappers for T.M in T's own package.
+       if rcvr.IsPtr() && rcvr.Elem() == method.Type.Recv().Type &&
+               rcvr.Elem().Sym() != nil && rcvr.Elem().Sym().Pkg != types.LocalPkg {
+               return lsym
+       }
+
+       // Only generate I.M wrappers for I in I's own package
+       // but keep doing it for error.Error (was issue #29304).
+       if rcvr.IsInterface() && rcvr.Sym() != nil && rcvr.Sym().Pkg != types.LocalPkg && rcvr != types.ErrorType {
+               return lsym
+       }
+
+       base.Pos = base.AutogeneratedPos
+       typecheck.DeclContext = ir.PEXTERN
+
+       tfn := ir.NewFuncType(base.Pos,
+               ir.NewField(base.Pos, typecheck.Lookup(".this"), nil, rcvr),
+               typecheck.NewFuncParams(method.Type.Params(), true),
+               typecheck.NewFuncParams(method.Type.Results(), false))
+
+       fn := typecheck.DeclFunc(newnam, tfn)
+       fn.SetDupok(true)
+
+       nthis := ir.AsNode(tfn.Type().Recv().Nname)
+
+       methodrcvr := method.Type.Recv().Type
+
+       // generate nil pointer check for better error
+       if rcvr.IsPtr() && rcvr.Elem() == methodrcvr {
+               // generating wrapper from *T to T.
+               n := ir.NewIfStmt(base.Pos, nil, nil, nil)
+               n.Cond = ir.NewBinaryExpr(base.Pos, ir.OEQ, nthis, typecheck.NodNil())
+               call := ir.NewCallExpr(base.Pos, ir.OCALL, typecheck.LookupRuntime("panicwrap"), nil)
+               n.Body = []ir.Node{call}
+               fn.Body.Append(n)
+       }
+
+       dot := typecheck.AddImplicitDots(ir.NewSelectorExpr(base.Pos, ir.OXDOT, nthis, method.Sym))
+
+       // generate call
+       // It's not possible to use a tail call when dynamic linking on ppc64le. The
+       // bad scenario is when a local call is made to the wrapper: the wrapper will
+       // call the implementation, which might be in a different module and so set
+       // the TOC to the appropriate value for that module. But if it returns
+       // directly to the wrapper's caller, nothing will reset it to the correct
+       // value for that function.
+       if !base.Flag.Cfg.Instrumenting && rcvr.IsPtr() && methodrcvr.IsPtr() && method.Embedded != 0 && !types.IsInterfaceMethod(method.Type) && !(base.Ctxt.Arch.Name == "ppc64le" && base.Ctxt.Flag_dynlink) {
+               // generate tail call: adjust pointer receiver and jump to embedded method.
+               left := dot.X // skip final .M
+               if !left.Type().IsPtr() {
+                       left = typecheck.NodAddr(left)
+               }
+               as := ir.NewAssignStmt(base.Pos, nthis, typecheck.ConvNop(left, rcvr))
+               fn.Body.Append(as)
+               fn.Body.Append(ir.NewTailCallStmt(base.Pos, method.Nname.(*ir.Name)))
+       } else {
+               fn.SetWrapper(true) // ignore frame for panic+recover matching
+               call := ir.NewCallExpr(base.Pos, ir.OCALL, dot, nil)
+               call.Args = ir.ParamNames(tfn.Type())
+               call.IsDDD = tfn.Type().IsVariadic()
+               if method.Type.NumResults() > 0 {
+                       ret := ir.NewReturnStmt(base.Pos, nil)
+                       ret.Results = []ir.Node{call}
+                       fn.Body.Append(ret)
+               } else {
+                       fn.Body.Append(call)
+               }
+       }
+
+       typecheck.FinishFuncBody()
+       if base.Debug.DclStack != 0 {
+               types.CheckDclstack()
+       }
+
+       typecheck.Func(fn)
+       ir.CurFunc = fn
+       typecheck.Stmts(fn.Body)
+
+       // Inline calls within (*T).M wrappers. This is safe because we only
+       // generate those wrappers within the same compilation unit as (T).M.
+       // TODO(mdempsky): Investigate why we can't enable this more generally.
+       if rcvr.IsPtr() && rcvr.Elem() == method.Type.Recv().Type && rcvr.Elem().Sym() != nil {
+               inline.InlineCalls(fn)
+       }
+       escape.Batch([]*ir.Func{fn}, false)
+
+       ir.CurFunc = nil
+       typecheck.Target.Decls = append(typecheck.Target.Decls, fn)
+
+       return lsym
+}
+
+var ZeroSize int64
+
+// MarkTypeUsedInInterface marks that type t is converted to an interface.
+// This information is used in the linker in dead method elimination.
+func MarkTypeUsedInInterface(t *types.Type, from *obj.LSym) {
+       tsym := TypeLinksym(t)
+       // Emit a marker relocation. The linker will know the type is converted
+       // to an interface if "from" is reachable.
+       r := obj.Addrel(from)
+       r.Sym = tsym
+       r.Type = objabi.R_USEIFACE
+}
+
+// MarkUsedIfaceMethod marks that an interface method is used in the current
+// function. n is OCALLINTER node.
+func MarkUsedIfaceMethod(n *ir.CallExpr) {
+       dot := n.X.(*ir.SelectorExpr)
+       ityp := dot.X.Type()
+       tsym := TypeLinksym(ityp)
+       r := obj.Addrel(ir.CurFunc.LSym)
+       r.Sym = tsym
+       // dot.Xoffset is the method index * Widthptr (the offset of code pointer
+       // in itab).
+       midx := dot.Offset() / int64(types.PtrSize)
+       r.Add = InterfaceMethodOffset(ityp, midx)
+       r.Type = objabi.R_USEIFACEMETHOD
 }