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

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

Making good progress on manual.

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

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