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

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

Further work on CIVL manual.

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

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