more bug fixes for the rest of the extensions

This commit is contained in:
Andrew Gallant (Ocelot) 2012-05-06 03:06:02 -04:00
parent 014a0598bf
commit ea30f1a0a7
10 changed files with 41 additions and 32 deletions

View File

@ -18,7 +18,7 @@ type Expression interface {
// Eval evaluates a concrete expression. It is an error to call Eval // Eval evaluates a concrete expression. It is an error to call Eval
// on any expression that is not concrete (or contains any sub-expression // on any expression that is not concrete (or contains any sub-expression
// that is not concrete). // that is not concrete).
Eval() uint Eval() int
// Reduce attempts to evaluate any concrete sub-expressions. // Reduce attempts to evaluate any concrete sub-expressions.
// i.e., (1 + 2 * (5 + 1 + someSizeOfStruct) reduces to // i.e., (1 + 2 * (5 + 1 + someSizeOfStruct) reduces to
@ -45,7 +45,7 @@ func (e *Function) Concrete() bool {
return false return false
} }
func (e *Function) Eval() uint { func (e *Function) Eval() int {
log.Fatalf("Cannot evaluate a 'Function'. It is not concrete.") log.Fatalf("Cannot evaluate a 'Function'. It is not concrete.")
panic("unreachable") panic("unreachable")
} }
@ -95,7 +95,7 @@ func (e *BinaryOp) Concrete() bool {
return e.Expr1.Concrete() && e.Expr2.Concrete() return e.Expr1.Concrete() && e.Expr2.Concrete()
} }
func (e *BinaryOp) Eval() uint { func (e *BinaryOp) Eval() int {
switch e.Op { switch e.Op {
case "+": case "+":
return e.Expr1.Eval() + e.Expr2.Eval() return e.Expr1.Eval() + e.Expr2.Eval()
@ -108,7 +108,7 @@ func (e *BinaryOp) Eval() uint {
case "&": case "&":
return e.Expr1.Eval() & e.Expr2.Eval() return e.Expr1.Eval() & e.Expr2.Eval()
case "<<": case "<<":
return e.Expr1.Eval() << e.Expr2.Eval() return int(uint(e.Expr1.Eval()) << uint(e.Expr2.Eval()))
} }
log.Fatalf("Invalid binary operator '%s' for expression.", e.Op) log.Fatalf("Invalid binary operator '%s' for expression.", e.Op)
@ -161,7 +161,7 @@ func (e *UnaryOp) Concrete() bool {
return e.Expr.Concrete() return e.Expr.Concrete()
} }
func (e *UnaryOp) Eval() uint { func (e *UnaryOp) Eval() int {
switch e.Op { switch e.Op {
case "~": case "~":
return ^e.Expr.Eval() return ^e.Expr.Eval()
@ -196,8 +196,8 @@ func (e *Padding) Concrete() bool {
return e.Expr.Concrete() return e.Expr.Concrete()
} }
func (e *Padding) Eval() uint { func (e *Padding) Eval() int {
return uint(pad(int(e.Expr.Eval()))) return pad(e.Expr.Eval())
} }
func (e *Padding) Reduce(prefix string) string { func (e *Padding) Reduce(prefix string) string {
@ -225,8 +225,8 @@ func (e *PopCount) Concrete() bool {
return e.Expr.Concrete() return e.Expr.Concrete()
} }
func (e *PopCount) Eval() uint { func (e *PopCount) Eval() int {
return popCount(e.Expr.Eval()) return int(popCount(uint(e.Expr.Eval())))
} }
func (e *PopCount) Reduce(prefix string) string { func (e *PopCount) Reduce(prefix string) string {
@ -246,14 +246,14 @@ func (e *PopCount) Initialize(p *Protocol) {
// Value represents some constant integer. // Value represents some constant integer.
type Value struct { type Value struct {
v uint v int
} }
func (e *Value) Concrete() bool { func (e *Value) Concrete() bool {
return true return true
} }
func (e *Value) Eval() uint { func (e *Value) Eval() int {
return e.v return e.v
} }
@ -269,15 +269,15 @@ func (e *Value) Initialize(p *Protocol) {}
// Bit represents some bit whose value is computed by '1 << bit'. // Bit represents some bit whose value is computed by '1 << bit'.
type Bit struct { type Bit struct {
b uint b int
} }
func (e *Bit) Concrete() bool { func (e *Bit) Concrete() bool {
return true return true
} }
func (e *Bit) Eval() uint { func (e *Bit) Eval() int {
return 1 << e.b return int(1 << uint(e.b))
} }
func (e *Bit) Reduce(prefix string) string { func (e *Bit) Reduce(prefix string) string {
@ -300,7 +300,7 @@ func (e *FieldRef) Concrete() bool {
return false return false
} }
func (e *FieldRef) Eval() uint { func (e *FieldRef) Eval() int {
log.Fatalf("Cannot evaluate a 'FieldRef'. It is not concrete.") log.Fatalf("Cannot evaluate a 'FieldRef'. It is not concrete.")
panic("unreachable") panic("unreachable")
} }
@ -333,7 +333,7 @@ func (e *EnumRef) Concrete() bool {
return false return false
} }
func (e *EnumRef) Eval() uint { func (e *EnumRef) Eval() int {
log.Fatalf("Cannot evaluate an 'EnumRef'. It is not concrete.") log.Fatalf("Cannot evaluate an 'EnumRef'. It is not concrete.")
panic("unreachable") panic("unreachable")
} }
@ -361,7 +361,7 @@ func (e *SumOf) Concrete() bool {
return false return false
} }
func (e *SumOf) Eval() uint { func (e *SumOf) Eval() int {
log.Fatalf("Cannot evaluate a 'SumOf'. It is not concrete.") log.Fatalf("Cannot evaluate a 'SumOf'. It is not concrete.")
panic("unreachable") panic("unreachable")
} }

View File

@ -142,8 +142,7 @@ func (f *ListField) Length() Size {
// special function written in go_struct.go to compute the size (since the // special function written in go_struct.go to compute the size (since the
// size in this case can only be computed recursively). // size in this case can only be computed recursively).
func (f *ListField) Size() Size { func (f *ListField) Size() Size {
simpleLen := &Function{ simpleLen := &Padding{
Name: "pad",
Expr: newBinaryOp("*", f.Length().Expression, f.Type.Size().Expression), Expr: newBinaryOp("*", f.Length().Expression, f.Type.Size().Expression),
} }

View File

@ -150,15 +150,21 @@ func (f *ExprField) Write(c *Context, prefix string) {
// Value field // Value field
func (f *ValueField) Define(c *Context) { func (f *ValueField) Define(c *Context) {
c.Putln("// valueparam field: type: %s, mask name: %s, list name: %s", c.Putln("%s %s", f.MaskName, f.SrcType())
f.MaskType.SrcName(), f.MaskName, f.ListName) c.Putln("%s []uint32", f.ListName)
panic("todo")
} }
func (f *ValueField) Read(c *Context, prefix string) { func (f *ValueField) Read(c *Context, prefix string) {
c.Putln("// reading valueparam: type: %s, mask name: %s, list name: %s", ReadSimpleSingleField(c,
f.MaskType.SrcName(), f.MaskName, f.ListName) fmt.Sprintf("%s%s", prefix, f.MaskName), f.MaskType)
panic("todo") c.Putln("")
c.Putln("%s%s = make([]uint32, %s)",
prefix, f.ListName, f.ListLength().Reduce(prefix))
c.Putln("for i := 0; i < %s; i++ {", f.ListLength().Reduce(prefix))
c.Putln("%s%s[i] = Get32(buf[b:])", prefix, f.ListName)
c.Putln("b += 4")
c.Putln("}")
c.Putln("b = pad(b)")
} }
func (f *ValueField) Write(c *Context, prefix string) { func (f *ValueField) Write(c *Context, prefix string) {

View File

@ -104,7 +104,11 @@ func (s *Struct) WriteListSize(c *Context) {
c.Putln("// Struct list size %s", s.SrcName()) c.Putln("// Struct list size %s", s.SrcName())
c.Putln("func %sListSize(list []%s) int {", s.SrcName(), s.SrcName()) c.Putln("func %sListSize(list []%s) int {", s.SrcName(), s.SrcName())
c.Putln("size := 0") c.Putln("size := 0")
if s.Size().Expression.Concrete() {
c.Putln("for _ = range list {")
} else {
c.Putln("for _, item := range list {") c.Putln("for _, item := range list {")
}
c.Putln("size += %s", s.Size().Reduce("item.")) c.Putln("size += %s", s.Size().Reduce("item."))
c.Putln("}") c.Putln("}")
c.Putln("return size") c.Putln("return size")

View File

@ -11,7 +11,7 @@ type Size struct {
// newFixedSize creates a new Size with some fixed and known value. // newFixedSize creates a new Size with some fixed and known value.
func newFixedSize(fixed uint) Size { func newFixedSize(fixed uint) Size {
return Size{&Value{v: fixed}} return Size{&Value{v: int(fixed)}}
} }
// newExpressionSize creates a new Size with some expression. // newExpressionSize creates a new Size with some expression.

View File

@ -89,7 +89,7 @@ func (xml *XML) Translate() *Protocol {
if !ok { if !ok {
continue continue
} }
nextValue := uint(0) nextValue := 0
for _, item := range enum.Items { for _, item := range enum.Items {
if item.Expr == nil { if item.Expr == nil {
item.Expr = &Value{v: nextValue} item.Expr = &Value{v: nextValue}
@ -267,16 +267,16 @@ func (x *XMLExpression) Translate() Expression {
Expr: x.Exprs[0].Translate(), Expr: x.Exprs[0].Translate(),
} }
case "value": case "value":
val, err := strconv.Atoi(x.Data) val, err := strconv.Atoi(strings.TrimSpace(x.Data))
if err != nil { if err != nil {
log.Panicf("Could not convert '%s' in 'value' expression to int.", log.Panicf("Could not convert '%s' in 'value' expression to int.",
x.Data) x.Data)
} }
return &Value{ return &Value{
v: uint(val), v: val,
} }
case "bit": case "bit":
bit, err := strconv.Atoi(x.Data) bit, err := strconv.Atoi(strings.TrimSpace(x.Data))
if err != nil { if err != nil {
log.Panicf("Could not convert '%s' in 'bit' expression to int.", log.Panicf("Could not convert '%s' in 'bit' expression to int.",
x.Data) x.Data)
@ -286,7 +286,7 @@ func (x *XMLExpression) Translate() Expression {
" is %d", bit) " is %d", bit)
} }
return &Bit{ return &Bit{
b: uint(bit), b: bit,
} }
case "fieldref": case "fieldref":
return &FieldRef{ return &FieldRef{