| 1 | \part{Language}
|
|---|
| 2 | \label{part:lang}
|
|---|
| 3 |
|
|---|
| 4 | \chapter{Overview of CIVL-C}
|
|---|
| 5 |
|
|---|
| 6 | CIVL-C is an extension of a subset of the C11 dialect of C. It
|
|---|
| 7 | includes the most commonly-used elements of C, including most of the
|
|---|
| 8 | syntax, types, expressions, and statements. Missing are some of the
|
|---|
| 9 | more esoteric type qualifiers and much of the standard library. None
|
|---|
| 10 | of the C11 language elements dealing with concurrency are included, as
|
|---|
| 11 | CIVL-C has its own concurrency primitives.
|
|---|
| 12 |
|
|---|
| 13 | The keywords in CIVL-C not already in C begin with symbol \cckey.
|
|---|
| 14 | This makes them readily identifiable and also prevents any naming
|
|---|
| 15 | conflicts with identifiers in C programs. This means that most
|
|---|
| 16 | legal C programs will also be legal CIVL-C programs.
|
|---|
| 17 |
|
|---|
| 18 | One of the most important features of CIVL-C not found in standard C
|
|---|
| 19 | is the ability to define functions in any scope. (Standard C allows
|
|---|
| 20 | function definitions only in the file scope.) This feature is also
|
|---|
| 21 | found in GNU C, the GNU extension of C.
|
|---|
| 22 |
|
|---|
| 23 | Another central CIVL-C feature is the ability to \emph{spawn}
|
|---|
| 24 | functions, i.e., run the function in a new process (thread).
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 | Key concepts: static scope tree, nested functions, dynamic scope tree,
|
|---|
| 30 | nested functions, processes, spawning, waiting, types, ...
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 | \chapter{Structure of a CIVL-C program}
|
|---|
| 34 |
|
|---|
| 35 | CIVL-C program may use preprocessor directives as specified in the C
|
|---|
| 36 | Standard. A source program is preprocessed, then parsed, resulting
|
|---|
| 37 | in a translation unit.
|
|---|
| 38 |
|
|---|
| 39 | A CIVL-C program begins with the line
|
|---|
| 40 | \begin{verbatim}
|
|---|
| 41 | #include <civlc.h>
|
|---|
| 42 | \end{verbatim}
|
|---|
| 43 | which includes the main CIVL-C header file, which declares all the
|
|---|
| 44 | types and other CIVL primitives.
|
|---|
| 45 |
|
|---|
| 46 | A translation unit consists of a sequence of variable declarations,
|
|---|
| 47 | function prototypes, function definitions, and \emph{assume}
|
|---|
| 48 | statements.
|
|---|
| 49 |
|
|---|
| 50 | % lexical scopes and the lexical scope tree
|
|---|
| 51 |
|
|---|
| 52 | % naming scopes (\$scope)
|
|---|
| 53 |
|
|---|
| 54 | % translation of source to model
|
|---|
| 55 |
|
|---|
| 56 | % root function, root scope and the main function
|
|---|
| 57 |
|
|---|
| 58 | % formal parameters to main, return value
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 | % write a grammar. Leave out type qualifiers, etc. Keep pointers.
|
|---|
| 62 | % Keep simple types? Why not keep all the standard types.
|
|---|
| 63 | % How about "symbolic types"? Make all the casts explicit.
|
|---|
| 64 | % Look at CIL?
|
|---|
| 65 |
|
|---|
| 66 | % Describe as subset of C, but leave out:... and add...
|
|---|
| 67 |
|
|---|
| 68 | % add Set<proc> and use it.
|
|---|
| 69 |
|
|---|
| 70 | \chapter{Sequential Elements}
|
|---|
| 71 |
|
|---|
| 72 | In this chapter we describe the main sequential elements of the
|
|---|
| 73 | language. For the most part these are the same as in C.
|
|---|
| 74 | Primitives dealing with concurrency are introduced in Chapter
|
|---|
| 75 | \ref{chap:concurrency}.
|
|---|
| 76 |
|
|---|
| 77 | \section{Types}
|
|---|
| 78 |
|
|---|
| 79 | \subsection{Standard types inherited from C}
|
|---|
| 80 |
|
|---|
| 81 | The boolean type is denoted \verb!_Bool!, as in C. Its values are $0$
|
|---|
| 82 | and $1$, which are also denoted by $\ctrue$ and $\cfalse$,
|
|---|
| 83 | respectively.
|
|---|
| 84 |
|
|---|
| 85 | There is one integer type, corresponding to the mathematical integers.
|
|---|
| 86 | Currently, all of the C integer types \texttt{int}, \texttt{long},
|
|---|
| 87 | \texttt{unsigned\ int}, \texttt{short}, etc., are mapped to the CIVL
|
|---|
| 88 | integer type.
|
|---|
| 89 |
|
|---|
| 90 | There is one real type, corresponding to the mathematical real
|
|---|
| 91 | numbers. Currently, all of the C real types \texttt{double},
|
|---|
| 92 | \texttt{float}, etc., are mapped to the CIVL real type.
|
|---|
| 93 |
|
|---|
| 94 | Array types, \texttt{struct} and \texttt{union} types, \texttt{char},
|
|---|
| 95 | and pointer types are all exactly as in C.
|
|---|
| 96 |
|
|---|
| 97 | % \subsection{The heap type $\cheap$ and handles}
|
|---|
| 98 |
|
|---|
| 99 | % Unlike C, a CIVL-C program does not necessarily have access to a
|
|---|
| 100 | % single, global heap. Instead, there is a $\cheap$ type, and heaps may
|
|---|
| 101 | % be declared explicitly wherever they are needed. Hence a CIVL-C
|
|---|
| 102 | % program may have several heaps, and these may exist in different
|
|---|
| 103 | % scopes.
|
|---|
| 104 |
|
|---|
| 105 | % A heap is declared and created as follows:
|
|---|
| 106 | % \begin{verbatim}
|
|---|
| 107 | % $heap h = $heap_create();
|
|---|
| 108 | % \end{verbatim}
|
|---|
| 109 | % The function \verb!$heap_create()! creates a new empty heap in the
|
|---|
| 110 | % current scope and returns a \emph{handle} to that heap. A handle is
|
|---|
| 111 | % like a pointer: it is a reference to another object. However, a handle
|
|---|
| 112 | % is much more restricted than a general pointer. In particular, it
|
|---|
| 113 | % cannot be dereferenced (by the \ct{*} operator). The underlying heap
|
|---|
| 114 | % object can only be accessed by using a handle to it as an argument to
|
|---|
| 115 | % a system function.
|
|---|
| 116 |
|
|---|
| 117 | % Handles can be used in assignments and passed as arguments to functions.
|
|---|
| 118 | % For example, this declaration could follow the one above:
|
|---|
| 119 | % \begin{verbatim}
|
|---|
| 120 | % $heap h2=h;
|
|---|
| 121 | % \end{verbatim}
|
|---|
| 122 | % After executing this code, \ct{h2} and \ct{h} will be aliased, i.e., the two
|
|---|
| 123 | % handles will refer to the same heap object.
|
|---|
| 124 |
|
|---|
| 125 | % The heap object exists in the scope in which it is created. In
|
|---|
| 126 | % particular, it will disappear when that scope disappears, i.e., when
|
|---|
| 127 | % control reaches the right curly brace that defines the end of the
|
|---|
| 128 | % scope. At that point, any references into the heap become invalid.
|
|---|
| 129 |
|
|---|
| 130 | % The following system functions deal with heaps:
|
|---|
| 131 | % \begin{verbatim}
|
|---|
| 132 | % void* $malloc($heap h, int size);
|
|---|
| 133 | % void free(void *p)
|
|---|
| 134 | % \end{verbatim}
|
|---|
| 135 | % The first function is like C's \texttt{malloc}, except that you
|
|---|
| 136 | % specify the heap in which the allocation takes place.
|
|---|
| 137 | % This modifies the specified heap and returns a pointer to the new object.
|
|---|
| 138 | % The function can only occur in a context in which the type of the object is
|
|---|
| 139 | % specified, as in:
|
|---|
| 140 | % \begin{verbatim}
|
|---|
| 141 | % $heap h;
|
|---|
| 142 | % int n = 10;
|
|---|
| 143 | % double *p = (double*)$malloc(h, n*sizeof(double));
|
|---|
| 144 | % \end{verbatim}
|
|---|
| 145 | % The function \ct{free} is exactly the same as in C. Note that
|
|---|
| 146 | % \texttt{free} modifies the heap which was used to allocate \texttt{p}.
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 | \subsection{The bundle type: $\cbundle$}
|
|---|
| 150 |
|
|---|
| 151 | CIVL-C includes a type named \cbundle. A bundle is basically a
|
|---|
| 152 | sequence of data, wrapped into an atomic package. A bundle is created
|
|---|
| 153 | using a function that specifies a region of memory. One can create a
|
|---|
| 154 | bundle from an array of integers, and another bundle from an array of
|
|---|
| 155 | reals. Both bundles have the same type, \cbundle. They can therefore
|
|---|
| 156 | be entered into an array of \cbundle, for example. Hence bundles are
|
|---|
| 157 | useful for mixing objects of different (even statically unknown) types
|
|---|
| 158 | into a single data structure. Later, the contents of a bundle can be
|
|---|
| 159 | extracted with another function that specifies a region of memory into
|
|---|
| 160 | which to unpack the bundle; if that memory does not have the right
|
|---|
| 161 | type to receive the contents of the bundle, a runtime error is
|
|---|
| 162 | generated.
|
|---|
| 163 |
|
|---|
| 164 | \begin{figure}
|
|---|
| 165 | \begin{verbatim}
|
|---|
| 166 | /* Creates a bundle from the memory region specified by ptr and size,
|
|---|
| 167 | * copying the data into the new bundle */
|
|---|
| 168 | $bundle $bundle_pack(void *ptr, int size);
|
|---|
| 169 |
|
|---|
| 170 | /* Returns the size (number of bytes) of the bundle */
|
|---|
| 171 | int $bundle_size($bundle b);
|
|---|
| 172 |
|
|---|
| 173 | /* Copies the data out of the bundle into the region specified */
|
|---|
| 174 | void $bundle_unpack($bundle bundle, void *ptr);
|
|---|
| 175 | \end{verbatim}
|
|---|
| 176 | \caption{The \emph{bundle} abstract data type}
|
|---|
| 177 | \label{fig:bundle}
|
|---|
| 178 | \end{figure}
|
|---|
| 179 |
|
|---|
| 180 | The relevant functions for creating and manipulating bundles
|
|---|
| 181 | are given in Figure \ref{fig:bundle}.
|
|---|
| 182 |
|
|---|
| 183 | \subsection{The \cscope{} type}
|
|---|
| 184 | \label{sec:scopetype}
|
|---|
| 185 |
|
|---|
| 186 | An object of type $\cscope$ is a reference to a dynamic scope. It may
|
|---|
| 187 | be thought of as a ``dynamic scope ID, '' but it is not an integer and
|
|---|
| 188 | cannot be converted to an integer. Operations defined on scopes are
|
|---|
| 189 | discussed in Section \ref{sec:scopeexpr}.
|
|---|
| 190 |
|
|---|
| 191 | \section{Expressions}
|
|---|
| 192 |
|
|---|
| 193 | \subsection{Expressions inherited from C}
|
|---|
| 194 |
|
|---|
| 195 | The following C expressions are included in CIVL:
|
|---|
| 196 | \begin{itemize}
|
|---|
| 197 | \item \emph{constant} expressions
|
|---|
| 198 | \item \emph{identifier} expressions (\texttt{x})
|
|---|
| 199 | \item parenthetical expressions (\verb!(e)!)
|
|---|
| 200 | \item numerical \emph{addition} (\verb!a+b!), \emph{subtraction} (\verb!a-b!),
|
|---|
| 201 | \emph{multiplication} (\verb!a*b!), \emph{division} (\verb!a/b!),
|
|---|
| 202 | \emph{unary plus} (\verb!+a!), \emph{unary minus} (\verb!-a!),
|
|---|
| 203 | \emph{integer division} (\verb!a/b!) and \emph{modulus} (\verb!a%b!),
|
|---|
| 204 | all with their ideal mathematical interpretations
|
|---|
| 205 | \item array \emph{index} expressions (\verb!a[e]!) and struct or union
|
|---|
| 206 | \emph{navigation} expressions (\verb!x.f!, \verb!p->f!)
|
|---|
| 207 | \item \emph{address-of} (\verb!&e!), pointer \emph{dereference} (\verb!*p!),
|
|---|
| 208 | pointer \emph{addition} (\verb!p+i!) and \emph{subtraction} (\verb!p-q!)
|
|---|
| 209 | expressions
|
|---|
| 210 | \item relational expressions (\verb!a==b!, \verb~a!=b~, \verb!a>=b!,
|
|---|
| 211 | \verb!a<=b!, \verb!a<b!, \verb!a>b!)
|
|---|
| 212 | \item logical \emph{not} (\verb~!p~), \emph{and} (\verb!p&&q!), and
|
|---|
| 213 | \emph{or} (\verb!p||q!)
|
|---|
| 214 | \item \emph{sizeof} a type (\verb!sizeof(t)!) or expression (\verb!sizeof(e)!)
|
|---|
| 215 | \item \emph{assignment} expressions (\verb!a=b!, \verb!a+=b!, \verb!a-=b!,
|
|---|
| 216 | \verb!a*=b!, \verb!a/=b!, \verb!a%=b!)
|
|---|
| 217 | \item function \emph{calls} \verb!f(e1,...,en)!
|
|---|
| 218 | \item \emph{conditional} expressions (\verb!b ? e : f!).
|
|---|
| 219 | \item \emph{cast} expressions (\verb!(t)e!)
|
|---|
| 220 | \end{itemize}
|
|---|
| 221 |
|
|---|
| 222 | Bit-wise operations are not yet supported.
|
|---|
| 223 |
|
|---|
| 224 | \subsection{Scope expressions}
|
|---|
| 225 | \label{sec:scopeexpr}
|
|---|
| 226 |
|
|---|
| 227 | As mentioned in Section \ref{sec:scopetype}, CIVL-C provides a type
|
|---|
| 228 | \cscope. An object of this type is a reference to a dynamic scope.
|
|---|
| 229 | Several constants, expressions, and functions dealing with the
|
|---|
| 230 | \cscope{} type are also provided.
|
|---|
| 231 |
|
|---|
| 232 | The $\cscope$ type is like any other object type. It may be used as
|
|---|
| 233 | the element type of an array, a field in a structure or union, and so
|
|---|
| 234 | on. Expressions of type $\cscope$ may occur on the left or right-hand
|
|---|
| 235 | sides of assignments and as arguments in function calls just like any
|
|---|
| 236 | other expression. Two different variables of type $\cscope$ may be
|
|---|
| 237 | aliased, i.e., they may refer to the same dynamic scope. When a
|
|---|
| 238 | dynamic scope leaves the state (because control reached the right
|
|---|
| 239 | curly brace which marks the end of the scope, or the process exits),
|
|---|
| 240 | any reference to that scope becomes ``undefined,'' and an attempt to
|
|---|
| 241 | use that scope will result in a runtime error.
|
|---|
| 242 |
|
|---|
| 243 | \subsubsection{The constant \chere}
|
|---|
| 244 |
|
|---|
| 245 | A constant \chere{} exists in every scope. This constant has
|
|---|
| 246 | type \cscope{} and refers to the dynamic scope in which it is
|
|---|
| 247 | contained. For example,
|
|---|
| 248 | \begin{verbatim}
|
|---|
| 249 | { // scope s
|
|---|
| 250 | int *p = (int*)$malloc($here, n*sizeof(int);
|
|---|
| 251 | }
|
|---|
| 252 | \end{verbatim}
|
|---|
| 253 | allocates an object consisting of $n$ ints in the scope $s$.
|
|---|
| 254 |
|
|---|
| 255 | \subsubsection{The constant \cscoperoot{}}
|
|---|
| 256 |
|
|---|
| 257 | There is a global constant \cscoperoot{} of type $\cscope$ which
|
|---|
| 258 | refers to the root dynamic scope.
|
|---|
| 259 |
|
|---|
| 260 | \subsubsection{Scope relational operators}
|
|---|
| 261 |
|
|---|
| 262 | Let $s_1$ and $s_2$ be expressions of type \cscope. The following are
|
|---|
| 263 | all CIVL-C expressions of boolean type:
|
|---|
| 264 | \begin{itemize}
|
|---|
| 265 | \item $s_1$ \ct{==} $s_2$. This is \emph{true} iff $s_1$ and $s_2$
|
|---|
| 266 | refer to the same dynamic scope.
|
|---|
| 267 | \item $s_1$ \ct{!=} $s_2$. This is \emph{true} iff $s_1$ and $s_2$
|
|---|
| 268 | refer to different dynamic scopes.
|
|---|
| 269 | \item $s_1$ \ct{<=} $s_2$. This is \emph{true} iff $s_1$ is equal to
|
|---|
| 270 | or a descendant of $s_2$, i.e., $s_1$ is equal to or contained in $s_2$.
|
|---|
| 271 | \item $s_1$ \ct{<} $s_2$. This is \emph{true} iff $s_1$ is a strict
|
|---|
| 272 | descendant of $s_2$, i.e., $s_1$ is contained in $s_2$ and is not
|
|---|
| 273 | equal to $s_2$.
|
|---|
| 274 | \item $s_1$ \ct{>} $s_2$. This is equivalent to $s_2$ \ct{<} $s_1$.
|
|---|
| 275 | \item $s_1$ \ct{>=} $s_2$. This is equivalent to $s_2$ \ct{<=} $s_1$.
|
|---|
| 276 | \end{itemize}
|
|---|
| 277 |
|
|---|
| 278 | \subsubsection{Scope parent function $\cscopeparent$}
|
|---|
| 279 |
|
|---|
| 280 | The system function
|
|---|
| 281 | \begin{verbatim}
|
|---|
| 282 | $scope $scope_parent($scope s);
|
|---|
| 283 | \end{verbatim}
|
|---|
| 284 | returns the parent dynamic scope of the dynamic scope referenced by
|
|---|
| 285 | \ct{s}. If \ct{s} is the root dynamic scope, it returns the undefined
|
|---|
| 286 | value of type $\cscope$.
|
|---|
| 287 |
|
|---|
| 288 | \subsubsection{Lowest Common Ancestor: \ct{+}}
|
|---|
| 289 |
|
|---|
| 290 | The expression $s_1$ \ct{+} $s_2$, where $s_1$ and $s_2$ are
|
|---|
| 291 | expressions of type \cscope, evaluates to the lowest common ancestor
|
|---|
| 292 | of $s_1$ and $s_2$ in the dynamic scope tree. This is the smallest
|
|---|
| 293 | dynamic scope containing both $s_1$ and $s_2$.
|
|---|
| 294 |
|
|---|
| 295 | \subsubsection{The \cscopeof{} expression}
|
|---|
| 296 |
|
|---|
| 297 | Given any left-hand-side expression \ct{expr}, the expression
|
|---|
| 298 | \begin{verbatim}
|
|---|
| 299 | $scopeof(expr)
|
|---|
| 300 | \end{verbatim}
|
|---|
| 301 | evaluates to the dynamic scope containing the object specified by
|
|---|
| 302 | \ct{expr}.
|
|---|
| 303 |
|
|---|
| 304 | The following example illustrates the semantics of the \cscopeof{}
|
|---|
| 305 | operator. All of the assertions hold:
|
|---|
| 306 | \begin{verbatim}
|
|---|
| 307 | {
|
|---|
| 308 | $scope s1 = $here;
|
|---|
| 309 | int x;
|
|---|
| 310 | double a[10];
|
|---|
| 311 |
|
|---|
| 312 | {
|
|---|
| 313 | $scope s2 = $here;
|
|---|
| 314 | int *p = &x;
|
|---|
| 315 | double *q = &a[4];
|
|---|
| 316 |
|
|---|
| 317 | assert($scopeof(x)==s1);
|
|---|
| 318 | assert($scopeof(p)==s2);
|
|---|
| 319 | assert($scopeof(*p)==s1);
|
|---|
| 320 | assert($scopeof(a)==s1);
|
|---|
| 321 | assert($scopeof(a[5])==s1);
|
|---|
| 322 | assert($scopeof(q)==s2);
|
|---|
| 323 | assert($scopeof(*q)==s1);
|
|---|
| 324 | }
|
|---|
| 325 | }
|
|---|
| 326 | \end{verbatim}
|
|---|
| 327 |
|
|---|
| 328 | \section{Statements}
|
|---|
| 329 |
|
|---|
| 330 | The usual C statements are supported:
|
|---|
| 331 | \begin{itemize}
|
|---|
| 332 | \item \emph{no-op} (\ct{;})
|
|---|
| 333 | \item expression statements (\ct{e;})
|
|---|
| 334 | \item labeled statements, including \ct{case} and \ct{default} labels
|
|---|
| 335 | (\ct{l: s})
|
|---|
| 336 | \item \emph{for} (\ct{for (init; cond; inc) s}), \emph{while}
|
|---|
| 337 | (\ct{while (cond) s}) and \emph{do} (\ct{do s while (cond)})
|
|---|
| 338 | loops
|
|---|
| 339 | \item compound statements (\lb \ct{s1;s2;} \ldots \rb)
|
|---|
| 340 | \item \texttt{if} and \verb!if! \ldots \verb!else!
|
|---|
| 341 | \item \verb!goto!
|
|---|
| 342 | \item \verb!switch!
|
|---|
| 343 | \item \verb!break!
|
|---|
| 344 | \item \verb!continue!
|
|---|
| 345 | \item \verb!return!
|
|---|
| 346 | \end{itemize}
|
|---|
| 347 |
|
|---|
| 348 | \section{Guards and nondeterminism}
|
|---|
| 349 |
|
|---|
| 350 | \subsection{Guarded commands: \cwhen}
|
|---|
| 351 |
|
|---|
| 352 | A guarded command is encoded in CIVL-C using a $\cwhen$ statement:
|
|---|
| 353 | \begin{verbatim}
|
|---|
| 354 | $when (expr) stmt;
|
|---|
| 355 | \end{verbatim}
|
|---|
| 356 | All statements have a guard, either implicit or explicit. For most
|
|---|
| 357 | statements, the guard is \ctrue. The \cwhen{} statement allows one to
|
|---|
| 358 | attach an explicit guard to a statement.
|
|---|
| 359 |
|
|---|
| 360 | When \texttt{expr} is \emph{true}, the statement is enabled, otherwise
|
|---|
| 361 | it is disabled. A disabled statement is \emph{blocked}---it will not
|
|---|
| 362 | be scheduled for execution. When it is enabled, it may execute by
|
|---|
| 363 | moving control to the \texttt{stmt} and executing the first atomic
|
|---|
| 364 | action in the \texttt{stmt}.
|
|---|
| 365 |
|
|---|
| 366 | If \texttt{stmt} itself has a non-trivial guard, the guard of the
|
|---|
| 367 | \cwhen{} statement is effectively the conjunction of the \texttt{expr}
|
|---|
| 368 | and the guard of \texttt{stmt}.
|
|---|
| 369 |
|
|---|
| 370 | The evaluation of \texttt{expr} and the first atomic action of
|
|---|
| 371 | \texttt{stmt} effectively occur as a single atomic action. There is
|
|---|
| 372 | no guarantee that execution of \texttt{stmt} will continue atomically
|
|---|
| 373 | if it contains more than one atomic action, i.e., other processes may
|
|---|
| 374 | be scheduled.
|
|---|
| 375 |
|
|---|
| 376 | Examples:
|
|---|
| 377 | \begin{verbatim}
|
|---|
| 378 | $when (s>0) s--;
|
|---|
| 379 | \end{verbatim}
|
|---|
| 380 | This will block until \texttt{s} is positive and then decrement
|
|---|
| 381 | \texttt{s}. The execution of \texttt{s--} is guaranteed to take place
|
|---|
| 382 | in an environment in which \texttt{s} is positive.
|
|---|
| 383 |
|
|---|
| 384 | \begin{verbatim}
|
|---|
| 385 | $when (s>0) {s--; t++}
|
|---|
| 386 | \end{verbatim}
|
|---|
| 387 | The execution of \texttt{s--} must happen when \texttt{s>0}, but
|
|---|
| 388 | between \texttt{s--} and \texttt{t++}, other processes may execute.
|
|---|
| 389 |
|
|---|
| 390 | \begin{verbatim}
|
|---|
| 391 | $when (s>0) $when (t>0) x=y*t;
|
|---|
| 392 | \end{verbatim}
|
|---|
| 393 | This blocks until both \texttt{x} and \texttt{t} are positive then
|
|---|
| 394 | executes the assignment in that state. It is equivalent to
|
|---|
| 395 | \begin{verbatim}
|
|---|
| 396 | $when (s>0 && t>0) x=y*t;
|
|---|
| 397 | \end{verbatim}
|
|---|
| 398 |
|
|---|
| 399 | \subsection{Nondeterministic selection statement: \cchoose}
|
|---|
| 400 |
|
|---|
| 401 | A \cchoose{} statement has the form
|
|---|
| 402 | \begin{verbatim}
|
|---|
| 403 | $choose {
|
|---|
| 404 | stmt1;
|
|---|
| 405 | stmt2;
|
|---|
| 406 | ...
|
|---|
| 407 | default: stmt
|
|---|
| 408 | }
|
|---|
| 409 | \end{verbatim}
|
|---|
| 410 | The \texttt{default} clause is optional.
|
|---|
| 411 |
|
|---|
| 412 | The guards of the statements are evaluated and among those that are
|
|---|
| 413 | \emph{true}, one is chosen nondeterministically and executed. If none
|
|---|
| 414 | are \emph{true} and the \texttt{default} clause is present, it is
|
|---|
| 415 | chosen. The \texttt{default} clause will only be selected if all
|
|---|
| 416 | guards are \emph{false}. If no \texttt{default} clause is present and
|
|---|
| 417 | all guards are \emph{false}, the statement blocks. Hence the implicit
|
|---|
| 418 | guard of the \cchoose{} statement without a \texttt{default} clause is
|
|---|
| 419 | the disjunction of the guards of its sub-statements. The implicit
|
|---|
| 420 | guard of the \cchoose{} statement with a default clause is
|
|---|
| 421 | \emph{true}.
|
|---|
| 422 |
|
|---|
| 423 | Example: this shows how to encode a ``low-level'' CIVL guarded
|
|---|
| 424 | transition system:
|
|---|
| 425 |
|
|---|
| 426 | \begin{verbatim}
|
|---|
| 427 | l1: $choose {
|
|---|
| 428 | $when (x>0) {x--; goto l2;}
|
|---|
| 429 | $when (x==0) {y=1; goto l3;}
|
|---|
| 430 | default: {z=1; goto l4;}
|
|---|
| 431 | }
|
|---|
| 432 | l2: $choose {
|
|---|
| 433 | ...
|
|---|
| 434 | }
|
|---|
| 435 | l3: $choose {
|
|---|
| 436 | ...
|
|---|
| 437 | }
|
|---|
| 438 | \end{verbatim}
|
|---|
| 439 |
|
|---|
| 440 |
|
|---|
| 441 | \subsection{Nondeterministic choice of integer: \cchooseint}
|
|---|
| 442 |
|
|---|
| 443 | The system function \cchooseint{} has the following signature:
|
|---|
| 444 | \begin{verbatim}
|
|---|
| 445 | int $choose_int(int n);
|
|---|
| 446 | \end{verbatim}
|
|---|
| 447 | This function takes as input a positive integer \texttt{n} and
|
|---|
| 448 | nondeterministicaly returns an integer in the range
|
|---|
| 449 | $[0,\texttt{n}-1]$.
|
|---|
| 450 |
|
|---|
| 451 |
|
|---|
| 452 | \chapter{Concurrency}
|
|---|
| 453 | \label{chap:concurrency}
|
|---|
| 454 |
|
|---|
| 455 | \section{Process creation and management}
|
|---|
| 456 |
|
|---|
| 457 | \subsection{The process type: \cproc}
|
|---|
| 458 |
|
|---|
| 459 | This is a primitive object type and functions like any other primitive
|
|---|
| 460 | C type (e.g., \texttt{int}). An object of this type refers to a
|
|---|
| 461 | process. It can be thought of as a process ID, but it is not an
|
|---|
| 462 | integer and cannot be cast to one. It is analogous to the $\cscope$
|
|---|
| 463 | type for dynamic scopes.
|
|---|
| 464 |
|
|---|
| 465 | Certain expressions take an argument of \cproc{} type and some return
|
|---|
| 466 | something of \cproc{} type.
|
|---|
| 467 |
|
|---|
| 468 | \subsection{The \emph{self} process constant: \cself}
|
|---|
| 469 |
|
|---|
| 470 | This is a constant of type \cproc. It can be used wherever an argument
|
|---|
| 471 | of type \cproc{} is called for. It refers to the process that is
|
|---|
| 472 | evaluating the expression containing \cself.
|
|---|
| 473 |
|
|---|
| 474 | \subsection{Spawning a new process: \cspawn}
|
|---|
| 475 |
|
|---|
| 476 | A \emph{spawn} expression is an expression with side-effects. It
|
|---|
| 477 | spawns a new process and returns a reference to the new process, i.e.,
|
|---|
| 478 | an object of type \cproc. The syntax is the same as a procedure
|
|---|
| 479 | invocation with the keyword \cspawn{} inserted in front:
|
|---|
| 480 | \begin{verbatim}
|
|---|
| 481 | $spawn f(expr1, ..., exprn)
|
|---|
| 482 | \end{verbatim}
|
|---|
| 483 | Typically the returned value is assigned to a variable, e.g.,
|
|---|
| 484 | \begin{verbatim}
|
|---|
| 485 | $proc p = $spawn f(i);
|
|---|
| 486 | \end{verbatim}
|
|---|
| 487 | If the invoked function \texttt{f} returns a value, that value is
|
|---|
| 488 | simply ignored.
|
|---|
| 489 |
|
|---|
| 490 | \subsection{Waiting for another process to terminate: \cwait}
|
|---|
| 491 |
|
|---|
| 492 | The system function $\cwait$ has signature
|
|---|
| 493 | \begin{verbatim}
|
|---|
| 494 | void $wait($proc p);
|
|---|
| 495 | \end{verbatim}
|
|---|
| 496 | When invoked, this function will not return until the process
|
|---|
| 497 | referenced by \ct{p} has terminated. Note that $p$ can be any
|
|---|
| 498 | expression of type \cproc{}, not just a variable.
|
|---|
| 499 |
|
|---|
| 500 | \subsection{Terminating a process immediately: \cexit}
|
|---|
| 501 |
|
|---|
| 502 | This function takes no arguments. It causes the
|
|---|
| 503 | calling process to terminate immediately, regardless of the state of
|
|---|
| 504 | its call stack:
|
|---|
| 505 | \begin{verbatim}
|
|---|
| 506 | void $exit(void);
|
|---|
| 507 | \end{verbatim}
|
|---|
| 508 |
|
|---|
| 509 | \section{Atomicity}
|
|---|
| 510 |
|
|---|
| 511 | \subsection{Atom blocks: \catom} This defines a number of statements to be executed
|
|---|
| 512 | as a single atomic transition. An \catom~block has the following
|
|---|
| 513 | form:
|
|---|
| 514 | \begin{verbatim}
|
|---|
| 515 | $atom {
|
|---|
| 516 | stmt1;
|
|---|
| 517 | stmt2;
|
|---|
| 518 | ...
|
|---|
| 519 | }
|
|---|
| 520 | \end{verbatim}
|
|---|
| 521 |
|
|---|
| 522 | The statements inside an \catom\ block are to be executed as one
|
|---|
| 523 | transition. It is required that the execution of the statements in an
|
|---|
| 524 | \catom\ block satisfy all of the following properties:
|
|---|
| 525 | \begin{enumerate}
|
|---|
| 526 | \item \emph{deterministic}: at each step in the execution of the atom
|
|---|
| 527 | block, there must be at most one enabled statement;
|
|---|
| 528 | \item \emph{nonblocking}: at each step in the execution, there must be
|
|---|
| 529 | at least one enabled statement, hence, together with (1), there must
|
|---|
| 530 | be exactly one enabled statement;
|
|---|
| 531 | \item \emph{finite}: the execution of the atom block must terminate
|
|---|
| 532 | after a finite number of steps; and
|
|---|
| 533 | \item \emph{isolated}: there are no jumps from outside the atom block
|
|---|
| 534 | to inside the atom block, or from inside the atomc block to outside
|
|---|
| 535 | of it.
|
|---|
| 536 | \end{enumerate}
|
|---|
| 537 |
|
|---|
| 538 | Violations of the \emph{deterministic}, \emph{nonblocking}, or
|
|---|
| 539 | \emph{isolated} properties will be reported either statically or
|
|---|
| 540 | dynamically. If the \emph{finite} property is violated, the
|
|---|
| 541 | verification may just run forever.
|
|---|
| 542 |
|
|---|
| 543 | Once the process enters an \catom\ block is said to be \emph{executing
|
|---|
| 544 | atomly}. The process remains executing atomly until it reaches the
|
|---|
| 545 | terminating right brace of the block. Hence \emph{executing atomly}
|
|---|
| 546 | is a dynamic, not static condition. For example, the block might
|
|---|
| 547 | contain a function call which takes the process to a point in code
|
|---|
| 548 | which is not statically contained in an atom block; that process is
|
|---|
| 549 | nevertheless still executing atomly and is subject to the rules above.
|
|---|
| 550 | The process only stops executing atomly when that function call
|
|---|
| 551 | returns and control finally reaches the right curly brace at the end
|
|---|
| 552 | of the atom block (assuming the block is not contained in another atom
|
|---|
| 553 | block).
|
|---|
| 554 |
|
|---|
| 555 | \emph{Note:} \cwait\ statements are not allowed in \catom\ blocks.
|
|---|
| 556 | The rationale for this is that there is never a way to know for
|
|---|
| 557 | certain that another process has terminated (until \cwait\ has
|
|---|
| 558 | returned) so there is never a way to be certain the \cwait\ statement
|
|---|
| 559 | will not block. If one does occur in an \catom\ block, an error will
|
|---|
| 560 | be reported statically (if it can be detected statically) or
|
|---|
| 561 | dynamically (otherwise). Note that it is not always possible to
|
|---|
| 562 | detect this statically because the \catom\ block may contain a
|
|---|
| 563 | function call, and the function may contain the \cwait\ statement.
|
|---|
| 564 |
|
|---|
| 565 | \subsection{Atomic blocks: \catomic} The statements in an \emph{atomic} block
|
|---|
| 566 | will be executed without other processes interleaving, to
|
|---|
| 567 | the extent possible. It has the form:
|
|---|
| 568 | \begin{verbatim}
|
|---|
| 569 | $atomic {
|
|---|
| 570 | stmt1;
|
|---|
| 571 | stmt2;
|
|---|
| 572 | ...
|
|---|
| 573 | }
|
|---|
| 574 | \end{verbatim}
|
|---|
| 575 | It is essentially a weaker form of \catom. Unlike \catom, there are
|
|---|
| 576 | no restrictions on the statements that can go inside an \catomic\
|
|---|
| 577 | block. A process executing an \catomic~block will try to execute the
|
|---|
| 578 | statements without interleaving with other processes, unless it
|
|---|
| 579 | becomes blocked. Unlike an \catom, the statements in an atomic block
|
|---|
| 580 | do not necessarily execute as a single transition; they may be spread
|
|---|
| 581 | out over multiple transitions.
|
|---|
| 582 |
|
|---|
| 583 | When no statement is enabled, the execution of the \catomic\ block
|
|---|
| 584 | will be interrupted. At this point, other processes are allowed to
|
|---|
| 585 | execute. Eventually, if the original process becomes enabled due to
|
|---|
| 586 | the actions of other processes, it may be scheduled again, in which
|
|---|
| 587 | case it regains atomicity and continues where it left off. For
|
|---|
| 588 | example, after executing the first loop, the process executing the
|
|---|
| 589 | following code will become blocked at the first \cwait\ statement:
|
|---|
| 590 | \begin{verbatim}
|
|---|
| 591 | $atomic{
|
|---|
| 592 | for(int i = 0; i < 5; i++) p[i] = $spawn foo(i);
|
|---|
| 593 | for(int i = 0; i < 5; i++) $wait p[i];
|
|---|
| 594 | }
|
|---|
| 595 | \end{verbatim}
|
|---|
| 596 | Other processes will then execute. Eventually, if the process being
|
|---|
| 597 | waited on terminates, the original process becomes enabled and may be
|
|---|
| 598 | scheduled, in which case it regain atomicity, increments \texttt{i}
|
|---|
| 599 | and proceeds to the next $\cwait$ statement. This is in fact a common
|
|---|
| 600 | idiom for spawning and waiting on a set of processes.
|
|---|
| 601 |
|
|---|
| 602 | A process that enters an $\catomic$ block is said to be
|
|---|
| 603 | \emph{executing atomically}; it remains executing atomically until it
|
|---|
| 604 | reaches the closing curly brace.
|
|---|
| 605 |
|
|---|
| 606 | Both $\catom$ and $\catomic$ blocks can be nested arbitrarily, but
|
|---|
| 607 | $\catom$ overrides $\catomic$: a process that is executing atomly will
|
|---|
| 608 | continue executing atomly if it encounters an $\catomic$ statement;
|
|---|
| 609 | but a process executing atomically that encounters an $\catom$ will
|
|---|
| 610 | begin executing atomly.
|
|---|
| 611 |
|
|---|
| 612 | The atomic semantics are defined more precisely as follows: there is a
|
|---|
| 613 | single global variable called the \emph{atomic lock}. This variable
|
|---|
| 614 | can either be null (meaning the atomic lock is ``free''), or it can
|
|---|
| 615 | hold the PID of a process; that process is said to ``hold'' the atomic
|
|---|
| 616 | lock. Moreover, each process contains a special integer variable, its
|
|---|
| 617 | \emph{atomic counter}, which is initially 0. Every time a process
|
|---|
| 618 | enters an atomic block, it increments its atomic counter; every time
|
|---|
| 619 | it exits an atomic block, it decrements its counter. In order to
|
|---|
| 620 | increment its counter from $0$ to $1$, it must first wait for the
|
|---|
| 621 | atomic lock to become free, and then take the lock. When it
|
|---|
| 622 | decrements its counter from $1$ to $0$, it releases the atomic lock.
|
|---|
| 623 | When a process executing atomically becomes blocked, it releases the
|
|---|
| 624 | lock (without changing the value of its atomic counter).
|
|---|
| 625 |
|
|---|
| 626 |
|
|---|
| 627 | \section{Message-Passing}
|
|---|
| 628 |
|
|---|
| 629 | CIVL-C provides a number of additional primitives that can be used to
|
|---|
| 630 | model message-passing systems. This part of the language is built in
|
|---|
| 631 | two layers: the lower layer defines an abstract data type for
|
|---|
| 632 | representing messages; the higher layer defines an abstract data type
|
|---|
| 633 | of \emph{communicators} for managing sets of messages being
|
|---|
| 634 | transferred among some set of processes.
|
|---|
| 635 |
|
|---|
| 636 | \subsection{Messages: \cmessage}
|
|---|
| 637 |
|
|---|
| 638 | Messages are similar to bundles, but with some additional meta-data.
|
|---|
| 639 | The \emph{data} component of the message is the ``contents'' of the
|
|---|
| 640 | message and is formed and extracted much like a bundle. The meta-data
|
|---|
| 641 | consists of an integer identifier for the \emph{source} place of the
|
|---|
| 642 | message, an integer identifier for the message \emph{destination}
|
|---|
| 643 | place, and an integer \emph{tag} which can be used by a process to
|
|---|
| 644 | discriminate among messages for reception. This is very similar to
|
|---|
| 645 | MPI.
|
|---|
| 646 |
|
|---|
| 647 | \begin{figure}
|
|---|
| 648 | \begin{small}
|
|---|
| 649 | \begin{verbatim}
|
|---|
| 650 | /* creates a new message, copying data from the specified buffer */
|
|---|
| 651 | $message $message_pack(int source, int dest, int tag, void *data, int size);
|
|---|
| 652 |
|
|---|
| 653 | /* returns the message source */
|
|---|
| 654 | int $message_source($message message);
|
|---|
| 655 |
|
|---|
| 656 | /* returns the message tag */
|
|---|
| 657 | int $message_tag($message message);
|
|---|
| 658 |
|
|---|
| 659 | /* returns the message destination */
|
|---|
| 660 | int $message_dest($message message);
|
|---|
| 661 |
|
|---|
| 662 | /* returns the message size */
|
|---|
| 663 | int $message_size($message message);
|
|---|
| 664 |
|
|---|
| 665 | /* transfers message data to buf, throwing exception if message
|
|---|
| 666 | * size exceeds specified size */
|
|---|
| 667 | void $message_unpack($message message, void *buf, int size);
|
|---|
| 668 | \end{verbatim}
|
|---|
| 669 | \end{small}
|
|---|
| 670 | \caption{The \emph{message} abstract data type}
|
|---|
| 671 | \label{fig:message}
|
|---|
| 672 | \end{figure}
|
|---|
| 673 |
|
|---|
| 674 | The functions for creating, and extracting information from, messages
|
|---|
| 675 | are given in Figure \ref{fig:message}.
|
|---|
| 676 |
|
|---|
| 677 | \subsection{Communicators: \cgcomm{} and \ccomm}
|
|---|
| 678 | \label{sec:communicators}
|
|---|
| 679 |
|
|---|
| 680 | CIVL-C defines a \emph{global communicator} type $\cgcomm$ and a
|
|---|
| 681 | \emph{local communicator} type $\ccomm$. The global communicator is an
|
|---|
| 682 | abstraction for a ``communication universe'' that stores buffered
|
|---|
| 683 | messages and perhaps other data. The local communicator wraps
|
|---|
| 684 | together a reference to a global communicator and an integer
|
|---|
| 685 | \emph{place}. Most of the message-passing commands take a local
|
|---|
| 686 | communicator as an argument to specify the communication universe used
|
|---|
| 687 | for that operation and the place from which that operation will be
|
|---|
| 688 | executed. The communication universes are isolated from one
|
|---|
| 689 | another---a message sent on one can never be received using a
|
|---|
| 690 | different communicator, for example.
|
|---|
| 691 |
|
|---|
| 692 | The global communicator is the shared object that must be declared in
|
|---|
| 693 | a scope containing all scopes in which communication in that universe
|
|---|
| 694 | will take place. It is created by specifying the number of
|
|---|
| 695 | \emph{places} that will comprise the communicator. A place is an
|
|---|
| 696 | address to which messages may be sent or where they may be received.
|
|---|
| 697 | There is not necessarily a one-to-one correspondence between places and
|
|---|
| 698 | processes: many processes can use the same place.
|
|---|
| 699 |
|
|---|
| 700 | Local communicators are created (typically in some child scope of the
|
|---|
| 701 | scope in which the global communicator is declared) by specifying the
|
|---|
| 702 | gobal communicator to which the local one will be associated and the
|
|---|
| 703 | place ID. The local communicator will be used in most of the
|
|---|
| 704 | message-passing functions; it may be thought of as an ordered pair
|
|---|
| 705 | consisting of a reference to the global communicator and the integer
|
|---|
| 706 | place.
|
|---|
| 707 |
|
|---|
| 708 | Both types ($\cgcomm$ and $\ccomm$) are handle types. When declared
|
|---|
| 709 | with a call to the corresponding creation function, they create an
|
|---|
| 710 | object in the specified scope and return a handle to that object. The
|
|---|
| 711 | object can only be accessed through the specified system functions
|
|---|
| 712 | that take this handle as an argument.
|
|---|
| 713 |
|
|---|
| 714 | \begin{figure}
|
|---|
| 715 | \begin{small}
|
|---|
| 716 | \begin{verbatim}
|
|---|
| 717 | /* Creates a new global communicator object and returns a handle to it.
|
|---|
| 718 | * The global communicator will have size communication places. The
|
|---|
| 719 | * global communicator defines a communication "universe" and encompasses
|
|---|
| 720 | * message buffers and all other components of the state associated to
|
|---|
| 721 | * message-passing. The new object will be allocated in the given scope. */
|
|---|
| 722 | $gcomm $gcomm_create($scope s, int size);
|
|---|
| 723 |
|
|---|
| 724 | /* Creates a new local communicator object and returns a handle to it.
|
|---|
| 725 | * The new communicator will be affiliated with the specified global
|
|---|
| 726 | * communicator. This local communicator handle will be used as an
|
|---|
| 727 | * argument in most message-passing functions. The place must be in
|
|---|
| 728 | * [0,size-1] and specifies the place in the global communication universe
|
|---|
| 729 | * that will be occupied by the local communicator. The local communicator
|
|---|
| 730 | * handle may be used by more than one process, but all of those
|
|---|
| 731 | * processes will be viewed as occupying the same place.
|
|---|
| 732 | * Only one call to $comm_create may occur for each gcomm-place pair.
|
|---|
| 733 | * The new object will be allocated in the given scope. */
|
|---|
| 734 | $comm $comm_create($scope s, $gcomm gcomm, int place);
|
|---|
| 735 |
|
|---|
| 736 | /* Returns the size (number of places) in the global communicator associated
|
|---|
| 737 | * to the given comm. */
|
|---|
| 738 | int $comm_size($comm comm);
|
|---|
| 739 |
|
|---|
| 740 | /* Returns the place of the local communicator. This is the same as the
|
|---|
| 741 | * place argument used to create the local communicator. */
|
|---|
| 742 | int $comm_place($comm comm);
|
|---|
| 743 |
|
|---|
| 744 | /* Adds the message to the appropriate message queue in the communication
|
|---|
| 745 | * universe specified by the comm. The source of the message must equal
|
|---|
| 746 | * the place of the comm. */
|
|---|
| 747 | void $comm_enqueue($comm comm, $message message);
|
|---|
| 748 |
|
|---|
| 749 | /* Returns true iff a matching message exists in the communication universe
|
|---|
| 750 | * specified by the comm. A message matches the arguments if the destination
|
|---|
| 751 | * of the message is the place of the comm, and the sources and tags match. */
|
|---|
| 752 | _Bool $comm_probe($comm comm, int source, int tag);
|
|---|
| 753 |
|
|---|
| 754 | /* Finds the first matching message and returns it without modifying
|
|---|
| 755 | * the communication universe. If no matching message exists, returns a message
|
|---|
| 756 | * with source, dest, and tag all negative. */
|
|---|
| 757 | $message $comm_seek($comm comm, int source, int tag);
|
|---|
| 758 |
|
|---|
| 759 | /* Finds the first matching message, removes it from the communicator,
|
|---|
| 760 | * and returns the message */
|
|---|
| 761 | $message $comm_dequeue($comm comm, int source, int tag);
|
|---|
| 762 | \end{verbatim}
|
|---|
| 763 | \end{small}
|
|---|
| 764 | \caption{The \emph{communicator} interface specifies handle
|
|---|
| 765 | types $\cgcomm$ and $\ccomm$ and the functions above}
|
|---|
| 766 | \label{fig:comm}
|
|---|
| 767 | \end{figure}
|
|---|
| 768 |
|
|---|
| 769 | The communicator interface is given in Figure \ref{fig:comm}.
|
|---|
| 770 |
|
|---|
| 771 | Certain restrictions are enforced on some relations between the
|
|---|
| 772 | objects involved in a communication universe.
|
|---|
| 773 |
|
|---|
| 774 | Fix a \cgcomm{} object. This object corresponds to a single
|
|---|
| 775 | communication universe with, say, $n$ places. At any time, there can
|
|---|
| 776 | be \emph{at most one} \ccomm{} object associated to a given place. If
|
|---|
| 777 | a program attempts to create a \ccomm{} object with the same \cgcomm{}
|
|---|
| 778 | and place as an earlier created \ccomm{} object, a runtime error will
|
|---|
| 779 | occur. In particular, there can be at most $n$ \ccomm{} objects
|
|---|
| 780 | associated to the \cgcomm.
|
|---|
| 781 |
|
|---|
| 782 | The relation between processes and \ccomm{} objects is unconstrained.
|
|---|
| 783 | One process may use any number of \ccomm{} objects. (Of course, the
|
|---|
| 784 | process must have access to handles for those \ccomm{} objects.)
|
|---|
| 785 | Dually, a single \ccomm{} object may be used by any number of
|
|---|
| 786 | processes; this situation arises naturally when modeling a
|
|---|
| 787 | multi-threaded MPI program.
|
|---|
| 788 |
|
|---|
| 789 | \begin{figure}
|
|---|
| 790 | \begin{small}
|
|---|
| 791 | \begin{verbatim}
|
|---|
| 792 | $gcomm gcomm = $gcomm_create($here, nprocs);
|
|---|
| 793 | void Process(int rank) {
|
|---|
| 794 | $comm comm = $comm_create($here, gcomm, rank);
|
|---|
| 795 |
|
|---|
| 796 | void Thread(int tid) {
|
|---|
| 797 | ...$comm_enqueue(comm, msg)...
|
|---|
| 798 | ...$comm_dequeue(comm, source, tag)...
|
|---|
| 799 | }
|
|---|
| 800 |
|
|---|
| 801 | for (int i=0; i<nthreads; i++) $spawn Thread(i);
|
|---|
| 802 | ...
|
|---|
| 803 | }
|
|---|
| 804 | for (int i=0; i<nprocs; i++) $spawn Process(i);
|
|---|
| 805 | ...
|
|---|
| 806 | \end{verbatim}
|
|---|
| 807 | \end{small}
|
|---|
| 808 | \caption{Code skeleton for model of multithreaded MPI program
|
|---|
| 809 | showing placement of global and local communicator objects}
|
|---|
| 810 | \label{fig:mpi-threads-comm}
|
|---|
| 811 | \end{figure}
|
|---|
| 812 |
|
|---|
| 813 | There is no special status given to the process which creates the
|
|---|
| 814 | \ccomm{} object of a given place. Any process which can access a
|
|---|
| 815 | handle for that \ccomm{} object can use it to send or receive
|
|---|
| 816 | messages, regardless of whether that process was the one that created
|
|---|
| 817 | the \ccomm{} object. However, users should be aware that verification
|
|---|
| 818 | is likely to be most efficient when variables are declared as locally
|
|---|
| 819 | as possible, so it is best to declare the \ccomm{} object in the
|
|---|
| 820 | innermost scope possible. Figure \ref{fig:mpi-threads-comm}
|
|---|
| 821 | illustrates an effective way to do this in the context of modeling a
|
|---|
| 822 | multithreaded MPI program. In the code skeleton, each thread can
|
|---|
| 823 | access the local communicator object of its process, but not that of
|
|---|
| 824 | any other process.
|
|---|
| 825 |
|
|---|
| 826 |
|
|---|
| 827 |
|
|---|
| 828 | % nonblocking interface: instead of message_pack, it is a request
|
|---|
| 829 | % form a new request and Isend it
|
|---|
| 830 | % can it be re-used?
|
|---|
| 831 | % buffer_create,
|
|---|
| 832 | % request create,
|
|---|
| 833 | % match, complete,
|
|---|
| 834 |
|
|---|
| 835 | \chapter{Specification}
|
|---|
| 836 |
|
|---|
| 837 | \section{Overview}
|
|---|
| 838 |
|
|---|
| 839 | Specification is the means by which one expresses what a program is
|
|---|
| 840 | supposed to do, i.e., what it means for it to be correct.
|
|---|
| 841 |
|
|---|
| 842 | There are several specification mechanisms in CIVL-C. First, there are
|
|---|
| 843 | the default properties: these are generic properties which are checked
|
|---|
| 844 | by default in any program, and require no additional specification
|
|---|
| 845 | effort. These properties include absence of deadlocks, division by 0,
|
|---|
| 846 | illegal pointer dereferences, and out of bounds array indexes.
|
|---|
| 847 |
|
|---|
| 848 | Many more program-specific properties can be specified using
|
|---|
| 849 | assertions. CIVL-C has a rich assertion language which extends the
|
|---|
| 850 | language of boolean-valued C expressions. Assumptions are a
|
|---|
| 851 | specification dual to assertions in that they restrict the set
|
|---|
| 852 | of executions on which the assertions are checked.
|
|---|
| 853 |
|
|---|
| 854 | Functional equivalence is a power specification mechanism. In this
|
|---|
| 855 | approach, two programs are provided, one playing the role of the
|
|---|
| 856 | specification, the other the role of the implementation. The
|
|---|
| 857 | implementation is correct if, for all inputs $x$, it produces the same
|
|---|
| 858 | output as that produced by the specification on input $x$. In other
|
|---|
| 859 | words, the two programs define the same function; this is sometimes
|
|---|
| 860 | known as \emph{input-output equivalence}. In order to take this
|
|---|
| 861 | approach, one must first have a way to specify what the inputs and
|
|---|
| 862 | outputs of a programs are; CIVL-C provides special keywords for this.
|
|---|
| 863 |
|
|---|
| 864 | Procedure contracts are another powerful specification mechanisms.
|
|---|
| 865 | These typically involve specifying preconditions and postconditions
|
|---|
| 866 | for a function. The function is correct if, whenever it is called in a
|
|---|
| 867 | state satisfying the precondition, when it returns the state will
|
|---|
| 868 | satsify the postcondition. A program is correct if all its functions
|
|---|
| 869 | satsify their contract.
|
|---|
| 870 |
|
|---|
| 871 | \section{Input-output signature}
|
|---|
| 872 |
|
|---|
| 873 | \subsection{Input type qualifier: \cinput}
|
|---|
| 874 |
|
|---|
| 875 | The declaration of a variable in the root scope may
|
|---|
| 876 | include the type qualifier \cinput, e.g.,
|
|---|
| 877 | \begin{verbatim}
|
|---|
| 878 | $input int n;
|
|---|
| 879 | \end{verbatim}
|
|---|
| 880 | This declares the variable to be an input variable, i.e., one which is
|
|---|
| 881 | considered to be an input to the program. Such a variable is
|
|---|
| 882 | initialized with an arbitrary (unconstrained) value of its type. When
|
|---|
| 883 | using symbolic execution to verify a program, such a variable will be
|
|---|
| 884 | assigned a unique symbolic constant of its type.
|
|---|
| 885 |
|
|---|
| 886 | In contrast, variables in the root scope which are not input variables
|
|---|
| 887 | will instead be initialized with the ``undefined'' value. If an
|
|---|
| 888 | undefined value is used in some way (such as in an argument to an
|
|---|
| 889 | operator), an error occurs.
|
|---|
| 890 |
|
|---|
| 891 | In addition, input variables may only be read, never written to.
|
|---|
| 892 |
|
|---|
| 893 | Alternatively, it is also possible to specify a particular concrete
|
|---|
| 894 | initial value for an input variable. This is done using a command
|
|---|
| 895 | line argument when verifying or running the program.
|
|---|
| 896 |
|
|---|
| 897 | Input (and output) variables also play a key role when determining
|
|---|
| 898 | whether two programs are functionally equivalent. Two programs are
|
|---|
| 899 | considered functionally equivalent if, whenever they are given the
|
|---|
| 900 | same inputs (i.e., corresponding \cinput{} variables are initialized
|
|---|
| 901 | with the same values) they will produce the same outputs (i.e.,
|
|---|
| 902 | corresponding \coutput{} variables will end up with the same values at
|
|---|
| 903 | termination).
|
|---|
| 904 |
|
|---|
| 905 | \subsection{Output type qualifier: \coutput}
|
|---|
| 906 |
|
|---|
| 907 | A variable in the root scope may be declared with this type qualifier
|
|---|
| 908 | to declare it to be an output variable. Output variables are ``dual''
|
|---|
| 909 | to input variables. They may only be written to, never read. They
|
|---|
| 910 | are used primarily in functional equivalence checking.
|
|---|
| 911 |
|
|---|
| 912 | \section{Assertions and assumptions}
|
|---|
| 913 |
|
|---|
| 914 | \subsection{Assertions: \cassert}
|
|---|
| 915 |
|
|---|
| 916 | The system function \cassert{} takes an argument of boolean type:
|
|---|
| 917 | \begin{verbatim}
|
|---|
| 918 | void $assert (_Bool expr);
|
|---|
| 919 | \end{verbatim}
|
|---|
| 920 | During verification, the assertion is checked. If it cannot be proved
|
|---|
| 921 | that it must hold, a violation is reported.
|
|---|
| 922 |
|
|---|
| 923 | Note that CIVL-C boolean expressions have a richer syntax than C
|
|---|
| 924 | expressions, and may include universal or existential quantifiers
|
|---|
| 925 | (see below), and the boolean values \ctrue{} and \cfalse{}.
|
|---|
| 926 |
|
|---|
| 927 | The assertion function may take additional optional arguments used to
|
|---|
| 928 | print a specific message if the assertion is violated. These
|
|---|
| 929 | additional arguments are similar in form to those used in C's
|
|---|
| 930 | \texttt{printf} statement: a format string, followed by some number of
|
|---|
| 931 | arguments which are evaluated and substituted for successive codes in
|
|---|
| 932 | the format string. For example,
|
|---|
| 933 | \begin{verbatim}
|
|---|
| 934 | $assert(x<=B, "x-coordinate %f exceeds bound %f", x, B);
|
|---|
| 935 | \end{verbatim}
|
|---|
| 936 |
|
|---|
| 937 | The C function \texttt{assert}, included in the standard library
|
|---|
| 938 | \texttt{assert.h}, is identical to \cassert. Programmers are
|
|---|
| 939 | free to use either one.
|
|---|
| 940 |
|
|---|
| 941 |
|
|---|
| 942 | \subsection{Assume statements: \cassume}
|
|---|
| 943 |
|
|---|
| 944 | As \emph{assume statement} has the form
|
|---|
| 945 | \begin{verbatim}
|
|---|
| 946 | $assume expr;
|
|---|
| 947 | \end{verbatim}
|
|---|
| 948 | During verification, the assumed expression is assumed to hold. If
|
|---|
| 949 | this leads to a contradiction on some execution, that execution is
|
|---|
| 950 | simply ignored. It never reports a violation, it only restricts the
|
|---|
| 951 | set of possible executions that will be explored by the verification
|
|---|
| 952 | algorithm.
|
|---|
| 953 |
|
|---|
| 954 | Like as assertion statement, as assume statement can be used any place
|
|---|
| 955 | a statement is expected. In addition, as assume statement can be used
|
|---|
| 956 | in file scope to place restrictions on the global variables of the
|
|---|
| 957 | programs. For example,
|
|---|
| 958 | \begin{verbatim}
|
|---|
| 959 | $input int B;
|
|---|
| 960 | $input int N;
|
|---|
| 961 | $assume 0<=N && N<=B;
|
|---|
| 962 | \end{verbatim}
|
|---|
| 963 | declares \texttt{N} and \texttt{B} to be integer inputs and restricts
|
|---|
| 964 | consideration to inputs satisfying $0\leq\texttt{N}\leq\texttt{B}$.
|
|---|
| 965 |
|
|---|
| 966 |
|
|---|
| 967 | \section{Formulas}
|
|---|
| 968 |
|
|---|
| 969 | A formula is a boolean expression that can be used in an assert
|
|---|
| 970 | statement, assume statement, procedure contract (below), or invariant.
|
|---|
| 971 | Any ordinary C boolean expression is a formula. CIVL-C provides some
|
|---|
| 972 | additional kinds of formulas, described below.
|
|---|
| 973 |
|
|---|
| 974 | \subsection{Implication: \cimplies}
|
|---|
| 975 |
|
|---|
| 976 | The binary operation \cimplies{} represents logical implication.
|
|---|
| 977 | The expression \verb!p=>q! is equivalent to \verb~(!p)||q~.
|
|---|
| 978 |
|
|---|
| 979 | \subsection{Universal quantifier: \cforall}
|
|---|
| 980 |
|
|---|
| 981 | The universally qunatified formula has the form
|
|---|
| 982 | \begin{verbatim}
|
|---|
| 983 | $forall { type identifier | restriction} expr
|
|---|
| 984 | \end{verbatim}
|
|---|
| 985 | where \verb!type! is a type name (e.g., \texttt{int} or
|
|---|
| 986 | \texttt{double}), \verb!identifier! is the name of the bound variable,
|
|---|
| 987 | \verb!restriction! is a boolean expression which expresses some
|
|---|
| 988 | restriction on the values that the bound variable can take, and
|
|---|
| 989 | \verb!expr! is a formula. The universally quantified formula
|
|---|
| 990 | holds iff for all values assignable to the bound variable
|
|---|
| 991 | for which the restriction holds, the formula \ct{expr} holds.
|
|---|
| 992 |
|
|---|
| 993 | A variation on the construct above can be used in the special case
|
|---|
| 994 | where the bound variable is to range over a finite interval
|
|---|
| 995 | of integers. In this case the quantified formula may be written:
|
|---|
| 996 | \begin{verbatim}
|
|---|
| 997 | $forall { type identifier=lower .. upper } expr
|
|---|
| 998 | \end{verbatim}
|
|---|
| 999 | where \ct{lower} and \ct{upper} are integer expressions.
|
|---|
| 1000 |
|
|---|
| 1001 | \subsection{Existential quantifier: \cexists}
|
|---|
| 1002 |
|
|---|
| 1003 | The syntax for existentially quantified expressions is exactly the
|
|---|
| 1004 | same as for universally quantified expressions, with \cexists{} in
|
|---|
| 1005 | place of \cforall{}.
|
|---|
| 1006 |
|
|---|
| 1007 | \section{Contracts}
|
|---|
| 1008 |
|
|---|
| 1009 | \subsection{Procedure contracts: \crequires{} and \censures{}}
|
|---|
| 1010 | The \crequires{} and \censures{} primitives are used to encode
|
|---|
| 1011 | procedure contracts. There are optional
|
|---|
| 1012 | elements that may occur in a procedure declaration or definition,
|
|---|
| 1013 | as follows. For a function prototype:
|
|---|
| 1014 | \begin{verbatim}
|
|---|
| 1015 | T f(...)
|
|---|
| 1016 | $requires expr;
|
|---|
| 1017 | $ensures expr;
|
|---|
| 1018 | ;
|
|---|
| 1019 | \end{verbatim}
|
|---|
| 1020 | For a function definition:
|
|---|
| 1021 | \begin{verbatim}
|
|---|
| 1022 | T f(...)
|
|---|
| 1023 | $requires expr;
|
|---|
| 1024 | $ensures expr;
|
|---|
| 1025 | {
|
|---|
| 1026 | ...
|
|---|
| 1027 | }
|
|---|
| 1028 | \end{verbatim}
|
|---|
| 1029 | The value \cresult{} may be used in post-conditions to refer
|
|---|
| 1030 | to the result returned by a procedure.
|
|---|
| 1031 |
|
|---|
| 1032 | \emph{Status}: parsed, but nothing is currently done with this
|
|---|
| 1033 | information.
|
|---|
| 1034 |
|
|---|
| 1035 | \subsection{Loop invariants: \cinvariant}
|
|---|
| 1036 |
|
|---|
| 1037 | This indicates a loop invariant. Each C loop
|
|---|
| 1038 | construct has an optional invariant clause as follows:
|
|---|
| 1039 | \begin{verbatim}
|
|---|
| 1040 | while (expr) $invariant (expr) stmt
|
|---|
| 1041 | for (e1; e2; e3) $invariant (expr) stmt
|
|---|
| 1042 | do stmt while (expr) $invariant (expr) ;
|
|---|
| 1043 | \end{verbatim}
|
|---|
| 1044 | The invariant encodes the claim that if \texttt{expr} holds upon
|
|---|
| 1045 | entering the loop and the loop condition holds, then it will hold
|
|---|
| 1046 | after completion of execution of the loop body. The invariant is used
|
|---|
| 1047 | by certain verification techniques.
|
|---|
| 1048 |
|
|---|
| 1049 | \emph{Status:} parsed, but nothing is currently done with this
|
|---|
| 1050 | information.
|
|---|
| 1051 |
|
|---|
| 1052 | \section{Concurrency specification}
|
|---|
| 1053 |
|
|---|
| 1054 | \subsection{Remote expressions: \texttt{e@x}}.
|
|---|
| 1055 |
|
|---|
| 1056 | These have the form \verb!expr@x! and refer to a variable in another
|
|---|
| 1057 | process, e.g., \verb!procs[i]@x!. This special kind of expression is
|
|---|
| 1058 | used in collective expressions, which are used to formulate collective
|
|---|
| 1059 | assertions and invariants.
|
|---|
| 1060 |
|
|---|
| 1061 | The expression \verb!expr! must have \cproc{} type. The variable
|
|---|
| 1062 | \texttt{x} must be a statically visible variable in the context in
|
|---|
| 1063 | which it is occurs. When this expression is evaluated, the evaluation
|
|---|
| 1064 | context will be shifted to the process referred to by \texttt{expr}.
|
|---|
| 1065 |
|
|---|
| 1066 | \emph{Status}: not implemented.
|
|---|
| 1067 |
|
|---|
| 1068 | \subsection{Collective expressions: \ccollective}. These have the form
|
|---|
| 1069 | \begin{verbatim}
|
|---|
| 1070 | $collective(proc_expr, int_expr) expr
|
|---|
| 1071 | \end{verbatim}
|
|---|
| 1072 | This is a collective expression over a set of processes. The
|
|---|
| 1073 | expression \texttt{proc{\U}expr} yields a pointer to the first element
|
|---|
| 1074 | of an array of \cproc. The expression \texttt{int{\U}expr} gives the
|
|---|
| 1075 | length of that array, i.e., the number of processes. Expression
|
|---|
| 1076 | \texttt{expr} is a boolean-valued expression; it may use remote
|
|---|
| 1077 | expressions to refer to variables in the processes specified in the
|
|---|
| 1078 | array. Example:
|
|---|
| 1079 | \begin{verbatim}
|
|---|
| 1080 | $proc procs[N];
|
|---|
| 1081 | ...
|
|---|
| 1082 | $assert $collective(procs, N) i==procs[(pid+1)%N]@i ;
|
|---|
| 1083 | \end{verbatim}
|
|---|
| 1084 |
|
|---|
| 1085 | \emph{Status}: not implemented.
|
|---|
| 1086 |
|
|---|
| 1087 | \chapter{Pointers and Heaps}
|
|---|
| 1088 | \label{chap:pointers}
|
|---|
| 1089 |
|
|---|
| 1090 | CIVL-C supports pointers, using the same operators with the same
|
|---|
| 1091 | meanings as C (\texttt{\&}, \texttt{*}, pointer arithmetic). There is
|
|---|
| 1092 | also a heap in every scope, and system functions to allocate and
|
|---|
| 1093 | deallocate objects in the specified scope.
|
|---|
| 1094 |
|
|---|
| 1095 | \section{Memory functions: \texttt{memcpy}}
|
|---|
| 1096 |
|
|---|
| 1097 | The function \texttt{memcpy} is defined in the standard C library
|
|---|
| 1098 | \texttt{string.h} and works exactly the same in CIVL-C: it copies
|
|---|
| 1099 | data from the region pointed to by \ct{q} to that pointed to by
|
|---|
| 1100 | \ct{p}. The signature is
|
|---|
| 1101 |
|
|---|
| 1102 | \begin{verbatim}
|
|---|
| 1103 | void memcpy(void *p, void *q, size_t size);
|
|---|
| 1104 | \end{verbatim}
|
|---|
| 1105 |
|
|---|
| 1106 | \section{Heaps, \cmalloc{} and \texttt{free}}
|
|---|
| 1107 |
|
|---|
| 1108 | As mentioned above, each dynamic scope has an implicit heap on which
|
|---|
| 1109 | objects can be allocated and deallocated dynamically. To allocate an
|
|---|
| 1110 | object, one first needs a reference to the dynamic scope to be used.
|
|---|
| 1111 | The system function $\cmalloc$ is like C's \texttt{malloc}, but takes
|
|---|
| 1112 | this extra scope argument:
|
|---|
| 1113 | \begin{verbatim}
|
|---|
| 1114 | void * $malloc($scope scope, int size);
|
|---|
| 1115 | \end{verbatim}
|
|---|
| 1116 | The standard C function
|
|---|
| 1117 | \begin{verbatim}
|
|---|
| 1118 | void * malloc(int size);
|
|---|
| 1119 | \end{verbatim}
|
|---|
| 1120 | is equivalent to \verb!$malloc($root, size)!.
|
|---|
| 1121 |
|
|---|
| 1122 | The system function \ct{free} is used to deallocate a heap object;
|
|---|
| 1123 | it is just like C's \texttt{free}:
|
|---|
| 1124 | \begin{verbatim}
|
|---|
| 1125 | void free(void *p);
|
|---|
| 1126 | \end{verbatim}
|
|---|
| 1127 |
|
|---|
| 1128 | % \section{Pointer types}
|
|---|
| 1129 |
|
|---|
| 1130 | % Given any object type $T$ and a static scope $s$ in a CIVL-C program,
|
|---|
| 1131 | % there is a type \emph{pointer-to-$T$-in-$s$}. The type is used to
|
|---|
| 1132 | % represent a pointer to a memory location of type $T$ in scope $s$ or a
|
|---|
| 1133 | % descendant of $s$ (i.e., some scope contained in $s$).
|
|---|
| 1134 |
|
|---|
| 1135 | % If scope $s_1$ is a descendant of $s_2$ (i.e., $s_1$ is lexically
|
|---|
| 1136 | % contained in $s_2$), the type \emph{pointer-to-$T$-in-$s_1$} is a
|
|---|
| 1137 | % subtype of \emph{pointer-to-$T$-in-$s_2$}. This means that any
|
|---|
| 1138 | % expression of the first type can be used wherever an object of the
|
|---|
| 1139 | % second type is expected. In particular, any expression $e$ of the
|
|---|
| 1140 | % subtype can be assigned to a left-hand-side expression of the
|
|---|
| 1141 | % supertype without explicit casts; also $e$ can be used as an argument
|
|---|
| 1142 | % to a function for which the corresponding parameter has the supertype.
|
|---|
| 1143 |
|
|---|
| 1144 | % The syntax for denoting this type adheres to the usual C syntax for
|
|---|
| 1145 | % denoting the type \emph{pointer-to-$T$} with the addition of a scope
|
|---|
| 1146 | % parameter within angular brackets immediately following the \texttt{*}
|
|---|
| 1147 | % token. For example, to declare a variable \texttt{p} of type
|
|---|
| 1148 | % \emph{pointer-to-$T$-in-$s$}, one writes
|
|---|
| 1149 | % \begin{verbatim}
|
|---|
| 1150 | % int *<s> p;
|
|---|
| 1151 | % \end{verbatim}
|
|---|
| 1152 | % If the scope modifier \texttt{<...>} is absent, the scope is taken to
|
|---|
| 1153 | % be the root scope $s_0$. The object has type
|
|---|
| 1154 | % \emph{pointer-to-$T$-in-$s_0$}, which is abreviated as
|
|---|
| 1155 | % \emph{pointer-to-$T$}. In this way, stanard C programs can be
|
|---|
| 1156 | % interpreted as CIVL-C programs.
|
|---|
| 1157 |
|
|---|
| 1158 | % \section{Address-of operator}
|
|---|
| 1159 |
|
|---|
| 1160 | % The address-of operator \texttt{\&} returns a pointer of the
|
|---|
| 1161 | % appropriate subtype using the innermost scope in which its left-hand-side
|
|---|
| 1162 | % argument is declared. For example
|
|---|
| 1163 |
|
|---|
| 1164 | % \begin{verbatim}
|
|---|
| 1165 | % {
|
|---|
| 1166 | % $scope s1 = $here();
|
|---|
| 1167 | % int x;
|
|---|
| 1168 | % double a[N];
|
|---|
| 1169 | % int *<s1> p = &x;
|
|---|
| 1170 | % double *<s1> q = &a[2];
|
|---|
| 1171 | % }
|
|---|
| 1172 | % \end{verbatim}
|
|---|
| 1173 | % is correct (in particular, it is type-correct) because \texttt{\&x}
|
|---|
| 1174 | % has type \emph{pointer-to-\texttt{int}-in-\texttt{s1}}, since
|
|---|
| 1175 | % \texttt{s1} is the scope in which \texttt{x} is declared.
|
|---|
| 1176 |
|
|---|
| 1177 | % Another pointer example:
|
|---|
| 1178 | % \begin{small}
|
|---|
| 1179 | % \begin{verbatim}
|
|---|
| 1180 | % { $scope s0 = $here();
|
|---|
| 1181 | % { $scope s1 = $here();
|
|---|
| 1182 | % double x;
|
|---|
| 1183 | % { $scope s2 = $here();
|
|---|
| 1184 | % double y;
|
|---|
| 1185 | % double *<s1> p;
|
|---|
| 1186 | % /* p can only point to something in s1 or descendant, for example, s2 */
|
|---|
| 1187 | % p = &x; // fine
|
|---|
| 1188 | % p = &y; // fine
|
|---|
| 1189 | % p = (double*)$malloc(s0, 10*sizeof(double)); // static type error
|
|---|
| 1190 | % }
|
|---|
| 1191 | % }
|
|---|
| 1192 | % }
|
|---|
| 1193 | % \end{verbatim}
|
|---|
| 1194 | % \end{small}
|
|---|
| 1195 |
|
|---|
| 1196 | % \section{Pointer addition and subtractions}
|
|---|
| 1197 |
|
|---|
| 1198 | % If \texttt{e} is an expression of type \emph{pointer-to-$T$-in-$s$}
|
|---|
| 1199 | % and \texttt{i} is an expression of integer type then \texttt{e+i} also
|
|---|
| 1200 | % has type \emph{pointer-to-$T$-in-$s$}. In other words, pointer
|
|---|
| 1201 | % addition cannot leave the scope of the original pointer. This
|
|---|
| 1202 | % reflects the fact that every object is contained in one scope, and
|
|---|
| 1203 | % pointer addition cannot leave the object.
|
|---|
| 1204 |
|
|---|
| 1205 | % Pointer subtraction is defined on two pointers of the same type, where
|
|---|
| 1206 | % ``same'' includes the scope. That is checked statically. As in C, it
|
|---|
| 1207 | % is only defined if the two pointers point to the same object. In
|
|---|
| 1208 | % CIVL-C, a runtime error will be thrown if they do not point to the
|
|---|
| 1209 | % same object.
|
|---|
| 1210 |
|
|---|
| 1211 | % \section{Semantics of scopes and pointer types}
|
|---|
| 1212 |
|
|---|
| 1213 | % A variable of type \cscope{} is treated like any other variable.
|
|---|
| 1214 | % It becomes part of the state when the scope in which it is declared
|
|---|
| 1215 | % is instantiated to form a dynamic scope. The variable is
|
|---|
| 1216 | % initialized at that time and its value cannot change.
|
|---|
| 1217 |
|
|---|
| 1218 | % Each time a dynamic scope is instantiated, it is assigned a unique ID
|
|---|
| 1219 | % number. The exactly value of the ID number is not relevant, it just
|
|---|
| 1220 | % has to be distince from any other scope ID number that currently
|
|---|
| 1221 | % exists in the state. This is the value that is assigned to the scope
|
|---|
| 1222 | % variable. Therefore, if a static scope contains a scope variable, and
|
|---|
| 1223 | % that scope is instantiated twice to form two distinct dynamic scopes,
|
|---|
| 1224 | % the values assigned to the two variables will be distinct.
|
|---|
| 1225 |
|
|---|
| 1226 | % A pointer value is an ordered pair $\langle \delta,r \rangle$, where
|
|---|
| 1227 | % $\delta$ is a dynamic scope ID and $r$ is a reference to a memory
|
|---|
| 1228 | % location in the static scope associated to $\delta$. (We will define
|
|---|
| 1229 | % the exact form of a reference later.)
|
|---|
| 1230 |
|
|---|
| 1231 | % When a dynamic scope is instantiated, each new variable created is
|
|---|
| 1232 | % assigned a \emph{dynamic type}. This is a refinement of the static
|
|---|
| 1233 | % type associated to the static variable. Every dynamic type
|
|---|
| 1234 | % is an instance of exactly one static type. The dynamic
|
|---|
| 1235 | % type of the newly instantiated variable is an instance of the
|
|---|
| 1236 | % static type of the static variable.
|
|---|
| 1237 |
|
|---|
| 1238 | % The dynamic pointer types have the form
|
|---|
| 1239 | % \emph{pointer-to-$t$-in-$\delta$}, where $t$ is a dynamic type and
|
|---|
| 1240 | % $\delta$ is a dynamic scope ID. For a program to be dynamically type
|
|---|
| 1241 | % safe, such a variable should hold only values of the form $\langle
|
|---|
| 1242 | % \delta, r\rangle$. In particular, the variable should never be
|
|---|
| 1243 | % assigned a value where the dynamic scope component is a different
|
|---|
| 1244 | % instance of the static scope $s$ associated to $\delta$.
|
|---|
| 1245 |
|
|---|
| 1246 | % \section{Pointer casts}
|
|---|
| 1247 |
|
|---|
| 1248 | % If scope $s_1$ is contained in scope $s_2$, an expression of type
|
|---|
| 1249 | % \emph{pointer-to-$T$-in-$s_1$} can always be cast to
|
|---|
| 1250 | % \emph{pointer-to-$T$-in-$s_2$},
|
|---|
| 1251 | % because the first is a subtype of the second. (As described above,
|
|---|
| 1252 | % the cast is unnecessary.)
|
|---|
| 1253 |
|
|---|
| 1254 | % The cast in the other direction is also allowed, but the dynamic type
|
|---|
| 1255 | % safety of that cast will only be checked at runtime. In particular, a
|
|---|
| 1256 | % runtime error will result if the cast attempts to cast the pointer
|
|---|
| 1257 | % value to a dynamic scope which does not contain (is an ancestor of)
|
|---|
| 1258 | % the dynamic scope component of the pointer value.
|
|---|
| 1259 |
|
|---|
| 1260 | % A type \emph{pointer-to-$T_1$-in-$s$} can be cast to a type
|
|---|
| 1261 | % \emph{pointer-to-$T_2$-in-$s$} according to the usual rules of C. In
|
|---|
| 1262 | % other words, usual casting rules apply as long as you don't change the
|
|---|
| 1263 | % scope.
|
|---|
| 1264 |
|
|---|
| 1265 | % \section{Scope-Parameterized Functions}
|
|---|
| 1266 |
|
|---|
| 1267 | % Coming soon. (Parsed, type checked, not currently used otherwise.)
|
|---|
| 1268 |
|
|---|
| 1269 | % \section{Scope-Parameterized Type Definitions}
|
|---|
| 1270 |
|
|---|
| 1271 | % Coming soon. (Ditto.)
|
|---|
| 1272 |
|
|---|
| 1273 | \chapter{Libraries}
|
|---|
| 1274 |
|
|---|
| 1275 | Each of the following libraries is at least partially implemented and can
|
|---|
| 1276 | be included in a CIVL-C program:
|
|---|
| 1277 | \begin{itemize}
|
|---|
| 1278 | \item \ct{stdlib}
|
|---|
| 1279 | \item \ct{stdbool}
|
|---|
| 1280 | \item \ct{stdio}
|
|---|
| 1281 | \item \ct{assert}
|
|---|
| 1282 | \end{itemize}
|
|---|