Skip to content

Commit

Permalink
WIP - BITFIELD HACKS - DO NOT SUBMIT
Browse files Browse the repository at this point in the history
  • Loading branch information
pwaller committed Apr 1, 2017
1 parent 67cc24c commit 6c435ce
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 38 deletions.
20 changes: 19 additions & 1 deletion generator/gen_bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,24 @@ func (gen *Generator) proxyValueFromGo(memTip tl.Tip, name string,
"(%s)(unsafe.Pointer((*sliceHeader)(unsafe.Pointer(&%s)).Data)), cgoAllocsUnknown",
cgoSpec, name)
return
case isPlain && goSpec.BitFieldWidth != 0: // ex: bit field...
// log.Panicf("Have bitfield: %v - %v - %v", name, goSpec.BitFieldWidth, goSpec.String(), goSpec.Base)
// goSpec.Unsigned
if goSpec.Base != "int" {
panic("only int bitfields supported at the moment...")
}
goType := ""
if goSpec.Unsigned {
goType += "u"
}
goType += "int"
goType += fmt.Sprint(goSpec.BitFieldWidth)

// Hm. The Go compiler would usually want the C type here,
// but unfortunately when it's a bit field of a specific type,
// it demands the Go type. Weird.
proxy = fmt.Sprintf("(%s)(%s), cgoAllocsUnknown", goType, name)
return
case isPlain: // ex: byte, [4]byte
if (goSpec.Kind == tl.PlainTypeKind || goSpec.Kind == tl.EnumKind) &&
len(goSpec.OuterArr)+len(goSpec.InnerArr) == 0 && goSpec.Pointers == 0 {
Expand Down Expand Up @@ -1053,7 +1071,7 @@ var (
m map[unsafe.Pointer]struct{}
}
var cgoAllocsUnknown = new(cgoAllocMap)
var cgoAllocsUnknown = new(cgoAllocMap)
func (a *cgoAllocMap) Add(ptr unsafe.Pointer) {
a.mux.Lock()
Expand Down
13 changes: 7 additions & 6 deletions translator/ast_walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (t *Translator) walkDeclaration(d *cc.Declaration) (declared []*CDecl) {
func (t *Translator) declarator(d *cc.Declarator) *CDecl {
specifier := d.RawSpecifier()
decl := &CDecl{
Spec: t.typeSpec(d.Type, 0, false),
Spec: t.typeSpec(d.Type, 0, false, 0),
Name: identifierOf(d.DirectDeclarator),
IsTypedef: specifier.IsTypedef(),
IsStatic: specifier.IsStatic(),
Expand Down Expand Up @@ -228,7 +228,7 @@ func (t *Translator) structSpec(base *CTypeSpec, typ cc.Type, deep int) *CStruct
}
spec.Members = append(spec.Members, &CDecl{
Name: memberName(i, m),
Spec: t.typeSpec(m.Type, deep+1, false),
Spec: t.typeSpec(m.Type, deep+1, false, m.Bits),
Pos: pos,
})
}
Expand All @@ -248,22 +248,23 @@ func (t *Translator) functionSpec(base *CTypeSpec, typ cc.Type, deep int) *CFunc
return spec
}
if ret := typ.Result(); ret != nil && ret.Kind() != cc.Void {
spec.Return = t.typeSpec(ret, deep+1, true)
spec.Return = t.typeSpec(ret, deep+1, true, 0)
}
params, _ := typ.Parameters()
for i, p := range params {
spec.Params = append(spec.Params, &CDecl{
Name: paramName(i, p),
Spec: t.typeSpec(p.Type, deep+1, false),
Spec: t.typeSpec(p.Type, deep+1, false, 0),
Pos: p.Declarator.Pos(),
})
}
return spec
}

func (t *Translator) typeSpec(typ cc.Type, deep int, isRet bool) CType {
func (t *Translator) typeSpec(typ cc.Type, deep int, isRet bool, bitFieldWidth int) CType {
spec := &CTypeSpec{
Const: typ.Specifier().IsConst(),
Const: typ.Specifier().IsConst(),
BitFieldWidth: bitFieldWidth,
}
if !isRet {
spec.Raw = typedefNameOf(typ)
Expand Down
19 changes: 10 additions & 9 deletions translator/model_declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,16 @@ type (
)

type CDecl struct {
Spec CType
Name string
Value Value
Expression string
IsStatic bool
IsTypedef bool
IsDefine bool
Pos token.Pos
Src string
Spec CType
Name string
Value Value
Expression string
IsStatic bool
IsTypedef bool
IsDefine bool
Pos token.Pos
Src string
BitFieldWidth int // Is a bitfield on a struct if nonzero.
}

func (c CDecl) String() string {
Expand Down
19 changes: 10 additions & 9 deletions translator/model_go_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import (
)

type GoTypeSpec struct {
Slices uint8
Pointers uint8
InnerArr ArraySpec
OuterArr ArraySpec
Unsigned bool
Kind CTypeKind
Base string
Raw string
Bits uint16
Slices uint8
Pointers uint8
InnerArr ArraySpec
OuterArr ArraySpec
Unsigned bool
Kind CTypeKind
Base string
Raw string
Bits uint16
BitFieldWidth int // Is a bitfield on a struct if nonzero.
}

func (spec *GoTypeSpec) splitPointers(ptrTip Tip, n uint8) {
Expand Down
29 changes: 17 additions & 12 deletions translator/model_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import (
)

type CTypeSpec struct {
Raw string
Base string
Const bool
Signed bool
Unsigned bool
Short bool
Long bool
Complex bool
Opaque bool
Pointers uint8
InnerArr ArraySpec
OuterArr ArraySpec
Raw string
Base string
Const bool
Signed bool
Unsigned bool
Short bool
Long bool
Complex bool
Opaque bool
Pointers uint8
InnerArr ArraySpec
OuterArr ArraySpec
BitFieldWidth int
}

func (spec CTypeSpec) String() string {
Expand Down Expand Up @@ -76,6 +77,10 @@ func (c *CTypeSpec) GetTag() string {
return ""
}

func (c *CTypeSpec) GetBitFieldWidth() int {
return c.BitFieldWidth
}

func (c *CTypeSpec) SetRaw(x string) {
c.Raw = x
}
Expand Down
8 changes: 7 additions & 1 deletion translator/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,13 @@ func (t *Translator) TipRxsForSpec(scope TipScope,
return
}

func (t *Translator) TranslateSpec(spec CType, tips ...Tip) GoTypeSpec {
func (t *Translator) TranslateSpec(spec CType, tips ...Tip) (ret GoTypeSpec) {
defer func() {
if cTypeSpec, ok := spec.(*CTypeSpec); ok {
// HACK probably broken. Pass bit field info.
ret.BitFieldWidth = cTypeSpec.BitFieldWidth
}
}()
var ptrTip Tip
var typeTip Tip
for _, tip := range tips {
Expand Down

0 comments on commit 6c435ce

Please sign in to comment.