LRM 18.5.12 :: Function in Constraints

Hi ,

I need assistance with following line from LRM .
LRM 18.5.12 :: Function in Constraints states states that


" Functions that appear in constraint expressions should be automatic (or preserve no state information) and have no side effects "

I understand that function with static lifetimes are illegal in classes .


  class ..
    ...
    function static ..  // Illegal !!
   ....
  endclass

However within a class I can have a static variable within automatic function ( by default function are automatic ) right ?


  class ..
    ...
    function automatic ..  
     static variable ; // Static Variable , Legal ?
    ....
  endclass

[1] So is it legal to call such a function ( with static variables ) in constraint ?
I mean to ask in context of LRM .
( Across Simulators I observe that they do allow , but I am not certain if its LRM violation or not )

[2] Also what does “and have no side effects” mean ?

 If possible a small snippet would be helpful 

Thanks

In reply to Have_A_Doubt:

https://verificationacademy.com/forums/systemverilog/“static-task”-vs.-“task-static”#reply-53393

The basic point here is that you should be able to call a function with a certain set of arguments and always get the same return value no matter how many times the function gets called or what order

In reply to dave_59:

Thanks , will have a look into the links .

w.r.t the last statement .

Since the LRM says the order of calling function in Constraint is Unspecified
( $display() statements within the function prove this ) , I tried following example ::


class FUNCTION_IN_Constraint ;

rand bit [1:0]  Arr[][];
 
rand bit [1:0] Q[];
 
 
constraint SIZE_CONSTRAINT_1D { Arr.size()  == 2 ; }
 
 
constraint S2_2d {       foreach(Arr[i])
                      {
		         Arr[i].size() == 2 ;	  
		      }
	         }
 
constraint Q_SIZE { Q.size() == 4 ; }   
 

 
constraint Q_VAL {  
                        foreach( Q[i] ) 
                     {
                           Q[i] == ELEMENT(Arr,i) ;

		      }
 
		 }    

function int ELEMENT ( input bit [1:0]  AB[][] , input int index );
	 
	 foreach(AB[i,j])
	   begin
            
	     if ( index == ( 2*i + j ) )
             begin
 
               return AB[i][j] ;

	     end
         
	   end
 
endfunction
 
endclass


Am I correct to expect the following as possible Outputs post randomization ::

‘{Arr:’{‘{0, 0}, ‘{0, 0}}, Q:’{0, 0, 0, 0}}
‘{Arr:’{’{1, 0}, ‘{1, 0}}, Q:’{1, 0, 1, 0}}
‘{Arr:’{‘{1, 1}, ‘{1, 0}}, Q:’{1, 1, 1, 0}}
‘{Arr:’{’{1, 1}, ‘{1, 0}}, q:’{1, 1, 1, 0}}

Since Unpacked Array ‘Arr’ is input to function it would be solved implicitly before
the function gets called .

So the order in which the function gets called shouldn’t matter since Arr has its
element values by then .