]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: clean up slice and string offsets/sizes
authorJosh Bleecher Snyder <josharian@gmail.com>
Wed, 8 Apr 2020 19:36:35 +0000 (12:36 -0700)
committerJosh Bleecher Snyder <josharian@gmail.com>
Thu, 9 Apr 2020 01:16:43 +0000 (01:16 +0000)
Minor cleanup:

* Modernize comments.
* Change from int to int64 to avoid conversions.
* Use idiomatic names.

Passes toolstash-check.

Change-Id: I93560c81926c0f4e00f33129cb4846b53bea99e6
Reviewed-on: https://go-review.googlesource.com/c/go/+/227548
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/gc/align.go
src/cmd/compile/internal/gc/go.go
src/cmd/compile/internal/gc/sinit.go
src/cmd/compile/internal/gc/ssa.go
src/cmd/compile/internal/gc/universe.go

index ec66cb43922d412ac9c99227b0451eabe567adc6..ab578ee8c7e82da9c8bff66792fe027d143973ac 100644 (file)
@@ -319,10 +319,10 @@ func dowidth(t *types.Type) {
                Fatalf("dowidth any")
 
        case TSTRING:
-               if sizeof_String == 0 {
+               if sizeofString == 0 {
                        Fatalf("early dowidth string")
                }
-               w = int64(sizeof_String)
+               w = sizeofString
                t.Align = uint8(Widthptr)
 
        case TARRAY:
@@ -344,7 +344,7 @@ func dowidth(t *types.Type) {
                if t.Elem() == nil {
                        break
                }
-               w = int64(sizeof_Slice)
+               w = sizeofSlice
                checkwidth(t.Elem())
                t.Align = uint8(Widthptr)
 
index d2a1b21cbd68df96adc48969d20a21c11a252daf..a7fdfd5b7a345fb61af5115636fc024787f2b08d 100644 (file)
@@ -64,32 +64,30 @@ const (
        _ = uint((1 << 3) - iota) // static assert for iota <= (1 << 3)
 )
 
-// note this is the runtime representation
-// of the compilers slices.
+// Slices in the runtime are represented by three components:
 //
-// typedef     struct
-// {                           // must not move anything
-//     uchar   array[8];       // pointer to data
-//     uchar   nel[4];         // number of elements
-//     uchar   cap[4];         // allocated number of elements
-// } Slice;
-var slice_array int // runtime offsetof(Slice,array) - same for String
-
-var slice_nel int // runtime offsetof(Slice,nel) - same for String
-
-var slice_cap int // runtime offsetof(Slice,cap)
-
-var sizeof_Slice int // runtime sizeof(Slice)
-
-// note this is the runtime representation
-// of the compilers strings.
+// type slice struct {
+//     ptr unsafe.Pointer
+//     len int
+//     cap int
+// }
+//
+// Strings in the runtime are represented by two components:
 //
-// typedef     struct
-// {                           // must not move anything
-//     uchar   array[8];       // pointer to data
-//     uchar   nel[4];         // number of elements
-// } String;
-var sizeof_String int // runtime sizeof(String)
+// type string struct {
+//     ptr unsafe.Pointer
+//     len int
+// }
+//
+// These variables are the offsets of fields and sizes of these structs.
+var (
+       slicePtrOffset int64
+       sliceLenOffset int64
+       sliceCapOffset int64
+
+       sizeofSlice  int64
+       sizeofString int64
+)
 
 var pragcgobuf [][]string
 
index 630b3999d1da677726af1fdef9a47c459a7991b1..446bd8ab177a2bd43fdc1c836c2e3adc9d858d27 100644 (file)
@@ -130,11 +130,11 @@ func (s *InitSchedule) staticcopy(l *Node, r *Node) bool {
                a := s.inittemps[r]
 
                n := l.copy()
-               n.Xoffset = l.Xoffset + int64(slice_array)
+               n.Xoffset = l.Xoffset + slicePtrOffset
                gdata(n, nod(OADDR, a, nil), Widthptr)
-               n.Xoffset = l.Xoffset + int64(slice_nel)
+               n.Xoffset = l.Xoffset + sliceLenOffset
                gdata(n, r.Right, Widthptr)
-               n.Xoffset = l.Xoffset + int64(slice_cap)
+               n.Xoffset = l.Xoffset + sliceCapOffset
                gdata(n, r.Right, Widthptr)
                return true
 
@@ -227,11 +227,11 @@ func (s *InitSchedule) staticassign(l *Node, r *Node) bool {
                a := staticname(ta)
                s.inittemps[r] = a
                n := l.copy()
-               n.Xoffset = l.Xoffset + int64(slice_array)
+               n.Xoffset = l.Xoffset + slicePtrOffset
                gdata(n, nod(OADDR, a, nil), Widthptr)
-               n.Xoffset = l.Xoffset + int64(slice_nel)
+               n.Xoffset = l.Xoffset + sliceLenOffset
                gdata(n, r.Right, Widthptr)
-               n.Xoffset = l.Xoffset + int64(slice_cap)
+               n.Xoffset = l.Xoffset + sliceCapOffset
                gdata(n, r.Right, Widthptr)
 
                // Fall through to init underlying array.
@@ -615,11 +615,11 @@ func slicelit(ctxt initContext, n *Node, var_ *Node, init *Nodes) {
                v.Type = types.Types[TINT]
                setintconst(&v, t.NumElem())
 
-               nam.Xoffset += int64(slice_array)
+               nam.Xoffset += slicePtrOffset
                gdata(&nam, nod(OADDR, vstat, nil), Widthptr)
-               nam.Xoffset += int64(slice_nel) - int64(slice_array)
+               nam.Xoffset += sliceLenOffset - slicePtrOffset
                gdata(&nam, &v, Widthptr)
-               nam.Xoffset += int64(slice_cap) - int64(slice_nel)
+               nam.Xoffset += sliceCapOffset - sliceLenOffset
                gdata(&nam, &v, Widthptr)
 
                return
index 03d541f807ac1ff3f5e4e85b25f1eb10234fc923..5f45361980cf662f9c224670733e753061307a1f 100644 (file)
@@ -2830,7 +2830,7 @@ func (s *state) append(n *Node, inplace bool) *ssa.Value {
                        // Tell liveness we're about to build a new slice
                        s.vars[&memVar] = s.newValue1A(ssa.OpVarDef, types.TypeMem, sn, s.mem())
                }
-               capaddr := s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.IntPtr, int64(slice_cap), addr)
+               capaddr := s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.IntPtr, sliceCapOffset, addr)
                s.store(types.Types[TINT], capaddr, r[2])
                s.store(pt, addr, r[0])
                // load the value we just stored to avoid having to spill it
@@ -2851,7 +2851,7 @@ func (s *state) append(n *Node, inplace bool) *ssa.Value {
        if inplace {
                l = s.variable(&lenVar, types.Types[TINT]) // generates phi for len
                nl = s.newValue2(s.ssaOp(OADD, types.Types[TINT]), types.Types[TINT], l, s.constInt(types.Types[TINT], nargs))
-               lenaddr := s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.IntPtr, int64(slice_nel), addr)
+               lenaddr := s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.IntPtr, sliceLenOffset, addr)
                s.store(types.Types[TINT], lenaddr, nl)
        }
 
index d1072997f198c15eb0f9479e52e28beed3948f6d..04861c8dd42dae517f26f9d94192879d331281a2 100644 (file)
@@ -342,13 +342,13 @@ func typeinit() {
        simtype[TFUNC] = TPTR
        simtype[TUNSAFEPTR] = TPTR
 
-       slice_array = int(Rnd(0, int64(Widthptr)))
-       slice_nel = int(Rnd(int64(slice_array)+int64(Widthptr), int64(Widthptr)))
-       slice_cap = int(Rnd(int64(slice_nel)+int64(Widthptr), int64(Widthptr)))
-       sizeof_Slice = int(Rnd(int64(slice_cap)+int64(Widthptr), int64(Widthptr)))
+       slicePtrOffset = 0
+       sliceLenOffset = Rnd(slicePtrOffset+int64(Widthptr), int64(Widthptr))
+       sliceCapOffset = Rnd(sliceLenOffset+int64(Widthptr), int64(Widthptr))
+       sizeofSlice = Rnd(sliceCapOffset+int64(Widthptr), int64(Widthptr))
 
        // string is same as slice wo the cap
-       sizeof_String = int(Rnd(int64(slice_nel)+int64(Widthptr), int64(Widthptr)))
+       sizeofString = Rnd(sliceLenOffset+int64(Widthptr), int64(Widthptr))
 
        dowidth(types.Types[TSTRING])
        dowidth(types.Idealstring)