structure declaration question
http://lxr.free-electrons.com/source/drivers/net/8139too.c#L498 On above link What I am not getting is the structure
static const struct {
const char *name;
u32 version; /* from RTL8139C/RTL8139D docs */
u32 flags;
} rtl_chip_info[] = {
{ "RTL-8139",
HW_REVID(1, 0, 0, 0, 0, 0, 0),
HasHltClk,
},
gets expanded to
static const struct {
const char *name;
u32 version; /* from RTL8139C/RTL8139D docs */
u32 flags;
} r开发者_Go百科tl_chip_info[] = {
{ "RTL-8139",
**(b30, b29, b28, b27, b26, b23, b22)
(b30<<30 | b29<<29 | b28<<28 | b27<<27 | b26<<26 | b23<<23 | b22<<22)
(1, 0, 0, 0, 0, 0, 0),**
HasHltClk,
},
I do not understand above type (2nd) of structure declaration.
The (b30, b29, b28, b27, b26, b23, b22)
doesn't actually make it into the code -- they're parameters for the HW_REVID
macro. In other words,
HW_REVID(1, 0, 0, 0, 0, 0, 0),
turns into
(1<<30 | 0<<29 | 0<<28 | 0<<27 | 0<<26 | 0<<23 | 0<<22)
because the b30
etc. are replaced (note by replaced I mean literally copy and pasted) by the 1, 0,
etc. So when all is said and done it looks like:
static const struct {
const char *name;
u32 version; /* from RTL8139C/RTL8139D docs */
u32 flags;
} rtl_chip_info[] = {
{ "RTL-8139",
(1<<30 | 0<<29 | 0<<28 | 0<<27 | 0<<26 | 0<<23 | 0<<22), /* originally HW_REVID(1, 0, 0, 0, 0, 0, 0), */
HasHltClk,
}
I don't know what compiler you're using, but if you're using gcc for example, you can pass the -E
switch to do only the preprocessing (replacing #defines and macros and so forth) so you can see exactly the code it is compiling looks like.
HW_REVID is simply a macro for settings 1's in various bit positions 30 through 22. It is defined as:
#define HW_REVID(b30, b29, b28, b27, b26, b23, b22) \
(b30<<30 | b29<<29 | b28<<28 | b27<<27 | b26<<26 | b23<<23 | b22<<22)
When called as HW_REVID(1, 0, 0, 0, 0, 0, 0) it gets expanded as:
(1<<30 | 0<<29 | 0<<28 | 0<<27 | 0<<26 | 0<<23 | 0<<22)
which shifts a 1 30 places left (into bit 30) initializing the 32-bit version field to 0x40000000.
精彩评论