Skip to content

Commit 4c52eac

Browse files
committed
cmd/compile: simplify value coding for unified IR
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 <[email protected]> Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Cuong Manh Le <[email protected]>
1 parent e30a090 commit 4c52eac

File tree

5 files changed

+14
-26
lines changed

5 files changed

+14
-26
lines changed

src/cmd/compile/internal/noder/decoder.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ func (r *decoder) strings() []string {
255255
return res
256256
}
257257

258-
func (r *decoder) rawValue() constant.Value {
258+
func (r *decoder) value() constant.Value {
259+
r.sync(syncValue)
259260
isComplex := r.bool()
260261
val := r.scalar()
261262
if isComplex {

src/cmd/compile/internal/noder/encoder.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ func (w *encoder) strings(ss []string) {
237237
}
238238
}
239239

240-
func (w *encoder) rawValue(val constant.Value) {
240+
func (w *encoder) value(val constant.Value) {
241+
w.sync(syncValue)
241242
if w.bool(val.Kind() == constant.Complex) {
242243
w.scalar(constant.Real(val))
243244
w.scalar(constant.Imag(val))

src/cmd/compile/internal/noder/reader.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,8 @@ func (pr *pkgReader) objIdx(idx int, implicits, explicits []*types.Type) ir.Node
626626

627627
case objConst:
628628
name := do(ir.OLITERAL, false)
629-
typ, val := r.value()
629+
typ := r.typ()
630+
val := FixValue(typ, r.value())
630631
setType(name, typ)
631632
setValue(name, val)
632633
return name
@@ -755,12 +756,6 @@ func (r *reader) typeParamNames() {
755756
}
756757
}
757758

758-
func (r *reader) value() (*types.Type, constant.Value) {
759-
r.sync(syncValue)
760-
typ := r.typ()
761-
return typ, FixValue(typ, r.rawValue())
762-
}
763-
764759
func (r *reader) method() *types.Field {
765760
r.sync(syncMethod)
766761
pos := r.pos()
@@ -1556,7 +1551,8 @@ func (r *reader) expr() (res ir.Node) {
15561551

15571552
case exprConst:
15581553
pos := r.pos()
1559-
typ, val := r.value()
1554+
typ := r.typ()
1555+
val := FixValue(typ, r.value())
15601556
op := r.op()
15611557
orig := r.string()
15621558
return typecheck.Expr(OrigConst(pos, typ, val, op, orig))

src/cmd/compile/internal/noder/reader2.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
package noder
88

99
import (
10-
"go/constant"
11-
1210
"cmd/compile/internal/base"
1311
"cmd/compile/internal/syntax"
1412
"cmd/compile/internal/types2"
@@ -388,7 +386,8 @@ func (pr *pkgReader2) objIdx(idx int) (*types2.Package, string) {
388386

389387
case objConst:
390388
pos := r.pos()
391-
typ, val := r.value()
389+
typ := r.typ()
390+
val := r.value()
392391
return types2.NewConst(pos, objPkg, objName, typ, val)
393392

394393
case objFunc:
@@ -428,11 +427,6 @@ func (pr *pkgReader2) objIdx(idx int) (*types2.Package, string) {
428427
return objPkg, objName
429428
}
430429

431-
func (r *reader2) value() (types2.Type, constant.Value) {
432-
r.sync(syncValue)
433-
return r.typ(), r.rawValue()
434-
}
435-
436430
func (pr *pkgReader2) objDictIdx(idx int) *reader2Dict {
437431
r := pr.newReader(relocObjDict, idx, syncObject1)
438432

src/cmd/compile/internal/noder/writer.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,8 @@ func (w *writer) doObj(obj types2.Object) codeObj {
542542

543543
case *types2.Const:
544544
w.pos(obj)
545-
w.value(obj.Type(), obj.Val())
545+
w.typ(obj.Type())
546+
w.value(obj.Val())
546547
return objConst
547548

548549
case *types2.Func:
@@ -598,12 +599,6 @@ func (w *writer) typExpr(expr syntax.Expr) {
598599
w.typ(tv.Type)
599600
}
600601

601-
func (w *writer) value(typ types2.Type, val constant.Value) {
602-
w.sync(syncValue)
603-
w.typ(typ)
604-
w.rawValue(val)
605-
}
606-
607602
// objDict writes the dictionary needed for reading the given object.
608603
func (w *writer) objDict(obj types2.Object, dict *writerDict) {
609604
// TODO(mdempsky): Split objDict into multiple entries? reader.go
@@ -1199,7 +1194,8 @@ func (w *writer) expr(expr syntax.Expr) {
11991194

12001195
w.code(exprConst)
12011196
w.pos(pos)
1202-
w.value(tv.Type, tv.Value)
1197+
w.typ(tv.Type)
1198+
w.value(tv.Value)
12031199

12041200
// TODO(mdempsky): These details are only important for backend
12051201
// diagnostics. Explore writing them out separately.

0 commit comments

Comments
 (0)