Static method v/s method with static lifetime

Hi,
I have a question on static method v/s method with static lifetime. System verilog LRM_1800-2012 says “A static method is different from a task with static lifetime. The former refers to the lifetime of the method within the class, while the latter refers to the lifetime of the arguments and variables within the task.” (section 8.10). I am not clear about what “lifetime of method within class” means. My understanding is that there will be a single copy of the methods (the code of the method) of a given class irrespective of the number of objects of that class (unlike the data members which will have separate copies for each class object or instance of the class) and the method (the code of the method) will exist in the memory irrespective of the data members of the class (which will be destroyed when the memory of the particular instance is released). For example in the following code, the class handle n is automatic and will be destroyed when the task ends and the memory for myclass instance (object pointed to by handle n) data members will be released if there are no more handles. In that case, what happens to memory of the code for member function f1 (which is nonstatic) and f2(which is static). Does the above statement “lifetime of the method” means the memory for static methods of class will remain and will not be released even if there is no instance of the class and memory for non-static function f1 will be released if there is no class instance existing.

Can someone help me to understand this.

-sunil

 

class myclass;
  int x;
  logic y;
  function f1(int a);
  //code of f1
  endfunction
  static function f2(int b);
  //code of f2
  endfunction
endclass

module msl;
 
  int st0; // static
  initial begin
    int st1; // static
    static int st2; // static
    automatic int auto1; // automatic
  end
  task automatic t1();
    int auto2;
    static int st3; // static
    automatic int auto3; // automatic
    myclass n;
    n = new;
    //more code ..
  endtask

endmodule

In reply to puranik.sunil@tcs.com:

See if these posts help you

https://verificationacademy.com/forums/systemverilog/automatic-variables-inside-static-method#reply-42858

https://verificationacademy.com/forums/systemverilog/what-exact-difference-between-static-tasks/functions-and-automatic-tasks/functions-please-explain-clear-example#reply-44935

Hi Dave,
thanks for the reply and links. I understand the difference between “static function” and “function static”. The former is the static function of class (similar to C++) and exists irrespective of the class instances while the “function static” means variables/parameters of the function are static in nature and not automatic. (not passed on the stack)
My question is slightly different and is about the methods of the class. For any class, there will be a single copy of the class methods (unlike data members for which each class instance will have separate copy). Now is the method of class (static or non-static) allocated memory when class is declared or when at least one instance of class is declared? Is the memory allocated to class method(the code of method) deallocated when memory all class instances is released and they are garbage collected or the memory still exists if there is no instance of the class? Again is this behavior different for static and non static class methods?
rgs,
-sunil

In reply to puranik.sunil@tcs.com:
Sunil,
Code is always generated at elaboration, before time 0. If you know C++, code is part of output of a compiler and exists in an object file that get loaded when you execute a program. There is only one copy of code for each task or function regardless of its lifetime, or whether it is a static, or non-static class method. The lifetime of a non-class task and functions only affect the lifetime of their arguments and the default lifetime of variables declared within. The lifetime of a class method is always automatic.

The question of whether code gets generated if no one references a task or function is left as a tool optimization. If you never reference something, you have no way of knowing if the code was generated or not (See the observer effect). I suspect it would be quite difficult to figure out beyond one level of references.

Hi Dave,
thanks for the clarification.I have one more question: System verilog LRM section 8.20 says “A method of a class may be identified with the keyword virtual. Virtual methods are a basic polymorphic construct. A virtual method shall override a method in all of its base classes, whereas a non-virtual method shall only override a method in that class and its descendants. One way to view this is that there is only one implementation of a virtual method per class hierarchy, and it is always the one in the latest derived class.”

I am not clear about the statement “whereas a non-virtual method shall only override a method in that class and its descendants.” My understanding is that if there are two non virtual methods of same name in derived class and base class, the one in derived class “hides” the base class one. (even if they have different parameters). How can a non virtual method override a method in “same class” or its “derived classes”? can you please explain.
rgs,
-sunil