|
|
Re: Packed structures using XDC [message #693464 is a reply to message #668422] |
Wed, 06 July 2011 14:09   |
R Zuber Messages: 5 Registered: April 2011 |
Junior Member |
|
|
I got around to trying this. A couple of notes for anyone else attempting to do this:
- Your .xdc file must be declared with the @CustomHeader attribute
- You must declare the structure as metaonly in the xdc file, then declare the @encoded typedef to whatever you want it to be in the C.
- There are two underscores in mod__epilogue.h and mod__prologue.h
- You only need to declare the structure in mod__prologue.h; mod__epilogue can remain empty.
- The desc argument passed into the eType$encode(desc) function is the metadomain representation of the structure. I am not exactly sure if this data can actually be used for static initialization of the structure.
- You must add the proper compiler option to your package.bld file to enable the GCC extensions (AKA packed). This will look something like:
var Pkg = xdc.useModule('xdc.bld.PackageContents');
Pkg.attrs.copts = '--gcc'
This attribute is somewhat parasitic as all downstream package.blds must include it as well.
Here are some quick snippets for everyone's reference:
myModule.xdc
@CustomHeader
module myModule
{
metaonly struct Foo
{
Bits8 Bar;
Bits16 Baz;
}
@Encoded typedef Foo Foo_s;
//The rest of your module here
}
myModule__prologue.h
typedef struct
{
xdc_Bits8 Bar;
xdc_Bits16 Baz;
}__attribute__((packed)) company_whatever_myModule_Foo_s;
myModule.xs
//Helpful description from Assert.xs
/* The three functions below must exist for each encoded type defined in this
* module's spec. sizeof and alignof are invoked if the encoded type is used
* in a structure, while encode is used when a value of the encoded type
* needs to be represented on the target.
* In this case, the encoded type is 'Id', which is why the function names
* start with that prefix.
*/
function Foo_s$alignof()
{
return 1;
}
//TODO: We might be able to / need to do more complicated things here
function Foo_s$encode(desc)
{
return "{ (xdc_Bits8)" + desc.Bar + ", /* Bar */\n" +
" (xdc_Bits16)" + desc.Baz + ", /* Baz */\n" +
" }";
}
function Foo_s$sizeof()
{
return 3;
}
I have two more questions for Dave or anyone else who may know:
1) How can I use the sizeof() attribute in a client .cfg script? I want to be able to create a mailbox in the metadomain sized to the size of the packed structure I'm making. It seems like the both the meta and c-type is unknown in the cfg file. For example, just trying to print out the size of the Log Event type:
var Log = xdc.useModule('xdc.runtime.Log');
print('meta-type - ' + Log.EventDesc);
print('c-type - ' + Log.Event);
print('sizeof - ' + Log.EventDesc.$sizeof());
print('sizeof - ' + Log.Event.$sizeof());
returns an error because it doesn't know the type:
meta-type - <unknown>
c-type - <unknown>
js: "./Test.cfg", line 72: XDC runtime error: <unknown>: no property named '$sizeof'
"./package/cfg/Test_x64P.cfg", line 747
"./package/cfg/Test_x64P.cfg", line 802
"./package/cfg/Test_x64P.cfg", line 734
I could just declare a constant for the size if need be, but it would be cleaner just to use $sizeof() directly.
2) Is my code above for the encode function correct?
[Updated on: Wed, 06 July 2011 14:53] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.01993 seconds