Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions internal/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package compiler

import (
"fmt"
"math"
"math/big"
"strconv"
"strings"
Expand All @@ -26,10 +27,13 @@ func (e compileError) Error() string { return e.msg }
// links to the enclosing builder and isBlock is true, so local resolution can
// reach enclosing locals by depth.
type builder struct {
name string
insns []bytecode.Instr
consts []object.Value
constIdx map[object.Value]int
name string
insns []bytecode.Instr
consts []object.Value
constIdx map[object.Value]int
// floatIdx pools Float literals by their bits rather than by their value;
// see addConst for why they cannot share constIdx.
floatIdx map[uint64]int
names []string
locals []string
params []string
Expand All @@ -45,7 +49,7 @@ type builder struct {
}

func newBuilder(name string, params []string) *builder {
b := &builder{name: name, constIdx: map[object.Value]int{}, params: params, numRequired: len(params), splatIndex: -1, kwRestSlot: -1, blockSlot: -1}
b := &builder{name: name, constIdx: map[object.Value]int{}, floatIdx: map[uint64]int{}, params: params, numRequired: len(params), splatIndex: -1, kwRestSlot: -1, blockSlot: -1}
for _, p := range params {
b.localSlot(p) // params occupy slots 0..n-1, in order
}
Expand Down Expand Up @@ -83,6 +87,25 @@ func (b *builder) here() int { return len(b.insns) }
func (b *builder) patch(at, target int) { b.insns[at].A = target }

func (b *builder) addConst(v object.Value) int {
// A Float is pooled by its bits, not by its value. Two floats can be equal
// and not be the same number: -0.0 == 0.0 is true and the two hash alike,
// so a map keyed on the value hands the second literal the first one's
// slot — `p 0.0` followed by `p(-0.0)` printed 0.0 twice, and reversing
// the two lines printed -0.0 twice. Whichever the file mentioned first won.
//
// NaN is the mirror image: it is equal to nothing, itself included, so it
// matched no entry and took a fresh slot every time it appeared. Bits pool
// both correctly.
if f, ok := v.(object.Float); ok {
bits := math.Float64bits(float64(f))
if i, ok := b.floatIdx[bits]; ok {
return i
}
i := len(b.consts)
b.consts = append(b.consts, v)
b.floatIdx[bits] = i
return i
}
if i, ok := b.constIdx[v]; ok {
return i
}
Expand Down
40 changes: 40 additions & 0 deletions internal/compiler/whitebox_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package compiler

import (
"math"
"strings"
"testing"

Expand Down Expand Up @@ -301,3 +302,42 @@ func TestRewriteAnonKwSplatUnchanged(t *testing.T) {
t.Fatalf("rewriteAnonKwSplat(explicit hash) = (%p, %v), want (%p, false)", got, changed, h)
}
}

// TestAddConstPoolsFloatsByBits pins the reason Float literals do not share the
// value-keyed constant pool. Two floats can be equal without being the same
// number, and one can be unequal to itself, so equality is the wrong question
// to ask of a literal that has to survive to run time exactly as written.
func TestAddConstPoolsFloatsByBits(t *testing.T) {
b := newBuilder("<t>", nil)
negZero := object.Float(math.Copysign(0, -1))
posZero := object.Float(0)
nan := object.Float(math.NaN())

// -0.0 == 0.0, so a value-keyed pool hands the second literal the first
// one's slot and the file's later mention prints as its earlier one.
i, j := b.addConst(posZero), b.addConst(negZero)
if i == j {
t.Fatalf("0.0 and -0.0 share slot %d; they are equal but not the same number", i)
}
if got := math.Signbit(float64(b.consts[i].(object.Float))); got {
t.Errorf("slot %d holds a negative zero, want positive", i)
}
if got := math.Signbit(float64(b.consts[j].(object.Float))); !got {
t.Errorf("slot %d holds a positive zero, want negative", j)
}

// The same number asked for twice keeps one slot, which is the whole point
// of pooling — including NaN, which is equal to nothing, itself included,
// and so took a fresh slot on every mention.
if a, c := b.addConst(posZero), b.addConst(posZero); a != c || a != i {
t.Errorf("0.0 pooled to %d then %d, want %d both times", a, c, i)
}
if a, c := b.addConst(nan), b.addConst(nan); a != c {
t.Errorf("NaN took slots %d and %d, want one slot", a, c)
}

// Non-Float constants still go through the value-keyed pool.
if a, c := b.addConst(object.Integer(7)), b.addConst(object.Integer(7)); a != c {
t.Errorf("Integer(7) took slots %d and %d, want one slot", a, c)
}
}
23 changes: 23 additions & 0 deletions internal/vm/literals_extra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,26 @@ func TestFloatExponentLiterals(t *testing.T) {
}
}
}

// TestFloatLiteralZeroSigns covers what the compiler's constant pool must not
// lose: a zero's sign, and a NaN's identity as a fresh constant. Both are
// literals whose printed form differs from a literal they compare equal (or
// unequal) to, so a pool keyed on the value gets them wrong — whichever of
// 0.0 / -0.0 the file mentioned first used to win both lines. Asserted
// against MRI Ruby 4.0.5.
func TestFloatLiteralZeroSigns(t *testing.T) {
cases := []struct{ src, want string }{
{"p 0.0\np(-0.0)", "0.0\n-0.0\n"},
{"p(-0.0)\np 0.0", "-0.0\n0.0\n"},
{`p [0.0, -0.0]`, "[0.0, -0.0]\n"},
{`p 0.0 == -0.0`, "true\n"}, // equal, and still not interchangeable
{`p (0.0).zero? && (-0.0).zero?`, "true\n"},
{`p 1.0 / -0.0`, "-Infinity\n"}, // the sign is load-bearing, not cosmetic
{`p 1.0 / 0.0`, "Infinity\n"},
}
for _, c := range cases {
if got := eval(t, c.src); got != c.want {
t.Errorf("src=%q got=%q want=%q", c.src, got, c.want)
}
}
}