Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
intro_example:template_deps [2025/05/02 10:00] – created rajitintro_example:template_deps [2025/05/07 18:26] (current) rajit
Line 1: Line 1:
 ====== Dependent templates ====== ====== Dependent templates ======
  
-We've already seen how template parameters can be used to construct [[intro_example:chp_fifo|parameterized circuits]], as well as change the size of arrays in the [[intro_example:templates|process port list]].+We've already seen how template parameters can be used to construct [[intro_example:chp_fifo|parameterized circuits]], as well as change the size of arrays in the [[intro_example:templates|process port list]].  In addition, they can also be used to specify parameters used in the template itself. 
 + 
 +<code act> 
 +template<pint N; pint data[N]> 
 +defproc test(chan!(int) out) 
 +
 +    chp { 
 +       (; i : N : out!data[i]) 
 +     } 
 +
 +</code> 
 + 
 +In the example above, the process ''test'' takes a parameter ''N'', followed by a parameter array of size ''N''
 + 
 +<code act> 
 +pint x[3]; 
 +x[0] = 1; x[1] = 3; x[2] = 5; 
 +test<3,x> t; 
 +</code> 
 + 
 +In the above example, the CHP body for instance ''t'' would be expanded into 
 +<code act> 
 +  out!1; out!3; out!5 
 +</code> 
 + 
 +Dependent templates have //left to right// evaluation order. In the example above this means that ''t.N'' is created and assigned value ''3'', and then after that the value of ''t.N'' can be used to infer the type of ''pint d[N]''. Switching the order would result in an error: 
 +<code act> 
 +template<pint data[N]; pint N> defproc test(chan!(int) out); 
 +</code> 
 +<code> 
 +-[ERROR]-> The identifier `N' does not exist in the current scope 
 +</code> 
 + 
 + 
 +Parameters can be used in expressions as well. For example, the memory definition in the [[stdlib:start|ACT standard library]] is: 
 +<code act> 
 +export template<pint N, W> 
 +defproc ram (chan?(int<2>) rd; chan?(int<std::ceil_log2(N)>) addr; 
 +             chan?(int<W>) din; chan!(int<W>) dout) 
 +
 +  ... 
 +
 +</code> 
 +The first template parameter ''N'' corresponds to the number of elements in the memory, and ''W'' corresponds to the bit-width of the data in the memory. The bit-width of the address channel ''addr'' is computed from ''N'' by a standard library parameter function that is used to compute the smallest number of address bits needed to be able to hold the address values.