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
Next revision Both sides next revision
language:langs:chp [2021/09/13 10:57]
rajit
language:langs:chp [2022/05/13 08:48]
rajit
Line 2: Line 2:
  
 The CHP sublanguage is an evolution of Dijkstra's guarded commands notation and Hoare's CSP. It is a programming notation augmented with send/receive primitives for communication and parallel composition. A chp program is specified in ACT as follows: The CHP sublanguage is an evolution of Dijkstra's guarded commands notation and Hoare's CSP. It is a programming notation augmented with send/receive primitives for communication and parallel composition. A chp program is specified in ACT as follows:
-<code>+<code act>
 chp {  chp { 
    /* program goes here */    /* program goes here */
Line 19: Line 19:
    * ''X!e'': communication action (send). The expression ''e'' is evaluated and sent over channel ''X''. Communication is blocking (slack zero). ''X!'' is also supported, which means complete the synchronization operation. This is normally used when no data needs to be sent on the channel.    * ''X!e'': communication action (send). The expression ''e'' is evaluated and sent over channel ''X''. Communication is blocking (slack zero). ''X!'' is also supported, which means complete the synchronization operation. This is normally used when no data needs to be sent on the channel.
    * ''X?v'': communication action (receive). ''v'' is a variable, and the value received on channel ''X'' is assigned to ''v''. ''X?'' is also supported, which means complete the synchronization operation. This is normally used when no data needs to be sent on the channel.    * ''X?v'': communication action (receive). ''v'' is a variable, and the value received on channel ''X'' is assigned to ''v''. ''X?'' is also supported, which means complete the synchronization operation. This is normally used when no data needs to be sent on the channel.
 +
 +==== Typechecking ====
 +
 +Expressions are evaluated using the [[language:expressions|standard expression rules]] for bit-width conversion. All operations are unsigned. Subtraction uses two's complement arithmetic, and unary minus computes the two's complement of the integer.
 +
 +Variables can be set via either assignment statements or via receive operations. When an integer variable of bit-width //w1// is set to an integer value of bitwidth //w2//, then
 +   * If //w1//=//w2//, then a standard bit-wise assignment is performed.
 +   * If //w1//<//w2//, then the high-order bits of the value being assigned are discarded.
 +   * If //w1//>//w2//, the value being assigned is zero-extended prior to assignment
 +An integer cannot be assigned a Boolean value or vice versa. Explicit conversion via ''bool()'' and ''int()'' can be used to support this functionality.
 +
 +The same rules outlined above apply to receive operations, where the value being assigned is the value received from the channel. To support int/bool conversion during a receive operation, we also support the following extended syntax:
 +   * ''X?bool(v)'' : permits the assignment of a Boolean value to an integer variable ''v''
 +   * ''X?int(v)'' : permits the assignment of an integer value to a Boolean variable ''v''
  
 ===== Composition operators ===== ===== Composition operators =====
Line 26: Line 40:
 Since circuit instances implicitly run in parallel, there's no actual requirement for an explicit parallel composition operator at the level of circuit modules. Internal parallelism where the two statements do not write a variable that is read by the other can use the '','' separator. The comma has a higher binding power compared to semicolon. Since circuit instances implicitly run in parallel, there's no actual requirement for an explicit parallel composition operator at the level of circuit modules. Internal parallelism where the two statements do not write a variable that is read by the other can use the '','' separator. The comma has a higher binding power compared to semicolon.
  
-<code>+<code act>
   x+,y+;z-   x+,y+;z-
 </code> </code>
Line 34: Line 48:
  
 The selection statement uses the guarded command syntax. The statement The selection statement uses the guarded command syntax. The statement
-<code>+<code act>
  [ g1 -> S1  [ g1 -> S1
  [] g2 -> S2  [] g2 -> S2
Line 47: Line 61:
  
 Often a circuit simply waits for some condition to be true before proceeding. This would be written  Often a circuit simply waits for some condition to be true before proceeding. This would be written 
-<code>+<code act>
 [ cond -> skip ] [ cond -> skip ]
 </code> </code>
 This happens so often that there is syntactic sugar to support this particular construct, shown below: This happens so often that there is syntactic sugar to support this particular construct, shown below:
-<code> +<code act
-[code]+[cond]
 </code> </code>
 For example, For example,
-<code>+<code act>
  [xi];xo+  [xi];xo+
 </code> </code>
Line 63: Line 77:
  
 Sometimes the selection statement can have multiple guards that are true. This requires the use of a non-deterministic circuit (an arbiter) to resolve the choice, and introduces implementation overhead. Hence, the programmer is required to use different syntax for non-deterministic selections to make this overhead explicit and visible. A non-deterministic selection is written as follows: Sometimes the selection statement can have multiple guards that are true. This requires the use of a non-deterministic circuit (an arbiter) to resolve the choice, and introduces implementation overhead. Hence, the programmer is required to use different syntax for non-deterministic selections to make this overhead explicit and visible. A non-deterministic selection is written as follows:
-<code>+<code act>
 [| g1 -> S1 [| g1 -> S1
 [] g2 -> S2  [] g2 -> S2 
Line 76: Line 90:
  
 Loops/repetitions use a syntax similar to selections, with the addition of the Kleene star.  Loops/repetitions use a syntax similar to selections, with the addition of the Kleene star. 
-<code>+<code act>
 *[ g1 -> S1 *[ g1 -> S1
 [] g2 -> S2 [] g2 -> S2
Line 86: Line 100:
  
 One of the most common cases in using loops is the infinite repetition. This is typically the "outer" loop in programs that describe most circuits. This would be written One of the most common cases in using loops is the infinite repetition. This is typically the "outer" loop in programs that describe most circuits. This would be written
-<code>+<code act>
 *[ true -> S ] *[ true -> S ]
 </code> </code>
 Since this is so common, the following syntactic sugar is provided to make this even more compact: Since this is so common, the following syntactic sugar is provided to make this even more compact:
-<code>+<code act>
 *[ S ] *[ S ]
 </code> </code>
  
 Using this, the classic greatest common divisor algorithm can be written as follows: Using this, the classic greatest common divisor algorithm can be written as follows:
-<code>+<code act>
 chp {  chp { 
  *[ X?x,Y?y;  *[ X?x,Y?y;
Line 108: Line 122:
  
 One last supported construct is a do-while loop. The main difference between the standard loop and the do-loop is that we are guaranteed that the loop will execute at least one iteration. This information can be useful during circuit synthesis. The syntax for a do-while loop is: One last supported construct is a do-while loop. The main difference between the standard loop and the do-loop is that we are guaranteed that the loop will execute at least one iteration. This information can be useful during circuit synthesis. The syntax for a do-while loop is:
-<code>+<code act>
 *[ S <- G ] *[ S <- G ]
 </code> </code>
 A do-while loop can have only one guard. This program executes ''S'', and then evaluates the guard ''G''. If ''G'' is true, then the loop repeats; otherwise, the loop terminates. A do-while loop can have only one guard. This program executes ''S'', and then evaluates the guard ''G''. If ''G'' is true, then the loop repeats; otherwise, the loop terminates.
 +
 +Loop guards can only use local variables. Hence, it is an error if a variable appearing in a loop guard is either (i) a global variable; or (ii) accessible via the port list of the process; or (iii) involves a channel probe. This ensures that loop guards cannot include any shared variables.
  
 ==== Channels in expressions ==== ==== Channels in expressions ====
Line 120: Line 136:
  
 The following program takes the arriving data on two different input channels, and merges them into a data sequence on its output channel: The following program takes the arriving data on two different input channels, and merges them into a data sequence on its output channel:
-<code>+<code act>
 *[ [| #A -> A?x *[ [| #A -> A?x
    [] #B -> B?x    [] #B -> B?x
Line 128: Line 144:
 </code> </code>
 This is sometimes called a non-deterministic merge. Note that a channel can be probed at either end (i.e. the sender end or the receiver end can probe the channel), but not at both. If we knew that the inputs on ''A'' and ''B'' are guaranteed to be mutually exclusive, then this can be written This is sometimes called a non-deterministic merge. Note that a channel can be probed at either end (i.e. the sender end or the receiver end can probe the channel), but not at both. If we knew that the inputs on ''A'' and ''B'' are guaranteed to be mutually exclusive, then this can be written
-<code>+<code act>
 *[[#A -> A?x *[[#A -> A?x
   []#B -> B?x   []#B -> B?x
Line 141: Line 157:
  
 If ''A'' is an input port, then ''A'' can participate in a //channel expression//. For example, we can write: If ''A'' is an input port, then ''A'' can participate in a //channel expression//. For example, we can write:
-<code>+<code act>
 *[[A=3 -> X!true;A?x *[[A=3 -> X!true;A?x
  []A!=3 -> X!false;A?x  []A!=3 -> X!false;A?x
Line 149: Line 165:
  
 Channel expressions can be used in any context that includes an expression (e.g. assignment statements, send operations). However, evaluating the expression will result in an error if the probe of the appropriate channels is false; i.e. the assignment statement //must be// non-blocking. So, if ''A'' is an input channel, the statement Channel expressions can be used in any context that includes an expression (e.g. assignment statements, send operations). However, evaluating the expression will result in an error if the probe of the appropriate channels is false; i.e. the assignment statement //must be// non-blocking. So, if ''A'' is an input channel, the statement
-<code>+<code act>
 x:=A x:=A
 </code> </code>
 might fail, the following alternative will not. might fail, the following alternative will not.
-<code>+<code act>
 [#A];x:=A [#A];x:=A
 </code> </code>
Line 160: Line 176:
  
 The interaction of Boolean expressions, probes, and channel expressions can be a bit tricky. For example, consider the following guards: The interaction of Boolean expressions, probes, and channel expressions can be a bit tricky. For example, consider the following guards:
-<code>+<code act>
 [ #A | #B -> ...  [ #A | #B -> ... 
 [] #C -> ... [] #C -> ...
Line 166: Line 182:
 </code> </code>
 The standard way ORs (and AND) operators are viewed is via short-circuit evaluation. Hence, the first guard can evaluate to true as soon as either ''#A'' or ''#B'' is true. Instead, suppose we had: The standard way ORs (and AND) operators are viewed is via short-circuit evaluation. Hence, the first guard can evaluate to true as soon as either ''#A'' or ''#B'' is true. Instead, suppose we had:
-<code>+<code act>
 [ A=0 | B=0 -> ... [ A=0 | B=0 -> ...
 [] #C -> ... [] #C -> ...
Line 174: Line 190:
  
 What about: What about:
-<code>+<code act>
 [| ~(#A | ~#B) -> ... [| ~(#A | ~#B) -> ...
 [] #C -> ... [] #C -> ...
Line 203: Line 219:
 Essentially every channel variable ''V'' can be translated into ''(#V ? V : ERR)''. Note that this is a different translation compared to guards. Note that since probes are only allowed in guards of selection statements, negated probes are not possible in other contexts.  Essentially every channel variable ''V'' can be translated into ''(#V ? V : ERR)''. Note that this is a different translation compared to guards. Note that since probes are only allowed in guards of selection statements, negated probes are not possible in other contexts. 
  
 +==== Exchange channels ====
 +
 +Exchange channels are those where data is exchanged between sender and receiver, and this is indicated directly in the channel type. The syntax for exchange channels is:
 +   * ''X!e?v'' : exchange send operation, sends ''e'' and receives into variable ''v''
 +   * ''X?v!e'' : exchange receive operation, receives into ''v'' and sends the value ''e''
 +In this case, we assume that the exchange send will initiate the operation, and hence only the exchange receive can be probed. This doesn't restrict functionality, since the symmetry of the exchange channel means we can simply swap the two ends.
  
 +==== Split synchronization ====
  
 +Four-phase handshake channels involve two synchronizations. If you need to make this explicit in the CHP, the ''!'' (send) and ''?'' (receive) operators can be replaced with ''!+'' (first half of send), ''!-'' (second half of send), or ''?+'' (first half of receive), ''?-'' (second half of receive). The second half is purely synchronization and should not have any data.