Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/papers/general/Paper.tex

    rd52a55b raa5fdac  
    228228Nevertheless, C, first standardized over thirty years ago, lacks many features that make programming in more modern languages safer and more productive.
    229229
    230 \CFA (pronounced ``C-for-all'', and written \CFA or Cforall) is an evolutionary extension of the C programming language that adds modern language-features to C, while maintaining both source and runtime compatibility with C and a familiar programming model for programmers.
     230\CFA (pronounced ``C-for-all'', and written \CFA or Cforall) is an evolutionary extension of the C programming language that aims to add modern language features to C, while maintaining both source and runtime compatibility with C and a familiar programming model for programmers.
    231231The four key design goals for \CFA~\cite{Bilson03} are:
    232232(1) The behaviour of standard C code must remain the same when translated by a \CFA compiler as when translated by a C compiler;
     
    237237\CC is used similarly, but has the disadvantages of multiple legacy design-choices that cannot be updated and active divergence of the language model from C, requiring significant effort and training to incrementally add \CC to a C-based project.
    238238
    239 All languages features discussed in this paper are working, except some advanced exception-handling features.
    240 Not discussed in this paper are the integrated concurrency-constructs and user-level threading-library~\cite{Delisle18}.
    241239\CFA is an \emph{open-source} project implemented as an source-to-source translator from \CFA to the gcc-dialect of C~\cite{GCCExtensions}, allowing it to leverage the portability and code optimizations provided by gcc, meeting goals (1)--(3).
    242240Ultimately, a compiler is necessary for advanced features and optimal performance.
    243 % @plg2[9]% cd cfa-cc/src; cloc ArgTweak CodeGen CodeTools Common Concurrency ControlStruct Designators GenPoly InitTweak MakeLibCfa.cc MakeLibCfa.h Parser ResolvExpr SymTab SynTree Tuples driver prelude main.cc
    244 % -------------------------------------------------------------------------------
    245 % Language                     files          blank        comment           code
    246 % -------------------------------------------------------------------------------
    247 % C++                            108           5420           5232          34961
    248 % C/C++ Header                    86           2379           2450           8464
    249 % Teamcenter def                   2            115             65           1387
    250 % make                             5            168             87           1052
    251 % C                               20            109            403            488
    252 % awk                              1             12             26            121
    253 % sed                              1              0              0              6
    254 % -------------------------------------------------------------------------------
    255 % SUM:                           223           8203           8263          46479
    256 % -------------------------------------------------------------------------------
    257 The \CFA translator is 200+ files and 46,000+ lines of code written in C/\CC.
    258 Starting with a translator versus a compiler makes it easier and faster to generate and debug C object-code rather than intermediate, assembler or machine code.
    259 The translator design is based on the \emph{visitor pattern}, allowing multiple passes over the abstract code-tree, which works well for incrementally adding new feature through additional visitor passes.
    260 At the heart of the translator is the type resolver, which handles the polymorphic routine/type overload-resolution.
    261 % @plg2[8]% cd cfa-cc/src; cloc libcfa
    262 % -------------------------------------------------------------------------------
    263 % Language                     files          blank        comment           code
    264 % -------------------------------------------------------------------------------
    265 % C                               35           1256           1240           9116
    266 % C/C++ Header                    54            358           1106           1198
    267 % make                             2            201            325           1167
    268 % C++                              3             18             17            124
    269 % Assembly                         3             56             97            111
    270 % Bourne Shell                     2              2              0             25
    271 % awk                              1              4              0             22
    272 % -------------------------------------------------------------------------------
    273 % SUM:                           100           1895           2785          11763
    274 % -------------------------------------------------------------------------------
    275 The \CFA runtime system is 100+ files and 11,000+ lines of code, written in \CFA.
    276 Currently, the \CFA runtime is the largest \emph{user} of \CFA providing a vehicle to test the language features and implementation.
    277 % @plg2[6]% cd cfa-cc/src; cloc tests examples benchmark
    278 % -------------------------------------------------------------------------------
    279 % Language                     files          blank        comment           code
    280 % -------------------------------------------------------------------------------
    281 % C                              237          12260           2869          23286
    282 % make                             8            464            245           2838
    283 % C/C++ Header                    22            225            175            785
    284 % Python                           5            131             93            420
    285 % C++                             10             48              5            201
    286 % Lua                              2             31              4            126
    287 % Java                             4              5              0             80
    288 % Go                               2             11              9             40
    289 % -------------------------------------------------------------------------------
    290 % SUM:                           290          13175           3400          27776
    291 % -------------------------------------------------------------------------------
    292 The \CFA tests are 290+ files and 27,000+ lines of code.
    293 The tests illustrate syntactic and semantic features in \CFA, plus a growing number of runtime benchmarks.
    294 The tests check for correctness and are used for daily regression testing of commits (3800+).
     241All features discussed in this paper are working, unless otherwise stated as under construction.
    295242
    296243Finally, it is impossible to describe a programming language without usages before definitions.
     
    313260There are only two hard things in Computer Science: cache invalidation and \emph{naming things} -- Phil Karlton
    314261\end{quote}
    315 \vspace{-10pt}
    316262C already has a limited form of ad-hoc polymorphism in the form of its basic arithmetic operators, which apply to a variety of different types using identical syntax.
    317263\CFA extends the built-in operator overloading by allowing users to define overloads for any function, not just operators, and even any variable;
     
    408354
    409355\CFA has replacement libraries condensing hundreds of existing C functions into tens of \CFA overloaded functions, all without rewriting the actual computations (see Section~\ref{sec:libraries}).
    410 For example, it is possible to write a type-safe \CFA wrapper @malloc@ based on the C @malloc@, where the return type supplies the type/size of the allocation, which is impossible in most type systems.
     356For example, it is possible to write a type-safe \CFA wrapper @malloc@ based on the C @malloc@:
    411357\begin{cfa}
    412358forall( dtype T | sized(T) ) T * malloc( void ) { return (T *)malloc( sizeof(T) ); }
    413 // select type and size from left-hand side
    414 int * ip = malloc();  double * dp = malloc();  struct S {...} * sp = malloc();
    415 \end{cfa}
     359int * ip = malloc();                                            $\C{// select type and size from left-hand side}$
     360double * dp = malloc();
     361struct S {...} * sp = malloc();
     362\end{cfa}
     363where the return type supplies the type/size of the allocation, which is impossible in most type systems.
    416364
    417365Call-site inferencing and nested functions provide a localized form of inheritance.
     
    425373}
    426374\end{cfa}
    427 The local version of @?<?@ performs @?>?@ overriding the built-in @?<?@ so it is passed to @qsort@.
     375Within the block, the nested version of @?<?@ performs @?>?@ and this local version overrides the built-in @?<?@ so it is passed to @qsort@.
    428376Hence, programmers can easily form local environments, adding and modifying appropriate functions, to maximize reuse of other existing functions and types.
    429377
     
    434382        inline {                                                                        $\C{// nested distribution block, add forall/inline to declarations}$
    435383                void push( stack(`T`) & s, `T` value ) ...      $\C{// generic operations}$
     384                T pop( stack(`T`) & s ) ...
    436385        }
    437386}
     
    439388
    440389
    441 \vspace*{-2pt}
    442390\subsection{Traits}
    443391
    444392\CFA provides \newterm{traits} to name a group of type assertions, where the trait name allows specifying the same set of assertions in multiple locations, preventing repetition mistakes at each function declaration:
    445 
    446393\begin{cquote}
    447394\lstDeleteShortInline@%
     
    515462
    516463
    517 \vspace*{-2pt}
    518464\section{Generic Types}
    519465
     
    528474
    529475\CC, Java, and other languages use \newterm{generic types} to produce type-safe abstract data-types.
    530 \CFA generic types integrate efficiently and naturally with the existing polymorphic functions, while retaining backwards compatibility with C and providing separate compilation.
     476\CFA also implements generic types that integrate efficiently and naturally with the existing polymorphic functions, while retaining backwards compatibility with C and providing separate compilation.
    531477However, for known concrete parameters, the generic-type definition can be inlined, like \CC templates.
    532478
     
    534480\begin{cquote}
    535481\lstDeleteShortInline@%
    536 \begin{tabular}{@{}l|@{\hspace{2\parindentlnth}}l@{}}
     482\begin{tabular}{@{}l@{\hspace{2\parindentlnth}}l@{}}
    537483\begin{cfa}
    538484forall( otype R, otype S ) struct pair {
     
    14271373resume( $\emph{alternate-stack}$ )
    14281374\end{cfa}
    1429 These overloads of @resume@ raise the specified exception or the currently propagating exception (reresume) at another \CFA coroutine or task~\cite{Delisle18}.
     1375These overloads of @resume@ raise the specified exception or the currently propagating exception (reresume) at another \CFA coroutine or task\footnote{\CFA coroutine and concurrency features are discussed in a separately submitted paper.}~\cite{Delisle18}.
    14301376Nonlocal raise is restricted to resumption to provide the exception handler the greatest flexibility because processing the exception does not unwind its stack, allowing it to continue after the handler returns.
    14311377
     
    20942040
    20952041
    2096 \begin{comment}
    20972042\subsection{Integral Suffixes}
    20982043
     
    21282073\lstMakeShortInline@%
    21292074\end{cquote}
    2130 \end{comment}
    21312075
    21322076
     
    27002644Figure~\ref{fig:eval} and Table~\ref{tab:eval} show the results of running the benchmark in Figure~\ref{fig:BenchmarkTest} and its C, \CC, and \CCV equivalents.
    27012645The graph plots the median of 5 consecutive runs of each program, with an initial warm-up run omitted.
    2702 All code is compiled at \texttt{-O2} by gcc or g++ 6.4.0, with all \CC code compiled as \CCfourteen.
     2646All code is compiled at \texttt{-O2} by gcc or g++ 6.3.0, with all \CC code compiled as \CCfourteen.
    27032647The benchmarks are run on an Ubuntu 16.04 workstation with 16 GB of RAM and a 6-core AMD FX-6300 CPU with 3.5 GHz maximum clock frequency.
    27042648
     
    27182662                                                                        & \CT{C}        & \CT{\CFA}     & \CT{\CC}      & \CT{\CCV}             \\ \hline
    27192663maximum memory usage (MB)                       & 10,001        & 2,502         & 2,503         & 11,253                \\
    2720 source code size (lines)                        & 201           & 191           & 125           & 294                   \\
     2664source code size (lines)                        & 196           & 186           & 125           & 290                   \\
    27212665redundant type annotations (lines)      & 27            & 0                     & 2                     & 16                    \\
    27222666binary size (KB)                                        & 14            & 257           & 14            & 37                    \\
     
    28212765data-parallel features have not yet been added to \CFA, but are easily incorporated within its design, while concurrency primitives similar to those in $\mu$\CC have already been added~\cite{Delisle18}.
    28222766Finally, CCured~\cite{Necula02} and Ironclad \CC~\cite{DeLozier13} attempt to provide a more memory-safe C by annotating pointer types with garbage collection information; type-checked polymorphism in \CFA covers several of C's memory-safety issues, but more aggressive approaches such as annotating all pointer types with their nullability or requiring runtime garbage collection are contradictory to \CFA's backwards compatibility goals.
     2767
     2768
     2769\begin{comment}
     2770\subsection{Control Structures / Declarations / Literals}
     2771
     2772Java has default fall through like C/\CC.
     2773Pascal/Ada/Go/Rust do not have default fall through.
     2774\Csharp does not have fall through but still requires a break.
     2775Python uses dictionary mapping. \\
     2776\CFA choose is like Rust match.
     2777
     2778Java has labelled break/continue. \\
     2779Languages with and without exception handling.
     2780
     2781Alternative C declarations. \\
     2782Different references \\
     2783Constructors/destructors
     2784
     27850/1 Literals \\
     2786user defined: D, Objective-C
     2787\end{comment}
    28232788
    28242789
     
    29102875}
    29112876_Bool stack_empty( const stack * s ) {
    2912         return s->head == NULL;
     2877         return s->head == NULL;
    29132878}
    29142879void push_stack( stack * s, void * v ) {
    2915         node * n = malloc( sizeof(node) ); /***/
     2880        node * n = malloc(sizeof(node)); /***/
    29162881        *n = (node){ v, s->head }; /***/
    29172882        s->head = n;
     
    29432908        };
    29442909        struct stack { node(T) * head; };
    2945         void ?{}( stack(T) & s, stack(T) t ) { // copy
     2910        void ?{}( stack(T) & s ) { (s.head){ 0 }; }
     2911        void ?{}( stack(T) & s, stack(T) t ) {
    29462912                node(T) ** cr = &s.head;
    29472913                for ( node(T) * nx = t.head; nx; nx = nx->next ) {
     
    29572923                        nx = cr->next;
    29582924                        ^(*cr){};
    2959                         free( cr );
     2925                        free(cr);
    29602926                }
    29612927                head = 0;
    29622928        }
    2963 
    29642929\end{cfa}
    29652930&
    29662931\begin{cfa}[xleftmargin=0pt,aboveskip=0pt,belowskip=0pt]
    2967         void ?{}( stack(T) & s ) { (s.head){ 0 }; }
    29682932        void ^?{}( stack(T) & s) { clear( s ); }
    29692933        stack(T) ?=?( stack(T) & s, stack(T) t ) {
     
    29902954        }
    29912955}
     2956
    29922957\end{cfa}
    29932958\end{tabular}
     
    30383003                return *this;
    30393004        }
    3040         bool empty() const {
    3041                 return head == nullptr;
    3042         }
     3005        bool empty() const { return head == nullptr; }
    30433006        void push( const T & value ) {
    30443007                head = new node{ value, head };  /***/
     
    30523015        }
    30533016};
     3017
     3018
    30543019
    30553020\end{cfa}
     
    31013066                return *this;
    31023067        }
    3103         bool empty() const {
    3104                 return head == nullptr;
    3105         }
     3068        bool empty() const { return head == nullptr; }
    31063069        void push( const object & value ) {
    31073070                head = new node{ value, head }; /***/
     
    31163079};
    31173080
     3081
     3082
    31183083\end{cfa}
    31193084\end{tabular}
Note: See TracChangeset for help on using the changeset viewer.