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

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

Adding more scope info to manual

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

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