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

1.23 2.0 main test-branch
Last change on this file since f8f3bec was 0ce4e13, checked in by Manchun Zheng <zmanchun@…>, 10 years ago

prepared manual and readme for v1.7.1 release

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

  • Property mode set to 100644
File size: 82.6 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 \texttt{civlc.cvh} defines standard types inherited from C.
178The boolean type is denoted \verb!_Bool!, as in C. Its values are $0$
179and $1$, which are also denoted by $\cfalse$ and $\ctrue$,
180respectively.
181
182There is one integer type, corresponding to the mathematical integers.
183Currently, all of the C integer types \texttt{int}, \texttt{long},
184\texttt{unsigned\ int}, \texttt{short}, etc., are mapped to the CIVL
185integer type.
186
187There is one real type, corresponding to the mathematical real
188numbers. Currently, all of the C real types \texttt{double},
189\texttt{float}, etc., are mapped to the CIVL real type.
190
191Array types, \texttt{struct} and \texttt{union} types, \texttt{char},
192and pointer types (including pointers to functions) are all exactly as
193in C.
194
195% \subsection{The heap type $\cheap$ and handles}
196
197% Unlike C, a CIVL-C program does not necessarily have access to a
198% single, global heap. Instead, there is a $\cheap$ type, and heaps may
199% be declared explicitly wherever they are needed. Hence a CIVL-C
200% program may have several heaps, and these may exist in different
201% scopes.
202
203% A heap is declared and created as follows:
204% \begin{verbatim}
205% $heap h = $heap_create();
206% \end{verbatim}
207% The function \verb!$heap_create()! creates a new empty heap in the
208% current scope and returns a \emph{handle} to that heap. A handle is
209% like a pointer: it is a reference to another object. However, a handle
210% is much more restricted than a general pointer. In particular, it
211% cannot be dereferenced (by the \ct{*} operator). The underlying heap
212% object can only be accessed by using a handle to it as an argument to
213% a system function.
214
215% Handles can be used in assignments and passed as arguments to functions.
216% For example, this declaration could follow the one above:
217% \begin{verbatim}
218% $heap h2=h;
219% \end{verbatim}
220% After executing this code, \ct{h2} and \ct{h} will be aliased, i.e., the two
221% handles will refer to the same heap object.
222
223% The heap object exists in the scope in which it is created. In
224% particular, it will disappear when that scope disappears, i.e., when
225% control reaches the right curly brace that defines the end of the
226% scope. At that point, any references into the heap become invalid.
227
228% The following system functions deal with heaps:
229% \begin{verbatim}
230% void* $malloc($heap h, int size);
231% void free(void *p)
232% \end{verbatim}
233% The first function is like C's \texttt{malloc}, except that you
234% specify the heap in which the allocation takes place.
235% This modifies the specified heap and returns a pointer to the new object.
236% The function can only occur in a context in which the type of the object is
237% specified, as in:
238% \begin{verbatim}
239% $heap h;
240% int n = 10;
241% double *p = (double*)$malloc(h, n*sizeof(double));
242% \end{verbatim}
243% The function \ct{free} is exactly the same as in C. Note that
244% \texttt{free} modifies the heap which was used to allocate \texttt{p}.
245
246
247\subsection{The bundle type: \cbundle}
248\label{subsec:bundleType}
249
250CIVL-C includes a type named \cbundle, declared in the CIVL-C standard header \texttt{bundle.cvh}. A bundle is basically a
251sequence of data, wrapped into an atomic package. A bundle is created
252using a function that specifies a region of memory. One can create a
253bundle from an array of integers, and another bundle from an array of
254reals. Both bundles have the same type, \cbundle. They can therefore
255be entered into an array of \cbundle, for example. Hence bundles are
256useful for mixing objects of different (even statically unknown) types
257into a single data structure. Later, the contents of a bundle can be
258extracted with another function that specifies a region of memory into
259which to unpack the bundle; if that memory does not have the right
260type to receive the contents of the bundle, a runtime error is
261generated. The bundle type and its functions are provided by the library \texttt{bundle.cvh}.
262
263The relevant functions for creating and manipulating bundles
264are given in Section \ref{subsec:bundleLibrary}.
265
266\subsection{The \cscope{} type}
267\label{sec:scopetype}
268
269An object of type $\cscope$ is a reference to a dynamic scope. It may
270be thought of as a ``dynamic scope ID, '' but it is not an integer and
271cannot be converted to an integer. Operations defined on scopes are
272discussed in Section \ref{sec:scopeexpr}.
273
274\subsection{The \crange{} and \cdomain{} types}
275
276CIVL-C provides certain abstract datatypes that are useful for
277representing iteration spaces of loops in an abstract way.
278
279First, there is a built-in type $\crange$. An object of this type
280represents an ordered set of integers. There are expressions for
281specifying range values; these are described in Section
282\ref{sec:range_expr}. Ranges are typically used as a step
283in constructing \emph{domains}, described next.
284
285A domain type is used to represent a set of tuples of integer values.
286Every tuple in a domain object has the same arity (i.e., number of
287components). The arity must be at least 1, and is called the
288\emph{dimension} of the domain object.
289
290For each integer constant expression $n$, there is a type
291\cdomainof{\(n\)}, representing domains of dimension $n$.
292% \texttt{\cdomain{}(}]\(n\)\texttt{)}
293The \emph{universal domain type}, denoted \cdomain{}, represents
294domains of all positive dimensions, i.e., it is the union over all
295$n\geq 1$ of \cdomainof{\(n\)}. In particular, each \cdomainof{\(n\)}
296is a subtype of \cdomain{}.
297
298There are expressions for specifying domain values; these are
299described in Section \ref{sec:domain_expr}. There are also certains
300statements that use domains, such as the ``CIVL-\emph{for}'' loop
301\cfor; see Section \ref{sec:cfor}.
302
303
304\section{Expressions}
305
306\subsection{Expressions inherited from C}
307
308The following C expressions are included in CIVL:
309\begin{itemize}
310\item \emph{constant} expressions
311\item \emph{identifier} expressions (\texttt{x})
312\item parenthetical expressions (\verb!(e)!)
313\item numerical \emph{addition} (\verb!a+b!), \emph{subtraction} (\verb!a-b!),
314 \emph{multiplication} (\verb!a*b!), \emph{division} (\verb!a/b!),
315 \emph{unary plus} (\verb!+a!), \emph{unary minus} (\verb!-a!),
316 \emph{integer division} (\verb!a/b!) and \emph{modulus} (\verb!a%b!),
317 all with their ideal mathematical interpretations
318\item array \emph{index} expressions (\verb!a[e]!) and struct or union
319 \emph{navigation} expressions (\verb!x.f!, \verb!p->f!)
320\item \emph{address-of} (\verb!&e!), pointer \emph{dereference} (\verb!*p!),
321 pointer \emph{addition} (\verb!p+i!) and \emph{subtraction} (\verb!p-q!)
322 expressions
323\item relational expressions (\verb!a==b!, \verb~a!=b~, \verb!a>=b!,
324 \verb!a<=b!, \verb!a<b!, \verb!a>b!)
325\item logical \emph{not} (\verb~!p~), \emph{and} (\verb!p&&q!), and
326 \emph{or} (\verb!p||q!)
327\item \emph{sizeof} a type (\verb!sizeof(t)!) or expression (\verb!sizeof(e)!)
328\item \emph{assignment} expressions (\verb!a=b!, \verb!a+=b!, \verb!a-=b!,
329 \verb!a*=b!, \verb!a/=b!, \verb!a%=b!, \verb!a++!, \verb!a--!)
330\item function \emph{calls} \verb!f(e1,...,en)!
331\item \emph{conditional} expressions (\verb!b ? e : f!).
332\item \emph{cast} expressions (\verb!(t)e!)
333\end{itemize}
334
335Bit-wise operations are not yet supported.
336
337\subsection{Scope expressions}
338\label{sec:scopeexpr}
339
340As mentioned in Section \ref{sec:scopetype}, CIVL-C provides a type
341\cscope. An object of this type is a reference to a dynamic scope.
342Several constants, expressions, and functions dealing with the
343\cscope{} type are also provided.
344
345The $\cscope$ type is like any other object type. It may be used as
346the element type of an array, a field in a structure or union, and so
347on. Expressions of type $\cscope$ may occur on the left or right-hand
348sides of assignments and as arguments in function calls just like any
349other expression. Two different variables of type $\cscope$ may be
350aliased, i.e., they may refer to the same dynamic scope.
351
352A dynamic scope $\delta$ is \emph{reachable} if there exists a path
353which starts from the dyscope referenced by some frame on the call
354stack of a process, follows the parent edges in the dyscope tree, and
355terminates in $\delta$. If a dyscope is not reachable, it can never
356become reachable, and it cannot have any effect on the subsequent
357execution of the program.
358
359Normally, a dynamic scope will eventually become unreachable. At some
360point after it becomes unreachable, it will be collected in a
361garbage-collection-like sweep, and any existing references to that
362scope will become \emph{undefined}. An object of type $\cscope$ is
363also undefined before it is initialized. Any use of an undefined
364value is reported as an error by CIVL, so it is important to be sure
365that a scope variable is defined before using it.
366
367
368\subsubsection{Checking if a dyscope is defined: \cscopedefined}
369
370The header \texttt{civlc.cvh} provides a function \cscopedefined{}, which checks if a given
371value of \cscope{} type is defined, as described in Section \ref{subsubsec:scopedefined}.
372
373\subsubsection{The constant \chere}
374
375A constant \chere{} exists in every scope. This constant has
376type \cscope{} and refers to the dynamic scope in which it is
377contained. For example,
378\begin{verbatim}
379 { // scope s
380 int *p = (int*)$malloc($here, n*sizeof(int));
381 }
382\end{verbatim}
383allocates an object consisting of $n$ ints in the scope $s$.
384
385\subsubsection{The constant \cscoperoot{}}
386
387There is a global constant \cscoperoot{} of type $\cscope$ which
388refers to the root dynamic scope.
389
390
391\subsubsection{Scope relational operators}
392
393Let $s_1$ and $s_2$ be expressions of type \cscope. The following are
394all CIVL-C expressions of boolean type:
395\begin{itemize}
396\item $s_1$ \ct{==} $s_2$. This is \emph{true} iff $s_1$ and $s_2$
397 refer to the same dynamic scope.
398\item $s_1$ \ct{!=} $s_2$. This is \emph{true} iff $s_1$ and $s_2$
399 refer to different dynamic scopes.
400\item $s_1$ \ct{<=} $s_2$. This is \emph{true} iff $s_1$ is equal to
401 or a descendant of $s_2$, i.e., $s_1$ is equal to or contained in $s_2$.
402\item $s_1$ \ct{<} $s_2$. This is \emph{true} iff $s_1$ is a strict
403 descendant of $s_2$, i.e., $s_1$ is contained in $s_2$ and is not
404 equal to $s_2$.
405\item $s_1$ \ct{>} $s_2$. This is equivalent to $s_2$ \ct{<} $s_1$.
406\item $s_1$ \ct{>=} $s_2$. This is equivalent to $s_2$ \ct{<=} $s_1$.
407\end{itemize}
408If $s_1$ or $s_2$ is undefined in any of these expressions, an error
409will be reported.
410
411\subsubsection{Scope parent function \texorpdfstring{\cscopeparent}{\$scope\_parent}}
412
413The CIVL-C header \texttt{scope.cvh} provides the function \cscopeparent{} that computes the immediate
414parent of a dynamic scope, as described in Section \ref{subsec:scopeLibrary}.
415
416\subsubsection{Lowest Common Ancestor: \ct{+}}
417
418The expression $s_1$ \ct{+} $s_2$, where $s_1$ and $s_2$ are
419expressions of type \cscope, evaluates to the lowest common ancestor
420of $s_1$ and $s_2$ in the dynamic scope tree. This is the smallest
421dynamic scope containing both $s_1$ and $s_2$.
422
423\subsubsection{The \cscopeof{} expression}
424
425Given any left-hand-side expression \ct{expr}, the expression
426\begin{verbatim}
427 $scopeof(expr)
428\end{verbatim}
429evaluates to the dynamic scope containing the object specified by
430\ct{expr}.
431
432The following example illustrates the semantics of the \cscopeof{}
433operator. All of the assertions hold:
434\begin{verbatim}
435{
436 $scope s1 = $here;
437 int x;
438 double a[10];
439
440 {
441 $scope s2 = $here;
442 int *p = &x;
443 double *q = &a[4];
444
445 assert($scopeof(x)==s1);
446 assert($scopeof(p)==s2);
447 assert($scopeof(*p)==s1);
448 assert($scopeof(a)==s1);
449 assert($scopeof(a[5])==s1);
450 assert($scopeof(q)==s2);
451 assert($scopeof(*q)==s1);
452 }
453}
454\end{verbatim}
455
456\subsection{Range and domain expressions}
457
458\subsubsection{Regular range expressions}
459\label{sec:range_expr}
460
461An expression of the form
462\begin{verbatim}
463 lo .. hi
464\end{verbatim}
465where \texttt{lo} and \texttt{hi} are integer expressions, represents
466the range consisting of the integers $\texttt{lo}, \texttt{lo}+1,
467\ldots, \texttt{hi}$ (in that order).
468
469An expression of the form
470\begin{verbatim}
471 lo .. hi # step
472\end{verbatim}
473where \texttt{lo}, \texttt{hi}, and \texttt{step} are integer
474expressions is interpreted as follows. If \texttt{step} is positive,
475it represents the range consisting of $\texttt{lo},
476\texttt{lo}+\texttt{step}, \texttt{lo}+2*\texttt{step}, \ldots$, up to
477and possibly including \texttt{hi}. To be precise, the infinite
478sequence is intersected with the set of integers less than or equal to
479\texttt{hi}.
480
481If \texttt{step} is negative, the expression represents the range
482consisting of $\texttt{hi}, \texttt{hi}+\texttt{step},
483\texttt{hi}+2*\texttt{step}, \ldots$, down to and possibly including
484\texttt{lo}. Precisely, the infinite sequence is intersected with the
485set of integers greater than or equal to \texttt{lo}.
486
487\subsubsection{Cartesian domain expressions}
488\label{sec:domain_expr}
489
490An expression of the form
491\begin{verbatim}
492 ($domain) { r1, ..., rn }
493\end{verbatim}
494where \texttt{r1}, \ldots, \texttt{rn} are $n$ expressions of type
495\crange, is a \emph{Cartesian domain expression}. It represents the
496domain of dimension $n$ which is the Cartesian product of the $n$
497ranges, i.e., it consists of all $n$-tuples $(x_1,\ldots,x_n)$ where
498$x_1\in\texttt{r1}$, \ldots, $x_n\in\texttt{rn}$. The order on the
499domain is the dictionary order on tuples. The type of this expression
500is \cdomainof{\(n\)}.
501
502When a Cartesian domain expression is used to initialize an object of
503domain type, the ``\texttt{(}\cdomain\texttt{)}'' may be omitted.
504For example:
505\begin{verbatim}
506 $domain(3) dom = { 0 .. 3, r2, 10 .. 2 # -2 };
507\end{verbatim}
508
509
510\section{Statements}
511
512\subsection{C Statements}
513
514The usual C statements are supported:
515\begin{itemize}
516\item \emph{no-op} (\ct{;})
517\item expression statements (\ct{e;})
518\item labeled statements, including \ct{case} and \ct{default} labels
519 (\ct{l: s})
520\item \emph{for} (\ct{for (init; cond; inc) s}), \emph{while}
521 (\ct{while (cond) s}) and \emph{do} (\ct{do s while (cond)})
522 loops
523\item compound statements (\lb \ct{s1;s2;} \ldots \rb)
524\item \texttt{if} and \verb!if! \ldots \verb!else!
525\item \verb!goto!
526\item \verb!switch!
527\item \verb!break!
528\item \verb!continue!
529\item \verb!return!
530\end{itemize}
531
532\subsection{Guards and nondeterminism}
533
534\subsubsection{Guarded commands: \cwhen}
535
536A guarded command is encoded in CIVL-C using a $\cwhen$ statement:
537\begin{verbatim}
538 $when (expr) stmt;
539\end{verbatim}
540All statements have a guard, either implicit or explicit. For most
541statements, the guard is \ctrue. The \cwhen{} statement allows one to
542attach an explicit guard to a statement.
543
544When \texttt{expr} is \emph{true}, the statement is enabled, otherwise
545it is disabled. A disabled statement is \emph{blocked}---it will not
546be scheduled for execution. When it is enabled, it may execute by
547moving control to the \texttt{stmt} and executing the first atomic
548action in the \texttt{stmt}.
549
550If \texttt{stmt} itself has a non-trivial guard, the guard of the
551\cwhen{} statement is effectively the conjunction of the \texttt{expr}
552and the guard of \texttt{stmt}.
553
554The evaluation of \texttt{expr} and the first atomic action of
555\texttt{stmt} effectively occur as a single atomic action. There is
556no guarantee that execution of \texttt{stmt} will continue atomically
557if it contains more than one atomic action, i.e., other processes may
558be scheduled.
559
560Examples:
561\begin{verbatim}
562 $when (s>0) s--;
563\end{verbatim}
564This will block until \texttt{s} is positive and then decrement
565\texttt{s}. The execution of \texttt{s--} is guaranteed to take place
566in an environment in which \texttt{s} is positive.
567
568\begin{verbatim}
569 $when (s>0) {s--; t++}
570\end{verbatim}
571The execution of \texttt{s--} must happen when \texttt{s>0}, but
572between \texttt{s--} and \texttt{t++}, other processes may execute.
573
574\begin{verbatim}
575 $when (s>0) $when (t>0) x=y*t;
576\end{verbatim}
577This blocks until both \texttt{x} and \texttt{t} are positive then
578executes the assignment in that state. It is equivalent to
579\begin{verbatim}
580 $when (s>0 && t>0) x=y*t;
581\end{verbatim}
582
583\subsubsection{Nondeterministic selection statement: \cchoose}
584
585A \cchoose{} statement has the form
586\begin{verbatim}
587 $choose {
588 stmt1;
589 stmt2;
590 ...
591 default: stmt
592 }
593\end{verbatim}
594The \texttt{default} clause is optional.
595
596The guards of the statements are evaluated and among those that are
597\emph{true}, one is chosen nondeterministically and executed. If none
598are \emph{true} and the \texttt{default} clause is present, it is
599chosen. The \texttt{default} clause will only be selected if all
600guards are \emph{false}. If no \texttt{default} clause is present and
601all guards are \emph{false}, the statement blocks. Hence the implicit
602guard of the \cchoose{} statement without a \texttt{default} clause is
603the disjunction of the guards of its sub-statements. The implicit
604guard of the \cchoose{} statement with a default clause is
605\emph{true}.
606
607Example: this shows how to encode a ``low-level'' CIVL guarded
608transition system:
609
610\begin{verbatim}
611 l1: $choose {
612 $when (x>0) {x--; goto l2;}
613 $when (x==0) {y=1; goto l3;}
614 default: {z=1; goto l4;}
615 }
616 l2: $choose {
617 ...
618 }
619 l3: $choose {
620 ...
621 }
622\end{verbatim}
623
624
625\subsubsection{Nondeterministic choice of integer:
626 \texorpdfstring{\cchooseint}{\$choose\_int}}
627The header \texttt{civlc.cvh} provides the function \cchooseint{} that returns an integer between 0 (inclusive) and the specified value (exclusive) in a nondeterministic way, as described in Section \ref{subsubsec:chooseint}.
628
629\subsection{Iteration using domains with \cfor}
630\label{sec:cfor}
631
632A \emph{CIVL-for} statement has the form
633\begin{verbatim}
634 $for (int i1, ..., in : dom) S
635\end{verbatim}
636where \texttt{i1}, \ldots, \texttt{in} are $n$ identifiers,
637\texttt{dom} is an expression of type \cdomainof{\(n\)}, and
638\texttt{S} is a statement. The identifiers declare $n$ variables of
639integer type. Control iterates over the values of the domain,
640assigning the integer variables the components of the current tuple in
641the domain at the start of each iteration. The scope of the variables
642extends to the end of \texttt{S}. The iterations takes place in the
643order specified by the domain, e.g., dictionary order for a Caretesian
644domain. Note that if a range expression can be used as \texttt{dom} here, it will be
645automatically converted to one dimensional domain. For example,
646 \begin{verbatim}
647 $for (int i1, ..., in : 0 .. 10) S
648\end{verbatim}
649is equivalent to
650\begin{verbatim}
651 $for (int i1, ..., in : ($domain(1){0 .. 10})) S
652\end{verbatim}
653
654There is a also a parallel version of this construct, \cparfor,
655described in \ref{sec:parfor}.
656
657\section{Functions}
658\subsection{Abstract function: \cabstract}
659
660An abstract function declares a function without a body, and it has the form
661
662\begin{verbatim}
663 $abstract type function(list-of-parameters);
664\end{verbatim}
665
666It is required that the function should have a non-void return type and take at least one parameter.
667The return value of the function is evaluated symbolically using the actual arguments of the function call.
668
669\chapter{Concurrency}
670\label{chap:concurrency}
671
672\section{Process creation and management}
673
674\subsection{The process type: \cproc}
675
676This is a primitive object type and functions like any other primitive
677C type (e.g., \texttt{int}). An object of this type refers to a
678process. It can be thought of as a process ID, but it is not an
679integer and cannot be cast to one. It is analogous to the $\cscope$
680type for dynamic scopes.
681
682Certain expressions take an argument of \cproc{} type and some return
683something of \cproc{} type. The operators \verb!==! and \verb~!=~ may
684be used with two arguments of type \cproc{} to determine whether the
685two arguments refer to the same process.
686
687\subsection{Checking if a process is defined: \cprocdefined}
688
689An object of type \cproc{} is initially undefined, so a use of that
690object would result in an error. One can check whether a \cproc{}
691object is defined using the function \cprocdefined, declared by the header \texttt{civlc.cvh}, as
692described in Section \ref{subsubsec:procdefined}.
693
694\subsection{The \emph{self} process constant: \cself}
695
696This is a constant of type \cproc. It can be used wherever an argument
697of type \cproc{} is called for. It refers to the process that is
698evaluating the expression containing \cself.
699
700\subsection{The \emph{null} process constant: \cprocNull}
701
702This is a constant of type \cproc. It can be used wherever an argument
703of type \cproc{} is called for. It simply means that the object doesnt refer to any process.
704
705\subsection{Spawning a new process: \cspawn}
706
707A \emph{spawn} expression is an expression with side-effects. It
708spawns a new process and returns a reference to the new process, i.e.,
709an object of type \cproc. The syntax is the same as a procedure
710invocation with the keyword \cspawn{} inserted in front:
711\begin{verbatim}
712 $spawn f(expr1, ..., exprn)
713\end{verbatim}
714Typically the returned value is assigned to a variable, e.g.,
715\begin{verbatim}
716 $proc p = $spawn f(i);
717\end{verbatim}
718If the invoked function \texttt{f} returns a value, that value is
719simply ignored.
720
721\subsection{Waiting for process(es) to terminate: \cwait\ and \cwaitall}
722
723Once the system function \cwait(\cwaitall) provided by the CIVL-C standard header \texttt{civlc.cvh}
724gets invoked, it will not return until the specified process(es) terminates(terminate), as described in Sections \ref{subsubsec:wait} and
725\ref{subsubsec:wait}.
726
727\subsection{Terminating a process immediately: \cexit}
728Once the function \cexit\, declared in the header \texttt{civlc.cvh}, is called, the calling process terminates immediately, as described in Section \ref{subsubsec:exit}.
729
730\section{Atomicity}
731
732\subsection{Atom blocks: \catom} This defines a number of statements
733to be executed as a single atomic transition. An \catom~block has the
734following form:
735\begin{verbatim}
736 $atom {
737 stmt1;
738 stmt2;
739 ...
740 }
741\end{verbatim}
742
743The statements inside an \catom\ block are to be executed as one
744transition. It is required that the execution of the statements in an
745\catom\ block satisfy all of the following properties:
746\begin{enumerate}
747\item \emph{deterministic}: at each step in the execution of the atom
748 block, there must be at most one enabled statement;
749\item \emph{nonblocking}: at each step in the execution, there must be
750 at least one enabled statement, hence, together with (1), there must
751 be exactly one enabled statement;
752\item \emph{finite}: the execution of the atom block must terminate
753 after a finite number of steps; and
754\item \emph{isolated}: there are no jumps from outside the atom block
755 to inside the atom block, or from inside the atomc block to outside
756 of it.
757\end{enumerate}
758
759Violations of the \emph{deterministic}, \emph{nonblocking}, or
760\emph{isolated} properties will be reported either statically or
761dynamically. If the \emph{finite} property is violated, the
762verification may just run forever.
763
764Once the process enters an \catom\ block is said to be \emph{executing
765 atomly}. The process remains executing atomly until it reaches the
766terminating right brace of the block. Hence \emph{executing atomly}
767is a dynamic, not static condition. For example, the block might
768contain a function call which takes the process to a point in code
769which is not statically contained in an atom block; that process is
770nevertheless still executing atomly and is subject to the rules above.
771The process only stops executing atomly when that function call
772returns and control finally reaches the right curly brace at the end
773of the atom block (assuming the block is not contained in another atom
774block).
775
776\emph{Note:} \cwait\ or \cwaitall\ calls are not allowed in \catom\ blocks.
777The rationale for this is that there is never a way to know for
778certain that another process has terminated (until \cwait\ or \cwaitall\ has
779returned) so there is never a way to be certain the \cwait\ or \cwaitall call
780will not block. If one does occur in an \catom\ block, an error will
781be reported statically (if it can be detected statically) or
782dynamically (otherwise). Note that it is not always possible to
783detect this statically because the \catom\ block may contain a
784function call, and the function may contain \cwait\ or \cwaitall\ calls.
785
786\subsection{Atomic blocks: \catomic}
787
788The statements in an \emph{atomic} block will be executed without
789other processes interleaving, to the extent possible. It has the
790form:
791\begin{verbatim}
792 $atomic {
793 stmt1;
794 stmt2;
795 ...
796 }
797\end{verbatim}
798It is essentially a weaker form of \catom. Unlike \catom, there are
799no restrictions on the statements that can go inside an \catomic\
800block. A process executing an \catomic~block will try to execute the
801statements without interleaving with other processes, unless it
802becomes blocked. Unlike an \catom, the statements in an atomic block
803do not necessarily execute as a single transition; they may be spread
804out over multiple transitions.
805
806When no statement is enabled, the execution of the \catomic\ block
807will be interrupted. At this point, other processes are allowed to
808execute. Eventually, if the original process becomes enabled due to
809the actions of other processes, it may be scheduled again, in which
810case it regains atomicity and continues where it left off. For
811example, after executing the first loop, the process executing the
812following code will become blocked at the first \cwait\ or \cwaitall\ call:
813 \begin{verbatim}
814$atomic {
815 for (int i = 0; i < 5; i++) p[i] = $spawn foo(i);
816 for (int i = 0; i < 5; i++) $wait p[i];
817}
818\end{verbatim}
819Other processes will then execute. Eventually, if the process being
820waited on terminates, the original process becomes enabled and may be
821scheduled, in which case it regain atomicity, increments \texttt{i}
822and proceeds to the next $\cwait$ or \cwaitall\ call. This is in fact a common
823idiom for spawning and waiting on a set of processes.
824
825A process that enters an $\catomic$ block is said to be
826\emph{executing atomically}; it remains executing atomically until it
827reaches the closing curly brace.
828
829Both $\catom$ and $\catomic$ blocks can be nested arbitrarily, but
830$\catom$ overrides $\catomic$: a process that is executing atomly will
831continue executing atomly if it encounters an $\catomic$ statement;
832but a process executing atomically that encounters an $\catom$ will
833begin executing atomly.
834
835The atomic semantics are defined more precisely as follows: there is a
836single global variable called the \emph{atomic lock}. This variable
837can either be null (meaning the atomic lock is ``free''), or it can
838hold the PID of a process; that process is said to ``hold'' the atomic
839lock. Moreover, each process contains a special integer variable, its
840\emph{atomic counter}, which is initially 0. Every time a process
841enters an atomic block, it increments its atomic counter; every time
842it exits an atomic block, it decrements its counter. In order to
843increment its counter from $0$ to $1$, it must first wait for the
844atomic lock to become free, and then take the lock. When it
845decrements its counter from $1$ to $0$, it releases the atomic lock.
846When a process executing atomically becomes blocked, it releases the
847lock (without changing the value of its atomic counter).
848
849\section{Parallel loops with \cparfor}
850\label{sec:parfor}
851
852A parallel loop statement has the form
853\begin{verbatim}
854 $parfor (int i1, ..., in : dom) S
855\end{verbatim}
856The syntax is exactly the same as that for the sequential loop \cfor
857(Section \ref{sec:cfor}), only with \cparfor{} replacing \cfor.
858
859The semantics are as follows: when control reaches the loop, one
860process is spawned for each element of the domain. That process has
861local variables corresponding to the iteration variables, and those
862local variables are initialized with the components of the tuple for
863the element of the domain that process is assigned. Each process
864executes the statement \texttt{S} in this context. Finally, each of
865these processes is waited on at the end. In particular, there is an
866effective barrier at the end of the loop, and all the spawned
867processes disappear after this point.
868
869\section{Message-Passing}
870
871CIVL-C provides a number of additional primitives that can be used to
872model message-passing systems. This part of the language is built in
873two layers: the lower layer defines an abstract data type for
874representing messages; the higher layer defines an abstract data type
875of \emph{communicators} for managing sets of messages being
876transferred among some set of processes.
877
878\subsection{Messages: \cmessage}
879
880Messages are similar to bundles, but with some additional meta-data.
881The \emph{data} component of the message is the ``contents'' of the
882message and is formed and extracted much like a bundle. The meta-data
883consists of an integer identifier for the \emph{source} place of the
884message, an integer identifier for the message \emph{destination}
885place, and an integer \emph{tag} which can be used by a process to
886discriminate among messages for reception. This is very similar to
887MPI.
888
889The functions for creating, and extracting information from, messages
890are given in Section \ref{subsubsec:messaging}.
891
892\subsection{Communicators: \cgcomm{} and \ccomm}
893\label{sec:communicators}
894
895CIVL-C defines a \emph{global communicator} type $\cgcomm$ and a
896\emph{local communicator} type $\ccomm$. The global communicator is an
897abstraction for a ``communication universe'' that stores buffered
898messages and perhaps other data. The local communicator wraps
899together a reference to a global communicator and an integer
900\emph{place}. Most of the message-passing commands take a local
901communicator as an argument to specify the communication universe used
902for that operation and the place from which that operation will be
903executed. The communication universes are isolated from one
904another---a message sent on one can never be received using a
905different communicator, for example.
906
907The global communicator is the shared object that must be declared in
908a scope containing all scopes in which communication in that universe
909will take place. It is created by specifying the number of
910\emph{places} that will comprise the communicator. A place is an
911address to which messages may be sent or where they may be received.
912There is not necessarily a one-to-one correspondence between places and
913processes: many processes can use the same place.
914
915Local communicators are created (typically in some child scope of the
916scope in which the global communicator is declared) by specifying the
917gobal communicator to which the local one will be associated and the
918place ID. The local communicator will be used in most of the
919message-passing functions; it may be thought of as an ordered pair
920consisting of a reference to the global communicator and the integer
921place ID. The place ID must be in $[0,\texttt{size}-1]$, where
922\texttt{size} is the size of the global communicator. The place ID
923specifies the place in the global communication universe that will be
924occupied by the local communicator. The local communicator handle may
925be used by more than one process, but all of those processes will be
926viewed as occupying the same place. Only one call to \ccommcreate{}
927may occur for each gcomm-place pair.
928
929
930Both types ($\cgcomm$ and $\ccomm$) are handle types. When declared
931with a call to the corresponding creation function, they create an
932object in the specified scope and return a handle to that object. The
933object can only be accessed through the specified system functions
934that take this handle as an argument.
935
936 % This local communicator handle will be used as an
937 % * argument in most message-passing functions. The place must be in
938 % * [0,size-1] and specifies the place in the global communication universe
939 % * that will be occupied by the local communicator. The local communicator
940 % * handle may be used by more than one process, but all of those
941 % * processes will be viewed as occupying the same place.
942 % * Only one call to $comm_create may occur for each gcomm-place pair.
943
944The communicator interface is given in Sections \ref{subsubsec:gcomm} and \ref{subsubsec:comm}.
945
946Certain restrictions are enforced on some relations between the
947objects involved in a communication universe.
948
949Fix a \cgcomm{} object. This object corresponds to a single
950communication universe with, say, $n$ places. At any time, there can
951be \emph{at most one} \ccomm{} object associated to a given place. If
952a program attempts to create a \ccomm{} object with the same \cgcomm{}
953and place as an earlier created \ccomm{} object, a runtime error will
954occur. In particular, there can be at most $n$ \ccomm{} objects
955associated to the \cgcomm.
956
957The relation between processes and \ccomm{} objects is unconstrained.
958One process may use any number of \ccomm{} objects. (Of course, the
959process must have access to handles for those \ccomm{} objects.)
960Dually, a single \ccomm{} object may be used by any number of
961processes; this situation arises naturally when modeling a
962multi-threaded MPI program.
963
964\begin{figure}
965 \begin{small}
966\begin{verbatim}
967$gcomm gcomm = $gcomm_create($here, nprocs);
968void Process(int rank) {
969 $comm comm = $comm_create($here, gcomm, rank);
970
971 void Thread(int tid) {
972 ...$comm_enqueue(comm, msg)...
973 ...$comm_dequeue(comm, source, tag)...
974 }
975
976 for (int i=0; i<nthreads; i++) $spawn Thread(i);
977 ...
978 $comm_destroy(comm);
979}
980for (int i=0; i<nprocs; i++) $spawn Process(i);
981...
982$gcomm_destroy(gcomm);
983\end{verbatim}
984 \end{small}
985 \caption{Code skeleton for model of multithreaded MPI program
986 showing placement of global and local communicator objects}
987 \label{fig:mpi-threads-comm}
988\end{figure}
989
990There is no special status given to the process which creates the
991\ccomm{} object of a given place. Any process which can access a
992handle for that \ccomm{} object can use it to send or receive
993messages, regardless of whether that process was the one that created
994the \ccomm{} object. However, users should be aware that verification
995is likely to be most efficient when variables are declared as locally
996as possible, so it is best to declare the \ccomm{} object in the
997innermost scope possible. Figure \ref{fig:mpi-threads-comm}
998illustrates an effective way to do this in the context of modeling a
999multithreaded MPI program. In the code skeleton, each thread can
1000access the local communicator object of its process, but not that of
1001any other process.
1002
1003\subsection{Barriers: \cgbarrier{} and \cbarrier}
1004\label{sec:barriers}
1005
1006The CIVL-C header \texttt{concurrency.cvh} defines a \emph{global barrier} type $\cgbarrier$ and a
1007\emph{local barrier} type $\cbarrier$. They provide an implementation of
1008a barrier for concurrent programs.
1009
1010The global barrier is a shared object that must be declared in
1011a scope containing all scopes in which the barrier will be called.
1012 It is created by specifying the number of
1013\emph{places} that will comprise the barrier.
1014
1015Local barriers are created (typically in some child scope of the
1016scope in which the global barrier is declared) by specifying the
1017gobal barrier to which the local one will be associated and the
1018place ID. The local barrier will be used in the call to the barrier;
1019it may be thought of as an ordered pair
1020consisting of a reference to the global barrier and the integer
1021place ID. The place ID must be in $[0,\texttt{size}-1]$, where
1022\texttt{size} is the size of the global barrier.
1023Only one call to \cbarriercreate{}
1024may occur for each gbarrier-place pair.
1025
1026
1027Both types ($\cgbarrier$ and $\cbarrier$) are handle types. When declared
1028with a call to the corresponding creation function, they create an
1029object in the specified scope and return a handle to that object. The
1030object can only be accessed through the specified system functions
1031that take this handle as an argument. The barrier interface is presented in Section \ref{subsec:concurrencyLibrary}.
1032
1033\subsection{Collators: \cgcollator{} and \ccollator}
1034\label{sec:collators}
1035Another useful structure is the \emph{collator}: an object that is
1036used to collect information from each process participating in some
1037collective action. A process creates an \emph{entry} each time it
1038passes through the collator, and that entry is enqueued in a FIFO
1039queue for that process. As soon as there is at least one entry in
1040each queue, one entry is dequeued from each queue and a checking
1041function is applied to those dequeued entries. This abstraction has
1042many uses in OpenMP as well as MPI. For example, it is used to check
1043that all MPI processes belonging to a communicator make the same
1044sequence of collective calls, that those calls agree on the ``root''
1045argument, the type and size of data, and so on.
1046
1047
1048\chapter{Specification}
1049
1050\section{Overview}
1051
1052Specification is the means by which one expresses what a program is
1053supposed to do, i.e., what it means for it to be correct.
1054
1055There are several specification mechanisms in CIVL-C. First, there are
1056the default properties: these are generic properties which are checked
1057by default in any program, and require no additional specification
1058effort. These properties include absence of deadlocks, division by 0,
1059illegal pointer dereferences, and out of bounds array indexes.
1060
1061Many more program-specific properties can be specified using
1062assertions. CIVL-C has a rich assertion language which extends the
1063language of boolean-valued C expressions. Assumptions are a
1064specification dual to assertions in that they restrict the set
1065of executions on which the assertions are checked.
1066
1067Functional equivalence is a power specification mechanism. In this
1068approach, two programs are provided, one playing the role of the
1069specification, the other the role of the implementation. The
1070implementation is correct if, for all inputs $x$, it produces the same
1071output as that produced by the specification on input $x$. In other
1072words, the two programs define the same function; this is sometimes
1073known as \emph{input-output equivalence}. In order to take this
1074approach, one must first have a way to specify what the inputs and
1075outputs of a programs are; CIVL-C provides special keywords for this.
1076
1077Procedure contracts are another powerful specification mechanisms.
1078These typically involve specifying preconditions and postconditions
1079for a function. The function is correct if, whenever it is called in a
1080state satisfying the precondition, when it returns the state will
1081satsify the postcondition. A program is correct if all its functions
1082satsify their contract.
1083
1084\section{Input-output signature}
1085
1086\subsection{Input type qualifier: \cinput}
1087
1088The declaration of a variable in the root scope may
1089include the type qualifier \cinput, e.g.,
1090\begin{verbatim}
1091 $input int n;
1092\end{verbatim}
1093This declares the variable to be an input variable, i.e., one which is
1094considered to be an input to the program. Such a variable is
1095initialized with an arbitrary (unconstrained) value of its type. When
1096using symbolic execution to verify a program, such a variable will be
1097assigned a unique symbolic constant of its type.
1098
1099In contrast, variables in the root scope which are not input variables
1100will instead be initialized with the ``undefined'' value. If an
1101undefined value is used in some way (such as in an argument to an
1102operator), an error occurs.
1103
1104In addition, input variables may only be read, never written to.
1105
1106Alternatively, it is also possible to specify a particular concrete
1107initial value for an input variable. This is done using a command
1108line argument when verifying or running the program.
1109
1110An input variable declaration may contain an initializer. The
1111semantics are as follows: if no command line value is specified for
1112the variable, the initializer is used to initialize the variable. If
1113a command line value is specified, the command line value is used and
1114the initializer is ignored.
1115
1116Input (and output) variables also play a key role when determining
1117whether two programs are functionally equivalent. Two programs are
1118considered functionally equivalent if, whenever they are given the
1119same inputs (i.e., corresponding \cinput{} variables are initialized
1120with the same values) they will produce the same outputs (i.e.,
1121corresponding \coutput{} variables will end up with the same values at
1122termination).
1123
1124\subsection{Output type qualifier: \coutput}
1125
1126A variable in the root scope may be declared with this type qualifier
1127to declare it to be an output variable. Output variables are ``dual''
1128to input variables. They may only be written to, never read. They
1129are used primarily in functional equivalence checking.
1130
1131\section{Assertions and assumptions}
1132\label{sec:assertionAndAssumption}
1133\subsection{Assertions: \cassert}
1134
1135The system function \cassert\ (provided by the \texttt{civlc} header) has the signature
1136\begin{verbatim}
1137void $assert(_Bool expr, ...);
1138\end{verbatim}
1139
1140It takes an boolean type expression and a number of optional expressions which are used to construct an error message.
1141Note that CIVL-C boolean expressions have a richer syntax than C
1142expressions, and may include universal or existential quantifiers
1143(see below), and the boolean values \ctrue{} and \cfalse{}.
1144
1145During verification, the assertion is checked. If it cannot be proved
1146that it must hold, a violation is reported. If additional arguments are present, then a specific message is printed as well if the assertion is violated. These
1147additional arguments are similar in form to those used in C's
1148\texttt{printf} statement: a format string, followed by some number of
1149arguments which are evaluated and substituted for successive codes in
1150the format string. For example,
1151\begin{verbatim}
1152 $assert(x<=B, "x-coordinate %f exceeds bound %f", x, B);
1153\end{verbatim}
1154
1155If \texttt{x=3} and \texttt{B=2}, then the above assertion will be violated and CIVL would print the error message ``x-coordinate 3 exceeds bound 2".
1156
1157\subsection{Assume statements: \cassume}
1158
1159The system function \cassume\ (provided by the \texttt{civlc} header) has the signature
1160\begin{verbatim}
1161void $assume(_Bool expr);
1162\end{verbatim}
1163
1164During verification, the given expression is assumed to hold. If
1165this leads to a contradiction on some execution, that execution is
1166simply ignored. It never reports a violation, it only restricts the
1167set of possible executions that will be explored by the verification
1168algorithm.
1169
1170Like an assertion call, an assume call can be used any place
1171a statement is expected. In addition, an assume call can be used
1172in file scope to place restrictions on the global variables of the
1173programs. For example,
1174\begin{verbatim}
1175$input int B;
1176$input int N;
1177$assume(0<=N && N<=B);
1178\end{verbatim}
1179declares \texttt{N} and \texttt{B} to be integer inputs and restricts
1180consideration to inputs satisfying $0\leq\texttt{N}\leq\texttt{B}$.
1181
1182
1183\section{Formulas}
1184
1185A formula is a boolean expression that can be used in an assert
1186statement, assume statement, procedure contract (below), or invariant.
1187Any ordinary C boolean expression is a formula. CIVL-C provides some
1188additional kinds of formulas, described below.
1189
1190\subsection{Implication: \cimplies}
1191
1192The binary operation \cimplies{} represents logical implication.
1193The expression \verb!p=>q! is equivalent to \verb~(!p)||q~.
1194
1195\subsection{Universal quantifier: \cforall}
1196
1197The universally quantified formula has the form
1198\begin{verbatim}
1199 $forall ( variable-declarations (; variable-declarations)* | restriction) expr
1200\end{verbatim}
1201where \verb!variable-declarations! declares a list of variables of a
1202given type with an optional domain, and has the form
1203\begin{verbatim}
1204type identifier (, identifier)* (: domain)?
1205\end{verbatim}
1206where \verb!type! is a type name (e.g., \texttt{int} or
1207\texttt{double}), \verb!identifier! is the name of the bound variable.
1208Moreover, \verb!restriction! is a boolean expression which expresses
1209some restriction on the values that the bound variable can take, and
1210\verb!expr! is a formula. The universally quantified formula holds
1211iff for all values assignable to the bound variable for which the
1212restriction holds, the formula \ct{expr} holds.
1213
1214
1215\subsection{Existential quantifier: \cexists}
1216
1217The syntax for existentially quantified expressions is exactly the
1218same as for universally quantified expressions, with \cexists{} in
1219place of \cforall{}.
1220
1221\section{Contracts}
1222
1223\subsection{Procedure contracts: \crequires{} and \censures{}}
1224The \crequires{} and \censures{} primitives are used to encode
1225procedure contracts. There are optional
1226elements that may occur in a procedure declaration or definition,
1227as follows. For a function prototype:
1228\begin{verbatim}
1229 T f(...)
1230 $requires expr;
1231 $ensures expr;
1232 ;
1233\end{verbatim}
1234For a function definition:
1235\begin{verbatim}
1236 T f(...)
1237 $requires expr;
1238 $ensures expr;
1239 {
1240 ...
1241 }
1242\end{verbatim}
1243The value \cresult{} may be used in post-conditions to refer
1244to the result returned by a procedure.
1245
1246\emph{Status}: parsed, but nothing is currently done with this
1247information.
1248
1249\subsection{Loop invariants: \cinvariant}
1250
1251This indicates a loop invariant. Each C loop
1252construct has an optional invariant clause as follows:
1253\begin{verbatim}
1254 while (expr) $invariant (expr) stmt
1255 for (e1; e2; e3) $invariant (expr) stmt
1256 do stmt while (expr) $invariant (expr) ;
1257\end{verbatim}
1258The invariant encodes the claim that if \texttt{expr} holds upon
1259entering the loop and the loop condition holds, then it will hold
1260after completion of execution of the loop body. The invariant is used
1261by certain verification techniques.
1262
1263\emph{Status:} parsed, but nothing is currently done with this
1264information.
1265
1266\section{Concurrency specification}
1267
1268\subsection{Remote expressions: \texttt{e@x}}.
1269
1270These have the form \verb!expr@x! and refer to a variable in another
1271process, e.g., \verb!procs[i]@x!. This special kind of expression is
1272used in collective expressions, which are used to formulate collective
1273assertions and invariants.
1274
1275The expression \verb!expr! must have \cproc{} type. The variable
1276\texttt{x} must be a statically visible variable in the context in
1277which it is occurs. When this expression is evaluated, the evaluation
1278context will be shifted to the process referred to by \texttt{expr}.
1279
1280\emph{Status}: implementation in progress.
1281
1282\subsection{Collective expressions: \ccollective}. These have the form
1283\begin{verbatim}
1284 $collective(proc_expr, int_expr) expr
1285\end{verbatim}
1286This is a collective expression over a set of processes. The
1287expression \texttt{proc{\U}expr} yields a pointer to the first element
1288of an array of \cproc. The expression \texttt{int{\U}expr} gives the
1289length of that array, i.e., the number of processes. Expression
1290\texttt{expr} is a boolean-valued expression; it may use remote
1291expressions to refer to variables in the processes specified in the
1292array. Example:
1293\begin{verbatim}
1294 $proc procs[N];
1295 ...
1296 $assert $collective(procs, N) i==procs[(pid+1)%N]@i ;
1297\end{verbatim}
1298
1299\emph{Status}: not implemented.
1300
1301\chapter{Pointers and Heaps}
1302\label{chap:pointers}
1303
1304CIVL-C supports pointers, using the same operators with the same
1305meanings as C (\texttt{\&}, \texttt{*}, pointer arithmetic). There is
1306also a heap in every scope, and system functions to allocate and
1307deallocate objects in the specified scope.
1308
1309\section{Memory functions: \texttt{memcpy}}
1310
1311The function \texttt{memcpy} is defined in the standard C library
1312\texttt{string.h} and works exactly the same in CIVL: it copies
1313data from the region pointed to by \ct{q} to that pointed to by
1314\ct{p}. The signature is
1315
1316\begin{verbatim}
1317 void memcpy(void *p, void *q, size_t size);
1318\end{verbatim}
1319
1320\section{Heaps, \cmalloc{} and \cfree}
1321
1322As mentioned above, each dynamic scope has an implicit heap on which
1323objects can be allocated and deallocated dynamically. The CIVL-C header
1324\texttt{civlc.cvh} provides the functions \cmalloc{} and \cfree{} for allocating and dealocating
1325memory, repectively, as described in Section \ref{subsubsec:mallocandfree}.
1326
1327% \section{Pointer types}
1328
1329% Given any object type $T$ and a static scope $s$ in a CIVL-C program,
1330% there is a type \emph{pointer-to-$T$-in-$s$}. The type is used to
1331% represent a pointer to a memory location of type $T$ in scope $s$ or a
1332% descendant of $s$ (i.e., some scope contained in $s$).
1333
1334% If scope $s_1$ is a descendant of $s_2$ (i.e., $s_1$ is lexically
1335% contained in $s_2$), the type \emph{pointer-to-$T$-in-$s_1$} is a
1336% subtype of \emph{pointer-to-$T$-in-$s_2$}. This means that any
1337% expression of the first type can be used wherever an object of the
1338% second type is expected. In particular, any expression $e$ of the
1339% subtype can be assigned to a left-hand-side expression of the
1340% supertype without explicit casts; also $e$ can be used as an argument
1341% to a function for which the corresponding parameter has the supertype.
1342
1343% The syntax for denoting this type adheres to the usual C syntax for
1344% denoting the type \emph{pointer-to-$T$} with the addition of a scope
1345% parameter within angular brackets immediately following the \texttt{*}
1346% token. For example, to declare a variable \texttt{p} of type
1347% \emph{pointer-to-$T$-in-$s$}, one writes
1348% \begin{verbatim}
1349% int *<s> p;
1350% \end{verbatim}
1351% If the scope modifier \texttt{<...>} is absent, the scope is taken to
1352% be the root scope $s_0$. The object has type
1353% \emph{pointer-to-$T$-in-$s_0$}, which is abreviated as
1354% \emph{pointer-to-$T$}. In this way, stanard C programs can be
1355% interpreted as CIVL-C programs.
1356
1357% \section{Address-of operator}
1358
1359% The address-of operator \texttt{\&} returns a pointer of the
1360% appropriate subtype using the innermost scope in which its left-hand-side
1361% argument is declared. For example
1362
1363% \begin{verbatim}
1364% {
1365% $scope s1 = $here();
1366% int x;
1367% double a[N];
1368% int *<s1> p = &x;
1369% double *<s1> q = &a[2];
1370% }
1371% \end{verbatim}
1372% is correct (in particular, it is type-correct) because \texttt{\&x}
1373% has type \emph{pointer-to-\texttt{int}-in-\texttt{s1}}, since
1374% \texttt{s1} is the scope in which \texttt{x} is declared.
1375
1376% Another pointer example:
1377% \begin{small}
1378% \begin{verbatim}
1379% { $scope s0 = $here();
1380% { $scope s1 = $here();
1381% double x;
1382% { $scope s2 = $here();
1383% double y;
1384% double *<s1> p;
1385% /* p can only point to something in s1 or descendant, for example, s2 */
1386% p = &x; // fine
1387% p = &y; // fine
1388% p = (double*)$malloc(s0, 10*sizeof(double)); // static type error
1389% }
1390% }
1391% }
1392% \end{verbatim}
1393% \end{small}
1394
1395% \section{Pointer addition and subtractions}
1396
1397% If \texttt{e} is an expression of type \emph{pointer-to-$T$-in-$s$}
1398% and \texttt{i} is an expression of integer type then \texttt{e+i} also
1399% has type \emph{pointer-to-$T$-in-$s$}. In other words, pointer
1400% addition cannot leave the scope of the original pointer. This
1401% reflects the fact that every object is contained in one scope, and
1402% pointer addition cannot leave the object.
1403
1404% Pointer subtraction is defined on two pointers of the same type, where
1405% ``same'' includes the scope. That is checked statically. As in C, it
1406% is only defined if the two pointers point to the same object. In
1407% CIVL-C, a runtime error will be thrown if they do not point to the
1408% same object.
1409
1410% \section{Semantics of scopes and pointer types}
1411
1412% A variable of type \cscope{} is treated like any other variable.
1413% It becomes part of the state when the scope in which it is declared
1414% is instantiated to form a dynamic scope. The variable is
1415% initialized at that time and its value cannot change.
1416
1417% Each time a dynamic scope is instantiated, it is assigned a unique ID
1418% number. The exactly value of the ID number is not relevant, it just
1419% has to be distince from any other scope ID number that currently
1420% exists in the state. This is the value that is assigned to the scope
1421% variable. Therefore, if a static scope contains a scope variable, and
1422% that scope is instantiated twice to form two distinct dynamic scopes,
1423% the values assigned to the two variables will be distinct.
1424
1425% A pointer value is an ordered pair $\langle \delta,r \rangle$, where
1426% $\delta$ is a dynamic scope ID and $r$ is a reference to a memory
1427% location in the static scope associated to $\delta$. (We will define
1428% the exact form of a reference later.)
1429
1430% When a dynamic scope is instantiated, each new variable created is
1431% assigned a \emph{dynamic type}. This is a refinement of the static
1432% type associated to the static variable. Every dynamic type
1433% is an instance of exactly one static type. The dynamic
1434% type of the newly instantiated variable is an instance of the
1435% static type of the static variable.
1436
1437% The dynamic pointer types have the form
1438% \emph{pointer-to-$t$-in-$\delta$}, where $t$ is a dynamic type and
1439% $\delta$ is a dynamic scope ID. For a program to be dynamically type
1440% safe, such a variable should hold only values of the form $\langle
1441% \delta, r\rangle$. In particular, the variable should never be
1442% assigned a value where the dynamic scope component is a different
1443% instance of the static scope $s$ associated to $\delta$.
1444
1445% \section{Pointer casts}
1446
1447% If scope $s_1$ is contained in scope $s_2$, an expression of type
1448% \emph{pointer-to-$T$-in-$s_1$} can always be cast to
1449% \emph{pointer-to-$T$-in-$s_2$},
1450% because the first is a subtype of the second. (As described above,
1451% the cast is unnecessary.)
1452
1453% The cast in the other direction is also allowed, but the dynamic type
1454% safety of that cast will only be checked at runtime. In particular, a
1455% runtime error will result if the cast attempts to cast the pointer
1456% value to a dynamic scope which does not contain (is an ancestor of)
1457% the dynamic scope component of the pointer value.
1458
1459% A type \emph{pointer-to-$T_1$-in-$s$} can be cast to a type
1460% \emph{pointer-to-$T_2$-in-$s$} according to the usual rules of C. In
1461% other words, usual casting rules apply as long as you don't change the
1462% scope.
1463
1464% \section{Scope-Parameterized Functions}
1465
1466% Coming soon. (Parsed, type checked, not currently used otherwise.)
1467
1468% \section{Scope-Parameterized Type Definitions}
1469
1470% Coming soon. (Ditto.)
1471
1472\chapter{Libraries}
1473
1474\section{Standard CIVL-C headers}
1475CIVL-C headers have the suffix \texttt{.cvh}. Here is the list of standard libraries provided by CIVL:
1476\begin{itemize}
1477\item \texttt{civlc} provides types and functions that are used frequently by CIVL-C programs;
1478\item \texttt{scope} provides utility functions related to dynamic scopes;
1479\item \texttt{pointer} provides utility functions dealing with pointers;
1480\item \texttt{seq} provides utility functions of sequences (realized as incomplete array in CIVL-C);
1481\item \texttt{concurrency} provides concurrency utilities such as the barrier;
1482\item \texttt{bundle} provides bundle types and methods;
1483\item \texttt{comm} provides communicators and methods;
1484\item \texttt{domain} provides operations on domains.
1485%\item \texttt{civlmpi} provides CIVL-C types for MPI.
1486\end{itemize}
1487
1488\subsection{CIVL basics \texttt{civlc.cvh}}
1489\label{subsec:civlcLibrary}
1490The header \texttt{civlc.cvh} declares four types, three macros and several functions. The types declared are \texttt{size\_t}, \texttt{\$proc},
1491\texttt{\$scope} and \texttt{\$operation}. The declared macros are \texttt{\$true}, \texttt{\$false} and \texttt{NULL}. The functions provided in this header
1492will be described in the following.
1493
1494\subsubsection{The \cassert\ and \cassume\ functions}
1495\label{subsubsec:wait}
1496The \cassert\ and \cassume \ functions have the following signatures
1497\begin{verbatim}
1498 void $assert(_Bool expr, ...);
1499 void $assume(_Bool expr);
1500\end{verbatim}
1501
1502Information about them could be found in Section~\ref{sec:assertionAndAssumption}.
1503
1504\subsubsection{The \cwait\ function}
1505\label{subsubsec:wait}
1506The \cwait\ function has signature
1507\begin{verbatim}
1508 void $wait($proc p);
1509\end{verbatim}
1510
1511When invoked, this function will not return until the process
1512referenced by \ct{p} has terminated. Note that $p$ can be any
1513expression of type \cproc{}, not just a variable.
1514
1515\subsubsection{The \cwaitall\ function}
1516\label{subsubsec:waitall}
1517The $\cwaitall$ function has signature
1518\begin{verbatim}
1519 void $waitall($proc *procs, int numProcs);
1520\end{verbatim}
1521When invoked, this function will not return until all the \ct{numProcs} processes
1522referenced by the memory specified by \ct{procs} have terminated.
1523
1524\subsubsection{The \cexit\ function}
1525\label{subsubsec:exit}
1526This function takes no arguments. It causes the
1527calling process to terminate immediately, regardless of the state of
1528its call stack:
1529\begin{verbatim}
1530 void $exit(void);
1531\end{verbatim}
1532
1533\subsubsection{The \cchooseint{} function}
1534\label{subsubsec:chooseint}
1535The function \cchooseint{} has the following signature:
1536\begin{verbatim}
1537 int $choose_int(int n);
1538\end{verbatim}
1539This function takes as input a positive integer \texttt{n} and
1540nondeterministically returns an integer in the range
1541$[0,\texttt{n}-1]$.
1542
1543\subsubsection{The heap-related functions: \cmalloc{} and \cfree}
1544\label{subsubsec:mallocandfree}
1545
1546The memory allocation function $\cmalloc$ is like C's \texttt{malloc}, but takes
1547an extra scope argument:
1548\begin{verbatim}
1549 void * $malloc($scope scope, int size);
1550\end{verbatim}
1551To allocate an object, one first needs a reference to the dynamic scope to be used.
1552
1553The function \cfree{} is used to deallocate a heap object;
1554it is just like C's \texttt{free}:
1555\begin{verbatim}
1556 void $free(void *p);
1557\end{verbatim}
1558An error is generated if the pointer is not one that was returned by
1559\cmalloc, or if it was already freed.
1560
1561\subsubsection{The \texttt{\$elaborate} function}
1562The function \texttt{\$elaborate} has the following signature:
1563\begin{verbatim}
1564 int $elaborate(int n);
1565\end{verbatim}
1566This function takes as input an integer expression \texttt{n} and
1567elaborates it using the current context.
1568
1569\subsubsection{The \texttt{\$scope\_parent} function}
1570The function \texttt{\$scope\_parent} has the following signature
1571\begin{verbatim}
1572 $scope $scope_parent($scope s);
1573\end{verbatim}
1574It returns the parent dynamic scope of the dynamic scope referenced by
1575\ct{s}. If \ct{s} is the root dynamic scope, it returns the undefined
1576value of type $\cscope$.
1577
1578\subsubsection{The \texttt{\$next\_time\_count} function}
1579The function \texttt{\$next\_time\_count} has the following signature
1580\begin{verbatim}
1581 int $next_time_count();
1582\end{verbatim}
1583It returns a unique integer value for representing the current time unit.
1584
1585\subsubsection{The \texttt{\$pathCondition} function}
1586The function \texttt{\$pathCondition} has the following signature
1587\begin{verbatim}
1588 void $pathCondition();
1589\end{verbatim}
1590It prints the context, i.e., the path condition, of the current state.
1591
1592\subsection{Pointer utilities \texttt{pointer.cvh}}
1593\label{subsec:pointerLibrary}
1594The header \texttt{pointer.cvh} declares functions taking pointers as the arguments for various purposes,
1595such as copying, equality checking, membership testing, and so on.
1596% including:
1597%\begin{itemize}
1598%\item function \cequals{} for equality checking;
1599%\item function \ccontains{} for membership testing;
1600%\item function \texttt{\$translate\_ptr} for pointer translation;
1601%\item function \ccopy{} for copying data through pointers.
1602%\end{itemize}
1603
1604\subsubsection{The \texttt{\$apply} function}
1605The function \texttt{\$apply} has the signature
1606\begin{verbatim}
1607 void $apply(void* ptr1, $operation op, void* ptr2, void* res);
1608\end{verbatim}
1609It applies the operation specified by $op$ to the two objects referenced by $ptr1$ and
1610$ptr2$, and assigns the result to the object pointed to by $res$.
1611
1612\subsubsection{The \cequals{} function}
1613
1614The \cequals{} function has the signature
1615\begin{verbatim}
1616 _Bool $equals(void *x, void *y);
1617\end{verbatim}
1618
1619This function takes two non-null pointers as input. If the two objects that the pointers refer to have the same value, then the
1620function returns \ctrue. Otherwise, it returns \cfalse.
1621
1622\subsubsection{The \texttt{\$assert\_equals} function}
1623The \texttt{\$assert\_equals} function has the signature
1624\begin{verbatim}
1625 _Bool $assert_equals(void *ptr1, void *ptr2);
1626\end{verbatim}
1627It asserts that two objects are equal to each other. It can take additional arguments
1628for error message if the assertion is violated.
1629
1630\subsubsection{The \ccontains{} function}
1631The function \ccontains{} has the signature
1632\begin{verbatim}
1633 _Bool $contains(void *ptr1, void *ptr2);
1634\end{verbatim}
1635It takes two non-null pointers as input. If the object that the pointer \texttt{ptr1} points to contains the object pointed to by \texttt{ptr2}, then the
1636function returns \ctrue. Otherwise, it returns \cfalse. For example, given
1637\begin{verbatim}
1638 int a[10];
1639 struct foo {int x; double y} f;
1640 struct foo b[10];
1641
1642 // ... initialize a, f and b
1643\end{verbatim}
1644
1645Here are the results of several invocations of \ccontains:
1646\begin{itemize}
1647\item \texttt{\ccontains(\&a, \&a[3])} returns \ctrue, since the array \texttt{a} contains the cell \texttt{a[3]};
1648\item \texttt{\ccontains(\&a[2], \&a[3])} returns \cfalse;
1649\item \texttt{\ccontains(\&a[2], \&a[2])} returns \ctrue, because the relation is relexive;
1650\item \texttt{\ccontains(\&f, \&f.y)} returns \ctrue, since the struct \texttt{f} contains its field \texttt{f.y};
1651\item \texttt{\ccontains(\&b, \&b[2].x)} returns \ctrue.
1652\end{itemize}
1653
1654\subsubsection{The \texttt{\$translate\_ptr} function}
1655The function \texttt{\$translate\_ptr} has the signature
1656\begin{verbatim}
1657 void * $translate_ptr(void *ptr, void *obj);
1658\end{verbatim}
1659
1660This function translates a pointer into one object (\texttt{ptr}) to a pointer into a different object (\texttt{obj}) with similar structure.
1661
1662For example:
1663\begin{verbatim}
1664 typedef struct node{
1665 int x;
1666 int y;
1667 } node;
1668 typedef struct point{
1669 double a;
1670 double b;
1671 double c;
1672 }point;
1673 node nodes[3];
1674 point points[5];
1675 // ... initialize nodes and points
1676 double *p = $translate_ptr(&(nodes[2].y), &points);
1677 // after the translation, p = &(points[2].b);
1678\end{verbatim}
1679
1680\subsubsection{The \ccopy{} function}
1681
1682The \ccopy{} function has the signature
1683\begin{verbatim}
1684 void $copy(void *ptr, void *value_ptr);
1685\end{verbatim}
1686
1687It copies the value pointed to by \texttt{value\_ptr} to the memory
1688location specified by \texttt{ptr}. This function is different from \texttt{memcpy} only in the way that
1689it can take pointers to (incomplete) array as the argument and copy the whole array to the other.
1690
1691\subsubsection{The \texttt{\$leaf\_node\_ptrs} function}
1692The function \texttt{\$leaf\_node\_ptrs} has the signature
1693\begin{verbatim}
1694 void $leaf_node_ptrs(void* array, void* obj);
1695\end{verbatim}
1696It copies the references to the leaf nodes of the object pointed to by $obj$
1697to the given array. A leaf node of an object has scalar type.
1698The type of $obj$ is pointer-to-$T'$, where all leaf nodes of $T'$ has type $T$,
1699and $array$ has type pointer to array of $T$. For example,
1700$T'$ could be $struct\{int~x;~int~y[10];~int~z;\}$.
1701
1702\subsubsection{The \texttt{\$set\_leaf\_nodes} function}
1703\begin{verbatim}
1704 void $set_leaf_nodes(void* obj, int value);
1705\end{verbatim}
1706
1707\subsubsection{The \texttt{\$set\_default} function}
1708The function \texttt{\$set\_default} has the following signature
1709\begin{verbatim}
1710 void $set_default(void* obj)
1711\end{verbatim}
1712It sets the leaf nodes of a variable to be the default value of zero. This function
1713is equivalent to \texttt{\$set\_leaf\_nodes(obj, 0)}.
1714
1715\subsubsection{The \texttt{\$is\_identity\_ref}}
1716\begin{verbatim}
1717 _Bool $is_identity_ref(void* ptr);
1718\end{verbatim}
1719
1720\subsubsection{The \texttt{\$leaf\_nodes\_equal\_to}}
1721\begin{verbatim}
1722 _Bool $leaf_nodes_equal_to(void* ptr, int value);
1723\end{verbatim}
1724
1725\subsubsection{The \texttt{\$has\_leaf\_node\_equal\_to}}
1726\begin{verbatim}
1727 _Bool $has_leaf_node_equal_to(void* ptr, int value);
1728\end{verbatim}
1729
1730
1731
1732\subsection{Sequence utilities \texttt{seq.cvh}}
1733\label{subsec:seqLibrary}
1734The header \texttt{seq.cvh} provides utility functions dealing with sequences. A sequence is realized as an incomplete array (i.e., an array with no extent specified) of any type \texttt{T}, which applies for all functions of \texttt{seq.cvh}. Functions declared in this header include:
1735\begin{itemize}
1736\item function \cseqinit{} for initializing sequences;
1737\item function \cseqlen{} for computing the length of a sequence;
1738\item function \cseqinsert{} for element insertion to a sequence;
1739\item function \cseqrm{} for element removal of a sequence.
1740\end{itemize}
1741
1742\subsubsection{The \cseqinit{} function}
1743
1744The \cseqinit{} function has the signature
1745\begin{verbatim}
1746 void $seq_init(void *seq, int count, void *value);
1747\end{verbatim}
1748
1749Given a pointer to a sequence of type \texttt{T}, this function sets that sequence to be an array of length \texttt{count} in which every element has the same value, specified by the given pointer \texttt{value}. The parameter \texttt{seq} has the type pointer-to-incomplete-array-of-T, \texttt{count} has any integer type and must be nonnegative, and \texttt{value} has the type pointer-to-T.
1750
1751\subsubsection{The \cseqlen{} function}
1752The \cseqlen{} function has the signature
1753\begin{verbatim}
1754 int $seq_length(void *seq);
1755\end{verbatim}
1756This function returns the length of the sequence pointed to by the pointer \texttt{seq}. The contract is that \texttt{seq} must be a pointer of a sequence of type \texttt{T}, i.e., \texttt{seq} should have the type pointer-to-incomplete-array-of-T.
1757
1758\subsubsection{The \cseqinsert{} function}
1759The \cseqinsert{} function has the signature
1760\begin{verbatim}
1761 void $seq_insert(void *seq, int index, void *values, int count);
1762\end{verbatim}
1763
1764Given a pointer to a sequence of type T, this function inserts \texttt{count} elements into the sequence starting at position
1765 \texttt{index}. The subsequence elements of the original sequence are shifted up, and the final length of the array will be its original length
1766plus \texttt{count}. The values to be inserted are taken from the region specified by \texttt{values}, which has type pointer-to-T.
1767
1768It is required that \texttt{0<=index<=length}, where length is the orginal length of the seqence. If \texttt{index=length}, this function appends the elements to the end of the array. If \texttt{index=0}, this inserts the elements at the beginning of the sequence. If \texttt{count=0}, this function is a no-op and \texttt{values} will never be evaluated (hence may be NULL).
1769
1770\subsubsection{The \cseqrm{} function}
1771The \cseqrm{} function has the signature
1772\begin{verbatim}
1773 void $seq_remove(void *seq, int index, void *values, int count);
1774\end{verbatim}
1775
1776This function removes \texttt{count} elements from the sequence of type T pointed to by \texttt{seq}, starting at position
1777\texttt{index}.
1778
1779If \texttt{values} is not NULL, the removed elements will be copied to the memory region beginning with \texttt{values}, which shoud have the type pointer-to-T. It is required that \texttt{0<=index<length} and \texttt{0<=count<=length-index}. If \texttt{count=0}, this function is a no-op.
1780
1781\subsection{Concurrency utilities \texttt{concurrency.cvh}}
1782\label{subsec:concurrencyLibrary}
1783
1784The header \texttt{concurrency.cvh} declares two types,, \cgbarrier{} and \cbarrier{}, and several functions dealing with barriers, including:
1785\begin{itemize}
1786\item functions \cgbarriercreate{} and \cbarriercreate{} for creating a new global and local barrier, repectively;
1787\item functions \cgbarrierdestroy{} and \cbarrierdestroy{} for destroying a global and local barriere, respectively;
1788\item function \cbarriercall{} for a barrier synchronization request.
1789\end{itemize}
1790
1791
1792\subsubsection{The \cgbarriercreate{} and \cbarriercreate{} functions}
1793The \cgbarriercreate{} function has the signature
1794
1795\begin{verbatim}
1796 $gbarrier $gbarrier_create($scope scope, int size);
1797\end{verbatim}
1798
1799It creates a new global object of the given \texttt{size}, puts it in the heap of the given dynamic \texttt{scope}, and returns a handle to the created \cgbarrier{} object.
1800\\~\\
1801The \cbarriercreate{} function has the signature
1802
1803\begin{verbatim}
1804 $barrier $barrier_create($scope scope, $gbarrier gbarrier, int place);
1805\end{verbatim}
1806
1807It creates a local barrier that joins the specified global barrier \texttt{gbarrier} with the id \texttt{place}. The new local barrier object is stored in the heap of the given dynamic \texttt{scope} , and a handle to that object is returned.
1808
1809\subsubsection{The \cgbarrierdestroy{} and \cbarrierdestroy{} functions}
1810
1811The \cgbarrierdestroy{} and \cbarrierdestroy{} functions has the signatures
1812
1813\begin{verbatim}
1814 void $gbarrier_destroy($gbarrier barrier);
1815 void $barrier_destroy($barrier barrier);
1816\end{verbatim}
1817
1818These functions deallocated the heap memory regions occupied by the specified \cgbarrier{} or \cbarrier{} object. They should be invoked before the corresponding dynamic scope becomes unreachable. Otherwise, a memory leak error will be reported.
1819
1820\subsubsection{The \cbarriercall{} function}
1821
1822The \cbarriercall{} function has the signature
1823
1824\begin{verbatim}
1825 void $barrier_call($barrier barrier);
1826\end{verbatim}
1827
1828When this funciton is called, the calling process will be blocked until all other processes associated with the same global barrier refered to by the given \texttt{barrier} have made the barrier call.
1829
1830\subsection{Bundle type and functions \texttt{bundle.cvh}}
1831\label{subsec:bundleLibrary}
1832
1833The header \texttt{bundle.cvh} defines the type \cbundle{} (see Section \ref{subsec:bundleType}) and several functions dealing with bundles:
1834\begin{itemize}
1835\item function \cbundlesize{} for computing the size of a bundle;
1836\item function \cbundlepack{} for composing a bundle from some given data;
1837\item function \cbundleunpack{} for extracting the data contained by a given bundle;
1838\item function \cbundleunpackapply{} for extacting the data contained by a given bundle and apply some operation to them.
1839\end{itemize}
1840
1841\subsubsection{The \cbundlesize{} function}
1842
1843The \cbundlesize{} function has the signature
1844\begin{verbatim}
1845 int $bundle_size($bundle b);
1846\end{verbatim}
1847
1848It takes a bundle and returns its size.
1849
1850\subsubsection{The \cbundlepack{} function}
1851
1852The \cbundlepack{} function has the signature
1853\begin{verbatim}
1854 $bundle $bundle_pack(void *ptr, int size);
1855\end{verbatim}
1856
1857This function creates a bundle from the memory region specified by \texttt{ptr} and \texttt{size}, copying the data into the new bundle, and returns the new bundle.
1858
1859\subsubsection{The \cbundleunpack{} function}
1860
1861The \cbundleunpack{} function has the signature
1862\begin{verbatim}
1863 void $bundle_unpack($bundle bundle, void *ptr);
1864\end{verbatim}
1865
1866Opposite to \cbundlepack, this function copies the data from the given \texttt{bundle} into the memory region specified by \texttt{ptr}.
1867
1868\subsubsection{The \cbundleunpackapply{} function}
1869
1870The \cbundleunpackapply{} function has the signature
1871\begin{verbatim}
1872 void $bundle_unpack_apply($bundle data, void *buf, int size, $operation op);
1873\end{verbatim}
1874
1875This function unpacks the bundle and applies the specified operation on the content of the bundle. For every binary operarion defined in operation, the content of the bundle will be used as the left operand and buf will be used as the right operand. The result of the operation is stored in buf once is it is done.
1876
1877
1878\subsection{Communicators \texttt{comm.cvh}}
1879\label{subsec:commLibrary}
1880
1881The header \texttt{comm.cvh} declares three types, two macros and a number of functions for communication. The two maros are \texttt{\$COMM\_ANY\_SOURCE} and \texttt{\$COMM\_ANY\_TAG}. The three types declared are \cmessage, \cgcomm{} and \ccomm.
1882
1883\subsubsection{Messaging functions}
1884\label{subsubsec:messaging}
1885
1886The function \cmessagesize{} returns the size of a given message and has the signature
1887
1888\begin{verbatim}
1889 int $message_size($message message);
1890\end{verbatim}
1891
1892\noindent The function \cmessagesource{} returns the source of a given message and has the signature
1893
1894\begin{verbatim}
1895 int $message_source($message message);
1896\end{verbatim}
1897
1898\noindent The function \cmessagedest{} returns the destination of a given message and has the signature
1899
1900\begin{verbatim}
1901 int $message_dest($message message);
1902\end{verbatim}
1903
1904\noindent The function \cmessagetag{} returns the tag of a given message and has the signature
1905
1906\begin{verbatim}
1907 int $message_tag($message message);
1908\end{verbatim}
1909
1910\noindent The function \cmessagepack{} has the signature
1911
1912\begin{verbatim}
1913 $message $message_pack(int source, int dest, int tag, void *data, int size);
1914\end{verbatim}
1915
1916This function creates a new message of the specified \texttt{source}, \texttt{dest} and \texttt{tag}, copying data from the memory region specified \texttt{data} and \texttt{size}, and returns the newly created message object.
1917\\~\\
1918\noindent The function \cmessageunpack{} has the signature
1919
1920\begin{verbatim}
1921 void $message_unpack($message message, void *buf, int size);
1922\end{verbatim}
1923
1924This function transfers data from \texttt{message} the memory region specified by \texttt{buf} and \texttt{size}, reproting an error if the size of the message exceeds the specified \texttt{size}.
1925
1926\subsubsection{\cgcomm{} functions}
1927\label{subsubsec:gcomm}
1928
1929\begin{verbatim}
1930/* Creates a new global communicator object and returns a handle to it.
1931 * The global communicator will have size communication places. The
1932 * global communicator defines a communication "universe" and encompasses
1933 * message buffers and all other components of the state associated to
1934 * message-passing. The new object will be allocated in the given scope. */
1935$gcomm $gcomm_create($scope s, int size);
1936
1937void $gcomm_destroy($gcomm gcomm); // Destroys the gcomm
1938
1939_Bool $gcomm_defined($gcomm gcomm); // Is the gcomm object defined?
1940\end{verbatim}
1941
1942\subsubsection{\ccomm{} functions}
1943\label{subsubsec:comm}
1944
1945\begin{verbatim}
1946
1947/* Creates a new local communicator object and returns a handle to it.
1948 * The new communicator will be affiliated with the specified global
1949 * communicator. The new object will be allocated in the given scope. */
1950$comm $comm_create($scope s, $gcomm gcomm, int place);
1951
1952void $comm_destroy($comm comm); // Destroys the comm
1953
1954_Bool $comm_defined($comm comm); // Is the comm object defined?
1955
1956/* Returns the size (number of places) in the global communicator associated
1957 * to the given comm. */
1958int $comm_size($comm comm);
1959
1960/* Returns the place of the local communicator. This is the same as the
1961 * place argument used to create the local communicator. */
1962int $comm_place($comm comm);
1963
1964/* Adds the message to the appropriate message queue in the communication
1965 * universe specified by the comm. The source of the message must equal
1966 * the place of the comm. */
1967void $comm_enqueue($comm comm, $message message);
1968
1969/* Returns true iff a matching message exists in the communication universe
1970 * specified by the comm. A message matches the arguments if the destination
1971 * of the message is the place of the comm, and the sources and tags match. */
1972_Bool $comm_probe($comm comm, int source, int tag);
1973
1974/* Finds the first matching message and returns it without modifying
1975 * the communication universe. If no matching message exists, returns a message
1976 * with source, dest, and tag all negative. */
1977$message $comm_seek($comm comm, int source, int tag);
1978
1979/* Finds the first matching message, removes it from the communicator,
1980 * and returns the message */
1981$message $comm_dequeue($comm comm, int source, int tag);
1982\end{verbatim}
1983
1984\section{C libraries}
1985
1986Each of the following libraries is at least partially implemented and can
1987be included in a CIVL-C program:
1988\begin{itemize}
1989\item \ct{assert}
1990 \begin{itemize}
1991 \item \verb!void assert(_Bool expr);!\\This is equivalent to an \cassert{} statement without error messages.
1992 \end{itemize}
1993\item \ct{math}
1994 \begin{itemize}
1995 \item \verb!double sqrt(double x);!
1996 \item \verb!double ceil(double x);!
1997 \item \verb!double exp(double x);!
1998 \end{itemize}
1999\item \ct{stdlib}
2000 \begin{itemize}
2001 \item \verb!size_t!
2002 \item \verb!void * malloc(size_t size);!\\
2003 This is equivalent to \verb!$malloc($root, size)!.
2004 \item \verb!void free(void * ptr);!\\
2005 This is identical to \verb!$free(ptr)!.
2006 \end{itemize}
2007\item \ct{stdbool}
2008 \begin{itemize}
2009 \item \verb!true!\\
2010 This is equivalent to \ctrue.
2011 \item \verb!false!\\
2012 This is equivalent to \cfalse.
2013 \end{itemize}
2014\item \ct{stddef}
2015 \begin{itemize}
2016 \item \verb!size_t!
2017 \item \verb!NULL!
2018 \end{itemize}
2019\item \ct{stdio}
2020 \begin{itemize}
2021 \item \verb!int printf(const char * restrict format, ...);!
2022 \end{itemize}
2023\item \ct{string}
2024 \begin{itemize}
2025 \item \verb!size_t!
2026 \item \verb!NULL!
2027 \item \verb!void memcpy(void * restrict dst, const void * restrict src, size_t n);!
2028 \end{itemize}
2029\end{itemize}
Note: See TracBrowser for help on using the repository browser.