[chirp_devel] cant use structs with bit type members totalling less than 8 bits
Hi Dan, So I have a use for a struct with 4 single-bit members.
currently vx3 (And others) have a separate structure that contains some bitmask fields for various flags. They are only 4 bits per channel. However, due to bitwise constraints, it seems we are left with doing something like: struct { u8 odd_flagA:1, odd_flagB:1, odd_flagC:1, odd_flagD:1, even_flagA:1, even_flagB:1, even_flagC:1, even_flagD:1; } flags [50];
and then using some unholy trickery to interleave this out where we need it - it's very ugly, and confusing.
What I'd like to do is take advantage of the simple "bit" type in bitwise. So I thought i'd simplify the above to: struct { bit flagA; bit flagB; bit flagC; bit flagD; } flags [100];
But when I do this bitwise.py croaks with "bit array must be divisible by 8." (trace below).
Why is it this way? The second form would be much simpler and cleaner overall in the code to work with. Is there some alignment concern? Is there any provision for a data type of less than a byte?
File "/Users/jens/build/chirp.hg/chirp/vx3.py", line 372, in process_mmap self._memobj = bitwise.parse(MEM_FORMAT, self._mmap) File "/Users/jens/build/chirp.hg/chirp/bitwise.py", line 857, in parse return p.parse(ast) File "/Users/jens/build/chirp.hg/chirp/bitwise.py", line 850, in parse self.parse_block(lang) File "/Users/jens/build/chirp.hg/chirp/bitwise.py", line 841, in parse_block self.parse_struct(d) File "/Users/jens/build/chirp.hg/chirp/bitwise.py", line 822, in parse_struct return self.parse_struct_decl(struct[0][1]) File "/Users/jens/build/chirp.hg/chirp/bitwise.py", line 805, in parse_struct_decl self.parse_block(block) File "/Users/jens/build/chirp.hg/chirp/bitwise.py", line 843, in parse_block self.parse_defn(d) File "/Users/jens/build/chirp.hg/chirp/bitwise.py", line 773, in parse_defn gen = self.do_bitarray(i, count) File "/Users/jens/build/chirp.hg/chirp/bitwise.py", line 745, in do_bitarray raise ValueError("bit array must be divisible by 8.")
But when I do this bitwise.py croaks with "bit array must be divisible by 8." (trace below).
Why is it this way? The second form would be much simpler and cleaner overall in the code to work with.
Because everything is bytes underneath? I'm not sure how to explain it, but if you look at bitwise, everything has to map to a byte and an offset at some point. The bit array maps to a whole byte, which is why it has to be eight elements long.
Is there some alignment concern? Is there any provision for a data type of less than a byte?
It gets really complicated if you try to support something like this (which is equivalent to what you're asking for):
struct { bit a; u8 b; bit c; u24 d; } foo;
In the above, b is eight bits that span a byte. c is in the third byte and d ends in the seventh byte with some bits left over. Implementation complexity aside, supporting the above also means it becomes very difficult to realize that there are some unaddressed bits left over at the end of foo, and it's difficult to know the address where the thing that comes after foo starts, especially if you think you've created something that is a whole number of bytes.
So, complexity of supporting the unaligned accesses, and the complexity of validating something by looking at it led me to write it such that everything had to be aligned to byte boundaries. For most cases, this is really not a problem, and I don't think the few cases that suffer are worth the additional complexity. It's pretty easy to just define a few helper methods to make things easier to understand, IMHO.
--Dan
participants (2)
-
Dan Smith
-
Jens J.