Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get name of an instance using a method operating on it in SystemVerilog?

Is there any way a method can get name of the object it operates on in SystemVerilog ?

Like implementing

object1.printName()  

should print the string

object1
like image 686
Jean Avatar asked Sep 26 '12 19:09

Jean


4 Answers

No, there is no mechanism in the language for doing this for classes.

For modules, you can use the %m format specifier for displaying a hierarchical name. But for classes, the output using %m shows the class type name, not the instance name. (At least that was the observed behavior with Incisive and Questa.) Also note that %m will include the the function name if called from within a function.

Example:

module test;

  // Print %m in a module
  function void printName();
    $display("%m");
  endfunction

  class foo;

    // Print %m in a class
    virtual function void printName();
      $display("%m");
    endfunction

  endclass

  foo foo_inst = new;

endmodule

module top;
   test test_inst();

   initial begin
     test_inst.foo_inst.printName();
     test_inst.printName();
   end
endmodule

Output:

top.test_inst.foo.printName
top.test_inst.printName

If the output from %m is useful, you can capture it in a string using $sformatf and then modify or do whatever with it.

like image 55
dwikle Avatar answered Nov 15 '22 11:11

dwikle


Instances don't have names. Consider this code:

someObject a
someObject b

initial begin
    a = new();
    b = a;
    a.printName();
    b.printName();
end

a and b point to the same instance of someObject. We only created one. Hence both calls would have to report the same name, but you want them to report the name of the handle we used to access them. That's just not possible.


OVM/UVM objects contain a member variable which contains an instance name. It can be given on construction or set using set_name(). It can be read using get_name(). Even if the objects you're talking about aren't OVM/UVM you can use a similar system.

like image 39
Paul S Avatar answered Nov 15 '22 10:11

Paul S


If you are using OVM/UVM then get_full_name() / get_name() will return the name of the component in the testbench hierarchy.

Objects are dynamic, so implicitly the question you are asking is invalid.

When someone wants to implement object names, what they'll do is pass a "string name" in the constructor of every object, so when the object is new'd the parent will say what it's name is.

If you look at every component in OVM/UVM, you'll see the constructor signature: function new(string name="", ovm_component parent=null)

these are used by the base classes to implement get_full_name() / get_name()

like image 26
engtech Avatar answered Nov 15 '22 12:11

engtech


For something that high-level, I'm not sure if there's something that's precisely as you describe.

However, there is the $typename system task. I don't know how it works with objects of classes, however. I have not found need for this before.

Usually what I see done (and what I do, as I find it useful), is to create a string stored in the class that is assigned by the constructor, which is the "name" of the object. Then I can use it when logging, so I know where different messages came from. Of course, this then depends on you creating new variables with useful names.

like image 41
Ben Richards Avatar answered Nov 15 '22 11:11

Ben Richards