1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 "cmd/compile/internal/base"
13 "cmd/compile/internal/types"
17 // NewBool returns an OLITERAL representing b as an untyped boolean.
18 func NewBool(pos src.XPos, b bool) Node {
19 return NewBasicLit(pos, types.UntypedBool, constant.MakeBool(b))
22 // NewInt returns an OLITERAL representing v as an untyped integer.
23 func NewInt(pos src.XPos, v int64) Node {
24 return NewBasicLit(pos, types.UntypedInt, constant.MakeInt64(v))
27 // NewString returns an OLITERAL representing s as an untyped string.
28 func NewString(pos src.XPos, s string) Node {
29 return NewBasicLit(pos, types.UntypedString, constant.MakeString(s))
32 // NewUintptr returns an OLITERAL representing v as a uintptr.
33 func NewUintptr(pos src.XPos, v int64) Node {
34 return NewBasicLit(pos, types.Types[types.TUINTPTR], constant.MakeInt64(v))
37 // NewOne returns an OLITERAL representing 1 with the given type.
38 func NewOne(pos src.XPos, typ *types.Type) Node {
39 var val constant.Value
48 base.FatalfAt(pos, "%v cannot represent 1", typ)
51 return NewBasicLit(pos, typ, val)
55 intOne = constant.MakeInt64(1)
56 floatOne = constant.ToFloat(intOne)
57 complexOne = constant.ToComplex(intOne)
61 // Maximum size in bits for big.Ints before signaling
62 // overflow and also mantissa precision for big.Floats.
66 func BigFloat(v constant.Value) *big.Float {
69 switch u := constant.Val(v).(type) {
79 base.Fatalf("unexpected: %v", u)
84 // ConstOverflow reports whether constant value v is too large
85 // to represent with type t.
86 func ConstOverflow(v constant.Value, t *types.Type) bool {
89 bits := uint(8 * t.Size())
91 x, ok := constant.Uint64Val(v)
92 return !ok || x>>bits != 0
94 x, ok := constant.Int64Val(v)
98 return !ok || x>>(bits-1) != 0
102 f, _ := constant.Float32Val(v)
103 return math.IsInf(float64(f), 0)
105 f, _ := constant.Float64Val(v)
106 return math.IsInf(f, 0)
109 ft := types.FloatForComplex(t)
110 return ConstOverflow(constant.Real(v), ft) || ConstOverflow(constant.Imag(v), ft)
112 base.Fatalf("ConstOverflow: %v, %v", v, t)
116 // IsConstNode reports whether n is a Go language constant (as opposed to a
117 // compile-time constant).
119 // Expressions derived from nil, like string([]byte(nil)), while they
120 // may be known at compile time, are not Go language constants.
121 func IsConstNode(n Node) bool {
122 return n.Op() == OLITERAL
125 func IsSmallIntConst(n Node) bool {
126 if n.Op() == OLITERAL {
127 v, ok := constant.Int64Val(n.Val())
128 return ok && int64(int32(v)) == v