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

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

Working on manual.

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

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