Giter Site home page Giter Site logo

Bitfield assignment is broken about c-for-go HOT 4 OPEN

xlab avatar xlab commented on July 17, 2024
Bitfield assignment is broken

from c-for-go.

Comments (4)

cznic avatar cznic commented on July 17, 2024

@pwaller & @xlab

I haven't really checked but it's quite possible the bitfield handling is broken in cc. I've run into several such issues and somehow bypassed them in ccir, for example. Once I will get [more] confident about the correctness of the bypass, I gues it's due to backport ccir's hack to cc.

However, please feel free to report any cc issue directly at its issue tracker. No promises about when the fixes will come, but I'm currently becoming more and more dependent on [at least some] correctness of cc, so bug reports are definitely welcome.

from c-for-go.

pwaller avatar pwaller commented on July 17, 2024

@cznic as far as I can tell, the interpretation of the bitfield is correct from cc's side. The problem is a difference between what c-for-go and cgo expect. From a careful inspection of the error output (above); you can see that for some reason, even though the bitfield's type is an int, if the bitfield with is 8 - even though the type is an int, cgo expects you to assign a uint8 to it! Not a C.uint8_t, but a go uint8. I think this is a surprising feature of cgo and I can't find it documented anywhere.


Separate thought:

I didn't think to check non-multiples of 8, but in that case I get:

typedef struct {
        int i : 1;
        int j : 7;
        int k : 15;
        int l : 31;
} Foo;
bitfield/cgo_helpers.go:159: x.refb43e23c1.i undefined (type *C.struct___0 has no field or method i)
bitfield/cgo_helpers.go:160: x.refb43e23c1.j undefined (type *C.struct___0 has no field or method j)
bitfield/cgo_helpers.go:161: x.refb43e23c1.k undefined (type *C.struct___0 has no field or method k)
bitfield/cgo_helpers.go:162: x.refb43e23c1.l undefined (type *C.struct___0 has no field or method l)

... which seems to suggest that CGo doesn't support odd-sized bitfields? Am I missing something here - is the above struct definition valid C?

from c-for-go.

cznic avatar cznic commented on July 17, 2024

Am I missing something here - is the above struct definition valid C?

Seems valid. I'm not aware of any requirement of the bitfileds to be of width a multiply of something.

from c-for-go.

CtrlZvi avatar CtrlZvi commented on July 17, 2024

I don't think it's just odd bitfield sizes. I think it might be any bitfields that are not powers of 2? From the cgo command documentation: "C struct fields that cannot be expressed in Go, such as bit fields or misaligned data, are omitted in the Go struct, replaced by appropriate padding to reach the next field or the end of the struct."

from c-for-go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.