Changes between Version 98 and Version 99 of IR2


Ignore:
Timestamp:
05/14/21 10:35:15 (5 years ago)
Author:
siegel
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • IR2

    v98 v99  
    1717
    18181. **Do variables have default initial values?**
    19   * Every variable holds a value of its type, always.  However the initial values of variables is unspecified.   Therefore a correct CIVL-IR program should explicitly initialize every variable before it is used.  Moreover, there is a defined bit associated to each variable, and it is initially false, indicating that the variable is "undefined".   It becomes defined when it is assigned.  Details are below.
     19  * No.  Every variable must be explicitly initialized.  However, `$undef` can be used as the initializer, indicating that the variable is undefined.  Even in this case, the variable has some unspecified value of its type.   Details are below.
    20201. **How do you initialize a variable?**
    21   * By assigning a value to it.   For example `n=$new($int);` will assign `n` an arbitrary integer, while `n=0;` will assign the integer `0` to `n`.
     21  * In the declaration.   For example `n=$new($int);` will assign `n` an arbitrary integer, while `n=0;` will assign the integer `0` to `n`.
    22221. **How is an array allocated?**
    23   * An array variable `a` is declared with a decl such as `T a[];`, and then a statement such as `a=$new_array(n, T);` will assign to `a` a new array value for an array of length `n` of elements of type `T` in which all entries are undefined.   (Alternatively, `a=$new(T[n])` will create an array of arbitrary defined values.)  For heap-allocation, a pointer is declared with a decl such as `T * p;`, and a heap variable is also declared somewhere with a decl such as `$heap heap;` and then a statement such as `p = $alloc(&heap, n, T);` will add a new object to the heap and return a pointer to the first element.  An `$alloc`-ed object can be deallocated with `$free(p);`.
     23  * An array variable `a` is declared with a decl such as `T a[] = $new_array(n, T);` which assigns to `a` a new array value for an array of length `n` of elements of type `T` in which all entries are undefined.   (Alternatively, `T a[] = $new(T[n])` will create an array of arbitrary defined values.)  For heap-allocation, `T * p = $undef;` followed by the statement `p = $alloc(&heap, n, T);` will add a new object to the heap and return a pointer to the first element.  An `$alloc`-ed object can be deallocated with `$free(p);`.
    24241. **Is there an "array-pointer pun", as in C?**
    2525  * No, if you want a pointer to element 0 of an array, you have to explicitly write something like `&a[0]`.
     
    5151  ';'
    5252  ;
    53 decl: type-param-list? qualifier* type-specifier declarator contract-clause* ';' ;
    54 function-definition: type-param-list? qualifier* type-specifier declarator contract-clause* block ;
    55 qualifier:
     53decl: function-decl | object-decl ;
     54function-decl: type-param-list? function-qualifier*  type-specifier declarator contract-clause* ';' ;
     55object-decl: type-specifier declarator '=' initializer ';' ;
     56function-definition: type-param-list? function-qualifier* type-specifier declarator contract-clause* block ;
     57function-qualifier:
    5658    '$abstract_f'  /* abstract function; only for function decls */
    5759  | '$pure_f'  /* function is a mathematical function of its parameters */
     
    169171  | lvalue '=' expr ';'  /* assignment */
    170172  | (lvalue '=')? expr type-args? '(' expression-list? ')' ';'  /* function call */
    171   | (lvalue '=')? '$spawn' expr type-args? '(' expression-list? ')' ';'  /* process creation */
     173  | lvalue '=' '$spawn' expr type-args? '(' expression-list? ')' ';'  /* process creation */
    172174  | '$wait' expr ';'  /* wait until p terminates */
    173175  | 'return' expr? ';'  /* return from function call */
    174   | (lvalue '=')? '$alloc' '(' expr ',' expr ',' type-name ')' ';'  /* heap allocation */
    175   | '$free' expr ';'  /* frees something that was $alloc-ed */
    176   | (lvalue '=')? '$new_array' '(' expr ',' type-name ')' ';'  /* allocation of array of undefined values*/
    177176  | '$atomic_enter' ';'  /* enter atomic region */
    178177  | '$atomic_exit' ';'  /* exit atomic region */
     
    227226  | expr type-args? '(' expression-list? ')'  /* application of abstract function (and perhaps other functions?) */
    228227  | '$new' '(' type-name ')'  /* returns a new arbitrary value of the given type */
     228  | '$new_array' '(' expr ',' type-name ')'  /* allocation of array of undefined values*/
    229229  | '$forall' '(' decl expr? ')' expr  /* universal quantification */
    230230  | '$exists' '(' decl expr? ')' expr  /* existential quantification */
     
    239239expr-pair-list: expr-pair (',' expr-pair)* ;
    240240expr-pair: '{' expr ',' expr '}' ;
    241 
     241initializer: expr | '$undef' ;
    242242}}}
    243243
     
    300300
    301301{{{
    302 $system void $exit(void);
    303 $system $bool $terminated($proc p);
    304 $system int $choose_int($int n); // how is this different than $new($int)?
     302<T> $system alloc($heap * h, $int n);  // heap allocation of n elements of type T
     303$system $free($void * p);  // frees something that was $alloc-ed
     304$system void $exit();  // terminate process immediately
     305$system $bool $terminated($proc p);  // has the process terminated?
     306$system int $choose_int($int n);  // nondeterministic choice of int in 0..n-1
    305307
    306308}}}