| 1 | // Package attr provides attributes for text and data sections. |
| 2 | package attr |
| 3 | |
| 4 | import ( |
| 5 | "fmt" |
| 6 | "math/bits" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | // Attribute represents TEXT or DATA flags. |
| 11 | type Attribute uint16 |
| 12 | |
| 13 | //go:generate go run make_textflag.go -output ztextflag.go |
| 14 | |
| 15 | // Asm returns a representation of the attributes in assembly syntax. This may use macros from "textflags.h"; see ContainsTextFlags() to determine if this header is required. |
| 16 | func (a Attribute) Asm() string { |
| 17 | parts, rest := a.split() |
| 18 | if len(parts) == 0 || rest != 0 { |
| 19 | parts = append(parts, fmt.Sprintf("%d", rest)) |
| 20 | } |
| 21 | return strings.Join(parts, "|") |
| 22 | } |
| 23 | |
| 24 | // ContainsTextFlags returns whether the Asm() representation requires macros in "textflags.h". |
| 25 | func (a Attribute) ContainsTextFlags() bool { |
| 26 | flags, _ := a.split() |
| 27 | return len(flags) > 0 |
| 28 | } |
| 29 | |
| 30 | // split splits a into known flags and any remaining bits. |
| 31 | func (a Attribute) split() ([]string, Attribute) { |
| 32 | var flags []string |
| 33 | var rest Attribute |
| 34 | for a != 0 { |
| 35 | i := uint(bits.TrailingZeros16(uint16(a))) |
| 36 | bit := Attribute(1) << i |
| 37 | if flag := attrname[bit]; flag != "" { |
| 38 | flags = append(flags, flag) |
| 39 | } else { |
| 40 | rest |= bit |
| 41 | } |
| 42 | a ^= bit |
| 43 | } |
| 44 | return flags, rest |
| 45 | } |