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

1.23 2.0 main test-branch
Last change on this file since 09d949f was b421139, checked in by Manchun Zheng <zmanchun@…>, 12 years ago

added explanation of more libraries.

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

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