Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
language:expressions [2024/07/19 14:22] – [Expressions in CHP] rajitlanguage:expressions [2025/04/21 17:39] (current) – [Structure references] rajit
Line 55: Line 55:
  
    * Each variable has the bit-width specified by its type.    * Each variable has the bit-width specified by its type.
-   * A constant uses the minimum number of bits needed to represent it. Note that a negative constant is assumed to be a two's complement value, an its bit-width is determined in the same way.+   * A constant uses the minimum number of bits needed to represent it. Note that a negative constant is assumed to be a two's complement value, an its bit-width is determined in the same way. If you are unsure about what is going to happen and want to control the bit-width of a constant, ''int(const, width)'' can be used. Be careful about is negative constants, since the ''int(.)'' operation will zero-extend a value and not sign-extend it.
    * For unary operators, the bit-width of the result is the same as the bit-width of the argument    * For unary operators, the bit-width of the result is the same as the bit-width of the argument
    * For binary operators and ternary operators where the result is an integer, let //left// be the bit-width of the left-hand side of the operator, and //right// be the bit-width of the right hand side. There are six categories of result bit-widths:    * For binary operators and ternary operators where the result is an integer, let //left// be the bit-width of the left-hand side of the operator, and //right// be the bit-width of the right hand side. There are six categories of result bit-widths:
Line 76: Line 76:
      - //left// + 2^//right// - 1      - //left// + 2^//right// - 1
         * 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 bitfield extraction, the bitwidth is determined by the number of bits extracted. 
 + 
 +==== Idiosyncrasies ====
  
 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: 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:
Line 182: Line 185:
   }   }
 } }
 +</code>
 +
 +===== Structure references =====
 +
 +Functions can return [[language:types2:data#pure_structures|pure structures]], and pure structures can have function methods. The expression syntax is rich enough to support these features. As an illustrative example, consider the following:
 +
 +<code act>
 +deftype mystruct (int a, b)
 +{
 +     methods {
 +          function getsum() : int
 +          {
 +              chp {
 +                  self := a + b
 +              }
 +         }
 +         function addone() : mystruct
 +         {
 +            chp {
 +                self.a := a + 1;
 +                self.b := b + 1
 +             }
 +         }
 +    }
 +}
 +</code>
 +
 +We can have expressions of the following form:
 +
 +<code act>
 +mystruct s;
 +int a;
 +...
 +chp {
 +   ...
 +   a := s.addone().getsum();
 +   ...
 +
 </code> </code>