diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2022-01-06 15:07:49 +0100 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2022-01-11 16:30:08 +0100 |
| commit | 16e21d13ea26a631e9b3a30c94635b1d565fd78f (patch) | |
| tree | 94bf8af878bd28fd7e43c3850952b53c5ca4c28a /pkg/compiler | |
| parent | 993f2038a54bae8d42ae20fc24ecee845b8b9c5d (diff) | |
pkg/compiler: refactor code
Slightly refactor code in preparation for future changes.
No functional changes intended.
Diffstat (limited to 'pkg/compiler')
| -rw-r--r-- | pkg/compiler/gen.go | 10 | ||||
| -rw-r--r-- | pkg/compiler/types.go | 21 |
2 files changed, 18 insertions, 13 deletions
diff --git a/pkg/compiler/gen.go b/pkg/compiler/gen.go index bd487432a..b4ee50a9b 100644 --- a/pkg/compiler/gen.go +++ b/pkg/compiler/gen.go @@ -360,6 +360,10 @@ func (comp *compiler) layoutStructFields(t *prog.StructType, varlen, packed bool byteOffset += f.Size() } } + t.Fields = comp.finalizeStructFields(t, newFields, varlen, structAlign, byteOffset, bitOffset) +} + +func (comp *compiler) finalizeStructFields(t *prog.StructType, fields []prog.Field, varlen bool, structAlign, byteOffset, bitOffset uint64) []prog.Field { if bitOffset != 0 { pad := roundup(bitOffset, 8) / 8 byteOffset += pad @@ -367,7 +371,7 @@ func (comp *compiler) layoutStructFields(t *prog.StructType, varlen, packed bool if i != 0 && t.Fields[i-1].IsBitfield() { setBitfieldTypeSize(t.Fields[i-1].Type, pad) } else { - newFields = append(newFields, genPad(pad)) + fields = append(fields, genPad(pad)) } } @@ -376,9 +380,9 @@ func (comp *compiler) layoutStructFields(t *prog.StructType, varlen, packed bool } if !varlen && structAlign != 0 && byteOffset%structAlign != 0 { pad := structAlign - byteOffset%structAlign - newFields = append(newFields, genPad(pad)) + fields = append(fields, genPad(pad)) } - t.Fields = newFields + return fields } func roundup(v, a uint64) uint64 { diff --git a/pkg/compiler/types.go b/pkg/compiler/types.go index 786520930..53ba8c86f 100644 --- a/pkg/compiler/types.go +++ b/pkg/compiler/types.go @@ -909,25 +909,26 @@ func init() { // Need to cache type in structTypes before generating fields to break recursion. comp.structTypes[t.Ident] = typ fields := comp.genFieldArray(s.Fields, make([]uint64, len(s.Fields))) - if s.IsUnion { - typ.(*prog.UnionType).Fields = fields + switch typ1 := typ.(type) { + case *prog.UnionType: + typ1.Fields = fields for _, f := range fields { - if a := f.Type.Alignment(); typ.(*prog.UnionType).TypeAlign < a { - typ.(*prog.UnionType).TypeAlign = a + if a := f.Type.Alignment(); typ1.TypeAlign < a { + typ1.TypeAlign = a } } - } else { - typ.(*prog.StructType).Fields = fields + case *prog.StructType: + typ1.Fields = fields attrs := comp.parseAttrs(structAttrs, s, s.Attrs) if align := attrs[attrAlign]; align != 0 { - typ.(*prog.StructType).TypeAlign = align + typ1.TypeAlign = align } else if attrs[attrPacked] != 0 { - typ.(*prog.StructType).TypeAlign = 1 + typ1.TypeAlign = 1 } else { for _, f := range fields { a := f.Type.Alignment() - if typ.(*prog.StructType).TypeAlign < a { - typ.(*prog.StructType).TypeAlign = a + if typ1.TypeAlign < a { + typ1.TypeAlign = a } } } |
