Changes between Version 86 and Version 87 of IR2


Ignore:
Timestamp:
05/12/21 09:10:53 (5 years ago)
Author:
siegel
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • IR2

    v86 v87  
    2121  * 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`.
    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(T[n]);` will assign to `a` a new arbitrary array value for an array of length `n` of elements of type `T`.    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[];`, 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);`.
    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]`.
     
    177177  | (lvalue '=')? '$alloc' '(' expr ',' expr ',' type-name ')' ';'  /* heap allocation */
    178178  | '$free' expr ';'  /* frees something that was $alloc-ed */
     179  | (lvalue '=')? '$new_array' '(' expr ',' type-name ')' ';'  /* allocation of array of undefined values*/
    179180  | '$atomic_enter' ';'  /* enter atomic region */
    180181  | '$atomic_exit' ';'  /* exit atomic region */
     
    233234  | expr '?' expr ':' expr  /* if-then-else expression */
    234235  | '$length' '(' expr ')'  /* length of array */
    235   | '$defined' '(' lvalue ')'  /* is the value stored a well-defined value of its type? */
     236  | '$defined' '(' expr ')'  /* does evaluation of expr result in a well-defined value? */
     237  | '$valid '(' expr ')'  /* does a pointer point to a memory location capable of holding a value of its base type? */
    236238  ;
    237239expr-pair-list: expr-pair (',' expr-pair)* ;
     
    240242}}}
    241243
    242 * For float types, which rounding mode is used?
     244=== Notes ===
     245
     246* For float types, which rounding mode is used?  Round-to-nearest?
    243247* Comparisons likes `==` and `<` return a `$bool` (not an int, like C)
    244 * need to think about `$defined`.   How can you allocate an array and have all cells undefined?
     248* explanation of `$defined`: a bit is associated to each memory location.  Memory locations are created by declaring variables and allocation.   Every variable initially holds an undefined value.   Memory locations become defined by assigning to them defined values.
     249  * Constants are defined values.  This includes integer and real constants, and `NULL`.
     250  * An operation returns a defined value if all inputs are defined and the inputs are acceptable for the specific operation.
     251  * It is arguable about whether we should make division by 0, out of bound array indexes, etc., undefined values.  They could be defined.
     252  * Once an array is allocated with `$new_array`, the array itself is still undefined and all elements are undefined.  However `$defined($length(a))` will return `$true`.
     253  * Once an object is heap allocated with `p=$alloc(...)`, `p` becomes defined, but `*p`, `*(p+1)`, ..., are all undefined as they hold uninitialized data.
     254  * A pointer that points to the location one past the last element of an array or object is defined, but not valid.  The same holds for `NULL`.
     255  * If a pointer points to an object on the heap and that object is freed, the pointer becomes undefined.  Similarly, if it points into an object that goes out of scope, the pointer becomes undefined.
     256  * If an object is heap allocated then a pointer `p` into that object will be defined, but `*p` will be undefined (as the memory location pointed to by `p` has uninitialized data), but `$valid(p)` will return `$true`, indicating it is safe to write to that location.
    245257
    246258=== Allowed Casts ===