Why run_phase is virtual in driver class?

Hi all,

Why we mention run_phase as virtual in driver class?
I saw some driver classes in my erliear projects there they mentioned run_phase as virtual. There is know extension of ips_driver class(i.e. ips_driver extends from uvm_driver ).

Can u give some scenarios or examples what is purpose of virtual used in run_phase.

Thanks in advance

In reply to VK_18:

Hi all,
Why we mention run_phase as virtual in driver class?
I saw some driver classes in my erliear projects there they mentioned run_phase as virtual. There is know extension of ips_driver class(i.e. ips_driver extends from uvm_driver ).
Can u give some scenarios or examples what is purpose of virtual used in run_phase.
Thanks in advance

The run_phase task is declared as virtual in uvm_component. Then in all derived class which extend from uvm_component (including uvm_driver or its derived class), this task is always treated as virtual no matter what you declare it with virtual keyword or not.

In reply to chris90:

Hi all,
Hi @chris90 thank u for quick reply, but my question is why we need to use virtual run_phase especially in driver class, if we extended that driver class then only it is useful otherwise it is not necessary to use virtual.

In most of my projects driver class didn’t extended but still they are using the virtual run_phase.

So, pls let me know 1. why we need to use virtual run_phase in driver class without extention ?
2. If extended then provide one example ?

Thanks in advance.

In reply to VK_18:

The “virtual” keyword of a function or task that allows polymorphism ability in OOP.
For example: Your base class has a function declare as virtual, now you want to change the behavior of that function, instead of modifying directly to the function, you can extend your base class, rewrite the function, and override to your base class.

Now, your question is: why we need “virtual” keyword before run_phase task in your project’s driver class (which is extended in uvm_driver class). The answer is: not necessary to have “virtual” keyword, because the run_phase task is always virtual implicitly, and your projects’ driver class always has polymorphism ability.

You can check easily, the uvm_driver is extended from uvm_component which has run_phase task declared as virtual.
According to LMR:

once a method has been identified as virtual, it shall remain virtual in any subclass that overrides it. In that case, the virtual keyword may be used in later declarations, but is not required.

That’s why the run_phase task of uvm_driver, your project’s driver class (extended from uvm_driver), so on… are virtual no matter you declare virtual keyword or not.

In reply to chris90:

Hi @chris90, Thanks for ur reply.

I am aware of polymorphism, and virtual methods also.

" once a method has been identified as virtual, it shall remain virtual in any subclass that overrides it. In that case, the virtual keyword may be used in later declarations, but is not required. "

Q1 : without virtual keyword in extended classes(sub classes) i.e. we need to assign the child class handler to base class handler how it will override.

I shared one link can u pls explain with that example. If i am wrong pls correct me.

Thanks In advance

In reply to VK_18:

In your example:

  • uvm_driver is base class, and you declared run_phase with virtual.
  • my_driver is derived from uvm_driver, you didn’t declare run_phase with virtual keyword, but according to LRM, the run_phase of my_driver is virtual implicitly.
  • update_driver is derived from my_driver, and the run_phase also virtual implicitly.

When you assign m_inst = up_inst, the polymorphism happened, you can see the message when you call m_inst.run_phase: “update_driver”, it means the run_phase of update_driver was executed.

Let remove the virtual keyword in uvm_driver class, you will see the message: “my_driver”.

In reply to chris90:

The keyword ‘virtual’ in connection with a method (task/function) provides the so called ‘late binding’. This is arun-time redirection of method calls. When a virtual method is called, the method implementation is chosen based on the object’s type, so you automatically get the correct implementation for that object.

@chr_sue

In reply to chr_sue:

In reply to chris90:
The keyword ‘virtual’ in connection with a method (task/function) provides the so called ‘late binding’. This is arun-time redirection of method calls. When a virtual method is called, the method implementation is chosen based on the object’s type, so you automatically get the correct implementation for that object.

I didn’t understand the sentence. can u elaborate.

Thanks in advance

In reply to VK_18:

@chr_sue
In reply to chr_sue:
I didn’t understand the sentence. can u elaborate.
Thanks in advance

This is concept in OOP, you can search “late binding” (also “early binding” to understand more).