File: attr.go

package info (click to toggle)
golang-github-mmcloughlin-avo 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 15,024 kB
  • sloc: xml: 71,029; asm: 14,862; sh: 194; makefile: 21; ansic: 11
file content (45 lines) | stat: -rw-r--r-- 1,171 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Package attr provides attributes for text and data sections.
package attr

import (
	"fmt"
	"math/bits"
	"strings"
)

// Attribute represents TEXT or DATA flags.
type Attribute uint16

//go:generate go run make_textflag.go -output ztextflag.go

// 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.
func (a Attribute) Asm() string {
	parts, rest := a.split()
	if len(parts) == 0 || rest != 0 {
		parts = append(parts, fmt.Sprintf("%d", rest))
	}
	return strings.Join(parts, "|")
}

// ContainsTextFlags returns whether the Asm() representation requires macros in "textflags.h".
func (a Attribute) ContainsTextFlags() bool {
	flags, _ := a.split()
	return len(flags) > 0
}

// split splits a into known flags and any remaining bits.
func (a Attribute) split() ([]string, Attribute) {
	var flags []string
	var rest Attribute
	for a != 0 {
		i := uint(bits.TrailingZeros16(uint16(a)))
		bit := Attribute(1) << i
		if flag := attrname[bit]; flag != "" {
			flags = append(flags, flag)
		} else {
			rest |= bit
		}
		a ^= bit
	}
	return flags, rest
}