2019-01-05 18:18:49 -08:00
// Package attr provides attributes for text and data sections.
package attr
2018-12-30 19:57:01 -08:00
import (
"fmt"
"math/bits"
"strings"
)
// Attribute represents TEXT or DATA flags.
type Attribute uint16
2021-04-10 23:45:40 -07:00
//go:generate go run make_textflag.go -output ztextflag.go
2018-12-30 19:57:01 -08:00
// 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 , "|" )
}
2019-01-05 00:31:46 -08:00
// ContainsTextFlags returns whether the Asm() representation requires macros in "textflags.h".
2018-12-30 19:57:01 -08:00
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 ) ) )
2018-12-30 21:45:35 -08:00
bit := Attribute ( 1 ) << i
2018-12-30 19:57:01 -08:00
if flag := attrname [ bit ] ; flag != "" {
flags = append ( flags , flag )
} else {
rest |= bit
}
a ^ = bit
}
return flags , rest
}