1
0
mirror of https://github.com/golang/go synced 2024-11-18 03:54:50 -07:00

cmd/compile: cleanup gdata slightly

In sinit.go, gdata can already handle strings and complex, so no
reason to handle them separately.

In obj.go, inline gdatastring and gdatacomplex into gdata, since it's
the only caller. Allows extracting out the common Linksym calls.

Passes toolstash -cmp.

Change-Id: I3cb18d9b4206a8a269c36e0d30a345d8e6caba1f
Reviewed-on: https://go-review.googlesource.com/31498
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
This commit is contained in:
Matthew Dempsky 2016-10-19 13:13:31 -07:00
parent 213ee3d20e
commit a2f77e9ef8
2 changed files with 24 additions and 57 deletions

View File

@ -361,25 +361,19 @@ func gdata(nam *Node, nr *Node, wid int) {
if nam.Sym == nil {
Fatalf("gdata nil nam sym")
}
s := Linksym(nam.Sym)
switch nr.Op {
case OLITERAL:
switch u := nr.Val().U.(type) {
case *Mpcplx:
gdatacomplex(nam, u)
case string:
gdatastring(nam, u)
case bool:
i := int64(obj.Bool2int(u))
Linksym(nam.Sym).WriteInt(Ctxt, nam.Xoffset, wid, i)
s.WriteInt(Ctxt, nam.Xoffset, wid, i)
case *Mpint:
Linksym(nam.Sym).WriteInt(Ctxt, nam.Xoffset, wid, u.Int64())
s.WriteInt(Ctxt, nam.Xoffset, wid, u.Int64())
case *Mpflt:
s := Linksym(nam.Sym)
f := u.Float64()
switch nam.Type.Etype {
case TFLOAT32:
@ -388,6 +382,23 @@ func gdata(nam *Node, nr *Node, wid int) {
s.WriteFloat64(Ctxt, nam.Xoffset, f)
}
case *Mpcplx:
r := u.Real.Float64()
i := u.Imag.Float64()
switch nam.Type.Etype {
case TCOMPLEX64:
s.WriteFloat32(Ctxt, nam.Xoffset, float32(r))
s.WriteFloat32(Ctxt, nam.Xoffset+4, float32(i))
case TCOMPLEX128:
s.WriteFloat64(Ctxt, nam.Xoffset, r)
s.WriteFloat64(Ctxt, nam.Xoffset+8, i)
}
case string:
symdata := stringsym(u)
s.WriteAddr(Ctxt, nam.Xoffset, Widthptr, symdata, 0)
s.WriteInt(Ctxt, nam.Xoffset+int64(Widthptr), Widthint, int64(len(u)))
default:
Fatalf("gdata unhandled OLITERAL %v", nr)
}
@ -397,38 +408,15 @@ func gdata(nam *Node, nr *Node, wid int) {
Fatalf("gdata ADDR left op %v", nr.Left.Op)
}
to := nr.Left
Linksym(nam.Sym).WriteAddr(Ctxt, nam.Xoffset, wid, Linksym(to.Sym), to.Xoffset)
s.WriteAddr(Ctxt, nam.Xoffset, wid, Linksym(to.Sym), to.Xoffset)
case ONAME:
if nr.Class != PFUNC {
Fatalf("gdata NAME not PFUNC %d", nr.Class)
}
Linksym(nam.Sym).WriteAddr(Ctxt, nam.Xoffset, wid, Linksym(funcsym(nr.Sym)), nr.Xoffset)
s.WriteAddr(Ctxt, nam.Xoffset, wid, Linksym(funcsym(nr.Sym)), nr.Xoffset)
default:
Fatalf("gdata unhandled op %v %v\n", nr, nr.Op)
}
}
func gdatacomplex(nam *Node, cval *Mpcplx) {
t := Types[cplxsubtype(nam.Type.Etype)]
r := cval.Real.Float64()
i := cval.Imag.Float64()
s := Linksym(nam.Sym)
switch t.Etype {
case TFLOAT32:
s.WriteFloat32(Ctxt, nam.Xoffset, float32(r))
s.WriteFloat32(Ctxt, nam.Xoffset+4, float32(i))
case TFLOAT64:
s.WriteFloat64(Ctxt, nam.Xoffset, r)
s.WriteFloat64(Ctxt, nam.Xoffset+8, i)
}
}
func gdatastring(nam *Node, sval string) {
s := Linksym(nam.Sym)
symdata := stringsym(sval)
s.WriteAddr(Ctxt, nam.Xoffset, Widthptr, symdata, 0)
s.WriteInt(Ctxt, nam.Xoffset+int64(Widthptr), Widthint, int64(len(sval)))
}

View File

@ -1409,32 +1409,11 @@ func genAsInitNoCheck(n *Node, reportOnly bool) bool {
return true
case OLITERAL:
break
}
switch nr.Type.Etype {
default:
return false
case TBOOL, TINT8, TUINT8, TINT16, TUINT16,
TINT32, TUINT32, TINT64, TUINT64,
TINT, TUINT, TUINTPTR, TUNSAFEPTR,
TPTR32, TPTR64,
TFLOAT32, TFLOAT64:
if !reportOnly {
gdata(&nam, nr, int(nr.Type.Width))
}
case TCOMPLEX64, TCOMPLEX128:
if !reportOnly {
gdatacomplex(&nam, nr.Val().U.(*Mpcplx))
}
case TSTRING:
if !reportOnly {
gdatastring(&nam, nr.Val().U.(string))
}
return true
}
return true
return false
}