Changes in / [9ff56e7:3873b5a1]


Ignore:
Files:
5 added
26 edited

Legend:

Unmodified
Added
Removed
  • doc/user/user.tex

    r9ff56e7 r3873b5a1  
    11%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -*- Mode: Latex -*- %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    2 %% 
     2%%
    33%% Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
    44%%
    55%% The contents of this file are covered under the licence agreement in the
    66%% file "LICENCE" distributed with Cforall.
    7 %% 
    8 %% user.tex -- 
    9 %% 
     7%%
     8%% user.tex --
     9%%
    1010%% Author           : Peter A. Buhr
    1111%% Created On       : Wed Apr  6 14:53:29 2016
     
    5757\CFAStyle                                                                                               % use default CFA format-style
    5858
    59 % inline code ©...© (copyright symbol) emacs: C-q M-)
    60 % red highlighting ®...® (registered trademark symbol) emacs: C-q M-.
    61 % blue highlighting ß...ß (sharp s symbol) emacs: C-q M-_
    62 % green highlighting ¢...¢ (cent symbol) emacs: C-q M-"
    63 % LaTex escape §...§ (section symbol) emacs: C-q M-'
    64 % keyword escape ¶...¶ (pilcrow symbol) emacs: C-q M-^
     59% inline code �...� (copyright symbol) emacs: C-q M-)
     60% red highlighting �...� (registered trademark symbol) emacs: C-q M-.
     61% blue highlighting �...� (sharp s symbol) emacs: C-q M-_
     62% green highlighting �...� (cent symbol) emacs: C-q M-"
     63% LaTex escape �...� (section symbol) emacs: C-q M-'
     64% keyword escape �...� (pilcrow symbol) emacs: C-q M-^
    6565% math escape $...$ (dollar symbol)
    6666
     
    158158\multicolumn{1}{c@{\hspace{1.5em}}}{\textbf{\CFA}}      & \multicolumn{1}{c}{\textbf{C}}        & \multicolumn{1}{c}{\textbf{\CC}}      \\
    159159\begin{cfa}
    160 #include <fstream>§\indexc{fstream}§
     160#include <fstream>�\indexc{fstream}�
    161161
    162162int main( void ) {
    163163        int x = 0, y = 1, z = 2;
    164         ®sout | x | y | z | endl;®
     164        �sout | x | y | z | endl;�
    165165}
    166166\end{cfa}
    167167&
    168168\begin{lstlisting}
    169 #include <stdio.h>§\indexc{stdio.h}§
     169#include <stdio.h>�\indexc{stdio.h}�
    170170
    171171int main( void ) {
    172172        int x = 0, y = 1, z = 2;
    173         ®printf( "%d %d %d\n", x, y, z );®
     173        �printf( "%d %d %d\n", x, y, z );�
    174174}
    175175\end{lstlisting}
    176176&
    177177\begin{lstlisting}
    178 #include <iostream>§\indexc{iostream}§
     178#include <iostream>�\indexc{iostream}�
    179179using namespace std;
    180180int main() {
    181181        int x = 0, y = 1, z = 2;
    182         ®cout<<x<<" "<<y<<" "<<z<<endl;®
     182        �cout<<x<<" "<<y<<" "<<z<<endl;�
    183183}
    184184\end{lstlisting}
     
    222222\CC~\cite{C++14,C++} is an example of a similar project;
    223223however, it largely extended the language, and did not address many existing problems.\footnote{%
    224 Two important existing problems addressed were changing the type of character literals from ©int© to ©char© and enumerator from ©int© to the type of its enumerators.}
     224Two important existing problems addressed were changing the type of character literals from �int� to �char� and enumerator from �int� to the type of its enumerators.}
    225225\Index*{Fortran}~\cite{Fortran08}, \Index*{Ada}~\cite{Ada12}, and \Index*{Cobol}~\cite{Cobol14} are examples of programming languages that took an evolutionary approach, where modern language features (\eg objects, concurrency) are added and problems fixed within the framework of the existing language.
    226226\Index*{Java}~\cite{Java8}, \Index*{Go}~\cite{Go}, \Index*{Rust}~\cite{Rust} and \Index*{D}~\cite{D} are examples of the revolutionary approach for modernizing C/\CC, resulting in a new language rather than an extension of the descendent.
     
    242242(See~\cite{Werther96} for similar work in \Index*[C++]{\CC{}}.)
    243243A first \CFA implementation of these extensions was by Esteves~\cite{Esteves04}.
    244 The signature feature of \CFA is parametric-polymorphic functions~\cite{forceone:impl,Cormack90,Duggan96} with functions generalized using a ©forall© clause (giving the language its name):
     244The signature feature of \CFA is parametric-polymorphic functions~\cite{forceone:impl,Cormack90,Duggan96} with functions generalized using a �forall� clause (giving the language its name):
    245245\begin{lstlisting}
    246 ®forall( otype T )® T identity( T val ) { return val; }
    247 int forty_two = identity( 42 );                 §\C{// T is bound to int, forty\_two == 42}§
     246�forall( otype T )� T identity( T val ) { return val; }
     247int forty_two = identity( 42 );                 �\C{// T is bound to int, forty\_two == 42}�
    248248\end{lstlisting}
    249249% extending the C type system with parametric polymorphism and overloading, as opposed to the \Index*[C++]{\CC{}} approach of object-oriented extensions.
     
    264264
    265265\begin{comment}
    266 A simple example is leveraging the existing type-unsafe (©void *©) C ©bsearch© to binary search a sorted floating-point array:
     266A simple example is leveraging the existing type-unsafe (�void *�) C �bsearch� to binary search a sorted floating-point array:
    267267\begin{lstlisting}
    268268void * bsearch( const void * key, const void * base, size_t dim, size_t size,
     
    288288int posn = bsearch( 5.0, vals, 10 );
    289289\end{lstlisting}
    290 The nested function ©comp© provides the hidden interface from typed \CFA to untyped (©void *©) C, plus the cast of the result.
    291 Providing a hidden ©comp© function in \CC is awkward as lambdas do not use C calling-conventions and template declarations cannot appear at block scope.
     290The nested function �comp� provides the hidden interface from typed \CFA to untyped (�void *�) C, plus the cast of the result.
     291Providing a hidden �comp� function in \CC is awkward as lambdas do not use C calling-conventions and template declarations cannot appear at block scope.
    292292As well, an alternate kind of return is made available: position versus pointer to found element.
    293 \CC's type-system cannot disambiguate between the two versions of ©bsearch© because it does not use the return type in overload resolution, nor can \CC separately compile a templated ©bsearch©.
     293\CC's type-system cannot disambiguate between the two versions of �bsearch� because it does not use the return type in overload resolution, nor can \CC separately compile a templated �bsearch�.
    294294
    295295\CFA has replacement libraries condensing hundreds of existing C functions into tens of \CFA overloaded functions, all without rewriting the actual computations.
    296 For example, it is possible to write a type-safe \CFA wrapper ©malloc© based on the C ©malloc©:
     296For example, it is possible to write a type-safe \CFA wrapper �malloc� based on the C �malloc�:
    297297\begin{lstlisting}
    298298forall( dtype T | sized(T) ) T * malloc( void ) { return (T *)malloc( sizeof(T) ); }
    299 int * ip = malloc();                                    §\C{// select type and size from left-hand side}§
     299int * ip = malloc();                                    �\C{// select type and size from left-hand side}�
    300300double * dp = malloc();
    301301struct S {...} * sp = malloc();
     
    305305
    306306However, it is necessary to differentiate between C and \CFA code because of name overloading, as for \CC.
    307 For example, the C math-library provides the following routines for computing the absolute value of the basic types: ©abs©, ©labs©, ©llabs©, ©fabs©, ©fabsf©, ©fabsl©, ©cabsf©, ©cabs©, and ©cabsl©.
    308 Whereas, \CFA wraps each of these routines into ones with the common name ©abs©:
     307For example, the C math-library provides the following routines for computing the absolute value of the basic types: �abs�, �labs�, �llabs�, �fabs�, �fabsf�, �fabsl�, �cabsf�, �cabs�, and �cabsl�.
     308Whereas, \CFA wraps each of these routines into ones with the common name �abs�:
    309309\begin{cfa}
    310310char abs( char );
    311 ®extern "C" {®
    312 int abs( int );                                                 §\C{// use default C routine for int}§
    313 ®}® // extern "C"
     311�extern "C" {�
     312int abs( int );                                                 �\C{// use default C routine for int}�
     313�}� // extern "C"
    314314long int abs( long int );
    315315long long int abs( long long int );
     
    321321long double _Complex abs( long double _Complex );
    322322\end{cfa}
    323 The problem is the name clash between the library routine ©abs© and the \CFA names ©abs©.
    324 Hence, names appearing in an ©extern "C"© block have \newterm*{C linkage}.
     323The problem is the name clash between the library routine �abs� and the \CFA names �abs�.
     324Hence, names appearing in an �extern "C"� block have \newterm*{C linkage}.
    325325Then overloading polymorphism uses a mechanism called \newterm{name mangling}\index{mangling!name} to create unique names that are different from C names, which are not mangled.
    326326Hence, there is the same need as in \CC, to know if a name is a C or \CFA name, so it can be correctly formed.
    327327There is no way around this problem, other than C's approach of creating unique names for each pairing of operation and type.
    328328This example strongly illustrates a core idea in \CFA: \emph{the power of a name}.
    329 The name ``©abs©'' evokes the notion of absolute value, and many mathematical types provide the notion of absolute value.
    330 Hence, knowing the name ©abs© should be sufficient to apply it to any type where it is applicable.
     329The name ``�abs�'' evokes the notion of absolute value, and many mathematical types provide the notion of absolute value.
     330Hence, knowing the name �abs� should be sufficient to apply it to any type where it is applicable.
    331331The time savings and safety of using one name uniformly versus $N$ unique names should not be underestimated.
    332332
     
    334334\section[Compiling CFA Program]{Compiling \CFA Program}
    335335
    336 The command ©cfa© is used to compile \CFA program(s), and is based on the GNU \Indexc{gcc} command, \eg:
    337 \begin{cfa}
    338 cfa§\indexc{cfa}\index{compilation!cfa@©cfa©}§ [ gcc-options ] C/§\CFA§-files [ assembler/loader-files ]
    339 \end{cfa}
    340 \CFA programs having the following ©gcc© flags turned on:
     336The command �cfa� is used to compile \CFA program(s), and is based on the GNU \Indexc{gcc} command, \eg:
     337\begin{cfa}
     338cfa�\indexc{cfa}\index{compilation!cfa@�cfa�}� [ gcc-options ] C/�\CFA�-files [ assembler/loader-files ]
     339\end{cfa}
     340\CFA programs having the following �gcc� flags turned on:
    341341\begin{description}
    342342\item
    343 \Indexc{-std=gnu99}\index{compilation option!-std=gnu99@{©-std=gnu99©}}
     343\Indexc{-std=gnu99}\index{compilation option!-std=gnu99@{�-std=gnu99�}}
    344344The 1999 C standard plus GNU extensions.
    345345\item
    346346{\lstset{deletekeywords={inline}}
    347 \Indexc{-fgnu89-inline}\index{compilation option!-fgnu89-inline@{©-fgnu89-inline©}}
     347\Indexc{-fgnu89-inline}\index{compilation option!-fgnu89-inline@{�-fgnu89-inline�}}
    348348Use the traditional GNU semantics for inline routines in C99 mode, which allows inline routines in header files.
    349349}%
     
    352352\begin{description}
    353353\item
    354 \Indexc{-CFA}\index{compilation option!-CFA@©-CFA©}
     354\Indexc{-CFA}\index{compilation option!-CFA@�-CFA�}
    355355Only the C preprocessor and the \CFA translator steps are performed and the transformed program is written to standard output, which makes it possible to examine the code generated by the \CFA translator.
    356356The generated code started with the standard \CFA prelude.
    357357
    358358\item
    359 \Indexc{-debug}\index{compilation option!-debug@©-debug©}
     359\Indexc{-debug}\index{compilation option!-debug@�-debug�}
    360360The program is linked with the debugging version of the runtime system.
    361361The debug version performs runtime checks to help during the debugging phase of a \CFA program, but substantially slows the execution of the program.
     
    364364
    365365\item
    366 \Indexc{-nodebug}\index{compilation option!-nodebug@©-nodebug©}
     366\Indexc{-nodebug}\index{compilation option!-nodebug@�-nodebug�}
    367367The program is linked with the non-debugging version of the runtime system, so the execution of the program is faster.
    368 \Emph{However, no runtime checks or ©assert©s are performed so errors usually result in abnormal program termination.}
    369 
    370 \item
    371 \Indexc{-help}\index{compilation option!-help@©-help©}
     368\Emph{However, no runtime checks or �assert�s are performed so errors usually result in abnormal program termination.}
     369
     370\item
     371\Indexc{-help}\index{compilation option!-help@�-help�}
    372372Information about the set of \CFA compilation flags is printed.
    373373
    374374\item
    375 \Indexc{-nohelp}\index{compilation option!-nohelp@©-nohelp©}
     375\Indexc{-nohelp}\index{compilation option!-nohelp@�-nohelp�}
    376376Information about the set of \CFA compilation flags is not printed.
    377377\textbf{This option is the default.}
    378378
    379379\item
    380 \Indexc{-quiet}\index{compilation option!-quiet@©-quiet©}
     380\Indexc{-quiet}\index{compilation option!-quiet@�-quiet�}
    381381The \CFA compilation message is not printed at the beginning of a compilation.
    382382
    383383\item
    384 \Indexc{-noquiet}\index{compilation option!-noquiet@©-noquiet©}
     384\Indexc{-noquiet}\index{compilation option!-noquiet@�-noquiet�}
    385385The \CFA compilation message is printed at the beginning of a compilation.
    386386\textbf{This option is the default.}
    387387
    388388\item
    389 \Indexc{-no-include-stdhdr}\index{compilation option!-no-include-stdhdr@©-no-include-stdhdr©}
    390 Do not supply ©extern "C"© wrappers for \Celeven standard include files (see~\VRef{s:StandardHeaders}).
     389\Indexc{-no-include-stdhdr}\index{compilation option!-no-include-stdhdr@�-no-include-stdhdr�}
     390Do not supply �extern "C"� wrappers for \Celeven standard include files (see~\VRef{s:StandardHeaders}).
    391391\textbf{This option is \emph{not} the default.}
    392392\end{description}
     
    395395\begin{description}
    396396\item
    397 \Indexc{__CFA_MAJOR__}\index{preprocessor variables!__CFA__@{©__CFA__©}}
     397\Indexc{__CFA_MAJOR__}\index{preprocessor variables!__CFA__@{�__CFA__�}}
    398398is available during preprocessing and its value is the major \Index{version number} of \CFA.\footnote{
    399399The C preprocessor allows only integer values in a preprocessor variable so a value like ``\Version'' is not allowed.
     
    401401
    402402\item
    403 \Indexc{__CFA_MINOR__}\index{preprocessor variables!__CFA_MINOR__@{©__CFA_MINOR__©}}
     403\Indexc{__CFA_MINOR__}\index{preprocessor variables!__CFA_MINOR__@{�__CFA_MINOR__�}}
    404404is available during preprocessing and its value is the minor \Index{version number} of \CFA.
    405405
    406406\item
    407 \Indexc{__CFA_PATCH__}\index{preprocessor variables!__CFA_PATCH__@©__CFA_PATCH__©}
     407\Indexc{__CFA_PATCH__}\index{preprocessor variables!__CFA_PATCH__@�__CFA_PATCH__�}
    408408is available during preprocessing and its value is the patch \Index{level number} of \CFA.
    409409
    410410\item
    411 \Indexc{__CFA__}\index{preprocessor variables!__CFA__@©__CFA__©},
    412 \Indexc{__CFORALL__}\index{preprocessor variables!__CFORALL__@©__CFORALL__©} and
    413 \Indexc{__cforall}\index{preprocessor variables!__cforall@©__cforall©}
     411\Indexc{__CFA__}\index{preprocessor variables!__CFA__@�__CFA__�},
     412\Indexc{__CFORALL__}\index{preprocessor variables!__CFORALL__@�__CFORALL__�} and
     413\Indexc{__cforall}\index{preprocessor variables!__cforall@�__cforall�}
    414414are always available during preprocessing and have no value.
    415415\end{description}
     
    418418\begin{cfa}
    419419#ifndef __CFORALL__
    420 #include <stdio.h>§\indexc{stdio.h}§    §\C{// C header file}§
     420#include <stdio.h>�\indexc{stdio.h}�    �\C{// C header file}�
    421421#else
    422 #include <fstream>§\indexc{fstream}§    §\C{// \CFA header file}§
     422#include <fstream>�\indexc{fstream}�    �\C{// \CFA header file}�
    423423#endif
    424424\end{cfa}
    425 which conditionally includes the correct header file, if the program is compiled using \Indexc{gcc} or \Indexc{cfa}. 
     425which conditionally includes the correct header file, if the program is compiled using \Indexc{gcc} or \Indexc{cfa}.
    426426
    427427
     
    430430Numeric constants are extended to allow \Index{underscore}s within constants\index{constant!underscore}, \eg:
    431431\begin{cfa}
    432 2®_®147®_®483®_®648;                                    §\C{// decimal constant}§
    433 56®_®ul;                                                                §\C{// decimal unsigned long constant}§
    434 0®_®377;                                                                §\C{// octal constant}§
    435 0x®_®ff®_®ff;                                                   §\C{// hexadecimal constant}§
    436 0x®_®ef3d®_®aa5c;                                               §\C{// hexadecimal constant}§
    437 3.141®_®592®_®654;                                              §\C{// floating point constant}§
    438 10®_®e®_®+1®_®00;                                               §\C{// floating point constant}§
    439 0x®_®ff®_®ff®_®p®_®3;                                   §\C{// hexadecimal floating point}§
    440 0x®_®1.ffff®_®ffff®_®p®_®128®_®l;               §\C{// hexadecimal floating point long constant}§
    441 L®_®§"\texttt{\textbackslash{x}}§®_®§\texttt{ff}§®_®§\texttt{ee}"§;     §\C{// wide character constant}§
     4322�_�147�_�483�_�648;                                    �\C{// decimal constant}�
     43356�_�ul;                                                                �\C{// decimal unsigned long constant}�
     4340�_�377;                                                                �\C{// octal constant}�
     4350x�_�ff�_�ff;                                                   �\C{// hexadecimal constant}�
     4360x�_�ef3d�_�aa5c;                                               �\C{// hexadecimal constant}�
     4373.141�_�592�_�654;                                              �\C{// floating point constant}�
     43810�_�e�_�+1�_�00;                                               �\C{// floating point constant}�
     4390x�_�ff�_�ff�_�p�_�3;                                   �\C{// hexadecimal floating point}�
     4400x�_�1.ffff�_�ffff�_�p�_�128�_�l;               �\C{// hexadecimal floating point long constant}�
     441L�_��"\texttt{\textbackslash{x}}��_��\texttt{ff}��_��\texttt{ee}"�;     �\C{// wide character constant}�
    442442\end{cfa}
    443443The rules for placement of underscores is as follows:
    444444\begin{enumerate}
    445445\item
    446 A sequence of underscores is disallowed, \eg ©12__34© is invalid.
     446A sequence of underscores is disallowed, \eg �12__34� is invalid.
    447447\item
    448448Underscores may only appear within a sequence of digits (regardless of the digit radix).
    449 In other words, an underscore cannot start or end a sequence of digits, \eg ©_1©, ©1_© and ©_1_© are invalid (actually, the 1st and 3rd examples are identifier names).
     449In other words, an underscore cannot start or end a sequence of digits, \eg �_1�, �1_� and �_1_� are invalid (actually, the 1st and 3rd examples are identifier names).
    450450\item
    451451A numeric prefix may end with an underscore;
    452452a numeric infix may begin and/or end with an underscore;
    453453a numeric suffix may begin with an underscore.
    454 For example, the octal ©0© or hexadecimal ©0x© prefix may end with an underscore ©0_377© or ©0x_ff©;
    455 the exponent infix ©E© may start or end with an underscore ©1.0_E10©, ©1.0E_10© or ©1.0_E_10©;
    456 the type suffixes ©U©, ©L©, etc. may start with an underscore ©1_U©, ©1_ll© or ©1.0E10_f©.
     454For example, the octal �0� or hexadecimal �0x� prefix may end with an underscore �0_377� or �0x_ff�;
     455the exponent infix �E� may start or end with an underscore �1.0_E10�, �1.0E_10� or �1.0_E_10�;
     456the type suffixes �U�, �L�, etc. may start with an underscore �1_U�, �1_ll� or �1.0E10_f�.
    457457\end{enumerate}
    458458It is significantly easier to read and enter long constants when they are broken up into smaller groupings (many cultures use comma and/or period among digits for the same purpose).
     
    465465\CFA accommodates keyword clashes with existing C variable-names by syntactic transformations using the \CFA backquote escape-mechanism:
    466466\begin{cfa}
    467 int ®`®otype®`® = 3;                    §\C{// make keyword an identifier}§
    468 double ®`®forall®`® = 3.5;
     467int �`�otype�`� = 3;                    �\C{// make keyword an identifier}�
     468double �`�forall�`� = 3.5;
    469469\end{cfa}
    470470Existing C programs with keyword clashes can be converted by enclosing keyword identifiers in backquotes, and eventually the identifier name can be changed to a non-keyword name.
    471 \VRef[Figure]{f:InterpositionHeaderFile} shows how clashes in C header files (see~\VRef{s:StandardHeaders}) can be handled using preprocessor \newterm{interposition}: ©#include_next© and ©-I filename©:
     471\VRef[Figure]{f:InterpositionHeaderFile} shows how clashes in C header files (see~\VRef{s:StandardHeaders}) can be handled using preprocessor \newterm{interposition}: �#include_next� and �-I filename�:
    472472
    473473\begin{figure}
    474474\begin{cfa}
    475475// include file uses the CFA keyword "otype".
    476 #if ! defined( otype )                  §\C{// nesting ?}§
    477 #define otype ®`®otype®`®               §\C{// make keyword an identifier}§
     476#if ! defined( otype )                  �\C{// nesting ?}�
     477#define otype �`�otype�`�               �\C{// make keyword an identifier}�
    478478#define __CFA_BFD_H__
    479479#endif // ! otype
    480480
    481 #®include_next® <bfd.h>                 §\C{// must have internal check for multiple expansion}§
    482 
    483 #if defined( otype ) && defined( __CFA_BFD_H__ )        §\C{// reset only if set}§
     481#�include_next� <bfd.h>                 �\C{// must have internal check for multiple expansion}�
     482
     483#if defined( otype ) && defined( __CFA_BFD_H__ )        �\C{// reset only if set}�
    484484#undef otype
    485485#undef __CFA_BFD_H__
     
    493493\section{Labelled Continue/Break}
    494494
    495 While C provides ©continue© and ©break© statements for altering control flow, both are restricted to one level of nesting for a particular control structure.
     495While C provides �continue� and �break� statements for altering control flow, both are restricted to one level of nesting for a particular control structure.
    496496Unfortunately, this restriction forces programmers to use \Indexc{goto} to achieve the equivalent control-flow for more than one level of nesting.
    497 To prevent having to switch to the ©goto©, \CFA extends the \Indexc{continue}\index{continue@\lstinline $continue$!labelled}\index{labelled!continue@©continue©} and \Indexc{break}\index{break@\lstinline $break$!labelled}\index{labelled!break@©break©} with a target label to support static multi-level exit\index{multi-level exit}\index{static multi-level exit}~\cite{Buhr85,Java}.
    498 For both ©continue© and ©break©, the target label must be directly associated with a ©for©, ©while© or ©do© statement;
    499 for ©break©, the target label can also be associated with a ©switch©, ©if© or compound (©{}©) statement.
    500 \VRef[Figure]{f:MultiLevelResumeTermination} shows the labelled ©continue© and ©break©, specifying which control structure is the target for exit, and the corresponding C program using only ©goto©.
     497To prevent having to switch to the �goto�, \CFA extends the \Indexc{continue}\index{continue@\lstinline $continue$!labelled}\index{labelled!continue@�continue�} and \Indexc{break}\index{break@\lstinline $break$!labelled}\index{labelled!break@�break�} with a target label to support static multi-level exit\index{multi-level exit}\index{static multi-level exit}~\cite{Buhr85,Java}.
     498For both �continue� and �break�, the target label must be directly associated with a �for�, �while� or �do� statement;
     499for �break�, the target label can also be associated with a �switch�, �if� or compound (�{}�) statement.
     500\VRef[Figure]{f:MultiLevelResumeTermination} shows the labelled �continue� and �break�, specifying which control structure is the target for exit, and the corresponding C program using only �goto�.
    501501The innermost loop has 7 exit points, which cause resumption or termination of one or more of the 7 \Index{nested control-structure}s.
    502502
     
    505505\multicolumn{1}{c@{\hspace{1.5em}}}{\textbf{\CFA}}      & \multicolumn{1}{c}{\textbf{C}}        \\
    506506\begin{cfa}
    507 ®LC:® {
    508         ... §declarations§ ...
    509         ®LS:® switch ( ... ) {
     507�LC:� {
     508        ... �declarations� ...
     509        �LS:� switch ( ... ) {
    510510          case 3:
    511                 ®LIF:® if ( ... ) {
    512                         ®LF:® for ( ... ) {
    513                                 ®LW:® while ( ... ) {
    514                                         ... break ®LC®; ...                     // terminate compound
    515                                         ... break ®LS®; ...                     // terminate switch
    516                                         ... break ®LIF®; ...                    // terminate if
    517                                         ... continue ®LF;® ...   // resume loop
    518                                         ... break ®LF®; ...                     // terminate loop
    519                                         ... continue ®LW®; ...   // resume loop
    520                                         ... break ®LW®; ...               // terminate loop
     511                �LIF:� if ( ... ) {
     512                        �LF:� for ( ... ) {
     513                                �LW:� while ( ... ) {
     514                                        ... break �LC�; ...                     // terminate compound
     515                                        ... break �LS�; ...                     // terminate switch
     516                                        ... break �LIF�; ...                    // terminate if
     517                                        ... continue �LF;� ...   // resume loop
     518                                        ... break �LF�; ...                     // terminate loop
     519                                        ... continue �LW�; ...   // resume loop
     520                                        ... break �LW�; ...               // terminate loop
    521521                                } // while
    522522                        } // for
    523523                } else {
    524                         ... break ®LIF®; ...                                     // terminate if
     524                        ... break �LIF�; ...                                     // terminate if
    525525                } // if
    526526        } // switch
     
    530530\begin{cfa}
    531531{
    532         ... §declarations§ ...
     532        ... �declarations� ...
    533533        switch ( ... ) {
    534534          case 3:
     
    536536                        for ( ... ) {
    537537                                while ( ... ) {
    538                                         ... goto ®LC®; ...
    539                                         ... goto ®LS®; ...
    540                                         ... goto ®LIF®; ...
    541                                         ... goto ®LFC®; ...
    542                                         ... goto ®LFB®; ...
    543                                         ... goto ®LWC®; ...
    544                                         ... goto ®LWB®; ...
    545                                   ®LWC®: ; } ®LWB:® ;
    546                           ®LFC:® ; } ®LFB:® ;
     538                                        ... goto �LC�; ...
     539                                        ... goto �LS�; ...
     540                                        ... goto �LIF�; ...
     541                                        ... goto �LFC�; ...
     542                                        ... goto �LFB�; ...
     543                                        ... goto �LWC�; ...
     544                                        ... goto �LWB�; ...
     545                                  �LWC�: ; } �LWB:� ;
     546                          �LFC:� ; } �LFB:� ;
    547547                } else {
    548                         ... goto ®LIF®; ...
    549                 } ®L3:® ;
    550         } ®LS:® ;
    551 } ®LC:® ;
     548                        ... goto �LIF�; ...
     549                } �L3:� ;
     550        } �LS:� ;
     551} �LC:� ;
    552552\end{cfa}
    553553\end{tabular}
     
    606606
    607607
    608 Both labelled ©continue© and ©break© are a ©goto©\index{goto@\lstinline $goto$!restricted} restricted in the following ways:
     608Both labelled �continue� and �break� are a �goto�\index{goto@\lstinline $goto$!restricted} restricted in the following ways:
    609609\begin{itemize}
    610610\item
     
    615615This restriction prevents missing initialization at the start of a control structure resulting in undefined behaviour.
    616616\end{itemize}
    617 The advantage of the labelled ©continue©/©break© is allowing static multi-level exits without having to use the ©goto© statement, and tying control flow to the target control structure rather than an arbitrary point in a program.
     617The advantage of the labelled �continue�/�break� is allowing static multi-level exits without having to use the �goto� statement, and tying control flow to the target control structure rather than an arbitrary point in a program.
    618618Furthermore, the location of the label at the \emph{beginning} of the target control structure informs the reader that complex control-flow is occurring in the body of the control structure.
    619 With ©goto©, the label is at the end of the control structure, which fails to convey this important clue early enough to the reader.
     619With �goto�, the label is at the end of the control structure, which fails to convey this important clue early enough to the reader.
    620620Finally, using an explicit target for the transfer instead of an implicit target allows new constructs to be added or removed without affecting existing constructs.
    621 The implicit targets of the current ©continue© and ©break©, \ie the closest enclosing loop or ©switch©, change as certain constructs are added or removed.
     621The implicit targets of the current �continue� and �break�, \ie the closest enclosing loop or �switch�, change as certain constructs are added or removed.
    622622
    623623
    624624\section{Switch Statement}
    625625
    626 C allows a number of questionable forms for the ©switch© statement:
     626C allows a number of questionable forms for the �switch� statement:
    627627\begin{enumerate}
    628628\item
    629 By default, the end of a ©case© clause\footnote{
    630 In this section, the term \emph{case clause} refers to either a ©case© or ©default© clause.}
    631 \emph{falls through} to the next ©case© clause in the ©switch© statement;
    632 to exit a ©switch© statement from a ©case© clause requires explicitly terminating the clause with a transfer statement, most commonly ©break©:
     629By default, the end of a �case� clause\footnote{
     630In this section, the term \emph{case clause} refers to either a �case� or �default� clause.}
     631\emph{falls through} to the next �case� clause in the �switch� statement;
     632to exit a �switch� statement from a �case� clause requires explicitly terminating the clause with a transfer statement, most commonly �break�:
    633633\begin{cfa}
    634634switch ( i ) {
     
    661661if ( argc == 3 ) {
    662662        // open output file
    663         ®// open input file
    664 ®} else if ( argc == 2 ) {
    665         ®// open input file
    666 
    667 ®} else {
     663        // open input file
     664} else if ( argc == 2 ) {
     665        // open input file
     666
     667} else {
    668668        // usage message
    669669}
     
    672672\end{quote2}
    673673In this example, case 2 is always done if case 3 is done.
    674 This control flow is difficult to simulate with if statements or a ©switch© statement without fall-through as code must be duplicated or placed in a separate routine.
     674This control flow is difficult to simulate with if statements or a �switch� statement without fall-through as code must be duplicated or placed in a separate routine.
    675675C also uses fall-through to handle multiple case-values resulting in the same action:
    676676\begin{cfa}
     
    685685\end{cfa}
    686686However, this situation is handled in other languages without fall-through by allowing a list of case values.
    687 While fall-through itself is not a problem, the problem occurs when fall-through is the default, as this semantics is unintuitive to many programmers and is different from virtually all other programming languages with a ©switch© statement.
    688 Hence, default fall-through semantics results in a large number of programming errors as programmers often forget the ©break© statement at the end of a ©case© clause, resulting in inadvertent fall-through.
    689 
    690 \item
    691 It is possible to place ©case© clauses on statements nested \emph{within} the body of the ©switch© statement:
     687While fall-through itself is not a problem, the problem occurs when fall-through is the default, as this semantics is unintuitive to many programmers and is different from virtually all other programming languages with a �switch� statement.
     688Hence, default fall-through semantics results in a large number of programming errors as programmers often forget the �break� statement at the end of a �case� clause, resulting in inadvertent fall-through.
     689
     690\item
     691It is possible to place �case� clauses on statements nested \emph{within} the body of the �switch� statement:
    692692\begin{cfa}
    693693switch ( i ) {
     
    695695        if ( j < k ) {
    696696                ...
    697           ®case 1:®             // transfer into "if" statement
     697          �case 1:�             // transfer into "if" statement
    698698                ...
    699699        } // if
     
    701701        while ( j < 5 ) {
    702702                ...
    703           ®case 3:®             // transfer into "while" statement
     703          �case 3:�             // transfer into "while" statement
    704704                ...
    705705        } // while
     
    726726}
    727727\end{cfa}
    728 which unrolls a loop N times (N = 8 above) and uses the ©switch© statement to deal with any iterations not a multiple of N.
     728which unrolls a loop N times (N = 8 above) and uses the �switch� statement to deal with any iterations not a multiple of N.
    729729While efficient, this sort of special purpose usage is questionable:
    730730\begin{quote}
     
    733733\end{quote}
    734734\item
    735 It is possible to place the ©default© clause anywhere in the list of labelled clauses for a ©switch© statement, rather than only at the end.
    736 Virtually all programming languages with a ©switch© statement require the ©default© clause to appear last in the case-clause list.
    737 The logic for this semantics is that after checking all the ©case© clauses without success, the ©default© clause is selected;
    738 hence, physically placing the ©default© clause at the end of the ©case© clause list matches with this semantics.
    739 This physical placement can be compared to the physical placement of an ©else© clause at the end of a series of connected ©if©/©else© statements.
    740 
    741 \item
    742 It is possible to place unreachable code at the start of a ©switch© statement, as in:
     735It is possible to place the �default� clause anywhere in the list of labelled clauses for a �switch� statement, rather than only at the end.
     736Virtually all programming languages with a �switch� statement require the �default� clause to appear last in the case-clause list.
     737The logic for this semantics is that after checking all the �case� clauses without success, the �default� clause is selected;
     738hence, physically placing the �default� clause at the end of the �case� clause list matches with this semantics.
     739This physical placement can be compared to the physical placement of an �else� clause at the end of a series of connected �if�/�else� statements.
     740
     741\item
     742It is possible to place unreachable code at the start of a �switch� statement, as in:
    743743\begin{cfa}
    744744switch ( x ) {
    745         ®int y = 1;®                            §\C{// unreachable initialization}§
    746         ®x = 7;®                                        §\C{// unreachable code without label/branch}§
     745        �int y = 1;�                            �\C{// unreachable initialization}�
     746        �x = 7;�                                        �\C{// unreachable code without label/branch}�
    747747  case 3: ...
    748748        ...
    749         ®int z = 0;®                            §\C{// unreachable initialization, cannot appear after case}§
     749        �int z = 0;�                            �\C{// unreachable initialization, cannot appear after case}�
    750750        z = 2;
    751751  case 3:
    752         ®x = z;®                                        §\C{// without fall through, z is uninitialized}§
    753 }
    754 \end{cfa}
    755 While the declaration of the local variable ©y© is useful with a scope across all ©case© clauses, the initialization for such a variable is defined to never be executed because control always transfers over it.
    756 Furthermore, any statements before the first ©case© clause can only be executed if labelled and transferred to using a ©goto©, either from outside or inside of the ©switch©, both of which are problematic.
    757 As well, the declaration of ©z© cannot occur after the ©case© because a label can only be attached to a statement, and without a fall through to case 3, ©z© is uninitialized.
    758 The key observation is that the ©switch© statement branches into control structure, \ie there are multiple entry points into its statement body.
     752        �x = z;�                                        �\C{// without fall through, z is uninitialized}�
     753}
     754\end{cfa}
     755While the declaration of the local variable �y� is useful with a scope across all �case� clauses, the initialization for such a variable is defined to never be executed because control always transfers over it.
     756Furthermore, any statements before the first �case� clause can only be executed if labelled and transferred to using a �goto�, either from outside or inside of the �switch�, both of which are problematic.
     757As well, the declaration of �z� cannot occur after the �case� because a label can only be attached to a statement, and without a fall through to case 3, �z� is uninitialized.
     758The key observation is that the �switch� statement branches into control structure, \ie there are multiple entry points into its statement body.
    759759\end{enumerate}
    760760
     
    762762\begin{itemize}
    763763\item
    764 the number of ©switch© statements is small,
    765 \item
    766 most ©switch© statements are well formed (\ie no \Index*{Duff's device}),
    767 \item
    768 the ©default© clause is usually written as the last case-clause,
    769 \item
    770 and there is only a medium amount of fall-through from one ©case© clause to the next, and most of these result from a list of case values executing common code, rather than a sequence of case actions that compound.
     764the number of �switch� statements is small,
     765\item
     766most �switch� statements are well formed (\ie no \Index*{Duff's device}),
     767\item
     768the �default� clause is usually written as the last case-clause,
     769\item
     770and there is only a medium amount of fall-through from one �case� clause to the next, and most of these result from a list of case values executing common code, rather than a sequence of case actions that compound.
    771771\end{itemize}
    772 These observations help to put the \CFA changes to the ©switch© into perspective.
     772These observations help to put the \CFA changes to the �switch� into perspective.
    773773\begin{enumerate}
    774774\item
    775775Eliminating default fall-through has the greatest potential for affecting existing code.
    776 However, even if fall-through is removed, most ©switch© statements would continue to work because of the explicit transfers already present at the end of each ©case© clause, the common placement of the ©default© clause at the end of the case list, and the most common use of fall-through, \ie a list of ©case© clauses executing common code, \eg:
     776However, even if fall-through is removed, most �switch� statements would continue to work because of the explicit transfers already present at the end of each �case� clause, the common placement of the �default� clause at the end of the case list, and the most common use of fall-through, \ie a list of �case� clauses executing common code, \eg:
    777777\begin{cfa}
    778778case 1:  case 2:  case 3: ...
     
    780780still works.
    781781Nevertheless, reversing the default action would have a non-trivial effect on case actions that compound, such as the above example of processing shell arguments.
    782 Therefore, to preserve backwards compatibility, it is necessary to introduce a new kind of ©switch© statement, called ©choose©, with no implicit fall-through semantics and an explicit fall-through if the last statement of a case-clause ends with the new keyword ©fallthrough©/©fallthru©, \eg:
    783 \begin{cfa}
    784 ®choose® ( i ) {
     782Therefore, to preserve backwards compatibility, it is necessary to introduce a new kind of �switch� statement, called �choose�, with no implicit fall-through semantics and an explicit fall-through if the last statement of a case-clause ends with the new keyword �fallthrough�/�fallthru�, \eg:
     783\begin{cfa}
     784�choose� ( i ) {
    785785  case 1:  case 2:  case 3:
    786786        ...
    787         ®// implicit end of switch (break)
    788   ®case 5:
     787        // implicit end of switch (break)
     788  case 5:
    789789        ...
    790         ®fallthru®;                                     §\C{// explicit fall through}§
     790        �fallthru�;                                     �\C{// explicit fall through}�
    791791  case 7:
    792792        ...
    793         ®break®                                         §\C{// explicit end of switch}§
     793        �break�                                         �\C{// explicit end of switch}�
    794794  default:
    795795        j = 3;
    796796}
    797797\end{cfa}
    798 Like the ©switch© statement, the ©choose© statement retains the fall-through semantics for a list of ©case© clauses;
    799 the implicit ©break© is applied only at the end of the \emph{statements} following a ©case© clause.
    800 The explicit ©fallthru© is retained because it is a C-idiom most C programmers expect, and its absence might discourage programmers from using the ©choose© statement.
    801 As well, allowing an explicit ©break© from the ©choose© is a carry over from the ©switch© statement, and expected by C programmers.
    802 \item
    803 \Index*{Duff's device} is eliminated from both ©switch© and ©choose© statements, and only invalidates a small amount of very questionable code.
    804 Hence, the ©case© clause must appear at the same nesting level as the ©switch©/©choose© body, as is done in most other programming languages with ©switch© statements.
    805 \item
    806 The issue of ©default© at locations other than at the end of the cause clause can be solved by using good programming style, and there are a few reasonable situations involving fall-through where the ©default© clause needs to appear is locations other than at the end.
     798Like the �switch� statement, the �choose� statement retains the fall-through semantics for a list of �case� clauses;
     799the implicit �break� is applied only at the end of the \emph{statements} following a �case� clause.
     800The explicit �fallthru� is retained because it is a C-idiom most C programmers expect, and its absence might discourage programmers from using the �choose� statement.
     801As well, allowing an explicit �break� from the �choose� is a carry over from the �switch� statement, and expected by C programmers.
     802\item
     803\Index*{Duff's device} is eliminated from both �switch� and �choose� statements, and only invalidates a small amount of very questionable code.
     804Hence, the �case� clause must appear at the same nesting level as the �switch�/�choose� body, as is done in most other programming languages with �switch� statements.
     805\item
     806The issue of �default� at locations other than at the end of the cause clause can be solved by using good programming style, and there are a few reasonable situations involving fall-through where the �default� clause needs to appear is locations other than at the end.
    807807Therefore, no change is made for this issue.
    808808\item
    809 Dealing with unreachable code in a ©switch©/©choose© body is solved by restricting declarations and associated initialization to the start of statement body, which is executed \emph{before} the transfer to the appropriate ©case© clause\footnote{
    810 Essentially, these declarations are hoisted before the ©switch©/©choose© statement and both declarations and statement are surrounded by a compound statement.} and precluding statements before the first ©case© clause.
     809Dealing with unreachable code in a �switch�/�choose� body is solved by restricting declarations and associated initialization to the start of statement body, which is executed \emph{before} the transfer to the appropriate �case� clause\footnote{
     810Essentially, these declarations are hoisted before the �switch�/�choose� statement and both declarations and statement are surrounded by a compound statement.} and precluding statements before the first �case� clause.
    811811Further declarations at the same nesting level as the statement body are disallowed to ensure every transfer into the body is sound.
    812812\begin{cfa}
    813813switch ( x ) {
    814         ®int i = 0;®                            §\C{// allowed only at start}§
     814        �int i = 0;�                            �\C{// allowed only at start}�
    815815  case 0:
    816816        ...
    817         ®int j = 0;®                            §\C{// disallowed}§
     817        �int j = 0;�                            �\C{// disallowed}�
    818818  case 1:
    819819        {
    820                 ®int k = 0;®                    §\C{// allowed at different nesting levels}§
     820                �int k = 0;�                    �\C{// allowed at different nesting levels}�
    821821                ...
    822822        }
     
    829829\section{Case Clause}
    830830
    831 C restricts the ©case© clause of a ©switch© statement to a single value.
    832 For multiple ©case© clauses associated with the same statement, it is necessary to have multiple ©case© clauses rather than multiple values.
    833 Requiring a ©case© clause for each value does not seem to be in the spirit of brevity normally associated with C.
    834 Therefore, the ©case© clause is extended with a list of values, as in:
     831C restricts the �case� clause of a �switch� statement to a single value.
     832For multiple �case� clauses associated with the same statement, it is necessary to have multiple �case� clauses rather than multiple values.
     833Requiring a �case� clause for each value does not seem to be in the spirit of brevity normally associated with C.
     834Therefore, the �case� clause is extended with a list of values, as in:
    835835\begin{quote2}
    836836\begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{}}
     
    838838\begin{cfa}
    839839switch ( i ) {
    840   case ®1, 3, 5®:
     840  case �1, 3, 5�:
    841841        ...
    842   case ®2, 4, 6®:
     842  case �2, 4, 6�:
    843843        ...
    844844}
     
    871871\begin{cfa}
    872872switch ( i ) {
    873   case ®1~5:®
     873  case �1~5:�
    874874        ...
    875   case ®10~15:®
     875  case �10~15:�
    876876        ...
    877877}
     
    880880\begin{cfa}
    881881switch ( i )
    882   case ®1 ... 5®:
     882  case �1 ... 5�:
    883883        ...
    884   case ®10 ... 15®:
     884  case �10 ... 15�:
    885885        ...
    886886}
     
    899899Lists of subranges are also allowed.
    900900\begin{cfa}
    901 case ®1~5, 12~21, 35~42®:
     901case �1~5, 12~21, 35~42�:
    902902\end{cfa}
    903903
     
    943943catch( Arithmetic )
    944944\end{cfa}
    945 matches all three derived exception-types: ©DivideByZero©, ©Overflow©, and ©Underflow©.
     945matches all three derived exception-types: �DivideByZero�, �Overflow�, and �Underflow�.
    946946Because the propagation mechanisms perform a simple linear search of the handler clause for a guarded block, and selects the first matching handler, the order of catch clauses in the handler clause becomes important, \eg:
    947947\begin{cfa}
     
    976976For example, a routine returning a \Index{pointer} to an array of integers is defined and used in the following way:
    977977\begin{cfa}
    978 int ®(*®f®())[®5®]® {...};                              §\C{definition}§
    979  ... ®(*®f®())[®3®]® += 1;                              §\C{usage}§
     978int �(*�f�())[�5�]� {...};                              �\C{definition}�
     979 ... �(*�f�())[�3�]� += 1;                              �\C{usage}�
    980980\end{cfa}
    981981Essentially, the return type is wrapped around the routine name in successive layers (like an \Index{onion}).
     
    990990\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
    991991\begin{cfa}
    992 ß[5] *ß ®int® x1;
    993 ß* [5]ß ®int® x2;
    994 ß[* [5] int]ß f®( int p )®;
     992�[5] *� �int� x1;
     993�* [5]� �int� x2;
     994�[* [5] int]� f�( int p )�;
    995995\end{cfa}
    996996&
    997997\begin{cfa}
    998 ®int® ß*ß x1 ß[5]ß;
    999 ®int® ß(*ßx2ß)[5]ß;
    1000 ßint (*ßf®( int p )®ß)[5]ß;
     998�int� �*� x1 �[5]�;
     999�int� �(*�x2�)[5]�;
     1000�int (*�f�( int p )��)[5]�;
    10011001\end{cfa}
    10021002\end{tabular}
    10031003\end{quote2}
    10041004The only exception is \Index{bit field} specification, which always appear to the right of the base type.
    1005 % Specifically, the character ©*© is used to indicate a pointer, square brackets ©[©\,©]© are used to represent an array or function return value, and parentheses ©()© are used to indicate a routine parameter.
     1005% Specifically, the character �*� is used to indicate a pointer, square brackets �[�\,�]� are used to represent an array or function return value, and parentheses �()� are used to indicate a routine parameter.
    10061006However, unlike C, \CFA type declaration tokens are distributed across all variables in the declaration list.
    1007 For instance, variables ©x© and ©y© of type \Index{pointer} to integer are defined in \CFA as follows:
     1007For instance, variables �x� and �y� of type \Index{pointer} to integer are defined in \CFA as follows:
    10081008\begin{quote2}
    10091009\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
    10101010\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
    10111011\begin{cfa}
    1012 ®*® int x, y;
     1012�*� int x, y;
    10131013\end{cfa}
    10141014&
    10151015\begin{cfa}
    1016 int ®*®x, ®*®y;
     1016int �*�x, �*�y;
    10171017\end{cfa}
    10181018\end{tabular}
     
    10231023\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
    10241024\begin{cfa}
    1025 ®*® int x;
     1025�*� int x;
    10261026int y;
    10271027\end{cfa}
    10281028&
    10291029\begin{cfa}
    1030 int ®*®x, y;
     1030int �*�x, y;
    10311031
    10321032\end{cfa}
     
    10731073\end{quote2}
    10741074
    1075 All type qualifiers, \eg ©const©, ©volatile©, etc., are used in the normal way with the new declarations and also appear left to right, \eg:
     1075All type qualifiers, \eg �const�, �volatile�, etc., are used in the normal way with the new declarations and also appear left to right, \eg:
    10761076\begin{quote2}
    10771077\begin{tabular}{@{}l@{\hspace{1em}}l@{\hspace{1em}}l@{}}
     
    10931093\end{tabular}
    10941094\end{quote2}
    1095 All declaration qualifiers, \eg ©extern©, ©static©, etc., are used in the normal way with the new declarations but can only appear at the start of a \CFA routine declaration,\footnote{\label{StorageClassSpecifier}
     1095All declaration qualifiers, \eg �extern�, �static�, etc., are used in the normal way with the new declarations but can only appear at the start of a \CFA routine declaration,\footnote{\label{StorageClassSpecifier}
    10961096The placement of a storage-class specifier other than at the beginning of the declaration specifiers in a declaration is an obsolescent feature.~\cite[\S~6.11.5(1)]{C11}} \eg:
    10971097\begin{quote2}
     
    11151115\end{quote2}
    11161116
    1117 The new declaration syntax can be used in other contexts where types are required, \eg casts and the pseudo-routine ©sizeof©:
     1117The new declaration syntax can be used in other contexts where types are required, \eg casts and the pseudo-routine �sizeof�:
    11181118\begin{quote2}
    11191119\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
    11201120\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
    11211121\begin{cfa}
    1122 y = (®* int®)x;
    1123 i = sizeof(®[ 5 ] * int®);
     1122y = (�* int�)x;
     1123i = sizeof(�[ 5 ] * int�);
    11241124\end{cfa}
    11251125&
    11261126\begin{cfa}
    1127 y = (®int *®)x;
    1128 i = sizeof(®int * [ 5 ]®);
     1127y = (�int *�)x;
     1128i = sizeof(�int * [ 5 ]�);
    11291129\end{cfa}
    11301130\end{tabular}
     
    11421142These types may be derived from an object or routine type, called the \newterm{referenced type}.
    11431143Objects of these types contain an \newterm{address}, which is normally a location in memory, but may also address memory-mapped registers in hardware devices.
    1144 An integer constant expression with the value 0, or such an expression cast to type ©void *©, is called a \newterm{null-pointer constant}.\footnote{
     1144An integer constant expression with the value 0, or such an expression cast to type �void *�, is called a \newterm{null-pointer constant}.\footnote{
    11451145One way to conceptualize the null pointer is that no variable is placed at this address, so the null-pointer address can be used to denote an uninitialized pointer/reference object;
    11461146\ie the null pointer is guaranteed to compare unequal to a pointer to any object or routine.}
     
    11621162&
    11631163\begin{cfa}
    1164 int * ®const® x = (int *)100
     1164int * �const� x = (int *)100
    11651165*x = 3;                 // implicit dereference
    1166 int * ®const® y = (int *)104;
     1166int * �const� y = (int *)104;
    11671167*y = *x;                // implicit dereference
    11681168\end{cfa}
     
    11711171where the right example is how the compiler logically interprets the variables in the left example.
    11721172Since a variable name only points to one address during its lifetime, it is an \Index{immutable} \Index{pointer};
    1173 hence, the implicit type of pointer variables ©x© and ©y© are constant pointers in the compiler interpretation.
     1173hence, the implicit type of pointer variables �x� and �y� are constant pointers in the compiler interpretation.
    11741174In general, variable addresses are stored in instructions instead of loaded from memory, and hence may not occupy storage.
    11751175These approaches are contrasted in the following:
     
    11941194\end{quote2}
    11951195Finally, the immutable nature of a variable's address and the fact that there is no storage for the variable pointer means pointer assignment\index{pointer!assignment}\index{assignment!pointer} is impossible.
    1196 Therefore, the expression ©x = y© has only one meaning, ©*x = *y©, \ie manipulate values, which is why explicitly writing the dereferences is unnecessary even though it occurs implicitly as part of \Index{instruction decoding}.
     1196Therefore, the expression �x = y� has only one meaning, �*x = *y�, \ie manipulate values, which is why explicitly writing the dereferences is unnecessary even though it occurs implicitly as part of \Index{instruction decoding}.
    11971197
    11981198A \Index{pointer}/\Index{reference} object is a generalization of an object variable-name, \ie a mutable address that can point to more than one memory location during its lifetime.
     
    12021202\begin{tabular}{@{}l@{\hspace{2em}}l@{}}
    12031203\begin{cfa}
    1204 int x, y, ®*® p1, ®*® p2, ®**® p3;
    1205 p1 = ®&®x;               // p1 points to x
     1204int x, y, �*� p1, �*� p2, �**� p3;
     1205p1 = �&�x;               // p1 points to x
    12061206p2 = p1;                 // p2 points to x
    1207 p1 = ®&®y;               // p1 points to y
     1207p1 = �&�y;               // p1 points to y
    12081208p3 = &p2;               // p3 points to p2
    12091209\end{cfa}
     
    12171217For example, \Index*{Algol68}~\cite{Algol68} infers pointer dereferencing to select the best meaning for each pointer usage
    12181218\begin{cfa}
    1219 p2 = p1 + x;                                    §\C{// compiler infers *p2 = *p1 + x;}§
    1220 \end{cfa}
    1221 Algol68 infers the following dereferencing ©*p2 = *p1 + x©, because adding the arbitrary integer value in ©x© to the address of ©p1© and storing the resulting address into ©p2© is an unlikely operation.
    1222 Unfortunately, automatic dereferencing does not work in all cases, and so some mechanism is necessary to fix incorrect choices. 
     1219p2 = p1 + x;                                    �\C{// compiler infers *p2 = *p1 + x;}�
     1220\end{cfa}
     1221Algol68 infers the following dereferencing �*p2 = *p1 + x�, because adding the arbitrary integer value in �x� to the address of �p1� and storing the resulting address into �p2� is an unlikely operation.
     1222Unfortunately, automatic dereferencing does not work in all cases, and so some mechanism is necessary to fix incorrect choices.
    12231223
    12241224Rather than inferring dereference, most programming languages pick one implicit dereferencing semantics, and the programmer explicitly indicates the other to resolve address-duality.
    12251225In C, objects of pointer type always manipulate the pointer object's address:
    12261226\begin{cfa}
    1227 p1 = p2;                                                §\C{// p1 = p2\ \ rather than\ \ *p1 = *p2}§
    1228 p2 = p1 + x;                                    §\C{// p2 = p1 + x\ \ rather than\ \ *p2 = *p1 + x}§
    1229 \end{cfa}
    1230 even though the assignment to ©p2© is likely incorrect, and the programmer probably meant:
    1231 \begin{cfa}
    1232 p1 = p2;                                                §\C{// pointer address assignment}§
    1233 ®*®p2 = ®*®p1 + x;                              §\C{// pointed-to value assignment / operation}§
    1234 \end{cfa}
    1235 The C semantics work well for situations where manipulation of addresses is the primary meaning and data is rarely accessed, such as storage management (©malloc©/©free©).
     1227p1 = p2;                                                �\C{// p1 = p2\ \ rather than\ \ *p1 = *p2}�
     1228p2 = p1 + x;                                    �\C{// p2 = p1 + x\ \ rather than\ \ *p2 = *p1 + x}�
     1229\end{cfa}
     1230even though the assignment to �p2� is likely incorrect, and the programmer probably meant:
     1231\begin{cfa}
     1232p1 = p2;                                                �\C{// pointer address assignment}�
     1233�*�p2 = �*�p1 + x;                              �\C{// pointed-to value assignment / operation}�
     1234\end{cfa}
     1235The C semantics work well for situations where manipulation of addresses is the primary meaning and data is rarely accessed, such as storage management (�malloc�/�free�).
    12361236
    12371237However, in most other situations, the pointed-to value is requested more often than the pointer address.
     
    12451245\end{cfa}
    12461246
    1247 To support this common case, a reference type is introduced in \CFA, denoted by ©&©, which is the opposite dereference semantics to a pointer type, making the value at the pointed-to location the implicit semantics for dereferencing (similar but not the same as \CC \Index{reference type}s).
    1248 \begin{cfa}
    1249 int x, y, ®&® r1, ®&® r2, ®&&® r3;
    1250 ®&®r1 = &x;                                             §\C{// r1 points to x}§
    1251 ®&®r2 = &r1;                                    §\C{// r2 points to x}§
    1252 ®&®r1 = &y;                                             §\C{// r1 points to y}§
    1253 ®&&®r3 = ®&®&r2;                                §\C{// r3 points to r2}§
    1254 r2 = ((r1 + r2) * (r3 - r1)) / (r3 - 15); §\C{// implicit dereferencing}§
     1247To support this common case, a reference type is introduced in \CFA, denoted by �&�, which is the opposite dereference semantics to a pointer type, making the value at the pointed-to location the implicit semantics for dereferencing (similar but not the same as \CC \Index{reference type}s).
     1248\begin{cfa}
     1249int x, y, �&� r1, �&� r2, �&&� r3;
     1250�&�r1 = &x;                                             �\C{// r1 points to x}�
     1251�&�r2 = &r1;                                    �\C{// r2 points to x}�
     1252�&�r1 = &y;                                             �\C{// r1 points to y}�
     1253�&&�r3 = �&�&r2;                                �\C{// r3 points to r2}�
     1254r2 = ((r1 + r2) * (r3 - r1)) / (r3 - 15); �\C{// implicit dereferencing}�
    12551255\end{cfa}
    12561256Except for auto-dereferencing by the compiler, this reference example is the same as the previous pointer example.
     
    12581258One way to conceptualize a reference is via a rewrite rule, where the compiler inserts a dereference operator before the reference variable for each reference qualifier in a declaration, so the previous example becomes:
    12591259\begin{cfa}
    1260 ®*®r2 = ((®*®r1 + ®*®r2) ®*® (®**®r3 - ®*®r1)) / (®**®r3 - 15);
    1261 \end{cfa}
    1262 When a reference operation appears beside a dereference operation, \eg ©&*©, they cancel out.
     1260�*�r2 = ((�*�r1 + �*�r2) �*� (�**�r3 - �*�r1)) / (�**�r3 - 15);
     1261\end{cfa}
     1262When a reference operation appears beside a dereference operation, \eg �&*�, they cancel out.
    12631263However, in C, the cancellation always yields a value (\Index{rvalue}).\footnote{
    1264 The unary ©&© operator yields the address of its operand.
     1264The unary �&� operator yields the address of its operand.
    12651265If the operand has type ``type'', the result has type ``pointer to type''.
    1266 If the operand is the result of a unary ©*© operator, neither that operator nor the ©&© operator is evaluated and the result is as if both were omitted, except that the constraints on the operators still apply and the result is not an lvalue.~\cite[\S~6.5.3.2--3]{C11}}
     1266If the operand is the result of a unary �*� operator, neither that operator nor the �&� operator is evaluated and the result is as if both were omitted, except that the constraints on the operators still apply and the result is not an lvalue.~\cite[\S~6.5.3.2--3]{C11}}
    12671267For a \CFA reference type, the cancellation on the left-hand side of assignment leaves the reference as an address (\Index{lvalue}):
    12681268\begin{cfa}
    1269 (&®*®)r1 = &x;                                  §\C{// (\&*) cancel giving address in r1 not variable pointed-to by r1}§
     1269(&�*�)r1 = &x;                                  �\C{// (\&*) cancel giving address in r1 not variable pointed-to by r1}�
    12701270\end{cfa}
    12711271Similarly, the address of a reference can be obtained for assignment or computation (\Index{rvalue}):
    12721272\begin{cfa}
    1273 (&(&®*®)®*®)r3 = &(&®*®)r2;             §\C{// (\&*) cancel giving address in r2, (\&(\&*)*) cancel giving address in r3}§
     1273(&(&�*�)�*�)r3 = &(&�*�)r2;             �\C{// (\&*) cancel giving address in r2, (\&(\&*)*) cancel giving address in r3}�
    12741274\end{cfa}
    12751275Cancellation\index{cancellation!pointer/reference}\index{pointer!cancellation} works to arbitrary depth.
     
    12791279int x, *p1 = &x, **p2 = &p1, ***p3 = &p2,
    12801280                 &r1 = x,    &&r2 = r1,   &&&r3 = r2;
    1281 ***p3 = 3;                                              §\C{// change x}§
    1282 r3 = 3;                                                 §\C{// change x, ***r3}§
    1283 **p3 = ...;                                             §\C{// change p1}§
    1284 &r3 = ...;                                              §\C{// change r1, (\&*)**r3, 1 cancellation}§
    1285 *p3 = ...;                                              §\C{// change p2}§
    1286 &&r3 = ...;                                             §\C{// change r2, (\&(\&*)*)*r3, 2 cancellations}§
    1287 &&&r3 = p3;                                             §\C{// change r3 to p3, (\&(\&(\&*)*)*)r3, 3 cancellations}§
     1281***p3 = 3;                                              �\C{// change x}�
     1282r3 = 3;                                                 �\C{// change x, ***r3}�
     1283**p3 = ...;                                             �\C{// change p1}�
     1284&r3 = ...;                                              �\C{// change r1, (\&*)**r3, 1 cancellation}�
     1285*p3 = ...;                                              �\C{// change p2}�
     1286&&r3 = ...;                                             �\C{// change r2, (\&(\&*)*)*r3, 2 cancellations}�
     1287&&&r3 = p3;                                             �\C{// change r3 to p3, (\&(\&(\&*)*)*)r3, 3 cancellations}�
    12881288\end{cfa}
    12891289Furthermore, both types are equally performant, as the same amount of dereferencing occurs for both types.
     
    12921292As for a pointer type, a reference type may have qualifiers:
    12931293\begin{cfa}
    1294 const int cx = 5;                                       §\C{// cannot change cx;}§
    1295 const int & cr = cx;                            §\C{// cannot change what cr points to}§
    1296 ®&®cr = &cx;                                            §\C{// can change cr}§
    1297 cr = 7;                                                         §\C{// error, cannot change cx}§
    1298 int & const rc = x;                                     §\C{// must be initialized}§
    1299 ®&®rc = &x;                                                     §\C{// error, cannot change rc}§
    1300 const int & const crc = cx;                     §\C{// must be initialized}§
    1301 crc = 7;                                                        §\C{// error, cannot change cx}§
    1302 ®&®crc = &cx;                                           §\C{// error, cannot change crc}§
    1303 \end{cfa}
    1304 Hence, for type ©& const©, there is no pointer assignment, so ©&rc = &x© is disallowed, and \emph{the address value cannot be the null pointer unless an arbitrary pointer is coerced\index{coercion} into the reference}:
    1305 \begin{cfa}
    1306 int & const cr = *0;                            §\C{// where 0 is the int * zero}§
     1294const int cx = 5;                                       �\C{// cannot change cx;}�
     1295const int & cr = cx;                            �\C{// cannot change what cr points to}�
     1296�&�cr = &cx;                                            �\C{// can change cr}�
     1297cr = 7;                                                         �\C{// error, cannot change cx}�
     1298int & const rc = x;                                     �\C{// must be initialized}�
     1299�&�rc = &x;                                                     �\C{// error, cannot change rc}�
     1300const int & const crc = cx;                     �\C{// must be initialized}�
     1301crc = 7;                                                        �\C{// error, cannot change cx}�
     1302�&�crc = &cx;                                           �\C{// error, cannot change crc}�
     1303\end{cfa}
     1304Hence, for type �& const�, there is no pointer assignment, so �&rc = &x� is disallowed, and \emph{the address value cannot be the null pointer unless an arbitrary pointer is coerced\index{coercion} into the reference}:
     1305\begin{cfa}
     1306int & const cr = *0;                            �\C{// where 0 is the int * zero}�
    13071307\end{cfa}
    13081308Note, constant reference-types do not prevent \Index{addressing errors} because of explicit storage-management:
     
    13111311cr = 5;
    13121312free( &cr );
    1313 cr = 7;                                                         §\C{// unsound pointer dereference}§
    1314 \end{cfa}
    1315 
    1316 The position of the ©const© qualifier \emph{after} the pointer/reference qualifier causes confuse for C programmers.
    1317 The ©const© qualifier cannot be moved before the pointer/reference qualifier for C style-declarations;
     1313cr = 7;                                                         �\C{// unsound pointer dereference}�
     1314\end{cfa}
     1315
     1316The position of the �const� qualifier \emph{after} the pointer/reference qualifier causes confuse for C programmers.
     1317The �const� qualifier cannot be moved before the pointer/reference qualifier for C style-declarations;
    13181318\CFA-style declarations (see \VRef{s:Declarations}) attempt to address this issue:
    13191319\begin{quote2}
     
    13211321\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
    13221322\begin{cfa}
    1323 ®const® * ®const® * const int ccp;
    1324 ®const® & ®const® & const int ccr;
     1323�const� * �const� * const int ccp;
     1324�const� & �const� & const int ccr;
    13251325\end{cfa}
    13261326&
    13271327\begin{cfa}
    1328 const int * ®const® * ®const® ccp;
     1328const int * �const� * �const� ccp;
    13291329
    13301330\end{cfa}
     
    13351335Finally, like pointers, references are usable and composable with other type operators and generators.
    13361336\begin{cfa}
    1337 int w, x, y, z, & ar[3] = { x, y, z }; §\C{// initialize array of references}§
    1338 &ar[1] = &w;                                            §\C{// change reference array element}§
    1339 typeof( ar[1] ) p;                                      §\C{// (gcc) is int, i.e., the type of referenced object}§
    1340 typeof( &ar[1] ) q;                                     §\C{// (gcc) is int \&, i.e., the type of reference}§
    1341 sizeof( ar[1] ) == sizeof( int );       §\C{// is true, i.e., the size of referenced object}§
    1342 sizeof( &ar[1] ) == sizeof( int *)      §\C{// is true, i.e., the size of a reference}§
    1343 \end{cfa}
    1344 
    1345 In contrast to \CFA reference types, \Index*[C++]{\CC{}}'s reference types are all ©const© references, preventing changes to the reference address, so only value assignment is possible, which eliminates half of the \Index{address duality}.
     1337int w, x, y, z, & ar[3] = { x, y, z }; �\C{// initialize array of references}�
     1338&ar[1] = &w;                                            �\C{// change reference array element}�
     1339typeof( ar[1] ) p;                                      �\C{// (gcc) is int, i.e., the type of referenced object}�
     1340typeof( &ar[1] ) q;                                     �\C{// (gcc) is int \&, i.e., the type of reference}�
     1341sizeof( ar[1] ) == sizeof( int );       �\C{// is true, i.e., the size of referenced object}�
     1342sizeof( &ar[1] ) == sizeof( int *)      �\C{// is true, i.e., the size of a reference}�
     1343\end{cfa}
     1344
     1345In contrast to \CFA reference types, \Index*[C++]{\CC{}}'s reference types are all �const� references, preventing changes to the reference address, so only value assignment is possible, which eliminates half of the \Index{address duality}.
    13461346Also, \CC does not allow \Index{array}s\index{array!reference} of reference\footnote{
    13471347The reason for disallowing arrays of reference is unknown, but possibly comes from references being ethereal (like a textual macro), and hence, replaceable by the referant object.}
     
    13571357Therefore, for pointer/reference initialization, the initializing value must be an address not a value.
    13581358\begin{cfa}
    1359 int * p = &x;                                           §\C{// assign address of x}§
    1360 ®int * p = x;®                                          §\C{// assign value of x}§
    1361 int & r = x;                                            §\C{// must have address of x}§
    1362 \end{cfa}
    1363 Like the previous example with C pointer-arithmetic, it is unlikely assigning the value of ©x© into a pointer is meaningful (again, a warning is usually given).
     1359int * p = &x;                                           �\C{// assign address of x}�
     1360�int * p = x;�                                          �\C{// assign value of x}�
     1361int & r = x;                                            �\C{// must have address of x}�
     1362\end{cfa}
     1363Like the previous example with C pointer-arithmetic, it is unlikely assigning the value of �x� into a pointer is meaningful (again, a warning is usually given).
    13641364Therefore, for safety, this context requires an address, so it is superfluous to require explicitly taking the address of the initialization object, even though the type is incorrect.
    13651365Note, this is strictly a convenience and safety feature for a programmer.
    1366 Hence, \CFA allows ©r© to be assigned ©x© because it infers a reference for ©x©, by implicitly inserting a address-of operator, ©&©, and it is an error to put an ©&© because the types no longer match due to the implicit dereference.
    1367 Unfortunately, C allows ©p© to be assigned with ©&x© (address) or ©x© (value), but most compilers warn about the latter assignment as being potentially incorrect.
     1366Hence, \CFA allows �r� to be assigned �x� because it infers a reference for �x�, by implicitly inserting a address-of operator, �&�, and it is an error to put an �&� because the types no longer match due to the implicit dereference.
     1367Unfortunately, C allows �p� to be assigned with �&x� (address) or �x� (value), but most compilers warn about the latter assignment as being potentially incorrect.
    13681368Similarly, when a reference type is used for a parameter/return type, the call-site argument does not require a reference operator for the same reason.
    13691369\begin{cfa}
    1370 int & f( int & r );                                     §\C{// reference parameter and return}§
    1371 z = f( x ) + f( y );                            §\C{// reference operator added, temporaries needed for call results}§
    1372 \end{cfa}
    1373 Within routine ©f©, it is possible to change the argument by changing the corresponding parameter, and parameter ©r© can be locally reassigned within ©f©.
    1374 Since operator routine ©?+?© takes its arguments by value, the references returned from ©f© are used to initialize compiler generated temporaries with value semantics that copy from the references.
     1370int & f( int & r );                                     �\C{// reference parameter and return}�
     1371z = f( x ) + f( y );                            �\C{// reference operator added, temporaries needed for call results}�
     1372\end{cfa}
     1373Within routine �f�, it is possible to change the argument by changing the corresponding parameter, and parameter �r� can be locally reassigned within �f�.
     1374Since operator routine �?+?� takes its arguments by value, the references returned from �f� are used to initialize compiler generated temporaries with value semantics that copy from the references.
    13751375\begin{cfa}
    13761376int temp1 = f( x ), temp2 = f( y );
     
    13801380otherwise references have the same syntactic  burden as pointers in these contexts.
    13811381
    1382 When a pointer/reference parameter has a ©const© value (immutable), it is possible to pass literals and expressions.
    1383 \begin{cfa}
    1384 void f( ®const® int & cr );
    1385 void g( ®const® int * cp );
    1386 f( 3 );                   g( ®&®3 );
    1387 f( x + y );             g( ®&®(x + y) );
    1388 \end{cfa}
    1389 Here, the compiler passes the address to the literal 3 or the temporary for the expression ©x + y©, knowing the argument cannot be changed through the parameter.
    1390 The ©&© before the constant/expression for the pointer-type parameter (©g©) is a \CFA extension necessary to type match and is a common requirement before a variable in C (\eg ©scanf©).
    1391 Importantly, ©&3© may not be equal to ©&3©, where the references occur across calls because the temporaries maybe different on each call.
     1382When a pointer/reference parameter has a �const� value (immutable), it is possible to pass literals and expressions.
     1383\begin{cfa}
     1384void f( �const� int & cr );
     1385void g( �const� int * cp );
     1386f( 3 );                   g( �&�3 );
     1387f( x + y );             g( �&�(x + y) );
     1388\end{cfa}
     1389Here, the compiler passes the address to the literal 3 or the temporary for the expression �x + y�, knowing the argument cannot be changed through the parameter.
     1390The �&� before the constant/expression for the pointer-type parameter (�g�) is a \CFA extension necessary to type match and is a common requirement before a variable in C (\eg �scanf�).
     1391Importantly, �&3� may not be equal to �&3�, where the references occur across calls because the temporaries maybe different on each call.
    13921392
    13931393\CFA \emph{extends} this semantics to a mutable pointer/reference parameter, and the compiler implicitly creates the necessary temporary (copying the argument), which is subsequently pointed-to by the reference parameter and can be changed.\footnote{
    1394 If whole program analysis is possible, and shows the parameter is not assigned, \ie it is ©const©, the temporary is unnecessary.}
     1394If whole program analysis is possible, and shows the parameter is not assigned, \ie it is �const�, the temporary is unnecessary.}
    13951395\begin{cfa}
    13961396void f( int & r );
    13971397void g( int * p );
    1398 f( 3 );                   g( ®&®3 );            §\C{// compiler implicit generates temporaries}§
    1399 f( x + y );             g( ®&®(x + y) );        §\C{// compiler implicit generates temporaries}§
     1398f( 3 );                   g( �&�3 );            �\C{// compiler implicit generates temporaries}�
     1399f( x + y );             g( �&�(x + y) );        �\C{// compiler implicit generates temporaries}�
    14001400\end{cfa}
    14011401Essentially, there is an implicit \Index{rvalue} to \Index{lvalue} conversion in this case.\footnote{
    1402 This conversion attempts to address the \newterm{const hell} problem, when the innocent addition of a ©const© qualifier causes a cascade of type failures, requiring an unknown number of additional ©const© qualifiers, until it is discovered a ©const© qualifier cannot be added and all the ©const© qualifiers must be removed.}
     1402This conversion attempts to address the \newterm{const hell} problem, when the innocent addition of a �const� qualifier causes a cascade of type failures, requiring an unknown number of additional �const� qualifiers, until it is discovered a �const� qualifier cannot be added and all the �const� qualifiers must be removed.}
    14031403The implicit conversion allows seamless calls to any routine without having to explicitly name/copy the literal/expression to allow the call.
    14041404
     
    14081408\begin{cfa}
    14091409void f( int i );
    1410 void (*fp)( int );                                      §\C{// routine pointer}§
    1411 fp = f;                                                         §\C{// reference initialization}§
    1412 fp = &f;                                                        §\C{// pointer initialization}§
    1413 fp = *f;                                                        §\C{// reference initialization}§
    1414 fp(3);                                                          §\C{// reference invocation}§
    1415 (*fp)(3);                                                       §\C{// pointer invocation}§
    1416 \end{cfa}
    1417 While C's treatment of routine objects has similarity to inferring a reference type in initialization contexts, the examples are assignment not initialization, and all possible forms of assignment are possible (©f©, ©&f©, ©*f©) without regard for type.
    1418 Instead, a routine object should be referenced by a ©const© reference:
    1419 \begin{cfa}
    1420 ®const® void (®&® fr)( int ) = f;       §\C{// routine reference}§
    1421 fr = ...                                                        §\C{// error, cannot change code}§
    1422 &fr = ...;                                                      §\C{// changing routine reference}§
    1423 fr( 3 );                                                        §\C{// reference call to f}§
    1424 (*fr)(3);                                                       §\C{// error, incorrect type}§
     1410void (*fp)( int );                                      �\C{// routine pointer}�
     1411fp = f;                                                         �\C{// reference initialization}�
     1412fp = &f;                                                        �\C{// pointer initialization}�
     1413fp = *f;                                                        �\C{// reference initialization}�
     1414fp(3);                                                          �\C{// reference invocation}�
     1415(*fp)(3);                                                       �\C{// pointer invocation}�
     1416\end{cfa}
     1417While C's treatment of routine objects has similarity to inferring a reference type in initialization contexts, the examples are assignment not initialization, and all possible forms of assignment are possible (�f�, �&f�, �*f�) without regard for type.
     1418Instead, a routine object should be referenced by a �const� reference:
     1419\begin{cfa}
     1420�const� void (�&� fr)( int ) = f;       �\C{// routine reference}�
     1421fr = ...                                                        �\C{// error, cannot change code}�
     1422&fr = ...;                                                      �\C{// changing routine reference}�
     1423fr( 3 );                                                        �\C{// reference call to f}�
     1424(*fr)(3);                                                       �\C{// error, incorrect type}�
    14251425\end{cfa}
    14261426because the value of the routine object is a routine literal, \ie the routine code is normally immutable during execution.\footnote{
     
    14311431\subsection{Address-of Semantics}
    14321432
    1433 In C, ©&E© is an rvalue for any expression ©E©.
    1434 \CFA extends the ©&© (address-of) operator as follows:
     1433In C, �&E� is an rvalue for any expression �E�.
     1434\CFA extends the �&� (address-of) operator as follows:
    14351435\begin{itemize}
    14361436\item
    1437 if ©R© is an \Index{rvalue} of type ©T &$_1$...&$_r$© where $r \ge 1$ references (©&© symbols) than ©&R© has type ©T ®*®&$_{\color{red}2}$...&$_{\color{red}r}$©, \ie ©T© pointer with $r-1$ references (©&© symbols).
    1438 
    1439 \item
    1440 if ©L© is an \Index{lvalue} of type ©T &$_1$...&$_l$© where $l \ge 0$ references (©&© symbols) then ©&L© has type ©T ®*®&$_{\color{red}1}$...&$_{\color{red}l}$©, \ie ©T© pointer with $l$ references (©&© symbols).
     1437if �R� is an \Index{rvalue} of type �T &$_1$...&$_r$� where $r \ge 1$ references (�&� symbols) than �&R� has type �T �*�&$_{\color{red}2}$...&$_{\color{red}r}$�, \ie �T� pointer with $r-1$ references (�&� symbols).
     1438
     1439\item
     1440if �L� is an \Index{lvalue} of type �T &$_1$...&$_l$� where $l \ge 0$ references (�&� symbols) then �&L� has type �T �*�&$_{\color{red}1}$...&$_{\color{red}l}$�, \ie �T� pointer with $l$ references (�&� symbols).
    14411441\end{itemize}
    14421442The following example shows the first rule applied to different \Index{rvalue} contexts:
     
    14671467\setcounter{enumi}{-1}
    14681468\item
    1469 lvalue to rvalue conversion: ©cv T© converts to ©T©, which allows implicit variable dereferencing.
     1469lvalue to rvalue conversion: �cv T� converts to �T�, which allows implicit variable dereferencing.
    14701470\begin{cfa}
    14711471int x;
    14721472x + 1;                  // lvalue variable (int) converts to rvalue for expression
    14731473\end{cfa}
    1474 An rvalue has no type qualifiers (©cv©), so the lvalue qualifiers are dropped.
     1474An rvalue has no type qualifiers (�cv�), so the lvalue qualifiers are dropped.
    14751475\end{enumerate}
    14761476\CFA provides three new implicit conversion for reference types to simplify reference usage.
    14771477\begin{enumerate}
    14781478\item
    1479 reference to rvalue conversion: ©cv T &© converts to ©T©, which allows implicit reference dereferencing.
     1479reference to rvalue conversion: �cv T &� converts to �T�, which allows implicit reference dereferencing.
    14801480\begin{cfa}
    14811481int x, &r = x, f( int p );
    1482 x = ®r® + f( ®r® );  // lvalue reference converts to rvalue
    1483 \end{cfa}
    1484 An rvalue has no type qualifiers (©cv©), so the reference qualifiers are dropped.
    1485 
    1486 \item
    1487 lvalue to reference conversion: \lstinline[deletekeywords={lvalue}]@lvalue-type cv1 T@ converts to ©cv2 T &©, which allows implicitly converting variables to references.
    1488 \begin{cfa}
    1489 int x, &r = ®x®, f( int & p ); // lvalue variable (int) convert to reference (int &)
    1490 f( ®x® );               // lvalue variable (int) convert to reference (int &)
    1491 \end{cfa}
    1492 Conversion can restrict a type, where ©cv1© $\le$ ©cv2©, \eg passing an ©int© to a ©const volatile int &©, which has low cost.
    1493 Conversion can expand a type, where ©cv1© $>$ ©cv2©, \eg passing a ©const volatile int© to an ©int &©, which has high cost (\Index{warning});
    1494 furthermore, if ©cv1© has ©const© but not ©cv2©, a temporary variable is created to preserve the immutable lvalue.
    1495 
    1496 \item
    1497 rvalue to reference conversion: ©T© converts to ©cv T &©, which allows binding references to temporaries.
     1482x = �r� + f( �r� );  // lvalue reference converts to rvalue
     1483\end{cfa}
     1484An rvalue has no type qualifiers (�cv�), so the reference qualifiers are dropped.
     1485
     1486\item
     1487lvalue to reference conversion: \lstinline[deletekeywords={lvalue}]@lvalue-type cv1 T@ converts to �cv2 T &�, which allows implicitly converting variables to references.
     1488\begin{cfa}
     1489int x, &r = �x�, f( int & p ); // lvalue variable (int) convert to reference (int &)
     1490f( �x� );               // lvalue variable (int) convert to reference (int &)
     1491\end{cfa}
     1492Conversion can restrict a type, where �cv1� $\le$ �cv2�, \eg passing an �int� to a �const volatile int &�, which has low cost.
     1493Conversion can expand a type, where �cv1� $>$ �cv2�, \eg passing a �const volatile int� to an �int &�, which has high cost (\Index{warning});
     1494furthermore, if �cv1� has �const� but not �cv2�, a temporary variable is created to preserve the immutable lvalue.
     1495
     1496\item
     1497rvalue to reference conversion: �T� converts to �cv T &�, which allows binding references to temporaries.
    14981498\begin{cfa}
    14991499int x, & f( int & p );
    1500 f( ®x + 3® );   // rvalue parameter (int) implicitly converts to lvalue temporary reference (int &)
    1501 ®&f®(...) = &x; // rvalue result (int &) implicitly converts to lvalue temporary reference (int &)
     1500f( �x + 3� );   // rvalue parameter (int) implicitly converts to lvalue temporary reference (int &)
     1501�&f�(...) = &x; // rvalue result (int &) implicitly converts to lvalue temporary reference (int &)
    15021502\end{cfa}
    15031503In both case, modifications to the temporary are inaccessible (\Index{warning}).
    1504 Conversion expands the temporary-type with ©cv©, which is low cost since the temporary is inaccessible.
     1504Conversion expands the temporary-type with �cv�, which is low cost since the temporary is inaccessible.
    15051505\end{enumerate}
    15061506
     
    16711671The point of the new syntax is to allow returning multiple values from a routine~\cite{Galletly96,CLU}, \eg:
    16721672\begin{cfa}
    1673 ®[ int o1, int o2, char o3 ]® f( int i1, char i2, char i3 ) {
    1674         §\emph{routine body}§
    1675 }
    1676 \end{cfa}
    1677 where routine ©f© has three output (return values) and three input parameters.
     1673�[ int o1, int o2, char o3 ]� f( int i1, char i2, char i3 ) {
     1674        �\emph{routine body}�
     1675}
     1676\end{cfa}
     1677where routine �f� has three output (return values) and three input parameters.
    16781678Existing C syntax cannot be extended with multiple return types because it is impossible to embed a single routine name within multiple return type specifications.
    16791679
    1680 In detail, the brackets, ©[]©, enclose the result type, where each return value is named and that name is a local variable of the particular return type.\footnote{
     1680In detail, the brackets, �[]�, enclose the result type, where each return value is named and that name is a local variable of the particular return type.\footnote{
    16811681\Index*{Michael Tiemann}, with help from \Index*{Doug Lea}, provided named return values in g++, circa 1989.}
    16821682The value of each local return variable is automatically returned at routine termination.
    16831683Declaration qualifiers can only appear at the start of a routine definition, \eg:
    16841684\begin{cfa}
    1685 ®extern® [ int x ] g( int y ) {§\,§}
     1685�extern� [ int x ] g( int y ) {�\,�}
    16861686\end{cfa}
    16871687Lastly, if there are no output parameters or input parameters, the brackets and/or parentheses must still be specified;
    16881688in both cases the type is assumed to be void as opposed to old style C defaults of int return type and unknown parameter types, respectively, as in:
    16891689\begin{cfa}
    1690 [§\,§] g();                                                     §\C{// no input or output parameters}§
    1691 [ void ] g( void );                                     §\C{// no input or output parameters}§
     1690[�\,�] g();                                                     �\C{// no input or output parameters}�
     1691[ void ] g( void );                                     �\C{// no input or output parameters}�
    16921692\end{cfa}
    16931693
     
    17021702int (*f(x))[ 5 ] int x; {}
    17031703\end{cfa}
    1704 The string ``©int (*f(x))[ 5 ]©'' declares a K\&R style routine of type returning a pointer to an array of 5 integers, while the string ``©[ 5 ] int x©'' declares a \CFA style parameter x of type array of 5 integers.
    1705 Since the strings overlap starting with the open bracket, ©[©, there is an ambiguous interpretation for the string.
     1704The string ``�int (*f(x))[ 5 ]�'' declares a K\&R style routine of type returning a pointer to an array of 5 integers, while the string ``�[ 5 ] int x�'' declares a \CFA style parameter x of type array of 5 integers.
     1705Since the strings overlap starting with the open bracket, �[�, there is an ambiguous interpretation for the string.
    17061706As well, \CFA-style declarations cannot be used to declare parameters for C-style routine-definitions because of the following ambiguity:
    17071707\begin{cfa}
    17081708typedef int foo;
    1709 int f( int (* foo) );                           §\C{// foo is redefined as a parameter name}§
    1710 \end{cfa}
    1711 The string ``©int (* foo)©'' declares a C-style named-parameter of type pointer to an integer (the parenthesis are superfluous), while the same string declares a \CFA style unnamed parameter of type routine returning integer with unnamed parameter of type pointer to foo.
    1712 The redefinition of a type name in a parameter list is the only context in C where the character ©*© can appear to the left of a type name, and \CFA relies on all type qualifier characters appearing to the right of the type name.
     1709int f( int (* foo) );                           �\C{// foo is redefined as a parameter name}�
     1710\end{cfa}
     1711The string ``�int (* foo)�'' declares a C-style named-parameter of type pointer to an integer (the parenthesis are superfluous), while the same string declares a \CFA style unnamed parameter of type routine returning integer with unnamed parameter of type pointer to foo.
     1712The redefinition of a type name in a parameter list is the only context in C where the character �*� can appear to the left of a type name, and \CFA relies on all type qualifier characters appearing to the right of the type name.
    17131713The inability to use \CFA declarations in these two contexts is probably a blessing because it precludes programmers from arbitrarily switching between declarations forms within a declaration contexts.
    17141714
    17151715C-style declarations can be used to declare parameters for \CFA style routine definitions, \eg:
    17161716\begin{cfa}
    1717 [ int ] f( * int, int * );                      §\C{// returns an integer, accepts 2 pointers to integers}§
    1718 [ * int, int * ] f( int );                      §\C{// returns 2 pointers to integers, accepts an integer}§
     1717[ int ] f( * int, int * );                      �\C{// returns an integer, accepts 2 pointers to integers}�
     1718[ * int, int * ] f( int );                      �\C{// returns 2 pointers to integers, accepts an integer}�
    17191719\end{cfa}
    17201720The reason for allowing both declaration styles in the new context is for backwards compatibility with existing preprocessor macros that generate C-style declaration-syntax, as in:
    17211721\begin{cfa}
    17221722#define ptoa( n, d ) int (*n)[ d ]
    1723 int f( ptoa( p, 5 ) ) ...                       §\C{// expands to int f( int (*p)[ 5 ] )}§
    1724 [ int ] f( ptoa( p, 5 ) ) ...           §\C{// expands to [ int ] f( int (*p)[ 5 ] )}§
     1723int f( ptoa( p, 5 ) ) ...                       �\C{// expands to int f( int (*p)[ 5 ] )}�
     1724[ int ] f( ptoa( p, 5 ) ) ...           �\C{// expands to [ int ] f( int (*p)[ 5 ] )}�
    17251725\end{cfa}
    17261726Again, programmers are highly encouraged to use one declaration form or the other, rather than mixing the forms.
     
    17291729\subsection{Named Return Values}
    17301730
    1731 \Index{Named return values} handle the case where it is necessary to define a local variable whose value is then returned in a ©return© statement, as in:
     1731\Index{Named return values} handle the case where it is necessary to define a local variable whose value is then returned in a �return� statement, as in:
    17321732\begin{cfa}
    17331733int f() {
     
    17371737}
    17381738\end{cfa}
    1739 Because the value in the return variable is automatically returned when a \CFA routine terminates, the ©return© statement \emph{does not} contain an expression, as in:
     1739Because the value in the return variable is automatically returned when a \CFA routine terminates, the �return� statement \emph{does not} contain an expression, as in:
    17401740\newline
    17411741\begin{minipage}{\linewidth}
    17421742\begin{cfa}
    1743 ®[ int x, int y ]® f() {
     1743�[ int x, int y ]� f() {
    17441744        int z;
    17451745        ... x = 0; ... y = z; ...
    1746         ®return;®                                                       §\C{// implicitly return x, y}§
     1746        �return;�                                                       �\C{// implicitly return x, y}�
    17471747}
    17481748\end{cfa}
    17491749\end{minipage}
    17501750\newline
    1751 When the return is encountered, the current values of ©x© and ©y© are returned to the calling routine.
    1752 As well, ``falling off the end'' of a routine without a ©return© statement is permitted, as in:
     1751When the return is encountered, the current values of �x� and �y� are returned to the calling routine.
     1752As well, ``falling off the end'' of a routine without a �return� statement is permitted, as in:
    17531753\begin{cfa}
    17541754[ int x, int y ] f() {
    17551755        ...
    1756 }                                                                               §\C{// implicitly return x, y}§
    1757 \end{cfa}
    1758 In this case, the current values of ©x© and ©y© are returned to the calling routine just as if a ©return© had been encountered.
     1756}                                                                               �\C{// implicitly return x, y}�
     1757\end{cfa}
     1758In this case, the current values of �x� and �y� are returned to the calling routine just as if a �return� had been encountered.
    17591759
    17601760Named return values may be used in conjunction with named parameter values;
     
    17631763[ int x, int y ] f( int, x, int y ) {
    17641764        ...
    1765 }                                                                               §\C{// implicitly return x, y}§
     1765}                                                                               �\C{// implicitly return x, y}�
    17661766\end{cfa}
    17671767This notation allows the compiler to eliminate temporary variables in nested routine calls.
    17681768\begin{cfa}
    1769 [ int x, int y ] f( int, x, int y );    §\C{// prototype declaration}§
     1769[ int x, int y ] f( int, x, int y );    �\C{// prototype declaration}�
    17701770int a, b;
    17711771[a, b] = f( f( f( a, b ) ) );
    17721772\end{cfa}
    17731773While the compiler normally ignores parameters names in prototype declarations, here they are used to eliminate temporary return-values by inferring that the results of each call are the inputs of the next call, and ultimately, the left-hand side of the assignment.
    1774 Hence, even without the body of routine ©f© (separate compilation), it is possible to perform a global optimization across routine calls.
     1774Hence, even without the body of routine �f� (separate compilation), it is possible to perform a global optimization across routine calls.
    17751775The compiler warns about naming inconsistencies between routine prototype and definition in this case, and behaviour is \Index{undefined} if the programmer is inconsistent.
    17761776
     
    17811781as well, parameter names are optional, \eg:
    17821782\begin{cfa}
    1783 [ int x ] f ();                                                 §\C{// returning int with no parameters}§
    1784 [ * int ] g (int y);                                    §\C{// returning pointer to int with int parameter}§
    1785 [ ] h ( int, char );                                    §\C{// returning no result with int and char parameters}§
    1786 [ * int, int ] j ( int );                               §\C{// returning pointer to int and int, with int parameter}§
     1783[ int x ] f ();                                                 �\C{// returning int with no parameters}�
     1784[ * int ] g (int y);                                    �\C{// returning pointer to int with int parameter}�
     1785[ ] h ( int, char );                                    �\C{// returning no result with int and char parameters}�
     1786[ * int, int ] j ( int );                               �\C{// returning pointer to int and int, with int parameter}�
    17871787\end{cfa}
    17881788This syntax allows a prototype declaration to be created by cutting and pasting source text from the routine definition header (or vice versa).
     
    18111811The syntax for pointers to \CFA routines specifies the pointer name on the right, \eg:
    18121812\begin{cfa}
    1813 * [ int x ] () fp;                                              §\C{// pointer to routine returning int with no parameters}§
    1814 * [ * int ] (int y) gp;                                 §\C{// pointer to routine returning pointer to int with int parameter}§
    1815 * [ ] (int,char) hp;                                    §\C{// pointer to routine returning no result with int and char parameters}§
    1816 * [ * int,int ] ( int ) jp;                             §\C{// pointer to routine returning pointer to int and int, with int parameter}§
     1813* [ int x ] () fp;                                              �\C{// pointer to routine returning int with no parameters}�
     1814* [ * int ] (int y) gp;                                 �\C{// pointer to routine returning pointer to int with int parameter}�
     1815* [ ] (int,char) hp;                                    �\C{// pointer to routine returning no result with int and char parameters}�
     1816* [ * int,int ] ( int ) jp;                             �\C{// pointer to routine returning pointer to int and int, with int parameter}�
    18171817\end{cfa}
    18181818While parameter names are optional, \emph{a routine name cannot be specified};
    18191819for example, the following is incorrect:
    18201820\begin{cfa}
    1821 * [ int x ] f () fp;                                    §\C{// routine name "f" is not allowed}§
     1821* [ int x ] f () fp;                                    �\C{// routine name "f" is not allowed}�
    18221822\end{cfa}
    18231823
     
    18421842whereas a named (keyword) call may be:
    18431843\begin{cfa}
    1844 p( z : 3, x : 4, y : 7 );       §\C{// rewrite $\Rightarrow$ p( 4, 7, 3 )}§
     1844p( z : 3, x : 4, y : 7 );       �\C{// rewrite $\Rightarrow$ p( 4, 7, 3 )}�
    18451845\end{cfa}
    18461846Here the order of the arguments is unimportant, and the names of the parameters are used to associate argument values with the corresponding parameters.
     
    18591859For example, the following routine prototypes and definition are all valid.
    18601860\begin{cfa}
    1861 void p( int, int, int );                        §\C{// equivalent prototypes}§
     1861void p( int, int, int );                        �\C{// equivalent prototypes}�
    18621862void p( int x, int y, int z );
    18631863void p( int y, int x, int z );
    18641864void p( int z, int y, int x );
    1865 void p( int q, int r, int s ) {}        §\C{// match with this definition}§
     1865void p( int q, int r, int s ) {}        �\C{// match with this definition}�
    18661866\end{cfa}
    18671867Forcing matching parameter names in routine prototypes with corresponding routine definitions is possible, but goes against a strong tradition in C programming.
     
    18701870
    18711871Furthermore, named arguments do not work well in a \CFA-style programming-languages because they potentially introduces a new criteria for type matching.
    1872 For example, it is technically possible to disambiguate between these two overloaded definitions of ©f© based on named arguments at the call site:
     1872For example, it is technically possible to disambiguate between these two overloaded definitions of �f� based on named arguments at the call site:
    18731873\begin{cfa}
    18741874int f( int i, int j );
    18751875int f( int x, double y );
    18761876
    1877 f( j : 3, i : 4 );                              §\C{// 1st f}§
    1878 f( x : 7, y : 8.1 );                    §\C{// 2nd f}§
    1879 f( 4, 5 );                                              §\C{// ambiguous call}§
     1877f( j : 3, i : 4 );                              �\C{// 1st f}�
     1878f( x : 7, y : 8.1 );                    �\C{// 2nd f}�
     1879f( 4, 5 );                                              �\C{// ambiguous call}�
    18801880\end{cfa}
    18811881However, named arguments compound routine resolution in conjunction with conversions:
    18821882\begin{cfa}
    1883 f( i : 3, 5.7 );                                §\C{// ambiguous call ?}§
     1883f( i : 3, 5.7 );                                �\C{// ambiguous call ?}�
    18841884\end{cfa}
    18851885Depending on the cost associated with named arguments, this call could be resolvable or ambiguous.
     
    18951895the allowable positional calls are:
    18961896\begin{cfa}
    1897 p();                                                    §\C{// rewrite $\Rightarrow$ p( 1, 2, 3 )}§
    1898 p( 4 );                                                 §\C{// rewrite $\Rightarrow$ p( 4, 2, 3 )}§
    1899 p( 4, 4 );                                              §\C{// rewrite $\Rightarrow$ p( 4, 4, 3 )}§
    1900 p( 4, 4, 4 );                                   §\C{// rewrite $\Rightarrow$ p( 4, 4, 4 )}§
     1897p();                                                    �\C{// rewrite $\Rightarrow$ p( 1, 2, 3 )}�
     1898p( 4 );                                                 �\C{// rewrite $\Rightarrow$ p( 4, 2, 3 )}�
     1899p( 4, 4 );                                              �\C{// rewrite $\Rightarrow$ p( 4, 4, 3 )}�
     1900p( 4, 4, 4 );                                   �\C{// rewrite $\Rightarrow$ p( 4, 4, 4 )}�
    19011901// empty arguments
    1902 p(  , 4, 4 );                                   §\C{// rewrite $\Rightarrow$ p( 1, 4, 4 )}§
    1903 p( 4,  , 4 );                                   §\C{// rewrite $\Rightarrow$ p( 4, 2, 4 )}§
    1904 p( 4, 4,   );                                   §\C{// rewrite $\Rightarrow$ p( 4, 4, 3 )}§
    1905 p( 4,  ,   );                                   §\C{// rewrite $\Rightarrow$ p( 4, 2, 3 )}§
    1906 p(  , 4,   );                                   §\C{// rewrite $\Rightarrow$ p( 1, 4, 3 )}§
    1907 p(  ,  , 4 );                                   §\C{// rewrite $\Rightarrow$ p( 1, 2, 4 )}§
    1908 p(  ,  ,   );                                   §\C{// rewrite $\Rightarrow$ p( 1, 2, 3 )}§
     1902p(  , 4, 4 );                                   �\C{// rewrite $\Rightarrow$ p( 1, 4, 4 )}�
     1903p( 4,  , 4 );                                   �\C{// rewrite $\Rightarrow$ p( 4, 2, 4 )}�
     1904p( 4, 4,   );                                   �\C{// rewrite $\Rightarrow$ p( 4, 4, 3 )}�
     1905p( 4,  ,   );                                   �\C{// rewrite $\Rightarrow$ p( 4, 2, 3 )}�
     1906p(  , 4,   );                                   �\C{// rewrite $\Rightarrow$ p( 1, 4, 3 )}�
     1907p(  ,  , 4 );                                   �\C{// rewrite $\Rightarrow$ p( 1, 2, 4 )}�
     1908p(  ,  ,   );                                   �\C{// rewrite $\Rightarrow$ p( 1, 2, 3 )}�
    19091909\end{cfa}
    19101910Here the missing arguments are inserted from the default values in the parameter list.
     
    19301930Default values may only appear in a prototype versus definition context:
    19311931\begin{cfa}
    1932 void p( int x, int y = 2, int z = 3 );          §\C{// prototype: allowed}§
    1933 void p( int, int = 2, int = 3 );                        §\C{// prototype: allowed}§
    1934 void p( int x, int y = 2, int z = 3 ) {}        §\C{// definition: not allowed}§
     1932void p( int x, int y = 2, int z = 3 );          �\C{// prototype: allowed}�
     1933void p( int, int = 2, int = 3 );                        �\C{// prototype: allowed}�
     1934void p( int x, int y = 2, int z = 3 ) {}        �\C{// definition: not allowed}�
    19351935\end{cfa}
    19361936The reason for this restriction is to allow separate compilation.
     
    19471947\begin{cfa}
    19481948p( int x, int y, int z, ... );
    1949 p( 1, 4, 5, 6, z : 3, y : 2 ); §\C{// assume p( /* positional */, ... , /* named */ );}§
    1950 p( 1, z : 3, y : 2, 4, 5, 6 ); §\C{// assume p( /* positional */, /* named */, ... );}§
     1949p( 1, 4, 5, 6, z : 3, y : 2 ); �\C{// assume p( /* positional */, ... , /* named */ );}�
     1950p( 1, z : 3, y : 2, 4, 5, 6 ); �\C{// assume p( /* positional */, /* named */, ... );}�
    19511951\end{cfa}
    19521952In the first call, it is necessary for the programmer to conceptually rewrite the call, changing named arguments into positional, before knowing where the ellipse arguments begin.
     
    19571957\begin{cfa}
    19581958void p( int x, int y = 2, int z = 3... );
    1959 p( 1, 4, 5, 6, z : 3 );         §\C{// assume p( /* positional */, ... , /* named */ );}§
    1960 p( 1, z : 3, 4, 5, 6 );         §\C{// assume p( /* positional */, /* named */, ... );}§
     1959p( 1, 4, 5, 6, z : 3 );         �\C{// assume p( /* positional */, ... , /* named */ );}�
     1960p( 1, z : 3, 4, 5, 6 );         �\C{// assume p( /* positional */, /* named */, ... );}�
    19611961\end{cfa}
    19621962The first call is an error because arguments 4 and 5 are actually positional not ellipse arguments;
     
    19881988Furthermore, overloading cannot handle accessing default arguments in the middle of a positional list, via a missing argument, such as:
    19891989\begin{cfa}
    1990 p( 1, /* default */, 5 );               §\C{// rewrite $\Rightarrow$ p( 1, 2, 5 )}§
     1990p( 1, /* default */, 5 );               �\C{// rewrite $\Rightarrow$ p( 1, 2, 5 )}�
    19911991\end{cfa}
    19921992
     
    20012001\begin{cfa}
    20022002struct {
    2003         int f1;                                 §\C{// named field}§
    2004         int f2 : 4;                             §\C{// named field with bit field size}§
    2005         int : 3;                                §\C{// unnamed field for basic type with bit field size}§
    2006         int ;                                   §\C{// disallowed, unnamed field}§
    2007         int *;                                  §\C{// disallowed, unnamed field}§
    2008         int (*)( int );                 §\C{// disallowed, unnamed field}§
     2003        int f1;                                 �\C{// named field}�
     2004        int f2 : 4;                             �\C{// named field with bit field size}�
     2005        int : 3;                                �\C{// unnamed field for basic type with bit field size}�
     2006        int ;                                   �\C{// disallowed, unnamed field}�
     2007        int *;                                  �\C{// disallowed, unnamed field}�
     2008        int (*)( int );                 �\C{// disallowed, unnamed field}�
    20092009};
    20102010\end{cfa}
     
    20142014\begin{cfa}
    20152015struct {
    2016         int , , ;                               §\C{// 3 unnamed fields}§
     2016        int , , ;                               �\C{// 3 unnamed fields}�
    20172017}
    20182018\end{cfa}
     
    20612061        struct T t;
    20622062} s;
    2063        
     2063
    20642064
    20652065
     
    20822082
    20832083int fred() {
    2084         s.t.c = ®S.®R;  // type qualification
    2085         struct ®S.®T t = { ®S.®R, 1, 2 };
    2086         enum ®S.®C c;
    2087         union ®S.T.®U u;
     2084        s.t.c = �S.�R;  // type qualification
     2085        struct �S.�T t = { �S.�R, 1, 2 };
     2086        enum �S.�C c;
     2087        union �S.T.�U u;
    20882088}
    20892089\end{cfa}
     
    20922092\label{f:TypeNestingQualification}
    20932093\end{figure}
    2094 In the left example in C, types ©C©, ©U© and ©T© are implicitly hoisted outside of type ©S© into the containing block scope.
    2095 In the right example in \CFA, the types are not hoisted and accessed using the field-selection operator ``©.©'' for type qualification, as does \Index*{Java}, rather than the \CC type-selection operator ``©::©''.
     2094In the left example in C, types �C�, �U� and �T� are implicitly hoisted outside of type �S� into the containing block scope.
     2095In the right example in \CFA, the types are not hoisted and accessed using the field-selection operator ``�.�'' for type qualification, as does \Index*{Java}, rather than the \CC type-selection operator ``�::�''.
    20962096
    20972097
     
    21082108const unsigned int size = 5;
    21092109int ia[size];
    2110 ...                                             §\C{// assign values to array ia}§
    2111 qsort( ia, size );              §\C{// sort ascending order using builtin ?<?}§
     2110...                                             �\C{// assign values to array ia}�
     2111qsort( ia, size );              �\C{// sort ascending order using builtin ?<?}�
    21122112{
    2113         ®int ?<?( int x, int y ) { return x > y; }® §\C{// nested routine}§
    2114         qsort( ia, size );      §\C{// sort descending order by local redefinition}§
     2113        �int ?<?( int x, int y ) { return x > y; }� �\C{// nested routine}�
     2114        qsort( ia, size );      �\C{// sort descending order by local redefinition}�
    21152115}
    21162116\end{cfa}
     
    21202120The following program in undefined in \CFA (and Indexc{gcc})
    21212121\begin{cfa}
    2122 [* [int]( int )] foo() {                §\C{// int (*foo())( int )}§
    2123         int ®i® = 7;
     2122[* [int]( int )] foo() {                �\C{// int (*foo())( int )}�
     2123        int �i� = 7;
    21242124        int bar( int p ) {
    2125                 ®i® += 1;                               §\C{// dependent on local variable}§
    2126                 sout | ®i® | endl;
     2125                �i� += 1;                               �\C{// dependent on local variable}�
     2126                sout | �i� | endl;
    21272127        }
    2128         return bar;                                     §\C{// undefined because of local dependence}§
     2128        return bar;                                     �\C{// undefined because of local dependence}�
    21292129}
    21302130int main() {
    2131         * [int]( int ) fp = foo();      §\C{// int (*fp)( int )}§
     2131        * [int]( int ) fp = foo();      �\C{// int (*fp)( int )}�
    21322132        sout | fp( 3 ) | endl;
    21332133}
    21342134\end{cfa}
    2135 because 
     2135because
    21362136
    21372137Currently, there are no \Index{lambda} expressions, \ie unnamed routines because routine names are very important to properly select the correct routine.
     
    21452145The general syntax of a lexical list is:
    21462146\begin{cfa}
    2147 [ §\emph{exprlist}§ ]
    2148 \end{cfa}
    2149 where ©$\emph{exprlist}$© is a list of one or more expressions separated by commas.
    2150 The brackets, ©[]©, allow differentiating between lexical lists and expressions containing the C comma operator.
     2147[ �\emph{exprlist}� ]
     2148\end{cfa}
     2149where �$\emph{exprlist}$� is a list of one or more expressions separated by commas.
     2150The brackets, �[]�, allow differentiating between lexical lists and expressions containing the C comma operator.
    21512151The following are examples of lexical lists:
    21522152\begin{cfa}
     
    21552155[ v+w, x*y, 3.14159, f() ]
    21562156\end{cfa}
    2157 Tuples are permitted to contain sub-tuples (\ie nesting), such as ©[ [ 14, 21 ], 9 ]©, which is a 2-element tuple whose first element is itself a tuple.
     2157Tuples are permitted to contain sub-tuples (\ie nesting), such as �[ [ 14, 21 ], 9 ]�, which is a 2-element tuple whose first element is itself a tuple.
    21582158Note, a tuple is not a record (structure);
    21592159a record denotes a single value with substructure, whereas a tuple is multiple values with no substructure (see flattening coercion in Section 12.1).
     
    21652165The general syntax of a tuple type is:
    21662166\begin{cfa}
    2167 [ §\emph{typelist}§ ]
    2168 \end{cfa}
    2169 where ©$\emph{typelist}$© is a list of one or more legal \CFA or C type specifications separated by commas, which may include other tuple type specifications.
     2167[ �\emph{typelist}� ]
     2168\end{cfa}
     2169where �$\emph{typelist}$� is a list of one or more legal \CFA or C type specifications separated by commas, which may include other tuple type specifications.
    21702170Examples of tuple types include:
    21712171\begin{cfa}
    21722172[ unsigned int, char ]
    21732173[ double, double, double ]
    2174 [ * int, int * ]                §\C{// mix of CFA and ANSI}§
     2174[ * int, int * ]                �\C{// mix of CFA and ANSI}�
    21752175[ * [ 5 ] int, * * char, * [ [ int, int ] ] (int, int) ]
    21762176\end{cfa}
    2177 Like tuples, tuple types may be nested, such as ©[ [ int, int ], int ]©, which is a 2-element tuple type whose first element is itself a tuple type.
     2177Like tuples, tuple types may be nested, such as �[ [ int, int ], int ]�, which is a 2-element tuple type whose first element is itself a tuple type.
    21782178
    21792179Examples of declarations using tuple types are:
    21802180\begin{cfa}
    2181 [ int, int ] x;                 §\C{// 2 element tuple, each element of type int}§
    2182 * [ char, char ] y;             §\C{// pointer to a 2 element tuple}§
     2181[ int, int ] x;                 �\C{// 2 element tuple, each element of type int}�
     2182* [ char, char ] y;             �\C{// pointer to a 2 element tuple}�
    21832183[ [ int, int ] ] z ([ int, int ]);
    21842184\end{cfa}
     
    22112211tuple does not have structure like a record; a tuple is simply converted into a list of components.
    22122212\begin{rationale}
    2213 The present implementation of \CFA does not support nested routine calls when the inner routine returns multiple values; \ie a statement such as ©g( f() )© is not supported.
     2213The present implementation of \CFA does not support nested routine calls when the inner routine returns multiple values; \ie a statement such as �g( f() )� is not supported.
    22142214Using a temporary variable to store the  results of the inner routine and then passing this variable to the outer routine works, however.
    22152215\end{rationale}
     
    22402240\begin{rationale}
    22412241Unfortunately, C's syntax for subscripts precluded treating them as tuples.
    2242 The C subscript list has the form ©[i][j]...© and not ©[i, j, ...]©.
    2243 Therefore, there is no syntactic way for a routine returning multiple values to specify the different subscript values, \eg ©f[g()]© always means a single subscript value because there is only one set of brackets.
    2244 Fixing this requires a major change to C because the syntactic form ©M[i, j, k]© already has a particular meaning: ©i, j, k© is a comma expression.
     2242The C subscript list has the form �[i][j]...� and not �[i, j, ...]�.
     2243Therefore, there is no syntactic way for a routine returning multiple values to specify the different subscript values, \eg �f[g()]� always means a single subscript value because there is only one set of brackets.
     2244Fixing this requires a major change to C because the syntactic form �M[i, j, k]� already has a particular meaning: �i, j, k� is a comma expression.
    22452245\end{rationale}
    22462246
     
    22612261[ a, b, c, d ] = w
    22622262\end{cfa}
    2263 ©w© is implicitly opened to yield a tuple of four values, which are then assigned individually.
     2263�w� is implicitly opened to yield a tuple of four values, which are then assigned individually.
    22642264
    22652265A \newterm{flattening coercion} coerces a nested tuple, \ie a tuple with one or more components, which are themselves tuples, into a flattened tuple, which is a tuple whose components are not tuples, as in:
     
    22692269First the right-hand tuple is flattened and then the values are assigned individually.
    22702270Flattening is also performed on tuple types.
    2271 For example, the type ©[ int, [ int, int ], int ]© can be coerced, using flattening, into the type ©[ int, int, int, int ]©.
     2271For example, the type �[ int, [ int, int ], int ]� can be coerced, using flattening, into the type �[ int, int, int, int ]�.
    22722272
    22732273A \newterm{structuring coercion} is the opposite of flattening;
    22742274a tuple is structured into a more complex nested tuple.
    2275 For example, structuring the tuple ©[ 1, 2, 3, 4 ]© into the tuple ©[ 1, [ 2, 3 ], 4 ]© or the tuple type ©[ int, int, int, int ]© into the tuple type ©[ int, [ int, int ], int ]©.
     2275For example, structuring the tuple �[ 1, 2, 3, 4 ]� into the tuple �[ 1, [ 2, 3 ], 4 ]� or the tuple type �[ int, int, int, int ]� into the tuple type �[ int, [ int, int ], int ]�.
    22762276In the following example, the last assignment illustrates all the tuple coercions:
    22772277\begin{cfa}
    22782278[ int, int, int, int ] w = [ 1, 2, 3, 4 ];
    22792279int x = 5;
    2280 [ x, w ] = [ w, x ];            §\C{// all four tuple coercions}§
     2280[ x, w ] = [ w, x ];            �\C{// all four tuple coercions}�
    22812281\end{cfa}
    22822282Starting on the right-hand tuple in the last assignment statement, w is opened, producing a tuple of four values;
    2283 therefore, the right-hand tuple is now the tuple ©[ [ 1, 2, 3, 4 ], 5 ]©.
    2284 This tuple is then flattened, yielding ©[ 1, 2, 3, 4, 5 ]©, which is structured into ©[ 1, [ 2, 3, 4, 5 ] ]© to match the tuple type of the left-hand side.
    2285 The tuple ©[ 2, 3, 4, 5 ]© is then closed to create a tuple value.
    2286 Finally, ©x© is assigned ©1© and ©w© is assigned the tuple value using multiple assignment (see Section 14).
     2283therefore, the right-hand tuple is now the tuple �[ [ 1, 2, 3, 4 ], 5 ]�.
     2284This tuple is then flattened, yielding �[ 1, 2, 3, 4, 5 ]�, which is structured into �[ 1, [ 2, 3, 4, 5 ] ]� to match the tuple type of the left-hand side.
     2285The tuple �[ 2, 3, 4, 5 ]� is then closed to create a tuple value.
     2286Finally, �x� is assigned �1� and �w� is assigned the tuple value using multiple assignment (see Section 14).
    22872287\begin{rationale}
    22882288A possible additional language extension is to use the structuring coercion for tuples to initialize a complex record with a tuple.
     
    22952295Mass assignment has the following form:
    22962296\begin{cfa}
    2297 [ §\emph{lvalue}§, ... , §\emph{lvalue}§ ] = §\emph{expr}§;
     2297[ �\emph{lvalue}�, ... , �\emph{lvalue}� ] = �\emph{expr}�;
    22982298\end{cfa}
    22992299\index{lvalue}
    23002300The left-hand side is a tuple of \emph{lvalues}, which is a list of expressions each yielding an address, \ie any data object that can appear on the left-hand side of a conventional assignment statement.
    2301 ©$\emph{expr}$© is any standard arithmetic expression.
     2301�$\emph{expr}$� is any standard arithmetic expression.
    23022302Clearly, the types of the entities being assigned must be type compatible with the value of the expression.
    23032303
     
    23252325*a1 = t; *a2 = t; *a3 = t;
    23262326\end{cfa}
    2327 The temporary ©t© is necessary to store the value of the expression to eliminate conversion issues.
     2327The temporary �t� is necessary to store the value of the expression to eliminate conversion issues.
    23282328The temporaries for the addresses are needed so that locations on the left-hand side do not change as the values are assigned.
    2329 In this case, ©y[i]© uses the previous value of ©i© and not the new value set at the beginning of the mass assignment.
     2329In this case, �y[i]� uses the previous value of �i� and not the new value set at the beginning of the mass assignment.
    23302330
    23312331
     
    23352335Multiple assignment has the following form:
    23362336\begin{cfa}
    2337 [ §\emph{lvalue}§, ... , §\emph{lvalue}§ ] = [ §\emph{expr}§, ... , §\emph{expr}§ ];
     2337[ �\emph{lvalue}�, ... , �\emph{lvalue}� ] = [ �\emph{expr}�, ... , �\emph{expr}� ];
    23382338\end{cfa}
    23392339\index{lvalue}
     
    23442344[ x, y, z ] = [ 1, 2, 3 ];
    23452345\end{cfa}
    2346 Here, the values ©1©, ©2© and ©3© are assigned, respectively, to the variables ©x©, ©y© and ©z©.
     2346Here, the values �1�, �2� and �3� are assigned, respectively, to the variables �x�, �y� and �z�.
    23472347 A more complex example is:
    23482348\begin{cfa}
    23492349[ i, y[ i ], z ] = [ 1, i, a + b ];
    23502350\end{cfa}
    2351 Here, the values ©1©, ©i© and ©a + b© are assigned to the variables ©i©, ©y[i]© and ©z©, respectively.
     2351Here, the values �1�, �i� and �a + b� are assigned to the variables �i�, �y[i]� and �z�, respectively.
    23522352 Note, the parallel semantics of
    23532353multiple assignment ensures:
     
    23552355[ x, y ] = [ y, x ];
    23562356\end{cfa}
    2357 correctly interchanges (swaps) the values stored in ©x© and ©y©.
     2357correctly interchanges (swaps) the values stored in �x� and �y�.
    23582358The following cases are errors:
    23592359\begin{cfa}
     
    23662366both these examples produce indeterminate results:
    23672367\begin{cfa}
    2368 f( x++, x++ );                          §\C{// C routine call with side effects in arguments}§
    2369 [ v1, v2 ] = [ x++, x++ ];      §\C{// side effects in righthand side of multiple assignment}§
     2368f( x++, x++ );                          �\C{// C routine call with side effects in arguments}�
     2369[ v1, v2 ] = [ x++, x++ ];      �\C{// side effects in righthand side of multiple assignment}�
    23702370\end{cfa}
    23712371
     
    23762376Cascade assignment has the following form:
    23772377\begin{cfa}
    2378 §\emph{tuple}§ = §\emph{tuple}§ = ... = §\emph{tuple}§;
     2378�\emph{tuple}� = �\emph{tuple}� = ... = �\emph{tuple}�;
    23792379\end{cfa}
    23802380and it has the same parallel semantics as for mass and multiple assignment.
     
    23942394Its general form is:
    23952395\begin{cfa}
    2396 §\emph{expr}§ . [ §\emph{fieldlist}§ ]
    2397 §\emph{expr}§ -> [ §\emph{fieldlist}§ ]
    2398 \end{cfa}
    2399 \emph{expr} is any expression yielding a value of type record, \eg ©struct©, ©union©.
     2396�\emph{expr}� . [ �\emph{fieldlist}� ]
     2397�\emph{expr}� -> [ �\emph{fieldlist}� ]
     2398\end{cfa}
     2399\emph{expr} is any expression yielding a value of type record, \eg �struct�, �union�.
    24002400Each element of \emph{ fieldlist} is an element of the record specified by \emph{expr}.
    24012401A record-field tuple may be used anywhere a tuple can be used. An example of the use of a record-field tuple is
     
    24072407        double f4;
    24082408} v;
    2409 v.[ f3, f1, f2 ] = ['x', 11, 17 ];      §\C{// equivalent to v.f3 = 'x', v.f1 = 11, v.f2 = 17}§
    2410 f( v.[ f3, f1, f2 ] );                          §\C{// equivalent to f( v.f3, v.f1, v.f2 )}§
     2409v.[ f3, f1, f2 ] = ['x', 11, 17 ];      �\C{// equivalent to v.f3 = 'x', v.f1 = 11, v.f2 = 17}�
     2410f( v.[ f3, f1, f2 ] );                          �\C{// equivalent to f( v.f3, v.f1, v.f2 )}�
    24112411\end{cfa}
    24122412Note, the fields appearing in a record-field tuple may be specified in any order;
    24132413also, it is unnecessary to specify all the fields of a struct in a multiple record-field tuple.
    24142414
    2415 If a field of a ©struct© is itself another ©struct©, multiple fields of this subrecord can be specified using a nested record-field tuple, as in the following example:
     2415If a field of a �struct� is itself another �struct�, multiple fields of this subrecord can be specified using a nested record-field tuple, as in the following example:
    24162416\begin{cfa}
    24172417struct inner {
     
    24422442\begin{cfa}
    24432443int x = 1, y = 2, z = 3;
    2444 sout | x ®|® y ®|® z | endl;
     2444sout | x �|� y �|� z | endl;
    24452445\end{cfa}
    24462446&
    24472447\begin{cfa}
    24482448
    2449 cout << x ®<< " "® << y ®<< " "® << z << endl;
     2449cout << x �<< " "� << y �<< " "� << z << endl;
    24502450\end{cfa}
    24512451\\
    24522452\begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]
    2453 1® ®2® ®3
     24531� �2� �3
    24542454\end{cfa}
    24552455&
     
    24632463\begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt]
    24642464[int, [ int, int ] ] t1 = [ 1, [ 2, 3 ] ], t2 = [ 3, [ 4, 5 ] ];
    2465 sout | t1 | t2 | endl;                                  §\C{// print tuples}§
     2465sout | t1 | t2 | endl;                                  �\C{// print tuples}�
    24662466\end{cfa}
    24672467\begin{cfa}[mathescape=off,showspaces=true,belowskip=0pt]
    2468 1®, ®2®, ®3 3®, ®4®, ®5
     24681�, �2�, �3 3�, �4�, �5
    24692469\end{cfa}
    24702470Finally, \CFA uses the logical-or operator for I/O as it is the lowest-priority overloadable operator, other than assignment.
     
    24812481&
    24822482\begin{cfa}
    2483 cout << x * 3 << y + 1 << ®(®z << 2®)® << ®(®x == y®)® << (x | y) << (x || y) << (x > z ? 1 : 2) << endl;
     2483cout << x * 3 << y + 1 << �(�z << 2�)� << �(�x == y�)� << (x | y) << (x || y) << (x > z ? 1 : 2) << endl;
    24842484\end{cfa}
    24852485\\
     
    25232523
    25242524\item
    2525 A separator does not appear before a C string starting with the (extended) \Index*{ASCII}\index{ASCII!extended} characters: \lstinline[mathescape=off,basicstyle=\tt]@([{=$£¥¡¿«@
     2525A separator does not appear before a C string starting with the (extended) \Index*{ASCII}\index{ASCII!extended} characters: \lstinline[mathescape=off,basicstyle=\tt]@([{=$�����@
    25262526%$
    25272527\begin{cfa}[mathescape=off]
    2528 sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x $" | 5 | "x £" | 6 | "x ¥"
    2529                 | 7 | "x ¡" | 8 | "x ¿" | 9 | "x «" | 10 | endl;
     2528sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x $" | 5 | "x �" | 6 | "x �"
     2529                | 7 | "x �" | 8 | "x �" | 9 | "x �" | 10 | endl;
    25302530\end{cfa}
    25312531%$
    25322532\begin{cfa}[mathescape=off,basicstyle=\tt,showspaces=true,aboveskip=0pt,belowskip=0pt]
    2533 x ®(®1 x ®[®2 x ®{®3 x ®=®4 x ®$®5 x ®£®6 x ®¥®7 x ®¡®8 x ®¿®9 x ®«®10
     2533x �(�1 x �[�2 x �{�3 x �=�4 x �$�5 x ���6 x ���7 x ���8 x ���9 x ���10
    25342534\end{cfa}
    25352535%$
    2536 where \lstinline[basicstyle=\tt]@¡¿@ are inverted opening exclamation and question marks, and \lstinline[basicstyle=\tt]@«@ is an opening citation mark.
    2537 
    2538 \item
    2539 {\lstset{language=CFA,deletedelim=**[is][]{¢}{¢}}
    2540 A seperator does not appear after a C string ending with the (extended) \Index*{ASCII}\index{ASCII!extended} characters: \lstinline[basicstyle=\tt]@,.;!?)]}%¢»@
     2536where \lstinline[basicstyle=\tt]@��@ are inverted opening exclamation and question marks, and \lstinline[basicstyle=\tt]@�@ is an opening citation mark.
     2537
     2538\item
     2539{\lstset{language=CFA,deletedelim=**[is][]{�}{�}}
     2540A seperator does not appear after a C string ending with the (extended) \Index*{ASCII}\index{ASCII!extended} characters: \lstinline[basicstyle=\tt]@,.;!?)]}%��@
    25412541\begin{cfa}[belowskip=0pt]
    25422542sout | 1 | ", x" | 2 | ". x" | 3 | "; x" | 4 | "! x" | 5 | "? x" | 6 | "% x"
    2543                 | 7 | "¢ x" | 8 | "» x" | 9 | ") x" | 10 | "] x" | 11 | "} x" | endl;
     2543                | 7 | "� x" | 8 | "� x" | 9 | ") x" | 10 | "] x" | 11 | "} x" | endl;
    25442544\end{cfa}
    25452545\begin{cfa}[basicstyle=\tt,showspaces=true,aboveskip=0pt,belowskip=0pt]
    2546 1®,® x 2®.® x 3®;® x 4®!® x 5®?® x 6®%® x 7§\color{red}\textcent§ x 8®»® x 9®)® x 10®]® x 11®}® x
     25461�,� x 2�.� x 3�;� x 4�!� x 5�?� x 6�%� x 7�\color{red}\textcent� x 8��� x 9�)� x 10�]� x 11�}� x
    25472547\end{cfa}}%
    2548 where \lstinline[basicstyle=\tt]@»@ is a closing citation mark.
     2548where \lstinline[basicstyle=\tt]@@ is a closing citation mark.
    25492549
    25502550\item
     
    25542554\end{cfa}
    25552555\begin{cfa}[basicstyle=\tt,showspaces=true,showtabs=true,aboveskip=0pt,belowskip=0pt]
    2556 x®`®1®`®x§\color{red}\texttt{'}§2§\color{red}\texttt{'}§x§\color{red}\texttt{"}§3§\color{red}\texttt{"}§x®:®4®:®x® ®5® ®x®      ®6®     ®x
     2556x�`�1�`�x�\color{red}\texttt{'}�2�\color{red}\texttt{'}�x�\color{red}\texttt{"}�3�\color{red}\texttt{"}�x�:�4�:�x� �5� �x�      �6�     �x
    25572557\end{cfa}
    25582558
     
    25602560If a space is desired before or after one of the special string start/end characters, simply insert a space.
    25612561\begin{cfa}[belowskip=0pt]
    2562 sout | "x (§\color{red}\texttt{\textvisiblespace}§" | 1 | "§\color{red}\texttt{\textvisiblespace}§) x" | 2 | "§\color{red}\texttt{\textvisiblespace}§, x" | 3 | "§\color{red}\texttt{\textvisiblespace}§:x:§\color{red}\texttt{\textvisiblespace}§" | 4 | endl;
     2562sout | "x (�\color{red}\texttt{\textvisiblespace}�" | 1 | "�\color{red}\texttt{\textvisiblespace}�) x" | 2 | "�\color{red}\texttt{\textvisiblespace}�, x" | 3 | "�\color{red}\texttt{\textvisiblespace}�:x:�\color{red}\texttt{\textvisiblespace}�" | 4 | endl;
    25632563\end{cfa}
    25642564\begin{cfa}[basicstyle=\tt,showspaces=true,showtabs=true,aboveskip=0pt,belowskip=0pt]
    2565 x (® ®1® ®) x 2® ®, x 3® ®:x:® ®4
     2565x (� �1� �) x 2� �, x 3� �:x:� �4
    25662566\end{cfa}
    25672567\end{enumerate}
     
    25732573\begin{enumerate}
    25742574\item
    2575 Routines \Indexc{sepSet}\index{manipulator!sepSet@©sepSet©} and \Indexc{sepGet}\index{manipulator!sepGet@©sepGet©} set and get the separator string.
    2576 The separator string can be at most 16 characters including the ©'\0'© string terminator (15 printable characters).
     2575Routines \Indexc{sepSet}\index{manipulator!sepSet@�sepSet�} and \Indexc{sepGet}\index{manipulator!sepGet@�sepGet�} set and get the separator string.
     2576The separator string can be at most 16 characters including the �'\0'� string terminator (15 printable characters).
    25772577\begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt]
    2578 sepSet( sout, ", $" );                                          §\C{// set separator from " " to ", \$"}§
    2579 sout | 1 | 2 | 3 | " \"" | ®sepGet( sout )® | "\"" | endl;
     2578sepSet( sout, ", $" );                                          �\C{// set separator from " " to ", \$"}�
     2579sout | 1 | 2 | 3 | " \"" | �sepGet( sout )� | "\"" | endl;
    25802580\end{cfa}
    25812581%$
    25822582\begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt]
    2583 1®, $®2®, $®3 ®", $"®
     25831�, $�2�, $�3 �", $"�
    25842584\end{cfa}
    25852585%$
    25862586\begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt]
    2587 sepSet( sout, " " );                                            §\C{// reset separator to " "}§
    2588 sout | 1 | 2 | 3 | " \"" | ®sepGet( sout )® | "\"" | endl;
     2587sepSet( sout, " " );                                            �\C{// reset separator to " "}�
     2588sout | 1 | 2 | 3 | " \"" | �sepGet( sout )� | "\"" | endl;
    25892589\end{cfa}
    25902590\begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt]
    2591 1® ®2® ®3 ®" "®
    2592 \end{cfa}
    2593 
    2594 \item
    2595 Routine \Indexc{sepSetTuple}\index{manipulator!sepSetTuple@©sepSetTuple©} and \Indexc{sepGetTuple}\index{manipulator!sepGetTuple@©sepGetTuple©} get and set the tuple separator-string.
    2596 The tuple separator-string can be at most 16 characters including the ©'\0'© string terminator (15 printable characters).
     25911� �2� �3 �" "�
     2592\end{cfa}
     2593
     2594\item
     2595Routine \Indexc{sepSetTuple}\index{manipulator!sepSetTuple@�sepSetTuple�} and \Indexc{sepGetTuple}\index{manipulator!sepGetTuple@�sepGetTuple�} get and set the tuple separator-string.
     2596The tuple separator-string can be at most 16 characters including the �'\0'� string terminator (15 printable characters).
    25972597\begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt]
    2598 sepSetTuple( sout, " " );                                       §\C{// set tuple separator from ", " to " "}§
    2599 sout | t1 | t2 | " \"" | ®sepGetTuple( sout )® | "\"" | endl;
     2598sepSetTuple( sout, " " );                                       �\C{// set tuple separator from ", " to " "}�
     2599sout | t1 | t2 | " \"" | �sepGetTuple( sout )� | "\"" | endl;
    26002600\end{cfa}
    26012601\begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt]
    2602 1 2 3 4 ®" "®
     26021 2 3 4 �" "�
    26032603\end{cfa}
    26042604\begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt]
    2605 sepSetTuple( sout, ", " );                                      §\C{// reset tuple separator to ", "}§
    2606 sout | t1 | t2 | " \"" | ®sepGetTuple( sout )® | "\"" | endl;
     2605sepSetTuple( sout, ", " );                                      �\C{// reset tuple separator to ", "}�
     2606sout | t1 | t2 | " \"" | �sepGetTuple( sout )� | "\"" | endl;
    26072607\end{cfa}
    26082608\begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt]
    2609 1, 2, 3, 4 ®", "®
    2610 \end{cfa}
    2611 
    2612 \item
    2613 Manipulators \Indexc{sepOn}\index{manipulator!sepOn@©sepOn©} and \Indexc{sepOff}\index{manipulator!sepOff@©sepOff©} \emph{locally} toggle printing the separator, \ie the seperator is adjusted only with respect to the next printed item.
     26091, 2, 3, 4 �", "�
     2610\end{cfa}
     2611
     2612\item
     2613Manipulators \Indexc{sepOn}\index{manipulator!sepOn@�sepOn�} and \Indexc{sepOff}\index{manipulator!sepOff@�sepOff�} \emph{locally} toggle printing the separator, \ie the seperator is adjusted only with respect to the next printed item.
    26142614\begin{cfa}[mathescape=off,belowskip=0pt]
    2615 sout | sepOn | 1 | 2 | 3 | sepOn | endl;        §\C{// separator at start/end of line}§
     2615sout | sepOn | 1 | 2 | 3 | sepOn | endl;        �\C{// separator at start/end of line}�
    26162616\end{cfa}
    26172617\begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]
    2618 ® ®1 2 3® ®
     2618� �1 2 3� �
    26192619\end{cfa}
    26202620\begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt]
    2621 sout | 1 | sepOff | 2 | 3 | endl;                       §\C{// locally turn off implicit separator}§
     2621sout | 1 | sepOff | 2 | 3 | endl;                       �\C{// locally turn off implicit separator}�
    26222622\end{cfa}
    26232623\begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]
     
    26262626The tuple separator also responses to being turned on and off.
    26272627\begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt]
    2628 sout | sepOn | t1 | sepOff | t2 | endl;         §\C{// locally turn on/off implicit separation}§
     2628sout | sepOn | t1 | sepOff | t2 | endl;         �\C{// locally turn on/off implicit separation}�
    26292629\end{cfa}
    26302630\begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]
     
    26342634
    26352635\item
    2636 Manipulators \Indexc{sepDisable}\index{manipulator!sepDisable@©sepDisable©} and \Indexc{sepEnable}\index{manipulator!sepEnable@©sepEnable©} \emph{globally} toggle printing the separator, \ie the seperator is adjusted with respect to all subsequent printed items, unless locally adjusted.
     2636Manipulators \Indexc{sepDisable}\index{manipulator!sepDisable@�sepDisable�} and \Indexc{sepEnable}\index{manipulator!sepEnable@�sepEnable�} \emph{globally} toggle printing the separator, \ie the seperator is adjusted with respect to all subsequent printed items, unless locally adjusted.
    26372637\begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt]
    2638 sout | sepDisable | 1 | 2 | 3 | endl;           §\C{// globally turn off implicit separation}§
     2638sout | sepDisable | 1 | 2 | 3 | endl;           �\C{// globally turn off implicit separation}�
    26392639\end{cfa}
    26402640\begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]
     
    26422642\end{cfa}
    26432643\begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt]
    2644 sout | 1 | ®sepOn® | 2 | 3 | endl;                      §\C{// locally turn on implicit separator}§
     2644sout | 1 | �sepOn� | 2 | 3 | endl;                      �\C{// locally turn on implicit separator}�
    26452645\end{cfa}
    26462646\begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]
    2647 1® ®23
     26471� �23
    26482648\end{cfa}
    26492649\begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt]
    2650 sout | sepEnable | 1 | 2 | 3 | endl;            §\C{// globally turn on implicit separation}§
     2650sout | sepEnable | 1 | 2 | 3 | endl;            �\C{// globally turn on implicit separation}�
    26512651\end{cfa}
    26522652\begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]
     
    26672667        sout | '1' | '2' | '3' | endl;
    26682668        sout | 1 | "" | 2 | "" | 3 | endl;
    2669         sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x $" | 5 | "x £" | 6 | "x ¥"
    2670                 | 7 | "x ¡" | 8 | "x ¿" | 9 | "x «" | 10 | endl;
     2669        sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x $" | 5 | "x �" | 6 | "x �"
     2670                | 7 | "x �" | 8 | "x �" | 9 | "x �" | 10 | endl;
    26712671        sout | 1 | ", x" | 2 | ". x" | 3 | "; x" | 4 | "! x" | 5 | "? x" | 6 | "% x"
    2672                 | 7 | "¢ x" | 8 | "» x" | 9 | ") x" | 10 | "] x" | 11 | "} x" | endl;
     2672                | 7 | "� x" | 8 | "� x" | 9 | ") x" | 10 | "] x" | 11 | "} x" | endl;
    26732673        sout | "x`" | 1 | "`x'" | 2 | "'x\"" | 3 | "\"x:" | 4 | ":x " | 5 | " x\t" | 6 | "\tx" | endl;
    26742674        sout | "x ( " | 1 | " ) x" | 2 | " , x" | 3 | " :x: " | 4 | endl;
     
    29092909In \CFA, as in C, all scalar types can be incremented and
    29102910decremented, which is defined in terms of adding or subtracting 1.
    2911 The operations ©&&©, ©||©, and ©!© can be applied to any scalar arguments and are defined in terms of comparison against 0 (ex. ©(a && b)© becomes ©(a != 0 && b != 0)©).
     2911The operations �&&�, �||�, and �!� can be applied to any scalar arguments and are defined in terms of comparison against 0 (ex. �(a && b)� becomes �(a != 0 && b != 0)�).
    29122912
    29132913In C, the integer constants 0 and 1 suffice because the integer promotion rules can convert them to any arithmetic type, and the rules for pointer expressions treat constant expressions evaluating to 0 as a special case.
    29142914However, user-defined arithmetic types often need the equivalent of a 1 or 0 for their functions or operators, polymorphic functions often need 0 and 1 constants of a type matching their polymorphic parameters, and user-defined pointer-like types may need a null value.
    2915 Defining special constants for a user-defined type is more efficient than defining a conversion to the type from ©_Bool©.
     2915Defining special constants for a user-defined type is more efficient than defining a conversion to the type from �_Bool�.
    29162916
    29172917Why just 0 and 1? Why not other integers? No other integers have special status in C.
     
    29962996\begin{tabular}[t]{ll}
    29972997%identifier & operation \\ \hline
    2998 ©?[?]© & subscripting \impl{?[?]}\\
    2999 ©?()© & function call \impl{?()}\\
    3000 ©?++© & postfix increment \impl{?++}\\
    3001 ©?--© & postfix decrement \impl{?--}\\
    3002 ©++?© & prefix increment \impl{++?}\\
    3003 ©--?© & prefix decrement \impl{--?}\\
    3004 ©*?© & dereference \impl{*?}\\
    3005 ©+?© & unary plus \impl{+?}\\
    3006 ©-?© & arithmetic negation \impl{-?}\\
    3007 ©~?© & bitwise negation \impl{~?}\\
    3008 ©!?© & logical complement \impl{"!?}\\
    3009 ©?*?© & multiplication \impl{?*?}\\
    3010 ©?/?© & division \impl{?/?}\\
     2998�?[?]� & subscripting \impl{?[?]}\\
     2999�?()� & function call \impl{?()}\\
     3000�?++� & postfix increment \impl{?++}\\
     3001�?--� & postfix decrement \impl{?--}\\
     3002�++?� & prefix increment \impl{++?}\\
     3003�--?� & prefix decrement \impl{--?}\\
     3004�*?� & dereference \impl{*?}\\
     3005�+?� & unary plus \impl{+?}\\
     3006�-?� & arithmetic negation \impl{-?}\\
     3007�~?� & bitwise negation \impl{~?}\\
     3008�!?� & logical complement \impl{"!?}\\
     3009�?*?� & multiplication \impl{?*?}\\
     3010�?/?� & division \impl{?/?}\\
    30113011\end{tabular}\hfil
    30123012\begin{tabular}[t]{ll}
    30133013%identifier & operation \\ \hline
    3014 ©?%?© & remainder \impl{?%?}\\
    3015 ©?+?© & addition \impl{?+?}\\
    3016 ©?-?© & subtraction \impl{?-?}\\
    3017 ©?<<?© & left shift \impl{?<<?}\\
    3018 ©?>>?© & right shift \impl{?>>?}\\
    3019 ©?<?© & less than \impl{?<?}\\
    3020 ©?<=?© & less than or equal \impl{?<=?}\\
    3021 ©?>=?© & greater than or equal \impl{?>=?}\\
    3022 ©?>?© & greater than \impl{?>?}\\
    3023 ©?==?© & equality \impl{?==?}\\
    3024 ©?!=?© & inequality \impl{?"!=?}\\
    3025 ©?&?© & bitwise AND \impl{?&?}\\
     3014�?%?� & remainder \impl{?%?}\\
     3015�?+?� & addition \impl{?+?}\\
     3016�?-?� & subtraction \impl{?-?}\\
     3017�?<<?� & left shift \impl{?<<?}\\
     3018�?>>?� & right shift \impl{?>>?}\\
     3019�?<?� & less than \impl{?<?}\\
     3020�?<=?� & less than or equal \impl{?<=?}\\
     3021�?>=?� & greater than or equal \impl{?>=?}\\
     3022�?>?� & greater than \impl{?>?}\\
     3023�?==?� & equality \impl{?==?}\\
     3024�?!=?� & inequality \impl{?"!=?}\\
     3025�?&?� & bitwise AND \impl{?&?}\\
    30263026\end{tabular}\hfil
    30273027\begin{tabular}[t]{ll}
    30283028%identifier & operation \\ \hline
    3029 ©?^?© & exclusive OR \impl{?^?}\\
    3030 ©?|?© & inclusive OR \impl{?"|?}\\
    3031 ©?=?© & simple assignment \impl{?=?}\\
    3032 ©?*=?© & multiplication assignment \impl{?*=?}\\
    3033 ©?/=?© & division assignment \impl{?/=?}\\
    3034 ©?%=?© & remainder assignment \impl{?%=?}\\
    3035 ©?+=?© & addition assignment \impl{?+=?}\\
    3036 ©?-=?© & subtraction assignment \impl{?-=?}\\
    3037 ©?<<=?© & left-shift assignment \impl{?<<=?}\\
    3038 ©?>>=?© & right-shift assignment \impl{?>>=?}\\
    3039 ©?&=?© & bitwise AND assignment \impl{?&=?}\\
    3040 ©?^=?© & exclusive OR assignment \impl{?^=?}\\
    3041 ©?|=?© & inclusive OR assignment \impl{?"|=?}\\
     3029�?^?� & exclusive OR \impl{?^?}\\
     3030�?|?� & inclusive OR \impl{?"|?}\\
     3031�?=?� & simple assignment \impl{?=?}\\
     3032�?*=?� & multiplication assignment \impl{?*=?}\\
     3033�?/=?� & division assignment \impl{?/=?}\\
     3034�?%=?� & remainder assignment \impl{?%=?}\\
     3035�?+=?� & addition assignment \impl{?+=?}\\
     3036�?-=?� & subtraction assignment \impl{?-=?}\\
     3037�?<<=?� & left-shift assignment \impl{?<<=?}\\
     3038�?>>=?� & right-shift assignment \impl{?>>=?}\\
     3039�?&=?� & bitwise AND assignment \impl{?&=?}\\
     3040�?^=?� & exclusive OR assignment \impl{?^=?}\\
     3041�?|=?� & inclusive OR assignment \impl{?"|=?}\\
    30423042\end{tabular}
    30433043\hfil
     
    30483048These identifiers are defined such that the question marks in the name identify the location of the operands.
    30493049These operands represent the parameters to the functions, and define how the operands are mapped to the function call.
    3050 For example, ©a + b© becomes ©?+?(a, b)©.
     3050For example, �a + b� becomes �?+?(a, b)�.
    30513051
    30523052In the example below, a new type, myComplex, is defined with an overloaded constructor, + operator, and string operator.
     
    31223122\end{itemize}
    31233123
    3124 In \CFA, ©typedef© provides a mechanism to alias long type names with short ones, both globally and locally, but not eliminate the use of the short name.
    3125 \Indexc{gcc} provides ©typeof© to declare a secondary variable from a primary variable.
     3124In \CFA, �typedef� provides a mechanism to alias long type names with short ones, both globally and locally, but not eliminate the use of the short name.
     3125\Indexc{gcc} provides �typeof� to declare a secondary variable from a primary variable.
    31263126\CFA also relies heavily on the specification of the left-hand side of assignment for type inferencing, so in many cases it is crucial to specify the type of the left-hand side to select the correct type of the right-hand expression.
    31273127Only for overloaded routines with the same return type is variable type-inferencing possible.
    3128 Finally, ©auto© presents the programming problem of tracking down a type when the type is actually needed.
     3128Finally, �auto� presents the programming problem of tracking down a type when the type is actually needed.
    31293129For example, given
    31303130\begin{cfa}
    3131 auto j = ®...®
    3132 \end{cfa}
    3133 and the need to write a routine to compute using ©j©
    3134 \begin{cfa}
    3135 void rtn( ®...® parm );
     3131auto j = �...�
     3132\end{cfa}
     3133and the need to write a routine to compute using �j�
     3134\begin{cfa}
     3135void rtn( �...� parm );
    31363136rtn( j );
    31373137\end{cfa}
    3138 A programmer must work backwards to determine the type of ©j©'s initialization expression, reconstructing the possibly long generic type-name.
     3138A programmer must work backwards to determine the type of �j�'s initialization expression, reconstructing the possibly long generic type-name.
    31393139In this situation, having the type name or a short alias is very useful.
    31403140
     
    31443144At some point, a programmer wants the type of the variable to remain constant and the expression to be in error when it changes.
    31453145
    3146 Given ©typedef© and ©typeof© in \CFA, and the strong need to use the type of left-hand side in inferencing, auto type-inferencing is not supported at this time.
     3146Given �typedef� and �typeof� in \CFA, and the strong need to use the type of left-hand side in inferencing, auto type-inferencing is not supported at this time.
    31473147Should a significant need arise, this feature can be revisited.
    31483148
     
    34563456
    34573457coroutine Fibonacci {
    3458         int fn;                                                         §\C{// used for communication}§
     3458        int fn;                                                         �\C{// used for communication}�
    34593459};
    34603460void ?{}( Fibonacci * this ) {
     
    34623462}
    34633463void main( Fibonacci * this ) {
    3464         int fn1, fn2;                                           §\C{// retained between resumes}§
    3465         this->fn = 0;                                           §\C{// case 0}§
     3464        int fn1, fn2;                                           �\C{// retained between resumes}�
     3465        this->fn = 0;                                           �\C{// case 0}�
    34663466        fn1 = this->fn;
    3467         suspend();                                                      §\C{// return to last resume}§
    3468 
    3469         this->fn = 1;                                           §\C{// case 1}§
     3467        suspend();                                                      �\C{// return to last resume}�
     3468
     3469        this->fn = 1;                                           �\C{// case 1}�
    34703470        fn2 = fn1;
    34713471        fn1 = this->fn;
    3472         suspend();                                                      §\C{// return to last resume}§
    3473 
    3474         for ( ;; ) {                                            §\C{// general case}§
     3472        suspend();                                                      �\C{// return to last resume}�
     3473
     3474        for ( ;; ) {                                            �\C{// general case}�
    34753475                this->fn = fn1 + fn2;
    34763476                fn2 = fn1;
    34773477                fn1 = this->fn;
    3478                 suspend();                                              §\C{// return to last resume}§
     3478                suspend();                                              �\C{// return to last resume}�
    34793479        } // for
    34803480}
    34813481int next( Fibonacci * this ) {
    3482         resume( this );                                         §\C{// transfer to last suspend}§
     3482        resume( this );                                         �\C{// transfer to last suspend}�
    34833483        return this->fn;
    34843484}
     
    36223622#include <thread>
    36233623
    3624 thread First  { signal_once * lock; };
    3625 thread Second { signal_once * lock; };
    3626 
    3627 void ?{}( First * this, signal_once* lock ) { this->lock = lock; }
    3628 void ?{}( Second * this, signal_once* lock ) { this->lock = lock; }
    3629 
    3630 void main( First * this ) {
    3631         for ( int i = 0; i < 10; i += 1 ) {
     3624thread First  { semaphore* lock; };
     3625thread Second { semaphore* lock; };
     3626
     3627void ?{}( First * this, semaphore* lock ) { this->lock = lock; }
     3628void ?{}( Second * this, semaphore* lock ) { this->lock = lock; }
     3629
     3630void main(First* this) {
     3631        for(int i = 0; i < 10; i++) {
    36323632                sout | "First : Suspend No." | i + 1 | endl;
    36333633                yield();
    36343634        }
    3635         signal( this->lock );
    3636 }
    3637 
    3638 void main( Second * this ) {
    3639         wait( this->lock );
    3640         for ( int i = 0; i < 10; i += 1 ) {
     3635        V(this->lock);
     3636}
     3637
     3638void main(Second* this) {
     3639        P(this->lock);
     3640        for(int i = 0; i < 10; i++) {
    36413641                sout | "Second : Suspend No." | i + 1 | endl;
    36423642                yield();
     
    36443644}
    36453645
    3646 int main( void ) {
    3647         signal_once lock;
     3646
     3647int main(int argc, char* argv[]) {
     3648        semaphore lock = { 0 };
    36483649        sout | "User main begin" | endl;
    36493650        {
     
    38593860In \CFA, multiple definitions are not necessary.
    38603861Within a module, all of the module's global definitions are visible throughout the module.
    3861 For example, the following code compiles, even though ©isOdd© was not declared before being called:
     3862For example, the following code compiles, even though �isOdd� was not declared before being called:
    38623863\begin{cfa}
    38633864bool isEven(unsigned int x) {
     
    51885189
    51895190C has a number of syntax ambiguities, which are resolved by taking the longest sequence of overlapping characters that constitute a token.
    5190 For example, the program fragment ©x+++++y© is parsed as \lstinline[showspaces=true]@x ++ ++ + y@ because operator tokens ©++© and ©+© overlap.
     5191For example, the program fragment �x+++++y� is parsed as \lstinline[showspaces=true]@x ++ ++ + y@ because operator tokens �++� and �+� overlap.
    51915192Unfortunately, the longest sequence violates a constraint on increment operators, even though the parse \lstinline[showspaces=true]@x ++ + ++ y@ might yield a correct expression.
    51925193Hence, C programmers are aware that spaces have to added to disambiguate certain syntactic cases.
    51935194
    5194 In \CFA, there are ambiguous cases with dereference and operator identifiers, \eg ©int *?*?()©, where the string ©*?*?© can be interpreted as:
    5195 \begin{cfa}
    5196 *?§\color{red}\textvisiblespace§*?              §\C{// dereference operator, dereference operator}§
    5197 *§\color{red}\textvisiblespace§?*?              §\C{// dereference, multiplication operator}§
     5195In \CFA, there are ambiguous cases with dereference and operator identifiers, \eg �int *?*?()�, where the string �*?*?� can be interpreted as:
     5196\begin{cfa}
     5197*?�\color{red}\textvisiblespace�*?              �\C{// dereference operator, dereference operator}�
     5198*�\color{red}\textvisiblespace�?*?              �\C{// dereference, multiplication operator}�
    51985199\end{cfa}
    51995200By default, the first interpretation is selected, which does not yield a meaningful parse.
     
    52015202Otherwise a space is needed between the unary operator and operator identifier to disambiguate this common case.
    52025203
    5203 A similar issue occurs with the dereference, ©*?(...)©, and routine-call, ©?()(...)© identifiers.
     5204A similar issue occurs with the dereference, �*?(...)�, and routine-call, �?()(...)� identifiers.
    52045205The ambiguity occurs when the deference operator has no parameters:
    52055206\begin{cfa}
    5206 *?()§\color{red}\textvisiblespace...§ ;
    5207 *?()§\color{red}\textvisiblespace...§(...) ;
     5207*?()�\color{red}\textvisiblespace...� ;
     5208*?()�\color{red}\textvisiblespace...�(...) ;
    52085209\end{cfa}
    52095210requiring arbitrary whitespace look-ahead for the routine-call parameter-list to disambiguate.
    5210 However, the dereference operator \emph{must} have a parameter/argument to dereference ©*?(...)©.
    5211 Hence, always interpreting the string ©*?()© as \lstinline[showspaces=true]@* ?()@ does not preclude any meaningful program.
     5211However, the dereference operator \emph{must} have a parameter/argument to dereference �*?(...)�.
     5212Hence, always interpreting the string �*?()� as \lstinline[showspaces=true]@* ?()@ does not preclude any meaningful program.
    52125213
    52135214The remaining cases are with the increment/decrement operators and conditional expression, \eg:
    52145215\begin{cfa}
    5215 i++?§\color{red}\textvisiblespace...§(...);
    5216 i?++§\color{red}\textvisiblespace...§(...);
     5216i++?�\color{red}\textvisiblespace...�(...);
     5217i?++�\color{red}\textvisiblespace...�(...);
    52175218\end{cfa}
    52185219requiring arbitrary whitespace look-ahead for the operator parameter-list, even though that interpretation is an incorrect expression (juxtaposed identifiers).
    52195220Therefore, it is necessary to disambiguate these cases with a space:
    52205221\begin{cfa}
    5221 i++§\color{red}\textvisiblespace§? i : 0;
    5222 i?§\color{red}\textvisiblespace§++i : 0;
     5222i++�\color{red}\textvisiblespace�? i : 0;
     5223i?�\color{red}\textvisiblespace�++i : 0;
    52235224\end{cfa}
    52245225
     
    52305231\begin{tabular}{llll}
    52315232\begin{tabular}{@{}l@{}}
    5232 ©_AT©                   \\
    5233 ©catch©                 \\
    5234 ©catchResume©   \\
    5235 ©choose©                \\
    5236 ©coroutine©             \\
    5237 ©disable©               \\
     5233�_AT�                   \\
     5234�catch�                 \\
     5235�catchResume�   \\
     5236�choose�                \\
     5237�coroutine�             \\
     5238�disable�               \\
    52385239\end{tabular}
    52395240&
    52405241\begin{tabular}{@{}l@{}}
    5241 ©dtype©                 \\
    5242 ©enable©                \\
    5243 ©fallthrough©   \\
    5244 ©fallthru©              \\
    5245 ©finally©               \\
    5246 ©forall©                \\
     5242�dtype�                 \\
     5243�enable�                \\
     5244�fallthrough�   \\
     5245�fallthru�              \\
     5246�finally�               \\
     5247�forall�                \\
    52475248\end{tabular}
    52485249&
    52495250\begin{tabular}{@{}l@{}}
    5250 ©ftype©                 \\
    5251 ©lvalue©                \\
    5252 ©monitor©               \\
    5253 ©mutex©                 \\
    5254 ©one_t©                 \\
    5255 ©otype©                 \\
     5251�ftype�                 \\
     5252�lvalue�                \\
     5253�monitor�               \\
     5254�mutex�                 \\
     5255�one_t�                 \\
     5256�otype�                 \\
    52565257\end{tabular}
    52575258&
    52585259\begin{tabular}{@{}l@{}}
    5259 ©throw©                 \\
    5260 ©throwResume©   \\
    5261 ©trait©                 \\
    5262 ©try©                   \\
    5263 ©ttype©                 \\
    5264 ©zero_t©                \\
     5260�throw�                 \\
     5261�throwResume�   \\
     5262�trait�                 \\
     5263�try�                   \\
     5264�ttype�                 \\
     5265�zero_t�                \\
    52655266\end{tabular}
    52665267\end{tabular}
     
    52885289\begin{description}
    52895290\item[Change:] drop K\&R C declarations \\
    5290 K\&R declarations allow an implicit base-type of ©int©, if no type is specified, plus an alternate syntax for declaring parameters.
     5291K\&R declarations allow an implicit base-type of �int�, if no type is specified, plus an alternate syntax for declaring parameters.
    52915292\eg:
    52925293\begin{cfa}
    5293 x;                                                              §\C{// int x}§
    5294 *y;                                                             §\C{// int *y}§
    5295 f( p1, p2 );                                    §\C{// int f( int p1, int p2 );}§
    5296 g( p1, p2 ) int p1, p2;                 §\C{// int g( int p1, int p2 );}§
     5294x;                                                              �\C{// int x}�
     5295*y;                                                             �\C{// int *y}�
     5296f( p1, p2 );                                    �\C{// int f( int p1, int p2 );}�
     5297g( p1, p2 ) int p1, p2;                 �\C{// int g( int p1, int p2 );}�
    52975298\end{cfa}
    52985299\CFA supports K\&R routine definitions:
    52995300\begin{cfa}
    5300 f( a, b, c )                                    §\C{// default int return}§
    5301         int a, b; char c                        §\C{// K\&R parameter declarations}§
     5301f( a, b, c )                                    �\C{// default int return}�
     5302        int a, b; char c                        �\C{// K\&R parameter declarations}�
    53025303{
    53035304        ...
     
    53145315\item
    53155316\begin{description}
    5316 \item[Change:] type of character literal ©int© to ©char© to allow more intuitive overloading:
     5317\item[Change:] type of character literal �int� to �char� to allow more intuitive overloading:
    53175318\begin{cfa}
    53185319int rtn( int i );
    53195320int rtn( char c );
    5320 rtn( 'x' );                                             §\C{// programmer expects 2nd rtn to be called}§
    5321 \end{cfa}
    5322 \item[Rationale:] it is more intuitive for the call to ©rtn© to match the second version of definition of ©rtn© rather than the first.
    5323 In particular, output of ©char© variable now print a character rather than the decimal ASCII value of the character.
     5321rtn( 'x' );                                             �\C{// programmer expects 2nd rtn to be called}�
     5322\end{cfa}
     5323\item[Rationale:] it is more intuitive for the call to �rtn� to match the second version of definition of �rtn� rather than the first.
     5324In particular, output of �char� variable now print a character rather than the decimal ASCII value of the character.
    53245325\begin{cfa}
    53255326sout | 'x' | " " | (int)'x' | endl;
    53265327x 120
    53275328\end{cfa}
    5328 Having to cast ©'x'© to ©char© is non-intuitive.
     5329Having to cast �'x'� to �char� is non-intuitive.
    53295330\item[Effect on original feature:] change to semantics of well-defined feature that depend on:
    53305331\begin{cfa}
     
    53335334no long work the same in \CFA programs.
    53345335\item[Difficulty of converting:] simple
    5335 \item[How widely used:] programs that depend upon ©sizeof( 'x' )© are rare and can be changed to ©sizeof(char)©.
     5336\item[How widely used:] programs that depend upon �sizeof( 'x' )� are rare and can be changed to �sizeof(char)�.
    53365337\end{description}
    53375338
    53385339\item
    53395340\begin{description}
    5340 \item[Change:] make string literals ©const©:
    5341 \begin{cfa}
    5342 char * p = "abc";                               §\C{// valid in C, deprecated in \CFA}§
    5343 char * q = expr ? "abc" : "de"; §\C{// valid in C, invalid in \CFA}§
    5344 \end{cfa}
    5345 The type of a string literal is changed from ©[] char© to ©const [] char©.
    5346 Similarly, the type of a wide string literal is changed from ©[] wchar_t© to ©const [] wchar_t©.
     5341\item[Change:] make string literals �const�:
     5342\begin{cfa}
     5343char * p = "abc";                               �\C{// valid in C, deprecated in \CFA}�
     5344char * q = expr ? "abc" : "de"; �\C{// valid in C, invalid in \CFA}�
     5345\end{cfa}
     5346The type of a string literal is changed from �[] char� to �const [] char�.
     5347Similarly, the type of a wide string literal is changed from �[] wchar_t� to �const [] wchar_t�.
    53475348\item[Rationale:] This change is a safety issue:
    53485349\begin{cfa}
    53495350char * p = "abc";
    5350 p[0] = 'w';                                             §\C{// segment fault or change constant literal}§
     5351p[0] = 'w';                                             �\C{// segment fault or change constant literal}�
    53515352\end{cfa}
    53525353The same problem occurs when passing a string literal to a routine that changes its argument.
    53535354\item[Effect on original feature:] change to semantics of well-defined feature.
    5354 \item[Difficulty of converting:] simple syntactic transformation, because string literals can be converted to ©char *©.
     5355\item[Difficulty of converting:] simple syntactic transformation, because string literals can be converted to �char *�.
    53555356\item[How widely used:] programs that have a legitimate reason to treat string literals as pointers to potentially modifiable memory are rare.
    53565357\end{description}
     
    53605361\item[Change:] remove \newterm{tentative definitions}, which only occurs at file scope:
    53615362\begin{cfa}
    5362 int i;                                                  §\C{// forward definition}§
    5363 int *j = ®&i®;                                  §\C{// forward reference, valid in C, invalid in \CFA}§
    5364 int i = 0;                                              §\C{// definition}§
     5363int i;                                                  �\C{// forward definition}�
     5364int *j = �&i�;                                  �\C{// forward reference, valid in C, invalid in \CFA}�
     5365int i = 0;                                              �\C{// definition}�
    53655366\end{cfa}
    53665367is valid in C, and invalid in \CFA because duplicate overloaded object definitions at the same scope level are disallowed.
     
    53685369\begin{cfa}
    53695370struct X { int i; struct X *next; };
    5370 static struct X a;                              §\C{// forward definition}§
    5371 static struct X b = { 0, ®&a® };        §\C{// forward reference, valid in C, invalid in \CFA}§
    5372 static struct X a = { 1, &b };  §\C{// definition}§
     5371static struct X a;                              �\C{// forward definition}�
     5372static struct X b = { 0, �&a� };        �\C{// forward reference, valid in C, invalid in \CFA}�
     5373static struct X a = { 1, &b };  �\C{// definition}�
    53735374\end{cfa}
    53745375\item[Rationale:] avoids having different initialization rules for builtin types and user-defined types.
     
    53805381\item
    53815382\begin{description}
    5382 \item[Change:] have ©struct© introduce a scope for nested types:
    5383 \begin{cfa}
    5384 enum ®Colour® { R, G, B, Y, C, M };
     5383\item[Change:] have �struct� introduce a scope for nested types:
     5384\begin{cfa}
     5385enum �Colour� { R, G, B, Y, C, M };
    53855386struct Person {
    5386         enum ®Colour® { R, G, B };      §\C{// nested type}§
    5387         struct Face {                           §\C{// nested type}§
    5388                 ®Colour® Eyes, Hair;    §\C{// type defined outside (1 level)}§
     5387        enum �Colour� { R, G, B };      �\C{// nested type}�
     5388        struct Face {                           �\C{// nested type}�
     5389                �Colour� Eyes, Hair;    �\C{// type defined outside (1 level)}�
    53895390        };
    5390         ®.Colour® shirt;                        §\C{// type defined outside (top level)}§
    5391         ®Colour® pants;                         §\C{// type defined same level}§
    5392         Face looks[10];                         §\C{// type defined same level}§
     5391        �.Colour� shirt;                        �\C{// type defined outside (top level)}�
     5392        �Colour� pants;                         �\C{// type defined same level}�
     5393        Face looks[10];                         �\C{// type defined same level}�
    53935394};
    5394 ®Colour® c = R;                                 §\C{// type/enum defined same level}§
    5395 Person®.Colour® pc = Person®.®R;        §\C{// type/enum defined inside}§
    5396 Person®.®Face pretty;                   §\C{// type defined inside}§
     5395�Colour� c = R;                                 �\C{// type/enum defined same level}�
     5396Person�.Colour� pc = Person�.�R;        �\C{// type/enum defined inside}�
     5397Person�.�Face pretty;                   �\C{// type defined inside}�
    53975398\end{cfa}
    53985399In C, the name of the nested types belongs to the same scope as the name of the outermost enclosing structure, \ie the nested types are hoisted to the scope of the outer-most type, which is not useful and confusing.
    53995400\CFA is C \emph{incompatible} on this issue, and provides semantics similar to \Index*[C++]{\CC{}}.
    5400 Nested types are not hoisted and can be referenced using the field selection operator ``©.©'', unlike the \CC scope-resolution operator ``©::©''.
    5401 \item[Rationale:] ©struct© scope is crucial to \CFA as an information structuring and hiding mechanism.
     5401Nested types are not hoisted and can be referenced using the field selection operator ``�.�'', unlike the \CC scope-resolution operator ``�::�''.
     5402\item[Rationale:] �struct� scope is crucial to \CFA as an information structuring and hiding mechanism.
    54025403\item[Effect on original feature:] change to semantics of well-defined feature.
    54035404\item[Difficulty of converting:] Semantic transformation.
     
    54115412\item[Difficulty of converting:] Semantic transformation. To make the struct type name visible in the scope of the enclosing struct, the struct tag could be declared in the scope of the enclosing struct, before the enclosing struct is defined. Example:
    54125413\begin{cfa}
    5413 struct Y;                                               §\C{// struct Y and struct X are at the same scope}§
     5414struct Y;                                               �\C{// struct Y and struct X are at the same scope}�
    54145415struct X {
    54155416        struct Y { /* ... */ } y;
     
    54245425\begin{description}
    54255426\item[Change:] comma expression is disallowed as subscript
    5426 \item[Rationale:] safety issue to prevent subscripting error for multidimensional arrays: ©x[i,j]© instead of ©x[i][j]©, and this syntactic form then taken by \CFA for new style arrays.
     5427\item[Rationale:] safety issue to prevent subscripting error for multidimensional arrays: �x[i,j]� instead of �x[i][j]�, and this syntactic form then taken by \CFA for new style arrays.
    54275428\item[Effect on original feature:] change to semantics of well-defined feature.
    5428 \item[Difficulty of converting:] semantic transformation of ©x[i,j]© to ©x[(i,j)]©
     5429\item[Difficulty of converting:] semantic transformation of �x[i,j]� to �x[(i,j)]�
    54295430\item[How widely used:] seldom.
    54305431\end{description}
     
    54975498\end{tabular}
    54985499\end{quote2}
    5499 For the prescribed head-files, \CFA uses header interposition to wraps these includes in an ©extern "C"©;
     5500For the prescribed head-files, \CFA uses header interposition to wraps these includes in an �extern "C"�;
    55005501hence, names in these include files are not mangled\index{mangling!name} (see~\VRef{s:Interoperability}).
    5501 All other C header files must be explicitly wrapped in ©extern "C"© to prevent name mangling.
    5502 For \Index*[C++]{\CC{}}, the name-mangling issue is handled implicitly because most C header-files are augmented with checks for preprocessor variable ©__cplusplus©, which adds appropriate ©extern "C"© qualifiers.
     5502All other C header files must be explicitly wrapped in �extern "C"� to prevent name mangling.
     5503For \Index*[C++]{\CC{}}, the name-mangling issue is handled implicitly because most C header-files are augmented with checks for preprocessor variable �__cplusplus�, which adds appropriate �extern "C"� qualifiers.
    55035504
    55045505
     
    55325533                &                                       & \multicolumn{1}{c|}{fill}     & resize        & alignment     & array \\
    55335534\hline
    5534 C               & ©malloc©                      & no                    & no            & no            & no    \\
    5535                 & ©calloc©                      & yes (0 only)  & no            & no            & yes   \\
    5536                 & ©realloc©                     & no/copy               & yes           & no            & no    \\
    5537                 & ©memalign©            & no                    & no            & yes           & no    \\
    5538                 & ©posix_memalign©      & no                    & no            & yes           & no    \\
    5539 C11             & ©aligned_alloc©       & no                    & no            & yes           & no    \\
    5540 \CFA    & ©alloc©                       & no/copy/yes   & no/yes        & no            & yes   \\
    5541                 & ©align_alloc©         & no/yes                & no            & yes           & yes   \\
     5535C               & �malloc�                      & no                    & no            & no            & no    \\
     5536                & �calloc�                      & yes (0 only)  & no            & no            & yes   \\
     5537                & �realloc�                     & no/copy               & yes           & no            & no    \\
     5538                & �memalign�            & no                    & no            & yes           & no    \\
     5539                & �posix_memalign�      & no                    & no            & yes           & no    \\
     5540C11             & �aligned_alloc�       & no                    & no            & yes           & no    \\
     5541\CFA    & �alloc�                       & no/copy/yes   & no/yes        & no            & yes   \\
     5542                & �align_alloc�         & no/yes                & no            & yes           & yes   \\
    55425543\end{tabular}
    55435544\end{center}
    5544 It is impossible to resize with alignment because the underlying ©realloc© allocates storage if more space is needed, and it does not honour alignment from the original allocation.
     5545It is impossible to resize with alignment because the underlying �realloc� allocates storage if more space is needed, and it does not honour alignment from the original allocation.
    55455546
    55465547\leavevmode
     
    55485549// C unsafe allocation
    55495550extern "C" {
    5550 void * mallac( size_t size );§\indexc{memset}§
    5551 void * calloc( size_t dim, size_t size );§\indexc{calloc}§
    5552 void * realloc( void * ptr, size_t size );§\indexc{realloc}§
    5553 void * memalign( size_t align, size_t size );§\indexc{memalign}§
    5554 int posix_memalign( void ** ptr, size_t align, size_t size );§\indexc{posix_memalign}§
    5555 }
    5556 
    5557 // §\CFA§ safe equivalents, i.e., implicit size specification
     5551void * mallac( size_t size );�\indexc{memset}�
     5552void * calloc( size_t dim, size_t size );�\indexc{calloc}�
     5553void * realloc( void * ptr, size_t size );�\indexc{realloc}�
     5554void * memalign( size_t align, size_t size );�\indexc{memalign}�
     5555int posix_memalign( void ** ptr, size_t align, size_t size );�\indexc{posix_memalign}�
     5556}
     5557
     5558// �\CFA� safe equivalents, i.e., implicit size specification
    55585559forall( dtype T | sized(T) ) T * malloc( void );
    55595560forall( dtype T | sized(T) ) T * calloc( size_t dim );
     
    55635564forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t align );
    55645565
    5565 // §\CFA§ safe general allocation, fill, resize, array
    5566 forall( dtype T | sized(T) ) T * alloc( void );§\indexc{alloc}§
     5566// �\CFA� safe general allocation, fill, resize, array
     5567forall( dtype T | sized(T) ) T * alloc( void );�\indexc{alloc}�
    55675568forall( dtype T | sized(T) ) T * alloc( char fill );
    55685569forall( dtype T | sized(T) ) T * alloc( size_t dim );
     
    55715572forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill );
    55725573
    5573 // §\CFA§ safe general allocation, align, fill, array
     5574// �\CFA� safe general allocation, align, fill, array
    55745575forall( dtype T | sized(T) ) T * align_alloc( size_t align );
    55755576forall( dtype T | sized(T) ) T * align_alloc( size_t align, char fill );
     
    55835584}
    55845585
    5585 // §\CFA§ safe initialization/copy, i.e., implicit size specification
    5586 forall( dtype T | sized(T) ) T * memset( T * dest, char c );§\indexc{memset}§
    5587 forall( dtype T | sized(T) ) T * memcpy( T * dest, const T * src );§\indexc{memcpy}§
    5588 
    5589 // §\CFA§ safe initialization/copy array
     5586// �\CFA� safe initialization/copy, i.e., implicit size specification
     5587forall( dtype T | sized(T) ) T * memset( T * dest, char c );�\indexc{memset}�
     5588forall( dtype T | sized(T) ) T * memcpy( T * dest, const T * src );�\indexc{memcpy}�
     5589
     5590// �\CFA� safe initialization/copy array
    55905591forall( dtype T | sized(T) ) T * memset( T dest[], size_t dim, char c );
    55915592forall( dtype T | sized(T) ) T * memcpy( T dest[], const T src[], size_t dim );
    55925593
    5593 // §\CFA§ allocation/deallocation and constructor/destructor
    5594 forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } ) T * new( Params p );§\indexc{new}§
    5595 forall( dtype T | { void ^?{}( T * ); } ) void delete( T * ptr );§\indexc{delete}§
     5594// �\CFA� allocation/deallocation and constructor/destructor
     5595forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } ) T * new( Params p );�\indexc{new}�
     5596forall( dtype T | { void ^?{}( T * ); } ) void delete( T * ptr );�\indexc{delete}�
    55965597forall( dtype T, ttype Params | { void ^?{}( T * ); void delete( Params ); } )
    55975598  void delete( T * ptr, Params rest );
    55985599
    5599 // §\CFA§ allocation/deallocation and constructor/destructor, array
    5600 forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } ) T * anew( size_t dim, Params p );§\indexc{anew}§
    5601 forall( dtype T | sized(T) | { void ^?{}( T * ); } ) void adelete( size_t dim, T arr[] );§\indexc{adelete}§
     5600// �\CFA� allocation/deallocation and constructor/destructor, array
     5601forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } ) T * anew( size_t dim, Params p );�\indexc{anew}�
     5602forall( dtype T | sized(T) | { void ^?{}( T * ); } ) void adelete( size_t dim, T arr[] );�\indexc{adelete}�
    56025603forall( dtype T | sized(T) | { void ^?{}( T * ); }, ttype Params | { void adelete( Params ); } )
    56035604  void adelete( size_t dim, T arr[], Params rest );
     
    56095610\leavevmode
    56105611\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    5611 int ato( const char * ptr );§\indexc{ato}§
     5612int ato( const char * ptr );�\indexc{ato}�
    56125613unsigned int ato( const char * ptr );
    56135614long int ato( const char * ptr );
     
    56415642\leavevmode
    56425643\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    5643 forall( otype T | { int ?<?( T, T ); } )        §\C{// location}§
    5644 T * bsearch( T key, const T * arr, size_t dim );§\indexc{bsearch}§
    5645 
    5646 forall( otype T | { int ?<?( T, T ); } )        §\C{// position}§
     5644forall( otype T | { int ?<?( T, T ); } )        �\C{// location}�
     5645T * bsearch( T key, const T * arr, size_t dim );�\indexc{bsearch}�
     5646
     5647forall( otype T | { int ?<?( T, T ); } )        �\C{// position}�
    56475648unsigned int bsearch( T key, const T * arr, size_t dim );
    56485649
    56495650forall( otype T | { int ?<?( T, T ); } )
    5650 void qsort( const T * arr, size_t dim );§\indexc{qsort}§
     5651void qsort( const T * arr, size_t dim );�\indexc{qsort}�
    56515652\end{cfa}
    56525653
     
    56565657\leavevmode
    56575658\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    5658 unsigned char abs( signed char );§\indexc{abs}§
     5659unsigned char abs( signed char );�\indexc{abs}�
    56595660int abs( int );
    56605661unsigned long int abs( long int );
     
    56755676\leavevmode
    56765677\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    5677 void rand48seed( long int s );§\indexc{rand48seed}§
    5678 char rand48();§\indexc{rand48}§
     5678void rand48seed( long int s );�\indexc{rand48seed}�
     5679char rand48();�\indexc{rand48}�
    56795680int rand48();
    56805681unsigned int rand48();
     
    56935694\leavevmode
    56945695\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    5695 forall( otype T | { int ?<?( T, T ); } ) T min( T t1, T t2 );§\indexc{min}§
    5696 forall( otype T | { int ?>?( T, T ); } ) T max( T t1, T t2 );§\indexc{max}§
    5697 forall( otype T | { T min( T, T ); T max( T, T ); } ) T clamp( T value, T min_val, T max_val );§\indexc{clamp}§
    5698 forall( otype T ) void swap( T * t1, T * t2 );§\indexc{swap}§
     5696forall( otype T | { int ?<?( T, T ); } ) T min( T t1, T t2 );�\indexc{min}�
     5697forall( otype T | { int ?>?( T, T ); } ) T max( T t1, T t2 );�\indexc{max}�
     5698forall( otype T | { T min( T, T ); T max( T, T ); } ) T clamp( T value, T min_val, T max_val );�\indexc{clamp}�
     5699forall( otype T ) void swap( T * t1, T * t2 );�\indexc{swap}�
    56995700\end{cfa}
    57005701
     
    57105711\leavevmode
    57115712\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    5712 float ?%?( float, float );§\indexc{fmod}§
     5713float ?%?( float, float );�\indexc{fmod}�
    57135714float fmod( float, float );
    57145715double ?%?( double, double );
     
    57175718long double fmod( long double, long double );
    57185719
    5719 float remainder( float, float );§\indexc{remainder}§
     5720float remainder( float, float );�\indexc{remainder}�
    57205721double remainder( double, double );
    57215722long double remainder( long double, long double );
    57225723
    5723 [ int, float ] remquo( float, float );§\indexc{remquo}§
     5724[ int, float ] remquo( float, float );�\indexc{remquo}�
    57245725float remquo( float, float, int * );
    57255726[ int, double ] remquo( double, double );
     
    57295730
    57305731[ int, float ] div( float, float );                                             // alternative name for remquo
    5731 float div( float, float, int * );§\indexc{div}§
     5732float div( float, float, int * );�\indexc{div}�
    57325733[ int, double ] div( double, double );
    57335734double div( double, double, int * );
     
    57355736long double div( long double, long double, int * );
    57365737
    5737 float fma( float, float, float );§\indexc{fma}§
     5738float fma( float, float, float );�\indexc{fma}�
    57385739double fma( double, double, double );
    57395740long double fma( long double, long double, long double );
    57405741
    5741 float fdim( float, float );§\indexc{fdim}§
     5742float fdim( float, float );�\indexc{fdim}�
    57425743double fdim( double, double );
    57435744long double fdim( long double, long double );
    57445745
    5745 float nan( const char * );§\indexc{nan}§
     5746float nan( const char * );�\indexc{nan}�
    57465747double nan( const char * );
    57475748long double nan( const char * );
     
    57535754\leavevmode
    57545755\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    5755 float exp( float );§\indexc{exp}§
     5756float exp( float );�\indexc{exp}�
    57565757double exp( double );
    57575758long double exp( long double );
     
    57605761long double _Complex exp( long double _Complex );
    57615762
    5762 float exp2( float );§\indexc{exp2}§
     5763float exp2( float );�\indexc{exp2}�
    57635764double exp2( double );
    57645765long double exp2( long double );
     
    57675768long double _Complex exp2( long double _Complex );
    57685769
    5769 float expm1( float );§\indexc{expm1}§
     5770float expm1( float );�\indexc{expm1}�
    57705771double expm1( double );
    57715772long double expm1( long double );
    57725773
    5773 float log( float );§\indexc{log}§
     5774float log( float );�\indexc{log}�
    57745775double log( double );
    57755776long double log( long double );
     
    57785779long double _Complex log( long double _Complex );
    57795780
    5780 float log2( float );§\indexc{log2}§
     5781float log2( float );�\indexc{log2}�
    57815782double log2( double );
    57825783long double log2( long double );
     
    57855786long double _Complex log2( long double _Complex );
    57865787
    5787 float log10( float );§\indexc{log10}§
     5788float log10( float );�\indexc{log10}�
    57885789double log10( double );
    57895790long double log10( long double );
     
    57925793long double _Complex log10( long double _Complex );
    57935794
    5794 float log1p( float );§\indexc{log1p}§
     5795float log1p( float );�\indexc{log1p}�
    57955796double log1p( double );
    57965797long double log1p( long double );
    57975798
    5798 int ilogb( float );§\indexc{ilogb}§
     5799int ilogb( float );�\indexc{ilogb}�
    57995800int ilogb( double );
    58005801int ilogb( long double );
    58015802
    5802 float logb( float );§\indexc{logb}§
     5803float logb( float );�\indexc{logb}�
    58035804double logb( double );
    58045805long double logb( long double );
     
    58105811\leavevmode
    58115812\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    5812 float sqrt( float );§\indexc{sqrt}§
     5813float sqrt( float );�\indexc{sqrt}�
    58135814double sqrt( double );
    58145815long double sqrt( long double );
     
    58175818long double _Complex sqrt( long double _Complex );
    58185819
    5819 float cbrt( float );§\indexc{cbrt}§
     5820float cbrt( float );�\indexc{cbrt}�
    58205821double cbrt( double );
    58215822long double cbrt( long double );
    58225823
    5823 float hypot( float, float );§\indexc{hypot}§
     5824float hypot( float, float );�\indexc{hypot}�
    58245825double hypot( double, double );
    58255826long double hypot( long double, long double );
    58265827
    5827 float pow( float, float );§\indexc{pow}§
     5828float pow( float, float );�\indexc{pow}�
    58285829double pow( double, double );
    58295830long double pow( long double, long double );
     
    58385839\leavevmode
    58395840\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    5840 float sin( float );§\indexc{sin}§
     5841float sin( float );�\indexc{sin}�
    58415842double sin( double );
    58425843long double sin( long double );
     
    58455846long double _Complex sin( long double _Complex );
    58465847
    5847 float cos( float );§\indexc{cos}§
     5848float cos( float );�\indexc{cos}�
    58485849double cos( double );
    58495850long double cos( long double );
     
    58525853long double _Complex cos( long double _Complex );
    58535854
    5854 float tan( float );§\indexc{tan}§
     5855float tan( float );�\indexc{tan}�
    58555856double tan( double );
    58565857long double tan( long double );
     
    58595860long double _Complex tan( long double _Complex );
    58605861
    5861 float asin( float );§\indexc{asin}§
     5862float asin( float );�\indexc{asin}�
    58625863double asin( double );
    58635864long double asin( long double );
     
    58665867long double _Complex asin( long double _Complex );
    58675868
    5868 float acos( float );§\indexc{acos}§
     5869float acos( float );�\indexc{acos}�
    58695870double acos( double );
    58705871long double acos( long double );
     
    58735874long double _Complex acos( long double _Complex );
    58745875
    5875 float atan( float );§\indexc{atan}§
     5876float atan( float );�\indexc{atan}�
    58765877double atan( double );
    58775878long double atan( long double );
     
    58805881long double _Complex atan( long double _Complex );
    58815882
    5882 float atan2( float, float );§\indexc{atan2}§
     5883float atan2( float, float );�\indexc{atan2}�
    58835884double atan2( double, double );
    58845885long double atan2( long double, long double );
    58855886
    58865887float atan( float, float );                                                             // alternative name for atan2
    5887 double atan( double, double );§\indexc{atan}§
     5888double atan( double, double );�\indexc{atan}�
    58885889long double atan( long double, long double );
    58895890\end{cfa}
     
    58945895\leavevmode
    58955896\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    5896 float sinh( float );§\indexc{sinh}§
     5897float sinh( float );�\indexc{sinh}�
    58975898double sinh( double );
    58985899long double sinh( long double );
     
    59015902long double _Complex sinh( long double _Complex );
    59025903
    5903 float cosh( float );§\indexc{cosh}§
     5904float cosh( float );�\indexc{cosh}�
    59045905double cosh( double );
    59055906long double cosh( long double );
     
    59085909long double _Complex cosh( long double _Complex );
    59095910
    5910 float tanh( float );§\indexc{tanh}§
     5911float tanh( float );�\indexc{tanh}�
    59115912double tanh( double );
    59125913long double tanh( long double );
     
    59155916long double _Complex tanh( long double _Complex );
    59165917
    5917 float asinh( float );§\indexc{asinh}§
     5918float asinh( float );�\indexc{asinh}�
    59185919double asinh( double );
    59195920long double asinh( long double );
     
    59225923long double _Complex asinh( long double _Complex );
    59235924
    5924 float acosh( float );§\indexc{acosh}§
     5925float acosh( float );�\indexc{acosh}�
    59255926double acosh( double );
    59265927long double acosh( long double );
     
    59295930long double _Complex acosh( long double _Complex );
    59305931
    5931 float atanh( float );§\indexc{atanh}§
     5932float atanh( float );�\indexc{atanh}�
    59325933double atanh( double );
    59335934long double atanh( long double );
     
    59425943\leavevmode
    59435944\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    5944 float erf( float );§\indexc{erf}§
     5945float erf( float );�\indexc{erf}�
    59455946double erf( double );
    59465947long double erf( long double );
     
    59495950long double _Complex erf( long double _Complex );
    59505951
    5951 float erfc( float );§\indexc{erfc}§
     5952float erfc( float );�\indexc{erfc}�
    59525953double erfc( double );
    59535954long double erfc( long double );
     
    59565957long double _Complex erfc( long double _Complex );
    59575958
    5958 float lgamma( float );§\indexc{lgamma}§
     5959float lgamma( float );�\indexc{lgamma}�
    59595960double lgamma( double );
    59605961long double lgamma( long double );
     
    59635964long double lgamma( long double, int * );
    59645965
    5965 float tgamma( float );§\indexc{tgamma}§
     5966float tgamma( float );�\indexc{tgamma}�
    59665967double tgamma( double );
    59675968long double tgamma( long double );
     
    59735974\leavevmode
    59745975\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    5975 float floor( float );§\indexc{floor}§
     5976float floor( float );�\indexc{floor}�
    59765977double floor( double );
    59775978long double floor( long double );
    59785979
    5979 float ceil( float );§\indexc{ceil}§
     5980float ceil( float );�\indexc{ceil}�
    59805981double ceil( double );
    59815982long double ceil( long double );
    59825983
    5983 float trunc( float );§\indexc{trunc}§
     5984float trunc( float );�\indexc{trunc}�
    59845985double trunc( double );
    59855986long double trunc( long double );
    59865987
    5987 float rint( float );§\indexc{rint}§
     5988float rint( float );�\indexc{rint}�
    59885989long double rint( long double );
    59895990long int rint( float );
     
    59945995long long int rint( long double );
    59955996
    5996 long int lrint( float );§\indexc{lrint}§
     5997long int lrint( float );�\indexc{lrint}�
    59975998long int lrint( double );
    59985999long int lrint( long double );
     
    60016002long long int llrint( long double );
    60026003
    6003 float nearbyint( float );§\indexc{nearbyint}§
     6004float nearbyint( float );�\indexc{nearbyint}�
    60046005double nearbyint( double );
    60056006long double nearbyint( long double );
    60066007
    6007 float round( float );§\indexc{round}§
     6008float round( float );�\indexc{round}�
    60086009long double round( long double );
    60096010long int round( float );
     
    60146015long long int round( long double );
    60156016
    6016 long int lround( float );§\indexc{lround}§
     6017long int lround( float );�\indexc{lround}�
    60176018long int lround( double );
    60186019long int lround( long double );
     
    60276028\leavevmode
    60286029\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    6029 float copysign( float, float );§\indexc{copysign}§
     6030float copysign( float, float );�\indexc{copysign}�
    60306031double copysign( double, double );
    60316032long double copysign( long double, long double );
    60326033
    6033 float frexp( float, int * );§\indexc{frexp}§
     6034float frexp( float, int * );�\indexc{frexp}�
    60346035double frexp( double, int * );
    60356036long double frexp( long double, int * );
    60366037
    6037 float ldexp( float, int );§\indexc{ldexp}§
     6038float ldexp( float, int );�\indexc{ldexp}�
    60386039double ldexp( double, int );
    60396040long double ldexp( long double, int );
    60406041
    6041 [ float, float ] modf( float );§\indexc{modf}§
     6042[ float, float ] modf( float );�\indexc{modf}�
    60426043float modf( float, float * );
    60436044[ double, double ] modf( double );
     
    60466047long double modf( long double, long double * );
    60476048
    6048 float nextafter( float, float );§\indexc{nextafter}§
     6049float nextafter( float, float );�\indexc{nextafter}�
    60496050double nextafter( double, double );
    60506051long double nextafter( long double, long double );
    60516052
    6052 float nexttoward( float, long double );§\indexc{nexttoward}§
     6053float nexttoward( float, long double );�\indexc{nexttoward}�
    60536054double nexttoward( double, long double );
    60546055long double nexttoward( long double, long double );
    60556056
    6056 float scalbn( float, int );§\indexc{scalbn}§
     6057float scalbn( float, int );�\indexc{scalbn}�
    60576058double scalbn( double, int );
    60586059long double scalbn( long double, int );
    60596060
    6060 float scalbln( float, long int );§\indexc{scalbln}§
     6061float scalbln( float, long int );�\indexc{scalbln}�
    60616062double scalbln( double, long int );
    60626063long double scalbln( long double, long int );
     
    60726073
    60736074\begin{cfa}
    6074 void ?{}( Int * this );                                 §\C{// constructor}§
     6075void ?{}( Int * this );                                 �\C{// constructor}�
    60756076void ?{}( Int * this, Int init );
    60766077void ?{}( Int * this, zero_t );
     
    60816082void ^?{}( Int * this );
    60826083
    6083 Int ?=?( Int * lhs, Int rhs );                  §\C{// assignment}§
     6084Int ?=?( Int * lhs, Int rhs );                  �\C{// assignment}�
    60846085Int ?=?( Int * lhs, long int rhs );
    60856086Int ?=?( Int * lhs, unsigned long int rhs );
     
    60986099unsigned long int narrow( Int val );
    60996100
    6100 int ?==?( Int oper1, Int oper2 );               §\C{// comparison}§
     6101int ?==?( Int oper1, Int oper2 );               �\C{// comparison}�
    61016102int ?==?( Int oper1, long int oper2 );
    61026103int ?==?( long int oper2, Int oper1 );
     
    61346135int ?>=?( unsigned long int oper1, Int oper2 );
    61356136
    6136 Int +?( Int oper );                                             §\C{// arithmetic}§
     6137Int +?( Int oper );                                             �\C{// arithmetic}�
    61376138Int -?( Int oper );
    61386139Int ~?( Int oper );
     
    62166217Int ?>>=?( Int * lhs, mp_bitcnt_t shift );
    62176218
    6218 Int abs( Int oper );                                    §\C{// number functions}§
     6219Int abs( Int oper );                                    �\C{// number functions}�
    62196220Int fact( unsigned long int N );
    62206221Int gcd( Int oper1, Int oper2 );
     
    62286229Int sqrt( Int oper );
    62296230
    6230 forall( dtype istype | istream( istype ) ) istype * ?|?( istype * is, Int * mp );  §\C{// I/O}§
     6231forall( dtype istype | istream( istype ) ) istype * ?|?( istype * is, Int * mp );  �\C{// I/O}�
    62316232forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype * os, Int mp );
    62326233\end{cfa}
     
    62396240\hline
    62406241\begin{cfa}
    6241 #include <gmp>§\indexc{gmp}§
     6242#include <gmp>�\indexc{gmp}�
    62426243int main( void ) {
    62436244        sout | "Factorial Numbers" | endl;
     
    62536254&
    62546255\begin{cfa}
    6255 #include <gmp.h>§\indexc{gmp.h}§
     6256#include <gmp.h>�\indexc{gmp.h}�
    62566257int main( void ) {
    6257         ®gmp_printf®( "Factorial Numbers\n" );
    6258         ®mpz_t® fact;
    6259         ®mpz_init_set_ui®( fact, 1 );
    6260         ®gmp_printf®( "%d %Zd\n", 0, fact );
     6258        �gmp_printf�( "Factorial Numbers\n" );
     6259        �mpz_t� fact;
     6260        �mpz_init_set_ui�( fact, 1 );
     6261        �gmp_printf�( "%d %Zd\n", 0, fact );
    62616262        for ( unsigned int i = 1; i <= 40; i += 1 ) {
    6262                 ®mpz_mul_ui®( fact, fact, i );
    6263                 ®gmp_printf®( "%d %Zd\n", i, fact );
     6263                �mpz_mul_ui�( fact, fact, i );
     6264                �gmp_printf�( "%d %Zd\n", i, fact );
    62646265        }
    62656266}
     
    63266327\begin{cfa}[belowskip=0pt]
    63276328// implementation
    6328 struct Rational {§\indexc{Rational}§
     6329struct Rational {�\indexc{Rational}�
    63296330        long int numerator, denominator;                                        // invariant: denominator > 0
    63306331}; // Rational
    63316332
    6332 Rational rational();                                    §\C{// constructors}§
     6333Rational rational();                                    �\C{// constructors}�
    63336334Rational rational( long int n );
    63346335Rational rational( long int n, long int d );
     
    63366337void ?{}( Rational * r, one_t );
    63376338
    6338 long int numerator( Rational r );               §\C{// numerator/denominator getter/setter}§
     6339long int numerator( Rational r );               �\C{// numerator/denominator getter/setter}�
    63396340long int numerator( Rational r, long int n );
    63406341long int denominator( Rational r );
    63416342long int denominator( Rational r, long int d );
    63426343
    6343 int ?==?( Rational l, Rational r );             §\C{// comparison}§
     6344int ?==?( Rational l, Rational r );             �\C{// comparison}�
    63446345int ?!=?( Rational l, Rational r );
    63456346int ?<?( Rational l, Rational r );
     
    63486349int ?>=?( Rational l, Rational r );
    63496350
    6350 Rational -?( Rational r );                              §\C{// arithmetic}§
     6351Rational -?( Rational r );                              �\C{// arithmetic}�
    63516352Rational ?+?( Rational l, Rational r );
    63526353Rational ?-?( Rational l, Rational r );
     
    63546355Rational ?/?( Rational l, Rational r );
    63556356
    6356 double widen( Rational r );                             §\C{// conversion}§
     6357double widen( Rational r );                             �\C{// conversion}�
    63576358Rational narrow( double f, long int md );
    63586359
  • src/benchmark/CorCtxSwitch.c

    r9ff56e7 r3873b5a1  
    3131
    3232        StartTime = Time();
    33         // for ( volatile unsigned int i = 0; i < NoOfTimes; i += 1 ) {
    34         //      resume( this_coroutine() );
    35         //      // resume( &s );       
    36         // }
    3733        resumer( &s, NoOfTimes );
    3834        EndTime = Time();
  • src/benchmark/csv-data.c

    r9ff56e7 r3873b5a1  
    3838
    3939        StartTime = Time();
    40         // for ( volatile unsigned int i = 0; i < NoOfTimes; i += 1 ) {
    41         //      resume( this_coroutine() );
    42         //      // resume( &s );
    43         // }
    4440        resumer( &s, NoOfTimes );
    4541        EndTime = Time();
  • src/libcfa/concurrency/alarm.c

    r9ff56e7 r3873b5a1  
    1616
    1717extern "C" {
     18#include <errno.h>
     19#include <stdio.h>
     20#include <string.h>
    1821#include <time.h>
     22#include <unistd.h>
    1923#include <sys/time.h>
    2024}
     25
     26#include "libhdr.h"
    2127
    2228#include "alarm.h"
     
    3137        timespec curr;
    3238        clock_gettime( CLOCK_REALTIME, &curr );
    33         return ((__cfa_time_t)curr.tv_sec * TIMEGRAN) + curr.tv_nsec;
     39        __cfa_time_t curr_time = ((__cfa_time_t)curr.tv_sec * TIMEGRAN) + curr.tv_nsec;
     40        // LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Kernel : current time is %lu\n", curr_time );
     41        return curr_time;
    3442}
    3543
    3644void __kernel_set_timer( __cfa_time_t alarm ) {
     45        LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Kernel : set timer to %lu\n", (__cfa_time_t)alarm );
    3746        itimerval val;
    3847        val.it_value.tv_sec = alarm / TIMEGRAN;                 // seconds
     
    7180}
    7281
     82LIB_DEBUG_DO( bool validate( alarm_list_t * this ) {
     83        alarm_node_t ** it = &this->head;
     84        while( (*it) ) {
     85                it = &(*it)->next;
     86        }
     87
     88        return it == this->tail;
     89})
     90
    7391static inline void insert_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t p ) {
    74         assert( !n->next );
     92        verify( !n->next );
    7593        if( p == this->tail ) {
    7694                this->tail = &n->next;
     
    8098        }
    8199        *p = n;
     100
     101        verify( validate( this ) );
    82102}
    83103
     
    89109
    90110        insert_at( this, n, it );
     111
     112        verify( validate( this ) );
    91113}
    92114
     
    100122                head->next = NULL;
    101123        }
     124        verify( validate( this ) );
    102125        return head;
    103126}
     
    105128static inline void remove_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t it ) {
    106129        verify( it );
    107         verify( (*it)->next == n );
     130        verify( (*it) == n );
    108131
    109         (*it)->next = n->next;
     132        (*it) = n->next;
    110133        if( !n-> next ) {
    111134                this->tail = it;
    112135        }
    113136        n->next = NULL;
     137
     138        verify( validate( this ) );
    114139}
    115140
    116141static inline void remove( alarm_list_t * this, alarm_node_t * n ) {
    117142        alarm_node_t ** it = &this->head;
    118         while( (*it) && (*it)->next != n ) {
     143        while( (*it) && (*it) != n ) {
    119144                it = &(*it)->next;
    120145        }
    121146
     147        verify( validate( this ) );
     148
    122149        if( *it ) { remove_at( this, n, it ); }
     150
     151        verify( validate( this ) );
    123152}
    124153
    125154void register_self( alarm_node_t * this ) {
    126155        disable_interrupts();
    127         assert( !systemProcessor->pending_alarm );
    128         lock( &systemProcessor->alarm_lock );
     156        verify( !systemProcessor->pending_alarm );
     157        lock( &systemProcessor->alarm_lock DEBUG_CTX2 );
    129158        {
     159                verify( validate( &systemProcessor->alarms ) );
     160                bool first = !systemProcessor->alarms.head;
     161
    130162                insert( &systemProcessor->alarms, this );
    131163                if( systemProcessor->pending_alarm ) {
    132164                        tick_preemption();
    133165                }
     166                if( first ) {
     167                        __kernel_set_timer( systemProcessor->alarms.head->alarm - __kernel_get_time() );
     168                }
    134169        }
    135170        unlock( &systemProcessor->alarm_lock );
    136171        this->set = true;
    137         enable_interrupts();
     172        enable_interrupts( DEBUG_CTX );
    138173}
    139174
    140175void unregister_self( alarm_node_t * this ) {
     176        // LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Kernel : unregister %p start\n", this );
    141177        disable_interrupts();
    142         lock( &systemProcessor->alarm_lock );
    143         remove( &systemProcessor->alarms, this );
     178        lock( &systemProcessor->alarm_lock DEBUG_CTX2 );
     179        {
     180                verify( validate( &systemProcessor->alarms ) );
     181                remove( &systemProcessor->alarms, this );
     182        }
    144183        unlock( &systemProcessor->alarm_lock );
    145         disable_interrupts();
     184        enable_interrupts( DEBUG_CTX );
    146185        this->set = false;
     186        // LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Kernel : unregister %p end\n", this );
    147187}
  • src/libcfa/concurrency/coroutine

    r9ff56e7 r3873b5a1  
    6363
    6464// Get current coroutine
    65 coroutine_desc * this_coroutine(void);
     65extern volatile thread_local coroutine_desc * this_coroutine;
    6666
    6767// Private wrappers for context switch and stack creation
     
    7171// Suspend implementation inlined for performance
    7272static inline void suspend() {
    73         coroutine_desc * src = this_coroutine();                // optimization
     73        coroutine_desc * src = this_coroutine;          // optimization
    7474
    7575        assertf( src->last != 0,
     
    8888forall(dtype T | is_coroutine(T))
    8989static inline void resume(T * cor) {
    90         coroutine_desc * src = this_coroutine();                // optimization
     90        coroutine_desc * src = this_coroutine;          // optimization
    9191        coroutine_desc * dst = get_coroutine(cor);
    9292
     
    112112
    113113static inline void resume(coroutine_desc * dst) {
    114         coroutine_desc * src = this_coroutine();                // optimization
     114        coroutine_desc * src = this_coroutine;          // optimization
    115115
    116116        // not resuming self ?
  • src/libcfa/concurrency/coroutine.c

    r9ff56e7 r3873b5a1  
    3232#include "invoke.h"
    3333
    34 extern thread_local processor * this_processor;
     34extern volatile thread_local processor * this_processor;
    3535
    3636//-----------------------------------------------------------------------------
     
    4444// Coroutine ctors and dtors
    4545void ?{}(coStack_t* this) {
    46         this->size              = 10240;        // size of stack
     46        this->size              = 65000;        // size of stack
    4747        this->storage   = NULL; // pointer to stack
    4848        this->limit             = NULL; // stack grows towards stack limit
     
    5050        this->context   = NULL; // address of cfa_context_t
    5151        this->top               = NULL; // address of top of storage
    52         this->userStack = false;       
     52        this->userStack = false;
    5353}
    5454
     
    106106
    107107        // set state of current coroutine to inactive
    108         src->state = Inactive;
     108        src->state = src->state == Halted ? Halted : Inactive;
    109109
    110110        // set new coroutine that task is executing
    111         this_processor->current_coroutine = dst;
     111        this_coroutine = dst;
    112112
    113113        // context switch to specified coroutine
     114        assert( src->stack.context );
    114115        CtxSwitch( src->stack.context, dst->stack.context );
    115         // when CtxSwitch returns we are back in the src coroutine             
     116        // when CtxSwitch returns we are back in the src coroutine
    116117
    117118        // set state of new coroutine to active
     
    131132                this->size = libCeiling( storageSize, 16 );
    132133                // use malloc/memalign because "new" raises an exception for out-of-memory
    133                
     134
    134135                // assume malloc has 8 byte alignment so add 8 to allow rounding up to 16 byte alignment
    135136                LIB_DEBUG_DO( this->storage = memalign( pageSize, cxtSize + this->size + pageSize ) );
  • src/libcfa/concurrency/invoke.c

    r9ff56e7 r3873b5a1  
    2929
    3030extern void __suspend_internal(void);
    31 extern void __leave_monitor_desc( struct monitor_desc * this );
     31extern void __leave_thread_monitor( struct thread_desc * this );
     32extern void disable_interrupts();
     33extern void enable_interrupts( DEBUG_CTX_PARAM );
    3234
    3335void CtxInvokeCoroutine(
    34       void (*main)(void *), 
    35       struct coroutine_desc *(*get_coroutine)(void *), 
     36      void (*main)(void *),
     37      struct coroutine_desc *(*get_coroutine)(void *),
    3638      void *this
    3739) {
     
    5658
    5759void CtxInvokeThread(
    58       void (*dtor)(void *), 
    59       void (*main)(void *), 
    60       struct thread_desc *(*get_thread)(void *), 
     60      void (*dtor)(void *),
     61      void (*main)(void *),
     62      struct thread_desc *(*get_thread)(void *),
    6163      void *this
    6264) {
     65      // First suspend, once the thread arrives here,
     66      // the function pointer to main can be invalidated without risk
    6367      __suspend_internal();
    6468
     69      // Fetch the thread handle from the user defined thread structure
    6570      struct thread_desc* thrd = get_thread( this );
    66       struct coroutine_desc* cor = &thrd->cor;
    67       struct monitor_desc* mon = &thrd->mon;
    68       cor->state = Active;
    6971
    70       // LIB_DEBUG_PRINTF("Invoke Thread : invoking main %p (args %p)\n", main, this);
     72      // Officially start the thread by enabling preemption
     73      enable_interrupts( DEBUG_CTX );
     74
     75      // Call the main of the thread
    7176      main( this );
    7277
    73       __leave_monitor_desc( mon );
     78      // To exit a thread we must :
     79      // 1 - Mark it as halted
     80      // 2 - Leave its monitor
     81      // 3 - Disable the interupts
     82      // The order of these 3 operations is very important
     83      __leave_thread_monitor( thrd );
    7484
    7585      //Final suspend, should never return
     
    8090
    8191void CtxStart(
    82       void (*main)(void *), 
    83       struct coroutine_desc *(*get_coroutine)(void *), 
    84       void *this, 
     92      void (*main)(void *),
     93      struct coroutine_desc *(*get_coroutine)(void *),
     94      void *this,
    8595      void (*invoke)(void *)
    8696) {
     
    108118        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke;
    109119      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
    110       ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7 
     120      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7
    111121
    112122#elif defined( __x86_64__ )
     
    128138      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke;
    129139      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
    130       ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7 
     140      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7
    131141#else
    132142      #error Only __i386__ and __x86_64__ is supported for threads in cfa
  • src/libcfa/concurrency/invoke.h

    r9ff56e7 r3873b5a1  
    3131      struct spinlock {
    3232            volatile int lock;
     33            #ifdef __CFA_DEBUG__
     34                  const char * prev_name;
     35                  void* prev_thrd;
     36            #endif
    3337      };
    3438
     
    8387            struct __thread_queue_t entry_queue;      // queue of threads that are blocked waiting for the monitor
    8488            struct __condition_stack_t signal_stack;  // stack of conditions to run next once we exit the monitor
    85             struct monitor_desc * stack_owner;        // if bulk acquiring was used we need to synchronize signals with an other monitor
    8689            unsigned int recursion;                   // monitor routines can be called recursively, we need to keep track of that
    8790      };
     
    99102#ifndef _INVOKE_PRIVATE_H_
    100103#define _INVOKE_PRIVATE_H_
    101      
     104
    102105      struct machine_context_t {
    103106            void *SP;
  • src/libcfa/concurrency/kernel

    r9ff56e7 r3873b5a1  
    2828//-----------------------------------------------------------------------------
    2929// Locks
    30 bool try_lock( spinlock * );
    31 void lock( spinlock * );
    32 void unlock( spinlock * );
     30bool try_lock  ( spinlock * DEBUG_CTX_PARAM2 );
     31void lock      ( spinlock * DEBUG_CTX_PARAM2 );
     32void lock_yield( spinlock * DEBUG_CTX_PARAM2 );
     33void unlock    ( spinlock * );
    3334
    34 struct signal_once {
    35         volatile bool cond;
    36         struct spinlock lock;
    37         struct __thread_queue_t blocked;
     35struct semaphore {
     36        spinlock lock;
     37        int count;
     38        __thread_queue_t waiting;
    3839};
    3940
    40 void ?{}(signal_once * this);
    41 void ^?{}(signal_once * this);
     41void  ?{}(semaphore * this, int count = 1);
     42void ^?{}(semaphore * this);
     43void P(semaphore * this);
     44void V(semaphore * this);
    4245
    43 void wait( signal_once * );
    44 void signal( signal_once * );
    4546
    4647//-----------------------------------------------------------------------------
     
    6869        unsigned short thrd_count;
    6970};
    70 static inline void ?{}(FinishAction * this) { 
     71static inline void ?{}(FinishAction * this) {
    7172        this->action_code = No_Action;
    7273        this->thrd = NULL;
     
    7879        struct processorCtx_t * runner;
    7980        cluster * cltr;
    80         coroutine_desc * current_coroutine;
    81         thread_desc * current_thread;
    8281        pthread_t kernel_thread;
    83        
    84         signal_once terminated;
     82
     83        semaphore terminated;
    8584        volatile bool is_terminated;
    8685
     
    9089        unsigned int preemption;
    9190
    92         unsigned short disable_preempt_count;
     91        bool pending_preemption;
    9392
    94         bool pending_preemption;
     93        char * last_enable;
    9594};
    9695
  • src/libcfa/concurrency/kernel.c

    r9ff56e7 r3873b5a1  
    1515//
    1616
    17 #include "startup.h"
    18 
    19 //Start and stop routine for the kernel, declared first to make sure they run first
    20 void kernel_startup(void)  __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) ));
    21 void kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) ));
    22 
    23 //Header
    24 #include "kernel_private.h"
     17#include "libhdr.h"
    2518
    2619//C Includes
     
    3528
    3629//CFA Includes
    37 #include "libhdr.h"
     30#include "kernel_private.h"
    3831#include "preemption.h"
     32#include "startup.h"
    3933
    4034//Private includes
    4135#define __CFA_INVOKE_PRIVATE__
    4236#include "invoke.h"
     37
     38//Start and stop routine for the kernel, declared first to make sure they run first
     39void kernel_startup(void)  __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) ));
     40void kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) ));
    4341
    4442//-----------------------------------------------------------------------------
     
    5957// Global state
    6058
    61 thread_local processor * this_processor;
    62 
    63 coroutine_desc * this_coroutine(void) {
    64         return this_processor->current_coroutine;
    65 }
    66 
    67 thread_desc * this_thread(void) {
    68         return this_processor->current_thread;
    69 }
     59volatile thread_local processor * this_processor;
     60volatile thread_local coroutine_desc * this_coroutine;
     61volatile thread_local thread_desc * this_thread;
     62volatile thread_local unsigned short disable_preempt_count = 1;
    7063
    7164//-----------------------------------------------------------------------------
    7265// Main thread construction
    7366struct current_stack_info_t {
    74         machine_context_t ctx; 
     67        machine_context_t ctx;
    7568        unsigned int size;              // size of stack
    7669        void *base;                             // base of stack
     
    10699
    107100void ?{}( coroutine_desc * this, current_stack_info_t * info) {
    108         (&this->stack){ info }; 
     101        (&this->stack){ info };
    109102        this->name = "Main Thread";
    110103        this->errno_ = 0;
     
    136129void ?{}(processor * this, cluster * cltr) {
    137130        this->cltr = cltr;
    138         this->current_coroutine = NULL;
    139         this->current_thread = NULL;
    140         (&this->terminated){};
     131        (&this->terminated){ 0 };
    141132        this->is_terminated = false;
    142133        this->preemption_alarm = NULL;
    143134        this->preemption = default_preemption();
    144         this->disable_preempt_count = 1;                //Start with interrupts disabled
    145135        this->pending_preemption = false;
    146136
     
    150140void ?{}(processor * this, cluster * cltr, processorCtx_t * runner) {
    151141        this->cltr = cltr;
    152         this->current_coroutine = NULL;
    153         this->current_thread = NULL;
    154         (&this->terminated){};
     142        (&this->terminated){ 0 };
    155143        this->is_terminated = false;
    156         this->disable_preempt_count = 0;
     144        this->preemption_alarm = NULL;
     145        this->preemption = default_preemption();
    157146        this->pending_preemption = false;
     147        this->kernel_thread = pthread_self();
    158148
    159149        this->runner = runner;
    160         LIB_DEBUG_PRINT_SAFE("Kernel : constructing processor context %p\n", runner);
     150        LIB_DEBUG_PRINT_SAFE("Kernel : constructing system processor context %p\n", runner);
    161151        runner{ this };
    162152}
     153
     154LIB_DEBUG_DO( bool validate( alarm_list_t * this ); )
    163155
    164156void ?{}(system_proc_t * this, cluster * cltr, processorCtx_t * runner) {
     
    168160
    169161        (&this->proc){ cltr, runner };
     162
     163        verify( validate( &this->alarms ) );
    170164}
    171165
     
    174168                LIB_DEBUG_PRINT_SAFE("Kernel : core %p signaling termination\n", this);
    175169                this->is_terminated = true;
    176                 wait( &this->terminated );
     170                P( &this->terminated );
     171                pthread_join( this->kernel_thread, NULL );
    177172        }
    178173}
     
    184179
    185180void ^?{}(cluster * this) {
    186        
     181
    187182}
    188183
     
    203198
    204199                thread_desc * readyThread = NULL;
    205                 for( unsigned int spin_count = 0; ! this->is_terminated; spin_count++ ) 
     200                for( unsigned int spin_count = 0; ! this->is_terminated; spin_count++ )
    206201                {
    207202                        readyThread = nextThread( this->cltr );
     
    209204                        if(readyThread)
    210205                        {
     206                                verify( disable_preempt_count > 0 );
     207
    211208                                runThread(this, readyThread);
     209
     210                                verify( disable_preempt_count > 0 );
    212211
    213212                                //Some actions need to be taken from the kernel
     
    225224        }
    226225
    227         signal( &this->terminated );
     226        V( &this->terminated );
     227
    228228        LIB_DEBUG_PRINT_SAFE("Kernel : core %p terminated\n", this);
    229229}
    230230
    231 // runThread runs a thread by context switching 
    232 // from the processor coroutine to the target thread 
     231// runThread runs a thread by context switching
     232// from the processor coroutine to the target thread
    233233void runThread(processor * this, thread_desc * dst) {
    234234        coroutine_desc * proc_cor = get_coroutine(this->runner);
    235235        coroutine_desc * thrd_cor = get_coroutine(dst);
    236        
     236
    237237        //Reset the terminating actions here
    238238        this->finish.action_code = No_Action;
    239239
    240240        //Update global state
    241         this->current_thread = dst;
     241        this_thread = dst;
    242242
    243243        // Context Switch to the thread
     
    246246}
    247247
    248 // Once a thread has finished running, some of 
     248// Once a thread has finished running, some of
    249249// its final actions must be executed from the kernel
    250250void finishRunning(processor * this) {
     
    256256        }
    257257        else if( this->finish.action_code == Release_Schedule ) {
    258                 unlock( this->finish.lock );           
     258                unlock( this->finish.lock );
    259259                ScheduleThread( this->finish.thrd );
    260260        }
     
    289289        processor * proc = (processor *) arg;
    290290        this_processor = proc;
     291        this_coroutine = NULL;
     292        this_thread = NULL;
     293        disable_preempt_count = 1;
    291294        // SKULLDUGGERY: We want to create a context for the processor coroutine
    292295        // which is needed for the 2-step context switch. However, there is no reason
    293         // to waste the perfectly valid stack create by pthread. 
     296        // to waste the perfectly valid stack create by pthread.
    294297        current_stack_info_t info;
    295298        machine_context_t ctx;
     
    300303
    301304        //Set global state
    302         proc->current_coroutine = &proc->runner->__cor;
    303         proc->current_thread = NULL;
     305        this_coroutine = &proc->runner->__cor;
     306        this_thread = NULL;
    304307
    305308        //We now have a proper context from which to schedule threads
    306309        LIB_DEBUG_PRINT_SAFE("Kernel : core %p created (%p, %p)\n", proc, proc->runner, &ctx);
    307310
    308         // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't 
    309         // resume it to start it like it normally would, it will just context switch 
    310         // back to here. Instead directly call the main since we already are on the 
     311        // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
     312        // resume it to start it like it normally would, it will just context switch
     313        // back to here. Instead directly call the main since we already are on the
    311314        // appropriate stack.
    312315        proc_cor_storage.__cor.state = Active;
     
    315318
    316319        // Main routine of the core returned, the core is now fully terminated
    317         LIB_DEBUG_PRINT_SAFE("Kernel : core %p main ended (%p)\n", proc, proc->runner); 
     320        LIB_DEBUG_PRINT_SAFE("Kernel : core %p main ended (%p)\n", proc, proc->runner);
    318321
    319322        return NULL;
     
    322325void start(processor * this) {
    323326        LIB_DEBUG_PRINT_SAFE("Kernel : Starting core %p\n", this);
    324        
     327
    325328        pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this );
    326329
    327         LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this);       
     330        LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this);
    328331}
    329332
     
    331334// Scheduler routines
    332335void ScheduleThread( thread_desc * thrd ) {
    333         if( !thrd ) return;
     336        // if( !thrd ) return;
     337        assert( thrd );
     338        assert( thrd->cor.state != Halted );
     339
     340        verify( disable_preempt_count > 0 );
    334341
    335342        verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
    336        
    337         lock( &systemProcessor->proc.cltr->lock );
     343
     344        lock( &systemProcessor->proc.cltr->lock DEBUG_CTX2 );
    338345        append( &systemProcessor->proc.cltr->ready_queue, thrd );
    339346        unlock( &systemProcessor->proc.cltr->lock );
     347
     348        verify( disable_preempt_count > 0 );
    340349}
    341350
    342351thread_desc * nextThread(cluster * this) {
    343         lock( &this->lock );
     352        verify( disable_preempt_count > 0 );
     353        lock( &this->lock DEBUG_CTX2 );
    344354        thread_desc * head = pop_head( &this->ready_queue );
    345355        unlock( &this->lock );
     356        verify( disable_preempt_count > 0 );
    346357        return head;
    347358}
    348359
    349 void ScheduleInternal() {
     360void BlockInternal() {
     361        disable_interrupts();
     362        verify( disable_preempt_count > 0 );
    350363        suspend();
    351 }
    352 
    353 void ScheduleInternal( spinlock * lock ) {
     364        verify( disable_preempt_count > 0 );
     365        enable_interrupts( DEBUG_CTX );
     366}
     367
     368void BlockInternal( spinlock * lock ) {
     369        disable_interrupts();
    354370        this_processor->finish.action_code = Release;
    355371        this_processor->finish.lock = lock;
     372
     373        verify( disable_preempt_count > 0 );
    356374        suspend();
    357 }
    358 
    359 void ScheduleInternal( thread_desc * thrd ) {
     375        verify( disable_preempt_count > 0 );
     376
     377        enable_interrupts( DEBUG_CTX );
     378}
     379
     380void BlockInternal( thread_desc * thrd ) {
     381        disable_interrupts();
     382        assert( thrd->cor.state != Halted );
    360383        this_processor->finish.action_code = Schedule;
    361384        this_processor->finish.thrd = thrd;
     385
     386        verify( disable_preempt_count > 0 );
    362387        suspend();
    363 }
    364 
    365 void ScheduleInternal( spinlock * lock, thread_desc * thrd ) {
     388        verify( disable_preempt_count > 0 );
     389
     390        enable_interrupts( DEBUG_CTX );
     391}
     392
     393void BlockInternal( spinlock * lock, thread_desc * thrd ) {
     394        disable_interrupts();
    366395        this_processor->finish.action_code = Release_Schedule;
    367396        this_processor->finish.lock = lock;
    368397        this_processor->finish.thrd = thrd;
     398
     399        verify( disable_preempt_count > 0 );
    369400        suspend();
    370 }
    371 
    372 void ScheduleInternal(spinlock ** locks, unsigned short count) {
     401        verify( disable_preempt_count > 0 );
     402
     403        enable_interrupts( DEBUG_CTX );
     404}
     405
     406void BlockInternal(spinlock ** locks, unsigned short count) {
     407        disable_interrupts();
    373408        this_processor->finish.action_code = Release_Multi;
    374409        this_processor->finish.locks = locks;
    375410        this_processor->finish.lock_count = count;
     411
     412        verify( disable_preempt_count > 0 );
    376413        suspend();
    377 }
    378 
    379 void ScheduleInternal(spinlock ** locks, unsigned short lock_count, thread_desc ** thrds, unsigned short thrd_count) {
     414        verify( disable_preempt_count > 0 );
     415
     416        enable_interrupts( DEBUG_CTX );
     417}
     418
     419void BlockInternal(spinlock ** locks, unsigned short lock_count, thread_desc ** thrds, unsigned short thrd_count) {
     420        disable_interrupts();
    380421        this_processor->finish.action_code = Release_Multi_Schedule;
    381422        this_processor->finish.locks = locks;
     
    383424        this_processor->finish.thrds = thrds;
    384425        this_processor->finish.thrd_count = thrd_count;
     426
     427        verify( disable_preempt_count > 0 );
    385428        suspend();
     429        verify( disable_preempt_count > 0 );
     430
     431        enable_interrupts( DEBUG_CTX );
    386432}
    387433
     
    392438// Kernel boot procedures
    393439void kernel_startup(void) {
    394         LIB_DEBUG_PRINT_SAFE("Kernel : Starting\n");   
     440        LIB_DEBUG_PRINT_SAFE("Kernel : Starting\n");
    395441
    396442        // Start by initializing the main thread
    397         // SKULLDUGGERY: the mainThread steals the process main thread 
     443        // SKULLDUGGERY: the mainThread steals the process main thread
    398444        // which will then be scheduled by the systemProcessor normally
    399445        mainThread = (thread_desc *)&mainThread_storage;
     
    403449        LIB_DEBUG_PRINT_SAFE("Kernel : Main thread ready\n");
    404450
    405         // Enable preemption
    406         kernel_start_preemption();
    407 
    408451        // Initialize the system cluster
    409452        systemCluster = (cluster *)&systemCluster_storage;
     
    417460        systemProcessor{ systemCluster, (processorCtx_t *)&systemProcessorCtx_storage };
    418461
    419         // Add the main thread to the ready queue 
     462        // Add the main thread to the ready queue
    420463        // once resume is called on systemProcessor->runner the mainThread needs to be scheduled like any normal thread
    421464        ScheduleThread(mainThread);
     
    423466        //initialize the global state variables
    424467        this_processor = &systemProcessor->proc;
    425         this_processor->current_thread = mainThread;
    426         this_processor->current_coroutine = &mainThread->cor;
     468        this_thread = mainThread;
     469        this_coroutine = &mainThread->cor;
     470        disable_preempt_count = 1;
     471
     472        // Enable preemption
     473        kernel_start_preemption();
    427474
    428475        // SKULLDUGGERY: Force a context switch to the system processor to set the main thread's context to the current UNIX
    429476        // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
    430         // mainThread is on the ready queue when this call is made. 
     477        // mainThread is on the ready queue when this call is made.
    431478        resume( systemProcessor->proc.runner );
    432479
     
    435482        // THE SYSTEM IS NOW COMPLETELY RUNNING
    436483        LIB_DEBUG_PRINT_SAFE("Kernel : Started\n--------------------------------------------------\n\n");
     484
     485        enable_interrupts( DEBUG_CTX );
    437486}
    438487
    439488void kernel_shutdown(void) {
    440489        LIB_DEBUG_PRINT_SAFE("\n--------------------------------------------------\nKernel : Shutting down\n");
     490
     491        disable_interrupts();
    441492
    442493        // SKULLDUGGERY: Notify the systemProcessor it needs to terminates.
     
    448499        // THE SYSTEM IS NOW COMPLETELY STOPPED
    449500
     501        // Disable preemption
     502        kernel_stop_preemption();
     503
    450504        // Destroy the system processor and its context in reverse order of construction
    451505        // These were manually constructed so we need manually destroy them
     
    457511        ^(mainThread){};
    458512
    459         LIB_DEBUG_PRINT_SAFE("Kernel : Shutdown complete\n");   
     513        LIB_DEBUG_PRINT_SAFE("Kernel : Shutdown complete\n");
    460514}
    461515
     
    467521        // abort cannot be recursively entered by the same or different processors because all signal handlers return when
    468522        // the globalAbort flag is true.
    469         lock( &kernel_abort_lock );
     523        lock( &kernel_abort_lock DEBUG_CTX2 );
    470524
    471525        // first task to abort ?
     
    473527                kernel_abort_called = true;
    474528                unlock( &kernel_abort_lock );
    475         } 
     529        }
    476530        else {
    477531                unlock( &kernel_abort_lock );
    478                
     532
    479533                sigset_t mask;
    480534                sigemptyset( &mask );
     
    482536                sigaddset( &mask, SIGUSR1 );                    // block SIGUSR1 signals
    483537                sigsuspend( &mask );                            // block the processor to prevent further damage during abort
    484                 _exit( EXIT_FAILURE );                          // if processor unblocks before it is killed, terminate it             
    485         }
    486 
    487         return this_thread();
     538                _exit( EXIT_FAILURE );                          // if processor unblocks before it is killed, terminate it
     539        }
     540
     541        return this_thread;
    488542}
    489543
     
    494548        __lib_debug_write( STDERR_FILENO, abort_text, len );
    495549
    496         if ( thrd != this_coroutine() ) {
    497                 len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", this_coroutine()->name, this_coroutine() );
     550        if ( thrd != this_coroutine ) {
     551                len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", this_coroutine->name, this_coroutine );
    498552                __lib_debug_write( STDERR_FILENO, abort_text, len );
    499         } 
     553        }
    500554        else {
    501555                __lib_debug_write( STDERR_FILENO, ".\n", 2 );
     
    505559extern "C" {
    506560        void __lib_debug_acquire() {
    507                 lock(&kernel_debug_lock);
     561                lock( &kernel_debug_lock DEBUG_CTX2 );
    508562        }
    509563
    510564        void __lib_debug_release() {
    511                 unlock(&kernel_debug_lock);
     565                unlock( &kernel_debug_lock );
    512566        }
    513567}
     
    525579}
    526580
    527 bool try_lock( spinlock * this ) {
     581bool try_lock( spinlock * this DEBUG_CTX_PARAM2 ) {
    528582        return this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0;
    529583}
    530584
    531 void lock( spinlock * this ) {
     585void lock( spinlock * this DEBUG_CTX_PARAM2 ) {
    532586        for ( unsigned int i = 1;; i += 1 ) {
    533                 if ( this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0 ) break;
    534         }
    535 }
     587                if ( this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0 ) { break; }
     588        }
     589        LIB_DEBUG_DO(
     590                this->prev_name = caller;
     591                this->prev_thrd = this_thread;
     592        )
     593}
     594
     595void lock_yield( spinlock * this DEBUG_CTX_PARAM2 ) {
     596        for ( unsigned int i = 1;; i += 1 ) {
     597                if ( this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0 ) { break; }
     598                yield();
     599        }
     600        LIB_DEBUG_DO(
     601                this->prev_name = caller;
     602                this->prev_thrd = this_thread;
     603        )
     604}
     605
    536606
    537607void unlock( spinlock * this ) {
     
    539609}
    540610
    541 void ?{}( signal_once * this ) {
    542         this->cond = false;
    543 }
    544 void ^?{}( signal_once * this ) {
    545 
    546 }
    547 
    548 void wait( signal_once * this ) {
    549         lock( &this->lock );
    550         if( !this->cond ) {
    551                 append( &this->blocked, this_thread() );
    552                 ScheduleInternal( &this->lock );
    553                 lock( &this->lock );
    554         }
     611void  ?{}( semaphore * this, int count = 1 ) {
     612        (&this->lock){};
     613        this->count = count;
     614        (&this->waiting){};
     615}
     616void ^?{}(semaphore * this) {}
     617
     618void P(semaphore * this) {
     619        lock( &this->lock DEBUG_CTX2 );
     620        this->count -= 1;
     621        if ( this->count < 0 ) {
     622                // queue current task
     623                append( &this->waiting, (thread_desc *)this_thread );
     624
     625                // atomically release spin lock and block
     626                BlockInternal( &this->lock );
     627        }
     628        else {
     629            unlock( &this->lock );
     630        }
     631}
     632
     633void V(semaphore * this) {
     634        thread_desc * thrd = NULL;
     635        lock( &this->lock DEBUG_CTX2 );
     636        this->count += 1;
     637        if ( this->count <= 0 ) {
     638                // remove task at head of waiting list
     639                thrd = pop_head( &this->waiting );
     640        }
     641
    555642        unlock( &this->lock );
    556 }
    557 
    558 void signal( signal_once * this ) {
    559         lock( &this->lock );
    560         {
    561                 this->cond = true;
    562 
    563                 thread_desc * it;
    564                 while( it = pop_head( &this->blocked) ) {
    565                         ScheduleThread( it );
    566                 }
    567         }
    568         unlock( &this->lock );
     643
     644        // make new owner
     645        WakeThread( thrd );
    569646}
    570647
     
    590667                }
    591668                head->next = NULL;
    592         }       
     669        }
    593670        return head;
    594671}
     
    609686                this->top = top->next;
    610687                top->next = NULL;
    611         }       
     688        }
    612689        return top;
    613690}
  • src/libcfa/concurrency/kernel_private.h

    r9ff56e7 r3873b5a1  
    1818#define KERNEL_PRIVATE_H
    1919
     20#include "libhdr.h"
     21
    2022#include "kernel"
    2123#include "thread"
     
    2325#include "alarm.h"
    2426
    25 #include "libhdr.h"
    2627
    2728//-----------------------------------------------------------------------------
    2829// Scheduler
     30
     31extern "C" {
     32        void disable_interrupts();
     33        void enable_interrupts_noRF();
     34        void enable_interrupts( DEBUG_CTX_PARAM );
     35}
     36
    2937void ScheduleThread( thread_desc * );
     38static inline void WakeThread( thread_desc * thrd ) {
     39        if( !thrd ) return;
     40
     41        disable_interrupts();
     42        ScheduleThread( thrd );
     43        enable_interrupts( DEBUG_CTX );
     44}
    3045thread_desc * nextThread(cluster * this);
    3146
    32 void ScheduleInternal(void);
    33 void ScheduleInternal(spinlock * lock);
    34 void ScheduleInternal(thread_desc * thrd);
    35 void ScheduleInternal(spinlock * lock, thread_desc * thrd);
    36 void ScheduleInternal(spinlock ** locks, unsigned short count);
    37 void ScheduleInternal(spinlock ** locks, unsigned short count, thread_desc ** thrds, unsigned short thrd_count);
     47void BlockInternal(void);
     48void BlockInternal(spinlock * lock);
     49void BlockInternal(thread_desc * thrd);
     50void BlockInternal(spinlock * lock, thread_desc * thrd);
     51void BlockInternal(spinlock ** locks, unsigned short count);
     52void BlockInternal(spinlock ** locks, unsigned short count, thread_desc ** thrds, unsigned short thrd_count);
    3853
    3954//-----------------------------------------------------------------------------
     
    6075extern cluster * systemCluster;
    6176extern system_proc_t * systemProcessor;
    62 extern thread_local processor * this_processor;
    63 
    64 static inline void disable_interrupts() {
    65         __attribute__((unused)) unsigned short prev = __atomic_fetch_add_2( &this_processor->disable_preempt_count, 1, __ATOMIC_SEQ_CST );
    66         assert( prev != (unsigned short) -1 );
    67 }
    68 
    69 static inline void enable_interrupts_noRF() {
    70         __attribute__((unused)) unsigned short prev = __atomic_fetch_add_2( &this_processor->disable_preempt_count, -1, __ATOMIC_SEQ_CST );
    71         verify( prev != (unsigned short) 0 );
    72 }
    73 
    74 static inline void enable_interrupts() {
    75         __attribute__((unused)) unsigned short prev = __atomic_fetch_add_2( &this_processor->disable_preempt_count, -1, __ATOMIC_SEQ_CST );
    76         verify( prev != (unsigned short) 0 );
    77         if( prev == 1 && this_processor->pending_preemption ) {
    78                 ScheduleInternal( this_processor->current_thread );
    79                 this_processor->pending_preemption = false;
    80         }
    81 }
     77extern volatile thread_local processor * this_processor;
     78extern volatile thread_local coroutine_desc * this_coroutine;
     79extern volatile thread_local thread_desc * this_thread;
     80extern volatile thread_local unsigned short disable_preempt_count;
    8281
    8382//-----------------------------------------------------------------------------
  • src/libcfa/concurrency/monitor

    r9ff56e7 r3873b5a1  
    2626static inline void ?{}(monitor_desc * this) {
    2727        this->owner = NULL;
    28         this->stack_owner = NULL;
    2928        this->recursion = 0;
    3029}
  • src/libcfa/concurrency/monitor.c

    r9ff56e7 r3873b5a1  
    1919#include <stdlib>
    2020
     21#include "libhdr.h"
    2122#include "kernel_private.h"
    22 #include "libhdr.h"
    2323
    2424//-----------------------------------------------------------------------------
     
    4444
    4545extern "C" {
    46         void __enter_monitor_desc(monitor_desc * this) {
    47                 lock( &this->lock );
    48                 thread_desc * thrd = this_thread();
    49 
    50                 LIB_DEBUG_PRINT_SAFE("%p Entering %p (o: %p, r: %i)\n", thrd, this, this->owner, this->recursion);
     46        void __enter_monitor_desc( monitor_desc * this ) {
     47                lock_yield( &this->lock DEBUG_CTX2 );
     48                thread_desc * thrd = this_thread;
     49
     50                // LIB_DEBUG_PRINT_SAFE("%p Entering %p (o: %p, r: %i)\n", thrd, this, this->owner, this->recursion);
    5151
    5252                if( !this->owner ) {
     
    6262                        //Some one else has the monitor, wait in line for it
    6363                        append( &this->entry_queue, thrd );
    64                         LIB_DEBUG_PRINT_SAFE("%p Blocking on entry\n", thrd);
    65                         ScheduleInternal( &this->lock );
    66 
    67                         //ScheduleInternal will unlock spinlock, no need to unlock ourselves
    68                         return; 
     64                        // LIB_DEBUG_PRINT_SAFE("%p Blocking on entry\n", thrd);
     65                        BlockInternal( &this->lock );
     66
     67                        //BlockInternal will unlock spinlock, no need to unlock ourselves
     68                        return;
    6969                }
    7070
     
    7575        // leave pseudo code :
    7676        //      TODO
    77         void __leave_monitor_desc(monitor_desc * this) {
    78                 lock( &this->lock );
    79 
    80                 LIB_DEBUG_PRINT_SAFE("%p Leaving %p (o: %p, r: %i)\n", thrd, this, this->owner, this->recursion);
    81                 verifyf( this_thread() == this->owner, "Expected owner to be %p, got %p (r: %i)", this_thread(), this->owner, this->recursion );
     77        void __leave_monitor_desc( monitor_desc * this ) {
     78                lock_yield( &this->lock DEBUG_CTX2 );
     79
     80                // LIB_DEBUG_PRINT_SAFE("%p Leaving %p (o: %p, r: %i). ", this_thread, this, this->owner, this->recursion);
     81                verifyf( this_thread == this->owner, "Expected owner to be %p, got %p (r: %i)", this_thread, this->owner, this->recursion );
    8282
    8383                //Leaving a recursion level, decrement the counter
     
    9696                unlock( &this->lock );
    9797
    98                 LIB_DEBUG_PRINT_SAFE("Next owner is %p\n", new_owner);
     98                // LIB_DEBUG_PRINT_SAFE("Next owner is %p\n", new_owner);
    9999
    100100                //We need to wake-up the thread
    101                 ScheduleThread( new_owner );
     101                WakeThread( new_owner );
     102        }
     103
     104        void __leave_thread_monitor( thread_desc * thrd ) {
     105                monitor_desc * this = &thrd->mon;
     106                lock_yield( &this->lock DEBUG_CTX2 );
     107
     108                disable_interrupts();
     109
     110                thrd->cor.state = Halted;
     111
     112                verifyf( thrd == this->owner, "Expected owner to be %p, got %p (r: %i)", thrd, this->owner, this->recursion );
     113
     114                //Leaving a recursion level, decrement the counter
     115                this->recursion -= 1;
     116
     117                //If we haven't left the last level of recursion
     118                //it means we don't need to do anything
     119                if( this->recursion != 0) {
     120                        unlock( &this->lock );
     121                        return;
     122                }
     123
     124                thread_desc * new_owner = next_thread( this );
     125
     126                //We can now let other threads in safely
     127                unlock( &this->lock );
     128
     129                //We need to wake-up the thread
     130                if( new_owner) ScheduleThread( new_owner );
    102131        }
    103132}
     
    121150        enter( this->m, this->count );
    122151
    123         this->prev_mntrs = this_thread()->current_monitors;
    124         this->prev_count = this_thread()->current_monitor_count;
    125 
    126         this_thread()->current_monitors      = m;
    127         this_thread()->current_monitor_count = count;
     152        this->prev_mntrs = this_thread->current_monitors;
     153        this->prev_count = this_thread->current_monitor_count;
     154
     155        this_thread->current_monitors      = m;
     156        this_thread->current_monitor_count = count;
    128157}
    129158
     
    131160        leave( this->m, this->count );
    132161
    133         this_thread()->current_monitors      = this->prev_mntrs;
    134         this_thread()->current_monitor_count = this->prev_count;
     162        this_thread->current_monitors      = this->prev_mntrs;
     163        this_thread->current_monitor_count = this->prev_count;
    135164}
    136165
     
    159188// Internal scheduling
    160189void wait( condition * this, uintptr_t user_info = 0 ) {
    161         LIB_DEBUG_PRINT_SAFE("Waiting\n");
     190        // LIB_DEBUG_PRINT_SAFE("Waiting\n");
    162191
    163192        brand_condition( this );
     
    170199        unsigned short count = this->monitor_count;
    171200        unsigned int recursions[ count ];               //Save the current recursion levels to restore them later
    172         spinlock *   locks     [ count ];               //We need to pass-in an array of locks to ScheduleInternal
    173 
    174         LIB_DEBUG_PRINT_SAFE("count %i\n", count);
    175 
    176         __condition_node_t waiter = { this_thread(), count, user_info };
     201        spinlock *   locks     [ count ];               //We need to pass-in an array of locks to BlockInternal
     202
     203        // LIB_DEBUG_PRINT_SAFE("count %i\n", count);
     204
     205        __condition_node_t waiter = { (thread_desc*)this_thread, count, user_info };
    177206
    178207        __condition_criterion_t criteria[count];
    179208        for(int i = 0; i < count; i++) {
    180209                (&criteria[i]){ this->monitors[i], &waiter };
    181                 LIB_DEBUG_PRINT_SAFE( "Criterion %p\n", &criteria[i] );
     210                // LIB_DEBUG_PRINT_SAFE( "Criterion %p\n", &criteria[i] );
    182211        }
    183212
     
    201230        }
    202231
    203         LIB_DEBUG_PRINT_SAFE("Will unblock: ");
     232        // LIB_DEBUG_PRINT_SAFE("Will unblock: ");
    204233        for(int i = 0; i < thread_count; i++) {
    205                 LIB_DEBUG_PRINT_SAFE("%p ", threads[i]);
    206         }
    207         LIB_DEBUG_PRINT_SAFE("\n");
     234                // LIB_DEBUG_PRINT_SAFE("%p ", threads[i]);
     235        }
     236        // LIB_DEBUG_PRINT_SAFE("\n");
    208237
    209238        // Everything is ready to go to sleep
    210         ScheduleInternal( locks, count, threads, thread_count );
     239        BlockInternal( locks, count, threads, thread_count );
    211240
    212241
     
    222251bool signal( condition * this ) {
    223252        if( is_empty( this ) ) {
    224                 LIB_DEBUG_PRINT_SAFE("Nothing to signal\n");
     253                // LIB_DEBUG_PRINT_SAFE("Nothing to signal\n");
    225254                return false;
    226255        }
     
    231260
    232261        unsigned short count = this->monitor_count;
    233        
     262
    234263        //Some more checking in debug
    235264        LIB_DEBUG_DO(
    236                 thread_desc * this_thrd = this_thread();
     265                thread_desc * this_thrd = this_thread;
    237266                if ( this->monitor_count != this_thrd->current_monitor_count ) {
    238267                        abortf( "Signal on condition %p made with different number of monitor(s), expected %i got %i", this, this->monitor_count, this_thrd->current_monitor_count );
     
    248277        //Lock all the monitors
    249278        lock_all( this->monitors, NULL, count );
    250         LIB_DEBUG_PRINT_SAFE("Signalling");
     279        // LIB_DEBUG_PRINT_SAFE("Signalling");
    251280
    252281        //Pop the head of the waiting queue
     
    256285        for(int i = 0; i < count; i++) {
    257286                __condition_criterion_t * crit = &node->criteria[i];
    258                 LIB_DEBUG_PRINT_SAFE(" %p", crit->target);
     287                // LIB_DEBUG_PRINT_SAFE(" %p", crit->target);
    259288                assert( !crit->ready );
    260289                push( &crit->target->signal_stack, crit );
    261290        }
    262291
    263         LIB_DEBUG_PRINT_SAFE("\n");
     292        // LIB_DEBUG_PRINT_SAFE("\n");
    264293
    265294        //Release
     
    281310        unsigned short count = this->monitor_count;
    282311        unsigned int recursions[ count ];               //Save the current recursion levels to restore them later
    283         spinlock *   locks     [ count ];               //We need to pass-in an array of locks to ScheduleInternal
     312        spinlock *   locks     [ count ];               //We need to pass-in an array of locks to BlockInternal
    284313
    285314        lock_all( this->monitors, locks, count );
    286315
    287316        //create creteria
    288         __condition_node_t waiter = { this_thread(), count, 0 };
     317        __condition_node_t waiter = { (thread_desc*)this_thread, count, 0 };
    289318
    290319        __condition_criterion_t criteria[count];
    291320        for(int i = 0; i < count; i++) {
    292321                (&criteria[i]){ this->monitors[i], &waiter };
    293                 LIB_DEBUG_PRINT_SAFE( "Criterion %p\n", &criteria[i] );
     322                // LIB_DEBUG_PRINT_SAFE( "Criterion %p\n", &criteria[i] );
    294323                push( &criteria[i].target->signal_stack, &criteria[i] );
    295324        }
     
    309338
    310339        //Everything is ready to go to sleep
    311         ScheduleInternal( locks, count, &signallee, 1 );
     340        BlockInternal( locks, count, &signallee, 1 );
    312341
    313342
     
    325354
    326355uintptr_t front( condition * this ) {
    327         verifyf( !is_empty(this), 
     356        verifyf( !is_empty(this),
    328357                "Attempt to access user data on an empty condition.\n"
    329358                "Possible cause is not checking if the condition is empty before reading stored data."
     
    335364// Internal scheduling
    336365void __accept_internal( unsigned short count, __acceptable_t * acceptables, void (*func)(void) ) {
    337         // thread_desc * this = this_thread();
     366        // thread_desc * this = this_thread;
    338367
    339368        // unsigned short count = this->current_monitor_count;
    340369        // unsigned int recursions[ count ];            //Save the current recursion levels to restore them later
    341         // spinlock *   locks     [ count ];            //We need to pass-in an array of locks to ScheduleInternal
     370        // spinlock *   locks     [ count ];            //We need to pass-in an array of locks to BlockInternal
    342371
    343372        // lock_all( this->current_monitors, locks, count );
     
    348377
    349378        // // // Everything is ready to go to sleep
    350         // // ScheduleInternal( locks, count, threads, thread_count );
     379        // // BlockInternal( locks, count, threads, thread_count );
    351380
    352381
     
    393422static inline void lock_all( spinlock ** locks, unsigned short count ) {
    394423        for( int i = 0; i < count; i++ ) {
    395                 lock( locks[i] );
     424                lock_yield( locks[i] DEBUG_CTX2 );
    396425        }
    397426}
     
    400429        for( int i = 0; i < count; i++ ) {
    401430                spinlock * l = &source[i]->lock;
    402                 lock( l );
     431                lock_yield( l DEBUG_CTX2 );
    403432                if(locks) locks[i] = l;
    404433        }
     
    443472        for(    int i = 0; i < count; i++ ) {
    444473
    445                 LIB_DEBUG_PRINT_SAFE( "Checking %p for %p\n", &criteria[i], target );
     474                // LIB_DEBUG_PRINT_SAFE( "Checking %p for %p\n", &criteria[i], target );
    446475                if( &criteria[i] == target ) {
    447476                        criteria[i].ready = true;
    448                         LIB_DEBUG_PRINT_SAFE( "True\n" );
     477                        // LIB_DEBUG_PRINT_SAFE( "True\n" );
    449478                }
    450479
     
    452481        }
    453482
    454         LIB_DEBUG_PRINT_SAFE( "Runing %i\n", ready2run );
     483        // LIB_DEBUG_PRINT_SAFE( "Runing %i\n", ready2run );
    455484        return ready2run ? node->waiting_thread : NULL;
    456485}
    457486
    458487static inline void brand_condition( condition * this ) {
    459         thread_desc * thrd = this_thread();
     488