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

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

Adding some images for the manual, finishing up the manual.

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

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