Jump to content

need clarification: local constructor issue in singleton class


Recommended Posts

One of the singleton class coding styles is like this:

module top;

class singleton;

static local singleton me = new;

local function new ();

endfunction

endclass: singleton

endmodule: top

The invocation of "new" constructor is in the class but not in the static function like "get_inst". Simulators don't handle this code uniquely.

Please help confirm whether it is allowed by the LRM.

Link to comment
Share on other sites

hi,

one issue here between the lines is that the execution order of static initializers is NOT defined in the LRM as far as i know. so it would be unclear what the result would be

class singleton;

int a=3; // 3 for sure

int c=a+1; // could be 1 or 4

endclass

another issue with the singleton code the way its written is that the singleton is unconditionally allocated and not just on demand. typically the singletons look like

module top;

class singleton;

static local singleton me;

local function new ();

endfunction

static function singleton get();

if(me==null)

me=new();

return me;

endfunction

endclass: singleton

initial

singleton::get();

initial

singleton::get();

endmodule: top

Link to comment
Share on other sites

Hi Uwe,

Actually we can have two kinds of coding style for singleton class: let's call them "starve" and "lazy". If I know the instance of the singleton is a must in a testbench and it doesn't play with other classes, it can use this "starve" coding style, eliminating the get_inst method and its invocation.

For example, we can put the waveform dump function call in the constructor with local attribute, use can "include" the class file inside his tb top module and it's done.

Not all the simulation can compile the code: static local singleton me = new; since the "new" has a "local" modifier.

How does the LRM state this point?

Link to comment
Share on other sites

hi,

one issue here between the lines is that the execution order of static initializers is NOT defined in the LRM as far as i know. so it would be unclear what the result would be

class singleton;

int a=3; // 3 for sure

int c=a+1; // could be 1 or 4

endclass

Is this really correct? The expression c=a+1 refers to a, and the rule is then that a must have been declared already. The initializer for a is part of the declaration of a, thus the value is known already to be 3 at point of declaration of c, and the initializer for c should therefore be 4. What do you mean the value could be 1?

One should think that one reason for merging initialization with the declaration was to avoid this confusion. In C++ for example, where the two steps are separated, the initializers can be listed in an order that isn't the actual initialization order (which is the declaration order). It would seem that if the SV improvement in this regard comes with undefined initialization order, then this isn't an improvement after all, but a defect.

Erling

Link to comment
Share on other sites

I found a statement in the LRM-2009, page 138.

8.17 Data hiding and encapsulation

A member identified as local is available only to methods inside the class.

That way the "static local singleton me = new;" should be illegal per the LRM.

However, I think the LRM statement is too restrictive. The invocation of local member should be allowed in the scope of its class, rather than only in the scope of methods inside the class. And constructor function is also a kind of member as well.

In addition, this is supported by all simulators, though it appears illegal per the LRM:

module top;

class singleton;

static local bit _dummy = do_something();

static local function bit do_something ();

endfunction

local function new();

endfunction

endclass: singleton

endmodule: top

Edited by Robert.g.Liu
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...