Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
language:expressions [2022/12/13 11:08]
rajit [External circuit functions]
language:expressions [2024/03/20 07:19] (current)
rajit [External circuit functions]
Line 9: Line 9:
    * The query expression ''(e ? e1 : e2)'' behaves like the C operator. The result is ''e1'' if ''e'' is true, and ''e2'' otherwise.    * The query expression ''(e ? e1 : e2)'' behaves like the C operator. The result is ''e1'' if ''e'' is true, and ''e2'' otherwise.
    * Logical shift operators are ''<<'' (left shift),  ''>>'' (logical right shift), and the arithmetic right shift operator ''>>>''.    * Logical shift operators are ''<<'' (left shift),  ''>>'' (logical right shift), and the arithmetic right shift operator ''>>>''.
-   * Bit operations: For a variable ''x'', the value ''x{b..a}'' extracts bits ''b'' through ''a'' (both included, with ''b'' at least ''a''). Both ''b'' and ''a'' must be constants (or computed by parameters, i.e. constants after expansion). The syntax ''x{a}'' is syntactic sugar for ''x{a..a}'', and extracts one bit from ''x''.+   * Bit operations: For a variable ''x'', the value ''x{b..a}'' extracts bits ''b'' through ''a'' (both included, with ''b'' at least ''a''). Both ''b'' and ''a'' must be constants (or computed by parameters, i.e. constants after expansion). The syntax ''x{a}'' is syntactic sugar for ''x{a..a}'', and extracts one bit from ''x''. Note that ''b'' must be at least ''a''; for example ''x{3..2}'' corresponds to two bits, but ''x{2..3}'' is not a valid reference.
    * Concatenation: Given a set of expressions, ''{e1,e2,e3,...,eN}'' concatenates the bits of the expressions to form a wide result.    * Concatenation: Given a set of expressions, ''{e1,e2,e3,...,eN}'' concatenates the bits of the expressions to form a wide result.
  
Line 18: Line 18:
    * ''int(x,w)'': ''x'' must be an integer expression, and ''w'' must be evaluate to an integer constant after expansion (i.e. it can only be an integer expression that includes parameters). This changes the bit-width of the integer to be ''w''. If the width is reduced, the high order bits are  truncated; if the bit-width is increased, the integer is zero extended.    * ''int(x,w)'': ''x'' must be an integer expression, and ''w'' must be evaluate to an integer constant after expansion (i.e. it can only be an integer expression that includes parameters). This changes the bit-width of the integer to be ''w''. If the width is reduced, the high order bits are  truncated; if the bit-width is increased, the integer is zero extended.
    * ''bool(x)'': ''x'' must be an integer expression. This returns false if the integer is zero, and true otherwise.    * ''bool(x)'': ''x'' must be an integer expression. This returns false if the integer is zero, and true otherwise.
 +
 +Syntactic replication is also supported for the operators ''&'', ''|'', ''^'', ''+'', and ''*''. This means the following expression is valid
 +<code act>
 +  (+ i : 3 : p[i] + 2*i)
 +</code>
 +and is equivalent to
 +<code act>
 +  p[0] + 2*0 + p[1] + 2*1 + p[2] + 2*2
 +</code>
  
 ===== Parameters and constant expressions ===== ===== Parameters and constant expressions =====
  
-Parameter expressions are used to compute parameter values (''pint''/''pbool'') and are evaluated at expansion time. These expressions are signed integers, and use 64-bit integer arithmetic. All constants are simplified at expansion time using the same 64-bit integer arithmetic as parameters.+Parameter expressions are used to compute parameter values (''pint''/''pbool''/"preal") and are evaluated at expansion time. These expressions are signed integers, and use 64-bit integer arithmetic. All constants are simplified at expansion time using the same 64-bit integer arithmetic as parameters.
  
 +In the context of expressions in the ACT language where the entire expression evaluates to a run-time constant, ''int(x)'' can also be specified when ''x'' is a real expression. In this case, the fractional part of the number is discarded. This means, for example, that
 +<code act>
 +pint y = 2;
 +pint x = int(5.4/y)
 +</code>
 +will succeed, while 
 +<code act>
 +int x, y;
 +chp {
 +   y := 2;
 +   x := int(5.4/y)
 + }
 +</code>
 +will report an error.
 ===== Expressions in CHP ===== ===== Expressions in CHP =====
  
Line 50: Line 73:
         * left shift ''<<''         * left shift ''<<''
   * For concatenation, the bit-width is the sum of all the components. For the bitfield extraction, the bitwidth is determined by the number of bits extracted.   * For concatenation, the bit-width is the sum of all the components. For the bitfield extraction, the bitwidth is determined by the number of bits extracted.
 +
 +While these bit-width rules are nice because you never lose bits, they can have some unexpected consequences. One of the not-so-nice effects of these rules is that, technically, addition is no longer associative in general! For example, consider the following two different assignment statements:
 +
 +<code act>
 +int<2> a;
 +int<3> b;
 +int<4> c;
 +...
 +chp {
 +   ...
 +   x := (a + b) + c;
 +   y := a + (b + c);
 +   ...
 +}
 +</code>
 +
 +Applying the bit-width rules, the expression ''(a+b) + c'' has bit-width 5, whereas ''a + (b + c)'' has bit-width 6. While this does not have any consequences in this particular example, it could become problematic if the bitwise complement operator is used, or in the case where this expression needs to be negated (since subtraction is essentially taking the two's complement and then adding, which includes the bitwise complement operator).
 +
 +Another strange example is:
 +
 +<code act>
 +...
 +chp {
 +   ...
 +   x := x - 1;
 +   y := y + (-1);
 +   ...
 +}
 +</code>
 +
 +Now the right hand side of the first assignment takes ''x'' and ''1'' and does the subtraction that was expected. The second assignment, on the other hand, takes the bit-pattern for ''-1'' (this turns out to be ''1''), and adds it to ''y''! In other words, ''x-1'' and ''x+(-1)'' are not the same because of the way the bit-width rules operate. If you have any doubts, the ''int(...)'' operator can be used to specify the bit-width.
 +
  
 ===== Functions ===== ===== Functions =====
Line 190: Line 245:
 </code> </code>
  
-The specification of the mapping is similar to external parameter functions, but instead of the block being within a ''begin act''...''end'' block, it must be within a ''begin sim''..''end'' block.+The specification of the mapping configuration file is similar to the case of external parameter functions. The only difference is that the outer most ''begin''/''end''  block is ''begin sim''..''end'' instead of ''begin act''..''end''. 
 + 
 +An example of file I/O implemented with external functions can be found in the ''actsim'' [[https://github.com/asyncvlsi/actsim|git repository]] in the ''simlib/'' directory. 
 + 
 +===== Operator Precedence =====
  
 +The operators have the following precedence, from the highest to lowest:
 +   - ''~'', ''!''
 +   - ''*'', ''/'', ''%''
 +   - ''+'', ''-''
 +   - ''<<'', ''>>'', ''>>>'', ''<'', ''>'', ''<='', ''>='', ''='', ''!=''
 +   - ''&''
 +   - ''^''
 +   - ''|''
 +   - ''?''