aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/typecheck/iexport.go
diff options
context:
space:
mode:
authorMatthew Dempsky <mdempsky@google.com>2021-01-24 23:39:16 -0800
committerMatthew Dempsky <mdempsky@google.com>2021-01-25 18:53:24 +0000
commit6a4739ccc5198449d58d2e90a040c4fb908b3cb0 (patch)
treedbbb2abdcc1f7d414343ec27150ebd6afb2b3955 /src/cmd/compile/internal/typecheck/iexport.go
parentbe9612a832186637173e35a2aa83ae193cf8d957 (diff)
downloadgo-6a4739ccc5198449d58d2e90a040c4fb908b3cb0.tar.gz
go-6a4739ccc5198449d58d2e90a040c4fb908b3cb0.zip
[dev.regabi] cmd/compile: enable rational constant arithmetic
This allows more precision and matches types2's behavior. For backwards compatibility with gcimporter, for now we still need to write out declared constants as limited-precision floating-point values. To ensure consistent behavior of constant arithmetic whether it spans package boundaries or not, we include the full-precision rational representation in the compiler's extension section of the export data. Also, this CL simply uses the math/big.Rat.String text representation as the encoding. This is inefficient, but because it's only in the compiler's extension section, we can easily revisit this in the future. Declaring exported untyped float and complex constants isn't very common anyway. Within the standard library, only package math declares any at all, containing just 15. And those 15 are only imported a total of 12 times elsewhere in the standard library. Change-Id: I85ea23ab712e93fd3b68e52d60cbedce9be696a0 Reviewed-on: https://go-review.googlesource.com/c/go/+/286215 Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Go Bot <gobot@golang.org> Trust: Matthew Dempsky <mdempsky@google.com> Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src/cmd/compile/internal/typecheck/iexport.go')
-rw-r--r--src/cmd/compile/internal/typecheck/iexport.go51
1 files changed, 48 insertions, 3 deletions
diff --git a/src/cmd/compile/internal/typecheck/iexport.go b/src/cmd/compile/internal/typecheck/iexport.go
index be4a689836..6fab74e61f 100644
--- a/src/cmd/compile/internal/typecheck/iexport.go
+++ b/src/cmd/compile/internal/typecheck/iexport.go
@@ -462,12 +462,16 @@ func (p *iexporter) doDecl(n *ir.Name) {
}
case ir.OLITERAL:
+ // TODO(mdempsky): Extend check to all declarations.
+ if n.Typecheck() == 0 {
+ base.FatalfAt(n.Pos(), "missed typecheck: %v", n)
+ }
+
// Constant.
- // TODO(mdempsky): Do we still need this typecheck? If so, why?
- n = Expr(n).(*ir.Name)
w.tag('C')
w.pos(n.Pos())
w.value(n.Type(), n.Val())
+ w.constExt(n)
case ir.OTYPE:
if types.IsDotAlias(n.Sym()) {
@@ -956,6 +960,17 @@ func (w *exportWriter) mpfloat(v constant.Value, typ *types.Type) {
}
}
+func (w *exportWriter) mprat(v constant.Value) {
+ r, ok := constant.Val(v).(*big.Rat)
+ if !w.bool(ok) {
+ return
+ }
+ // TODO(mdempsky): Come up with a more efficient binary
+ // encoding before bumping iexportVersion to expose to
+ // gcimporter.
+ w.string(r.String())
+}
+
func (w *exportWriter) bool(b bool) bool {
var x uint64
if b {
@@ -971,7 +986,37 @@ func (w *exportWriter) string(s string) { w.uint64(w.p.stringOff(s)) }
// Compiler-specific extensions.
-func (w *exportWriter) varExt(n ir.Node) {
+func (w *exportWriter) constExt(n *ir.Name) {
+ // Internally, we now represent untyped float and complex
+ // constants with infinite-precision rational numbers using
+ // go/constant, but the "public" export data format known to
+ // gcimporter only supports 512-bit floating point constants.
+ // In case rationals turn out to be a bad idea and we want to
+ // switch back to fixed-precision constants, for now we
+ // continue writing out the 512-bit truncation in the public
+ // data section, and write the exact, rational constant in the
+ // compiler's extension data. Also, we only need to worry
+ // about exporting rationals for declared constants, because
+ // constants that appear in an expression will already have
+ // been coerced to a concrete, fixed-precision type.
+ //
+ // Eventually, assuming we stick with using rationals, we
+ // should bump iexportVersion to support rationals, and do the
+ // whole gcimporter update song-and-dance.
+ //
+ // TODO(mdempsky): Prepare vocals for that.
+
+ switch n.Type() {
+ case types.UntypedFloat:
+ w.mprat(n.Val())
+ case types.UntypedComplex:
+ v := n.Val()
+ w.mprat(constant.Real(v))
+ w.mprat(constant.Imag(v))
+ }
+}
+
+func (w *exportWriter) varExt(n *ir.Name) {
w.linkname(n.Sym())
w.symIdx(n.Sym())
}