...
1
2
3
4
5 package ppc64asm
6
7 import (
8 "fmt"
9 "strings"
10 )
11
12
13
14 type BitField struct {
15 Offs uint8
16 Bits uint8
17
18
19
20 Word uint8
21 }
22
23 func (b BitField) String() string {
24 if b.Bits > 1 {
25 return fmt.Sprintf("[%d:%d]", b.Offs, int(b.Offs+b.Bits)-1)
26 } else if b.Bits == 1 {
27 return fmt.Sprintf("[%d]", b.Offs)
28 } else {
29 return fmt.Sprintf("[%d, len=0]", b.Offs)
30 }
31 }
32
33
34
35 func (b BitField) Parse(i [2]uint32) uint32 {
36 if b.Bits > 32 || b.Bits == 0 || b.Offs > 31 || b.Offs+b.Bits > 32 {
37 panic(fmt.Sprintf("invalid bitfiled %v", b))
38 }
39 return (i[b.Word] >> (32 - b.Offs - b.Bits)) & ((1 << b.Bits) - 1)
40 }
41
42
43
44 func (b BitField) ParseSigned(i [2]uint32) int32 {
45 u := int32(b.Parse(i))
46 return u << (32 - b.Bits) >> (32 - b.Bits)
47 }
48
49
50 type BitFields []BitField
51
52 func (bs BitFields) String() string {
53 ss := make([]string, len(bs))
54 for i, bf := range bs {
55 ss[i] = bf.String()
56 }
57 return fmt.Sprintf("<%s>", strings.Join(ss, "|"))
58 }
59
60 func (bs *BitFields) Append(b BitField) {
61 *bs = append(*bs, b)
62 }
63
64
65
66
67
68 func (bs BitFields) parse(i [2]uint32) (u uint64, Bits uint8) {
69 for _, b := range bs {
70 u = (u << b.Bits) | uint64(b.Parse(i))
71 Bits += b.Bits
72 }
73 return u, Bits
74 }
75
76
77
78 func (bs BitFields) Parse(i [2]uint32) uint64 {
79 u, _ := bs.parse(i)
80 return u
81 }
82
83
84
85 func (bs BitFields) ParseSigned(i [2]uint32) int64 {
86 u, l := bs.parse(i)
87 return int64(u) << (64 - l) >> (64 - l)
88 }
89
90
91 func (bs BitFields) NumBits() int {
92 num := 0
93 for _, b := range bs {
94 num += int(b.Bits)
95 }
96 return num
97 }
98
View as plain text