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
// on any expression that is not concrete (or contains any sub-expression
// that is not concrete).
Eval() uint
Eval() int
// Reduce attempts to evaluate any concrete sub-expressions.
// i.e., (1 + 2 * (5 + 1 + someSizeOfStruct) reduces to
@ -45,7 +45,7 @@ func (e *Function) Concrete() bool {
return false
}
func (e *Function) Eval() uint {
func (e *Function) Eval() int {
log.Fatalf("Cannot evaluate a 'Function'. It is not concrete.")
panic("unreachable")
}
@ -95,7 +95,7 @@ func (e *BinaryOp) Concrete() bool {
return e.Expr1.Concrete() && e.Expr2.Concrete()
}
func (e *BinaryOp) Eval() uint {
func (e *BinaryOp) Eval() int {
switch e.Op {
case "+":
return e.Expr1.Eval() + e.Expr2.Eval()
@ -108,7 +108,7 @@ func (e *BinaryOp) Eval() uint {
case "&":
return e.Expr1.Eval() & e.Expr2.Eval()
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)
@ -161,7 +161,7 @@ func (e *UnaryOp) Concrete() bool {
return e.Expr.Concrete()
}
func (e *UnaryOp) Eval() uint {
func (e *UnaryOp) Eval() int {
switch e.Op {
case "~":
return ^e.Expr.Eval()
@ -196,8 +196,8 @@ func (e *Padding) Concrete() bool {
return e.Expr.Concrete()
}
func (e *Padding) Eval() uint {
return uint(pad(int(e.Expr.Eval())))
func (e *Padding) Eval() int {
return pad(e.Expr.Eval())
}
func (e *Padding) Reduce(prefix string) string {
@ -225,8 +225,8 @@ func (e *PopCount) Concrete() bool {
return e.Expr.Concrete()
}
func (e *PopCount) Eval() uint {
return popCount(e.Expr.Eval())
func (e *PopCount) Eval() int {
return int(popCount(uint(e.Expr.Eval())))
}
func (e *PopCount) Reduce(prefix string) string {
@ -246,14 +246,14 @@ func (e *PopCount) Initialize(p *Protocol) {
// Value represents some constant integer.
type Value struct {
v uint
v int
}
func (e *Value) Concrete() bool {
return true
}
func (e *Value) Eval() uint {
func (e *Value) Eval() int {
return e.v
}
@ -269,15 +269,15 @@ func (e *Value) Initialize(p *Protocol) {}
// Bit represents some bit whose value is computed by '1 << bit'.
type Bit struct {
b uint
b int
}
func (e *Bit) Concrete() bool {
return true
}
func (e *Bit) Eval() uint {
return 1 << e.b
func (e *Bit) Eval() int {
return int(1 << uint(e.b))
}
func (e *Bit) Reduce(prefix string) string {
@ -300,7 +300,7 @@ func (e *FieldRef) Concrete() bool {
return false
}
func (e *FieldRef) Eval() uint {
func (e *FieldRef) Eval() int {
log.Fatalf("Cannot evaluate a 'FieldRef'. It is not concrete.")
panic("unreachable")
}
@ -333,7 +333,7 @@ func (e *EnumRef) Concrete() bool {
return false
}
func (e *EnumRef) Eval() uint {
func (e *EnumRef) Eval() int {
log.Fatalf("Cannot evaluate an 'EnumRef'. It is not concrete.")
panic("unreachable")
}
@ -361,7 +361,7 @@ func (e *SumOf) Concrete() bool {
return false
}
func (e *SumOf) Eval() uint {
func (e *SumOf) Eval() int {
log.Fatalf("Cannot evaluate a 'SumOf'. It is not concrete.")
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
// size in this case can only be computed recursively).
func (f *ListField) Size() Size {
simpleLen := &Function{
Name: "pad",
simpleLen := &Padding{
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
func (f *ValueField) Define(c *Context) {
c.Putln("// valueparam field: type: %s, mask name: %s, list name: %s",
f.MaskType.SrcName(), f.MaskName, f.ListName)
panic("todo")
c.Putln("%s %s", f.MaskName, f.SrcType())
c.Putln("%s []uint32", f.ListName)
}
func (f *ValueField) Read(c *Context, prefix string) {
c.Putln("// reading valueparam: type: %s, mask name: %s, list name: %s",
f.MaskType.SrcName(), f.MaskName, f.ListName)
panic("todo")
ReadSimpleSingleField(c,
fmt.Sprintf("%s%s", prefix, f.MaskName), f.MaskType)
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) {

View File

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

View File

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

View File

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