]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: simplify value coding for unified IR
authorMatthew Dempsky <mdempsky@google.com>
Tue, 7 Sep 2021 20:23:08 +0000 (13:23 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Thu, 9 Sep 2021 00:29:35 +0000 (00:29 +0000)
In indexed export, values are always exported along with their type
and are encoded in a type-sensitive manner, because this matches how
cmd/compile handled constants internally.

However, go/types intentionally differs from this, decoupling type
from value representation. As unified IR strives to be more
go/types-centric, it makes sense to embrace this and make values a
more first-class encoding.

Change-Id: If21d849c4f610358bd776d5665469d180bcd5f6e
Reviewed-on: https://go-review.googlesource.com/c/go/+/348014
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
src/cmd/compile/internal/noder/decoder.go
src/cmd/compile/internal/noder/encoder.go
src/cmd/compile/internal/noder/reader.go
src/cmd/compile/internal/noder/reader2.go
src/cmd/compile/internal/noder/writer.go

index 3dc61c6a69208e93dc0e745a5af2e334ff27cc0a..2c18727420aec23281eac633cb89f31e1b30efce 100644 (file)
@@ -255,7 +255,8 @@ func (r *decoder) strings() []string {
        return res
 }
 
-func (r *decoder) rawValue() constant.Value {
+func (r *decoder) value() constant.Value {
+       r.sync(syncValue)
        isComplex := r.bool()
        val := r.scalar()
        if isComplex {
index d8ab0f6255dbca273a8b2af2e8d4a725d3302876..b07b3a4a480573b8a2279bf9dce2df1445608e22 100644 (file)
@@ -237,7 +237,8 @@ func (w *encoder) strings(ss []string) {
        }
 }
 
-func (w *encoder) rawValue(val constant.Value) {
+func (w *encoder) value(val constant.Value) {
+       w.sync(syncValue)
        if w.bool(val.Kind() == constant.Complex) {
                w.scalar(constant.Real(val))
                w.scalar(constant.Imag(val))
index b3cb10dadbd40588dadd63347b41899071a90b0d..e235dd57922c4b2d6d87c5a8d861843df624539d 100644 (file)
@@ -626,7 +626,8 @@ func (pr *pkgReader) objIdx(idx int, implicits, explicits []*types.Type) ir.Node
 
        case objConst:
                name := do(ir.OLITERAL, false)
-               typ, val := r.value()
+               typ := r.typ()
+               val := FixValue(typ, r.value())
                setType(name, typ)
                setValue(name, val)
                return name
@@ -755,12 +756,6 @@ func (r *reader) typeParamNames() {
        }
 }
 
-func (r *reader) value() (*types.Type, constant.Value) {
-       r.sync(syncValue)
-       typ := r.typ()
-       return typ, FixValue(typ, r.rawValue())
-}
-
 func (r *reader) method() *types.Field {
        r.sync(syncMethod)
        pos := r.pos()
@@ -1556,7 +1551,8 @@ func (r *reader) expr() (res ir.Node) {
 
        case exprConst:
                pos := r.pos()
-               typ, val := r.value()
+               typ := r.typ()
+               val := FixValue(typ, r.value())
                op := r.op()
                orig := r.string()
                return typecheck.Expr(OrigConst(pos, typ, val, op, orig))
index 6e2d1f2e76ea775e2dd9f09864d0bc583118030b..0cfde24b58e0fa7f4a6d2f744ad2c85576b5449f 100644 (file)
@@ -7,8 +7,6 @@
 package noder
 
 import (
-       "go/constant"
-
        "cmd/compile/internal/base"
        "cmd/compile/internal/syntax"
        "cmd/compile/internal/types2"
@@ -388,7 +386,8 @@ func (pr *pkgReader2) objIdx(idx int) (*types2.Package, string) {
 
                case objConst:
                        pos := r.pos()
-                       typ, val := r.value()
+                       typ := r.typ()
+                       val := r.value()
                        return types2.NewConst(pos, objPkg, objName, typ, val)
 
                case objFunc:
@@ -428,11 +427,6 @@ func (pr *pkgReader2) objIdx(idx int) (*types2.Package, string) {
        return objPkg, objName
 }
 
-func (r *reader2) value() (types2.Type, constant.Value) {
-       r.sync(syncValue)
-       return r.typ(), r.rawValue()
-}
-
 func (pr *pkgReader2) objDictIdx(idx int) *reader2Dict {
        r := pr.newReader(relocObjDict, idx, syncObject1)
 
index d1e5605739e2146e16e28326b340920fb50a7ca6..694035b73fea016cb87ea4124163befb77181d87 100644 (file)
@@ -542,7 +542,8 @@ func (w *writer) doObj(obj types2.Object) codeObj {
 
        case *types2.Const:
                w.pos(obj)
-               w.value(obj.Type(), obj.Val())
+               w.typ(obj.Type())
+               w.value(obj.Val())
                return objConst
 
        case *types2.Func:
@@ -598,12 +599,6 @@ func (w *writer) typExpr(expr syntax.Expr) {
        w.typ(tv.Type)
 }
 
-func (w *writer) value(typ types2.Type, val constant.Value) {
-       w.sync(syncValue)
-       w.typ(typ)
-       w.rawValue(val)
-}
-
 // objDict writes the dictionary needed for reading the given object.
 func (w *writer) objDict(obj types2.Object, dict *writerDict) {
        // TODO(mdempsky): Split objDict into multiple entries? reader.go
@@ -1199,7 +1194,8 @@ func (w *writer) expr(expr syntax.Expr) {
 
                        w.code(exprConst)
                        w.pos(pos)
-                       w.value(tv.Type, tv.Value)
+                       w.typ(tv.Type)
+                       w.value(tv.Value)
 
                        // TODO(mdempsky): These details are only important for backend
                        // diagnostics. Explore writing them out separately.