aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2016-10-29 23:42:36 +0200
committerDmitry Vyukov <dvyukov@google.com>2016-11-11 14:31:55 -0800
commitb40d502736438fcd899cda22e92fd0a159eecf4f (patch)
tree7c8998ec46220f2e42f34df34d28306c7661bc99
parent1838728cc121fd6c4c6f52d9c837f1e32e0c86f4 (diff)
prog: remote Type argument from Arg.Size/Value
They are not necessary since we now always have types attached to args. Also remove sys.Type.InnerType as it is not necessary now as well.
-rw-r--r--prog/analysis.go21
-rw-r--r--prog/encodingexec.go14
-rw-r--r--prog/mutation.go8
-rw-r--r--prog/prog.go39
-rw-r--r--prog/rand.go4
-rw-r--r--sys/decl.go57
6 files changed, 41 insertions, 102 deletions
diff --git a/prog/analysis.go b/prog/analysis.go
index f5219895d..98973d4c7 100644
--- a/prog/analysis.go
+++ b/prog/analysis.go
@@ -160,12 +160,12 @@ func generateSize(arg *Arg, lenType *sys.LenType) *Arg {
return pageSizeArg(lenType, arg.AddrPagesNum, 0)
case *sys.ArrayType:
if lenType.ByteSize {
- return constArg(lenType, arg.Size(arg.Type))
+ return constArg(lenType, arg.Size())
} else {
return constArg(lenType, uintptr(len(arg.Inner)))
}
default:
- return constArg(lenType, arg.Size(arg.Type))
+ return constArg(lenType, arg.Size())
}
}
@@ -174,22 +174,19 @@ func assignSizes(args []*Arg) {
argsMap := make(map[string]*Arg)
var parentSize uintptr
for _, arg := range args {
- parentSize += arg.Size(arg.Type)
+ parentSize += arg.Size()
if sys.IsPad(arg.Type) {
continue
}
- argsMap[arg.Type.Name()] = arg.InnerArg(arg.Type)
+ argsMap[arg.Type.Name()] = arg
}
// Fill in size arguments.
for _, arg := range args {
- if typ, ok := arg.Type.InnerType().(*sys.LenType); ok {
- arg = arg.InnerArg(arg.Type)
- if arg == nil {
- // Pointer to optional len field, no need to fill in value.
- continue
- }
-
+ if arg = arg.InnerArg(); arg == nil {
+ continue // Pointer to optional len field, no need to fill in value.
+ }
+ if typ, ok := arg.Type.(*sys.LenType); ok {
if typ.Buf == "parent" {
arg.Val = parentSize
continue
@@ -201,7 +198,7 @@ func assignSizes(args []*Arg) {
typ.Name(), typ.Buf, argsMap))
}
- *arg = *generateSize(buf, typ)
+ *arg = *generateSize(buf.InnerArg(), typ)
}
}
}
diff --git a/prog/encodingexec.go b/prog/encodingexec.go
index f30cb7073..4c2cd2524 100644
--- a/prog/encodingexec.go
+++ b/prog/encodingexec.go
@@ -46,7 +46,7 @@ func (p *Prog) SerializeForExec() []byte {
w.args[base] = &argInfo{}
}
w.args[arg] = &argInfo{Offset: w.args[base].CurSize}
- w.args[base].CurSize += arg.Size(arg.Type)
+ w.args[base].CurSize += arg.Size()
})
// Generate copyin instructions that fill in data into pointer arguments.
foreachArg(c, func(arg, _ *Arg, _ *[]*Arg) {
@@ -105,7 +105,7 @@ func (p *Prog) SerializeForExec() []byte {
instrSeq++
w.write(ExecInstrCopyout)
w.write(physicalAddr(base) + info.Offset)
- w.write(arg.Size(arg.Type))
+ w.write(arg.Size())
default:
panic("bad arg kind in copyout")
}
@@ -147,21 +147,21 @@ func (w *execContext) writeArg(arg *Arg) {
switch arg.Kind {
case ArgConst:
w.write(ExecArgConst)
- w.write(arg.Size(arg.Type))
- w.write(arg.Value(arg.Type))
+ w.write(arg.Size())
+ w.write(arg.Value())
case ArgResult:
w.write(ExecArgResult)
- w.write(arg.Size(arg.Type))
+ w.write(arg.Size())
w.write(w.args[arg.Res].Idx)
w.write(arg.OpDiv)
w.write(arg.OpAdd)
case ArgPointer:
w.write(ExecArgConst)
- w.write(arg.Size(arg.Type))
+ w.write(arg.Size())
w.write(physicalAddr(arg))
case ArgPageSize:
w.write(ExecArgConst)
- w.write(arg.Size(arg.Type))
+ w.write(arg.Size())
w.write(arg.AddrPage * pageSize)
case ArgData:
w.write(ExecArgData)
diff --git a/prog/mutation.go b/prog/mutation.go
index c014381d7..da8ad83bf 100644
--- a/prog/mutation.go
+++ b/prog/mutation.go
@@ -57,7 +57,7 @@ func (p *Prog) Mutate(rs rand.Source, ncalls int, ct *ChoiceTable) {
if base.Kind != ArgPointer || base.Res == nil {
panic("bad base arg")
}
- baseSize = base.Res.Size(base.Res.Type)
+ baseSize = base.Res.Size()
}
switch a := arg.Type.(type) {
case *sys.IntType, *sys.FlagsType, *sys.FileoffType, *sys.ResourceType, *sys.VmaType:
@@ -146,7 +146,7 @@ func (p *Prog) Mutate(rs rand.Source, ncalls int, ct *ChoiceTable) {
// TODO: we don't know size for out args
size := uintptr(1)
if arg.Res != nil {
- size = arg.Res.Size(arg.Res.Type)
+ size = arg.Res.Size()
}
arg1, calls1 := r.addr(s, a, size, arg.Res)
p.replaceArg(c, arg, arg1, calls1)
@@ -178,8 +178,8 @@ func (p *Prog) Mutate(rs rand.Source, ncalls int, ct *ChoiceTable) {
}
// Update base pointer if size has increased.
- if base != nil && baseSize < base.Res.Size(base.Res.Type) {
- arg1, calls1 := r.addr(s, base.Type, base.Res.Size(base.Res.Type), base.Res)
+ if base != nil && baseSize < base.Res.Size() {
+ arg1, calls1 := r.addr(s, base.Type, base.Res.Size(), base.Res)
for _, c1 := range calls1 {
sanitizeCall(c1)
}
diff --git a/prog/prog.go b/prog/prog.go
index 47c99b1d5..26e7dc0b8 100644
--- a/prog/prog.go
+++ b/prog/prog.go
@@ -52,17 +52,16 @@ const (
)
// Returns inner arg for PtrType args
-func (a *Arg) InnerArg(typ sys.Type) *Arg {
- switch typ1 := typ.(type) {
+func (a *Arg) InnerArg() *Arg {
+ switch typ := a.Type.(type) {
case *sys.PtrType:
if a.Res == nil {
- if typ.Optional() {
- return nil
- } else {
- panic(fmt.Sprintf("non-optional pointer is nil\narg: %+v\ntype: %+v", a, typ1))
+ if !typ.Optional() {
+ panic(fmt.Sprintf("non-optional pointer is nil\narg: %+v\ntype: %+v", a, typ))
}
+ return nil
} else {
- return a.Res.InnerArg(typ1.Type)
+ return a.Res.InnerArg()
}
default:
return a
@@ -86,24 +85,24 @@ func encodeValue(value, size uintptr, bigEndian bool) uintptr {
}
// Returns value taking endianness into consideration.
-func (a *Arg) Value(typ sys.Type) uintptr {
- switch t := typ.(type) {
+func (a *Arg) Value() uintptr {
+ switch typ := a.Type.(type) {
case *sys.IntType:
- return encodeValue(a.Val, t.Size(), t.BigEndian)
+ return encodeValue(a.Val, typ.Size(), typ.BigEndian)
case *sys.ConstType:
- return encodeValue(a.Val, t.Size(), t.BigEndian)
+ return encodeValue(a.Val, typ.Size(), typ.BigEndian)
case *sys.FlagsType:
- return encodeValue(a.Val, t.Size(), t.BigEndian)
+ return encodeValue(a.Val, typ.Size(), typ.BigEndian)
case *sys.LenType:
- return encodeValue(a.Val, t.Size(), t.BigEndian)
+ return encodeValue(a.Val, typ.Size(), typ.BigEndian)
case *sys.FileoffType:
- return encodeValue(a.Val, t.Size(), t.BigEndian)
+ return encodeValue(a.Val, typ.Size(), typ.BigEndian)
}
return a.Val
}
-func (a *Arg) Size(typ sys.Type) uintptr {
- switch typ1 := typ.(type) {
+func (a *Arg) Size() uintptr {
+ switch typ := a.Type.(type) {
case *sys.IntType, *sys.LenType, *sys.FlagsType, *sys.ConstType, *sys.StrConstType,
*sys.FileoffType, *sys.ResourceType, *sys.VmaType, *sys.PtrType:
return typ.Size()
@@ -113,16 +112,16 @@ func (a *Arg) Size(typ sys.Type) uintptr {
return uintptr(len(a.Data))
case *sys.StructType:
var size uintptr
- for i, f := range typ1.Fields {
- size += a.Inner[i].Size(f)
+ for _, fld := range a.Inner {
+ size += fld.Size()
}
return size
case *sys.UnionType:
- return a.Option.Size(a.OptionType)
+ return a.Option.Size()
case *sys.ArrayType:
var size uintptr
for _, in := range a.Inner {
- size += in.Size(typ1.Type)
+ size += in.Size()
}
return size
default:
diff --git a/prog/rand.go b/prog/rand.go
index 62cd81acc..99fcd96a5 100644
--- a/prog/rand.go
+++ b/prog/rand.go
@@ -817,7 +817,7 @@ func (r *randGen) generateArg(s *state, typ sys.Type) (arg *Arg, calls []*Call)
inner, calls := r.generateArg(s, a.Type)
if a.Dir() == sys.DirOut && inner == nil {
// No data, but we should have got size.
- arg, calls1 := r.addr(s, a, inner.Size(a.Type), nil)
+ arg, calls1 := r.addr(s, a, inner.Size(), nil)
calls = append(calls, calls1...)
return arg, calls
}
@@ -829,7 +829,7 @@ func (r *randGen) generateArg(s *state, typ sys.Type) (arg *Arg, calls []*Call)
arg = pointerArg(a, addr.AddrPage, addr.AddrOffset, addr.AddrPagesNum, inner)
return arg, calls
}
- arg, calls1 := r.addr(s, a, inner.Size(a.Type), inner)
+ arg, calls1 := r.addr(s, a, inner.Size(), inner)
calls = append(calls, calls1...)
return arg, calls
case *sys.LenType:
diff --git a/sys/decl.go b/sys/decl.go
index e19e4c028..66dc4370a 100644
--- a/sys/decl.go
+++ b/sys/decl.go
@@ -34,7 +34,6 @@ type Type interface {
Default() uintptr
Size() uintptr
Align() uintptr
- InnerType() Type // returns inner type for PtrType
}
func IsPad(t Type) bool {
@@ -98,10 +97,6 @@ func (t *ResourceType) Align() uintptr {
return t.Desc.Type.Align()
}
-func (t *ResourceType) InnerType() Type {
- return t
-}
-
type FileoffType struct {
TypeCommon
TypeSize uintptr
@@ -117,10 +112,6 @@ func (t *FileoffType) Align() uintptr {
return t.Size()
}
-func (t *FileoffType) InnerType() Type {
- return t
-}
-
type BufferKind int
const (
@@ -160,10 +151,6 @@ func (t *BufferType) Align() uintptr {
return 1
}
-func (t *BufferType) InnerType() Type {
- return t
-}
-
type VmaType struct {
TypeCommon
}
@@ -176,10 +163,6 @@ func (t *VmaType) Align() uintptr {
return t.Size()
}
-func (t *VmaType) InnerType() Type {
- return t
-}
-
type LenType struct {
TypeCommon
TypeSize uintptr
@@ -196,10 +179,6 @@ func (t *LenType) Align() uintptr {
return t.Size()
}
-func (t *LenType) InnerType() Type {
- return t
-}
-
type FlagsType struct {
TypeCommon
TypeSize uintptr
@@ -215,10 +194,6 @@ func (t *FlagsType) Align() uintptr {
return t.Size()
}
-func (t *FlagsType) InnerType() Type {
- return t
-}
-
type ConstType struct {
TypeCommon
TypeSize uintptr
@@ -235,10 +210,6 @@ func (t *ConstType) Align() uintptr {
return t.Size()
}
-func (t *ConstType) InnerType() Type {
- return t
-}
-
type StrConstType struct {
TypeCommon
TypeSize uintptr
@@ -253,10 +224,6 @@ func (t *StrConstType) Align() uintptr {
return 1
}
-func (t *StrConstType) InnerType() Type {
- return t
-}
-
type IntKind int
const (
@@ -284,10 +251,6 @@ func (t *IntType) Align() uintptr {
return t.Size()
}
-func (t *IntType) InnerType() Type {
- return t
-}
-
type FilenameType struct {
TypeCommon
}
@@ -300,10 +263,6 @@ func (t *FilenameType) Align() uintptr {
return 1
}
-func (t *FilenameType) InnerType() Type {
- return t
-}
-
type ArrayKind int
const (
@@ -330,10 +289,6 @@ func (t *ArrayType) Align() uintptr {
return t.Type.Align()
}
-func (t *ArrayType) InnerType() Type {
- return t
-}
-
type PtrType struct {
TypeCommon
Type Type
@@ -347,10 +302,6 @@ func (t *PtrType) Align() uintptr {
return t.Size()
}
-func (t *PtrType) InnerType() Type {
- return t.Type.InnerType()
-}
-
type StructType struct {
TypeCommon
Fields []Type
@@ -383,10 +334,6 @@ func (t *StructType) Align() uintptr {
return align
}
-func (t *StructType) InnerType() Type {
- return t
-}
-
type UnionType struct {
TypeCommon
Options []Type
@@ -416,10 +363,6 @@ func (t *UnionType) Align() uintptr {
return align
}
-func (t *UnionType) InnerType() Type {
- return t
-}
-
var ctors = make(map[string][]*Call)
// ResourceConstructors returns a list of calls that can create a resource of the given kind.