Commit adf4c38b authored by Tamir Duberstein's avatar Tamir Duberstein Committed by Russ Cox

cmd/yacc: memory allocation improvements

Places a fixed size initial stack and the lval inside the parser
struct so that they are allocated together. Places $$char inside the
parser struct to avoid allocating the closure used in Lookahead().

Change-Id: I0de664a6d612279fdc3255633e2dff904030bc36
Reviewed-on: https://go-review.googlesource.com/16705Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
parent db4ef216
...@@ -172,7 +172,7 @@ func init() { ...@@ -172,7 +172,7 @@ func init() {
flag.BoolVar(&lflag, "l", false, "disable line directives") flag.BoolVar(&lflag, "l", false, "disable line directives")
} }
var stacksize = 200 var initialstacksize = 16
// communication variables between various I/O routines // communication variables between various I/O routines
var infile string // input file name var infile string // input file name
...@@ -384,7 +384,7 @@ func setup() { ...@@ -384,7 +384,7 @@ func setup() {
if flag.NArg() != 1 { if flag.NArg() != 1 {
usage() usage()
} }
if stacksize < 1 { if initialstacksize < 1 {
// never set so cannot happen // never set so cannot happen
fmt.Fprintf(stderr, "yacc: stack size too small\n") fmt.Fprintf(stderr, "yacc: stack size too small\n")
usage() usage()
...@@ -719,7 +719,7 @@ outer: ...@@ -719,7 +719,7 @@ outer:
ftable.WriteRune('\n') ftable.WriteRune('\n')
fmt.Fprintf(ftable, "const %sEofCode = 1\n", prefix) fmt.Fprintf(ftable, "const %sEofCode = 1\n", prefix)
fmt.Fprintf(ftable, "const %sErrCode = 2\n", prefix) fmt.Fprintf(ftable, "const %sErrCode = 2\n", prefix)
fmt.Fprintf(ftable, "const %sMaxDepth = %v\n", prefix, stacksize) fmt.Fprintf(ftable, "const %sInitialStackSize = %v\n", prefix, initialstacksize)
// //
// copy any postfix code // copy any postfix code
...@@ -3332,18 +3332,17 @@ type $$Parser interface { ...@@ -3332,18 +3332,17 @@ type $$Parser interface {
} }
type $$ParserImpl struct { type $$ParserImpl struct {
lookahead func() int lval $$SymType
stack [$$InitialStackSize]$$SymType
char int
} }
func (p *$$ParserImpl) Lookahead() int { func (p *$$ParserImpl) Lookahead() int {
return p.lookahead() return p.char
} }
func $$NewParser() $$Parser { func $$NewParser() $$Parser {
p := &$$ParserImpl{ return &$$ParserImpl{}
lookahead: func() int { return -1 },
}
return p
} }
const $$Flag = -1000 const $$Flag = -1000
...@@ -3471,22 +3470,20 @@ func $$Parse($$lex $$Lexer) int { ...@@ -3471,22 +3470,20 @@ func $$Parse($$lex $$Lexer) int {
func ($$rcvr *$$ParserImpl) Parse($$lex $$Lexer) int { func ($$rcvr *$$ParserImpl) Parse($$lex $$Lexer) int {
var $$n int var $$n int
var $$lval $$SymType
var $$VAL $$SymType var $$VAL $$SymType
var $$Dollar []$$SymType var $$Dollar []$$SymType
_ = $$Dollar // silence set and not used _ = $$Dollar // silence set and not used
$$S := make([]$$SymType, $$MaxDepth) $$S := $$rcvr.stack[:]
Nerrs := 0 /* number of errors */ Nerrs := 0 /* number of errors */
Errflag := 0 /* error recovery flag */ Errflag := 0 /* error recovery flag */
$$state := 0 $$state := 0
$$char := -1 $$rcvr.char = -1
$$token := -1 // $$char translated into internal numbering $$token := -1 // $$rcvr.char translated into internal numbering
$$rcvr.lookahead = func() int { return $$char }
defer func() { defer func() {
// Make sure we report no lookahead when not parsing. // Make sure we report no lookahead when not parsing.
$$state = -1 $$state = -1
$$char = -1 $$rcvr.char = -1
$$token = -1 $$token = -1
}() }()
$$p := -1 $$p := -1
...@@ -3518,8 +3515,8 @@ $$newstate: ...@@ -3518,8 +3515,8 @@ $$newstate:
if $$n <= $$Flag { if $$n <= $$Flag {
goto $$default /* simple state */ goto $$default /* simple state */
} }
if $$char < 0 { if $$rcvr.char < 0 {
$$char, $$token = $$lex1($$lex, &$$lval) $$rcvr.char, $$token = $$lex1($$lex, &$$rcvr.lval)
} }
$$n += $$token $$n += $$token
if $$n < 0 || $$n >= $$Last { if $$n < 0 || $$n >= $$Last {
...@@ -3527,9 +3524,9 @@ $$newstate: ...@@ -3527,9 +3524,9 @@ $$newstate:
} }
$$n = $$Act[$$n] $$n = $$Act[$$n]
if $$Chk[$$n] == $$token { /* valid shift */ if $$Chk[$$n] == $$token { /* valid shift */
$$char = -1 $$rcvr.char = -1
$$token = -1 $$token = -1
$$VAL = $$lval $$VAL = $$rcvr.lval
$$state = $$n $$state = $$n
if Errflag > 0 { if Errflag > 0 {
Errflag-- Errflag--
...@@ -3541,8 +3538,8 @@ $$default: ...@@ -3541,8 +3538,8 @@ $$default:
/* default state action */ /* default state action */
$$n = $$Def[$$state] $$n = $$Def[$$state]
if $$n == -2 { if $$n == -2 {
if $$char < 0 { if $$rcvr.char < 0 {
$$char, $$token = $$lex1($$lex, &$$lval) $$rcvr.char, $$token = $$lex1($$lex, &$$rcvr.lval)
} }
/* look through exception table */ /* look through exception table */
...@@ -3605,7 +3602,7 @@ $$default: ...@@ -3605,7 +3602,7 @@ $$default:
if $$token == $$EofCode { if $$token == $$EofCode {
goto ret1 goto ret1
} }
$$char = -1 $$rcvr.char = -1
$$token = -1 $$token = -1
goto $$newstate /* try again in the same state */ goto $$newstate /* try again in the same state */
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment