Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Accessing a structure member after a cast

19 views
Skip to first unread message

Chris Higgs

unread,
Dec 9, 2011, 1:39:02 PM12/9/11
to
Greetings,

Is there a legal way to access a member of a structure after a cast?
For example, given the structure:

<code>
typedef struct packed {
logic [7:0] byte_member;
logic flag_member;
} my_struct_t;

my_struct_t mystruct;
logic [$bits(my_struct_t)-1:0] mybits;
logic [7:0] mybyte;
</code>

I would expect to be able to access the structure members like so:

<code>
// This doesn't work, is apparently a syntax error.
mybyte = my_struct_t'(mybits).byte_member;
</code>

However to get VCS to play ball I have to cast to an intermediate
variable first and then access the member:

<code>
mystruct_temp = my_struct_t'(mybits);
mybyte = mystruct_temp.byte_member;
</code>

This is ugly and means I have to create unwanted variables just to
pull fields out of my structure. There must be a better way which I'm
missing...

I can't seem to find anything in the LRM regarding this and the books
I have consulted do not cover it. Any suggestions greatly appreciated.

Thanks,

Chris

Jonathan Bromley

unread,
Dec 24, 2011, 5:52:39 AM12/24/11
to
On Fri, 9 Dec 2011 10:39:02 -0800 (PST), Chris Higgs
<chig...@gmail.com> wrote:

>Greetings,
>
>Is there a legal way to access a member of a structure after a cast?
>For example, given the structure:
>
><code>
> typedef struct packed {
> logic [7:0] byte_member;
> logic flag_member;
> } my_struct_t;
>
> my_struct_t mystruct;
> logic [$bits(my_struct_t)-1:0] mybits;
> logic [7:0] mybyte;
></code>
>
>I would expect to be able to access the structure members like so:
>
><code>
> // This doesn't work, is apparently a syntax error.
> mybyte = my_struct_t'(mybits).byte_member;
></code>

Hmmm. Is there any reason at all why your variable "mybits"
could not be declared as my_struct_t? it's a packed struct,
so you can still slice-and-dice it like any other vector,
so you don't lose anything by declaring it as the struct type.

The BNF syntax for expressions is quite hard to navigate
manually, but I'm fairly sure you cannot put anything
in front of the element-select '.' except an identifier.
It is tiresome. Another place it's a PITA is when a
function returns a class object or a struct, and you
want to select from the result:
func_returns_struct().element // illegal
Maybe one day... SV-2014???
--
Jonathan Bromley
0 new messages