Tudor Timi
Tudor Timi

Reputation: 7573

Configure 'print' to display full struct fields

I have nested structs defined as follows:

struct some_struct {
  some_field : uint;
  some_struct_field : some_other_struct;
  some_other_field : uint;
};

struct some_other_struct {
  some_field : uint;
  some_other_field : uint;
};

When I print an instance of some_struct using:

extend sys {
  run() is also {
    var some_struct : some_struct;
    gen some_struct;
    print some_struct;
  };
};

I get the following:

  some_struct = some_struct-@1: some_struct   of unit: sys 
    ----------------------------------------------  @test
0   some_field:                     3435783455
1   some_struct_field:              some_other_struct-@2
2   some_other_field:               2907638895

I'd like to see a detailed display of some_struct_field, which would show its sub-fields. Something like:

  some_struct = some_struct-@1: some_struct   of unit: sys 
    ----------------------------------------------  @test
0   some_field:                     3435783455
1   some_struct_field:              some_other_struct-@2
    0   some_field:                 1753518447
    1   some_other_field:           1744092907
2   some_other_field:               2907638895

I've tried using print ... full=TRUE, but that doesn't help either. I can't find any config knob for this behavior.

Do fields have any kind of attributes to customize how they're printed (like in UVM/SV field macros)?

I know there's a do_print() method that gets called when printing, that I can use to customize the displayed text, but I don't see how I could use it without re-implementing the whole print routine. If there were a way to capture the text that gets constructed, I could use that.

Could anyone help me out here?

Upvotes: 0

Views: 1038

Answers (1)

yuvalg
yuvalg

Reputation: 331

Generally it is not recommended to edit the do_print() method of any_struct to print the struct’s content recursively. This is because some structs have pointers to their parent or another struct that is points back to this one (like driver and bfm), And If you apply the modified do_print() method (by using ‘print’) to such a struct, will cause a never ending recursive printing Which will most likely end in an OS 11 segmentation violation (due to stack overflow).

I would suggest creating a new method in any_struct which you will then use to print structs that do not have cyclic pointers Anywhere in them. My ides of such a method will make use of the reflection mechanism and will look like this:

extend any_struct{
printMe(s:string="") is{
    var fld_indx:uint=0;
    var printed_line:string;
    var get_struct: rf_struct = rf_manager.get_struct_of_instance(me); // get a reflection pointer to me
    var fields_in_struct : list of rf_field = get_struct.get_fields(); // get all my fields
    for each (strct_field) in fields_in_struct  do { 
        printed_line=append(s,fld_indx," ",strct_field.get_name());    // build the string that will display for this field
        out(str_pad(printed_line,40)," : ",strct_field.get_type().value_to_string(strct_field.get_value_unsafe(me)));
        var elem_type : rf_type = strct_field.get_type();              
        if (elem_type is a rf_struct) {                                // if the field is a struct, call this method recursively
            var st:any_struct= strct_field.get_value_unsafe(me).unsafe() ; 
            st.printMe(append(s,"    "));
        };
            fld_indx=fld_indx+1;
    };
 };
};

And you can then define a macro that will handle this in a manner more similar to how the print action looks like:

define <printr'action> "printr <exp>" as {
<exp>.printMe();
};

In the code, you can then write:

printr me; // usage example

Upvotes: 1

Related Questions