Bitfields and arrays in structures
Hello all, I'm new to MacRuby so please bear with me. Yesterday I was going through the buglist and happened upon bug #280 (https://www.macruby.org/trac/ticket/280 ) "NSNumber#decimalValue cant convert to ruby object". I created an objective c file to print out the method signature and I got this: {?=b8b4b1b1b18[8S]}16@0:8 (The ? would be replaced by NSDecimal_ when in MacRuby) The initial problem is happening inside of RoxorCompiler::convert_type (compiler.cpp) which doesn't handle the _C_BFLD type. Upon some further testing, it also doesn't handle the [8S] part (array of 8 short), which is also bug #178 (https://www.macruby.org/trac/ticket/ 178). So it seems like I need to add bitfield and structure array support. My question is, how is all of this supposed to be represented. I assume the arrays part can just be ArrayType, while we probably want to treat a bitfield declaration as just a regular structure, i.e. typedef struct { signed int _exponent:8; unsigned int _length:4; // length == 0 && isNegative -> NaN unsigned int _isNegative:1; unsigned int _isCompact:1; unsigned int _reserved:18; unsigned short _mantissa[NSDecimalMaxSize]; } NSDecimal; would be interpreted as typedef struct { signed int _exponent; unsigned int _length; // length == 0 && isNegative -> NaN unsigned int _isNegative; unsigned int _isCompact; unsigned int _reserved; unsigned short _mantissa[NSDecimalMaxSize]; } NSDecimal; The only problem I see with this is that we have to convert back to NSDecimal packing on return. Maybe it would be better to have a ruby class that provides accessors to the NSDecimal similar to Boxed (or modify Boxed to handle this situation). Just looking for a little bit of guidance, -Steve
Does anyone have any thoughts on this? The plan was to have a function returning a type like: typedef struct arrayType { int values[4]; } arrayType; give a Boxed object that has an array of values. Does that make sense? Any idea on how to handle C bitfields? I'm trudging ahead with a patch and some tests but I want some input ;) -Steve On Jan 4, 2010, at 9:58 AM, Steven Canfield wrote:
Hello all, I'm new to MacRuby so please bear with me. Yesterday I was going through the buglist and happened upon bug #280 (https://www.macruby.org/trac/ticket/280 ) "NSNumber#decimalValue cant convert to ruby object". I created an objective c file to print out the method signature and I got this: {?=b8b4b1b1b18[8S]}16@0:8 (The ? would be replaced by NSDecimal_ when in MacRuby)
The initial problem is happening inside of RoxorCompiler::convert_type (compiler.cpp) which doesn't handle the _C_BFLD type. Upon some further testing, it also doesn't handle the [8S] part (array of 8 short), which is also bug #178 (https://www.macruby.org/trac/ticket/178 ). So it seems like I need to add bitfield and structure array support.
My question is, how is all of this supposed to be represented. I assume the arrays part can just be ArrayType, while we probably want to treat a bitfield declaration as just a regular structure, i.e.
typedef struct { signed int _exponent:8; unsigned int _length:4; // length == 0 && isNegative -> NaN unsigned int _isNegative:1; unsigned int _isCompact:1; unsigned int _reserved:18; unsigned short _mantissa[NSDecimalMaxSize]; } NSDecimal;
would be interpreted as
typedef struct { signed int _exponent; unsigned int _length; // length == 0 && isNegative -> NaN unsigned int _isNegative; unsigned int _isCompact; unsigned int _reserved; unsigned short _mantissa[NSDecimalMaxSize]; } NSDecimal;
The only problem I see with this is that we have to convert back to NSDecimal packing on return. Maybe it would be better to have a ruby class that provides accessors to the NSDecimal similar to Boxed (or modify Boxed to handle this situation).
Just looking for a little bit of guidance, -Steve
Any idea on how to handle C bitfields?
I don't know exactly how the compiler packs bitfields (especially it seems to depend on the compiler and the platform). Anyway for MacRuby we only officially support two platforms : MacOS X (>= 10.5) on i386 and x86_64, both being of course little endian. You do not need to care about other environments. Even if you make something that only works on one of those platforms it would be very nice. I'm sure we could find someone that can adapt it to work on the other. You first need to check how the compiler does its packing. For instance creating something like union { struct { int a : 3; unsigned int b : 4; } char data[10]; } assigning different values to the values in the struct, trying with data in the struct of multiple sizes (having something of less than 1 byte, less than 2, less than 4...), and looking the data's size and how the data is represented in memory. Then do some automatic tests to test the desired behavior, like what we do currently in ./spec/macruby/language/objc_method_spec.rb And then do the coding, at least if you're developing tests first. Anyway having tests is pretty important. Cheers
participants (2)
-
Steven Canfield
-
Vincent Isambart