Commit 58f78727 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent af3ac0d2
...@@ -267,6 +267,7 @@ type CodeGenerator interface { ...@@ -267,6 +267,7 @@ type CodeGenerator interface {
// common part of codegenerators // common part of codegenerators
type commonCodeGen struct { type commonCodeGen struct {
buf Buffer // code is emitted here
recvName string // receiver/type for top-level func recvName string // receiver/type for top-level func
typeName string // or empty typeName string // or empty
typ types.Type typ types.Type
...@@ -274,6 +275,10 @@ type commonCodeGen struct { ...@@ -274,6 +275,10 @@ type commonCodeGen struct {
varUsed map[string]bool // whether a variable was used varUsed map[string]bool // whether a variable was used
} }
func (c *commonCodeGen) emit(format string, a ...interface{}) {
c.buf.emit(format, a...)
}
func (c *commonCodeGen) setFunc(recvName, typeName string, typ types.Type) { func (c *commonCodeGen) setFunc(recvName, typeName string, typ types.Type) {
c.recvName = recvName c.recvName = recvName
c.typeName = typeName c.typeName = typeName
...@@ -341,26 +346,23 @@ func (s *SymSize) Reset() { ...@@ -341,26 +346,23 @@ func (s *SymSize) Reset() {
// XXX naming ok? // XXX naming ok?
// XXX -> Gen_NEOEncodedLen ? // XXX -> Gen_NEOEncodedLen ?
type sizeCodeGen struct { type sizeCodeGen struct {
Buffer // buffer for code
size SymSize // currently accumulated packet size
commonCodeGen commonCodeGen
size SymSize // currently accumulated packet size
} }
// encoder generates code to encode a packet // encoder generates code to encode a packet
type encoder struct { type encoder struct {
Buffer // XXX
n int // current write position in data
commonCodeGen commonCodeGen
n int // current write position in data
} }
// decoder generates code to decode a packet // decoder generates code to decode a packet
type decoder struct { type decoder struct {
// buffers for generated code commonCodeGen
// current delayed overflow check will be inserted in between buf & bufCur
buf Buffer // done buffer for generated code
bufCur Buffer // current delayed overflow check will be inserted in between buf & bufDone
bufDone Buffer
// current read position in data. // current read position in data.
n int n int
...@@ -370,8 +372,6 @@ type decoder struct { ...@@ -370,8 +372,6 @@ type decoder struct {
// whether overflow was already checked for current decodings // whether overflow was already checked for current decodings
overflowChecked bool overflowChecked bool
commonCodeGen
} }
var _ CodeGenerator = (*sizeCodeGen)(nil) var _ CodeGenerator = (*sizeCodeGen)(nil)
...@@ -387,7 +387,7 @@ func (s *sizeCodeGen) generatedCode() string { ...@@ -387,7 +387,7 @@ func (s *sizeCodeGen) generatedCode() string {
code.emit("var %s int", s.var_("size")) code.emit("var %s int", s.var_("size"))
} }
code.Write(s.Bytes()) // XXX -> s.buf.Bytes() ? code.Write(s.buf.Bytes())
// epilogue // epilogue
size := s.size.String() size := s.size.String()
...@@ -405,7 +405,7 @@ func (e *encoder) generatedCode() string { ...@@ -405,7 +405,7 @@ func (e *encoder) generatedCode() string {
// prologue // prologue
code.emit("func (%s *%s) NEOEncode(data []byte) {", e.recvName, e.typeName) code.emit("func (%s *%s) NEOEncode(data []byte) {", e.recvName, e.typeName)
code.Write(e.Bytes()) // XXX -> e.buf.Bytes() ? code.Write(e.buf.Bytes())
// epilogue // epilogue
code.emit("}\n") code.emit("}\n")
...@@ -413,11 +413,6 @@ func (e *encoder) generatedCode() string { ...@@ -413,11 +413,6 @@ func (e *encoder) generatedCode() string {
return code.String() return code.String()
} }
// XXX place?
func (d *decoder) emit(format string, a ...interface{}) {
d.bufCur.emit(format, a...)
}
// XXX place? // XXX place?
// data <- data[pos:] // data <- data[pos:]
// pos <- 0 // pos <- 0
...@@ -448,13 +443,13 @@ func (d *decoder) resetPos() { ...@@ -448,13 +443,13 @@ func (d *decoder) resetPos() {
func (d *decoder) overflowCheckpoint() { func (d *decoder) overflowCheckpoint() {
//d.buf.emit("// overflow check point") //d.buf.emit("// overflow check point")
if !d.overflowCheckSize.IsZero() { if !d.overflowCheckSize.IsZero() {
d.buf.emit("if uint32(len(data)) < %v { goto overflow }", &d.overflowCheckSize) d.bufDone.emit("if uint32(len(data)) < %v { goto overflow }", &d.overflowCheckSize)
} }
d.overflowCheckSize.Reset() d.overflowCheckSize.Reset()
d.buf.Write(d.bufCur.Bytes()) d.bufDone.Write(d.buf.Bytes())
d.bufCur.Reset() d.buf.Reset()
} }
func (d *decoder) generatedCode() string { func (d *decoder) generatedCode() string {
...@@ -467,7 +462,7 @@ func (d *decoder) generatedCode() string { ...@@ -467,7 +462,7 @@ func (d *decoder) generatedCode() string {
code.emit("var %v uint32", d.var_("nread")) code.emit("var %v uint32", d.var_("nread"))
} }
code.Write(d.buf.Bytes()) code.Write(d.bufDone.Bytes())
// epilogue // epilogue
retexpr := fmt.Sprintf("%v", d.n) retexpr := fmt.Sprintf("%v", d.n)
......
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