Commit cd42a2aa authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 4a177116
This diff is collapsed.
...@@ -31,13 +31,13 @@ import ( ...@@ -31,13 +31,13 @@ import (
"go/types" "go/types"
"log" "log"
"os" "os"
"strings"
) )
// parsed & typechecked input // parsed & typechecked input
var fset = token.NewFileSet() var fset = token.NewFileSet()
var info = &types.Info{ var info = &types.Info{
Types: make(map[ast.Expr]types.TypeAndValue), Types: make(map[ast.Expr]types.TypeAndValue),
//Uses: make(map[*ast.Ident]types.Object), XXX seems not needed
Defs: make(map[*ast.Ident]types.Object), Defs: make(map[*ast.Ident]types.Object),
} }
...@@ -226,12 +226,12 @@ type CodecCodeGen interface { ...@@ -226,12 +226,12 @@ type CodecCodeGen interface {
generatedCode() string generatedCode() string
} }
// encode/decode codegen // sizer/encode/decode codegen
type sizer struct { type sizer struct {
Buffer // XXX Buffer // XXX
n int n int
symLenv []string // symbolic lengths TODO symLenv []string // symbolic lengths to add to size
sizeVarUsed bool // whether size var was used varSizeUsed bool // whether var size was used
} }
type encoder struct { type encoder struct {
...@@ -262,7 +262,7 @@ func (d *decoder) generatedCode() string { ...@@ -262,7 +262,7 @@ func (d *decoder) generatedCode() string {
func (s *sizer) genPrologue(recvName, typeName string) { func (s *sizer) genPrologue(recvName, typeName string) {
s.emit("func (%s *%s) NEOEncodedLen() int {", recvName, typeName) s.emit("func (%s *%s) NEOEncodedLen() int {", recvName, typeName)
if s.sizeVarUsed { if s.varSizeUsed {
s.emit("var size int") s.emit("var size int")
} }
} }
...@@ -279,10 +279,10 @@ func (d *decoder) genPrologue(recvName, typeName string) { ...@@ -279,10 +279,10 @@ func (d *decoder) genPrologue(recvName, typeName string) {
func (s *sizer) genEpilogue() { func (s *sizer) genEpilogue() {
size := fmt.Sprintf("%v", s.n) size := fmt.Sprintf("%v", s.n)
if len(s.symLenv) > 0 { if len(s.symLenv) > 0 {
size += " + " + strings.Join(s.synLenv, " + ") size += " + " + strings.Join(s.symLenv, " + ")
} }
if s.sizeVarUsed { if s.varSizeUsed {
size += " + size") size += " + size"
} }
s.emit("return %v", size) s.emit("return %v", size)
s.emit("}\n") s.emit("}\n")
...@@ -376,15 +376,19 @@ func (s *sizer) genSlice(path string, typ *types.Slice, obj types.Object) { ...@@ -376,15 +376,19 @@ func (s *sizer) genSlice(path string, typ *types.Slice, obj types.Object) {
// if size(item)==const - size update in one go // if size(item)==const - size update in one go
elemSize, ok := typeSizeFixed(typ.Elem()) elemSize, ok := typeSizeFixed(typ.Elem())
if ok { if ok {
s.emit("size += 4 + len(%v) * %v", path, elemSize) s.n += 4
s.symLenv = append(s.symLenv, fmt.Sprintf("len(%v) * %v", path, elemSize))
return return
} }
s.emit("size += %v + 4", s.n) s.varSizeUsed = true
s.n += 4
s.emit("size += %v", s.n)
s.n = 0 s.n = 0
s.emit("for i := 0; i < len(%v); i++ {", path) s.emit("for i := 0; i < len(%v); i++ {", path)
s.emit("a := &%s[i]", path) s.emit("a := &%s[i]", path)
codegenType("(*a)", typ.Elem(), obj, s) //codegenType("(*a)", typ.Elem(), obj, s)
codegenType("(*a)", typ.Elem(), obj, &sizer{})
s.emit("size += %v", s.n) s.emit("size += %v", s.n)
s.emit("}") s.emit("}")
s.n = 0 s.n = 0
......
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