source: CIVL/doc/manual/part-language.tex@ 50f834b

1.23 2.0 main test-branch
Last change on this file since 50f834b was 3afdb2e, checked in by Andre Marianiello <andre.marianiello@…>, 12 years ago

Changed "chile" to "child"

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@1060 fb995dde-84ed-4084-dfe6-e5aef3e2452c

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