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

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since a9666d4 was 2bce394, checked in by Stephen Siegel <siegel@…>, 12 years ago

Updated manual

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

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