forked from TrueCloudLab/neoneo-go
compiler: do not allocate static slot for constants
Their value is known at compile time.
This commit is contained in:
parent
18204ec21a
commit
0b44a43043
2 changed files with 24 additions and 2 deletions
|
@ -109,8 +109,13 @@ func countGlobals(f ast.Node) (i int) {
|
||||||
return false
|
return false
|
||||||
// After skipping all funcDecls we are sure that each value spec
|
// After skipping all funcDecls we are sure that each value spec
|
||||||
// is a global declared variable or constant.
|
// is a global declared variable or constant.
|
||||||
case *ast.ValueSpec:
|
case *ast.GenDecl:
|
||||||
i += len(n.Names)
|
if n.Tok == token.VAR {
|
||||||
|
for _, s := range n.Specs {
|
||||||
|
i += len(s.(*ast.ValueSpec).Names)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package compiler_test
|
package compiler_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -238,3 +239,19 @@ func TestMultipleFuncSameName(t *testing.T) {
|
||||||
eval(t, src, big.NewInt(42))
|
eval(t, src, big.NewInt(42))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConstDontUseSlots(t *testing.T) {
|
||||||
|
const count = 256
|
||||||
|
buf := bytes.NewBufferString("package foo\n")
|
||||||
|
for i := 0; i < count; i++ {
|
||||||
|
buf.WriteString(fmt.Sprintf("const n%d = 1\n", i))
|
||||||
|
}
|
||||||
|
buf.WriteString("func Main() int { sum := 0\n")
|
||||||
|
for i := 0; i < count; i++ {
|
||||||
|
buf.WriteString(fmt.Sprintf("sum += n%d\n", i))
|
||||||
|
}
|
||||||
|
buf.WriteString("return sum }")
|
||||||
|
|
||||||
|
src := buf.String()
|
||||||
|
eval(t, src, big.NewInt(count))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue