Build vs new

Hi,

I am a bit confused with the need for build.

As far as I can see, both new and build start from some top class and thereafter the new method of their parent class is called. This is because the new method call super.new as the first thing. Build also seems to do the same thing, call parent build, allocate instantiated objects and call their build method. Trying to understand the difference.

Regards,

They look and feel same, don’t they? Without build(), if class need to have any configuration variables then they will all have be be passed via constructor arguments. This approach has two problems:

  1. The constructor argument list will eventually grow to unreasonably large size. So every user need to know the exact order of the arguments and it is just plain ugly.
  2. Unless parent class “pipes though” all constructor arguments of all children classes, user may loose ability to directly configure children classes from above.

By introducing build(), user will have a chance to change configuration fields (at any level in hierarchy) by calling set_config_* functions before the build() phase. Now the build() phases of all classes can change their construction based on these configuration fields.

They look and feel same, don’t they? Without build(), if class need to have any configuration variables then they will all have be be passed via constructor arguments. This approach has two problems:

  1. The constructor argument list will eventually grow to unreasonably large size. So every user need to know the exact order of the arguments and it is just plain ugly.
  2. Unless parent class “pipes though” all constructor arguments of all children classes, user may loose ability to directly configure children classes from above.
    By introducing build(), user will have a chance to change configuration fields (at any level in hierarchy) by calling set_config_* functions before the build() phase. Now the build() phases of all classes can change their construction based on these configuration fields.

Hi Jigar,

The same thing can be done by using set_config from some top class’ new method to configure before the new object hierarchy really gets build, since set/get_config are common infrastructure.

Regards,

Hi,

Even though new and build may appear to be doing similar things, there are fundamental differences between them.

new() is the class constructor and as such is constrained by the rules in the SystemVerilog LRM in its behaviour. In particular, a derived class cannot override the actions of new in its base class - the base class new() ALWAYS gets called, either implicitly or explicitly. Also, if super.new() is called explicitly, it must be the first “real” statement in the child new().

The build() function is behaves like any other virtual member function - it can be overridden in a derived class and calling super.build is optional (although usually recommended). The build function in a child component may also perform any number of operations to configure the environment before it calls super.build. These differences gives it a lot more flexibility for building complex (possibly parameterisable) verification environments.

Regards,
Dave

Another point is if you do not have build() the set_config_() API will be just ignore if called before new() is called for any class. The reason is set_config_() can only configure the variables that exists i.e. the classes containing those variables must have been created before calling set_config_*(). And hance we need another place in the creation process to use these “configuration” variables in order to affect the properties and/or topology of children classes.

Dave already pointed out the polymorphism requirement.