diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2017-09-04 19:53:05 +0200 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2017-09-04 20:25:23 +0200 |
| commit | 399addc8754ed0b484d3c159ac35befe1d3f652c (patch) | |
| tree | 38f937a6c625d253b50b0f51e988ed1c22630b57 /sys/align.go | |
| parent | 94e151ceb51191698a068d96191cdd86326050f9 (diff) | |
sys, pkg/compiler: move padding computation to compiler
This makes types constant during execution, everything is precomputed.
Diffstat (limited to 'sys/align.go')
| -rw-r--r-- | sys/align.go | 96 |
1 files changed, 0 insertions, 96 deletions
diff --git a/sys/align.go b/sys/align.go deleted file mode 100644 index 1be1ccc82..000000000 --- a/sys/align.go +++ /dev/null @@ -1,96 +0,0 @@ -// Copyright 2015 syzkaller project authors. All rights reserved. -// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. - -package sys - -import ( - "fmt" -) - -func initAlign() { - var rec func(t Type) - rec = func(t Type) { - switch t1 := t.(type) { - case *PtrType: - rec(t1.Type) - case *ArrayType: - rec(t1.Type) - case *StructType: - if !t1.padded { - t1.padded = true - for _, f := range t1.Fields { - rec(f) - } - t1.Varlen() // dummy call to initialize t1.varlen - addAlignment(t1) - } - case *UnionType: - for _, opt := range t1.Options { - rec(opt) - } - } - } - - for _, c := range Calls { - for _, a := range c.Args { - rec(a) - } - if c.Ret != nil { - rec(c.Ret) - } - } -} - -func addAlignment(t *StructType) { - if t.IsPacked { - // If a struct is packed, statically sized and has explicitly set alignment, add a padding. - if !t.Varlen() && t.AlignAttr != 0 && t.Size()%t.AlignAttr != 0 { - pad := t.AlignAttr - t.Size()%t.AlignAttr - t.Fields = append(t.Fields, makePad(pad)) - } - return - } - var fields []Type - var off uint64 - align := t.AlignAttr - for i, f := range t.Fields { - a := f.Align() - if align < a { - align = a - } - if i > 0 && (t.Fields[i-1].BitfieldLength() == 0 || t.Fields[i-1].BitfieldLast()) { - // Append padding if the last field is not a bitfield or it's the last bitfield in a set. - if off%a != 0 { - pad := a - off%a - off += pad - fields = append(fields, makePad(pad)) - } - } - if f.Varlen() && i != len(t.Fields)-1 { - panic(fmt.Sprintf("variable length field %+v in the middle of a struct %+v", f, t)) - } - fields = append(fields, f) - if (f.BitfieldLength() == 0 || f.BitfieldLast()) && (i != len(t.Fields)-1 || !f.Varlen()) { - // Increase offset if the current field is not a bitfield or it's the last bitfield in a set, - // except when it's the last field in a struct and has variable length. - off += f.Size() - } - } - if align != 0 && off%align != 0 && !t.Varlen() { - pad := align - off%align - off += pad - fields = append(fields, makePad(pad)) - } - t.Fields = fields -} - -func makePad(sz uint64) Type { - return &ConstType{ - IntTypeCommon: IntTypeCommon{ - TypeCommon: TypeCommon{TypeName: "pad", IsOptional: false}, - TypeSize: sz, - }, - Val: 0, - IsPad: true, - } -} |
