Changeset 8e4aa05


Ignore:
Timestamp:
Mar 4, 2021, 7:40:25 PM (5 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
77d601f
Parents:
342af53 (diff), a5040fe (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

Files:
51 added
4 deleted
209 edited
34 moved

Legend:

Unmodified
Added
Removed
  • .gitignore

    r342af53 r8e4aa05  
    7979# generated by npm
    8080package-lock.json
     81
     82# generated by benchmark
     83benchmark/Cargo.toml
  • Jenkins/FullBuild

    r342af53 r8e4aa05  
    2121                                        gcc_7_x86_old: { trigger_build( 'gcc-7',   'x86', false ) },
    2222                                        gcc_6_x86_old: { trigger_build( 'gcc-6',   'x86', false ) },
    23                                         gcc_9_x64_old: { trigger_build( 'gcc-9',   'x64', false ) },
    24                                         gcc_8_x64_old: { trigger_build( 'gcc-8',   'x64', false ) },
    25                                         gcc_7_x64_old: { trigger_build( 'gcc-7',   'x64', false ) },
    26                                         gcc_6_x64_old: { trigger_build( 'gcc-6',   'x64', false ) },
    27                                         gcc_5_x64_old: { trigger_build( 'gcc-5',   'x64', false ) },
     23                                        gcc_9_x64_new: { trigger_build( 'gcc-9',   'x64', true  ) },
     24                                        gcc_8_x64_new: { trigger_build( 'gcc-8',   'x64', true  ) },
     25                                        gcc_7_x64_new: { trigger_build( 'gcc-7',   'x64', true  ) },
     26                                        gcc_6_x64_new: { trigger_build( 'gcc-6',   'x64', true  ) },
     27                                        gcc_5_x64_new: { trigger_build( 'gcc-5',   'x64', true  ) },
     28                                        clang_x64_new: { trigger_build( 'clang',   'x64', true  ) },
    2829                                        clang_x64_old: { trigger_build( 'clang',   'x64', false ) },
    29                                         clang_x64_new: { trigger_build( 'clang',   'x64', true  ) },
    3030                                )
    3131                        }
     
    6666
    6767def trigger_build(String cc, String arch, boolean new_ast) {
     68        // Randomly delay the builds by a random amount to avoid hitting the SC server to hard
     69        sleep(time: 5 * Math.random(), unit:"MINUTES")
     70
     71        // Run the build
     72        // Don't propagate, it doesn't play nice with our email setup
    6873        def result = build job: 'Cforall/master',               \
    6974                parameters: [                                           \
  • benchmark/Makefile.am

    r342af53 r8e4aa05  
    502502
    503503compile-io$(EXEEXT):
    504         $(CFACOMPILE) -DNO_COMPILED_PRAGMA -fsyntax-only -w $(testdir)/io1.cfa
     504        $(CFACOMPILE) -DNO_COMPILED_PRAGMA -fsyntax-only -w $(testdir)/io/io.cfa
    505505
    506506compile-monitor$(EXEEXT):
  • benchmark/io/http/filecache.cfa

    r342af53 r8e4aa05  
    44#include <string.h>
    55
     6#include <fstream.hfa>
    67#include <stdlib.hfa>
    78
     
    182183                conflicts += put_file( raw[i], fd );
    183184        }
    184         printf("Filled cache from path \"%s\" with %zu files\n", path, fcount);
     185        sout | "Filled cache from path \"" | path | "\" with" | fcount | "files";
    185186        if( conflicts > 0 ) {
    186                 printf("Found %d conflicts (seed: %u)\n", conflicts, options.file_cache.hash_seed);
     187                sout | "Found" | conflicts | "conflicts (seed: " | options.file_cache.hash_seed | ")";
    187188                #if defined(REJECT_CONFLICTS)
    188189                        abort("Conflicts found in the cache");
     
    191192
    192193        if(options.file_cache.list) {
    193                 printf("Listing files and exiting\n");
     194                sout | "Listing files and exiting";
    194195                for(i; fcount) {
    195196                        int s; char u;
    196197                        [s, u] = human_size(raw[i].size);
    197                         printf("%4d%c - %s\n", s, u, raw[i].file);
     198                        sout | s | u | "-" | raw[i].file;
    198199                        free(raw[i].file);
    199200                }
     
    208209
    209210[int *, int] filefds(int extra) {
     211        if(!options.file_cache.path) {
     212                int * data = alloc(extra);
     213                return [data, 0];
     214        }
     215
    210216        if(!file_cache.entries) {
    211217                abort("File cache not filled!\n");
  • benchmark/io/http/main.cfa

    r342af53 r8e4aa05  
    66#include <unistd.h>
    77extern "C" {
     8        #include <signal.h>
    89        #include <sys/socket.h>
    910        #include <netinet/in.h>
    1011}
    1112
     13#include <fstream.hfa>
    1214#include <kernel.hfa>
     15#include <iofwd.hfa>
    1316#include <stats.hfa>
    1417#include <time.hfa>
     
    2629
    2730//=============================================================================================
     31// Stats Printer
     32//============================================================================================='
     33
     34thread StatsPrinter {};
     35
     36void ?{}( StatsPrinter & this, cluster & cl ) {
     37        ((thread&)this){ "Stats Printer Thread", cl };
     38}
     39
     40void ^?{}( StatsPrinter & mutex this ) {}
     41
     42void main(StatsPrinter & this) {
     43        LOOP: for() {
     44                waitfor( ^?{} : this) {
     45                        break LOOP;
     46                }
     47                or else {}
     48
     49                sleep(10`s);
     50
     51                print_stats_now( *active_cluster(), CFA_STATS_READY_Q | CFA_STATS_IO );
     52        }
     53}
     54
     55//=============================================================================================
    2856// Globals
    2957//=============================================================================================
    30 struct ServerProc {
    31         processor self;
     58struct ServerCluster {
     59        cluster self;
     60        processor    * procs;
     61        // io_context   * ctxs;
     62        StatsPrinter * prnt;
     63
    3264};
    3365
    34 void ?{}( ServerProc & this ) {
    35         /* paranoid */ assert( options.clopts.instance != 0p );
    36         (this.self){ "Benchmark Processor", *options.clopts.instance };
     66void ?{}( ServerCluster & this ) {
     67        (this.self){ "Server Cluster", options.clopts.params };
     68
     69        this.procs = alloc(options.clopts.nprocs);
     70        for(i; options.clopts.nprocs) {
     71                (this.procs[i]){ "Benchmark Processor", this.self };
     72
     73                #if !defined(__CFA_NO_STATISTICS__)
     74                        if( options.clopts.procstats ) {
     75                                print_stats_at_exit( *this.procs, this.self.print_stats );
     76                        }
     77                        if( options.clopts.viewhalts ) {
     78                                print_halts( *this.procs );
     79                        }
     80                #endif
     81        }
     82
     83        if(options.stats) {
     84                this.prnt = alloc();
     85                (*this.prnt){ this.self };
     86        } else {
     87                this.prnt = 0p;
     88        }
    3789
    3890        #if !defined(__CFA_NO_STATISTICS__)
    39                 if( options.clopts.procstats ) {
    40                         print_stats_at_exit( this.self, options.clopts.instance->print_stats );
    41                 }
    42                 if( options.clopts.viewhalts ) {
    43                         print_halts( this.self );
    44                 }
     91                print_stats_at_exit( this.self, CFA_STATS_READY_Q | CFA_STATS_IO );
    4592        #endif
     93
     94        options.clopts.instance[options.clopts.cltr_cnt] = &this.self;
     95        options.clopts.cltr_cnt++;
     96}
     97
     98void ^?{}( ServerCluster & this ) {
     99        delete(this.prnt);
     100
     101        for(i; options.clopts.nprocs) {
     102                ^(this.procs[i]){};
     103        }
     104        free(this.procs);
     105
     106        ^(this.self){};
    46107}
    47108
     
    53114//============================================================================================='
    54115int main( int argc, char * argv[] ) {
     116        __sighandler_t s = 1p;
     117        signal(SIGPIPE, s);
     118
    55119        //===================
    56120        // Parse args
    57         const char * path = parse_options(argc, argv);
     121        parse_options(argc, argv);
    58122
    59123        //===================
    60124        // Open Files
    61         printf("Filling cache from %s\n", path);
    62         fill_cache( path );
     125        if( options.file_cache.path ) {
     126                sout | "Filling cache from" | options.file_cache.path;
     127                fill_cache( options.file_cache.path );
     128        }
    63129
    64130        //===================
    65131        // Open Socket
    66         printf("%ld : Listening on port %d\n", getpid(), options.socket.port);
     132        sout | getpid() | ": Listening on port" | options.socket.port;
    67133        int server_fd = socket(AF_INET, SOCK_STREAM, 0);
    68134        if(server_fd < 0) {
     
    84150                        if(errno == EADDRINUSE) {
    85151                                if(waited == 0) {
    86                                         printf("Waiting for port\n");
     152                                        sout | "Waiting for port";
    87153                                } else {
    88                                         printf("\r%d", waited);
    89                                         fflush(stdout);
     154                                        sout | "\r" | waited | nonl;
     155                                        flush( sout );
    90156                                }
    91157                                waited ++;
     
    106172        // Run Server Cluster
    107173        {
    108                 cluster cl = { "Server Cluster", options.clopts.params };
    109                 #if !defined(__CFA_NO_STATISTICS__)
    110                         print_stats_at_exit( cl, CFA_STATS_READY_Q | CFA_STATS_IO );
    111                 #endif
    112                 options.clopts.instance = &cl;
    113 
    114 
    115174                int pipe_cnt = options.clopts.nworkers * 2;
    116175                int pipe_off;
     
    122181                }
    123182
    124                 if(options.file_cache.fixed_fds) {
    125                         register_fixed_files(cl, fds, pipe_off);
    126                 }
     183                // if(options.file_cache.path && options.file_cache.fixed_fds) {
     184                //      register_fixed_files(cl, fds, pipe_off);
     185                // }
    127186
    128187                {
    129                         ServerProc procs[options.clopts.nprocs];
     188                        ServerCluster cl[options.clopts.nclusters];
    130189
    131190                        init_protocol();
     
    148207                                        unpark( workers[i] );
    149208                                }
    150                                 printf("%d workers started on %d processors\n", options.clopts.nworkers, options.clopts.nprocs);
     209                                sout | options.clopts.nworkers | "workers started on" | options.clopts.nprocs | "processors /" | options.clopts.nclusters | "clusters";
     210                                for(i; options.clopts.nclusters) {
     211                                        sout | options.clopts.thrd_cnt[i] | nonl;
     212                                }
     213                                sout | nl;
    151214                                {
    152215                                        char buffer[128];
    153                                         while(!feof(stdin)) {
    154                                                 fgets(buffer, 128, stdin);
     216                                        for() {
     217                                                int ret = cfa_read(0, buffer, 128, 0);
     218                                                if(ret == 0) break;
     219                                                if(ret < 0) abort( "main read error: (%d) %s\n", (int)errno, strerror(errno) );
     220                                                sout | "User wrote '" | "" | nonl;
     221                                                write(sout, buffer, ret - 1);
     222                                                sout | "'";
    155223                                        }
    156224
    157                                         printf("Shutting Down\n");
    158                                 }
    159 
     225                                        sout | "Shutdown received";
     226                                }
     227
     228                                sout | "Notifying connections..." | nonl; flush( sout );
    160229                                for(i; options.clopts.nworkers) {
    161                                         printf("Cancelling %p\n", (void*)workers[i].cancel.target);
    162230                                        workers[i].done = true;
    163                                         cancel(workers[i].cancel);
    164                                 }
    165 
    166                                 printf("Shutting down socket\n");
     231                                }
     232                                sout | "done";
     233
     234                                sout | "Shutting down socket..." | nonl; flush( sout );
    167235                                int ret = shutdown( server_fd, SHUT_RD );
    168                                 if( ret < 0 ) { abort( "shutdown error: (%d) %s\n", (int)errno, strerror(errno) ); }
     236                                if( ret < 0 ) {
     237                                        abort( "shutdown error: (%d) %s\n", (int)errno, strerror(errno) );
     238                                }
     239                                sout | "done";
    169240
    170241                                //===================
    171242                                // Close Socket
    172                                 printf("Closing Socket\n");
     243                                sout | "Closing Socket..." | nonl; flush( sout );
    173244                                ret = close( server_fd );
    174245                                if(ret < 0) {
    175246                                        abort( "close socket error: (%d) %s\n", (int)errno, strerror(errno) );
    176247                                }
    177                         }
    178                         printf("Workers Closed\n");
    179 
     248                                sout | "done";
     249
     250                                sout | "Stopping connection threads..." | nonl; flush( sout );
     251                        }
     252                        sout | "done";
     253
     254                        sout | "Stopping protocol threads..." | nonl; flush( sout );
    180255                        deinit_protocol();
    181                 }
    182 
     256                        sout | "done";
     257
     258                        sout | "Stopping processors/clusters..." | nonl; flush( sout );
     259                }
     260                sout | "done";
     261
     262                sout | "Closing splice fds..." | nonl; flush( sout );
    183263                for(i; pipe_cnt) {
    184264                        ret = close( fds[pipe_off + i] );
     
    188268                }
    189269                free(fds);
    190 
    191         }
     270                sout | "done";
     271
     272                sout | "Stopping processors..." | nonl; flush( sout );
     273        }
     274        sout | "done";
    192275
    193276        //===================
    194277        // Close Files
    195         printf("Closing Files\n");
    196         close_cache();
    197 }
     278        if( options.file_cache.path ) {
     279                sout | "Closing open files..." | nonl; flush( sout );
     280                close_cache();
     281                sout | "done";
     282        }
     283}
  • benchmark/io/http/options.cfa

    r342af53 r8e4aa05  
    99}
    1010
     11#include <bitmanip.hfa>
     12#include <fstream.hfa>
    1113#include <kernel.hfa>
    1214#include <parseargs.hfa>
     15#include <stdlib.hfa>
    1316
     17#include <stdlib.h>
    1418#include <string.h>
    1519
    1620Options options @= {
    1721        false, // log
     22        false, // stats
    1823
    1924        { // file_cache
     25                0,     // path
    2026                0,     // open_flags;
    2127                42u,   // hash_seed;
     
    3238
    3339        { // cluster
     40                1,     // nclusters;
    3441                1,     // nprocs;
    3542                1,     // nworkers;
    36                 0,     // flags;
     43                {},     // params;
    3744                false, // procstats
    3845                false, // viewhalts
     
    4148};
    4249
    43 const char * parse_options( int argc, char * argv[] ) {
    44         bool subthrd = false;
    45         bool eagrsub = false;
    46         bool fixedfd = false;
    47         bool sqkpoll = false;
    48         bool iokpoll = false;
    49         unsigned sublen = 16;
     50void parse_options( int argc, char * argv[] ) {
     51        // bool fixedfd = false;
     52        // bool sqkpoll = false;
     53        // bool iokpoll = false;
     54        unsigned nentries = 16;
     55        bool isolate = false;
     56
    5057
    5158        static cfa_option opt[] = {
    52                 {'p', "port",           "Port the server will listen on", options.socket.port},
    53                 {'c', "cpus",           "Number of processors to use", options.clopts.nprocs},
    54                 {'L', "log",            "Enable logs", options.log, parse_settrue},
    55                 {'t', "threads",        "Number of worker threads to use", options.clopts.nworkers},
    56                 {'b', "accept-backlog", "Maximum number of pending accepts", options.socket.backlog},
    57                 {'r', "request_len",    "Maximum number of bytes in the http request, requests with more data will be answered with Http Code 414", options.socket.buflen},
    58                 {'S', "seed",           "seed to use for hashing", options.file_cache.hash_seed },
    59                 {'C', "cache-size",     "Size of the cache to use, if set to small, will uses closes power of 2", options.file_cache.size },
    60                 {'l', "list-files",     "List the files in the specified path and exit", options.file_cache.list, parse_settrue },
    61                 {'s', "submitthread",   "If set, cluster uses polling thread to submit I/O", subthrd, parse_settrue },
    62                 {'e', "eagersubmit",    "If set, cluster submits I/O eagerly but still aggregates submits", eagrsub, parse_settrue},
    63                 {'f', "fixed-fds",      "If set, files are open eagerly and pre-registered with the cluster", fixedfd, parse_settrue},
    64                 {'k', "kpollsubmit",    "If set, cluster uses IORING_SETUP_SQPOLL, implies -f", sqkpoll, parse_settrue },
    65                 {'i', "kpollcomplete",  "If set, cluster uses IORING_SETUP_IOPOLL", iokpoll, parse_settrue },
    66                 {'L', "submitlength",   "Max number of submitions that can be submitted together", sublen },
     59                { 'p', "port",           "Port the server will listen on", options.socket.port},
     60                { 'c', "cpus",           "Number of processors to use", options.clopts.nprocs},
     61                { 't', "threads",        "Number of worker threads to use", options.clopts.nworkers},
     62                {'\0', "isolate",        "Create one cluster per processor", isolate, parse_settrue},
     63                {'\0', "log",            "Enable logs", options.log, parse_settrue},
     64                {'\0', "stats",          "Enable statistics", options.stats, parse_settrue},
     65                {'\0', "accept-backlog", "Maximum number of pending accepts", options.socket.backlog},
     66                {'\0', "request_len",    "Maximum number of bytes in the http request, requests with more data will be answered with Http Code 414", options.socket.buflen},
     67                {'\0', "seed",           "seed to use for hashing", options.file_cache.hash_seed },
     68                {'\0', "cache-size",     "Size of the cache to use, if set to small, will uses closes power of 2", options.file_cache.size },
     69                {'\0', "list-files",     "List the files in the specified path and exit", options.file_cache.list, parse_settrue },
     70                // { 'f', "fixed-fds",      "If set, files are open eagerly and pre-registered with the cluster", fixedfd, parse_settrue},
     71                // { 'k', "kpollsubmit",    "If set, cluster uses IORING_SETUP_SQPOLL, implies -f", sqkpoll, parse_settrue },
     72                // { 'i', "kpollcomplete",  "If set, cluster uses IORING_SETUP_IOPOLL", iokpoll, parse_settrue },
     73                {'e', "numentries",     "Number of I/O entries", nentries },
    6774
    6875        };
     
    7279        parse_args( argc, argv, opt, opt_cnt, "[OPTIONS]... [PATH]\ncforall http server", left );
    7380
    74         options.clopts.params.poller_submits = subthrd;
    75         options.clopts.params.eager_submits  = eagrsub;
    76 
    77         if( fixedfd ) {
    78                 options.file_cache.fixed_fds = true;
     81        if( !is_pow2(nentries) ) {
     82                unsigned v = nentries;
     83                v--;
     84                v |= v >> 1;
     85                v |= v >> 2;
     86                v |= v >> 4;
     87                v |= v >> 8;
     88                v |= v >> 16;
     89                v++;
     90                serr | "Warning: num_entries not a power of 2" | '(' | nentries | ')' | "raising to " | v;
     91                nentries = v;
     92        }
     93        if(isolate) {
     94                options.clopts.nclusters = options.clopts.nprocs;
     95                options.clopts.nprocs = 1;
     96        }
     97        options.clopts.params.num_entries = nentries;
     98        options.clopts.instance = alloc(options.clopts.nclusters);
     99        options.clopts.thrd_cnt = alloc(options.clopts.nclusters);
     100        options.clopts.cltr_cnt = 0;
     101        for(i; options.clopts.nclusters) {
     102                options.clopts.thrd_cnt[i] = 0;
    79103        }
    80104
    81         if( sqkpoll ) {
    82                 options.clopts.params.poll_submit = true;
    83                 options.file_cache.fixed_fds = true;
    84         }
    85105
    86         if( iokpoll ) {
    87                 options.clopts.params.poll_complete = true;
    88                 options.file_cache.open_flags |= O_DIRECT;
    89         }
     106        // if( fixedfd ) {
     107        //      options.file_cache.fixed_fds = true;
     108        // }
    90109
    91         options.clopts.params.num_ready = sublen;
     110        // if( sqkpoll ) {
     111        //      options.file_cache.fixed_fds = true;
     112        // }
    92113
    93         if( left[0] == 0p ) { return "."; }
     114        // if( iokpoll ) {
     115        //      options.file_cache.open_flags |= O_DIRECT;
     116        // }
     117
     118        if( left[0] == 0p ) { return; }
    94119
    95120        const char * path = left[0];
     
    97122
    98123        if( left[0] != 0p ) {
    99                 abort("Too many trailing arguments!\n");
     124                serr | "Too many trailing arguments!" | '\'' | path | '\'';
     125                while(left[0] != 0p) {
     126                        serr | " - " | left[0];
     127                        left++;
     128                }
     129                exit(EXIT_FAILURE);
    100130        }
    101131
    102         return path;
     132        options.file_cache.path = path;
    103133}
  • benchmark/io/http/options.hfa

    r342af53 r8e4aa05  
    99struct Options {
    1010        bool log;
     11        bool stats;
    1112
    1213        struct {
     14                const char * path;
    1315                int open_flags;
    1416                uint32_t hash_seed;
     
    2527
    2628        struct {
     29                int nclusters;
    2730                int nprocs;
    2831                int nworkers;
     
    3033                bool procstats;
    3134                bool viewhalts;
    32                 cluster * instance;
     35                cluster ** instance;
     36                size_t   * thrd_cnt;
     37                size_t     cltr_cnt;
    3338        } clopts;
    3439};
     
    3641extern Options options;
    3742
    38 const char * parse_options( int argc, char * argv[] );
     43void parse_options( int argc, char * argv[] );
  • benchmark/io/http/protocol.cfa

    r342af53 r8e4aa05  
    55        #include <fcntl.h>
    66}
     7
     8#include <fstream.hfa>
    79#include <iofwd.hfa>
    810
     
    1113extern "C" {
    1214      int snprintf ( char * s, size_t n, const char * format, ... );
    13         #include <linux/io_uring.h>
     15        // #include <linux/io_uring.h>
    1416}
    1517#include <string.h>
     
    1820#include "options.hfa"
    1921
    20 const char * volatile date = 0p;
    21 
    22 const char * http_msgs[] = {
    23         "HTTP/1.1 200 OK\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: %zu \n\n",
    24         "HTTP/1.1 400 Bad Request\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
    25         "HTTP/1.1 404 Not Found\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
    26         "HTTP/1.1 413 Payload Too Large\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
    27         "HTTP/1.1 414 URI Too Long\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
    28 };
     22#define PLAINTEXT_1WRITE
     23#define PLAINTEXT_NOCOPY
     24
     25struct https_msg_str {
     26        char msg[512];
     27        size_t len;
     28};
     29
     30const https_msg_str * volatile http_msgs[KNOWN_CODES] = { 0 };
    2931
    3032_Static_assert( KNOWN_CODES == (sizeof(http_msgs ) / sizeof(http_msgs [0])));
    3133
    32 const int http_codes[] = {
     34const int http_codes[KNOWN_CODES] = {
     35        200,
    3336        200,
    3437        400,
    3538        404,
     39        405,
     40        408,
    3641        413,
    3742        414,
     
    4752        while(len > 0) {
    4853                // Call write
    49                 int ret = cfa_write(fd, it, len, 0, -1`s, 0p, 0p);
    50                 // int ret = write(fd, it, len);
    51                 if( ret < 0 ) { if( errno != EAGAIN && errno != EWOULDBLOCK) abort( "'answer error' error: (%d) %s\n", (int)errno, strerror(errno) ); }
     54                int ret = cfa_send(fd, it, len, 0, CFA_IO_LAZY);
     55                if( ret < 0 ) {
     56                        if( errno == ECONNRESET || errno == EPIPE ) return -ECONNRESET;
     57                        if( errno == EAGAIN || errno == EWOULDBLOCK) return -EAGAIN;
     58
     59                        abort( "'answer error' error: (%d) %s\n", (int)errno, strerror(errno) );
     60                }
    5261
    5362                // update it/len
     
    6170        /* paranoid */ assert( code < KNOWN_CODES && code != OK200 );
    6271        int idx = (int)code;
    63         return answer( fd, http_msgs[idx], strlen( http_msgs[idx] ) );
     72        return answer( fd, http_msgs[idx]->msg, http_msgs[idx]->len );
    6473}
    6574
    6675int answer_header( int fd, size_t size ) {
    67         const char * fmt = http_msgs[OK200];
    68         int len = 200;
    69         char buffer[len];
    70         len = snprintf(buffer, len, fmt, date, size);
     76        char buffer[512];
     77        char * it = buffer;
     78        memcpy(it, http_msgs[OK200]->msg, http_msgs[OK200]->len);
     79        it += http_msgs[OK200]->len;
     80        int len = http_msgs[OK200]->len;
     81        len += snprintf(it, 512 - len, "%d \n\n", size);
    7182        return answer( fd, buffer, len );
    7283}
    7384
    74 int answer_plain( int fd, char buffer[], size_t size ) {
    75         int ret = answer_header(fd, size);
     85#if defined(PLAINTEXT_NOCOPY)
     86int answer_plaintext( int fd ) {
     87        return answer(fd, http_msgs[OK200_PlainText]->msg, http_msgs[OK200_PlainText]->len + 1); // +1 cause snprintf doesn't count nullterminator
     88}
     89#elif defined(PLAINTEXT_1WRITE)
     90int answer_plaintext( int fd ) {
     91        char text[] = "Hello, World!\n";
     92        char buffer[512 + sizeof(text)];
     93        char * it = buffer;
     94        memcpy(it, http_msgs[OK200]->msg, http_msgs[OK200]->len);
     95        it += http_msgs[OK200]->len;
     96        int len = http_msgs[OK200]->len;
     97        int r = snprintf(it, 512 - len, "%d \n\n", sizeof(text));
     98        it += r;
     99        len += r;
     100        memcpy(it, text, sizeof(text));
     101        return answer(fd, buffer, len + sizeof(text));
     102}
     103#else
     104int answer_plaintext( int fd ) {
     105        char text[] = "Hello, World!\n";
     106        int ret = answer_header(fd, sizeof(text));
    76107        if( ret < 0 ) return ret;
    77         return answer(fd, buffer, size);
    78 }
     108        return answer(fd, text, sizeof(text));
     109}
     110#endif
    79111
    80112int answer_empty( int fd ) {
     
    83115
    84116
    85 [HttpCode code, bool closed, * const char file, size_t len] http_read(int fd, []char buffer, size_t len, io_cancellation * cancel) {
     117[HttpCode code, bool closed, * const char file, size_t len] http_read(int fd, []char buffer, size_t len) {
    86118        char * it = buffer;
    87119        size_t count = len - 1;
     
    89121        READ:
    90122        for() {
    91                 int ret = cfa_read(fd, (void*)it, count, 0, -1`s, cancel, 0p);
     123                int ret = cfa_recv(fd, (void*)it, count, 0, CFA_IO_LAZY);
    92124                // int ret = read(fd, (void*)it, count);
    93125                if(ret == 0 ) return [OK200, true, 0, 0];
    94126                if(ret < 0 ) {
    95127                        if( errno == EAGAIN || errno == EWOULDBLOCK) continue READ;
    96                         // if( errno == EINVAL ) return [E400, true, 0, 0];
     128                        if( errno == ECONNRESET ) return [E408, true, 0, 0];
     129                        if( errno == EPIPE ) return [E408, true, 0, 0];
    97130                        abort( "read error: (%d) %s\n", (int)errno, strerror(errno) );
    98131                }
     
    108141        }
    109142
    110         if( options.log ) printf("%.*s\n", rlen, buffer);
     143        if( options.log ) {
     144                write(sout, buffer, rlen);
     145                sout | nl;
     146        }
    111147
    112148        it = buffer;
     
    119155}
    120156
    121 void sendfile( int pipe[2], int fd, int ans_fd, size_t count ) {
     157int sendfile( int pipe[2], int fd, int ans_fd, size_t count ) {
    122158        unsigned sflags = SPLICE_F_MOVE; // | SPLICE_F_MORE;
    123159        off_t offset = 0;
    124160        ssize_t ret;
    125161        SPLICE1: while(count > 0) {
    126                 ret = cfa_splice(ans_fd, &offset, pipe[1], 0p, count, sflags, 0, -1`s, 0p, 0p);
    127                 // ret = splice(ans_fd, &offset, pipe[1], 0p, count, sflags);
     162                ret = cfa_splice(ans_fd, &offset, pipe[1], 0p, count, sflags, CFA_IO_LAZY);
    128163                if( ret < 0 ) {
    129164                        if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE1;
     165                        if( errno == ECONNRESET ) return -ECONNRESET;
     166                        if( errno == EPIPE ) return -EPIPE;
    130167                        abort( "splice [0] error: (%d) %s\n", (int)errno, strerror(errno) );
    131168                }
     
    135172                size_t in_pipe = ret;
    136173                SPLICE2: while(in_pipe > 0) {
    137                         ret = cfa_splice(pipe[0], 0p, fd, 0p, in_pipe, sflags, 0, -1`s, 0p, 0p);
    138                         // ret = splice(pipe[0], 0p, fd, 0p, in_pipe, sflags);
     174                        ret = cfa_splice(pipe[0], 0p, fd, 0p, in_pipe, sflags, CFA_IO_LAZY);
    139175                        if( ret < 0 ) {
    140176                                if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE2;
     177                                if( errno == ECONNRESET ) return -ECONNRESET;
     178                                if( errno == EPIPE ) return -EPIPE;
    141179                                abort( "splice [1] error: (%d) %s\n", (int)errno, strerror(errno) );
    142180                        }
     
    145183
    146184        }
     185        return count;
    147186}
    148187
     
    153192#include <thread.hfa>
    154193
     194const char * original_http_msgs[] = {
     195        "HTTP/1.1 200 OK\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: ",
     196        "HTTP/1.1 200 OK\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 15\n\nHello, World!\n",
     197        "HTTP/1.1 400 Bad Request\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
     198        "HTTP/1.1 404 Not Found\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
     199        "HTTP/1.1 405 Method Not Allowed\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
     200        "HTTP/1.1 408 Request Timeout\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
     201        "HTTP/1.1 413 Payload Too Large\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
     202        "HTTP/1.1 414 URI Too Long\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
     203};
     204
    155205struct date_buffer {
    156         char buff[100];
     206        https_msg_str strs[KNOWN_CODES];
    157207};
    158208
     
    163213
    164214void ?{}( DateFormater & this ) {
    165         ((thread&)this){ "Server Date Thread", *options.clopts.instance };
     215        ((thread&)this){ "Server Date Thread", *options.clopts.instance[0] };
    166216        this.idx = 0;
    167         memset( this.buffers[0].buff, 0, sizeof(this.buffers[0]) );
    168         memset( this.buffers[1].buff, 0, sizeof(this.buffers[1]) );
     217        memset( &this.buffers[0], 0, sizeof(this.buffers[0]) );
     218        memset( &this.buffers[1], 0, sizeof(this.buffers[1]) );
    169219}
    170220
     
    176226                or else {}
    177227
     228
     229                char buff[100];
    178230                Time now = getTimeNsec();
    179 
    180                 strftime( this.buffers[this.idx].buff, 100, "%a, %d %b %Y %H:%M:%S %Z", now );
    181 
    182                 char * next = this.buffers[this.idx].buff;
    183                 __atomic_exchange_n((char * volatile *)&date, next, __ATOMIC_SEQ_CST);
     231                strftime( buff, 100, "%a, %d %b %Y %H:%M:%S %Z", now );
     232                sout | "Updated date to '" | buff | "'";
     233
     234                for(i; KNOWN_CODES) {
     235                        size_t len = snprintf( this.buffers[this.idx].strs[i].msg, 512, original_http_msgs[i], buff );
     236                        this.buffers[this.idx].strs[i].len = len;
     237                }
     238
     239                for(i; KNOWN_CODES) {
     240                        https_msg_str * next = &this.buffers[this.idx].strs[i];
     241                        __atomic_exchange_n((https_msg_str * volatile *)&http_msgs[i], next, __ATOMIC_SEQ_CST);
     242                }
    184243                this.idx = (this.idx + 1) % 2;
     244
     245                sout | "Date thread sleeping";
    185246
    186247                sleep(1`s);
  • benchmark/io/http/protocol.hfa

    r342af53 r8e4aa05  
    11#pragma once
    2 
    3 struct io_cancellation;
    42
    53enum HttpCode {
    64        OK200 = 0,
     5        OK200_PlainText,
    76        E400,
    87        E404,
     8        E405,
     9        E408,
    910        E413,
    1011        E414,
     
    1617int answer_error( int fd, HttpCode code );
    1718int answer_header( int fd, size_t size );
    18 int answer_plain( int fd, char buffer [], size_t size );
     19int answer_plaintext( int fd );
    1920int answer_empty( int fd );
    2021
    21 [HttpCode code, bool closed, * const char file, size_t len] http_read(int fd, []char buffer, size_t len, io_cancellation *);
     22[HttpCode code, bool closed, * const char file, size_t len] http_read(int fd, []char buffer, size_t len);
    2223
    23 void sendfile( int pipe[2], int fd, int ans_fd, size_t count );
     24int sendfile( int pipe[2], int fd, int ans_fd, size_t count );
  • benchmark/io/http/worker.cfa

    r342af53 r8e4aa05  
    66#include <unistd.h>
    77
     8#include <fstream.hfa>
    89#include <iofwd.hfa>
    910
     
    1617//=============================================================================================
    1718void ?{}( Worker & this ) {
    18         ((thread&)this){ "Server Worker Thread", *options.clopts.instance };
     19        size_t cli = rand() % options.clopts.cltr_cnt;
     20        ((thread&)this){ "Server Worker Thread", *options.clopts.instance[cli] };
     21        options.clopts.thrd_cnt[cli]++;
    1922        this.pipe[0] = -1;
    2023        this.pipe[1] = -1;
     
    3336        CONNECTION:
    3437        for() {
    35                 if( options.log ) printf("=== Accepting connection ===\n");
    36                 int fd = cfa_accept4( this.[sockfd, addr, addrlen, flags], 0, -1`s, &this.cancel, 0p );
    37                 // int fd = accept4( this.[sockfd, addr, addrlen, flags] );
     38                if( options.log ) sout | "=== Accepting connection ===";
     39                int fd = cfa_accept4( this.[sockfd, addr, addrlen, flags], CFA_IO_LAZY );
    3840                if(fd < 0) {
    3941                        if( errno == ECONNABORTED ) break;
    40                         if( errno == EINVAL && this.done ) break;
     42                        if( this.done && (errno == EINVAL || errno == EBADF) ) break;
    4143                        abort( "accept error: (%d) %s\n", (int)errno, strerror(errno) );
    4244                }
     45                if(this.done) break;
    4346
    44                 if( options.log ) printf("=== New connection %d, waiting for requests ===\n", fd);
     47                if( options.log ) sout | "=== New connection" | fd | "" | ", waiting for requests ===";
    4548                REQUEST:
    4649                for() {
     
    5356                        size_t len = options.socket.buflen;
    5457                        char buffer[len];
    55                         if( options.log ) printf("=== Reading request ===\n");
    56                         [code, closed, file, name_size] = http_read(fd, buffer, len, &this.cancel);
     58                        if( options.log ) sout | "=== Reading request ===";
     59                        [code, closed, file, name_size] = http_read(fd, buffer, len);
    5760
    5861                        // if we are done, break out of the loop
    59                         if( closed ) {
    60                                 if( options.log ) printf("=== Connection closed ===\n");
    61                                 close(fd);
    62                                 continue CONNECTION;
    63                         }
     62                        if( closed ) break REQUEST;
    6463
    6564                        // If this wasn't a request retrun 400
    6665                        if( code != OK200 ) {
    67                                 printf("=== Invalid Request : %d ===\n", code_val(code));
     66                                sout | "=== Invalid Request :" | code_val(code) | "===";
    6867                                answer_error(fd, code);
    6968                                continue REQUEST;
     
    7170
    7271                        if(0 == strncmp(file, "plaintext", min(name_size, sizeof("plaintext") ))) {
    73                                 if( options.log ) printf("=== Request for /plaintext ===\n");
     72                                if( options.log ) sout | "=== Request for /plaintext ===";
    7473
    75                                 char text[] = "Hello, World!\n";
     74                                int ret = answer_plaintext(fd);
     75                                if( ret == -ECONNRESET ) break REQUEST;
    7676
    77                                 // Send the header
    78                                 answer_plain(fd, text, sizeof(text));
    79 
    80                                 if( options.log ) printf("=== Answer sent ===\n");
     77                                if( options.log ) sout | "=== Answer sent ===";
    8178                                continue REQUEST;
    8279                        }
    8380
    8481                        if(0 == strncmp(file, "ping", min(name_size, sizeof("ping") ))) {
    85                                 if( options.log ) printf("=== Request for /ping ===\n");
     82                                if( options.log ) sout | "=== Request for /ping ===";
    8683
    8784                                // Send the header
    88                                 answer_empty(fd);
     85                                int ret = answer_empty(fd);
     86                                if( ret == -ECONNRESET ) break REQUEST;
    8987
    90                                 if( options.log ) printf("=== Answer sent ===\n");
     88                                if( options.log ) sout | "=== Answer sent ===";
    9189                                continue REQUEST;
    9290                        }
    9391
    94                         if( options.log ) printf("=== Request for file %.*s ===\n", (int)name_size, file);
     92                        if( options.log ) {
     93                                sout | "=== Request for file " | nonl;
     94                                write(sout, file, name_size);
     95                                sout | " ===";
     96                        }
     97
     98                        if( !options.file_cache.path ) {
     99                                if( options.log ) {
     100                                        sout | "=== File Not Found (" | nonl;
     101                                        write(sout, file, name_size);
     102                                        sout | ") ===";
     103                                }
     104                                answer_error(fd, E405);
     105                                continue REQUEST;
     106                        }
    95107
    96108                        // Get the fd from the file cache
     
    101113                        // If we can't find the file, return 404
    102114                        if( ans_fd < 0 ) {
    103                                 printf("=== File Not Found ===\n");
     115                                if( options.log ) {
     116                                        sout | "=== File Not Found (" | nonl;
     117                                        write(sout, file, name_size);
     118                                        sout | ") ===";
     119                                }
    104120                                answer_error(fd, E404);
    105121                                continue REQUEST;
     
    107123
    108124                        // Send the header
    109                         answer_header(fd, count);
     125                        int ret = answer_header(fd, count);
     126                        if( ret == -ECONNRESET ) break REQUEST;
    110127
    111128                        // Send the desired file
    112                         sendfile( this.pipe, fd, ans_fd, count);
     129                        ret = sendfile( this.pipe, fd, ans_fd, count);
     130                        if( ret == -ECONNRESET ) break REQUEST;
    113131
    114                         if( options.log ) printf("=== Answer sent ===\n");
     132                        if( options.log ) sout | "=== Answer sent ===";
    115133                }
     134
     135                if( options.log ) sout | "=== Connection closed ===";
     136                close(fd);
     137                continue CONNECTION;
    116138        }
    117139}
  • benchmark/io/http/worker.hfa

    r342af53 r8e4aa05  
    1717        socklen_t * addrlen;
    1818        int flags;
    19         io_cancellation cancel;
    2019        volatile bool done;
    2120};
  • doc/LaTeXmacros/common.tex

    r342af53 r8e4aa05  
    1111%% Created On       : Sat Apr  9 10:06:17 2016
    1212%% Last Modified By : Peter A. Buhr
    13 %% Last Modified On : Mon Oct  5 09:34:46 2020
    14 %% Update Count     : 464
     13%% Last Modified On : Sun Feb 14 15:52:46 2021
     14%% Update Count     : 524
    1515%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    1616
     
    3232\setlist[enumerate]{listparindent=\parindent}% global
    3333\setlist[enumerate,2]{leftmargin=\parindent,labelsep=*,align=parleft,label=\alph*.}% local
    34 \setlist[description]{itemsep=0pt,listparindent=\parindent,leftmargin=\parindent,labelsep=1.5ex}
     34\setlist[description]{topsep=0.5ex,itemsep=0pt,listparindent=\parindent,leftmargin=\parindent,labelsep=1.5ex}
    3535
    3636% Names used in the document.
    3737
    3838\usepackage{xspace}
    39 \newcommand{\CFAIcon}{\textsf{C}\raisebox{\depth}{\rotatebox{180}{\textsf{A}}}\xspace} % Cforall symbolic name
    40 \newcommand{\CFA}{\protect\CFAIcon}             % safe for section/caption
    41 \newcommand{\CFL}{\textrm{Cforall}\xspace} % Cforall symbolic name
    42 \newcommand{\Celeven}{\textrm{C11}\xspace} % C11 symbolic name
    43 \newcommand{\CC}{\textrm{C}\kern-.1em\hbox{+\kern-.25em+}\xspace} % C++ symbolic name
    44 \newcommand{\CCeleven}{\textrm{C}\kern-.1em\hbox{+\kern-.25em+}11\xspace} % C++11 symbolic name
    45 \newcommand{\CCfourteen}{\textrm{C}\kern-.1em\hbox{+\kern-.25em+}14\xspace} % C++14 symbolic name
    46 \newcommand{\CCseventeen}{\textrm{C}\kern-.1em\hbox{+\kern-.25em+}17\xspace} % C++17 symbolic name
    47 \newcommand{\CCtwenty}{\textrm{C}\kern-.1em\hbox{+\kern-.25em+}20\xspace} % C++20 symbolic name
     39\newcommand{\CFAIcon}{\textsf{C}\raisebox{\depth}{\rotatebox{180}{\textsf{A}}}} % Cforall icon
     40\newcommand{\CFA}{\protect\CFAIcon\xspace}                      % CFA symbolic name
     41\newcommand{\CFL}{\textrm{Cforall}\xspace}                      % Cforall non-icon name
     42\newcommand{\Celeven}{\textrm{C11}\xspace}                      % C11 symbolic name
     43\newcommand{\CCIcon}{\textrm{C}\kern-.1em\hbox{+\kern-.25em+}} % C++ icon
     44\newcommand{\CC}{\protect\CCIcon\xspace}                        % C++ symbolic name
     45% numbers disallowed in latex variables names => use number names
     46\newcommand{\CCeleven}{\protect\CCIcon{11}\xspace}      % C++11 symbolic name
     47\newcommand{\CCfourteen}{\protect\CCIcon{14}\xspace} % C++14 symbolic name
     48\newcommand{\CCseventeen}{\protect\CCIcon{17}\xspace} % C++17 symbolic name
     49\newcommand{\CCtwenty}{\protect\CCIcon{20}\xspace}      % C++20 symbolic name
    4850\newcommand{\Csharp}{C\raisebox{-0.7ex}{\Large$^\sharp$}\xspace} % C# symbolic name
    4951
    5052%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    5153
     54% remove special-character warning in PDF side-bar names
    5255\makeatletter
     56\@ifpackageloaded{hyperref}{
     57  \pdfstringdefDisableCommands{
     58  \def\CFA{\CFL}
     59  \def\Celeven{C11\xspace}
     60  \def\CC{C++\xspace}
     61  \def\CCeleven{C++11\xspace}
     62  \def\CCfourteen{C++14\xspace}
     63  \def\CCseventeen{C++17\xspace}
     64  \def\CCtwenty{C++20\xspace}
     65  \def\Csharp{C\#\xspace}
     66  \def\lstinline{\xspace}% must use {} as delimiters, e.g., \lstinline{...}
     67  }{}
     68}
     69
    5370% parindent is relative, i.e., toggled on/off in environments like itemize, so store the value for
    5471% use rather than use \parident directly.
     
    8198    \vskip 50\p@
    8299  }}
    83 \renewcommand\section{\@startsection{section}{1}{\z@}{-3.5ex \@plus -1ex \@minus -.2ex}{1.75ex \@plus .2ex}{\normalfont\large\bfseries}}
    84 \renewcommand\subsection{\@startsection{subsection}{2}{\z@}{-3.25ex \@plus -1ex \@minus -.2ex}{1.5ex \@plus .2ex}{\normalfont\normalsize\bfseries}}
     100\renewcommand\section{\@startsection{section}{1}{\z@}{-3.0ex \@plus -1ex \@minus -.2ex}{1.5ex \@plus .2ex}{\normalfont\large\bfseries}}
     101\renewcommand\subsection{\@startsection{subsection}{2}{\z@}{-2.75ex \@plus -1ex \@minus -.2ex}{1.25ex \@plus .2ex}{\normalfont\normalsize\bfseries}}
    85102\renewcommand\subsubsection{\@startsection{subsubsection}{3}{\z@}{-2.5ex \@plus -1ex \@minus -.2ex}{1.0ex \@plus .2ex}{\normalfont\normalsize\bfseries}}
    86103\renewcommand\paragraph{\@startsection{paragraph}{4}{\z@}{-2.0ex \@plus -1ex \@minus -.2ex}{-1em}{\normalfont\normalsize\bfseries}}
     
    89106\newcommand{\italic}[1]{\emph{\hyperpage{#1}}}
    90107\newcommand{\Definition}[1]{\textbf{\hyperpage{#1}}}
    91 \newcommand{\see}[1]{\emph{see}~#1}
     108\newcommand{\see}[1]{(see #1)}
    92109
    93110% Define some commands that produce formatted index entries suitable for cross-references.
     
    129146% The star version does not lowercase the index information, e.g., \newterm*{IBM}.
    130147\newcommand{\newtermFontInline}{\emph}
    131 \newcommand{\newterm}{\@ifstar\@snewterm\@newterm}
     148\newcommand{\newterm}{\protect\@ifstar\@snewterm\@newterm}
    132149\newcommand{\@newterm}[2][\@empty]{\lowercase{\def\temp{#2}}{\newtermFontInline{#2}}\ifx#1\@empty\index{\temp}\else\index{#1@{\protect#2}}\fi}
    133150\newcommand{\@snewterm}[2][\@empty]{{\newtermFontInline{#2}}\ifx#1\@empty\index{#2}\else\index{#1@{\protect#2}}\fi}
     
    229246\usepackage{listings}                                                                   % format program code
    230247\usepackage{lstlang}
     248\usepackage{calc}                                                                               % latex arithmetic
     249
    231250\makeatletter
    232 
    233251\newcommand{\LstBasicStyle}[1]{{\lst@basicstyle{#1}}}
    234252\newcommand{\LstKeywordStyle}[1]{{\lst@basicstyle{\lst@keywordstyle{#1}}}}
    235253\newcommand{\LstCommentStyle}[1]{{\lst@basicstyle{\lst@commentstyle{#1}}}}
     254\newcommand{\LstStringStyle}[1]{{\lst@basicstyle{\lst@stringstyle{#1}}}}
    236255
    237256\newlength{\gcolumnposn}                                % temporary hack because lstlisting does not handle tabs correctly
     
    259278xleftmargin=\parindentlnth,                             % indent code to paragraph indentation
    260279extendedchars=true,                                             % allow ASCII characters in the range 128-255
    261 escapechar=§,                                                   % LaTeX escape in CFA code §...§ (section symbol), emacs: C-q M-'
    262 mathescape=true,                                                % LaTeX math escape in CFA code $...$
     280escapechar=\$,                                                  % LaTeX escape in CFA code §...§ (section symbol), emacs: C-q M-'
     281mathescape=false,                                               % LaTeX math escape in CFA code $...$
    263282keepspaces=true,                                                %
    264283showstringspaces=false,                                 % do not show spaces with cup
    265284showlines=true,                                                 % show blank lines at end of code
    266285aboveskip=4pt,                                                  % spacing above/below code block
    267 belowskip=3pt,
     286belowskip=0pt,
     287numberstyle=\footnotesize\sf,                   % numbering style
    268288% replace/adjust listing characters that look bad in sanserif
    269289literate={-}{\makebox[1ex][c]{\raisebox{0.4ex}{\rule{0.75ex}{0.1ex}}}}1 {^}{\raisebox{0.6ex}{$\scriptscriptstyle\land\,$}}1
     
    274294
    275295\ifdefined\CFALatin% extra Latin-1 escape characters
    276 \lstnewenvironment{cfa}[1][]{
     296\lstnewenvironment{cfa}[1][]{% necessary
    277297\lstset{
    278298language=CFA,
    279 moredelim=**[is][\color{red}]{®}{®},    % red highlighting ®...® (registered trademark symbol) emacs: C-q M-.
    280 moredelim=**[is][\color{blue}]{ß}{ß},   % blue highlighting ß...ß (sharp s symbol) emacs: C-q M-_
    281 moredelim=**[is][\color{OliveGreen}]{¢}{¢}, % green highlighting ¢...¢ (cent symbol) emacs: C-q M-"
    282 moredelim=[is][\lstset{keywords={}}]{¶}{¶}, % keyword escape ¶...¶ (pilcrow symbol) emacs: C-q M-^
    283 % replace/adjust listing characters that look bad in sanserif
    284 add to literate={`}{\ttfamily\upshape\hspace*{-0.1ex}`}1
     299moredelim=**[is][\color{red}]{@}{@},    % red highlighting @...@
     300%moredelim=**[is][\color{red}]{®}{®},   % red highlighting ®...® (registered trademark symbol) emacs: C-q M-.
     301%moredelim=**[is][\color{blue}]{ß}{ß},  % blue highlighting ß...ß (sharp s symbol) emacs: C-q M-_
     302%moredelim=**[is][\color{OliveGreen}]{¢}{¢}, % green highlighting ¢...¢ (cent symbol) emacs: C-q M-"
     303%moredelim=[is][\lstset{keywords={}}]{¶}{¶}, % keyword escape ¶...¶ (pilcrow symbol) emacs: C-q M-^
    285304}% lstset
    286 \lstset{#1}
     305\lstset{#1}% necessary
    287306}{}
    288307% inline code ©...© (copyright symbol) emacs: C-q M-)
    289308\lstMakeShortInline©                                    % single-character for \lstinline
    290309\else% regular ASCI characters
    291 \lstnewenvironment{cfa}[1][]{
     310\lstnewenvironment{cfa}[1][]{% necessary
    292311\lstset{
    293312language=CFA,
    294313escapechar=\$,                                                  % LaTeX escape in CFA code
     314mathescape=false,                                               % LaTeX math escape in CFA code $...$
    295315moredelim=**[is][\color{red}]{@}{@},    % red highlighting @...@
    296316}% lstset
    297 \lstset{#1}
     317\lstset{#1}% necessary
    298318}{}
    299319% inline code @...@ (at symbol)
  • doc/bibliography/pl.bib

    r342af53 r8e4aa05  
    688688    title       = {Asynchronous Exception Propagation in Blocked Tasks},
    689689    booktitle   = {4th International Workshop on Exception Handling (WEH.08)},
    690     organization= {16th International Symposium on the Foundations of Software Engineering (FSE 16)},
     690    optorganization= {16th International Symposium on the Foundations of Software Engineering (FSE 16)},
    691691    address     = {Atlanta, U.S.A},
    692692    month       = nov,
     
    17971797}
    17981798
    1799 @article{Delisle19,
     1799@article{Delisle20,
    18001800    keywords    = {concurrency, Cforall},
    18011801    contributer = {pabuhr@plg},
    18021802    author      = {Thierry Delisle and Peter A. Buhr},
    18031803    title       = {Advanced Control-flow and Concurrency in \textsf{C}$\mathbf{\forall}$},
    1804     year        = 2019,
     1804    year        = 2020,
    18051805    journal     = spe,
    1806     pages       = {1-33},
    1807     note        = {submitted},
     1806    pages       = {1-38},
     1807    note        = {\href{https://doi-org.proxy.lib.uwaterloo.ca/10.1002/spe.2925}{https://\-doi-org.proxy.lib.uwaterloo.ca/\-10.1002/\-spe.2925}},
     1808    note        = {},
    18081809}
    18091810
     
    72467247
    72477248@inproceedings{Edelson92,
    7248     keywords    = {persistence, pointers},
     7249    keywords    = {persistence, smart pointers},
    72497250    contributer = {pabuhr@plg},
    72507251    author      = {Daniel R. Edelson},
     
    72567257    year        = 1992,
    72577258    pages       = {1-19},
     7259}
     7260
     7261@incollection{smartpointers,
     7262    keywords    = {smart pointers},
     7263    contributer = {pabuhr@plg},
     7264    author      = {Andrei Alexandrescu},
     7265    title       = {Smart Pointers},
     7266    booktitle   = {Modern C++ Design: Generic Programming and Design Patterns Applied},
     7267    publisher   = {Addison-Wesley},
     7268    year        = 2001,
     7269    chapter     = 7,
     7270    optpages    = {?-?},
    72587271}
    72597272
     
    82458258}
    82468259
     8260@misc{vistorpattern,
     8261    keywords    = {visitor pattern},
     8262    contributer = {pabuhr@plg},
     8263    key         = {vistor pattern},
     8264    title       = {vistor pattern},
     8265    year        = 2020,
     8266    note        = {WikipediA},
     8267    howpublished= {\href{https://en.wikipedia.org/wiki/Visitor\_pattern}
     8268                  {https://\-en.wikipedia.org/\-wiki/\-Visitor\_pattern}},
     8269}
     8270
    82478271% W
    82488272
  • doc/papers/concurrency/mail2

    r342af53 r8e4aa05  
    12881288
    12891289
     1290From: "Wiley Online Proofing" <onlineproofing@eproofing.in>
     1291To: pabuhr@uwaterloo.ca
     1292Reply-To: eproofing@wiley.com
     1293Date: 3 Nov 2020 08:25:06 +0000
     1294Subject: Action: Proof of SPE_EV_SPE2925 for Software: Practice And Experience ready for review
     1295
     1296Dear Dr. Peter Buhr,
     1297
     1298The proof of your Software: Practice And Experience article Advanced control-flow in Cforall is now available for review:
     1299
     1300Edit Article https://wiley.eproofing.in/Proof.aspx?token=ab7739d5678447fbbe5036f3bcba2445081500061
     1301
     1302To review your article, please complete the following steps, ideally within 48 hours*, so we can publish your article as quickly as possible.
     1303
     13041. Open your proof in the online proofing system using the button above.
     13052. Check the article for correctness and respond to all queries.For instructions on using the system, please see the "Help" menu in the upper right corner.
     13063. Submit your changes by clicking the "Submit" button in the proofing system.
     1307
     1308Helpful Tips
     1309
     1310*  Your manuscript has been formatted following the style requirements for the journal. Any requested changes that go against journal style will not be made.
     1311*  Your proof will include queries. These must be replied to using the system before the proof can be submitted.
     1312*  The only acceptable changes at this stage are corrections to grammatical errors or data accuracy, or to provide higher resolution figure files (if requested by the typesetter).
     1313*  Any changes to scientific content or authorship will require editorial review and approval.
     1314*  Once your changes are complete, submit the article after which no additional corrections can be requested.
     1315*  Most authors complete their corrections within 48 hours. Returning any corrections promptly will accelerate publication of your article.
     1316
     1317If you encounter any problems or have questions, please contact the production office at (SPEproofs@wiley.com). For the quickest response, include the journal name and your article ID (found in the subject line) in all correspondence.
     1318
     1319Best regards,
     1320Software: Practice And Experience Production Office
     1321
     1322* We appreciate that the COVID-19 pandemic may create conditions for you that make it difficult for you to review your proof within standard timeframes. If you have any problems keeping to this schedule, please reach out to me at (SPEproofs@wiley.com) to discuss alternatives.
     1323
     1324
     1325
    12901326From: "Pacaanas, Joel -" <jpacaanas@wiley.com>
    12911327To: "Peter A. Buhr" <pabuhr@uwaterloo.ca>
     
    13451381
    13461382Since the proof was reset, your added corrections before has also been removed. Please add them back.
    1347 
    13481383Please return your corrections at your earliest convenience.
    13491384
     
    13841419Best regards,
    13851420Joel Pacaanas
     1421
     1422
     1423
     1424Date: Wed, 2 Dec 2020 08:49:52 +0000
     1425From: <cs-author@wiley.com>
     1426To: <pabuhr@uwaterloo.ca>
     1427Subject: Published: Your article is now published in Early View!
     1428
     1429Dear Peter Buhr,
     1430
     1431Your article Advanced Control-flow and Concurrency in C A in Software: Practice and Experience has the following publication status: Published as Early View
     1432
     1433To access your article, please click the following link to register or log in:
     1434
     1435  https://authorservices.wiley.com/index.html#register
     1436
     1437You can also access your published article via this link: http://dx.doi.org/10.1002/spe.2925
     1438
     1439If you need any assistance, please click here https://hub.wiley.com/community/support/authorservices to view our Help section.
     1440
     1441Sincerely,                                                                                 
     1442Wiley Author Services
     1443
     1444
     1445Date: Wed, 2 Dec 2020 02:16:23 -0500
     1446From: <no-reply@copyright.com>
     1447To: <pabuhr@uwaterloo.ca>
     1448CC: <SPEproofs@wiley.com>
     1449Subject: Please submit your publication fee(s) SPE2925
     1450 
     1451John Wiley and Sons
     1452Please submit your selection and payment for publication fee(s).
     1453
     1454Dear Peter A. Buhr,
     1455
     1456Congratulations, your article in Software: Practice and Experience has published online:
     1457
     1458Manuscript DOI: 10.1002/spe.2925
     1459Manuscript ID: SPE2925
     1460Manuscript Title: Advanced control-flow in Cforall
     1461Published by: John Wiley and Sons
     1462
     1463Please carefully review your publication options. If you wish your colour
     1464figures to be printed in colour, you must select and pay for that option now
     1465using the RightsLink e-commerce solution from CCC.
     1466
     1467  Review my options & pay charges 
     1468  https://oa.copyright.com/apc-payment-ui/overview?id=f46ba36a-2565-4c8d-8865-693bb94d87e5&chargeset=CHARGES 
     1469
     1470To review and pay your charge(s), please click here
     1471<https://oa.copyright.com/apc-payment-ui/overview?id=f46ba36a-2565-4c8d-8865-693bb94d87e5&chargeset=CHARGES>. You
     1472can also forward this link to another party for processing.
     1473
     1474To complete a secure transaction, you will need a RightsLink account
     1475<https://oa.copyright.com/apc-payment-ui/registration?id=f46ba36a-2565-4c8d-8865-693bb94d87e5&chargeset=CHARGES>. If
     1476you do not have one already, you will be prompted to register as you are
     1477checking out your author charges. This is a very quick process; the majority of
     1478your registration form will be pre-populated automatically with information we
     1479have already supplied to RightsLink.
     1480
     1481If you have any questions about these charges, please contact CCC Customer
     1482Service <wileysupport@copyright.com> using the information below. Please do not
     1483reply directly to this email as this is an automated email notification sent
     1484from an unmonitored account.
     1485
     1486Sincerely,
     1487John Wiley and Sons
     1488       
     1489Tel.: +1-877-622-5543 / +1-978-646-2777
     1490wileysupport@copyright.com
     1491www.copyright.com
     1492       
     1493Copyright Clearance Center
     1494RightsLink
     1495       
     1496This message (including attachments) is confidential, unless marked
     1497otherwise. It is intended for the addressee(s) only. If you are not an intended
     1498recipient, please delete it without further distribution and reply to the
     1499sender that you have received the message in error.
     1500
     1501
     1502
     1503From: "Pacaanas, Joel -" <jpacaanas@wiley.com>
     1504To: "Peter A. Buhr" <pabuhr@uwaterloo.ca>
     1505Subject: RE: Please submit your publication fee(s) SPE2925
     1506Date: Thu, 3 Dec 2020 08:45:10 +0000
     1507
     1508Dear Dr Buhr,
     1509
     1510Thank you for your email and concern with regard to the RightsLink account. As
     1511you have mentioned that all figures will be printed as black and white, then I
     1512have selected it manually from the system to proceed further.
     1513
     1514Best regards,
     1515Joel
     1516
     1517Joel Q. Pacaanas
     1518Production Editor
     1519On behalf of Wiley
     1520Manila
     1521We partner with global experts to further innovative research.
     1522
     1523E-mail: jpacaanas@wiley.com
     1524Tel: +632 88558618
     1525Fax: +632 5325 0768
     1526
     1527-----Original Message-----
     1528From: Peter A. Buhr [mailto:pabuhr@uwaterloo.ca]
     1529Sent: Thursday, December 3, 2020 12:28 AM
     1530To: SPE Proofs <speproofs@wiley.com>
     1531Subject: Re: Please submit your publication fee(s) SPE2925
     1532
     1533I am trying to complete the forms to submit my publication fee.
     1534
     1535I clicked all the boxs to print in Black and White, so there is no fee.
     1536
     1537I then am asked to create RightsLink account, which I did.
     1538
     1539However, it requires that I click a box agreeing to:
     1540
     1541   I consent to have my contact information shared with my publisher and/or
     1542   funding organization, as needed, to facilitate APC payment(s), reporting and
     1543   customer care.
     1544
     1545I do not agree to this sharing and will not click this button.
     1546
     1547How would you like to proceed?
     1548
     1549
     1550
     1551From: "Pacaanas, Joel -" <jpacaanas@wiley.com>
     1552To: "Peter A. Buhr" <pabuhr@uwaterloo.ca>
     1553Subject: RE: Please submit your publication fee(s) SPE2925
     1554Date: Fri, 4 Dec 2020 07:55:59 +0000
     1555
     1556Dear Peter,
     1557
     1558Yes, you are now done with this selection.
     1559
     1560Thank you.
     1561
     1562Best regards,
     1563Joel
     1564
     1565Joel Q. Pacaanas
     1566Production Editor
     1567On behalf of Wiley
     1568Manila
     1569We partner with global experts to further innovative research.
     1570
     1571E-mail: jpacaanas@wiley.com
     1572Tel: +632 88558618
     1573Fax: +632 5325 0768
     1574
     1575-----Original Message-----
     1576From: Peter A. Buhr [mailto:pabuhr@uwaterloo.ca]
     1577Sent: Thursday, December 3, 2020 10:29 PM
     1578To: Pacaanas, Joel - <jpacaanas@wiley.com>
     1579Subject: Re: Please submit your publication fee(s) SPE2925
     1580
     1581    Thank you for your email and concern with regard to the RightsLink
     1582    account. As you have mentioned that all figures will be printed as black and
     1583    white, then I have selected it manually from the system to proceed further.
     1584
     1585Just be clear, am I done? Meaning I do not have to go back to that web-page again.
  • doc/theses/andrew_beach_MMath/.gitignore

    r342af53 r8e4aa05  
    33
    44# Final Files:
    5 thesis.pdf
     5*.pdf
    66
    77# The Makefile here is not generated.
  • doc/theses/andrew_beach_MMath/Makefile

    r342af53 r8e4aa05  
    11### Makefile for Andrew Beach's Masters Thesis
    22
    3 DOC=thesis.pdf
     3DOC=uw-ethesis.pdf
    44BUILD=out
    55TEXSRC=$(wildcard *.tex)
     
    77STYSRC=$(wildcard *.sty)
    88CLSSRC=$(wildcard *.cls)
    9 TEXLIB= .:${BUILD}:
     9TEXLIB= .:../../LaTeXmacros:${BUILD}:
    1010BIBLIB= .:../../bibliography
    1111
     
    2929        ${LATEX} ${BASE}
    3030        ${BIBTEX} ${BUILD}/${BASE}
     31        ${LATEX} ${BASE}
    3132        ${GLOSSARY} ${BUILD}/${BASE}
    3233        ${LATEX} ${BASE}
  • doc/theses/andrew_beach_MMath/existing.tex

    r342af53 r8e4aa05  
    1 \chapter{\CFA{} Existing Features}
    2 
    3 \section{Overloading and extern}
    4 Cforall has overloading, allowing multiple definitions of the same name to
    5 be defined.
    6 
    7 This also adds name mangling so that the assembly symbols are unique for
    8 different overloads. For compatability with names in C there is also a
    9 syntax to diable the name mangling. These unmangled names cannot be overloaded
    10 but act as the interface between C and \CFA code.
    11 
    12 The syntax for disabling mangling is:
    13 \begin{lstlisting}
    14 extern "C" {
    15     ...
    16 }
    17 \end{lstlisting}
    18 
    19 To re-enable mangling once it is disabled the syntax is:
    20 \begin{lstlisting}
    21 extern "Cforall" {
    22     ...
    23 }
    24 \end{lstlisting}
    25 
    26 Both should occur at the declaration level and effect all the declarations
    27 in \texttt{...}. Neither care about the state of mangling when they begin
    28 and will return to that state after the group is finished. So re-enabling
    29 is only used to nest areas of mangled and unmangled declarations.
    30 
    31 \section{References}
    32 \CFA adds references to C. These are auto-dereferencing pointers and use the
    33 same syntax as pointers except they use ampersand (\codeCFA{\&}) instead of
    34 the asterisk (\codeCFA{*}). They can also be constaint or mutable, if they
    35 are mutable they may be assigned to by using the address-of operator
    36 (\codeCFA\&) which converts them into a pointer.
     1\chapter{\CFA Existing Features}
     2
     3\CFA (C-for-all)~\cite{Cforall} is an open-source project extending ISO C with
     4modern safety and productivity features, while still ensuring backwards
     5compatibility with C and its programmers.  \CFA is designed to have an
     6orthogonal feature-set based closely on the C programming paradigm
     7(non-object-oriented) and these features can be added incrementally to an
     8existing C code-base allowing programmers to learn \CFA on an as-needed basis.
     9
     10Only those \CFA features pertinent to this thesis are discussed.  Many of the
     11\CFA syntactic and semantic features used in the thesis should be fairly
     12obvious to the reader.
     13
     14\section{Overloading and \lstinline{extern}}
     15\CFA has extensive overloading, allowing multiple definitions of the same name
     16to be defined.~\cite{Moss18}
     17\begin{cfa}
     18char i; int i; double i;                        $\C[3.75in]{// variable overload}$
     19int f(); double f();                            $\C{// return overload}$
     20void g( int ); void g( double );        $\C{// parameter overload}\CRT$
     21\end{cfa}
     22This feature requires name mangling so the assembly symbols are unique for
     23different overloads. For compatibility with names in C, there is also a syntax
     24to disable name mangling. These unmangled names cannot be overloaded but act as
     25the interface between C and \CFA code.  The syntax for disabling/enabling
     26mangling is:
     27\begin{cfa}
     28// name mangling
     29int i; // _X1ii_1
     30@extern "C"@ {  // no name mangling
     31        int j; // j
     32        @extern "Cforall"@ {  // name mangling
     33                int k; // _X1ki_1
     34        }
     35        // no name mangling
     36}
     37// name mangling
     38\end{cfa}
     39Both forms of @extern@ affect all the declarations within their nested lexical
     40scope and transition back to the previous mangling state when the lexical scope
     41ends.
     42
     43\section{Reference Type}
     44\CFA adds a rebindable reference type to C, but more expressive than the \Cpp
     45reference.  Multi-level references are allowed and act like auto-dereferenced
     46pointers using the ampersand (@&@) instead of the pointer asterisk (@*@). \CFA
     47references may also be mutable or non-mutable. If mutable, a reference variable
     48may be assigned to using the address-of operator (@&@), which converts the
     49reference to a pointer.
     50\begin{cfa}
     51int i, j;
     52int @&@ ri = i, @&&@ rri = ri;
     53rri = 3;  // auto-dereference assign to i
     54@&@ri = @&@j; // rebindable
     55ri = 5;   // assign to j
     56\end{cfa}
    3757
    3858\section{Constructors and Destructors}
    3959
    4060Both constructors and destructors are operators, which means they are just
    41 functions with special names. The special names are used to define them and
    42 may be used to call the functions expicately. The \CFA special names are
    43 constructed by taking the tokens in the operators and putting \texttt{?} where
    44 the arguments would go. So multiplication is \texttt{?*?} while dereference
    45 is \texttt{*?}. This also make it easy to tell the difference between
    46 pre-fix operations (such as \texttt{++?}) and post-fix operations
    47 (\texttt{?++}).
    48 
    49 The special name for contructors is \texttt{?\{\}}, which comes from the
    50 initialization syntax in C. The special name for destructors is
    51 \texttt{\^{}?\{\}}. % I don't like the \^{} symbol but $^\wedge$ isn't better.
    52 
    53 Any time a type T goes out of scope the destructor matching
    54 \codeCFA{void ^?\{\}(T \&);} is called. In theory this is also true of
    55 primitive types such as \codeCFA{int}, but in practice those are no-ops and
    56 are usually omitted for optimization.
     61functions with special operator names rather than type names in \Cpp. The
     62special operator names may be used to call the functions explicitly (not
     63allowed in \Cpp for constructors).
     64
     65In general, operator names in \CFA are constructed by bracketing an operator
     66token with @?@, which indicates where the arguments. For example, infixed
     67multiplication is @?*?@ while prefix dereference is @*?@. This syntax make it
     68easy to tell the difference between prefix operations (such as @++?@) and
     69post-fix operations (@?++@).
     70
     71The special name for a constructor is @?{}@, which comes from the
     72initialization syntax in C. The special name for a destructor is @^{}@, where
     73the @^@ has no special meaning.
     74% I don't like the \^{} symbol but $^\wedge$ isn't better.
     75\begin{cfa}
     76struct T { ... };
     77void ?@{}@(@T &@ this, ...) { ... }  // constructor
     78void ?@^{}@(@T &@ this, ...) { ... } // destructor
     79{
     80        T s = @{@ ... @}@;  // same constructor/initialization braces
     81} // destructor call automatically generated
     82\end{cfa}
     83The first parameter is a reference parameter to the type for the
     84constructor/destructor. Destructors may have multiple parameters.  The compiler
     85implicitly matches an overloaded constructor @void ^?{}(T &, ...);@ to an
     86object declaration with associated initialization, and generates a construction
     87call after the object is allocated. When an object goes out of scope, the
     88matching overloaded destructor @void ^?{}(T &);@ is called.  Without explicit
     89definition, \CFA creates a default and copy constructor, destructor and
     90assignment (like \Cpp). It is possible to define constructors/destructors for
     91basic and existing types.
    5792
    5893\section{Polymorphism}
    59 \CFA uses polymorphism to create functions and types that are defined over
    60 different types. \CFA polymorphic declarations serve the same role as \CPP
    61 templates or Java generics.
    62 
    63 Polymorphic declaractions start with a forall clause that goes before the
    64 standard (monomorphic) declaration. These declarations have the same syntax
    65 except that you may use the names introduced by the forall clause in them.
    66 
    67 Forall clauses are written \codeCFA{forall( ... )} where \codeCFA{...} becomes
    68 the list of polymorphic variables (local type names) and assertions, which
    69 repersent required operations on those types.
    70 
    71 \begin{lstlisting}
    72 forall(dtype T | { void do_once(T &); })
    73 void do_twice(T & value) {
    74     do_once(value);
    75     do_once(value);
    76 }
    77 \end{lstlisting}
    78 
    79 A polymorphic function can be used in the same way normal functions are.
    80 The polymorphics variables are filled in with concrete types and the
    81 assertions are checked. An assertion checked by seeing if that name of that
    82 type (with all the variables replaced with the concrete types) is defined at
    83 the the call site.
    84 
    85 As an example, even if no function named \codeCFA{do_once} is not defined
    86 near the definition of \codeCFA{do_twice} the following code will work.
    87 \begin{lstlisting}
     94\CFA uses parametric polymorphism to create functions and types that are
     95defined over multiple types. \CFA polymorphic declarations serve the same role
     96as \Cpp templates or Java generics. The ``parametric'' means the polymorphism is
     97accomplished by passing argument operations to associate \emph{parameters} at
     98the call site, and these parameters are used in the function to differentiate
     99among the types the function operates on.
     100
     101Polymorphic declarations start with a universal @forall@ clause that goes
     102before the standard (monomorphic) declaration. These declarations have the same
     103syntax except they may use the universal type names introduced by the @forall@
     104clause.  For example, the following is a polymorphic identity function that
     105works on any type @T@:
     106\begin{cfa}
     107@forall( T )@ @T@ identity( @T@ val ) { return val; }
     108int forty_two = identity( 42 ); // T bound to int, forty_two == 42
     109\end{cfa}
     110
     111To allow a polymorphic function to be separately compiled, the type @T@ must be
     112constrained by the operations used on @T@ in the function body. The @forall@
     113clauses is augmented with a list of polymorphic variables (local type names)
     114and assertions (constraints), which represent the required operations on those
     115types used in a function, \eg:
     116\begin{cfa}
     117forall( T @| { void do_once(T); }@) // assertion
     118void do_twice(T value) {
     119        do_once(value);
     120        do_once(value);
     121}
     122void do_once(int i) { ... }  // provide assertion
     123int i;
     124do_twice(i); // implicitly pass assertion do_once to do_twice
     125\end{cfa}
     126Any object with a type fulfilling the assertion may be passed as an argument to
     127a @do_twice@ call.
     128
     129A polymorphic function can be used in the same way as a normal function.  The
     130polymorphic variables are filled in with concrete types and the assertions are
     131checked. An assertion is checked by verifying each assertion operation (with
     132all the variables replaced with the concrete types from the arguments) is
     133defined at a call site.
     134
     135Note, a function named @do_once@ is not required in the scope of @do_twice@ to
     136compile it, unlike \Cpp template expansion. Furthermore, call-site inferencing
     137allows local replacement of the most specific parametric functions needs for a
     138call.
     139\begin{cfa}
     140void do_once(double y) { ... } // global
    88141int quadruple(int x) {
    89     void do_once(int & y) {
    90         y = y * 2;
    91     }
    92     do_twice(x);
    93     return x;
    94 }
    95 \end{lstlisting}
    96 This is not the recommended way to implement a quadruple function but it
    97 does work. The complier will deduce that \codeCFA{do_twice}'s T is an
    98 integer from the argument. It will then look for a definition matching the
    99 assertion which is the \codeCFA{do_once} defined within the function. That
    100 function will be passed in as a function pointer to \codeCFA{do_twice} and
    101 called within it.
    102 
    103 To avoid typing out long lists of assertions again and again there are also
    104 traits which collect assertions into convenent packages that can then be used
    105 in assertion lists instead of all of their components.
    106 \begin{lstlisting}
    107 trait done_once(dtype T) {
    108     void do_once(T &);
    109 }
    110 \end{lstlisting}
    111 
    112 After this the forall list in the previous example could instead be written
    113 with the trait instead of the assertion itself.
    114 \begin{lstlisting}
    115 forall(dtype T | done_once(T))
    116 \end{lstlisting}
    117 
    118 Traits can have arbitrary number of assertions in them and are usually used to
    119 create short hands for, and give descriptive names to, commond groupings of
    120 assertions.
    121 
    122 Polymorphic structures and unions may also be defined by putting a forall
    123 clause before the declaration. The type variables work the same way except
    124 are now used in field declaractions instead of parameters and local variables.
    125 
    126 \begin{lstlisting}
     142        void do_once(int y) { y = y * 2; } // local
     143        do_twice(x); // using local "do_once"
     144        return x;
     145}
     146\end{cfa}
     147Specifically, the complier deduces that @do_twice@'s T is an integer from the
     148argument @x@. It then looks for the most specific definition matching the
     149assertion, which is the nested integral @do_once@ defined within the
     150function. The matched assertion function is then passed as a function pointer
     151to @do_twice@ and called within it.
     152
     153To avoid typing long lists of assertions, constraints can be collect into
     154convenient packages called a @trait@, which can then be used in an assertion
     155instead of the individual constraints.
     156\begin{cfa}
     157trait done_once(T) {
     158        void do_once(T);
     159}
     160\end{cfa}
     161and the @forall@ list in the previous example is replaced with the trait.
     162\begin{cfa}
     163forall(dtype T | @done_once(T)@)
     164\end{cfa}
     165In general, a trait can contain an arbitrary number of assertions, both
     166functions and variables, and are usually used to create a shorthand for, and
     167give descriptive names to, common groupings of assertions describing a certain
     168functionality, like @sumable@, @listable@, \etc.
     169
     170Polymorphic structures and unions are defined by qualifying the aggregate type
     171with @forall@. The type variables work the same except they are used in field
     172declarations instead of parameters, returns, and local variable declarations.
     173\begin{cfa}
    127174forall(dtype T)
    128175struct node {
    129     node(T) * next;
    130     T * data;
    131 }
    132 \end{lstlisting}
    133 
    134 The \codeCFA{node(T)} is a use of a polymorphic structure. Polymorphic types
    135 must be provided their polymorphic parameters.
    136 
    137 There are many other features of polymorphism that have not given here but
    138 these are the ones used by the exception system.
     176        node(T) * next;  // generic linked node
     177        T * data;
     178}
     179\end{cfa}
     180The generic type @node(T)@ is an example of a polymorphic-type usage.  Like \Cpp
     181templates usage, a polymorphic-type usage must specify a type parameter.
     182
     183There are many other polymorphism features in \CFA but these are the ones used
     184by the exception system.
    139185
    140186\section{Concurrency}
    141 
    142 \CFA has a number of concurrency features, \codeCFA{thread}s,
    143 \codeCFA{monitor}s and \codeCFA{mutex} parameters, \codeCFA{coroutine}s and
    144 \codeCFA{generator}s. The two features that interact with the exception system
    145 are \codeCFA{thread}s and \codeCFA{coroutine}s; they and their supporting
    146 constructs will be described here.
    147 
    148 \subsection{Coroutines}
    149 Coroutines are routines that do not have to finish execution to hand control
    150 back to their caller, instead they may suspend their execution at any time
    151 and resume it later.
    152 Coroutines are not true concurrency but share some similarities and many of
    153 the same underpinnings and so are included as part of the \CFA threading
    154 library.
    155 
    156 In \CFA coroutines are created using the \codeCFA{coroutine} keyword which
    157 works just like \codeCFA{struct} except that the created structure will be
    158 modified by the compiler to satify the \codeCFA{is_coroutine} trait.
    159 
    160 These structures act as the interface between callers and the coroutine,
    161 the fields are used to pass information in and out. Here is a simple example
    162 where the single field is used to pass the next number in a sequence out.
    163 \begin{lstlisting}
     187\CFA has a number of concurrency features: @thread@, @monitor@, @mutex@
     188parameters, @coroutine@ and @generator@. The two features that interact with
     189the exception system are @thread@ and @coroutine@; they and their supporting
     190constructs are described here.
     191
     192\subsection{Coroutine}
     193A coroutine is a type with associated functions, where the functions are not
     194required to finish execution when control is handed back to the caller. Instead
     195they may suspend execution at any time and be resumed later at the point of
     196last suspension. (Generators are stackless and coroutines are stackful.) These
     197types are not concurrent but share some similarities along with common
     198underpinnings, so they are combined with the \CFA threading library. Further
     199discussion in this section only refers to the coroutine because generators are
     200similar.
     201
     202In \CFA, a coroutine is created using the @coroutine@ keyword, which is an
     203aggregate type like @struct,@ except the structure is implicitly modified by
     204the compiler to satisfy the @is_coroutine@ trait; hence, a coroutine is
     205restricted by the type system to types that provide this special trait.  The
     206coroutine structure acts as the interface between callers and the coroutine,
     207and its fields are used to pass information in and out of coroutine interface
     208functions.
     209
     210Here is a simple example where a single field is used to pass (communicate) the
     211next number in a sequence.
     212\begin{cfa}
    164213coroutine CountUp {
    165     unsigned int next;
    166 }
    167 \end{lstlisting}
    168 
    169 The routine part of the coroutine is a main function for the coroutine. It
    170 takes a reference to a coroutine object and returns nothing. In this function,
    171 and any functions called by this function, the suspend statement may be used
    172 to return execution to the coroutine's caller. When control returns to the
    173 function it continue from that same suspend statement instead of at the top
    174 of the function.
    175 \begin{lstlisting}
    176 void main(CountUp & this) {
    177     unsigned int next = 0;
    178     while (true) {
    179         this.next = next;
    180         suspend;
    181         next = next + 1;
    182     }
    183 }
    184 \end{lstlisting}
    185 
    186 Control is passed to the coroutine with the resume function. This includes the
    187 first time when the coroutine is starting up. The resume function takes a
    188 reference to the coroutine structure and returns the same reference. The
    189 return value is for easy access to communication variables. For example the
    190 next value from a count-up can be generated and collected in a single
    191 expression: \codeCFA{resume(count).next}.
     214        unsigned int next; // communication variable
     215}
     216CountUp countup;
     217\end{cfa}
     218Each coroutine has @main@ function, which takes a reference to a coroutine
     219object and returns @void@.
     220\begin{cfa}[numbers=left]
     221void main(@CountUp & this@) { // argument matches trait is_coroutine
     222        unsigned int up = 0;  // retained between calls
     223        while (true) {
     224                next = up; // make "up" available outside function
     225                @suspend;@$\label{suspend}$
     226                up += 1;
     227        }
     228}
     229\end{cfa}
     230In this function, or functions called by this function (helper functions), the
     231@suspend@ statement is used to return execution to the coroutine's caller
     232without terminating the coroutine.
     233
     234A coroutine is resumed by calling the @resume@ function, \eg @resume(countup)@.
     235The first resume calls the @main@ function at the top. Thereafter, resume calls
     236continue a coroutine in the last suspended function after the @suspend@
     237statement, in this case @main@ line~\ref{suspend}.  The @resume@ function takes
     238a reference to the coroutine structure and returns the same reference. The
     239return value allows easy access to communication variables defined in the
     240coroutine object. For example, the @next@ value for coroutine object @countup@
     241is both generated and collected in the single expression:
     242@resume(countup).next@.
    192243
    193244\subsection{Monitors and Mutex}
    194 
    195 True concurrency does not garrenty ordering. To get some of that ordering back
    196 \CFA uses monitors and mutex (mutual exclution) parameters. A monitor is
    197 another special declaration that contains a lock and is compatable with mutex
    198 parameters.
    199 
    200 Function parameters can have the \codeCFA{mutex} qualifiers on reference
    201 arguments, for example \codeCFA{void example(a_monitor & mutex arg);}. When the
    202 function is called it will acquire the lock on all of the mutex parameters.
    203 
    204 This means that all functions that mutex on a type are part of a critical
    205 section and only one will ever run at a time.
     245Concurrency does not guarantee ordering; without ordering results are
     246non-deterministic. To claw back ordering, \CFA uses monitors and @mutex@
     247(mutual exclusion) parameters. A monitor is another kind of aggregate, where
     248the compiler implicitly inserts a lock and instances are compatible with
     249@mutex@ parameters.
     250
     251A function that requires deterministic (ordered) execution, acquires mutual
     252exclusion on a monitor object by qualifying an object reference parameter with
     253@mutex@.
     254\begin{cfa}
     255void example(MonitorA & @mutex@ argA, MonitorB & @mutex@ argB);
     256\end{cfa}
     257When the function is called, it implicitly acquires the monitor lock for all of
     258the mutex parameters without deadlock.  This semantics means all functions with
     259the same mutex type(s) are part of a critical section for objects of that type
     260and only one runs at a time.
    206261
    207262\subsection{Threads}
    208 While coroutines allow new things to be done with a single execution path
    209 threads actually introduce new paths of execution that continue independently.
    210 Now for threads to work together their must be some communication between them
    211 and that means the timing of certain operations does have to be known. There
    212 or various means of syncronization and mutual exclution provided by \CFA but
    213 for exceptions only the basic two -- fork and join -- are needed.
    214 
    215 Threads are created like coroutines except the keyword is changed:
    216 \begin{lstlisting}
     263Functions, generators, and coroutines are sequential so there is only a single
     264(but potentially sophisticated) execution path in a program. Threads introduce
     265multiple execution paths that continue independently.
     266
     267For threads to work safely with objects requires mutual exclusion using
     268monitors and mutex parameters. For threads to work safely with other threads,
     269also requires mutual exclusion in the form of a communication rendezvous, which
     270also supports internal synchronization as for mutex objects. For exceptions
     271only the basic two basic operations are important: thread fork and join.
     272
     273Threads are created like coroutines with an associated @main@ function:
     274\begin{cfa}
    217275thread StringWorker {
    218     const char * input;
    219     int result;
     276        const char * input;
     277        int result;
    220278};
    221 
    222279void main(StringWorker & this) {
    223     const char * localCopy = this.input;
    224     // ... do some work, perhaps hashing the string ...
    225     this.result = result;
    226 }
    227 \end{lstlisting}
    228 The main function will start executing after the fork operation and continue
    229 executing until it is finished. If another thread joins with this one it will
    230 wait until main has completed execution. In other words everything the thread
    231 does is between fork and join.
    232 
    233 From the outside this is the creation and destruction of the thread object.
    234 Fork happens after the constructor is run and join happens before the
    235 destructor runs. Join also happens during the \codeCFA{join} function which
    236 can be used to join a thread earlier. If it is used the destructor does not
    237 join as that has already been completed.
     280        const char * localCopy = this.input;
     281        // ... do some work, perhaps hashing the string ...
     282        this.result = result;
     283}
     284{
     285        StringWorker stringworker; // fork thread running in "main"
     286} // implicitly join with thread $\(\Rightarrow\)$ wait for completion
     287\end{cfa}
     288The thread main is where a new thread starts execution after a fork operation
     289and then the thread continues executing until it is finished. If another thread
     290joins with an executing thread, it waits until the executing main completes
     291execution. In other words, everything a thread does is between a fork and join.
     292
     293From the outside, this behaviour is accomplished through creation and
     294destruction of a thread object.  Implicitly, fork happens after a thread
     295object's constructor is run and join happens before the destructor runs. Join
     296can also be specified explicitly using the @join@ function to wait for a
     297thread's completion independently from its deallocation (\ie destructor
     298call). If @join@ is called explicitly, the destructor does not implicitly join.
  • doc/theses/andrew_beach_MMath/features.tex

    r342af53 r8e4aa05  
    1 \chapter{Features}
    2 
    3 This chapter covers the design and user interface of the \CFA exception
    4 handling mechanism.
    5 
    6 \section{Virtual Casts}
    7 
    8 Virtual casts and virtual types are not truly part of the exception system but
    9 they did not exist in \CFA and are useful in exceptions. So a minimal version
    10 of they virtual system was designed and implemented.
    11 
    12 Virtual types are organizied in simple hierarchies. Each virtual type may have
    13 a parent and can have any number of children. A type's descendants are its
    14 children and its children's descendants. A type may not be its own descendant.
    15 
    16 Each virtual type has an associated virtual table type. A virtual table is a
    17 structure that has fields for all the virtual members of a type. A virtual
    18 type has all the virtual members of its parent and can add more. It may also
    19 update the values of the virtual members.
    20 
    21 Except for virtual casts, this is only used internally in the exception
    22 system. There is no general purpose interface for the other features. A
    23 a virtual cast has the following syntax:
    24 
    25 \begin{lstlisting}
     1\chapter{Exception Features}
     2
     3This chapter covers the design and user interface of the \CFA
     4exception-handling mechanism.
     5
     6\section{Virtuals}
     7Virtual types and casts are not part of the exception system nor are they
     8required for an exception system. But an object-oriented style hierarchy is a
     9great way of organizing exceptions so a minimal virtual system has been added
     10to \CFA.
     11
     12The pattern of a simple hierarchy was borrowed from object-oriented
     13programming was chosen for several reasons.
     14The first is that it allows new exceptions to be added in user code
     15and in libraries independently of each other. Another is it allows for
     16different levels of exception grouping (all exceptions, all IO exceptions or
     17a particular IO exception). Also it also provides a simple way of passing
     18data back and forth across the throw.
     19
     20Virtual types and casts are not required for a basic exception-system but are
     21useful for advanced exception features. However, \CFA is not object-oriented so
     22there is no obvious concept of virtuals. Hence, to create advanced exception
     23features for this work, I needed to design and implement a virtual-like
     24system for \CFA.
     25
     26% NOTE: Maybe we should but less of the rational here.
     27Object-oriented languages often organized exceptions into a simple hierarchy,
     28\eg Java.
     29\begin{center}
     30\setlength{\unitlength}{4000sp}%
     31\begin{picture}(1605,612)(2011,-1951)
     32\put(2100,-1411){\vector(1, 0){225}}
     33\put(3450,-1411){\vector(1, 0){225}}
     34\put(3550,-1411){\line(0,-1){225}}
     35\put(3550,-1636){\vector(1, 0){150}}
     36\put(3550,-1636){\line(0,-1){225}}
     37\put(3550,-1861){\vector(1, 0){150}}
     38\put(2025,-1490){\makebox(0,0)[rb]{\LstBasicStyle{exception}}}
     39\put(2400,-1460){\makebox(0,0)[lb]{\LstBasicStyle{arithmetic}}}
     40\put(3750,-1460){\makebox(0,0)[lb]{\LstBasicStyle{underflow}}}
     41\put(3750,-1690){\makebox(0,0)[lb]{\LstBasicStyle{overflow}}}
     42\put(3750,-1920){\makebox(0,0)[lb]{\LstBasicStyle{zerodivide}}}
     43\end{picture}%
     44\end{center}
     45The hierarchy provides the ability to handle an exception at different degrees
     46of specificity (left to right). Hence, it is possible to catch a more general
     47exception-type in higher-level code where the implementation details are
     48unknown, which reduces tight coupling to the lower-level implementation.
     49Otherwise, low-level code changes require higher-level code changes, \eg,
     50changing from raising @underflow@ to @overflow@ at the low level means changing
     51the matching catch at the high level versus catching the general @arithmetic@
     52exception. In detail, each virtual type may have a parent and can have any
     53number of children. A type's descendants are its children and its children's
     54descendants. A type may not be its own descendant.
     55
     56The exception hierarchy allows a handler (@catch@ clause) to match multiple
     57exceptions, \eg a base-type handler catches both base and derived
     58exception-types.
     59\begin{cfa}
     60try {
     61        ...
     62} catch(arithmetic &) {
     63        ... // handle arithmetic, underflow, overflow, zerodivide
     64}
     65\end{cfa}
     66Most exception mechanisms perform a linear search of the handlers and select
     67the first matching handler, so the order of handers is now important because
     68matching is many to one.
     69
     70Each virtual type needs an associated virtual table. A virtual table is a
     71structure with fields for all the virtual members of a type. A virtual type has
     72all the virtual members of its parent and can add more. It may also update the
     73values of the virtual members and often does.
     74
     75While much of the virtual infrastructure is created, it is currently only used
     76internally for exception handling. The only user-level feature is the virtual
     77cast, which is the same as the \Cpp \lstinline[language=C++]|dynamic_cast|.
     78\label{p:VirtualCast}
     79\begin{cfa}
    2680(virtual TYPE)EXPRESSION
    27 \end{lstlisting}
    28 
    29 This has the same precidence as a traditional C-cast and can be used in the
    30 same places. This will convert the result of EXPRESSION to the type TYPE. Both
    31 the type of EXPRESSION and TYPE must be pointers to virtual types.
    32 
    33 The cast is checked and will either return the original value or null, based
    34 on the result of the check. The check is does the object pointed at have a
    35 type that is a descendant of the target type. If it is the result is the
    36 pointer, otherwise the result is null.
    37 
    38 \section{Exceptions}
     81\end{cfa}
     82Note, the syntax and semantics matches a C-cast, rather than the function-like
     83\Cpp syntax for special casts. Both the type of @EXPRESSION@ and @TYPE@ must be
     84a pointer to a virtual type.
     85The cast dynamically checks if the @EXPRESSION@ type is the same or a subtype
     86of @TYPE@, and if true, returns a pointer to the
     87@EXPRESSION@ object, otherwise it returns @0p@ (null pointer).
     88
     89\section{Exception}
    3990% Leaving until later, hopefully it can talk about actual syntax instead
    4091% of my many strange macros. Syntax aside I will also have to talk about the
    4192% features all exceptions support.
    4293
    43 \section{Termination}
    44 
    45 Termination exception throws are likely the most framilar kind, as they are
    46 used in several popular programming languages. A termination will throw an
    47 exception, search the stack for a handler, unwind the stack to where the
    48 handler is defined, execute the handler and then continue execution after
    49 the handler. They are used when execution cannot continue here.
    50 
    51 Termination has two pieces of syntax it uses. The first is the throw:
    52 \begin{lstlisting}
     94Exceptions are defined by the trait system; there are a series of traits, and
     95if a type satisfies them, then it can be used as an exception. The following
     96is the base trait all exceptions need to match.
     97\begin{cfa}
     98trait is_exception(exceptT &, virtualT &) {
     99        virtualT const & get_exception_vtable(exceptT *);
     100};
     101\end{cfa}
     102The trait is defined over two types, the exception type and the virtual table
     103type. This should be one-to-one, each exception type has only one virtual
     104table type and vice versa. The only assertion in the trait is
     105@get_exception_vtable@, which takes a pointer of the exception type and
     106returns a reference to the virtual table type instance.
     107
     108The function @get_exception_vtable@ is actually a constant function.
     109Recardless of the value passed in (including the null pointer) it should
     110return a reference to the virtual table instance for that type.
     111The reason it is a function instead of a constant is that it make type
     112annotations easier to write as you can use the exception type instead of the
     113virtual table type; which usually has a mangled name.
     114% Also \CFA's trait system handles functions better than constants and doing
     115% it this way reduce the amount of boiler plate we need.
     116
     117% I did have a note about how it is the programmer's responsibility to make
     118% sure the function is implemented correctly. But this is true of every
     119% similar system I know of (except Agda's I guess) so I took it out.
     120
     121There are two more traits for exceptions @is_termination_exception@ and
     122@is_resumption_exception@. They are defined as follows:
     123
     124\begin{cfa}
     125trait is_termination_exception(
     126                exceptT &, virtualT & | is_exception(exceptT, virtualT)) {
     127        void defaultTerminationHandler(exceptT &);
     128};
     129
     130trait is_resumption_exception(
     131                exceptT &, virtualT & | is_exception(exceptT, virtualT)) {
     132        void defaultResumptionHandler(exceptT &);
     133};
     134\end{cfa}
     135
     136In other words they make sure that a given type and virtual type is an
     137exception and defines one of the two default handlers. These default handlers
     138are used in the main exception handling operations \see{Exception Handling}
     139and their use will be detailed there.
     140
     141However all three of these traits can be trickly to use directly.
     142There is a bit of repetition required but
     143the largest issue is that the virtual table type is mangled and not in a user
     144facing way. So there are three macros that can be used to wrap these traits
     145when you need to refer to the names:
     146@IS_EXCEPTION@, @IS_TERMINATION_EXCEPTION@ and @IS_RESUMPTION_EXCEPTION@.
     147
     148All take one or two arguments. The first argument is the name of the
     149exception type. Its unmangled and mangled form are passed to the trait.
     150The second (optional) argument is a parenthesized list of polymorphic
     151arguments. This argument should only with polymorphic exceptions and the
     152list will be passed to both types.
     153In the current set-up the base name and the polymorphic arguments have to
     154match so these macros can be used without losing flexability.
     155
     156For example consider a function that is polymorphic over types that have a
     157defined arithmetic exception:
     158\begin{cfa}
     159forall(Num | IS_EXCEPTION(Arithmetic, (Num)))
     160void some_math_function(Num & left, Num & right);
     161\end{cfa}
     162
     163\section{Exception Handling}
     164\CFA provides two kinds of exception handling, termination and resumption.
     165These twin operations are the core of the exception handling mechanism and
     166are the reason for the features of exceptions.
     167This section will cover the general patterns shared by the two operations and
     168then go on to cover the details each individual operation.
     169
     170Both operations follow the same set of steps to do their operation. They both
     171start with the user preforming a throw on an exception.
     172Then there is the search for a handler, if one is found than the exception
     173is caught and the handler is run. After that control returns to normal
     174execution.
     175
     176If the search fails a default handler is run and then control
     177returns to normal execution immediately. That is where the default handlers
     178@defaultTermiationHandler@ and @defaultResumptionHandler@ are used.
     179
     180\subsection{Termination}
     181\label{s:Termination}
     182
     183Termination handling is more familiar kind and used in most programming
     184languages with exception handling.
     185It is dynamic, non-local goto. If a throw is successful then the stack will
     186be unwound and control will (usually) continue in a different function on
     187the call stack. They are commonly used when an error has occured and recovery
     188is impossible in the current function.
     189
     190% (usually) Control can continue in the current function but then a different
     191% control flow construct should be used.
     192
     193A termination throw is started with the @throw@ statement:
     194\begin{cfa}
    53195throw EXPRESSION;
    54 \end{lstlisting}
    55 
    56 The expression must evaluate to a reference to a termination exception. A
    57 termination exception is any exception with a
    58 \codeCFA{void defaultTerminationHandler(T &);} (the default handler) defined
    59 on it. The handler is taken from the call sight with \CFA's trait system and
    60 passed into the exception system along with the exception itself.
    61 
    62 The exception passed into the system is then copied into managed memory.
    63 This is to ensure it remains in scope during unwinding. It is the user's
    64 responsibility to make sure the original exception is freed when it goes out
    65 of scope. Being allocated on the stack is sufficient for this.
    66 
    67 Then the exception system will search the stack starting from the throw and
    68 proceding towards the base of the stack, from callee to caller. As it goes
    69 it will check any termination handlers it finds:
    70 
    71 \begin{lstlisting}
    72 try {
    73     TRY_BLOCK
    74 } catch (EXCEPTION_TYPE * NAME) {
    75     HANDLER
    76 }
    77 \end{lstlisting}
    78 
    79 This shows a try statement with a single termination handler. The statements
    80 in TRY\_BLOCK will be executed when control reaches this statement. While
    81 those statements are being executed if a termination exception is thrown and
    82 it is not handled by a try statement further up the stack the EHM will check
    83 all of the terminations handlers attached to the try block, top to bottom.
    84 
    85 At each handler the EHM will check to see if the thrown exception is a
    86 descendant of EXCEPTION\_TYPE. If it is the pointer to the exception is
    87 bound to NAME and the statements in HANDLER are executed. If control reaches
    88 the end of the handler then it exits the block, the exception is freed and
    89 control continues after the try statement.
    90 
    91 The default handler is only used if no handler for the exception is found
    92 after the entire stack is searched. When that happens the default handler
    93 is called with a reference to the exception as its only argument. If the
    94 handler returns control continues from after the throw statement.
    95 
    96 \paragraph{Conditional Catches}
    97 
    98 Catch clauses may also be written as:
    99 \begin{lstlisting}
     196\end{cfa}
     197The expression must return a reference to a termination exception, where the
     198termination exception is any type that satifies @is_termination_exception@
     199at the call site.
     200Through \CFA's trait system the functions in the traits are passed into the
     201throw code. A new @defaultTerminationHandler@ can be defined in any scope to
     202change the throw's behavior (see below).
     203
     204The throw will copy the provided exception into managed memory. It is the
     205user's responcibility to ensure the original exception is cleaned up if the
     206stack is unwound (allocating it on the stack should be sufficient).
     207
     208Then the exception system searches the stack using the copied exception.
     209It starts starts from the throw and proceeds to the base of the stack,
     210from callee to caller.
     211At each stack frame, a check is made for resumption handlers defined by the
     212@catch@ clauses of a @try@ statement.
     213\begin{cfa}
     214try {
     215        GUARDED_BLOCK
     216} catch (EXCEPTION_TYPE$\(_1\)$ * NAME$\(_1\)$) {
     217        HANDLER_BLOCK$\(_1\)$
     218} catch (EXCEPTION_TYPE$\(_2\)$ * NAME$\(_2\)$) {
     219        HANDLER_BLOCK$\(_2\)$
     220}
     221\end{cfa}
     222When viewed on its own a try statement will simply exceute the statements in
     223@GUARDED_BLOCK@ and when those are finished the try statement finishes.
     224
     225However, while the guarded statements are being executed, including any
     226functions they invoke, all the handlers following the try block are now
     227or any functions invoked from those
     228statements, throws an exception, and the exception
     229is not handled by a try statement further up the stack, the termination
     230handlers are searched for a matching exception type from top to bottom.
     231
     232Exception matching checks the representation of the thrown exception-type is
     233the same or a descendant type of the exception types in the handler clauses. If
     234it is the same of a descendent of @EXCEPTION_TYPE@$_i$ then @NAME@$_i$ is
     235bound to a pointer to the exception and the statements in @HANDLER_BLOCK@$_i$
     236are executed. If control reaches the end of the handler, the exception is
     237freed and control continues after the try statement.
     238
     239If no handler is found during the search then the default handler is run.
     240Through \CFA's trait system the best match at the throw sight will be used.
     241This function is run and is passed the copied exception. After the default
     242handler is run control continues after the throw statement.
     243
     244There is a global @defaultTerminationHandler@ that cancels the current stack
     245with the copied exception. However it is generic over all exception types so
     246new default handlers can be defined for different exception types and so
     247different exception types can have different default handlers.
     248
     249\subsection{Resumption}
     250\label{s:Resumption}
     251
     252Resumption exception handling is a less common form than termination but is
     253just as old~\cite{Goodenough75} and is in some sense simpler.
     254It is a dynamic, non-local function call. If the throw is successful a
     255closure will be taken from up the stack and executed, after which the throwing
     256function will continue executing.
     257These are most often used when an error occured and if the error is repaired
     258then the function can continue.
     259
     260A resumption raise is started with the @throwResume@ statement:
     261\begin{cfa}
     262throwResume EXPRESSION;
     263\end{cfa}
     264The semantics of the @throwResume@ statement are like the @throw@, but the
     265expression has return a reference a type that satifies the trait
     266@is_resumption_exception@. The assertions from this trait are available to
     267the exception system while handling the exception.
     268
     269At runtime, no copies are made. As the stack is not unwound the exception and
     270any values on the stack will remain in scope while the resumption is handled.
     271
     272Then the exception system searches the stack using the provided exception.
     273It starts starts from the throw and proceeds to the base of the stack,
     274from callee to caller.
     275At each stack frame, a check is made for resumption handlers defined by the
     276@catchResume@ clauses of a @try@ statement.
     277\begin{cfa}
     278try {
     279        GUARDED_BLOCK
     280} catchResume (EXCEPTION_TYPE$\(_1\)$ * NAME$\(_1\)$) {
     281        HANDLER_BLOCK$\(_1\)$
     282} catchResume (EXCEPTION_TYPE$\(_2\)$ * NAME$\(_2\)$) {
     283        HANDLER_BLOCK$\(_2\)$
     284}
     285\end{cfa}
     286If the handlers are not involved in a search this will simply execute the
     287@GUARDED_BLOCK@ and then continue to the next statement.
     288Its purpose is to add handlers onto the stack.
     289(Note, termination and resumption handlers may be intermixed in a @try@
     290statement but the kind of throw must be the same as the handler for it to be
     291considered as a possible match.)
     292
     293If a search for a resumption handler reaches a try block it will check each
     294@catchResume@ clause, top-to-bottom.
     295At each handler if the thrown exception is or is a child type of
     296@EXCEPTION_TYPE@$_i$ then the a pointer to the exception is bound to
     297@NAME@$_i$ and then @HANDLER_BLOCK@$_i$ is executed. After the block is
     298finished control will return to the @throwResume@ statement.
     299
     300Like termination, if no resumption handler is found, the default handler
     301visible at the throw statement is called. It will use the best match at the
     302call sight according to \CFA's overloading rules. The default handler is
     303passed the exception given to the throw. When the default handler finishes
     304execution continues after the throw statement.
     305
     306There is a global @defaultResumptionHandler@ is polymorphic over all
     307termination exceptions and preforms a termination throw on the exception.
     308The @defaultTerminationHandler@ for that throw is matched at the original
     309throw statement (the resumption @throwResume@) and it can be customized by
     310introducing a new or better match as well.
     311
     312% \subsubsection?
     313
     314A key difference between resumption and termination is that resumption does
     315not unwind the stack. A side effect that is that when a handler is matched
     316and run it's try block (the guarded statements) and every try statement
     317searched before it are still on the stack. This can lead to the recursive
     318resumption problem.
     319
     320The recursive resumption problem is any situation where a resumption handler
     321ends up being called while it is running.
     322Consider a trivial case:
     323\begin{cfa}
     324try {
     325        throwResume (E &){};
     326} catchResume(E *) {
     327        throwResume (E &){};
     328}
     329\end{cfa}
     330When this code is executed the guarded @throwResume@ will throw, start a
     331search and match the handler in the @catchResume@ clause. This will be
     332call and placed on the stack on top of the try-block. The second throw then
     333throws and will seach the same try block and put call another instance of the
     334same handler leading to an infinite loop.
     335
     336This situation is trivial and easy to avoid, but much more complex cycles
     337can form with multiple handlers and different exception types.
     338
     339To prevent all of these cases we mask sections of the stack, or equvilantly
     340the try statements on the stack, so that the resumption seach skips over
     341them and continues with the next unmasked section of the stack.
     342
     343A section of the stack is marked when it is searched to see if it contains
     344a handler for an exception and unmarked when that exception has been handled
     345or the search was completed without finding a handler.
     346
     347% This might need a diagram. But it is an important part of the justification
     348% of the design of the traversal order.
     349\begin{verbatim}
     350       throwResume2 ----------.
     351            |                 |
     352 generated from handler       |
     353            |                 |
     354         handler              |
     355            |                 |
     356        throwResume1 -----.   :
     357            |             |   :
     358           try            |   : search skip
     359            |             |   :
     360        catchResume  <----'   :
     361            |                 |
     362\end{verbatim}
     363
     364The rules can be remembered as thinking about what would be searched in
     365termination. So when a throw happens in a handler; a termination handler
     366skips everything from the original throw to the original catch because that
     367part of the stack has been unwound, a resumption handler skips the same
     368section of stack because it has been masked.
     369A throw in a default handler will preform the same search as the original
     370throw because; for termination nothing has been unwound, for resumption
     371the mask will be the same.
     372
     373The symmetry with termination is why this pattern was picked. Other patterns,
     374such as marking just the handlers that caught, also work but lack the
     375symmetry whih means there is more to remember.
     376
     377\section{Conditional Catch}
     378Both termination and resumption handler clauses can be given an additional
     379condition to further control which exceptions they handle:
     380\begin{cfa}
    100381catch (EXCEPTION_TYPE * NAME ; CONDITION)
    101 \end{lstlisting}
    102 This has the same behaviour as a regular catch clause except that if the
    103 exception matches the given type the condition is also run. If the result is
    104 true only then is this considered a matching handler. If the result is false
    105 then the handler does not match and the search continues with the next clause
    106 in the try block.
    107 
    108 The condition considers all names in scope at the beginning of the try block
    109 to be in scope along with the name introduce in the catch clause itself.
    110 
    111 \paragraph{Re-Throwing}
    112 
    113 You can also rethrow the most recent termination exception with
    114 \codeCFA{throw;}. % This is terrible and you should never do it.
    115 This can be done in a handler or any function that could be called from a
    116 handler.
    117 
    118 This will start another termination throw reusing the exception, meaning it
    119 does not copy the exception or allocated any more memory for it. However the
    120 default handler is still at the original through and could refer to data that
    121 was on the unwound section of the stack. So instead a new default handler that
    122 does a program level abort is used.
    123 
    124 \section{Resumption}
    125 
    126 Resumption exceptions are less popular then termination but in many
    127 regards are simpler and easier to understand. A resumption throws an exception,
    128 searches for a handler on the stack, executes that handler on top of the stack
    129 and then continues execution from the throw. These are used when a problem
    130 needs to be fixed before execution continues.
    131 
    132 A resumption is thrown with a throw resume statement:
    133 \begin{lstlisting}
    134 throwResume EXPRESSION;
    135 \end{lstlisting}
    136 The result of EXPRESSION must be a resumption exception type. A resumption
    137 exception type is any type that satifies the assertion
    138 \codeCFA{void defaultResumptionHandler(T &);} (the default handler). When the
    139 statement is executed the expression is evaluated and the result is thrown.
    140 
    141 Handlers are declared using clauses in try statements:
    142 \begin{lstlisting}
    143 try {
    144     TRY_BLOCK
    145 } catchResume (EXCEPTION_TYPE * NAME) {
    146     HANDLER
    147 }
    148 \end{lstlisting}
    149 This is a simple example with the try block and a single resumption handler.
    150 Multiple resumption handlers can be put in a try statement and they can be
    151 mixed with termination handlers.
    152 
    153 When a resumption begins it will start searching the stack starting from
    154 the throw statement and working its way to the callers. In each try statement
    155 handlers will be tried top to bottom. Each handler is checked by seeing if
    156 the thrown exception is a descendant of EXCEPTION\_TYPE. If not the search
    157 continues. Otherwise NAME is bound to a pointer to the exception and the
    158 HANDLER statements are executed. After they are finished executing control
    159 continues from the throw statement.
    160 
    161 If no approprate handler is found then the default handler is called. The
    162 throw statement acts as a regular function call passing the exception to
    163 the default handler and after the handler finishes executing control continues
    164 from the throw statement.
    165 
    166 The exception system also tracks the position of a search on the stack. If
    167 another resumption exception is thrown while a resumption handler is running
    168 it will first check handlers pushed to the stack by the handler and any
    169 functions it called, then it will continue from the try statement that the
    170 handler is a part of; except for the default handler where it continues from
    171 the throw the default handler was passed to.
    172 
    173 This makes the search pattern for resumption reflect the one for termination,
    174 which is what most users expect.
    175 
    176 % This might need a diagram. But it is an important part of the justifaction
    177 % of the design of the traversal order.
    178 It also avoids the recursive resumption problem. If the entire stack is
    179 searched loops of resumption can form. Consider a handler that handles an
    180 exception of type A by resuming an exception of type B and on the same stack,
    181 later in the search path, is a second handler that handles B by resuming A.
    182 
    183 Assuming no other handlers on the stack handle A or B then in either traversal
    184 system an A resumed from the top of the stack will be handled by the first
    185 handler. A B resumed from the top or from the first handler it will be handled
    186 by the second hander. The only difference is when A is thrown from the second
    187 handler. The entire stack search will call the first handler again, creating a
    188 loop. Starting from the position in the stack though will break this loop.
    189 
    190 \paragraph{Conditional Catches}
    191 
    192 Resumption supports conditional catch clauses like termination does. They
    193 use the same syntax except the keyword is changed:
    194 \begin{lstlisting}
    195 catchResume (EXCEPTION_TYPE * NAME ; CONDITION) 
    196 \end{lstlisting}
    197 
    198 It also has the same behaviour, after the exception type has been matched
    199 with the EXCEPTION\_TYPE the CONDITION is evaluated with NAME in scope. If
    200 the result is true then the hander is run, otherwise the search continues
    201 just as if there had been a type mismatch.
    202 
    203 \paragraph{Re-Throwing}
    204 
    205 You may also re-throw resumptions with a \codeCFA{throwResume;} statement.
    206 This can only be done from inside of a \codeCFA{catchResume} block.
    207 
    208 Outside of any side effects of any code already run in the handler this will
    209 have the same effect as if the exception had not been caught in the first
    210 place.
     382\end{cfa}
     383First, the same semantics is used to match the exception type. Second, if the
     384exception matches, @CONDITION@ is executed. The condition expression may
     385reference all names in scope at the beginning of the try block and @NAME@
     386introduced in the handler clause. If the condition is true, then the handler
     387matches. Otherwise, the exception search continues as if the exception type
     388did not match.
     389\begin{cfa}
     390try {
     391        f1 = open( ... );
     392        f2 = open( ... );
     393        ...
     394} catch( IOFailure * f ; fd( f ) == f1 ) {
     395        // only handle IO failure for f1
     396}
     397\end{cfa}
     398Note, catching @IOFailure@, checking for @f1@ in the handler, and reraising the
     399exception if not @f1@ is different because the reraise does not examine any of
     400remaining handlers in the current try statement.
     401
     402\section{Rethrowing}
     403\colour{red}{From Andrew: I recomend we talk about why the language doesn't
     404have rethrows/reraises instead.}
     405
     406\label{s:Rethrowing}
     407Within the handler block or functions called from the handler block, it is
     408possible to reraise the most recently caught exception with @throw@ or
     409@throwResume@, respectively.
     410\begin{cfa}
     411try {
     412        ...
     413} catch( ... ) {
     414        ... throw;
     415} catchResume( ... ) {
     416        ... throwResume;
     417}
     418\end{cfa}
     419The only difference between a raise and a reraise is that reraise does not
     420create a new exception; instead it continues using the current exception, \ie
     421no allocation and copy. However the default handler is still set to the one
     422visible at the raise point, and hence, for termination could refer to data that
     423is part of an unwound stack frame. To prevent this problem, a new default
     424handler is generated that does a program-level abort.
    211425
    212426\section{Finally Clauses}
    213 
    214 A \codeCFA{finally} clause may be placed at the end of a try statement after
    215 all the handler clauses. In the simply case, with no handlers, it looks like
    216 this:
    217 
    218 \begin{lstlisting}
    219 try {
    220     TRY_BLOCK
    221 } finally {
    222     FINAL_STATEMENTS
    223 }
    224 \end{lstlisting}
    225 
    226 Any number of termination handlers and resumption handlers may proceed the
    227 finally clause.
    228 
    229 The FINAL\_STATEMENTS, the finally block, are executed whenever the try
    230 statement is removed from the stack. This includes: the TRY\_BLOCK finishes
    231 executing, a termination exception finishes executing and the stack unwinds.
    232 
    233 Execution of the finally block should finish by letting control run off
    234 the end of the block. This is because after the finally block is complete
    235 control will continue to where ever it would if the finally clause was not
    236 present.
    237 
    238 Because of this local control flow out of the finally block is forbidden.
    239 The compiler rejects uses of \codeCFA{break}, \codeCFA{continue},
    240 \codeCFA{fallthru} and \codeCFA{return} that would cause control to leave
    241 the finally block. Other ways to leave the finally block - such as a long
    242 jump or termination - are much harder to check, at best requiring additional
    243 runtime overhead, and so are merely discouraged.
     427Finally clauses are used to preform unconditional clean-up when leaving a
     428scope. They are placed at the end of a try statement:
     429\begin{cfa}
     430try {
     431        GUARDED_BLOCK
     432} ... // any number or kind of handler clauses
     433... finally {
     434        FINALLY_BLOCK
     435}
     436\end{cfa}
     437The @FINALLY_BLOCK@ is executed when the try statement is removed from the
     438stack, including when the @GUARDED_BLOCK@ finishes, any termination handler
     439finishes or during an unwind.
     440The only time the block is not executed is if the program is exited before
     441the stack is unwound.
     442
     443Execution of the finally block should always finish, meaning control runs off
     444the end of the block. This requirement ensures always continues as if the
     445finally clause is not present, \ie finally is for cleanup not changing control
     446flow. Because of this requirement, local control flow out of the finally block
     447is forbidden. The compiler precludes any @break@, @continue@, @fallthru@ or
     448@return@ that causes control to leave the finally block. Other ways to leave
     449the finally block, such as a long jump or termination are much harder to check,
     450and at best requiring additional run-time overhead, and so are mearly
     451discouraged.
     452
     453Not all languages with exceptions have finally clauses. Notably \Cpp does
     454without it as descructors serve a similar role. Although destructors and
     455finally clauses can be used in many of the same areas they have their own
     456use cases like top-level functions and lambda functions with closures.
     457Destructors take a bit more work to set up but are much easier to reuse while
     458finally clauses are good for once offs and can include local information.
    244459
    245460\section{Cancellation}
    246 
    247 Cancellation can be thought of as a stack-level abort or as an uncatchable
    248 termination. It unwinds the entirety of the current exception and if possible
    249 passes an exception to a different stack as a message.
    250 
    251 There is no special statement for starting a cancellation, instead you call
    252 the standard libary function \codeCFA{cancel\_stack} which takes an exception.
    253 Unlike in a throw this exception is not used in control flow but is just there
    254 to pass information about why the cancellation happened.
    255 
    256 The handler is decided entirely by which stack is being cancelled. There are
    257 three handlers that apply to three different groups of stacks:
    258 \begin{itemize}
    259 \item Main Stack:
    260 The main stack is the one on which the program main is called at the beginning
    261 of your program. It is also the only stack you have without the libcfathreads.
    262 
    263 Because of this there is no other stack ``above" (or possibly at all) for main
    264 to notify when a cancellation occurs. So after the stack is unwound we do a
    265 program level abort.
    266 
    267 \item Thread Stack:
    268 Thread stacks are those created \codeCFA{thread} or otherwise satify the
    269 \codeCFA{is\_thread} trait.
    270 
    271 Threads only have two structural points of communication that must happen,
    272 start and join. As the thread must be running to preform a cancellation it
    273 will be after start and before join, so join is one cancellation uses.
    274 
    275 After the stack is unwound the thread will halt as if had completed normally
    276 and wait for another thread to join with it. The other thread, when it joins,
    277 checks for a cancellation. If so it will throw the resumption exception
    278 \codeCFA{ThreadCancelled}.
    279 
    280 There is a difference here in how explicate joins (with the \codeCFA{join}
    281 function) and implicate joins (from a destructor call). Explicate joins will
    282 take the default handler (\codeCFA{defaultResumptionHandler}) from the context
    283 and use like a regular through does if the exception is not caught. The
    284 implicate join does a program abort instead.
    285 
    286 This is for safety. One of the big problems in exceptions is you cannot handle
    287 two terminations or cancellations on the same stack as either can destroy the
    288 context required for the other. This can happen with join but as the
    289 destructors will always be run when the stack is being unwound and one
    290 termination/cancellation is already active. Also since they are implicite they
    291 are easier to forget about.
    292 
    293 \item Coroutine Stack:
    294 Coroutine stacks are those created with \codeCFA{coroutine} or otherwise
    295 satify the \codeCFA{is\_coroutine} trait.
    296 
    297 A coroutine knows of two other coroutines, its starter and its last resumer.
    298 The last resumer is ``closer" so that is the one notified.
    299 
    300 After the stack is unwound control goes to the last resumer.
    301 Resume will resume throw a \codeCFA{CoroutineCancelled} exception, which is
    302 polymorphic over the coroutine type and has a pointer to the coroutine being
    303 cancelled and the cancelling exception. The resume function also has an
    304 assertion that the \codeCFA{defaultResumptionHandler} for the exception. So it
    305 will use the default handler like a regular throw.
    306 
    307 \end{itemize}
     461Cancellation is a stack-level abort, which can be thought of as as an
     462uncatchable termination. It unwinds the entirety of the current stack, and if
     463possible forwards the cancellation exception to a different stack.
     464
     465Cancellation is not an exception operation like termination or resumption.
     466There is no special statement for starting a cancellation; instead the standard
     467library function @cancel_stack@ is called passing an exception. Unlike a
     468throw, this exception is not used in matching only to pass information about
     469the cause of the cancellation.
     470(This also means matching cannot fail so there is no default handler either.)
     471
     472After @cancel_stack@ is called the exception is copied into the exception
     473handling mechanism's memory. Then the entirety of the current stack is
     474unwound. After that it depends one which stack is being cancelled.
     475\begin{description}
     476\item[Main Stack:]
     477The main stack is the one used by the program main at the start of execution,
     478and is the only stack in a sequential program. Even in a concurrent program
     479the main stack is only dependent on the environment that started the program.
     480Hence, when the main stack is cancelled there is nowhere else in the program
     481to notify. After the stack is unwound, there is a program-level abort.
     482
     483\item[Thread Stack:]
     484A thread stack is created for a @thread@ object or object that satisfies the
     485@is_thread@ trait. A thread only has two points of communication that must
     486happen: start and join. As the thread must be running to perform a
     487cancellation, it must occur after start and before join, so join is used
     488for communication here.
     489After the stack is unwound, the thread halts and waits for
     490another thread to join with it. The joining thread checks for a cancellation,
     491and if present, resumes exception @ThreadCancelled@.
     492
     493There is a subtle difference between the explicit join (@join@ function) and
     494implicit join (from a destructor call). The explicit join takes the default
     495handler (@defaultResumptionHandler@) from its calling context, which is used if
     496the exception is not caught. The implicit join does a program abort instead.
     497
     498This semantics is for safety. If an unwind is triggered while another unwind
     499is underway only one of them can proceed as they both want to ``consume'' the
     500stack. Letting both try to proceed leads to very undefined behaviour.
     501Both termination and cancellation involve unwinding and, since the default
     502@defaultResumptionHandler@ preforms a termination that could more easily
     503happen in an implicate join inside a destructor. So there is an error message
     504and an abort instead.
     505\todo{Perhaps have a more general disucssion of unwind collisions before
     506this point.}
     507
     508The recommended way to avoid the abort is to handle the intial resumption
     509from the implicate join. If required you may put an explicate join inside a
     510finally clause to disable the check and use the local
     511@defaultResumptionHandler@ instead.
     512
     513\item[Coroutine Stack:] A coroutine stack is created for a @coroutine@ object
     514or object that satisfies the @is_coroutine@ trait. A coroutine only knows of
     515two other coroutines, its starter and its last resumer. Of the two the last
     516resumer has the tightest coupling to the coroutine it activated and the most
     517up-to-date information.
     518
     519Hence, cancellation of the active coroutine is forwarded to the last resumer
     520after the stack is unwound. When the resumer restarts, it resumes exception
     521@CoroutineCancelled@, which is polymorphic over the coroutine type and has a
     522pointer to the cancelled coroutine.
     523
     524The resume function also has an assertion that the @defaultResumptionHandler@
     525for the exception. So it will use the default handler like a regular throw.
     526\end{description}
  • doc/theses/andrew_beach_MMath/future.tex

    r342af53 r8e4aa05  
    11\chapter{Future Work}
    22
     3\section{Language Improvements}
     4\CFA is a developing programming language. As such, there are partially or
     5unimplemented features of the language (including several broken components)
     6that I had to workaround while building an exception handling system largely in
     7the \CFA language (some C components).  The following are a few of these
     8issues, and once implemented/fixed, how this would affect the exception system.
     9\begin{itemize}
     10\item
     11The implementation of termination is not portable because it includes
     12hand-crafted assembly statements. These sections must be ported by hand to
     13support more hardware architectures, such as the ARM processor.
     14\item
     15Due to a type-system problem, the catch clause cannot bind the exception to a
     16reference instead of a pointer. Since \CFA has a very general reference
     17capability, programmers will want to use it. Once fixed, this capability should
     18result in little or no change in the exception system.
     19\item
     20Termination handlers cannot use local control-flow transfers, \eg by @break@,
     21@return@, \etc. The reason is that current code generation hoists a handler
     22into a nested function for convenience (versus assemble-code generation at the
     23@try@ statement). Hence, when the handler runs, its code is not in the lexical
     24scope of the @try@ statement, where the local control-flow transfers are
     25meaningful.
     26\item
     27There is no detection of colliding unwinds. It is possible for clean-up code
     28run during an unwind to trigger another unwind that escapes the clean-up code
     29itself; such as a termination exception caught further down the stack or a
     30cancellation. There do exist ways to handle this but currently they are not
     31even detected and the first unwind will simply be forgotten, often leaving
     32it in a bad state.
     33\item
     34Also the exception system did not have a lot of time to be tried and tested.
     35So just letting people use the exception system more will reveal new
     36quality of life upgrades that can be made with time.
     37\end{itemize}
     38
    339\section{Complete Virtual System}
    4 The virtual system should be completed. It was never supposed to be part of
    5 this project and so minimal work was done on it. A draft of what the complete
    6 system might look like was created but it was never finalized or implemented.
    7 A future project in \CFA would be to complete that work and to update the
    8 parts of the exception system that use the current version.
     40The virtual system should be completed. It was not supposed to be part of this
     41project, but was thrust upon it to do exception inheritance; hence, only
     42minimal work was done. A draft for a complete virtual system is available but
     43it is not finalized.  A future \CFA project is to complete that work and then
     44update the exception system that uses the current version.
    945
    10 For instance a full virtual system would probably allow for several
    11 improvements to the exception traits. Although they do currently work they
    12 could be made easier to use by making the virtual table type implitate in the
    13 trait (which would remove the need for those wrapper marcos) or allowing
    14 for assertions that give the layout of a virtual table for safety.
     46There are several improvements to the virtual system that would improve the
     47exception traits. The most important one is an assertion to check one virtual
     48type is a child of another. This check precisely captures many of the
     49correctness requirements.
    1550
    16 \section{Additional Throws}
    17 Several other kinds of throws, beyond the termination throw (\codeCFA{throw}),
    18 the resumption throw (\codeCFA{throwResume}) and the re-throws, were considered.
    19 None were as useful as the core throws but they would likely be worth
    20 revising.
     51The full virtual system might also include other improvement like associated
     52types to allow traits to refer to types not listed in their header. This
     53feature allows exception traits to not refer to the virtual-table type
     54explicitly, removing the need for the current interface macros.
    2155
    22 The first ones are throws for asynchronous exceptions, throwing exceptions
    23 from one stack to another. These act like signals allowing for communication
    24 between the stacks. This is usually used with resumption as it allows the
    25 target stack to continue execution normally after the exception has been
    26 handled.
     56\section{Additional Raises}
     57Several other kinds of exception raises were considered beyond termination
     58(@throw@), resumption (@throwResume@), and reraise.
    2759
    28 This would much more coordination between the concurrency system and the
    29 exception system to handle. Most of the interesting design decisions around
    30 applying asynchronous exceptions appear to be around masking (controlling
    31 which exceptions may be thrown at a stack). It would likely require more of
    32 the virtual system and would also effect how default handlers are set.
     60The first is a non-local/concurrent raise providing asynchronous exceptions,
     61\ie raising an exception on another stack. This semantics acts like signals
     62allowing for out-of-band communication among coroutines and threads. This kind
     63of raise is often restricted to resumption to allow the target stack to
     64continue execution normally after the exception has been handled. That is,
     65allowing one coroutine/thread to unwind the stack of another via termination is
     66bad software engineering.
    3367
    34 The other throws were designed to mimic bidirectional algebraic effects.
    35 Algebraic effects are used in some functional languages and allow a function
     68Non-local/concurrent requires more coordination between the concurrency system
     69and the exception system. Many of the interesting design decisions centre
     70around masking (controlling which exceptions may be thrown at a stack). It
     71would likely require more of the virtual system and would also effect how
     72default handlers are set.
     73
     74Other raises were considered to mimic bidirectional algebraic effects.
     75Algebraic effects are used in some functional languages allowing one function
    3676to have another function on the stack resolve an effect (which is defined with
    37 a function-like interface).
    38 These can be mimiced with resumptions and the the new throws were designed
    39 to try and mimic bidirectional algebraic effects, where control can go back
    40 and forth between the function effect caller and handler while the effect
    41 is underway.
     77a functional-like interface).  This semantics can be mimicked with resumptions
     78and new raises were discussed to mimic bidirectional algebraic-effects, where
     79control can go back and forth between the function-effect caller and handler
     80while the effect is underway.
    4281% resume-top & resume-reply
     82These raises would be like the resumption raise except using different search
     83patterns to find the handler.
    4384
    44 These throws would likely be just like the resumption throw except they would
    45 use different search patterns to find the handler to reply to.
     85\section{Zero-Cost Try}
     86\CFA does not have zero-cost try-statements because the compiler generates C
     87code rather than assembler code \see{\VPageref{p:zero-cost}}. When the compiler
     88does create its own assembly (or LLVM byte-code), then zero-cost try-statements
     89are possible. The downside of zero-cost try-statements is the LSDA complexity,
     90its size (program bloat), and the high cost of raising an exception.
    4691
    47 \section{Zero-Cost Exceptions}
    48 \CFA does not have zero-cost exceptions because it does not generate assembly
    49 but instead generates C code. See the implementation section. When the
    50 compiler does start to create its own assembly (or LLVM byte code) then
    51 zero-cost exceptions could be implemented.
     92Alternatively, some research could be done into the simpler alternative method
     93with a non-zero-cost try-statement but much lower cost exception raise. For
     94example, programs are starting to use exception in the normal control path, so
     95more exceptions are thrown. In these cases, the cost balance switches towards
     96low-cost raise. Unfortunately, while exceptions remain exceptional, the
     97libunwind model will probably remain the most effective option.
    5298
    53 Now in zero-cost exceptions the only part that is zero-cost are the try
    54 blocks. Some research could be done into the alternative methods for systems
    55 that expect a lot more exceptions to be thrown, allowing some overhead in
    56 entering and leaving try blocks to make throws faster. But while exceptions
    57 remain exceptional the libunwind model will probably remain the most effective
    58 option.
     99Zero-cost resumptions is still an open problem. First, because libunwind does
     100not support a successful-exiting stack-search without doing an unwind.
     101Workarounds are possible but awkward. Ideally an extension to libunwind could
     102be made, but that would either require separate maintenance or gain enough
     103support to have it folded into the standard.
    59104
    60 Zero-cost resumptions have more problems to solve. First because libunwind
    61 does not support a successful exiting stack search without doing an unwind.
    62 There are several ways to hack that functionality in. Ideally an extension to
    63 libunwind could be made, but that would either require seperate maintenance
    64 or gain enough support to have it folded into the standard.
     105Also new techniques to skip previously searched parts of the stack need to be
     106developed to handle the recursive resume problem and support advanced algebraic
     107effects.
    65108
    66 Also new techniques to skip previously searched parts of the stack will have
    67 to be developed.
     109\section{Signal Exceptions}
     110Goodenough~\cite{Goodenough75} suggests three types of exceptions: escape,
     111notify and signal.  Escape are termination exceptions, notify are resumption
     112exceptions, leaving signal unimplemented.
    68113
    69 \section{Support for More Platforms}
    70 Termination is not portable because it is implemented with inline assembly.
    71 Those sections will have to be rewritten to support different architectures
     114A signal exception allows either behaviour, \ie after an exception is handled,
     115the handler has the option of returning to the raise or after the @try@
     116statement. Currently, \CFA fixes the semantics of the handler return
     117syntactically by the @catch@ or @catchResume@ clause.
    72118
    73 \section{Quality-of-Life Improvements}
    74 Finally come various improvements to the usability of \CFA. Most of these
    75 would just require time. Time that would not lead to interesting research so
    76 it has been left aside for now. A few examples are included here but there
    77 are more:
     119Signal exception should be reexamined and possibly be supported in \CFA. A very
     120direct translation is to have a new raise and catch pair, and a new statement
     121(or statements) would indicate if the handler returns to the raise or continues
     122where it is; but there may be other options.
    78123
    79 \begin{itemize}
    80 \item Allowing exception handler to bind the exception to a reference instead
    81 of a pointer. This should actually result in no change in behaviour so there
    82 is no reason not to allow it. It is however a small improvement; giving a bit
    83 of flexibility to the user in what style they want to use.
    84 \item Enabling local control flow (by \codeCFA{break}, \codeCFA{return} and
    85 similar statements) out of a termination handler. The current set-up makes
    86 this very difficult but the catch function that runs the handler after it has
    87 been matched could be inlined into the function's body, which would make this
    88 much easier. (To do the same for try blocks would probably wait for zero-cost
    89 exceptions, which would allow the try block to be inlined as well.)
    90 \item Enabling local control flow out of a resumption handler. This would be
    91 a weighty operation, causing a stack unwind like a termination, so there might
    92 be a different statement or a statement modifier to make sure the user does
    93 this purposefully.
     124For instance, resumption could be extended to cover this use by allowing local
     125control flow out of it. This approach would require an unwind as part of the
     126transition as there are stack frames that have to be removed.  This approach
     127means there is no notify raise, but because \CFA does not have exception
     128signatures, a termination can be thrown from within any resumption handler so
     129there is already a way to do mimic this in existing \CFA.
    94130
    95 However this would require the more complex system as they cannot be inlined
    96 into the original function as they can be run at a different place on the
    97 stack. So instead the unwinding will have to carry with it information on
    98 which one of these points to continue at and possibly also the return value
    99 for the function if a \codeCFA{return} statement was used.
    100 \end{itemize}
     131% Maybe talk about the escape; and escape CONTROL_STMT; statements or how
     132% if we could choose if _Unwind_Resume proceeded to the clean-up stage this
     133% would be much easier to implement.
  • doc/theses/andrew_beach_MMath/implement.tex

    r342af53 r8e4aa05  
    22% Goes over how all the features are implemented.
    33
     4The implementation work for this thesis covers two components: the virtual
     5system and exceptions. Each component is discussed in detail.
     6
    47\section{Virtual System}
     8\label{s:VirtualSystem}
    59% Virtual table rules. Virtual tables, the pointer to them and the cast.
    6 The \CFA virtual system only has one public facing feature: virtual casts.
    7 However there is a lot of structure to support that and provide some other
    8 features for the standard library.
    9 
    10 All of this is accessed through a field inserted at the beginning of every
    11 virtual type. Currently it is called \codeC{virtual_table} but it is not
    12 ment to be accessed by the user. This field is a pointer to the type's
    13 virtual table instance. It is assigned once during the object's construction
    14 and left alone after that.
    15 
    16 \subsection{Virtual Table Construction}
    17 For each virtual type a virtual table is constructed. This is both a new type
    18 and an instance of that type. Other instances of the type could be created
    19 but the system doesn't use them. So this section will go over the creation of
    20 the type and the instance.
    21 
    22 Creating the single instance is actually very important. The address of the
    23 table acts as the unique identifier for the virtual type. Similarly the first
    24 field in every virtual table is the parent's id; a pointer to the parent
    25 virtual table instance.
    26 
    27 The remaining fields contain the type's virtual members. First come the ones
    28 present on the parent type, in the same order as they were the parent, and
    29 then any that this type introduces. The types of the ones inherited from the
    30 parent may have a slightly modified type, in that references to the
    31 dispatched type are replaced with the current virtual type. These are always
    32 taken by pointer or reference.
    33 
    34 The structure itself is created where the virtual type is created. The name
    35 of the type is created by mangling the name of the base type. The name of the
    36 instance is also generated by name mangling.
    37 
    38 The fields are initialized automatically.
     10While the \CFA virtual system currently has only one public feature, virtual
     11cast \see{\VPageref{p:VirtualCast}}, substantial structure is required to
     12support it, and provide features for exception handling and the standard
     13library.
     14
     15\subsection{Virtual Type}
     16Virtual types only have one change to their structure, the addition of a
     17pointer to the virtual table. This is always the first field so that
     18if it is cast to a supertype the field's location is still known.
     19
     20This field is set as part of all new generated constructors.
     21\todo{They only come as part exceptions and don't work.}
     22After the object is created the field is constant.
     23
     24However it can be read from, internally it is just a regular field called
     25@virtual_table@. Dereferencing it gives the virtual table and access to the
     26type's virtual members.
     27
     28\subsection{Virtual Table}
     29Every time a virtual type is defined the new virtual table type must also be
     30defined.
     31
     32The unique instance is important because the address of the virtual table
     33instance is used as the identifier for the virtual type. So a pointer to the
     34virtual table and the ID for the virtual type are interchangable.
     35\todo{Unique instances might be going so we will have to talk about the new
     36system instead.}
     37
     38The first step in putting it all together is to create the virtual table type.
     39The virtual table type is just a structure and can be described in terms of
     40its fields. The first field is always the parent type ID (or a pointer to
     41the parent virtual table) or 0 (the null pointer).
     42Next are other fields on the parent virtual table are repeated.
     43Finally are the fields used to store any new virtual members of the new
     44The virtual type
     45
     46The virtual system is accessed through a private constant field inserted at the
     47beginning of every virtual type, called the virtual-table pointer. This field
     48points at a type's virtual table and is assigned during the object's
     49construction. The address of a virtual table acts as the unique identifier for
     50the virtual type, and the first field of a virtual table is a pointer to the
     51parent virtual-table or @0p@. The remaining fields are duplicated from the
     52parent tables in this type's inheritance chain, followed by any fields this type
     53introduces. Parent fields are duplicated so they can be changed (all virtual
     54members are overridable), so that references to the dispatched type
     55are replaced with the current virtual type.
     56% These are always taken by pointer or reference.
     57
     58% Simple ascii diragram:
     59\begin{verbatim}
     60parent_pointer  \
     61parent_field0   |
     62...             | Same layout as parent.
     63parent_fieldN   /
     64child_field0
     65...
     66child_fieldN
     67\end{verbatim}
     68\todo{Refine the diagram}
     69
     70% For each virtual type, a virtual table is constructed. This is both a new type
     71% and an instance of that type. Other instances of the type could be created
     72% but the system doesn't use them. So this section will go over the creation of
     73% the type and the instance.
     74
     75A virtual table is created when the virtual type is created. The name of the
     76type is created by mangling the name of the base type. The name of the instance
     77is also generated by name mangling. The fields are initialized automatically.
    3978The parent field is initialized by getting the type of the parent field and
    4079using that to calculate the mangled name of the parent's virtual table type.
    4180There are two special fields that are included like normal fields but have
    42 special initialization rules: the \codeC{size} field is the type's size and is
    43 initialized with a sizeof expression, the \codeC{align} field is the type's
    44 alignment and uses an alignof expression. The remaining fields are resolved
    45 to a name matching the field's name and type using the normal visibility
    46 and overload resolution rules of the type system.
    47 
    48 These operations are split up into several groups depending on where they
    49 take place which can vary for monomorphic and polymorphic types. The first
    50 devision is between the declarations and the definitions. Declarations, such
    51 as a function signature or a structure's name, must always be visible but may
    52 be repeated so they go in headers. Definitions, such as function bodies and a
    53 structure's layout, don't have to be visible on use but must occur exactly
    54 once and go into source files.
    55 
     81special initialization rules: the @size@ field is the type's size and is
     82initialized with a @sizeof@ expression, the @align@ field is the type's
     83alignment and uses an @alignof@ expression. The remaining fields are resolved
     84to a name matching the field's name and type using the normal visibility and
     85overload resolution rules of the type system.
     86
     87These operations are split up into several groups depending on where they take
     88place which varies for monomorphic and polymorphic types. The first devision is
     89between the declarations and the definitions. Declarations, such as a function
     90signature or a aggregate's name, must always be visible but may be repeated in
     91the form of forward declarations in headers. Definitions, such as function
     92bodies and a aggregate's layout, can be separately compiled but must occur
     93exactly once in a source file.
     94
     95\begin{sloppypar}
    5696The declarations include the virtual type definition and forward declarations
    5797of the virtual table instance, constructor, message function and
    58 \codeCFA{get_exception_vtable}. The definition includes the storage and
    59 initialization of the virtual table instance and the bodies of the three
    60 functions.
     98@get_exception_vtable@. The definition includes the storage and initialization
     99of the virtual table instance and the bodies of the three functions.
     100\end{sloppypar}
    61101
    62102Monomorphic instances put all of these two groups in one place each.
    63 
    64 Polymorphic instances also split out the core declarations and definitions
    65 from the per-instance information. The virtual table type and most of the
    66 functions are polymorphic so they are all part of the core. The virtual table
    67 instance and the \codeCFA{get_exception_vtable} function.
    68 
    69 Coroutines and threads need instances of \codeCFA{CoroutineCancelled} and
    70 \codeCFA{ThreadCancelled} respectively to use all of their functionality.
    71 When a new data type is declared with \codeCFA{coroutine} or \codeCFA{thread}
    72 the forward declaration for the instance is created as well. The definition
    73 of the virtual table is created at the definition of the main function.
     103Polymorphic instances also split out the core declarations and definitions from
     104the per-instance information. The virtual table type and most of the functions
     105are polymorphic so they are all part of the core. The virtual table instance
     106and the @get_exception_vtable@ function.
     107
     108\begin{sloppypar}
     109Coroutines and threads need instances of @CoroutineCancelled@ and
     110@ThreadCancelled@ respectively to use all of their functionality. When a new
     111data type is declared with @coroutine@ or @thread@ the forward declaration for
     112the instance is created as well. The definition of the virtual table is created
     113at the definition of the main function.
     114\end{sloppypar}
    74115
    75116\subsection{Virtual Cast}
    76 Virtual casts are implemented as a function call that does the check and a
    77 old C-style cast to do the type conversion. The C-cast is just to make sure
    78 the generated code is correct so the rest of the section is about that
    79 function.
    80 
    81 The function is \codeC{__cfa__virtual_cast} and it is implemented in the
    82 standard library. It takes a pointer to the target type's virtual table and
    83 the object pointer being cast. The function is very simple, getting the
    84 object's virtual table pointer and then checking to see if it or any of
    85 its ancestors, by using the parent pointers, are the same as the target type
    86 virtual table pointer. It does this in a simple loop.
    87 
    88 For the generated code a forward decaration of the virtual works as follows.
    89 There is a forward declaration of \codeC{__cfa__virtual_cast} in every cfa
    90 file so it can just be used. The object argument is the expression being cast
    91 so that is just placed in the argument list.
    92 
    93 To build the target type parameter the compiler will create a mapping from
    94 concrete type-name -- so for polymorphic types the parameters are filled in
    95 -- to virtual table address. Every virtual table declaraction is added to the
    96 this table; repeats are ignored unless they have conflicting definitions.
    97 This does mean the declaractions have to be in scope, but they should usually
    98 be introduced as part of the type definition.
     117Virtual casts are implemented as a function call that does the subtype check
     118and a C coercion-cast to do the type conversion.
     119% The C-cast is just to make sure the generated code is correct so the rest of
     120% the section is about that function.
     121The function is
     122\begin{cfa}
     123void * __cfa__virtual_cast(
     124        struct __cfa__parent_vtable const * parent,
     125        struct __cfa__parent_vtable const * const * child );
     126\end{cfa}
     127and it is implemented in the standard library. The structure reperents the
     128head of a vtable which is the pointer to the parent virtual table. The
     129@parent@ points directly at the parent type virtual table while the @child@
     130points at the object of the (possibe) child type.
     131
     132In terms of the virtual cast expression, @parent@ comes from looking up the
     133type being cast to and @child@ is the result of the expression being cast.
     134Because the complier outputs C code, some type C type casts are also used.
     135The last bit of glue is an map that saves every virtual type the compiler
     136sees. This is used to check the type used in a virtual cast is a virtual
     137type and to get its virtual table.
     138(It also checks for conflicting definitions.)
     139
     140Inside the function it is a simple conditional. If the type repersented by
     141@parent@ is or is an ancestor of the type repersented by @*child@ (it
     142requires one more level of derefence to pass through the object) then @child@
     143is returned, otherwise the null pointer is returned.
     144
     145The check itself is preformed is a simple linear search. If the child
     146virtual table or any of its ancestors (which are retreved through the first
     147field of every virtual table) are the same as the parent virtual table then
     148the cast succeeds.
    99149
    100150\section{Exceptions}
     
    106156% resumption doesn't as well.
    107157
    108 Many modern languages work with an interal stack that function push and pop
    109 their local data to. Stack unwinding removes large sections of the stack,
    110 often across functions.
    111 
    112 At a very basic level this can be done with \codeC{setjmp} \& \codeC{longjmp}
    113 which simply move the top of the stack, discarding everything on the stack
    114 above a certain point. However this ignores all the clean-up code that should
    115 be run when certain sections of the stack are removed (for \CFA these are from
    116 destructors and finally clauses) and also requires that the point to which the
    117 stack is being unwound is known ahead of time. libunwind is used to address
    118 both of these problems.
    119 
    120 Libunwind, provided in \texttt{unwind.h} on most platorms, is a C library
    121 that provides \CPP style stack unwinding. Its operation is divided into two
    122 phases. The search phase -- phase 1 -- is used to scan the stack and decide
    123 where the unwinding will stop, this allows for a dynamic target. The clean-up
    124 phase -- phase 2 -- does the actual unwinding and also runs any clean-up code
    125 as it goes.
    126 
    127 To use the libunwind each function must have a personality function and an
    128 LSDA (Language Specific Data Area). Libunwind actually does very little, it
    129 simply moves down the stack from function to function. Most of the actions are
    130 implemented by the personality function which libunwind calls on every
    131 function. Since this is shared across many functions or even every function in
    132 a language it will need a bit more information. This is provided by the LSDA
    133 which has the unique information for each function.
    134 
    135 Theoretically the LSDA can contain anything but conventionally it is a table
    136 with entries reperenting areas of the function and what has to be done there
    137 during unwinding. These areas are described in terms of where the instruction
    138 pointer is. If the current value of the instruction pointer is between two
    139 values reperenting the beginning and end of a region then execution is
    140 currently being executed. These are used to mark out try blocks and the
    141 scopes of objects with destructors to run.
    142 
    143 GCC will generate an LSDA and attach its personality function with the
    144 \texttt{-fexceptions} flag. However this only handles the cleanup attribute.
    145 This attribute is used on a variable and specifies a function that should be
    146 run when the variable goes out of scope. The function is passed a pointer to
    147 the object as well so it can be used to mimic destructors. It however cannot
    148 be used to mimic try statements.
    149 
    150 \subsection{Implementing Personality Functions}
    151 Personality functions have a complex interface specified by libunwind.
    152 This section will cover some of the important parts of that interface.
    153 
    154 \begin{lstlisting}
    155 typedef _Unwind_Reason_Code (*_Unwind_Personality_Fn)(
    156     int version,
    157     _Unwind_Action action,
    158     _Unwind_Exception_Class exception_class,
    159     _Unwind_Exception * exception,
    160     struct _Unwind_Context * context);
     158% Many modern languages work with an interal stack that function push and pop
     159% their local data to. Stack unwinding removes large sections of the stack,
     160% often across functions.
     161
     162Stack unwinding is the process of removing stack frames (activations) from the
     163stack. On function entry and return, unwinding is handled directly by the code
     164embedded in the function. Usually, the stack-frame size is known statically
     165based on parameter and local variable declarations. For dynamically-sized
     166local variables, a runtime computation is necessary to know the frame
     167size. Finally, a function's frame-size may change during execution as local
     168variables (static or dynamic sized) go in and out of scope.
     169Allocating/deallocating stack space is usually an $O(1)$ operation achieved by
     170bumping the hardware stack-pointer up or down as needed.
     171
     172Unwinding across multiple stack frames is more complex because individual stack
     173management code associated with each frame is bypassed. That is, the location
     174of a function's frame-management code is largely unknown and dispersed
     175throughout the function, hence the current frame size managed by that code is
     176also unknown. Hence, code unwinding across frames does not have direct
     177knowledge about what is on the stack, and hence, how much of the stack needs to
     178be removed.
     179
     180% At a very basic level this can be done with @setjmp@ \& @longjmp@ which simply
     181% move the top of the stack, discarding everything on the stack above a certain
     182% point. However this ignores all the cleanup code that should be run when
     183% certain sections of the stack are removed (for \CFA these are from destructors
     184% and finally clauses) and also requires that the point to which the stack is
     185% being unwound is known ahead of time. libunwind is used to address both of
     186% these problems.
     187
     188The traditional unwinding mechanism for C is implemented by saving a snap-shot
     189of a function's state with @setjmp@ and restoring that snap-shot with
     190@longjmp@. This approach bypasses the need to know stack details by simply
     191reseting to a snap-shot of an arbitrary but existing function frame on the
     192stack. It is up to the programmer to ensure the snap-shot is valid when it is
     193reset, making this unwinding approach fragile with potential errors that are
     194difficult to debug because the stack becomes corrupted.
     195
     196However, many languages define cleanup actions that must be taken when objects
     197are deallocated from the stack or blocks end, such as running a variable's
     198destructor or a @try@ statement's @finally@ clause. Handling these mechanisms
     199requires walking the stack and checking each stack frame for these potential
     200actions.
     201
     202For exceptions, it must be possible to walk the stack frames in search of @try@
     203statements to match and execute a handler. For termination exceptions, it must
     204also be possible to unwind all stack frames from the throw to the matching
     205catch, and each of these frames must be checked for cleanup actions. Stack
     206walking is where most of the complexity and expense of exception handling
     207appears.
     208
     209One of the most popular tools for stack management is libunwind, a low-level
     210library that provides tools for stack walking, handler execution, and
     211unwinding. What follows is an overview of all the relevant features of
     212libunwind needed for this work, and how \CFA uses them to implement exception
     213handling.
     214
     215\subsection{libunwind Usage}
     216Libunwind, accessed through @unwind.h@ on most platforms, is a C library that
     217provides \CC-style stack-unwinding. Its operation is divided into two phases:
     218search and cleanup. The dynamic target search -- phase 1 -- is used to scan the
     219stack and decide where unwinding should stop (but no unwinding occurs). The
     220cleanup -- phase 2 -- does the unwinding and also runs any cleanup code.
     221
     222To use libunwind, each function must have a personality function and a Language
     223Specific Data Area (LSDA). The LSDA has the unique information for each
     224function to tell the personality function where a function is executing, its
     225current stack frame, and what handlers should be checked. Theoretically, the
     226LSDA can contain any information but conventionally it is a table with entries
     227representing regions of the function and what has to be done there during
     228unwinding. These regions are bracketed by the instruction pointer. If the
     229instruction pointer is within a region's start/end, then execution is currently
     230executing in that region. Regions are used to mark out the scopes of objects
     231with destructors and try blocks.
     232
     233% Libunwind actually does very little, it simply moves down the stack from
     234% function to function. Most of the actions are implemented by the personality
     235% function which libunwind calls on every function. Since this is shared across
     236% many functions or even every function in a language it will need a bit more
     237% information.
     238
     239The GCC compilation flag @-fexceptions@ causes the generation of an LSDA and
     240attaches its personality function. However, this
     241flag only handles the cleanup attribute:
     242\todo{Peter: What is attached? Andrew: It uses the .cfi\_personality directive
     243and that's all I know.}
     244\begin{cfa}
     245void clean_up( int * var ) { ... }
     246int avar __attribute__(( cleanup(clean_up) ));
     247\end{cfa}
     248which is used on a variable and specifies a function, in this case @clean_up@,
     249run when the variable goes out of scope.
     250The function is passed a pointer to the object being removed from the stack
     251so it can be used to mimic destructors.
     252However, this feature cannot be used to mimic @try@ statements as it cannot
     253control the unwinding.
     254
     255\subsection{Personality Functions}
     256Personality functions have a complex interface specified by libunwind. This
     257section covers some of the important parts of the interface.
     258
     259A personality function can preform different actions depending on how it is
     260called.
     261\begin{lstlisting}[language=C,{moredelim=**[is][\color{red}]{@}{@}}]
     262typedef _Unwind_Reason_Code (*@_Unwind_Personality_Fn@) (
     263        _Unwind_Action @action@,
     264        _Unwind_Exception_Class @exception_class@,
     265        _Unwind_Exception * @exception@,
     266        struct _Unwind_Context * @context@
     267);
    161268\end{lstlisting}
    162 
    163 The return value, the reason code, is an enumeration of possible messages
     269The @action@ argument is a bitmask of possible actions:
     270\begin{enumerate}
     271\item
     272@_UA_SEARCH_PHASE@ specifies a search phase and tells the personality function
     273to check for handlers. If there is a handler in a stack frame, as defined by
     274the language, the personality function returns @_URC_HANDLER_FOUND@; otherwise
     275it return @_URC_CONTINUE_UNWIND@.
     276
     277\item
     278@_UA_CLEANUP_PHASE@ specifies a cleanup phase, where the entire frame is
     279unwound and all cleanup code is run. The personality function does whatever
     280cleanup the language defines (such as running destructors/finalizers) and then
     281generally returns @_URC_CONTINUE_UNWIND@.
     282
     283\item
     284\begin{sloppypar}
     285@_UA_HANDLER_FRAME@ specifies a cleanup phase on a function frame that found a
     286handler. The personality function must prepare to return to normal code
     287execution and return @_URC_INSTALL_CONTEXT@.
     288\end{sloppypar}
     289
     290\item
     291@_UA_FORCE_UNWIND@ specifies a forced unwind call. Forced unwind only performs
     292the cleanup phase and uses a different means to decide when to stop
     293\see{\VRef{s:ForcedUnwind}}.
     294\end{enumerate}
     295
     296The @exception_class@ argument is a copy of the
     297\lstinline[language=C]|exception|'s @exception_class@ field.
     298
     299The \lstinline[language=C]|exception| argument is a pointer to the user
     300provided storage object. It has two public fields, the exception class, which
     301is actually just a number, identifying the exception handling mechanism that
     302created it, and the cleanup function. The cleanup function is called if
     303required by the exception.
     304
     305The @context@ argument is a pointer to an opaque type passed to helper
     306functions called inside the personality function.
     307
     308The return value, @_Unwind_Reason_Code@, is an enumeration of possible messages
    164309that can be passed several places in libunwind. It includes a number of
    165310messages for special cases (some of which should never be used by the
    166311personality function) and error codes but unless otherwise noted the
    167 personality function should always return \codeC{_URC_CONTINUE_UNWIND}.
    168 
    169 The \codeC{version} argument is the verson of the implementation that is
    170 calling the personality function. At this point it appears to always be 1 and
    171 it will likely stay that way until a new version of the API is updated.
    172 
    173 The \codeC{action} argument is set of flags that tell the personality
    174 function when it is being called and what it must do on this invocation.
    175 The flags are as follows:
    176 \begin{itemize}
    177 \item\codeC{_UA_SEARCH_PHASE}: This flag is set whenever the personality
    178 function is called during the search phase. The personality function should
    179 decide if unwinding will stop in this function or not. If it does then the
    180 personality function should return \codeC{_URC_HANDLER_FOUND}.
    181 \item\codeC{_UA_CLEANUP_PHASE}: This flag is set whenever the personality
    182 function is called during the cleanup phase. If no other flags are set this
    183 means the entire frame will be unwound and all cleanup code should be run.
    184 \item\codeC{_UA_HANDLER_FRAME}: This flag is set during the cleanup phase
    185 on the function frame that found the handler. The personality function must
    186 prepare to return to normal code execution and return
    187 \codeC{_URC_INSTALL_CONTEXT}.
    188 \item\codeC{_UA_FORCE_UNWIND}: This flag is set if the personality function
    189 is called through a forced unwind call. Forced unwind only performs the
    190 cleanup phase and uses a different means to decide when to stop. See its
    191 section below.
    192 \end{itemize}
    193 
    194 The \codeC{exception_class} argument is a copy of the \codeC{exception}'s
    195 \codeC{exception_class} field.
    196 
    197 The \codeC{exception} argument is a pointer to the user provided storage
    198 object. It has two public fields, the exception class which is actually just
    199 a number that identifies the exception handling mechanism that created it and
    200 the other is the clean-up function. The clean-up function is called if the
    201 exception needs to
    202 
    203 The \codeC{context} argument is a pointer to an opaque type. This is passed
    204 to the many helper functions that can be called inside the personality
    205 function.
     312personality function should always return @_URC_CONTINUE_UNWIND@.
    206313
    207314\subsection{Raise Exception}
    208 This could be considered the central function of libunwind. It preforms the
    209 two staged unwinding the library is built around and most of the rest of the
    210 interface of libunwind is here to support it. It's signature is as follows:
    211 
    212 \begin{lstlisting}
     315Raising an exception is the central function of libunwind and it performs a
     316two-staged unwinding.
     317\begin{cfa}
    213318_Unwind_Reason_Code _Unwind_RaiseException(_Unwind_Exception *);
     319\end{cfa}
     320First, the function begins the search phase, calling the personality function
     321of the most recent stack frame. It continues to call personality functions
     322traversing the stack from newest to oldest until a function finds a handler or
     323the end of the stack is reached. In the latter case, raise exception returns
     324@_URC_END_OF_STACK@.
     325
     326Second, when a handler is matched, raise exception continues onto the cleanup
     327phase.
     328Once again, it calls the personality functions of each stack frame from newest
     329to oldest. This pass stops at the stack frame containing the matching handler.
     330If that personality function has not install a handler, it is an error.
     331
     332If an error is encountered, raise exception returns either
     333@_URC_FATAL_PHASE1_ERROR@ or @_URC_FATAL_PHASE2_ERROR@ depending on when the
     334error occurred.
     335
     336\subsection{Forced Unwind}
     337\label{s:ForcedUnwind}
     338Forced Unwind is the other central function in libunwind.
     339\begin{cfa}
     340_Unwind_Reason_Code _Unwind_ForcedUnwind( _Unwind_Exception *,
     341        _Unwind_Stop_Fn, void *);
     342\end{cfa}
     343It also unwinds the stack but it does not use the search phase. Instead another
     344function, the stop function, is used to stop searching. The exception is the
     345same as the one passed to raise exception. The extra arguments are the stop
     346function and the stop parameter. The stop function has a similar interface as a
     347personality function, except it is also passed the stop parameter.
     348\begin{lstlisting}[language=C,{moredelim=**[is][\color{red}]{@}{@}}]
     349typedef _Unwind_Reason_Code (*@_Unwind_Stop_Fn@)(
     350        _Unwind_Action @action@,
     351        _Unwind_Exception_Class @exception_class@,
     352        _Unwind_Exception * @exception@,
     353        struct _Unwind_Context * @context@,
     354        void * @stop_parameter@);
    214355\end{lstlisting}
    215356
    216 When called the function begins the search phase, calling the personality
    217 function of the most recent stack frame. It will continue to call personality
    218 functions traversing the stack new-to-old until a function finds a handler or
    219 the end of the stack is reached. In the latter case raise exception will
    220 return with \codeC{_URC_END_OF_STACK}.
    221 
    222 Once a handler has been found raise exception continues onto the the cleanup
    223 phase. Once again it will call the personality functins of each stack frame
    224 from newest to oldest. This pass will stop at the stack frame that found the
    225 handler last time, if that personality function does not install the handler
    226 it is an error.
    227 
    228 If an error is encountered raise exception will return either
    229 \codeC{_URC_FATAL_PHASE1_ERROR} or \codeC{_URC_FATAL_PHASE2_ERROR} depending
    230 on when the error occured.
    231 
    232 \subsection{Forced Unwind}
    233 This is the second big function in libunwind. It also unwinds a stack but it
    234 does not use the search phase. Instead another function, the stop function,
    235 is used to decide when to stop.
    236 
    237 \begin{lstlisting}
    238 _Unwind_Reason_Code _Unwind_ForcedUnwind(
    239     _Unwind_Exception *, _Unwind_Stop_Fn, void *);
    240 \end{lstlisting}
    241 
    242 The exception is the same as the one passed to raise exception. The extra
    243 arguments are the stop function and the stop parameter. The stop function has
    244 a similar interface as a personality function, except it is also passed the
    245 stop parameter.
    246 
    247 \begin{lstlisting}
    248 typedef _Unwind_Reason_Code (*_Unwind_Stop_Fn)(
    249     int version,
    250     _Unwind_Action action,
    251     _Unwind_Exception_Class exception_class,
    252     _Unwind_Exception * exception,
    253     struct _Unwind_Context * context,
    254     void * stop_parameter);
    255 \end{lstlisting}
    256 
    257357The stop function is called at every stack frame before the personality
    258 function is called and then once more once after all frames of the stack have
    259 been unwound.
    260 
    261 Each time it is called the stop function should return \codeC{_URC_NO_REASON}
    262 or transfer control directly to other code outside of libunwind. The
    263 framework does not provide any assistance here.
    264 
    265 Its arguments are the same as the paired personality function.
    266 The actions \codeC{_UA_CLEANUP_PHASE} and \codeC{_UA_FORCE_UNWIND} are always
    267 set when it is called. By the official standard that is all but both GCC and
    268 Clang add an extra action on the last call at the end of the stack:
    269 \codeC{_UA_END_OF_STACK}.
     358function is called and then once more after all frames of the stack are
     359unwound.
     360
     361Each time it is called, the stop function should return @_URC_NO_REASON@ or
     362transfer control directly to other code outside of libunwind. The framework
     363does not provide any assistance here.
     364
     365\begin{sloppypar}
     366Its arguments are the same as the paired personality function. The actions
     367@_UA_CLEANUP_PHASE@ and @_UA_FORCE_UNWIND@ are always set when it is
     368called. Beyond the libunwind standard, both GCC and Clang add an extra action
     369on the last call at the end of the stack: @_UA_END_OF_STACK@.
     370\end{sloppypar}
    270371
    271372\section{Exception Context}
    272373% Should I have another independent section?
    273374% There are only two things in it, top_resume and current_exception. How it is
    274 % stored changes depending on wheither or not the thread-library is linked.
    275 
    276 The exception context is a piece of global storage used to maintain data
    277 across different exception operations and to communicate between different
    278 components.
    279 
    280 Each stack has its own exception context. In a purely sequental program, using
    281 only core Cforall, there is only one stack and the context is global. However
    282 if the library \texttt{libcfathread} is linked then there can be multiple
    283 stacks so they will each need their own.
    284 
    285 To handle this code always gets the exception context from the function
    286 \codeC{this_exception_context}. The main exception handling code is in
    287 \texttt{libcfa} and that library also defines the function as a weak symbol
    288 so it acts as a default. Meanwhile in \texttt{libcfathread} the function is
    289 defined as a strong symbol that replaces it when the libraries are linked
    290 together.
    291 
    292 The version of the function defined in \texttt{libcfa} is very simple. It
    293 returns a pointer to a global static variable. With only one stack this
    294 global instance is associated with the only stack.
    295 
    296 The version of the function defined in \texttt{libcfathread} has to handle
    297 more as there are multiple stacks. The exception context is included as
    298 part of the per-stack data stored as part of coroutines. In the cold data
    299 section, stored at the base of each stack, is the exception context for that
    300 stack. The \codeC{this_exception_context} uses the concurrency library to get
    301 the current coroutine and through it the cold data section and the exception
    302 context.
     375% stored changes depending on whether or not the thread-library is linked.
     376
     377The exception context is global storage used to maintain data across different
     378exception operations and to communicate among different components.
     379
     380Each stack must have its own exception context. In a sequential \CFA program,
     381there is only one stack with a single global exception-context. However, when
     382the library @libcfathread@ is linked, there are multiple stacks where each
     383needs its own exception context.
     384
     385General access to the exception context is provided by function
     386@this_exception_context@. For sequential execution, this function is defined as
     387a weak symbol in the \CFA system-library, @libcfa@. When a \CFA program is
     388concurrent, it links with @libcfathread@, where this function is defined with a
     389strong symbol replacing the sequential version.
     390
     391The sequential @this_exception_context@ returns a hard-coded pointer to the
     392global execption context.
     393The concurrent version adds the exception context to the data stored at the
     394base of each stack. When @this_exception_context@ is called it retrieves the
     395active stack and returns the address of the context saved there.
    303396
    304397\section{Termination}
     
    306399% catches. Talk about GCC nested functions.
    307400
    308 Termination exceptions use libunwind quite heavily because it matches the
    309 intended use from \CPP exceptions very closely. The main complication is that
    310 since the \CFA compiler works by translating to C code it cannot generate the
    311 assembly to form the LSDA for try blocks or destructors.
     401Termination exceptions use libunwind heavily because it matches the intended
     402use from \CC exceptions closely. The main complication for \CFA is that the
     403compiler generates C code, making it very difficult to generate the assembly to
     404form the LSDA for try blocks or destructors.
    312405
    313406\subsection{Memory Management}
    314 The first step of termination is to copy the exception into memory managed by
    315 the exception system. Currently the system just uses malloc, without reserved
    316 memory or and ``small allocation" optimizations. The exception handling
    317 mechanism manages memory for the exception as well as memory for libunwind
    318 and the system's own per-exception storage.
    319 
    320 Exceptions are stored in variable sized block. The first component is a fixed
    321 sized data structure that contains the information for libunwind and the
    322 exception system. The second component is a blob of memory that is big enough
    323 to store the exception. Macros with pointer arthritic and type cast are
    324 used to move between the components or go from the embedded
    325 \codeC{_Unwind_Exception} to the entire node.
    326 
    327 All of these nodes are strung together in a linked list. One linked list per
    328 stack, with the head stored in the exception context. Within each linked list
    329 the most recently thrown exception is at the head and the older exceptions
    330 are further down the list. This list format allows exceptions to be thrown
    331 while a different exception is being handled. Only the exception at the head
    332 of the list is currently being handled, the other will wait for the
    333 exceptions before them to be removed.
    334 
    335 The virtual members in the exception's virtual table. The size of the
    336 exception, the copy function and the free function are all in the virtual
    337 table so they are decided per-exception type. The size and copy function are
    338 used right away when the exception is copied in to managed memory. After the
    339 exception is handled the free function is used to clean up the exception and
    340 then the entire node is passed to free.
    341 
    342 \subsection{Try Statements \& Catch Clauses}
    343 The try statements with termination handlers have a pretty complex conversion
    344 to compensate for the lack of assembly generation. Libunwind requires an LSDA
    345 (Language Specific Data Area) and personality function for a function to
    346 unwind across it. The LSDA in particular is hard to generate at the level of
    347 C which is what the \CFA compiler outputs so a work-around is used.
    348 
    349 This work around is a function called \codeC{__cfaehm_try_terminate} in the
    350 standard library. The contents of a try block and the termination handlers
    351 are converted into functions. These are then passed to the try terminate
    352 function and it calls them. This puts the try statements in their own
    353 functions so that no function has to deal with both termination handlers and
    354 destructors.
    355 
    356 This function has some custom embedded assembly that defines its personality
    357 function and LSDA. This is hand coded in C which is why there is only one
    358 version of it, the compiler has no capability to generate it. The personality
    359 function is structured so that it may be expanded, but really it only handles
    360 this one function. Notably it does not handle any destructors so the function
    361 is constructed so that it does need to run it.
     407The first step of a termination raise is to copy the exception into memory
     408managed by the exception system. Currently, the system uses @malloc@, rather
     409than reserved memory or the stack top. The exception handling mechanism manages
     410memory for the exception as well as memory for libunwind and the system's own
     411per-exception storage.
     412
     413[Quick ASCII diagram to get started.]
     414\begin{verbatim}
     415Fixed Header  | _Unwind_Exception   <- pointer target
     416              |
     417              | Cforall storage
     418              |
     419Variable Body | the exception       <- fixed offset
     420              V ...
     421\end{verbatim}
     422
     423Exceptions are stored in variable-sized blocks.
     424The first component is a fixed sized data structure that contains the
     425information for libunwind and the exception system. The second component is an
     426area of memory big enough to store the exception. Macros with pointer arthritic
     427and type cast are used to move between the components or go from the embedded
     428@_Unwind_Exception@ to the entire node.
     429
     430All of these nodes are linked together in a list, one list per stack, with the
     431list head stored in the exception context. Within each linked list, the most
     432recently thrown exception is at the head followed by older thrown
     433exceptions. This format allows exceptions to be thrown, while a different
     434exception is being handled. The exception at the head of the list is currently
     435being handled, while other exceptions wait for the exceptions before them to be
     436removed.
     437
     438The virtual members in the exception's virtual table provide the size of the
     439exception, the copy function, and the free function, so they are specific to an
     440exception type. The size and copy function are used immediately to copy an
     441exception into managed memory. After the exception is handled the free function
     442is used to clean up the exception and then the entire node is passed to free
     443so the memory can be given back to the heap.
     444
     445\subsection{Try Statements and Catch Clauses}
     446The try statement with termination handlers is complex because it must
     447compensate for the lack of assembly-code generated from \CFA. Libunwind
     448requires an LSDA and personality function for control to unwind across a
     449function. The LSDA in particular is hard to mimic in generated C code.
     450
     451The workaround is a function called @__cfaehm_try_terminate@ in the standard
     452library. The contents of a try block and the termination handlers are converted
     453into functions. These are then passed to the try terminate function and it
     454calls them.
     455Because this function is known and fixed (and not an arbitrary function that
     456happens to contain a try statement) this means the LSDA can be generated ahead
     457of time.
     458
     459Both the LSDA and the personality function are set ahead of time using
     460embedded assembly. This is handcrafted using C @asm@ statements and contains
     461enough information for the single try statement the function repersents.
    362462
    363463The three functions passed to try terminate are:
    364 \begin{itemize}
    365 \item The try function: This function is the try block, all the code inside
    366 the try block is placed inside the try function. It takes no parameters and
    367 has no return value. This function is called during regular execution to run
    368 the try block.
    369 \item The match function: This function decides if this try statement should
    370 handle any given termination exception. It takes a pointer to the exception
    371 and returns 0 if the exception is not handled here. Otherwise the return value
    372 is the id of the handler that should handle the exception. It is called
    373 during the search phase.
    374 It is constructed from the conditional part of each handler. It runs each
    375 check in turn, first checking to see if the object
    376 \item The catch function: This function handles the exception. It takes a
    377 pointer to the exception and the handler's id and returns nothing. It is
    378 called after the clean-up phase.
    379 It is constructed by stitching together the bodies of each handler
    380 \end{itemize}
    381 All three are created with GCC nested functions. GCC nested functions can be
    382 used to create closures, functions that can refer to the state of other
    383 functions on the stack. This allows the functions to refer to the main
    384 function and all the variables in scope.
    385 
    386 These nested functions and all other functions besides
    387 \codeC{__cfaehm_try_terminate} in \CFA use the GCC personality function and
    388 the \texttt{-fexceptions} flag to generate the LSDA. This allows destructors
    389 to be implemented with the cleanup attribute.
     464\begin{description}
     465\item[try function:] This function is the try block, all the code inside the
     466try block is placed inside the try function. It takes no parameters and has no
     467return value. This function is called during regular execution to run the try
     468block.
     469
     470\item[match function:] This function is called during the search phase and
     471decides if a catch clause matches the termination exception. It is constructed
     472from the conditional part of each handler and runs each check, top to bottom,
     473in turn, first checking to see if the exception type matches and then if the
     474condition is true. It takes a pointer to the exception and returns 0 if the
     475exception is not handled here. Otherwise the return value is the id of the
     476handler that matches the exception.
     477
     478\item[handler function:] This function handles the exception. It takes a
     479pointer to the exception and the handler's id and returns nothing. It is called
     480after the cleanup phase. It is constructed by stitching together the bodies of
     481each handler and dispatches to the selected handler.
     482\end{description}
     483All three functions are created with GCC nested functions. GCC nested functions
     484can be used to create closures, functions that can refer to the state of other
     485functions on the stack. This approach allows the functions to refer to all the
     486variables in scope for the function containing the @try@ statement. These
     487nested functions and all other functions besides @__cfaehm_try_terminate@ in
     488\CFA use the GCC personality function and the @-fexceptions@ flag to generate
     489the LSDA. This allows destructors to be implemented with the cleanup attribute.
    390490
    391491\section{Resumption}
    392492% The stack-local data, the linked list of nodes.
    393493
    394 Resumption uses a list of nodes for its stack traversal. The head of the list
    395 is stored in the exception context. The nodes in the list just have a pointer
     494Resumption simple to implement because there is no stack unwinding. The
     495resumption raise uses a list of nodes for its stack traversal. The head of the
     496list is stored in the exception context. The nodes in the list have a pointer
    396497to the next node and a pointer to the handler function.
    397498
    398 The on a resumption throw the this list is traversed. At each node the
    399 handler function is called and is passed the exception by pointer. It returns
    400 true if the exception was handled and false otherwise.
    401 
    402 The handler function does both the matching and catching. It tries each
    403 the condition of \codeCFA{catchResume} in order, top-to-bottom and until it
    404 finds a handler that matches. If no handler matches then the function returns
    405 false. Otherwise the matching handler is run, if it completes successfully
    406 the function returns true. Rethrows, through the \codeCFA{throwResume;}
    407 statement, cause the function to return true.
    408 
    409 \subsection{Libunwind Compatibility}
    410 Resumption does not use libunwind for two simple reasons. The first is that
    411 it does not have to unwind anything so would never need to use the clean-up
    412 phase. Still the search phase could be used to make it free to enter or exit
    413 a try statement with resumption handlers in the same way termination handlers
    414 are for the same trade off in the cost of the throw. This is where the second
    415 reason comes in, there is no way to return from a search without installing
    416 a handler or raising an error.
    417 
    418 Although work arounds could be created none seemed to be worth it for the
    419 prototype. This implementation has no difference in behaviour and is much
    420 simpler.
     499A resumption raise traverses this list. At each node the handler function is
     500called, passing the exception by pointer. It returns true if the exception is
     501handled and false otherwise.
     502
     503The handler function does both the matching and handling. It computes the
     504condition of each @catchResume@ in top-to-bottom order, until it finds a
     505handler that matches. If no handler matches then the function returns
     506false. Otherwise the matching handler is run; if it completes successfully, the
     507function returns true. Rethrowing, through the @throwResume;@ statement,
     508causes the function to return true.
     509
     510% Recursive Resumption Stuff:
     511Search skipping \see{\VPageref{p:searchskip}}, which ignores parts of the stack
     512already examined, is accomplished by updating the front of the list as the
     513search continues. Before the handler at a node is called the head of the list
     514is updated to the next node of the current node. After the search is complete,
     515successful or not, the head of the list is reset.
     516
     517This mechanism means the current handler and every handler that has already
     518been checked are not on the list while a handler is run. If a resumption is
     519thrown during the handling of another resumption the active handlers and all
     520the other handler checked up to this point are not checked again.
     521
     522This structure also supports new handler added while the resumption is being
     523handled. These are added to the front of the list, pointing back along the
     524stack -- the first one points over all the checked handlers -- and the ordering
     525is maintained.
     526
     527\label{p:zero-cost}
     528Note, the resumption implementation has a cost for entering/exiting a @try@
     529statement with @catchResume@ clauses, whereas a @try@ statement with @catch@
     530clauses has zero-cost entry/exit. While resumption does not need the stack
     531unwinding and cleanup provided by libunwind, it could use the search phase to
     532providing zero-cost enter/exit using the LSDA. Unfortunately, there is no way
     533to return from a libunwind search without installing a handler or raising an
     534error. Although workarounds might be possible, they are beyond the scope of
     535this thesis. The current resumption implementation has simplicity in its
     536favour.
    421537% Seriously, just compare the size of the two chapters and then consider
    422538% that unwind is required knowledge for that chapter.
     
    424540\section{Finally}
    425541% Uses destructors and GCC nested functions.
    426 Finally clauses are a simple decomposition to some of the existing features.
    427 The code in the block is placed into a GCC nested function with a unique name,
    428 no arguments or return values. This nested function is then set as the
    429 clean-up function of an empty object that is declared at the beginning of a
    430 block placed around the contexts of the try statement.
     542Finally clauses is placed into a GCC nested-function with a unique name, and no
     543arguments or return values. This nested function is then set as the cleanup
     544function of an empty object that is declared at the beginning of a block placed
     545around the context of the associated @try@ statement.
    431546
    432547The rest is handled by GCC. The try block and all handlers are inside the
    433 block. When they are complete control exits the block and the empty object
    434 is cleaned up, which runs the function that contains the finally code.
     548block. At completion, control exits the block and the empty object is cleaned
     549up, which runs the function that contains the finally code.
    435550
    436551\section{Cancellation}
     
    438553
    439554Cancellation also uses libunwind to do its stack traversal and unwinding,
    440 however it uses a different primary function \codeC{_Unwind_ForcedUnwind}.
    441 Details of its interface can be found in the unwind section.
    442 
    443 The first step of cancellation is to find the stack was cancelled and which
    444 type of stack it is. Luckily the threads library stores the main thread
    445 pointer and the current thread pointer and every thread stores a pointer to
     555however it uses a different primary function @_Unwind_ForcedUnwind@. Details
     556of its interface can be found in the \VRef{s:ForcedUnwind}.
     557
     558The first step of cancellation is to find the cancelled stack and its type:
     559coroutine or thread. Fortunately, the thread library stores the main thread
     560pointer and the current thread pointer, and every thread stores a pointer to
    446561its main coroutine and the coroutine it is currently executing.
    447562
    448 So if the the current thread's main and current coroutine do not match, it is
    449 a coroutine cancellation. Otherwise if the main and current thread do not
    450 match, it is a thread cancellation. Otherwise it is a main thread
    451 cancellation.
    452 
    453 However if the threading library is not linked then execution must be on the
    454 main stack as that is the only one that exists. So the entire check is skipped
    455 using the linker and weak symbols. Instead the main thread cancellation is
    456 unconditionally preformed.
    457 
    458 Regardless of how they are choosen afterwords the stop function and the stop
    459 parameter are passed to the forced unwind functon. The general pattern of all
    460 three stop functions is the same, they continue unwinding until the end of
    461 stack when they do there primary work.
    462 
    463 Main stack cancellation it is very simple. The ``transfer" is just an abort,
    464 the program stops executing.
    465 
    466 The coroutine cancellation stores the exception on the coroutine and then
    467 does a coroutine context switch. The rest is handled inside resume. Every time
    468 control returns from a resumed thread there is a check to see if it is
    469 cancelled. If it is the exception is retrieved and the CoroutineCancelled
    470 exception is constructed and loaded. It is then thrown as a regular exception
    471 with the default handler coming from the context of the resumption call.
    472 
    473 The thread cancellation stores the exception on the thread's main stack and
    474 then returns to the scheduler. The rest is handled by the joiner. The wait
    475 for the joined thread to finish works the same but after that it checks
    476 to see if there was a cancellation. If there was the exception is retrieved
    477 and the ThreadCancelled exception is constructed. The default handler is
    478 passed in as a function pointer. If it is null (as it is for the
    479 auto-generated joins on destructor call) it a default is used that simply
    480 calls abort; which gives the required handling on implicate join.
     563So if the active thread's main and current coroutine are the same. If they
     564are then the current stack is a thread stack, otherwise it is a coroutine
     565stack. If it is a thread stack then an equality check with the stored main
     566thread pointer and current thread pointer is enough to tell if the current
     567thread is the main thread or not.
     568
     569However, if the threading library is not linked, the sequential execution is on
     570the main stack. Hence, the entire check is skipped because the weak-symbol
     571function is loaded. Therefore, a main thread cancellation is unconditionally
     572performed.
     573
     574Regardless of how the stack is chosen, the stop function and parameter are
     575passed to the forced-unwind function. The general pattern of all three stop
     576functions is the same: they continue unwinding until the end of stack when they
     577do there primary work.
     578
     579For main stack cancellation, the transfer is just a program abort.
     580
     581For coroutine cancellation, the exception is stored on the coroutine's stack,
     582and the coroutine context switches to its last resumer. The rest is handled on
     583the backside of the resume, which check if the resumed coroutine is
     584cancelled. If cancelled, the exception is retrieved from the resumed coroutine,
     585and a @CoroutineCancelled@ exception is constructed and loaded with the
     586cancelled exception. It is then resumed as a regular exception with the default
     587handler coming from the context of the resumption call.
     588
     589For thread cancellation, the exception is stored on the thread's main stack and
     590then context switched to the scheduler. The rest is handled by the thread
     591joiner. When the join is complete, the joiner checks if the joined thread is
     592cancelled. If cancelled, the exception is retrieved and the joined thread, and
     593a @ThreadCancelled@ exception is constructed and loaded with the cancelled
     594exception. The default handler is passed in as a function pointer. If it is
     595null (as it is for the auto-generated joins on destructor call), the default is
     596used, which is a program abort.
     597%; which gives the required handling on implicate join.
  • doc/theses/andrew_beach_MMath/thesis-frontpgs.tex

    r342af53 r8e4aa05  
    3636
    3737        A thesis \\
    38         presented to the University of Waterloo \\ 
     38        presented to the University of Waterloo \\
    3939        in fulfillment of the \\
    4040        thesis requirement for the degree of \\
     
    6464\cleardoublepage
    6565
    66  
     66
    6767%----------------------------------------------------------------------
    6868% EXAMINING COMMITTEE (Required for Ph.D. theses only)
     
    7171\begin{center}\textbf{Examining Committee Membership}\end{center}
    7272  \noindent
    73 The following served on the Examining Committee for this thesis. The decision of the Examining Committee is by majority vote.
    74   \bigskip
    75  
    76   \noindent
    77 \begin{tabbing}
    78 Internal-External Member: \=  \kill % using longest text to define tab length
    79 External Examiner: \>  Bruce Bruce \\
     73The following served on the Examining Committee for this thesis. The decision
     74of the Examining Committee is by majority vote.
     75  \bigskip
     76
     77  \noindent
     78\begin{tabbing}
     79Internal-External Member: \=  \kill % using longest text to define tab length
     80External Examiner: \>  Bruce Bruce \\
    8081\> Professor, Dept. of Philosophy of Zoology, University of Wallamaloo \\
    81 \end{tabbing} 
    82   \bigskip
    83  
     82\end{tabbing}
     83  \bigskip
     84
    8485  \noindent
    8586\begin{tabbing}
     
    9192\end{tabbing}
    9293  \bigskip
    93  
     94
    9495  \noindent
    9596  \begin{tabbing}
     
    99100\end{tabbing}
    100101  \bigskip
    101  
     102
    102103  \noindent
    103104\begin{tabbing}
     
    107108\end{tabbing}
    108109  \bigskip
    109  
     110
    110111  \noindent
    111112\begin{tabbing}
     
    123124  % December 13th, 2006.  It is designed for an electronic thesis.
    124125  \noindent
    125 I hereby declare that I am the sole author of this thesis. This is a true copy of the thesis, including any required final revisions, as accepted by my examiners.
    126 
    127   \bigskip
    128  
     126I hereby declare that I am the sole author of this thesis. This is a true copy
     127of the thesis, including any required final revisions, as accepted by my
     128examiners.
     129
     130  \bigskip
     131
    129132  \noindent
    130133I understand that my thesis may be made electronically available to the public.
  • doc/theses/andrew_beach_MMath/thesis.tex

    r342af53 r8e4aa05  
    4545% FRONT MATERIAL
    4646%----------------------------------------------------------------------
    47 \input{thesis-frontpgs} 
     47\input{thesis-frontpgs}
    4848
    4949%----------------------------------------------------------------------
     
    6565A \gls{computer} could compute $\pi$ all day long. In fact, subsets of digits
    6666of $\pi$'s decimal approximation would make a good source for psuedo-random
    67 vectors, \gls{rvec} . 
     67vectors, \gls{rvec} .
    6868
    6969%----------------------------------------------------------------------
     
    9696
    9797\begin{itemize}
    98 \item A well-prepared PDF should be 
     98\item A well-prepared PDF should be
    9999  \begin{enumerate}
    100100    \item Of reasonable size, {\it i.e.} photos cropped and compressed.
    101     \item Scalable, to allow enlargment of text and drawings. 
    102   \end{enumerate} 
     101    \item Scalable, to allow enlargment of text and drawings.
     102  \end{enumerate}
    103103\item Photos must be bit maps, and so are not scaleable by definition. TIFF and
    104104BMP are uncompressed formats, while JPEG is compressed. Most photos can be
    105105compressed without losing their illustrative value.
    106 \item Drawings that you make should be scalable vector graphics, \emph{not} 
     106\item Drawings that you make should be scalable vector graphics, \emph{not}
    107107bit maps. Some scalable vector file formats are: EPS, SVG, PNG, WMF. These can
    108 all be converted into PNG or PDF, that pdflatex recognizes. Your drawing 
    109 package probably can export to one of these formats directly. Otherwise, a 
    110 common procedure is to print-to-file through a Postscript printer driver to 
    111 create a PS file, then convert that to EPS (encapsulated PS, which has a 
    112 bounding box to describe its exact size rather than a whole page). 
     108all be converted into PNG or PDF, that pdflatex recognizes. Your drawing
     109package probably can export to one of these formats directly. Otherwise, a
     110common procedure is to print-to-file through a Postscript printer driver to
     111create a PS file, then convert that to EPS (encapsulated PS, which has a
     112bounding box to describe its exact size rather than a whole page).
    113113Programs such as GSView (a Ghostscript GUI) can create both EPS and PDF from
    114114PS files. Appendix~\ref{AppendixA} shows how to generate properly sized Matlab
    115115plots and save them as PDF.
    116116\item It's important to crop your photos and draw your figures to the size that
    117 you want to appear in your thesis. Scaling photos with the 
    118 includegraphics command will cause loss of resolution. And scaling down 
     117you want to appear in your thesis. Scaling photos with the
     118includegraphics command will cause loss of resolution. And scaling down
    119119drawings may cause any text annotations to become too small.
    120120\end{itemize}
    121  
     121
    122122For more information on \LaTeX\, see the uWaterloo Skills for the
    123 Academic Workplace \href{https://uwaterloo.ca/information-systems-technology/services/electronic-thesis-preparation-and-submission-support/ethesis-guide/creating-pdf-version-your-thesis/creating-pdf-files-using-latex/latex-ethesis-and-large-documents}{course notes}. 
     123Academic Workplace \href{https://uwaterloo.ca/information-systems-technology/services/electronic-thesis-preparation-and-submission-support/ethesis-guide/creating-pdf-version-your-thesis/creating-pdf-files-using-latex/latex-ethesis-and-large-documents}{course notes}.
    124124\footnote{
    125125Note that while it is possible to include hyperlinks to external documents,
    126 it is not wise to do so, since anything you can't control may change over time. 
    127 It \emph{would} be appropriate and necessary to provide external links to 
    128 additional resources for a multimedia ``enhanced'' thesis. 
    129 But also note that if the \package{hyperref} package is not included, 
    130 as for the print-optimized option in this thesis template, any \cmmd{href} 
     126it is not wise to do so, since anything you can't control may change over time.
     127It \emph{would} be appropriate and necessary to provide external links to
     128additional resources for a multimedia ``enhanced'' thesis.
     129But also note that if the \package{hyperref} package is not included,
     130as for the print-optimized option in this thesis template, any \cmmd{href}
    131131commands in your logical document are no longer defined.
    132132A work-around employed by this thesis template is to define a dummy
    133 \cmmd{href} command (which does nothing) in the preamble of the document, 
    134 before the \package{hyperref} package is included. 
     133\cmmd{href} command (which does nothing) in the preamble of the document,
     134before the \package{hyperref} package is included.
    135135The dummy definition is then redifined by the
    136136\package{hyperref} package when it is included.
     
    138138
    139139The classic book by Leslie Lamport \cite{lamport.book}, author of \LaTeX , is
    140 worth a look too, and the many available add-on packages are described by 
     140worth a look too, and the many available add-on packages are described by
    141141Goossens \textit{et al} \cite{goossens.book}.
    142142
     
    180180Export Setup button in the figure Property Editor.
    181181
    182 \section{From the Command Line} 
     182\section{From the Command Line}
    183183All figure properties can also be manipulated from the command line. Here's an
    184 example: 
     184example:
    185185\begin{verbatim}
    186186x=[0:0.1:pi];
  • doc/theses/andrew_beach_MMath/unwinding.tex

    r342af53 r8e4aa05  
    11\chapter{Unwinding in \CFA}
    22
    3 Stack unwinding is the process of removing things from the stack. Within
    4 functions and on function return this is handled directly by the code in the
    5 function itself as it knows exactly what is on the stack just from the
    6 current location in the function. Unwinding across stack frames means that it
    7 is no longer knows exactly what is on the stack or even how much of the stack
    8 needs to be removed.
     3Stack unwinding is the process of removing stack frames (activations) from the
     4stack. On function entry and return, unwinding is handled directly by the code
     5embedded in the function. Usually, the stack-frame size is known statically
     6based on parameters and local variable declarations.  For dynamically-sized
     7local variables, a runtime computation is necessary to know the frame
     8size. Finally, a function's frame-size may change during execution as local
     9variables (static or dynamic sized) go in and out of scope.
     10Allocating/deallocating stack space is usually an $O(1)$ operation achieved by
     11bumping the hardware stack-pointer up or down as needed.
    912
    10 Even this is fairly simple if nothing needs to happen when the stack unwinds.
    11 Traditional C can unwind the stack by saving and restoring state (with
    12 \codeC{setjmp} \& \codeC{longjmp}). However many languages define actions that
    13 have to be taken when something is removed from the stack, such as running
    14 a variable's destructor or a \codeCFA{try} statement's \codeCFA{finally}
    15 clause. Handling this requires walking the stack going through each stack
    16 frame.
     13Unwinding across multiple stack frames is more complex because individual stack
     14management code associated with each frame is bypassed. That is, the location
     15of a function's frame code is largely unknown and dispersed throughout the
     16function, hence the current stack-frame size managed by that code is also
     17unknown. Hence, code unwinding across frames does not have direct knowledge
     18about what is on the stack, and hence, how much of the stack needs to be
     19removed.
    1720
    18 For exceptions, this means everything from the point the exception is raised
    19 to the point it is caught, while checking each frame for handlers during the
    20 stack walk to find out where it should be caught. This is where the most of
    21 the expense and complexity of exception handling comes from.
     21The traditional unwinding mechanism for C is implemented by saving a snap-shot
     22of a function's state with @setjmp@ and restoring that snap-shot with
     23@longjmp@. This approach bypasses the need to know stack details by simply
     24reseting to a snap-shot of an arbitrary but existing function frame on the
     25stack. It is up to the programmer to ensure the snap-shot is valid when it is
     26reset, making the code fragile with potential errors that are difficult to
     27debug because the stack becomes corrupted.
    2228
    23 To do all of this we use libunwind, a low level library that provides tools
    24 for stack walking and stack unwinding. What follows is an overview of all the
    25 relivant features of libunwind and then how \CFA uses them to implement its
    26 exception handling.
     29However, many languages define cleanup actions that have to be taken when
     30something is deallocated from the stack or blocks end, such as running a
     31variable's destructor or a @try@ statement's @finally@ clause. Handling these
     32mechanisms requires walking the stack and checking each stack frame for these
     33potential actions.
     34
     35For exceptions, it must be possible to walk the stack frames in search of try
     36statements with handlers to perform exception matching. For termination
     37exceptions, it must be possible to unwind all stack frames from the throw to
     38the matching catch, and each of these frames must be checked for cleanup
     39actions. Stack walking is where the most of the complexity and expense of
     40exception handling comes from.
     41
     42One of the most popular tools for stack management is libunwind, a low level
     43library that provides tools for stack walking and unwinding. What follows is an
     44overview of all the relevant features of libunwind and how \CFA uses them to
     45implement its exception handling.
    2746
    2847\section{libunwind Usage}
    29 
    30 \CFA uses two primary functions in libunwind to create most of its
    31 exceptional control-flow: \codeC{_Unwind_RaiseException} and
    32 \codeC{_Unwind_ForcedUnwind}.
    33 Their operation is divided into two phases: search and clean-up. The search
    34 phase -- phase 1 -- is used to scan the stack but not unwinding it. The
    35 clean-up phase -- phase 2 -- is used for unwinding.
     48\CFA uses two primary functions in libunwind to create most of its exceptional
     49control-flow: @_Unwind_RaiseException@ and @_Unwind_ForcedUnwind@.  Their
     50operation is divided into two phases: search and clean-up. The search phase --
     51phase 1 -- is used to scan the stack but not unwinding it. The clean-up phase
     52-- phase 2 -- is used for unwinding.
    3653
    3754The raise-exception function uses both phases. It starts by searching for a
     
    4461A personality function performs three tasks, although not all have to be
    4562present. The tasks performed are decided by the actions provided.
    46 \codeC{_Unwind_Action} is a bitmask of possible actions and an argument of
    47 this type is passed into the personality function.
     63@_Unwind_Action@ is a bitmask of possible actions and an argument of this type
     64is passed into the personality function.
    4865\begin{itemize}
    49 \item\codeC{_UA_SEARCH_PHASE} is passed in search phase and tells the
    50 personality function to check for handlers. If there is a handler in this
    51 stack frame, as defined by the language, the personality function should
    52 return \codeC{_URC_HANDLER_FOUND}. Otherwise it should return
    53 \codeC{_URC_CONTINUE_UNWIND}.
    54 \item\codeC{_UA_CLEANUP_PHASE} is passed in during the clean-up phase and
    55 means part or all of the stack frame is removed. The personality function
    56 should do whatever clean-up the language defines
    57 (such as running destructors/finalizers) and then generally returns
    58 \codeC{_URC_CONTINUE_UNWIND}.
    59 \item\codeC{_UA_HANDLER_FRAME} means the personality function must install
    60 a handler. It is also passed in during the clean-up phase and is in addition
    61 to the clean-up action. libunwind provides several helpers for the personality
    62 function here. Once it is done, the personality function must return
    63 \codeC{_URC_INSTALL_CONTEXT}.
     66\item
     67\begin{sloppypar}
     68@_UA_SEARCH_PHASE@ is passed in for the search phase and tells the personality
     69function to check for handlers. If there is a handler in a stack frame, as
     70defined by the language, the personality function returns @_URC_HANDLER_FOUND@;
     71otherwise it return @_URC_CONTINUE_UNWIND@.
     72\end{sloppypar}
     73\item
     74@_UA_CLEANUP_PHASE@ is passed in during the clean-up phase and means part or
     75all of the stack frame is removed. The personality function does whatever
     76clean-up the language defines (such as running destructors/finalizers) and then
     77generally returns @_URC_CONTINUE_UNWIND@.
     78\item
     79@_UA_HANDLER_FRAME@ means the personality function must install a handler. It
     80is also passed in during the clean-up phase and is in addition to the clean-up
     81action. libunwind provides several helpers for the personality function. Once
     82it is done, the personality function returns @_URC_INSTALL_CONTEXT@.
    6483\end{itemize}
    65 The personality function is given a number of other arguments. Some are for
    66 compatability and there is the \codeC{struct _Unwind_Context} pointer which
    67 passed to many helpers to get information about the current stack frame.
     84The personality function is given a number of other arguments. Some arguments
     85are for compatibility, and there is the @struct _Unwind_Context@ pointer which
     86is passed to many helpers to get information about the current stack frame.
    6887
    69 Forced-unwind only performs the clean-up phase. It takes three arguments:
    70 a pointer to the exception, a pointer to the stop function and a pointer to
    71 the stop parameter. It does most of the same things as phase two of
    72 raise-exception but with some extras.
    73 The first it passes in an extra action to the personality function on each
    74 stack frame, \codeC{_UA_FORCE_UNWIND}, which means a handler cannot be
     88For cancellation, forced-unwind only performs the clean-up phase. It takes
     89three arguments: a pointer to the exception, a pointer to the stop function and
     90a pointer to the stop parameter. It does most of the same actions as phase two
     91of raise-exception but passes in an extra action to the personality function on
     92each stack frame, @_UA_FORCE_UNWIND@, which means a handler cannot be
    7593installed.
    7694
    77 The big change is that forced-unwind calls the stop function. Each time it
    78 steps into a frame, before calling the personality function, it calls the
    79 stop function. The stop function receives all the same arguments as the
    80 personality function will and the stop parameter supplied to forced-unwind.
     95As well, forced-unwind calls the stop function each time it steps into a frame,
     96before calling the personality function. The stop function receives all the
     97same arguments as the personality function and the stop parameter supplied to
     98forced-unwind.
    8199
    82100The stop function is called one more time at the end of the stack after all
    83 stack frames have been removed. By the standard API this is marked by setting
     101stack frames have been removed. The standard API marks this frame by setting
    84102the stack pointer inside the context passed to the stop function. However both
    85 GCC and Clang add an extra action for this case \codeC{_UA_END_OF_STACK}.
     103GCC and Clang add an extra action for this case @_UA_END_OF_STACK@.
    86104
    87 Each time function the stop function is called it can do one or two things.
    88 When it is not the end of the stack it can return \codeC{_URC_NO_REASON} to
    89 continue unwinding.
     105Each time the stop function is called, it can do one or two things.  When it is
     106not the end of the stack it can return @_URC_NO_REASON@ to continue unwinding.
    90107% Is there a reason that NO_REASON is used instead of CONTINUE_UNWIND?
    91 Its only other option is to use its own means to transfer control elsewhere
    92 and never return to its caller. It may always do this and no additional tools
    93 are provided to do it.
     108The other option is to use some other means to transfer control elsewhere and
     109never return to its caller. libunwind provides no additional tools for
     110alternate transfers of control.
    94111
    95112\section{\CFA Implementation}
    96113
    97 To use libunwind, \CFA provides several wrappers, its own storage,
    98 personality functions, and a stop function.
     114To use libunwind, \CFA provides several wrappers, its own storage, personality
     115functions, and a stop function.
    99116
    100117The wrappers perform three tasks: set-up, clean-up and controlling the
     
    108125The core control code is called every time a throw -- after set-up -- or
    109126re-throw is run. It uses raise-exception to search for a handler and to run it
    110 if one is found. If no handler is found and raise-exception returns then
     127if one is found. If no handler is found and raise-exception returns, then
    111128forced-unwind is called to run all destructors on the stack before terminating
    112129the process.
    113130
    114 The stop function is very simple. It checks the end of stack flag to see if
    115 it is finished unwinding. If so, it calls \codeC{exit} to end the process,
    116 otherwise it returns with no-reason to continue unwinding.
     131The stop function is simple. It checks for the end of stack flag to see if
     132unwinding is finished. If so, it calls @exit@ to end the process, otherwise it
     133returns with no-reason to continue unwinding.
    117134% Yeah, this is going to have to change.
    118135
    119136The personality routine is more complex because it has to obtain information
    120 about the function by scanning the LSDA (Language Specific Data Area). This
     137about the function by scanning the Language Specific Data Area (LSDA). This
    121138step allows a single personality function to be used for multiple functions and
    122 let that personaliity function figure out exactly where in the function
    123 execution was, what is currently in the stack frame and what handlers should
    124 be checked.
     139lets that personality function figure out exactly where in the function
     140execution is, what is currently in the stack frame, and what handlers should be
     141checked.
    125142% Not that we do that yet.
    126143
    127 However, generating the LSDA is difficult. It requires knowledge about the
    128 location of the instruction pointer and stack layout, which varies with
    129 compiler and optimization levels. So for frames where there are only
    130 destructors, GCC's attribute cleanup with the \texttt{-fexception} flag is
    131 sufficient to handle unwinding.
     144It is also necessary to generate the LSDA, which is difficult. It requires
     145knowledge about the location of the instruction pointer and stack layout, which
     146varies with compiler and optimization levels. Fortunately, for frames where
     147there are only destructors, GCC's attribute cleanup with the @-fexception@ flag
     148is sufficient to handle unwinding.
    132149
    133 The only functions that require more than that are those that contain
    134 \codeCFA{try} statements. A \codeCFA{try} statement has a \codeCFA{try}
    135 clause, some number of \codeCFA{catch} clauses and \codeCFA{catchResume}
    136 clauses and may have a \codeCFA{finally} clause. Of these only \codeCFA{try}
    137 statements with \codeCFA{catch} clauses need to be transformed and only they
    138 and the \codeCFA{try} clause are involved.
     150The only functions that require more information are those containing @try@
     151statements. Specifically, only @try@ statements with @catch@ clauses need to be
     152transformed.  The @try@ statement is converted into a series of closures that
     153can access other parts of the function according to scoping rules but can be
     154passed around. The @catch@ clauses are converted into two functions: the match
     155function and the handler function.
    139156
    140 The \codeCFA{try} statement is converted into a series of closures which can
    141 access other parts of the function according to scoping rules but can be
    142 passed around. The \codeCFA{try} clause is converted into the try functions,
    143 almost entirely unchanged. The \codeCFA{catch} clauses are converted into two
    144 functions; the match function and the catch function.
     157Together the match function and the catch function form the code that runs when
     158an exception passes out of the guarded block for a try statement. The match
     159function is used during the search phase: it is passed an exception and checks
     160each handler to see if the raised exception matches the handler exception. It
     161returns an index that represents which handler matched or there is no
     162match. The catch function is used during the clean-up phase, it is passed an
     163exception and the index of a handler. It casts the exception to the exception
     164type declared in that handler and then runs the handler's body.
    145165
    146 Together the match function and the catch function form the code that runs
    147 when an exception passes out of a try block. The match function is used during
    148 the search phase, it is passed an exception and checks each handler to see if
    149 it will handle the exception. It returns an index that repersents which
    150 handler matched or that none of them did. The catch function is used during
    151 the clean-up phase, it is passed an exception and the index of a handler. It
    152 casts the exception to the exception type declared in that handler and then
    153 runs the handler's body.
    154 
    155 These three functions are passed to \codeC{try_terminate}. This is an
     166These three functions are passed to @try_terminate@, which is an
    156167% Maybe I shouldn't quote that, it isn't its actual name.
    157 internal hand-written function that has its own personality function and
    158 custom assembly LSD does the exception handling in \CFA. During normal
    159 execution all this function does is call the try function and then return.
    160 It is only when exceptions are thrown that anything interesting happens.
     168internal hand-written function that has its own personality function and custom
     169assembly LSDA for doing the exception handling in \CFA. During normal
     170execution, this function calls the try function and then return.  It is only
     171when exceptions are thrown that anything interesting happens.
    161172
    162173During the search phase the personality function gets the pointer to the match
    163 function and calls it. If the match function returns a handler index the
     174function and calls it. If the match function returns a handler index, the
    164175personality function saves it and reports that the handler has been found,
    165 otherwise unwinding continues.
    166 During the clean-up phase the personality function only does anything if the
    167 handler was found in this frame. If it was then the personality function
    168 installs the handler, which is setting the instruction pointer in
    169 \codeC{try_terminate} to an otherwise unused section that calls the catch
    170 function, passing it the current exception and handler index.
    171 \codeC{try_terminate} returns as soon as the catch function returns.
     176otherwise unwinding continues.  During the clean-up phase, the personality
     177function only performs an action, when a handler is found in a frame. For each
     178found frame, the personality function installs the handler, which sets the
     179instruction pointer in @try_terminate@ to an otherwise unused section that
     180calls the catch function, passing it the current exception and handler index.
     181@try_terminate@ returns as soon as the catch function returns.  At this point
     182control has returned to normal control flow.
    172183
    173 At this point control has returned to normal control flow.
     184\PAB{Maybe a diagram would be helpful?}
  • doc/theses/fangren_yu_COOP_F20/Report.tex

    r342af53 r8e4aa05  
    1717\usepackage[usenames]{color}
    1818\input{common}                                          % common CFA document macros
    19 \usepackage[dvips,plainpages=false,pdfpagelabels,pdfpagemode=UseNone,colorlinks=true,pagebackref=true,linkcolor=blue,citecolor=blue,urlcolor=blue,pagebackref=true,breaklinks=true]{hyperref}
     19\usepackage[dvips,plainpages=false,pdfpagelabels,pdfpagemode=UseNone,colorlinks=true,linkcolor=blue,citecolor=blue,urlcolor=blue,pagebackref=true,breaklinks=true]{hyperref}
    2020\usepackage{breakurl}
    2121\urlstyle{sf}
     
    7676\renewcommand{\subsectionmark}[1]{\markboth{\thesubsection\quad #1}{\thesubsection\quad #1}}
    7777\pagenumbering{roman}
    78 \linenumbers                                            % comment out to turn off line numbering
     78%\linenumbers                                            % comment out to turn off line numbering
    7979
    8080\maketitle
    8181\pdfbookmark[1]{Contents}{section}
    82 \tableofcontents
    83 
    84 \clearpage
     82
    8583\thispagestyle{plain}
    8684\pagenumbering{arabic}
    8785
    8886\begin{abstract}
     87\CFA is an evolutionary, non-object-oriented extension of the C programming language, featuring a parametric type-system, and is currently under active development. The reference compiler for the \CFA language, @cfa-cc@, has some of its major components dated back to the early 2000s, which are based on inefficient data structures and algorithms. This report introduces improvements targeting the expression resolution algorithm, suggested by a recent prototype experiment on a simplified model, which are implemented in @cfa-cc@ to support the full \CFA language. These optimizations speed up the compiler by a factor of 20 across the existing \CFA codebase, bringing the compilation time of a mid-sized \CFA source file down to the 10-second level. A few problem cases derived from realistic code examples are analyzed in detail, with proposed solutions. This work is a critical step in the \CFA project development to achieve its eventual goal of being used alongside C for large software systems.
    8988\end{abstract}
    9089
     90\clearpage
     91\section*{Acknowledgements}
     92\begin{sloppypar}
     93I would like to thank everyone in the \CFA team for their contribution towards this project. Programming language design and development is a tough subject and requires a lot of teamwork. Without the collaborative efforts from the team, this project could not have been a success. Specifically, I would like to thank Andrew Beach for introducing me to the \CFA codebase, Thierry Delisle for maintaining the test and build automation framework, Michael Brooks for providing example programs of various experimental language and type system features, and most importantly, Professor Martin Karsten for recommending me to the \CFA team, and my supervisor, Professor Peter Buhr for encouraging me to explore deeply into intricate compiler algorithms. Finally, I gratefully acknowledge the help from Aaron Moss, former graduate from the team and the author of the precedent thesis work, to participate in the \CFA team's virtual conferences and email correspondence, and provide many critical arguments and suggestions. 2020 had been an unusually challenging year for everyone and we managed to keep a steady pace.
     94\end{sloppypar}
     95
     96\clearpage
     97\tableofcontents
     98
     99\clearpage
    91100\section{Introduction}
    92101
    93 \section{Completed work}
     102\CFA language, developed by the Programming Language Group at the University of Waterloo, has a long history, with the initial language design in 1992 by Glen Ditchfield~\cite{Ditchfield92} and the first proof-of-concept compiler built in 2003 by Richard Bilson~\cite{Bilson03}. Many new features have been added to the language over time, but the core of \CFA's type-system --- parametric functions introduced by the @forall@ clause (hence the name of the language) providing parametric overloading --- remains mostly unchanged.
     103
     104The current \CFA reference compiler, @cfa-cc@, is designed using the visitor pattern~\cite{vistorpattern} over an abstract syntax tree (AST), where multiple passes over the AST modify it for subsequent passes. @cfa-cc@ still includes many parts taken directly from the original Bilson implementation, which served as the starting point for this enhancement work to the type system. Unfortunately, the prior implementation did not provide the efficiency required for the language to be practical: a \CFA source file of approximately 1000 lines of code can take multiple minutes to compile. The cause of the problem is that the old compiler used inefficient data structures and algorithms for expression resolution, which involved significant copying and redundant work.
     105
     106This report presents a series of optimizations to the performance-critical parts of the resolver, with a major rework of the compiler data-structures using a functional-programming approach to reduce memory complexity. The improvements were suggested by running the compiler builds with a performance profiler against the \CFA standard-library source-code and a test suite to find the most underperforming components in the compiler algorithm.
     107
     108The \CFA team endorses a pragmatic philosophy that focuses on practical implications of language design and implementation rather than theoretical limits. In particular, the compiler is designed to be expressive with respect to code reuse while maintaining type safety, but compromise theoretical soundness in extreme corner cases. However, when these corner cases do appear in actual usage, they need to be thoroughly investigated. A case-by-case analysis is presented for several of these corner cases, some of which point to certain weaknesses in the language design with solutions proposed based on experimental results.
     109
     110\section{AST restructuring}
    94111
    95112\subsection{Memory model with sharing}
    96113
    97 A major rework of the abstract syntax tree (AST) data structure in the compiler is completed as the first step of the project. The majority of work were documented in the reference manual of the compiler~\cite{cfa-cc}. To summarize:
    98 \begin{itemize}
    99 \item
    100 AST nodes (and therefore subtrees) can be shared without copying when reused.
    101 \item
    102 Modifications apply the functional programming principle, making copies for local changes without affecting the original data shared by other owners. In-place mutations are permitted as a special case when sharing does not happen. The logic is implemented by reference counting.
    103 \item
    104 Memory allocation and freeing are performed automatically using smart pointers.
    105 \end{itemize}
    106 The resolver algorithm designed for overload resolution naturally introduces a significant amount of reused intermediate representations, especially in the following two places:
    107 \begin{itemize}
    108 \item
    109 Function overload candidates are computed by combining the argument candidates bottom-up, with many of them being a common term. For example, if $n$ overloads of a function @f@ all take an integer for the first parameter but different types for the second (@f( int, int )@, @f( int, double )@, etc.) the first term is reused $n$ times for each of the generated candidate expressions. This effect is particularly bad for deep expression trees.
    110 \item
    111 In the unification algorithm and candidate elimination step, actual types are obtained by substituting the type parameters by their bindings. Let $n$ be the complexity (\ie number of nodes in representation) of the original type, $m$ be the complexity of bound type for parameters, and $k$ be the number of occurrences of type parameters in the original type. If everything needs to be deep-copied, the substitution step takes $O(n+mk)$ time and memory, while using shared nodes it is reduced to $O(n)$ time and $O(k)$ memory.
    112 \end{itemize}
    113 One of the worst examples for the old compiler is a long chain of I/O operations
    114 \begin{cfa}
    115 sout | 1 | 2 | 3 | 4 | ...
    116 \end{cfa}
    117 The pipe operator is overloaded by \CFA I/O library for every primitive type in C language, as well as I/O manipulators defined by the library. In total there are around 50 overloads for the output stream operation. On resolving the $n$-th pipe operator in the sequence, the first term, which is the result of sub-expression containing $n-1$ pipe operators, is reused to resolve every overload. Therefore at least $O(n^2)$ copies of expression nodes are made during resolution, not even counting type unification cost; combined with two large factors from number of overloads of pipe operators, and that the ``output stream type'' in \CFA is a trait with 27 assertions (which adds to complexity of the pipe operator's type) this makes compiling a long output sequence extremely slow. In new AST representation only $O(n)$ copies are required and type of pipe operator is not copied at all.
    118 
    119 Reduction in space complexity is especially important, as preliminary profiling result on the old compiler build shows that over half of time spent in expression resolution are on memory allocations.
    120  
     114A major rework of the AST data-structure in the compiler was completed as the first step of the project. The majority of this work is documented in my prior report documenting the compiler reference-manual~\cite{cfa-cc}. To summarize:
     115\begin{itemize}
     116\item
     117AST nodes (and therefore subtrees) can be shared without copying.
     118\item
     119Modifications are performed using functional-programming principles, making copies for local changes without affecting the original data shared by other owners. In-place mutations are permitted as a special case when there is no sharing. The logic is implemented by reference counting.
     120\item
     121Memory allocation and freeing are performed automatically using smart pointers~\cite{smartpointers}.
     122\end{itemize}
     123
     124The resolver algorithm, designed for overload resolution, allows a significant amount of code reused, and hence copying, for the intermediate representations, especially in the following two places:
     125\begin{itemize}
     126\item
     127Function overload candidates are computed by combining the argument candidates bottom-up, with many being a common term. For example, if $n$ overloads of a function @f@ all take an integer for the first parameter but different types for the second, \eg @f( int, int )@, @f( int, double )@, etc., the first term is copied $n$ times for each of the generated candidate expressions. This copying is particularly bad for deep expression trees.
     128\item
     129In the unification algorithm and candidate elimination step, actual types are obtained by substituting the type parameters by their bindings. Let $n$ be the complexity (\ie number of nodes in representation) of the original type, $m$ be the complexity of the bound type for parameters, and $k$ be the number of occurrences of type parameters in the original type. If every substitution needs to be deep-copied, these copy step takes $O(n+mk)$ time and memory, while using shared nodes it is reduced to $O(n)$ time and $O(k)$ memory.
     130\end{itemize}
     131One of the worst examples for the old compiler is a long chain of I/O operations:
     132\begin{cfa}
     133sout | 1 | 2 | 3 | 4 | ...;   // print integer constants
     134\end{cfa}
     135The pipe operator is overloaded by the \CFA I/O library for every primitive type in the C language, as well as I/O manipulators defined by the library. In total, there are around 50 overloads for the output stream operation. On resolving the $n$-th pipe operator in the sequence, the first term, which is the result of sub-expression containing $n-1$ pipe operators, is reused to resolve every overload. Therefore at least $O(n^2)$ copies of expression nodes are made during resolution, not even counting type unification cost; combined with the two large factors from number of overloads of pipe operators, and that the ``output stream type'' in \CFA is a trait with 27 assertions (which adds to complexity of the pipe operator's type) this makes compiling a long output sequence extremely slow. In the new AST representation, only $O(n)$ copies are required and the type of the pipe operator is not copied at all.
     136Reduction in space complexity is especially important, as preliminary profiling results on the old compiler build showed over half of the time spent in expression resolution is on memory allocations.
     137
     138Since the compiler codebase is large and the new memory model mostly benefits expression resolution, some of the old data structures are still kept, and a conversion pass happens before and after the general resolve phase. Rewriting every compiler module will take longer, and whether the new model is correct was unknown when this project started, therefore only the resolver is currently implemented with the new data structure.
     139
    121140
    122141\subsection{Merged resolver calls}
    123142
    124 The pre-resolve phase of compilation, inadequately called ``validate'' in the compiler source code, does more than just simple syntax validation, as it also normalizes input program. Some of them, however, requires type information on expressions and therefore needs to call the resolver before the general resolve phase. There are three notable places where the resolver is invoked:
    125 \begin{itemize}
    126 \item
    127 Attempt to generate default constructor, copy constructor and destructor for user-defined @struct@ types
    128 \item
    129 Resolve @with@ statements (the same as in Python, which introduces fields of a structure directly in scope)
     143The pre-resolve phase of compilation, inappropriately called ``validate'' in the compiler source code, has a number of passes that do more than simple syntax and semantic validation; some passes also normalizes the input program. A few of these passes require type information for expressions, and therefore, need to call the resolver before the general resolve phase. There are three notable places where the resolver is invoked:
     144\begin{itemize}
     145\item
     146Generate default constructor, copy constructor and destructor for user-defined @struct@ types.
     147\item
     148Resolve @with@ statements (the same as in Pascal~\cite{pascal}), which introduces fields of a structure directly into a scope.
    130149\item
    131150Resolve @typeof@ expressions (cf. @decltype@ in \CC); note that this step may depend on symbols introduced by @with@ statements.
    132151\end{itemize}
    133 Since the compiler codebase is large and the new memory model mostly only benefits expression resolution, the old data structure is still kept, and a conversion pass happens before and after resolve phase. Rewriting every compiler module will take a long time, and whether the new model is correct is still unknown when started, therefore only the resolver is implemented with the new data structure.
    134 
    135 Since the constructor calls were one of the most expensive to resolve (reason will be shown in the next section), pre-resolve phase were taking more time after resolver moves to the more efficient new implementation. To better facilitate the new resolver, every step that requires type information are reintegrated as part of resolver.
    136 
    137 A by-product of this work is that the reversed dependence of @with@ statement and @typeof@ can now be handled. Previously, the compiler is unable to handle cases such as
     152
     153Since the constructor calls are one of the most expensive to resolve (reason given in~\VRef{s:SpecialFunctionLookup}), this pre-resolve phase was taking a large amount of time even after the resolver was changed to the more efficient new implementation. The problem is that multiple resolutions repeat a significant amount of work. Therefore, to better facilitate the new resolver, every step that requires type information should be integrated as part of the general resolver phase.
     154
     155A by-product of this work is that reversed dependence between @with@ statement and @typeof@ can now be handled. Previously, the compiler was unable to handle cases such as:
    138156\begin{cfa}
    139157struct S { int x; };
    140158S foo();
    141159typeof( foo() ) s; // type is S
    142 with (s) { 
     160with (s) {
    143161        x; // refers to s.x
    144162}
    145163\end{cfa}
    146 since type of @s@ is still unresolved when handling @with@ expressions. Instead, the new (and correct) approach is to evaluate @typeof@ expressions when the declaration is first seen, and it suffices because of the declaration-before-use rule.
     164since the type of @s@ is unresolved when handling @with@ expressions because the @with@ pass follows the @typeof@ pass (interchanging passes only interchanges the problem). Instead, the new (and correct) approach is to evaluate @typeof@ expressions when the declaration is first seen during resolution, and it suffices because of the declaration-before-use rule.
    147165
    148166
    149167\subsection{Special function lookup}
    150 
    151 Reducing the number of functions looked up for overload resolution is an effective way to gain performance when there are many overloads but most of them are trivially wrong. In practice, most functions have few (if any) overloads but there are notable exceptions. Most importantly, constructor @?{}@, destructor @^?{}@, and assignment @?=?@ are generated for every user-defined type, and in a large source file there can be hundreds of them. Furthermore, many calls to them are generated for initializing variables and passing arguments. This fact makes them the most overloaded and most called functions.
    152 
    153 In an object-oriented programming language, object has methods declared with their types, so a call such as @obj.f()@ only needs to perform lookup in the method table corresponding to type of @obj@. \CFA on the other hand, does not have methods, and all types are open (\ie new operations can be defined on them), so a similar approach will not work in general. However, the ``big 3'' operators have a unique property enforced by the language rules, such that the first parameter must have a reference type. Since \CFA does not have class inheritance, reference type must always match exactly. Therefore, argument-dependent lookup can be implemented for these operators, by using a dedicated symbol table.
    154 
    155 The lookup key used for the special functions is the mangled type name of the first parameter, which acts as the @this@ parameter in an object-oriented language. To handle generic types, the type parameters are stripped off, and only the base type is matched. Note that a constructor (destructor, assignment operator) taking arbitrary @this@ argument, for example @forall( dtype T ) void ?{}( T & );@ is not allowed, and it guarantees that if the @this@ type is known, all possible overloads can be found by searching with the given type. In case that the @this@ argument itself is overloaded, it is resolved first and all possible result types are used for lookup.
    156 
    157 Note that for the generated expressions, the particular variable for @this@ argument is fully known, without overloads, so the majority of constructor call resolutions only need to check for one given object type. Explicit constructor calls and assignment statements sometimes may require lookup for multiple types. In the extremely rare case that type of @this@ argument is yet unbound, everything will have to be checked, just like without the argument-dependent lookup algorithm; fortunately, this case almost never happens in practice. An example is found in the library function @new@:
     168\label{s:SpecialFunctionLookup}
     169
     170Reducing the number of function looked ups for overload resolution is an effective way to gain performance when there are many overloads but most of them are trivially wrong. In practice, most functions have few (if any) overloads but there are notable exceptions. Most importantly, constructor @?{}@, destructor @^?{}@, and assignment @?=?@ are generated for every user-defined type (@struct@ and @union@ in C), and in a large source file there can be hundreds of them. Furthermore, many calls are generated for initializing variables, passing arguments and copying values. This fact makes them the most overloaded and most called functions.
     171
     172In an object-oriented programming language, the object-method types are scoped within a class, so a call such as @obj.f()@ only needs to perform lookup in the method table corresponding to the type of @obj@. \CFA on the other hand, does not have methods, and all types are open, \ie new operations can be defined on them without inheritance; at best a \CFA type can be constrained by a translation unit. However, the ``big 3'' operators have a unique property enforced by the language rules: the first parameter must be a reference to its associated type, which acts as the @this@ parameter in an object-oriented language. Since \CFA does not have class inheritance, the reference type must always match exactly. Therefore, argument-dependent lookup can be implemented for these operators by using a dedicated, fast symbol-table.
     173
     174The lookup key for the special functions is the mangled type name of the first parameter. To handle generic types, the type parameters are stripped off, and only the base type is matched. Note a constructor (destructor, assignment operator) may not take an arbitrary @this@ argument, \eg @forall( dtype T ) void ?{}( T & )@, thus guaranteeing that if the @this@ type is known, all possible overloads can be found by searching with this given type. In the case where the @this@ argument itself is overloaded, it is resolved first and all possible result types are used for lookup.
     175
     176Note that for a generated expression, the particular variable for the @this@ argument is fully known, without overloads, so the majority of constructor-call resolutions only need to check for one given object type. Explicit constructor calls and assignment statements sometimes require lookup for multiple types. In the extremely rare case that the @this@-argument type is unbound, all necessary types are guaranteed to be checked, as for the previous lookup without the argument-dependent lookup; fortunately, this complex case almost never happens in practice. An example is found in the library function @new@:
    158177\begin{cfa}
    159178forall( dtype T | sized( T ), ttype TT | { void ?{}( T &, TT ); } )
    160179T * new( TT p ) { return &(*malloc()){ p }; }
    161180\end{cfa}
    162 as @malloc@ may return a pointer to any type, depending on context. 
    163 
    164 Interestingly, this particular line of code actually caused another complicated issue, where the unusually massive work of checking every constructor in presence makes the case even worse. Section~\ref{s:TtypeResolutionInfiniteRecursion} presents a detailed analysis for the problem.
    165 
    166 The ``callable'' operator @?()@ (cf. @operator()@ in \CC) could also be included in the special operator list, as it is usually only on user-defined types, and the restriction that first argument must be a reference seems reasonable in this case.
     181as @malloc@ may return a pointer to any type, depending on context.
     182
     183Interestingly, this particular declaration actually causes another complicated issue, making the complex checking of every constructor even worse. \VRef[Section]{s:TtypeResolutionInfiniteRecursion} presents a detailed analysis of this problem.
     184
     185The ``callable'' operator @?()@ (cf. @operator()@ in \CC) can also be included in this special operator list, as it is usually only on user-defined types, and the restriction that the first argument must be a reference seems reasonable in this case.
    167186
    168187
    169188\subsection{Improvement of function type representation}
    170189
    171 Since substituting type parameters with their bound types is one fundamental operation in many parts of resolver algorithm (particularly unification and environment binding), making as few copies of type nodes as possible helps reducing memory complexity. Even with the new memory management model, allocation is still a significant factor of resolver performance. Conceptually, operations on type nodes of AST should be performed in functional programming style, treating the data structure as immutable and only copy when necessary. The in-place mutation is a mere optimization that does not change logic of operations.
    172 The model was broken on function types by an inappropriate design. Function types require some special treatment due to the existence of assertions. In particular, it must be able to distinguish two different kinds of type parameter usage:
     190Since substituting type parameters with their bound types is one fundamental operation in many parts of resolver algorithm (particularly unification and environment binding), making as few copies of type nodes as possible helps reducing memory complexity. Even with the new memory management model, allocation is still a significant factor of resolver performance. Conceptually, operations on type nodes of the AST should be performed in functional-programming style, treating the data structure as immutable and only copying when necessary. The in-place mutation is a mere optimization that does not change the logic for operations.
     191
     192However, the model was broken for function types by an inappropriate design. Function types require special treatment due to the existence of assertions that constrain the types it supports. Specifically, it must be possible to distinguish two different kinds of type parameter usage:
    173193\begin{cfa}
    174194forall( dtype T ) void foo( T * t ) {
    175         forall( dtype U ) void bar( T * t, U * u ) { ... }
    176 }
    177 \end{cfa}
    178 Here, only @U@ is a free parameter in declaration of @bar@, as it appears in the function's own forall clause; while @T@ is not free.
    179 
    180 Moreover, the resolution algorithm also has to distinguish type bindings of multiple calls to the same function, for example with
     195        forall( dtype U ) void bar( @T@ * t, @U@ * u ) { ... }
     196}
     197\end{cfa}
     198Here, only @U@ is a free parameter in the nested declaration of function @bar@, as @T@ must be bound at the call site when resolving @bar@.
     199
     200Moreover, the resolution algorithm also has to distinguish type bindings of multiple calls to the same function, \eg:
    181201\begin{cfa}
    182202forall( dtype T ) int foo( T x );
    183 foo( foo( 1.0 ) );
    184 \end{cfa}
    185 The inner call has binding (T: double) while the outer call has binding (T: int). Therefore a unique representation of free parameters in each expression is required. This was previously done by creating a copy of the parameter declarations inside function type, and fixing references afterwards. However, fixing references is an inherently deep operation that does not work well with functional programming model, as it must be evaluated eagerly on the entire syntax tree representing the function type.
    186 
    187 The revised approach generates a unique ID value for each function call expression instance and represents an occurrence of free parameter type with a pair of generated ID and the original parameter declaration, so that references do not need to be fixed, and a shallow copy of function type is possible.
    188 
    189 Note that after the change, all declaration nodes in syntax tree representation maps one-to-one with the actual declarations in the program, and therefore are guaranteed to be unique. Such property can potentially enable more optimizations, and some related ideas are presented after Section~\ref{s:SharedSub-ExpressionCaseUniqueExpressions}.
     203int i = foo( foo( 1.0 ) );
     204\end{cfa}
     205The inner call has binding (T: double) while the outer call has binding (T: int). Therefore a unique representation for the free parameters is required in each expression. This type binding was previously done by creating a copy of the parameter declarations inside the function type and fixing references afterwards. However, fixing references is an inherently deep operation that does not work well with the functional-programming style, as it forces eager evaluation on the entire syntax tree representing the function type.
     206
     207The revised approach generates a unique ID value for each function call expression instance and represents an occurrence of a free-parameter type with a pair of generated ID and original parameter declaration, so references are unique and a shallow copy of the function type is possible.
     208
     209Note that after the change, all declaration nodes in the syntax-tree representation now map one-to-one with the actual declarations in the program, and therefore are guaranteed to be unique. This property can potentially enable more optimizations, and some related ideas are presented at the end of \VRef{s:SharedSub-ExpressionCaseUniqueExpressions}.
    190210
    191211
    192212\subsection{Improvement of pruning steps}
    193213
    194 A minor improvement for candidate elimination is to skip the step on the function overloads themselves and only perform on results of function application. As function calls are usually by name, the name resolution rule dictates that every function candidate necessarily has a different type; indirect function calls are rare, and when they do appear, they usually will not have many possible interpretations, and those rarely matches exactly in argument type. Since function types have a much more complex representation than data types (with multiple parameters and assertions), checking equality on them also takes longer.
    195 
    196 A brief test of this approach shows that the number of function overloads considered in expression resolution increases by a negligible amount of less than 1 percent, while type comparisons in candidate elimination are cut by more than half. Improvement is consistent over all \CFA source files in the test suite.
     214A minor improvement for candidate elimination is to skip the step on the function overloads and only check the results of function application. As function calls are usually by name (versus pointers to functions), the name resolution rule dictates that every function candidate necessarily has a different type; indirect function calls are rare, and when they do appear, there are even fewer cases with multiple interpretations, and these rarely match exactly in argument type. Since function types have a much more complex representation (with multiple parameters and assertions) than data types, checking equality on them also takes longer.
     215
     216A brief test of this approach shows that the number of function overloads considered in expression resolution increases by an amount of less than 1 percent, while type comparisons in candidate elimination are reduced by more than half. This improvement is consistent over all \CFA source files in the test suite.
    197217
    198218
     
    200220\label{s:SharedSub-ExpressionCaseUniqueExpressions}
    201221
    202 Unique expression denotes an expression that must be evaluated only once, to prevent unwanted side effects. It is currently only a compiler artifact, generated on tuple member expression of the form
     222Unique expression denotes an expression evaluated only once to prevent unwanted side effects. It is currently only a compiler artifact, generated for tuple-member expression of the form:
    203223\begin{cfa}
    204224struct S { int a; int b; };
     
    206226s.[a, b]; // tuple member expression, type is [int, int]
    207227\end{cfa}
    208 If the aggregate expression contains function calls, it cannot be evaluated multiple times:
     228If the aggregate expression is function call, it cannot be evaluated multiple times:
    209229\begin{cfa}
    210230S makeS();
    211 makeS().[a, b]; // this should only make one S
     231makeS().[a, b]; // this should only generate a unique S
    212232\end{cfa}
    213233Before code generation, the above expression is internally represented as
     
    226246\end{cfa}
    227247at code generation, where @_unique_var@ and @_unique_var_evaluated@ are generated variables whose scope covers all appearances of the same expression.
    228 
    229 Note that although the unique expression is only used for tuple expansion now, it is a generally useful construction, and can be seen in other languages, such as Scala's @lazy val@~\cite{Scala}; therefore it could be worthwhile to introduce the unique expression to a broader context in \CFA and even make it directly available to programmers.
    230 
    231 In the compiler's visitor pattern, however, this creates a problem where multiple paths to a logically unique expression exist, so it may be modified more than once and become ill-formed; some specific intervention is required to ensure that unique expressions are only visited once. Furthermore, a unique expression appearing in more than one places will be copied on mutation so its representation is no longer unique. Some hacks are required to keep it in sync, and the methods are different when mutating the unique expression instance itself or its underlying expression.
    232 
    233 Example when mutating the underlying expression (visit-once guard)
     248The conditional check ensures a single call to @makeS()@ even though there are logically multiple calls because of the tuple field expansion.
     249
     250Note that although the unique expression is only used for tuple expansion now, it is a generally useful construction, and is seen in other programming languages, such as Scala's @lazy val@~\cite{Scala}; therefore it may be worthwhile to introduce the unique expression to a broader context in \CFA and even make it directly available to programmers.
     251
     252In the compiler's visitor pattern, however, this creates a problem where multiple paths to a logically unique expression exist, so it may be modified more than once and become ill-formed; some specific intervention is required to ensure unique expressions are only visited once. Furthermore, a unique expression appearing in more than one places is copied on mutation so its representation is no longer unique.
     253
     254Currently, special cases are required to keep everything synchronized, and the methods are different when mutating the unique expression instance itself or its underlying expression:
     255\begin{itemize}
     256\item
     257When mutating the underlying expression (visit-once guard)
    234258\begin{cfa}
    235259void InsertImplicitCalls::previsit( const ast::UniqueExpr * unqExpr ) {
    236         if ( visitedIds.count( unqExpr->id ) ) visit_children = false;
     260        @if ( visitedIds.count( unqExpr->id ) ) visit_children = false;@
    237261        else visitedIds.insert( unqExpr->id );
    238262}
    239263\end{cfa}
    240 Example when mutating the unique instance itself, which actually creates copies
     264\item
     265When mutating the unique instance itself, which actually creates copies
    241266\begin{cfa}
    242267auto mutExpr = mutate( unqExpr ); // internally calls copy when shared
    243 if ( ! unqMap.count( unqExpr->id ) ) {
     268@if ( ! unqMap.count( unqExpr->id ) ) {@
    244269        ...
    245270} else {
     
    248273}
    249274\end{cfa}
    250 Such workaround seems difficult to be fit into a common visitor template. This suggests the memory model may need different kinds of nodes to accurately represent the syntax tree.
    251 
    252 Together with the fact that declaration nodes are always unique, it is possible that AST nodes can be classified by three different types:
    253 \begin{itemize}
    254 \item
    255 \textbf{Strictly unique} with only one owner (declarations);
    256 \item
    257 \textbf{Logically unique} with (possibly) many owners but should not be copied (unique expression example presented here);
    258 \item
    259 \textbf{Shared} by functional programming model, which assume immutable data structure and are copied on mutation.
     275\end{itemize}
     276Such workarounds are difficult to fit into the common visitor pattern, which suggests the memory model may need different kinds of nodes to accurately represent this feature in the AST.
     277
     278Given that declaration nodes are unique, it is possible for AST nodes to be divided into three different types:
     279\begin{itemize}
     280\item
     281\textbf{Singleton} with only one owner (declarations);
     282\item
     283\textbf{No-copy} with multiple owners but cannot be copied (unique expression example presented here);
     284\item
     285\textbf{Copy} by functional-programming style, which assumes immutable data structures that are copied on mutation.
    260286\end{itemize}
    261287The boilerplate code can potentially handle these three cases differently.
     
    264290\section{Analysis of resolver algorithm complexity}
    265291
    266 The focus of this chapter is to identify and analyze some realistic cases that cause resolver algorithm to have an exponential run time. As previous work has shown [3], the overload resolution problem in \CFA has worst-case exponential complexity; however, only few specific patterns can trigger the exponential complexity in practice. Implementing heuristic-based optimization for those selected cases is helpful to alleviate the problem.
     292The focus of this section is to identify and analyze some realistic cases that cause the resolver algorithm to have an exponential runtime. As previous work has shown~\cite[\S~4.2.1]{Moss19}, the overload resolution problem in \CFA has worst-case exponential complexity; however, only few specific patterns can trigger the exponential complexity in practice. Implementing heuristic-based optimization for those selected cases is helpful to alleviate the problem.
    267293
    268294
     
    270296\label{s:UnboundReturnType}
    271297
    272 The interaction of return type overloading and polymorphic functions creates this problem of function calls with unbound return type, and is further complicated by the presence of assertions.
     298The interaction of return-type overloading and polymorphic functions creates function calls with unbounded return-type, and is further complicated by the presence of assertions.
    273299The prime example of a function with unbound return type is the type-safe version of C @malloc@:
    274300\begin{cfa}
    275 // size deduced from type, so no need to provide the size argument
    276 forall( dtype T | sized( T ) ) T * malloc( void );
    277 \end{cfa}
    278 Unbound return type can be problematic in resolver algorithm complexity because a single match of function call with unbound return type may create multiple candidates. In the worst case, consider a function declared to return any @otype@:
     301forall( dtype T | sized( T ) )
     302T * malloc( void ) { return (T *)malloc( sizeof(T) ); } // call C malloc
     303int * i = malloc();  // type deduced from left-hand size $\(\Rightarrow\)$ no size argument or return cast
     304\end{cfa}
     305An unbound return-type is problematic in resolver complexity because a single match of a function call with an unbound return type may create multiple candidates. In the worst case, consider a function declared that returns any @otype@ (defined \VPageref{otype}):
    279306\begin{cfa}
    280307forall( otype T ) T anyObj( void );
    281308\end{cfa}
    282 As the resolver attempts to satisfy the otype constraint on @T@, a single call to @anyObj()@ without the result type known creates at least as many candidates as the number of complete types currently in scope; with generic types it becomes even worse, for example, assuming a declaration of generic pair is available at that point:
     309As the resolver attempts to satisfy the otype constraint on @T@, a call to @anyObj()@ in an expression, without the result type known, creates at least as many candidates as the number of complete types currently in scope; with generic types it becomes even worse, \eg assuming a declaration of a generic @pair@ is available at that point:
    283310\begin{cfa}
    284311forall( otype T, otype U ) struct pair { T first; U second; };
    285312\end{cfa}
    286 Then an @anyObj()@ call can result in arbitrarily complex types, such as @pair( pair( int,int ), pair( int,int ) )@, and the depth can grow indefinitely until the specified parameter depth limit, thus creating exponentially many candidates. However, the expected types allowed by parent expressions are practically very few, so most of those interpretations are invalid; if the result type is never bound up to top level, by the semantic rules it is ambiguous if there are more than one valid bindings, and resolution can fail fast. It is therefore reasonable to delay resolving assertions on an unbound parameter in return type; however, with the current cost model, such behavior may further cause irregularities in candidate selection, such that the presence of assertions can change the preferred candidate, even when order of expression costs are supposed to stay the same. Detailed analysis of this issue will be presented later, in the correctness part.
     313Then an @anyObj()@ call can result in arbitrarily complex types, such as @pair( pair( int, int ), pair( int, int ) )@, and the depth can grow indefinitely until a specified parameter-depth limit, thus creating exponentially many candidates. However, the expected types allowed by parent expressions are practically very few, so most of those interpretations are invalid; if the result type is never bound up to the top level, by the semantic rules it is ambiguous if there is more than one valid binding and resolution fails quickly. It is therefore reasonable to delay resolving assertions on an unbound parameter in a return type; however, with the current cost model, such behavior may further cause irregularities in candidate selection, such that the presence of assertions can change the preferred candidate, even when order of expression costs are supposed to stay the same. A detailed analysis of this issue is presented in \VRef{s:AnalysisTypeSystemCorrectness}.
    287314
    288315
     
    290317\label{s:TtypeResolutionInfiniteRecursion}
    291318
    292 @ttype@ (``tuple type'') is a relatively new addition to the language that attempts to provide type-safe variadic argument semantics. Unlike regular @dtype@ parameters, @ttype@ is only valid in function parameter list, and may only appear once as the type of last parameter. At the call site, a @ttype@ parameter is bound to the tuple type of all remaining function call arguments.
     319@ttype@ (``tuple type'') is a relatively new addition to the language that attempts to provide type-safe variadic argument semantics. Unlike regular @dtype@ parameters, @ttype@ is only valid in a function parameter-list, and may only appear once as the last parameter type. At the call site, a @ttype@ parameter is bound to the tuple type of all remaining function-call arguments.
    293320
    294321There are two kinds of idiomatic @ttype@ usage: one is to provide flexible argument forwarding, similar to the variadic template in \CC (\lstinline[language=C++]|template<typename... args>|), as shown below in the implementation of @unique_ptr@
     
    298325        T * data;
    299326};
    300 forall( dtype T | sized( T ), ttype Args | { void ?{}( T &, Args ); })
    301 void ?{}( unique_ptr( T ) & this, Args args ) {
    302         this.data = new( args );
    303 }
    304 \end{cfa}
    305 the other is to implement structural recursion in the first-rest manner:
    306 \begin{cfa}
    307 forall( otype T, ttype Params | { void process( T ); void func( Params ); })
     327forall( dtype T | sized( T ), @ttype Args@ | { void ?{}( T &, Args ); })
     328void ?{}( unique_ptr( T ) & this, Args @args@ ) {
     329        this.data = new( @args@ );  // forward constructor arguments to dynamic allocator
     330}
     331\end{cfa}
     332The other usage is to implement structural recursion in the first-rest pattern:
     333\begin{cfa}
     334forall( otype T, @ttype Params@ | { void process( T ); void func( Params ); })
    308335void func( T arg1, Params p ) {
    309336        process( arg1 );
    310         func( p );
    311 }
    312 \end{cfa}
    313 For the second use case, it is important that the number of parameters in the recursive call go down, since the call site must deduce all assertion candidates, and that is only possible if by just looking at argument types (and not their values), the recursion is known to be completed in a finite number of steps.
    314 
    315 In recent experiments, however, some flaw in the type binding rules can lead to the first kind of @ttype@ use case produce an invalid candidate that the resolver enters an infinite loop.
    316 
    317 This bug was discovered in an attempt to raise assertion recursive depth limit and one of the library program takes exponentially longer time to compile. The cause of the problem is identified to be the following set of functions.
    318 File @memory.cfa@ contains
    319 \begin{cfa}
    320 #include "memory.hfa"
    321 #include "stdlib.hfa"
    322 \end{cfa}
    323 where file @memory.hfa@ contains the @unique_ptr@ declaration above, and two other similar functions with @ttype@ parameter:
    324 \begin{cfa}
    325 forall( dtype T | sized( T ), ttype Args | { void ?{}( T &, Args ); }) {
     337        func( @p@ );  // recursive call until base case of one argument
     338}
     339\end{cfa}
     340For the second use case, it is imperative the number of parameters in the recursive call goes down, since the call site must deduce all assertion candidates, and that is only possible if by observation of the argument types (and not their values), the recursion is known to be completed in a finite number of steps.
     341
     342In recent experiments, however, a flaw in the type-binding rules can lead to the first kind of @ttype@ use case producing an invalid candidate and the resolver enters an infinite loop.
     343This bug was discovered in an attempt to raise the assertion recursive-depth limit and one of the library programs took exponentially longer to compile. The cause of the problem is the following set of functions:
     344\begin{cfa}
     345// unique_ptr  declaration from above
     346
     347forall( dtype T | sized( T ), ttype Args | { void ?{}( T &, Args ); } ) { // distribute forall clause
    326348        void ?{}( counter_data( T ) & this, Args args );
    327349        void ?{}( counter_ptr( T ) & this, Args args );
    328350        void ?{}( unique_ptr( T ) & this, Args args );
    329351}
    330 \end{cfa}
    331 File @stdlib.hfa@ contains
    332 \begin{cfa}
     352
    333353forall( dtype T | sized( T ), ttype TT | { void ?{}( T &, TT ); } )
    334 T * new( TT p ) { return &(*malloc()){ p }; }
    335 \end{cfa}
    336 
    337 In the expression @(*malloc()){p}@, the type of object being constructed is yet unknown, since the return type information is not immediately provided. That caused every constructor to be searched, and while normally a bound @ttype@ cannot be unified with any free parameter, it is possible with another free @ttype@. Therefore in addition to the correct option provided by assertion, 3 wrong options are examined, each of which again requires the same assertion, for an unknown base type T and @ttype@ arguments, and that becomes an infinite loop, until the specified recursion limit and resolution is forced to fail. Moreover, during the recursion steps, number of candidates grows exponentially, since there are always 3 options at each step.
    338 
    339 Unfortunately, @ttype@ to @ttype@ binding is necessary, to allow calling the function provided by assertion indirectly.
    340 \begin{cfa}
    341 forall( dtype T | sized( T ), ttype Args | { void ?{}( T &, Args ); })
    342 void ?{}( unique_ptr( T ) & this, Args args ) { this.data = (T * )new( args ); }
    343 \end{cfa}
    344 Here the constructor assertion is used for the @new( args )@ call.
     354T * new( TT p ) { return @&(*malloc()){ p };@ }
     355\end{cfa}
     356In the expression @(*malloc()){p}@, the type of the object being constructed is unknown, since the return-type information is not immediately available. That causes every constructor to be searched, and while normally a bound @ttype@ cannot be unified with any free parameter, it is possible with another free @ttype@. Therefore, in addition to the correct option provided by the assertion, 3 wrong options are examined, each of which again requires the same assertion, for an unknown base-type @T@ and @ttype@ argument, which becomes an infinite loop until the specified recursion limit and resolution is fails. Moreover, during the recursion steps, the number of candidates grows exponentially, since there are always 3 options at each step.
     357
     358Unfortunately, @ttype@ to @ttype@ binding is necessary, to allow indirectly calling a function provided in an assertion.
     359\begin{cfa}
     360forall( dtype T | sized( T ), ttype Args | { @void ?{}( T &, Args );@ })
     361void ?{}( unique_ptr( T ) & this, Args args ) { this.data = (T *)@new( args )@; } // constructor call
     362\end{cfa}
     363Here the constructor assertion is used by the @new( args )@ call to indirectly call the constructor on the allocated storage.
    345364Therefore, it is hard, perhaps impossible, to solve this problem by tweaking the type binding rules. An assertion caching algorithm can help improve this case by detecting cycles in recursion.
    346365
    347 Meanwhile, without the caching algorithm implemented, some changes in the \CFA source code are enough to eliminate this problem, at least in the current codebase. Note that the issue only happens with an overloaded variadic function, which rarely appears in practice, since the idiomatic use cases are for argument forwarding and self-recursion. The only overloaded @ttype@ function so far discovered in all of \CFA standard library code is the constructor, and by utilizing the argument-dependent lookup process described in Section~\ref{s:UnboundReturnType}, adding a cast before constructor call gets rid of the issue.
    348 \begin{cfa}
    349 T * new( TT p ) { return &(*(T * )malloc()){ p }; }
     366Meanwhile, without a caching algorithm implemented, some changes in the \CFA source code are enough to eliminate this problem, at least in the current codebase. Note that the issue only happens with an overloaded variadic function, which rarely appears in practice, since the idiomatic use cases are for argument forwarding and self-recursion. The only overloaded @ttype@ function so far discovered in all of \CFA standard library is the constructor, and by utilizing the argument-dependent lookup process described in \VRef{s:UnboundReturnType}, adding a cast before the constructor call removes the issue.
     367\begin{cfa}
     368T * new( TT p ) { return &(*@(T * )@malloc()){ p }; }
    350369\end{cfa}
    351370
     
    353372\subsection{Reused assertions in nested generic type}
    354373
    355 The following test of deeply nested dynamic generic type reveals that locally caching reused assertions is necessary, rather than just a resolver optimization, because recomputing assertions can result in bloated generated code size:
     374The following test of deeply nested, dynamic generic type reveals that locally caching reused assertions is necessary, rather than just a resolver optimization, because recomputing assertions can result in bloated generated code size:
    356375\begin{cfa}
    357376struct nil {};
     
    361380int main() {
    362381        #if   N==0
    363         nil x;   
     382        nil @x@;
    364383        #elif N==1
    365         cons( size_t, nil ) x;
     384        cons( size_t, nil ) @x@;
    366385        #elif N==2
    367         cons( size_t, cons( size_t, nil ) ) x;
     386        cons( size_t, cons( size_t, nil ) ) @x@;
    368387        #elif N==3
    369         cons( size_t, cons( size_t, cons( size_t, nil ) ) ) x;
     388        cons( size_t, cons( size_t, cons( size_t, nil ) ) ) @x@;
    370389        // similarly for N=4,5,6
    371390        #endif
    372391}
    373392\end{cfa}
    374 At the declaration of @x@, it is implicitly initialized by generated constructor call, whose signature is given by
     393At the declaration of @x@, it is implicitly initialized by generated constructor call, with signature:
    375394\begin{cfa}
    376395forall( otype L, otype R ) void ?{}( cons( L, R ) & );
    377396\end{cfa}
    378 Note that the @otype@ constraint contains 4 assertions:
     397where the @otype@ constraint contains the 4 assertions:\label{otype}
    379398\begin{cfa}
    380399void ?{}( L & ); // default constructor
     
    383402L & ?=?( L &, L & ); // assignment
    384403\end{cfa}
    385 Now since the right hand side of outermost cons is again a cons, recursive assertions are required. When the compiler cannot cache and reuse already resolved assertions, it becomes a problem, as each of those 4 pending assertions again asks for 4 more assertions one level below. Without any caching, number of resolved assertions grows exponentially, while that is obviously unnecessary since there are only $n+1$ different types involved. Even worse, this causes exponentially many wrapper functions generated later at the codegen step, and results in huge compiled binary.
    386 
    387 \begin{table}[h]
     404
     405\begin{table}[htb]
     406\centering
    388407\caption{Compilation results of nested cons test}
     408\label{t:NestedConsTest}
    389409\begin{tabular}{|r|r|r|}
    390410\hline
     
    402422\end{table}
    403423
    404 As the local functions are implemented by emitting executable code on the stack~\cite{gcc-nested-func}, it eventually means that compiled code also has exponential run time. This problem has evident practical implications, as nested collection types are frequently used in real production code.
    405 
     424Now since the right hand side of outermost cons is again a cons, recursive assertions are required. \VRef[Table]{t:NestedConsTest} shows when the compiler does not cache and reuse already resolved assertions, it becomes a problem, as each of these 4 pending assertions again asks for 4 more assertions one level below. Without caching, the number of resolved assertions grows exponentially, which is unnecessary since there are only $n+1$ different types involved. Even worse, this problem causes exponentially many wrapper functions to be generated at the backend, resulting in a huge binary. As the local functions are implemented by emitting executable code on the stack~\cite{gcc-nested-func}, it means that compiled code also has exponential run time. This problem has practical implications, as nested collection types are frequently used in real production code.
    406425
    407426\section{Analysis of type system correctness}
     427\label{s:AnalysisTypeSystemCorrectness}
    408428
    409429In Moss' thesis~\cite[\S~4.1.2,~p.~45]{Moss19}, the author presents the following example:
     
    412432\begin{cfa}
    413433void f( int );
    414 double g$_1$( int );
    415 int g$_2$( long );
     434double g$\(_1\)$( int );
     435int g$\(_2\)$( long );
    416436f( g( 42 ) );
    417437\end{cfa}
     
    422442From the set of candidates whose parameter and argument types have been unified and whose assertions have been satisfied, those whose sub-expression interpretations have the smallest total cost of conversion are selected ... The total cost of conversion for each of these candidates is then calculated based on the implicit conversions and polymorphism involved in adapting the types of the sub-expression interpretations to the formal parameter types.
    423443\end{quote}
    424 With this model, the algorithm picks @g1@ in resolving the @f( g( 42 ) )@ call, which seems to be undesirable.
    425 
    426 There are further evidence that shows the Bilson model is fundamentally incorrect, following the discussion of unbound return type in Section~\ref{s:UnboundReturnType}. By the conversion cost specification, a binding from a polymorphic type parameter to a concrete type incurs a polymorphic cost of 1. It remains unspecified \emph{when} the type parameters should become bound. When the parameterized types appear in the function parameters, they can be deduced from the argument type, and there is no ambiguity. In the unbound return case, however, the binding may happen at any stage in expression resolution, therefore it is impossible to define a unique local conversion cost. Note that type binding happens exactly once per parameter in resolving the entire expression, so the global binding cost is unambiguously 1.
    427 
    428 As per the current compiler implementation, it does have a notable inconsistency in handling such case. For any unbound parameter that does \emph{not} come with an associated assertion, it remains unbound to the parent expression; for those that does however, they are immediately bound in the assertion resolution step, and concrete result types are used in the parent expressions.
    429 
     444With this model, the algorithm picks @g1@ in resolving the @f( g( 42 ) )@ call, which is undesirable.
     445
     446There is further evidence that shows the Bilson model is fundamentally incorrect, following the discussion of unbound return type in \VRef{s:UnboundReturnType}. By the conversion-cost specification, a binding from a polymorphic type-parameter to a concrete type incurs a polymorphic cost of 1. It remains unspecified \emph{when} the type parameters should become bound. When the parameterized types appear in function parameters, they can be deduced from the argument type, and there is no ambiguity. In the unbound return case, however, the binding may happen at any stage in expression resolution, therefore it is impossible to define a unique local conversion cost. Note that type binding happens exactly once per parameter in resolving the entire expression, so the global binding cost is unambiguously 1.
     447
     448In the current compiler implementation, there is a notable inconsistency in handling this case. For any unbound parameter that does \emph{not} come with an associated assertion, it remains unbound to the parent expression; for those that do, however, they are immediately bound in the assertion resolution step, and concrete result types are used in the parent expressions.
    430449Consider the following example:
    431450\begin{cfa}
     
    433452void h( int * );
    434453\end{cfa}
    435 The expression @h( f() )@ eventually has a total cost of 1 from binding (T: int), but in the eager resolution model, the cost of 1 may occur either at call to @f@ or at call to @h@, and with the assertion resolution triggering a binding, the local cost of @f()@ is (0 poly, 0 spec) with no assertions, but (1 poly, -1 spec) with an assertion:
    436 \begin{cfa}
    437 forall( dtype T | { void g( T * ); } ) T * f( void );
     454The expression @h( f() )@ eventually has a total cost of 1 from binding (T: int), but in the eager-resolution model, the cost of 1 may occur either at the call to @f@ or at call to @h@, and with the assertion resolution triggering a binding, the local cost of @f()@ is (0 poly, 0 spec) with no assertions, but (1 poly, -1 spec) with an assertion:
     455\begin{cfa}
     456forall( dtype T | @{ void g( T * ); }@ ) T * f( void );
    438457void g( int * );
    439458void h( int * );
    440459\end{cfa}
    441 and that contradicts the principle that adding assertions should make expression cost lower. Furthermore, the time at which type binding and assertion resolution happens is an implementation detail of the compiler, but not a part of language definition. That means two compliant \CFA compilers, one performing immediate assertion resolution at each step, and one delaying assertion resolution on unbound types, can produce different expression costs and therefore different candidate selection, making the language rule itself partially undefined and therefore unsound. By the above reasoning, the updated cost model using global sum of costs should be accepted as the standard. It also allows the compiler to freely choose when to resolve assertions, as the sum of total costs is independent of that choice; more optimizations regarding assertion resolution can also be implemented.
     460and that contradicts the principle that adding assertions should make expression cost lower. Furthermore, the time at which type binding and assertion resolution happens is an implementation detail of the compiler, not part of the language definition. That means two compliant \CFA compilers, one performing immediate assertion resolution at each step, and one delaying assertion resolution on unbound types, can produce different expression costs and therefore different candidate selection, making the language rule itself partially undefined, and therefore, unsound. By the above reasoning, the updated cost model using global sum of costs should be accepted as the standard. It also allows the compiler to freely choose when to resolve assertions, as the sum of total costs is independent of that choice; more optimizations regarding assertion resolution can also be implemented.
    442461
    443462
    444463\section{Timing results}
    445464
    446 For the timing results presented here, the \CFA compiler is built with gcc 9.3.0, and tested on a server machine running Ubuntu 20.04, 64GB RAM and 32-core 2.2 GHz CPU, results reported by the time command, and using only 8 cores in parallel such that the time is close to the case with 100% CPU utilization on a single thread.
    447 
    448 On the most recent build, the \CFA standard library (~1.3 MB of source code) compiles in 4 minutes 47 seconds total processor time (single thread equivalent), with the slowest file taking 13 seconds. The test suite (178 test cases, ~2.2MB of source code) completes within 25 minutes total processor time,\footnote{Including a few runtime tests; total time spent in compilation is approximately 21 minutes.} with the slowest file taking 23 seconds. In contrast, the library build on old compiler takes 85 minutes total, 5 minutes for the slowest file. Full test suite takes too long with old compiler build and is therefore not run, but the slowest test cases take approximately 5 minutes. Overall, the most recent build compared to old build in April 2020, before the project started, is consistently faster by a factor of 20.
    449 
    450 Additionally, 6 selected \CFA source files with distinct features from library and test suite are used to test compiler performance after each of the optimizations are implemented. Test files are from the most recent build and run through C preprocessor to eliminate the factor of header file changes. The selected tests are:
    451 \begin{itemize}
    452 \item
    453 @lib/fstream@ (112 KB)\footnote{File sizes are after preprocessing, with no line information (\lstinline|gcc -E -P|).}: implementation of I/O library
     465For the timing results presented here, the \CFA compiler is built with gcc 9.3.0, and tested on a server machine running Ubuntu 20.04, 64GB RAM and 32-core 2.2 GHz CPU.
     466Timing is reported by the @time@ command and an experiment is run using 8 cores, where each core is at 100\% CPU utilization.
     467
     468On the most recent build, the \CFA standard library ($\approx$1.3 MB of source code) compiles in 4 minutes 47 seconds total processor time (single thread equivalent), with the slowest file taking 13 seconds. The test suite (178 test cases, $\approx$2.2MB of source code) completes within 25 minutes total processor time,
     469% PAB: I do not understand this footnote.
     470%\footnote{Including a few runtime tests; total time spent in compilation is approximately 21 minutes.}
     471with the slowest file taking 23 seconds. In contrast, the library build with the old compiler takes 85 minutes total, 5 minutes for the slowest file. The full test-suite takes too long with old compiler build and is therefore not run, but the slowest test cases take approximately 5 minutes. Overall, the most recent build compared to an old build is consistently faster by a factor of 20.
     472
     473Additionally, 6 selected \CFA source files with distinct features from the library and test suite are used to illustrate the compiler performance change after each of the implemented optimizations. Test files are from the most recent build and run through the C preprocessor to expand header file, perform macro expansions, but no line number information (@gcc -E -P@).
     474\VRef[Table]{t:SelectedFileByCompilerBuild} shows the selected tests:
     475\begin{itemize}
     476\item
     477@lib/fstream@ (112 KB)
    454478\item
    455479@lib/mutex@ (166 KB): implementation of concurrency primitive
     
    459483@lib/stdlib@ (64 KB): type-safe wrapper to @void *@-based C standard library functions
    460484\item
    461 @test/ISO2@ (55 KB): application of I/O library
     485@test/io2@ (55 KB): application of I/O library
    462486\item
    463487@test/thread@ (188 KB): application of threading library
    464488\end{itemize}
    465 
    466 The \CFA compiler builds are picked from git commit history that passed the test suite, and implement the optimizations incrementally:
    467 \begin{itemize}
    468 \item
    469 \#0 is the first working build of new AST data structure
     489versus \CFA compiler builds picked from the git commit history that implement the optimizations incrementally:
     490\begin{itemize}
     491\item
     492old resolver
     493\item
     494\#0 is the first working build of the new AST data structure
    470495\item
    471496\#1 implements special symbol table and argument-dependent lookup
    472497\item
    473 \#2 implements late assertion satisfaction
    474 \item
    475 \#3 implements revised function type representation
    476 \item
    477 \#4 skips pruning on expressions with function type (most recent build)
    478 \end{itemize}
    479 The old resolver with no memory sharing and none of the optimizations above is also tested.
    480 \begin{table}
     498\#2 implements late assertion-satisfaction
     499\item
     500\#3 implements revised function-type representation
     501\item
     502\#4 skips pruning on expressions for function types (most recent build)
     503\end{itemize}
     504Reading left to right for a test shows the benefit of each optimization on the cost of compilation.
     505
     506\begin{table}[htb]
     507\centering
    481508\caption{Compile time of selected files by compiler build, in seconds}
     509\label{t:SelectedFileByCompilerBuild}
    482510\begin{tabular}{|l|r|r|r|r|r|r|}
    483511\hline
     
    502530\end{table}
    503531
    504 
    505532\section{Conclusion}
    506533
    507 Over the course of 8 months of active research and development in \CFA type system and compiler algorithm, performance of the reference \CFA compiler, cfa-cc, has been greatly improved, allowing mid-sized \CFA programs to be compiled and built reasonably fast. As there are also ongoing efforts in the team on building a standard library, evaluating the runtime performance, and attempting to incorporate \CFA with existing software written in C, this project is especially meaningful for practical purposes.
    508 
    509 Analysis conducted in the project were based significantly on heuristics and practical evidence, as the theoretical bounds and average cases for the expression resolution problem differ. This approach was difficult at start to follow, with an unacceptably slow compiler, since running the program through debugger and validation tools (\eg @gdb@, @valgrind@) adds another order of magnitude to run time, which was already in minutes. However, near the end of the project, many significant improvements have already been made and new optimizations can be tested immediately. The positive feedback in development cycle benefits the \CFA team as a whole, more than just for the compiler optimizations.
    510 
    511 Some potential issues of the language that may happen frequently in practice have been identified. Due to the time constraint and complex nature of these problems, a handful of them remain unsolved, but some constructive proposals are made. Notably, introducing a local assertion cache in the resolver is a common solution for a few remaining problems, so that should be the focus of work soon.
    512 
    513 The \CFA team are planning on a public alpha release of the language as the compiler performance becomes promising, and other parts of the system, such as a standard library, are also being enhanced. Ideally, the remaining problems should be resolved before release, and the solutions will also be integral to drafting a formal specification.
     534Over the course of 8 months of active research and development of the \CFA type system and compiler algorithms, performance of the reference \CFA compiler, cfa-cc, has been greatly improved. Now, mid-sized \CFA programs are compiled reasonably fast. Currently, there are ongoing efforts by the \CFA team to augment the standard library and evaluate its runtime performance, and incorporate \CFA with existing software written in C; therefore this project is especially meaningful for these practical purposes.
     535
     536Accomplishing this work was difficult. Analysis conducted in the project is based significantly on heuristics and practical evidence, as the theoretical bounds and average cases for the expression resolution problem differ. As well, the slowness of the initial compiler made attempts to understand why and where problems exist extremely difficult because both debugging and validation tools (\eg @gdb@, @valgrind@, @pref@) further slowed down compilation time. However, by the end of the project, I had found and fixed several significant problems and new optimizations are easier to introduce and test. The reduction in the development cycle benefits the \CFA team as a whole.
     537
     538Some potential issues of the language, which happen frequently in practice, have been identified. Due to the time constraint and complex nature of these problems, a handful of them remain unsolved, but some constructive proposals are made. Notably, introducing a local assertion cache in the resolver is a reasonable solution for a few remaining problems, so that should be the focus of future work.
     539
     540The \CFA team are planning on a public alpha release of the language as the compiler performance, given my recent improvements, is now useable. Other parts of the system, such as the standard library, have made significant gains due to the speed up in the development cycle. Ideally, the remaining problems should be resolved before release, and the solutions will also be integral to drafting a formal specification.
    514541
    515542\addcontentsline{toc}{section}{\refname}
  • doc/theses/fangren_yu_COOP_S20/Report.tex

    r342af53 r8e4aa05  
    1717\usepackage[usenames]{color}
    1818\input{common}                                          % common CFA document macros
    19 \usepackage[dvips,plainpages=false,pdfpagelabels,pdfpagemode=UseNone,colorlinks=true,pagebackref=true,linkcolor=blue,citecolor=blue,urlcolor=blue,pagebackref=true,breaklinks=true]{hyperref}
     19\usepackage[dvips,plainpages=false,pdfpagelabels,pdfpagemode=UseNone,colorlinks=true,linkcolor=blue,citecolor=blue,urlcolor=blue,pagebackref=true,breaklinks=true]{hyperref}
    2020\usepackage{breakurl}
    2121\urlstyle{sf}
  • doc/theses/thierry_delisle_PhD/thesis/Makefile

    r342af53 r8e4aa05  
    88BibTeX = BIBINPUTS=${TeXLIB} && export BIBINPUTS && bibtex
    99
    10 MAKEFLAGS = --no-print-directory --silent
     10MAKEFLAGS = --no-print-directory # --silent
    1111VPATH = ${Build} ${Figures}
    1212
     
    3232        emptytree \
    3333        fairness \
     34        io_uring \
     35        pivot_ring \
    3436        system \
    3537}
     
    4345## Define the documents that need to be made.
    4446all: thesis.pdf
    45 thesis.pdf: ${TEXTS} ${FIGURES} ${PICTURES} glossary.tex local.bib
     47thesis.pdf: ${TEXTS} ${FIGURES} ${PICTURES} thesis.tex glossary.tex local.bib
    4648
    4749DOCUMENT = thesis.pdf
     
    4951
    5052# Directives #
     53
     54.NOTPARALLEL:                                           # cannot make in parallel
    5155
    5256.PHONY : all clean                                      # not file names
     
    8185        ${LaTeX} $<
    8286
    83 build/fairness.svg : fig/fairness.py | ${Build}
    84         python3 $< $@
    85 
    8687## Define the default recipes.
    8788
     
    105106        sed -i 's/$@/${Build}\/$@/g' ${Build}/$@_t
    106107
     108build/fairness.svg : fig/fairness.py | ${Build}
     109        python3 $< $@
     110
    107111## pstex with inverted colors
    108112%.dark.pstex : fig/%.fig Makefile | ${Build}
  • doc/theses/thierry_delisle_PhD/thesis/local.bib

    r342af53 r8e4aa05  
    512512}
    513513
     514@manual{MAN:bsd/kqueue,
     515  title = {KQUEUE(2) - FreeBSD System Calls Manual},
     516  url   = {https://www.freebsd.org/cgi/man.cgi?query=kqueue},
     517  year  = {2020},
     518  month = {may}
     519}
     520
    514521% Apple's MAC OS X
    515522@manual{MAN:apple/scheduler,
     
    577584
    578585% --------------------------------------------------
     586% Man Pages
     587@manual{MAN:open,
     588  key        = "open",
     589  title      = "open(2) Linux User's Manual",
     590  year       = "2020",
     591  month      = "February",
     592}
     593
     594@manual{MAN:accept,
     595  key        = "accept",
     596  title      = "accept(2) Linux User's Manual",
     597  year       = "2019",
     598  month      = "March",
     599}
     600
     601@manual{MAN:select,
     602  key        = "select",
     603  title      = "select(2) Linux User's Manual",
     604  year       = "2019",
     605  month      = "March",
     606}
     607
     608@manual{MAN:poll,
     609  key        = "poll",
     610  title      = "poll(2) Linux User's Manual",
     611  year       = "2019",
     612  month      = "July",
     613}
     614
     615@manual{MAN:epoll,
     616  key        = "epoll",
     617  title      = "epoll(7) Linux User's Manual",
     618  year       = "2019",
     619  month      = "March",
     620}
     621
     622@manual{MAN:aio,
     623  key        = "aio",
     624  title      = "aio(7) Linux User's Manual",
     625  year       = "2019",
     626  month      = "March",
     627}
     628
     629@misc{MAN:io_uring,
     630  title   = {Efficient IO with io\_uring},
     631  author  = {Axboe, Jens},
     632  year    = "2019",
     633  month   = "March",
     634  version = {0,4},
     635  howpublished = {\url{https://kernel.dk/io_uring.pdf}}
     636}
     637
     638% --------------------------------------------------
    579639% Wikipedia Entries
    580640@misc{wiki:taskparallel,
     
    617677  note = "[Online; accessed 2-January-2021]"
    618678}
     679
     680@misc{wiki:future,
     681  author = "{Wikipedia contributors}",
     682  title = "Futures and promises --- {W}ikipedia{,} The Free Encyclopedia",
     683  year = "2020",
     684  url = "https://en.wikipedia.org/wiki/Futures_and_promises",
     685  note = "[Online; accessed 9-February-2021]"
     686}
  • doc/theses/thierry_delisle_PhD/thesis/text/core.tex

    r342af53 r8e4aa05  
    4949
    5050\section{Design}
    51 In general, a na\"{i}ve \glsxtrshort{fifo} ready-queue does not scale with increased parallelism from \glspl{hthrd}, resulting in decreased performance. The problem is adding/removing \glspl{thrd} is a single point of contention. As shown in the evaluation sections, most production schedulers do scale when adding \glspl{hthrd}. The common solution to the single point of contention is to shard the ready-queue so each \gls{hthrd} can access the ready-queue without contention, increasing performance though lack of contention.
     51In general, a na\"{i}ve \glsxtrshort{fifo} ready-queue does not scale with increased parallelism from \glspl{hthrd}, resulting in decreased performance. The problem is adding/removing \glspl{thrd} is a single point of contention. As shown in the evaluation sections, most production schedulers do scale when adding \glspl{hthrd}. The common solution to the single point of contention is to shard the ready-queue so each \gls{hthrd} can access the ready-queue without contention, increasing performance.
    5252
    5353\subsection{Sharding} \label{sec:sharding}
    54 An interesting approach to sharding a queue is presented in \cit{Trevors paper}. This algorithm presents a queue with a relaxed \glsxtrshort{fifo} guarantee using an array of strictly \glsxtrshort{fifo} sublists as shown in Figure~\ref{fig:base}. Each \emph{cell} of the array has a timestamp for the last operation and a pointer to a linked-list with a lock and each node in the list is marked with a timestamp indicating when it is added to the list. A push operation is done by picking a random cell, acquiring the list lock, and pushing to the list. If the cell is locked, the operation is simply retried on another random cell until a lock is acquired. A pop operation is done in a similar fashion except two random cells are picked. If both cells are unlocked with non-empty lists, the operation pops the node with the oldest cell timestamp. If one of the cells is unlocked and non-empty, the operation pops from that cell. If both cells are either locked or empty, the operation picks two new random cells and tries again.
     54An interesting approach to sharding a queue is presented in \cit{Trevors paper}. This algorithm presents a queue with a relaxed \glsxtrshort{fifo} guarantee using an array of strictly \glsxtrshort{fifo} sublists as shown in Figure~\ref{fig:base}. Each \emph{cell} of the array has a timestamp for the last operation and a pointer to a linked-list with a lock. Each node in the list is marked with a timestamp indicating when it is added to the list. A push operation is done by picking a random cell, acquiring the list lock, and pushing to the list. If the cell is locked, the operation is simply retried on another random cell until a lock is acquired. A pop operation is done in a similar fashion except two random cells are picked. If both cells are unlocked with non-empty lists, the operation pops the node with the oldest timestamp. If one of the cells is unlocked and non-empty, the operation pops from that cell. If both cells are either locked or empty, the operation picks two new random cells and tries again.
    5555
    5656\begin{figure}
     
    100100\paragraph{Local Information} Figure~\ref{fig:emptytls} shows an approach using dense information, similar to the bitmap, but each \gls{hthrd} keeps its own independent copy. While this approach can offer good scalability \emph{and} low latency, the liveliness and discovery of the information can become a problem. This case is made worst in systems with few processors where even blind random picks can find \glspl{thrd} in a few tries.
    101101
    102 I built a prototype of these approaches and none of these techniques offer satisfying performance when few threads are present. All of these approach hit the same 2 problems. First, randomly picking sub-queues is very fast but means any improvement to the hit rate can easily be countered by a slow-down in look-up speed when there are empty lists. Second, the array is already as sharded to avoid contention bottlenecks, so any denser data structure tends to become a bottleneck. In all cases, these factors meant the best cases scenario, \ie many threads, would get worst throughput, and the worst-case scenario, few threads, would get a better hit rate, but an equivalent poor throughput. As a result I tried an entirely different approach.
     102I built a prototype of these approaches and none of these techniques offer satisfying performance when few threads are present. All of these approach hit the same 2 problems. First, randomly picking sub-queues is very fast. That speed means any improvement to the hit rate can easily be countered by a slow-down in look-up speed, whether or not there are empty lists. Second, the array is already sharded to avoid contention bottlenecks, so any denser data structure tends to become a bottleneck. In all cases, these factors meant the best cases scenario, \ie many threads, would get worst throughput, and the worst-case scenario, few threads, would get a better hit rate, but an equivalent poor throughput. As a result I tried an entirely different approach.
    103103
    104104\subsection{Dynamic Entropy}\cit{https://xkcd.com/2318/}
    105 In the worst-case scenario there are only few \glspl{thrd} ready to run, or more precisely given $P$ \glspl{proc}\footnote{For simplicity, this assumes there is a one-to-one match between \glspl{proc} and \glspl{hthrd}.}, $T$ \glspl{thrd} and $\epsilon$ a very small number, than the worst case scenario can be represented by $\epsilon \ll P$, than $T = P + \epsilon$. It is important to note in this case that fairness is effectively irrelevant. Indeed, this case is close to \emph{actually matching} the model of the ``Ideal multi-tasking CPU'' on page \pageref{q:LinuxCFS}. In this context, it is possible to use a purely internal-locality based approach and still meet the fairness requirements. This approach simply has each \gls{proc} running a single \gls{thrd} repeatedly. Or from the shared ready-queue viewpoint, each \gls{proc} pushes to a given sub-queue and then popes from the \emph{same} subqueue. In cases where $T \gg P$, the scheduler should also achieves similar performance without affecting the fairness guarantees.
     105In the worst-case scenario there are only few \glspl{thrd} ready to run, or more precisely given $P$ \glspl{proc}\footnote{For simplicity, this assumes there is a one-to-one match between \glspl{proc} and \glspl{hthrd}.}, $T$ \glspl{thrd} and $\epsilon$ a very small number, than the worst case scenario can be represented by $T = P + \epsilon$, with $\epsilon \ll P$. It is important to note in this case that fairness is effectively irrelevant. Indeed, this case is close to \emph{actually matching} the model of the ``Ideal multi-tasking CPU'' on page \pageref{q:LinuxCFS}. In this context, it is possible to use a purely internal-locality based approach and still meet the fairness requirements. This approach simply has each \gls{proc} running a single \gls{thrd} repeatedly. Or from the shared ready-queue viewpoint, each \gls{proc} pushes to a given sub-queue and then pops from the \emph{same} subqueue. The challenge is for the the scheduler to achieve good performance in both the $T = P + \epsilon$ case and the $T \gg P$ case, without affecting the fairness guarantees in the later.
    106106
    107 To handle this case, I use a pseudo random-number generator, \glsxtrshort{prng} in a novel way. When the scheduler uses a \glsxtrshort{prng} instance per \gls{proc} exclusively, the random-number seed effectively starts an encoding that produces a list of all accessed subqueues, from latest to oldest. The novel approach is to be able to ``replay'' the \glsxtrshort{prng} backwards and there exist \glsxtrshort{prng}s that are fast, compact \emph{and} can be run forward and backwards. Linear congruential generators~\cite{wiki:lcg} are an example of \glsxtrshort{prng}s that match these requirements.
     107To handle this case, I use a \glsxtrshort{prng}\todo{Fix missing long form} in a novel way. There exist \glsxtrshort{prng}s that are fast, compact and can be run forward \emph{and} backwards.  Linear congruential generators~\cite{wiki:lcg} are an example of \glsxtrshort{prng}s of such \glsxtrshort{prng}s. The novel approach is to use the ability to run backwards to ``replay'' the \glsxtrshort{prng}. The scheduler uses an exclusive \glsxtrshort{prng} instance per \gls{proc}, the random-number seed effectively starts an encoding that produces a list of all accessed subqueues, from latest to oldest. Replaying the \glsxtrshort{prng} to identify cells accessed recently and which probably have data still cached.
    108108
    109109The algorithm works as follows:
  • doc/theses/thierry_delisle_PhD/thesis/text/intro.tex

    r342af53 r8e4aa05  
    77While previous work on the concurrent package of \CFA focused on features and interfaces, this thesis focuses on performance, introducing \glsxtrshort{api} changes only when required by performance considerations. More specifically, this thesis concentrates on scheduling and \glsxtrshort{io}. Prior to this work, the \CFA runtime used a strictly \glsxtrshort{fifo} \gls{rQ}.
    88
    9 This work exclusively concentrates on Linux as it's operating system since the existing \CFA runtime and compiler does not already support other operating systems. Furthermore, as \CFA is yet to be released, supporting version of Linux older that the latest version is not a goal of this work.
     9This work exclusively concentrates on Linux as it's operating system since the existing \CFA runtime and compiler does not already support other operating systems. Furthermore, as \CFA is yet to be released, supporting version of Linux older than the latest version is not a goal of this work.
  • doc/theses/thierry_delisle_PhD/thesis/text/io.tex

    r342af53 r8e4aa05  
    1 \chapter{User Level \glsxtrshort{io}}
    2 As mentionned in Section~\ref{prev:io}, User-Level \glsxtrshort{io} requires multiplexing the \glsxtrshort{io} operations of many \glspl{thrd} onto fewer \glspl{proc} using asynchronous \glsxtrshort{io} operations. Various operating systems offer various forms of asynchronous operations and as mentioned in Chapter~\ref{intro}, this work is exclusively focuesd on Linux.
     1\chapter{User Level \io}
     2As mentioned in Section~\ref{prev:io}, User-Level \io requires multiplexing the \io operations of many \glspl{thrd} onto fewer \glspl{proc} using asynchronous \io operations. Different operating systems offer various forms of asynchronous operations and as mentioned in Chapter~\ref{intro}, this work is exclusively focused on the Linux operating-system.
    33
    4 \section{Existing options}
    5 Since \glsxtrshort{io} operations are generally handled by the
     4\section{Kernel Interface}
     5Since this work fundamentally depends on operating-system support, the first step of any design is to discuss the available interfaces and pick one (or more) as the foundations of the non-blocking \io subsystem.
    66
    7 \subsection{\lstinline|epoll|, \lstinline|poll| and \lstinline|select|}
     7\subsection{\lstinline{O_NONBLOCK}}
     8In Linux, files can be opened with the flag @O_NONBLOCK@~\cite{MAN:open} (or @SO_NONBLOCK@~\cite{MAN:accept}, the equivalent for sockets) to use the file descriptors in ``nonblocking mode''. In this mode, ``Neither the @open()@ nor any subsequent \io operations on the [opened file descriptor] will cause the calling
     9process to wait''~\cite{MAN:open}. This feature can be used as the foundation for the non-blocking \io subsystem. However, for the subsystem to know when an \io operation completes, @O_NONBLOCK@ must be use in conjunction with a system call that monitors when a file descriptor becomes ready, \ie, the next \io operation on it does not cause the process to wait\footnote{In this context, ready means \emph{some} operation can be performed without blocking. It does not mean an operation returning \lstinline{EAGAIN} succeeds on the next try. For example, a ready read may only return a subset of bytes and the read must be issues again for the remaining bytes, at which point it may return \lstinline{EAGAIN}.}.
     10This mechanism is also crucial in determining when all \glspl{thrd} are blocked and the application \glspl{kthrd} can now block.
    811
    9 \subsection{Linux's AIO}
     12There are three options to monitor file descriptors in Linux\footnote{For simplicity, this section omits \lstinline{pselect} and \lstinline{ppoll}. The difference between these system calls and \lstinline{select} and \lstinline{poll}, respectively, is not relevant for this discussion.}, @select@~\cite{MAN:select}, @poll@~\cite{MAN:poll} and @epoll@~\cite{MAN:epoll}. All three of these options offer a system call that blocks a \gls{kthrd} until at least one of many file descriptors becomes ready. The group of file descriptors being waited is called the \newterm{interest set}.
    1013
     14\paragraph{\lstinline{select}} is the oldest of these options, it takes as an input a contiguous array of bits, where each bits represent a file descriptor of interest. On return, it modifies the set in place to identify which of the file descriptors changed status. This destructive change means that calling select in a loop requires re-initializing the array each time and the number of file descriptors supported has a hard limit. Another limit of @select@ is that once the call is started, the interest set can no longer be modified. Monitoring a new file descriptor generally requires aborting any in progress call to @select@\footnote{Starting a new call to \lstinline{select} is possible but requires a distinct kernel thread, and as a result is not an acceptable multiplexing solution when the interest set is large and highly dynamic unless the number of parallel calls to \lstinline{select} can be strictly bounded.}.
    1115
     16\paragraph{\lstinline{poll}} is an improvement over select, which removes the hard limit on the number of file descriptors and the need to re-initialize the input on every call. It works using an array of structures as an input rather than an array of bits, thus allowing a more compact input for small interest sets. Like @select@, @poll@ suffers from the limitation that the interest set cannot be changed while the call is blocked.
     17
     18\paragraph{\lstinline{epoll}} further improves these two functions by allowing the interest set to be dynamically added to and removed from while a \gls{kthrd} is blocked on an @epoll@ call. This dynamic capability is accomplished by creating an \emph{epoll instance} with a persistent interest set, which is used across multiple calls. This capability significantly reduces synchronization overhead on the part of the caller (in this case the \io subsystem), since the interest set can be modified when adding or removing file descriptors without having to synchronize with other \glspl{kthrd} potentially calling @epoll@.
     19
     20However, all three of these system calls have limitations. The @man@ page for @O_NONBLOCK@ mentions that ``[@O_NONBLOCK@] has no effect for regular files and block devices'', which means none of these three system calls are viable multiplexing strategies for these types of \io operations. Furthermore, @epoll@ has been shown to have problems with pipes and ttys~\cit{Peter's examples in some fashion}. Finally, none of these are useful solutions for multiplexing \io operations that do not have a corresponding file descriptor and can be awkward for operations using multiple file descriptors.
     21
     22\subsection{POSIX asynchronous I/O (AIO)}
     23An alternative to @O_NONBLOCK@ is the AIO interface. Its interface lets programmers enqueue operations to be performed asynchronously by the kernel. Completions of these operations can be communicated in various ways: either by spawning a new \gls{kthrd}, sending a Linux signal, or by polling for completion of one or more operation. For this work, spawning a new \gls{kthrd} is counter-productive but a related solution is discussed in Section~\ref{io:morethreads}. Using interrupts handlers can also lead to fairly complicated interactions between subsystems. Leaving polling for completion, which is similar to the previous system calls. While AIO only supports read and write operations to file descriptors, it does not have the same limitation as @O_NONBLOCK@, \ie, the file descriptors can be regular files and blocked devices. It also supports batching multiple operations in a single system call.
     24
     25AIO offers two different approach to polling: @aio_error@ can be used as a spinning form of polling, returning @EINPROGRESS@ until the operation is completed, and @aio_suspend@ can be used similarly to @select@, @poll@ or @epoll@, to wait until one or more requests have completed. For the purpose of \io multiplexing, @aio_suspend@ is the best interface. However, even if AIO requests can be submitted concurrently, @aio_suspend@ suffers from the same limitation as @select@ and @poll@, \ie, the interest set cannot be dynamically changed while a call to @aio_suspend@ is in progress. AIO also suffers from the limitation of specifying which requests have completed, \ie programmers have to poll each request in the interest set using @aio_error@ to identify the completed requests. This limitation means that, like @select@ and @poll@ but not @epoll@, the time needed to examine polling results increases based on the total number of requests monitored, not the number of completed requests.
     26Finally, AIO does not seem to be a popular interface, which I believe is due in part to this poor polling interface. Linus Torvalds talks about this interface as follows:
    1227
    1328\begin{displayquote}
    14         AIO is a horrible ad-hoc design, with the main excuse being "other,
     29        AIO is a horrible ad-hoc design, with the main excuse being ``other,
    1530        less gifted people, made that design, and we are implementing it for
    1631        compatibility because database people - who seldom have any shred of
    17         taste - actually use it".
     32        taste - actually use it''.
    1833
    1934        But AIO was always really really ugly.
     
    2439\end{displayquote}
    2540
    26 Interestingly, in this e-mail answer, Linus goes on to describe
     41Interestingly, in this e-mail, Linus goes on to describe
    2742``a true \textit{asynchronous system call} interface''
    2843that does
     
    3045in
    3146``some kind of arbitrary \textit{queue up asynchronous system call} model''.
    32 This description is actually quite close to the interface of the interface described in the next section.
     47This description is actually quite close to the interface described in the next section.
    3348
    34 \subsection{\texttt{io\_uring}}
    35 A very recent addition to Linux, @io_uring@\cit{io\_uring} is a framework that aims to solve many of the problems listed with the above mentioned solutions.
     49\subsection{\lstinline{io_uring}}
     50A very recent addition to Linux, @io_uring@~\cite{MAN:io_uring}, is a framework that aims to solve many of the problems listed in the above interfaces. Like AIO, it represents \io operations as entries added to a queue. But like @epoll@, new requests can be submitted while a blocking call waiting for requests to complete is already in progress. The @io_uring@ interface uses two ring buffers (referred to simply as rings) at its core: a submit ring to which programmers push \io requests and a completion ring from which programmers poll for completion.
     51
     52One of the big advantages over the prior interfaces is that @io_uring@ also supports a much wider range of operations. In addition to supporting reads and writes to any file descriptor like AIO, it supports other operations like @open@, @close@, @fsync@, @accept@, @connect@, @send@, @recv@, @splice@, \etc.
     53
     54On top of these, @io_uring@ adds many extras like avoiding copies between the kernel and user-space using shared memory, allowing different mechanisms to communicate with device drivers, and supporting chains of requests, \ie, requests that automatically trigger followup requests on completion.
    3655
    3756\subsection{Extra Kernel Threads}\label{io:morethreads}
    38 Finally, if the operating system does not offer any satisfying forms of asynchronous \glsxtrshort{io} operations, a solution is to fake it by creating a pool of \glspl{kthrd} and delegating operations to them in order to avoid blocking \glspl{proc}.
     57Finally, if the operating system does not offer a satisfactory form of asynchronous \io operations, an ad-hoc solution is to create a pool of \glspl{kthrd} and delegate operations to it to avoid blocking \glspl{proc}, which is a compromise for multiplexing. In the worst case, where all \glspl{thrd} are consistently blocking on \io, it devolves into 1-to-1 threading. However, regardless of the frequency of \io operations, it achieves the fundamental goal of not blocking \glspl{proc} when \glspl{thrd} are ready to run. This approach is used by languages like Go\cit{Go} and frameworks like libuv\cit{libuv}, since it has the advantage that it can easily be used across multiple operating systems. This advantage is especially relevant for languages like Go, which offer a homogeneous \glsxtrshort{api} across all platforms. As opposed to C, which has a very limited standard api for \io, \eg, the C standard library has no networking.
    3958
    4059\subsection{Discussion}
     60These options effectively fall into two broad camps: waiting for \io to be ready versus waiting for \io to complete. All operating systems that support asynchronous \io must offer an interface along one of these lines, but the details vary drastically. For example, Free BSD offers @kqueue@~\cite{MAN:bsd/kqueue}, which behaves similarly to @epoll@, but with some small quality of use improvements, while Windows (Win32)~\cit{https://docs.microsoft.com/en-us/windows/win32/fileio/synchronous-and-asynchronous-i-o} offers ``overlapped I/O'', which handles submissions similarly to @O_NONBLOCK@ with extra flags on the synchronous system call, but waits for completion events, similarly to @io_uring@.
    4161
     62For this project, I selected @io_uring@, in large parts because to its generality. While @epoll@ has been shown to be a good solution for socket \io (\cite{DBLP:journals/pomacs/KarstenB20}), @io_uring@'s transparent support for files, pipes, and more complex operations, like @splice@ and @tee@, make it a better choice as the foundation for a general \io subsystem.
    4263
    4364\section{Event-Engine}
     65An event engine's responsibility is to use the kernel interface to multiplex many \io operations onto few \glspl{kthrd}. In concrete terms, this means \glspl{thrd} enter the engine through an interface, the event engines then starts the operation and parks the calling \glspl{thrd}, returning control to the \gls{proc}. The parked \glspl{thrd} are then rescheduled by the event engine once the desired operation has completed.
     66
     67\subsection{\lstinline{io_uring} in depth}
     68Before going into details on the design of my event engine, more details on @io_uring@ usage are presented, each important in the design of the engine.
     69Figure~\ref{fig:iouring} shows an overview of an @io_uring@ instance.
     70Two ring buffers are used to communicate with the kernel: one for submissions~(left) and one for completions~(right).
     71The submission ring contains entries, \newterm{Submit Queue Entries} (SQE), produced (appended) by the application when an operation starts and then consumed by the kernel.
     72The completion ring contains entries, \newterm{Completion Queue Entries} (CQE), produced (appended) by the kernel when an operation completes and then consumed by the application.
     73The submission ring contains indexes into the SQE array (denoted \emph{S}) containing entries describing the I/O operation to start;
     74the completion ring contains entries for the completed I/O operation.
     75Multiple @io_uring@ instances can be created, in which case they each have a copy of the data structures in the figure.
     76
     77\begin{figure}
     78        \centering
     79        \input{io_uring.pstex_t}
     80        \caption{Overview of \lstinline{io_uring}}
     81%       \caption[Overview of \lstinline{io_uring}]{Overview of \lstinline{io_uring} \smallskip\newline Two ring buffer are used to communicate with the kernel, one for completions~(right) and one for submissions~(left). The completion ring contains entries, \newterm{CQE}s: Completion Queue Entries, that are produced by the kernel when an operation completes and then consumed by the application. On the other hand, the application produces \newterm{SQE}s: Submit Queue Entries, which it appends to the submission ring for the kernel to consume. Unlike the completion ring, the submission ring does not contain the entries directly, it indexes into the SQE array (denoted \emph{S}) instead.}
     82        \label{fig:iouring}
     83\end{figure}
     84
     85New \io operations are submitted to the kernel following 4 steps, which use the components shown in the figure.
     86\begin{enumerate}
     87\item
     88An SQE is allocated from the pre-allocated array (denoted \emph{S} in Figure~\ref{fig:iouring}). This array is created at the same time as the @io_uring@ instance, is in kernel-locked memory visible by both the kernel and the application, and has a fixed size determined at creation. How these entries are allocated is not important for the functioning of @io_uring@, the only requirement is that no entry is reused before the kernel has consumed it.
     89\item
     90The SQE is filled according to the desired operation. This step is straight forward, the only detail worth mentioning is that SQEs have a @user_data@ field that must be filled in order to match submission and completion entries.
     91\item
     92The SQE is submitted to the submission ring by appending the index of the SQE to the ring following regular ring buffer steps: \lstinline{buffer[head] = item; head++}. Since the head is visible to the kernel, some memory barriers may be required to prevent the compiler from reordering these operations. Since the submission ring is a regular ring buffer, more than one SQE can be added at once and the head is updated only after all entries are updated.
     93\item
     94The kernel is notified of the change to the ring using the system call @io_uring_enter@. The number of elements appended to the submission ring is passed as a parameter and the number of elements consumed is returned. The @io_uring@ instance can be constructed so this step is not required, but this requires elevated privilege.% and an early version of @io_uring@ had additional restrictions.
     95\end{enumerate}
     96
     97\begin{sloppypar}
     98The completion side is simpler: applications call @io_uring_enter@ with the flag @IORING_ENTER_GETEVENTS@ to wait on a desired number of operations to complete. The same call can be used to both submit SQEs and wait for operations to complete. When operations do complete, the kernel appends a CQE to the completion ring and advances the head of the ring. Each CQE contains the result of the operation as well as a copy of the @user_data@ field of the SQE that triggered the operation. It is not necessary to call @io_uring_enter@ to get new events because the kernel can directly modify the completion ring. The system call is only needed if the application wants to block waiting for operations to complete.
     99\end{sloppypar}
     100
     101The @io_uring_enter@ system call is protected by a lock inside the kernel. This protection means that concurrent call to @io_uring_enter@ using the same instance are possible, but there is no performance gained from parallel calls to @io_uring_enter@. It is possible to do the first three submission steps in parallel, however, doing so requires careful synchronization.
     102
     103@io_uring@ also introduces constraints on the number of simultaneous operations that can be ``in flight''. Obviously, SQEs are allocated from a fixed-size array, meaning that there is a hard limit to how many SQEs can be submitted at once. In addition, the @io_uring_enter@ system call can fail because ``The  kernel [...] ran out of resources to handle [a request]'' or ``The application is attempting to overcommit the number of requests it can  have  pending.''. This restriction means \io request bursts may have to be subdivided and submitted in chunks at a later time.
     104
     105\subsection{Multiplexing \io: Submission}
     106The submission side is the most complicated aspect of @io_uring@ and its design largely dictates the completion side.
     107
     108While it is possible to do the first steps of submission in parallel, the duration of the system call scales with number of entries submitted. The consequence is that the amount of parallelism used to prepare submissions for the next system call is limited. Beyond this limit, the length of the system call is the throughput limiting factor. I concluded from early experiments that preparing submissions seems to take about as long as the system call itself, which means that with a single @io_uring@ instance, there is no benefit in terms of \io throughput to having more than two \glspl{hthrd}. Therefore the design of the submission engine must manage multiple instances of @io_uring@ running in parallel, effectively sharding @io_uring@ instances. Similarly to scheduling, this sharding can be done privately, \ie, one instance per \glspl{proc}, or in decoupled pools, \ie, a pool of \glspl{proc} use a pool of @io_uring@ instances without one-to-one coupling between any given instance and any given \gls{proc}.
     109
     110\subsubsection{Pool of Instances}
     111One approach is to have multiple shared instances. \Glspl{thrd} attempting \io operations pick one of the available instances and submits operations to that instance. Since the completion will be sent to the same instance, all instances with pending operations must be polled continuously\footnote{As will be described in Chapter~\ref{practice}, this does not translate into constant CPU usage.}. Since there is no coupling between \glspl{proc} and @io_uring@ instances in this approach, \glspl{thrd} running on more than one \gls{proc} can attempt to submit to the same instance concurrently. Since @io_uring@ effectively sets the amount of sharding needed to avoid contention on its internal locks, performance in this approach is based on two aspects: the synchronization needed to submit does not induce more contention than @io_uring@ already does and the scheme to route \io requests to specific @io_uring@ instances does not introduce contention. This second aspect has an oversized importance because it comes into play before the sharding of instances, and as such, all \glspl{hthrd} can contend on the routing algorithm.
     112
     113Allocation in this scheme can be handled fairly easily. Free SQEs, \ie, SQEs that aren't currently being used to represent a request, can be written to safely and have a field called @user_data@ which the kernel only reads to copy to CQEs. Allocation also requires no ordering guarantee as all free SQEs are interchangeable. This requires a simple concurrent bag. The only added complexity is that the number of SQEs is fixed, which means allocation can fail. This failure needs to be pushed up to the routing algorithm, \glspl{thrd} attempting \io operations must not be directed to @io_uring@ instances without any available SQEs. Ideally, the routing algorithm would block operations up-front if none of the instances have available SQEs.
     114
     115Once an SQE is allocated, \glspl{thrd} can fill them normally, they simply need to keep track of the SQE index and which instance it belongs to.
     116
     117Once an SQE is filled in, what needs to happen is that the SQE must be added to the submission ring buffer, an operation that is not thread-safe on itself, and the kernel must be notified using the @io_uring_enter@ system call. The submission ring buffer is the same size as the pre-allocated SQE buffer, therefore pushing to the ring buffer cannot fail\footnote{This is because it is invalid to have the same \lstinline{sqe} multiple times in the ring buffer.}. However, as mentioned, the system call itself can fail with the expectation that it will be retried once some of the already submitted operations complete. Since multiple SQEs can be submitted to the kernel at once, it is important to strike a balance between batching and latency. Operations that are ready to be submitted should be batched together in few system calls, but at the same time, operations should not be left pending for long period of times before being submitted. This can be handled by either designating one of the submitting \glspl{thrd} as the being responsible for the system call for the current batch of SQEs or by having some other party regularly submitting all ready SQEs, \eg, the poller \gls{thrd} mentioned later in this section.
     118
     119In the case of designating a \gls{thrd}, ideally, when multiple \glspl{thrd} attempt to submit operations to the same @io_uring@ instance, all requests would be batched together and one of the \glspl{thrd} would do the system call on behalf of the others, referred to as the \newterm{submitter}. In practice however, it is important that the \io requests are not left pending indefinitely and as such, it may be required to have a current submitter and a next submitter. Indeed, as long as there is a ``next'' submitter, \glspl{thrd} submitting new \io requests can move on, knowing that some future system call will include their request. Once the system call is done, the submitter must also free SQEs so that the allocator can reused them.
     120
     121Finally, the completion side is much simpler since the @io_uring@ system call enforces a natural synchronization point. Polling simply needs to regularly do the system call, go through the produced CQEs and communicate the result back to the originating \glspl{thrd}. Since CQEs only own a signed 32 bit result, in addition to the copy of the @user_data@ field, all that is needed to communicate the result is a simple future~\cite{wiki:future}. If the submission side does not designate submitters, polling can also submit all SQEs as it is polling events.  A simple approach to polling is to allocate a \gls{thrd} per @io_uring@ instance and simply let the poller \glspl{thrd} poll their respective instances when scheduled. This design is especially convenient for reasons explained in Chapter~\ref{practice}.
     122
     123With this pool of instances approach, the big advantage is that it is fairly flexible. It does not impose restrictions on what \glspl{thrd} submitting \io operations can and cannot do between allocations and submissions. It also can gracefully handle running out of resources, SQEs or the kernel returning @EBUSY@. The down side to this is that many of the steps used for submitting need complex synchronization to work properly. The routing and allocation algorithm needs to keep track of which ring instances have available SQEs, block incoming requests if no instance is available, prevent barging if \glspl{thrd} are already queued up waiting for SQEs and handle SQEs being freed. The submission side needs to safely append SQEs to the ring buffer, make sure no SQE is dropped or left pending forever, notify the allocation side when SQEs can be reused and handle the kernel returning @EBUSY@. Sharding the @io_uring@ instances should alleviate much of the contention caused by this, but all this synchronization may still have non-zero cost.
     124
     125\subsubsection{Private Instances}
     126Another approach is to simply create one ring instance per \gls{proc}. This alleviate the need for synchronization on the submissions, requiring only that \glspl{thrd} are not interrupted in between two submission steps. This is effectively the same requirement as using @thread_local@ variables. Since SQEs that are allocated must be submitted to the same ring, on the same \gls{proc}, this effectively forces the application to submit SQEs in allocation order\footnote{The actual requirement is that \glspl{thrd} cannot context switch between allocation and submission. This requirement means that from the subsystem's point of view, the allocation and submission are sequential. To remove this requirement, a \gls{thrd} would need the ability to ``yield to a specific \gls{proc}'', \ie, park with the promise that it will be run next on a specific \gls{proc}, the \gls{proc} attached to the correct ring. This is not a current or planned feature of \CFA.}, greatly simplifying both allocation and submission. In this design, allocation and submission form a ring partitioned ring buffer as shown in Figure~\ref{fig:pring}. Once added to the ring buffer, the attached \gls{proc} has a significant amount of flexibility with regards to when to do the system call. Possible options are: when the \gls{proc} runs out of \glspl{thrd} to run, after running a given number of threads \glspl{thrd}, etc.
     127
     128\begin{figure}
     129        \centering
     130        \input{pivot_ring.pstex_t}
     131        \caption[Partitioned ring buffer]{Partitioned ring buffer \smallskip\newline Allocated sqes are appending to the first partition. When submitting, the partition is simply advanced to include all the sqes that should be submitted. The kernel considers the partition as the head of the ring.}
     132        \label{fig:pring}
     133\end{figure}
     134
     135This approach has the advantage that it does not require much of the synchronization needed in the shared approach. This comes at the cost that \glspl{thrd} submitting \io operations have less flexibility, they cannot park or yield, and several exceptional cases are handled poorly. Instances running out of SQEs cannot run \glspl{thrd} wanting to do \io operations, in such a case the \gls{thrd} needs to be moved to a different \gls{proc}, the only current way of achieving this would be to @yield()@ hoping to be scheduled on a different \gls{proc}, which is not guaranteed. Another problematic case is that \glspl{thrd} that do not park for long periods of time will delay the submission of any SQE not already submitted. This issue is similar to fairness issues which schedulers that use work-stealing mentioned in the previous chapter.
     136
    44137
    45138
    46139\section{Interface}
     140Finally, the last important part of the \io subsystem is it's interface. There are multiple approaches that can be offered to programmers, each with advantages and disadvantages. The new \io subsystem can replace the C runtime's API or extend it. And in the later case the interface can go from very similar to vastly different. The following sections discuss some useful options using @read@ as an example. The standard Linux interface for C is :
     141
     142@ssize_t read(int fd, void *buf, size_t count);@.
     143
     144\subsection{Replacement}
     145Replacing the C \glsxtrshort{api}
     146
     147\subsection{Synchronous Extension}
     148
     149\subsection{Asynchronous Extension}
     150
     151\subsection{Interface directly to \lstinline{io_uring}}
  • doc/theses/thierry_delisle_PhD/thesis/text/runtime.tex

    r342af53 r8e4aa05  
    1111
    1212\section{Clusters}
    13 \CFA allows the option to group user-level threading, in the form of clusters. Both \glspl{thrd} and \glspl{proc} belong to a specific cluster. \Glspl{thrd} are only be scheduled onto \glspl{proc} in the same cluster and scheduling is done independently of other clusters. Figure~\ref{fig:system} shows an overview of the \CFA runtime, which allows programmers to tightly control parallelism. It also opens the door to handling effects like NUMA, by pining clusters to a specific NUMA node\footnote{This is not currently implemented in \CFA, but the only hurdle left is creating a generic interface for cpu masks.}.
     13\CFA allows the option to group user-level threading, in the form of clusters. Both \glspl{thrd} and \glspl{proc} belong to a specific cluster. \Glspl{thrd} are only scheduled onto \glspl{proc} in the same cluster and scheduling is done independently of other clusters. Figure~\ref{fig:system} shows an overview of the \CFA runtime, which allows programmers to tightly control parallelism. It also opens the door to handling effects like NUMA, by pining clusters to a specific NUMA node\footnote{This is not currently implemented in \CFA, but the only hurdle left is creating a generic interface for cpu masks.}.
    1414
    1515\begin{figure}
     
    2525
    2626\section{\glsxtrshort{io}}\label{prev:io}
    27 Prior to this work, the \CFA runtime did not add any particular support for \glsxtrshort{io} operations. %\CFA being built on C, this means that,
    28 While all I/O operations available in C are available in \CFA, \glsxtrshort{io} operations are designed for the POSIX threading model~\cite{pthreads}. Using these 1:1 threading operations in an M:N threading model means I/O operations block \glspl{proc} instead of \glspl{thrd}. While this can work in certain cases, it limits the number of concurrent operations to the number of \glspl{proc} rather than \glspl{thrd}. It also means deadlock can occur because all \glspl{proc} are blocked even if at least one \gls{thrd} is ready to run. A simple example of this type of deadlock would be as follows:
     27Prior to this work, the \CFA runtime did not add any particular support for \glsxtrshort{io} operations. While all \glsxtrshort{io} operations available in C are available in \CFA, \glsxtrshort{io} operations are designed for the POSIX threading model~\cite{pthreads}. Using these 1:1 threading operations in an M:N threading model means \glsxtrshort{io} operations block \glspl{proc} instead of \glspl{thrd}. While this can work in certain cases, it limits the number of concurrent operations to the number of \glspl{proc} rather than \glspl{thrd}. It also means deadlock can occur because all \glspl{proc} are blocked even if at least one \gls{thrd} is ready to run. A simple example of this type of deadlock would be as follows:
     28
    2929\begin{quote}
    3030Given a simple network program with 2 \glspl{thrd} and a single \gls{proc}, one \gls{thrd} sends network requests to a server and the other \gls{thrd} waits for a response from the server. If the second \gls{thrd} races ahead, it may wait for responses to requests that have not been sent yet. In theory, this should not be a problem, even if the second \gls{thrd} waits, because the first \gls{thrd} is still ready to run and should be able to get CPU time to send the request. With M:N threading, while the first \gls{thrd} is ready, the lone \gls{proc} \emph{cannot} run the first \gls{thrd} if it is blocked in the \glsxtrshort{io} operation of the second \gls{thrd}. If this happen, the system is in a synchronization deadlock\footnote{In this example, the deadlocked could be resolved if the server sends unprompted messages to the client. However, this solution is not general and may not be appropriate even in this simple case.}.
    3131\end{quote}
    32 Therefore, one of the objective of this work is to introduce \emph{User-Level \glsxtrshort{io}}, like \glslink{uthrding}{User-Level \emph{Threading}} blocks \glspl{thrd} rather than \glspl{proc} when doing \glsxtrshort{io} operations, which entails multiplexing the \glsxtrshort{io} operations of many \glspl{thrd} onto fewer \glspl{proc}. This multiplexing requires that a single \gls{proc} be able to execute multiple I/O operations in parallel. This requirement cannot be done with operations that block \glspl{proc}, \ie \glspl{kthrd}, since the first operation would prevent starting new operations for its blocking duration. Executing I/O operations in parallel requires \emph{asynchronous} \glsxtrshort{io}, sometimes referred to as \emph{non-blocking}, since the \gls{kthrd} does not block.
    3332
    34 \section{Interoperating with C}
     33Therefore, one of the objective of this work is to introduce \emph{User-Level \glsxtrshort{io}}, like \glslink{uthrding}{User-Level \emph{Threading}} blocks \glspl{thrd} rather than \glspl{proc} when doing \glsxtrshort{io} operations, which entails multiplexing the \glsxtrshort{io} operations of many \glspl{thrd} onto fewer \glspl{proc}. This multiplexing requires that a single \gls{proc} be able to execute multiple \glsxtrshort{io} operations in parallel. This requirement cannot be done with operations that block \glspl{proc}, \ie \glspl{kthrd}, since the first operation would prevent starting new operations for its blocking duration. Executing \glsxtrshort{io} operations in parallel requires \emph{asynchronous} \glsxtrshort{io}, sometimes referred to as \emph{non-blocking}, since the \gls{kthrd} does not block.
     34
     35\section{Interoperating with \texttt{C}}
    3536While \glsxtrshort{io} operations are the classical example of operations that block \glspl{kthrd}, the non-blocking challenge extends to all blocking system-calls. The POSIX standard states~\cite[\S~2.9.1]{POSIX17}:
    3637\begin{quote}
     
    4445\begin{enumerate}
    4546        \item Precisely identifying blocking C calls is difficult.
    46         \item Introducing new code can have a significant impact on general performance.
     47        \item Introducing control points code can have a significant impact on general performance.
    4748\end{enumerate}
    48 Because of these consequences, this work does not attempt to ``sandbox'' calls to C. Therefore, it is possible for an unidentified library calls to block a \gls{kthrd} leading to deadlocks in \CFA's M:N threading model, which would not occur in a traditional 1:1 threading model. Currently, all M:N thread systems interacting with UNIX without sandboxing suffer from this problem but manage to work very well in the majority of applications. Therefore, a complete solution to this problem is outside the scope of this thesis.
     49Because of these consequences, this work does not attempt to ``sandbox'' calls to C. Therefore, it is possible calls from an unidentified library will block a \gls{kthrd} leading to deadlocks in \CFA's M:N threading model, which would not occur in a traditional 1:1 threading model. Currently, all M:N thread systems interacting with UNIX without sandboxing suffer from this problem but manage to work very well in the majority of applications. Therefore, a complete solution to this problem is outside the scope of this thesis.
  • doc/theses/thierry_delisle_PhD/thesis/thesis.tex

    r342af53 r8e4aa05  
    1 % uWaterloo Thesis Template for LaTeX
    2 % Last Updated June 14, 2017 by Stephen Carr, IST Client Services
    3 % FOR ASSISTANCE, please send mail to rt-IST-CSmathsci@ist.uwaterloo.ca
    4 
    5 % Effective October 2006, the University of Waterloo
    6 % requires electronic thesis submission. See the uWaterloo thesis regulations at
     1%======================================================================
     2% University of Waterloo Thesis Template for LaTeX
     3% Last Updated November, 2020
     4% by Stephen Carr, IST Client Services,
     5% University of Waterloo, 200 University Ave. W., Waterloo, Ontario, Canada
     6% FOR ASSISTANCE, please send mail to request@uwaterloo.ca
     7
     8% DISCLAIMER
     9% To the best of our knowledge, this template satisfies the current uWaterloo thesis requirements.
     10% However, it is your responsibility to assure that you have met all requirements of the University and your particular department.
     11
     12% Many thanks for the feedback from many graduates who assisted the development of this template.
     13% Also note that there are explanatory comments and tips throughout this template.
     14%======================================================================
     15% Some important notes on using this template and making it your own...
     16
     17% The University of Waterloo has required electronic thesis submission since October 2006.
     18% See the uWaterloo thesis regulations at
    719% https://uwaterloo.ca/graduate-studies/thesis.
    8 
    9 % DON'T FORGET TO ADD YOUR OWN NAME AND TITLE in the "hyperref" package
    10 % configuration below. THIS INFORMATION GETS EMBEDDED IN THE PDF FINAL PDF DOCUMENT.
    11 % You can view the information if you view Properties of the PDF document.
    12 
    13 % Many faculties/departments also require one or more printed
    14 % copies. This template attempts to satisfy both types of output.
    15 % It is based on the standard "book" document class which provides all necessary
    16 % sectioning structures and allows multi-part theses.
    17 
    18 % DISCLAIMER
    19 % To the best of our knowledge, this template satisfies the current uWaterloo requirements.
    20 % However, it is your responsibility to assure that you have met all
    21 % requirements of the University and your particular department.
    22 % Many thanks for the feedback from many graduates that assisted the development of this template.
    23 
    24 % -----------------------------------------------------------------------
    25 
    26 % By default, output is produced that is geared toward generating a PDF
    27 % version optimized for viewing on an electronic display, including
    28 % hyperlinks within the PDF.
    29 
     20% This thesis template is geared towards generating a PDF version optimized for viewing on an electronic display, including hyperlinks within the PDF.
     21
     22% DON'T FORGET TO ADD YOUR OWN NAME AND TITLE in the "hyperref" package configuration below.
     23% THIS INFORMATION GETS EMBEDDED IN THE PDF FINAL PDF DOCUMENT.
     24% You can view the information if you view properties of the PDF document.
     25
     26% Many faculties/departments also require one or more printed copies.
     27% This template attempts to satisfy both types of output.
     28% See additional notes below.
     29% It is based on the standard "book" document class which provides all necessary sectioning structures and allows multi-part theses.
     30
     31% If you are using this template in Overleaf (cloud-based collaboration service), then it is automatically processed and previewed for you as you edit.
     32
     33% For people who prefer to install their own LaTeX distributions on their own computers, and process the source files manually, the following notes provide the sequence of tasks:
     34 
    3035% E.g. to process a thesis called "mythesis.tex" based on this template, run:
    3136
    3237% pdflatex mythesis     -- first pass of the pdflatex processor
    3338% bibtex mythesis       -- generates bibliography from .bib data file(s)
    34 % makeindex         -- should be run only if an index is used
     39% makeindex         -- should be run only if an index is used 
    3540% pdflatex mythesis     -- fixes numbering in cross-references, bibliographic references, glossaries, index, etc.
    36 % pdflatex mythesis     -- fixes numbering in cross-references, bibliographic references, glossaries, index, etc.
    37 
    38 % If you use the recommended LaTeX editor, Texmaker, you would open the mythesis.tex
    39 % file, then click the PDFLaTeX button. Then run BibTeX (under the Tools menu).
    40 % Then click the PDFLaTeX button two more times. If you have an index as well,
    41 % you'll need to run MakeIndex from the Tools menu as well, before running pdflatex
     41% pdflatex mythesis     -- it takes a couple of passes to completely process all cross-references
     42
     43% If you use the recommended LaTeX editor, Texmaker, you would open the mythesis.tex file, then click the PDFLaTeX button. Then run BibTeX (under the Tools menu).
     44% Then click the PDFLaTeX button two more times.
     45% If you have an index as well,you'll need to run MakeIndex from the Tools menu as well, before running pdflatex
    4246% the last two times.
    4347
    44 % N.B. The "pdftex" program allows graphics in the following formats to be
    45 % included with the "\includegraphics" command: PNG, PDF, JPEG, TIFF
    46 % Tip 1: Generate your figures and photos in the size you want them to appear
    47 % in your thesis, rather than scaling them with \includegraphics options.
    48 % Tip 2: Any drawings you do should be in scalable vector graphic formats:
    49 % SVG, PNG, WMF, EPS and then converted to PNG or PDF, so they are scalable in
    50 % the final PDF as well.
    51 % Tip 3: Photographs should be cropped and compressed so as not to be too large.
    52 
    53 % To create a PDF output that is optimized for double-sided printing:
    54 %
    55 % 1) comment-out the \documentclass statement in the preamble below, and
    56 % un-comment the second \documentclass line.
    57 %
    58 % 2) change the value assigned below to the boolean variable
    59 % "PrintVersion" from "false" to "true".
    60 
    61 % --------------------- Start of Document Preamble -----------------------
    62 
    63 % Specify the document class, default style attributes, and page dimensions
     48% N.B. The "pdftex" program allows graphics in the following formats to be included with the "\includegraphics" command: PNG, PDF, JPEG, TIFF
     49% Tip: Generate your figures and photos in the size you want them to appear in your thesis, rather than scaling them with \includegraphics options.
     50% Tip: Any drawings you do should be in scalable vector graphic formats: SVG, PNG, WMF, EPS and then converted to PNG or PDF, so they are scalable in the final PDF as well.
     51% Tip: Photographs should be cropped and compressed so as not to be too large.
     52
     53% To create a PDF output that is optimized for double-sided printing:
     54% 1) comment-out the \documentclass statement in the preamble below, and un-comment the second \documentclass line.
     55% 2) change the value assigned below to the boolean variable "PrintVersion" from " false" to "true".
     56
     57%======================================================================
     58%   D O C U M E N T   P R E A M B L E
     59% Specify the document class, default style attributes, and page dimensions, etc.
    6460% For hyperlinked PDF, suitable for viewing on a computer, use this:
    6561\documentclass[letterpaper,12pt,titlepage,oneside,final]{book}
    6662
    67 % For PDF, suitable for double-sided printing, change the PrintVersion variable below
    68 % to "true" and use this \documentclass line instead of the one above:
     63% For PDF, suitable for double-sided printing, change the PrintVersion variable below to "true" and use this \documentclass line instead of the one above:
    6964%\documentclass[letterpaper,12pt,titlepage,openright,twoside,final]{book}
    7065
    71 \newcommand{\href}[1]{#1} % does nothing, but defines the command so the
    72     % print-optimized version will ignore \href tags (redefined by hyperref pkg).
     66% Some LaTeX commands I define for my own nomenclature.
     67% If you have to, it's easier to make changes to nomenclature once here than in a million places throughout your thesis!
     68\newcommand{\package}[1]{\textbf{#1}} % package names in bold text
     69\newcommand{\cmmd}[1]{\textbackslash\texttt{#1}} % command name in tt font
     70\newcommand{\href}[1]{#1} % does nothing, but defines the command so the print-optimized version will ignore \href tags (redefined by hyperref pkg).
     71%\newcommand{\texorpdfstring}[2]{#1} % does nothing, but defines the command
     72% Anything defined here may be redefined by packages added below...
    7373
    7474% This package allows if-then-else control structures.
     
    7676\newboolean{PrintVersion}
    7777\setboolean{PrintVersion}{false}
    78 % CHANGE THIS VALUE TO "true" as necessary, to improve printed results for hard copies
    79 % by overriding some options of the hyperref package below.
     78% CHANGE THIS VALUE TO "true" as necessary, to improve printed results for hard copies by overriding some options of the hyperref package, called below.
    8079
    8180%\usepackage{nomencl} % For a nomenclature (optional; available from ctan.org)
    8281\usepackage{amsmath,amssymb,amstext} % Lots of math symbols and environments
     82\usepackage{xcolor}
    8383\usepackage{graphicx} % For including graphics
    8484
    8585% Hyperlinks make it very easy to navigate an electronic document.
    86 % In addition, this is where you should specify the thesis title
    87 % and author as they appear in the properties of the PDF document.
     86% In addition, this is where you should specify the thesis title and author as they appear in the properties of the PDF document.
    8887% Use the "hyperref" package
    8988% N.B. HYPERREF MUST BE THE LAST PACKAGE LOADED; ADD ADDITIONAL PKGS ABOVE
    9089\usepackage[pagebackref=false]{hyperref} % with basic options
    91                 % N.B. pagebackref=true provides links back from the References to the body text. This can cause trouble for printing.
     90%\usepackage[pdftex,pagebackref=true]{hyperref}
     91% N.B. pagebackref=true provides links back from the References to the body text. This can cause trouble for printing.
    9292\hypersetup{
    9393        plainpages=false,       % needed if Roman numbers in frontpages
    94         unicode=false,          % non-Latin characters in Acrobats bookmarks
    95         pdftoolbar=true,        % show Acrobats toolbar?
    96         pdfmenubar=true,        % show Acrobats menu?
     94        unicode=false,          % non-Latin characters in Acrobat's bookmarks
     95        pdftoolbar=true,        % show Acrobat's toolbar?
     96        pdfmenubar=true,        % show Acrobat's menu?
    9797        pdffitwindow=false,     % window fit to page when opened
    9898        pdfstartview={FitH},    % fits the width of the page to the window
     
    110110\ifthenelse{\boolean{PrintVersion}}{   % for improved print quality, change some hyperref options
    111111\hypersetup{    % override some previously defined hyperref options
    112         citecolor=black,
    113         filecolor=black,
    114         linkcolor=black,
     112        citecolor=black,%
     113        filecolor=black,%
     114        linkcolor=black,%
    115115        urlcolor=black
    116116}}{} % end of ifthenelse (no else)
     
    120120% although it's supposed to be in both the TeX Live and MikTeX distributions. There are also documentation and
    121121% installation instructions there.
    122 \renewcommand*{\glstextformat}[1]{\textsf{#1}}
     122\makeatletter
     123\newcommand*{\glsplainhyperlink}[2]{%
     124  \colorlet{currenttext}{.}% store current text color
     125  \colorlet{currentlink}{\@linkcolor}% store current link color
     126  \hypersetup{linkcolor=currenttext}% set link color
     127  \hyperlink{#1}{#2}%
     128  \hypersetup{linkcolor=currentlink}% reset to default
     129}
     130\let\@glslink\glsplainhyperlink
     131\makeatother
    123132
    124133\usepackage{csquotes}
     
    126135
    127136% Setting up the page margins...
    128 \setlength{\textheight}{9in}\setlength{\topmargin}{-0.45in}\setlength{\headsep}{0.25in}
     137\setlength{\textheight}{9in}
     138\setlength{\topmargin}{-0.45in}
     139\setlength{\headsep}{0.25in}
    129140% uWaterloo thesis requirements specify a minimum of 1 inch (72pt) margin at the
    130 % top, bottom, and outside page edges and a 1.125 in. (81pt) gutter
    131 % margin (on binding side). While this is not an issue for electronic
    132 % viewing, a PDF may be printed, and so we have the same page layout for
    133 % both printed and electronic versions, we leave the gutter margin in.
     141% top, bottom, and outside page edges and a 1.125 in. (81pt) gutter margin (on binding side).
     142% While this is not an issue for electronic viewing, a PDF may be printed, and so we have the same page layout for both printed and electronic versions, we leave the gutter margin in.
    134143% Set margins to minimum permitted by uWaterloo thesis regulations:
    135144\setlength{\marginparwidth}{0pt} % width of margin notes
     
    140149\setlength{\evensidemargin}{0.125in} % Adds 1/8 in. to binding side of all
    141150% even-numbered pages when the "twoside" printing option is selected
    142 \setlength{\oddsidemargin}{0.125in} % Adds 1/8 in. to the left of all pages
    143 % when "oneside" printing is selected, and to the left of all odd-numbered
    144 % pages when "twoside" printing is selected
    145 \setlength{\textwidth}{6.375in} % assuming US letter paper (8.5 in. x 11 in.) and
    146 % side margins as above
     151\setlength{\oddsidemargin}{0.125in} % Adds 1/8 in. to the left of all pages when "oneside" printing is selected, and to the left of all odd-numbered pages when "twoside" printing is selected
     152\setlength{\textwidth}{6.375in} % assuming US letter paper (8.5 in. x 11 in.) and side margins as above
    147153\raggedbottom
    148154
    149 % The following statement specifies the amount of space between
    150 % paragraphs. Other reasonable specifications are \bigskipamount and \smallskipamount.
     155% The following statement specifies the amount of space between paragraphs. Other reasonable specifications are \bigskipamount and \smallskipamount.
    151156\setlength{\parskip}{\medskipamount}
    152157
    153 % The following statement controls the line spacing.  The default
    154 % spacing corresponds to good typographic conventions and only slight
    155 % changes (e.g., perhaps "1.2"), if any, should be made.
     158% The following statement controls the line spacing.
     159% The default spacing corresponds to good typographic conventions and only slight changes (e.g., perhaps "1.2"), if any, should be made.
    156160\renewcommand{\baselinestretch}{1} % this is the default line space setting
    157161
    158 % By default, each chapter will start on a recto (right-hand side)
    159 % page.  We also force each section of the front pages to start on
    160 % a recto page by inserting \cleardoublepage commands.
    161 % In many cases, this will require that the verso page be
    162 % blank and, while it should be counted, a page number should not be
    163 % printed.  The following statements ensure a page number is not
    164 % printed on an otherwise blank verso page.
     162% By default, each chapter will start on a recto (right-hand side) page.
     163% We also force each section of the front pages to start on a recto page by inserting \cleardoublepage commands.
     164% In many cases, this will require that the verso (left-hand) page be blank, and while it should be counted, a page number should not be printed.
     165% The following statements ensure a page number is not printed on an otherwise blank verso page.
    165166\let\origdoublepage\cleardoublepage
    166167\newcommand{\clearemptydoublepage}{%
     
    194195\input{common}
    195196\CFAStyle                                               % CFA code-style for all languages
    196 \lstset{basicstyle=\linespread{0.9}\tt}
     197\lstset{language=CFA,basicstyle=\linespread{0.9}\tt}    % CFA default language
    197198
    198199% glossary of terms to use
     
    200201\makeindex
    201202
    202 %======================================================================
    203 %   L O G I C A L    D O C U M E N T -- the content of your thesis
     203\newcommand\io{\glsxtrshort{io}\xspace}%
     204
     205%======================================================================
     206%   L O G I C A L    D O C U M E N T
     207% The logical document contains the main content of your thesis.
     208% Being a large document, it is a good idea to divide your thesis into several files, each one containing one chapter or other significant chunk of content, so you can easily shuffle things around later if desired.
    204209%======================================================================
    205210\begin{document}
    206211
    207 % For a large document, it is a good idea to divide your thesis
    208 % into several files, each one containing one chapter.
    209 % To illustrate this idea, the "front pages" (i.e., title page,
    210 % declaration, borrowers' page, abstract, acknowledgements,
    211 % dedication, table of contents, list of tables, list of figures,
    212 % nomenclature) are contained within the file "uw-ethesis-frontpgs.tex" which is
    213 % included into the document by the following statement.
    214212%----------------------------------------------------------------------
    215213% FRONT MATERIAL
     214% title page,declaration, borrowers' page, abstract, acknowledgements,
     215% dedication, table of contents, list of tables, list of figures, nomenclature, etc.
    216216%----------------------------------------------------------------------
    217217\input{text/front.tex}
    218218
    219 
    220219%----------------------------------------------------------------------
    221220% MAIN BODY
    222 %----------------------------------------------------------------------
    223 % Because this is a short document, and to reduce the number of files
    224 % needed for this template, the chapters are not separate
    225 % documents as suggested above, but you get the idea. If they were
    226 % separate documents, they would each start with the \chapter command, i.e,
    227 % do not contain \documentclass or \begin{document} and \end{document} commands.
     221% We suggest using a separate file for each chapter of your thesis.
     222% Start each chapter file with the \chapter command.
     223% Only use \documentclass or \begin{document} and \end{document} commands in this master document.
     224% Tip: Putting each sentence on a new line is a way to simplify later editing.
     225%----------------------------------------------------------------------
     226
    228227\part{Introduction}
    229228\input{text/intro.tex}
     
    232231\part{Design}
    233232\input{text/core.tex}
     233\input{text/io.tex}
    234234\input{text/practice.tex}
    235 \input{text/io.tex}
    236235\part{Evaluation}
    237236\label{Evaluation}
     
    243242%----------------------------------------------------------------------
    244243% END MATERIAL
    245 %----------------------------------------------------------------------
    246 
    247 % B I B L I O G R A P H Y
    248 % -----------------------
    249 
    250 % The following statement selects the style to use for references.  It controls the sort order of the entries in the bibliography and also the formatting for the in-text labels.
     244% Bibliography, Appendices, Index, etc.
     245%----------------------------------------------------------------------
     246
     247% Bibliography
     248
     249% The following statement selects the style to use for references.
     250% It controls the sort order of the entries in the bibliography and also the formatting for the in-text labels.
    251251\bibliographystyle{plain}
    252252% This specifies the location of the file containing the bibliographic information.
    253 % It assumes you're using BibTeX (if not, why not?).
    254 \cleardoublepage % This is needed if the book class is used, to place the anchor in the correct page,
    255                  % because the bibliography will start on its own page.
    256                  % Use \clearpage instead if the document class uses the "oneside" argument
     253% It assumes you're using BibTeX to manage your references (if not, why not?).
     254\cleardoublepage % This is needed if the "book" document class is used, to place the anchor in the correct page, because the bibliography will start on its own page.
     255% Use \clearpage instead if the document class uses the "oneside" argument
    257256\phantomsection  % With hyperref package, enables hyperlinking from the table of contents to bibliography
    258257% The following statement causes the title "References" to be used for the bibliography section:
     
    263262
    264263\bibliography{local,pl}
    265 % Tip 5: You can create multiple .bib files to organize your references.
     264% Tip: You can create multiple .bib files to organize your references.
    266265% Just list them all in the \bibliogaphy command, separated by commas (no spaces).
    267266
    268 % % The following statement causes the specified references to be added to the bibliography% even if they were not
    269 % % cited in the text. The asterisk is a wildcard that causes all entries in the bibliographic database to be included (optional).
     267% The following statement causes the specified references to be added to the bibliography even if they were not cited in the text.
     268% The asterisk is a wildcard that causes all entries in the bibliographic database to be included (optional).
    270269% \nocite{*}
     270%----------------------------------------------------------------------
     271
     272% Appendices
    271273
    272274% The \appendix statement indicates the beginning of the appendices.
    273275\appendix
    274 % Add a title page before the appendices and a line in the Table of Contents
     276% Add an un-numbered title page before the appendices and a line in the Table of Contents
    275277\chapter*{APPENDICES}
    276278\addcontentsline{toc}{chapter}{APPENDICES}
     279% Appendices are just more chapters, with different labeling (letters instead of numbers).
    277280%======================================================================
    278281\chapter[PDF Plots From Matlab]{Matlab Code for Making a PDF Plot}
     
    312315%\input{thesis.ind}                             % index
    313316
    314 \phantomsection
    315 
    316 \end{document}
     317\phantomsection         % allows hyperref to link to the correct page
     318
     319%----------------------------------------------------------------------
     320\end{document} % end of logical document
  • doc/user/figures/Cdecl.fig

    r342af53 r8e4aa05  
    19192 2 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 5
    2020         2850 1200 3600 1200 3600 1350 2850 1350 2850 1200
    21 4 1 0 50 -1 4 10 0.0000 2 120 90 2925 1325 0\001
    22 4 1 0 50 -1 4 10 0.0000 2 120 90 3075 1325 1\001
    23 4 1 0 50 -1 4 10 0.0000 2 120 90 3225 1325 2\001
    24 4 1 0 50 -1 4 10 0.0000 2 120 90 3375 1325 3\001
    25 4 1 0 50 -1 4 10 0.0000 2 120 90 3525 1325 4\001
     214 1 0 50 -1 4 11 0.0000 2 120 90 2925 1325 0\001
     224 1 0 50 -1 4 11 0.0000 2 120 90 3075 1325 1\001
     234 1 0 50 -1 4 11 0.0000 2 120 90 3225 1325 2\001
     244 1 0 50 -1 4 11 0.0000 2 120 90 3375 1325 3\001
     254 1 0 50 -1 4 11 0.0000 2 120 90 3525 1325 4\001
    2626-6
    27272 2 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 5
     
    5555        1 1 1.00 45.00 60.00
    5656         2550 1275 2850 1275
    57 4 1 0 50 -1 4 10 0.0000 2 120 90 1350 1650 0\001
    58 4 1 0 50 -1 4 10 0.0000 2 120 90 1500 1650 1\001
    59 4 1 0 50 -1 4 10 0.0000 2 120 90 1650 1650 2\001
    60 4 1 0 50 -1 4 10 0.0000 2 120 90 1800 1650 3\001
    61 4 1 0 50 -1 4 10 0.0000 2 120 90 1950 1650 4\001
    62 4 1 0 50 -1 4 10 0.0000 2 90 90 1200 1325 x\001
    63 4 1 0 50 -1 4 10 0.0000 2 90 90 2400 1325 x\001
     574 1 0 50 -1 4 11 0.0000 2 120 90 1350 1650 0\001
     584 1 0 50 -1 4 11 0.0000 2 120 90 1500 1650 1\001
     594 1 0 50 -1 4 11 0.0000 2 120 90 1650 1650 2\001
     604 1 0 50 -1 4 11 0.0000 2 120 90 1800 1650 3\001
     614 1 0 50 -1 4 11 0.0000 2 120 90 1950 1650 4\001
     624 1 0 50 -1 4 11 0.0000 2 90 90 1200 1325 x\001
     634 1 0 50 -1 4 11 0.0000 2 90 90 2400 1325 x\001
  • doc/user/user.tex

    r342af53 r8e4aa05  
    1111%% Created On       : Wed Apr  6 14:53:29 2016
    1212%% Last Modified By : Peter A. Buhr
    13 %% Last Modified On : Mon Oct  5 08:57:29 2020
    14 %% Update Count     : 3998
     13%% Last Modified On : Mon Feb 15 13:48:53 2021
     14%% Update Count     : 4452
    1515%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    1616
     
    3737\usepackage{mathptmx}                                   % better math font with "times"
    3838\usepackage[usenames]{color}
     39\usepackage[dvips,plainpages=false,pdfpagelabels,pdfpagemode=UseNone,colorlinks=true,pagebackref=true,linkcolor=blue,citecolor=blue,urlcolor=blue,pagebackref=true,breaklinks=true]{hyperref}
     40\usepackage{breakurl}
     41
     42\renewcommand\footnoterule{\kern -3pt\rule{0.3\linewidth}{0.15pt}\kern 2pt}
     43
     44\usepackage[pagewise]{lineno}
     45\renewcommand{\linenumberfont}{\scriptsize\sffamily}
     46\usepackage[firstpage]{draftwatermark}
     47\SetWatermarkLightness{0.9}
     48
     49% Default underscore is too low and wide. Cannot use lstlisting "literate" as replacing underscore
     50% removes it as a variable-name character so keywords in variables are highlighted. MUST APPEAR
     51% AFTER HYPERREF.
     52\renewcommand{\textunderscore}{\leavevmode\makebox[1.2ex][c]{\rule{1ex}{0.075ex}}}
     53
     54\setlength{\topmargin}{-0.45in}                                                 % move running title into header
     55\setlength{\headsep}{0.25in}
     56
     57%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     58
    3959\newcommand{\CFALatin}{}
    4060% inline code ©...© (copyright symbol) emacs: C-q M-)
     
    4666% math escape $...$ (dollar symbol)
    4767\input{common}                                          % common CFA document macros
    48 \usepackage[dvips,plainpages=false,pdfpagelabels,pdfpagemode=UseNone,colorlinks=true,pagebackref=true,linkcolor=blue,citecolor=blue,urlcolor=blue,pagebackref=true,breaklinks=true]{hyperref}
    49 \usepackage{breakurl}
    50 
    51 \renewcommand\footnoterule{\kern -3pt\rule{0.3\linewidth}{0.15pt}\kern 2pt}
    52 
    53 \usepackage[pagewise]{lineno}
    54 \renewcommand{\linenumberfont}{\scriptsize\sffamily}
    55 \usepackage[firstpage]{draftwatermark}
    56 \SetWatermarkLightness{0.9}
    57 
    58 % Default underscore is too low and wide. Cannot use lstlisting "literate" as replacing underscore
    59 % removes it as a variable-name character so keywords in variables are highlighted. MUST APPEAR
    60 % AFTER HYPERREF.
    61 \renewcommand{\textunderscore}{\leavevmode\makebox[1.2ex][c]{\rule{1ex}{0.075ex}}}
    62 
    63 \setlength{\topmargin}{-0.45in}                                                 % move running title into header
    64 \setlength{\headsep}{0.25in}
    65 
    66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    67 
    6868\CFAStyle                                                                                               % use default CFA format-style
     69\lstset{language=CFA}                                                                   % CFA default lnaguage
    6970\lstnewenvironment{C++}[1][]                            % use C++ style
    70 {\lstset{language=C++,moredelim=**[is][\protect\color{red}]{®}{®},#1}}
     71{\lstset{language=C++,moredelim=**[is][\protect\color{red}]{@}{@},#1}}
    7172{}
    7273
     
    8182\newcommand{\Emph}[2][red]{{\color{#1}\textbf{\emph{#2}}}}
    8283\newcommand{\R}[1]{\Textbf{#1}}
     84\newcommand{\RC}[1]{\Textbf{\LstBasicStyle{#1}}}
    8385\newcommand{\B}[1]{{\Textbf[blue]{#1}}}
    8486\newcommand{\G}[1]{{\Textbf[OliveGreen]{#1}}}
     
    103105
    104106\author{
    105 \huge \CFA Team \medskip \\
    106 \Large Andrew Beach, Richard Bilson, Peter A. Buhr, Thierry Delisle, \smallskip \\
    107 \Large Glen Ditchfield, Rodolfo G. Esteves, Aaron Moss, Rob Schluntz
     107\huge \CFA Team (past and present) \medskip \\
     108\Large Andrew Beach, Richard Bilson, Michael Brooks, Peter A. Buhr, Thierry Delisle, \smallskip \\
     109\Large Glen Ditchfield, Rodolfo G. Esteves, Aaron Moss, Colby Parsons, Rob Schluntz, \smallskip \\
     110\Large Fangren Yu, Mubeen Zulfiqar
    108111}% author
    109112
     
    126129\vspace*{\fill}
    127130\noindent
    128 \copyright\,2016 \CFA Project \\ \\
     131\copyright\,2016, 2018, 2021 \CFA Project \\ \\
    129132\noindent
    130133This work is licensed under the Creative Commons Attribution 4.0 International License.
     
    144147\section{Introduction}
    145148
    146 \CFA{}\index{cforall@\CFA}\footnote{Pronounced ``\Index*{C-for-all}'', and written \CFA, CFA, or \CFL.} is a modern general-purpose programming-language, designed as an evolutionary step forward for the C programming language.
     149\CFA{}\index{cforall@\CFA}\footnote{Pronounced ``\Index*{C-for-all}'', and written \CFA, CFA, or \CFL.} is a modern general-purpose concurrent programming-language, designed as an evolutionary step forward for the C programming language.
    147150The syntax of \CFA builds from C and should look immediately familiar to C/\Index*[C++]{\CC{}} programmers.
    148151% Any language feature that is not described here can be assumed to be using the standard \Celeven syntax.
    149 \CFA adds many modern programming-language features that directly lead to increased \emph{\Index{safety}} and \emph{\Index{productivity}}, while maintaining interoperability with existing C programs and achieving similar performance.
     152\CFA adds many modern features that directly lead to increased \emph{\Index{safety}} and \emph{\Index{productivity}}, while maintaining interoperability with existing C programs and achieving similar performance.
    150153Like C, \CFA is a statically typed, procedural (non-\Index{object-oriented}) language with a low-overhead runtime, meaning there is no global \Index{garbage-collection}, but \Index{regional garbage-collection}\index{garbage-collection!regional} is possible.
    151154The primary new features include polymorphic routines and types, exceptions, concurrency, and modules.
     
    157160instead, a programmer evolves a legacy program into \CFA by incrementally incorporating \CFA features.
    158161As well, new programs can be written in \CFA using a combination of C and \CFA features.
     162In many ways, \CFA is to C as \Index{Scala}~\cite{Scala} is to Java, providing a vehicle for new typing and control-flow capabilities on top of a highly popular programming language allowing immediate dissemination.
    159163
    160164\Index*[C++]{\CC{}}~\cite{c++:v1} had a similar goal 30 years ago, allowing object-oriented programming to be incrementally added to C.
     
    165169For example, the following programs compare the C, \CFA, and \CC I/O mechanisms, where the programs output the same result.
    166170\begin{center}
    167 \begin{tabular}{@{}l@{\hspace{1.5em}}l@{\hspace{1.5em}}l@{}}
    168 \multicolumn{1}{c@{\hspace{1.5em}}}{\textbf{C}} & \multicolumn{1}{c}{\textbf{\CFA}}     & \multicolumn{1}{c}{\textbf{\CC}}      \\
    169 \begin{cfa}
    170 #include <stdio.h>§\indexc{stdio.h}§
     171\begin{tabular}{@{}l@{\hspace{1em}}l@{\hspace{1em}}l@{}}
     172\multicolumn{1}{c@{\hspace{1em}}}{\textbf{C}}   & \multicolumn{1}{c}{\textbf{\CFA}}     & \multicolumn{1}{c}{\textbf{\CC}}      \\
     173\begin{cfa}
     174#include <stdio.h>$\indexc{stdio.h}$
    171175
    172176int main( void ) {
    173177        int x = 0, y = 1, z = 2;
    174         ®printf( "%d %d %d\n", x, y, z );®
     178        @printf( "%d %d %d\n", x, y, z );@
    175179}
    176180\end{cfa}
    177181&
    178182\begin{cfa}
    179 #include <fstream>§\indexc{fstream}§
     183#include <fstream>$\indexc{fstream}$
    180184
    181185int main( void ) {
    182186        int x = 0, y = 1, z = 2;
    183         ®sout | x | y | z;®§\indexc{sout}§
     187        @sout | x | y | z;@$\indexc{sout}$
    184188}
    185189\end{cfa}
    186190&
    187191\begin{cfa}
    188 #include <iostream>§\indexc{iostream}§
     192#include <iostream>$\indexc{iostream}$
    189193using namespace std;
    190194int main() {
    191195        int x = 0, y = 1, z = 2;
    192         ®cout<<x<<" "<<y<<" "<<z<<endl;®
     196        @cout<<x<<" "<<y<<" "<<z<<endl;@
    193197}
    194198\end{cfa}
    195199\end{tabular}
    196200\end{center}
    197 While the \CFA I/O looks similar to the \Index*[C++]{\CC{}} output style, there are important differences, such as automatic spacing between variables as in \Index*{Python} (see~\VRef{s:IOLibrary}).
     201While \CFA I/O \see{\VRef{s:StreamIOLibrary}} looks similar to \Index*[C++]{\CC{}}, there are important differences, such as automatic spacing between variables and an implicit newline at the end of the expression list, similar to \Index*{Python}~\cite{Python}.
    198202
    199203
     
    210214\section{Why fix C?}
    211215
    212 The C programming language is a foundational technology for modern computing with millions of lines of code implementing everything from hobby projects to commercial operating-systems.
     216The C programming language is a foundational technology for modern computing with billions of lines of code implementing everything from hobby projects to commercial operating-systems.
    213217This installation base and the programmers producing it represent a massive software-engineering investment spanning decades and likely to continue for decades more.
    214218Even with all its problems, C continues to be popular because it allows writing software at virtually any level in a computer system without restriction.
    215 For system programming, where direct access to hardware, storage management, and real-time issues are a requirement, C is usually the only language of choice.
    216 The TIOBE index~\cite{TIOBE} for February 2020 ranks the top six most \emph{popular} programming languages as \Index*{Java} 17.4\%, C 16.8\%, Python 9.3\%, \Index*[C++]{\CC{}} 6.2\%, \Csharp 5.9\%, Visual Basic 5.9\% = 61.5\%, where the next 50 languages are less than 2\% each, with a long tail.
     219For system programming, where direct access to hardware, storage management, and real-time issues are a requirement, C is the only language of choice.
     220The TIOBE index~\cite{TIOBE} for February 2021 ranks the top six most \emph{popular} programming languages as C 17.4\%, \Index*{Java} 12\%, Python 12\%, \Index*[C++]{\CC{}} 7.6\%, \Csharp 4\%, Visual Basic 3.8\% = 56.8\%, where the next 50 languages are less than 2\% each, with a long tail.
    217221The top 4 rankings over the past 35 years are:
    218222\begin{center}
    219223\setlength{\tabcolsep}{10pt}
    220224\begin{tabular}{@{}rcccccccc@{}}
    221                 & 2020  & 2015  & 2010  & 2005  & 2000  & 1995  & 1990  & 1985  \\ \hline
    222 Java    & 1             & 2             & 1             & 2             & 3             & -             & -             & -             \\
    223 \R{C}   & \R{2} & \R{1} & \R{2} & \R{1} & \R{1} & \R{2} & \R{1} & \R{1} \\
    224 Python  & 3             & 7             & 6             & 6             & 22    & 21    & -             & -             \\
    225 \CC             & 4             & 4             & 4             & 3             & 2             & 1             & 2             & 12    \\
     225                & 2021  & 2016  & 2011  & 2006  & 2001  & 1996  & 1991  & 1986  \\ \hline
     226\R{C}   & \R{1} & \R{2} & \R{2} & \R{1} & \R{1} & \R{1} & \R{1} & \R{1} \\
     227Java    & 2             & 1             & 1             & 2             & 3             & 28    & -             & -             \\
     228Python  & 3             & 5             & 6             & 7             & 23    & 13    & -             & -             \\
     229\CC             & 4             & 3             & 3             & 3             & 2             & 2             & 2             & 8             \\
    226230\end{tabular}
    227231\end{center}
     
    232236As stated, the goal of the \CFA project is to engineer modern language-features into C in an evolutionary rather than revolutionary way.
    233237\CC~\cite{C++14,C++} is an example of a similar project;
    234 however, it largely extended the C language, and did not address most of C's existing problems.\footnote{%
     238however, it largely extended the C language, and did not address many of C's existing problems.\footnote{%
    235239Two 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.}
    236240\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.
     
    241245
    242246The result of this project is a language that is largely backwards compatible with \Index*[C11]{\Celeven{}}~\cite{C11}, but fixes many of the well known C problems while adding modern language-features.
    243 To achieve these goals required a significant engineering exercise, where we had to ``think inside the existing C box''.
    244 Without these significant extension to C, it is unable to cope with the needs of modern programming problems and programmers;
    245 as a result, it will fade into disuse.
    246 Considering the large body of existing C code and programmers, there is significant impetus to ensure C is transformed into a modern programming language.
     247To achieve these goals required a significant engineering exercise, \ie ``thinking \emph{inside} the C box''.
     248Considering the large body of existing C code and programmers, there is significant impetus to ensure C is transformed into a modern language.
    247249While \Index*[C11]{\Celeven{}} made a few simple extensions to the language, nothing was added to address existing problems in the language or to augment the language with modern language-features.
    248250While some may argue that modern language-features may make C complex and inefficient, it is clear a language without modern capabilities is insufficient for the advanced programming problems existing today.
     
    251253\section{History}
    252254
    253 The \CFA project started with \Index*{Dave Till}\index{Till, Dave}'s \Index*{K-W C}~\cite{Buhr94a,Till89}, which extended C with new declaration syntax, multiple return values from routines, and advanced assignment capabilities using the notion of tuples.
    254 (See~\cite{Werther96} for similar work in \Index*[C++]{\CC{}}.)
     255The \CFA project started with \Index*{Dave Till}\index{Till, Dave}'s \Index*{K-W C}~\cite{Buhr94a,Till89}, which extended C with new declaration syntax, multiple return values from routines, and advanced assignment capabilities using the notion of tuples \see{\cite{Werther96} for similar work in \Index*[C++]{\CC{}}}.
    255256The first \CFA implementation of these extensions was by \Index*{Rodolfo Esteves}\index{Esteves, Rodolfo}~\cite{Esteves04}.
    256257
    257258The signature feature of \CFA is \emph{\Index{overload}able} \Index{parametric-polymorphic} functions~\cite{forceone:impl,Cormack90,Duggan96} with functions generalized using a ©forall© clause (giving the language its name):
    258259\begin{cfa}
    259 ®forall( otype T )® T identity( T val ) { return val; }
    260 int forty_two = identity( 42 ); §\C{// T is bound to int, forty\_two == 42}§
     260@forall( otype T )@ T identity( T val ) { return val; }
     261int forty_two = identity( 42 ); $\C{// T is bound to int, forty\_two == 42}$
    261262\end{cfa}
    262263% extending the C type system with parametric polymorphism and overloading, as opposed to the \Index*[C++]{\CC{}} approach of object-oriented extensions.
    263264\CFA{}\hspace{1pt}'s polymorphism was originally formalized by \Index*{Glen Ditchfield}\index{Ditchfield, Glen}~\cite{Ditchfield92}, and first implemented by \Index*{Richard Bilson}\index{Bilson, Richard}~\cite{Bilson03}.
    264265However, at that time, there was little interesting in extending C, so work did not continue.
    265 As the saying goes, ``\Index*{What goes around, comes around.}'', and there is now renewed interest in the C programming language because of legacy code-bases, so the \CFA project has been restarted.
     266As the saying goes, ``\Index*{What goes around, comes around.}'', and there is now renewed interest in the C programming language because of the legacy code-base, so the \CFA project was restarted in 2015.
    266267
    267268
     
    273274This feature allows \CFA programmers to take advantage of the existing panoply of C libraries to access thousands of external software features.
    274275Language developers often state that adequate \Index{library support} takes more work than designing and implementing the language itself.
    275 Fortunately, \CFA, like \Index*[C++]{\CC{}}, starts with immediate access to all exiting C libraries, and in many cases, can easily wrap library routines with simpler and safer interfaces, at very low cost.
     276Fortunately, \CFA, like \Index*[C++]{\CC{}}, starts with immediate access to all exiting C libraries, and in many cases, can easily wrap library routines with simpler and safer interfaces, at zero or very low cost.
    276277Hence, \CFA begins by leveraging the large repository of C libraries, and than allows programmers to incrementally augment their C programs with modern \Index{backward-compatible} features.
    277278
     
    286287
    287288double key = 5.0, vals[10] = { /* 10 sorted floating values */ };
    288 double * val = (double *)bsearch( &key, vals, 10, sizeof(vals[0]), comp ); §\C{// search sorted array}§
     289double * val = (double *)bsearch( &key, vals, 10, sizeof(vals[0]), comp ); $\C{// search sorted array}$
    289290\end{cfa}
    290291which can be augmented simply with a polymorphic, type-safe, \CFA-overloaded wrappers:
     
    295296
    296297forall( otype T | { int ?<?( T, T ); } ) unsigned int bsearch( T key, const T * arr, size_t size ) {
    297         T * result = bsearch( key, arr, size ); §\C{// call first version}§
    298         return result ? result - arr : size; } §\C{// pointer subtraction includes sizeof(T)}§
    299 
    300 double * val = bsearch( 5.0, vals, 10 ); §\C{// selection based on return type}§
     298        T * result = bsearch( key, arr, size ); $\C{// call first version}$
     299        return result ? result - arr : size; } $\C{// pointer subtraction includes sizeof(T)}$
     300
     301double * val = bsearch( 5.0, vals, 10 ); $\C{// selection based on return type}$
    301302int posn = bsearch( 5.0, vals, 10 );
    302303\end{cfa}
     
    310311\begin{cfa}
    311312forall( dtype T | sized(T) ) T * malloc( void ) { return (T *)malloc( sizeof(T) ); }
    312 int * ip = malloc(); §\C{// select type and size from left-hand side}§
     313int * ip = malloc(); $\C{// select type and size from left-hand side}$
    313314double * dp = malloc();
    314315struct S {...} * sp = malloc();
     
    319320However, it is necessary to differentiate between C and \CFA code because of name \Index{overload}ing, as for \CC.
    320321For 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©.
    321 Whereas, \CFA wraps each of these routines into ones with the overloaded name ©abs©:
    322 \begin{cfa}
    323 char ®abs®( char );
    324 extern "C" { int ®abs®( int ); } §\C{// use default C routine for int}§
    325 long int ®abs®( long int );
    326 long long int ®abs®( long long int );
    327 float ®abs®( float );
    328 double ®abs®( double );
    329 long double ®abs®( long double );
    330 float _Complex ®abs®( float _Complex );
    331 double _Complex ®abs®( double _Complex );
    332 long double _Complex ®abs®( long double _Complex );
    333 \end{cfa}
    334 The problem is the name clash between the library routine ©abs© and the \CFA names ©abs©.
    335 Hence, names appearing in an ©extern "C"© block have \newterm*{C linkage}.
    336 Then 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.
    337 Hence, 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.
    338 There is no way around this problem, other than C's approach of creating unique names for each pairing of operation and types.
    339 
    340 This example strongly illustrates a core idea in \CFA: \emph{the \Index{power of a name}}.
     322Whereas, \CFA wraps each of these routines into one overloaded name ©abs©:
     323\begin{cfa}
     324char @abs@( char );
     325extern "C" { int @abs@( int ); } $\C{// use default C routine for int}$
     326long int @abs@( long int );
     327long long int @abs@( long long int );
     328float @abs@( float );
     329double @abs@( double );
     330long double @abs@( long double );
     331float _Complex @abs@( float _Complex );
     332double _Complex @abs@( double _Complex );
     333long double _Complex @abs@( long double _Complex );
     334\end{cfa}
     335The problem is \Index{name clash} between the C name ©abs© and the \CFA names ©abs©, resulting in two name linkages\index{C linkage}: ©extern "C"© and ©extern "Cforall"© (default).
     336Overloaded names must use \newterm{name mangling}\index{mangling!name} to create unique names that are different from unmangled C names.
     337Hence, 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.
     338The only way around this problem is C's approach of creating unique names for each pairing of operation and type.
     339
     340This example illustrates a core idea in \CFA: \emph{the \Index{power of a name}}.
    341341The name ``©abs©'' evokes the notion of absolute value, and many mathematical types provide the notion of absolute value.
    342342Hence, knowing the name ©abs© is sufficient to apply it to any type where it is applicable.
     
    344344
    345345
    346 \section[Compiling a CFA Program]{Compiling a \CFA Program}
     346\section{\CFA Compilation}
    347347
    348348The command ©cfa© is used to compile a \CFA program and is based on the \Index{GNU} \Indexc{gcc} command, \eg:
    349349\begin{cfa}
    350 cfa§\indexc{cfa}\index{compilation!cfa@©cfa©}§ [ gcc-options ] [ C/§\CFA{}§ source-files ] [ assembler/loader files ]
    351 \end{cfa}
    352 \CFA programs having the following ©gcc© flags turned on:
    353 \begin{description}
     350cfa$\indexc{cfa}\index{compilation!cfa@©cfa©}$ [ gcc/$\CFA{}$-options ] [ C/$\CFA{}$ source-files ] [ assembler/loader files ]
     351\end{cfa}
     352There is no ordering among options (flags) and files, unless an option has an argument, which must appear immediately after the option possibly with or without a space separating option and argument.
     353
     354\CFA has the following ©gcc© flags turned on:
     355\begin{description}[topsep=0pt]
    354356\item
    355357\Indexc{-std=gnu11}\index{compilation option!-std=gnu11@{©-std=gnu11©}}
     
    359361Use the traditional GNU semantics for inline routines in C11 mode, which allows inline routines in header files.
    360362\end{description}
    361 The following new \CFA options are available:
    362 \begin{description}
     363
     364\CFA has the following new options:
     365\begin{description}[topsep=0pt]
    363366\item
    364367\Indexc{-CFA}\index{compilation option!-CFA@©-CFA©}
    365 Only 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.
     368Only the C preprocessor (flag ©-E©) 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.
    366369The generated code starts with the standard \CFA \Index{prelude}.
     370
     371\item
     372\Indexc{-XCFA}\index{compilation option!-XCFA@©-XCFA©}
     373Pass next flag as-is to the ©cfa-cpp© translator (see details below).
    367374
    368375\item
    369376\Indexc{-debug}\index{compilation option!-debug@©-debug©}
    370377The program is linked with the debugging version of the runtime system.
    371 The debug version performs runtime checks to help during the debugging phase of a \CFA program, but can substantially slow program execution.
     378The debug version performs runtime checks to aid the debugging phase of a \CFA program, but can substantially slow program execution.
    372379The runtime checks should only be removed after the program is completely debugged.
    373380\textbf{This option is the default.}
     
    399406\item
    400407\Indexc{-no-include-stdhdr}\index{compilation option!-no-include-stdhdr@©-no-include-stdhdr©}
    401 Do not supply ©extern "C"© wrappers for \Celeven standard include files (see~\VRef{s:StandardHeaders}).
     408Do not supply ©extern "C"© wrappers for \Celeven standard include files \see{\VRef{s:StandardHeaders}}.
    402409\textbf{This option is \emph{not} the default.}
    403410\end{comment}
     
    430437\begin{cfa}
    431438#ifndef __CFORALL__
    432 #include <stdio.h>§\indexc{stdio.h}§ §\C{// C header file}§
     439#include <stdio.h>$\indexc{stdio.h}$ $\C{// C header file}$
    433440#else
    434 #include <fstream>§\indexc{fstream}§ §\C{// \CFA header file}§
     441#include <fstream>$\indexc{fstream}$ $\C{// \CFA header file}$
    435442#endif
    436443\end{cfa}
     
    438445
    439446The \CFA translator has multiple steps.
    440 The following flags control how the tranlator works, the stages run, and printing within a stage.
     447The following flags control how the translator works, the stages run, and printing within a stage.
    441448The majority of these flags are used by \CFA developers, but some are occasionally useful to programmers.
     449Each option must be escaped with \Indexc{-XCFA}\index{translator option!-XCFA@{©-XCFA©}} to direct it to the compiler step, similar to the ©-Xlinker© flag for the linker, \eg:
     450\begin{lstlisting}[language=sh]
     451cfa $test$.cfa -CFA -XCFA -p # print translated code without printing the standard prelude
     452cfa $test$.cfa -XCFA -P -XCFA parse -XCFA -n # show program parse without prelude
     453\end{lstlisting}
    442454\begin{description}[topsep=5pt,itemsep=0pt,parsep=0pt]
    443455\item
    444 \Indexc{-h}\index{translator option!-h@{©-h©}}, \Indexc{--help}\index{translator option!--help@{©--help©}} \, print help message
    445 \item
    446 \Indexc{-l}\index{translator option!-l@{©-l©}}, \Indexc{--libcfa}\index{translator option!--libcfa@{©--libcfa©}} \, generate libcfa.c
     456\Indexc{-c}\index{translator option!-c@{©-c©}}, \Indexc{--colors}\index{translator option!--colors@{©--colors©}} \, diagnostic color: ©never©, ©always©, \lstinline[deletekeywords=auto]{auto}
     457\item
     458\Indexc{-g}\index{translator option!-g@{©-g©}}, \Indexc{--gdb}\index{translator option!--gdb@{©--gdb©}} \, wait for gdb to attach
     459\item
     460\Indexc{-h}\index{translator option!-h@{©-h©}}, \Indexc{--help}\index{translator option!--help@{©--help©}} \, print translator help message
     461\item
     462\Indexc{-l}\index{translator option!-l@{©-l©}}, \Indexc{--libcfa}\index{translator option!--libcfa@{©--libcfa©}} \, generate ©libcfa.c©
    447463\item
    448464\Indexc{-L}\index{translator option!-L@{©-L©}}, \Indexc{--linemarks}\index{translator option!--linemarks@{©--linemarks©}} \, generate line marks
     
    454470\Indexc{-n}\index{translator option!-n@{©-n©}}, \Indexc{--no-prelude}\index{translator option!--no-prelude@{©--no-prelude©}} \, do not read prelude
    455471\item
    456 \Indexc{-p}\index{translator option!-p@{©-p©}}, \Indexc{--prototypes}\index{translator option!--prototypes@{©--prototypes©}} \, generate prototypes for prelude functions
     472\Indexc{-p}\index{translator option!-p@{©-p©}}, \Indexc{--prototypes}\index{translator option!--prototypes@{©--prototypes©}} \, do not generate prelude prototypes $\Rightarrow$ prelude not printed
     473\item
     474\Indexc{-d}\index{translator option!-d@{©-d©}}, \Indexc{--deterministic-out}\index{translator option!--deterministic-out@{©--deterministic-out©}} \, only print deterministic output
    457475\item
    458476\Indexc{-P}\index{translator option!-P@{©-P©}}, \Indexc{--print}\index{translator option!--print@{©--print©}} \, one of:
    459477\begin{description}[topsep=0pt,itemsep=0pt,parsep=0pt]
    460478\item
     479\Indexc{ascodegen}\index{translator option!-P@{©-P©}!©ascodegen©}\index{translator option!--print@{©-print©}!©ascodegen©} \, as codegen rather than AST
     480\item
     481\Indexc{asterr}\index{translator option!-P@{©-P©}!©asterr©}\index{translator option!--print@{©-print©}!©asterr©} \, AST on error
     482\item
     483\Indexc{declstats}\index{translator option!-P@{©-P©}!©declstats©}\index{translator option!--print@{©-print©}!©declstats©} \, code property statistics
     484\item
     485\Indexc{parse}\index{translator option!-P@{©-P©}!©parse©}\index{translator option!--print@{©-print©}!©parse©} \, yacc (parsing) debug information
     486\item
     487\Indexc{pretty}\index{translator option!-P@{©-P©}!©pretty©}\index{translator option!--print@{©-print©}!©pretty©} \, prettyprint for ©ascodegen© flag
     488\item
     489\Indexc{rproto}\index{translator option!-P@{©-P©}!©rproto©}\index{translator option!--print@{©-print©}!©rproto©} \, resolver-proto instance
     490\item
     491\Indexc{rsteps}\index{translator option!-P@{©-P©}!©rsteps©}\index{translator option!--print@{©-print©}!©rsteps©} \, resolver steps
     492\item
     493\Indexc{tree}\index{translator option!-P@{©-P©}!©tree©}\index{translator option!--print@{©-print©}!©tree©} \, parse tree
     494\item
     495\Indexc{ast}\index{translator option!-P@{©-P©}!©ast©}\index{translator option!--print@{©-print©}!©ast©} \, AST after parsing
     496\item
     497\Indexc{symevt}\index{translator option!-P@{©-P©}!©symevt©}\index{translator option!--print@{©-print©}!©symevt©} \, symbol table events
     498\item
    461499\Indexc{altexpr}\index{translator option!-P@{©-P©}!©altexpr©}\index{translator option!--print@{©-print©}!©altexpr©} \, alternatives for expressions
    462500\item
    463 \Indexc{ascodegen}\index{translator option!-P@{©-P©}!©ascodegen©}\index{translator option!--print@{©-print©}!©ascodegen©} \, as codegen rather than AST
    464 \item
    465 \Indexc{ast}\index{translator option!-P@{©-P©}!©ast©}\index{translator option!--print@{©-print©}!©ast©} \, AST after parsing
    466 \item
    467501\Indexc{astdecl}\index{translator option!-P@{©-P©}!©astdecl©}\index{translator option!--print@{©-print©}!©astdecl©} \, AST after declaration validation pass
    468502\item
    469 \Indexc{asterr}\index{translator option!-P@{©-P©}!©asterr©}\index{translator option!--print@{©-print©}!©asterr©} \, AST on error
     503\Indexc{resolver}\index{translator option!-P@{©-P©}!©resolver©}\index{translator option!--print@{©-print©}!©resolver©} \, before resolver step
    470504\item
    471505\Indexc{astexpr}\index{translator option!-P@{©-P©}!©astexpr©}\index{translator option!--print@{©-print©}!©altexpr©} \, AST after expression analysis
    472506\item
     507\Indexc{ctordtor}\index{translator option!-P@{©-P©}!©ctordtor©}\index{translator option!--print@{©-print©}!©ctordtor©} \, after ctor/dtor are replaced
     508\item
     509\Indexc{tuple}\index{translator option!-P@{©-P©}!©tuple©}\index{translator option!--print@{©-print©}!©tuple©} \, after tuple expansion
     510\item
    473511\Indexc{astgen}\index{translator option!-P@{©-P©}!©astgen©}\index{translator option!--print@{©-print©}!©astgen©} \, AST after instantiate generics
    474512\item
    475513\Indexc{box}\index{translator option!-P@{©-P©}!©box©}\index{translator option!--print@{©-print©}!©box©} \, before box step
    476514\item
    477 \Indexc{ctordtor}\index{translator option!-P@{©-P©}!©ctordtor©}\index{translator option!--print@{©-print©}!©ctordtor©} \, after ctor/dtor are replaced
    478 \item
    479515\Indexc{codegen}\index{translator option!-P@{©-P©}!©codegen©}\index{translator option!--print@{©-print©}!©codegen©} \, before code generation
    480 \item
    481 \Indexc{declstats}\index{translator option!-P@{©-P©}!©declstats©}\index{translator option!--print@{©-print©}!©declstats©} \, code property statistics
    482 \item
    483 \Indexc{parse}\index{translator option!-P@{©-P©}!©parse©}\index{translator option!--print@{©-print©}!©parse©} \, yacc (parsing) debug information
    484 \item
    485 \Indexc{pretty}\index{translator option!-P@{©-P©}!©pretty©}\index{translator option!--print@{©-print©}!©pretty©} \, prettyprint for ascodegen flag
    486 \item
    487 \Indexc{resolver}\index{translator option!-P@{©-P©}!©resolver©}\index{translator option!--print@{©-print©}!©resolver©} \, before resolver step
    488 \item
    489 \Indexc{rproto}\index{translator option!-P@{©-P©}!©rproto©}\index{translator option!--print@{©-print©}!©rproto©} \, resolver-proto instance
    490 \item
    491 \Indexc{rsteps}\index{translator option!-P@{©-P©}!©rsteps©}\index{translator option!--print@{©-print©}!©rsteps©} \, resolver steps
    492 \item
    493 \Indexc{symevt}\index{translator option!-P@{©-P©}!©symevt©}\index{translator option!--print@{©-print©}!©symevt©} \, symbol table events
    494 \item
    495 \Indexc{tree}\index{translator option!-P@{©-P©}!©tree©}\index{translator option!--print@{©-print©}!©tree©} \, parse tree
    496 \item
    497 \Indexc{tuple}\index{translator option!-P@{©-P©}!©tuple©}\index{translator option!--print@{©-print©}!©tuple©} \, after tuple expansion
    498516\end{description}
    499517\item
    500518\Indexc{--prelude-dir} <directory> \, prelude directory for debug/nodebug
    501519\item
    502 \Indexc{-S}\index{translator option!-S@{©-S©}!©counters,heap,time,all,none©}, \Indexc{--statistics}\index{translator option!--statistics@{©--statistics©}!©counters,heap,time,all,none©} <option-list> \, enable profiling information:
    503 \begin{description}[topsep=0pt,itemsep=0pt,parsep=0pt]
    504 \item
    505 \Indexc{counters,heap,time,all,none}
    506 \end{description}
     520\Indexc{-S}\index{translator option!-S@{©-S©}!©counters,heap,time,all,none©}, \Indexc{--statistics}\index{translator option!--statistics@{©--statistics©}!©counters,heap,time,all,none©} <option-list> \, enable profiling information: ©counters©, ©heap©, ©time©, ©all©, ©none©
    507521\item
    508522\Indexc{-t}\index{translator option!-t@{©-t©}}, \Indexc{--tree}\index{translator option!--tree@{©--tree©}} build in tree
     
    513527\label{s:BackquoteIdentifiers}
    514528
    515 \CFA introduces several new keywords (see \VRef{s:CFAKeywords}) that can clash with existing C variable-names in legacy code.
     529\CFA introduces several new keywords \see{\VRef{s:CFAKeywords}} that can clash with existing C variable-names in legacy code.
    516530Keyword clashes are accommodated by syntactic transformations using the \CFA backquote escape-mechanism:
    517531\begin{cfa}
    518 int ®``®otype = 3; §\C{// make keyword an identifier}§
    519 double ®``®forall = 3.5;
     532int @``@otype = 3; $\C{// make keyword an identifier}$
     533double @``@forall = 3.5;
    520534\end{cfa}
    521535
    522536Existing 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.
    523 \VRef[Figure]{f:HeaderFileInterposition} shows how clashes in existing C header-files (see~\VRef{s:StandardHeaders}) can be handled using preprocessor \newterm{interposition}: ©#include_next© and ©-I filename©.
     537\VRef[Figure]{f:HeaderFileInterposition} shows how clashes in existing C header-files \see{\VRef{s:StandardHeaders}} can be handled using preprocessor \newterm{interposition}: ©#include_next© and ©-I filename©.
    524538Several common C header-files with keyword clashes are fixed in the standard \CFA header-library, so there is a seamless programming-experience.
    525539
     
    527541\begin{cfa}
    528542// include file uses the CFA keyword "with".
    529 #if ! defined( with ) §\C{// nesting ?}§
    530 #define with ®``®with §\C{// make keyword an identifier}§
     543#if ! defined( with )                                                   $\C{// nesting ?}$
     544#define with @``@with                                                   $\C{// make keyword an identifier}$
    531545#define __CFA_BFD_H__
    532546#endif
    533 §{\color{red}\#\textbf{include\_next} <bfdlink.h>}§ §\C{// must have internal check for multiple expansion}§
    534 #if defined( with ) && defined( __CFA_BFD_H__ ) §\C{// reset only if set}§
     547$\R{\#include\_next} <bfdlink.h>$                               $\C{// must have internal check for multiple expansion}$
     548#if defined( with ) && defined( __CFA_BFD_H__ ) $\C{// reset only if set}$
    535549#undef with
    536550#undef __CFA_BFD_H__
     
    544558\section{Constant Underscores}
    545559
    546 Numeric constants are extended to allow \Index{underscore}s\index{constant!underscore}, \eg:
    547 \begin{cfa}
    548 2®_®147®_®483®_®648; §\C{// decimal constant}§
    549 56®_®ul; §\C{// decimal unsigned long constant}§
    550 0®_®377; §\C{// octal constant}§
    551 0x®_®ff®_®ff; §\C{// hexadecimal constant}§
    552 0x®_®ef3d®_®aa5c; §\C{// hexadecimal constant}§
    553 3.141®_®592®_®654; §\C{// floating constant}§
    554 10®_®e®_®+1®_®00; §\C{// floating constant}§
    555 0x®_®ff®_®ff®_®p®_®3; §\C{// hexadecimal floating}§
    556 0x®_®1.ffff®_®ffff®_®p®_®128®_®l; §\C{// hexadecimal floating long constant}§
    557 L®_®§"\texttt{\textbackslash{x}}§®_®§\texttt{ff}§®_®§\texttt{ee}"§; §\C{// wide character constant}§
     560Numeric constants are extended to allow \Index{underscore}s\index{constant!underscore} as a separator, \eg:
     561\begin{cfa}
     5622@_@147@_@483@_@648; $\C{// decimal constant}$
     56356@_@ul; $\C{// decimal unsigned long constant}$
     5640@_@377; $\C{// octal constant}$
     5650x@_@ff@_@ff; $\C{// hexadecimal constant}$
     5660x@_@ef3d@_@aa5c; $\C{// hexadecimal constant}$
     5673.141@_@592@_@654; $\C{// floating constant}$
     56810@_@e@_@+1@_@00; $\C{// floating constant}$
     5690x@_@ff@_@ff@_@p@_@3; $\C{// hexadecimal floating}$
     5700x@_@1.ffff@_@ffff@_@p@_@128@_@l; $\C{// hexadecimal floating long constant}$
     571L@_@$"\texttt{\textbackslash{x}}$@_@$\texttt{ff}$@_@$\texttt{ee}"$; $\C{// wide character constant}$
    558572\end{cfa}
    559573The rules for placement of underscores are:
     
    574588It 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).
    575589This extension is backwards compatible, matches with the use of underscore in variable names, and appears in \Index*{Ada} and \Index*{Java} 8.
     590\CC uses the single quote (©'©) as a separator, restricted within a sequence of digits, \eg ©0xaa©©'©©ff©, ©3.141©©'©©592E1©©'©©1©.
    576591
    577592
    578593\section{Exponentiation Operator}
    579594
    580 C, \CC, and Java (and many other programming languages) have no exponentiation operator\index{exponentiation!operator}\index{operator!exponentiation}, \ie $x^y$, and instead use a routine, like \Indexc{pow(x,y)}, to perform the exponentiation operation.
    581 \CFA extends the basic operators with the exponentiation operator ©?®\®?©\index{?\\?@©?®\®?©} and ©?\=?©\index{?\\=?@©®\®=?©}, as in, ©x ®\® y© and ©x ®\®= y©, which means $x^y$ and $x \leftarrow x^y$.
    582 The priority of the exponentiation operator is between the cast and multiplicative operators, so that ©w * (int)x \ (int)y * z© is parenthesized as ©((w * (((int)x) \ ((int)y))) * z)©.
     595C, \CC, and Java (and other programming languages) have no exponentiation operator\index{exponentiation!operator}\index{operator!exponentiation}, \ie $x^y$, and instead use a routine, like \Indexc{pow(x,y)}, to perform the exponentiation operation.
     596\CFA extends the basic operators with the exponentiation operator ©?©\R{©\\©}©?©\index{?\\?@©?@\@?©} and ©?©\R{©\\©}©=?©\index{?\\=?@©@\@=?©}, as in, ©x ©\R{©\\©}© y© and ©x ©\R{©\\©}©= y©, which means $x^y$ and $x \leftarrow x^y$.
     597The priority of the exponentiation operator is between the cast and multiplicative operators, so that ©w * (int)x \ (int)y * z© is parenthesized as ©(w * (((int)x) \ ((int)y))) * z©.
    583598
    584599There are exponentiation operators for integral and floating types, including the builtin \Index{complex} types.
     
    587602Floating exponentiation\index{exponentiation!floating} is performed using \Index{logarithm}s\index{exponentiation!logarithm}, so the exponent cannot be negative.
    588603\begin{cfa}
    589 sout | 1 ®\® 0 | 1 ®\® 1 | 2 ®\® 8 | -4 ®\® 3 | 5 ®\® 3 | 5 ®\® 32 | 5L ®\® 32 | 5L ®\® 64 | -4 ®\® -3 | -4.0 ®\® -3 | 4.0 ®\® 2.1
    590            | (1.0f+2.0fi) ®\® (3.0f+2.0fi);
    591 1 1 256 -64 125 ®0® 3273344365508751233 ®0® ®0® -0.015625 18.3791736799526 0.264715-1.1922i
     604sout | 1 @\@ 0 | 1 @\@ 1 | 2 @\@ 8 | -4 @\@ 3 | 5 @\@ 3 | 5 @\@ 32 | 5L @\@ 32 | 5L @\@ 64 | -4 @\@ -3 | -4.0 @\@ -3 | 4.0 @\@ 2.1
     605           | (1.0f+2.0fi) @\@ (3.0f+2.0fi);
     6061 1 256 -64 125 @0@ 3273344365508751233 @0@ @0@ -0.015625 18.3791736799526 0.264715-1.1922i
    592607\end{cfa}
    593608Note, ©5 \ 32© and ©5L \ 64© overflow, and ©-4 \ -3© is a fraction but stored in an integer so all three computations generate an integral zero.
    594 Parenthesis are necessary for complex constants or the expression is parsed as ©1.0f+®(®2.0fi \ 3.0f®)®+2.0fi©.
     609Because exponentiation has higher priority than ©+©, parenthesis are necessary for exponentiation of \Index{complex constant}s or the expression is parsed as ©1.0f+©\R{©(©}©2.0fi \ 3.0f©\R{©)©}©+2.0fi©, requiring \R{©(©}©1.0f+2.0fi©\R{©)©}© \ ©\R{©(©}©3.0f+2.0fi©\R{©)©}.
     610
    595611The exponentiation operator is available for all the basic types, but for user-defined types, only the integral-computation version is available.
    596612\begin{cfa}
    597 forall( otype OT | { void ?{}( OT & this, one_t ); OT ?*?( OT, OT ); } )
    598 OT ?®\®?( OT ep, unsigned int y );
    599 forall( otype OT | { void ?{}( OT & this, one_t ); OT ?*?( OT, OT ); } )
    600 OT ?®\®?( OT ep, unsigned long int y );
     613forall( otype T | { void ?{}( T & this, one_t ); T ?*?( T, T ); } )
     614T ?@\@?( T ep, unsigned int y );
     615forall( otype T | { void ?{}( T & this, one_t ); T ?*?( T, T ); } )
     616T ?@\@?( T ep, unsigned long int y );
    601617\end{cfa}
    602618The user type ©T© must define multiplication, one (©1©), and ©*©.
     
    609625
    610626%\subsection{\texorpdfstring{\protect\lstinline@if@/\protect\lstinline@while@ Statement}{if Statement}}
    611 \subsection{\texorpdfstring{\LstKeywordStyle{if}/\LstKeywordStyle{while} Statement}{if/while Statement}}
    612 
    613 The ©if©/©while© expression allows declarations, similar to ©for© declaration expression.
    614 (Does not make sense for ©do©-©while©.)
    615 \begin{cfa}
    616 if ( ®int x = f()® ) ... §\C{// x != 0}§
    617 if ( ®int x = f(), y = g()® ) ... §\C{// x != 0 \&\& y != 0}§
    618 if ( ®int x = f(), y = g(); x < y® ) ... §\C{// relational expression}§
    619 if ( ®struct S { int i; } x = { f() }; x.i < 4® ) §\C{// relational expression}§
    620 
    621 while ( ®int x = f()® ) ... §\C{// x != 0}§
    622 while ( ®int x = f(), y = g()® ) ... §\C{// x != 0 \&\& y != 0}§
    623 while ( ®int x = f(), y = g(); x < y® ) ... §\C{// relational expression}§
    624 while ( ®struct S { int i; } x = { f() }; x.i < 4® ) ... §\C{// relational expression}§
    625 \end{cfa}
    626 Unless a relational expression is specified, each variable is compared not equal to 0, which is the standard semantics for the ©if©/©while© expression, and the results are combined using the logical ©&&© operator.\footnote{\CC only provides a single declaration always compared not equal to 0.}
    627 The scope of the declaration(s) is local to the @if@ statement but exist within both the ``then'' and ``else'' clauses.
     627\subsection{\texorpdfstring{\LstKeywordStyle{if} / \LstKeywordStyle{while} Statement}{if / while Statement}}
     628
     629The ©if©/©while© expression allows declarations, similar to ©for© declaration expression.\footnote{
     630Declarations in the ©do©-©while© condition are not useful because they appear after the loop body.}
     631\begin{cfa}
     632if ( @int x = f()@ ) ... $\C{// x != 0}$
     633if ( @int x = f(), y = g()@ ) ... $\C{// x != 0 \&\& y != 0}$
     634if ( @int x = f(), y = g(); x < y@ ) ... $\C{// relational expression}$
     635if ( @struct S { int i; } x = { f() }; x.i < 4@ ) $\C{// relational expression}$
     636
     637while ( @int x = f()@ ) ... $\C{// x != 0}$
     638while ( @int x = f(), y = g()@ ) ... $\C{// x != 0 \&\& y != 0}$
     639while ( @int x = f(), y = g(); x < y@ ) ... $\C{// relational expression}$
     640while ( @struct S { int i; } x = { f() }; x.i < 4@ ) ... $\C{// relational expression}$
     641\end{cfa}
     642Unless a relational expression is specified, each variable is compared not equal to 0, which is the standard semantics for the ©if©/©while© expression, and the results are combined using the logical ©&&© operator.
     643The scope of the declaration(s) is local to the ©if© statement but exist within both the \emph{then} and \emph{else} clauses.
     644\CC only provides a single declaration always compared ©!=© to 0.
    628645
    629646
    630647%\section{\texorpdfstring{\protect\lstinline@case@ Clause}{case Clause}}
    631648\subsection{\texorpdfstring{\LstKeywordStyle{case} Clause}{case Clause}}
     649\label{s:caseClause}
    632650
    633651C restricts the ©case© clause of a ©switch© statement to a single value.
     
    640658\begin{cfa}
    641659switch ( i ) {
    642   case ®1, 3, 5®:
     660  case @1, 3, 5@:
    643661        ...
    644   case ®2, 4, 6®:
     662  case @2, 4, 6@:
    645663        ...
    646664}
     
    670688\begin{cfa}
    671689switch ( i ) {
    672   case ®1~5:® §\C{// 1, 2, 3, 4, 5}§
     690  case @1~5:@ $\C{// 1, 2, 3, 4, 5}$
    673691        ...
    674   case ®10~15:® §\C{// 10, 11, 12, 13, 14, 15}§
     692  case @10~15:@ $\C{// 10, 11, 12, 13, 14, 15}$
    675693        ...
    676694}
     
    678696Lists of subranges are also allowed.
    679697\begin{cfa}
    680 case ®1~5, 12~21, 35~42®:
     698case @1~5, 12~21, 35~42@:
    681699\end{cfa}
    682700
     
    722740if ( argc == 3 ) {
    723741        // open output file
    724         ®// open input file
    725 ®} else if ( argc == 2 ) {
    726         ®// open input file (duplicate)
    727 
    728 ®} else {
     742        @// open input file
     743@} else if ( argc == 2 ) {
     744        @// open input file (duplicate)
     745
     746@} else {
    729747        // usage message
    730748}
     
    733751\end{cquote}
    734752In this example, case 2 is always done if case 3 is done.
    735 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.
     753This 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.
    736754C also uses fall-through to handle multiple case-values resulting in the same action:
    737755\begin{cfa}
    738756switch ( i ) {
    739   ®case 1: case 3: case 5:®     // odd values
     757  @case 1: case 3: case 5:@     // odd values
    740758        // odd action
    741759        break;
    742   ®case 2: case 4: case 6:®     // even values
     760  @case 2: case 4: case 6:@     // even values
    743761        // even action
    744762        break;
    745763}
    746764\end{cfa}
    747 However, this situation is handled in other languages without fall-through by allowing a list of case values.
    748 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.
     765This situation better handled without fall-through by allowing a list of case values \see{\VRef{s:caseClause}}.
     766While 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 most programming languages with a ©switch© statement.
    749767Hence, default fall-through semantics results in a large number of programming errors as programmers often \emph{forget} the ©break© statement at the end of a ©case© clause, resulting in inadvertent fall-through.
    750768
     
    756774        if ( j < k ) {
    757775                ...
    758           ®case 1:®             // transfer into "if" statement
     776          @case 1:@             // transfer into "if" statement
    759777                ...
    760778        } // if
     
    762780        while ( j < 5 ) {
    763781                ...
    764           ®case 3:®             // transfer into "while" statement
     782          @case 3:@             // transfer into "while" statement
    765783                ...
    766784        } // while
    767785} // switch
    768786\end{cfa}
    769 The problem with this usage is branching into control structures, which is known to cause both comprehension and technical difficulties.
    770 The comprehension problem occurs from the inability to determine how control reaches a particular point due to the number of branches leading to it.
     787This usage branches into control structures, which is known to cause both comprehension and technical difficulties.
     788The comprehension problem results from the inability to determine how control reaches a particular point due to the number of branches leading to it.
    771789The technical problem results from the inability to ensure declaration and initialization of variables when blocks are not entered at the beginning.
    772 There are no positive arguments for this kind of control flow, and therefore, there is a strong impetus to eliminate it.
     790There are few arguments for this kind of control flow, and therefore, there is a strong impetus to eliminate it.
    773791Nevertheless, C does have an idiom where this capability is used, known as ``\Index*{Duff's device}''~\cite{Duff83}:
    774792\begin{cfa}
     
    794812\item
    795813It is possible to place the ©default© clause anywhere in the list of labelled clauses for a ©switch© statement, rather than only at the end.
    796 Virtually all programming languages with a ©switch© statement require the ©default© clause to appear last in the case-clause list.
     814Most programming languages with a ©switch© statement require the ©default© clause to appear last in the case-clause list.
    797815The logic for this semantics is that after checking all the ©case© clauses without success, the ©default© clause is selected;
    798816hence, physically placing the ©default© clause at the end of the ©case© clause list matches with this semantics.
     
    803821\begin{cfa}
    804822switch ( x ) {
    805         ®int y = 1;® §\C{// unreachable initialization}§
    806         ®x = 7;® §\C{// unreachable code without label/branch}§
     823        @int y = 1;@ $\C{// unreachable initialization}$
     824        @x = 7;@ $\C{// unreachable code without label/branch}$
    807825  case 0: ...
    808826        ...
    809         ®int z = 0;® §\C{// unreachable initialization, cannot appear after case}§
     827        @int z = 0;@ $\C{// unreachable initialization, cannot appear after case}$
    810828        z = 2;
    811829  case 1:
    812         ®x = z;® §\C{// without fall through, z is uninitialized}§
     830        @x = z;@ $\C{// without fall through, z is uninitialized}$
    813831}
    814832\end{cfa}
    815833While 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.
    816 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.
    817 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.
    818 The key observation is that the ©switch© statement branches into control structure, \ie there are multiple entry points into its statement body.
     834Furthermore, 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©, where both are problematic.
     835As 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.
     836The key observation is that the ©switch© statement branches into a control structure, \ie there are multiple entry points into its statement body.
    819837\end{enumerate}
    820838
     
    842860Therefore, 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:
    843861\begin{cfa}
    844 ®choose® ( i ) {
     862@choose@ ( i ) {
    845863  case 1:  case 2:  case 3:
    846864        ...
    847         ®// implicit end of switch (break)
    848   ®case 5:
     865        @// implicit end of switch (break)
     866  @case 5:
    849867        ...
    850         ®fallthru®; §\C{// explicit fall through}§
     868        @fallthru@; $\C{// explicit fall through}$
    851869  case 7:
    852870        ...
    853         ®break® §\C{// explicit end of switch (redundant)}§
     871        @break@ $\C{// explicit end of switch (redundant)}$
    854872  default:
    855873        j = 3;
    856874}
    857875\end{cfa}
    858 Like the ©switch© statement, the ©choose© statement retains the fall-through semantics for a list of ©case© clauses;
     876Like the ©switch© statement, the ©choose© statement retains the fall-through semantics for a list of ©case© clauses.
    859877An implicit ©break© is applied only at the end of the \emph{statements} following a ©case© clause.
    860878An 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.
     
    872890\begin{cfa}
    873891switch ( x ) {
    874         ®int i = 0;® §\C{// allowed only at start}§
     892        @int i = 0;@ $\C{// allowed only at start}$
    875893  case 0:
    876894        ...
    877         ®int j = 0;® §\C{// disallowed}§
     895        @int j = 0;@ $\C{// disallowed}$
    878896  case 1:
    879897        {
    880                 ®int k = 0;® §\C{// allowed at different nesting levels}§
     898                @int k = 0;@ $\C{// allowed at different nesting levels}$
    881899                ...
    882           ®case 2:® §\C{// disallow case in nested statements}§
     900          @case 2:@ $\C{// disallow case in nested statements}$
    883901        }
    884902  ...
     
    897915  case 3:
    898916        if ( ... ) {
    899                 ... ®fallthru;® // goto case 4
     917                ... @fallthru;@ // goto case 4
    900918        } else {
    901919                ...
     
    912930choose ( ... ) {
    913931  case 3:
    914         ... ®fallthrough common;®
     932        ... @fallthrough common;@
    915933  case 4:
    916         ... ®fallthrough common;®
    917 
    918   ®common:® // below fallthrough
     934        ... @fallthrough common;@
     935
     936  @common:@ // below fallthrough
    919937                          // at case-clause level
    920938        ...     // common code for cases 3/4
     
    932950                for ( ... ) {
    933951                        // multi-level transfer
    934                         ... ®fallthru common;®
     952                        ... @fallthru common;@
    935953                }
    936954                ...
    937955        }
    938956        ...
    939   ®common:® // below fallthrough
     957  @common:@ // below fallthrough
    940958                          // at case-clause level
    941959\end{cfa}
     
    948966
    949967\begin{figure}
    950 \begin{tabular}{@{}l|l@{}}
    951 \multicolumn{1}{c|}{loop control} & \multicolumn{1}{c}{output} \\
     968\begin{tabular}{@{}l@{\hspace{25pt}}|l@{}}
     969\multicolumn{1}{@{}c@{\hspace{25pt}}|}{loop control} & \multicolumn{1}{c@{}}{output} \\
    952970\hline
    953 \begin{cfa}[xleftmargin=0pt]
    954 while ®()® { sout | "empty"; break; }
    955 do { sout | "empty"; break; } while ®()®;
    956 for ®()® { sout | "empty"; break; }
    957 for ( ®0® ) { sout | "A"; } sout | "zero";
    958 for ( ®1® ) { sout | "A"; }
    959 for ( ®10® ) { sout | "A"; }
    960 for ( ®= 10® ) { sout | "A"; }
    961 for ( ®1 ~= 10 ~ 2® ) { sout | "B"; }
    962 for ( ®10 -~= 1 ~ 2® ) { sout | "C"; }
    963 for ( ®0.5 ~ 5.5® ) { sout | "D"; }
    964 for ( ®5.5 -~ 0.5® ) { sout | "E"; }
    965 for ( ®i; 10® ) { sout | i; }
    966 for ( ®i; = 10® ) { sout | i; }
    967 for ( ®i; 1 ~= 10 ~ 2® ) { sout | i; }
    968 for ( ®i; 10 -~= 1 ~ 2® ) { sout | i; }
    969 for ( ®i; 0.5 ~ 5.5® ) { sout | i; }
    970 for ( ®i; 5.5 -~ 0.5® ) { sout | i; }
    971 for ( ®ui; 2u ~= 10u ~ 2u® ) { sout | ui; }
    972 for ( ®ui; 10u -~= 2u ~ 2u® ) { sout | ui; }
     971\begin{cfa}
     972while @($\,$)@ { sout | "empty"; break; }
     973do { sout | "empty"; break; } while @($\,$)@;
     974for @($\,$)@ { sout | "empty"; break; }
     975for ( @0@ ) { sout | "A"; } sout | "zero";
     976for ( @1@ ) { sout | "A"; }
     977for ( @10@ ) { sout | "A"; }
     978for ( @= 10@ ) { sout | "A"; }
     979for ( @1 ~= 10 ~ 2@ ) { sout | "B"; }
     980for ( @10 -~= 1 ~ 2@ ) { sout | "C"; }
     981for ( @0.5 ~ 5.5@ ) { sout | "D"; }
     982for ( @5.5 -~ 0.5@ ) { sout | "E"; }
     983for ( @i; 10@ ) { sout | i; }
     984for ( @i; = 10@ ) { sout | i; }
     985for ( @i; 1 ~= 10 ~ 2@ ) { sout | i; }
     986for ( @i; 10 -~= 1 ~ 2@ ) { sout | i; }
     987for ( @i; 0.5 ~ 5.5@ ) { sout | i; }
     988for ( @i; 5.5 -~ 0.5@ ) { sout | i; }
     989for ( @ui; 2u ~= 10u ~ 2u@ ) { sout | ui; }
     990for ( @ui; 10u -~= 2u ~ 2u@ ) { sout | ui; }
    973991enum { N = 10 };
    974 for ( ®N® ) { sout | "N"; }
    975 for ( ®i; N® ) { sout | i; }
    976 for ( ®i; N -~ 0® ) { sout | i; }
     992for ( @N@ ) { sout | "N"; }
     993for ( @i; N@ ) { sout | i; }
     994for ( @i; N -~ 0@ ) { sout | i; }
    977995const int start = 3, comp = 10, inc = 2;
    978 for ( ®i; start ~ comp ~ inc + 1® ) { sout | i; }
    979 for ( i; 1 ~ ®@® ) { if ( i > 10 ) break; sout | i; }
    980 for ( i; 10 -~ ®@® ) { if ( i < 0 ) break; sout | i; }
    981 for ( i; 2 ~ ®@® ~ 2 ) { if ( i > 10 ) break; sout | i; }
    982 for ( i; 2.1 ~ ®@® ~ ®@® ) { if ( i > 10.5 ) break; sout | i; i += 1.7; }
    983 for ( i; 10 -~ ®@® ~ 2 ) { if ( i < 0 ) break; sout | i; }
    984 for ( i; 12.1 ~ ®@® ~ ®@® ) { if ( i < 2.5 ) break; sout | i; i -= 1.7; }
    985 for ( i; 5 ®:® j; -5 ~ @ ) { sout | i | j; }
    986 for ( i; 5 ®:® j; -5 -~ @ ) { sout | i | j; }
    987 for ( i; 5 ®:® j; -5 ~ @ ~ 2 ) { sout | i | j; }
    988 for ( i; 5 ®:® j; -5 -~ @ ~ 2 ) { sout | i | j; }
    989 for ( i; 5 ®:® j; -5 ~ @ ) { sout | i | j; }
    990 for ( i; 5 ®:® j; -5 -~ @ ) { sout | i | j; }
    991 for ( i; 5 ®:® j; -5 ~ @ ~ 2 ) { sout | i | j; }
    992 for ( i; 5 ®:® j; -5 -~ @ ~ 2 ) { sout | i | j; }
    993 for ( i; 5 ®:® j; -5 -~ @ ~ 2 ®:® k; 1.5 ~ @ ) { sout | i | j | k; }
    994 for ( i; 5 ®:® j; -5 -~ @ ~ 2 ®:® k; 1.5 ~ @ ) { sout | i | j | k; }
    995 for ( i; 5 ®:® k; 1.5 ~ @ ®:® j; -5 -~ @ ~ 2 ) { sout | i | j | k; }
     996for ( @i; start ~ comp ~ inc + 1@ ) { sout | i; }
     997for ( i; 1 ~ $\R{@}$ ) { if ( i > 10 ) break; sout | i; }
     998for ( i; 10 -~ $\R{@}$ ) { if ( i < 0 ) break; sout | i; }
     999for ( i; 2 ~ $\R{@}$ ~ 2 ) { if ( i > 10 ) break; sout | i; }
     1000for ( i; 2.1 ~ $\R{@}$ ~ $\R{@}$ ) { if ( i > 10.5 ) break; sout | i; i += 1.7; }
     1001for ( i; 10 -~ $\R{@}$ ~ 2 ) { if ( i < 0 ) break; sout | i; }
     1002for ( i; 12.1 ~ $\R{@}$ ~ $\R{@}$ ) { if ( i < 2.5 ) break; sout | i; i -= 1.7; }
     1003for ( i; 5 @:@ j; -5 ~ $@$ ) { sout | i | j; }
     1004for ( i; 5 @:@ j; -5 -~ $@$ ) { sout | i | j; }
     1005for ( i; 5 @:@ j; -5 ~ $@$ ~ 2 ) { sout | i | j; }
     1006for ( i; 5 @:@ j; -5 -~ $@$ ~ 2 ) { sout | i | j; }
     1007for ( i; 5 @:@ j; -5 ~ $@$ ) { sout | i | j; }
     1008for ( i; 5 @:@ j; -5 -~ $@$ ) { sout | i | j; }
     1009for ( i; 5 @:@ j; -5 ~ $@$ ~ 2 ) { sout | i | j; }
     1010for ( i; 5 @:@ j; -5 -~ $@$ ~ 2 ) { sout | i | j; }
     1011for ( i; 5 @:@ j; -5 -~ $@$ ~ 2 @:@ k; 1.5 ~ $@$ ) { sout | i | j | k; }
     1012for ( i; 5 @:@ j; -5 -~ $@$ ~ 2 @:@ k; 1.5 ~ $@$ ) { sout | i | j | k; }
     1013for ( i; 5 @:@ k; 1.5 ~ $@$ @:@ j; -5 -~ $@$ ~ 2 ) { sout | i | j | k; }
    9961014\end{cfa}
    9971015&
     
    10561074\subsection{Loop Control}
    10571075
    1058 The ©for©/©while©/©do-while© loop-control allows empty or simplified ranges (see Figure~\ref{f:LoopControlExamples}).
    1059 \begin{itemize}
     1076Looping a fixed number of times, possibly with a loop index, occurs frequently.
     1077\CFA condenses simply looping to facilitate coding speed and safety.
     1078The ©for©/©while©/©do-while© loop-control is augmented as follows \see{examples in \VRef[Figure]{f:LoopControlExamples}}:
     1079\begin{itemize}[itemsep=0pt]
     1080\item
     1081©0© is the implicit start value;
     1082\item
     1083©1© is the implicit increment value.
     1084\item
     1085The up-to range uses operator ©+=© for increment;
     1086\item
     1087The down-to range uses operator ©-=© for decrement.
    10601088\item
    10611089The loop index is polymorphic in the type of the comparison value N (when the start value is implicit) or the start value M.
     1090\begin{cfa}
     1091for ( i; @5@ )                                  $\C[2.5in]{// typeof(5) i; 5 is comparison value}$
     1092for ( i; @1.5@~5.5~0.5 )                $\C{// typeof(1.5) i; 1.5 is start value}$
     1093\end{cfa}
    10621094\item
    10631095An empty conditional implies comparison value of ©1© (true).
    1064 \item
    1065 A comparison N is implicit up-to exclusive range [0,N©®)®©.
    1066 \item
    1067 A comparison ©=© N is implicit up-to inclusive range [0,N©®]®©.
    1068 \item
    1069 The up-to range M ©~©\index{~@©~©} N means exclusive range [M,N©®)®©.
    1070 \item
    1071 The up-to range M ©~=©\index{~=@©~=©} N means inclusive range [M,N©®]®©.
    1072 \item
    1073 The down-to range M ©-~©\index{-~@©-~©} N means exclusive range [N,M©®)®©.
    1074 \item
    1075 The down-to range M ©-~=©\index{-~=@©-~=©} N means inclusive range [N,M©®]®©.
    1076 \item
    1077 ©0© is the implicit start value;
    1078 \item
    1079 ©1© is the implicit increment value.
    1080 \item
    1081 The up-to range uses operator ©+=© for increment;
    1082 \item
    1083 The down-to range uses operator ©-=© for decrement.
     1096\begin{cfa}
     1097while ( $\R{/*empty*/}$ )               $\C{// while ( true )}$
     1098for ( $\R{/*empty*/}$ )                 $\C{// for ( ; true; )}$
     1099do ... while ( $\R{/*empty*/}$ ) $\C{// do ... while ( true )}$
     1100\end{cfa}
     1101\item
     1102A comparison N is implicit up-to exclusive range [0,N\R{)}.
     1103\begin{cfa}
     1104for ( @5@ )                                             $\C{// for ( typeof(5) i; i < 5; i += 1 )}$
     1105\end{cfa}
     1106\item
     1107A comparison ©=© N is implicit up-to inclusive range [0,N\R{]}.
     1108\begin{cfa}
     1109for ( @=@5 )                                    $\C{// for ( typeof(5) i; i <= 5; i += 1 )}$
     1110\end{cfa}
     1111\item
     1112The up-to range M ©~©\index{~@©~©} N means exclusive range [M,N\R{)}.
     1113\begin{cfa}
     1114for ( 1@~@5 )                                   $\C{// for ( typeof(1) i = 1; i < 5; i += 1 )}$
     1115\end{cfa}
     1116\item
     1117The up-to range M ©~=©\index{~=@©~=©} N means inclusive range [M,N\R{]}.
     1118\begin{cfa}
     1119for ( 1@~=@5 )                                  $\C{// for ( typeof(1) i = 1; i <= 5; i += 1 )}$
     1120\end{cfa}
     1121\item
     1122The down-to range M ©-~©\index{-~@©-~©} N means exclusive range [N,M\R{)}.
     1123\begin{cfa}
     1124for ( 1@-~@5 )                                  $\C{// for ( typeof(1) i = 5; i > 0; i -= 1 )}$
     1125\end{cfa}
     1126\item
     1127The down-to range M ©-~=©\index{-~=@©-~=©} N means inclusive range [N,M\R{]}.
     1128\begin{cfa}
     1129for ( 1@-~=@5 )                                 $\C{// for ( typeof(1) i = 5; i >= 0; i -= 1 )}$
     1130\end{cfa}
    10841131\item
    10851132©@© means put nothing in this field.
     1133\begin{cfa}
     1134for ( 1~$\R{@}$~2 )                             $\C{// for ( typeof(1) i = 1; /*empty*/; i += 2 )}$
     1135\end{cfa}
    10861136\item
    10871137©:© means start another index.
     1138\begin{cfa}
     1139for ( i; 5 @:@ j; 2~12~3 )              $\C{// for ( typeof(i) i = 1, j = 2; i < 5 \&\& j < 12; i += 1, j += 3 )}\CRT$
     1140\end{cfa}
    10881141\end{itemize}
    10891142
     
    10921145\subsection{\texorpdfstring{Labelled \LstKeywordStyle{continue} / \LstKeywordStyle{break} Statement}{Labelled continue / break Statement}}
    10931146
    1094 While C provides ©continue© and ©break© statements for altering control flow, both are restricted to one level of nesting for a particular control structure.
    1095 Unfortunately, this restriction forces programmers to use \Indexc{goto} to achieve the equivalent control-flow for more than one level of nesting.
     1147C ©continue© and ©break© statements, for altering control flow, are restricted to one level of nesting for a particular control structure.
     1148This restriction forces programmers to use \Indexc{goto} to achieve the equivalent control-flow for more than one level of nesting.
    10961149To prevent having to switch to the ©goto©, \CFA extends the \Indexc{continue}\index{continue@©continue©!labelled}\index{labelled!continue@©continue©} and \Indexc{break}\index{break@©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}, as in Java.
    10971150For both ©continue© and ©break©, the target label must be directly associated with a ©for©, ©while© or ©do© statement;
    10981151for ©break©, the target label can also be associated with a ©switch©, ©if© or compound (©{}©) statement.
    1099 \VRef[Figure]{f:MultiLevelExit} shows ©continue© and ©break© indicating the specific control structure, and the corresponding C program using only ©goto© and labels.
     1152\VRef[Figure]{f:MultiLevelExit} shows a comparison between labelled ©continue© and ©break© and the corresponding C equivalent using ©goto© and labels.
    11001153The innermost loop has 8 exit points, which cause continuation or termination of one or more of the 7 \Index{nested control-structure}s.
    11011154
     
    11041157\begin{lrbox}{\myboxA}
    11051158\begin{cfa}[tabsize=3]
    1106 ®Compound:® {
    1107         ®Try:® try {
    1108                 ®For:® for ( ... ) {
    1109                         ®While:® while ( ... ) {
    1110                                 ®Do:® do {
    1111                                         ®If:® if ( ... ) {
    1112                                                 ®Switch:® switch ( ... ) {
     1159@Compound:@ {
     1160        @Try:@ try {
     1161                @For:@ for ( ... ) {
     1162                        @While:@ while ( ... ) {
     1163                                @Do:@ do {
     1164                                        @If:@ if ( ... ) {
     1165                                                @Switch:@ switch ( ... ) {
    11131166                                                        case 3:
    1114                                                                 ®break Compound®;
    1115                                                                 ®break Try®;
    1116                                                                 ®break For®;      /* or */  ®continue For®;
    1117                                                                 ®break While®;  /* or */  ®continue While®;
    1118                                                                 ®break Do®;      /* or */  ®continue Do®;
    1119                                                                 ®break If®;
    1120                                                                 ®break Switch®;
     1167                                                                @break Compound@;
     1168                                                                @break Try@;
     1169                                                                @break For@;      /* or */  @continue For@;
     1170                                                                @break While@;  /* or */  @continue While@;
     1171                                                                @break Do@;      /* or */  @continue Do@;
     1172                                                                @break If@;
     1173                                                                @break Switch@;
    11211174                                                        } // switch
    11221175                                                } else {
    1123                                                         ... ®break If®; ...     // terminate if
     1176                                                        ... @break If@; ...     // terminate if
    11241177                                                } // if
    11251178                                } while ( ... ); // do
    11261179                        } // while
    11271180                } // for
    1128         } ®finally® { // always executed
     1181        } @finally@ { // always executed
    11291182        } // try
    11301183} // compound
     
    11361189{
    11371190
    1138                 ®ForC:® for ( ... ) {
    1139                         ®WhileC:® while ( ... ) {
    1140                                 ®DoC:® do {
     1191                @ForC:@ for ( ... ) {
     1192                        @WhileC:@ while ( ... ) {
     1193                                @DoC:@ do {
    11411194                                        if ( ... ) {
    11421195                                                switch ( ... ) {
    11431196                                                        case 3:
    1144                                                                 ®goto Compound®;
    1145                                                                 ®goto Try®;
    1146                                                                 ®goto ForB®;      /* or */  ®goto ForC®;
    1147                                                                 ®goto WhileB®;  /* or */  ®goto WhileC®;
    1148                                                                 ®goto DoB®;      /* or */  ®goto DoC®;
    1149                                                                 ®goto If®;
    1150                                                                 ®goto Switch®;
    1151                                                         } ®Switch:® ;
     1197                                                                @goto Compound@;
     1198                                                                @goto Try@;
     1199                                                                @goto ForB@;      /* or */  @goto ForC@;
     1200                                                                @goto WhileB@;  /* or */  @goto WhileC@;
     1201                                                                @goto DoB@;      /* or */  @goto DoC@;
     1202                                                                @goto If@;
     1203                                                                @goto Switch@;
     1204                                                        } @Switch:@ ;
    11521205                                                } else {
    1153                                                         ... ®goto If®; ...      // terminate if
    1154                                                 } ®If:®;
    1155                                 } while ( ... ); ®DoB:® ;
    1156                         } ®WhileB:® ;
    1157                 } ®ForB:® ;
    1158 
    1159 
    1160 } ®Compound:® ;
     1206                                                        ... @goto If@; ...      // terminate if
     1207                                                } @If:@;
     1208                                } while ( ... ); @DoB:@ ;
     1209                        } @WhileB:@ ;
     1210                } @ForB:@ ;
     1211
     1212
     1213} @Compound:@ ;
    11611214\end{cfa}
    11621215\end{lrbox}
    11631216
    11641217\subfloat[\CFA]{\label{f:CFibonacci}\usebox\myboxA}
    1165 \hspace{2pt}
     1218\hspace{3pt}
    11661219\vrule
    1167 \hspace{2pt}
     1220\hspace{3pt}
    11681221\subfloat[C]{\label{f:CFAFibonacciGen}\usebox\myboxB}
    11691222\caption{Multi-level Exit}
     
    11801233This restriction prevents missing declarations and/or initializations at the start of a control structure resulting in undefined behaviour.
    11811234\end{itemize}
    1182 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.
     1235The 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 via a label.
    11831236Furthermore, the location of the label at the \emph{beginning} of the target control structure informs the reader (\Index{eye candy}) that complex control-flow is occurring in the body of the control structure.
    11841237With ©goto©, the label is at the end of the control structure, which fails to convey this important clue early enough to the reader.
     
    11871240
    11881241
    1189 %\section{\texorpdfstring{\protect\lstinline@with@ Statement}{with Statement}}
    1190 \section{\texorpdfstring{\LstKeywordStyle{with} Statement}{with Statement}}
     1242%\subsection{\texorpdfstring{\protect\lstinline@with@ Statement}{with Statement}}
     1243\subsection{\texorpdfstring{\LstKeywordStyle{with} Statement}{with Statement}}
    11911244\label{s:WithStatement}
    11921245
    1193 Grouping heterogeneous data into \newterm{aggregate}s (structure/union) is a common programming practice, and an aggregate can be further organized into more complex structures, such as arrays and containers:
    1194 \begin{cfa}
    1195 struct S { §\C{// aggregate}§
    1196         char c; §\C{// fields}§
    1197         int i;
    1198         double d;
     1246Grouping heterogeneous data into an \newterm{aggregate} (structure/union) is a common programming practice, and aggregates may be nested:
     1247\begin{cfa}
     1248struct Person {                                                         $\C{// aggregate}$
     1249        struct Name { char first[20], last[20]; } name $\C{// nesting}$
     1250        struct Address { ... } address                  $\C{// nesting}$
     1251        int sex;
    11991252};
    1200 S s, as[10];
    1201 \end{cfa}
    1202 However, functions manipulating aggregates must repeat the aggregate name to access its containing fields:
    1203 \begin{cfa}
    1204 void f( S s ) {
    1205         ®s.®c; ®s.®i; ®s.®d; §\C{// access containing fields}§
    1206 }
    1207 \end{cfa}
    1208 which extends to multiple levels of qualification for nested aggregates.
    1209 A similar situation occurs in object-oriented programming, \eg \CC:
     1253\end{cfa}
     1254Functions manipulating aggregates must repeat the aggregate name to access its containing fields.
     1255\begin{cfa}
     1256Person p
     1257@p.@name; @p.@address; @p.@sex; $\C{// access containing fields}$
     1258\end{cfa}
     1259which extends to multiple levels of qualification for nested aggregates and multiple aggregates.
     1260\begin{cfa}
     1261struct Ticket { ... } t;
     1262@p.name@.first; @p.address@.street;             $\C{// access nested fields}$
     1263@t.@departure; @t.@cost;                                $\C{// access multiple aggregate}$
     1264\end{cfa}
     1265Repeated aggregate qualification is tedious and makes code difficult to read.
     1266Therefore, reducing aggregate qualification is a useful language design goal.
     1267
     1268C allows unnamed nested aggregates that open their scope into the containing aggregate.
     1269This feature is used to group fields for attributes and/or with ©union© aggregates.
     1270\begin{cfa}
     1271struct S {
     1272        struct { int g,  h; } __attribute__(( aligned(64) ));
     1273        int tag;
     1274        union {
     1275                struct { char c1,  c2; } __attribute__(( aligned(128) ));
     1276                struct { int i1,  i2; };
     1277                struct { double d1,  d2; };
     1278        };
     1279};
     1280s.g; s.h; s.tag; s.c1; s.c2; s.i1; s.i2; s.d1; s.d2;
     1281\end{cfa}
     1282
     1283Object-oriented languages reduce qualification for class variables within member functions, \eg \CC:
    12101284\begin{C++}
    12111285struct S {
    1212         char c; §\C{// fields}§
    1213         int i;
    1214         double d;
    1215         void f() { §\C{// implicit ``this'' aggregate}§
    1216                 ®this->®c; ®this->®i; ®this->®d; §\C{// access containing fields}§
     1286        char @c@;   int @i@;   double @d@;
     1287        void f( /* S * this */ ) {                              $\C{// implicit ``this'' parameter}$
     1288                @c@;   @i@;   @d@;                                      $\C{// this->c; this->i; this->d;}$
    12171289        }
    12181290}
    12191291\end{C++}
    1220 Object-oriented nesting of member functions in a \lstinline[language=C++]@class/struct@ allows eliding \lstinline[language=C++]@this->@ because of lexical scoping.
    1221 However, for other aggregate parameters, qualification is necessary:
    1222 \begin{cfa}
    1223 struct T { double m, n; };
    1224 int S::f( T & t ) { §\C{// multiple aggregate parameters}§
    1225         c; i; d; §\C{\color{red}// this--{\textgreater}.c, this--{\textgreater}.i, this--{\textgreater}.d}§
    1226         ®t.®m; ®t.®n; §\C{// must qualify}§
    1227 }
    1228 \end{cfa}
    1229 
    1230 To simplify the programmer experience, \CFA provides a ©with© statement (see Pascal~\cite[\S~4.F]{Pascal}) to elide aggregate qualification to fields by opening a scope containing the field identifiers.
    1231 Hence, the qualified fields become variables with the side-effect that it is easier to optimizing field references in a block.
    1232 \begin{cfa}
    1233 void f( S & this ) ®with ( this )® { §\C{// with statement}§
    1234         c; i; d; §\C{\color{red}// this.c, this.i, this.d}§
     1292In general, qualification is elided for the variables and functions in the lexical scopes visible from a member function.
     1293However, qualification is necessary for name shadowing and explicit aggregate parameters.
     1294\begin{cfa}
     1295struct T {
     1296        char @m@;   int @i@;   double @n@;              $\C{// derived class variables}$
     1297};
     1298struct S : public T {
     1299        char @c@;   int @i@;   double @d@;              $\C{// class variables}$
     1300        void g( double @d@, T & t ) {
     1301                d;   @t@.m;   @t@.i;   @t@.n;           $\C{// function parameter}$
     1302                c;   i;   @this->@d;   @S::@d;          $\C{// class S variables}$
     1303                m;   @T::@i;   n;                                       $\C{// class T variables}$
     1304        }
     1305};
     1306\end{cfa}
     1307Note the three different forms of qualification syntax in \CC, ©.©, ©->©, ©::©, which is confusing.
     1308
     1309Since \CFA in not object-oriented, it has no implicit parameter with its implicit qualification.
     1310Instead \CFA introduces a general mechanism using the ©with© statement \see{Pascal~\cite[\S~4.F]{Pascal}} to explicitly elide aggregate qualification by opening a scope containing the field identifiers.
     1311Hence, the qualified fields become variables with the side-effect that it is simpler to write, easier to read, and optimize field references in a block.
     1312\begin{cfa}
     1313void f( S & this ) @with ( this )@ {            $\C{// with statement}$
     1314        @c@;   @i@;   @d@;                                              $\C{// this.c, this.i, this.d}$
    12351315}
    12361316\end{cfa}
    12371317with the generality of opening multiple aggregate-parameters:
    12381318\begin{cfa}
    1239 void f( S & s, T & t ) ®with ( s, t )® { §\C{// multiple aggregate parameters}§
    1240         c; i; d; §\C{\color{red}// s.c, s.i, s.d}§
    1241         m; n; §\C{\color{red}// t.m, t.n}§
    1242 }
    1243 \end{cfa}
    1244 
    1245 In detail, the ©with© statement has the form:
    1246 \begin{cfa}
    1247 §\emph{with-statement}§:
    1248         'with' '(' §\emph{expression-list}§ ')' §\emph{compound-statement}§
    1249 \end{cfa}
    1250 and may appear as the body of a function or nested within a function body.
    1251 Each expression in the expression-list provides a type and object.
    1252 The type must be an aggregate type.
     1319void g( S & s, T & t ) @with ( s, t )@ {        $\C{// multiple aggregate parameters}$
     1320        c;   @s.@i;   d;                                                $\C{// s.c, s.i, s.d}$
     1321        m;   @t.@i;   n;                                                $\C{// t.m, t.i, t.n}$
     1322}
     1323\end{cfa}
     1324where qualification is only necessary to disambiguate the shadowed variable ©i©.
     1325
     1326In detail, the ©with© statement may appear as the body of a function or nested within a function body.
     1327The ©with© clause takes a list of expressions, where each expression provides an aggregate type and object.
    12531328(Enumerations are already opened.)
    1254 The object is the implicit qualifier for the open structure-fields.
    1255 
     1329To open a pointer type, the pointer must be dereferenced to obtain a reference to the aggregate type.
     1330\begin{cfa}
     1331S * sp;
     1332with ( *sp ) { ... }
     1333\end{cfa}
     1334The expression object is the implicit qualifier for the open structure-fields.
     1335\CFA's ability to overload variables \see{\VRef{s:VariableOverload}} and use the left-side of assignment in type resolution means most fields with the same name but different types are automatically disambiguated, eliminating qualification.
    12561336All expressions in the expression list are open in parallel within the compound statement.
    12571337This semantic is different from Pascal, which nests the openings from left to right.
    12581338The difference between parallel and nesting occurs for fields with the same name and type:
    12591339\begin{cfa}
    1260 struct S { int ®i®; int j; double m; } s, w;
    1261 struct T { int ®i®; int k; int m; } t, w;
    1262 with ( s, t ) {
    1263         j + k; §\C{// unambiguous, s.j + t.k}§
    1264         m = 5.0; §\C{// unambiguous, t.m = 5.0}§
    1265         m = 1; §\C{// unambiguous, s.m = 1}§
    1266         int a = m; §\C{// unambiguous, a = s.i }§
    1267         double b = m; §\C{// unambiguous, b = t.m}§
    1268         int c = s.i + t.i; §\C{// unambiguous, qualification}§
    1269         (double)m; §\C{// unambiguous, cast}§
    1270 }
    1271 \end{cfa}
    1272 For parallel semantics, both ©s.i© and ©t.i© are visible, so ©i© is ambiguous without qualification;
    1273 for nested semantics, ©t.i© hides ©s.i©, so ©i© implies ©t.i©.
    1274 \CFA's ability to overload variables means fields with the same name but different types are automatically disambiguated, eliminating most qualification when opening multiple aggregates.
    1275 Qualification or a cast is used to disambiguate.
    1276 
    1277 There is an interesting problem between parameters and the function-body ©with©, \eg:
    1278 \begin{cfa}
    1279 void ?{}( S & s, int i ) with ( s ) { §\C{// constructor}§
    1280         ®s.i = i;®  j = 3;  m = 5.5; §\C{// initialize fields}§
     1340struct Q { int @i@; int k; int @m@; } q, w;
     1341struct R { int @i@; int j; double @m@; } r, w;
     1342with ( r, q ) {
     1343        j + k;                                                                  $\C{// unambiguous, r.j + q.k}$
     1344        m = 5.0;                                                                $\C{// unambiguous, q.m = 5.0}$
     1345        m = 1;                                                                  $\C{// unambiguous, r.m = 1}$
     1346        int a = m;                                                              $\C{// unambiguous, a = r.i }$
     1347        double b = m;                                                   $\C{// unambiguous, b = q.m}$
     1348        int c = r.i + q.i;                                              $\C{// disambiguate with qualification}$
     1349        (double)m;                                                              $\C{// disambiguate with cast}$
     1350}
     1351\end{cfa}
     1352For parallel semantics, both ©r.i© and ©q.i© are visible, so ©i© is ambiguous without qualification;
     1353for nested semantics, ©q.i© hides ©r.i©, so ©i© implies ©q.i©.
     1354Pascal nested-semantics is possible by nesting ©with© statements.
     1355\begin{cfa}
     1356with ( r ) {
     1357        i;                                                                              $\C{// unambiguous, r.i}$
     1358        with ( q ) {
     1359                i;                                                                      $\C{// unambiguous, q.i}$
     1360        }
     1361}
     1362\end{cfa}
     1363A cast or qualification can be used to disambiguate variables within a ©with© \emph{statement}.
     1364A cast can be used to disambiguate among overload variables in a ©with© \emph{expression}:
     1365\begin{cfa}
     1366with ( w ) { ... }                                                      $\C{// ambiguous, same name and no context}$
     1367with ( (Q)w ) { ... }                                           $\C{// unambiguous, cast}$
     1368\end{cfa}
     1369Because there is no left-side in the ©with© expression to implicitly disambiguate between the ©w© variables, it is necessary to explicitly disambiguate by casting ©w© to type ©Q© or ©R©.
     1370
     1371Finally, there is an interesting problem between parameters and the function-body ©with©, \eg:
     1372\begin{cfa}
     1373void ?{}( S & s, int i ) with ( s ) { $\C{// constructor}$
     1374        @s.i = i;@  j = 3;  m = 5.5; $\C{// initialize fields}$
    12811375}
    12821376\end{cfa}
     
    12911385and implicitly opened \emph{after} a function-body open, to give them higher priority:
    12921386\begin{cfa}
    1293 void ?{}( S & s, int ®i® ) with ( s ) ®with( §\emph{\color{red}params}§ )® {
    1294         s.i = ®i®; j = 3; m = 5.5;
    1295 }
    1296 \end{cfa}
    1297 Finally, a cast may be used to disambiguate among overload variables in a ©with© expression:
    1298 \begin{cfa}
    1299 with ( w ) { ... } §\C{// ambiguous, same name and no context}§
    1300 with ( (S)w ) { ... } §\C{// unambiguous, cast}§
    1301 \end{cfa}
    1302 and ©with© expressions may be complex expressions with type reference (see Section~\ref{s:References}) to aggregate:
    1303 % \begin{cfa}
    1304 % struct S { int i, j; } sv;
    1305 % with ( sv ) { §\C{// implicit reference}§
    1306 %       S & sr = sv;
    1307 %       with ( sr ) { §\C{// explicit reference}§
    1308 %               S * sp = &sv;
    1309 %               with ( *sp ) { §\C{// computed reference}§
    1310 %                       i = 3; j = 4; §\C{\color{red}// sp--{\textgreater}i, sp--{\textgreater}j}§
    1311 %               }
    1312 %               i = 2; j = 3; §\C{\color{red}// sr.i, sr.j}§
    1313 %       }
    1314 %       i = 1; j = 2; §\C{\color{red}// sv.i, sv.j}§
    1315 % }
    1316 % \end{cfa}
    1317 
    1318 In \Index{object-oriented} programming, there is an implicit first parameter, often names \textbf{©self©} or \textbf{©this©}, which is elided.
    1319 \begin{C++}
    1320 class C {
    1321         int i, j;
    1322         int mem() { §\C{\color{red}// implicit "this" parameter}§
    1323                 i = 1; §\C{\color{red}// this->i}§
    1324                 j = 2; §\C{\color{red}// this->j}§
    1325         }
    1326 }
    1327 \end{C++}
    1328 Since \CFA is non-object-oriented, the equivalent object-oriented program looks like:
    1329 \begin{cfa}
    1330 struct S { int i, j; };
    1331 int mem( S & ®this® ) { §\C{// explicit "this" parameter}§
    1332         ®this.®i = 1; §\C{// "this" is not elided}§
    1333         ®this.®j = 2;
    1334 }
    1335 \end{cfa}
    1336 but it is cumbersome having to write ``©this.©'' many times in a member.
    1337 
    1338 \CFA provides a ©with© clause/statement (see Pascal~\cite[\S~4.F]{Pascal}) to elided the "©this.©" by opening a scope containing field identifiers, changing the qualified fields into variables and giving an opportunity for optimizing qualified references.
    1339 \begin{cfa}
    1340 int mem( S & this ) ®with( this )® { §\C{// with clause}§
    1341         i = 1; §\C{\color{red}// this.i}§
    1342         j = 2; §\C{\color{red}// this.j}§
    1343 }
    1344 \end{cfa}
    1345 which extends to multiple routine parameters:
    1346 \begin{cfa}
    1347 struct T { double m, n; };
    1348 int mem2( S & this1, T & this2 ) ®with( this1, this2 )® {
    1349         i = 1; j = 2;
    1350         m = 1.0; n = 2.0;
    1351 }
    1352 \end{cfa}
    1353 
    1354 The statement form is used within a block:
    1355 \begin{cfa}
    1356 int foo() {
    1357         struct S1 { ... } s1;
    1358         struct S2 { ... } s2;
    1359         ®with( s1 )® { §\C{// with statement}§
    1360                 // access fields of s1 without qualification
    1361                 ®with s2® { §\C{// nesting}§
    1362                         // access fields of s1 and s2 without qualification
    1363                 }
    1364         }
    1365         ®with s1, s2® {
    1366                 // access unambiguous fields of s1 and s2 without qualification
    1367         }
    1368 }
    1369 \end{cfa}
    1370 
    1371 When opening multiple structures, fields with the same name and type are ambiguous and must be fully qualified.
    1372 For fields with the same name but different type, context/cast can be used to disambiguate.
    1373 \begin{cfa}
    1374 struct S { int i; int j; double m; } a, c;
    1375 struct T { int i; int k; int m } b, c;
    1376 with( a, b )
    1377 {
    1378 }
    1379 \end{cfa}
    1380 
    1381 \begin{comment}
    1382 The components in the "with" clause
    1383 
    1384   with a, b, c { ... }
    1385 
    1386 serve 2 purposes: each component provides a type and object. The type must be a
    1387 structure type. Enumerations are already opened, and I think a union is opened
    1388 to some extent, too. (Or is that just unnamed unions?) The object is the target
    1389 that the naked structure-fields apply to. The components are open in "parallel"
    1390 at the scope of the "with" clause/statement, so opening "a" does not affect
    1391 opening "b", etc. This semantic is different from Pascal, which nests the
    1392 openings.
    1393 
    1394 Having said the above, it seems reasonable to allow a "with" component to be an
    1395 expression. The type is the static expression-type and the object is the result
    1396 of the expression. Again, the type must be an aggregate. Expressions require
    1397 parenthesis around the components.
    1398 
    1399   with( a, b, c ) { ... }
    1400 
    1401 Does this now make sense?
    1402 
    1403 Having written more CFA code, it is becoming clear to me that I *really* want
    1404 the "with" to be implemented because I hate having to type all those object
    1405 names for fields. It's a great way to drive people away from the language.
    1406 \end{comment}
     1387void ?{}( S & s, int @i@ ) with ( s ) @with( $\emph{\R{params}}$ )@ { // syntax not allowed, illustration only
     1388        s.i = @i@; j = 3; m = 5.5;
     1389}
     1390\end{cfa}
     1391This implicit semantic matches with programmer expectation.
     1392
    14071393
    14081394
     
    14141400Non-local transfer can cause stack unwinding, \ie non-local routine termination, depending on the kind of raise.
    14151401\begin{cfa}
    1416 exception_t E {}; §\C{// exception type}§
     1402exception_t E {}; $\C{// exception type}$
    14171403void f(...) {
    1418         ... throw E{}; ... §\C{// termination}§
    1419         ... throwResume E{}; ... §\C{// resumption}§
     1404        ... throw E{}; ... $\C{// termination}$
     1405        ... throwResume E{}; ... $\C{// resumption}$
    14201406}
    14211407try {
    14221408        f(...);
    1423 } catch( E e ; §boolean-predicate§ ) {          §\C{// termination handler}§
     1409} catch( E e ; $boolean-predicate$ ) {          $\C{// termination handler}$
    14241410        // recover and continue
    1425 } catchResume( E e ; §boolean-predicate§ ) { §\C{// resumption handler}§
     1411} catchResume( E e ; $boolean-predicate$ ) { $\C{// resumption handler}$
    14261412        // repair and return
    14271413} finally {
     
    14301416\end{cfa}
    14311417The kind of raise and handler match: ©throw© with ©catch© and ©throwResume© with ©catchResume©.
    1432 Then the exception type must match along with any additonal predicate must be true.
     1418Then the exception type must match along with any additional predicate must be true.
    14331419The ©catch© and ©catchResume© handlers may appear in any oder.
    14341420However, the ©finally© clause must appear at the end of the ©try© statement.
     
    14831469For example, a routine returning a \Index{pointer} to an array of integers is defined and used in the following way:
    14841470\begin{cfa}
    1485 int ®(*®f®())[®5®]® {...}; §\C{// definition}§
    1486  ... ®(*®f®())[®3®]® += 1; §\C{// usage}§
     1471int @(*@f@())[@5@]@ {...}; $\C{// definition}$
     1472 ... @(*@f@())[@3@]@ += 1; $\C{// usage}$
    14871473\end{cfa}
    14881474Essentially, the return type is wrapped around the routine name in successive layers (like an \Index{onion}).
     
    14991485\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
    15001486\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
    1501 \begin{cfa}
    1502 ß[5] *ß ®int® x1;
    1503 ß* [5]ß ®int® x2;
    1504 ß[* [5] int]ß f®( int p )®;
     1487\begin{cfa}[moredelim={**[is][\color{blue}]{\#}{\#}}]
     1488#[5] *# @int@ x1;
     1489#* [5]# @int@ x2;
     1490#[* [5] int]# f@( int p )@;
    15051491\end{cfa}
    15061492&
    1507 \begin{cfa}
    1508 ®int® ß*ß x1 ß[5]ß;
    1509 ®int® ß(*ßx2ß)[5]ß;
    1510 ßint (*ßf®( int p )®ß)[5]ß;
     1493\begin{cfa}[moredelim={**[is][\color{blue}]{\#}{\#}}]
     1494@int@ #*# x1 #[5]#;
     1495@int@ #(*#x2#)[5]#;
     1496#int (*#f@( int p )@#)[5]#;
    15111497\end{cfa}
    15121498\end{tabular}
     
    15201506\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
    15211507\begin{cfa}
    1522 ®*® int x, y;
     1508@*@ int x, y;
    15231509\end{cfa}
    15241510&
    15251511\begin{cfa}
    1526 int ®*®x, ®*®y;
     1512int @*@x, @*@y;
    15271513\end{cfa}
    15281514\end{tabular}
     
    15331519\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
    15341520\begin{cfa}
    1535 ®*® int x;
     1521@*@ int x;
    15361522int y;
    15371523\end{cfa}
    15381524&
    15391525\begin{cfa}
    1540 int ®*®x, y;
     1526int @*@x, y;
    15411527
    15421528\end{cfa}
     
    16471633
    16481634\section{Pointer / Reference}
     1635\label{s:PointerReference}
    16491636
    16501637C provides a \newterm{pointer type};
     
    16731660&
    16741661\begin{cfa}
    1675 int * ®const® x = (int *)100
     1662int * @const@ x = (int *)100
    16761663*x = 3;                 // implicit dereference
    1677 int * ®const® y = (int *)104;
     1664int * @const@ y = (int *)104;
    16781665*y = *x;                        // implicit dereference
    16791666\end{cfa}
     
    17131700\begin{tabular}{@{}l@{\hspace{2em}}l@{}}
    17141701\begin{cfa}
    1715 int x, y, ®*® p1, ®*® p2, ®**® p3;
    1716 p1 = ®&®x;     // p1 points to x
     1702int x, y, @*@ p1, @*@ p2, @**@ p3;
     1703p1 = @&@x;     // p1 points to x
    17171704p2 = p1;     // p2 points to x
    1718 p1 = ®&®y;     // p1 points to y
     1705p1 = @&@y;     // p1 points to y
    17191706p3 = &p2;  // p3 points to p2
    17201707\end{cfa}
     
    17281715For example, \Index*{Algol68}~\cite{Algol68} infers pointer dereferencing to select the best meaning for each pointer usage
    17291716\begin{cfa}
    1730 p2 = p1 + x; §\C{// compiler infers *p2 = *p1 + x;}§
     1717p2 = p1 + x; $\C{// compiler infers *p2 = *p1 + x;}$
    17311718\end{cfa}
    17321719Algol68 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.
     
    17361723In C, objects of pointer type always manipulate the pointer object's address:
    17371724\begin{cfa}
    1738 p1 = p2; §\C{// p1 = p2\ \ rather than\ \ *p1 = *p2}§
    1739 p2 = p1 + x; §\C{// p2 = p1 + x\ \ rather than\ \ *p2 = *p1 + x}§
     1725p1 = p2; $\C{// p1 = p2\ \ rather than\ \ *p1 = *p2}$
     1726p2 = p1 + x; $\C{// p2 = p1 + x\ \ rather than\ \ *p2 = *p1 + x}$
    17401727\end{cfa}
    17411728even though the assignment to ©p2© is likely incorrect, and the programmer probably meant:
    17421729\begin{cfa}
    1743 p1 = p2; §\C{// pointer address assignment}§
    1744 ®*®p2 = ®*®p1 + x; §\C{// pointed-to value assignment / operation}§
     1730p1 = p2; $\C{// pointer address assignment}$
     1731@*@p2 = @*@p1 + x; $\C{// pointed-to value assignment / operation}$
    17451732\end{cfa}
    17461733The 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©).
     
    17581745To 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).
    17591746\begin{cfa}
    1760 int x, y, ®&® r1, ®&® r2, ®&&® r3;
    1761 ®&®r1 = &x; §\C{// r1 points to x}§
    1762 ®&®r2 = &r1; §\C{// r2 points to x}§
    1763 ®&®r1 = &y; §\C{// r1 points to y}§
    1764 ®&&®r3 = ®&®&r2; §\C{// r3 points to r2}§
    1765 r2 = ((r1 + r2) * (r3 - r1)) / (r3 - 15); §\C{// implicit dereferencing}§
     1747int x, y, @&@ r1, @&@ r2, @&&@ r3;
     1748@&@r1 = &x; $\C{// r1 points to x}$
     1749@&@r2 = &r1; $\C{// r2 points to x}$
     1750@&@r1 = &y; $\C{// r1 points to y}$
     1751@&&@r3 = @&@&r2; $\C{// r3 points to r2}$
     1752r2 = ((r1 + r2) * (r3 - r1)) / (r3 - 15); $\C{// implicit dereferencing}$
    17661753\end{cfa}
    17671754Except for auto-dereferencing by the compiler, this reference example is the same as the previous pointer example.
     
    17691756One 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:
    17701757\begin{cfa}
    1771 ®*®r2 = ((®*®r1 + ®*®r2) ®*® (®**®r3 - ®*®r1)) / (®**®r3 - 15);
     1758@*@r2 = ((@*@r1 + @*@r2) @*@ (@**@r3 - @*@r1)) / (@**@r3 - 15);
    17721759\end{cfa}
    17731760When a reference operation appears beside a dereference operation, \eg ©&*©, they cancel out.
     
    17781765For a \CFA reference type, the cancellation on the left-hand side of assignment leaves the reference as an address (\Index{lvalue}):
    17791766\begin{cfa}
    1780 (&®*®)r1 = &x; §\C{// (\&*) cancel giving address in r1 not variable pointed-to by r1}§
     1767(&@*@)r1 = &x; $\C{// (\&*) cancel giving address in r1 not variable pointed-to by r1}$
    17811768\end{cfa}
    17821769Similarly, the address of a reference can be obtained for assignment or computation (\Index{rvalue}):
    17831770\begin{cfa}
    1784 (&(&®*®)®*®)r3 = &(&®*®)r2; §\C{// (\&*) cancel giving address in r2, (\&(\&*)*) cancel giving address in r3}§
     1771(&(&@*@)@*@)r3 = &(&@*@)r2; $\C{// (\&*) cancel giving address in r2, (\&(\&*)*) cancel giving address in r3}$
    17851772\end{cfa}
    17861773Cancellation\index{cancellation!pointer/reference}\index{pointer!cancellation} works to arbitrary depth.
     
    17901777int x, *p1 = &x, **p2 = &p1, ***p3 = &p2,
    17911778                 &r1 = x,    &&r2 = r1,   &&&r3 = r2;
    1792 ***p3 = 3; §\C{// change x}§
    1793 r3 = 3; §\C{// change x, ***r3}§
    1794 **p3 = ...; §\C{// change p1}§
    1795 &r3 = ...; §\C{// change r1, (\&*)**r3, 1 cancellation}§
    1796 *p3 = ...; §\C{// change p2}§
    1797 &&r3 = ...; §\C{// change r2, (\&(\&*)*)*r3, 2 cancellations}§
    1798 &&&r3 = p3; §\C{// change r3 to p3, (\&(\&(\&*)*)*)r3, 3 cancellations}§
     1779***p3 = 3; $\C{// change x}$
     1780r3 = 3; $\C{// change x, ***r3}$
     1781**p3 = ...; $\C{// change p1}$
     1782&r3 = ...; $\C{// change r1, (\&*)**r3, 1 cancellation}$
     1783*p3 = ...; $\C{// change p2}$
     1784&&r3 = ...; $\C{// change r2, (\&(\&*)*)*r3, 2 cancellations}$
     1785&&&r3 = p3; $\C{// change r3 to p3, (\&(\&(\&*)*)*)r3, 3 cancellations}$
    17991786\end{cfa}
    18001787Furthermore, both types are equally performant, as the same amount of dereferencing occurs for both types.
     
    18031790As for a pointer type, a reference type may have qualifiers:
    18041791\begin{cfa}
    1805 const int cx = 5; §\C{// cannot change cx;}§
    1806 const int & cr = cx; §\C{// cannot change what cr points to}§
    1807 ®&®cr = &cx; §\C{// can change cr}§
    1808 cr = 7; §\C{// error, cannot change cx}§
    1809 int & const rc = x; §\C{// must be initialized}§
    1810 ®&®rc = &x; §\C{// error, cannot change rc}§
    1811 const int & const crc = cx; §\C{// must be initialized}§
    1812 crc = 7; §\C{// error, cannot change cx}§
    1813 ®&®crc = &cx; §\C{// error, cannot change crc}§
     1792const int cx = 5; $\C{// cannot change cx;}$
     1793const int & cr = cx; $\C{// cannot change what cr points to}$
     1794@&@cr = &cx; $\C{// can change cr}$
     1795cr = 7; $\C{// error, cannot change cx}$
     1796int & const rc = x; $\C{// must be initialized}$
     1797@&@rc = &x; $\C{// error, cannot change rc}$
     1798const int & const crc = cx; $\C{// must be initialized}$
     1799crc = 7; $\C{// error, cannot change cx}$
     1800@&@crc = &cx; $\C{// error, cannot change crc}$
    18141801\end{cfa}
    18151802Hence, 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}:
    18161803\begin{cfa}
    1817 int & const cr = *0; §\C{// where 0 is the int * zero}§
     1804int & const cr = *0; $\C{// where 0 is the int * zero}$
    18181805\end{cfa}
    18191806Note, constant reference-types do not prevent \Index{addressing errors} because of explicit storage-management:
     
    18221809cr = 5;
    18231810free( &cr );
    1824 cr = 7; §\C{// unsound pointer dereference}§
     1811cr = 7; $\C{// unsound pointer dereference}$
    18251812\end{cfa}
    18261813
    18271814The position of the ©const© qualifier \emph{after} the pointer/reference qualifier causes confuse for C programmers.
    18281815The ©const© qualifier cannot be moved before the pointer/reference qualifier for C style-declarations;
    1829 \CFA-style declarations (see \VRef{s:AlternativeDeclarations}) attempt to address this issue:
     1816\CFA-style declarations \see{\VRef{s:AlternativeDeclarations}} attempt to address this issue:
    18301817\begin{cquote}
    18311818\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
    18321819\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
    18331820\begin{cfa}
    1834 ®const® * ®const® * const int ccp;
    1835 ®const® & ®const® & const int ccr;
     1821@const@ * @const@ * const int ccp;
     1822@const@ & @const@ & const int ccr;
    18361823\end{cfa}
    18371824&
    18381825\begin{cfa}
    1839 const int * ®const® * ®const® ccp;
     1826const int * @const@ * @const@ ccp;
    18401827
    18411828\end{cfa}
     
    18461833Finally, like pointers, references are usable and composable with other type operators and generators.
    18471834\begin{cfa}
    1848 int w, x, y, z, & ar[3] = { x, y, z }; §\C{// initialize array of references}§
    1849 &ar[1] = &w; §\C{// change reference array element}§
    1850 typeof( ar[1] ) p; §\C{// (gcc) is int, \ie the type of referenced object}§
    1851 typeof( &ar[1] ) q; §\C{// (gcc) is int \&, \ie the type of reference}§
    1852 sizeof( ar[1] ) == sizeof( int ); §\C{// is true, \ie the size of referenced object}§
    1853 sizeof( &ar[1] ) == sizeof( int *) §\C{// is true, \ie the size of a reference}§
     1835int w, x, y, z, & ar[3] = { x, y, z }; $\C{// initialize array of references}$
     1836&ar[1] = &w; $\C{// change reference array element}$
     1837typeof( ar[1] ) p; $\C{// (gcc) is int, \ie the type of referenced object}$
     1838typeof( &ar[1] ) q; $\C{// (gcc) is int \&, \ie the type of reference}$
     1839sizeof( ar[1] ) == sizeof( int ); $\C{// is true, \ie the size of referenced object}$
     1840sizeof( &ar[1] ) == sizeof( int *) $\C{// is true, \ie the size of a reference}$
    18541841\end{cfa}
    18551842
    18561843In 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}.
    18571844Also, \CC does not allow \Index{array}s\index{array!reference} of reference\footnote{
    1858 The 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.}
     1845The reason for disallowing arrays of reference is unknown, but possibly comes from references being ethereal (like a textual macro), and hence, replaceable by the referent object.}
    18591846\Index*{Java}'s reference types to objects (all Java objects are on the heap) are like C pointers, which always manipulate the address, and there is no (bit-wise) object assignment, so objects are explicitly cloned by shallow or deep copying, which eliminates half of the address duality.
    18601847
     
    18681855Therefore, for pointer/reference initialization, the initializing value must be an address not a value.
    18691856\begin{cfa}
    1870 int * p = &x; §\C{// assign address of x}§
    1871 ®int * p = x;® §\C{// assign value of x}§
    1872 int & r = x; §\C{// must have address of x}§
     1857int * p = &x; $\C{// assign address of x}$
     1858@int * p = x;@ $\C{// assign value of x}$
     1859int & r = x; $\C{// must have address of x}$
    18731860\end{cfa}
    18741861Like 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).
     
    18791866Similarly, 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.
    18801867\begin{cfa}
    1881 int & f( int & r ); §\C{// reference parameter and return}§
    1882 z = f( x ) + f( y ); §\C{// reference operator added, temporaries needed for call results}§
     1868int & f( int & r ); $\C{// reference parameter and return}$
     1869z = f( x ) + f( y ); $\C{// reference operator added, temporaries needed for call results}$
    18831870\end{cfa}
    18841871Within routine ©f©, it is possible to change the argument by changing the corresponding parameter, and parameter ©r© can be locally reassigned within ©f©.
     
    18931880When a pointer/reference parameter has a ©const© value (immutable), it is possible to pass literals and expressions.
    18941881\begin{cfa}
    1895 void f( ®const® int & cr );
    1896 void g( ®const® int * cp );
    1897 f( 3 );                   g( ®&®3 );
    1898 f( x + y );             g( ®&®(x + y) );
     1882void f( @const@ int & cr );
     1883void g( @const@ int * cp );
     1884f( 3 );                   g( @&@3 );
     1885f( x + y );             g( @&@(x + y) );
    18991886\end{cfa}
    19001887Here, 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.
     
    19071894void f( int & r );
    19081895void g( int * p );
    1909 f( 3 );                   g( ®&®3 ); §\C{// compiler implicit generates temporaries}§
    1910 f( x + y );             g( ®&®(x + y) ); §\C{// compiler implicit generates temporaries}§
     1896f( 3 );                   g( @&@3 ); $\C{// compiler implicit generates temporaries}$
     1897f( x + y );             g( @&@(x + y) ); $\C{// compiler implicit generates temporaries}$
    19111898\end{cfa}
    19121899Essentially, there is an implicit \Index{rvalue} to \Index{lvalue} conversion in this case.\footnote{
     
    19191906\begin{cfa}
    19201907void f( int i );
    1921 void (* fp)( int ); §\C{// routine pointer}§
    1922 fp = f; §\C{// reference initialization}§
    1923 fp = &f; §\C{// pointer initialization}§
    1924 fp = *f; §\C{// reference initialization}§
    1925 fp(3); §\C{// reference invocation}§
    1926 (*fp)(3); §\C{// pointer invocation}§
     1908void (* fp)( int ); $\C{// routine pointer}$
     1909fp = f; $\C{// reference initialization}$
     1910fp = &f; $\C{// pointer initialization}$
     1911fp = *f; $\C{// reference initialization}$
     1912fp(3); $\C{// reference invocation}$
     1913(*fp)(3); $\C{// pointer invocation}$
    19271914\end{cfa}
    19281915While 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.
    19291916Instead, a routine object should be referenced by a ©const© reference:
    19301917\begin{cfa}
    1931 ®const® void (®&® fr)( int ) = f; §\C{// routine reference}§
    1932 fr = ... §\C{// error, cannot change code}§
    1933 &fr = ...; §\C{// changing routine reference}§
    1934 fr( 3 ); §\C{// reference call to f}§
    1935 (*fr)(3); §\C{// error, incorrect type}§
     1918@const@ void (@&@ fr)( int ) = f; $\C{// routine reference}$
     1919fr = ... $\C{// error, cannot change code}$
     1920&fr = ...; $\C{// changing routine reference}$
     1921fr( 3 ); $\C{// reference call to f}$
     1922(*fr)(3); $\C{// error, incorrect type}$
    19361923\end{cfa}
    19371924because the value of the routine object is a routine literal, \ie the routine code is normally immutable during execution.\footnote{
     
    19461933\begin{itemize}
    19471934\item
    1948 if ©R© is an \Index{rvalue} of type ©T &©$_1\cdots$ ©&©$_r$, where $r \ge 1$ references (©&© symbols), than ©&R© has type ©T ®*®&©$_{\color{red}2}\cdots$ ©&©$_{\color{red}r}$, \ie ©T© pointer with $r-1$ references (©&© symbols).
    1949 
    1950 \item
    1951 if ©L© is an \Index{lvalue} of type ©T &©$_1\cdots$ ©&©$_l$, where $l \ge 0$ references (©&© symbols), than ©&L© has type ©T ®*®&©$_{\color{red}1}\cdots$ ©&©$_{\color{red}l}$, \ie ©T© pointer with $l$ references (©&© symbols).
     1935if ©R© is an \Index{rvalue} of type ©T &©$_1\cdots$ ©&©$_r$, where $r \ge 1$ references (©&© symbols), than ©&R© has type ©T ©\R{©*©}©&©\R{$_2$}$\cdots$ ©&©\R{$_r$}, \ie ©T© pointer with $r-1$ references (©&© symbols).
     1936
     1937\item
     1938if ©L© is an \Index{lvalue} of type ©T &©$_1\cdots$ ©&©$_l$, where $l \ge 0$ references (©&© symbols), than ©&L© has type ©T ©\R{©*©}©&©\R{$_1$}$\cdots$ ©&©\R{$_l$}, \ie ©T© pointer with $l$ references (©&© symbols).
    19521939\end{itemize}
    19531940The following example shows the first rule applied to different \Index{rvalue} contexts:
     
    19551942int x, * px, ** ppx, *** pppx, **** ppppx;
    19561943int & rx = x, && rrx = rx, &&& rrrx = rrx ;
    1957 x = rrrx; §\C[2.0in]{// rrrx is an lvalue with type int \&\&\& (equivalent to x)}§
    1958 px = &rrrx; §\C{// starting from rrrx, \&rrrx is an rvalue with type int *\&\&\& (\&x)}§
    1959 ppx = &&rrrx; §\C{// starting from \&rrrx, \&\&rrrx is an rvalue with type int **\&\& (\&rx)}§
    1960 pppx = &&&rrrx; §\C{// starting from \&\&rrrx, \&\&\&rrrx is an rvalue with type int ***\& (\&rrx)}§
    1961 ppppx = &&&&rrrx; §\C{// starting from \&\&\&rrrx, \&\&\&\&rrrx is an rvalue with type int **** (\&rrrx)}§
     1944x = rrrx; $\C[2.0in]{// rrrx is an lvalue with type int \&\&\& (equivalent to x)}$
     1945px = &rrrx; $\C{// starting from rrrx, \&rrrx is an rvalue with type int *\&\&\& (\&x)}$
     1946ppx = &&rrrx; $\C{// starting from \&rrrx, \&\&rrrx is an rvalue with type int **\&\& (\&rx)}$
     1947pppx = &&&rrrx; $\C{// starting from \&\&rrrx, \&\&\&rrrx is an rvalue with type int ***\& (\&rrx)}$
     1948ppppx = &&&&rrrx; $\C{// starting from \&\&\&rrrx, \&\&\&\&rrrx is an rvalue with type int **** (\&rrrx)}$
    19621949\end{cfa}
    19631950The following example shows the second rule applied to different \Index{lvalue} contexts:
     
    19651952int x, * px, ** ppx, *** pppx;
    19661953int & rx = x, && rrx = rx, &&& rrrx = rrx ;
    1967 rrrx = 2; §\C{// rrrx is an lvalue with type int \&\&\& (equivalent to x)}§
    1968 &rrrx = px; §\C{// starting from rrrx, \&rrrx is an rvalue with type int *\&\&\& (rx)}§
    1969 &&rrrx = ppx; §\C{// starting from \&rrrx, \&\&rrrx is an rvalue with type int **\&\& (rrx)}§
    1970 &&&rrrx = pppx; §\C{// starting from \&\&rrrx, \&\&\&rrrx is an rvalue with type int ***\& (rrrx)}\CRT§
     1954rrrx = 2; $\C{// rrrx is an lvalue with type int \&\&\& (equivalent to x)}$
     1955&rrrx = px; $\C{// starting from rrrx, \&rrrx is an rvalue with type int *\&\&\& (rx)}$
     1956&&rrrx = ppx; $\C{// starting from \&rrrx, \&\&rrrx is an rvalue with type int **\&\& (rrx)}$
     1957&&&rrrx = pppx; $\C{// starting from \&\&rrrx, \&\&\&rrrx is an rvalue with type int ***\& (rrrx)}\CRT$
    19711958\end{cfa}
    19721959
     
    19811968\begin{cfa}
    19821969int x;
    1983 x + 1; §\C[2.0in]{// lvalue variable (int) converts to rvalue for expression}§
     1970x + 1; $\C[2.0in]{// lvalue variable (int) converts to rvalue for expression}$
    19841971\end{cfa}
    19851972An rvalue has no type qualifiers (©cv©), so the lvalue qualifiers are dropped.
     
    19911978\begin{cfa}
    19921979int x, &r = x, f( int p );
    1993 x = ®r® + f( ®r® ); §\C{// lvalue reference converts to rvalue}§
     1980x = @r@ + f( @r@ ); $\C{// lvalue reference converts to rvalue}$
    19941981\end{cfa}
    19951982An rvalue has no type qualifiers (©cv©), so the reference qualifiers are dropped.
     
    19981985lvalue to reference conversion: \lstinline[deletekeywords=lvalue]@lvalue-type cv1 T@ converts to ©cv2 T &©, which allows implicitly converting variables to references.
    19991986\begin{cfa}
    2000 int x, &r = ®x®, f( int & p ); §\C{// lvalue variable (int) convert to reference (int \&)}§
    2001 f( ®x® ); §\C{// lvalue variable (int) convert to reference (int \&)}§
     1987int x, &r = @x@, f( int & p ); $\C{// lvalue variable (int) convert to reference (int \&)}$
     1988f( @x@ ); $\C{// lvalue variable (int) convert to reference (int \&)}$
    20021989\end{cfa}
    20031990Conversion can restrict a type, where ©cv1© $\le$ ©cv2©, \eg passing an ©int© to a ©const volatile int &©, which has low cost.
     
    20091996\begin{cfa}
    20101997int x, & f( int & p );
    2011 f( ®x + 3® );   §\C[1.5in]{// rvalue parameter (int) implicitly converts to lvalue temporary reference (int \&)}§
    2012 ®&f®(...) = &x; §\C{// rvalue result (int \&) implicitly converts to lvalue temporary reference (int \&)}\CRT§
     1998f( @x + 3@ );   $\C[1.5in]{// rvalue parameter (int) implicitly converts to lvalue temporary reference (int \&)}$
     1999@&f@(...) = &x; $\C{// rvalue result (int \&) implicitly converts to lvalue temporary reference (int \&)}\CRT$
    20132000\end{cfa}
    20142001In both case, modifications to the temporary are inaccessible (\Index{warning}).
     
    21822169The point of the new syntax is to allow returning multiple values from a routine~\cite{Galletly96,CLU}, \eg:
    21832170\begin{cfa}
    2184 ®[ int o1, int o2, char o3 ]® f( int i1, char i2, char i3 ) {
    2185         §\emph{routine body}§
     2171@[ int o1, int o2, char o3 ]@ f( int i1, char i2, char i3 ) {
     2172        $\emph{routine body}$
    21862173}
    21872174\end{cfa}
     
    21942181Declaration qualifiers can only appear at the start of a routine definition, \eg:
    21952182\begin{cfa}
    2196 ®extern® [ int x ] g( int y ) {§\,§}
     2183@extern@ [ int x ] g( int y ) {$\,$}
    21972184\end{cfa}
    21982185Lastly, if there are no output parameters or input parameters, the brackets and/or parentheses must still be specified;
    21992186in 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:
    22002187\begin{cfa}
    2201 [§\,§] g(); §\C{// no input or output parameters}§
    2202 [ void ] g( void ); §\C{// no input or output parameters}§
     2188[$\,$] g(); $\C{// no input or output parameters}$
     2189[ void ] g( void ); $\C{// no input or output parameters}$
    22032190\end{cfa}
    22042191
     
    22182205\begin{cfa}
    22192206typedef int foo;
    2220 int f( int (* foo) ); §\C{// foo is redefined as a parameter name}§
     2207int f( int (* foo) ); $\C{// foo is redefined as a parameter name}$
    22212208\end{cfa}
    22222209The 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.
     
    22262213C-style declarations can be used to declare parameters for \CFA style routine definitions, \eg:
    22272214\begin{cfa}
    2228 [ int ] f( * int, int * ); §\C{// returns an integer, accepts 2 pointers to integers}§
    2229 [ * int, int * ] f( int ); §\C{// returns 2 pointers to integers, accepts an integer}§
     2215[ int ] f( * int, int * ); $\C{// returns an integer, accepts 2 pointers to integers}$
     2216[ * int, int * ] f( int ); $\C{// returns 2 pointers to integers, accepts an integer}$
    22302217\end{cfa}
    22312218The 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:
    22322219\begin{cfa}
    22332220#define ptoa( n, d ) int (*n)[ d ]
    2234 int f( ptoa( p, 5 ) ) ... §\C{// expands to int f( int (*p)[ 5 ] )}§
    2235 [ int ] f( ptoa( p, 5 ) ) ... §\C{// expands to [ int ] f( int (*p)[ 5 ] )}§
     2221int f( ptoa( p, 5 ) ) ... $\C{// expands to int f( int (*p)[ 5 ] )}$
     2222[ int ] f( ptoa( p, 5 ) ) ... $\C{// expands to [ int ] f( int (*p)[ 5 ] )}$
    22362223\end{cfa}
    22372224Again, programmers are highly encouraged to use one declaration form or the other, rather than mixing the forms.
     
    22522239\begin{minipage}{\linewidth}
    22532240\begin{cfa}
    2254 ®[ int x, int y ]® f() {
     2241@[ int x, int y ]@ f() {
    22552242        int z;
    22562243        ... x = 0; ... y = z; ...
    2257         ®return;® §\C{// implicitly return x, y}§
     2244        @return;@ $\C{// implicitly return x, y}$
    22582245}
    22592246\end{cfa}
     
    22652252[ int x, int y ] f() {
    22662253        ...
    2267 } §\C{// implicitly return x, y}§
     2254} $\C{// implicitly return x, y}$
    22682255\end{cfa}
    22692256In this case, the current values of ©x© and ©y© are returned to the calling routine just as if a ©return© had been encountered.
     
    22742261[ int x, int y ] f( int, x, int y ) {
    22752262        ...
    2276 } §\C{// implicitly return x, y}§
     2263} $\C{// implicitly return x, y}$
    22772264\end{cfa}
    22782265This notation allows the compiler to eliminate temporary variables in nested routine calls.
    22792266\begin{cfa}
    2280 [ int x, int y ] f( int, x, int y ); §\C{// prototype declaration}§
     2267[ int x, int y ] f( int, x, int y ); $\C{// prototype declaration}$
    22812268int a, b;
    22822269[a, b] = f( f( f( a, b ) ) );
     
    22922279as well, parameter names are optional, \eg:
    22932280\begin{cfa}
    2294 [ int x ] f (); §\C{// returning int with no parameters}§
    2295 [ * int ] g (int y); §\C{// returning pointer to int with int parameter}§
    2296 [ ] h ( int, char ); §\C{// returning no result with int and char parameters}§
    2297 [ * int, int ] j ( int ); §\C{// returning pointer to int and int, with int parameter}§
     2281[ int x ] f (); $\C{// returning int with no parameters}$
     2282[ * int ] g (int y); $\C{// returning pointer to int with int parameter}$
     2283[ ] h ( int, char ); $\C{// returning no result with int and char parameters}$
     2284[ * int, int ] j ( int ); $\C{// returning pointer to int and int, with int parameter}$
    22982285\end{cfa}
    22992286This syntax allows a prototype declaration to be created by cutting and pasting source text from the routine definition header (or vice versa).
    2300 Like C, it is possible to declare multiple routine-prototypes in a single declaration, where the return type is distributed across \emph{all} routine names in the declaration list (see~\VRef{s:AlternativeDeclarations}), \eg:
     2287Like C, it is possible to declare multiple routine-prototypes in a single declaration, where the return type is distributed across \emph{all} routine names in the declaration list \see{\VRef{s:AlternativeDeclarations}}, \eg:
    23012288\begin{cfa}
    23022289C :             const double bar1(), bar2( int ), bar3( double );
    2303 §\CFA§: [const double] foo(), foo( int ), foo( double ) { return 3.0; }
     2290$\CFA$: [const double] foo(), foo( int ), foo( double ) { return 3.0; }
    23042291\end{cfa}
    23052292\CFA allows the last routine in the list to define its body.
     
    23162303The syntax for pointers to \CFA routines specifies the pointer name on the right, \eg:
    23172304\begin{cfa}
    2318 * [ int x ] () fp; §\C{// pointer to routine returning int with no parameters}§
    2319 * [ * int ] (int y) gp; §\C{// pointer to routine returning pointer to int with int parameter}§
    2320 * [ ] (int,char) hp; §\C{// pointer to routine returning no result with int and char parameters}§
    2321 * [ * int,int ] ( int ) jp; §\C{// pointer to routine returning pointer to int and int, with int parameter}§
     2305* [ int x ] () fp; $\C[2.25in]{// pointer to routine returning int with no parameters}$
     2306* [ * int ] (int y) gp; $\C{// pointer to routine returning pointer to int with int parameter}$
     2307* [ ] (int,char) hp; $\C{// pointer to routine returning no result with int and char parameters}$
     2308* [ * int,int ] ( int ) jp; $\C{// pointer to routine returning pointer to int and int, with int parameter}\CRT$
    23222309\end{cfa}
    23232310While parameter names are optional, \emph{a routine name cannot be specified};
    23242311for example, the following is incorrect:
    23252312\begin{cfa}
    2326 * [ int x ] f () fp; §\C{// routine name "f" is not allowed}§
     2313* [ int x ] f () fp; $\C{// routine name "f" is not allowed}$
    23272314\end{cfa}
    23282315
     
    23472334whereas a named (keyword) call may be:
    23482335\begin{cfa}
    2349 p( z : 3, x : 4, y : 7 );  §\C{// rewrite $\Rightarrow$ p( 4, 7, 3 )}§
     2336p( z : 3, x : 4, y : 7 );  $\C{// rewrite \(\Rightarrow\) p( 4, 7, 3 )}$
    23502337\end{cfa}
    23512338Here the order of the arguments is unimportant, and the names of the parameters are used to associate argument values with the corresponding parameters.
     
    23642351For example, the following routine prototypes and definition are all valid.
    23652352\begin{cfa}
    2366 void p( int, int, int ); §\C{// equivalent prototypes}§
     2353void p( int, int, int ); $\C{// equivalent prototypes}$
    23672354void p( int x, int y, int z );
    23682355void p( int y, int x, int z );
    23692356void p( int z, int y, int x );
    2370 void p( int q, int r, int s ) {} §\C{// match with this definition}§
     2357void p( int q, int r, int s ) {} $\C{// match with this definition}$
    23712358\end{cfa}
    23722359Forcing matching parameter names in routine prototypes with corresponding routine definitions is possible, but goes against a strong tradition in C programming.
     
    23802367int f( int x, double y );
    23812368
    2382 f( j : 3, i : 4 ); §\C{// 1st f}§
    2383 f( x : 7, y : 8.1 ); §\C{// 2nd f}§
    2384 f( 4, 5 );  §\C{// ambiguous call}§
     2369f( j : 3, i : 4 ); $\C{// 1st f}$
     2370f( x : 7, y : 8.1 ); $\C{// 2nd f}$
     2371f( 4, 5 );  $\C{// ambiguous call}$
    23852372\end{cfa}
    23862373However, named arguments compound routine resolution in conjunction with conversions:
    23872374\begin{cfa}
    2388 f( i : 3, 5.7 ); §\C{// ambiguous call ?}§
     2375f( i : 3, 5.7 ); $\C{// ambiguous call ?}$
    23892376\end{cfa}
    23902377Depending on the cost associated with named arguments, this call could be resolvable or ambiguous.
     
    24002387the allowable positional calls are:
    24012388\begin{cfa}
    2402 p(); §\C{// rewrite $\Rightarrow$ p( 1, 2, 3 )}§
    2403 p( 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 2, 3 )}§
    2404 p( 4, 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 4, 3 )}§
    2405 p( 4, 4, 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 4, 4 )}§
     2389p(); $\C{// rewrite \(\Rightarrow\) p( 1, 2, 3 )}$
     2390p( 4 ); $\C{// rewrite \(\Rightarrow\) p( 4, 2, 3 )}$
     2391p( 4, 4 ); $\C{// rewrite \(\Rightarrow\) p( 4, 4, 3 )}$
     2392p( 4, 4, 4 ); $\C{// rewrite \(\Rightarrow\) p( 4, 4, 4 )}$
    24062393// empty arguments
    2407 p(  , 4, 4 ); §\C{// rewrite $\Rightarrow$ p( 1, 4, 4 )}§
    2408 p( 4,  , 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 2, 4 )}§
    2409 p( 4, 4,   ); §\C{// rewrite $\Rightarrow$ p( 4, 4, 3 )}§
    2410 p( 4,  ,   ); §\C{// rewrite $\Rightarrow$ p( 4, 2, 3 )}§
    2411 p(  , 4,   ); §\C{// rewrite $\Rightarrow$ p( 1, 4, 3 )}§
    2412 p(  ,  , 4 ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 4 )}§
    2413 p(  ,  ,   ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 3 )}§
     2394p(  , 4, 4 ); $\C{// rewrite \(\Rightarrow\) p( 1, 4, 4 )}$
     2395p( 4,  , 4 ); $\C{// rewrite \(\Rightarrow\) p( 4, 2, 4 )}$
     2396p( 4, 4,   ); $\C{// rewrite \(\Rightarrow\) p( 4, 4, 3 )}$
     2397p( 4,  ,   ); $\C{// rewrite \(\Rightarrow\) p( 4, 2, 3 )}$
     2398p(  , 4,   ); $\C{// rewrite \(\Rightarrow\) p( 1, 4, 3 )}$
     2399p(  ,  , 4 ); $\C{// rewrite \(\Rightarrow\) p( 1, 2, 4 )}$
     2400p(  ,  ,   ); $\C{// rewrite \(\Rightarrow\) p( 1, 2, 3 )}$
    24142401\end{cfa}
    24152402Here the missing arguments are inserted from the default values in the parameter list.
     
    24352422Default values may only appear in a prototype versus definition context:
    24362423\begin{cfa}
    2437 void p( int x, int y = 2, int z = 3 ); §\C{// prototype: allowed}§
    2438 void p( int, int = 2, int = 3 ); §\C{// prototype: allowed}§
    2439 void p( int x, int y = 2, int z = 3 ) {} §\C{// definition: not allowed}§
     2424void p( int x, int y = 2, int z = 3 ); $\C{// prototype: allowed}$
     2425void p( int, int = 2, int = 3 ); $\C{// prototype: allowed}$
     2426void p( int x, int y = 2, int z = 3 ) {} $\C{// definition: not allowed}$
    24402427\end{cfa}
    24412428The reason for this restriction is to allow separate compilation.
     
    24522439\begin{cfa}
    24532440p( int x, int y, int z, ... );
    2454 p( 1, 4, 5, 6, z : 3, y : 2 ); §\C{// assume p( /* positional */, ... , /* named */ );}§
    2455 p( 1, z : 3, y : 2, 4, 5, 6 ); §\C{// assume p( /* positional */, /* named */, ... );}§
     2441p( 1, 4, 5, 6, z : 3, y : 2 ); $\C{// assume p( /* positional */, ... , /* named */ );}$
     2442p( 1, z : 3, y : 2, 4, 5, 6 ); $\C{// assume p( /* positional */, /* named */, ... );}$
    24562443\end{cfa}
    24572444In 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.
     
    24622449\begin{cfa}
    24632450void p( int x, int y = 2, int z = 3... );
    2464 p( 1, 4, 5, 6, z : 3 ); §\C{// assume p( /* positional */, ... , /* named */ );}§
    2465 p( 1, z : 3, 4, 5, 6 ); §\C{// assume p( /* positional */, /* named */, ... );}§
     2451p( 1, 4, 5, 6, z : 3 ); $\C{// assume p( /* positional */, ... , /* named */ );}$
     2452p( 1, z : 3, 4, 5, 6 ); $\C{// assume p( /* positional */, /* named */, ... );}$
    24662453\end{cfa}
    24672454The first call is an error because arguments 4 and 5 are actually positional not ellipse arguments;
     
    24692456In the second call, the default value for y is implicitly inserted after argument 1 and the named arguments separate the positional and ellipse arguments, making it trivial to read the call.
    24702457For these reasons, \CFA requires named arguments before ellipse arguments.
    2471 Finally, while ellipse arguments are needed for a small set of existing C routines, like printf, the extended \CFA type system largely eliminates the need for ellipse arguments (see Section 24), making much of this discussion moot.
    2472 
    2473 Default arguments and overloading (see Section 24) are complementary.
     2458Finally, while ellipse arguments are needed for a small set of existing C routines, like ©printf©, the extended \CFA type system largely eliminates the need for ellipse arguments \see{\VRef{s:Overloading}}, making much of this discussion moot.
     2459
     2460Default arguments and overloading \see{\VRef{s:Overloading}} are complementary.
    24742461While in theory default arguments can be simulated with overloading, as in:
    24752462\begin{cquote}
     
    24932480Furthermore, overloading cannot handle accessing default arguments in the middle of a positional list, via a missing argument, such as:
    24942481\begin{cfa}
    2495 p( 1, /* default */, 5 ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 5 )}§
     2482p( 1, /* default */, 5 ); $\C{// rewrite \(\Rightarrow\) p( 1, 2, 5 )}$
    24962483\end{cfa}
    24972484
     
    25062493\begin{cfa}
    25072494struct {
    2508         int f1; §\C{// named field}§
    2509         int f2 : 4; §\C{// named field with bit field size}§
    2510         int : 3; §\C{// unnamed field for basic type with bit field size}§
    2511         int ; §\C{// disallowed, unnamed field}§
    2512         int *; §\C{// disallowed, unnamed field}§
    2513         int (*)( int ); §\C{// disallowed, unnamed field}§
     2495        int f1; $\C{// named field}$
     2496        int f2 : 4; $\C{// named field with bit field size}$
     2497        int : 3; $\C{// unnamed field for basic type with bit field size}$
     2498        int ; $\C{// disallowed, unnamed field}$
     2499        int *; $\C{// disallowed, unnamed field}$
     2500        int (*)( int ); $\C{// disallowed, unnamed field}$
    25142501};
    25152502\end{cfa}
     
    25192506\begin{cfa}
    25202507struct {
    2521         int , , ; §\C{// 3 unnamed fields}§
     2508        int , , ; $\C{// 3 unnamed fields}$
    25222509}
    25232510\end{cfa}
     
    25312518\subsection{Type Nesting}
    25322519
    2533 \CFA allows \Index{type nesting}, and type qualification of the nested types (see \VRef[Figure]{f:TypeNestingQualification}), where as C hoists\index{type hoisting} (refactors) nested types into the enclosing scope and has no type qualification.
     2520\CFA allows \Index{type nesting}, and type qualification of the nested types \see{\VRef[Figure]{f:TypeNestingQualification}}, where as C hoists\index{type hoisting} (refactors) nested types into the enclosing scope and has no type qualification.
    25342521\begin{figure}
    25352522\centering
     
    25872574
    25882575int fred() {
    2589         s.t.c = ®S.®R;  // type qualification
    2590         struct ®S.®T t = { ®S.®R, 1, 2 };
    2591         enum ®S.®C c;
    2592         union ®S.T.®U u;
     2576        s.t.c = @S.@R;  // type qualification
     2577        struct @S.@T t = { @S.@R, 1, 2 };
     2578        enum @S.@C c;
     2579        union @S.T.@U u;
    25932580}
    25942581\end{cfa}
     
    26132600const unsigned int size = 5;
    26142601int ia[size];
    2615 ... §\C{// assign values to array ia}§
    2616 qsort( ia, size ); §\C{// sort ascending order using builtin ?<?}§
     2602... $\C{// assign values to array ia}$
     2603qsort( ia, size ); $\C{// sort ascending order using builtin ?<?}$
    26172604{
    2618         ®int ?<?( int x, int y ) { return x > y; }® §\C{// nested routine}§
    2619         qsort( ia, size ); §\C{// sort descending order by local redefinition}§
     2605        @int ?<?( int x, int y ) { return x > y; }@ $\C{// nested routine}$
     2606        qsort( ia, size ); $\C{// sort descending order by local redefinition}$
    26202607}
    26212608\end{cfa}
     
    26252612The following program in undefined in \CFA (and Indexc{gcc})
    26262613\begin{cfa}
    2627 [* [int]( int )] foo() { §\C{// int (* foo())( int )}§
    2628         int ®i® = 7;
     2614[* [int]( int )] foo() { $\C{// int (* foo())( int )}$
     2615        int @i@ = 7;
    26292616        int bar( int p ) {
    2630                 ®i® += 1; §\C{// dependent on local variable}§
    2631                 sout | ®i®;
     2617                @i@ += 1; $\C{// dependent on local variable}$
     2618                sout | @i@;
    26322619        }
    2633         return bar; §\C{// undefined because of local dependence}§
     2620        return bar; $\C{// undefined because of local dependence}$
    26342621}
    26352622int main() {
    2636         * [int]( int ) fp = foo(); §\C{// int (* fp)( int )}§
     2623        * [int]( int ) fp = foo(); $\C{// int (* fp)( int )}$
    26372624        sout | fp( 3 );
    26382625}
     
    26472634In C and \CFA, lists of elements appear in several contexts, such as the parameter list of a routine call.
    26482635\begin{cfa}
    2649 f( ®2, x, 3 + i® ); §\C{// element list}§
     2636f( @2, x, 3 + i@ ); $\C{// element list}$
    26502637\end{cfa}
    26512638A list of elements is called a \newterm{tuple}, and is different from a \Index{comma expression}.
     
    26562643
    26572644In C and most programming languages, functions return at most one value;
    2658 however, many operations have multiple outcomes, some exceptional (see~\VRef{s:ExceptionHandling}).
     2645however, many operations have multiple outcomes, some exceptional \see{\VRef{s:ExceptionHandling}}.
    26592646To emulate functions with multiple return values, \emph{\Index{aggregation}} and/or \emph{\Index{aliasing}} is used.
    26602647
     
    26622649For example, consider C's \Indexc{div} function, which returns the quotient and remainder for a division of an integer value.
    26632650\begin{cfa}
    2664 typedef struct { int quot, rem; } div_t;        §\C[7cm]{// from include stdlib.h}§
     2651typedef struct { int quot, rem; } div_t;        $\C[7cm]{// from include stdlib.h}$
    26652652div_t div( int num, int den );
    2666 div_t qr = div( 13, 5 ); §\C{// return quotient/remainder aggregate}§
    2667 printf( "%d %d\n", qr.quot, qr.rem ); §\C{// print quotient/remainder}§
     2653div_t qr = div( 13, 5 ); $\C{// return quotient/remainder aggregate}$
     2654printf( "%d %d\n", qr.quot, qr.rem ); $\C{// print quotient/remainder}$
    26682655\end{cfa}
    26692656This approach requires a name for the return type and fields, where \Index{naming} is a common programming-language issue.
     
    26752662For example, consider C's \Indexc{modf} function, which returns the integral and fractional part of a floating value.
    26762663\begin{cfa}
    2677 double modf( double x, double * i ); §\C{// from include math.h}§
    2678 double intp, frac = modf( 13.5, &intp ); §\C{// return integral and fractional components}§
    2679 printf( "%g %g\n", intp, frac ); §\C{// print integral/fractional components}§
     2664double modf( double x, double * i ); $\C{// from include math.h}$
     2665double intp, frac = modf( 13.5, &intp ); $\C{// return integral and fractional components}$
     2666printf( "%g %g\n", intp, frac ); $\C{// print integral/fractional components}$
    26802667\end{cfa}
    26812668This approach requires allocating storage for the return values, which complicates the call site with a sequence of variable declarations leading to the call.
     
    27042691When a function call is passed as an argument to another call, the best match of actual arguments to formal parameters is evaluated given all possible expression interpretations in the current scope.
    27052692\begin{cfa}
    2706 void g( int, int ); §\C{// 1}§
    2707 void g( double, double ); §\C{// 2}§
    2708 g( div( 13, 5 ) ); §\C{// select 1}§
    2709 g( modf( 13.5 ) ); §\C{// select 2}§
     2693void g( int, int ); $\C{// 1}$
     2694void g( double, double ); $\C{// 2}$
     2695g( div( 13, 5 ) ); $\C{// select 1}$
     2696g( modf( 13.5 ) ); $\C{// select 2}$
    27102697\end{cfa}
    27112698In this case, there are two overloaded ©g© routines.
     
    27162703The previous examples can be rewritten passing the multiple returned-values directly to the ©printf© function call.
    27172704\begin{cfa}
    2718 [ int, int ] div( int x, int y ); §\C{// from include stdlib}§
    2719 printf( "%d %d\n", div( 13, 5 ) ); §\C{// print quotient/remainder}§
    2720 
    2721 [ double, double ] modf( double x ); §\C{// from include math}§
    2722 printf( "%g %g\n", modf( 13.5 ) ); §\C{// print integral/fractional components}§
     2705[ int, int ] div( int x, int y ); $\C{// from include stdlib}$
     2706printf( "%d %d\n", div( 13, 5 ) ); $\C{// print quotient/remainder}$
     2707
     2708[ double, double ] modf( double x ); $\C{// from include math}$
     2709printf( "%g %g\n", modf( 13.5 ) ); $\C{// print integral/fractional components}$
    27232710\end{cfa}
    27242711This approach provides the benefits of compile-time checking for appropriate return statements as in aggregation, but without the required verbosity of declaring a new named type.
     
    27302717\begin{cfa}
    27312718int quot, rem;
    2732 [ quot, rem ] = div( 13, 5 ); §\C{// assign multiple variables}§
    2733 printf( "%d %d\n", quot, rem ); §\C{// print quotient/remainder}\CRT§
     2719[ quot, rem ] = div( 13, 5 ); $\C{// assign multiple variables}$
     2720printf( "%d %d\n", quot, rem ); $\C{// print quotient/remainder}\CRT$
    27342721\end{cfa}
    27352722Here, the multiple return-values are matched in much the same way as passing multiple return-values to multiple parameters in a call.
     
    27602747In \CFA, it is possible to overcome this restriction by declaring a \newterm{tuple variable}.
    27612748\begin{cfa}
    2762 [int, int] ®qr® = div( 13, 5 ); §\C{// initialize tuple variable}§
    2763 printf( "%d %d\n", ®qr® ); §\C{// print quotient/remainder}§
     2749[int, int] @qr@ = div( 13, 5 ); $\C{// initialize tuple variable}$
     2750printf( "%d %d\n", @qr@ ); $\C{// print quotient/remainder}$
    27642751\end{cfa}
    27652752It is now possible to match the multiple return-values to a single variable, in much the same way as \Index{aggregation}.
     
    27672754One way to access the individual components of a tuple variable is with assignment.
    27682755\begin{cfa}
    2769 [ quot, rem ] = qr; §\C{// assign multiple variables}§
     2756[ quot, rem ] = qr; $\C{// assign multiple variables}$
    27702757\end{cfa}
    27712758
     
    27902777[int, double] * p;
    27912778
    2792 int y = x.0; §\C{// access int component of x}§
    2793 y = f().1; §\C{// access int component of f}§
    2794 p->0 = 5; §\C{// access int component of tuple pointed-to by p}§
    2795 g( x.1, x.0 ); §\C{// rearrange x to pass to g}§
    2796 double z = [ x, f() ].0.1; §\C{// access second component of first component of tuple expression}§
     2779int y = x.0; $\C{// access int component of x}$
     2780y = f().1; $\C{// access int component of f}$
     2781p->0 = 5; $\C{// access int component of tuple pointed-to by p}$
     2782g( x.1, x.0 ); $\C{// rearrange x to pass to g}$
     2783double z = [ x, f() ].0.1; $\C{// access second component of first component of tuple expression}$
    27972784\end{cfa}
    27982785Tuple-index expressions can occur on any tuple-typed expression, including tuple-returning functions, square-bracketed tuple expressions, and other tuple-index expressions, provided the retrieved component is also a tuple.
     
    28012788
    28022789\subsection{Flattening and Structuring}
     2790\label{s:FlatteningStructuring}
    28032791
    28042792As evident in previous examples, tuples in \CFA do not have a rigid structure.
     
    28612849double y;
    28622850[int, double] z;
    2863 [y, x] = 3.14; §\C{// mass assignment}§
    2864 [x, y] = z;                                                         §\C{// multiple assignment}§
    2865 z = 10;                                                         §\C{// mass assignment}§
    2866 z = [x, y]; §\C{// multiple assignment}§
     2851[y, x] = 3.14; $\C{// mass assignment}$
     2852[x, y] = z;                                                         $\C{// multiple assignment}$
     2853z = 10;                                                         $\C{// mass assignment}$
     2854z = [x, y]; $\C{// multiple assignment}$
    28672855\end{cfa}
    28682856Let $L_i$ for $i$ in $[0, n)$ represent each component of the flattened left side, $R_i$ represent each component of the flattened right side of a multiple assignment, and $R$ represent the right side of a mass assignment.
     
    28722860\begin{cfa}
    28732861[ int, int ] x, y, z;
    2874 [ x, y ] = z;                                              §\C{// multiple assignment, invalid 4 != 2}§
     2862[ x, y ] = z;                                              $\C{// multiple assignment, invalid 4 != 2}$
    28752863\end{cfa}
    28762864Multiple assignment assigns $R_i$ to $L_i$ for each $i$.
     
    29082896        double c, d;
    29092897        [ void ] f( [ int, int ] );
    2910         f( [ c, a ] = [ b, d ] = 1.5 ); §\C{// assignments in parameter list}§
     2898        f( [ c, a ] = [ b, d ] = 1.5 ); $\C{// assignments in parameter list}$
    29112899\end{cfa}
    29122900The tuple expression begins with a mass assignment of ©1.5© into ©[b, d]©, which assigns ©1.5© into ©b©, which is truncated to ©1©, and ©1.5© into ©d©, producing the tuple ©[1, 1.5]© as a result.
     
    29212909\begin{cfa}
    29222910struct S;
    2923 void ?{}(S *); §\C{// (1)}§
    2924 void ?{}(S *, int); §\C{// (2)}§
    2925 void ?{}(S * double); §\C{// (3)}§
    2926 void ?{}(S *, S); §\C{// (4)}§
    2927 
    2928 [S, S] x = [3, 6.28]; §\C{// uses (2), (3), specialized constructors}§
    2929 [S, S] y; §\C{// uses (1), (1), default constructor}§
    2930 [S, S] z = x.0; §\C{// uses (4), (4), copy constructor}§
     2911void ?{}(S *); $\C{// (1)}$
     2912void ?{}(S *, int); $\C{// (2)}$
     2913void ?{}(S * double); $\C{// (3)}$
     2914void ?{}(S *, S); $\C{// (4)}$
     2915
     2916[S, S] x = [3, 6.28]; $\C{// uses (2), (3), specialized constructors}$
     2917[S, S] y; $\C{// uses (1), (1), default constructor}$
     2918[S, S] z = x.0; $\C{// uses (4), (4), copy constructor}$
    29312919\end{cfa}
    29322920In this example, ©x© is initialized by the multiple constructor calls ©?{}(&x.0, 3)© and ©?{}(&x.1, 6.28)©, while ©y© is initialized by two default constructor calls ©?{}(&y.0)© and ©?{}(&y.1)©.
     
    29692957A member-access tuple may be used anywhere a tuple can be used, \eg:
    29702958\begin{cfa}
    2971 s.[ y, z, x ] = [ 3, 3.2, 'x' ]; §\C{// equivalent to s.x = 'x', s.y = 3, s.z = 3.2}§
    2972 f( s.[ y, z ] ); §\C{// equivalent to f( s.y, s.z )}§
     2959s.[ y, z, x ] = [ 3, 3.2, 'x' ]; $\C{// equivalent to s.x = 'x', s.y = 3, s.z = 3.2}$
     2960f( s.[ y, z ] ); $\C{// equivalent to f( s.y, s.z )}$
    29732961\end{cfa}
    29742962Note, the fields appearing in a record-field tuple may be specified in any order;
     
    29802968void f( double, long );
    29812969
    2982 f( x.[ 0, 3 ] ); §\C{// f( x.0, x.3 )}§
    2983 x.[ 0, 1 ] = x.[ 1, 0 ]; §\C{// [ x.0, x.1 ] = [ x.1, x.0 ]}§
     2970f( x.[ 0, 3 ] ); $\C{// f( x.0, x.3 )}$
     2971x.[ 0, 1 ] = x.[ 1, 0 ]; $\C{// [ x.0, x.1 ] = [ x.1, x.0 ]}$
    29842972[ long, int, long ] y = x.[ 2, 0, 2 ];
    29852973\end{cfa}
     
    29982986\begin{cfa}
    29992987[ int, float, double ] f();
    3000 [ double, float ] x = f().[ 2, 1 ]; §\C{// f() called once}§
     2988[ double, float ] x = f().[ 2, 1 ]; $\C{// f() called once}$
    30012989\end{cfa}
    30022990
     
    30112999That is, a cast can be used to select the type of an expression when it is ambiguous, as in the call to an overloaded function.
    30123000\begin{cfa}
    3013 int f(); §\C{// (1)}§
    3014 double f(); §\C{// (2)}§
    3015 
    3016 f(); §\C{// ambiguous - (1),(2) both equally viable}§
    3017 (int)f(); §\C{// choose (2)}§
     3001int f(); $\C{// (1)}$
     3002double f(); $\C{// (2)}$
     3003
     3004f(); $\C{// ambiguous - (1),(2) both equally viable}$
     3005(int)f(); $\C{// choose (2)}$
    30183006\end{cfa}
    30193007Since casting is a fundamental operation in \CFA, casts need to be given a meaningful interpretation in the context of tuples.
     
    30233011void g();
    30243012
    3025 (void)f(); §\C{// valid, ignore results}§
    3026 (int)g(); §\C{// invalid, void cannot be converted to int}§
     3013(void)f(); $\C{// valid, ignore results}$
     3014(int)g(); $\C{// invalid, void cannot be converted to int}$
    30273015
    30283016struct A { int x; };
    3029 (struct A)f(); §\C{// invalid, int cannot be converted to A}§
     3017(struct A)f(); $\C{// invalid, int cannot be converted to A}$
    30303018\end{cfa}
    30313019In C, line 4 is a valid cast, which calls ©f© and discards its result.
     
    30433031        [int, [int, int], int] g();
    30443032
    3045         ([int, double])f(); §\C{// (1) valid}§
    3046         ([int, int, int])g(); §\C{// (2) valid}§
    3047         ([void, [int, int]])g(); §\C{// (3) valid}§
    3048         ([int, int, int, int])g(); §\C{// (4) invalid}§
    3049         ([int, [int, int, int]])g(); §\C{// (5) invalid}§
     3033        ([int, double])f(); $\C{// (1) valid}$
     3034        ([int, int, int])g(); $\C{// (2) valid}$
     3035        ([void, [int, int]])g(); $\C{// (3) valid}$
     3036        ([int, int, int, int])g(); $\C{// (4) invalid}$
     3037        ([int, [int, int, int]])g(); $\C{// (5) invalid}$
    30503038\end{cfa}
    30513039
     
    31073095void f([int, int], int, int);
    31083096
    3109 f([0, 0], 0, 0); §\C{// no cost}§
    3110 f(0, 0, 0, 0); §\C{// cost for structuring}§
    3111 f([0, 0,], [0, 0]); §\C{// cost for flattening}§
    3112 f([0, 0, 0], 0); §\C{// cost for flattening and structuring}§
     3097f([0, 0], 0, 0); $\C{// no cost}$
     3098f(0, 0, 0, 0); $\C{// cost for structuring}$
     3099f([0, 0,], [0, 0]); $\C{// cost for flattening}$
     3100f([0, 0, 0], 0); $\C{// cost for flattening and structuring}$
    31133101\end{cfa}
    31143102
     
    31463134The general syntax of a lexical list is:
    31473135\begin{cfa}
    3148 [ §\emph{exprlist}§ ]
     3136[ $\emph{exprlist}$ ]
    31493137\end{cfa}
    31503138where ©$\emph{exprlist}$© is a list of one or more expressions separated by commas.
     
    31583146Tuples 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.
    31593147Note, a tuple is not a record (structure);
    3160 a record denotes a single value with substructure, whereas a tuple is multiple values with no substructure (see flattening coercion in Section 12.1).
     3148a record denotes a single value with substructure, whereas a tuple is multiple values with no substructure \see{flattening coercion in \VRef{s:FlatteningStructuring}}.
    31613149In essence, tuples are largely a compile time phenomenon, having little or no runtime presence.
    31623150
     
    31663154The general syntax of a tuple type is:
    31673155\begin{cfa}
    3168 [ §\emph{typelist}§ ]
     3156[ $\emph{typelist}$ ]
    31693157\end{cfa}
    31703158where ©$\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.
     
    31733161[ unsigned int, char ]
    31743162[ double, double, double ]
    3175 [ * int, int * ] §\C{// mix of CFA and ANSI}§
     3163[ * int, int * ] $\C{// mix of CFA and ANSI}$
    31763164[ * [ 5 ] int, * * char, * [ [ int, int ] ] (int, int) ]
    31773165\end{cfa}
     
    31803168Examples of declarations using tuple types are:
    31813169\begin{cfa}
    3182 [ int, int ] x; §\C{// 2 element tuple, each element of type int}§
    3183 * [ char, char ] y; §\C{// pointer to a 2 element tuple}§
     3170[ int, int ] x; $\C{// 2 element tuple, each element of type int}$
     3171* [ char, char ] y; $\C{// pointer to a 2 element tuple}$
    31843172[ [ int, int ] ] z ([ int, int ]);
    31853173\end{cfa}
     
    31983186[ int, int ] w1;
    31993187[ int, int, int ] w2;
    3200 [ void ] f (int, int, int); §\C{// three input parameters of type int}§
    3201 [ void ] g ([ int, int, int ]); §\C{3 element tuple as input}§
     3188[ void ] f (int, int, int); $\C{// three input parameters of type int}$
     3189[ void ] g ([ int, int, int ]); $\C{3 element tuple as input}$
    32023190f( [ 1, 2, 3 ] );
    32033191f( w1, 3 );
     
    32793267[ int, int, int, int ] w = [ 1, 2, 3, 4 ];
    32803268int x = 5;
    3281 [ x, w ] = [ w, x ]; §\C{// all four tuple coercions}§
     3269[ x, w ] = [ w, x ]; $\C{// all four tuple coercions}$
    32823270\end{cfa}
    32833271Starting on the right-hand tuple in the last assignment statement, w is opened, producing a tuple of four values;
     
    32853273This 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.
    32863274The tuple ©[ 2, 3, 4, 5 ]© is then closed to create a tuple value.
    3287 Finally, ©x© is assigned ©1© and ©w© is assigned the tuple value using multiple assignment (see Section 14).
     3275Finally, ©x© is assigned ©1© and ©w© is assigned the tuple value using \Index{multiple assignment} \see{\VRef{s:TupleAssignment}}.
    32883276\begin{rationale}
    32893277A possible additional language extension is to use the structuring coercion for tuples to initialize a complex record with a tuple.
     
    32963284Mass assignment has the following form:
    32973285\begin{cfa}
    3298 [ §\emph{lvalue}§, ... , §\emph{lvalue}§ ] = §\emph{expr}§;
     3286[ $\emph{lvalue}$, ... , $\emph{lvalue}$ ] = $\emph{expr}$;
    32993287\end{cfa}
    33003288\index{lvalue}
     
    33363324Multiple assignment has the following form:
    33373325\begin{cfa}
    3338 [ §\emph{lvalue}§, ... , §\emph{lvalue}§ ] = [ §\emph{expr}§, ... , §\emph{expr}§ ];
     3326[ $\emph{lvalue}$, ... , $\emph{lvalue}$ ] = [ $\emph{expr}$, ... , $\emph{expr}$ ];
    33393327\end{cfa}
    33403328\index{lvalue}
     
    33673355both these examples produce indeterminate results:
    33683356\begin{cfa}
    3369 f( x++, x++ ); §\C{// C routine call with side effects in arguments}§
    3370 [ v1, v2 ] = [ x++, x++ ]; §\C{// side effects in righthand side of multiple assignment}§
     3357f( x++, x++ ); $\C{// C routine call with side effects in arguments}$
     3358[ v1, v2 ] = [ x++, x++ ]; $\C{// side effects in right-hand side of multiple assignment}$
    33713359\end{cfa}
    33723360
     
    33773365Cascade assignment has the following form:
    33783366\begin{cfa}
    3379 §\emph{tuple}§ = §\emph{tuple}§ = ... = §\emph{tuple}§;
     3367$\emph{tuple}$ = $\emph{tuple}$ = ... = $\emph{tuple}$;
    33803368\end{cfa}
    33813369and it has the same parallel semantics as for mass and multiple assignment.
     
    34243412\begin{cfa}
    34253413int x = 1, y = 2, z = 3;
    3426 sout | x ®|® y ®|® z;
     3414sout | x @|@ y @|@ z;
    34273415\end{cfa}
    34283416&
    34293417\begin{cfa}
    34303418
    3431 cout << x ®<< " "® << y ®<< " "® << z << endl;
     3419cout << x @<< " "@ << y @<< " "@ << z << endl;
    34323420\end{cfa}
    34333421&
     
    34383426\\
    34393427\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    3440 1® ®2® ®3
     34281@ @2@ @3
    34413429\end{cfa}
    34423430&
    34433431\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    3444 1® ®2® ®3
     34321@ @2@ @3
    34453433\end{cfa}
    34463434&
    34473435\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    3448 1® ®2® ®3
     34361@ @2@ @3
    34493437\end{cfa}
    34503438\end{tabular}
     
    34543442\begin{cfa}
    34553443[int, [ int, int ] ] t1 = [ 1, [ 2, 3 ] ], t2 = [ 4, [ 5, 6 ] ];
    3456 sout | t1 | t2; §\C{// print tuples}§
     3444sout | t1 | t2; $\C{// print tuples}$
    34573445\end{cfa}
    34583446\begin{cfa}[showspaces=true,aboveskip=0pt]
    3459 1®, ®2®, ®3 4®, ®5®, ®6
     34471@, @2@, @3 4@, @5@, @6
    34603448\end{cfa}
    34613449Finally, \CFA uses the logical-or operator for I/O as it is the lowest-priority \emph{overloadable} operator, other than assignment.
     
    34663454&
    34673455\begin{cfa}
    3468 sout | x * 3 | y + 1 | z << 2 | x == y | ®(®x | y®)® | ®(®x || y®)® | ®(®x > z ? 1 : 2®)®;
     3456sout | x * 3 | y + 1 | z << 2 | x == y | @(@x | y@)@ | @(@x || y@)@ | @(@x > z ? 1 : 2@)@;
    34693457\end{cfa}
    34703458\\
     
    34723460&
    34733461\begin{cfa}
    3474 cout << x * 3 << y + 1 << ®(®z << 2®)® << ®(®x == y®)® << ®(®x | y®)® << ®(®x || y®)® << ®(®x > z ? 1 : 2®)® << endl;
     3462cout << x * 3 << y + 1 << @(@z << 2@)@ << @(@x == y@)@ << @(@x | y@)@ << @(@x || y@)@ << @(@x > z ? 1 : 2@)@ << endl;
    34753463\end{cfa}
    34763464\\
     
    35073495\\
    35083496\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    3509 ®1® ®2.5® ®A®
     3497@1@ @2.5@ @A@
    35103498
    35113499
     
    35133501&
    35143502\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    3515 ®1® ®2.5® ®A®
     3503@1@ @2.5@ @A@
    35163504
    35173505
     
    35193507&
    35203508\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    3521 ®1®
    3522 ®2.5®
    3523 ®A®
     3509@1@
     3510@2.5@
     3511@A@
    35243512\end{cfa}
    35253513\end{tabular}
     
    35573545
    35583546\item
    3559 {\lstset{language=CFA,deletedelim=**[is][]{¢}{¢}}
    3560 A separator does not appear before a C string starting with the (extended) \Index*{ASCII}\index{ASCII!extended} characters: \lstinline[basicstyle=\tt]@,.;!?)]}%¢»@, where \lstinline[basicstyle=\tt]@»@ is a closing citation mark.
    3561 \begin{cfa}[belowskip=0pt]
     3547A separator does not appear before a C string starting with the (extended) \Index*{ASCII}\index{ASCII!extended} characters: \LstStringStyle{,.;!?)]\}\%\textcent\guillemotright}, where \LstStringStyle{\guillemotright} a closing citation mark.
     3548\begin{cfa}
    35623549sout | 1 | ", x" | 2 | ". x" | 3 | "; x" | 4 | "! x" | 5 | "? x" | 6 | "% x"
    3563                 | 7 | "¢ x" | 8 | "» x" | 9 | ") x" | 10 | "] x" | 11 | "} x";
    3564 \end{cfa}
    3565 \begin{cfa}[basicstyle=\tt,showspaces=true,aboveskip=0pt,belowskip=0pt]
    3566 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
    3567 \end{cfa}}%
    3568 
    3569 \item
    3570 A separator does not appear after a C string ending with the (extended) \Index*{ASCII}\index{ASCII!extended} characters: \lstinline[mathescape=off,basicstyle=\tt]@([{=$£¥¡¿«@, where \lstinline[basicstyle=\tt]@¡¿@ are inverted opening exclamation and question marks, and \lstinline[basicstyle=\tt]@«@ is an opening citation mark.
     3550           | 7 | "$\LstStringStyle{\textcent}$ x" | 8 | "$\LstStringStyle{\guillemotright}$ x" | 9 | ") x" | 10 | "] x" | 11 | "} x";
     3551\end{cfa}
     3552\begin{cfa}[showspaces=true]
     35531@,@ x 2@.@ x 3@;@ x 4@!@ x 5@?@ x 6@%@ x 7$\R{\LstStringStyle{\textcent}}$ x 8$\R{\LstStringStyle{\guillemotright}}$ x 9@)@ x 10@]@ x 11@}@ x
     3554\end{cfa}
     3555
     3556\item
     3557A separator does not appear after a C string ending with the (extended) \Index*{ASCII}\index{ASCII!extended} characters: \LstStringStyle{([\{=\$\textsterling\textyen\textexclamdown\textquestiondown\guillemotleft}, where \LstStringStyle{\textexclamdown\textquestiondown} are inverted opening exclamation and question marks, and \LstStringStyle{\guillemotleft} is an opening citation mark.
    35713558%$
    3572 \begin{cfa}[mathescape=off]
    3573 sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x $" | 5 | "x £" | 6 | "x ¥"
    3574                 | 7 | "x ¡" | 8 | "x ¿" | 9 | "x «" | 10;
     3559\begin{cfa}
     3560sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x $" | 5 | "x $\LstStringStyle{\textsterling}$" | 6 | "x $\LstStringStyle{\textyen}$"
     3561           | 7 | "x $\LstStringStyle{\textexclamdown}$" | 8 | "x $\LstStringStyle{\textquestiondown}$" | 9 | "x $\LstStringStyle{\guillemotleft}$" | 10;
    35753562\end{cfa}
    35763563%$
    3577 \begin{cfa}[mathescape=off,basicstyle=\tt,showspaces=true,aboveskip=0pt,belowskip=0pt]
    3578 x ®(®1 x ®[®2 x ®{®3 x ®=®4 x ®$®5 x ®£®6 x ®¥®7 x ®¡®8 x ®¿®9 x ®«®10
     3564\begin{cfa}[showspaces=true]
     3565x @(@1 x @[@2 x @{@3 x @=@4 x $\LstStringStyle{\textdollar}$5 x $\R{\LstStringStyle{\textsterling}}$6 x $\R{\LstStringStyle{\textyen}}$7 x $\R{\LstStringStyle{\textexclamdown}}$8 x $\R{\LstStringStyle{\textquestiondown}}$9 x $\R{\LstStringStyle{\guillemotleft}}$10
    35793566\end{cfa}
    35803567%$
    35813568
    35823569\item
    3583 A seperator does not appear before/after a C string starting/ending with the \Index*{ASCII} quote or whitespace characters: \lstinline[basicstyle=\tt,showspaces=true]@`'": \t\v\f\r\n@
    3584 \begin{cfa}[belowskip=0pt]
     3570A seperator does not appear before/after a C string starting/ending with the \Index*{ASCII} quote or whitespace characters: \lstinline[basicstyle=\tt,showspaces=true]{`'": \t\v\f\r\n}
     3571\begin{cfa}
    35853572sout | "x`" | 1 | "`x'" | 2 | "'x\"" | 3 | "\"x:" | 4 | ":x " | 5 | " x\t" | 6 | "\tx";
    35863573\end{cfa}
    3587 \begin{cfa}[basicstyle=\tt,showspaces=true,showtabs=true,aboveskip=0pt,belowskip=0pt]
    3588 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
     3574\begin{cfa}[showspaces=true,showtabs=true]
     3575x@`@1@`@x$\R{\texttt{'}}$2$\R{\texttt{'}}$x$\R{\texttt{"}}$3$\R{\texttt{"}}$x@:@4@:@x@ @5@ @x@  @6@     @x
    35893576\end{cfa}
    35903577
    35913578\item
    35923579If a space is desired before or after one of the special string start/end characters, simply insert a space.
    3593 \begin{cfa}[belowskip=0pt]
    3594 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;
    3595 \end{cfa}
    3596 \begin{cfa}[basicstyle=\tt,showspaces=true,showtabs=true,aboveskip=0pt,belowskip=0pt]
    3597 x (® ®1® ®) x 2® ®, x 3® ®:x:® ®4
     3580\begin{cfa}
     3581sout | "x ($\R{\texttt{\textvisiblespace}}$" | 1 | "$\R{\texttt{\textvisiblespace}}$) x" | 2 | "$\R{\texttt{\textvisiblespace}}$, x" | 3 | "$\R{\texttt{\textvisiblespace}}$:x:$\R{\texttt{\textvisiblespace}}$" | 4;
     3582\end{cfa}
     3583\begin{cfa}[showspaces=true,showtabs=true]
     3584x (@ @1@ @) x 2@ @, x 3@ @:x:@ @4
    35983585\end{cfa}
    35993586\end{enumerate}
     
    36083595\Indexc{sepSet}\index{manipulator!sepSet@©sepSet©} and \Indexc{sep}\index{manipulator!sep@©sep©}/\Indexc{sepGet}\index{manipulator!sepGet@©sepGet©} set and get the separator string.
    36093596The separator string can be at most 16 characters including the ©'\0'© string terminator (15 printable characters).
    3610 \begin{cfa}[mathescape=off,belowskip=0pt]
    3611 sepSet( sout, ", $" ); §\C{// set separator from " " to ", \$"}§
    3612 sout | 1 | 2 | 3 | " \"" | ®sep® | "\"";
     3597\begin{cfa}[escapechar=off,belowskip=0pt]
     3598sepSet( sout, ", $" ); $\C{// set separator from " " to ", \$"}$
     3599sout | 1 | 2 | 3 | " \"" | @sep@ | "\"";
    36133600\end{cfa}
    36143601%$
    36153602\begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt]
    3616 1®, $®2®, $®3 ®", $"®
     36031@, $@2@, $@3 @", $"@
    36173604\end{cfa}
    36183605%$
    36193606\begin{cfa}[belowskip=0pt]
    3620 sepSet( sout, " " ); §\C{// reset separator to " "}§
    3621 sout | 1 | 2 | 3 | " \"" | ®sepGet( sout )® | "\"";
     3607sepSet( sout, " " ); $\C{// reset separator to " "}$
     3608sout | 1 | 2 | 3 | " \"" | @sepGet( sout )@ | "\"";
    36223609\end{cfa}
    36233610\begin{cfa}[showspaces=true,aboveskip=0pt]
    3624 1® ®2® ®3 ®" "®
     36111@ @2@ @3 @" "@
    36253612\end{cfa}
    36263613©sepGet© can be used to store a separator and then restore it:
    36273614\begin{cfa}[belowskip=0pt]
    3628 char store[®sepSize®]; §\C{// sepSize is the maximum separator size}§
    3629 strcpy( store, sepGet( sout ) ); §\C{// copy current separator}§
    3630 sepSet( sout, "_" ); §\C{// change separator to underscore}§
     3615char store[@sepSize@]; $\C{// sepSize is the maximum separator size}$
     3616strcpy( store, sepGet( sout ) ); $\C{// copy current separator}$
     3617sepSet( sout, "_" ); $\C{// change separator to underscore}$
    36313618sout | 1 | 2 | 3;
    36323619\end{cfa}
    36333620\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    3634 1®_®2®_®3
     36211@_@2@_@3
    36353622\end{cfa}
    36363623\begin{cfa}[belowskip=0pt]
    3637 sepSet( sout, store ); §\C{// change separator back to original}§
     3624sepSet( sout, store ); $\C{// change separator back to original}$
    36383625sout | 1 | 2 | 3;
    36393626\end{cfa}
    36403627\begin{cfa}[showspaces=true,aboveskip=0pt]
    3641 1® ®2® ®3
     36281@ @2@ @3
    36423629\end{cfa}
    36433630
     
    36463633The tuple separator-string can be at most 16 characters including the ©'\0'© string terminator (15 printable characters).
    36473634\begin{cfa}[belowskip=0pt]
    3648 sepSetTuple( sout, " " ); §\C{// set tuple separator from ", " to " "}§
    3649 sout | t1 | t2 | " \"" | ®sepTuple® | "\"";
     3635sepSetTuple( sout, " " ); $\C{// set tuple separator from ", " to " "}$
     3636sout | t1 | t2 | " \"" | @sepTuple@ | "\"";
    36503637\end{cfa}
    36513638\begin{cfa}[showspaces=true,aboveskip=0pt]
    3652 1 2 3 4 5 6 ®" "®
     36391 2 3 4 5 6 @" "@
    36533640\end{cfa}
    36543641\begin{cfa}[belowskip=0pt]
    3655 sepSetTuple( sout, ", " ); §\C{// reset tuple separator to ", "}§
    3656 sout | t1 | t2 | " \"" | ®sepGetTuple( sout )® | "\"";
     3642sepSetTuple( sout, ", " ); $\C{// reset tuple separator to ", "}$
     3643sout | t1 | t2 | " \"" | @sepGetTuple( sout )@ | "\"";
    36573644\end{cfa}
    36583645\begin{cfa}[showspaces=true,aboveskip=0pt]
    3659 1, 2, 3 4, 5, 6 ®", "®
     36461, 2, 3 4, 5, 6 @", "@
    36603647\end{cfa}
    36613648As for ©sepGet©, ©sepGetTuple© can be use to store a tuple separator and then restore it.
     
    36643651\Indexc{sepDisable}\index{manipulator!sepDisable@©sepDisable©} and \Indexc{sepEnable}\index{manipulator!sepEnable@©sepEnable©} toggle printing the separator.
    36653652\begin{cfa}[belowskip=0pt]
    3666 sout | sepDisable | 1 | 2 | 3; §\C{// turn off implicit separator}§
     3653sout | sepDisable | 1 | 2 | 3; $\C{// turn off implicit separator}$
    36673654\end{cfa}
    36683655\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
     
    36703657\end{cfa}
    36713658\begin{cfa}[belowskip=0pt]
    3672 sout | sepEnable | 1 | 2 | 3; §\C{// turn on implicit separator}§
     3659sout | sepEnable | 1 | 2 | 3; $\C{// turn on implicit separator}$
    36733660\end{cfa}
    36743661\begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]
     
    36793666\Indexc{sepOn}\index{manipulator!sepOn@©sepOn©} and \Indexc{sepOff}\index{manipulator!sepOff@©sepOff©} toggle printing the separator with respect to the next printed item, and then return to the global seperator setting.
    36803667\begin{cfa}[belowskip=0pt]
    3681 sout | 1 | sepOff | 2 | 3; §\C{// turn off implicit separator for the next item}§
     3668sout | 1 | sepOff | 2 | 3; $\C{// turn off implicit separator for the next item}$
    36823669\end{cfa}
    36833670\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
     
    36853672\end{cfa}
    36863673\begin{cfa}[belowskip=0pt]
    3687 sout | sepDisable | 1 | sepOn | 2 | 3; §\C{// turn on implicit separator for the next item}§
     3674sout | sepDisable | 1 | sepOn | 2 | 3; $\C{// turn on implicit separator for the next item}$
    36883675\end{cfa}
    36893676\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
     
    36923679The tuple separator also responses to being turned on and off.
    36933680\begin{cfa}[belowskip=0pt]
    3694 sout | t1 | sepOff | t2; §\C{// turn off implicit separator for the next item}§
     3681sout | t1 | sepOff | t2; $\C{// turn off implicit separator for the next item}$
    36953682\end{cfa}
    36963683\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
     
    37003687use ©sep© to accomplish this functionality.
    37013688\begin{cfa}[belowskip=0pt]
    3702 sout | sepOn | 1 | 2 | 3 | sepOn; §\C{// sepOn does nothing at start/end of line}§
     3689sout | sepOn | 1 | 2 | 3 | sepOn; $\C{// sepOn does nothing at start/end of line}$
    37033690\end{cfa}
    37043691\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
     
    37063693\end{cfa}
    37073694\begin{cfa}[belowskip=0pt]
    3708 sout | sep | 1 | 2 | 3 | sep ; §\C{// use sep to print separator at start/end of line}§
     3695sout | sep | 1 | 2 | 3 | sep ; $\C{// use sep to print separator at start/end of line}$
    37093696\end{cfa}
    37103697\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    3711 ® ®1 2 3® ®
     3698@ @1 2 3@ @
    37123699\end{cfa}
    37133700\end{enumerate}
     
    37213708\begin{enumerate}[parsep=0pt]
    37223709\item
    3723 \Indexc{nl}\index{manipulator!nl@©nl©} scans characters until the next newline character, i.e., ignore the remaining characters in the line.
     3710\Indexc{nl}\index{manipulator!nl@©nl©} scans characters until the next newline character, \ie ignore the remaining characters in the line.
    37243711\item
    37253712\Indexc{nlOn}\index{manipulator!nlOn@©nlOn©} reads the newline character, when reading single characters.
     
    37293716For example, in:
    37303717\begin{cfa}
    3731 sin | i | ®nl® | j;
    3732 1 ®2®
     3718sin | i | @nl@ | j;
     37191 @2@
    373337203
    37343721\end{cfa}
     
    37403727\Indexc{nl}\index{manipulator!nl@©nl©} inserts a newline.
    37413728\begin{cfa}
    3742 sout | nl; §\C{// only print newline}§
    3743 sout | 2; §\C{// implicit newline}§
    3744 sout | 3 | nl | 4 | nl; §\C{// terminating nl merged with implicit newline}§
    3745 sout | 5 | nl | nl; §\C{// again terminating nl merged with implicit newline}§
    3746 sout | 6; §\C{// implicit newline}§
     3729sout | nl; $\C{// only print newline}$
     3730sout | 2; $\C{// implicit newline}$
     3731sout | 3 | nl | 4 | nl; $\C{// terminating nl merged with implicit newline}$
     3732sout | 5 | nl | nl; $\C{// again terminating nl merged with implicit newline}$
     3733sout | 6; $\C{// implicit newline}$
    37473734
    374837352
     
    377137580b0 0b11011 0b11011 0b11011 0b11011
    37723759sout | bin( -27HH ) | bin( -27H ) | bin( -27 ) | bin( -27L );
    3773 0b11100101 0b1111111111100101 0b11111111111111111111111111100101 0b®(58 1s)®100101
     37600b11100101 0b1111111111100101 0b11111111111111111111111111100101 0b@(58 1s)@100101
    37743761\end{cfa}
    37753762
     
    38103797\begin{cfa}[belowskip=0pt]
    38113798sout | upcase( bin( 27 ) ) | upcase( hex( 27 ) ) | upcase( 27.5e-10 ) | upcase( hex( 27.5 ) );
    3812 0®B®11011 0®X®1®B® 2.75®E®-09 0®X®1.®B®8®P®+4
     37990@B@11011 0@X@1@B@ 2.75@E@-09 0@X@1.@B@8@P@+4
    38133800\end{cfa}
    38143801
     
    38263813\begin{cfa}[belowskip=0pt]
    38273814sout | 0. | nodp( 0. ) | 27.0 | nodp( 27.0 ) | nodp( 27.5 );
    3828 0.0 ®0® 27.0 ®27® 27.5
     38150.0 @0@ 27.0 @27@ 27.5
    38293816\end{cfa}
    38303817
     
    38333820\begin{cfa}[belowskip=0pt]
    38343821sout | sign( 27 ) | sign( -27 ) | sign( 27. ) | sign( -27. ) | sign( 27.5 ) | sign( -27.5 );
    3835 ®+®27 -27 ®+®27.0 -27.0 ®+®27.5 -27.5
     3822@+@27 -27 @+@27.0 -27.0 @+@27.5 -27.5
    38363823\end{cfa}
    38373824
     
    38463833\end{cfa}
    38473834\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    3848 ®  ®34 ® ®34 34
    3849 ®  ®4.000000 ® ®4.000000 4.000000
    3850 ®  ®ab ® ®ab ab
     3835@  @34 @ @34 34
     3836@  @4.000000 @ @4.000000 4.000000
     3837@  @ab @ @ab ab
    38513838\end{cfa}
    38523839If the value is larger, it is printed without truncation, ignoring the ©minimum©.
     
    38573844\end{cfa}
    38583845\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    3859 3456®7® 345®67® 34®567®
    3860 3456®.® 345®6.® 34®56.®
    3861 abcd®e® abc®de® ab®cde®
     38463456@7@ 345@67@ 34@567@
     38473456@.@ 345@6.@ 34@56.@
     3848abcd@e@ abc@de@ ab@cde@
    38623849\end{cfa}
    38633850
     
    38683855\end{cfa}
    38693856\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    3870  ®0®34     ®00®34 ®00000000®34
     3857 @0@34     @00@34 @00000000@34
    38713858\end{cfa}
    38723859If the value is larger, it is printed without truncation, ignoring the ©precision©.
     
    38833870\end{cfa}
    38843871\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    3885 ®    ® ®00000000®34
     3872@    @ @00000000@34
    38863873\end{cfa}
    38873874For floating-point types, ©precision© is the minimum number of digits after the decimal point.
     
    38903877\end{cfa}
    38913878\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    3892 27.®500®     27.®5®      28. 27.®50000000®
    3893 \end{cfa}
    3894 For the C-string type, ©precision© is the maximum number of printed characters, so the string is truncared if it exceeds the maximum.
     387927.@500@     27.@5@      28. 27.@50000000@
     3880\end{cfa}
     3881For the C-string type, ©precision© is the maximum number of printed characters, so the string is truncated if it exceeds the maximum.
    38953882\begin{cfa}[belowskip=0pt]
    38963883sout | wd( 6,8, "abcd" ) | wd( 6,8, "abcdefghijk" ) | wd( 6,3, "abcd" );
     
    39083895\end{cfa}
    39093896\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    3910 234.567 234.5®7®  234.®6®    23®5®
     3897234.567 234.5@7@  234.@6@    23@5@
    39113898\end{cfa}
    39123899If a value's magnitude is greater than ©significant©, the value is printed in scientific notation with the specified number of significant digits.
     
    39153902\end{cfa}
    39163903\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    3917 234567. 2.3457®e+05® 2.346®e+05® 2.35®e+05®
     3904234567. 2.3457@e+05@ 2.346@e+05@ 2.35@e+05@
    39183905\end{cfa}
    39193906If ©significant© is greater than ©minimum©, it defines the number of printed characters.
     
    39313918\end{cfa}
    39323919\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    3933 27®  ® 27.000000  27.500000  027  27.500®    ®
     392027@  @ 27.000000  27.500000  027  27.500@    @
    39343921\end{cfa}
    39353922
     
    39383925\begin{cfa}[belowskip=0pt]
    39393926sout | pad0( wd( 4, 27 ) ) | pad0( wd( 4,3, 27 ) ) | pad0( wd( 8,3, 27.5 ) );
    3940 ®00®27  ®0®27 ®00®27.500
     3927@00@27  @0@27 @00@27.500
    39413928\end{cfa}
    39423929\end{enumerate}
     
    40344021\end{cfa}
    40354022\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    4036 ®abc   ®
    4037 ®abc  ®
    4038 ®xx®
     4023@abc   @
     4024@abc  @
     4025@xx@
    40394026\end{cfa}
    40404027
     
    40474034\end{cfa}
    40484035\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    4049 ®abcd1233.456E+2®
     4036@abcd1233.456E+2@
    40504037\end{cfa}
    40514038Note, input ©wdi© cannot be overloaded with output ©wd© because both have the same parameters but return different types.
     
    40604047\end{cfa}
    40614048\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    4062 ®  -75.35e-4® 25
     4049@  -75.35e-4@ 25
    40634050\end{cfa}
    40644051
     
    40724059\end{cfa}
    40734060\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    4074 ®bca®xyz
     4061@bca@xyz
    40754062\end{cfa}
    40764063
     
    40844071\end{cfa}
    40854072\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    4086 ®xyz®bca
     4073@xyz@bca
    40874074\end{cfa}
    40884075\end{enumerate}
     
    41014088
    41024089A type definition is different from a typedef in C because a typedef just creates an alias for a type,  while Do.s type definition creates a distinct type.
    4103 This means that users can define distinct function overloads for the new type (see Overloading for more information).
     4090This means that users can define distinct function overloads for the new type \see{\VRef{s:Overloading} for more information}.
    41044091For example:
    41054092
     
    42074194\CFA supports C initialization of structures, but it also adds constructors for more advanced initialization.
    42084195Additionally, \CFA adds destructors that are called when a variable is deallocated (variable goes out of scope or object is deleted).
    4209 These functions take a reference to the structure as a parameter (see References for more information).
     4196These functions take a reference to the structure as a parameter \see{\VRef{s:PointerReference} for more information}.
    42104197
    42114198\begin{figure}
     
    42584245
    42594246\section{Overloading}
     4247\label{s:Overloading}
    42604248
    42614249Overloading refers to the capability of a programmer to define and use multiple objects in a program with the same name.
     
    42904278
    42914279
    4292 \subsection{Overloaded Constant}
     4280\subsection{Constant}
    42934281
    42944282The constants 0 and 1 have special meaning.
     
    43294317
    43304318
    4331 \subsection{Variable Overloading}
     4319\subsection{Variable}
     4320\label{s:VariableOverload}
    43324321
    43334322The overload rules of \CFA allow a programmer to define multiple variables with the same name, but different types.
     
    43724361
    43734362
    4374 \subsection{Operator Overloading}
     4363\subsection{Operator}
    43754364
    43764365\CFA also allows operators to be overloaded, to simplify the use of user-defined types.
     
    44684457For example, given
    44694458\begin{cfa}
    4470 auto j = ®...®
     4459auto j = @...@
    44714460\end{cfa}
    44724461and the need to write a routine to compute using ©j©
    44734462\begin{cfa}
    4474 void rtn( ®...® parm );
     4463void rtn( @...@ parm );
    44754464rtn( j );
    44764465\end{cfa}
     
    47134702
    47144703coroutine Fibonacci {
    4715         int fn; §\C{// used for communication}§
     4704        int fn; $\C{// used for communication}$
    47164705};
    47174706void ?{}( Fibonacci * this ) {
     
    47194708}
    47204709void main( Fibonacci * this ) {
    4721         int fn1, fn2; §\C{// retained between resumes}§
    4722         this->fn = 0; §\C{// case 0}§
     4710        int fn1, fn2; $\C{// retained between resumes}$
     4711        this->fn = 0; $\C{// case 0}$
    47234712        fn1 = this->fn;
    4724         suspend(); §\C{// return to last resume}§
    4725 
    4726         this->fn = 1; §\C{// case 1}§
     4713        suspend(); $\C{// return to last resume}$
     4714
     4715        this->fn = 1; $\C{// case 1}$
    47274716        fn2 = fn1;
    47284717        fn1 = this->fn;
    4729         suspend(); §\C{// return to last resume}§
    4730 
    4731         for ( ;; ) { §\C{// general case}§
     4718        suspend(); $\C{// return to last resume}$
     4719
     4720        for ( ;; ) { $\C{// general case}$
    47324721                this->fn = fn1 + fn2;
    47334722                fn2 = fn1;
    47344723                fn1 = this->fn;
    4735                 suspend(); §\C{// return to last resume}§
     4724                suspend(); $\C{// return to last resume}$
    47364725        } // for
    47374726}
    47384727int next( Fibonacci * this ) {
    4739         resume( this ); §\C{// transfer to last suspend}§
     4728        resume( this ); $\C{// transfer to last suspend}$
    47404729        return this->fn;
    47414730}
     
    49644953When building a \CFA module which needs to be callable from C code, users can use the tools to generate a header file suitable for including in these C files with all of the needed declarations.
    49654954
    4966 In order to interoperate with existing C code, \CFA files can still include header files, the contents of which will be enclosed in a C linkage section to indicate C calling conventions (see Interoperability for more information).
     4955In order to interoperate with existing C code, \CFA files can still include header files, the contents of which will be enclosed in a C linkage section to indicate C calling conventions \see{\VRef{s:Interoperability} for more information}.
    49674956
    49684957
     
    56305619\end{cfa}
    56315620&
    5632 \begin{lstlisting}[language=C++]
     5621\begin{C++}
    56335622class Line {
    56345623        float lnth;
     
    56575646Line line1;
    56585647Line line2( 3.4 );
    5659 \end{lstlisting}
     5648\end{C++}
    56605649&
    56615650\begin{lstlisting}[language=Golang]
     
    62826271In \CFA, there are ambiguous cases with dereference and operator identifiers, \eg ©int *?*?()©, where the string ©*?*?© can be interpreted as:
    62836272\begin{cfa}
    6284 *?§\color{red}\textvisiblespace§*? §\C{// dereference operator, dereference operator}§
    6285 *§\color{red}\textvisiblespace§?*? §\C{// dereference, multiplication operator}§
     6273*?$\R{\textvisiblespace}$*? $\C{// dereference operator, dereference operator}$
     6274*$\R{\textvisiblespace}$?*? $\C{// dereference, multiplication operator}$
    62866275\end{cfa}
    62876276By default, the first interpretation is selected, which does not yield a meaningful parse.
     
    62926281The ambiguity occurs when the deference operator has no parameters:
    62936282\begin{cfa}
    6294 *?()§\color{red}\textvisiblespace...§ ;
    6295 *?()§\color{red}\textvisiblespace...§(...) ;
     6283*?()$\R{\textvisiblespace...}$ ;
     6284*?()$\R{\textvisiblespace...}$(...) ;
    62966285\end{cfa}
    62976286requiring arbitrary whitespace look-ahead for the routine-call parameter-list to disambiguate.
     
    63016290The remaining cases are with the increment/decrement operators and conditional expression, \eg:
    63026291\begin{cfa}
    6303 i++?§\color{red}\textvisiblespace...§(...);
    6304 i?++§\color{red}\textvisiblespace...§(...);
     6292i++?$\R{\textvisiblespace...}$(...);
     6293i?++$\R{\textvisiblespace...}$(...);
    63056294\end{cfa}
    63066295requiring arbitrary whitespace look-ahead for the operator parameter-list, even though that interpretation is an incorrect expression (juxtaposed identifiers).
    63076296Therefore, it is necessary to disambiguate these cases with a space:
    63086297\begin{cfa}
    6309 i++§\color{red}\textvisiblespace§? i : 0;
    6310 i?§\color{red}\textvisiblespace§++i : 0;
     6298i++$\R{\textvisiblespace}$? i : 0;
     6299i?$\R{\textvisiblespace}$++i : 0;
    63116300\end{cfa}
    63126301
     
    63216310\begin{description}
    63226311\item[Change:] add new keywords \\
    6323 New keywords are added to \CFA (see~\VRef{s:CFAKeywords}).
     6312New keywords are added to \CFA \see{\VRef{s:CFAKeywords}}.
    63246313\item[Rationale:] keywords added to implement new semantics of \CFA.
    63256314\item[Effect on original feature:] change to semantics of well-defined feature. \\
    63266315Any \Celeven programs using these keywords as identifiers are invalid \CFA programs.
    6327 \item[Difficulty of converting:] keyword clashes are accommodated by syntactic transformations using the \CFA backquote escape-mechanism (see~\VRef{s:BackquoteIdentifiers}).
     6316\item[Difficulty of converting:] keyword clashes are accommodated by syntactic transformations using the \CFA backquote escape-mechanism \see{\VRef{s:BackquoteIdentifiers}}.
    63286317\item[How widely used:] clashes among new \CFA keywords and existing identifiers are rare.
    63296318\end{description}
     
    63356324\eg:
    63366325\begin{cfa}
    6337 x; §\C{// int x}§
    6338 *y; §\C{// int *y}§
    6339 f( p1, p2 ); §\C{// int f( int p1, int p2 );}§
    6340 g( p1, p2 ) int p1, p2; §\C{// int g( int p1, int p2 );}§
     6326x; $\C{// int x}$
     6327*y; $\C{// int *y}$
     6328f( p1, p2 ); $\C{// int f( int p1, int p2 );}$
     6329g( p1, p2 ) int p1, p2; $\C{// int g( int p1, int p2 );}$
    63416330\end{cfa}
    63426331\CFA continues to support K\&R routine definitions:
    63436332\begin{cfa}
    6344 f( a, b, c ) §\C{// default int return}§
    6345         int a, b; char c §\C{// K\&R parameter declarations}§
     6333f( a, b, c ) $\C{// default int return}$
     6334        int a, b; char c $\C{// K\&R parameter declarations}$
    63466335{
    63476336        ...
     
    63626351int rtn( int i );
    63636352int rtn( char c );
    6364 rtn( 'x' ); §\C{// programmer expects 2nd rtn to be called}§
     6353rtn( 'x' ); $\C{// programmer expects 2nd rtn to be called}$
    63656354\end{cfa}
    63666355\item[Rationale:] it is more intuitive for the call to ©rtn© to match the second version of definition of ©rtn© rather than the first.
     
    63846373\item[Change:] make string literals ©const©:
    63856374\begin{cfa}
    6386 char * p = "abc"; §\C{// valid in C, deprecated in \CFA}§
    6387 char * q = expr ? "abc" : "de"; §\C{// valid in C, invalid in \CFA}§
     6375char * p = "abc"; $\C{// valid in C, deprecated in \CFA}$
     6376char * q = expr ? "abc" : "de"; $\C{// valid in C, invalid in \CFA}$
    63886377\end{cfa}
    63896378The type of a string literal is changed from ©[] char© to ©const [] char©.
     
    63926381\begin{cfa}
    63936382char * p = "abc";
    6394 p[0] = 'w'; §\C{// segment fault or change constant literal}§
     6383p[0] = 'w'; $\C{// segment fault or change constant literal}$
    63956384\end{cfa}
    63966385The same problem occurs when passing a string literal to a routine that changes its argument.
     
    64046393\item[Change:] remove \newterm{tentative definitions}, which only occurs at file scope:
    64056394\begin{cfa}
    6406 int i; §\C{// forward definition}§
    6407 int *j = ®&i®; §\C{// forward reference, valid in C, invalid in \CFA}§
    6408 int i = 0; §\C{// definition}§
     6395int i; $\C{// forward definition}$
     6396int *j = @&i@; $\C{// forward reference, valid in C, invalid in \CFA}$
     6397int i = 0; $\C{// definition}$
    64096398\end{cfa}
    64106399is valid in C, and invalid in \CFA because duplicate overloaded object definitions at the same scope level are disallowed.
     
    64126401\begin{cfa}
    64136402struct X { int i; struct X *next; };
    6414 static struct X a; §\C{// forward definition}§
    6415 static struct X b = { 0, ®&a® };§\C{// forward reference, valid in C, invalid in \CFA}§
    6416 static struct X a = { 1, &b }; §\C{// definition}§
     6403static struct X a; $\C{// forward definition}$
     6404static struct X b = { 0, @&a@ };$\C{// forward reference, valid in C, invalid in \CFA}$
     6405static struct X a = { 1, &b }; $\C{// definition}$
    64176406\end{cfa}
    64186407\item[Rationale:] avoids having different initialization rules for builtin types and user-defined types.
     
    64266415\item[Change:] have ©struct© introduce a scope for nested types:
    64276416\begin{cfa}
    6428 enum ®Colour® { R, G, B, Y, C, M };
     6417enum @Colour@ { R, G, B, Y, C, M };
    64296418struct Person {
    6430         enum ®Colour® { R, G, B };      §\C[7cm]{// nested type}§
    6431         struct Face { §\C{// nested type}§
    6432                 ®Colour® Eyes, Hair; §\C{// type defined outside (1 level)}§
     6419        enum @Colour@ { R, G, B };      $\C[7cm]{// nested type}$
     6420        struct Face { $\C{// nested type}$
     6421                @Colour@ Eyes, Hair; $\C{// type defined outside (1 level)}$
    64336422        };
    6434         ®.Colour® shirt; §\C{// type defined outside (top level)}§
    6435         ®Colour® pants; §\C{// type defined same level}§
    6436         Face looks[10]; §\C{// type defined same level}§
     6423        @.Colour@ shirt; $\C{// type defined outside (top level)}$
     6424        @Colour@ pants; $\C{// type defined same level}$
     6425        Face looks[10]; $\C{// type defined same level}$
    64376426};
    6438 ®Colour® c = R; §\C{// type/enum defined same level}§
    6439 Person®.Colour® pc = Person®.®R;§\C{// type/enum defined inside}§
    6440 Person®.®Face pretty; §\C{// type defined inside}\CRT§
     6427@Colour@ c = R; $\C{// type/enum defined same level}$
     6428Person@.Colour@ pc = Person@.@R;$\C{// type/enum defined inside}$
     6429Person@.@Face pretty; $\C{// type defined inside}\CRT$
    64416430\end{cfa}
    64426431In 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.
     
    64556444\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:
    64566445\begin{cfa}
    6457 struct Y; §\C{// struct Y and struct X are at the same scope}§
     6446struct Y; $\C{// struct Y and struct X are at the same scope}$
    64586447struct X {
    64596448        struct Y { /* ... */ } y;
     
    64706459\begin{cfa}
    64716460void foo() {
    6472         int * b = malloc( sizeof(int) ); §\C{// implicitly convert void * to int *}§
    6473         char * c = b; §\C{// implicitly convert int * to void *, and then void * to char *}§
     6461        int * b = malloc( sizeof(int) ); $\C{// implicitly convert void * to int *}$
     6462        char * c = b; $\C{// implicitly convert int * to void *, and then void * to char *}$
    64746463}
    64756464\end{cfa}
    64766465\item[Rationale:] increase type safety
    64776466\item[Effect on original feature:] deletion of semantically well-defined feature.
    6478 \item[Difficulty of converting:] requires adding a cast (see \VRef{s:StorageManagement} for better alternatives):
     6467\item[Difficulty of converting:] requires adding a cast \see{\VRef{s:StorageManagement} for better alternatives}:
    64796468\begin{cfa}
    64806469        int * b = (int *)malloc( sizeof(int) );
     
    65866575\end{cquote}
    65876576For the prescribed head-files, \CFA uses header interposition to wraps these includes in an ©extern "C"©;
    6588 hence, names in these include files are not mangled\index{mangling!name} (see~\VRef{s:Interoperability}).
     6577hence, names in these include files are not mangled\index{mangling!name} \see{\VRef{s:Interoperability}}.
    65896578All other C header files must be explicitly wrapped in ©extern "C"© to prevent name mangling.
    65906579This approach is different from \Index*[C++]{\CC{}} where the name-mangling issue is handled internally in C header-files through checks for preprocessor variable ©__cplusplus©, which adds appropriate ©extern "C"© qualifiers.
     
    66496638Type-safe allocation is provided for all C allocation routines and new \CFA allocation routines, \eg in
    66506639\begin{cfa}
    6651 int * ip = (int *)malloc( sizeof(int) );                §\C{// C}§
    6652 int * ip = malloc();                                                    §\C{// \CFA type-safe version of C malloc}§
    6653 int * ip = alloc();                                                             §\C{// \CFA type-safe uniform alloc}§
     6640int * ip = (int *)malloc( sizeof(int) );                $\C{// C}$
     6641int * ip = malloc();                                                    $\C{// \CFA type-safe version of C malloc}$
     6642int * ip = alloc();                                                             $\C{// \CFA type-safe uniform alloc}$
    66546643\end{cfa}
    66556644the latter two allocations determine the allocation size from the type of ©p© (©int©) and cast the pointer to the allocated storage to ©int *©.
     
    66586647\begin{cfa}
    66596648struct S { int i; } __attribute__(( aligned( 128 ) )); // cache-line alignment
    6660 S * sp = malloc();                                                              §\C{// honour type alignment}§
     6649S * sp = malloc();                                                              $\C{// honour type alignment}$
    66616650\end{cfa}
    66626651the storage allocation is implicitly aligned to 128 rather than the default 16.
     
    66736662\CFA memory management extends allocation to support constructors for initialization of allocated storage, \eg in
    66746663\begin{cfa}
    6675 struct S { int i; };                                                    §\C{// cache-line aglinment}§
     6664struct S { int i; };                                                    $\C{// cache-line alignment}$
    66766665void ?{}( S & s, int i ) { s.i = i; }
    66776666// assume ?|? operator for printing an S
    66786667
    6679 S & sp = *®new®( 3 );                                                   §\C{// call constructor after allocation}§
     6668S & sp = *@new@( 3 );                                                   $\C{// call constructor after allocation}$
    66806669sout | sp.i;
    6681 ®delete®( &sp );
    6682 
    6683 S * spa = ®anew®( 10, 5 );                                              §\C{// allocate array and initialize each array element}§
     6670@delete@( &sp );
     6671
     6672S * spa = @anew@( 10, 5 );                                              $\C{// allocate array and initialize each array element}$
    66846673for ( i; 10 ) sout | spa[i] | nonl;
    66856674sout | nl;
    6686 ®adelete®( 10, spa );
     6675@adelete@( 10, spa );
    66876676\end{cfa}
    66886677Allocation routines ©new©/©anew© allocate a variable/array and initialize storage using the allocated type's constructor.
     
    66936682extern "C" {
    66946683        // C unsafe allocation
    6695         void * malloc( size_t size );§\indexc{malloc}§
    6696         void * calloc( size_t dim, size_t size );§\indexc{calloc}§
    6697         void * realloc( void * ptr, size_t size );§\indexc{realloc}§
    6698         void * memalign( size_t align, size_t size );§\indexc{memalign}§
    6699         void * aligned_alloc( size_t align, size_t size );§\indexc{aligned_alloc}§
    6700         int posix_memalign( void ** ptr, size_t align, size_t size );§\indexc{posix_memalign}§
    6701         void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize );§\indexc{cmemalign}§ // CFA
     6684        void * malloc( size_t size );$\indexc{malloc}$
     6685        void * calloc( size_t dim, size_t size );$\indexc{calloc}$
     6686        void * realloc( void * ptr, size_t size );$\indexc{realloc}$
     6687        void * memalign( size_t align, size_t size );$\indexc{memalign}$
     6688        void * aligned_alloc( size_t align, size_t size );$\indexc{aligned_alloc}$
     6689        int posix_memalign( void ** ptr, size_t align, size_t size );$\indexc{posix_memalign}$
     6690        void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize );$\indexc{cmemalign}$ // CFA
    67026691
    67036692        // C unsafe initialization/copy
    6704         void * memset( void * dest, int c, size_t size );§\indexc{memset}§
    6705         void * memcpy( void * dest, const void * src, size_t size );§\indexc{memcpy}§
     6693        void * memset( void * dest, int c, size_t size );$\indexc{memset}$
     6694        void * memcpy( void * dest, const void * src, size_t size );$\indexc{memcpy}$
    67066695}
    67076696
     
    67096698
    67106699forall( dtype T | sized(T) ) {
    6711         // §\CFA§ safe equivalents, i.e., implicit size specification
     6700        // $\CFA$ safe equivalents, i.e., implicit size specification
    67126701        T * malloc( void );
    67136702        T * calloc( size_t dim );
     
    67186707        int posix_memalign( T ** ptr, size_t align );
    67196708
    6720         // §\CFA§ safe general allocation, fill, resize, alignment, array
    6721         T * alloc( void );§\indexc{alloc}§                                      §\C[3.5in]{// variable, T size}§
    6722         T * alloc( size_t dim );                                                        §\C{// array[dim], T size elements}§
    6723         T * alloc( T ptr[], size_t dim );                                       §\C{// realloc array[dim], T size elements}§
    6724 
    6725         T * alloc_set( char fill );§\indexc{alloc_set}§         §\C{// variable, T size, fill bytes with value}§
    6726         T * alloc_set( T fill );                                                        §\C{// variable, T size, fill with value}§
    6727         T * alloc_set( size_t dim, char fill );                         §\C{// array[dim], T size elements, fill bytes with value}§
    6728         T * alloc_set( size_t dim, T fill );                            §\C{// array[dim], T size elements, fill elements with value}§
    6729         T * alloc_set( size_t dim, const T fill[] );            §\C{// array[dim], T size elements, fill elements with array}§
    6730         T * alloc_set( T ptr[], size_t dim, char fill );        §\C{// realloc array[dim], T size elements, fill bytes with value}§
    6731 
    6732         T * alloc_align( size_t align );                                        §\C{// aligned variable, T size}§
    6733         T * alloc_align( size_t align, size_t dim );            §\C{// aligned array[dim], T size elements}§
    6734         T * alloc_align( T ptr[], size_t align );                       §\C{// realloc new aligned array}§
    6735         T * alloc_align( T ptr[], size_t align, size_t dim ); §\C{// realloc new aligned array[dim]}§
    6736 
    6737         T * alloc_align_set( size_t align, char fill );         §\C{// aligned variable, T size, fill bytes with value}§
    6738         T * alloc_align_set( size_t align, T fill );            §\C{// aligned variable, T size, fill with value}§
    6739         T * alloc_align_set( size_t align, size_t dim, char fill ); §\C{// aligned array[dim], T size elements, fill bytes with value}§
    6740         T * alloc_align_set( size_t align, size_t dim, T fill ); §\C{// aligned array[dim], T size elements, fill elements with value}§
    6741         T * alloc_align_set( size_t align, size_t dim, const T fill[] ); §\C{// aligned array[dim], T size elements, fill elements with array}§
    6742         T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); §\C{// realloc new aligned array[dim], fill new bytes with value}§
    6743 
    6744         // §\CFA§ safe initialization/copy, i.e., implicit size specification
    6745         T * memset( T * dest, char fill );§\indexc{memset}§
    6746         T * memcpy( T * dest, const T * src );§\indexc{memcpy}§
    6747 
    6748         // §\CFA§ safe initialization/copy, i.e., implicit size specification, array types
     6709        // $\CFA$ safe general allocation, fill, resize, alignment, array
     6710        T * alloc( void );$\indexc{alloc}$                                      $\C[3.5in]{// variable, T size}$
     6711        T * alloc( size_t dim );                                                        $\C{// array[dim], T size elements}$
     6712        T * alloc( T ptr[], size_t dim );                                       $\C{// realloc array[dim], T size elements}$
     6713
     6714        T * alloc_set( char fill );$\indexc{alloc_set}$         $\C{// variable, T size, fill bytes with value}$
     6715        T * alloc_set( T fill );                                                        $\C{// variable, T size, fill with value}$
     6716        T * alloc_set( size_t dim, char fill );                         $\C{// array[dim], T size elements, fill bytes with value}$
     6717        T * alloc_set( size_t dim, T fill );                            $\C{// array[dim], T size elements, fill elements with value}$
     6718        T * alloc_set( size_t dim, const T fill[] );            $\C{// array[dim], T size elements, fill elements with array}$
     6719        T * alloc_set( T ptr[], size_t dim, char fill );        $\C{// realloc array[dim], T size elements, fill bytes with value}$
     6720
     6721        T * alloc_align( size_t align );                                        $\C{// aligned variable, T size}$
     6722        T * alloc_align( size_t align, size_t dim );            $\C{// aligned array[dim], T size elements}$
     6723        T * alloc_align( T ptr[], size_t align );                       $\C{// realloc new aligned array}$
     6724        T * alloc_align( T ptr[], size_t align, size_t dim ); $\C{// realloc new aligned array[dim]}$
     6725
     6726        T * alloc_align_set( size_t align, char fill );         $\C{// aligned variable, T size, fill bytes with value}$
     6727        T * alloc_align_set( size_t align, T fill );            $\C{// aligned variable, T size, fill with value}$
     6728        T * alloc_align_set( size_t align, size_t dim, char fill ); $\C{// aligned array[dim], T size elements, fill bytes with value}$
     6729        T * alloc_align_set( size_t align, size_t dim, T fill ); $\C{// aligned array[dim], T size elements, fill elements with value}$
     6730        T * alloc_align_set( size_t align, size_t dim, const T fill[] ); $\C{// aligned array[dim], T size elements, fill elements with array}$
     6731        T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); $\C{// realloc new aligned array[dim], fill new bytes with value}$
     6732
     6733        // $\CFA$ safe initialization/copy, i.e., implicit size specification
     6734        T * memset( T * dest, char fill );$\indexc{memset}$
     6735        T * memcpy( T * dest, const T * src );$\indexc{memcpy}$
     6736
     6737        // $\CFA$ safe initialization/copy, i.e., implicit size specification, array types
    67496738        T * amemset( T dest[], char fill, size_t dim );
    67506739        T * amemcpy( T dest[], const T src[], size_t dim );
    67516740}
    67526741
    6753 // §\CFA§ allocation/deallocation and constructor/destructor, non-array types
    6754 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );§\indexc{new}§
    6755 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );§\indexc{delete}§
     6742// $\CFA$ allocation/deallocation and constructor/destructor, non-array types
     6743forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );$\indexc{new}$
     6744forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );$\indexc{delete}$
    67566745forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } )
    67576746  void delete( T * ptr, Params rest );
    67586747
    6759 // §\CFA§ allocation/deallocation and constructor/destructor, array types
    6760 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );§\indexc{anew}§
    6761 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );§\indexc{adelete}§
     6748// $\CFA$ allocation/deallocation and constructor/destructor, array types
     6749forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );$\indexc{anew}$
     6750forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );$\indexc{adelete}$
    67626751forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } )
    67636752  void adelete( size_t dim, T arr[], Params rest );
     
    67696758\leavevmode
    67706759\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    6771 int ato( const char * ptr );§\indexc{ato}§
     6760int ato( const char * ptr );$\indexc{ato}$
    67726761unsigned int ato( const char * ptr );
    67736762long int ato( const char * ptr );
     
    68016790\leavevmode
    68026791\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    6803 forall( otype T | { int ?<?( T, T ); } ) §\C{// location}§
    6804 T * bsearch( T key, const T * arr, size_t dim );§\indexc{bsearch}§
    6805 
    6806 forall( otype T | { int ?<?( T, T ); } ) §\C{// position}§
     6792forall( otype T | { int ?<?( T, T ); } ) $\C{// location}$
     6793T * bsearch( T key, const T * arr, size_t dim );$\indexc{bsearch}$
     6794
     6795forall( otype T | { int ?<?( T, T ); } ) $\C{// position}$
    68076796unsigned int bsearch( T key, const T * arr, size_t dim );
    68086797
    68096798forall( otype T | { int ?<?( T, T ); } )
    6810 void qsort( const T * arr, size_t dim );§\indexc{qsort}§
     6799void qsort( const T * arr, size_t dim );$\indexc{qsort}$
    68116800
    68126801forall( otype E | { int ?<?( E, E ); } ) {
    6813         E * bsearch( E key, const E * vals, size_t dim );§\indexc{bsearch}§ §\C{// location}§
    6814         size_t bsearch( E key, const E * vals, size_t dim );§\C{// position}§
    6815         E * bsearchl( E key, const E * vals, size_t dim );§\indexc{bsearchl}§
     6802        E * bsearch( E key, const E * vals, size_t dim );$\indexc{bsearch}$ $\C{// location}$
     6803        size_t bsearch( E key, const E * vals, size_t dim );$\C{// position}$
     6804        E * bsearchl( E key, const E * vals, size_t dim );$\indexc{bsearchl}$
    68166805        size_t bsearchl( E key, const E * vals, size_t dim );
    6817         E * bsearchu( E key, const E * vals, size_t dim );§\indexc{bsearchu}§
     6806        E * bsearchu( E key, const E * vals, size_t dim );$\indexc{bsearchu}$
    68186807        size_t bsearchu( E key, const E * vals, size_t dim );
    68196808}
     
    68296818
    68306819forall( otype E | { int ?<?( E, E ); } ) {
    6831         void qsort( E * vals, size_t dim );§\indexc{qsort}§
     6820        void qsort( E * vals, size_t dim );$\indexc{qsort}$
    68326821}
    68336822\end{cfa}
     
    68386827\leavevmode
    68396828\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    6840 unsigned char abs( signed char );§\indexc{abs}§
     6829unsigned char abs( signed char );$\indexc{abs}$
    68416830int abs( int );
    68426831unsigned long int abs( long int );
     
    68576846\leavevmode
    68586847\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    6859 void srandom( unsigned int seed );§\indexc{srandom}§
    6860 char random( void );§\indexc{random}§
    6861 char random( char u ); §\C{// [0,u)}§
    6862 char random( char l, char u ); §\C{// [l,u)}§
     6848void srandom( unsigned int seed );$\indexc{srandom}$
     6849char random( void );$\indexc{random}$
     6850char random( char u ); $\C{// [0,u)}$
     6851char random( char l, char u ); $\C{// [l,u)}$
    68636852int random( void );
    6864 int random( int u ); §\C{// [0,u)}§
    6865 int random( int l, int u ); §\C{// [l,u)}§
     6853int random( int u ); $\C{// [0,u)}$
     6854int random( int l, int u ); $\C{// [l,u)}$
    68666855unsigned int random( void );
    6867 unsigned int random( unsigned int u ); §\C{// [0,u)}§
    6868 unsigned int random( unsigned int l, unsigned int u ); §\C{// [l,u)}§
     6856unsigned int random( unsigned int u ); $\C{// [0,u)}$
     6857unsigned int random( unsigned int l, unsigned int u ); $\C{// [l,u)}$
    68696858long int random( void );
    6870 long int random( long int u ); §\C{// [0,u)}§
    6871 long int random( long int l, long int u ); §\C{// [l,u)}§
     6859long int random( long int u ); $\C{// [0,u)}$
     6860long int random( long int l, long int u ); $\C{// [l,u)}$
    68726861unsigned long int random( void );
    6873 unsigned long int random( unsigned long int u ); §\C{// [0,u)}§
    6874 unsigned long int random( unsigned long int l, unsigned long int u ); §\C{// [l,u)}§
    6875 float random( void );                                            §\C{// [0.0, 1.0)}§
    6876 double random( void );                                           §\C{// [0.0, 1.0)}§
    6877 float _Complex random( void );                           §\C{// [0.0, 1.0)+[0.0, 1.0)i}§
    6878 double _Complex random( void );                          §\C{// [0.0, 1.0)+[0.0, 1.0)i}§
    6879 long double _Complex random( void );             §\C{// [0.0, 1.0)+[0.0, 1.0)i}§
     6862unsigned long int random( unsigned long int u ); $\C{// [0,u)}$
     6863unsigned long int random( unsigned long int l, unsigned long int u ); $\C{// [l,u)}$
     6864float random( void );                                            $\C{// [0.0, 1.0)}$
     6865double random( void );                                           $\C{// [0.0, 1.0)}$
     6866float _Complex random( void );                           $\C{// [0.0, 1.0)+[0.0, 1.0)i}$
     6867double _Complex random( void );                          $\C{// [0.0, 1.0)+[0.0, 1.0)i}$
     6868long double _Complex random( void );             $\C{// [0.0, 1.0)+[0.0, 1.0)i}$
    68806869\end{cfa}
    68816870
     
    68856874\leavevmode
    68866875\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    6887 forall( otype T | { int ?<?( T, T ); } ) T min( T t1, T t2 );§\indexc{min}§
    6888 forall( otype T | { int ?>?( T, T ); } ) T max( T t1, T t2 );§\indexc{max}§
    6889 forall( otype T | { T min( T, T ); T max( T, T ); } ) T clamp( T value, T min_val, T max_val );§\indexc{clamp}§
    6890 forall( otype T ) void swap( T * t1, T * t2 );§\indexc{swap}§
     6876forall( otype T | { int ?<?( T, T ); } ) T min( T t1, T t2 );$\indexc{min}$
     6877forall( otype T | { int ?>?( T, T ); } ) T max( T t1, T t2 );$\indexc{max}$
     6878forall( otype T | { T min( T, T ); T max( T, T ); } ) T clamp( T value, T min_val, T max_val );$\indexc{clamp}$
     6879forall( otype T ) void swap( T * t1, T * t2 );$\indexc{swap}$
    68916880\end{cfa}
    68926881
     
    69026891\leavevmode
    69036892\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    6904 float ?%?( float, float );§\indexc{fmod}§
     6893float ?%?( float, float );$\indexc{fmod}$
    69056894float fmod( float, float );
    69066895double ?%?( double, double );
     
    69096898long double fmod( long double, long double );
    69106899
    6911 float remainder( float, float );§\indexc{remainder}§
     6900float remainder( float, float );$\indexc{remainder}$
    69126901double remainder( double, double );
    69136902long double remainder( long double, long double );
    69146903
    6915 float remquo( float, float, int * );§\indexc{remquo}§
     6904float remquo( float, float, int * );$\indexc{remquo}$
    69166905double remquo( double, double, int * );
    69176906long double remquo( long double, long double, int * );
     
    69206909[ int, long double ] remquo( long double, long double );
    69216910
    6922 float div( float, float, int * );§\indexc{div}§ §\C{// alternative name for remquo}§
     6911float div( float, float, int * );$\indexc{div}$ $\C{// alternative name for remquo}$
    69236912double div( double, double, int * );
    69246913long double div( long double, long double, int * );
     
    69276916[ int, long double ] div( long double, long double );
    69286917
    6929 float fma( float, float, float );§\indexc{fma}§
     6918float fma( float, float, float );$\indexc{fma}$
    69306919double fma( double, double, double );
    69316920long double fma( long double, long double, long double );
    69326921
    6933 float fdim( float, float );§\indexc{fdim}§
     6922float fdim( float, float );$\indexc{fdim}$
    69346923double fdim( double, double );
    69356924long double fdim( long double, long double );
    69366925
    6937 float nan( const char * );§\indexc{nan}§
     6926float nan( const char * );$\indexc{nan}$
    69386927double nan( const char * );
    69396928long double nan( const char * );
     
    69456934\leavevmode
    69466935\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    6947 float exp( float );§\indexc{exp}§
     6936float exp( float );$\indexc{exp}$
    69486937double exp( double );
    69496938long double exp( long double );
     
    69526941long double _Complex exp( long double _Complex );
    69536942
    6954 float exp2( float );§\indexc{exp2}§
     6943float exp2( float );$\indexc{exp2}$
    69556944double exp2( double );
    69566945long double exp2( long double );
     
    69596948// long double _Complex exp2( long double _Complex );
    69606949
    6961 float expm1( float );§\indexc{expm1}§
     6950float expm1( float );$\indexc{expm1}$
    69626951double expm1( double );
    69636952long double expm1( long double );
    69646953
    6965 float pow( float, float );§\indexc{pow}§
     6954float pow( float, float );$\indexc{pow}$
    69666955double pow( double, double );
    69676956long double pow( long double, long double );
     
    69766965\leavevmode
    69776966\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    6978 float log( float );§\indexc{log}§
     6967float log( float );$\indexc{log}$
    69796968double log( double );
    69806969long double log( long double );
     
    69836972long double _Complex log( long double _Complex );
    69846973
    6985 float log2( float );§\indexc{log2}§
     6974float log2( float );$\indexc{log2}$
    69866975double log2( double );
    69876976long double log2( long double );
     
    69906979// long double _Complex log2( long double _Complex );
    69916980
    6992 float log10( float );§\indexc{log10}§
     6981float log10( float );$\indexc{log10}$
    69936982double log10( double );
    69946983long double log10( long double );
     
    69976986// long double _Complex log10( long double _Complex );
    69986987
    6999 float log1p( float );§\indexc{log1p}§
     6988float log1p( float );$\indexc{log1p}$
    70006989double log1p( double );
    70016990long double log1p( long double );
    70026991
    7003 int ilogb( float );§\indexc{ilogb}§
     6992int ilogb( float );$\indexc{ilogb}$
    70046993int ilogb( double );
    70056994int ilogb( long double );
    70066995
    7007 float logb( float );§\indexc{logb}§
     6996float logb( float );$\indexc{logb}$
    70086997double logb( double );
    70096998long double logb( long double );
    70106999
    7011 float sqrt( float );§\indexc{sqrt}§
     7000float sqrt( float );$\indexc{sqrt}$
    70127001double sqrt( double );
    70137002long double sqrt( long double );
     
    70167005long double _Complex sqrt( long double _Complex );
    70177006
    7018 float cbrt( float );§\indexc{cbrt}§
     7007float cbrt( float );$\indexc{cbrt}$
    70197008double cbrt( double );
    70207009long double cbrt( long double );
    70217010
    7022 float hypot( float, float );§\indexc{hypot}§
     7011float hypot( float, float );$\indexc{hypot}$
    70237012double hypot( double, double );
    70247013long double hypot( long double, long double );
     
    70307019\leavevmode
    70317020\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    7032 float sin( float );§\indexc{sin}§
     7021float sin( float );$\indexc{sin}$
    70337022double sin( double );
    70347023long double sin( long double );
     
    70377026long double _Complex sin( long double _Complex );
    70387027
    7039 float cos( float );§\indexc{cos}§
     7028float cos( float );$\indexc{cos}$
    70407029double cos( double );
    70417030long double cos( long double );
     
    70447033long double _Complex cos( long double _Complex );
    70457034
    7046 float tan( float );§\indexc{tan}§
     7035float tan( float );$\indexc{tan}$
    70477036double tan( double );
    70487037long double tan( long double );
     
    70517040long double _Complex tan( long double _Complex );
    70527041
    7053 float asin( float );§\indexc{asin}§
     7042float asin( float );$\indexc{asin}$
    70547043double asin( double );
    70557044long double asin( long double );
     
    70587047long double _Complex asin( long double _Complex );
    70597048
    7060 float acos( float );§\indexc{acos}§
     7049float acos( float );$\indexc{acos}$
    70617050double acos( double );
    70627051long double acos( long double );
     
    70657054long double _Complex acos( long double _Complex );
    70667055
    7067 float atan( float );§\indexc{atan}§
     7056float atan( float );$\indexc{atan}$
    70687057double atan( double );
    70697058long double atan( long double );
     
    70727061long double _Complex atan( long double _Complex );
    70737062
    7074 float atan2( float, float );§\indexc{atan2}§
     7063float atan2( float, float );$\indexc{atan2}$
    70757064double atan2( double, double );
    70767065long double atan2( long double, long double );
    70777066
    7078 float atan( float, float ); §\C{// alternative name for atan2}§
    7079 double atan( double, double );§\indexc{atan}§
     7067float atan( float, float ); $\C{// alternative name for atan2}$
     7068double atan( double, double );$\indexc{atan}$
    70807069long double atan( long double, long double );
    70817070\end{cfa}
     
    70867075\leavevmode
    70877076\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    7088 float sinh( float );§\indexc{sinh}§
     7077float sinh( float );$\indexc{sinh}$
    70897078double sinh( double );
    70907079long double sinh( long double );
     
    70937082long double _Complex sinh( long double _Complex );
    70947083
    7095 float cosh( float );§\indexc{cosh}§
     7084float cosh( float );$\indexc{cosh}$
    70967085double cosh( double );
    70977086long double cosh( long double );
     
    71007089long double _Complex cosh( long double _Complex );
    71017090
    7102 float tanh( float );§\indexc{tanh}§
     7091float tanh( float );$\indexc{tanh}$
    71037092double tanh( double );
    71047093long double tanh( long double );
     
    71077096long double _Complex tanh( long double _Complex );
    71087097
    7109 float asinh( float );§\indexc{asinh}§
     7098float asinh( float );$\indexc{asinh}$
    71107099double asinh( double );
    71117100long double asinh( long double );
     
    71147103long double _Complex asinh( long double _Complex );
    71157104
    7116 float acosh( float );§\indexc{acosh}§
     7105float acosh( float );$\indexc{acosh}$
    71177106double acosh( double );
    71187107long double acosh( long double );
     
    71217110long double _Complex acosh( long double _Complex );
    71227111
    7123 float atanh( float );§\indexc{atanh}§
     7112float atanh( float );$\indexc{atanh}$
    71247113double atanh( double );
    71257114long double atanh( long double );
     
    71347123\leavevmode
    71357124\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    7136 float erf( float );§\indexc{erf}§
     7125float erf( float );$\indexc{erf}$
    71377126double erf( double );
    71387127long double erf( long double );
     
    71417130long double _Complex erf( long double _Complex );
    71427131
    7143 float erfc( float );§\indexc{erfc}§
     7132float erfc( float );$\indexc{erfc}$
    71447133double erfc( double );
    71457134long double erfc( long double );
     
    71487137long double _Complex erfc( long double _Complex );
    71497138
    7150 float lgamma( float );§\indexc{lgamma}§
     7139float lgamma( float );$\indexc{lgamma}$
    71517140double lgamma( double );
    71527141long double lgamma( long double );
     
    71557144long double lgamma( long double, int * );
    71567145
    7157 float tgamma( float );§\indexc{tgamma}§
     7146float tgamma( float );$\indexc{tgamma}$
    71587147double tgamma( double );
    71597148long double tgamma( long double );
     
    71657154\leavevmode
    71667155\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    7167 float floor( float );§\indexc{floor}§
     7156float floor( float );$\indexc{floor}$
    71687157double floor( double );
    71697158long double floor( long double );
    71707159
    7171 float ceil( float );§\indexc{ceil}§
     7160float ceil( float );$\indexc{ceil}$
    71727161double ceil( double );
    71737162long double ceil( long double );
    71747163
    7175 float trunc( float );§\indexc{trunc}§
     7164float trunc( float );$\indexc{trunc}$
    71767165double trunc( double );
    71777166long double trunc( long double );
    71787167
    7179 float rint( float );§\indexc{rint}§
     7168float rint( float );$\indexc{rint}$
    71807169long double rint( long double );
    71817170long int rint( float );
     
    71867175long long int rint( long double );
    71877176
    7188 long int lrint( float );§\indexc{lrint}§
     7177long int lrint( float );$\indexc{lrint}$
    71897178long int lrint( double );
    71907179long int lrint( long double );
     
    71937182long long int llrint( long double );
    71947183
    7195 float nearbyint( float );§\indexc{nearbyint}§
     7184float nearbyint( float );$\indexc{nearbyint}$
    71967185double nearbyint( double );
    71977186long double nearbyint( long double );
    71987187
    7199 float round( float );§\indexc{round}§
     7188float round( float );$\indexc{round}$
    72007189long double round( long double );
    72017190long int round( float );
     
    72067195long long int round( long double );
    72077196
    7208 long int lround( float );§\indexc{lround}§
     7197long int lround( float );$\indexc{lround}$
    72097198long int lround( double );
    72107199long int lround( long double );
     
    72197208\leavevmode
    72207209\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    7221 float copysign( float, float );§\indexc{copysign}§
     7210float copysign( float, float );$\indexc{copysign}$
    72227211double copysign( double, double );
    72237212long double copysign( long double, long double );
    72247213
    7225 float frexp( float, int * );§\indexc{frexp}§
     7214float frexp( float, int * );$\indexc{frexp}$
    72267215double frexp( double, int * );
    72277216long double frexp( long double, int * );
    72287217
    7229 float ldexp( float, int );§\indexc{ldexp}§
     7218float ldexp( float, int );$\indexc{ldexp}$
    72307219double ldexp( double, int );
    72317220long double ldexp( long double, int );
    72327221
    7233 [ float, float ] modf( float );§\indexc{modf}§
     7222[ float, float ] modf( float );$\indexc{modf}$
    72347223float modf( float, float * );
    72357224[ double, double ] modf( double );
     
    72387227long double modf( long double, long double * );
    72397228
    7240 float nextafter( float, float );§\indexc{nextafter}§
     7229float nextafter( float, float );$\indexc{nextafter}$
    72417230double nextafter( double, double );
    72427231long double nextafter( long double, long double );
    72437232
    7244 float nexttoward( float, long double );§\indexc{nexttoward}§
     7233float nexttoward( float, long double );$\indexc{nexttoward}$
    72457234double nexttoward( double, long double );
    72467235long double nexttoward( long double, long double );
    72477236
    7248 float scalbn( float, int );§\indexc{scalbn}§
     7237float scalbn( float, int );$\indexc{scalbn}$
    72497238double scalbn( double, int );
    72507239long double scalbn( long double, int );
    72517240
    7252 float scalbln( float, long int );§\indexc{scalbln}§
     7241float scalbln( float, long int );$\indexc{scalbln}$
    72537242double scalbln( double, long int );
    72547243long double scalbln( long double, long int );
     
    72677256\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    72687257struct Duration {
    7269         int64_t tv; §\C{// nanoseconds}§
     7258        int64_t tv; $\C{// nanoseconds}$
    72707259};
    72717260
     
    73977386\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    73987387struct Time {
    7399         uint64_t tv; §\C{// nanoseconds since UNIX epoch}§
     7388        uint64_t tv; $\C{// nanoseconds since UNIX epoch}$
    74007389};
    74017390
     
    74687457\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    74697458struct Clock {
    7470         Duration offset; §\C{// for virtual clock: contains offset from real-time}§
    7471         int clocktype; §\C{// implementation only -1 (virtual), CLOCK\_REALTIME}§
     7459        Duration offset; $\C{// for virtual clock: contains offset from real-time}$
     7460        int clocktype; $\C{// implementation only -1 (virtual), CLOCK\_REALTIME}$
    74727461};
    74737462
     
    74777466void ?{}( Clock & clk, Duration adj );
    74787467
    7479 Duration getResNsec(); §\C{// with nanoseconds}§
    7480 Duration getRes(); §\C{// without nanoseconds}§
    7481 
    7482 Time getTimeNsec(); §\C{// with nanoseconds}§
    7483 Time getTime(); §\C{// without nanoseconds}§
     7468Duration getResNsec(); $\C{// with nanoseconds}$
     7469Duration getRes(); $\C{// without nanoseconds}$
     7470
     7471Time getTimeNsec(); $\C{// with nanoseconds}$
     7472Time getTime(); $\C{// without nanoseconds}$
    74847473Time getTime( Clock & clk );
    74857474Time ?()( Clock & clk );
     
    74977486
    74987487\begin{cfa}
    7499 void ?{}( Int * this ); §\C{// constructor/destructor}§
     7488void ?{}( Int * this ); $\C{// constructor/destructor}$
    75007489void ?{}( Int * this, Int init );
    75017490void ?{}( Int * this, zero_t );
     
    75067495void ^?{}( Int * this );
    75077496
    7508 Int ?=?( Int * lhs, Int rhs ); §\C{// assignment}§
     7497Int ?=?( Int * lhs, Int rhs ); $\C{// assignment}$
    75097498Int ?=?( Int * lhs, long int rhs );
    75107499Int ?=?( Int * lhs, unsigned long int rhs );
     
    75237512unsigned long int narrow( Int val );
    75247513
    7525 int ?==?( Int oper1, Int oper2 ); §\C{// comparison}§
     7514int ?==?( Int oper1, Int oper2 ); $\C{// comparison}$
    75267515int ?==?( Int oper1, long int oper2 );
    75277516int ?==?( long int oper2, Int oper1 );
     
    75597548int ?>=?( unsigned long int oper1, Int oper2 );
    75607549
    7561 Int +?( Int oper ); §\C{// arithmetic}§
     7550Int +?( Int oper ); $\C{// arithmetic}$
    75627551Int -?( Int oper );
    75637552Int ~?( Int oper );
     
    76417630Int ?>>=?( Int * lhs, mp_bitcnt_t shift );
    76427631
    7643 Int abs( Int oper ); §\C{// number functions}§
     7632Int abs( Int oper ); $\C{// number functions}$
    76447633Int fact( unsigned long int N );
    76457634Int gcd( Int oper1, Int oper2 );
     
    76537642Int sqrt( Int oper );
    76547643
    7655 forall( dtype istype | istream( istype ) ) istype * ?|?( istype * is, Int * mp );  §\C{// I/O}§
     7644forall( dtype istype | istream( istype ) ) istype * ?|?( istype * is, Int * mp );  $\C{// I/O}$
    76567645forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype * os, Int mp );
    76577646\end{cfa}
     
    76647653\hline
    76657654\begin{cfa}
    7666 #include <gmp>§\indexc{gmp}§
     7655#include <gmp>$\indexc{gmp}$
    76677656int main( void ) {
    76687657        sout | "Factorial Numbers";
     
    76787667&
    76797668\begin{cfa}
    7680 #include <gmp.h>§\indexc{gmp.h}§
     7669#include <gmp.h>$\indexc{gmp.h}$
    76817670int main( void ) {
    7682         ®gmp_printf®( "Factorial Numbers\n" );
    7683         ®mpz_t® fact;
    7684         ®mpz_init_set_ui®( fact, 1 );
    7685         ®gmp_printf®( "%d %Zd\n", 0, fact );
     7671        @gmp_printf@( "Factorial Numbers\n" );
     7672        @mpz_t@ fact;
     7673        @mpz_init_set_ui@( fact, 1 );
     7674        @gmp_printf@( "%d %Zd\n", 0, fact );
    76867675        for ( unsigned int i = 1; i <= 40; i += 1 ) {
    7687                 ®mpz_mul_ui®( fact, fact, i );
    7688                 ®gmp_printf®( "%d %Zd\n", i, fact );
     7676                @mpz_mul_ui@( fact, fact, i );
     7677                @gmp_printf@( "%d %Zd\n", i, fact );
    76897678        }
    76907679}
     
    77517740\begin{cfa}[belowskip=0pt]
    77527741// implementation
    7753 struct Rational {§\indexc{Rational}§
    7754         long int numerator, denominator; §\C{// invariant: denominator > 0}§
     7742struct Rational {$\indexc{Rational}$
     7743        long int numerator, denominator; $\C{// invariant: denominator > 0}$
    77557744}; // Rational
    77567745
    7757 Rational rational(); §\C{// constructors}§
     7746Rational rational(); $\C{// constructors}$
    77587747Rational rational( long int n );
    77597748Rational rational( long int n, long int d );
     
    77617750void ?{}( Rational * r, one_t );
    77627751
    7763 long int numerator( Rational r ); §\C{// numerator/denominator getter/setter}§
     7752long int numerator( Rational r ); $\C{// numerator/denominator getter/setter}$
    77647753long int numerator( Rational r, long int n );
    77657754long int denominator( Rational r );
    77667755long int denominator( Rational r, long int d );
    77677756
    7768 int ?==?( Rational l, Rational r ); §\C{// comparison}§
     7757int ?==?( Rational l, Rational r ); $\C{// comparison}$
    77697758int ?!=?( Rational l, Rational r );
    77707759int ?<?( Rational l, Rational r );
     
    77737762int ?>=?( Rational l, Rational r );
    77747763
    7775 Rational -?( Rational r ); §\C{// arithmetic}§
     7764Rational -?( Rational r ); $\C{// arithmetic}$
    77767765Rational ?+?( Rational l, Rational r );
    77777766Rational ?-?( Rational l, Rational r );
     
    77797768Rational ?/?( Rational l, Rational r );
    77807769
    7781 double widen( Rational r ); §\C{// conversion}§
     7770double widen( Rational r ); $\C{// conversion}$
    77827771Rational narrow( double f, long int md );
    77837772
  • driver/cfa.cc

    r342af53 r8e4aa05  
    1010// Created On       : Tue Aug 20 13:44:49 2002
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Nov 17 14:27:28 2020
    13 // Update Count     : 440
     12// Last Modified On : Sat Jan 16 07:30:19 2021
     13// Update Count     : 442
    1414//
    1515
     
    499499                args[nargs++] = "-no-integrated-cpp";
    500500                args[nargs++] = "-Wno-deprecated";
     501                args[nargs++] = "-Wno-strict-aliasing";                 // casting from one type to another
    501502                #ifdef HAVE_CAST_FUNCTION_TYPE
    502503                args[nargs++] = "-Wno-cast-function-type";
  • libcfa/configure.ac

    r342af53 r8e4aa05  
    169169AH_TEMPLATE([CFA_HAVE_IOSQE_FIXED_FILE],[Defined if io_uring support is present when compiling libcfathread and supports the flag FIXED_FILE.])
    170170AH_TEMPLATE([CFA_HAVE_IOSQE_IO_DRAIN],[Defined if io_uring support is present when compiling libcfathread and supports the flag IO_DRAIN.])
    171 AH_TEMPLATE([CFA_HAVE_IOSQE_ASYNC],[Defined if io_uring support is present when compiling libcfathread and supports the flag ASYNC.])
    172171AH_TEMPLATE([CFA_HAVE_IOSQE_IO_LINK],[Defined if io_uring support is present when compiling libcfathread and supports the flag IO_LINK.])
    173172AH_TEMPLATE([CFA_HAVE_IOSQE_IO_HARDLINK],[Defined if io_uring support is present when compiling libcfathread and supports the flag IO_HARDLINK.])
     173AH_TEMPLATE([CFA_HAVE_IOSQE_ASYNC],[Defined if io_uring support is present when compiling libcfathread and supports the flag ASYNC.])
     174AH_TEMPLATE([CFA_HAVE_IOSQE_BUFFER_SELECT],[Defined if io_uring support is present when compiling libcfathread and supports the flag BUFFER_SELEC.])
    174175AH_TEMPLATE([CFA_HAVE_SPLICE_F_FD_IN_FIXED],[Defined if io_uring support is present when compiling libcfathread and supports the flag SPLICE_F_FD_IN_FIXED.])
    175176AH_TEMPLATE([CFA_HAVE_IORING_SETUP_ATTACH_WQ],[Defined if io_uring support is present when compiling libcfathread and supports the flag IORING_SETUP_ATTACH_WQ.])
     
    182183
    183184define(ioring_ops, [IORING_OP_NOP,IORING_OP_READV,IORING_OP_WRITEV,IORING_OP_FSYNC,IORING_OP_READ_FIXED,IORING_OP_WRITE_FIXED,IORING_OP_POLL_ADD,IORING_OP_POLL_REMOVE,IORING_OP_SYNC_FILE_RANGE,IORING_OP_SENDMSG,IORING_OP_RECVMSG,IORING_OP_TIMEOUT,IORING_OP_TIMEOUT_REMOVE,IORING_OP_ACCEPT,IORING_OP_ASYNC_CANCEL,IORING_OP_LINK_TIMEOUT,IORING_OP_CONNECT,IORING_OP_FALLOCATE,IORING_OP_OPENAT,IORING_OP_CLOSE,IORING_OP_FILES_UPDATE,IORING_OP_STATX,IORING_OP_READ,IORING_OP_WRITE,IORING_OP_FADVISE,IORING_OP_MADVISE,IORING_OP_SEND,IORING_OP_RECV,IORING_OP_OPENAT2,IORING_OP_EPOLL_CTL,IORING_OP_SPLICE,IORING_OP_PROVIDE_BUFFERS,IORING_OP_REMOVE_BUFFER,IORING_OP_TEE])
    184 define(ioring_flags, [IOSQE_FIXED_FILE,IOSQE_IO_DRAIN,IOSQE_ASYNC,IOSQE_IO_LINK,IOSQE_IO_HARDLINK,SPLICE_F_FD_IN_FIXED,IORING_SETUP_ATTACH_WQ])
     185define(ioring_flags, [IOSQE_FIXED_FILE,IOSQE_IO_DRAIN,IOSQE_IO_LINK,IOSQE_IO_HARDLINK,IOSQE_ASYNC,IOSQE_BUFFER_SELECT,SPLICE_F_FD_IN_FIXED,IORING_SETUP_ATTACH_WQ])
    185186
    186187define(ioring_from_decls, [
  • libcfa/prelude/builtins.c

    r342af53 r8e4aa05  
    1818// type that wraps a pointer and a destructor-like function - used in generating implicit destructor calls for struct members in user-defined functions
    1919// Note: needs to occur early, because it is used to generate destructor calls during code generation
    20 forall(dtype T)
     20forall(T &)
    2121struct __Destructor {
    2222        T * object;
     
    2525
    2626// defined destructor in the case that non-generated code wants to use __Destructor
    27 forall(dtype T)
     27forall(T &)
    2828static inline void ^?{}(__Destructor(T) & x) {
    2929        if (x.object && x.dtor) {
     
    3434// easy interface into __Destructor's destructor for easy codegen purposes
    3535extern "C" {
    36         forall(dtype T)
     36        forall(T &)
    3737        static inline void __destroy_Destructor(__Destructor(T) * dtor) {
    3838                ^(*dtor){};
     
    5151void abort( const char fmt[], ... ) __attribute__ (( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ ));
    5252
    53 forall(dtype T)
     53forall(T &)
    5454static inline T & identity(T & i) {
    5555        return i;
     
    6464static inline void ^?{}($generator &) {}
    6565
    66 trait is_generator(dtype T) {
     66trait is_generator(T &) {
    6767      void main(T & this);
    6868      $generator * get_generator(T & this);
    6969};
    7070
    71 forall(dtype T | is_generator(T))
     71forall(T & | is_generator(T))
    7272static inline T & resume(T & gen) {
    7373        main(gen);
     
    7878
    7979static inline {
    80         forall( dtype DT | { DT & ?+=?( DT &, one_t ); } )
     80        forall( DT & | { DT & ?+=?( DT &, one_t ); } )
    8181        DT & ++?( DT & x ) { return x += 1; }
    8282
    83         forall( dtype DT | sized(DT) | { void ?{}( DT &, DT ); void ^?{}( DT & ); DT & ?+=?( DT &, one_t ); } )
     83        forall( DT & | sized(DT) | { void ?{}( DT &, DT ); void ^?{}( DT & ); DT & ?+=?( DT &, one_t ); } )
    8484        DT & ?++( DT & x ) { DT tmp = x; x += 1; return tmp; }
    8585
    86         forall( dtype DT | { DT & ?-=?( DT &, one_t ); } )
     86        forall( DT & | { DT & ?-=?( DT &, one_t ); } )
    8787        DT & --?( DT & x ) { return x -= 1; }
    8888
    89         forall( dtype DT | sized(DT) | { void ?{}( DT &, DT ); void ^?{}( DT & ); DT & ?-=?( DT &, one_t ); } )
     89        forall( DT & | sized(DT) | { void ?{}( DT &, DT ); void ^?{}( DT & ); DT & ?-=?( DT &, one_t ); } )
    9090        DT & ?--( DT & x ) { DT tmp = x; x -= 1; return tmp; }
    9191
    92         forall( dtype DT | { int ?!=?( const DT &, zero_t ); } )
     92        forall( DT & | { int ?!=?( const DT &, zero_t ); } )
    9393        int !?( const DT & x ) { return !( x != 0 ); }
    9494} // distribution
    9595
    9696// universal typed pointer constant
    97 static inline forall( dtype DT ) DT * intptr( uintptr_t addr ) { return (DT *)addr; }
     97static inline forall( DT & ) DT * intptr( uintptr_t addr ) { return (DT *)addr; }
    9898static inline forall( ftype FT ) FT * intptr( uintptr_t addr ) { return (FT *)addr; }
    9999
     
    156156#define __CFA_EXP_OVERFLOW__()
    157157
    158 static inline forall( otype OT | { void ?{}( OT & this, one_t ); OT ?*?( OT, OT ); } ) {
     158static inline forall( OT | { void ?{}( OT & this, one_t ); OT ?*?( OT, OT ); } ) {
    159159        OT ?\?( OT ep, unsigned int y ) { __CFA_EXP__(); }
    160160        OT ?\?( OT ep, unsigned long int y ) { __CFA_EXP__(); }
  • libcfa/prelude/defines.hfa.in

    r342af53 r8e4aa05  
    149149
    150150/* Defined if io_uring support is present when compiling libcfathread and
     151   supports the flag BUFFER_SELEC. */
     152#undef CFA_HAVE_IOSQE_BUFFER_SELECT
     153
     154/* Defined if io_uring support is present when compiling libcfathread and
    151155   supports the flag FIXED_FILE. */
    152156#undef CFA_HAVE_IOSQE_FIXED_FILE
  • libcfa/prelude/prelude-gen.cc

    r342af53 r8e4aa05  
    159159int main() {
    160160        cout << "# 2 \"prelude.cfa\"  // needed for error messages from this file" << endl;
    161         cout << "trait sized(dtype T) {};" << endl;
     161        cout << "trait sized(T &) {};" << endl;
    162162
    163163        cout << "//////////////////////////" << endl;
     
    264264                for (auto cvq : qualifiersPair) {
    265265                        for (auto is_vol : { "        ", "volatile" }) {
    266                                 cout << "forall(dtype DT) void  ?{}(" << cvq.first << type << " * " << is_vol << " &, " << cvq.second << "DT *);" << endl;
     266                                cout << "forall(DT &) void  ?{}(" << cvq.first << type << " * " << is_vol << " &, " << cvq.second << "DT *);" << endl;
    267267                        }
    268268                }
     
    279279        for (auto cvq : qualifiersSingle) {
    280280                for (auto is_vol : { "        ", "volatile" }) {
    281                         cout << "forall(dtype DT) void  ?{}(" << cvq << "  DT" << " * " << is_vol << " &);" << endl;
     281                        cout << "forall(DT &) void  ?{}(" << cvq << "  DT" << " * " << is_vol << " &);" << endl;
    282282                }
    283283                for (auto is_vol : { "        ", "volatile" }) {
    284                         cout << "forall(dtype DT) void ^?{}(" << cvq << "  DT" << " * " << is_vol << " &);" << endl;
     284                        cout << "forall(DT &) void ^?{}(" << cvq << "  DT" << " * " << is_vol << " &);" << endl;
    285285                }
    286286        }
     
    290290                for (auto is_vol : { "        ", "volatile" }) {
    291291                        for (auto cvq : qualifiersSingle) {
    292                                 cout << "forall(dtype DT) void ?{}( " << cvq << type << " * " << is_vol << " &, zero_t);" << endl;
     292                                cout << "forall(DT &) void ?{}( " << cvq << type << " * " << is_vol << " &, zero_t);" << endl;
    293293                        }
    294294                }
     
    317317        for (auto op : pointerOperators) {
    318318                auto forall = [&op]() {
    319                         cout << "forall(dtype DT" << op.sized << ") ";
     319                        cout << "forall(DT &" << op.sized << ") ";
    320320                };
    321321                for (auto type : { "DT"/*, "void"*/ } ) {
     
    408408        for (auto is_vol : { "        ", "volatile" }) {
    409409                for (auto cvq : qualifiersPair) {
    410                                 cout << "forall(dtype DT) " << cvq.first << "void * ?=?( " << cvq.first << "void * " << is_vol << " &, " << cvq.second << "DT *);" << endl;
     410                                cout << "forall(DT &) " << cvq.first << "void * ?=?( " << cvq.first << "void * " << is_vol << " &, " << cvq.second << "DT *);" << endl;
    411411                }
    412412                for (auto cvq : qualifiersSingle) {
    413                         cout << "forall(dtype DT) " << cvq <<   "  DT * ?=?( " << cvq << "  DT * " << is_vol << " &, zero_t);" << endl;
     413                        cout << "forall(DT &) " << cvq <<   "  DT * ?=?( " << cvq << "  DT * " << is_vol << " &, zero_t);" << endl;
    414414                }
    415415        }
  • libcfa/prelude/prelude.old.cf

    r342af53 r8e4aa05  
    2323// ------------------------------------------------------------
    2424
    25 trait sized(dtype T) {};
     25trait sized(T &) {};
    2626
    2727// ------------------------------------------------------------
     
    6868long double _Complex    ?--( long double _Complex & ),          ?--( volatile long double _Complex & );
    6969
    70 forall( dtype T | sized(T) ) T *                         ?++(                T *& );
    71 forall( dtype T | sized(T) ) const T *           ?++( const          T *& );
    72 forall( dtype T | sized(T) ) volatile T *                ?++(       volatile T *& );
    73 forall( dtype T | sized(T) ) const volatile T *  ?++( const volatile T *& );
    74 forall( dtype T | sized(T) ) T *                         ?--(                T *& );
    75 forall( dtype T | sized(T) ) const T *           ?--( const          T *& );
    76 forall( dtype T | sized(T) ) volatile T *                ?--(       volatile T *& );
    77 forall( dtype T | sized(T) ) const volatile T *  ?--( const volatile T *& );
    78 
    79 forall( dtype T | sized(T) ) T &                 ?[?](                T *,          ptrdiff_t );
    80 forall( dtype T | sized(T) ) const T &   ?[?]( const          T *,          ptrdiff_t );
    81 forall( dtype T | sized(T) ) volatile T &        ?[?](       volatile T *,          ptrdiff_t );
    82 forall( dtype T | sized(T) ) const volatile T & ?[?]( const volatile T *,           ptrdiff_t );
    83 forall( dtype T | sized(T) ) T &                 ?[?](          ptrdiff_t,                T * );
    84 forall( dtype T | sized(T) ) const T &   ?[?](          ptrdiff_t, const          T * );
    85 forall( dtype T | sized(T) ) volatile T &        ?[?](          ptrdiff_t,       volatile T * );
    86 forall( dtype T | sized(T) ) const volatile T & ?[?](           ptrdiff_t, const volatile T * );
     70forall( T & | sized(T) ) T *                     ?++(                T *& );
     71forall( T & | sized(T) ) const T *               ?++( const          T *& );
     72forall( T & | sized(T) ) volatile T *            ?++(       volatile T *& );
     73forall( T & | sized(T) ) const volatile T *      ?++( const volatile T *& );
     74forall( T & | sized(T) ) T *                     ?--(                T *& );
     75forall( T & | sized(T) ) const T *               ?--( const          T *& );
     76forall( T & | sized(T) ) volatile T *            ?--(       volatile T *& );
     77forall( T & | sized(T) ) const volatile T *      ?--( const volatile T *& );
     78
     79forall( T & | sized(T) ) T &             ?[?](                T *,          ptrdiff_t );
     80forall( T & | sized(T) ) const T &       ?[?]( const          T *,          ptrdiff_t );
     81forall( T & | sized(T) ) volatile T &    ?[?](       volatile T *,          ptrdiff_t );
     82forall( T & | sized(T) ) const volatile T & ?[?]( const volatile T *,       ptrdiff_t );
     83forall( T & | sized(T) ) T &             ?[?](          ptrdiff_t,                T * );
     84forall( T & | sized(T) ) const T &       ?[?](          ptrdiff_t, const          T * );
     85forall( T & | sized(T) ) volatile T &    ?[?](          ptrdiff_t,       volatile T * );
     86forall( T & | sized(T) ) const volatile T & ?[?](               ptrdiff_t, const volatile T * );
    8787
    8888// ------------------------------------------------------------
     
    107107long double _Complex    ++?( long double _Complex & ),          --?( long double _Complex & );
    108108
    109 forall( dtype T | sized(T) ) T *                         ++?(                T *& );
    110 forall( dtype T | sized(T) ) const T *           ++?( const          T *& );
    111 forall( dtype T | sized(T) ) volatile T *                ++?(       volatile T *& );
    112 forall( dtype T | sized(T) ) const volatile T *  ++?( const volatile T *& );
    113 forall( dtype T | sized(T) ) T *                         --?(                T *& );
    114 forall( dtype T | sized(T) ) const T *           --?( const          T *& );
    115 forall( dtype T | sized(T) ) volatile T *                --?(       volatile T *& );
    116 forall( dtype T | sized(T) ) const volatile T *  --?( const volatile T *& );
    117 
    118 forall( dtype T | sized(T) ) T &                 *?(                 T * );
    119 forall( dtype T | sized(T) ) const T &           *?( const           T * );
    120 forall( dtype T | sized(T) ) volatile T &        *?(       volatile  T * );
    121 forall( dtype T | sized(T) ) const volatile T & *?( const volatile  T * );
     109forall( T & | sized(T) ) T *                     ++?(                T *& );
     110forall( T & | sized(T) ) const T *               ++?( const          T *& );
     111forall( T & | sized(T) ) volatile T *            ++?(       volatile T *& );
     112forall( T & | sized(T) ) const volatile T *      ++?( const volatile T *& );
     113forall( T & | sized(T) ) T *                     --?(                T *& );
     114forall( T & | sized(T) ) const T *               --?( const          T *& );
     115forall( T & | sized(T) ) volatile T *            --?(       volatile T *& );
     116forall( T & | sized(T) ) const volatile T *      --?( const volatile T *& );
     117
     118forall( T & | sized(T) ) T &             *?(                 T * );
     119forall( T & | sized(T) ) const T &               *?( const           T * );
     120forall( T & | sized(T) ) volatile T &    *?(       volatile  T * );
     121forall( T & | sized(T) ) const volatile T & *?( const volatile  T * );
    122122forall( ftype FT ) FT &          *?( FT * );
    123123
     
    142142                !?( float _Complex ),           !?( double _Complex ),          !?( long double _Complex );
    143143
    144 forall( dtype DT ) int !?(                DT * );
    145 forall( dtype DT ) int !?( const          DT * );
    146 forall( dtype DT ) int !?(       volatile DT * );
    147 forall( dtype DT ) int !?( const volatile DT * );
     144forall( DT & ) int !?(                DT * );
     145forall( DT & ) int !?( const          DT * );
     146forall( DT & ) int !?(       volatile DT * );
     147forall( DT & ) int !?( const volatile DT * );
    148148forall( ftype FT ) int !?( FT * );
    149149
     
    191191long double _Complex    ?+?( long double _Complex, long double _Complex ),      ?-?( long double _Complex, long double _Complex );
    192192
    193 forall( dtype T | sized(T) ) T *                ?+?(                T *,          ptrdiff_t );
    194 forall( dtype T | sized(T) ) T *                ?+?(          ptrdiff_t,                T * );
    195 forall( dtype T | sized(T) ) const T *          ?+?( const          T *,          ptrdiff_t );
    196 forall( dtype T | sized(T) ) const T *          ?+?(          ptrdiff_t, const          T * );
    197 forall( dtype T | sized(T) ) volatile T *       ?+?(       volatile T *,          ptrdiff_t );
    198 forall( dtype T | sized(T) ) volatile T *       ?+?(          ptrdiff_t,       volatile T * );
    199 forall( dtype T | sized(T) ) const volatile T * ?+?( const volatile T *,          ptrdiff_t );
    200 forall( dtype T | sized(T) ) const volatile T * ?+?(          ptrdiff_t, const volatile T * );
    201 forall( dtype T | sized(T) ) T *                ?-?(                T *,          ptrdiff_t );
    202 forall( dtype T | sized(T) ) const T *          ?-?( const          T *,          ptrdiff_t );
    203 forall( dtype T | sized(T) ) volatile T *       ?-?(       volatile T *,          ptrdiff_t );
    204 forall( dtype T | sized(T) ) const volatile T * ?-?( const volatile T *,          ptrdiff_t );
    205 forall( dtype T | sized(T) ) ptrdiff_t          ?-?( const volatile T *, const volatile T * );
     193forall( T & | sized(T) ) T *            ?+?(                T *,          ptrdiff_t );
     194forall( T & | sized(T) ) T *            ?+?(          ptrdiff_t,                T * );
     195forall( T & | sized(T) ) const T *              ?+?( const          T *,          ptrdiff_t );
     196forall( T & | sized(T) ) const T *              ?+?(          ptrdiff_t, const          T * );
     197forall( T & | sized(T) ) volatile T *   ?+?(       volatile T *,          ptrdiff_t );
     198forall( T & | sized(T) ) volatile T *   ?+?(          ptrdiff_t,       volatile T * );
     199forall( T & | sized(T) ) const volatile T *     ?+?( const volatile T *,          ptrdiff_t );
     200forall( T & | sized(T) ) const volatile T *     ?+?(          ptrdiff_t, const volatile T * );
     201forall( T & | sized(T) ) T *            ?-?(                T *,          ptrdiff_t );
     202forall( T & | sized(T) ) const T *              ?-?( const          T *,          ptrdiff_t );
     203forall( T & | sized(T) ) volatile T *   ?-?(       volatile T *,          ptrdiff_t );
     204forall( T & | sized(T) ) const volatile T *     ?-?( const volatile T *,          ptrdiff_t );
     205forall( T & | sized(T) ) ptrdiff_t              ?-?( const volatile T *, const volatile T * );
    206206
    207207// ------------------------------------------------------------
     
    255255           ?>?( long double, long double ),                             ?>=?( long double, long double );
    256256
    257 forall( dtype DT ) signed int ?<?(                 DT *,                DT * );
    258 forall( dtype DT ) signed int ?<?(  const          DT *, const          DT * );
    259 forall( dtype DT ) signed int ?<?(        volatile DT *,       volatile DT * );
    260 forall( dtype DT ) signed int ?<?(  const volatile DT *, const volatile DT * );
    261 
    262 forall( dtype DT ) signed int ?>?(                 DT *,                DT * );
    263 forall( dtype DT ) signed int ?>?(  const          DT *, const          DT * );
    264 forall( dtype DT ) signed int ?>?(        volatile DT *,       volatile DT * );
    265 forall( dtype DT ) signed int ?>?(  const volatile DT *, const volatile DT * );
    266 
    267 forall( dtype DT ) signed int ?<=?(                 DT *,                DT * );
    268 forall( dtype DT ) signed int ?<=?(  const          DT *, const          DT * );
    269 forall( dtype DT ) signed int ?<=?(        volatile DT *,       volatile DT * );
    270 forall( dtype DT ) signed int ?<=?( const volatile DT *, const volatile DT * );
    271 
    272 forall( dtype DT ) signed int ?>=?(                 DT *,                DT * );
    273 forall( dtype DT ) signed int ?>=?(  const          DT *, const          DT * );
    274 forall( dtype DT ) signed int ?>=?(        volatile DT *,       volatile DT * );
    275 forall( dtype DT ) signed int ?>=?( const volatile DT *, const volatile DT * );
     257forall( DT & ) signed int ?<?(                 DT *,                DT * );
     258forall( DT & ) signed int ?<?(  const          DT *, const          DT * );
     259forall( DT & ) signed int ?<?(        volatile DT *,       volatile DT * );
     260forall( DT & ) signed int ?<?(  const volatile DT *, const volatile DT * );
     261
     262forall( DT & ) signed int ?>?(                 DT *,                DT * );
     263forall( DT & ) signed int ?>?(  const          DT *, const          DT * );
     264forall( DT & ) signed int ?>?(        volatile DT *,       volatile DT * );
     265forall( DT & ) signed int ?>?(  const volatile DT *, const volatile DT * );
     266
     267forall( DT & ) signed int ?<=?(                 DT *,                DT * );
     268forall( DT & ) signed int ?<=?(  const          DT *, const          DT * );
     269forall( DT & ) signed int ?<=?(        volatile DT *,       volatile DT * );
     270forall( DT & ) signed int ?<=?( const volatile DT *, const volatile DT * );
     271
     272forall( DT & ) signed int ?>=?(                 DT *,                DT * );
     273forall( DT & ) signed int ?>=?(  const          DT *, const          DT * );
     274forall( DT & ) signed int ?>=?(        volatile DT *,       volatile DT * );
     275forall( DT & ) signed int ?>=?( const volatile DT *, const volatile DT * );
    276276
    277277// ------------------------------------------------------------
     
    302302signed int ?==?( one_t, one_t ),                                                        ?!=?( one_t, one_t );
    303303
    304 forall( dtype DT ) signed int ?==?(                DT *,                DT * );
    305 forall( dtype DT ) signed int ?==?( const          DT *, const          DT * );
    306 forall( dtype DT ) signed int ?==?(       volatile DT *,       volatile DT * );
    307 forall( dtype DT ) signed int ?==?( const volatile DT *, const volatile DT * );
     304forall( DT & ) signed int ?==?(            DT *,                DT * );
     305forall( DT & ) signed int ?==?( const      DT *, const          DT * );
     306forall( DT & ) signed int ?==?(       volatile DT *,       volatile DT * );
     307forall( DT & ) signed int ?==?( const volatile DT *, const volatile DT * );
    308308forall( ftype FT ) signed int ?==?( FT *, FT * );
    309 forall( dtype DT ) signed int ?!=?(                DT *,                DT * );
    310 forall( dtype DT ) signed int ?!=?( const          DT *, const          DT * );
    311 forall( dtype DT ) signed int ?!=?(       volatile DT *,       volatile DT * );
    312 forall( dtype DT ) signed int ?!=?( const volatile DT *, const volatile DT * );
     309forall( DT & ) signed int ?!=?(            DT *,                DT * );
     310forall( DT & ) signed int ?!=?( const      DT *, const          DT * );
     311forall( DT & ) signed int ?!=?(       volatile DT *,       volatile DT * );
     312forall( DT & ) signed int ?!=?( const volatile DT *, const volatile DT * );
    313313forall( ftype FT ) signed int ?!=?( FT *, FT * );
    314314
     
    376376
    377377forall( ftype FT ) FT *                 ?=?( FT *&, FT * );
    378 forall( ftype FT ) FT *                 ?=?( FT * volatile &, FT * );
    379 
    380 forall( dtype DT ) DT *                 ?=?(                 DT *          &,                   DT * );
    381 forall( dtype DT ) DT *                 ?=?(                 DT * volatile &,                   DT * );
    382 forall( dtype DT ) const DT *           ?=?( const           DT *          &,                   DT * );
    383 forall( dtype DT ) const DT *           ?=?( const           DT * volatile &,                   DT * );
    384 forall( dtype DT ) const DT *           ?=?( const           DT *          &, const             DT * );
    385 forall( dtype DT ) const DT *           ?=?( const           DT * volatile &, const             DT * );
    386 forall( dtype DT ) volatile DT *        ?=?(       volatile  DT *          &,                   DT * );
    387 forall( dtype DT ) volatile DT *        ?=?(       volatile  DT * volatile &,                   DT * );
    388 forall( dtype DT ) volatile DT *        ?=?(       volatile  DT *          &,       volatile    DT * );
    389 forall( dtype DT ) volatile DT *        ?=?(       volatile  DT * volatile &,       volatile    DT * );
    390 
    391 forall( dtype DT ) const volatile DT *  ?=?( const volatile  DT *          &,                   DT * );
    392 forall( dtype DT ) const volatile DT *  ?=?( const volatile  DT * volatile &,                   DT * );
    393 forall( dtype DT ) const volatile DT *  ?=?( const volatile  DT *          &, const             DT * );
    394 forall( dtype DT ) const volatile DT *  ?=?( const volatile  DT * volatile &, const             DT * );
    395 forall( dtype DT ) const volatile DT *  ?=?( const volatile  DT *          &,       volatile    DT * );
    396 forall( dtype DT ) const volatile DT *  ?=?( const volatile  DT * volatile &,       volatile    DT * );
    397 forall( dtype DT ) const volatile DT *  ?=?( const volatile  DT *          &, const volatile    DT * );
    398 forall( dtype DT ) const volatile DT *  ?=?( const volatile  DT * volatile &, const volatile    DT * );
    399 
    400 forall( dtype DT ) void *                ?=?(                void *          &,                 DT * );
    401 forall( dtype DT ) void *                ?=?(                void * volatile &,                 DT * );
    402 forall( dtype DT ) const void *          ?=?( const          void *          &,                 DT * );
    403 forall( dtype DT ) const void *          ?=?( const          void * volatile &,                 DT * );
    404 forall( dtype DT ) const void *          ?=?( const          void *          &, const           DT * );
    405 forall( dtype DT ) const void *          ?=?( const          void * volatile &, const           DT * );
    406 forall( dtype DT ) volatile void *       ?=?(       volatile void *          &,                 DT * );
    407 forall( dtype DT ) volatile void *       ?=?(       volatile void * volatile &,                 DT * );
    408 forall( dtype DT ) volatile void *       ?=?(       volatile void *          &,       volatile  DT * );
    409 forall( dtype DT ) volatile void *       ?=?(       volatile void * volatile &,       volatile  DT * );
    410 forall( dtype DT ) const volatile void * ?=?( const volatile void *          &,                 DT * );
    411 forall( dtype DT ) const volatile void * ?=?( const volatile void * volatile &,                 DT * );
    412 forall( dtype DT ) const volatile void * ?=?( const volatile void *          &, const           DT * );
    413 forall( dtype DT ) const volatile void * ?=?( const volatile void * volatile &, const           DT * );
    414 forall( dtype DT ) const volatile void * ?=?( const volatile void *          &,       volatile  DT * );
    415 forall( dtype DT ) const volatile void * ?=?( const volatile void * volatile &,       volatile  DT * );
    416 forall( dtype DT ) const volatile void * ?=?( const volatile void *          &, const volatile  DT * );
    417 forall( dtype DT ) const volatile void * ?=?( const volatile void * volatile &, const volatile  DT * );
     378forall( ftyep FT ) FT *                 ?=?( FT * volatile &, FT * );
     379
     380forall( DT & ) DT *                     ?=?(                 DT *          &,                   DT * );
     381forall( DT & ) DT *                     ?=?(                 DT * volatile &,                   DT * );
     382forall( DT & ) const DT *               ?=?( const           DT *          &,                   DT * );
     383forall( DT & ) const DT *               ?=?( const           DT * volatile &,                   DT * );
     384forall( DT & ) const DT *               ?=?( const           DT *          &, const             DT * );
     385forall( DT & ) const DT *               ?=?( const           DT * volatile &, const             DT * );
     386forall( DT & ) volatile DT *    ?=?(       volatile  DT *          &,                   DT * );
     387forall( DT & ) volatile DT *    ?=?(       volatile  DT * volatile &,                   DT * );
     388forall( DT & ) volatile DT *    ?=?(       volatile  DT *          &,       volatile    DT * );
     389forall( DT & ) volatile DT *    ?=?(       volatile  DT * volatile &,       volatile    DT * );
     390
     391forall( DT & ) const volatile DT *      ?=?( const volatile  DT *          &,                   DT * );
     392forall( DT & ) const volatile DT *  ?=?( const volatile  DT * volatile &,                       DT * );
     393forall( DT & ) const volatile DT *  ?=?( const volatile  DT *      &, const             DT * );
     394forall( DT & ) const volatile DT *  ?=?( const volatile  DT * volatile &, const         DT * );
     395forall( DT & ) const volatile DT *  ?=?( const volatile  DT *      &,       volatile    DT * );
     396forall( DT & ) const volatile DT *  ?=?( const volatile  DT * volatile &,           volatile    DT * );
     397forall( DT & ) const volatile DT *  ?=?( const volatile  DT *      &, const volatile    DT * );
     398forall( DT & ) const volatile DT *  ?=?( const volatile  DT * volatile &, const volatile        DT * );
     399
     400forall( DT & ) void *            ?=?(                void *          &,                 DT * );
     401forall( DT & ) void *            ?=?(                void * volatile &,                 DT * );
     402forall( DT & ) const void *              ?=?( const          void *          &,                 DT * );
     403forall( DT & ) const void *              ?=?( const          void * volatile &,                 DT * );
     404forall( DT & ) const void *              ?=?( const          void *          &, const           DT * );
     405forall( DT & ) const void *              ?=?( const          void * volatile &, const           DT * );
     406forall( DT & ) volatile void *   ?=?(       volatile void *          &,                 DT * );
     407forall( DT & ) volatile void *   ?=?(       volatile void * volatile &,                 DT * );
     408forall( DT & ) volatile void *   ?=?(       volatile void *          &,       volatile  DT * );
     409forall( DT & ) volatile void *   ?=?(       volatile void * volatile &,       volatile  DT * );
     410forall( DT & ) const volatile void * ?=?( const volatile void *      &,                 DT * );
     411forall( DT & ) const volatile void * ?=?( const volatile void * volatile &,                     DT * );
     412forall( DT & ) const volatile void * ?=?( const volatile void *      &, const           DT * );
     413forall( DT & ) const volatile void * ?=?( const volatile void * volatile &, const               DT * );
     414forall( DT & ) const volatile void * ?=?( const volatile void *      &,       volatile  DT * );
     415forall( DT & ) const volatile void * ?=?( const volatile void * volatile &,           volatile  DT * );
     416forall( DT & ) const volatile void * ?=?( const volatile void *      &, const volatile  DT * );
     417forall( DT & ) const volatile void * ?=?( const volatile void * volatile &, const volatile      DT * );
    418418
    419419//forall( dtype DT ) DT *                       ?=?(                DT *          &, zero_t );
    420420//forall( dtype DT ) DT *                       ?=?(                DT * volatile &, zero_t );
    421 forall( dtype DT ) const DT *           ?=?( const          DT *          &, zero_t );
    422 forall( dtype DT ) const DT *           ?=?( const          DT * volatile &, zero_t );
     421forall( DT & ) const DT *               ?=?( const          DT *          &, zero_t );
     422forall( DT & ) const DT *               ?=?( const          DT * volatile &, zero_t );
    423423//forall( dtype DT ) volatile DT *      ?=?( volatile       DT *          &, zero_t );
    424424//forall( dtype DT ) volatile DT *      ?=?( volatile       DT * volatile &, zero_t );
    425 forall( dtype DT ) const volatile DT *  ?=?( const volatile DT *          &, zero_t );
    426 forall( dtype DT ) const volatile DT *  ?=?( const volatile DT * volatile &, zero_t );
     425forall( DT & ) const volatile DT *      ?=?( const volatile DT *          &, zero_t );
     426forall( DT & ) const volatile DT *      ?=?( const volatile DT * volatile &, zero_t );
    427427
    428428forall( ftype FT ) FT *                 ?=?( FT *          &, zero_t );
    429429forall( ftype FT ) FT *                 ?=?( FT * volatile &, zero_t );
    430430
    431 forall( dtype T | sized(T) ) T *                ?+=?(                T *          &, ptrdiff_t );
    432 forall( dtype T | sized(T) ) T *                ?+=?(                T * volatile &, ptrdiff_t );
    433 forall( dtype T | sized(T) ) const T *          ?+=?( const          T *          &, ptrdiff_t );
    434 forall( dtype T | sized(T) ) const T *          ?+=?( const          T * volatile &, ptrdiff_t );
    435 forall( dtype T | sized(T) ) volatile T *       ?+=?(       volatile T *          &, ptrdiff_t );
    436 forall( dtype T | sized(T) ) volatile T *       ?+=?(       volatile T * volatile &, ptrdiff_t );
    437 forall( dtype T | sized(T) ) const volatile T * ?+=?( const volatile T *          &, ptrdiff_t );
    438 forall( dtype T | sized(T) ) const volatile T * ?+=?( const volatile T * volatile &, ptrdiff_t );
    439 forall( dtype T | sized(T) ) T *                ?-=?(                T *          &, ptrdiff_t );
    440 forall( dtype T | sized(T) ) T *                ?-=?(                T * volatile &, ptrdiff_t );
    441 forall( dtype T | sized(T) ) const T *          ?-=?( const          T *          &, ptrdiff_t );
    442 forall( dtype T | sized(T) ) const T *          ?-=?( const          T * volatile &, ptrdiff_t );
    443 forall( dtype T | sized(T) ) volatile T *       ?-=?(       volatile T *          &, ptrdiff_t );
    444 forall( dtype T | sized(T) ) volatile T *       ?-=?(       volatile T * volatile &, ptrdiff_t );
    445 forall( dtype T | sized(T) ) const volatile T * ?-=?( const volatile T *          &, ptrdiff_t );
    446 forall( dtype T | sized(T) ) const volatile T * ?-=?( const volatile T * volatile &, ptrdiff_t );
     431forall( T & | sized(T) ) T *            ?+=?(                T *          &, ptrdiff_t );
     432forall( T & | sized(T) ) T *            ?+=?(                T * volatile &, ptrdiff_t );
     433forall( T & | sized(T) ) const T *              ?+=?( const          T *          &, ptrdiff_t );
     434forall( T & | sized(T) ) const T *              ?+=?( const          T * volatile &, ptrdiff_t );
     435forall( T & | sized(T) ) volatile T *   ?+=?(       volatile T *          &, ptrdiff_t );
     436forall( T & | sized(T) ) volatile T *   ?+=?(       volatile T * volatile &, ptrdiff_t );
     437forall( T & | sized(T) ) const volatile T *     ?+=?( const volatile T *          &, ptrdiff_t );
     438forall( T & | sized(T) ) const volatile T *     ?+=?( const volatile T * volatile &, ptrdiff_t );
     439forall( T & | sized(T) ) T *            ?-=?(                T *          &, ptrdiff_t );
     440forall( T & | sized(T) ) T *            ?-=?(                T * volatile &, ptrdiff_t );
     441forall( T & | sized(T) ) const T *              ?-=?( const          T *          &, ptrdiff_t );
     442forall( T & | sized(T) ) const T *              ?-=?( const          T * volatile &, ptrdiff_t );
     443forall( T & | sized(T) ) volatile T *   ?-=?(       volatile T *          &, ptrdiff_t );
     444forall( T & | sized(T) ) volatile T *   ?-=?(       volatile T * volatile &, ptrdiff_t );
     445forall( T & | sized(T) ) const volatile T *     ?-=?( const volatile T *          &, ptrdiff_t );
     446forall( T & | sized(T) ) const volatile T *     ?-=?( const volatile T * volatile &, ptrdiff_t );
    447447
    448448_Bool                   ?=?( _Bool &, _Bool ),                                  ?=?( volatile _Bool &, _Bool );
     
    723723forall( ftype FT ) void ?{}( FT * volatile &, FT * );
    724724
    725 forall( dtype DT ) void ?{}(                 DT *          &,                   DT * );
    726 forall( dtype DT ) void ?{}( const           DT *          &,                   DT * );
    727 forall( dtype DT ) void ?{}( const           DT *          &, const             DT * );
    728 forall( dtype DT ) void ?{}(       volatile  DT *          &,                   DT * );
    729 forall( dtype DT ) void ?{}(       volatile  DT *          &,       volatile    DT * );
    730 forall( dtype DT ) void ?{}( const volatile  DT *          &,                   DT * );
    731 forall( dtype DT ) void ?{}( const volatile  DT *          &, const             DT * );
    732 forall( dtype DT ) void ?{}( const volatile  DT *          &,       volatile    DT * );
    733 forall( dtype DT ) void ?{}( const volatile  DT *          &, const volatile    DT * );
    734 
    735 forall( dtype DT ) void ?{}(                 void *          &,                 DT * );
    736 forall( dtype DT ) void ?{}( const           void *          &,                 DT * );
    737 forall( dtype DT ) void ?{}( const           void *          &, const           DT * );
    738 forall( dtype DT ) void ?{}(        volatile void *          &,                 DT * );
    739 forall( dtype DT ) void ?{}(        volatile void *          &,       volatile  DT * );
    740 forall( dtype DT ) void ?{}( const volatile void *           &,                 DT * );
    741 forall( dtype DT ) void ?{}( const volatile void *           &, const           DT * );
    742 forall( dtype DT ) void ?{}( const volatile void *           &,       volatile  DT * );
    743 forall( dtype DT ) void ?{}( const volatile void *           &, const volatile  DT * );
     725forall( DT & ) void ?{}(                     DT *          &,                   DT * );
     726forall( DT & ) void ?{}( const       DT *          &,                   DT * );
     727forall( DT & ) void ?{}( const       DT *          &, const             DT * );
     728forall( DT & ) void ?{}(           volatile  DT *          &,                   DT * );
     729forall( DT & ) void ?{}(           volatile  DT *          &,       volatile    DT * );
     730forall( DT & ) void ?{}( const volatile  DT *      &,                   DT * );
     731forall( DT & ) void ?{}( const volatile  DT *      &, const             DT * );
     732forall( DT & ) void ?{}( const volatile  DT *      &,       volatile    DT * );
     733forall( DT & ) void ?{}( const volatile  DT *      &, const volatile    DT * );
     734
     735forall( DT & ) void ?{}(                     void *          &,                 DT * );
     736forall( DT & ) void ?{}( const       void *          &,                 DT * );
     737forall( DT & ) void ?{}( const       void *          &, const           DT * );
     738forall( DT & ) void ?{}(            volatile void *          &,                 DT * );
     739forall( DT & ) void ?{}(            volatile void *          &,       volatile  DT * );
     740forall( DT & ) void ?{}( const volatile void *       &,                 DT * );
     741forall( DT & ) void ?{}( const volatile void *       &, const           DT * );
     742forall( DT & ) void ?{}( const volatile void *       &,       volatile  DT * );
     743forall( DT & ) void ?{}( const volatile void *       &, const volatile  DT * );
    744744
    745745//forall( dtype DT ) void ?{}(              DT *          &, zero_t );
    746746//forall( dtype DT ) void ?{}(              DT * volatile &, zero_t );
    747 forall( dtype DT ) void ?{}( const          DT *          &, zero_t );
     747forall( DT & ) void ?{}( const      DT *          &, zero_t );
    748748//forall( dtype DT ) void ?{}( volatile     DT *          &, zero_t );
    749749//forall( dtype DT ) void ?{}( volatile     DT * volatile &, zero_t );
    750 forall( dtype DT ) void ?{}( const volatile DT *          &, zero_t );
     750forall( DT & ) void ?{}( const volatile DT *      &, zero_t );
    751751
    752752forall( ftype FT ) void ?{}( FT *          &, zero_t );
     
    755755forall( ftype FT ) void ?{}( FT *          & );
    756756
    757 forall( dtype DT ) void ?{}(                 DT *          &);
    758 forall( dtype DT ) void ?{}( const           DT *          &);
    759 forall( dtype DT ) void ?{}(       volatile  DT *          &);
    760 forall( dtype DT ) void ?{}( const volatile  DT *          &);
     757forall( DT & ) void     ?{}(                 DT *          &);
     758forall( DT & ) void     ?{}( const           DT *          &);
     759forall( DT & ) void     ?{}(       volatile  DT *          &);
     760forall( DT & ) void ?{}( const volatile  DT *      &);
    761761
    762762void    ?{}(                void *          &);
     
    768768forall( ftype FT ) void ^?{}( FT *         & );
    769769
    770 forall( dtype DT ) void ^?{}(                DT *          &);
    771 forall( dtype DT ) void ^?{}( const          DT *          &);
    772 forall( dtype DT ) void ^?{}(      volatile  DT *          &);
    773 forall( dtype DT ) void ^?{}( const volatile  DT *         &);
     770forall( DT & ) void     ^?{}(                DT *          &);
     771forall( DT & ) void     ^?{}( const          DT *          &);
     772forall( DT & ) void     ^?{}(      volatile  DT *          &);
     773forall( DT & ) void ^?{}( const volatile  DT *     &);
    774774
    775775void ^?{}(                  void *          &);
  • libcfa/prelude/sync-builtins.cf

    r342af53 r8e4aa05  
    206206_Bool __sync_bool_compare_and_swap(volatile unsigned __int128 *, unsigned __int128, unsigned __int128,...);
    207207#endif
    208 forall(dtype T) _Bool __sync_bool_compare_and_swap(T * volatile *, T *, T*, ...);
     208forall(T &) _Bool __sync_bool_compare_and_swap(T * volatile *, T *, T*, ...);
    209209
    210210char __sync_val_compare_and_swap(volatile char *, char, char,...);
     
    223223unsigned __int128 __sync_val_compare_and_swap(volatile unsigned __int128 *, unsigned __int128, unsigned __int128,...);
    224224#endif
    225 forall(dtype T) T * __sync_val_compare_and_swap(T * volatile *, T *, T*,...);
     225forall(T &) T * __sync_val_compare_and_swap(T * volatile *, T *, T*,...);
    226226
    227227char __sync_lock_test_and_set(volatile char *, char,...);
     
    326326void __atomic_exchange(volatile unsigned __int128 *, volatile unsigned __int128 *, volatile unsigned __int128 *, int);
    327327#endif
    328 forall(dtype T) T * __atomic_exchange_n(T * volatile *, T *, int);
    329 forall(dtype T) void __atomic_exchange(T * volatile *, T * volatile *, T * volatile *, int);
     328forall(T &) T * __atomic_exchange_n(T * volatile *, T *, int);
     329forall(T &) void __atomic_exchange(T * volatile *, T * volatile *, T * volatile *, int);
    330330
    331331_Bool __atomic_load_n(const volatile _Bool *, int);
     
    359359void __atomic_load(const volatile unsigned __int128 *, volatile unsigned __int128 *, int);
    360360#endif
    361 forall(dtype T) T * __atomic_load_n(T * const volatile *, int);
    362 forall(dtype T) void __atomic_load(T * const volatile *, T **, int);
     361forall(T &) T * __atomic_load_n(T * const volatile *, int);
     362forall(T &) void __atomic_load(T * const volatile *, T **, int);
    363363
    364364_Bool __atomic_compare_exchange_n(volatile char *, char *, char, _Bool, int, int);
     
    390390_Bool __atomic_compare_exchange   (volatile unsigned __int128 *, unsigned __int128 *, unsigned __int128 *, _Bool, int, int);
    391391#endif
    392 forall(dtype T) _Bool __atomic_compare_exchange_n (T * volatile *, T **, T*, _Bool, int, int);
    393 forall(dtype T) _Bool __atomic_compare_exchange   (T * volatile *, T **, T**, _Bool, int, int);
     392forall(T &) _Bool __atomic_compare_exchange_n (T * volatile *, T **, T*, _Bool, int, int);
     393forall(T &) _Bool __atomic_compare_exchange   (T * volatile *, T **, T**, _Bool, int, int);
    394394
    395395void __atomic_store_n(volatile _Bool *, _Bool, int);
     
    423423void __atomic_store(volatile unsigned __int128 *, unsigned __int128 *, int);
    424424#endif
    425 forall(dtype T) void __atomic_store_n(T * volatile *, T *, int);
    426 forall(dtype T) void __atomic_store(T * volatile *, T **, int);
     425forall(T &) void __atomic_store_n(T * volatile *, T *, int);
     426forall(T &) void __atomic_store(T * volatile *, T **, int);
    427427
    428428char __atomic_add_fetch  (volatile char *, char, int);
  • libcfa/src/Makefile.am

    r342af53 r8e4aa05  
    7676        stdlib.hfa \
    7777        time.hfa \
     78        bits/weakso_locks.hfa \
    7879        containers/maybe.hfa \
    7980        containers/pair.hfa \
  • libcfa/src/bitmanip.hfa

    r342af53 r8e4aa05  
    100100        unsigned long long int floor2( unsigned long long int n, unsigned long long int align ) { verify( is_pow2( align ) ); return n & -align; }
    101101
    102         // forall( otype T | { T ?&?( T, T ); T -?( T ); } )
     102        // forall( T | { T ?&?( T, T ); T -?( T ); } )
    103103        // T floor2( T n, T align ) { verify( is_pow2( align ) ); return n & -align; }
    104104
     
    115115        unsigned long long int ceiling2( unsigned long long int n, unsigned long long int align ) { verify( is_pow2( align ) ); return -floor2( -n, align ); }
    116116
    117         // forall( otype T | { T floor2( T, T ); T -?( T ); } )
     117        // forall( T | { T floor2( T, T ); T -?( T ); } )
    118118        // T ceiling2( T n, T align ) { verify( is_pow2( align ) ); return -floor2( -n, align ); }
    119119} // distribution
  • libcfa/src/bits/algorithm.hfa

    r342af53 r8e4aa05  
    1717
    1818#ifdef SAFE_SORT
    19 forall( otype T | {  int ?<?( T, T ); int ?>?( T, T ); } ) static inline void __libcfa_small_sort2( T * arr );
    20 forall( otype T | {  int ?<?( T, T ); int ?>?( T, T ); } ) static inline void __libcfa_small_sort3( T * arr );
    21 forall( otype T | {  int ?<?( T, T ); int ?>?( T, T ); } ) static inline void __libcfa_small_sort4( T * arr );
    22 forall( otype T | {  int ?<?( T, T ); int ?>?( T, T ); } ) static inline void __libcfa_small_sort5( T * arr );
    23 forall( otype T | {  int ?<?( T, T ); int ?>?( T, T ); } ) static inline void __libcfa_small_sort6( T * arr );
    24 forall( otype T | {  int ?<?( T, T ); int ?>?( T, T ); } ) static inline void __libcfa_small_sortN( T * arr, size_t dim );
     19forall( T | {  int ?<?( T, T ); int ?>?( T, T ); } ) static inline void __libcfa_small_sort2( T * arr );
     20forall( T | {  int ?<?( T, T ); int ?>?( T, T ); } ) static inline void __libcfa_small_sort3( T * arr );
     21forall( T | {  int ?<?( T, T ); int ?>?( T, T ); } ) static inline void __libcfa_small_sort4( T * arr );
     22forall( T | {  int ?<?( T, T ); int ?>?( T, T ); } ) static inline void __libcfa_small_sort5( T * arr );
     23forall( T | {  int ?<?( T, T ); int ?>?( T, T ); } ) static inline void __libcfa_small_sort6( T * arr );
     24forall( T | {  int ?<?( T, T ); int ?>?( T, T ); } ) static inline void __libcfa_small_sortN( T * arr, size_t dim );
    2525
    26 forall( otype T | {  int ?<?( T, T ); int ?>?( T, T ); } )
     26forall( T | {  int ?<?( T, T ); int ?>?( T, T ); } )
    2727static inline void __libcfa_small_sort( T * arr, size_t dim ) {
    2828        switch( dim ) {
     
    4141#define SWAP(x,y) { T a = min(arr[x], arr[y]); T b = max(arr[x], arr[y]); arr[x] = a; arr[y] = b;}
    4242
    43 forall( otype T | {  int ?<?( T, T ); int ?>?( T, T ); } )
     43forall( T | {  int ?<?( T, T ); int ?>?( T, T ); } )
    4444static inline void __libcfa_small_sort2( T * arr ) {
    4545        SWAP(0, 1);
    4646}
    4747
    48 forall( otype T | {  int ?<?( T, T ); int ?>?( T, T ); } )
     48forall( T | {  int ?<?( T, T ); int ?>?( T, T ); } )
    4949static inline void __libcfa_small_sort3( T * arr ) {
    5050        SWAP(1, 2);
     
    5353}
    5454
    55 forall( otype T | {  int ?<?( T, T ); int ?>?( T, T ); } )
     55forall( T | {  int ?<?( T, T ); int ?>?( T, T ); } )
    5656static inline void __libcfa_small_sort4( T * arr ) {
    5757        SWAP(0, 1);
     
    6262}
    6363
    64 forall( otype T | {  int ?<?( T, T ); int ?>?( T, T ); } )
     64forall( T | {  int ?<?( T, T ); int ?>?( T, T ); } )
    6565static inline void __libcfa_small_sort5( T * arr ) {
    6666        SWAP(0, 1);
     
    7575}
    7676
    77 forall( otype T | {  int ?<?( T, T ); int ?>?( T, T ); } )
     77forall( T | {  int ?<?( T, T ); int ?>?( T, T ); } )
    7878static inline void __libcfa_small_sort6( T * arr ) {
    7979        SWAP(1, 2);
     
    9191}
    9292
    93 forall( otype T | {  int ?<?( T, T ); int ?>?( T, T ); } )
     93forall( T | {  int ?<?( T, T ); int ?>?( T, T ); } )
    9494static inline void __libcfa_small_sortN( T * arr, size_t dim ) {
    9595        int i, j;
     
    112112static inline void __libcfa_small_sortN( void* * arr, size_t dim );
    113113
    114 forall( dtype T )
     114forall( T & )
    115115static inline void __libcfa_small_sort( T* * arr, size_t dim ) {
    116116        switch( dim ) {
  • libcfa/src/bits/collection.hfa

    r342af53 r8e4aa05  
     1//
     2// Cforall Version 1.0.0 Copyright (C) 2021 University of Waterloo
     3//
     4// The contents of this file are covered under the licence agreement in the
     5// file "LICENCE" distributed with Cforall.
     6//
     7// bits/collection.hfa -- PUBLIC
     8// Intrusive singly-linked list
     9//
     10// Author           : Colby Alexander Parsons & Peter A. Buhr
     11// Created On       : Thu Jan 21 19:46:50 2021
     12// Last Modified By :
     13// Last Modified On :
     14// Update Count     :
     15//
     16
    117#pragma once
    2 #include <stdio.h> // REMOVE THIS AFTER DEBUGGING
    3 
    418
    519struct Colable {
    6         struct Colable * next;                                                                          // next node in the list
     20        // next node in the list
    721        // invariant: (next != 0) <=> listed()
     22        struct Colable * next;
    823};
    924#ifdef __cforall
     
    3146
    3247        // // wrappers to make Collection have T
    33         // forall( dtype T ) {
     48        // forall( T & ) {
    3449        //      T *& Next( T * n ) {
    3550        //              return (T *)Next( (Colable *)n );
     
    3853} // distribution
    3954
    40 forall( dtype T | { T *& Next ( T * ); } ) {
     55static inline forall( T & | { T *& Next ( T * ); } ) {
    4156        bool listed( T * n ) {
    4257                return Next( n ) != 0p;
     
    5368        Collection & ?=?( const Collection & ) = void;          // no assignment
    5469
    55         void ?{}( Collection & collection ) with( collection ) {       
     70        void ?{}( Collection & collection ) with( collection ) {
    5671                root = 0p;
    5772        } // post: empty()
     
    7691        } // post: elts = null
    7792
    78         forall( dtype T ) {
     93        forall( T & ) {
    7994                T * Curr( ColIter & ci ) with( ci ) {
    8095                        return (T *)curr;
  • libcfa/src/bits/containers.hfa

    r342af53 r8e4aa05  
    2323
    2424#ifdef __cforall
    25         forall(dtype T)
     25        forall(T &)
    2626#else
    2727        #define T void
     
    4040
    4141#ifdef __cforall
    42         // forall(otype T | sized(T))
     42        // forall(T | sized(T))
    4343        // static inline void ?{}(__small_array(T) & this) {}
    4444
    45         forall(dtype T | sized(T))
     45        forall(T & | sized(T))
    4646        static inline T & ?[?]( __small_array(T) & this, __lock_size_t idx ) {
    4747                return ((typeof(this.data))this.data)[idx];
    4848        }
    4949
    50         forall(dtype T | sized(T))
     50        forall(T & | sized(T))
    5151        static inline T & ?[?]( const __small_array(T) & this, __lock_size_t idx ) {
    5252                return ((typeof(this.data))this.data)[idx];
    5353        }
    5454
    55         forall(dtype T)
     55        forall(T &)
    5656        static inline T * begin( const __small_array(T) & this ) {
    5757                return ((typeof(this.data))this.data);
    5858        }
    5959
    60         forall(dtype T | sized(T))
     60        forall(T & | sized(T))
    6161        static inline T * end( const __small_array(T) & this ) {
    6262                return ((typeof(this.data))this.data) + this.size;
     
    6969
    7070#ifdef __cforall
    71         trait is_node(dtype T) {
     71        trait is_node(T &) {
    7272                T *& get_next( T & );
    7373        };
     
    7878//-----------------------------------------------------------------------------
    7979#ifdef __cforall
    80         forall(dtype TYPE)
     80        forall(TYPE &)
    8181        #define T TYPE
    8282#else
     
    9595
    9696#ifdef __cforall
    97         forall(dtype T)
     97        forall(T &)
    9898        static inline void ?{}( __stack(T) & this ) {
    9999                (this.top){ 0p };
    100100        }
    101101
    102         static inline forall( dtype T | is_node(T) ) {
     102        static inline forall( T & | is_node(T) ) {
    103103                void push( __stack(T) & this, T * val ) {
    104104                        verify( !get_next( *val ) );
     
    126126//-----------------------------------------------------------------------------
    127127#ifdef __cforall
    128         forall(dtype TYPE)
     128        forall(TYPE &)
    129129        #define T TYPE
    130130#else
     
    144144
    145145#ifdef __cforall
    146         static inline forall( dtype T | is_node(T) ) {
     146        static inline forall( T & | is_node(T) ) {
    147147                void ?{}( __queue(T) & this ) with( this ) {
    148148                        (this.head){ 1p };
     
    151151                }
    152152
    153                 void append( __queue(T) & this, T * val ) with( this ) {
     153                void append( __queue(T) & this, T * val ) with(this) {
    154154                        verify(this.tail != 0p);
    155155                        verify(*this.tail == 1p);
     
    161161                T * peek( __queue(T) & this ) {
    162162                        verify(*this.tail == 1p);
    163                         T * front = this.head;
    164                         if( front != 1p ) {
     163                        T * frontnode = this.head;
     164                        if( frontnode != 1p ) {
    165165                                verify(*this.tail == 1p);
    166                                 return front;
     166                                return frontnode;
    167167                        }
    168168                        verify(*this.tail == 1p);
     
    215215//-----------------------------------------------------------------------------
    216216#ifdef __cforall
    217         forall(dtype TYPE)
     217        forall(TYPE &)
    218218        #define T TYPE
    219219        #define __getter_t * [T * & next, T * & prev] ( T & )
     
    237237
    238238#ifdef __cforall
    239         forall(dtype T )
     239        forall(T & )
    240240        static inline [void] ?{}( __dllist(T) & this, * [T * & next, T * & prev] ( T & ) __get ) {
    241241                (this.head){ 0p };
     
    245245        #define next 0
    246246        #define prev 1
    247         static inline forall(dtype T) {
     247        static inline forall(T &) {
    248248                void push_front( __dllist(T) & this, T & node ) with( this ) {
    249249                        verify(__get);
  • libcfa/src/bits/defs.hfa

    r342af53 r8e4aa05  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // defs.hfa --
     7// defs.hfa -- Commen macros, functions and typedefs
     8// Most files depend on them and they are always useful to have.
     9//
     10//  *** Must not contain code specific to libcfathread ***
    811//
    912// Author           : Thierry Delisle
     
    6265        #endif
    6366}
     67
     68// pause to prevent excess processor bus usage
     69#if defined( __i386 ) || defined( __x86_64 )
     70        #define Pause() __asm__ __volatile__ ( "pause" : : : )
     71#elif defined( __ARM_ARCH )
     72        #define Pause() __asm__ __volatile__ ( "YIELD" : : : )
     73#else
     74        #error unsupported architecture
     75#endif
     76
     77#define CFA_IO_LAZY (1_l64u << 32_l64u)
  • libcfa/src/bits/locks.hfa

    r342af53 r8e4aa05  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // bits/locks.hfa -- Fast internal locks.
     7// bits/locks.hfa -- Basic spinlocks that are reused in the system.
     8// Used for locks that aren't specific to cforall threads and can be used anywhere
     9//
     10//  *** Must not contain code specific to libcfathread ***
    811//
    912// Author           : Thierry Delisle
     
    1922#include "bits/defs.hfa"
    2023#include <assert.h>
    21 
    22 #ifdef __cforall
    23         extern "C" {
    24                 #include <pthread.h>
    25         }
    26 #endif
    27 
    28 // pause to prevent excess processor bus usage
    29 #if defined( __i386 ) || defined( __x86_64 )
    30         #define Pause() __asm__ __volatile__ ( "pause" : : : )
    31 #elif defined( __ARM_ARCH )
    32         #define Pause() __asm__ __volatile__ ( "YIELD" : : : )
    33 #else
    34         #error unsupported architecture
    35 #endif
    3624
    3725struct __spinlock_t {
     
    10492                enable_interrupts_noPoll();
    10593        }
    106 
    107 
    108         #ifdef __CFA_WITH_VERIFY__
    109                 extern bool __cfaabi_dbg_in_kernel();
    110         #endif
    111 
    112         extern "C" {
    113                 char * strerror(int);
    114         }
    115         #define CHECKED(x) { int err = x; if( err != 0 ) abort("KERNEL ERROR: Operation \"" #x "\" return error %d - %s\n", err, strerror(err)); }
    116 
    117         struct __bin_sem_t {
    118                 pthread_mutex_t         lock;
    119                 pthread_cond_t          cond;
    120                 int                     val;
    121         };
    122 
    123         static inline void ?{}(__bin_sem_t & this) with( this ) {
    124                 // Create the mutex with error checking
    125                 pthread_mutexattr_t mattr;
    126                 pthread_mutexattr_init( &mattr );
    127                 pthread_mutexattr_settype( &mattr, PTHREAD_MUTEX_ERRORCHECK_NP);
    128                 pthread_mutex_init(&lock, &mattr);
    129 
    130                 pthread_cond_init (&cond, (const pthread_condattr_t *)0p);  // workaround trac#208: cast should not be required
    131                 val = 0;
    132         }
    133 
    134         static inline void ^?{}(__bin_sem_t & this) with( this ) {
    135                 CHECKED( pthread_mutex_destroy(&lock) );
    136                 CHECKED( pthread_cond_destroy (&cond) );
    137         }
    138 
    139         static inline void wait(__bin_sem_t & this) with( this ) {
    140                 verify(__cfaabi_dbg_in_kernel());
    141                 CHECKED( pthread_mutex_lock(&lock) );
    142                         while(val < 1) {
    143                                 pthread_cond_wait(&cond, &lock);
    144                         }
    145                         val -= 1;
    146                 CHECKED( pthread_mutex_unlock(&lock) );
    147         }
    148 
    149         static inline bool post(__bin_sem_t & this) with( this ) {
    150                 bool needs_signal = false;
    151 
    152                 CHECKED( pthread_mutex_lock(&lock) );
    153                         if(val < 1) {
    154                                 val += 1;
    155                                 pthread_cond_signal(&cond);
    156                                 needs_signal = true;
    157                         }
    158                 CHECKED( pthread_mutex_unlock(&lock) );
    159 
    160                 return needs_signal;
    161         }
    162 
    163         #undef CHECKED
    164 
    165         struct $thread;
    166         extern void park( void );
    167         extern void unpark( struct $thread * this );
    168         static inline struct $thread * active_thread ();
    169 
    170         // Semaphore which only supports a single thread
    171         struct single_sem {
    172                 struct $thread * volatile ptr;
    173         };
    174 
    175         static inline {
    176                 void  ?{}(single_sem & this) {
    177                         this.ptr = 0p;
    178                 }
    179 
    180                 void ^?{}(single_sem &) {}
    181 
    182                 bool wait(single_sem & this) {
    183                         for() {
    184                                 struct $thread * expected = this.ptr;
    185                                 if(expected == 1p) {
    186                                         if(__atomic_compare_exchange_n(&this.ptr, &expected, 0p, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
    187                                                 return false;
    188                                         }
    189                                 }
    190                                 else {
    191                                         /* paranoid */ verify( expected == 0p );
    192                                         if(__atomic_compare_exchange_n(&this.ptr, &expected, active_thread(), false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
    193                                                 park();
    194                                                 return true;
    195                                         }
    196                                 }
    197 
    198                         }
    199                 }
    200 
    201                 bool post(single_sem & this) {
    202                         for() {
    203                                 struct $thread * expected = this.ptr;
    204                                 if(expected == 1p) return false;
    205                                 if(expected == 0p) {
    206                                         if(__atomic_compare_exchange_n(&this.ptr, &expected, 1p, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
    207                                                 return false;
    208                                         }
    209                                 }
    210                                 else {
    211                                         if(__atomic_compare_exchange_n(&this.ptr, &expected, 0p, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
    212                                                 unpark( expected );
    213                                                 return true;
    214                                         }
    215                                 }
    216                         }
    217                 }
    218         }
    219 
    220         // Synchronozation primitive which only supports a single thread and one post
    221         // Similar to a binary semaphore with a 'one shot' semantic
    222         // is expected to be discarded after each party call their side
    223         struct oneshot {
    224                 // Internal state :
    225                 //     0p     : is initial state (wait will block)
    226                 //     1p     : fulfilled (wait won't block)
    227                 // any thread : a thread is currently waiting
    228                 struct $thread * volatile ptr;
    229         };
    230 
    231         static inline {
    232                 void  ?{}(oneshot & this) {
    233                         this.ptr = 0p;
    234                 }
    235 
    236                 void ^?{}(oneshot &) {}
    237 
    238                 // Wait for the post, return immidiately if it already happened.
    239                 // return true if the thread was parked
    240                 bool wait(oneshot & this) {
    241                         for() {
    242                                 struct $thread * expected = this.ptr;
    243                                 if(expected == 1p) return false;
    244                                 /* paranoid */ verify( expected == 0p );
    245                                 if(__atomic_compare_exchange_n(&this.ptr, &expected, active_thread(), false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
    246                                         park();
    247                                         /* paranoid */ verify( this.ptr == 1p );
    248                                         return true;
    249                                 }
    250                         }
    251                 }
    252 
    253                 // Mark as fulfilled, wake thread if needed
    254                 // return true if a thread was unparked
    255                 bool post(oneshot & this) {
    256                         struct $thread * got = __atomic_exchange_n( &this.ptr, 1p, __ATOMIC_SEQ_CST);
    257                         if( got == 0p ) return false;
    258                         unpark( got );
    259                         return true;
    260                 }
    261         }
    262 
    263         // base types for future to build upon
    264         // It is based on the 'oneshot' type to allow multiple futures
    265         // to block on the same instance, permitting users to block a single
    266         // thread on "any of" [a given set of] futures.
    267         // does not support multiple threads waiting on the same future
    268         struct future_t {
    269                 // Internal state :
    270                 //     0p      : is initial state (wait will block)
    271                 //     1p      : fulfilled (wait won't block)
    272                 //     2p      : in progress ()
    273                 //     3p      : abandoned, server should delete
    274                 // any oneshot : a context has been setup to wait, a thread could wait on it
    275                 struct oneshot * volatile ptr;
    276         };
    277 
    278         static inline {
    279                 void  ?{}(future_t & this) {
    280                         this.ptr = 0p;
    281                 }
    282 
    283                 void ^?{}(future_t &) {}
    284 
    285                 void reset(future_t & this) {
    286                         // needs to be in 0p or 1p
    287                         __atomic_exchange_n( &this.ptr, 0p, __ATOMIC_SEQ_CST);
    288                 }
    289 
    290                 // check if the future is available
    291                 bool available( future_t & this ) {
    292                         return this.ptr == 1p;
    293                 }
    294 
    295                 // Prepare the future to be waited on
    296                 // intented to be use by wait, wait_any, waitfor, etc. rather than used directly
    297                 bool setup( future_t & this, oneshot & wait_ctx ) {
    298                         /* paranoid */ verify( wait_ctx.ptr == 0p );
    299                         // The future needs to set the wait context
    300                         for() {
    301                                 struct oneshot * expected = this.ptr;
    302                                 // Is the future already fulfilled?
    303                                 if(expected == 1p) return false; // Yes, just return false (didn't block)
    304 
    305                                 // The future is not fulfilled, try to setup the wait context
    306                                 /* paranoid */ verify( expected == 0p );
    307                                 if(__atomic_compare_exchange_n(&this.ptr, &expected, &wait_ctx, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
    308                                         return true;
    309                                 }
    310                         }
    311                 }
    312 
    313                 // Stop waiting on a future
    314                 // When multiple futures are waited for together in "any of" pattern
    315                 // futures that weren't fulfilled before the thread woke up
    316                 // should retract the wait ctx
    317                 // intented to be use by wait, wait_any, waitfor, etc. rather than used directly
    318                 void retract( future_t & this, oneshot & wait_ctx ) {
    319                         // Remove the wait context
    320                         struct oneshot * got = __atomic_exchange_n( &this.ptr, 0p, __ATOMIC_SEQ_CST);
    321 
    322                         // got == 0p: future was never actually setup, just return
    323                         if( got == 0p ) return;
    324 
    325                         // got == wait_ctx: since fulfil does an atomic_swap,
    326                         // if we got back the original then no one else saw context
    327                         // It is safe to delete (which could happen after the return)
    328                         if( got == &wait_ctx ) return;
    329 
    330                         // got == 1p: the future is ready and the context was fully consumed
    331                         // the server won't use the pointer again
    332                         // It is safe to delete (which could happen after the return)
    333                         if( got == 1p ) return;
    334 
    335                         // got == 2p: the future is ready but the context hasn't fully been consumed
    336                         // spin until it is safe to move on
    337                         if( got == 2p ) {
    338                                 while( this.ptr != 1p ) Pause();
    339                                 return;
    340                         }
    341 
    342                         // got == any thing else, something wen't wrong here, abort
    343                         abort("Future in unexpected state");
    344                 }
    345 
    346                 // Mark the future as abandoned, meaning it will be deleted by the server
    347                 bool abandon( future_t & this ) {
    348                         /* paranoid */ verify( this.ptr != 3p );
    349 
    350                         // Mark the future as abandonned
    351                         struct oneshot * got = __atomic_exchange_n( &this.ptr, 3p, __ATOMIC_SEQ_CST);
    352 
    353                         // If the future isn't already fulfilled, let the server delete it
    354                         if( got == 0p ) return false;
    355 
    356                         // got == 2p: the future is ready but the context hasn't fully been consumed
    357                         // spin until it is safe to move on
    358                         if( got == 2p ) {
    359                                 while( this.ptr != 1p ) Pause();
    360                                 got = 1p;
    361                         }
    362 
    363                         // The future is completed delete it now
    364                         /* paranoid */ verify( this.ptr != 1p );
    365                         free( &this );
    366                         return true;
    367                 }
    368 
    369                 // from the server side, mark the future as fulfilled
    370                 // delete it if needed
    371                 bool fulfil( future_t & this ) {
    372                         for() {
    373                                 struct oneshot * expected = this.ptr;
    374                                 // was this abandoned?
    375                                 #if defined(__GNUC__) && __GNUC__ >= 7
    376                                         #pragma GCC diagnostic push
    377                                         #pragma GCC diagnostic ignored "-Wfree-nonheap-object"
    378                                 #endif
    379                                         if( expected == 3p ) { free( &this ); return false; }
    380                                 #if defined(__GNUC__) && __GNUC__ >= 7
    381                                         #pragma GCC diagnostic pop
    382                                 #endif
    383 
    384                                 /* paranoid */ verify( expected != 1p ); // Future is already fulfilled, should not happen
    385                                 /* paranoid */ verify( expected != 2p ); // Future is bein fulfilled by someone else, this is even less supported then the previous case.
    386 
    387                                 // If there is a wait context, we need to consume it and mark it as consumed after
    388                                 // If there is no context then we can skip the in progress phase
    389                                 struct oneshot * want = expected == 0p ? 1p : 2p;
    390                                 if(__atomic_compare_exchange_n(&this.ptr, &expected, want, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
    391                                         if( expected == 0p ) { /* paranoid */ verify( this.ptr == 1p); return false; }
    392                                         bool ret = post( *expected );
    393                                         __atomic_store_n( &this.ptr, 1p, __ATOMIC_SEQ_CST);
    394                                         return ret;
    395                                 }
    396                         }
    397 
    398                 }
    399 
    400                 // Wait for the future to be fulfilled
    401                 bool wait( future_t & this ) {
    402                         oneshot temp;
    403                         if( !setup(this, temp) ) return false;
    404 
    405                         // Wait context is setup, just wait on it
    406                         bool ret = wait( temp );
    407 
    408                         // Wait for the future to tru
    409                         while( this.ptr == 2p ) Pause();
    410                         // Make sure the state makes sense
    411                         // Should be fulfilled, could be in progress but it's out of date if so
    412                         // since if that is the case, the oneshot was fulfilled (unparking this thread)
    413                         // and the oneshot should not be needed any more
    414                         __attribute__((unused)) struct oneshot * was = this.ptr;
    415                         /* paranoid */ verifyf( was == 1p, "Expected this.ptr to be 1p, was %p\n", was );
    416 
    417                         // Mark the future as fulfilled, to be consistent
    418                         // with potential calls to avail
    419                         // this.ptr = 1p;
    420                         return ret;
    421                 }
    422         }
    42394#endif
  • libcfa/src/bits/queue.hfa

    r342af53 r8e4aa05  
    99// instead of being null.
    1010
    11 forall( dtype T | { T *& Next ( T * ); } ) {
     11forall( T & | { T *& Next ( T * ); } ) {
    1212        struct Queue {
    1313                inline Collection;                                                              // Plan 9 inheritance
     
    151151} // distribution
    152152
    153 forall( dtype T | { T *& Next ( T * ); } ) {
     153forall( T & | { T *& Next ( T * ); } ) {
    154154        struct QueueIter {
    155155                inline ColIter;                                                                 // Plan 9 inheritance
  • libcfa/src/bits/sequence.hfa

    r342af53 r8e4aa05  
     1//
     2// Cforall Version 1.0.0 Copyright (C) 2021 University of Waterloo
     3//
     4// The contents of this file are covered under the licence agreement in the
     5// file "LICENCE" distributed with Cforall.
     6//
     7// bits/sequence.hfa -- PUBLIC
     8// Intrusive doubly-linked list
     9//
     10// Author           : Colby Alexander Parsons & Peter A. Buhr
     11// Created On       : Thu Jan 21 19:46:50 2021
     12// Last Modified By :
     13// Last Modified On :
     14// Update Count     :
     15//
     16
    117#pragma once
    218
     
    622struct Seqable {
    723        __cfa_anonymous_object(Colable);
    8         struct Seqable * back;                                                                          // pointer to previous node in the list
     24        // pointer to previous node in the list
     25        struct Seqable * back;
    926};
    1027
     
    1330        // PUBLIC
    1431
    15         void ?{}( Seqable & sq ) with( sq ) {
     32        void ?{}( Seqable & sq ) {
    1633                ((Colable &)sq){};
    17                 back = 0p;
     34                sq.back = 0p;
    1835        } // post: ! listed()
    1936
     
    2744                return sq->back;
    2845        }
    29 
    30         // // wrappers to make Collection have T
    31         // forall( dtype T ) {
    32         //      T *& Back( T * n ) {
    33         //              return (T *)Back( (Seqable *)n );
    34         //      }
    35         // } // distribution
    3646} // distribution
    3747
     
    4353// and the back field of the last node points at the first node (circular).
    4454
    45 forall( dtype T | { T *& Back ( T * ); T *& Next ( T * ); } ) {
     55forall( T & ) {
    4656        struct Sequence {
    47                 inline Collection;                                                              // Plan 9 inheritance
     57                // Plan 9 inheritance
     58                inline Collection;
    4859        };
    4960
    5061        static inline {
     62                void ?{}( Sequence(T) &, const Sequence(T) & ) = void; // no copy
     63                Sequence(T) & ?=?( const Sequence(T) & ) = void; // no assignment
     64
     65                void ?{}( Sequence(T) & s ) with( s ) {
     66                        ((Collection &)s){};
     67                }       // post: isEmpty()
     68        }
     69
     70        static inline forall(| { T *& Back ( T * ); T *& Next ( T * ); }) {
    5171                // wrappers to make Collection have T
    5272                T & head( Sequence(T) & s ) with( s ) {
    5373                        return *(T *)head( (Collection &)s );
    5474                } // post: empty() & head() == 0 | !empty() & head() in *s
    55 
    56                 void ?{}( Sequence(T) &, const Sequence(T) & ) = void; // no copy
    57                 Sequence(T) & ?=?( const Sequence(T) & ) = void; // no assignment
    58 
    59                 void ?{}( Sequence(T) & s ) with( s ) {
    60                         ((Collection &)s){};
    61                 }       // post: isEmpty()
    6275
    6376                // Return a pointer to the last sequence element, without removing it.
     
    145158                        return n;
    146159                } // post: n->listed() & *n in *s & succ(n) == bef
    147                
     160
    148161                // pre: n->listed() & *n in *s
    149162                T & remove( Sequence(T) & s, T & n ) with( s ) { // O(1)
     
    231244} // distribution
    232245
    233 forall( dtype T | { T *& Back ( T * ); T *& Next ( T * ); } ) {
     246forall( T & | { T *& Back ( T * ); T *& Next ( T * ); } ) {
    234247        // SeqIter(T) is used to iterate over a Sequence(T) in head-to-tail order.
    235248        struct SeqIter {
     
    285298
    286299        static inline {
    287                 void ?{}( SeqIterRev(T) & si ) with( si ) {     
     300                void ?{}( SeqIterRev(T) & si ) with( si ) {
    288301                        ((ColIter &)si){};
    289302                        seq = 0p;
     
    291304
    292305                // Create a iterator active in sequence s.
    293                 void ?{}( SeqIterRev(T) & si, Sequence(T) & s ) with( si ) {   
     306                void ?{}( SeqIterRev(T) & si, Sequence(T) & s ) with( si ) {
    294307                        ((ColIter &)si){};
    295308                        seq = &s;
     
    297310                } // post: elts = null
    298311
    299                 void ?{}( SeqIterRev(T) & si, Sequence(T) & s, T & start ) with( si ) { 
     312                void ?{}( SeqIterRev(T) & si, Sequence(T) & s, T & start ) with( si ) {
    300313                        ((ColIter &)si){};
    301314                        seq = &s;
  • libcfa/src/bits/stack.hfa

    r342af53 r8e4aa05  
    99// instead of being null.
    1010
    11 forall( dtype T | { T *& Next ( T * ); } ) {
     11forall( T & | { T *& Next ( T * ); } ) {
    1212        struct Stack {
    1313                inline Collection;                                                              // Plan 9 inheritance
     
    6767// order returned by drop().
    6868
    69 forall( dtype T | { T *& Next ( T * ); } ) {
     69forall( T & | { T *& Next ( T * ); } ) {
    7070        struct StackIter {
    7171                inline ColIter;                                                                 // Plan 9 inheritance
  • libcfa/src/common.cfa

    r342af53 r8e4aa05  
    2323[ long int, long int ] div( long int num, long int denom ) { ldiv_t qr = ldiv( num, denom ); return [ qr.quot, qr.rem ]; }
    2424[ long long int, long long int ] div( long long int num, long long int denom ) { lldiv_t qr = lldiv( num, denom ); return [ qr.quot, qr.rem ]; }
    25 forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
     25forall( T | { T ?/?( T, T ); T ?%?( T, T ); } )
    2626[ T, T ] div( T num, T denom ) { return [ num / denom, num % denom ]; }
    2727
  • libcfa/src/common.hfa

    r342af53 r8e4aa05  
    2121[ long int, long int ] div( long int num, long int denom );
    2222[ long long int, long long int ] div( long long int num, long long int denom );
    23 forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
     23forall( T | { T ?/?( T, T ); T ?%?( T, T ); } )
    2424[ T, T ] div( T num, T demon );
    2525
     
    6161} // distribution
    6262
    63 forall( otype T | { void ?{}( T &, zero_t ); int ?<?( T, T ); T -?( T ); } )
     63forall( T | { void ?{}( T &, zero_t ); int ?<?( T, T ); T -?( T ); } )
    6464T abs( T );
    6565
     
    7070        intptr_t min( intptr_t t1, intptr_t t2 ) { return t1 < t2 ? t1 : t2; } // optimization
    7171        uintptr_t min( uintptr_t t1, uintptr_t t2 ) { return t1 < t2 ? t1 : t2; } // optimization
    72         forall( otype T | { int ?<?( T, T ); } )
     72        forall( T | { int ?<?( T, T ); } )
    7373        T min( T t1, T t2 ) { return t1 < t2 ? t1 : t2; }
    7474
     
    7676        intptr_t max( intptr_t t1, intptr_t t2 ) { return t1 > t2 ? t1 : t2; } // optimization
    7777        uintptr_t max( uintptr_t t1, uintptr_t t2 ) { return t1 > t2 ? t1 : t2; } // optimization
    78         forall( otype T | { int ?>?( T, T ); } )
     78        forall( T | { int ?>?( T, T ); } )
    7979        T max( T t1, T t2 ) { return t1 > t2 ? t1 : t2; }
    8080
    81         forall( otype T | { T min( T, T ); T max( T, T ); } )
     81        forall( T | { T min( T, T ); T max( T, T ); } )
    8282        T clamp( T value, T min_val, T max_val ) { return max( min_val, min( value, max_val ) ); }
    8383
    84         forall( otype T )
     84        forall( T )
    8585        void swap( T & v1, T & v2 ) { T temp = v1; v1 = v2; v2 = temp; }
    8686} // distribution
  • libcfa/src/concurrency/coroutine.cfa

    r342af53 r8e4aa05  
    4646
    4747//-----------------------------------------------------------------------------
    48 FORALL_DATA_INSTANCE(CoroutineCancelled, (dtype coroutine_t), (coroutine_t))
    49 
    50 forall(dtype T)
     48FORALL_DATA_INSTANCE(CoroutineCancelled, (coroutine_t &), (coroutine_t))
     49
     50forall(T &)
    5151void mark_exception(CoroutineCancelled(T) *) {}
    5252
    53 forall(dtype T)
     53forall(T &)
    5454void copy(CoroutineCancelled(T) * dst, CoroutineCancelled(T) * src) {
    5555        dst->virtual_table = src->virtual_table;
     
    5858}
    5959
    60 forall(dtype T)
     60forall(T &)
    6161const char * msg(CoroutineCancelled(T) *) {
    6262        return "CoroutineCancelled(...)";
     
    6464
    6565// This code should not be inlined. It is the error path on resume.
    66 forall(dtype T | is_coroutine(T))
     66forall(T & | is_coroutine(T))
    6767void __cfaehm_cancelled_coroutine( T & cor, $coroutine * desc ) {
    6868        verify( desc->cancellation );
     
    148148// Part of the Public API
    149149// Not inline since only ever called once per coroutine
    150 forall(dtype T | is_coroutine(T))
     150forall(T & | is_coroutine(T))
    151151void prime(T& cor) {
    152152        $coroutine* this = get_coroutine(cor);
     
    196196
    197197void __stack_clean  ( __stack_info_t * this ) {
    198         size_t size = ((intptr_t)this->storage->base) - ((intptr_t)this->storage->limit) + sizeof(__stack_t);
    199198        void * storage = this->storage->limit;
    200199
    201200        #if CFA_COROUTINE_USE_MMAP
     201                size_t size = ((intptr_t)this->storage->base) - ((intptr_t)this->storage->limit) + sizeof(__stack_t);
    202202                storage = (void *)(((intptr_t)storage) - __page_size);
    203203                if(munmap(storage, size + __page_size) == -1) {
  • libcfa/src/concurrency/coroutine.hfa

    r342af53 r8e4aa05  
    2222//-----------------------------------------------------------------------------
    2323// Exception thrown from resume when a coroutine stack is cancelled.
    24 FORALL_DATA_EXCEPTION(CoroutineCancelled, (dtype coroutine_t), (coroutine_t)) (
     24FORALL_DATA_EXCEPTION(CoroutineCancelled, (coroutine_t &), (coroutine_t)) (
    2525        coroutine_t * the_coroutine;
    2626        exception_t * the_exception;
    2727);
    2828
    29 forall(dtype T)
     29forall(T &)
    3030void copy(CoroutineCancelled(T) * dst, CoroutineCancelled(T) * src);
    3131
    32 forall(dtype T)
     32forall(T &)
    3333const char * msg(CoroutineCancelled(T) *);
    3434
     
    3737// Anything that implements this trait can be resumed.
    3838// Anything that is resumed is a coroutine.
    39 trait is_coroutine(dtype T | IS_RESUMPTION_EXCEPTION(CoroutineCancelled, (T))) {
     39trait is_coroutine(T & | IS_RESUMPTION_EXCEPTION(CoroutineCancelled, (T))) {
    4040        void main(T & this);
    4141        $coroutine * get_coroutine(T & this);
     
    6060//-----------------------------------------------------------------------------
    6161// Public coroutine API
    62 forall(dtype T | is_coroutine(T))
     62forall(T & | is_coroutine(T))
    6363void prime(T & cor);
    6464
     
    7272        void __cfactx_invoke_coroutine(void (*main)(void *), void * this);
    7373
    74         forall(dtype T)
     74        forall(T &)
    7575        void __cfactx_start(void (*main)(T &), struct $coroutine * cor, T & this, void (*invoke)(void (*main)(void *), void *));
    7676
     
    129129}
    130130
    131 forall(dtype T | is_coroutine(T))
     131forall(T & | is_coroutine(T))
    132132void __cfaehm_cancelled_coroutine( T & cor, $coroutine * desc );
    133133
    134134// Resume implementation inlined for performance
    135 forall(dtype T | is_coroutine(T))
     135forall(T & | is_coroutine(T))
    136136static inline T & resume(T & cor) {
    137137        // optimization : read TLS once and reuse it
  • libcfa/src/concurrency/future.hfa

    r342af53 r8e4aa05  
    1919#include "monitor.hfa"
    2020
    21 forall( otype T ) {
     21forall( T ) {
    2222        struct future {
    2323                inline future_t;
     
    5858}
    5959
    60 forall( otype T ) {
     60forall( T ) {
    6161        monitor multi_future {
    6262                inline future_t;
  • libcfa/src/concurrency/io.cfa

    r342af53 r8e4aa05  
    3232        extern "C" {
    3333                #include <sys/syscall.h>
     34                #include <sys/eventfd.h>
    3435
    3536                #include <linux/io_uring.h>
     
    4142        #include "io/types.hfa"
    4243
    43         static const char * opcodes[] = {
     44        __attribute__((unused)) static const char * opcodes[] = {
    4445                "OP_NOP",
    4546                "OP_READV",
     
    7980        };
    8081
    81         // returns true of acquired as leader or second leader
    82         static inline bool try_lock( __leaderlock_t & this ) {
    83                 const uintptr_t thrd = 1z | (uintptr_t)active_thread();
    84                 bool block;
    85                 disable_interrupts();
    86                 for() {
    87                         struct $thread * expected = this.value;
    88                         if( 1p != expected && 0p != expected ) {
    89                                 /* paranoid */ verify( thrd != (uintptr_t)expected ); // We better not already be the next leader
    90                                 enable_interrupts( __cfaabi_dbg_ctx );
    91                                 return false;
    92                         }
    93                         struct $thread * desired;
    94                         if( 0p == expected ) {
    95                                 // If the lock isn't locked acquire it, no need to block
    96                                 desired = 1p;
    97                                 block = false;
    98                         }
    99                         else {
    100                                 // If the lock is already locked try becomming the next leader
    101                                 desired = (struct $thread *)thrd;
    102                                 block = true;
    103                         }
    104                         if( __atomic_compare_exchange_n(&this.value, &expected, desired, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ) break;
    105                 }
    106                 if( block ) {
    107                         enable_interrupts( __cfaabi_dbg_ctx );
    108                         park();
    109                         disable_interrupts();
    110                 }
    111                 return true;
    112         }
    113 
    114         static inline bool next( __leaderlock_t & this ) {
     82        static $io_context * __ioarbiter_allocate( $io_arbiter & mutex this, processor *, __u32 idxs[], __u32 want );
     83        static void __ioarbiter_submit( $io_arbiter & mutex this, $io_context * , __u32 idxs[], __u32 have, bool lazy );
     84        static void __ioarbiter_flush ( $io_arbiter & mutex this, $io_context * );
     85        static inline void __ioarbiter_notify( $io_context & ctx );
     86//=============================================================================================
     87// I/O Polling
     88//=============================================================================================
     89        static inline unsigned __flush( struct $io_context & );
     90        static inline __u32 __release_sqes( struct $io_context & );
     91
     92        void __cfa_io_drain( processor * proc ) {
    11593                /* paranoid */ verify( ! __preemption_enabled() );
    116                 struct $thread * nextt;
    117                 for() {
    118                         struct $thread * expected = this.value;
    119                         /* paranoid */ verify( (1 & (uintptr_t)expected) == 1 ); // The lock better be locked
    120 
    121                         struct $thread * desired;
    122                         if( 1p == expected ) {
    123                                 // No next leader, just unlock
    124                                 desired = 0p;
    125                                 nextt   = 0p;
    126                         }
    127                         else {
    128                                 // There is a next leader, remove but keep locked
    129                                 desired = 1p;
    130                                 nextt   = (struct $thread *)(~1z & (uintptr_t)expected);
    131                         }
    132                         if( __atomic_compare_exchange_n(&this.value, &expected, desired, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ) break;
    133                 }
    134 
    135                 if(nextt) {
    136                         unpark( nextt );
    137                         enable_interrupts( __cfaabi_dbg_ctx );
    138                         return true;
    139                 }
    140                 enable_interrupts( __cfaabi_dbg_ctx );
    141                 return false;
    142         }
    143 
    144 //=============================================================================================
    145 // I/O Syscall
    146 //=============================================================================================
    147         static int __io_uring_enter( struct __io_data & ring, unsigned to_submit, bool get ) {
    148                 bool need_sys_to_submit = false;
    149                 bool need_sys_to_complete = false;
    150                 unsigned flags = 0;
    151 
    152                 TO_SUBMIT:
    153                 if( to_submit > 0 ) {
    154                         if( !(ring.ring_flags & IORING_SETUP_SQPOLL) ) {
    155                                 need_sys_to_submit = true;
    156                                 break TO_SUBMIT;
    157                         }
    158                         if( (*ring.submit_q.flags) & IORING_SQ_NEED_WAKEUP ) {
    159                                 need_sys_to_submit = true;
    160                                 flags |= IORING_ENTER_SQ_WAKEUP;
    161                         }
    162                 }
    163 
    164                 if( get && !(ring.ring_flags & IORING_SETUP_SQPOLL) ) {
    165                         flags |= IORING_ENTER_GETEVENTS;
    166                         if( (ring.ring_flags & IORING_SETUP_IOPOLL) ) {
    167                                 need_sys_to_complete = true;
    168                         }
    169                 }
    170 
    171                 int ret = 0;
    172                 if( need_sys_to_submit || need_sys_to_complete ) {
    173                         __cfadbg_print_safe(io_core, "Kernel I/O : IO_URING enter %d %u %u\n", ring.fd, to_submit, flags);
    174                         ret = syscall( __NR_io_uring_enter, ring.fd, to_submit, 0, flags, (sigset_t *)0p, _NSIG / 8);
    175                         if( ret < 0 ) {
    176                                 switch((int)errno) {
    177                                 case EAGAIN:
    178                                 case EINTR:
    179                                         ret = -1;
    180                                         break;
    181                                 default:
    182                                         abort( "KERNEL ERROR: IO_URING SYSCALL - (%d) %s\n", (int)errno, strerror(errno) );
    183                                 }
    184                         }
    185                 }
    186 
    187                 // Memory barrier
    188                 __atomic_thread_fence( __ATOMIC_SEQ_CST );
    189                 return ret;
    190         }
    191 
    192 //=============================================================================================
    193 // I/O Polling
    194 //=============================================================================================
    195         static unsigned __collect_submitions( struct __io_data & ring );
    196         static __u32 __release_consumed_submission( struct __io_data & ring );
    197         static inline void __clean( volatile struct io_uring_sqe * sqe );
    198 
    199         // Process a single completion message from the io_uring
    200         // This is NOT thread-safe
    201         static inline void process( volatile struct io_uring_cqe & cqe ) {
    202                 struct io_future_t * future = (struct io_future_t *)(uintptr_t)cqe.user_data;
    203                 __cfadbg_print_safe( io, "Kernel I/O : Syscall completed : cqe %p, result %d for %p\n", &cqe, cqe.res, future );
    204 
    205                 fulfil( *future, cqe.res );
    206         }
    207 
    208         static [int, bool] __drain_io( & struct __io_data ring ) {
    209                 /* paranoid */ verify( ! __preemption_enabled() );
    210 
    211                 unsigned to_submit = 0;
    212                 if( ring.poller_submits ) {
    213                         // If the poller thread also submits, then we need to aggregate the submissions which are ready
    214                         to_submit = __collect_submitions( ring );
    215                 }
    216 
    217                 int ret = __io_uring_enter(ring, to_submit, true);
    218                 if( ret < 0 ) {
    219                         return [0, true];
    220                 }
    221 
    222                 // update statistics
    223                 if (to_submit > 0) {
    224                         __STATS__( true,
    225                                 if( to_submit > 0 ) {
    226                                         io.submit_q.submit_avg.rdy += to_submit;
    227                                         io.submit_q.submit_avg.csm += ret;
    228                                         io.submit_q.submit_avg.cnt += 1;
    229                                 }
    230                         )
    231                 }
    232 
    233                 __atomic_thread_fence( __ATOMIC_SEQ_CST );
    234 
    235                 // Release the consumed SQEs
    236                 __release_consumed_submission( ring );
     94                /* paranoid */ verify( proc );
     95                /* paranoid */ verify( proc->io.ctx );
    23796
    23897                // Drain the queue
    239                 unsigned head = *ring.completion_q.head;
    240                 unsigned tail = *ring.completion_q.tail;
    241                 const __u32 mask = *ring.completion_q.mask;
    242 
    243                 // Nothing was new return 0
    244                 if (head == tail) {
    245                         return [0, to_submit > 0];
    246                 }
     98                $io_context * ctx = proc->io.ctx;
     99                unsigned head = *ctx->cq.head;
     100                unsigned tail = *ctx->cq.tail;
     101                const __u32 mask = *ctx->cq.mask;
    247102
    248103                __u32 count = tail - head;
    249                 /* paranoid */ verify( count != 0 );
     104                __STATS__( false, io.calls.drain++; io.calls.completed += count; )
     105
    250106                for(i; count) {
    251107                        unsigned idx = (head + i) & mask;
    252                         volatile struct io_uring_cqe & cqe = ring.completion_q.cqes[idx];
     108                        volatile struct io_uring_cqe & cqe = ctx->cq.cqes[idx];
    253109
    254110                        /* paranoid */ verify(&cqe);
    255111
    256                         process( cqe );
    257                 }
     112                        struct io_future_t * future = (struct io_future_t *)(uintptr_t)cqe.user_data;
     113                        __cfadbg_print_safe( io, "Kernel I/O : Syscall completed : cqe %p, result %d for %p\n", &cqe, cqe.res, future );
     114
     115                        fulfil( *future, cqe.res );
     116                }
     117
     118                __cfadbg_print_safe(io, "Kernel I/O : %u completed\n", count);
    258119
    259120                // Mark to the kernel that the cqe has been seen
    260121                // Ensure that the kernel only sees the new value of the head index after the CQEs have been read.
    261                 __atomic_fetch_add( ring.completion_q.head, count, __ATOMIC_SEQ_CST );
    262 
    263                 return [count, count > 0 || to_submit > 0];
    264         }
    265 
    266         void main( $io_ctx_thread & this ) {
    267                 __ioctx_register( this );
    268 
    269                 __cfadbg_print_safe(io_core, "Kernel I/O : IO poller %d (%p) ready\n", this.ring->fd, &this);
    270 
    271                 const int reset_cnt = 5;
    272                 int reset = reset_cnt;
    273                 // Then loop until we need to start
    274                 LOOP:
    275                 while(!__atomic_load_n(&this.done, __ATOMIC_SEQ_CST)) {
    276                         // Drain the io
    277                         int count;
    278                         bool again;
    279                         disable_interrupts();
    280                                 [count, again] = __drain_io( *this.ring );
    281 
    282                                 if(!again) reset--;
    283 
     122                __atomic_store_n( ctx->cq.head, head + count, __ATOMIC_SEQ_CST );
     123
     124                /* paranoid */ verify( ! __preemption_enabled() );
     125
     126                return;
     127        }
     128
     129        void __cfa_io_flush( processor * proc ) {
     130                /* paranoid */ verify( ! __preemption_enabled() );
     131                /* paranoid */ verify( proc );
     132                /* paranoid */ verify( proc->io.ctx );
     133
     134                $io_context & ctx = *proc->io.ctx;
     135
     136                if(!ctx.ext_sq.empty) {
     137                        __ioarbiter_flush( *ctx.arbiter, &ctx );
     138                }
     139
     140                __STATS__( true, io.calls.flush++; )
     141                int ret = syscall( __NR_io_uring_enter, ctx.fd, ctx.sq.to_submit, 0, 0, (sigset_t *)0p, _NSIG / 8);
     142                if( ret < 0 ) {
     143                        switch((int)errno) {
     144                        case EAGAIN:
     145                        case EINTR:
     146                        case EBUSY:
    284147                                // Update statistics
    285                                 __STATS__( true,
    286                                         io.complete_q.completed_avg.val += count;
    287                                         io.complete_q.completed_avg.cnt += 1;
    288                                 )
    289                         enable_interrupts( __cfaabi_dbg_ctx );
    290 
    291                         // If we got something, just yield and check again
    292                         if(reset > 1) {
    293                                 yield();
    294                                 continue LOOP;
     148                                __STATS__( false, io.calls.errors.busy ++; )
     149                                return;
     150                        default:
     151                                abort( "KERNEL ERROR: IO_URING SYSCALL - (%d) %s\n", (int)errno, strerror(errno) );
    295152                        }
    296 
    297                         // We alread failed to find completed entries a few time.
    298                         if(reset == 1) {
    299                                 // Rearm the context so it can block
    300                                 // but don't block right away
    301                                 // we need to retry one last time in case
    302                                 // something completed *just now*
    303                                 __ioctx_prepare_block( this );
    304                                 continue LOOP;
    305                         }
    306 
    307                                 __STATS__( false,
    308                                         io.complete_q.blocks += 1;
    309                                 )
    310                                 __cfadbg_print_safe(io_core, "Kernel I/O : Parking io poller %d (%p)\n", this.ring->fd, &this);
    311 
    312                                 // block this thread
    313                                 wait( this.sem );
    314 
    315                         // restore counter
    316                         reset = reset_cnt;
    317                 }
    318 
    319                 __cfadbg_print_safe(io_core, "Kernel I/O : Fast poller %d (%p) stopping\n", this.ring->fd, &this);
     153                }
     154
     155                __cfadbg_print_safe(io, "Kernel I/O : %u submitted to io_uring %d\n", ret, ctx.fd);
     156                __STATS__( true, io.calls.submitted += ret; )
     157                /* paranoid */ verify( ctx.sq.to_submit <= *ctx.sq.num );
     158                /* paranoid */ verify( ctx.sq.to_submit >= ret );
     159
     160                ctx.sq.to_submit -= ret;
     161
     162                /* paranoid */ verify( ctx.sq.to_submit <= *ctx.sq.num );
     163
     164                // Release the consumed SQEs
     165                __release_sqes( ctx );
     166
     167                /* paranoid */ verify( ! __preemption_enabled() );
     168
     169                ctx.proc->io.pending = false;
    320170        }
    321171
     
    339189//         head and tail must be fully filled and shouldn't ever be touched again.
    340190//
     191        //=============================================================================================
     192        // Allocation
     193        // for user's convenience fill the sqes from the indexes
     194        static inline void __fill(struct io_uring_sqe * out_sqes[], __u32 want, __u32 idxs[], struct $io_context * ctx)  {
     195                struct io_uring_sqe * sqes = ctx->sq.sqes;
     196                for(i; want) {
     197                        __cfadbg_print_safe(io, "Kernel I/O : filling loop\n");
     198                        out_sqes[i] = &sqes[idxs[i]];
     199                }
     200        }
     201
     202        // Try to directly allocate from the a given context
     203        // Not thread-safe
     204        static inline bool __alloc(struct $io_context * ctx, __u32 idxs[], __u32 want) {
     205                __sub_ring_t & sq = ctx->sq;
     206                const __u32 mask  = *sq.mask;
     207                __u32 fhead = sq.free_ring.head;    // get the current head of the queue
     208                __u32 ftail = sq.free_ring.tail;    // get the current tail of the queue
     209
     210                // If we don't have enough sqes, fail
     211                if((ftail - fhead) < want) { return false; }
     212
     213                // copy all the indexes we want from the available list
     214                for(i; want) {
     215                        __cfadbg_print_safe(io, "Kernel I/O : allocating loop\n");
     216                        idxs[i] = sq.free_ring.array[(fhead + i) & mask];
     217                }
     218
     219                // Advance the head to mark the indexes as consumed
     220                __atomic_store_n(&sq.free_ring.head, fhead + want, __ATOMIC_RELEASE);
     221
     222                // return success
     223                return true;
     224        }
    341225
    342226        // Allocate an submit queue entry.
     
    345229        // for convenience, return both the index and the pointer to the sqe
    346230        // sqe == &sqes[idx]
    347         [* volatile struct io_uring_sqe, __u32] __submit_alloc( struct __io_data & ring, __u64 data ) {
    348                 /* paranoid */ verify( data != 0 );
    349 
    350                 // Prepare the data we need
    351                 __attribute((unused)) int len   = 0;
    352                 __attribute((unused)) int block = 0;
    353                 __u32 cnt = *ring.submit_q.num;
    354                 __u32 mask = *ring.submit_q.mask;
    355 
    356                 __u32 off = thread_rand();
    357 
    358                 // Loop around looking for an available spot
    359                 for() {
    360                         // Look through the list starting at some offset
    361                         for(i; cnt) {
    362                                 __u64 expected = 3;
    363                                 __u32 idx = (i + off) & mask; // Get an index from a random
    364                                 volatile struct io_uring_sqe * sqe = &ring.submit_q.sqes[idx];
    365                                 volatile __u64 * udata = &sqe->user_data;
    366 
    367                                 // Allocate the entry by CASing the user_data field from 0 to the future address
    368                                 if( *udata == expected &&
    369                                         __atomic_compare_exchange_n( udata, &expected, data, true, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED ) )
    370                                 {
    371                                         // update statistics
    372                                         __STATS__( false,
    373                                                 io.submit_q.alloc_avg.val   += len;
    374                                                 io.submit_q.alloc_avg.block += block;
    375                                                 io.submit_q.alloc_avg.cnt   += 1;
    376                                         )
    377 
    378                                         // debug log
    379                                         __cfadbg_print_safe( io, "Kernel I/O : allocated [%p, %u] for %p (%p)\n", sqe, idx, active_thread(), (void*)data );
    380 
    381                                         // Success return the data
    382                                         return [sqe, idx];
    383                                 }
    384                                 verify(expected != data);
    385 
    386                                 // This one was used
    387                                 len ++;
    388                         }
    389 
    390                         block++;
    391 
    392                         abort( "Kernel I/O : all submit queue entries used, yielding\n" );
    393 
    394                         yield();
    395                 }
    396         }
    397 
    398         static inline __u32 __submit_to_ready_array( struct __io_data & ring, __u32 idx, const __u32 mask ) {
    399                 /* paranoid */ verify( idx <= mask   );
    400                 /* paranoid */ verify( idx != -1ul32 );
    401 
    402                 // We need to find a spot in the ready array
    403                 __attribute((unused)) int len   = 0;
    404                 __attribute((unused)) int block = 0;
    405                 __u32 ready_mask = ring.submit_q.ready_cnt - 1;
    406 
    407                 __u32 off = thread_rand();
    408 
    409                 __u32 picked;
    410                 LOOKING: for() {
    411                         for(i; ring.submit_q.ready_cnt) {
    412                                 picked = (i + off) & ready_mask;
    413                                 __u32 expected = -1ul32;
    414                                 if( __atomic_compare_exchange_n( &ring.submit_q.ready[picked], &expected, idx, true, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED ) ) {
    415                                         break LOOKING;
    416                                 }
    417                                 verify(expected != idx);
    418 
    419                                 len ++;
    420                         }
    421 
    422                         block++;
    423 
    424                         __u32 released = __release_consumed_submission( ring );
    425                         if( released == 0 ) {
    426                                 yield();
    427                         }
    428                 }
    429 
    430                 // update statistics
    431                 __STATS__( false,
    432                         io.submit_q.look_avg.val   += len;
    433                         io.submit_q.look_avg.block += block;
    434                         io.submit_q.look_avg.cnt   += 1;
    435                 )
    436 
    437                 return picked;
    438         }
    439 
    440         void __submit( struct io_context * ctx, __u32 idx ) __attribute__((nonnull (1))) {
    441                 __io_data & ring = *ctx->thrd.ring;
    442 
     231        struct $io_context * cfa_io_allocate(struct io_uring_sqe * sqes[], __u32 idxs[], __u32 want) {
     232                __cfadbg_print_safe(io, "Kernel I/O : attempting to allocate %u\n", want);
     233
     234                disable_interrupts();
     235                processor * proc = __cfaabi_tls.this_processor;
     236                $io_context * ctx = proc->io.ctx;
     237                /* paranoid */ verify( __cfaabi_tls.this_processor );
     238                /* paranoid */ verify( ctx );
     239
     240                __cfadbg_print_safe(io, "Kernel I/O : attempting to fast allocation\n");
     241
     242                // We can proceed to the fast path
     243                if( __alloc(ctx, idxs, want) ) {
     244                        // Allocation was successful
     245                        __STATS__( true, io.alloc.fast += 1; )
     246                        enable_interrupts( __cfaabi_dbg_ctx );
     247
     248                        __cfadbg_print_safe(io, "Kernel I/O : fast allocation successful from ring %d\n", ctx->fd);
     249
     250                        __fill( sqes, want, idxs, ctx );
     251                        return ctx;
     252                }
     253                // The fast path failed, fallback
     254                __STATS__( true, io.alloc.fail += 1; )
     255
     256                // Fast path failed, fallback on arbitration
     257                __STATS__( true, io.alloc.slow += 1; )
     258                enable_interrupts( __cfaabi_dbg_ctx );
     259
     260                $io_arbiter * ioarb = proc->cltr->io.arbiter;
     261                /* paranoid */ verify( ioarb );
     262
     263                __cfadbg_print_safe(io, "Kernel I/O : falling back on arbiter for allocation\n");
     264
     265                struct $io_context * ret = __ioarbiter_allocate(*ioarb, proc, idxs, want);
     266
     267                __cfadbg_print_safe(io, "Kernel I/O : slow allocation completed from ring %d\n", ret->fd);
     268
     269                __fill( sqes, want, idxs,ret );
     270                return ret;
     271        }
     272
     273
     274        //=============================================================================================
     275        // submission
     276        static inline void __submit( struct $io_context * ctx, __u32 idxs[], __u32 have, bool lazy) {
     277                // We can proceed to the fast path
     278                // Get the right objects
     279                __sub_ring_t & sq = ctx->sq;
     280                const __u32 mask  = *sq.mask;
     281                __u32 tail = *sq.kring.tail;
     282
     283                // Add the sqes to the array
     284                for( i; have ) {
     285                        __cfadbg_print_safe(io, "Kernel I/O : __submit loop\n");
     286                        sq.kring.array[ (tail + i) & mask ] = idxs[i];
     287                }
     288
     289                // Make the sqes visible to the submitter
     290                __atomic_store_n(sq.kring.tail, tail + have, __ATOMIC_RELEASE);
     291                sq.to_submit++;
     292
     293                ctx->proc->io.pending = true;
     294                ctx->proc->io.dirty   = true;
     295                if(sq.to_submit > 30 || !lazy) {
     296                        __cfa_io_flush( ctx->proc );
     297                }
     298        }
     299
     300        void cfa_io_submit( struct $io_context * inctx, __u32 idxs[], __u32 have, bool lazy ) __attribute__((nonnull (1))) {
     301                __cfadbg_print_safe(io, "Kernel I/O : attempting to submit %u (%s)\n", have, lazy ? "lazy" : "eager");
     302
     303                disable_interrupts();
     304                processor * proc = __cfaabi_tls.this_processor;
     305                $io_context * ctx = proc->io.ctx;
     306                /* paranoid */ verify( __cfaabi_tls.this_processor );
     307                /* paranoid */ verify( ctx );
     308
     309                // Can we proceed to the fast path
     310                if( ctx == inctx )              // We have the right instance?
    443311                {
    444                         __attribute__((unused)) volatile struct io_uring_sqe * sqe = &ring.submit_q.sqes[idx];
    445                         __cfadbg_print_safe( io,
    446                                 "Kernel I/O : submitting %u (%p) for %p\n"
    447                                 "    data: %p\n"
    448                                 "    opcode: %s\n"
    449                                 "    fd: %d\n"
    450                                 "    flags: %d\n"
    451                                 "    prio: %d\n"
    452                                 "    off: %p\n"
    453                                 "    addr: %p\n"
    454                                 "    len: %d\n"
    455                                 "    other flags: %d\n"
    456                                 "    splice fd: %d\n"
    457                                 "    pad[0]: %llu\n"
    458                                 "    pad[1]: %llu\n"
    459                                 "    pad[2]: %llu\n",
    460                                 idx, sqe,
    461                                 active_thread(),
    462                                 (void*)sqe->user_data,
    463                                 opcodes[sqe->opcode],
    464                                 sqe->fd,
    465                                 sqe->flags,
    466                                 sqe->ioprio,
    467                                 sqe->off,
    468                                 sqe->addr,
    469                                 sqe->len,
    470                                 sqe->accept_flags,
    471                                 sqe->splice_fd_in,
    472                                 sqe->__pad2[0],
    473                                 sqe->__pad2[1],
    474                                 sqe->__pad2[2]
    475                         );
    476                 }
    477 
    478 
    479                 // Get now the data we definetely need
    480                 volatile __u32 * const tail = ring.submit_q.tail;
    481                 const __u32 mask  = *ring.submit_q.mask;
    482 
    483                 // There are 2 submission schemes, check which one we are using
    484                 if( ring.poller_submits ) {
    485                         // If the poller thread submits, then we just need to add this to the ready array
    486                         __submit_to_ready_array( ring, idx, mask );
    487 
    488                         post( ctx->thrd.sem );
    489 
    490                         __cfadbg_print_safe( io, "Kernel I/O : Added %u to ready for %p\n", idx, active_thread() );
    491                 }
    492                 else if( ring.eager_submits ) {
    493                         __u32 picked = __submit_to_ready_array( ring, idx, mask );
    494 
    495                         #if defined(LEADER_LOCK)
    496                                 if( !try_lock(ring.submit_q.submit_lock) ) {
    497                                         __STATS__( false,
    498                                                 io.submit_q.helped += 1;
    499                                         )
    500                                         return;
    501                                 }
    502                                 /* paranoid */ verify( ! __preemption_enabled() );
    503                                 __STATS__( true,
    504                                         io.submit_q.leader += 1;
    505                                 )
    506                         #else
    507                                 for() {
    508                                         yield();
    509 
    510                                         if( try_lock(ring.submit_q.submit_lock __cfaabi_dbg_ctx2) ) {
    511                                                 __STATS__( false,
    512                                                         io.submit_q.leader += 1;
    513                                                 )
    514                                                 break;
    515                                         }
    516 
    517                                         // If some one else collected our index, we are done
    518                                         #warning ABA problem
    519                                         if( ring.submit_q.ready[picked] != idx ) {
    520                                                 __STATS__( false,
    521                                                         io.submit_q.helped += 1;
    522                                                 )
    523                                                 return;
    524                                         }
    525 
    526                                         __STATS__( false,
    527                                                 io.submit_q.busy += 1;
    528                                         )
    529                                 }
    530                         #endif
    531 
    532                         // We got the lock
    533                         // Collect the submissions
    534                         unsigned to_submit = __collect_submitions( ring );
    535 
    536                         // Actually submit
    537                         int ret = __io_uring_enter( ring, to_submit, false );
    538 
    539                         #if defined(LEADER_LOCK)
    540                                 /* paranoid */ verify( ! __preemption_enabled() );
    541                                 next(ring.submit_q.submit_lock);
    542                         #else
    543                                 unlock(ring.submit_q.submit_lock);
    544                         #endif
    545                         if( ret < 0 ) {
    546                                 return;
    547                         }
    548 
    549                         // Release the consumed SQEs
    550                         __release_consumed_submission( ring );
    551 
    552                         // update statistics
    553                         __STATS__( false,
    554                                 io.submit_q.submit_avg.rdy += to_submit;
    555                                 io.submit_q.submit_avg.csm += ret;
    556                                 io.submit_q.submit_avg.cnt += 1;
    557                         )
    558 
    559                         __cfadbg_print_safe( io, "Kernel I/O : submitted %u (among %u) for %p\n", idx, ret, active_thread() );
    560                 }
    561                 else
    562                 {
    563                         // get mutual exclusion
    564                         #if defined(LEADER_LOCK)
    565                                 while(!try_lock(ring.submit_q.submit_lock));
    566                         #else
    567                                 lock(ring.submit_q.submit_lock __cfaabi_dbg_ctx2);
    568                         #endif
    569 
    570                         /* paranoid */ verifyf( ring.submit_q.sqes[ idx ].user_data != 3ul64,
    571                         /* paranoid */  "index %u already reclaimed\n"
    572                         /* paranoid */  "head %u, prev %u, tail %u\n"
    573                         /* paranoid */  "[-0: %u,-1: %u,-2: %u,-3: %u]\n",
    574                         /* paranoid */  idx,
    575                         /* paranoid */  *ring.submit_q.head, ring.submit_q.prev_head, *tail
    576                         /* paranoid */  ,ring.submit_q.array[ ((*ring.submit_q.head) - 0) & (*ring.submit_q.mask) ]
    577                         /* paranoid */  ,ring.submit_q.array[ ((*ring.submit_q.head) - 1) & (*ring.submit_q.mask) ]
    578                         /* paranoid */  ,ring.submit_q.array[ ((*ring.submit_q.head) - 2) & (*ring.submit_q.mask) ]
    579                         /* paranoid */  ,ring.submit_q.array[ ((*ring.submit_q.head) - 3) & (*ring.submit_q.mask) ]
    580                         /* paranoid */ );
    581 
    582                         // Append to the list of ready entries
    583 
    584                         /* paranoid */ verify( idx <= mask );
    585                         ring.submit_q.array[ (*tail) & mask ] = idx;
    586                         __atomic_fetch_add(tail, 1ul32, __ATOMIC_SEQ_CST);
    587 
    588                         // Submit however, many entries need to be submitted
    589                         int ret = __io_uring_enter( ring, 1, false );
    590                         if( ret < 0 ) {
    591                                 switch((int)errno) {
    592                                 default:
    593                                         abort( "KERNEL ERROR: IO_URING SUBMIT - %s\n", strerror(errno) );
    594                                 }
    595                         }
    596 
    597                         /* paranoid */ verify(ret == 1);
    598 
    599                         // update statistics
    600                         __STATS__( false,
    601                                 io.submit_q.submit_avg.csm += 1;
    602                                 io.submit_q.submit_avg.cnt += 1;
    603                         )
    604 
    605                         {
    606                                 __attribute__((unused)) volatile __u32 * const head = ring.submit_q.head;
    607                                 __attribute__((unused)) __u32 last_idx = ring.submit_q.array[ ((*head) - 1) & mask ];
    608                                 __attribute__((unused)) volatile struct io_uring_sqe * sqe = &ring.submit_q.sqes[last_idx];
    609 
    610                                 __cfadbg_print_safe( io,
    611                                         "Kernel I/O : last submitted is %u (%p)\n"
    612                                         "    data: %p\n"
    613                                         "    opcode: %s\n"
    614                                         "    fd: %d\n"
    615                                         "    flags: %d\n"
    616                                         "    prio: %d\n"
    617                                         "    off: %p\n"
    618                                         "    addr: %p\n"
    619                                         "    len: %d\n"
    620                                         "    other flags: %d\n"
    621                                         "    splice fd: %d\n"
    622                                         "    pad[0]: %llu\n"
    623                                         "    pad[1]: %llu\n"
    624                                         "    pad[2]: %llu\n",
    625                                         last_idx, sqe,
    626                                         (void*)sqe->user_data,
    627                                         opcodes[sqe->opcode],
    628                                         sqe->fd,
    629                                         sqe->flags,
    630                                         sqe->ioprio,
    631                                         sqe->off,
    632                                         sqe->addr,
    633                                         sqe->len,
    634                                         sqe->accept_flags,
    635                                         sqe->splice_fd_in,
    636                                         sqe->__pad2[0],
    637                                         sqe->__pad2[1],
    638                                         sqe->__pad2[2]
    639                                 );
    640                         }
    641 
    642                         __atomic_thread_fence( __ATOMIC_SEQ_CST );
    643                         // Release the consumed SQEs
    644                         __release_consumed_submission( ring );
    645                         // ring.submit_q.sqes[idx].user_data = 3ul64;
    646 
    647                         #if defined(LEADER_LOCK)
    648                                 next(ring.submit_q.submit_lock);
    649                         #else
    650                                 unlock(ring.submit_q.submit_lock);
    651                         #endif
    652 
    653                         __cfadbg_print_safe( io, "Kernel I/O : submitted %u for %p\n", idx, active_thread() );
    654                 }
    655         }
    656 
    657         // #define PARTIAL_SUBMIT 32
    658 
    659         // go through the list of submissions in the ready array and moved them into
    660         // the ring's submit queue
    661         static unsigned __collect_submitions( struct __io_data & ring ) {
    662                 /* paranoid */ verify( ring.submit_q.ready != 0p );
    663                 /* paranoid */ verify( ring.submit_q.ready_cnt > 0 );
    664 
    665                 unsigned to_submit = 0;
    666                 __u32 tail = *ring.submit_q.tail;
    667                 const __u32 mask = *ring.submit_q.mask;
    668                 #if defined(PARTIAL_SUBMIT)
    669                         #if defined(LEADER_LOCK)
    670                                 #error PARTIAL_SUBMIT and LEADER_LOCK cannot co-exist
    671                         #endif
    672                         const __u32 cnt = ring.submit_q.ready_cnt > PARTIAL_SUBMIT ? PARTIAL_SUBMIT : ring.submit_q.ready_cnt;
    673                         const __u32 offset = ring.submit_q.prev_ready;
    674                         ring.submit_q.prev_ready += cnt;
    675                 #else
    676                         const __u32 cnt = ring.submit_q.ready_cnt;
    677                         const __u32 offset = 0;
    678                 #endif
    679 
    680                 // Go through the list of ready submissions
    681                 for( c; cnt ) {
    682                         __u32 i = (offset + c) % ring.submit_q.ready_cnt;
    683 
    684                         // replace any submission with the sentinel, to consume it.
    685                         __u32 idx = __atomic_exchange_n( &ring.submit_q.ready[i], -1ul32, __ATOMIC_RELAXED);
    686 
    687                         // If it was already the sentinel, then we are done
    688                         if( idx == -1ul32 ) continue;
    689 
    690                         // If we got a real submission, append it to the list
    691                         ring.submit_q.array[ (tail + to_submit) & mask ] = idx & mask;
    692                         to_submit++;
    693                 }
    694 
    695                 // Increment the tail based on how many we are ready to submit
    696                 __atomic_fetch_add(ring.submit_q.tail, to_submit, __ATOMIC_SEQ_CST);
    697 
    698                 return to_submit;
    699         }
    700 
     312                        __submit(ctx, idxs, have, lazy);
     313
     314                        // Mark the instance as no longer in-use, re-enable interrupts and return
     315                        __STATS__( true, io.submit.fast += 1; )
     316                        enable_interrupts( __cfaabi_dbg_ctx );
     317
     318                        __cfadbg_print_safe(io, "Kernel I/O : submitted on fast path\n");
     319                        return;
     320                }
     321
     322                // Fast path failed, fallback on arbitration
     323                __STATS__( true, io.submit.slow += 1; )
     324                enable_interrupts( __cfaabi_dbg_ctx );
     325
     326                __cfadbg_print_safe(io, "Kernel I/O : falling back on arbiter for submission\n");
     327
     328                __ioarbiter_submit(*inctx->arbiter, inctx, idxs, have, lazy);
     329        }
     330
     331        //=============================================================================================
     332        // Flushing
    701333        // Go through the ring's submit queue and release everything that has already been consumed
    702334        // by io_uring
    703         static __u32 __release_consumed_submission( struct __io_data & ring ) {
    704                 const __u32 smask = *ring.submit_q.mask;
    705 
    706                 // We need to get the lock to copy the old head and new head
    707                 if( !try_lock(ring.submit_q.release_lock __cfaabi_dbg_ctx2) ) return 0;
     335        // This cannot be done by multiple threads
     336        static __u32 __release_sqes( struct $io_context & ctx ) {
     337                const __u32 mask = *ctx.sq.mask;
     338
    708339                __attribute__((unused))
    709                 __u32 ctail = *ring.submit_q.tail;        // get the current tail of the queue
    710                 __u32 chead = *ring.submit_q.head;              // get the current head of the queue
    711                 __u32 phead = ring.submit_q.prev_head;  // get the head the last time we were here
    712                 ring.submit_q.prev_head = chead;                // note up to were we processed
    713                 unlock(ring.submit_q.release_lock);
     340                __u32 ctail = *ctx.sq.kring.tail;    // get the current tail of the queue
     341                __u32 chead = *ctx.sq.kring.head;        // get the current head of the queue
     342                __u32 phead = ctx.sq.kring.released; // get the head the last time we were here
     343
     344                __u32 ftail = ctx.sq.free_ring.tail;  // get the current tail of the queue
    714345
    715346                // the 3 fields are organized like this diagram
     
    730361                __u32 count = chead - phead;
    731362
     363                if(count == 0) {
     364                        return 0;
     365                }
     366
    732367                // We acquired an previous-head/current-head range
    733368                // go through the range and release the sqes
    734369                for( i; count ) {
    735                         __u32 idx = ring.submit_q.array[ (phead + i) & smask ];
    736 
    737                         /* paranoid */ verify( 0 != ring.submit_q.sqes[ idx ].user_data );
    738                         __clean( &ring.submit_q.sqes[ idx ] );
    739                 }
     370                        __cfadbg_print_safe(io, "Kernel I/O : release loop\n");
     371                        __u32 idx = ctx.sq.kring.array[ (phead + i) & mask ];
     372                        ctx.sq.free_ring.array[ (ftail + i) & mask ] = idx;
     373                }
     374
     375                ctx.sq.kring.released = chead;          // note up to were we processed
     376                __atomic_store_n(&ctx.sq.free_ring.tail, ftail + count, __ATOMIC_SEQ_CST);
     377
     378                __ioarbiter_notify(ctx);
     379
    740380                return count;
    741381        }
    742382
    743         void __sqe_clean( volatile struct io_uring_sqe * sqe ) {
    744                 __clean( sqe );
    745         }
    746 
    747         static inline void __clean( volatile struct io_uring_sqe * sqe ) {
    748                 // If we are in debug mode, thrash the fields to make sure we catch reclamation errors
    749                 __cfaabi_dbg_debug_do(
    750                         memset(sqe, 0xde, sizeof(*sqe));
    751                         sqe->opcode = (sizeof(opcodes) / sizeof(const char *)) - 1;
    752                 );
    753 
    754                 // Mark the entry as unused
    755                 __atomic_store_n(&sqe->user_data, 3ul64, __ATOMIC_SEQ_CST);
     383//=============================================================================================
     384// I/O Arbiter
     385//=============================================================================================
     386        static $io_context * __ioarbiter_allocate( $io_arbiter & mutex this, processor * proc, __u32 idxs[], __u32 want ) {
     387                __cfadbg_print_safe(io, "Kernel I/O : arbiter allocating\n");
     388
     389                __STATS__( false, io.alloc.block += 1; )
     390
     391                // No one has any resources left, wait for something to finish
     392                // Mark as pending
     393                __atomic_store_n( &this.pending.flag, true, __ATOMIC_SEQ_CST );
     394
     395                // Wait for our turn to submit
     396                wait( this.pending.blocked, want );
     397
     398                __attribute((unused)) bool ret =
     399                __alloc( this.pending.ctx, idxs, want);
     400                /* paranoid */ verify( ret );
     401
     402                return this.pending.ctx;
     403
     404        }
     405
     406        static void __ioarbiter_notify( $io_arbiter & mutex this, $io_context * ctx ) {
     407                /* paranoid */ verify( !is_empty(this.pending.blocked) );
     408                this.pending.ctx = ctx;
     409
     410                while( !is_empty(this.pending.blocked) ) {
     411                        __cfadbg_print_safe(io, "Kernel I/O : notifying\n");
     412                        __u32 have = ctx->sq.free_ring.tail - ctx->sq.free_ring.head;
     413                        __u32 want = front( this.pending.blocked );
     414
     415                        if( have > want ) return;
     416
     417                        signal_block( this.pending.blocked );
     418                }
     419
     420                this.pending.flag = false;
     421        }
     422
     423        static void __ioarbiter_notify( $io_context & ctx ) {
     424                if(__atomic_load_n( &ctx.arbiter->pending.flag, __ATOMIC_SEQ_CST)) {
     425                        __ioarbiter_notify( *ctx.arbiter, &ctx );
     426                }
     427        }
     428
     429        // Simply append to the pending
     430        static void __ioarbiter_submit( $io_arbiter & mutex this, $io_context * ctx, __u32 idxs[], __u32 have, bool lazy ) {
     431                __cfadbg_print_safe(io, "Kernel I/O : submitting %u from the arbiter to context %u\n", have, ctx->fd);
     432
     433                /* paranoid */ verify( &this == ctx->arbiter );
     434
     435                // Mark as pending
     436                __atomic_store_n( &ctx->ext_sq.empty, false, __ATOMIC_SEQ_CST );
     437
     438                __cfadbg_print_safe(io, "Kernel I/O : waiting to submit %u\n", have);
     439
     440                // Wait for our turn to submit
     441                wait( ctx->ext_sq.blocked );
     442
     443                // Submit our indexes
     444                __submit(ctx, idxs, have, lazy);
     445
     446                __cfadbg_print_safe(io, "Kernel I/O : %u submitted from arbiter\n", have);
     447        }
     448
     449        static void __ioarbiter_flush( $io_arbiter & mutex this, $io_context * ctx ) {
     450                /* paranoid */ verify( &this == ctx->arbiter );
     451
     452                __STATS__( false, io.flush.external += 1; )
     453
     454                __cfadbg_print_safe(io, "Kernel I/O : arbiter flushing\n");
     455
     456                condition & blcked = ctx->ext_sq.blocked;
     457                /* paranoid */ verify( ctx->ext_sq.empty == is_empty( blcked ) );
     458                while(!is_empty( blcked )) {
     459                        signal_block( blcked );
     460                }
     461
     462                ctx->ext_sq.empty = true;
    756463        }
    757464#endif
  • libcfa/src/concurrency/io/call.cfa.in

    r342af53 r8e4aa05  
    5454                        | IOSQE_IO_DRAIN
    5555                #endif
     56                #if defined(CFA_HAVE_IOSQE_IO_LINK)
     57                        | IOSQE_IO_LINK
     58                #endif
     59                #if defined(CFA_HAVE_IOSQE_IO_HARDLINK)
     60                        | IOSQE_IO_HARDLINK
     61                #endif
    5662                #if defined(CFA_HAVE_IOSQE_ASYNC)
    5763                        | IOSQE_ASYNC
    5864                #endif
    59         ;
    60 
    61         static const __u32 LINK_FLAGS = 0
    62                 #if defined(CFA_HAVE_IOSQE_IO_LINK)
    63                         | IOSQE_IO_LINK
    64                 #endif
    65                 #if defined(CFA_HAVE_IOSQE_IO_HARDLINK)
    66                         | IOSQE_IO_HARDLINK
     65                #if defined(CFA_HAVE_IOSQE_BUFFER_SELECTED)
     66                        | IOSQE_BUFFER_SELECTED
    6767                #endif
    6868        ;
     
    7474        ;
    7575
    76         extern [* volatile struct io_uring_sqe, __u32] __submit_alloc( struct __io_data & ring, __u64 data );
    77         extern void __submit( struct io_context * ctx, __u32 idx ) __attribute__((nonnull (1)));
    78 
    79         static inline io_context * __get_io_context( void ) {
    80                 cluster * cltr = active_cluster();
    81 
    82                 /* paranoid */ verifyf( cltr, "No active cluster for io operation\\n");
    83                 assertf( cltr->io.cnt > 0, "Cluster %p has no default io contexts and no context was specified\\n", cltr );
    84 
    85                 /* paranoid */ verifyf( cltr->io.ctxs, "default io contexts for cluster %p are missing\\n", cltr);
    86                 return &cltr->io.ctxs[ thread_rand() % cltr->io.cnt ];
    87         }
     76        extern struct $io_context * cfa_io_allocate(struct io_uring_sqe * out_sqes[], __u32 out_idxs[], __u32 want)  __attribute__((nonnull (1,2)));
     77        extern void cfa_io_submit( struct $io_context * in_ctx, __u32 in_idxs[], __u32 have, bool lazy ) __attribute__((nonnull (1,2)));
    8878#endif
    8979
     
    9888
    9989extern "C" {
    100         #include <sys/types.h>
     90        #include <asm/types.h>
    10191        #include <sys/socket.h>
    10292        #include <sys/syscall.h>
     
    142132        extern int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
    143133
    144         extern ssize_t splice(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_t len, unsigned int flags);
     134        extern ssize_t splice(int fd_in, __off64_t *off_in, int fd_out, __off64_t *off_out, size_t len, unsigned int flags);
    145135        extern ssize_t tee(int fd_in, int fd_out, size_t len, unsigned int flags);
    146136}
     
    195185                return ', '.join(args_a)
    196186
    197 AsyncTemplate = """inline void async_{name}(io_future_t & future, {params}, int submit_flags, io_cancellation * cancellation, io_context * context) {{
     187AsyncTemplate = """inline void async_{name}(io_future_t & future, {params}, __u64 submit_flags) {{
    198188        #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_{op})
    199189                ssize_t res = {name}({args});
     
    205195                }}
    206196        #else
    207                 // we don't support LINK yet
    208                 if( 0 != (submit_flags & LINK_FLAGS) ) {{
    209                         errno = ENOTSUP; return -1;
    210                 }}
    211 
    212                 if( !context ) {{
    213                         context = __get_io_context();
    214                 }}
    215                 if(cancellation) {{
    216                         cancellation->target = (__u64)(uintptr_t)&future;
    217                 }}
    218 
    219197                __u8 sflags = REGULAR_FLAGS & submit_flags;
    220                 struct __io_data & ring = *context->thrd.ring;
    221 
    222198                __u32 idx;
    223199                struct io_uring_sqe * sqe;
    224                 [(volatile struct io_uring_sqe *) sqe, idx] = __submit_alloc( ring, (__u64)(uintptr_t)&future );
     200                struct $io_context * ctx = cfa_io_allocate( &sqe, &idx, 1 );
    225201
    226202                sqe->opcode = IORING_OP_{op};
     203                sqe->user_data = (__u64)(uintptr_t)&future;
    227204                sqe->flags = sflags;
    228205                sqe->ioprio = 0;
     
    239216
    240217                verify( sqe->user_data == (__u64)(uintptr_t)&future );
    241                 __submit( context, idx );
     218                cfa_io_submit( ctx, &idx, 1, 0 != (submit_flags & CFA_IO_LAZY) );
    242219        #endif
    243220}}"""
    244221
    245 SyncTemplate = """{ret} cfa_{name}({params}, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {{
    246         if( timeout >= 0 ) {{
    247                 errno = ENOTSUP;
    248                 return -1;
    249         }}
     222SyncTemplate = """{ret} cfa_{name}({params}, __u64 submit_flags) {{
    250223        io_future_t future;
    251224
    252         async_{name}( future, {args}, submit_flags, cancellation, context );
     225        async_{name}( future, {args}, submit_flags );
    253226
    254227        wait( future );
     
    393366        }),
    394367        # CFA_HAVE_IORING_OP_SPLICE
    395         Call('SPLICE', 'ssize_t splice(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_t len, unsigned int flags)', {
     368        Call('SPLICE', 'ssize_t splice(int fd_in, __off64_t *off_in, int fd_out, __off64_t *off_out, size_t len, unsigned int flags)', {
    396369                'splice_fd_in': 'fd_in',
    397370                'splice_off_in': 'off_in ? (__u64)*off_in : (__u64)-1',
     
    415388        if c.define:
    416389                print("""#if defined({define})
    417         {ret} cfa_{name}({params}, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context);
     390        {ret} cfa_{name}({params}, __u64 submit_flags);
    418391#endif""".format(define=c.define,ret=c.ret, name=c.name, params=c.params))
    419392        else:
    420                 print("{ret} cfa_{name}({params}, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context);"
     393                print("{ret} cfa_{name}({params}, __u64 submit_flags);"
    421394                .format(ret=c.ret, name=c.name, params=c.params))
    422395
     
    426399        if c.define:
    427400                print("""#if defined({define})
    428         void async_{name}(io_future_t & future, {params}, int submit_flags, io_cancellation * cancellation, io_context * context);
     401        void async_{name}(io_future_t & future, {params}, __u64 submit_flags);
    429402#endif""".format(define=c.define,name=c.name, params=c.params))
    430403        else:
    431                 print("void async_{name}(io_future_t & future, {params}, int submit_flags, io_cancellation * cancellation, io_context * context);"
     404                print("void async_{name}(io_future_t & future, {params}, __u64 submit_flags);"
    432405                .format(name=c.name, params=c.params))
    433406print("\n")
     
    474447
    475448print("""
    476 //-----------------------------------------------------------------------------
    477 bool cancel(io_cancellation & this) {
    478         #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_ASYNC_CANCEL)
    479                 return false;
    480         #else
    481                 io_future_t future;
    482 
    483                 io_context * context = __get_io_context();
    484 
    485                 __u8 sflags = 0;
    486                 struct __io_data & ring = *context->thrd.ring;
    487 
    488                 __u32 idx;
    489                 volatile struct io_uring_sqe * sqe;
    490                 [sqe, idx] = __submit_alloc( ring, (__u64)(uintptr_t)&future );
    491 
    492                 sqe->__pad2[0] = sqe->__pad2[1] = sqe->__pad2[2] = 0;
    493                 sqe->opcode = IORING_OP_ASYNC_CANCEL;
    494                 sqe->flags = sflags;
    495                 sqe->addr = this.target;
    496 
    497                 verify( sqe->user_data == (__u64)(uintptr_t)&future );
    498                 __submit( context, idx );
    499 
    500                 wait(future);
    501 
    502                 if( future.result == 0 ) return true; // Entry found
    503                 if( future.result == -EALREADY) return true; // Entry found but in progress
    504                 if( future.result == -ENOENT ) return false; // Entry not found
    505                 return false;
    506         #endif
    507 }
    508 
    509449//-----------------------------------------------------------------------------
    510450// Check if a function is has asynchronous
  • libcfa/src/concurrency/io/setup.cfa

    r342af53 r8e4aa05  
    2626
    2727#if !defined(CFA_HAVE_LINUX_IO_URING_H)
    28         void __kernel_io_startup() {
    29                 // Nothing to do without io_uring
    30         }
    31 
    32         void __kernel_io_shutdown() {
    33                 // Nothing to do without io_uring
    34         }
    35 
    3628        void ?{}(io_context_params & this) {}
    3729
    38         void ?{}(io_context & this, struct cluster & cl) {}
    39         void ?{}(io_context & this, struct cluster & cl, const io_context_params & params) {}
    40 
    41         void ^?{}(io_context & this) {}
    42         void ^?{}(io_context & this, bool cluster_context) {}
     30        void  ?{}($io_context & this, struct cluster & cl) {}
     31        void ^?{}($io_context & this) {}
     32
     33        void __cfa_io_start( processor * proc ) {}
     34        void __cfa_io_flush( processor * proc ) {}
     35        void __cfa_io_stop ( processor * proc ) {}
     36
     37        $io_arbiter * create(void) { return 0p; }
     38        void destroy($io_arbiter *) {}
    4339
    4440#else
     
    6561        void ?{}(io_context_params & this) {
    6662                this.num_entries = 256;
    67                 this.num_ready = 256;
    68                 this.submit_aff = -1;
    69                 this.eager_submits = false;
    70                 this.poller_submits = false;
    71                 this.poll_submit = false;
    72                 this.poll_complete = false;
    7363        }
    7464
     
    10393
    10494//=============================================================================================
    105 // I/O Startup / Shutdown logic + Master Poller
    106 //=============================================================================================
    107 
    108         // IO Master poller loop forward
    109         static void * iopoll_loop( __attribute__((unused)) void * args );
    110 
    111         static struct {
    112                 pthread_t     thrd;    // pthread handle to io poller thread
    113                 void *        stack;   // pthread stack for io poller thread
    114                 int           epollfd; // file descriptor to the epoll instance
    115                 volatile bool run;     // Whether or not to continue
    116         } iopoll;
    117 
    118         void __kernel_io_startup(void) {
    119                 __cfadbg_print_safe(io_core, "Kernel : Creating EPOLL instance\n" );
    120 
    121                 iopoll.epollfd = epoll_create1(0);
    122                 if (iopoll.epollfd == -1) {
    123                         abort( "internal error, epoll_create1\n");
    124                 }
    125 
    126                 __cfadbg_print_safe(io_core, "Kernel : Starting io poller thread\n" );
    127 
    128                 iopoll.run = true;
    129                 iopoll.stack = __create_pthread( &iopoll.thrd, iopoll_loop, 0p );
    130         }
    131 
    132         void __kernel_io_shutdown(void) {
    133                 // Notify the io poller thread of the shutdown
    134                 iopoll.run = false;
    135                 sigval val = { 1 };
    136                 pthread_sigqueue( iopoll.thrd, SIGUSR1, val );
    137 
    138                 // Wait for the io poller thread to finish
    139 
    140                 __destroy_pthread( iopoll.thrd, iopoll.stack, 0p );
    141 
    142                 int ret = close(iopoll.epollfd);
    143                 if (ret == -1) {
    144                         abort( "internal error, close epoll\n");
    145                 }
    146 
    147                 // Io polling is now fully stopped
    148 
    149                 __cfadbg_print_safe(io_core, "Kernel : IO poller stopped\n" );
    150         }
    151 
    152         static void * iopoll_loop( __attribute__((unused)) void * args ) {
    153                 __processor_id_t id;
    154                 id.full_proc = false;
    155                 id.id = doregister(&id);
    156                 __cfaabi_tls.this_proc_id = &id;
    157                 __cfadbg_print_safe(io_core, "Kernel : IO poller thread starting\n" );
    158 
    159                 // Block signals to control when they arrive
    160                 sigset_t mask;
    161                 sigfillset(&mask);
    162                 if ( pthread_sigmask( SIG_BLOCK, &mask, 0p ) == -1 ) {
    163                 abort( "internal error, pthread_sigmask" );
    164                 }
    165 
    166                 sigdelset( &mask, SIGUSR1 );
    167 
    168                 // Create sufficient events
    169                 struct epoll_event events[10];
    170                 // Main loop
    171                 while( iopoll.run ) {
    172                         __cfadbg_print_safe(io_core, "Kernel I/O - epoll : waiting on io_uring contexts\n");
    173 
    174                         // Wait for events
    175                         int nfds = epoll_pwait( iopoll.epollfd, events, 10, -1, &mask );
    176 
    177                         __cfadbg_print_safe(io_core, "Kernel I/O - epoll : %d io contexts events, waking up\n", nfds);
    178 
    179                         // Check if an error occured
    180                         if (nfds == -1) {
    181                                 if( errno == EINTR ) continue;
    182                                 abort( "internal error, pthread_sigmask" );
    183                         }
    184 
    185                         for(i; nfds) {
    186                                 $io_ctx_thread * io_ctx = ($io_ctx_thread *)(uintptr_t)events[i].data.u64;
    187                                 /* paranoid */ verify( io_ctx );
    188                                 __cfadbg_print_safe(io_core, "Kernel I/O - epoll : Unparking io poller %d (%p)\n", io_ctx->ring->fd, io_ctx);
    189                                 #if !defined( __CFA_NO_STATISTICS__ )
    190                                         __cfaabi_tls.this_stats = io_ctx->self.curr_cluster->stats;
    191                                 #endif
    192 
    193                                 eventfd_t v;
    194                                 eventfd_read(io_ctx->ring->efd, &v);
    195 
    196                                 post( io_ctx->sem );
    197                         }
    198                 }
    199 
    200                 __cfadbg_print_safe(io_core, "Kernel : IO poller thread stopping\n" );
    201                 unregister(&id);
    202                 return 0p;
    203         }
    204 
    205 //=============================================================================================
    20695// I/O Context Constrution/Destruction
    20796//=============================================================================================
    20897
    209         void ?{}($io_ctx_thread & this, struct cluster & cl) { (this.self){ "IO Poller", cl }; }
    210         void main( $io_ctx_thread & this );
    211         static inline $thread * get_thread( $io_ctx_thread & this ) { return &this.self; }
    212         void ^?{}( $io_ctx_thread & mutex this ) {}
    213 
    214         static void __io_create ( __io_data & this, const io_context_params & params_in );
    215         static void __io_destroy( __io_data & this );
    216 
    217         void ?{}(io_context & this, struct cluster & cl, const io_context_params & params) {
    218                 (this.thrd){ cl };
    219                 this.thrd.ring = malloc();
    220                 __cfadbg_print_safe(io_core, "Kernel I/O : Creating ring for io_context %p\n", &this);
    221                 __io_create( *this.thrd.ring, params );
    222 
    223                 __cfadbg_print_safe(io_core, "Kernel I/O : Starting poller thread for io_context %p\n", &this);
    224                 this.thrd.done = false;
    225                 __thrd_start( this.thrd, main );
    226 
    227                 __cfadbg_print_safe(io_core, "Kernel I/O : io_context %p ready\n", &this);
    228         }
    229 
    230         void ?{}(io_context & this, struct cluster & cl) {
    231                 io_context_params params;
    232                 (this){ cl, params };
    233         }
    234 
    235         void ^?{}(io_context & this, bool cluster_context) {
    236                 __cfadbg_print_safe(io_core, "Kernel I/O : tearing down io_context %p\n", &this);
    237 
    238                 // Notify the thread of the shutdown
    239                 __atomic_store_n(&this.thrd.done, true, __ATOMIC_SEQ_CST);
    240 
    241                 // If this is an io_context within a cluster, things get trickier
    242                 $thread & thrd = this.thrd.self;
    243                 if( cluster_context ) {
    244                         // We are about to do weird things with the threads
    245                         // we don't need interrupts to complicate everything
    246                         disable_interrupts();
    247 
    248                         // Get cluster info
    249                         cluster & cltr = *thrd.curr_cluster;
    250                         /* paranoid */ verify( cltr.idles.total == 0 || &cltr == mainCluster );
    251                         /* paranoid */ verify( !ready_mutate_islocked() );
    252 
    253                         // We need to adjust the clean-up based on where the thread is
    254                         if( thrd.state == Ready || thrd.preempted != __NO_PREEMPTION ) {
    255                                 // This is the tricky case
    256                                 // The thread was preempted or ready to run and now it is on the ready queue
    257                                 // but the cluster is shutting down, so there aren't any processors to run the ready queue
    258                                 // the solution is to steal the thread from the ready-queue and pretend it was blocked all along
    259 
    260                                 ready_schedule_lock();
    261                                         // The thread should on the list
    262                                         /* paranoid */ verify( thrd.link.next != 0p );
    263 
    264                                         // Remove the thread from the ready queue of this cluster
    265                                         // The thread should be the last on the list
    266                                         __attribute__((unused)) bool removed = remove_head( &cltr, &thrd );
    267                                         /* paranoid */ verify( removed );
    268                                         thrd.link.next = 0p;
    269                                         thrd.link.prev = 0p;
    270 
    271                                         // Fixup the thread state
    272                                         thrd.state = Blocked;
    273                                         thrd.ticket = TICKET_BLOCKED;
    274                                         thrd.preempted = __NO_PREEMPTION;
    275 
    276                                 ready_schedule_unlock();
    277 
    278                                 // Pretend like the thread was blocked all along
    279                         }
    280                         // !!! This is not an else if !!!
    281                         // Ok, now the thread is blocked (whether we cheated to get here or not)
    282                         if( thrd.state == Blocked ) {
    283                                 // This is the "easy case"
    284                                 // The thread is parked and can easily be moved to active cluster
    285                                 verify( thrd.curr_cluster != active_cluster() || thrd.curr_cluster == mainCluster );
    286                                 thrd.curr_cluster = active_cluster();
    287 
    288                                 // unpark the fast io_poller
    289                                 unpark( &thrd );
    290                         }
    291                         else {
    292                                 // The thread is in a weird state
    293                                 // I don't know what to do here
    294                                 abort("io_context poller thread is in unexpected state, cannot clean-up correctly\n");
    295                         }
    296 
    297                         // The weird thread kidnapping stuff is over, restore interrupts.
    298                         enable_interrupts( __cfaabi_dbg_ctx );
    299                 } else {
    300                         post( this.thrd.sem );
    301                 }
    302 
    303                 ^(this.thrd){};
    304                 __cfadbg_print_safe(io_core, "Kernel I/O : Stopped poller thread for io_context %p\n", &this);
    305 
    306                 __io_destroy( *this.thrd.ring );
    307                 __cfadbg_print_safe(io_core, "Kernel I/O : Destroyed ring for io_context %p\n", &this);
    308 
    309                 free(this.thrd.ring);
    310         }
    311 
    312         void ^?{}(io_context & this) {
    313                 ^(this){ false };
    314         }
    315 
    316         static void __io_create( __io_data & this, const io_context_params & params_in ) {
     98
     99
     100        static void __io_uring_setup ( $io_context & this, const io_context_params & params_in, int procfd );
     101        static void __io_uring_teardown( $io_context & this );
     102        static void __epoll_register($io_context & ctx);
     103        static void __epoll_unregister($io_context & ctx);
     104        void __ioarbiter_register( $io_arbiter & mutex, $io_context & ctx );
     105        void __ioarbiter_unregister( $io_arbiter & mutex, $io_context & ctx );
     106
     107        void ?{}($io_context & this, processor * proc, struct cluster & cl) {
     108                /* paranoid */ verify( cl.io.arbiter );
     109                this.proc = proc;
     110                this.arbiter = cl.io.arbiter;
     111                this.ext_sq.empty = true;
     112                (this.ext_sq.blocked){};
     113                __io_uring_setup( this, cl.io.params, proc->idle );
     114                __cfadbg_print_safe(io_core, "Kernel I/O : Created ring for io_context %u (%p)\n", this.fd, &this);
     115        }
     116
     117        void ^?{}($io_context & this) {
     118                __cfadbg_print_safe(io_core, "Kernel I/O : tearing down io_context %u\n", this.fd);
     119
     120                __io_uring_teardown( this );
     121                __cfadbg_print_safe(io_core, "Kernel I/O : Destroyed ring for io_context %u\n", this.fd);
     122        }
     123
     124        extern void __disable_interrupts_hard();
     125        extern void __enable_interrupts_hard();
     126
     127        static void __io_uring_setup( $io_context & this, const io_context_params & params_in, int procfd ) {
    317128                // Step 1 : call to setup
    318129                struct io_uring_params params;
    319130                memset(&params, 0, sizeof(params));
    320                 if( params_in.poll_submit   ) params.flags |= IORING_SETUP_SQPOLL;
    321                 if( params_in.poll_complete ) params.flags |= IORING_SETUP_IOPOLL;
     131                // if( params_in.poll_submit   ) params.flags |= IORING_SETUP_SQPOLL;
     132                // if( params_in.poll_complete ) params.flags |= IORING_SETUP_IOPOLL;
    322133
    323134                __u32 nentries = params_in.num_entries != 0 ? params_in.num_entries : 256;
     
    325136                        abort("ERROR: I/O setup 'num_entries' must be a power of 2\n");
    326137                }
    327                 if( params_in.poller_submits && params_in.eager_submits ) {
    328                         abort("ERROR: I/O setup 'poller_submits' and 'eager_submits' cannot be used together\n");
    329                 }
    330138
    331139                int fd = syscall(__NR_io_uring_setup, nentries, &params );
     
    335143
    336144                // Step 2 : mmap result
    337                 memset( &this, 0, sizeof(struct __io_data) );
    338                 struct __submition_data  & sq = this.submit_q;
    339                 struct __completion_data & cq = this.completion_q;
     145                struct __sub_ring_t & sq = this.sq;
     146                struct __cmp_ring_t & cq = this.cq;
    340147
    341148                // calculate the right ring size
     
    386193                // Get the pointers from the kernel to fill the structure
    387194                // submit queue
    388                 sq.head    = (volatile __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.head);
    389                 sq.tail    = (volatile __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.tail);
    390                 sq.mask    = (   const __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.ring_mask);
    391                 sq.num     = (   const __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.ring_entries);
    392                 sq.flags   = (         __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.flags);
    393                 sq.dropped = (         __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.dropped);
    394                 sq.array   = (         __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.array);
    395                 sq.prev_head = *sq.head;
    396 
    397                 {
    398                         const __u32 num = *sq.num;
    399                         for( i; num ) {
    400                                 __sqe_clean( &sq.sqes[i] );
    401                         }
    402                 }
    403 
    404                 (sq.submit_lock){};
    405                 (sq.release_lock){};
    406 
    407                 if( params_in.poller_submits || params_in.eager_submits ) {
    408                         /* paranoid */ verify( is_pow2( params_in.num_ready ) || (params_in.num_ready < 8) );
    409                         sq.ready_cnt = max( params_in.num_ready, 8 );
    410                         sq.ready = alloc( sq.ready_cnt, 64`align );
    411                         for(i; sq.ready_cnt) {
    412                                 sq.ready[i] = -1ul32;
    413                         }
    414                         sq.prev_ready = 0;
    415                 }
    416                 else {
    417                         sq.ready_cnt = 0;
    418                         sq.ready = 0p;
    419                         sq.prev_ready = 0;
    420                 }
     195                sq.kring.head  = (volatile __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.head);
     196                sq.kring.tail  = (volatile __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.tail);
     197                sq.kring.array = (         __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.array);
     198                sq.mask        = (   const __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.ring_mask);
     199                sq.num         = (   const __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.ring_entries);
     200                sq.flags       = (         __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.flags);
     201                sq.dropped     = (         __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.dropped);
     202
     203                sq.kring.released = 0;
     204
     205                sq.free_ring.head = 0;
     206                sq.free_ring.tail = *sq.num;
     207                sq.free_ring.array = alloc( *sq.num, 128`align );
     208                for(i; (__u32)*sq.num) {
     209                        sq.free_ring.array[i] = i;
     210                }
     211
     212                sq.to_submit = 0;
    421213
    422214                // completion queue
     
    429221
    430222                // Step 4 : eventfd
    431                 int efd;
    432                 for() {
    433                         efd = eventfd(0, 0);
    434                         if (efd < 0) {
    435                                 if (errno == EINTR) continue;
    436                                 abort("KERNEL ERROR: IO_URING EVENTFD - %s\n", strerror(errno));
    437                         }
    438                         break;
    439                 }
    440 
    441                 int ret;
    442                 for() {
    443                         ret = syscall( __NR_io_uring_register, fd, IORING_REGISTER_EVENTFD, &efd, 1);
    444                         if (ret < 0) {
    445                                 if (errno == EINTR) continue;
    446                                 abort("KERNEL ERROR: IO_URING EVENTFD REGISTER - %s\n", strerror(errno));
    447                         }
    448                         break;
    449                 }
     223                // io_uring_register is so f*cking slow on some machine that it
     224                // will never succeed if preemption isn't hard blocked
     225                __cfadbg_print_safe(io_core, "Kernel I/O : registering %d for completion with ring %d\n", procfd, fd);
     226
     227                __disable_interrupts_hard();
     228
     229                int ret = syscall( __NR_io_uring_register, fd, IORING_REGISTER_EVENTFD, &procfd, 1);
     230                if (ret < 0) {
     231                        abort("KERNEL ERROR: IO_URING EVENTFD REGISTER - %s\n", strerror(errno));
     232                }
     233
     234                __enable_interrupts_hard();
     235
     236                __cfadbg_print_safe(io_core, "Kernel I/O : registered %d for completion with ring %d\n", procfd, fd);
    450237
    451238                // some paranoid checks
     
    457244                /* paranoid */ verifyf( (*sq.mask) == ((*sq.num) - 1ul32), "IO_URING Expected mask to be %u (%u entries), was %u", (*sq.num) - 1ul32, *sq.num, *sq.mask );
    458245                /* paranoid */ verifyf( (*sq.num) >= nentries, "IO_URING Expected %u entries, got %u", nentries, *sq.num );
    459                 /* paranoid */ verifyf( (*sq.head) == 0, "IO_URING Expected head to be 0, got %u", *sq.head );
    460                 /* paranoid */ verifyf( (*sq.tail) == 0, "IO_URING Expected tail to be 0, got %u", *sq.tail );
     246                /* paranoid */ verifyf( (*sq.kring.head) == 0, "IO_URING Expected head to be 0, got %u", *sq.kring.head );
     247                /* paranoid */ verifyf( (*sq.kring.tail) == 0, "IO_URING Expected tail to be 0, got %u", *sq.kring.tail );
    461248
    462249                // Update the global ring info
    463                 this.ring_flags = params.flags;
     250                this.ring_flags = 0;
    464251                this.fd         = fd;
    465                 this.efd        = efd;
    466                 this.eager_submits  = params_in.eager_submits;
    467                 this.poller_submits = params_in.poller_submits;
    468         }
    469 
    470         static void __io_destroy( __io_data & this ) {
     252        }
     253
     254        static void __io_uring_teardown( $io_context & this ) {
    471255                // Shutdown the io rings
    472                 struct __submition_data  & sq = this.submit_q;
    473                 struct __completion_data & cq = this.completion_q;
     256                struct __sub_ring_t & sq = this.sq;
     257                struct __cmp_ring_t & cq = this.cq;
    474258
    475259                // unmap the submit queue entries
     
    486270                // close the file descriptor
    487271                close(this.fd);
    488                 close(this.efd);
    489 
    490                 free( this.submit_q.ready ); // Maybe null, doesn't matter
     272
     273                free( this.sq.free_ring.array ); // Maybe null, doesn't matter
     274        }
     275
     276        void __cfa_io_start( processor * proc ) {
     277                proc->io.ctx = alloc();
     278                (*proc->io.ctx){proc, *proc->cltr};
     279        }
     280        void __cfa_io_stop ( processor * proc ) {
     281                ^(*proc->io.ctx){};
     282                free(proc->io.ctx);
    491283        }
    492284
     
    494286// I/O Context Sleep
    495287//=============================================================================================
    496         #define IOEVENTS EPOLLIN | EPOLLONESHOT
    497 
    498         static inline void __ioctx_epoll_ctl($io_ctx_thread & ctx, int op, const char * error) {
    499                 struct epoll_event ev;
    500                 ev.events = IOEVENTS;
    501                 ev.data.u64 = (__u64)&ctx;
    502                 int ret = epoll_ctl(iopoll.epollfd, op, ctx.ring->efd, &ev);
    503                 if (ret < 0) {
    504                         abort( "KERNEL ERROR: EPOLL %s - (%d) %s\n", error, (int)errno, strerror(errno) );
    505                 }
    506         }
    507 
    508         void __ioctx_register($io_ctx_thread & ctx) {
    509                 __ioctx_epoll_ctl(ctx, EPOLL_CTL_ADD, "ADD");
    510         }
    511 
    512         void __ioctx_prepare_block($io_ctx_thread & ctx) {
    513                 __cfadbg_print_safe(io_core, "Kernel I/O - epoll : Re-arming io poller %d (%p)\n", ctx.ring->fd, &ctx);
    514                 __ioctx_epoll_ctl(ctx, EPOLL_CTL_MOD, "REARM");
    515         }
     288        // static inline void __epoll_ctl($io_context & ctx, int op, const char * error) {
     289        //      struct epoll_event ev;
     290        //      ev.events = EPOLLIN | EPOLLONESHOT;
     291        //      ev.data.u64 = (__u64)&ctx;
     292        //      int ret = epoll_ctl(iopoll.epollfd, op, ctx.efd, &ev);
     293        //      if (ret < 0) {
     294        //              abort( "KERNEL ERROR: EPOLL %s - (%d) %s\n", error, (int)errno, strerror(errno) );
     295        //      }
     296        // }
     297
     298        // static void __epoll_register($io_context & ctx) {
     299        //      __epoll_ctl(ctx, EPOLL_CTL_ADD, "ADD");
     300        // }
     301
     302        // static void __epoll_unregister($io_context & ctx) {
     303        //      // Read the current epoch so we know when to stop
     304        //      size_t curr = __atomic_load_n(&iopoll.epoch, __ATOMIC_SEQ_CST);
     305
     306        //      // Remove the fd from the iopoller
     307        //      __epoll_ctl(ctx, EPOLL_CTL_DEL, "REMOVE");
     308
     309        //      // Notify the io poller thread of the shutdown
     310        //      iopoll.run = false;
     311        //      sigval val = { 1 };
     312        //      pthread_sigqueue( iopoll.thrd, SIGUSR1, val );
     313
     314        //      // Make sure all this is done
     315        //      __atomic_thread_fence(__ATOMIC_SEQ_CST);
     316
     317        //      // Wait for the next epoch
     318        //      while(curr == iopoll.epoch && !iopoll.stopped) Pause();
     319        // }
     320
     321        // void __ioctx_prepare_block($io_context & ctx) {
     322        //      __cfadbg_print_safe(io_core, "Kernel I/O - epoll : Re-arming io poller %d (%p)\n", ctx.fd, &ctx);
     323        //      __epoll_ctl(ctx, EPOLL_CTL_MOD, "REARM");
     324        // }
     325
    516326
    517327//=============================================================================================
    518328// I/O Context Misc Setup
    519329//=============================================================================================
    520         void register_fixed_files( io_context & ctx, int * files, unsigned count ) {
    521                 int ret = syscall( __NR_io_uring_register, ctx.thrd.ring->fd, IORING_REGISTER_FILES, files, count );
    522                 if( ret < 0 ) {
    523                         abort( "KERNEL ERROR: IO_URING SYSCALL - (%d) %s\n", (int)errno, strerror(errno) );
    524                 }
    525 
    526                 __cfadbg_print_safe( io_core, "Kernel I/O : Performed io_register for %p, returned %d\n", active_thread(), ret );
    527         }
    528 
    529         void register_fixed_files( cluster & cltr, int * files, unsigned count ) {
    530                 for(i; cltr.io.cnt) {
    531                         register_fixed_files( cltr.io.ctxs[i], files, count );
    532                 }
    533         }
     330        void ?{}( $io_arbiter & this ) {
     331                this.pending.flag = false;
     332        }
     333
     334        void ^?{}( $io_arbiter & mutex this ) {
     335                // /* paranoid */ verify( empty(this.assigned) );
     336                // /* paranoid */ verify( empty(this.available) );
     337                /* paranoid */ verify( is_empty(this.pending.blocked) );
     338        }
     339
     340        $io_arbiter * create(void) {
     341                return new();
     342        }
     343        void destroy($io_arbiter * arbiter) {
     344                delete(arbiter);
     345        }
     346
     347//=============================================================================================
     348// I/O Context Misc Setup
     349//=============================================================================================
     350
    534351#endif
  • libcfa/src/concurrency/io/types.hfa

    r342af53 r8e4aa05  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // io/types.hfa --
     7// io/types.hfa -- PRIVATE
     8// Types used by the I/O subsystem
    89//
    910// Author           : Thierry Delisle
     
    2122
    2223#include "bits/locks.hfa"
     24#include "kernel/fwd.hfa"
    2325
    2426#if defined(CFA_HAVE_LINUX_IO_URING_H)
    25         #define LEADER_LOCK
    26         struct __leaderlock_t {
    27                 struct $thread * volatile value;        // ($thread) next_leader | (bool:1) is_locked
    28         };
     27        #include "bits/sequence.hfa"
     28        #include "monitor.hfa"
    2929
    30         static inline void ?{}( __leaderlock_t & this ) { this.value = 0p; }
     30        struct processor;
     31        monitor $io_arbiter;
    3132
    3233        //-----------------------------------------------------------------------
    3334        // Ring Data structure
    34       struct __submition_data {
    35                 // Head and tail of the ring (associated with array)
    36                 volatile __u32 * head;
    37                 volatile __u32 * tail;
    38                 volatile __u32 prev_head;
     35      struct __sub_ring_t {
     36                struct {
     37                        // Head and tail of the ring (associated with array)
     38                        volatile __u32 * head;   // one passed last index consumed by the kernel
     39                        volatile __u32 * tail;   // one passed last index visible to the kernel
     40                        volatile __u32 released; // one passed last index released back to the free list
    3941
    40                 // The actual kernel ring which uses head/tail
    41                 // indexes into the sqes arrays
    42                 __u32 * array;
     42                        // The actual kernel ring which uses head/tail
     43                        // indexes into the sqes arrays
     44                        __u32 * array;
     45                } kring;
     46
     47                struct {
     48                        volatile __u32 head;
     49                        volatile __u32 tail;
     50                        // The ring which contains free allocations
     51                        // indexes into the sqes arrays
     52                        __u32 * array;
     53                } free_ring;
     54
     55                // number of sqes to submit on next system call.
     56                __u32 to_submit;
    4357
    4458                // number of entries and mask to go with it
     
    4660                const __u32 * mask;
    4761
    48                 // Submission flags (Not sure what for)
     62                // Submission flags, currently only IORING_SETUP_SQPOLL
    4963                __u32 * flags;
    5064
    51                 // number of sqes not submitted (whatever that means)
     65                // number of sqes not submitted
     66                // From documentation : [dropped] is incremented for each invalid submission queue entry encountered in the ring buffer.
    5267                __u32 * dropped;
    5368
    54                 // Like head/tail but not seen by the kernel
    55                 volatile __u32 * ready;
    56                 __u32 ready_cnt;
    57                 __u32 prev_ready;
    58 
    59                 #if defined(LEADER_LOCK)
    60                         __leaderlock_t submit_lock;
    61                 #else
    62                         __spinlock_t submit_lock;
    63                 #endif
    64                 __spinlock_t  release_lock;
    65 
    6669                // A buffer of sqes (not the actual ring)
    67                 volatile struct io_uring_sqe * sqes;
     70                struct io_uring_sqe * sqes;
    6871
    6972                // The location and size of the mmaped area
     
    7275        };
    7376
    74         struct __completion_data {
     77        struct __cmp_ring_t {
    7578                // Head and tail of the ring
    7679                volatile __u32 * head;
     
    8184                const __u32 * num;
    8285
    83                 // number of cqes not submitted (whatever that means)
     86                // I don't know what this value is for
    8487                __u32 * overflow;
    8588
     
    9295        };
    9396
    94         struct __io_data {
    95                 struct __submition_data submit_q;
    96                 struct __completion_data completion_q;
     97        struct __attribute__((aligned(128))) $io_context {
     98                $io_arbiter * arbiter;
     99                processor * proc;
     100
     101                struct {
     102                        volatile bool empty;
     103                        condition blocked;
     104                } ext_sq;
     105
     106                struct __sub_ring_t sq;
     107                struct __cmp_ring_t cq;
    97108                __u32 ring_flags;
    98109                int fd;
    99                 int efd;
    100                 bool eager_submits:1;
    101                 bool poller_submits:1;
     110        };
     111
     112        monitor __attribute__((aligned(128))) $io_arbiter {
     113                struct {
     114                        condition blocked;
     115                        $io_context * ctx;
     116                        volatile bool flag;
     117                } pending;
    102118        };
    103119
     
    131147        #endif
    132148
    133         struct $io_ctx_thread;
    134         void __ioctx_register($io_ctx_thread & ctx);
    135         void __ioctx_prepare_block($io_ctx_thread & ctx);
    136         void __sqe_clean( volatile struct io_uring_sqe * sqe );
     149        // void __ioctx_prepare_block($io_context & ctx);
    137150#endif
    138151
  • libcfa/src/concurrency/iofwd.hfa

    r342af53 r8e4aa05  
    1818#include <unistd.h>
    1919extern "C" {
    20         #include <sys/types.h>
     20        #include <asm/types.h>
    2121        #if CFA_HAVE_LINUX_IO_URING_H
    2222                #include <linux/io_uring.h>
     
    4848struct cluster;
    4949struct io_future_t;
    50 struct io_context;
    51 struct io_cancellation;
     50struct $io_context;
    5251
    5352struct iovec;
     
    5554struct sockaddr;
    5655struct statx;
     56struct epoll_event;
     57
     58//----------
     59// underlying calls
     60extern struct $io_context * cfa_io_allocate(struct io_uring_sqe * out_sqes[], __u32 out_idxs[], __u32 want)  __attribute__((nonnull (1,2)));
     61extern void cfa_io_submit( struct $io_context * in_ctx, __u32 in_idxs[], __u32 have, bool lazy ) __attribute__((nonnull (1,2)));
    5762
    5863//----------
    5964// synchronous calls
    6065#if defined(CFA_HAVE_PREADV2)
    61         extern ssize_t cfa_preadv2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context);
     66        extern ssize_t cfa_preadv2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags, __u64 submit_flags);
    6267#endif
    6368#if defined(CFA_HAVE_PWRITEV2)
    64         extern ssize_t cfa_pwritev2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context);
     69        extern ssize_t cfa_pwritev2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags, __u64 submit_flags);
    6570#endif
    66 extern int cfa_fsync(int fd, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context);
    67 extern int cfa_epoll_ctl(int epfd, int op, int fd, struct epoll_event *event, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context);
    68 extern int cfa_sync_file_range(int fd, off64_t offset, off64_t nbytes, unsigned int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context);
    69 extern  ssize_t cfa_sendmsg(int sockfd, const struct msghdr *msg, int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context);
    70 extern ssize_t cfa_recvmsg(int sockfd, struct msghdr *msg, int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context);
    71 extern ssize_t cfa_send(int sockfd, const void *buf, size_t len, int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context);
    72 extern ssize_t cfa_recv(int sockfd, void *buf, size_t len, int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context);
    73 extern int cfa_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context);
    74 extern int cfa_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context);
    75 extern int cfa_fallocate(int fd, int mode, off_t offset, off_t len, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context);
    76 extern int cfa_posix_fadvise(int fd, off_t offset, off_t len, int advice, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context);
    77 extern int cfa_madvise(void *addr, size_t length, int advice, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context);
    78 extern int cfa_openat(int dirfd, const char *pathname, int flags, mode_t mode, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context);
     71extern int cfa_fsync(int fd, __u64 submit_flags);
     72extern int cfa_epoll_ctl(int epfd, int op, int fd, struct epoll_event *event, __u64 submit_flags);
     73extern int cfa_sync_file_range(int fd, off64_t offset, off64_t nbytes, unsigned int flags, __u64 submit_flags);
     74extern  ssize_t cfa_sendmsg(int sockfd, const struct msghdr *msg, int flags, __u64 submit_flags);
     75extern ssize_t cfa_recvmsg(int sockfd, struct msghdr *msg, int flags, __u64 submit_flags);
     76extern ssize_t cfa_send(int sockfd, const void *buf, size_t len, int flags, __u64 submit_flags);
     77extern ssize_t cfa_recv(int sockfd, void *buf, size_t len, int flags, __u64 submit_flags);
     78extern int cfa_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags, __u64 submit_flags);
     79extern int cfa_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen, __u64 submit_flags);
     80extern int cfa_fallocate(int fd, int mode, off_t offset, off_t len, __u64 submit_flags);
     81extern int cfa_posix_fadvise(int fd, off_t offset, off_t len, int advice, __u64 submit_flags);
     82extern int cfa_madvise(void *addr, size_t length, int advice, __u64 submit_flags);
     83extern int cfa_openat(int dirfd, const char *pathname, int flags, mode_t mode, __u64 submit_flags);
    7984#if defined(CFA_HAVE_OPENAT2)
    80         extern int cfa_openat2(int dirfd, const char *pathname, struct open_how * how, size_t size, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context);
     85        extern int cfa_openat2(int dirfd, const char *pathname, struct open_how * how, size_t size, __u64 submit_flags);
    8186#endif
    82 extern int cfa_close(int fd, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context);
     87extern int cfa_close(int fd, __u64 submit_flags);
    8388#if defined(CFA_HAVE_STATX)
    84         extern int cfa_statx(int dirfd, const char *pathname, int flags, unsigned int mask, struct statx *statxbuf, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context);
     89        extern int cfa_statx(int dirfd, const char *pathname, int flags, unsigned int mask, struct statx *statxbuf, __u64 submit_flags);
    8590#endif
    86 extern ssize_t cfa_read(int fd, void * buf, size_t count, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context);
    87 extern ssize_t cfa_write(int fd, void * buf, size_t count, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context);
    88 extern ssize_t cfa_splice(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_t len, unsigned int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context);
    89 extern ssize_t cfa_tee(int fd_in, int fd_out, size_t len, unsigned int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context);
     91extern ssize_t cfa_read(int fd, void * buf, size_t count, __u64 submit_flags);
     92extern ssize_t cfa_write(int fd, void * buf, size_t count, __u64 submit_flags);
     93extern ssize_t cfa_splice(int fd_in, __off64_t *off_in, int fd_out, __off64_t *off_out, size_t len, unsigned int flags, __u64 submit_flags);
     94extern ssize_t cfa_tee(int fd_in, int fd_out, size_t len, unsigned int flags, __u64 submit_flags);
    9095
    9196//----------
    9297// asynchronous calls
    9398#if defined(CFA_HAVE_PREADV2)
    94         extern void async_preadv2(io_future_t & future, int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags, int submit_flags, io_cancellation * cancellation, io_context * context);
     99        extern void async_preadv2(io_future_t & future, int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags, __u64 submit_flags);
    95100#endif
    96101#if defined(CFA_HAVE_PWRITEV2)
    97         extern void async_pwritev2(io_future_t & future, int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags, int submit_flags, io_cancellation * cancellation, io_context * context);
     102        extern void async_pwritev2(io_future_t & future, int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags, __u64 submit_flags);
    98103#endif
    99 extern void async_fsync(io_future_t & future, int fd, int submit_flags, io_cancellation * cancellation, io_context * context);
    100 extern void async_epoll_ctl(io_future_t & future, int epfd, int op, int fd, struct epoll_event *event, int submit_flags, io_cancellation * cancellation, io_context * context);
    101 extern void async_sync_file_range(io_future_t & future, int fd, off64_t offset, off64_t nbytes, unsigned int flags, int submit_flags, io_cancellation * cancellation, io_context * context);
    102 extern void async_sendmsg(io_future_t & future, int sockfd, const struct msghdr *msg, int flags, int submit_flags, io_cancellation * cancellation, io_context * context);
    103 extern void async_recvmsg(io_future_t & future, int sockfd, struct msghdr *msg, int flags, int submit_flags, io_cancellation * cancellation, io_context * context);
    104 extern void async_send(io_future_t & future, int sockfd, const void *buf, size_t len, int flags, int submit_flags, io_cancellation * cancellation, io_context * context);
    105 extern void async_recv(io_future_t & future, int sockfd, void *buf, size_t len, int flags, int submit_flags, io_cancellation * cancellation, io_context * context);
    106 extern void async_accept4(io_future_t & future, int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags, int submit_flags, io_cancellation * cancellation, io_context * context);
    107 extern void async_connect(io_future_t & future, int sockfd, const struct sockaddr *addr, socklen_t addrlen, int submit_flags, io_cancellation * cancellation, io_context * context);
    108 extern void async_fallocate(io_future_t & future, int fd, int mode, off_t offset, off_t len, int submit_flags, io_cancellation * cancellation, io_context * context);
    109 extern void async_posix_fadvise(io_future_t & future, int fd, off_t offset, off_t len, int advice, int submit_flags, io_cancellation * cancellation, io_context * context);
    110 extern void async_madvise(io_future_t & future, void *addr, size_t length, int advice, int submit_flags, io_cancellation * cancellation, io_context * context);
    111 extern void async_openat(io_future_t & future, int dirfd, const char *pathname, int flags, mode_t mode, int submit_flags, io_cancellation * cancellation, io_context * context);
     104extern void async_fsync(io_future_t & future, int fd, __u64 submit_flags);
     105extern void async_epoll_ctl(io_future_t & future, int epfd, int op, int fd, struct epoll_event *event, __u64 submit_flags);
     106extern void async_sync_file_range(io_future_t & future, int fd, off64_t offset, off64_t nbytes, unsigned int flags, __u64 submit_flags);
     107extern void async_sendmsg(io_future_t & future, int sockfd, const struct msghdr *msg, int flags, __u64 submit_flags);
     108extern void async_recvmsg(io_future_t & future, int sockfd, struct msghdr *msg, int flags, __u64 submit_flags);
     109extern void async_send(io_future_t & future, int sockfd, const void *buf, size_t len, int flags, __u64 submit_flags);
     110extern void async_recv(io_future_t & future, int sockfd, void *buf, size_t len, int flags, __u64 submit_flags);
     111extern void async_accept4(io_future_t & future, int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags, __u64 submit_flags);
     112extern void async_connect(io_future_t & future, int sockfd, const struct sockaddr *addr, socklen_t addrlen, __u64 submit_flags);
     113extern void async_fallocate(io_future_t & future, int fd, int mode, off_t offset, off_t len, __u64 submit_flags);
     114extern void async_posix_fadvise(io_future_t & future, int fd, off_t offset, off_t len, int advice, __u64 submit_flags);
     115extern void async_madvise(io_future_t & future, void *addr, size_t length, int advice, __u64 submit_flags);
     116extern void async_openat(io_future_t & future, int dirfd, const char *pathname, int flags, mode_t mode, __u64 submit_flags);
    112117#if defined(CFA_HAVE_OPENAT2)
    113         extern void async_openat2(io_future_t & future, int dirfd, const char *pathname, struct open_how * how, size_t size, int submit_flags, io_cancellation * cancellation, io_context * context);
     118        extern void async_openat2(io_future_t & future, int dirfd, const char *pathname, struct open_how * how, size_t size, __u64 submit_flags);
    114119#endif
    115 extern void async_close(io_future_t & future, int fd, int submit_flags, io_cancellation * cancellation, io_context * context);
     120extern void async_close(io_future_t & future, int fd, __u64 submit_flags);
    116121#if defined(CFA_HAVE_STATX)
    117         extern void async_statx(io_future_t & future, int dirfd, const char *pathname, int flags, unsigned int mask, struct statx *statxbuf, int submit_flags, io_cancellation * cancellation, io_context * context);
     122        extern void async_statx(io_future_t & future, int dirfd, const char *pathname, int flags, unsigned int mask, struct statx *statxbuf, __u64 submit_flags);
    118123#endif
    119 void async_read(io_future_t & future, int fd, void * buf, size_t count, int submit_flags, io_cancellation * cancellation, io_context * context);
    120 extern void async_write(io_future_t & future, int fd, void * buf, size_t count, int submit_flags, io_cancellation * cancellation, io_context * context);
    121 extern void async_splice(io_future_t & future, int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_t len, unsigned int flags, int submit_flags, io_cancellation * cancellation, io_context * context);
    122 extern void async_tee(io_future_t & future, int fd_in, int fd_out, size_t len, unsigned int flags, int submit_flags, io_cancellation * cancellation, io_context * context);
     124void async_read(io_future_t & future, int fd, void * buf, size_t count, __u64 submit_flags);
     125extern void async_write(io_future_t & future, int fd, void * buf, size_t count, __u64 submit_flags);
     126extern void async_splice(io_future_t & future, int fd_in, __off64_t *off_in, int fd_out, __off64_t *off_out, size_t len, unsigned int flags, __u64 submit_flags);
     127extern void async_tee(io_future_t & future, int fd_in, int fd_out, size_t len, unsigned int flags, __u64 submit_flags);
    123128
    124129
     
    126131// Check if a function is blocks a only the user thread
    127132bool has_user_level_blocking( fptr_t func );
    128 
    129 //-----------------------------------------------------------------------------
    130 void register_fixed_files( io_context & ctx , int * files, unsigned count );
    131 void register_fixed_files( cluster    & cltr, int * files, unsigned count );
  • libcfa/src/concurrency/kernel.cfa

    r342af53 r8e4aa05  
    2222#include <signal.h>
    2323#include <unistd.h>
     24extern "C" {
     25        #include <sys/eventfd.h>
     26}
    2427
    2528//CFA Includes
     
    114117static [unsigned idle, unsigned total, * processor] query( & __cluster_idles idles );
    115118
     119extern void __cfa_io_start( processor * );
     120extern void __cfa_io_drain( processor * );
     121extern void __cfa_io_flush( processor * );
     122extern void __cfa_io_stop ( processor * );
     123static inline void __maybe_io_drain( processor * );
     124
     125extern void __disable_interrupts_hard();
     126extern void __enable_interrupts_hard();
    116127
    117128//=============================================================================================
     
    129140        verify(this);
    130141
     142        __cfa_io_start( this );
     143
    131144        __cfadbg_print_safe(runtime_core, "Kernel : core %p starting\n", this);
    132145        #if !defined(__CFA_NO_STATISTICS__)
     
    140153                preemption_scope scope = { this };
    141154
     155                #if !defined(__CFA_NO_STATISTICS__)
     156                        unsigned long long last_tally = rdtscl();
     157                #endif
     158
     159
    142160                __cfadbg_print_safe(runtime_core, "Kernel : core %p started\n", this);
    143161
     
    145163                MAIN_LOOP:
    146164                for() {
     165                        // Check if there is pending io
     166                        __maybe_io_drain( this );
     167
    147168                        // Try to get the next thread
    148169                        readyThread = __next_thread( this->cltr );
    149170
    150171                        if( !readyThread ) {
     172                                __cfa_io_flush( this );
    151173                                readyThread = __next_thread_slow( this->cltr );
    152174                        }
     
    184206                                #endif
    185207
    186                                 wait( this->idle );
     208                                __cfadbg_print_safe(runtime_core, "Kernel : core %p waiting on eventfd %d\n", this, this->idle);
     209
     210                                __disable_interrupts_hard();
     211                                eventfd_t val;
     212                                eventfd_read( this->idle, &val );
     213                                __enable_interrupts_hard();
    187214
    188215                                #if !defined(__CFA_NO_STATISTICS__)
     
    201228                        /* paranoid */ verify( readyThread );
    202229
     230                        // Reset io dirty bit
     231                        this->io.dirty = false;
     232
    203233                        // We found a thread run it
    204234                        __run_thread(this, readyThread);
     
    206236                        // Are we done?
    207237                        if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP;
     238
     239                        #if !defined(__CFA_NO_STATISTICS__)
     240                                unsigned long long curr = rdtscl();
     241                                if(curr > (last_tally + 500000000)) {
     242                                        __tally_stats(this->cltr->stats, __cfaabi_tls.this_stats);
     243                                        last_tally = curr;
     244                                }
     245                        #endif
     246
     247                        if(this->io.pending && !this->io.dirty) {
     248                                __cfa_io_flush( this );
     249                        }
    208250                }
    209251
     
    211253        }
    212254
    213         V( this->terminated );
     255        __cfa_io_stop( this );
     256
     257        post( this->terminated );
     258
    214259
    215260        if(this == mainProcessor) {
     
    234279        /* paranoid */ verifyf( thrd_dst->link.next == 0p, "Expected null got %p", thrd_dst->link.next );
    235280        __builtin_prefetch( thrd_dst->context.SP );
     281
     282        __cfadbg_print_safe(runtime_core, "Kernel : core %p running thread %p (%s)\n", this, thrd_dst, thrd_dst->self_cor.name);
    236283
    237284        $coroutine * proc_cor = get_coroutine(this->runner);
     
    316363        // Just before returning to the processor, set the processor coroutine to active
    317364        proc_cor->state = Active;
     365
     366        __cfadbg_print_safe(runtime_core, "Kernel : core %p finished running thread %p\n", this, thrd_dst);
    318367
    319368        /* paranoid */ verify( ! __preemption_enabled() );
     
    550599
    551600        // We found a processor, wake it up
    552         post( p->idle );
     601        eventfd_t val;
     602        val = 1;
     603        eventfd_write( p->idle, val );
    553604
    554605        #if !defined(__CFA_NO_STATISTICS__)
     
    568619        disable_interrupts();
    569620                /* paranoid */ verify( ! __preemption_enabled() );
    570                 post( this->idle );
     621                eventfd_t val;
     622                val = 1;
     623                eventfd_write( this->idle, val );
    571624        enable_interrupts( __cfaabi_dbg_ctx );
    572625}
     
    611664// Unexpected Terminating logic
    612665//=============================================================================================
    613 static __spinlock_t kernel_abort_lock;
    614 static bool kernel_abort_called = false;
    615 
    616 void * kernel_abort(void) __attribute__ ((__nothrow__)) {
    617         // abort cannot be recursively entered by the same or different processors because all signal handlers return when
    618         // the globalAbort flag is true.
    619         lock( kernel_abort_lock __cfaabi_dbg_ctx2 );
    620 
    621         // disable interrupts, it no longer makes sense to try to interrupt this processor
    622         disable_interrupts();
    623 
    624         // first task to abort ?
    625         if ( kernel_abort_called ) {                    // not first task to abort ?
    626                 unlock( kernel_abort_lock );
    627 
    628                 sigset_t mask;
    629                 sigemptyset( &mask );
    630                 sigaddset( &mask, SIGALRM );            // block SIGALRM signals
    631                 sigaddset( &mask, SIGUSR1 );            // block SIGALRM signals
    632                 sigsuspend( &mask );                            // block the processor to prevent further damage during abort
    633                 _exit( EXIT_FAILURE );                          // if processor unblocks before it is killed, terminate it
    634         }
    635         else {
    636                 kernel_abort_called = true;
    637                 unlock( kernel_abort_lock );
    638         }
    639 
    640         return __cfaabi_tls.this_thread;
    641 }
    642 
    643 void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) {
    644         $thread * thrd = ( $thread * ) kernel_data;
     666void __kernel_abort_msg( char * abort_text, int abort_text_size ) {
     667        $thread * thrd = __cfaabi_tls.this_thread;
    645668
    646669        if(thrd) {
     
    662685}
    663686
    664 int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) {
    665         return get_coroutine(kernelTLS().this_thread) == get_coroutine(mainThread) ? 4 : 2;
     687int __kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) {
     688        return get_coroutine(__cfaabi_tls.this_thread) == get_coroutine(mainThread) ? 4 : 2;
    666689}
    667690
     
    681704// Kernel Utilities
    682705//=============================================================================================
    683 //-----------------------------------------------------------------------------
    684 // Locks
    685 void  ?{}( semaphore & this, int count = 1 ) {
    686         (this.lock){};
    687         this.count = count;
    688         (this.waiting){};
    689 }
    690 void ^?{}(semaphore & this) {}
    691 
    692 bool P(semaphore & this) with( this ){
    693         lock( lock __cfaabi_dbg_ctx2 );
    694         count -= 1;
    695         if ( count < 0 ) {
    696                 // queue current task
    697                 append( waiting, active_thread() );
    698 
    699                 // atomically release spin lock and block
    700                 unlock( lock );
    701                 park();
    702                 return true;
    703         }
    704         else {
    705             unlock( lock );
    706             return false;
    707         }
    708 }
    709 
    710 bool V(semaphore & this) with( this ) {
    711         $thread * thrd = 0p;
    712         lock( lock __cfaabi_dbg_ctx2 );
    713         count += 1;
    714         if ( count <= 0 ) {
    715                 // remove task at head of waiting list
    716                 thrd = pop_head( waiting );
    717         }
    718 
    719         unlock( lock );
    720 
    721         // make new owner
    722         unpark( thrd );
    723 
    724         return thrd != 0p;
    725 }
    726 
    727 bool V(semaphore & this, unsigned diff) with( this ) {
    728         $thread * thrd = 0p;
    729         lock( lock __cfaabi_dbg_ctx2 );
    730         int release = max(-count, (int)diff);
    731         count += diff;
    732         for(release) {
    733                 unpark( pop_head( waiting ) );
    734         }
    735 
    736         unlock( lock );
    737 
    738         return thrd != 0p;
     706#if defined(CFA_HAVE_LINUX_IO_URING_H)
     707#include "io/types.hfa"
     708#endif
     709
     710static inline void __maybe_io_drain( processor * proc ) {
     711        #if defined(CFA_HAVE_LINUX_IO_URING_H)
     712                __cfadbg_print_safe(runtime_core, "Kernel : core %p checking io for ring %d\n", proc, proc->io.ctx->fd);
     713
     714                // Check if we should drain the queue
     715                $io_context * ctx = proc->io.ctx;
     716                unsigned head = *ctx->cq.head;
     717                unsigned tail = *ctx->cq.tail;
     718                if(head != tail) __cfa_io_drain( proc );
     719        #endif
    739720}
    740721
  • libcfa/src/concurrency/kernel.hfa

    r342af53 r8e4aa05  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // kernel --
     7// kernel -- Header containing the core of the kernel API
    88//
    99// Author           : Thierry Delisle
     
    2424extern "C" {
    2525        #include <bits/pthreadtypes.h>
     26        #include <pthread.h>
    2627        #include <linux/types.h>
    2728}
    2829
    29 //-----------------------------------------------------------------------------
    30 // Locks
    31 struct semaphore {
    32         __spinlock_t lock;
    33         int count;
    34         __queue_t($thread) waiting;
    35 };
    36 
    37 void  ?{}(semaphore & this, int count = 1);
    38 void ^?{}(semaphore & this);
    39 bool   P (semaphore & this);
    40 bool   V (semaphore & this);
    41 bool   V (semaphore & this, unsigned count);
    42 
     30#ifdef __CFA_WITH_VERIFY__
     31        extern bool __cfaabi_dbg_in_kernel();
     32#endif
     33
     34//-----------------------------------------------------------------------------
     35// I/O
     36struct cluster;
     37struct $io_context;
     38struct $io_arbiter;
     39
     40struct io_context_params {
     41        int num_entries;
     42};
     43
     44void  ?{}(io_context_params & this);
    4345
    4446//-----------------------------------------------------------------------------
     
    8082        pthread_t kernel_thread;
    8183
     84        struct {
     85                $io_context * ctx;
     86                bool pending;
     87                bool dirty;
     88        } io;
     89
    8290        // Preemption data
    8391        // Node which is added in the discrete event simulaiton
     
    8896
    8997        // Idle lock (kernel semaphore)
    90         __bin_sem_t idle;
     98        int idle;
    9199
    92100        // Termination synchronisation (user semaphore)
    93         semaphore terminated;
     101        oneshot terminated;
    94102
    95103        // pthread Stack
     
    118126
    119127DLISTED_MGD_IMPL_OUT(processor)
    120 
    121 //-----------------------------------------------------------------------------
    122 // I/O
    123 struct __io_data;
    124 
    125 // IO poller user-thread
    126 // Not using the "thread" keyword because we want to control
    127 // more carefully when to start/stop it
    128 struct $io_ctx_thread {
    129         struct __io_data * ring;
    130         single_sem sem;
    131         volatile bool done;
    132         $thread self;
    133 };
    134 
    135 
    136 struct io_context {
    137         $io_ctx_thread thrd;
    138 };
    139 
    140 struct io_context_params {
    141         int num_entries;
    142         int num_ready;
    143         int submit_aff;
    144         bool eager_submits:1;
    145         bool poller_submits:1;
    146         bool poll_submit:1;
    147         bool poll_complete:1;
    148 };
    149 
    150 void  ?{}(io_context_params & this);
    151 
    152 void  ?{}(io_context & this, struct cluster & cl);
    153 void  ?{}(io_context & this, struct cluster & cl, const io_context_params & params);
    154 void ^?{}(io_context & this);
    155 
    156 struct io_cancellation {
    157         __u64 target;
    158 };
    159 
    160 static inline void  ?{}(io_cancellation & this) { this.target = -1u; }
    161 static inline void ^?{}(io_cancellation &) {}
    162 bool cancel(io_cancellation & this);
    163128
    164129//-----------------------------------------------------------------------------
     
    246211
    247212        struct {
    248                 io_context * ctxs;
    249                 unsigned cnt;
     213                $io_arbiter * arbiter;
     214                io_context_params params;
    250215        } io;
    251216
  • libcfa/src/concurrency/kernel/fwd.hfa

    r342af53 r8e4aa05  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // kernel/fwd.hfa --
     7// kernel/fwd.hfa -- PUBLIC
     8// Fundamental code needed to implement threading M.E.S. algorithms.
    89//
    910// Author           : Thierry Delisle
     
    134135                extern uint64_t thread_rand();
    135136
     137                // Semaphore which only supports a single thread
     138                struct single_sem {
     139                        struct $thread * volatile ptr;
     140                };
     141
     142                static inline {
     143                        void  ?{}(single_sem & this) {
     144                                this.ptr = 0p;
     145                        }
     146
     147                        void ^?{}(single_sem &) {}
     148
     149                        bool wait(single_sem & this) {
     150                                for() {
     151                                        struct $thread * expected = this.ptr;
     152                                        if(expected == 1p) {
     153                                                if(__atomic_compare_exchange_n(&this.ptr, &expected, 0p, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
     154                                                        return false;
     155                                                }
     156                                        }
     157                                        else {
     158                                                /* paranoid */ verify( expected == 0p );
     159                                                if(__atomic_compare_exchange_n(&this.ptr, &expected, active_thread(), false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
     160                                                        park();
     161                                                        return true;
     162                                                }
     163                                        }
     164
     165                                }
     166                        }
     167
     168                        bool post(single_sem & this) {
     169                                for() {
     170                                        struct $thread * expected = this.ptr;
     171                                        if(expected == 1p) return false;
     172                                        if(expected == 0p) {
     173                                                if(__atomic_compare_exchange_n(&this.ptr, &expected, 1p, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
     174                                                        return false;
     175                                                }
     176                                        }
     177                                        else {
     178                                                if(__atomic_compare_exchange_n(&this.ptr, &expected, 0p, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
     179                                                        unpark( expected );
     180                                                        return true;
     181                                                }
     182                                        }
     183                                }
     184                        }
     185                }
     186
     187                // Synchronozation primitive which only supports a single thread and one post
     188                // Similar to a binary semaphore with a 'one shot' semantic
     189                // is expected to be discarded after each party call their side
     190                struct oneshot {
     191                        // Internal state :
     192                        //     0p     : is initial state (wait will block)
     193                        //     1p     : fulfilled (wait won't block)
     194                        // any thread : a thread is currently waiting
     195                        struct $thread * volatile ptr;
     196                };
     197
     198                static inline {
     199                        void  ?{}(oneshot & this) {
     200                                this.ptr = 0p;
     201                        }
     202
     203                        void ^?{}(oneshot &) {}
     204
     205                        // Wait for the post, return immidiately if it already happened.
     206                        // return true if the thread was parked
     207                        bool wait(oneshot & this) {
     208                                for() {
     209                                        struct $thread * expected = this.ptr;
     210                                        if(expected == 1p) return false;
     211                                        /* paranoid */ verify( expected == 0p );
     212                                        if(__atomic_compare_exchange_n(&this.ptr, &expected, active_thread(), false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
     213                                                park();
     214                                                /* paranoid */ verify( this.ptr == 1p );
     215                                                return true;
     216                                        }
     217                                }
     218                        }
     219
     220                        // Mark as fulfilled, wake thread if needed
     221                        // return true if a thread was unparked
     222                        bool post(oneshot & this) {
     223                                struct $thread * got = __atomic_exchange_n( &this.ptr, 1p, __ATOMIC_SEQ_CST);
     224                                if( got == 0p ) return false;
     225                                unpark( got );
     226                                return true;
     227                        }
     228                }
     229
     230                // base types for future to build upon
     231                // It is based on the 'oneshot' type to allow multiple futures
     232                // to block on the same instance, permitting users to block a single
     233                // thread on "any of" [a given set of] futures.
     234                // does not support multiple threads waiting on the same future
     235                struct future_t {
     236                        // Internal state :
     237                        //     0p      : is initial state (wait will block)
     238                        //     1p      : fulfilled (wait won't block)
     239                        //     2p      : in progress ()
     240                        //     3p      : abandoned, server should delete
     241                        // any oneshot : a context has been setup to wait, a thread could wait on it
     242                        struct oneshot * volatile ptr;
     243                };
     244
     245                static inline {
     246                        void  ?{}(future_t & this) {
     247                                this.ptr = 0p;
     248                        }
     249
     250                        void ^?{}(future_t &) {}
     251
     252                        void reset(future_t & this) {
     253                                // needs to be in 0p or 1p
     254                                __atomic_exchange_n( &this.ptr, 0p, __ATOMIC_SEQ_CST);
     255                        }
     256
     257                        // check if the future is available
     258                        bool available( future_t & this ) {
     259                                return this.ptr == 1p;
     260                        }
     261
     262                        // Prepare the future to be waited on
     263                        // intented to be use by wait, wait_any, waitfor, etc. rather than used directly
     264                        bool setup( future_t & this, oneshot & wait_ctx ) {
     265                                /* paranoid */ verify( wait_ctx.ptr == 0p );
     266                                // The future needs to set the wait context
     267                                for() {
     268                                        struct oneshot * expected = this.ptr;
     269                                        // Is the future already fulfilled?
     270                                        if(expected == 1p) return false; // Yes, just return false (didn't block)
     271
     272                                        // The future is not fulfilled, try to setup the wait context
     273                                        /* paranoid */ verify( expected == 0p );
     274                                        if(__atomic_compare_exchange_n(&this.ptr, &expected, &wait_ctx, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
     275                                                return true;
     276                                        }
     277                                }
     278                        }
     279
     280                        // Stop waiting on a future
     281                        // When multiple futures are waited for together in "any of" pattern
     282                        // futures that weren't fulfilled before the thread woke up
     283                        // should retract the wait ctx
     284                        // intented to be use by wait, wait_any, waitfor, etc. rather than used directly
     285                        void retract( future_t & this, oneshot & wait_ctx ) {
     286                                // Remove the wait context
     287                                struct oneshot * got = __atomic_exchange_n( &this.ptr, 0p, __ATOMIC_SEQ_CST);
     288
     289                                // got == 0p: future was never actually setup, just return
     290                                if( got == 0p ) return;
     291
     292                                // got == wait_ctx: since fulfil does an atomic_swap,
     293                                // if we got back the original then no one else saw context
     294                                // It is safe to delete (which could happen after the return)
     295                                if( got == &wait_ctx ) return;
     296
     297                                // got == 1p: the future is ready and the context was fully consumed
     298                                // the server won't use the pointer again
     299                                // It is safe to delete (which could happen after the return)
     300                                if( got == 1p ) return;
     301
     302                                // got == 2p: the future is ready but the context hasn't fully been consumed
     303                                // spin until it is safe to move on
     304                                if( got == 2p ) {
     305                                        while( this.ptr != 1p ) Pause();
     306                                        return;
     307                                }
     308
     309                                // got == any thing else, something wen't wrong here, abort
     310                                abort("Future in unexpected state");
     311                        }
     312
     313                        // Mark the future as abandoned, meaning it will be deleted by the server
     314                        bool abandon( future_t & this ) {
     315                                /* paranoid */ verify( this.ptr != 3p );
     316
     317                                // Mark the future as abandonned
     318                                struct oneshot * got = __atomic_exchange_n( &this.ptr, 3p, __ATOMIC_SEQ_CST);
     319
     320                                // If the future isn't already fulfilled, let the server delete it
     321                                if( got == 0p ) return false;
     322
     323                                // got == 2p: the future is ready but the context hasn't fully been consumed
     324                                // spin until it is safe to move on
     325                                if( got == 2p ) {
     326                                        while( this.ptr != 1p ) Pause();
     327                                        got = 1p;
     328                                }
     329
     330                                // The future is completed delete it now
     331                                /* paranoid */ verify( this.ptr != 1p );
     332                                free( &this );
     333                                return true;
     334                        }
     335
     336                        // from the server side, mark the future as fulfilled
     337                        // delete it if needed
     338                        bool fulfil( future_t & this ) {
     339                                for() {
     340                                        struct oneshot * expected = this.ptr;
     341                                        // was this abandoned?
     342                                        #if defined(__GNUC__) && __GNUC__ >= 7
     343                                                #pragma GCC diagnostic push
     344                                                #pragma GCC diagnostic ignored "-Wfree-nonheap-object"
     345                                        #endif
     346                                                if( expected == 3p ) { free( &this ); return false; }
     347                                        #if defined(__GNUC__) && __GNUC__ >= 7
     348                                                #pragma GCC diagnostic pop
     349                                        #endif
     350
     351                                        /* paranoid */ verify( expected != 1p ); // Future is already fulfilled, should not happen
     352                                        /* paranoid */ verify( expected != 2p ); // Future is bein fulfilled by someone else, this is even less supported then the previous case.
     353
     354                                        // If there is a wait context, we need to consume it and mark it as consumed after
     355                                        // If there is no context then we can skip the in progress phase
     356                                        struct oneshot * want = expected == 0p ? 1p : 2p;
     357                                        if(__atomic_compare_exchange_n(&this.ptr, &expected, want, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
     358                                                if( expected == 0p ) { /* paranoid */ verify( this.ptr == 1p); return false; }
     359                                                bool ret = post( *expected );
     360                                                __atomic_store_n( &this.ptr, 1p, __ATOMIC_SEQ_CST);
     361                                                return ret;
     362                                        }
     363                                }
     364
     365                        }
     366
     367                        // Wait for the future to be fulfilled
     368                        bool wait( future_t & this ) {
     369                                oneshot temp;
     370                                if( !setup(this, temp) ) return false;
     371
     372                                // Wait context is setup, just wait on it
     373                                bool ret = wait( temp );
     374
     375                                // Wait for the future to tru
     376                                while( this.ptr == 2p ) Pause();
     377                                // Make sure the state makes sense
     378                                // Should be fulfilled, could be in progress but it's out of date if so
     379                                // since if that is the case, the oneshot was fulfilled (unparking this thread)
     380                                // and the oneshot should not be needed any more
     381                                __attribute__((unused)) struct oneshot * was = this.ptr;
     382                                /* paranoid */ verifyf( was == 1p, "Expected this.ptr to be 1p, was %p\n", was );
     383
     384                                // Mark the future as fulfilled, to be consistent
     385                                // with potential calls to avail
     386                                // this.ptr = 1p;
     387                                return ret;
     388                        }
     389                }
     390
    136391                //-----------------------------------------------------------------------
    137392                // Statics call at the end of each thread to register statistics
  • libcfa/src/concurrency/kernel/startup.cfa

    r342af53 r8e4aa05  
    2222extern "C" {
    2323      #include <limits.h>       // PTHREAD_STACK_MIN
     24        #include <sys/eventfd.h>  // eventfd
    2425      #include <sys/mman.h>     // mprotect
    2526      #include <sys/resource.h> // getrlimit
     
    8990extern void __kernel_alarm_startup(void);
    9091extern void __kernel_alarm_shutdown(void);
    91 extern void __kernel_io_startup (void);
    92 extern void __kernel_io_shutdown(void);
    9392
    9493//-----------------------------------------------------------------------------
     
    102101KERNEL_STORAGE($thread,              mainThread);
    103102KERNEL_STORAGE(__stack_t,            mainThreadCtx);
    104 KERNEL_STORAGE(io_context,           mainPollerThread);
    105103KERNEL_STORAGE(__scheduler_RWLock_t, __scheduler_lock);
    106104#if !defined(__CFA_NO_STATISTICS__)
     
    198196
    199197        void ?{}(processor & this) with( this ) {
    200                 ( this.idle ){};
    201                 ( this.terminated ){ 0 };
     198                ( this.terminated ){};
    202199                ( this.runner ){};
    203200                init( this, "Main Processor", *mainCluster );
     
    226223        __kernel_alarm_startup();
    227224
    228         // Start IO
    229         __kernel_io_startup();
    230 
    231225        // Add the main thread to the ready queue
    232226        // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread
     
    241235        // THE SYSTEM IS NOW COMPLETELY RUNNING
    242236
    243 
    244         // SKULLDUGGERY: The constructor for the mainCluster will call alloc with a dimension of 0
    245         // malloc *can* return a non-null value, we should free it if that is the case
    246         free( mainCluster->io.ctxs );
    247 
    248         // Now that the system is up, finish creating systems that need threading
    249         mainCluster->io.ctxs = (io_context *)&storage_mainPollerThread;
    250         mainCluster->io.cnt  = 1;
    251         (*mainCluster->io.ctxs){ *mainCluster };
    252 
    253237        __cfadbg_print_safe(runtime_core, "Kernel : Started\n--------------------------------------------------\n\n");
    254238
     
    260244
    261245static void __kernel_shutdown(void) {
    262         //Before we start shutting things down, wait for systems that need threading to shutdown
    263         ^(*mainCluster->io.ctxs){};
    264         mainCluster->io.cnt  = 0;
    265         mainCluster->io.ctxs = 0p;
    266 
    267246        /* paranoid */ verify( __preemption_enabled() );
    268247        disable_interrupts();
     
    282261        // Disable preemption
    283262        __kernel_alarm_shutdown();
    284 
    285         // Stop IO
    286         __kernel_io_shutdown();
    287263
    288264        // Destroy the main processor and its context in reverse order of construction
     
    484460        pending_preemption = false;
    485461
     462        this.io.ctx = 0p;
     463        this.io.pending = false;
     464        this.io.dirty   = false;
     465
     466        this.idle = eventfd(0, 0);
     467        if (idle < 0) {
     468                abort("KERNEL ERROR: PROCESSOR EVENTFD - %s\n", strerror(errno));
     469        }
     470
    486471        #if !defined(__CFA_NO_STATISTICS__)
    487472                print_stats = 0;
     
    524509        // Finally we don't need the read_lock any more
    525510        unregister((__processor_id_t*)&this);
     511
     512        close(this.idle);
    526513}
    527514
    528515void ?{}(processor & this, const char name[], cluster & _cltr) {
    529         ( this.idle ){};
    530         ( this.terminated ){ 0 };
     516        ( this.terminated ){};
    531517        ( this.runner ){};
    532518
     
    549535                __wake_proc( &this );
    550536
    551                 P( terminated );
     537                wait( terminated );
    552538                /* paranoid */ verify( active_processor() != &this);
    553539        }
     
    582568        threads{ __get };
    583569
     570        io.arbiter = create();
     571        io.params = io_params;
     572
    584573        doregister(this);
    585574
     
    594583        ready_mutate_unlock( last_size );
    595584        enable_interrupts_noPoll(); // Don't poll, could be in main cluster
    596 
    597 
    598         this.io.cnt  = num_io;
    599         this.io.ctxs = aalloc(num_io);
    600         for(i; this.io.cnt) {
    601                 (this.io.ctxs[i]){ this, io_params };
    602         }
    603585}
    604586
    605587void ^?{}(cluster & this) {
    606         for(i; this.io.cnt) {
    607                 ^(this.io.ctxs[i]){ true };
    608         }
    609         free(this.io.ctxs);
     588        destroy(this.io.arbiter);
    610589
    611590        // Lock the RWlock so no-one pushes/pops while we are changing the queue
     
    736715}
    737716
    738 
    739717#if defined(__CFA_WITH_VERIFY__)
    740718static bool verify_fwd_bck_rng(void) {
  • libcfa/src/concurrency/kernel_private.hfa

    r342af53 r8e4aa05  
    7777//-----------------------------------------------------------------------------
    7878// I/O
    79 void ^?{}(io_context & this, bool );
     79$io_arbiter * create(void);
     80void destroy($io_arbiter *);
    8081
    8182//=======================================================================
  • libcfa/src/concurrency/locks.cfa

    r342af53 r8e4aa05  
     1//
     2// Cforall Version 1.0.0 Copyright (C) 2021 University of Waterloo
     3//
     4// The contents of this file are covered under the licence agreement in the
     5// file "LICENCE" distributed with Cforall.
     6//
     7// locks.hfa -- LIBCFATHREAD
     8// Runtime locks that used with the runtime thread system.
     9//
     10// Author           : Colby Alexander Parsons
     11// Created On       : Thu Jan 21 19:46:50 2021
     12// Last Modified By :
     13// Last Modified On :
     14// Update Count     :
     15//
     16
     17#define __cforall_thread__
     18
    119#include "locks.hfa"
    220#include "kernel_private.hfa"
     
    725//-----------------------------------------------------------------------------
    826// info_thread
    9 forall(dtype L | is_blocking_lock(L)) {
     27forall(L & | is_blocking_lock(L)) {
    1028        struct info_thread {
    1129                // used to put info_thread on a dl queue (aka sequence)
     
    5674
    5775void ^?{}( blocking_lock & this ) {}
    58 void  ?{}( single_acquisition_lock & this ) {((blocking_lock &)this){ false, false };}
    59 void ^?{}( single_acquisition_lock & this ) {}
    60 void  ?{}( owner_lock & this ) {((blocking_lock &)this){ true, true };}
    61 void ^?{}( owner_lock & this ) {}
    62 void  ?{}( multiple_acquisition_lock & this ) {((blocking_lock &)this){ true, false };}
    63 void ^?{}( multiple_acquisition_lock & this ) {}
     76
    6477
    6578void lock( blocking_lock & this ) with( this ) {
     
    170183
    171184//-----------------------------------------------------------------------------
    172 // Overloaded routines for traits
    173 // These routines are temporary until an inheritance bug is fixed
    174 void   lock      ( single_acquisition_lock & this ) { lock   ( (blocking_lock &)this ); }
    175 void   unlock    ( single_acquisition_lock & this ) { unlock ( (blocking_lock &)this ); }
    176 void   on_wait   ( single_acquisition_lock & this ) { on_wait( (blocking_lock &)this ); }
    177 void   on_notify ( single_acquisition_lock & this, struct $thread * t ) { on_notify( (blocking_lock &)this, t ); }
    178 void   set_recursion_count( single_acquisition_lock & this, size_t recursion ) { set_recursion_count( (blocking_lock &)this, recursion ); }
    179 size_t get_recursion_count( single_acquisition_lock & this ) { return get_recursion_count( (blocking_lock &)this ); }
    180 
    181 void   lock     ( owner_lock & this ) { lock   ( (blocking_lock &)this ); }
    182 void   unlock   ( owner_lock & this ) { unlock ( (blocking_lock &)this ); }
    183 void   on_wait  ( owner_lock & this ) { on_wait( (blocking_lock &)this ); }
    184 void   on_notify( owner_lock & this, struct $thread * t ) { on_notify( (blocking_lock &)this, t ); }
    185 void   set_recursion_count( owner_lock & this, size_t recursion ) { set_recursion_count( (blocking_lock &)this, recursion ); }
    186 size_t get_recursion_count( owner_lock & this ) { return get_recursion_count( (blocking_lock &)this ); }
    187 
    188 void   lock     ( multiple_acquisition_lock & this ) { lock   ( (blocking_lock &)this ); }
    189 void   unlock   ( multiple_acquisition_lock & this ) { unlock ( (blocking_lock &)this ); }
    190 void   on_wait  ( multiple_acquisition_lock & this ) { on_wait( (blocking_lock &)this ); }
    191 void   on_notify( multiple_acquisition_lock & this, struct $thread * t ){ on_notify( (blocking_lock &)this, t ); }
    192 void   set_recursion_count( multiple_acquisition_lock & this, size_t recursion ){ set_recursion_count( (blocking_lock &)this, recursion ); }
    193 size_t get_recursion_count( multiple_acquisition_lock & this ){ return get_recursion_count( (blocking_lock &)this ); }
    194 
    195 //-----------------------------------------------------------------------------
    196185// alarm node wrapper
    197 forall(dtype L | is_blocking_lock(L)) {
     186forall(L & | is_blocking_lock(L)) {
    198187        struct alarm_node_wrap {
    199188                alarm_node_t alarm_node;
     
    239228//-----------------------------------------------------------------------------
    240229// condition variable
    241 forall(dtype L | is_blocking_lock(L)) {
     230forall(L & | is_blocking_lock(L)) {
    242231
    243232        void ?{}( condition_variable(L) & this ){
     
    356345        bool wait( condition_variable(L) & this, L & l, uintptr_t info, Time time         ) with(this) { WAIT_TIME( info, &l , time ) }
    357346}
     347
     348//-----------------------------------------------------------------------------
     349// Semaphore
     350void  ?{}( semaphore & this, int count = 1 ) {
     351        (this.lock){};
     352        this.count = count;
     353        (this.waiting){};
     354}
     355void ^?{}(semaphore & this) {}
     356
     357bool P(semaphore & this) with( this ){
     358        lock( lock __cfaabi_dbg_ctx2 );
     359        count -= 1;
     360        if ( count < 0 ) {
     361                // queue current task
     362                append( waiting, active_thread() );
     363
     364                // atomically release spin lock and block
     365                unlock( lock );
     366                park();
     367                return true;
     368        }
     369        else {
     370            unlock( lock );
     371            return false;
     372        }
     373}
     374
     375bool V(semaphore & this) with( this ) {
     376        $thread * thrd = 0p;
     377        lock( lock __cfaabi_dbg_ctx2 );
     378        count += 1;
     379        if ( count <= 0 ) {
     380                // remove task at head of waiting list
     381                thrd = pop_head( waiting );
     382        }
     383
     384        unlock( lock );
     385
     386        // make new owner
     387        unpark( thrd );
     388
     389        return thrd != 0p;
     390}
     391
     392bool V(semaphore & this, unsigned diff) with( this ) {
     393        $thread * thrd = 0p;
     394        lock( lock __cfaabi_dbg_ctx2 );
     395        int release = max(-count, (int)diff);
     396        count += diff;
     397        for(release) {
     398                unpark( pop_head( waiting ) );
     399        }
     400
     401        unlock( lock );
     402
     403        return thrd != 0p;
     404}
  • libcfa/src/concurrency/locks.hfa

    r342af53 r8e4aa05  
     1//
     2// Cforall Version 1.0.0 Copyright (C) 2021 University of Waterloo
     3//
     4// The contents of this file are covered under the licence agreement in the
     5// file "LICENCE" distributed with Cforall.
     6//
     7// locks.hfa -- PUBLIC
     8// Runtime locks that used with the runtime thread system.
     9//
     10// Author           : Colby Alexander Parsons
     11// Created On       : Thu Jan 21 19:46:50 2021
     12// Last Modified By :
     13// Last Modified On :
     14// Update Count     :
     15//
     16
    117#pragma once
    218
    319#include <stdbool.h>
    420
    5 #include "bits/locks.hfa"
    6 #include "bits/sequence.hfa"
    7 
    8 #include "invoke.h"
     21#include "bits/weakso_locks.hfa"
    922
    1023#include "time_t.hfa"
    1124#include "time.hfa"
    1225
     26//----------
     27struct single_acquisition_lock {
     28        inline blocking_lock;
     29};
     30
     31static inline void  ?{}( single_acquisition_lock & this ) {((blocking_lock &)this){ false, false };}
     32static inline void ^?{}( single_acquisition_lock & this ) {}
     33static inline void   lock      ( single_acquisition_lock & this ) { lock   ( (blocking_lock &)this ); }
     34static inline void   unlock    ( single_acquisition_lock & this ) { unlock ( (blocking_lock &)this ); }
     35static inline void   on_wait   ( single_acquisition_lock & this ) { on_wait( (blocking_lock &)this ); }
     36static inline void   on_notify ( single_acquisition_lock & this, struct $thread * t ) { on_notify( (blocking_lock &)this, t ); }
     37static inline void   set_recursion_count( single_acquisition_lock & this, size_t recursion ) { set_recursion_count( (blocking_lock &)this, recursion ); }
     38static inline size_t get_recursion_count( single_acquisition_lock & this ) { return get_recursion_count( (blocking_lock &)this ); }
     39
     40//----------
     41struct owner_lock {
     42        inline blocking_lock;
     43};
     44
     45static inline void  ?{}( owner_lock & this ) {((blocking_lock &)this){ true, true };}
     46static inline void ^?{}( owner_lock & this ) {}
     47static inline void   lock     ( owner_lock & this ) { lock   ( (blocking_lock &)this ); }
     48static inline void   unlock   ( owner_lock & this ) { unlock ( (blocking_lock &)this ); }
     49static inline void   on_wait  ( owner_lock & this ) { on_wait( (blocking_lock &)this ); }
     50static inline void   on_notify( owner_lock & this, struct $thread * t ) { on_notify( (blocking_lock &)this, t ); }
     51static inline void   set_recursion_count( owner_lock & this, size_t recursion ) { set_recursion_count( (blocking_lock &)this, recursion ); }
     52static inline size_t get_recursion_count( owner_lock & this ) { return get_recursion_count( (blocking_lock &)this ); }
     53
    1354//-----------------------------------------------------------------------------
    1455// is_blocking_lock
    15 trait is_blocking_lock(dtype L | sized(L)) {
     56trait is_blocking_lock(L & | sized(L)) {
    1657        // For synchronization locks to use when acquiring
    1758        void on_notify( L &, struct $thread * );
     
    3172// the info thread is a wrapper around a thread used
    3273// to store extra data for use in the condition variable
    33 forall(dtype L | is_blocking_lock(L)) {
     74forall(L & | is_blocking_lock(L)) {
    3475        struct info_thread;
    3576
     
    4081
    4182//-----------------------------------------------------------------------------
    42 // Blocking Locks
    43 struct blocking_lock {
    44         // Spin lock used for mutual exclusion
    45         __spinlock_t lock;
    46 
    47         // List of blocked threads
    48         Sequence( $thread ) blocked_threads;
    49 
    50         // Count of current blocked threads
    51         size_t wait_count;
    52 
    53         // Flag if the lock allows multiple acquisition
    54         bool multi_acquisition;
    55 
    56         // Flag if lock can be released by non owner
    57         bool strict_owner;
    58 
    59         // Current thread owning the lock
    60         struct $thread * owner;
    61 
    62         // Number of recursion level
    63         size_t recursion_count;
    64 };
    65 
    66 struct single_acquisition_lock {
    67         inline blocking_lock;
    68 };
    69 
    70 struct owner_lock {
    71         inline blocking_lock;
    72 };
    73 
    74 struct multiple_acquisition_lock {
    75         inline blocking_lock;
    76 };
    77 
    78 void  ?{}( blocking_lock & this, bool multi_acquisition, bool strict_owner );
    79 void ^?{}( blocking_lock & this );
    80 
    81 void  ?{}( single_acquisition_lock & this );
    82 void ^?{}( single_acquisition_lock & this );
    83 
    84 void  ?{}( owner_lock & this );
    85 void ^?{}( owner_lock & this );
    86 
    87 void  ?{}( multiple_acquisition_lock & this );
    88 void ^?{}( multiple_acquisition_lock & this );
    89 
    90 void lock( blocking_lock & this );
    91 bool try_lock( blocking_lock & this );
    92 void unlock( blocking_lock & this );
    93 void on_notify( blocking_lock & this, struct $thread * t );
    94 void on_wait( blocking_lock & this );
    95 size_t wait_count( blocking_lock & this );
    96 void set_recursion_count( blocking_lock & this, size_t recursion );
    97 size_t get_recursion_count( blocking_lock & this );
    98 
    99 void lock( single_acquisition_lock & this );
    100 void unlock( single_acquisition_lock & this );
    101 void on_notify( single_acquisition_lock & this, struct $thread * t );
    102 void on_wait( single_acquisition_lock & this );
    103 void set_recursion_count( single_acquisition_lock & this, size_t recursion );
    104 size_t get_recursion_count( single_acquisition_lock & this );
    105 
    106 void lock( owner_lock & this );
    107 void unlock( owner_lock & this );
    108 void on_notify( owner_lock & this, struct $thread * t );
    109 void on_wait( owner_lock & this );
    110 void set_recursion_count( owner_lock & this, size_t recursion );
    111 size_t get_recursion_count( owner_lock & this );
    112 
    113 void lock( multiple_acquisition_lock & this );
    114 void unlock( multiple_acquisition_lock & this );
    115 void on_notify( multiple_acquisition_lock & this, struct $thread * t );
    116 void on_wait( multiple_acquisition_lock & this );
    117 void set_recursion_count( multiple_acquisition_lock & this, size_t recursion );
    118 size_t get_recursion_count( multiple_acquisition_lock & this );
    119 
    120 //-----------------------------------------------------------------------------
    12183// Synchronization Locks
    122 forall(dtype L | is_blocking_lock(L)) {
     84forall(L & | is_blocking_lock(L)) {
    12385        struct condition_variable {
    12486                // Spin lock used for mutual exclusion
     
    157119        bool wait( condition_variable(L) & this, L & l, uintptr_t info, Time time );
    158120}
     121
     122//-----------------------------------------------------------------------------
     123// Semaphore
     124struct semaphore {
     125        __spinlock_t lock;
     126        int count;
     127        __queue_t($thread) waiting;
     128};
     129
     130void  ?{}(semaphore & this, int count = 1);
     131void ^?{}(semaphore & this);
     132bool   P (semaphore & this);
     133bool   V (semaphore & this);
     134bool   V (semaphore & this, unsigned count);
  • libcfa/src/concurrency/monitor.cfa

    r342af53 r8e4aa05  
    5050static inline [$thread *, int] search_entry_queue( const __waitfor_mask_t &, $monitor * monitors [], __lock_size_t count );
    5151
    52 forall(dtype T | sized( T ))
     52forall(T & | sized( T ))
    5353static inline __lock_size_t insert_unique( T * array [], __lock_size_t & size, T * val );
    5454static inline __lock_size_t count_max    ( const __waitfor_mask_t & mask );
     
    949949}
    950950
    951 forall(dtype T | sized( T ))
     951forall(T & | sized( T ))
    952952static inline __lock_size_t insert_unique( T * array [], __lock_size_t & size, T * val ) {
    953953        if( !val ) return size;
  • libcfa/src/concurrency/monitor.hfa

    r342af53 r8e4aa05  
    2222#include "stdlib.hfa"
    2323
    24 trait is_monitor(dtype T) {
     24trait is_monitor(T &) {
    2525        $monitor * get_monitor( T & );
    2626        void ^?{}( T & mutex );
     
    5959void ^?{}( monitor_dtor_guard_t & this );
    6060
    61 static inline forall( dtype T | sized(T) | { void ^?{}( T & mutex ); } )
     61static inline forall( T & | sized(T) | { void ^?{}( T & mutex ); } )
    6262void delete( T * th ) {
    63         ^(*th){};
     63        if(th) ^(*th){};
    6464        free( th );
    6565}
  • libcfa/src/concurrency/mutex.cfa

    r342af53 r8e4aa05  
    164164}
    165165
    166 forall(dtype L | is_lock(L))
     166forall(L & | is_lock(L))
    167167void wait(condition_variable & this, L & l) {
    168168        lock( this.lock __cfaabi_dbg_ctx2 );
     
    176176//-----------------------------------------------------------------------------
    177177// Scopes
    178 forall(dtype L | is_lock(L))
     178forall(L & | is_lock(L))
    179179void lock_all  ( L * locks[], size_t count) {
    180180        // Sort locks based on addresses
     
    188188}
    189189
    190 forall(dtype L | is_lock(L))
     190forall(L & | is_lock(L))
    191191void unlock_all( L * locks[], size_t count) {
    192192        // Lock all
  • libcfa/src/concurrency/mutex.hfa

    r342af53 r8e4aa05  
    4242};
    4343
    44 void ?{}(mutex_lock & this);
    45 void ^?{}(mutex_lock & this);
    46 void lock(mutex_lock & this);
    47 bool try_lock(mutex_lock & this);
    48 void unlock(mutex_lock & this);
     44void ?{}(mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
     45void ^?{}(mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
     46void lock(mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
     47bool try_lock(mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
     48void unlock(mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
    4949
    5050// Exclusive lock - recursive
     
    6464};
    6565
    66 void ?{}(recursive_mutex_lock & this);
    67 void ^?{}(recursive_mutex_lock & this);
    68 void lock(recursive_mutex_lock & this);
    69 bool try_lock(recursive_mutex_lock & this);
    70 void unlock(recursive_mutex_lock & this);
     66void ?{}(recursive_mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
     67void ^?{}(recursive_mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
     68void lock(recursive_mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
     69bool try_lock(recursive_mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
     70void unlock(recursive_mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
    7171
    72 trait is_lock(dtype L | sized(L)) {
     72trait is_lock(L & | sized(L)) {
    7373        void lock  (L &);
    7474        void unlock(L &);
     
    8686};
    8787
    88 void ?{}(condition_variable & this);
    89 void ^?{}(condition_variable & this);
     88void ?{}(condition_variable & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
     89void ^?{}(condition_variable & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
    9090
    91 void notify_one(condition_variable & this);
    92 void notify_all(condition_variable & this);
     91void notify_one(condition_variable & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
     92void notify_all(condition_variable & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
    9393
    94 void wait(condition_variable & this);
     94void wait(condition_variable & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
    9595
    96 forall(dtype L | is_lock(L))
    97 void wait(condition_variable & this, L & l);
     96forall(L & | is_lock(L))
     97void wait(condition_variable & this, L & l) __attribute__((deprecated("use concurrency/locks.hfa instead")));
    9898
    9999//-----------------------------------------------------------------------------
    100100// Scopes
    101 forall(dtype L | is_lock(L)) {
     101forall(L & | is_lock(L)) {
    102102        #if !defined( __TUPLE_ARRAYS_EXIST__ )
    103103        void lock  ( L * locks [], size_t count);
  • libcfa/src/concurrency/preemption.cfa

    r342af53 r8e4aa05  
    424424static void timeout( $thread * this ) {
    425425        unpark( this );
     426}
     427
     428void __disable_interrupts_hard() {
     429        sigset_t oldset;
     430        int ret;
     431        ret = pthread_sigmask(0, ( const sigset_t * ) 0p, &oldset);  // workaround trac#208: cast should be unnecessary
     432        if(ret != 0) { abort("ERROR sigprocmask returned %d", ret); }
     433
     434        ret = sigismember(&oldset, SIGUSR1);
     435        if(ret <  0) { abort("ERROR sigismember returned %d", ret); }
     436        if(ret == 1) { abort("ERROR SIGUSR1 is disabled"); }
     437
     438        ret = sigismember(&oldset, SIGALRM);
     439        if(ret <  0) { abort("ERROR sigismember returned %d", ret); }
     440        if(ret == 0) { abort("ERROR SIGALRM is enabled"); }
     441
     442        signal_block( SIGUSR1 );
     443}
     444
     445void __enable_interrupts_hard() {
     446        signal_unblock( SIGUSR1 );
     447
     448        sigset_t oldset;
     449        int ret;
     450        ret = pthread_sigmask(0, ( const sigset_t * ) 0p, &oldset);  // workaround trac#208: cast should be unnecessary
     451        if(ret != 0) { abort("ERROR sigprocmask returned %d", ret); }
     452
     453        ret = sigismember(&oldset, SIGUSR1);
     454        if(ret <  0) { abort("ERROR sigismember returned %d", ret); }
     455        if(ret == 1) { abort("ERROR SIGUSR1 is disabled"); }
     456
     457        ret = sigismember(&oldset, SIGALRM);
     458        if(ret <  0) { abort("ERROR sigismember returned %d", ret); }
     459        if(ret == 0) { abort("ERROR SIGALRM is enabled"); }
    426460}
    427461
     
    551585
    552586        // Setup proper signal handlers
    553         __cfaabi_sigaction( SIGUSR1, sigHandler_ctxSwitch, SA_SIGINFO | SA_RESTART ); // __cfactx_switch handler
    554         __cfaabi_sigaction( SIGALRM, sigHandler_alarm    , SA_SIGINFO | SA_RESTART ); // debug handler
     587        __cfaabi_sigaction( SIGUSR1, sigHandler_ctxSwitch, SA_SIGINFO ); // __cfactx_switch handler
     588        __cfaabi_sigaction( SIGALRM, sigHandler_alarm    , SA_SIGINFO ); // debug handler
    555589
    556590        signal_block( SIGALRM );
     
    580614
    581615        __cfaabi_dbg_print_safe( "Kernel : Preemption stopped\n" );
     616}
     617
     618// Prevent preemption since we are about to start terminating things
     619void __kernel_abort_lock(void) {
     620        signal_block( SIGUSR1 );
    582621}
    583622
  • libcfa/src/concurrency/ready_queue.cfa

    r342af53 r8e4aa05  
    330330        #if defined(BIAS)
    331331                // Don't bother trying locally too much
    332                 int local_tries = 8;
    333332                preferred = kernelTLS().this_processor->id * 4;
    334333        #endif
  • libcfa/src/concurrency/stats.cfa

    r342af53 r8e4aa05  
    2525
    2626                #if defined(CFA_HAVE_LINUX_IO_URING_H)
    27                         stats->io.submit_q.submit_avg.rdy = 0;
    28                         stats->io.submit_q.submit_avg.csm = 0;
    29                         stats->io.submit_q.submit_avg.cnt = 0;
    30                         stats->io.submit_q.look_avg.val   = 0;
    31                         stats->io.submit_q.look_avg.cnt   = 0;
    32                         stats->io.submit_q.look_avg.block = 0;
    33                         stats->io.submit_q.alloc_avg.val   = 0;
    34                         stats->io.submit_q.alloc_avg.cnt   = 0;
    35                         stats->io.submit_q.alloc_avg.block = 0;
    36                         stats->io.submit_q.helped = 0;
    37                         stats->io.submit_q.leader = 0;
    38                         stats->io.submit_q.busy   = 0;
    39                         stats->io.complete_q.completed_avg.val = 0;
    40                         stats->io.complete_q.completed_avg.cnt = 0;
    41                         stats->io.complete_q.blocks = 0;
     27                        stats->io.alloc.fast        = 0;
     28                        stats->io.alloc.slow        = 0;
     29                        stats->io.alloc.fail        = 0;
     30                        stats->io.alloc.revoke      = 0;
     31                        stats->io.alloc.block       = 0;
     32                        stats->io.submit.fast       = 0;
     33                        stats->io.submit.slow       = 0;
     34                        stats->io.flush.external    = 0;
     35                        stats->io.calls.flush       = 0;
     36                        stats->io.calls.submitted   = 0;
     37                        stats->io.calls.drain       = 0;
     38                        stats->io.calls.completed   = 0;
     39                        stats->io.calls.errors.busy = 0;
     40                        stats->io.poller.sleeps     = 0;
    4241                #endif
    4342        }
     
    6059
    6160                #if defined(CFA_HAVE_LINUX_IO_URING_H)
    62                         __atomic_fetch_add( &cltr->io.submit_q.submit_avg.rdy     , proc->io.submit_q.submit_avg.rdy     , __ATOMIC_SEQ_CST ); proc->io.submit_q.submit_avg.rdy      = 0;
    63                         __atomic_fetch_add( &cltr->io.submit_q.submit_avg.csm     , proc->io.submit_q.submit_avg.csm     , __ATOMIC_SEQ_CST ); proc->io.submit_q.submit_avg.csm      = 0;
    64                         __atomic_fetch_add( &cltr->io.submit_q.submit_avg.avl     , proc->io.submit_q.submit_avg.avl     , __ATOMIC_SEQ_CST ); proc->io.submit_q.submit_avg.avl      = 0;
    65                         __atomic_fetch_add( &cltr->io.submit_q.submit_avg.cnt     , proc->io.submit_q.submit_avg.cnt     , __ATOMIC_SEQ_CST ); proc->io.submit_q.submit_avg.cnt      = 0;
    66                         __atomic_fetch_add( &cltr->io.submit_q.look_avg.val       , proc->io.submit_q.look_avg.val       , __ATOMIC_SEQ_CST ); proc->io.submit_q.look_avg.val        = 0;
    67                         __atomic_fetch_add( &cltr->io.submit_q.look_avg.cnt       , proc->io.submit_q.look_avg.cnt       , __ATOMIC_SEQ_CST ); proc->io.submit_q.look_avg.cnt        = 0;
    68                         __atomic_fetch_add( &cltr->io.submit_q.look_avg.block     , proc->io.submit_q.look_avg.block     , __ATOMIC_SEQ_CST ); proc->io.submit_q.look_avg.block      = 0;
    69                         __atomic_fetch_add( &cltr->io.submit_q.alloc_avg.val      , proc->io.submit_q.alloc_avg.val      , __ATOMIC_SEQ_CST ); proc->io.submit_q.alloc_avg.val       = 0;
    70                         __atomic_fetch_add( &cltr->io.submit_q.alloc_avg.cnt      , proc->io.submit_q.alloc_avg.cnt      , __ATOMIC_SEQ_CST ); proc->io.submit_q.alloc_avg.cnt       = 0;
    71                         __atomic_fetch_add( &cltr->io.submit_q.alloc_avg.block    , proc->io.submit_q.alloc_avg.block    , __ATOMIC_SEQ_CST ); proc->io.submit_q.alloc_avg.block     = 0;
    72                         __atomic_fetch_add( &cltr->io.submit_q.helped             , proc->io.submit_q.helped             , __ATOMIC_SEQ_CST ); proc->io.submit_q.helped              = 0;
    73                         __atomic_fetch_add( &cltr->io.submit_q.leader             , proc->io.submit_q.leader             , __ATOMIC_SEQ_CST ); proc->io.submit_q.leader              = 0;
    74                         __atomic_fetch_add( &cltr->io.submit_q.busy               , proc->io.submit_q.busy               , __ATOMIC_SEQ_CST ); proc->io.submit_q.busy                = 0;
    75                         __atomic_fetch_add( &cltr->io.complete_q.completed_avg.val, proc->io.complete_q.completed_avg.val, __ATOMIC_SEQ_CST ); proc->io.complete_q.completed_avg.val = 0;
    76                         __atomic_fetch_add( &cltr->io.complete_q.completed_avg.cnt, proc->io.complete_q.completed_avg.cnt, __ATOMIC_SEQ_CST ); proc->io.complete_q.completed_avg.cnt = 0;
    77                         __atomic_fetch_add( &cltr->io.complete_q.blocks           , proc->io.complete_q.blocks           , __ATOMIC_SEQ_CST ); proc->io.complete_q.blocks            = 0;
     61                        __atomic_fetch_add( &cltr->io.alloc.fast       , proc->io.alloc.fast       , __ATOMIC_SEQ_CST ); proc->io.alloc.fast        = 0;
     62                        __atomic_fetch_add( &cltr->io.alloc.slow       , proc->io.alloc.slow       , __ATOMIC_SEQ_CST ); proc->io.alloc.slow        = 0;
     63                        __atomic_fetch_add( &cltr->io.alloc.fail       , proc->io.alloc.fail       , __ATOMIC_SEQ_CST ); proc->io.alloc.fail        = 0;
     64                        __atomic_fetch_add( &cltr->io.alloc.revoke     , proc->io.alloc.revoke     , __ATOMIC_SEQ_CST ); proc->io.alloc.revoke      = 0;
     65                        __atomic_fetch_add( &cltr->io.alloc.block      , proc->io.alloc.block      , __ATOMIC_SEQ_CST ); proc->io.alloc.block       = 0;
     66                        __atomic_fetch_add( &cltr->io.submit.fast      , proc->io.submit.fast      , __ATOMIC_SEQ_CST ); proc->io.submit.fast       = 0;
     67                        __atomic_fetch_add( &cltr->io.submit.slow      , proc->io.submit.slow      , __ATOMIC_SEQ_CST ); proc->io.submit.slow       = 0;
     68                        __atomic_fetch_add( &cltr->io.flush.external   , proc->io.flush.external   , __ATOMIC_SEQ_CST ); proc->io.flush.external    = 0;
     69                        __atomic_fetch_add( &cltr->io.calls.flush      , proc->io.calls.flush      , __ATOMIC_SEQ_CST ); proc->io.calls.flush       = 0;
     70                        __atomic_fetch_add( &cltr->io.calls.submitted  , proc->io.calls.submitted  , __ATOMIC_SEQ_CST ); proc->io.calls.submitted   = 0;
     71                        __atomic_fetch_add( &cltr->io.calls.drain      , proc->io.calls.drain      , __ATOMIC_SEQ_CST ); proc->io.calls.drain       = 0;
     72                        __atomic_fetch_add( &cltr->io.calls.completed  , proc->io.calls.completed  , __ATOMIC_SEQ_CST ); proc->io.calls.completed   = 0;
     73                        __atomic_fetch_add( &cltr->io.calls.errors.busy, proc->io.calls.errors.busy, __ATOMIC_SEQ_CST ); proc->io.calls.errors.busy = 0;
     74                        __atomic_fetch_add( &cltr->io.poller.sleeps    , proc->io.poller.sleeps    , __ATOMIC_SEQ_CST ); proc->io.poller.sleeps     = 0;
    7875                #endif
    7976        }
     
    8279
    8380                if( flags & CFA_STATS_READY_Q ) {
    84                         double push_sur = (100.0 * ((double)ready.pick.push.success) / ready.pick.push.attempt);
    85                         double pop_sur  = (100.0 * ((double)ready.pick.pop .success) / ready.pick.pop .attempt);
    86 
    8781                        double push_len = ((double)ready.pick.push.attempt) / ready.pick.push.success;
    8882                        double pop_len  = ((double)ready.pick.pop .attempt) / ready.pick.pop .success;
    89 
    90                         double lpush_sur = (100.0 * ((double)ready.pick.push.lsuccess) / ready.pick.push.local);
    91                         double lpop_sur  = (100.0 * ((double)ready.pick.pop .lsuccess) / ready.pick.pop .local);
    9283
    9384                        double lpush_len = ((double)ready.pick.push.local) / ready.pick.push.lsuccess;
     
    9687                        __cfaabi_bits_print_safe( STDOUT_FILENO,
    9788                                "----- %s \"%s\" (%p) - Ready Q Stats -----\n"
    98                                 "- total threads run      : %'15" PRIu64 "\n"
    99                                 "- total threads scheduled: %'15" PRIu64 "\n"
    100                                 "- push average probe len : %'18.2lf, %'18.2lf%% (%'15" PRIu64 " attempts)\n"
    101                                 "- pop  average probe len : %'18.2lf, %'18.2lf%% (%'15" PRIu64 " attempts)\n"
    102                                 "- local push avg prb len : %'18.2lf, %'18.2lf%% (%'15" PRIu64 " attempts)\n"
    103                                 "- local pop  avg prb len : %'18.2lf, %'18.2lf%% (%'15" PRIu64 " attempts)\n"
    104                                 "- thread migrations      : %'15" PRIu64 "\n"
    105                                 "- Idle Sleep -\n"
    106                                 "-- halts                 : %'15" PRIu64 "\n"
    107                                 "-- cancelled halts       : %'15" PRIu64 "\n"
    108                                 "-- schedule wake         : %'15" PRIu64 "\n"
    109                                 "-- wake on exit          : %'15" PRIu64 "\n"
     89                                "- total threads  : %'15" PRIu64 "run, %'15" PRIu64 "schd (%'" PRIu64 "mig )\n"
     90                                "- push avg probe : %'3.2lf, %'3.2lfl (%'15" PRIu64 " attempts, %'15" PRIu64 " locals)\n"
     91                                "- pop  avg probe : %'3.2lf, %'3.2lfl (%'15" PRIu64 " attempts, %'15" PRIu64 " locals)\n"
     92                                "- Idle Sleep     : %'15" PRIu64 "h, %'15" PRIu64 "c, %'15" PRIu64 "w, %'15" PRIu64 "e\n"
    11093                                "\n"
    11194                                , type, name, id
    11295                                , ready.pick.pop.success
    11396                                , ready.pick.push.success
    114                                 , push_len, push_sur, ready.pick.push.attempt
    115                                 , pop_len , pop_sur , ready.pick.pop .attempt
    116                                 , lpush_len, lpush_sur, ready.pick.push.local
    117                                 , lpop_len , lpop_sur , ready.pick.pop .local
    11897                                , ready.threads.migration
     98                                , push_len, lpush_len, ready.pick.push.attempt, ready.pick.push.local
     99                                , pop_len , lpop_len , ready.pick.pop .attempt, ready.pick.pop .local
    119100                                , ready.sleep.halts, ready.sleep.cancels, ready.sleep.wakes, ready.sleep.exits
    120101                        );
     
    123104                #if defined(CFA_HAVE_LINUX_IO_URING_H)
    124105                        if( flags & CFA_STATS_IO ) {
    125                                 double avgrdy = ((double)io.submit_q.submit_avg.rdy) / io.submit_q.submit_avg.cnt;
    126                                 double avgcsm = ((double)io.submit_q.submit_avg.csm) / io.submit_q.submit_avg.cnt;
     106                                uint64_t total_allocs = io.alloc.fast + io.alloc.slow;
     107                                double avgfasta = ((double)io.alloc.fast) / total_allocs;
    127108
    128                                 double lavgv = 0;
    129                                 double lavgb = 0;
    130                                 if(io.submit_q.look_avg.cnt != 0) {
    131                                         lavgv = ((double)io.submit_q.look_avg.val  ) / io.submit_q.look_avg.cnt;
    132                                         lavgb = ((double)io.submit_q.look_avg.block) / io.submit_q.look_avg.cnt;
    133                                 }
     109                                uint64_t total_submits = io.submit.fast + io.submit.slow;
     110                                double avgfasts = ((double)io.submit.fast) / total_submits;
    134111
    135                                 double aavgv = 0;
    136                                 double aavgb = 0;
    137                                 if(io.submit_q.alloc_avg.cnt != 0) {
    138                                         aavgv = ((double)io.submit_q.alloc_avg.val  ) / io.submit_q.alloc_avg.cnt;
    139                                         aavgb = ((double)io.submit_q.alloc_avg.block) / io.submit_q.alloc_avg.cnt;
    140                                 }
     112                                double avgsubs = ((double)io.calls.submitted) / io.calls.flush;
     113                                double avgcomp = ((double)io.calls.completed) / io.calls.drain;
    141114
    142115                                __cfaabi_bits_print_safe( STDOUT_FILENO,
    143116                                        "----- %s \"%s\" (%p) - I/O Stats -----\n"
    144                                         "- total submit calls     : %'15" PRIu64 "\n"
    145                                         "- avg ready entries      : %'18.2lf\n"
    146                                         "- avg submitted entries  : %'18.2lf\n"
    147                                         "- total helped entries   : %'15" PRIu64 "\n"
    148                                         "- total leader entries   : %'15" PRIu64 "\n"
    149                                         "- total busy submit      : %'15" PRIu64 "\n"
    150                                         "- total ready search     : %'15" PRIu64 "\n"
    151                                         "- avg ready search len   : %'18.2lf\n"
    152                                         "- avg ready search block : %'18.2lf\n"
    153                                         "- total alloc search     : %'15" PRIu64 "\n"
    154                                         "- avg alloc search len   : %'18.2lf\n"
    155                                         "- avg alloc search block : %'18.2lf\n"
    156                                         "- total wait calls       : %'15" PRIu64 "\n"
    157                                         "- avg completion/wait    : %'18.2lf\n"
    158                                         "- total completion blocks: %'15" PRIu64 "\n"
     117                                        "- total allocations : %'" PRIu64 "f, %'" PRIu64 "s (%'2.2lff) \n"
     118                                        "-     failures      : %'" PRIu64 "oom, %'" PRIu64 "rvk, %'" PRIu64 "blk\n"
     119                                        "- total submits     : %'" PRIu64 "f, %'" PRIu64 "s (%'2.2lf) \n"
     120                                        "- flush external    : %'" PRIu64 "\n"
     121                                        "- io_uring_enter    : %'" PRIu64 " (%'" PRIu64 ", %'" PRIu64 " EBUSY)\n"
     122                                        "-     submits       : %'" PRIu64 " (%'.2lf) \n"
     123                                        "-     completes     : %'" PRIu64 " (%'.2lf) \n"
     124                                        "- poller sleeping   : %'" PRIu64 "\n"
    159125                                        "\n"
    160126                                        , type,  name, id
    161                                         , io.submit_q.submit_avg.cnt
    162                                         , avgrdy, avgcsm
    163                                         , io.submit_q.helped, io.submit_q.leader, io.submit_q.busy
    164                                         , io.submit_q.look_avg.cnt
    165                                         , lavgv, lavgb
    166                                         , io.submit_q.alloc_avg.cnt
    167                                         , aavgv, aavgb
    168                                         , io.complete_q.completed_avg.cnt
    169                                         , ((double)io.complete_q.completed_avg.val) / io.complete_q.completed_avg.cnt
    170                                         , io.complete_q.blocks
     127                                        , io.alloc.fast, io.alloc.slow, avgfasta
     128                                        , io.alloc.fail, io.alloc.revoke, io.alloc.block
     129                                        , io.submit.fast, io.submit.slow, avgfasts
     130                                        , io.flush.external
     131                                        , io.calls.flush, io.calls.drain, io.calls.errors.busy
     132                                        , io.calls.submitted, avgsubs
     133                                        , io.calls.completed, avgcomp
     134                                        , io.poller.sleeps
    171135                                );
    172136                        }
  • libcfa/src/concurrency/stats.hfa

    r342af53 r8e4aa05  
    22
    33#include <stdint.h>
     4
     5enum {
     6        CFA_STATS_READY_Q  = 0x01,
     7        CFA_STATS_IO = 0x02,
     8};
    49
    510#if defined(__CFA_NO_STATISTICS__)
     
    914        static inline void __print_stats( struct __stats_t *, int, const char *, const char *, void * ) {}
    1015#else
    11         enum {
    12                 CFA_STATS_READY_Q  = 0x01,
    13                 #if defined(CFA_HAVE_LINUX_IO_URING_H)
    14                         CFA_STATS_IO = 0x02,
    15                 #endif
    16         };
    1716
    1817        struct __attribute__((aligned(64))) __stats_readQ_t {
     
    6766                struct __attribute__((aligned(64))) __stats_io_t{
    6867                        struct {
     68                                volatile uint64_t fast;
     69                                volatile uint64_t slow;
     70                                volatile uint64_t fail;
     71                                volatile uint64_t revoke;
     72                                volatile uint64_t block;
     73                        } alloc;
     74                        struct {
     75                                volatile uint64_t fast;
     76                                volatile uint64_t slow;
     77                        } submit;
     78                        struct {
     79                                volatile uint64_t external;
     80                        } flush;
     81                        struct {
     82                                volatile uint64_t drain;
     83                                volatile uint64_t completed;
     84                                volatile uint64_t flush;
     85                                volatile uint64_t submitted;
    6986                                struct {
    70                                         volatile uint64_t rdy;
    71                                         volatile uint64_t csm;
    72                                         volatile uint64_t avl;
    73                                         volatile uint64_t cnt;
    74                                 } submit_avg;
    75                                 struct {
    76                                         volatile uint64_t val;
    77                                         volatile uint64_t cnt;
    78                                         volatile uint64_t block;
    79                                 } look_avg;
    80                                 struct {
    81                                         volatile uint64_t val;
    82                                         volatile uint64_t cnt;
    83                                         volatile uint64_t block;
    84                                 } alloc_avg;
    85                                 volatile uint64_t helped;
    86                                 volatile uint64_t leader;
    87                                 volatile uint64_t busy;
    88                         } submit_q;
     87                                        volatile uint64_t busy;
     88                                } errors;
     89                        } calls;
    8990                        struct {
    90                                 struct {
    91                                         volatile uint64_t val;
    92                                         volatile uint64_t cnt;
    93                                 } completed_avg;
    94                                 volatile uint64_t blocks;
    95                         } complete_q;
     91                                volatile uint64_t sleeps;
     92                        } poller;
    9693                };
    9794        #endif
  • libcfa/src/concurrency/thread.cfa

    r342af53 r8e4aa05  
    6262}
    6363
    64 FORALL_DATA_INSTANCE(ThreadCancelled, (dtype thread_t), (thread_t))
     64FORALL_DATA_INSTANCE(ThreadCancelled, (thread_t &), (thread_t))
    6565
    66 forall(dtype T)
     66forall(T &)
    6767void copy(ThreadCancelled(T) * dst, ThreadCancelled(T) * src) {
    6868        dst->virtual_table = src->virtual_table;
     
    7171}
    7272
    73 forall(dtype T)
     73forall(T &)
    7474const char * msg(ThreadCancelled(T) *) {
    7575        return "ThreadCancelled";
    7676}
    7777
    78 forall(dtype T)
     78forall(T &)
    7979static void default_thread_cancel_handler(ThreadCancelled(T) & ) {
    8080        abort( "Unhandled thread cancellation.\n" );
    8181}
    8282
    83 forall(dtype T | is_thread(T) | IS_EXCEPTION(ThreadCancelled, (T)))
     83forall(T & | is_thread(T) | IS_EXCEPTION(ThreadCancelled, (T)))
    8484void ?{}( thread_dtor_guard_t & this,
    85                 T & thrd, void(*defaultResumptionHandler)(ThreadCancelled(T) &)) {
    86         $monitor * m = get_monitor(thrd);
     85                T & thrd, void(*cancelHandler)(ThreadCancelled(T) &)) {
     86        $monitor * m = get_monitor(thrd);
    8787        $thread * desc = get_thread(thrd);
    8888
    8989        // Setup the monitor guard
    9090        void (*dtor)(T& mutex this) = ^?{};
    91         bool join = defaultResumptionHandler != (void(*)(ThreadCancelled(T)&))0;
     91        bool join = cancelHandler != (void(*)(ThreadCancelled(T)&))0;
    9292        (this.mg){&m, (void(*)())dtor, join};
    9393
     
    103103        }
    104104        desc->state = Cancelled;
    105         if (!join) {
    106                 defaultResumptionHandler = default_thread_cancel_handler;
    107         }
     105        void(*defaultResumptionHandler)(ThreadCancelled(T) &) =
     106                join ? cancelHandler : default_thread_cancel_handler;
    108107
    109108        ThreadCancelled(T) except;
     
    125124//-----------------------------------------------------------------------------
    126125// Starting and stopping threads
    127 forall( dtype T | is_thread(T) )
     126forall( T & | is_thread(T) )
    128127void __thrd_start( T & this, void (*main_p)(T &) ) {
    129128        $thread * this_thrd = get_thread(this);
     
    141140//-----------------------------------------------------------------------------
    142141// Support for threads that don't ues the thread keyword
    143 forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T&); } )
     142forall( T & | sized(T) | is_thread(T) | { void ?{}(T&); } )
    144143void ?{}( scoped(T)& this ) with( this ) {
    145144        handle{};
     
    147146}
    148147
    149 forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T&, P); } )
     148forall( T &, P... | sized(T) | is_thread(T) | { void ?{}(T&, P); } )
    150149void ?{}( scoped(T)& this, P params ) with( this ) {
    151150        handle{ params };
     
    153152}
    154153
    155 forall( dtype T | sized(T) | is_thread(T) )
     154forall( T & | sized(T) | is_thread(T) )
    156155void ^?{}( scoped(T)& this ) with( this ) {
    157156        ^handle{};
     
    159158
    160159//-----------------------------------------------------------------------------
    161 forall(dtype T | is_thread(T) | IS_RESUMPTION_EXCEPTION(ThreadCancelled, (T)))
     160forall(T & | is_thread(T) | IS_RESUMPTION_EXCEPTION(ThreadCancelled, (T)))
    162161T & join( T & this ) {
    163162        thread_dtor_guard_t guard = { this, defaultResumptionHandler };
  • libcfa/src/concurrency/thread.hfa

    r342af53 r8e4aa05  
    2626//-----------------------------------------------------------------------------
    2727// thread trait
    28 trait is_thread(dtype T) {
     28trait is_thread(T &) {
    2929        void ^?{}(T& mutex this);
    3030        void main(T& this);
     
    3232};
    3333
    34 FORALL_DATA_EXCEPTION(ThreadCancelled, (dtype thread_t), (thread_t)) (
     34FORALL_DATA_EXCEPTION(ThreadCancelled, (thread_t &), (thread_t)) (
    3535        thread_t * the_thread;
    3636        exception_t * the_exception;
    3737);
    3838
    39 forall(dtype T)
     39forall(T &)
    4040void copy(ThreadCancelled(T) * dst, ThreadCancelled(T) * src);
    4141
    42 forall(dtype T)
     42forall(T &)
    4343const char * msg(ThreadCancelled(T) *);
    4444
     
    4747
    4848// Inline getters for threads/coroutines/monitors
    49 forall( dtype T | is_thread(T) )
     49forall( T & | is_thread(T) )
    5050static inline $coroutine* get_coroutine(T & this) __attribute__((const)) { return &get_thread(this)->self_cor; }
    5151
    52 forall( dtype T | is_thread(T) )
     52forall( T & | is_thread(T) )
    5353static inline $monitor  * get_monitor  (T & this) __attribute__((const)) { return &get_thread(this)->self_mon; }
    5454
     
    6060extern struct cluster * mainCluster;
    6161
    62 forall( dtype T | is_thread(T) )
     62forall( T & | is_thread(T) )
    6363void __thrd_start( T & this, void (*)(T &) );
    6464
     
    8282};
    8383
    84 forall( dtype T | is_thread(T) | IS_EXCEPTION(ThreadCancelled, (T)) )
     84forall( T & | is_thread(T) | IS_EXCEPTION(ThreadCancelled, (T)) )
    8585void ?{}( thread_dtor_guard_t & this, T & thrd, void(*)(ThreadCancelled(T) &) );
    8686void ^?{}( thread_dtor_guard_t & this );
     
    8989// thread runner
    9090// Structure that actually start and stop threads
    91 forall( dtype T | sized(T) | is_thread(T) )
     91forall( T & | sized(T) | is_thread(T) )
    9292struct scoped {
    9393        T handle;
    9494};
    9595
    96 forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T&); } )
     96forall( T & | sized(T) | is_thread(T) | { void ?{}(T&); } )
    9797void ?{}( scoped(T)& this );
    9898
    99 forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T&, P); } )
     99forall( T &, P... | sized(T) | is_thread(T) | { void ?{}(T&, P); } )
    100100void ?{}( scoped(T)& this, P params );
    101101
    102 forall( dtype T | sized(T) | is_thread(T) )
     102forall( T & | sized(T) | is_thread(T) )
    103103void ^?{}( scoped(T)& this );
    104104
     
    115115void unpark( $thread * this );
    116116
    117 forall( dtype T | is_thread(T) )
     117forall( T & | is_thread(T) )
    118118static inline void unpark( T & this ) { if(!&this) return; unpark( get_thread( this ) );}
    119119
     
    128128//----------
    129129// join
    130 forall( dtype T | is_thread(T) | IS_RESUMPTION_EXCEPTION(ThreadCancelled, (T)) )
     130forall( T & | is_thread(T) | IS_RESUMPTION_EXCEPTION(ThreadCancelled, (T)) )
    131131T & join( T & this );
    132132
  • libcfa/src/containers/list.hfa

    r342af53 r8e4aa05  
    6666#define __DLISTED_MGD_JUSTIMPL(STRUCT)
    6767
    68 forall( dtype tE ) {
     68forall( tE & ) {
    6969        struct $mgd_link {
    7070                tE *elem;
     
    8383                (this.is_terminator){ 1 };
    8484        }
    85         forall ( otype tInit | { void ?{}( $mgd_link(tE) &, tInit); } )
     85        forall ( tInit | { void ?{}( $mgd_link(tE) &, tInit); } )
    8686        static inline void ?=?( $mgd_link(tE) &this, tInit i ) {
    8787                ^?{}( this );
     
    115115  __DLISTED_MGD_COMMON(STRUCT, STRUCT, $links)
    116116
    117 trait $dlistable(dtype Tnode, dtype Telem) {
     117trait $dlistable(Tnode &, Telem &) {
    118118        $mgd_link(Telem) & $prev_link(Tnode &);
    119119        $mgd_link(Telem) & $next_link(Tnode &);
     
    125125};
    126126
    127 forall (dtype Tnode, dtype Telem | $dlistable(Tnode, Telem)) {
     127forall (Tnode &, Telem & | $dlistable(Tnode, Telem)) {
    128128
    129129        // implemented as a sentinel item in an underlying cicrular list
  • libcfa/src/containers/maybe.cfa

    r342af53 r8e4aa05  
    1818
    1919
    20 forall(otype T)
     20forall(T)
    2121void ?{}(maybe(T) & this) {
    2222        this.has_value = false;
    2323}
    2424
    25 forall(otype T)
     25forall(T)
    2626void ?{}(maybe(T) & this, T value) {
    2727        this.has_value = true;
     
    2929}
    3030
    31 forall(otype T)
     31forall(T)
    3232void ?{}(maybe(T) & this, maybe(T) other) {
    3333        this.has_value = other.has_value;
     
    3737}
    3838
    39 forall(otype T)
     39forall(T)
    4040maybe(T) ?=?(maybe(T) & this, maybe(T) that) {
    4141        if (this.has_value && that.has_value) {
     
    5151}
    5252
    53 forall(otype T)
     53forall(T)
    5454void ^?{}(maybe(T) & this) {
    5555        if (this.has_value) {
     
    5858}
    5959
    60 forall(otype T)
     60forall(T)
    6161bool ?!=?(maybe(T) this, zero_t) {
    6262        return this.has_value;
    6363}
    6464
    65 forall(otype T)
     65forall(T)
    6666maybe(T) maybe_value(T value) {
    6767        return (maybe(T)){value};
    6868}
    6969
    70 forall(otype T)
     70forall(T)
    7171maybe(T) maybe_none() {
    7272        return (maybe(T)){};
    7373}
    7474
    75 forall(otype T)
     75forall(T)
    7676bool has_value(maybe(T) * this) {
    7777        return this->has_value;
    7878}
    7979
    80 forall(otype T)
     80forall(T)
    8181T get(maybe(T) * this) {
    8282        assertf(this->has_value, "attempt to get from maybe without value");
     
    8484}
    8585
    86 forall(otype T)
     86forall(T)
    8787void set(maybe(T) * this, T value) {
    8888        if (this->has_value) {
     
    9494}
    9595
    96 forall(otype T)
     96forall(T)
    9797void set_none(maybe(T) * this) {
    9898        if (this->has_value) {
  • libcfa/src/containers/maybe.hfa

    r342af53 r8e4aa05  
    1919
    2020// DO NOT USE DIRECTLY!
    21 forall(otype T)
     21forall(T)
    2222struct maybe {
    2323    bool has_value;
     
    2626
    2727
    28 forall(otype T)
     28forall(T)
    2929void ?{}(maybe(T) & this);
    3030
    31 forall(otype T)
     31forall(T)
    3232void ?{}(maybe(T) & this, T value);
    3333
    34 forall(otype T)
     34forall(T)
    3535void ?{}(maybe(T) & this, maybe(T) other);
    3636
    37 forall(otype T)
     37forall(T)
    3838void ^?{}(maybe(T) & this);
    3939
    40 forall(otype T)
     40forall(T)
    4141maybe(T) ?=?(maybe(T) & this, maybe(T) other);
    4242
    43 forall(otype T)
     43forall(T)
    4444bool ?!=?(maybe(T) this, zero_t);
    4545
    4646/* Waiting for bug#11 to be fixed.
    47 forall(otype T)
     47forall(T)
    4848maybe(T) maybe_value(T value);
    4949
    50 forall(otype T)
     50forall(T)
    5151maybe(T) maybe_none();
    5252*/
    5353
    54 forall(otype T)
     54forall(T)
    5555bool has_value(maybe(T) * this);
    5656
    57 forall(otype T)
     57forall(T)
    5858T get(maybe(T) * this);
    5959
    60 forall(otype T)
     60forall(T)
    6161void set(maybe(T) * this, T value);
    6262
    63 forall(otype T)
     63forall(T)
    6464void set_none(maybe(T) * this);
    6565
  • libcfa/src/containers/pair.cfa

    r342af53 r8e4aa05  
    1313#include <containers/pair.hfa>
    1414
    15 forall(otype R, otype S
     15forall(R, S
    1616        | { int ?==?(R, R); int ?<?(R, R); int ?<?(S, S); })
    1717int ?<?(pair(R, S) p, pair(R, S) q) {
     
    1919}
    2020
    21 forall(otype R, otype S
     21forall(R, S
    2222        | { int ?==?(R, R); int ?<?(R, R); int ?<=?(S, S); })
    2323int ?<=?(pair(R, S) p, pair(R, S) q) {
     
    2525}
    2626
    27 forall(otype R, otype S | { int ?==?(R, R); int ?==?(S, S); })
     27forall(R, S | { int ?==?(R, R); int ?==?(S, S); })
    2828int ?==?(pair(R, S) p, pair(R, S) q) {
    2929        return p.first == q.first && p.second == q.second;
    3030}
    3131
    32 forall(otype R, otype S | { int ?!=?(R, R); int ?!=?(S, S); })
     32forall(R, S | { int ?!=?(R, R); int ?!=?(S, S); })
    3333int ?!=?(pair(R, S) p, pair(R, S) q) {
    3434        return p.first != q.first || p.second != q.second;
    3535}
    3636
    37 forall(otype R, otype S
     37forall(R, S
    3838        | { int ?==?(R, R); int ?>?(R, R); int ?>?(S, S); })
    3939int ?>?(pair(R, S) p, pair(R, S) q) {
     
    4141}
    4242
    43 forall(otype R, otype S
     43forall(R, S
    4444        | { int ?==?(R, R); int ?>?(R, R); int ?>=?(S, S); })
    4545int ?>=?(pair(R, S) p, pair(R, S) q) {
  • libcfa/src/containers/pair.hfa

    r342af53 r8e4aa05  
    1616#pragma once
    1717
    18 forall(otype R, otype S) struct pair {
     18forall(R, S) struct pair {
    1919        R first;
    2020        S second;
    2121};
    2222
    23 forall(otype R, otype S
     23forall(R, S
    2424        | { int ?==?(R, R); int ?<?(R, R); int ?<?(S, S); })
    2525int ?<?(pair(R, S) p, pair(R, S) q);
    2626
    27 forall(otype R, otype S
     27forall(R, S
    2828        | { int ?==?(R, R); int ?<?(R, R); int ?<=?(S, S); })
    2929int ?<=?(pair(R, S) p, pair(R, S) q);
    3030
    31 forall(otype R, otype S | { int ?==?(R, R); int ?==?(S, S); })
     31forall(R, S | { int ?==?(R, R); int ?==?(S, S); })
    3232int ?==?(pair(R, S) p, pair(R, S) q);
    3333
    34 forall(otype R, otype S | { int ?!=?(R, R); int ?!=?(S, S); })
     34forall(R, S | { int ?!=?(R, R); int ?!=?(S, S); })
    3535int ?!=?(pair(R, S) p, pair(R, S) q);
    3636
    37 forall(otype R, otype S
     37forall(R, S
    3838        | { int ?==?(R, R); int ?>?(R, R); int ?>?(S, S); })
    3939int ?>?(pair(R, S) p, pair(R, S) q);
    4040
    41 forall(otype R, otype S
     41forall(R, S
    4242        | { int ?==?(R, R); int ?>?(R, R); int ?>=?(S, S); })
    4343int ?>=?(pair(R, S) p, pair(R, S) q);
  • libcfa/src/containers/result.cfa

    r342af53 r8e4aa05  
    1818
    1919
    20 forall(otype T, otype E)
     20forall(T, E)
    2121void ?{}(result(T, E) & this) {
    2222        this.has_value = false;
     
    2424}
    2525
    26 forall(otype T, otype E)
     26forall(T, E)
    2727void ?{}(result(T, E) & this, one_t, T value) {
    2828        this.has_value = true;
     
    3030}
    3131
    32 forall(otype T, otype E)
     32forall(T, E)
    3333void ?{}(result(T, E) & this, zero_t, E error) {
    3434        this.has_value = false;
     
    3636}
    3737
    38 forall(otype T, otype E)
     38forall(T, E)
    3939void ?{}(result(T, E) & this, result(T, E) other) {
    4040        this.has_value = other.has_value;
     
    4646}
    4747
    48 forall(otype T, otype E)
     48forall(T, E)
    4949result(T, E) ?=?(result(T, E) & this, result(T, E) that) {
    5050        if (this.has_value && that.has_value) {
     
    6363}
    6464
    65 forall(otype T, otype E)
     65forall(T, E)
    6666void ^?{}(result(T, E) & this) {
    6767        if (this.has_value) {
     
    7272}
    7373
    74 forall(otype T, otype E)
     74forall(T, E)
    7575bool ?!=?(result(T, E) this, zero_t) {
    7676        return this.has_value;
    7777}
    7878
    79 forall(otype T, otype E)
     79forall(T, E)
    8080result(T, E) result_value(T value) {
    8181        return (result(T, E)){1, value};
    8282}
    8383
    84 forall(otype T, otype E)
     84forall(T, E)
    8585result(T, E) result_error(E error) {
    8686        return (result(T, E)){0, error};
    8787}
    8888
    89 forall(otype T, otype E)
     89forall(T, E)
    9090bool has_value(result(T, E) * this) {
    9191        return this->has_value;
    9292}
    9393
    94 forall(otype T, otype E)
     94forall(T, E)
    9595T get(result(T, E) * this) {
    9696        assertf(this->has_value, "attempt to get from result without value");
     
    9898}
    9999
    100 forall(otype T, otype E)
     100forall(T, E)
    101101E get_error(result(T, E) * this) {
    102102        assertf(!this->has_value, "attempt to get from result without error");
     
    104104}
    105105
    106 forall(otype T, otype E)
     106forall(T, E)
    107107void set(result(T, E) * this, T value) {
    108108        if (this->has_value) {
     
    115115}
    116116
    117 forall(otype T, otype E)
     117forall(T, E)
    118118void set_error(result(T, E) * this, E error) {
    119119        if (this->has_value) {
  • libcfa/src/containers/result.hfa

    r342af53 r8e4aa05  
    1919
    2020// DO NOT USE DIRECTLY!
    21 forall(otype T, otype E)
     21forall(T, E)
    2222union inner_result{
    2323        T value;
     
    2525};
    2626
    27 forall(otype T, otype E)
     27forall(T, E)
    2828struct result {
    2929        bool has_value;
     
    3232
    3333
    34 forall(otype T, otype E)
     34forall(T, E)
    3535void ?{}(result(T, E) & this);
    3636
    37 forall(otype T, otype E)
     37forall(T, E)
    3838void ?{}(result(T, E) & this, one_t, T value);
    3939
    40 forall(otype T, otype E)
     40forall(T, E)
    4141void ?{}(result(T, E) & this, zero_t, E error);
    4242
    43 forall(otype T, otype E)
     43forall(T, E)
    4444void ?{}(result(T, E) & this, result(T, E) other);
    4545
    46 forall(otype T, otype E)
     46forall(T, E)
    4747void ^?{}(result(T, E) & this);
    4848
    49 forall(otype T, otype E)
     49forall(T, E)
    5050result(T, E) ?=?(result(T, E) & this, result(T, E) other);
    5151
    52 forall(otype T, otype E)
     52forall(T, E)
    5353bool ?!=?(result(T, E) this, zero_t);
    5454
    5555/* Wating for bug#11 to be fixed.
    56 forall(otype T, otype E)
     56forall(T, E)
    5757result(T, E) result_value(T value);
    5858
    59 forall(otype T, otype E)
     59forall(T, E)
    6060result(T, E) result_error(E error);
    6161*/
    6262
    63 forall(otype T, otype E)
     63forall(T, E)
    6464bool has_value(result(T, E) * this);
    6565
    66 forall(otype T, otype E)
     66forall(T, E)
    6767T get(result(T, E) * this);
    6868
    69 forall(otype T, otype E)
     69forall(T, E)
    7070E get_error(result(T, E) * this);
    7171
    72 forall(otype T, otype E)
     72forall(T, E)
    7373void set(result(T, E) * this, T value);
    7474
    75 forall(otype T, otype E)
     75forall(T, E)
    7676void set_error(result(T, E) * this, E error);
    7777
  • libcfa/src/containers/stackLockFree.hfa

    r342af53 r8e4aa05  
    99// Created On       : Wed May 13 20:58:58 2020
    1010// Last Modified By : Peter A. Buhr
    11 // Last Modified On : Sun Jun 14 13:25:09 2020
    12 // Update Count     : 64
     11// Last Modified On : Wed Jan 20 20:40:03 2021
     12// Update Count     : 67
    1313//
    1414
     
    1717#include <stdint.h>
    1818
    19 forall( dtype T )
     19forall( T & )
    2020union Link {
    2121        struct {                                                                                        // 32/64-bit x 2
     
    3131}; // Link
    3232
    33 forall( otype T | sized(T) | { Link(T) * ?`next( T * ); } ) {
     33forall( T | sized(T) | { Link(T) * ?`next( T * ); } ) {
    3434        struct StackLF {
    3535                Link(T) stack;
     
    4242
    4343                void push( StackLF(T) & this, T & n ) with(this) {
    44                         *( &n )`next = stack;                                   // atomic assignment unnecessary, or use CAA
     44                        *( &n )`next = stack;                                           // atomic assignment unnecessary, or use CAA
    4545                        for () {                                                                        // busy wait
    4646                          if ( __atomic_compare_exchange_n( &stack.atom, &( &n )`next->atom, (Link(T))@{ {&n, ( &n )`next->count + 1} }.atom, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST ) ) break; // attempt to update top node
     
    6565                                }
    6666                                if( next == 0p ) return false;
    67                                 link = (next)`next;
     67                                link = ( next )`next;
    6868                        }
    6969                }
  • libcfa/src/containers/vector.cfa

    r342af53 r8e4aa05  
    1818#include <stdlib.hfa>
    1919
    20 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     20forall(T, allocator_t | allocator_c(T, allocator_t))
    2121void copy_internal(vector(T, allocator_t)* this, vector(T, allocator_t)* other);
    2222
    2323//------------------------------------------------------------------------------
    2424//Initialization
    25 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     25forall(T, allocator_t | allocator_c(T, allocator_t))
    2626void ?{}(vector(T, allocator_t)& this)
    2727{
     
    3030}
    3131
    32 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     32forall(T, allocator_t | allocator_c(T, allocator_t))
    3333void ?{}(vector(T, allocator_t)& this, vector(T, allocator_t) rhs)
    3434{
     
    3737}
    3838
    39 // forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     39// forall(T, allocator_t | allocator_c(T, allocator_t))
    4040// vector(T, allocator_t) ?=?(vector(T, allocator_t)* this, vector(T, allocator_t) rhs)
    4141// {
     
    4545// }
    4646
    47 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     47forall(T, allocator_t | allocator_c(T, allocator_t))
    4848void ^?{}(vector(T, allocator_t)& this)
    4949{
     
    5454//------------------------------------------------------------------------------
    5555//Modifiers
    56 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     56forall(T, allocator_t | allocator_c(T, allocator_t))
    5757void push_back(vector(T, allocator_t)* this, T value)
    5858{
     
    6262}
    6363
    64 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     64forall(T, allocator_t | allocator_c(T, allocator_t))
    6565void pop_back(vector(T, allocator_t)* this)
    6666{
     
    6969}
    7070
    71 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     71forall(T, allocator_t | allocator_c(T, allocator_t))
    7272void clear(vector(T, allocator_t)* this)
    7373{
     
    8282//Internal Helpers
    8383
    84 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     84forall(T, allocator_t | allocator_c(T, allocator_t))
    8585void copy_internal(vector(T, allocator_t)* this, vector(T, allocator_t)* other)
    8686{
     
    9393//------------------------------------------------------------------------------
    9494//Allocator
    95 forall(otype T)
     95forall(T)
    9696void ?{}(heap_allocator(T)& this)
    9797{
     
    100100}
    101101
    102 forall(otype T)
     102forall(T)
    103103void ?{}(heap_allocator(T)& this, heap_allocator(T) rhs)
    104104{
     
    107107}
    108108
    109 forall(otype T)
     109forall(T)
    110110heap_allocator(T) ?=?(heap_allocator(T)& this, heap_allocator(T) rhs)
    111111{
     
    115115}
    116116
    117 forall(otype T)
     117forall(T)
    118118void ^?{}(heap_allocator(T)& this)
    119119{
     
    121121}
    122122
    123 forall(otype T)
     123forall(T)
    124124inline void realloc_storage(heap_allocator(T)* this, size_t size)
    125125{
  • libcfa/src/containers/vector.hfa

    r342af53 r8e4aa05  
    2020//------------------------------------------------------------------------------
    2121//Allocator
    22 forall(otype T)
     22forall(T)
    2323struct heap_allocator
    2424{
     
    2727};
    2828
    29 forall(otype T)
     29forall(T)
    3030void ?{}(heap_allocator(T)& this);
    3131
    32 forall(otype T)
     32forall(T)
    3333void ?{}(heap_allocator(T)& this, heap_allocator(T) rhs);
    3434
    35 forall(otype T)
     35forall(T)
    3636heap_allocator(T) ?=?(heap_allocator(T)& this, heap_allocator(T) rhs);
    3737
    38 forall(otype T)
     38forall(T)
    3939void ^?{}(heap_allocator(T)& this);
    4040
    41 forall(otype T)
     41forall(T)
    4242void realloc_storage(heap_allocator(T)* this, size_t size);
    4343
    44 forall(otype T)
     44forall(T)
    4545static inline T* data(heap_allocator(T)* this)
    4646{
     
    5050//------------------------------------------------------------------------------
    5151//Declaration
    52 trait allocator_c(otype T, otype allocator_t)
     52trait allocator_c(T, allocator_t)
    5353{
    5454        void realloc_storage(allocator_t*, size_t);
     
    5656};
    5757
    58 forall(otype T, otype allocator_t = heap_allocator(T) | allocator_c(T, allocator_t))
     58forall(T, allocator_t = heap_allocator(T) | allocator_c(T, allocator_t))
    5959struct vector;
    6060
    6161//------------------------------------------------------------------------------
    6262//Initialization
    63 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     63forall(T, allocator_t | allocator_c(T, allocator_t))
    6464void ?{}(vector(T, allocator_t)& this);
    6565
    66 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     66forall(T, allocator_t | allocator_c(T, allocator_t))
    6767void ?{}(vector(T, allocator_t)& this, vector(T, allocator_t) rhs);
    6868
    69 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     69forall(T, allocator_t | allocator_c(T, allocator_t))
    7070vector(T, allocator_t) ?=?(vector(T, allocator_t)& this, vector(T, allocator_t) rhs);
    7171
    72 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     72forall(T, allocator_t | allocator_c(T, allocator_t))
    7373void ^?{}(vector(T, allocator_t)& this);
    7474
    75 forall(otype T, otype allocator_t = heap_allocator(T) | allocator_c(T, allocator_t))
     75forall(T, allocator_t = heap_allocator(T) | allocator_c(T, allocator_t))
    7676struct vector
    7777{
     
    8282//------------------------------------------------------------------------------
    8383//Capacity
    84 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     84forall(T, allocator_t | allocator_c(T, allocator_t))
    8585static inline bool empty(vector(T, allocator_t)* this)
    8686{
     
    8888}
    8989
    90 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     90forall(T, allocator_t | allocator_c(T, allocator_t))
    9191static inline size_t size(vector(T, allocator_t)* this)
    9292{
     
    9494}
    9595
    96 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     96forall(T, allocator_t | allocator_c(T, allocator_t))
    9797static inline void reserve(vector(T, allocator_t)* this, size_t size)
    9898{
     
    102102//------------------------------------------------------------------------------
    103103//Element access
    104 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     104forall(T, allocator_t | allocator_c(T, allocator_t))
    105105static inline T at(vector(T, allocator_t)* this, size_t index)
    106106{
     
    108108}
    109109
    110 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     110forall(T, allocator_t | allocator_c(T, allocator_t))
    111111static inline T ?[?](vector(T, allocator_t)* this, size_t index)
    112112{
     
    114114}
    115115
    116 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     116forall(T, allocator_t | allocator_c(T, allocator_t))
    117117static inline T front(vector(T, allocator_t)* this)
    118118{
     
    120120}
    121121
    122 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     122forall(T, allocator_t | allocator_c(T, allocator_t))
    123123static inline T back(vector(T, allocator_t)* this)
    124124{
     
    128128//------------------------------------------------------------------------------
    129129//Modifiers
    130 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     130forall(T, allocator_t | allocator_c(T, allocator_t))
    131131void push_back(vector(T, allocator_t)* this, T value);
    132132
    133 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     133forall(T, allocator_t | allocator_c(T, allocator_t))
    134134void pop_back(vector(T, allocator_t)* this);
    135135
    136 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     136forall(T, allocator_t | allocator_c(T, allocator_t))
    137137void clear(vector(T, allocator_t)* this);
    138138
    139139//------------------------------------------------------------------------------
    140140//Iterators
    141 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     141forall(T, allocator_t | allocator_c(T, allocator_t))
    142142static inline T* begin(vector(T, allocator_t)* this)
    143143{
     
    145145}
    146146
    147 // forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     147// forall(T, allocator_t | allocator_c(T, allocator_t))
    148148// static inline const T* cbegin(const vector(T, allocator_t)* this)
    149149// {
     
    151151// }
    152152
    153 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     153forall(T, allocator_t | allocator_c(T, allocator_t))
    154154static inline T* end(vector(T, allocator_t)* this)
    155155{
     
    157157}
    158158
    159 // forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     159// forall(T, allocator_t | allocator_c(T, allocator_t))
    160160// static inline const T* cend(const vector(T, allocator_t)* this)
    161161// {
  • libcfa/src/exception.h

    r342af53 r8e4aa05  
    101101// implemented in the .c file either so they all have to be inline.
    102102
    103 trait is_exception(dtype exceptT, dtype virtualT) {
     103trait is_exception(exceptT &, virtualT &) {
    104104        /* The first field must be a pointer to a virtual table.
    105105         * That virtual table must be a decendent of the base exception virtual table.
     
    109109};
    110110
    111 trait is_termination_exception(dtype exceptT, dtype virtualT | is_exception(exceptT, virtualT)) {
     111trait is_termination_exception(exceptT &, virtualT & | is_exception(exceptT, virtualT)) {
    112112        void defaultTerminationHandler(exceptT &);
    113113};
    114114
    115 trait is_resumption_exception(dtype exceptT, dtype virtualT | is_exception(exceptT, virtualT)) {
     115trait is_resumption_exception(exceptT &, virtualT & | is_exception(exceptT, virtualT)) {
    116116        void defaultResumptionHandler(exceptT &);
    117117};
    118118
    119 forall(dtype exceptT, dtype virtualT | is_termination_exception(exceptT, virtualT))
     119forall(exceptT &, virtualT & | is_termination_exception(exceptT, virtualT))
    120120static inline void $throw(exceptT & except) {
    121121        __cfaehm_throw_terminate(
     
    125125}
    126126
    127 forall(dtype exceptT, dtype virtualT | is_resumption_exception(exceptT, virtualT))
     127forall(exceptT &, virtualT & | is_resumption_exception(exceptT, virtualT))
    128128static inline void $throwResume(exceptT & except) {
    129129        __cfaehm_throw_resume(
     
    133133}
    134134
    135 forall(dtype exceptT, dtype virtualT | is_exception(exceptT, virtualT))
     135forall(exceptT &, virtualT & | is_exception(exceptT, virtualT))
    136136static inline void cancel_stack(exceptT & except) __attribute__((noreturn)) {
    137137        __cfaehm_cancel_stack( (exception_t *)&except );
    138138}
    139139
    140 forall(dtype exceptT, dtype virtualT | is_exception(exceptT, virtualT))
     140forall(exceptT &, virtualT & | is_exception(exceptT, virtualT))
    141141static inline void defaultTerminationHandler(exceptT & except) {
    142142        return cancel_stack( except );
    143143}
    144144
    145 forall(dtype exceptT, dtype virtualT | is_exception(exceptT, virtualT))
     145forall(exceptT &, virtualT & | is_exception(exceptT, virtualT))
    146146static inline void defaultResumptionHandler(exceptT & except) {
    147147        throw except;
  • libcfa/src/executor.cfa

    r342af53 r8e4aa05  
    77#include <containers/list.hfa>
    88
    9 forall( dtype T | $dlistable(T, T) ) {
     9forall( T & | $dlistable(T, T) ) {
    1010        monitor Buffer {                                                                        // unbounded buffer
    1111                dlist( T, T ) queue;                                                    // unbounded list of work requests
  • libcfa/src/fstream.cfa

    r342af53 r8e4aa05  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jun 19 16:24:54 2020
    13 // Update Count     : 384
     12// Last Modified On : Mon Mar  1 21:12:15 2021
     13// Update Count     : 424
    1414//
    1515
     
    2525#include <errno.h>                                                                              // errno
    2626
    27 
    2827// *********************************** ofstream ***********************************
    2928
     
    3837        os.$prt = false;
    3938        os.$sawNL = false;
     39        os.$acquired = false;
    4040        $sepSetCur( os, sepGet( os ) );
    4141        sepSet( os, " " );
     
    109109        if ( &os == &exit ) exit( EXIT_FAILURE );
    110110        if ( &os == &abort ) abort();
     111        if ( os.$acquired ) { os.$acquired = false; release( os ); }
    111112} // ends
    112113
     
    171172} // fmt
    172173
     174inline void acquire( ofstream & os ) {
     175        lock( os.$lock );
     176        if ( ! os.$acquired ) os.$acquired = true;
     177        else unlock( os.$lock );
     178} // acquire
     179
     180inline void release( ofstream & os ) {
     181        unlock( os.$lock );
     182} // release
     183
     184void ?{}( osacquire & acq, ofstream & os ) { &acq.os = &os; lock( os.$lock ); }
     185void ^?{}( osacquire & acq ) { release( acq.os ); }
     186
    173187static ofstream soutFile = { (FILE *)stdout };
    174188ofstream & sout = soutFile, & stdout = soutFile;
     
    176190ofstream & serr = serrFile, & stderr = serrFile;
    177191
     192static ofstream lsoutFile = { (FILE *)stdout };
     193ofstream & lsout = lsoutFile;
     194
    178195static ofstream exitFile = { (FILE *)stdout };
    179196ofstream & exit = exitFile;
     
    189206        is.$file = file;
    190207        is.$nlOnOff = false;
     208        is.$acquired = false;
    191209} // ?{}
    192210
     
    213231        return is.$file == 0p || ferror( (FILE *)(is.$file) );
    214232} // fail
     233
     234void ends( ifstream & is ) {
     235        if ( is.$acquired ) { is.$acquired = false; release( is ); }
     236} // ends
    215237
    216238int eof( ifstream & is ) {
     
    279301} // fmt
    280302
     303inline void acquire( ifstream & is ) {
     304        lock( is.$lock );
     305        if ( ! is.$acquired ) is.$acquired = true;
     306        else unlock( is.$lock );
     307} // acquire
     308
     309inline void release( ifstream & is ) {
     310        unlock( is.$lock );
     311} // release
     312
     313void ?{}( isacquire & acq, ifstream & is ) { &acq.is = &is; lock( is.$lock ); }
     314void ^?{}( isacquire & acq ) { release( acq.is ); }
     315
    281316static ifstream sinFile = { (FILE *)stdin };
    282317ifstream & sin = sinFile, & stdin = sinFile;
  • libcfa/src/fstream.hfa

    r342af53 r8e4aa05  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jun 19 16:29:17 2020
    13 // Update Count     : 189
     12// Last Modified On : Mon Mar  1 22:45:08 2021
     13// Update Count     : 217
    1414//
    1515
    1616#pragma once
    1717
     18#include "bits/weakso_locks.hfa"                                                // mutex_lock
    1819#include "iostream.hfa"
    1920#include <exception.hfa>
     
    3435        char $separator[sepSize];
    3536        char $tupleSeparator[sepSize];
     37        multiple_acquisition_lock $lock;
     38        bool $acquired;
    3639}; // ofstream
    3740
     
    6972ofstream & write( ofstream &, const char data[], size_t size );
    7073int fmt( ofstream &, const char format[], ... ) __attribute__(( format(printf, 2, 3) ));
     74void acquire( ofstream & os );
     75void release( ofstream & os );
     76
     77struct osacquire {
     78        ofstream & os;
     79};
     80void ?{}( osacquire & acq, ofstream & os );
     81void ^?{}( osacquire & acq );
    7182
    7283void ?{}( ofstream & os );
     
    8596        void * $file;
    8697        bool $nlOnOff;
     98        multiple_acquisition_lock $lock;
     99        bool $acquired;
    87100}; // ifstream
    88101
     
    91104void nlOff( ifstream & );
    92105bool getANL( ifstream & );
     106void ends( ifstream & );
    93107int fail( ifstream & is );
    94108int eof( ifstream & is );
     
    99113ifstream & ungetc( ifstream & is, char c );
    100114int fmt( ifstream &, const char format[], ... ) __attribute__(( format(scanf, 2, 3) ));
     115void acquire( ifstream & is );
     116void release( ifstream & is );
     117
     118struct isacquire {
     119        ifstream & is;
     120};
     121void ?{}( isacquire & acq, ifstream & is );
     122void ^?{}( isacquire & acq );
    101123
    102124void ?{}( ifstream & is );
  • libcfa/src/gmp.hfa

    r342af53 r8e4aa05  
    255255
    256256        // I/O
    257         forall( dtype istype | istream( istype ) )
     257        forall( istype & | istream( istype ) )
    258258                istype & ?|?( istype & is, Int & mp ) {
    259259                gmp_scanf( "%Zd", &mp );
     
    261261        } // ?|?
    262262
    263         forall( dtype ostype | ostream( ostype ) ) {
     263        forall( ostype & | ostream( ostype ) ) {
    264264                ostype & ?|?( ostype & os, Int mp ) {
    265265                        if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
  • libcfa/src/interpose.cfa

    r342af53 r8e4aa05  
    125125
    126126                // Failure handler
    127                 __cfaabi_sigaction( SIGSEGV, sigHandler_segv, SA_SIGINFO | SA_ONSTACK );
    128                 __cfaabi_sigaction( SIGBUS , sigHandler_segv, SA_SIGINFO | SA_ONSTACK );
    129                 __cfaabi_sigaction( SIGILL , sigHandler_ill , SA_SIGINFO | SA_ONSTACK );
    130                 __cfaabi_sigaction( SIGFPE , sigHandler_fpe , SA_SIGINFO | SA_ONSTACK );
    131                 __cfaabi_sigaction( SIGTERM, sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // one shot handler, return to default
    132                 __cfaabi_sigaction( SIGINT , sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND );
    133                 __cfaabi_sigaction( SIGABRT, sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND );
    134                 __cfaabi_sigaction( SIGHUP , sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // terminal hangup
     127                 // internal errors
     128                __cfaabi_sigaction( SIGSEGV, sigHandler_segv, SA_SIGINFO | SA_ONSTACK ); // Invalid memory reference (default: Core)
     129                __cfaabi_sigaction( SIGBUS , sigHandler_segv, SA_SIGINFO | SA_ONSTACK ); // Bus error, bad memory access (default: Core)
     130                __cfaabi_sigaction( SIGILL , sigHandler_ill , SA_SIGINFO | SA_ONSTACK ); // Illegal Instruction (default: Core)
     131                __cfaabi_sigaction( SIGFPE , sigHandler_fpe , SA_SIGINFO | SA_ONSTACK ); // Floating-point exception (default: Core)
     132
     133                // handlers to outside errors
     134                // reset in-case they insist and send it over and over
     135                __cfaabi_sigaction( SIGTERM, sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Termination signal (default: Term)
     136                __cfaabi_sigaction( SIGINT , sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Interrupt from keyboard (default: Term)
     137                __cfaabi_sigaction( SIGHUP , sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Hangup detected on controlling terminal or death of controlling process (default: Term)
     138                __cfaabi_sigaction( SIGQUIT, sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Quit from keyboard (default: Core)
     139                __cfaabi_sigaction( SIGABRT, sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Abort signal from abort(3) (default: Core)
    135140        }
    136141}
     
    163168}
    164169
    165 void * kernel_abort( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) { return 0p; }
    166 void kernel_abort_msg( void * data, char buffer[], int size ) __attribute__(( __nothrow__, __leaf__, __weak__ )) {}
    167 // See concurrency/kernel.cfa for strong definition used in multi-processor mode.
    168 int kernel_abort_lastframe( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) { return 4; }
     170// See concurrency/kernel.cfa and concurrency/preemption.cfa for strong definition used in multi-processor mode.
     171void __kernel_abort_lock( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) {}
     172void __kernel_abort_msg( char buffer[], int size ) __attribute__(( __nothrow__, __leaf__, __weak__ )) {}
     173int __kernel_abort_lastframe( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) { return 4; }
    169174
    170175enum { abort_text_size = 1024 };
     
    173178static void __cfaabi_backtrace( int start ) {
    174179        enum { Frames = 50, };                                                          // maximum number of stack frames
    175         int last = kernel_abort_lastframe();                            // skip last N stack frames
     180        int last = __kernel_abort_lastframe();                          // skip last N stack frames
    176181
    177182        void * array[Frames];
     
    220225}
    221226
    222 static volatile int __abort_stage = 0;
     227static volatile bool __abort_first = 0;
    223228
    224229// Cannot forward va_list.
    225230void __abort( bool signalAbort, const char fmt[], va_list args ) {
    226         int stage = __atomic_add_fetch( &__abort_stage, 1, __ATOMIC_SEQ_CST );
    227 
    228         // First stage: stop the cforall kernel and print
    229         if(stage == 1) {
    230                 // increment stage
    231                 stage = __atomic_add_fetch( &__abort_stage, 1, __ATOMIC_SEQ_CST );
    232 
    233                 // must be done here to lock down kernel
    234                 void * kernel_data = kernel_abort();
    235                 int len;
    236 
    237                 signal( SIGABRT, SIG_DFL );                                                     // prevent final "real" abort from recursing to handler
    238 
    239                 len = snprintf( abort_text, abort_text_size, "Cforall Runtime error (UNIX pid:%ld) ", (long int)getpid() ); // use UNIX pid (versus getPid)
    240                 __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
    241 
    242                 assert( fmt );
    243                 len = vsnprintf( abort_text, abort_text_size, fmt, args );
    244                 __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
    245 
    246                 // add optional newline if missing at the end of the format text
    247                 if ( fmt[strlen( fmt ) - 1] != '\n' ) {
    248                         __cfaabi_bits_write( STDERR_FILENO, "\n", 1 );
    249                 } // if
    250                 kernel_abort_msg( kernel_data, abort_text, abort_text_size );
    251         }
    252 
    253         // Second stage: print the backtrace
    254         if(stage == 2) {
    255                 // increment stage
    256                 stage = __atomic_add_fetch( &__abort_stage, 1, __ATOMIC_SEQ_CST );
    257 
    258                 // print stack trace in handler
    259                 __cfaabi_backtrace( signalAbort ? 4 : 2 );
    260         }
    261 
    262         do {
    263                 // Finally call abort
     231        // Multiple threads can come here from multiple paths
     232        // To make sure this is safe any concurrent/subsequent call to abort is redirected to libc-abort
     233        bool first = ! __atomic_test_and_set( &__abort_first, __ATOMIC_SEQ_CST);
     234
     235        // Prevent preemption from kicking-in and messing with the abort
     236        __kernel_abort_lock();
     237
     238        // first to abort ?
     239        if ( !first ) {
     240                // We aren't the first to abort just let C handle it
     241                signal( SIGABRT, SIG_DFL );     // restore default in case we came here through the function.
    264242                __cabi_libc.abort();
    265 
    266                 // Loop so that we never return
    267         } while(true);
     243        }
     244
     245        int len = snprintf( abort_text, abort_text_size, "Cforall Runtime error (UNIX pid:%ld) ", (long int)getpid() ); // use UNIX pid (versus getPid)
     246        __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
     247
     248        // print the cause of the error
     249        assert( fmt );
     250        len = vsnprintf( abort_text, abort_text_size, fmt, args );
     251        __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
     252
     253        // add optional newline if missing at the end of the format text
     254        if ( fmt[strlen( fmt ) - 1] != '\n' ) {
     255                __cfaabi_bits_write( STDERR_FILENO, "\n", 1 );
     256        } // if
     257
     258        // Give the kernel the chance to add some data in here
     259        __kernel_abort_msg( abort_text, abort_text_size );
     260
     261        // print stack trace in handler
     262        __cfaabi_backtrace( signalAbort ? 4 : 2 );
     263
     264        // Finally call abort
     265        __cabi_libc.abort();
     266
    268267}
    269268
  • libcfa/src/iostream.cfa

    r342af53 r8e4aa05  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Aug 24 08:31:35 2020
    13 // Update Count     : 1130
     12// Last Modified On : Tue Mar  2 14:51:30 2021
     13// Update Count     : 1151
    1414//
    1515
     
    3636
    3737
    38 forall( dtype ostype | ostream( ostype ) ) {
     38forall( ostype & | ostream( ostype ) ) {
    3939        ostype & ?|?( ostype & os, bool b ) {
    4040                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     
    266266        } // ?|?
    267267
    268         ostype & ?|?( ostype & os, const char str[] ) {
     268        ostype & ?|?( ostype & os, const char s[] ) {
    269269                enum { Open = 1, Close, OpenClose };
    270270                static const unsigned char mask[256] @= {
     
    282282                }; // mask
    283283
    284           if ( str[0] == '\0' ) { sepOff( os ); return os; } // null string => no separator
     284          if ( s[0] == '\0' ) { sepOff( os ); return os; } // null string => no separator
    285285
    286286                // first character IS NOT spacing or closing punctuation => add left separator
    287                 unsigned char ch = str[0];                                              // must make unsigned
     287                unsigned char ch = s[0];                                                // must make unsigned
    288288                if ( $sepPrt( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) {
    289289                        fmt( os, "%s", $sepGetCur( os ) );
     
    294294
    295295                // last character IS spacing or opening punctuation => turn off separator for next item
    296                 size_t len = strlen( str );
    297                 ch = str[len - 1];                                                              // must make unsigned
     296                size_t len = strlen( s );
     297                ch = s[len - 1];                                                                // must make unsigned
    298298                if ( $sepPrt( os ) && mask[ ch ] != Open && mask[ ch ] != OpenClose ) {
    299299                        sepOn( os );
     
    302302                } // if
    303303                if ( ch == '\n' ) $setNL( os, true );                   // check *AFTER* $sepPrt call above as it resets NL flag
    304                 return write( os, str, len );
    305         } // ?|?
    306 
    307         void ?|?( ostype & os, const char str[] ) {
    308                 (ostype &)(os | str); ends( os );
    309         } // ?|?
    310 
    311 //      ostype & ?|?( ostype & os, const char16_t * str ) {
     304                return write( os, s, len );
     305        } // ?|?
     306        void ?|?( ostype & os, const char s[] ) {
     307                (ostype &)(os | s); ends( os );
     308        } // ?|?
     309
     310//      ostype & ?|?( ostype & os, const char16_t * s ) {
    312311//              if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
    313 //              fmt( os, "%ls", str );
     312//              fmt( os, "%ls", s );
    314313//              return os;
    315314//      } // ?|?
    316315
    317316// #if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous
    318 //      ostype & ?|?( ostype & os, const char32_t * str ) {
     317//      ostype & ?|?( ostype & os, const char32_t * s ) {
    319318//              if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
    320 //              fmt( os, "%ls", str );
     319//              fmt( os, "%ls", s );
    321320//              return os;
    322321//      } // ?|?
    323322// #endif // ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 )
    324323
    325 //      ostype & ?|?( ostype & os, const wchar_t * str ) {
     324//      ostype & ?|?( ostype & os, const wchar_t * s ) {
    326325//              if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
    327 //              fmt( os, "%ls", str );
     326//              fmt( os, "%ls", s );
    328327//              return os;
    329328//      } // ?|?
     
    340339        // manipulators
    341340        ostype & ?|?( ostype & os, ostype & (* manip)( ostype & ) ) {
    342                 (ostype &)(manip( os ));
    343                 return os;
     341                return manip( os );
    344342        } // ?|?
    345343        void ?|?( ostype & os, ostype & (* manip)( ostype & ) ) {
    346                 (ostype &)(manip( os ));
     344                manip( os );
    347345                if ( $getPrt( os ) ) ends( os );                                // something printed ?
    348346                $setPrt( os, false );                                                   // turn off
     
    399397                return os;
    400398        } // nlOff
     399
     400        ostype & acquire( ostype & os ) {
     401                acquire( os );                                                                  // call void returning
     402                return os;
     403        } // acquire
    401404} // distribution
    402405
    403406// tuples
    404 forall( dtype ostype, otype T, ttype Params | writeable( T, ostype ) | { ostype & ?|?( ostype &, Params ); } ) {
     407forall( ostype &, T, Params... | writeable( T, ostype ) | { ostype & ?|?( ostype &, Params ); } ) {
    405408        ostype & ?|?( ostype & os, T arg, Params rest ) {
    406409                (ostype &)(os | arg);                                                   // print first argument
     
    421424
    422425// writes the range [begin, end) to the given stream
    423 forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) ) {
     426forall( ostype &, elt_type | writeable( elt_type, ostype ), iterator_type | iterator( iterator_type, elt_type ) ) {
    424427        void write( iterator_type begin, iterator_type end, ostype & os ) {
    425428                void print( elt_type i ) { os | i; }
     
    442445// Default prefix for non-decimal prints is 0b, 0, 0x.
    443446#define IntegralFMTImpl( T, IFMTNP, IFMTP ) \
    444 forall( dtype ostype | ostream( ostype ) ) { \
     447forall( ostype & | ostream( ostype ) ) { \
    445448        ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \
    446449                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); \
     
    517520                return os; \
    518521        } /* ?|? */ \
    519         void ?|?( ostype & os, _Ostream_Manip(T) f ) { (ostype &)(os | f); ends( os ); } \
     522        void ?|?( ostype & os, _Ostream_Manip(T) f ) { \
     523                (ostype &)(os | f); ends( os ); \
     524        } /* ?|? */ \
    520525} // distribution
    521526
     
    535540// Default prefix for non-decimal prints is 0b, 0, 0x.
    536541#define IntegralFMTImpl128( T, SIGNED, CODE, IFMTNP, IFMTP ) \
    537 forall( dtype ostype | ostream( ostype ) ) \
     542forall( ostype & | ostream( ostype ) ) \
    538543static void base10_128( ostype & os, _Ostream_Manip(T) f ) { \
    539544        if ( f.val > UINT64_MAX ) { \
     
    552557        } /* if */ \
    553558} /* base10_128 */ \
    554 forall( dtype ostype | ostream( ostype ) ) { \
     559forall( ostype & | ostream( ostype ) ) { \
    555560        ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \
    556561                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); \
     
    654659#if defined( __SIZEOF_INT128__ )
    655660// Default prefix for non-decimal prints is 0b, 0, 0x.
    656 forall( dtype ostype | ostream( ostype ) )
     661forall( ostype & | ostream( ostype ) )
    657662static inline void base_128( ostype & os, unsigned int128 val, unsigned int128 power, _Ostream_Manip(uint64_t) & f, unsigned int maxdig, unsigned int bits, unsigned int cnt = 0 ) {
    658663        int wd = 1;                                                                                     // f.wd is never 0 because 0 implies left-pad
     
    719724
    720725#define IntegralFMTImpl128( T ) \
    721 forall( dtype ostype | ostream( ostype ) ) { \
     726forall( ostype & | ostream( ostype ) ) { \
    722727        ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \
    723728                _Ostream_Manip(uint64_t) fmt; \
     
    767772
    768773#define FloatingPointFMTImpl( T, DFMTNP, DFMTP ) \
    769 forall( dtype ostype | ostream( ostype ) ) { \
     774forall( ostype & | ostream( ostype ) ) { \
    770775        ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \
    771776                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); \
     
    801806// *********************************** character ***********************************
    802807
    803 forall( dtype ostype | ostream( ostype ) ) {
     808forall( ostype & | ostream( ostype ) ) {
    804809        ostype & ?|?( ostype & os, _Ostream_Manip(char) f ) {
    805810                if ( f.base != 'c' ) {                                                  // bespoke binary/octal/hex format
     
    834839// *********************************** C string ***********************************
    835840
    836 forall( dtype ostype | ostream( ostype ) ) {
     841forall( ostype & | ostream( ostype ) ) {
    837842        ostype & ?|?( ostype & os, _Ostream_Manip(const char *) f ) {
    838843                if ( ! f.val ) return os;                                               // null pointer ?
     
    882887
    883888
    884 forall( dtype istype | istream( istype ) ) {
     889forall( istype & | istream( istype ) ) {
    885890        istype & ?|?( istype & is, bool & b ) {
    886891                char val[6];
     
    894899                return is;
    895900        } // ?|?
     901        void ?|?( istype & is, bool & b ) {
     902                (istype &)(is | b); ends( is );
     903        } // ?|?
    896904
    897905        istype & ?|?( istype & is, char & c ) {
     
    905913                return is;
    906914        } // ?|?
     915        void ?|?( istype & is, char & c ) {
     916                (istype &)(is | c); ends( is );
     917        } // ?|?
    907918
    908919        istype & ?|?( istype & is, signed char & sc ) {
     
    910921                return is;
    911922        } // ?|?
     923        void ?|?( istype & is, signed char & sc ) {
     924                (istype &)(is | sc); ends( is );
     925        } // ?|?
    912926
    913927        istype & ?|?( istype & is, unsigned char & usc ) {
     
    915929                return is;
    916930        } // ?|?
     931        void ?|?( istype & is, unsigned char & usc ) {
     932                (istype &)(is | usc); ends( is );
     933        } // ?|?
    917934
    918935        istype & ?|?( istype & is, short int & si ) {
     
    920937                return is;
    921938        } // ?|?
     939        void ?|?( istype & is, short int & si ) {
     940                (istype &)(is | si); ends( is );
     941        } // ?|?
    922942
    923943        istype & ?|?( istype & is, unsigned short int & usi ) {
     
    925945                return is;
    926946        } // ?|?
     947        void ?|?( istype & is, unsigned short int & usi ) {
     948                (istype &)(is | usi); ends( is );
     949        } // ?|?
    927950
    928951        istype & ?|?( istype & is, int & i ) {
     
    930953                return is;
    931954        } // ?|?
     955        void ?|?( istype & is, int & i ) {
     956                (istype &)(is | i); ends( is );
     957        } // ?|?
    932958
    933959        istype & ?|?( istype & is, unsigned int & ui ) {
     
    935961                return is;
    936962        } // ?|?
     963        void ?|?( istype & is, unsigned int & ui ) {
     964                (istype &)(is | ui); ends( is );
     965        } // ?|?
    937966
    938967        istype & ?|?( istype & is, long int & li ) {
     
    940969                return is;
    941970        } // ?|?
     971        void ?|?( istype & is, long int & li ) {
     972                (istype &)(is | li); ends( is );
     973        } // ?|?
    942974
    943975        istype & ?|?( istype & is, unsigned long int & ulli ) {
     
    945977                return is;
    946978        } // ?|?
     979        void ?|?( istype & is, unsigned long int & ulli ) {
     980                (istype &)(is | ulli); ends( is );
     981        } // ?|?
    947982
    948983        istype & ?|?( istype & is, long long int & lli ) {
     
    950985                return is;
    951986        } // ?|?
     987        void ?|?( istype & is, long long int & lli ) {
     988                (istype &)(is | lli); ends( is );
     989        } // ?|?
    952990
    953991        istype & ?|?( istype & is, unsigned long long int & ulli ) {
     
    955993                return is;
    956994        } // ?|?
     995        void & ?|?( istype & is, unsigned long long int & ulli ) {
     996                (istype &)(is | ulli); ends( is );
     997        } // ?|?
    957998
    958999#if defined( __SIZEOF_INT128__ )
    959         istype & ?|?( istype & is, int128 & i128 ) {
    960                 return (istype &)(is | (unsigned int128 &)i128);
    961         } // ?|?
    962 
    963         istype & ?|?( istype & is, unsigned int128 & ui128 ) {
     1000        istype & ?|?( istype & is, int128 & llli ) {
     1001                return (istype &)(is | (unsigned int128 &)llli);
     1002        } // ?|?
     1003        void ?|?( istype & is, int128 & llli ) {
     1004                (istype &)(is | llli); ends( is );
     1005        } // ?|?
     1006
     1007        istype & ?|?( istype & is, unsigned int128 & ullli ) {
    9641008                char s[40];
    9651009                bool sign = false;
     
    9681012                // If the input is too large, the value returned is undefined. If there is no input, no value is returned
    9691013                if ( fmt( is, "%39[0-9]%*[0-9]", s ) == 1 ) {   // take first 39 characters, ignore remaining
    970                         ui128 = 0;
     1014                        ullli = 0;
    9711015                        for ( unsigned int i = 0; s[i] != '\0'; i += 1 ) {
    972                                 ui128 = ui128 * 10 + s[i] - '0';
     1016                                ullli = ullli * 10 + s[i] - '0';
    9731017                        } // for
    974                         if ( sign ) ui128 = -ui128;
     1018                        if ( sign ) ullli = -ullli;
    9751019                } else if ( sign ) ungetc( is, '-' );                   // return minus when no digits
    9761020                return is;
     1021        } // ?|?
     1022        void ?|?( istype & is, unsigned int128 & ullli ) {
     1023                (istype &)(is | ullli); ends( is );
    9771024        } // ?|?
    9781025#endif // __SIZEOF_INT128__
     
    9821029                return is;
    9831030        } // ?|?
     1031        void ?|?( istype & is, float & f ) {
     1032                (istype &)(is | f); ends( is );
     1033        } // ?|?
    9841034
    9851035        istype & ?|?( istype & is, double & d ) {
     
    9871037                return is;
    9881038        } // ?|?
     1039        void ?|?( istype & is, double & d ) {
     1040                (istype &)(is | d); ends( is );
     1041        } // ?|?
    9891042
    9901043        istype & ?|?( istype & is, long double & ld ) {
     
    9921045                return is;
    9931046        } // ?|?
    994 
     1047        void ?|?( istype & is, long double & ld ) {
     1048                (istype &)(is | ld); ends( is );
     1049        } // ?|?
    9951050
    9961051        istype & ?|?( istype & is, float _Complex & fc ) {
     
    10001055                return is;
    10011056        } // ?|?
     1057        void ?|?( istype & is, float _Complex & fc ) {
     1058                (istype &)(is | fc); ends( is );
     1059        } // ?|?
    10021060
    10031061        istype & ?|?( istype & is, double _Complex & dc ) {
     
    10071065                return is;
    10081066        } // ?|?
     1067        void ?|?( istype & is, double _Complex & dc ) {
     1068                (istype &)(is | dc); ends( is );
     1069        } // ?|?
    10091070
    10101071        istype & ?|?( istype & is, long double _Complex & ldc ) {
     
    10141075                return is;
    10151076        } // ?|?
     1077        void ?|?( istype & is, long double _Complex & ldc ) {
     1078                (istype &)(is | ldc); ends( is );
     1079        } // ?|?
    10161080
    10171081        // istype & ?|?( istype & is, const char fmt[] ) {
     
    10201084        // } // ?|?
    10211085
    1022         istype & ?|?( istype & is, char * s ) {
     1086        istype & ?|?( istype & is, char s[] ) {
    10231087                fmt( is, "%s", s );
    10241088                return is;
     1089        } // ?|?
     1090        void ?|?( istype & is, char s[] ) {
     1091                (istype &)(is | s); ends( is );
    10251092        } // ?|?
    10261093
     
    10291096                return manip( is );
    10301097        } // ?|?
     1098        void ?|?( istype & is, istype & (* manip)( istype & ) ) {
     1099                manip( is ); ends( is );
     1100        } // ?|?
    10311101
    10321102        istype & nl( istype & is ) {
     
    10441114                return is;
    10451115        } // nlOff
     1116
     1117        istype & acquire( istype & is ) {
     1118                acquire( is );                                                                  // call void returning
     1119                return is;
     1120        } // acquire
    10461121} // distribution
    10471122
    10481123// *********************************** manipulators ***********************************
    10491124
    1050 forall( dtype istype | istream( istype ) )
    1051 istype & ?|?( istype & is, _Istream_Cstr f ) {
    1052         // skip xxx
    1053         if ( ! f.s ) {
    1054                 // printf( "skip %s %d\n", f.scanset, f.wd );
    1055                 if ( f.wd == -1 ) fmt( is, f.scanset, "" );             // no input arguments
    1056                 else for ( f.wd ) fmt( is, "%*c" );
    1057                 return is;
    1058         } // if
    1059         size_t len = 0;
    1060         if ( f.scanset ) len = strlen( f.scanset );
    1061         char fmtstr[len + 16];
    1062         int start = 1;
    1063         fmtstr[0] = '%';
    1064         if ( f.flags.ignore ) { fmtstr[1] = '*'; start += 1; }
    1065         if ( f.wd != -1 ) { start += sprintf( &fmtstr[start], "%d", f.wd ); }
    1066         // cstr %s, %*s, %ws, %*ws
    1067         if ( ! f.scanset ) {
    1068                 fmtstr[start] = 's'; fmtstr[start + 1] = '\0';
    1069                 // printf( "cstr %s\n", fmtstr );
     1125forall( istype & | istream( istype ) ) {
     1126        istype & ?|?( istype & is, _Istream_Cstr f ) {
     1127                // skip xxx
     1128                if ( ! f.s ) {
     1129                        // printf( "skip %s %d\n", f.scanset, f.wd );
     1130                        if ( f.wd == -1 ) fmt( is, f.scanset, "" );             // no input arguments
     1131                        else for ( f.wd ) fmt( is, "%*c" );
     1132                        return is;
     1133                } // if
     1134                size_t len = 0;
     1135                if ( f.scanset ) len = strlen( f.scanset );
     1136                char fmtstr[len + 16];
     1137                int start = 1;
     1138                fmtstr[0] = '%';
     1139                if ( f.flags.ignore ) { fmtstr[1] = '*'; start += 1; }
     1140                if ( f.wd != -1 ) { start += sprintf( &fmtstr[start], "%d", f.wd ); }
     1141                // cstr %s, %*s, %ws, %*ws
     1142                if ( ! f.scanset ) {
     1143                        fmtstr[start] = 's'; fmtstr[start + 1] = '\0';
     1144                        // printf( "cstr %s\n", fmtstr );
     1145                        fmt( is, fmtstr, f.s );
     1146                        return is;
     1147                } // if
     1148                // incl %[xxx],  %*[xxx],  %w[xxx],  %*w[xxx]
     1149                // excl %[^xxx], %*[^xxx], %w[^xxx], %*w[^xxx]
     1150                fmtstr[start] = '['; start += 1;
     1151                if ( f.flags.inex ) { fmtstr[start] = '^'; start += 1; }
     1152                strcpy( &fmtstr[start], f.scanset );                            // copy includes '\0'
     1153                len += start;
     1154                fmtstr[len] = ']'; fmtstr[len + 1] = '\0';
     1155                // printf( "incl/excl %s\n", fmtstr );
    10701156                fmt( is, fmtstr, f.s );
    10711157                return is;
    1072         } // if
    1073         // incl %[xxx],  %*[xxx],  %w[xxx],  %*w[xxx]
    1074         // excl %[^xxx], %*[^xxx], %w[^xxx], %*w[^xxx]
    1075         fmtstr[start] = '['; start += 1;
    1076         if ( f.flags.inex ) { fmtstr[start] = '^'; start += 1; }
    1077         strcpy( &fmtstr[start], f.scanset );                            // copy includes '\0'
    1078         len += start;
    1079         fmtstr[len] = ']'; fmtstr[len + 1] = '\0';
    1080         // printf( "incl/excl %s\n", fmtstr );
    1081         fmt( is, fmtstr, f.s );
    1082         return is;
    1083 } // ?|?
    1084 
    1085 forall( dtype istype | istream( istype ) )
    1086 istype & ?|?( istype & is, _Istream_Char f ) {
    1087         fmt( is, "%*c" );                                                                       // argument variable unused
    1088         return is;
    1089 } // ?|?
     1158        } // ?|?
     1159        void ?|?( istype & is, _Istream_Cstr f ) {
     1160                (istype &)(is | f); ends( is );
     1161        } // ?|?
     1162
     1163        istype & ?|?( istype & is, _Istream_Char f ) {
     1164                fmt( is, "%*c" );                                                                       // argument variable unused
     1165                return is;
     1166        } // ?|?
     1167        void ?|?( istype & is, _Istream_Char f ) {
     1168                (istype &)(is | f); ends( is );
     1169        } // ?|?
     1170} // distribution
    10901171
    10911172#define InputFMTImpl( T, CODE ) \
    1092 forall( dtype istype | istream( istype ) ) \
    1093 istype & ?|?( istype & is, _Istream_Manip(T) f ) { \
    1094         enum { size = 16 }; \
    1095         char fmtstr[size]; \
    1096         if ( f.wd == -1 ) { \
    1097                 snprintf( fmtstr, size, "%%%s%s", f.ignore ? "*" : "", CODE ); \
    1098         } else { \
    1099                 snprintf( fmtstr, size, "%%%s%d%s", f.ignore ? "*" : "", f.wd, CODE ); \
    1100         } /* if */ \
    1101         /* printf( "%d %s %p\n", f.wd, fmtstr, &f.val ); */ \
    1102         fmt( is, fmtstr, &f.val ); \
    1103         return is; \
    1104 } // ?|?
     1173forall( istype & | istream( istype ) ) { \
     1174        istype & ?|?( istype & is, _Istream_Manip(T) f ) { \
     1175                enum { size = 16 }; \
     1176                char fmtstr[size]; \
     1177                if ( f.wd == -1 ) { \
     1178                        snprintf( fmtstr, size, "%%%s%s", f.ignore ? "*" : "", CODE ); \
     1179                } else { \
     1180                        snprintf( fmtstr, size, "%%%s%d%s", f.ignore ? "*" : "", f.wd, CODE ); \
     1181                } /* if */ \
     1182                /* printf( "%d %s %p\n", f.wd, fmtstr, &f.val ); */ \
     1183                fmt( is, fmtstr, &f.val ); \
     1184                return is; \
     1185        } /* ?|? */ \
     1186        void ?|?( istype & is, _Istream_Manip(T) f ) { \
     1187                (istype &)(is | f); ends( is ); \
     1188        } /* ?|? */ \
     1189} // distribution
    11051190
    11061191InputFMTImpl( signed char, "hhi" )
     
    11191204InputFMTImpl( long double, "Lf" )
    11201205
    1121 forall( dtype istype | istream( istype ) )
    1122 istype & ?|?( istype & is, _Istream_Manip(float _Complex) fc ) {
    1123         float re, im;
    1124         _Istream_Manip(float) fmtuc @= { re, fc.wd, fc.ignore };
    1125         is | fmtuc;
    1126         &fmtuc.val = &im;
    1127         is | fmtuc;
    1128         if ( ! fc.ignore ) fc.val = re + im * _Complex_I;       // re/im are uninitialized for ignore
    1129         return is;
    1130 } // ?|?
    1131 
    1132 forall( dtype istype | istream( istype ) )
    1133 istype & ?|?( istype & is, _Istream_Manip(double _Complex) dc ) {
    1134         double re, im;
    1135         _Istream_Manip(double) fmtuc @= { re, dc.wd, dc.ignore };
    1136         is | fmtuc;
    1137         &fmtuc.val = &im;
    1138         is | fmtuc;
    1139         if ( ! dc.ignore ) dc.val = re + im * _Complex_I;       // re/im are uninitialized for ignore
    1140         return is;
    1141 } // ?|?
    1142 
    1143 forall( dtype istype | istream( istype ) )
    1144 istype & ?|?( istype & is, _Istream_Manip(long double _Complex) ldc ) {
    1145         long double re, im;
    1146         _Istream_Manip(long double) fmtuc @= { re, ldc.wd, ldc.ignore };
    1147         is | fmtuc;
    1148         &fmtuc.val = &im;
    1149         is | fmtuc;
    1150         if ( ! ldc.ignore ) ldc.val = re + im * _Complex_I;     // re/im are uninitialized for ignore
    1151         return is;
    1152 } // ?|?
     1206forall( istype & | istream( istype ) ) {
     1207        istype & ?|?( istype & is, _Istream_Manip(float _Complex) fc ) {
     1208                float re, im;
     1209                _Istream_Manip(float) fmtuc @= { re, fc.wd, fc.ignore };
     1210                is | fmtuc;
     1211                &fmtuc.val = &im;
     1212                is | fmtuc;
     1213                if ( ! fc.ignore ) fc.val = re + im * _Complex_I; // re/im are uninitialized for ignore
     1214                return is;
     1215        } // ?|?
     1216        void ?|?( istype & is, _Istream_Manip(float _Complex) fc ) {
     1217                (istype &)(is | fc); ends( is );
     1218        } // ?|?
     1219
     1220        istype & ?|?( istype & is, _Istream_Manip(double _Complex) dc ) {
     1221                double re, im;
     1222                _Istream_Manip(double) fmtuc @= { re, dc.wd, dc.ignore };
     1223                is | fmtuc;
     1224                &fmtuc.val = &im;
     1225                is | fmtuc;
     1226                if ( ! dc.ignore ) dc.val = re + im * _Complex_I; // re/im are uninitialized for ignore
     1227                return is;
     1228        } // ?|?
     1229        void ?|?( istype & is, _Istream_Manip(double _Complex) dc ) {
     1230                (istype &)(is | dc); ends( is );
     1231        } // ?|?
     1232
     1233        istype & ?|?( istype & is, _Istream_Manip(long double _Complex) ldc ) {
     1234                long double re, im;
     1235                _Istream_Manip(long double) fmtuc @= { re, ldc.wd, ldc.ignore };
     1236                is | fmtuc;
     1237                &fmtuc.val = &im;
     1238                is | fmtuc;
     1239                if ( ! ldc.ignore ) ldc.val = re + im * _Complex_I;     // re/im are uninitialized for ignore
     1240                return is;
     1241        } // ?|?
     1242        void ?|?( istype & is, _Istream_Manip(long double _Complex) ldc ) {
     1243                (istype &)(is | ldc); ends( is );
     1244        } // ?|?
     1245} // distribution
    11531246
    11541247// Local Variables: //
  • libcfa/src/iostream.hfa

    r342af53 r8e4aa05  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Aug 11 22:16:14 2020
    13 // Update Count     : 350
     12// Last Modified On : Tue Mar  2 14:05:08 2021
     13// Update Count     : 369
    1414//
    1515
     
    2222
    2323
    24 trait ostream( dtype ostype ) {
     24trait ostream( ostype & ) {
    2525        // private
    2626        bool $sepPrt( ostype & );                                                       // get separator state (on/off)
     
    5454        ostype & write( ostype &, const char [], size_t );
    5555        int fmt( ostype &, const char format[], ... ) __attribute__(( format(printf, 2, 3) ));
     56        void acquire( ostype & );
    5657}; // ostream
    5758
    58 // trait writeable( otype T ) {
    59 //      forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, T );
     59// trait writeable( T ) {
     60//      forall( ostype & | ostream( ostype ) ) ostype & ?|?( ostype &, T );
    6061// }; // writeable
    6162
    62 trait writeable( otype T, dtype ostype | ostream( ostype ) ) {
     63trait writeable( T, ostype & | ostream( ostype ) ) {
    6364        ostype & ?|?( ostype &, T );
    6465}; // writeable
     
    6667// implement writable for intrinsic types
    6768
    68 forall( dtype ostype | ostream( ostype ) ) {
     69forall( ostype & | ostream( ostype ) ) {
    6970        ostype & ?|?( ostype &, bool );
    7071        void ?|?( ostype &, bool );
     
    137138        ostype & nlOn( ostype & );
    138139        ostype & nlOff( ostype & );
     140        ostype & acquire( ostype & );
    139141} // distribution
    140142
    141143// tuples
    142 forall( dtype ostype, otype T, ttype Params | writeable( T, ostype ) | { ostype & ?|?( ostype &, Params ); } ) {
     144forall( ostype &, T, Params... | writeable( T, ostype ) | { ostype & ?|?( ostype &, Params ); } ) {
    143145        ostype & ?|?( ostype & os, T arg, Params rest );
    144146        void ?|?( ostype & os, T arg, Params rest );
     
    146148
    147149// writes the range [begin, end) to the given stream
    148 forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) ) {
     150forall( ostype &, elt_type | writeable( elt_type, ostype ), iterator_type | iterator( iterator_type, elt_type ) ) {
    149151        void write( iterator_type begin, iterator_type end, ostype & os );
    150152        void write_reverse( iterator_type begin, iterator_type end, ostype & os );
     
    153155// *********************************** manipulators ***********************************
    154156
    155 forall( otype T )
     157forall( T )
    156158struct _Ostream_Manip {
    157159        T val;                                                                                          // polymorphic base-type
     
    193195        _Ostream_Manip(T) & sign( _Ostream_Manip(T) & fmt ) { fmt.flags.sign = true; return fmt; } \
    194196} /* distribution */ \
    195 forall( dtype ostype | ostream( ostype ) ) { \
     197forall( ostype & | ostream( ostype ) ) { \
    196198        ostype & ?|?( ostype & os, _Ostream_Manip(T) f ); \
    197199        void ?|?( ostype & os, _Ostream_Manip(T) f ); \
     
    234236        _Ostream_Manip(T) & nodp( _Ostream_Manip(T) & fmt ) { fmt.flags.nobsdp = true; return fmt; } \
    235237} /* distribution */ \
    236 forall( dtype ostype | ostream( ostype ) ) { \
     238forall( ostype & | ostream( ostype ) ) { \
    237239        ostype & ?|?( ostype & os, _Ostream_Manip(T) f ); \
    238240        void ?|?( ostype & os, _Ostream_Manip(T) f ); \
     
    254256        _Ostream_Manip(char) & nobase( _Ostream_Manip(char) & fmt ) { fmt.flags.nobsdp = true; return fmt; }
    255257} // distribution
    256 forall( dtype ostype | ostream( ostype ) ) {
     258forall( ostype & | ostream( ostype ) ) {
    257259        ostype & ?|?( ostype & os, _Ostream_Manip(char) f );
    258260        void ?|?( ostype & os, _Ostream_Manip(char) f );
     
    272274        _Ostream_Manip(const char *) & nobase( _Ostream_Manip(const char *) & fmt ) { fmt.flags.nobsdp = true; return fmt; }
    273275} // distribution
    274 forall( dtype ostype | ostream( ostype ) ) {
     276forall( ostype & | ostream( ostype ) ) {
    275277        ostype & ?|?( ostype & os, _Ostream_Manip(const char *) f );
    276278        void ?|?( ostype & os, _Ostream_Manip(const char *) f );
     
    281283
    282284
    283 trait istream( dtype istype ) {
     285trait istream( istype & ) {
    284286        void nlOn( istype & );                                                          // read newline
    285287        void nlOff( istype & );                                                         // scan newline
    286288        bool getANL( istype & );                                                        // get scan newline (on/off)
     289
     290        void ends( istype & os );                                                       // end of output statement
    287291        int fail( istype & );
    288292        int eof( istype & );
     
    292296        istype & ungetc( istype &, char );
    293297        int fmt( istype &, const char format[], ... ) __attribute__(( format(scanf, 2, 3) ));
     298        void acquire( istype & );
    294299}; // istream
    295300
    296 trait readable( otype T ) {
    297         forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, T );
     301trait readable( T ) {
     302        forall( istype & | istream( istype ) ) istype & ?|?( istype &, T );
    298303}; // readable
    299304
    300 forall( dtype istype | istream( istype ) ) {
     305forall( istype & | istream( istype ) ) {
    301306        istype & ?|?( istype &, bool & );
     307        void ?|?( istype &, bool & );
    302308
    303309        istype & ?|?( istype &, char & );
     310        void ?|?( istype &, char & );
    304311        istype & ?|?( istype &, signed char & );
     312        void ?|?( istype &, signed char & );
    305313        istype & ?|?( istype &, unsigned char & );
     314        void ?|?( istype &, unsigned char & );
    306315
    307316        istype & ?|?( istype &, short int & );
     317        void ?|?( istype &, short int & );
    308318        istype & ?|?( istype &, unsigned short int & );
     319        void ?|?( istype &, unsigned short int & );
    309320        istype & ?|?( istype &, int & );
     321        void ?|?( istype &, int & );
    310322        istype & ?|?( istype &, unsigned int & );
     323        void ?|?( istype &, unsigned int & );
    311324        istype & ?|?( istype &, long int & );
     325        void ?|?( istype &, long int & );
    312326        istype & ?|?( istype &, unsigned long int & );
     327        void ?|?( istype &, unsigned long int & );
    313328        istype & ?|?( istype &, long long int & );
     329        void ?|?( istype &, long long int & );
    314330        istype & ?|?( istype &, unsigned long long int & );
     331        void ?|?( istype &, unsigned long long int & );
    315332#if defined( __SIZEOF_INT128__ )
    316333        istype & ?|?( istype &, int128 & );
     334        void ?|?( istype &, int128 & );
    317335        istype & ?|?( istype &, unsigned int128 & );
     336        void ?|?( istype &, unsigned int128 & );
    318337#endif // __SIZEOF_INT128__
    319338
    320339        istype & ?|?( istype &, float & );
     340        void ?|?( istype &, float & );
    321341        istype & ?|?( istype &, double & );
     342        void ?|?( istype &, double & );
    322343        istype & ?|?( istype &, long double & );
     344        void ?|?( istype &, long double & );
    323345
    324346        istype & ?|?( istype &, float _Complex & );
     347        void ?|?( istype &, float _Complex & );
    325348        istype & ?|?( istype &, double _Complex & );
     349        void ?|?( istype &, double _Complex & );
    326350        istype & ?|?( istype &, long double _Complex & );
     351        void ?|?( istype &, long double _Complex & );
    327352
    328353//      istype & ?|?( istype &, const char [] );
    329         istype & ?|?( istype &, char * );
     354        istype & ?|?( istype &, char [] );
     355        void ?|?( istype &, char [] );
    330356
    331357        // manipulators
    332358        istype & ?|?( istype &, istype & (*)( istype & ) );
     359        void ?|?( istype &, istype & (*)( istype & ) );
    333360        istype & nl( istype & is );
    334361        istype & nlOn( istype & );
    335362        istype & nlOff( istype & );
     363        istype & acquire( istype & );
    336364} // distribution
    337365
     
    363391        _Istream_Cstr & wdi( unsigned int w, _Istream_Cstr & fmt ) { fmt.wd = w; return fmt; }
    364392} // distribution
    365 forall( dtype istype | istream( istype ) ) istype & ?|?( istype & is, _Istream_Cstr f );
     393forall( istype & | istream( istype ) ) {
     394        istype & ?|?( istype & is, _Istream_Cstr f );
     395        void ?|?( istype & is, _Istream_Cstr f );
     396}
    366397
    367398struct _Istream_Char {
     
    373404        _Istream_Char & ignore( _Istream_Char & fmt ) { fmt.ignore = true; return fmt; }
    374405} // distribution
    375 forall( dtype istype | istream( istype ) ) istype & ?|?( istype & is, _Istream_Char f );
    376 
    377 forall( dtype T | sized( T ) )
     406forall( istype & | istream( istype ) ) {
     407        istype & ?|?( istype & is, _Istream_Char f );
     408        void ?|?( istype & is, _Istream_Char f );
     409}
     410
     411forall( T & | sized( T ) )
    378412struct _Istream_Manip {
    379413        T & val;                                                                                        // polymorphic base-type
     
    389423        _Istream_Manip(T) & wdi( unsigned int w, _Istream_Manip(T) & fmt ) { fmt.wd = w; return fmt; } \
    390424} /* distribution */ \
    391 forall( dtype istype | istream( istype ) ) { \
     425forall( istype & | istream( istype ) ) { \
    392426        istype & ?|?( istype & is, _Istream_Manip(T) f ); \
     427        void ?|?( istype & is, _Istream_Manip(T) f ); \
    393428} // ?|?
    394429
     
    418453#include <time_t.hfa>                                                                   // Duration (constructors) / Time (constructors)
    419454
    420 forall( dtype ostype | ostream( ostype ) ) {
     455forall( ostype & | ostream( ostype ) ) {
    421456        ostype & ?|?( ostype & os, Duration dur );
    422457        void ?|?( ostype & os, Duration dur );
  • libcfa/src/iterator.cfa

    r342af53 r8e4aa05  
    1616#include "iterator.hfa"
    1717
    18 forall( otype iterator_type, otype elt_type | iterator( iterator_type, elt_type ) )
     18forall( iterator_type, elt_type | iterator( iterator_type, elt_type ) )
    1919void for_each( iterator_type begin, iterator_type end, void (* func)( elt_type ) ) {
    2020        for ( iterator_type i = begin; i != end; ++i ) {
     
    2323} // for_each
    2424
    25 forall( otype iterator_type, otype elt_type | iterator( iterator_type, elt_type ) )
     25forall( iterator_type, elt_type | iterator( iterator_type, elt_type ) )
    2626void for_each_reverse( iterator_type begin, iterator_type end, void (* func)( elt_type ) ) {
    2727        for ( iterator_type i = end; i != begin; ) {
  • libcfa/src/iterator.hfa

    r342af53 r8e4aa05  
    1717
    1818// An iterator can be used to traverse a data structure.
    19 trait iterator( otype iterator_type, otype elt_type ) {
     19trait iterator( iterator_type, elt_type ) {
    2020        // point to the next element
    2121//      iterator_type ?++( iterator_type & );
     
    3131};
    3232
    33 trait iterator_for( otype iterator_type, otype collection_type, otype elt_type | iterator( iterator_type, elt_type ) ) {
     33trait iterator_for( iterator_type, collection_type, elt_type | iterator( iterator_type, elt_type ) ) {
    3434//      [ iterator_type begin, iterator_type end ] get_iterators( collection_type );
    3535        iterator_type begin( collection_type );
     
    3737};
    3838
    39 forall( otype iterator_type, otype elt_type | iterator( iterator_type, elt_type ) )
     39forall( iterator_type, elt_type | iterator( iterator_type, elt_type ) )
    4040void for_each( iterator_type begin, iterator_type end, void (* func)( elt_type ) );
    4141
    42 forall( otype iterator_type, otype elt_type | iterator( iterator_type, elt_type ) )
     42forall( iterator_type, elt_type | iterator( iterator_type, elt_type ) )
    4343void for_each_reverse( iterator_type begin, iterator_type end, void (* func)( elt_type ) );
    4444
  • libcfa/src/math.hfa

    r342af53 r8e4aa05  
    286286        unsigned long long int floor( unsigned long long int n, unsigned long long int align ) { return n / align * align; }
    287287
    288         // forall( otype T | { T ?/?( T, T ); T ?*?( T, T ); } )
     288        // forall( T | { T ?/?( T, T ); T ?*?( T, T ); } )
    289289        // T floor( T n, T align ) { return n / align * align; }
    290290
     
    300300        unsigned long long int ceiling_div( unsigned long long int n, unsigned long long int align ) { return (n + (align - 1)) / align; }
    301301
    302         // forall( otype T | { T ?+?( T, T ); T ?-?( T, T ); T ?%?( T, T ); } )
     302        // forall( T | { T ?+?( T, T ); T ?-?( T, T ); T ?%?( T, T ); } )
    303303        // T ceiling_div( T n, T align ) { verify( is_pow2( align ) );return (n + (align - 1)) / align; }
    304304       
     
    315315        unsigned long long int ceiling( unsigned long long int n, unsigned long long int align ) { return floor( n + (n % align != 0 ? align - 1 : 0), align ); }
    316316
    317         // forall( otype T | { void ?{}( T &, one_t ); T ?+?( T, T ); T ?-?( T, T ); T ?/?( T, T ); } )
     317        // forall( T | { void ?{}( T &, one_t ); T ?+?( T, T ); T ?-?( T, T ); T ?/?( T, T ); } )
    318318        // T ceiling( T n, T align ) { return return floor( n + (n % align != 0 ? align - 1 : 0), align ); *}
    319319
     
    414414
    415415static inline {
    416         forall( otype T | { void ?{}( T &, one_t ); T ?+?( T, T ); T ?-?( T, T );T ?*?( T, T ); } )
     416        forall( T | { void ?{}( T &, one_t ); T ?+?( T, T ); T ?-?( T, T );T ?*?( T, T ); } )
    417417        T lerp( T x, T y, T a ) { return x * ((T){1} - a) + y * a; }
    418418
    419         forall( otype T | { void ?{}( T &, zero_t ); void ?{}( T &, one_t ); int ?<?( T, T ); } )
     419        forall( T | { void ?{}( T &, zero_t ); void ?{}( T &, one_t ); int ?<?( T, T ); } )
    420420        T step( T edge, T x ) { return x < edge ? (T){0} : (T){1}; }
    421421
    422         forall( otype T | { void ?{}( T &, int ); T clamp( T, T, T ); T ?-?( T, T ); T ?*?( T, T ); T ?/?( T, T ); } )
     422        forall( T | { void ?{}( T &, int ); T clamp( T, T, T ); T ?-?( T, T ); T ?*?( T, T ); T ?/?( T, T ); } )
    423423        T smoothstep( T edge0, T edge1, T x ) { T t = clamp( (x - edge0) / (edge1 - edge0), (T){0}, (T){1} ); return t * t * ((T){3} - (T){2} * t); }
    424424} // distribution
  • libcfa/src/memory.cfa

    r342af53 r8e4aa05  
    1010// Created On       : Tue Jun  2 16:48:00 2020
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Tue Jun  3 12:30:00 2020
    13 // Update Count     : 0
     12// Last Modified On : Mon Feb  1 16:10:00 2021
     13// Update Count     : 1
    1414//
    1515
     
    1818
    1919// Internal data object.
    20 forall(dtype T | sized(T), ttype Args | { void ?{}(T &, Args); })
     20forall(T & | sized(T), Args... | { void ?{}(T &, Args); })
    2121void ?{}(counter_data(T) & this, Args args) {
    2222        (this.counter){1};
     
    2424}
    2525
    26 forall(dtype T | sized(T) | { void ^?{}(T &); })
     26forall(T & | sized(T) | { void ^?{}(T &); })
    2727void ^?{}(counter_data(T) & this) {
    2828        assert(0 == this.counter);
     
    3131
    3232// This is one of many pointers keeping this alive.
    33 forall(dtype T | sized(T))
     33forall(T & | sized(T))
    3434void ?{}(counter_ptr(T) & this) {
    3535        this.data = 0p;
    3636}
    3737
    38 forall(dtype T | sized(T))
     38forall(T & | sized(T))
    3939void ?{}(counter_ptr(T) & this, zero_t) {
    4040        this.data = 0p;
    4141}
    4242
    43 forall(dtype T | sized(T) | { void ^?{}(T &); })
     43forall(T & | sized(T) | { void ^?{}(T &); })
    4444static void internal_decrement(counter_ptr(T) & this) {
    4545        if (this.data && 0 == --this.data->counter) {
     
    4848}
    4949
    50 forall(dtype T | sized(T))
     50forall(T & | sized(T))
    5151static void internal_copy(counter_ptr(T) & this, counter_ptr(T) & that) {
    5252        this.data = that.data;
     
    5656}
    5757
    58 forall(dtype T | sized(T) | { void ^?{}(T &); })
     58forall(T & | sized(T))
    5959void ?{}(counter_ptr(T) & this, counter_ptr(T) that) {
    6060        // `that` is a copy but it should have neither a constructor
    6161        // nor destructor run on it so it shouldn't need adjustment.
    62         internal_decrement(this);
    6362        internal_copy(this, that);
    6463}
    6564
    66 forall(dtype T | sized(T), ttype Args | { void ?{}(T&, Args); })
     65forall(T & | sized(T), Args... | { void ?{}(T&, Args); })
    6766void ?{}(counter_ptr(T) & this, Args args) {
    68         this.data = (counter_data(T)*)new(args);
     67        this.data = malloc();
     68        this.data->counter = 1;
     69        (this.data->object){args};
    6970}
    7071
    71 forall(dtype T | sized(T) | { void ^?{}(T &); })
     72forall(T & | sized(T) | { void ^?{}(T &); })
    7273void ^?{}(counter_ptr(T) & this) {
    7374        internal_decrement(this);
    7475}
    7576
    76 forall(dtype T | sized(T))
     77forall(T & | sized(T))
    7778T & *?(counter_ptr(T) & this) {
    7879        return *((this.data) ? &this.data->object : 0p);
    7980}
    8081
    81 forall(dtype T | sized(T) | { void ^?{}(T &); })
     82forall(T & | sized(T) | { void ^?{}(T &); })
    8283void ?=?(counter_ptr(T) & this, counter_ptr(T) that) {
    8384        if (this.data != that.data) {
     
    8788}
    8889
    89 forall(dtype T | sized(T) | { void ^?{}(T &); })
     90forall(T & | sized(T) | { void ^?{}(T &); })
    9091void ?=?(counter_ptr(T) & this, zero_t) {
    9192        internal_decrement(this);
     
    9394}
    9495
    95 forall(dtype T | sized(T))
     96forall(T & | sized(T))
    9697int ?==?(counter_ptr(T) const & this, counter_ptr(T) const & that) {
    9798        return this.data == that.data;
    9899}
    99100
    100 forall(dtype T | sized(T))
     101forall(T & | sized(T))
    101102int ?!=?(counter_ptr(T) const & this, counter_ptr(T) const & that) {
    102103        return !?==?(this, that);
    103104}
    104105
    105 forall(dtype T | sized(T))
     106forall(T & | sized(T))
    106107int ?==?(counter_ptr(T) const & this, zero_t) {
    107108        return this.data == 0;
    108109}
    109110
    110 forall(dtype T | sized(T))
     111forall(T & | sized(T))
    111112int ?!=?(counter_ptr(T) const & this, zero_t) {
    112113        return !?==?(this, (zero_t)0);
     
    114115
    115116// This is the only pointer that keeps this alive.
    116 forall(dtype T)
     117forall(T &)
    117118void ?{}(unique_ptr(T) & this) {
    118119        this.data = 0p;
    119120}
    120121
    121 forall(dtype T)
     122forall(T &)
    122123void ?{}(unique_ptr(T) & this, zero_t) {
    123124        this.data = 0p;
    124125}
    125126
    126 forall(dtype T | sized(T), ttype Args | { void ?{}(T &, Args); })
     127forall(T & | sized(T), Args... | { void ?{}(T &, Args); })
    127128void ?{}(unique_ptr(T) & this, Args args) {
    128         this.data = (T *)new(args);
     129        this.data = malloc();
     130        (*this.data){args};
    129131}
    130132
    131 forall(dtype T | { void ^?{}(T &); })
     133forall(T & | { void ^?{}(T &); })
    132134void ^?{}(unique_ptr(T) & this) {
    133135        delete(this.data);
    134136}
    135137
    136 forall(dtype T)
     138forall(T &)
    137139T & *?(unique_ptr(T) & this) {
    138140        return *this.data;
    139141}
    140142
    141 forall(dtype T | { void ^?{}(T &); })
     143forall(T & | { void ^?{}(T &); })
    142144void ?=?(unique_ptr(T) & this, zero_t) {
    143145        delete(this.data);
     
    145147}
    146148
    147 forall(dtype T | { void ^?{}(T &); })
     149forall(T & | { void ^?{}(T &); })
    148150void move(unique_ptr(T) & this, unique_ptr(T) & that) {
    149151        delete(this.data);
     
    152154}
    153155
    154 forall(dtype T)
     156forall(T &)
    155157int ?==?(unique_ptr(T) const & this, unique_ptr(T) const & that) {
    156158        return this.data == that.data;
    157159}
    158160
    159 forall(dtype T)
     161forall(T &)
    160162int ?!=?(unique_ptr(T) const & this, unique_ptr(T) const & that) {
    161163        return !?==?(this, that);
    162164}
    163165
    164 forall(dtype T)
     166forall(T &)
    165167int ?==?(unique_ptr(T) const & this, zero_t) {
    166168        return this.data == 0;
    167169}
    168170
    169 forall(dtype T)
     171forall(T &)
    170172int ?!=?(unique_ptr(T) const & this, zero_t) {
    171173        return !?==?(this, (zero_t)0);
  • libcfa/src/memory.hfa

    r342af53 r8e4aa05  
    1010// Created On       : Tue Jun  2 16:48:00 2020
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Tue Jun  3 12:29:00 2020
    13 // Update Count     : 0
     12// Last Modified On : Fri Jan 29 15:52:00 2021
     13// Update Count     : 1
    1414//
    1515
     
    1717
    1818// Internal data object.
    19 forall(dtype T | sized(T)) {
    20         struct counter_data {
    21                 unsigned int counter;
    22                 T object;
    23         };
     19forall(T & | sized(T))
     20struct counter_data {
     21        unsigned int counter;
     22        T object;
     23};
    2424
    25         forall(ttype Args | { void ?{}(T &, Args); })
    26         void ?{}(counter_data(T) & this, Args args);
     25forall(T & | sized(T), Args... | { void ?{}(T &, Args); })
     26void ?{}(counter_data(T) & this, Args args);
    2727
    28         forall( | { void ^?{}(T &); })
    29         void ^?{}(counter_data(T) & this);
    30 }
     28forall(T & | sized(T) | { void ^?{}(T &); })
     29void ^?{}(counter_data(T) & this);
    3130
    3231// This is one of many pointers keeping this alive.
    33 forall(dtype T | sized(T)) {
    34         struct counter_ptr {
    35                 counter_data(T) * data;
    36         };
     32forall(T & | sized(T))
     33struct counter_ptr {
     34        counter_data(T) * data;
     35};
    3736
    38         void ?{}(counter_ptr(T) & this);
    39         void ?{}(counter_ptr(T) & this, zero_t);
    40         forall( | { void ^?{}(T &); })
    41         void ?{}(counter_ptr(T) & this, counter_ptr(T) that);
    42         forall(ttype Args | { void ?{}(T&, Args); })
    43         void ?{}(counter_ptr(T) & this, Args args);
     37forall(T & | sized(T))
     38void ?{}(counter_ptr(T) & this);
     39forall(T & | sized(T))
     40void ?{}(counter_ptr(T) & this, zero_t);
     41forall(T & | sized(T))
     42void ?{}(counter_ptr(T) & this, counter_ptr(T) that);
     43forall(T & | sized(T), Args... | { void ?{}(T&, Args); })
     44void ?{}(counter_ptr(T) & this, Args args);
    4445
    45         forall( | { void ^?{}(T &); })
    46         void ^?{}(counter_ptr(T) & this);
     46forall(T & | sized(T) | { void ^?{}(T &); })
     47void ^?{}(counter_ptr(T) & this);
    4748
    48         T & *?(counter_ptr(T) & this);
     49forall(T & | sized(T))
     50T & *?(counter_ptr(T) & this);
    4951
    50         forall( | { void ^?{}(T &); })
    51         void ?=?(counter_ptr(T) & this, counter_ptr(T) that);
    52         forall( | { void ^?{}(T &); })
    53         void ?=?(counter_ptr(T) & this, zero_t);
     52forall(T & | sized(T) | { void ^?{}(T &); })
     53void ?=?(counter_ptr(T) & this, counter_ptr(T) that);
     54forall(T & | sized(T) | { void ^?{}(T &); })
     55void ?=?(counter_ptr(T) & this, zero_t);
    5456
    55         int ?==?(counter_ptr(T) const & this, counter_ptr(T) const & that);
    56         int ?!=?(counter_ptr(T) const & this, counter_ptr(T) const & that);
    57         int ?==?(counter_ptr(T) const & this, zero_t);
    58         int ?!=?(counter_ptr(T) const & this, zero_t);
    59 }
     57forall(T & | sized(T))
     58int ?==?(counter_ptr(T) const & this, counter_ptr(T) const & that);
     59forall(T & | sized(T))
     60int ?!=?(counter_ptr(T) const & this, counter_ptr(T) const & that);
     61forall(T & | sized(T))
     62int ?==?(counter_ptr(T) const & this, zero_t);
     63forall(T & | sized(T))
     64int ?!=?(counter_ptr(T) const & this, zero_t);
    6065
    6166// This is the only pointer that keeps this alive.
    62 forall(dtype T) {
    63         struct unique_ptr {
    64                 T * data;
    65         };
     67forall(T &)
     68struct unique_ptr {
     69        T * data;
     70};
    6671
    67         void ?{}(unique_ptr(T) & this);
    68         void ?{}(unique_ptr(T) & this, zero_t);
    69         void ?{}(unique_ptr(T) & this, unique_ptr(T) that) = void;
    70         forall( | sized(T), ttype Args | { void ?{}(T &, Args); })
    71         void ?{}(unique_ptr(T) & this, Args args);
     72forall(T &)
     73void ?{}(unique_ptr(T) & this);
     74forall(T &)
     75void ?{}(unique_ptr(T) & this, zero_t);
     76forall(T &)
     77void ?{}(unique_ptr(T) & this, unique_ptr(T) that) = void;
     78forall(T & | sized(T), Args... | { void ?{}(T &, Args); })
     79void ?{}(unique_ptr(T) & this, Args args);
    7280
    73         forall( | { void ^?{}(T &); })
    74         void ^?{}(unique_ptr(T) & this);
     81forall(T & | { void ^?{}(T &); })
     82void ^?{}(unique_ptr(T) & this);
    7583
    76         T & *?(unique_ptr(T) & this);
     84forall(T & )
     85T & *?(unique_ptr(T) & this);
    7786
    78         void ?=?(unique_ptr(T) & this, unique_ptr(T) that) = void;
    79         forall( | { void ^?{}(T &); })
    80         void ?=?(unique_ptr(T) & this, zero_t);
     87forall(T &)
     88void ?=?(unique_ptr(T) & this, unique_ptr(T) that) = void;
     89forall(T & | { void ^?{}(T &); })
     90void ?=?(unique_ptr(T) & this, zero_t);
    8191
    82         forall( | { void ^?{}(T &); })
    83         void move(unique_ptr(T) & this, unique_ptr(T) & that);
     92forall(T & | { void ^?{}(T &); })
     93void move(unique_ptr(T) & this, unique_ptr(T) & that);
    8494
    85         int ?==?(unique_ptr(T) const & this, unique_ptr(T) const & that);
    86         int ?!=?(unique_ptr(T) const & this, unique_ptr(T) const & that);
    87         int ?==?(unique_ptr(T) const & this, zero_t);
    88         int ?!=?(unique_ptr(T) const & this, zero_t);
    89 }
     95forall(T &)
     96int ?==?(unique_ptr(T) const & this, unique_ptr(T) const & that);
     97forall(T &)
     98int ?!=?(unique_ptr(T) const & this, unique_ptr(T) const & that);
     99forall(T &)
     100int ?==?(unique_ptr(T) const & this, zero_t);
     101forall(T &)
     102int ?!=?(unique_ptr(T) const & this, zero_t);
  • libcfa/src/parseargs.cfa

    r342af53 r8e4aa05  
    3030
    3131static void usage(char * cmd, cfa_option options[], size_t opt_count, const char * usage, FILE * out)  __attribute__ ((noreturn));
    32 
     32//-----------------------------------------------------------------------------
     33// checking
     34static void check_args(cfa_option options[], size_t opt_count) {
     35        for(i; opt_count) {
     36                for(j; opt_count) {
     37                        if(i == j) continue;
     38
     39                        if( options[i].short_name != '\0'
     40                        && options[i].short_name == options[j].short_name)
     41                                abort("Parse Args error: two options have short name '%c' (%zu & %zu)", options[i].short_name, i, j);
     42
     43                        if(0 == strcmp(options[i].long_name, options[j].long_name)) abort("Parse Args error: two options have long name '%s' (%zu & %zu)", options[i].long_name, i, j);
     44                }
     45        }
     46}
     47
     48
     49//-----------------------------------------------------------------------------
     50// Parsing args
    3351void parse_args( cfa_option options[], size_t opt_count, const char * usage, char ** & left ) {
    3452        if( 0p != &cfa_args_argc ) {
     
    4159}
    4260
    43 //-----------------------------------------------------------------------------
    44 // getopt_long wrapping
    4561void parse_args(
    4662        int argc,
     
    5167        char ** & left
    5268) {
     69        check_args(options, opt_count);
     70
     71        int maxv = 'h';
     72        char optstring[opt_count * 3] = { '\0' };
     73        {
     74                int idx = 0;
     75                for(i; opt_count) {
     76                        if (options[i].short_name) {
     77                                maxv = max(options[i].short_name, maxv);
     78                                optstring[idx] = options[i].short_name;
     79                                idx++;
     80                                if(    ((intptr_t)options[i].parse) != ((intptr_t)parse_settrue)
     81                                && ((intptr_t)options[i].parse) != ((intptr_t)parse_setfalse) ) {
     82                                        optstring[idx] = ':';
     83                                        idx++;
     84                                }
     85                        }
     86                }
     87                optstring[idx+0] = 'h';
     88                optstring[idx+1] = '\0';
     89        }
     90
    5391        struct option optarr[opt_count + 2];
    5492        {
     
    5694                for(i; opt_count) {
    5795                        if(options[i].long_name) {
     96                                options[i].val = (options[i].short_name != '\0') ? ((int)options[i].short_name) : ++maxv;
    5897                                optarr[idx].name = options[i].long_name;
    5998                                optarr[idx].flag = 0p;
    60                                 optarr[idx].val  = options[i].short_name;
     99                                optarr[idx].val  = options[i].val;
    61100                                if(    ((intptr_t)options[i].parse) == ((intptr_t)parse_settrue)
    62101                                    || ((intptr_t)options[i].parse) == ((intptr_t)parse_setfalse) ) {
     
    70109                optarr[idx+0].[name, has_arg, flag, val] = ["help", no_argument, 0, 'h'];
    71110                optarr[idx+1].[name, has_arg, flag, val] = [0, no_argument, 0, 0];
    72         }
    73 
    74         char optstring[opt_count * 3] = { '\0' };
    75         {
    76                 int idx = 0;
    77                 for(i; opt_count) {
    78                         optstring[idx] = options[i].short_name;
    79                         idx++;
    80                         if(    ((intptr_t)options[i].parse) != ((intptr_t)parse_settrue)
    81                             && ((intptr_t)options[i].parse) != ((intptr_t)parse_setfalse) ) {
    82                                 optstring[idx] = ':';
    83                                 idx++;
    84                         }
    85                 }
    86                 optstring[idx+0] = 'h';
    87                 optstring[idx+1] = '\0';
    88111        }
    89112
     
    103126                        default:
    104127                                for(i; opt_count) {
    105                                         if(opt == options[i].short_name) {
     128                                        if(opt == options[i].val) {
    106129                                                const char * arg = optarg ? optarg : "";
    107130                                                if( arg[0] == '=' ) { arg++; }
     
    125148        if(hwidth <= 0) hwidth = max;
    126149
    127         fprintf(out, "  -%c, --%-*s   %.*s\n", sn, width, ln, hwidth, help);
     150        char sname[4] = { ' ', ' ', ' ', '\0' };
     151        if(sn != '\0') {
     152                sname[0] = '-';
     153                sname[1] = sn;
     154                sname[2] = ',';
     155        }
     156
     157        fprintf(out, "  %s --%-*s   %.*s\n", sname, width, ln, hwidth, help);
    128158        for() {
    129159                help += min(strlen(help), hwidth);
  • libcfa/src/parseargs.hfa

    r342af53 r8e4aa05  
    22
    33struct cfa_option {
     4      int val; // reserved
    45      char short_name;
    56      const char * long_name;
     
    1314static inline void ?{}( cfa_option & this ) {}
    1415
    15 forall(dtype T | { bool parse(const char *, T & ); })
     16forall(T & | { bool parse(const char *, T & ); })
    1617static inline void ?{}( cfa_option & this, char short_name, const char * long_name, const char * help, T & variable ) {
     18      this.val        = 0;
    1719      this.short_name = short_name;
    1820      this.long_name  = long_name;
     
    2224}
    2325
    24 forall(dtype T)
     26forall(T &)
    2527static inline void ?{}( cfa_option & this, char short_name, const char * long_name, const char * help, T & variable, bool (*parse)(const char *, T & )) {
     28      this.val        = 0;
    2629      this.short_name = short_name;
    2730      this.long_name  = long_name;
  • libcfa/src/rational.cfa

    r342af53 r8e4aa05  
    1818#include "stdlib.hfa"
    1919
    20 forall( otype RationalImpl | arithmetic( RationalImpl ) ) {
     20forall( RationalImpl | arithmetic( RationalImpl ) ) {
    2121        // helper routines
    2222
     
    159159        // I/O
    160160
    161         forall( dtype istype | istream( istype ) | { istype & ?|?( istype &, RationalImpl & ); } )
     161        forall( istype & | istream( istype ) | { istype & ?|?( istype &, RationalImpl & ); } )
    162162        istype & ?|?( istype & is, Rational(RationalImpl) & r ) {
    163163                is | r.numerator | r.denominator;
     
    168168        } // ?|?
    169169
    170         forall( dtype ostype | ostream( ostype ) | { ostype & ?|?( ostype &, RationalImpl ); } ) {
     170        forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, RationalImpl ); } ) {
    171171                ostype & ?|?( ostype & os, Rational(RationalImpl) r ) {
    172172                        return os | r.numerator | '/' | r.denominator;
     
    179179} // distribution
    180180
    181 forall( otype RationalImpl | arithmetic( RationalImpl ) | { RationalImpl ?\?( RationalImpl, unsigned long ); } )
     181forall( RationalImpl | arithmetic( RationalImpl ) | { RationalImpl ?\?( RationalImpl, unsigned long ); } )
    182182Rational(RationalImpl) ?\?( Rational(RationalImpl) x, long int y ) {
    183183        if ( y < 0 ) {
     
    190190// conversion
    191191
    192 forall( otype RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); } )
     192forall( RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); } )
    193193double widen( Rational(RationalImpl) r ) {
    194194        return convert( r.numerator ) / convert( r.denominator );
    195195} // widen
    196196
    197 forall( otype RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); RationalImpl convert( double ); } )
     197forall( RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); RationalImpl convert( double ); } )
    198198Rational(RationalImpl) narrow( double f, RationalImpl md ) {
    199199        // http://www.ics.uci.edu/~eppstein/numth/frap.c
  • libcfa/src/rational.hfa

    r342af53 r8e4aa05  
    2020#include "iostream.hfa"
    2121
    22 trait scalar( otype T ) {
     22trait scalar( T ) {
    2323};
    2424
    25 trait arithmetic( otype T | scalar( T ) ) {
     25trait arithmetic( T | scalar( T ) ) {
    2626        int !?( T );
    2727        int ?==?( T, T );
     
    4646// implementation
    4747
    48 forall( otype RationalImpl | arithmetic( RationalImpl ) ) {
     48forall( RationalImpl | arithmetic( RationalImpl ) ) {
    4949        struct Rational {
    5050                RationalImpl numerator, denominator;                    // invariant: denominator > 0
     
    8989
    9090        // I/O
    91         forall( dtype istype | istream( istype ) | { istype & ?|?( istype &, RationalImpl & ); } )
     91        forall( istype & | istream( istype ) | { istype & ?|?( istype &, RationalImpl & ); } )
    9292        istype & ?|?( istype &, Rational(RationalImpl) & );
    9393
    94         forall( dtype ostype | ostream( ostype ) | { ostype & ?|?( ostype &, RationalImpl ); } ) {
     94        forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, RationalImpl ); } ) {
    9595                ostype & ?|?( ostype &, Rational(RationalImpl) );
    9696                void ?|?( ostype &, Rational(RationalImpl) );
     
    9898} // distribution
    9999
    100 forall( otype RationalImpl | arithmetic( RationalImpl ) |{RationalImpl ?\?( RationalImpl, unsigned long );} )
     100forall( RationalImpl | arithmetic( RationalImpl ) |{RationalImpl ?\?( RationalImpl, unsigned long );} )
    101101Rational(RationalImpl) ?\?( Rational(RationalImpl) x, long int y );
    102102
    103103// conversion
    104 forall( otype RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); } )
     104forall( RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); } )
    105105double widen( Rational(RationalImpl) r );
    106 forall( otype RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl );  RationalImpl convert( double );} )
     106forall( RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl );  RationalImpl convert( double );} )
    107107Rational(RationalImpl) narrow( double f, RationalImpl md );
    108108
  • libcfa/src/stdlib.cfa

    r342af53 r8e4aa05  
    2828// Cforall allocation/deallocation and constructor/destructor, array types
    2929
    30 forall( dtype T | sized(T), ttype TT | { void ?{}( T &, TT ); } )
     30forall( T & | sized(T), TT... | { void ?{}( T &, TT ); } )
    3131T * anew( size_t dim, TT p ) {
    3232        T * arr = alloc( dim );
     
    3737} // anew
    3838
    39 forall( dtype T | sized(T) | { void ^?{}( T & ); } )
     39forall( T & | sized(T) | { void ^?{}( T & ); } )
    4040void adelete( T arr[] ) {
    4141        if ( arr ) {                                                                            // ignore null
     
    4848} // adelete
    4949
    50 forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype TT | { void adelete( TT ); } )
     50forall( T & | sized(T) | { void ^?{}( T & ); }, TT... | { void adelete( TT ); } )
    5151void adelete( T arr[], TT rest ) {
    5252        if ( arr ) {                                                                            // ignore null
     
    9797//---------------------------------------
    9898
    99 forall( otype E | { int ?<?( E, E ); } ) {
     99forall( E | { int ?<?( E, E ); } ) {
    100100        E * bsearch( E key, const E * vals, size_t dim ) {
    101101                int cmp( const void * t1, const void * t2 ) {
     
    156156
    157157
    158 forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) {
     158forall( K, E | { int ?<?( K, K ); K getKey( const E & ); } ) {
    159159        E * bsearch( K key, const E * vals, size_t dim ) {
    160160                int cmp( const void * t1, const void * t2 ) {
  • libcfa/src/stdlib.hfa

    r342af53 r8e4aa05  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Dec 12 13:52:34 2020
    13 // Update Count     : 536
     12// Last Modified On : Thu Jan 21 22:02:13 2021
     13// Update Count     : 574
    1414//
    1515
     
    4848        else return (T *)alignment( _Alignof(T), dim, sizeof(T) )
    4949
    50 static inline forall( dtype T | sized(T) ) {
     50static inline forall( T & | sized(T) ) {
    5151        // CFA safe equivalents, i.e., implicit size specification
    5252
     
    108108
    109109        1. Replace the current forall-block that contains defintions of S_fill and S_realloc with following:
    110                 forall( dtype T | sized(T) ) {
     110                forall( T & | sized(T) ) {
    111111                        union  U_fill           { char c; T * a; T t; };
    112112                        struct S_fill           { char tag; U_fill(T) fill; };
     
    151151typedef struct S_resize                 { inline void *;  }     T_resize;
    152152
    153 forall( dtype T ) {
     153forall( T & ) {
    154154        struct S_fill           { char tag; char c; size_t size; T * at; char t[50]; };
    155155        struct S_realloc        { inline T *; };
     
    159159static inline T_resize  ?`resize  ( void * a )  { return (T_resize){a}; }
    160160
    161 static inline forall( dtype T | sized(T) ) {
     161static inline forall( T & | sized(T) ) {
    162162        S_fill(T) ?`fill ( T t ) {
    163163                S_fill(T) ret = { 't' };
    164164                size_t size = sizeof(T);
    165                 if(size > sizeof(ret.t)) { printf("ERROR: const object of size greater than 50 bytes given for dynamic memory fill\n"); exit(1); }
     165                if ( size > sizeof(ret.t) ) {
     166                        abort( "ERROR: const object of size greater than 50 bytes given for dynamic memory fill\n" );
     167                } // if
    166168                memcpy( &ret.t, &t, size );
    167169                return ret;
     
    173175        S_realloc(T)    ?`realloc ( T * a )                             { return (S_realloc(T)){a}; }
    174176
    175         T * $alloc_internal( void * Resize, T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill) {
     177        T * $alloc_internal( void * Resize, T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill ) {
    176178                T * ptr = NULL;
    177179                size_t size = sizeof(T);
     
    181183                        ptr = (T*) (void *) resize( (void *)Resize, Align, Dim * size );
    182184                } else if ( Realloc ) {
    183                         if (Fill.tag != '0') copy_end = min(malloc_size( Realloc ), Dim * size);
    184                         ptr = (T*) (void *) realloc( (void *)Realloc, Align, Dim * size );
     185                        if ( Fill.tag != '0' ) copy_end = min(malloc_size( Realloc ), Dim * size );
     186                        ptr = (T *) (void *) realloc( (void *)Realloc, Align, Dim * size );
    185187                } else {
    186                         ptr = (T*) (void *) memalign( Align, Dim * size );
    187                 }
    188 
    189                 if(Fill.tag == 'c') {
     188                        ptr = (T *) (void *) memalign( Align, Dim * size );
     189                }
     190
     191                if ( Fill.tag == 'c' ) {
    190192                        memset( (char *)ptr + copy_end, (int)Fill.c, Dim * size - copy_end );
    191                 } else if(Fill.tag == 't') {
     193                } else if ( Fill.tag == 't' ) {
    192194                        for ( int i = copy_end; i < Dim * size; i += size ) {
     195                                #pragma GCC diagnostic push
     196                                #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
     197                                assert( size <= sizeof(Fill.t) );
    193198                                memcpy( (char *)ptr + i, &Fill.t, size );
     199                                #pragma GCC diagnostic pop
    194200                        }
    195                 } else if(Fill.tag == 'a') {
     201                } else if ( Fill.tag == 'a' ) {
    196202                        memcpy( (char *)ptr + copy_end, Fill.at, min(Dim * size - copy_end, Fill.size) );
    197                 } else if(Fill.tag == 'T') {
    198                         for ( int i = copy_end; i < Dim * size; i += size ) {
    199                                 memcpy( (char *)ptr + i, Fill.at, size );
    200                         }
     203                } else if ( Fill.tag == 'T' ) {
     204                        memcpy( (char *)ptr + copy_end, Fill.at, Dim * size );
    201205                }
    202206
     
    204208        } // $alloc_internal
    205209
    206         forall( ttype TT | { T * $alloc_internal( void *, T *, size_t, size_t, S_fill(T), TT ); } ) {
     210        forall( TT... | { T * $alloc_internal( void *, T *, size_t, size_t, S_fill(T), TT ); } ) {
    207211
    208212                T * $alloc_internal( void *       , T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill, T_resize Resize, TT rest) {
     
    233237} // distribution T
    234238
    235 static inline forall( dtype T | sized(T) ) {
     239static inline forall( T & | sized(T) ) {
    236240        // CFA safe initialization/copy, i.e., implicit size specification, non-array types
    237241        T * memset( T * dest, char fill ) {
     
    254258
    255259// CFA deallocation for multiple objects
    256 static inline forall( dtype T )                                                 // FIX ME, problems with 0p in list
     260static inline forall( T & )                                                     // FIX ME, problems with 0p in list
    257261void free( T * ptr ) {
    258262        free( (void *)ptr );                                                            // C free
    259263} // free
    260 static inline forall( dtype T, ttype TT | { void free( TT ); } )
     264static inline forall( T &, TT... | { void free( TT ); } )
    261265void free( T * ptr, TT rest ) {
    262266        free( ptr );
     
    265269
    266270// CFA allocation/deallocation and constructor/destructor, non-array types
    267 static inline forall( dtype T | sized(T), ttype TT | { void ?{}( T &, TT ); } )
     271static inline forall( T & | sized(T), TT... | { void ?{}( T &, TT ); } )
    268272T * new( TT p ) {
    269         return &(*(T *)malloc()){ p };                                                  // run constructor
     273        return &(*(T *)malloc()){ p };                                          // run constructor
    270274} // new
    271275
    272 static inline forall( dtype T | { void ^?{}( T & ); } )
     276static inline forall( T & | { void ^?{}( T & ); } )
    273277void delete( T * ptr ) {
    274278        // special case for 0-sized object => always call destructor
     
    278282        free( ptr );                                                                            // always call free
    279283} // delete
    280 static inline forall( dtype T, ttype TT | { void ^?{}( T & ); void delete( TT ); } )
     284static inline forall( T &, TT... | { void ^?{}( T & ); void delete( TT ); } )
    281285void delete( T * ptr, TT rest ) {
    282286        delete( ptr );
     
    285289
    286290// CFA allocation/deallocation and constructor/destructor, array types
    287 forall( dtype T | sized(T), ttype TT | { void ?{}( T &, TT ); } ) T * anew( size_t dim, TT p );
    288 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( T arr[] );
    289 forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype TT | { void adelete( TT ); } ) void adelete( T arr[], TT rest );
     291forall( T & | sized(T), TT... | { void ?{}( T &, TT ); } ) T * anew( size_t dim, TT p );
     292forall( T & | sized(T) | { void ^?{}( T & ); } ) void adelete( T arr[] );
     293forall( T & | sized(T) | { void ^?{}( T & ); }, TT... | { void adelete( TT ); } ) void adelete( T arr[], TT rest );
    290294
    291295//---------------------------------------
     
    327331//---------------------------------------
    328332
    329 forall( otype E | { int ?<?( E, E ); } ) {
     333forall( E | { int ?<?( E, E ); } ) {
    330334        E * bsearch( E key, const E * vals, size_t dim );
    331335        size_t bsearch( E key, const E * vals, size_t dim );
     
    336340} // distribution
    337341
    338 forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) {
     342forall( K, E | { int ?<?( K, K ); K getKey( const E & ); } ) {
    339343        E * bsearch( K key, const E * vals, size_t dim );
    340344        size_t bsearch( K key, const E * vals, size_t dim );
     
    345349} // distribution
    346350
    347 forall( otype E | { int ?<?( E, E ); } ) {
     351forall( E | { int ?<?( E, E ); } ) {
    348352        void qsort( E * vals, size_t dim );
    349353} // distribution
  • libcfa/src/time.cfa

    r342af53 r8e4aa05  
    3131
    3232
    33 forall( dtype ostype | ostream( ostype ) ) {
     33forall( ostype & | ostream( ostype ) ) {
    3434        ostype & ?|?( ostype & os, Duration dur ) with( dur ) {
    3535                (ostype &)(os | tn / TIMEGRAN);                                 // print seconds
     
    136136} // strftime
    137137
    138 forall( dtype ostype | ostream( ostype ) ) {
     138forall( ostype & | ostream( ostype ) ) {
    139139        ostype & ?|?( ostype & os, Time time ) with( time ) {
    140140                char buf[32];                                                                   // at least 26
  • libcfa/src/vec/vec.hfa

    r342af53 r8e4aa05  
    1818#include <math.hfa>
    1919
    20 trait fromint(otype T) {
     20trait fromint(T) {
    2121    void ?{}(T&, int);
    2222};
    23 trait zeroinit(otype T) {
     23trait zeroinit(T) {
    2424    void ?{}(T&, zero_t);
    2525};
    26 trait zero_assign(otype T) {
     26trait zero_assign(T) {
    2727    T ?=?(T&, zero_t);
    2828};
    29 trait subtract(otype T) {
     29trait subtract(T) {
    3030    T ?-?(T, T);
    3131};
    32 trait negate(otype T) {
     32trait negate(T) {
    3333    T -?(T);
    3434};
    35 trait add(otype T) {
     35trait add(T) {
    3636    T ?+?(T, T);
    3737};
    38 trait multiply(otype T) {
     38trait multiply(T) {
    3939    T ?*?(T, T);
    4040};
    41 trait divide(otype T) {
     41trait divide(T) {
    4242    T ?/?(T, T);
    4343};
    44 trait lessthan(otype T) {
     44trait lessthan(T) {
    4545    int ?<?(T, T);
    4646};
    47 trait equality(otype T) {
     47trait equality(T) {
    4848    int ?==?(T, T);
    4949};
    50 trait sqrt(otype T) {
     50trait sqrt(T) {
    5151    T sqrt(T);
    5252};
     
    6868}
    6969
    70 trait dottable(otype V, otype T) {
     70trait dottable(V, T) {
    7171    T dot(V, V);
    7272};
     
    7474static inline {
    7575
    76 forall(otype T | sqrt(T), otype V | dottable(V, T))
     76forall(T | sqrt(T), V | dottable(V, T))
    7777T length(V v) {
    7878   return sqrt(dot(v, v));
    7979}
    8080
    81 forall(otype T, otype V | dottable(V, T))
     81forall(T, V | dottable(V, T))
    8282T length_squared(V v) {
    8383   return dot(v, v);
    8484}
    8585
    86 forall(otype T, otype V | { T length(V); } | subtract(V))
     86forall(T, V | { T length(V); } | subtract(V))
    8787T distance(V v1, V v2) {
    8888    return length(v1 - v2);
    8989}
    9090
    91 forall(otype T, otype V | { T length(V); V ?/?(V, T); })
     91forall(T, V | { T length(V); V ?/?(V, T); })
    9292V normalize(V v) {
    9393    return v / length(v);
     
    9595
    9696// Project vector u onto vector v
    97 forall(otype T, otype V | dottable(V, T) | { V normalize(V); V ?*?(V, T); })
     97forall(T, V | dottable(V, T) | { V normalize(V); V ?*?(V, T); })
    9898V project(V u, V v) {
    9999    V v_norm = normalize(v);
     
    102102
    103103// Reflect incident vector v with respect to surface with normal n
    104 forall(otype T | fromint(T), otype V | { V project(V, V); V ?*?(T, V); V ?-?(V,V); })
     104forall(T | fromint(T), V | { V project(V, V); V ?*?(T, V); V ?-?(V,V); })
    105105V reflect(V v, V n) {
    106106    return v - (T){2} * project(v, n);
     
    111111// entering material (i.e., from air to water, eta = 1/1.33)
    112112// v and n must already be normalized
    113 forall(otype T | fromint(T) | subtract(T) | multiply(T) | add(T) | lessthan(T) | sqrt(T),
    114        otype V | dottable(V, T) | { V ?*?(T, V); V ?-?(V,V); void ?{}(V&, zero_t); })
     113forall(T | fromint(T) | subtract(T) | multiply(T) | add(T) | lessthan(T) | sqrt(T),
     114       V | dottable(V, T) | { V ?*?(T, V); V ?-?(V,V); void ?{}(V&, zero_t); })
    115115V refract(V v, V n, T eta) {
    116116    T dotValue = dot(n, v);
     
    128128// i is the incident vector
    129129// ng is the geometric normal of the surface
    130 forall(otype T | lessthan(T) | zeroinit(T), otype V | dottable(V, T) | negate(V))
     130forall(T | lessthan(T) | zeroinit(T), V | dottable(V, T) | negate(V))
    131131V faceforward(V n, V i, V ng) {
    132132    return dot(ng, i) < (T){0} ? n : -n;
  • libcfa/src/vec/vec2.hfa

    r342af53 r8e4aa05  
    1919#include "vec.hfa"
    2020
    21 forall (otype T) {
     21forall (T) {
    2222    struct vec2 {
    2323        T x, y;
     
    2525}
    2626
    27 forall (otype T) {
     27forall (T) {
    2828    static inline {
    2929
     
    279279}
    280280
    281 forall(dtype ostype, otype T | writeable(T, ostype)) {
     281forall(ostype &, T | writeable(T, ostype)) {
    282282    ostype & ?|?(ostype & os, vec2(T) v) with (v) {
    283283        return os | '<' | x | ',' | y | '>';
  • libcfa/src/vec/vec3.hfa

    r342af53 r8e4aa05  
    1919#include "vec.hfa"
    2020
    21 forall (otype T) {
     21forall (T) {
    2222    struct vec3 {
    2323        T x, y, z;
     
    2525}
    2626
    27 forall (otype T) {
     27forall (T) {
    2828    static inline {
    2929
     
    288288}
    289289
    290 forall(dtype ostype, otype T | writeable(T, ostype)) {
     290forall(ostype &, T | writeable(T, ostype)) {
    291291    ostype & ?|?(ostype & os, vec3(T) v) with (v) {
    292292        return os | '<' | x | ',' | y | ',' | z | '>';
  • libcfa/src/vec/vec4.hfa

    r342af53 r8e4aa05  
    1919#include "vec.hfa"
    2020
    21 forall (otype T) {
     21forall (T) {
    2222    struct vec4 {
    2323        T x, y, z, w;
     
    2525}
    2626
    27 forall (otype T) {
     27forall (T) {
    2828    static inline {
    2929
     
    283283}
    284284
    285 forall(dtype ostype, otype T | writeable(T, ostype)) {
     285forall(ostype &, T | writeable(T, ostype)) {
    286286    ostype & ?|?(ostype & os, vec4(T) v) with (v) {
    287287        return os | '<' | x | ',' | y | ',' | z | ',' | w | '>';
  • src/Parser/lex.ll

    r342af53 r8e4aa05  
    1010 * Created On       : Sat Sep 22 08:58:10 2001
    1111 * Last Modified By : Peter A. Buhr
    12  * Last Modified On : Tue Oct  6 18:15:41 2020
    13  * Update Count     : 743
     12 * Last Modified On : Wed Feb 17 08:38:13 2021
     13 * Update Count     : 752
    1414 */
    1515
     
    221221break                   { KEYWORD_RETURN(BREAK); }
    222222case                    { KEYWORD_RETURN(CASE); }
    223 catch                   { KEYWORD_RETURN(CATCH); }                              // CFA
    224 catchResume             { KEYWORD_RETURN(CATCHRESUME); }                // CFA
     223catch                   { QKEYWORD_RETURN(CATCH); }                             // CFA
     224catchResume             { QKEYWORD_RETURN(CATCHRESUME); }               // CFA
    225225char                    { KEYWORD_RETURN(CHAR); }
    226226choose                  { KEYWORD_RETURN(CHOOSE); }                             // CFA
     
    247247fallthrough             { KEYWORD_RETURN(FALLTHROUGH); }                // CFA
    248248fallthru                { KEYWORD_RETURN(FALLTHRU); }                   // CFA
    249 finally                 { KEYWORD_RETURN(FINALLY); }                    // CFA
     249finally                 { QKEYWORD_RETURN(FINALLY); }                   // CFA
     250fixup                   { QKEYWORD_RETURN(FIXUP); }                             // CFA
    250251float                   { KEYWORD_RETURN(FLOAT); }
    251252__float80               { KEYWORD_RETURN(uuFLOAT80); }                  // GCC
     
    287288or                              { QKEYWORD_RETURN(WOR); }                               // CFA
    288289otype                   { KEYWORD_RETURN(OTYPE); }                              // CFA
     290recover                 { QKEYWORD_RETURN(RECOVER); }                   // CFA
    289291register                { KEYWORD_RETURN(REGISTER); }
     292report                  { KEYWORD_RETURN(THROWRESUME); }                // CFA
    290293restrict                { KEYWORD_RETURN(RESTRICT); }                   // C99
    291294__restrict              { KEYWORD_RETURN(RESTRICT); }                   // GCC
     
    324327__volatile              { KEYWORD_RETURN(VOLATILE); }                   // GCC
    325328__volatile__    { KEYWORD_RETURN(VOLATILE); }                   // GCC
    326 waitfor                 { KEYWORD_RETURN(WAITFOR); }
    327 when                    { KEYWORD_RETURN(WHEN); }
     329waitfor                 { KEYWORD_RETURN(WAITFOR); }                    // CFA
     330when                    { KEYWORD_RETURN(WHEN); }                               // CFA
    328331while                   { KEYWORD_RETURN(WHILE); }
    329332with                    { KEYWORD_RETURN(WITH); }                               // CFA
  • src/Parser/parser.yy

    r342af53 r8e4aa05  
    1010// Created On       : Sat Sep  1 20:22:55 2001
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Jan 11 21:32:10 2021
    13 // Update Count     : 4633
     12// Last Modified On : Wed Feb 17 09:03:07 2021
     13// Update Count     : 4722
    1414//
    1515
     
    4141
    4242%{
    43 #define YYDEBUG_LEXER_TEXT (yylval)                                             // lexer loads this up each time
     43#define YYDEBUG_LEXER_TEXT( yylval )                                    // lexer loads this up each time
    4444#define YYDEBUG 1                                                                               // get the pretty debugging code to compile
    4545#define YYERROR_VERBOSE                                                                 // more information in syntax errors
     
    6363extern TypedefTable typedefTable;
    6464
    65 stack< LinkageSpec::Spec > linkageStack;
     65stack<LinkageSpec::Spec> linkageStack;
    6666
    6767bool appendStr( string & to, string & from ) {
     
    187187        ConstantExpr * constant = dynamic_cast<ConstantExpr *>(type->expr.get());
    188188        if ( constant && (constant->get_constant()->get_value() == "0" || constant->get_constant()->get_value() == "1") ) {
    189         type = new ExpressionNode( new CastExpr( maybeMoveBuild< Expression >(type), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ) );
     189                type = new ExpressionNode( new CastExpr( maybeMoveBuild<Expression>(type), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ) );
    190190        } // if
    191191        return new ForCtrl(
     
    282282%token ATTRIBUTE EXTENSION                                                              // GCC
    283283%token IF ELSE SWITCH CASE DEFAULT DO WHILE FOR BREAK CONTINUE GOTO RETURN
    284 %token CHOOSE DISABLE ENABLE FALLTHRU FALLTHROUGH TRY CATCH CATCHRESUME FINALLY THROW THROWRESUME AT WITH WHEN WAITFOR // CFA
     284%token CHOOSE DISABLE ENABLE FALLTHRU FALLTHROUGH TRY THROW THROWRESUME AT WITH WHEN WAITFOR // CFA
    285285%token ASM                                                                                              // C99, extension ISO/IEC 9899:1999 Section J.5.10(1)
    286286%token ALIGNAS ALIGNOF GENERIC STATICASSERT                             // C11
    287287
    288288// names and constants: lexer differentiates between identifier and typedef names
    289 %token<tok> IDENTIFIER                  QUOTED_IDENTIFIER               TYPEDEFname                             TYPEGENname
    290 %token<tok> TIMEOUT                             WOR
    291 %token<tok> INTEGERconstant             CHARACTERconstant               STRINGliteral
     289%token<tok> IDENTIFIER          QUOTED_IDENTIFIER       TYPEDEFname             TYPEGENname
     290%token<tok> TIMEOUT                     WOR                                     CATCH                   RECOVER                 CATCHRESUME             FIXUP           FINALLY         // CFA
     291%token<tok> INTEGERconstant     CHARACTERconstant       STRINGliteral
    292292%token<tok> DIRECTIVE
    293293// Floating point constant is broken into three kinds of tokens because of the ambiguity with tuple indexing and
     
    440440
    441441%type<decl> type_qualifier type_qualifier_name forall type_qualifier_list_opt type_qualifier_list
    442 %type<decl> type_specifier type_specifier_nobody
     442%type<decl> type_specifier type_specifier_nobody enum_specifier_nobody
    443443
    444444%type<decl> variable_declarator variable_ptr variable_array variable_function
    445445%type<decl> variable_abstract_declarator variable_abstract_ptr variable_abstract_array variable_abstract_function
    446446
    447 %type<decl> attribute_list_opt attribute_list attribute_name_list attribute attribute_name
     447%type<decl> attribute_list_opt attribute_list attribute_opt attribute attribute_name_list attribute_name
    448448
    449449// initializers
     
    462462// Order of these lines matters (low-to-high precedence). THEN is left associative over WOR/TIMEOUT/ELSE, WOR is left
    463463// associative over TIMEOUT/ELSE, and TIMEOUT is left associative over ELSE.
    464 %precedence THEN        // rule precedence for IF/WAITFOR statement
    465 %precedence WOR         // token precedence for start of WOR in WAITFOR statement
    466 %precedence TIMEOUT     // token precedence for start of TIMEOUT in WAITFOR statement
    467 %precedence ELSE        // token precedence for start of else clause in IF/WAITFOR statement
     464%precedence THEN                // rule precedence for IF/WAITFOR statement
     465%precedence WOR                 // token precedence for start of WOR in WAITFOR statement
     466%precedence TIMEOUT             // token precedence for start of TIMEOUT in WAITFOR statement
     467%precedence CATCH               // token precedence for start of TIMEOUT in WAITFOR statement
     468%precedence RECOVER             // token precedence for start of TIMEOUT in WAITFOR statement
     469%precedence CATCHRESUME // token precedence for start of TIMEOUT in WAITFOR statement
     470%precedence FIXUP               // token precedence for start of TIMEOUT in WAITFOR statement
     471%precedence FINALLY             // token precedence for start of TIMEOUT in WAITFOR statement
     472%precedence ELSE                // token precedence for start of else clause in IF/WAITFOR statement
     473
    468474
    469475// Handle shift/reduce conflict for generic type by shifting the '(' token. For example, this string is ambiguous:
     
    544550        TIMEOUT
    545551        | WOR
     552        | CATCH
     553        | RECOVER
     554        | CATCHRESUME
     555        | FIXUP
     556        | FINALLY
    546557        ;
    547558
     
    578589                { $$ = $2; }
    579590        | '(' compound_statement ')'                                            // GCC, lambda expression
    580                 { $$ = new ExpressionNode( new StmtExpr( dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >($2) ) ) ); }
     591                { $$ = new ExpressionNode( new StmtExpr( dynamic_cast<CompoundStmt *>(maybeMoveBuild<Statement>($2) ) ) ); }
    581592        | type_name '.' identifier                                                      // CFA, nested type
    582593                { SemanticError( yylloc, "Qualified name is currently unimplemented." ); $$ = nullptr; }
     
    610621                {
    611622                        // create a GenericExpr wrapper with one association pair
    612                         $$ = new GenericExpr( nullptr, { { maybeMoveBuildType($1), maybeMoveBuild<Expression>($3) } } );
     623                        $$ = new GenericExpr( nullptr, { { maybeMoveBuildType($1), maybeMoveBuild<Expression>( $3 ) } } );
    613624                }
    614625        | DEFAULT ':' assignment_expression
    615                 { $$ = new GenericExpr( nullptr, { { maybeMoveBuild<Expression>($3) } } ); }
     626                { $$ = new GenericExpr( nullptr, { { maybeMoveBuild<Expression>( $3 ) } } ); }
    616627        ;
    617628
    618629postfix_expression:
    619630        primary_expression
     631        | postfix_expression '[' assignment_expression ',' comma_expression ']'
     632                // { $$ = new ExpressionNode( build_binary_val( OperKinds::Index, $1, new ExpressionNode( build_binary_val( OperKinds::Index, $3, $5 ) ) ) ); }
     633                { SemanticError( yylloc, "New array subscript is currently unimplemented." ); $$ = nullptr; }
    620634        | postfix_expression '[' assignment_expression ']'
    621635                // CFA, comma_expression disallowed in this context because it results in a common user error: subscripting a
     
    743757                        switch ( $1 ) {
    744758                          case OperKinds::AddressOf:
    745                                 $$ = new ExpressionNode( new AddressExpr( maybeMoveBuild< Expression >( $2 ) ) );
     759                                $$ = new ExpressionNode( new AddressExpr( maybeMoveBuild<Expression>( $2 ) ) );
    746760                                break;
    747761                          case OperKinds::PointTo:
     
    749763                                break;
    750764                          case OperKinds::And:
    751                                 $$ = new ExpressionNode( new AddressExpr( new AddressExpr( maybeMoveBuild< Expression >( $2 ) ) ) );
     765                                $$ = new ExpressionNode( new AddressExpr( new AddressExpr( maybeMoveBuild<Expression>( $2 ) ) ) );
    752766                                break;
    753767                          default:
     
    762776                { $$ = new ExpressionNode( build_unary_ptr( OperKinds::Decr, $2 ) ); }
    763777        | SIZEOF unary_expression
    764                 { $$ = new ExpressionNode( new SizeofExpr( maybeMoveBuild< Expression >( $2 ) ) ); }
     778                { $$ = new ExpressionNode( new SizeofExpr( maybeMoveBuild<Expression>( $2 ) ) ); }
    765779        | SIZEOF '(' type_no_function ')'
    766780                { $$ = new ExpressionNode( new SizeofExpr( maybeMoveBuildType( $3 ) ) ); }
    767781        | ALIGNOF unary_expression                                                      // GCC, variable alignment
    768                 { $$ = new ExpressionNode( new AlignofExpr( maybeMoveBuild< Expression >( $2 ) ) ); }
     782                { $$ = new ExpressionNode( new AlignofExpr( maybeMoveBuild<Expression>( $2 ) ) ); }
    769783        | ALIGNOF '(' type_no_function ')'                                      // GCC, type alignment
    770784                { $$ = new ExpressionNode( new AlignofExpr( maybeMoveBuildType( $3 ) ) ); }
     
    794808                { $$ = new ExpressionNode( build_keyword_cast( $2, $5 ) ); }
    795809        | '(' VIRTUAL ')' cast_expression                                       // CFA
    796                 { $$ = new ExpressionNode( new VirtualCastExpr( maybeMoveBuild< Expression >( $4 ), maybeMoveBuildType( nullptr ) ) ); }
     810                { $$ = new ExpressionNode( new VirtualCastExpr( maybeMoveBuild<Expression>( $4 ), maybeMoveBuildType( nullptr ) ) ); }
    797811        | '(' VIRTUAL type_no_function ')' cast_expression      // CFA
    798                 { $$ = new ExpressionNode( new VirtualCastExpr( maybeMoveBuild< Expression >( $5 ), maybeMoveBuildType( $3 ) ) ); }
     812                { $$ = new ExpressionNode( new VirtualCastExpr( maybeMoveBuild<Expression>( $5 ), maybeMoveBuildType( $3 ) ) ); }
    799813        | '(' RETURN type_no_function ')' cast_expression       // CFA
    800814                { SemanticError( yylloc, "Return cast is currently unimplemented." ); $$ = nullptr; }
     
    977991        assignment_expression
    978992        | comma_expression ',' assignment_expression
    979                 { $$ = new ExpressionNode( new CommaExpr( maybeMoveBuild< Expression >( $1 ), maybeMoveBuild< Expression >( $3 ) ) ); }
     993                { $$ = new ExpressionNode( new CommaExpr( maybeMoveBuild<Expression>( $1 ), maybeMoveBuild<Expression>( $3 ) ) ); }
    980994        ;
    981995
     
    11021116        constant_expression                                                     { $$ = $1; }
    11031117        | constant_expression ELLIPSIS constant_expression      // GCC, subrange
    1104                 { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild< Expression >( $1 ), maybeMoveBuild< Expression >( $3 ) ) ); }
     1118                { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild<Expression>( $1 ), maybeMoveBuild<Expression>( $3 ) ) ); }
    11051119        | subrange                                                                                      // CFA, subrange
    11061120        ;
     
    12471261                { $$ = new StatementNode( build_computedgoto( $3 ) ); }
    12481262                // A semantic check is required to ensure fallthru appears only in the body of a choose statement.
    1249     | fall_through_name ';'                                                             // CFA
     1263        | fall_through_name ';'                                                         // CFA
    12501264                { $$ = new StatementNode( build_branch( BranchStmt::FallThrough ) ); }
    1251     | fall_through_name identifier_or_type_name ';'             // CFA
     1265        | fall_through_name identifier_or_type_name ';'         // CFA
    12521266                { $$ = new StatementNode( build_branch( $2, BranchStmt::FallThrough ) ); }
    12531267        | fall_through_name DEFAULT ';'                                         // CFA
     
    13631377
    13641378exception_statement:
    1365         TRY compound_statement handler_clause
     1379        TRY compound_statement handler_clause                                   %prec THEN
    13661380                { $$ = new StatementNode( build_try( $2, $3, 0 ) ); }
    13671381        | TRY compound_statement finally_clause
     
    13861400handler_key:
    13871401        CATCH                                                                           { $$ = CatchStmt::Terminate; }
     1402        | RECOVER                                                                       { $$ = CatchStmt::Terminate; }
    13881403        | CATCHRESUME                                                           { $$ = CatchStmt::Resume; }
     1404        | FIXUP                                                                         { $$ = CatchStmt::Resume; }
    13891405        ;
    13901406
     
    14481464asm_operand:                                                                                    // GCC
    14491465        string_literal '(' constant_expression ')'
    1450                 { $$ = new ExpressionNode( new AsmExpr( nullptr, $1, maybeMoveBuild< Expression >( $3 ) ) ); }
     1466                { $$ = new ExpressionNode( new AsmExpr( nullptr, $1, maybeMoveBuild<Expression>( $3 ) ) ); }
    14511467        | '[' IDENTIFIER ']' string_literal '(' constant_expression ')'
    1452                 { $$ = new ExpressionNode( new AsmExpr( $2, $4, maybeMoveBuild< Expression >( $6 ) ) ); }
     1468                { $$ = new ExpressionNode( new AsmExpr( $2, $4, maybeMoveBuild<Expression>( $6 ) ) ); }
    14531469        ;
    14541470
     
    17361752        | sue_type_specifier_nobody
    17371753        | type_type_specifier
     1754        ;
     1755
     1756enum_specifier_nobody:                                                                  // type specifier - {...}
     1757                // Preclude SUE declarations in restricted scopes (see type_specifier_nobody)
     1758        basic_type_specifier
     1759        | sue_type_specifier_nobody
    17381760        ;
    17391761
     
    20042026        ;
    20052027
    2006 fred:
    2007         // empty
    2008                 { yyy = false; }
    2009         ;
    2010 
    20112028aggregate_type:                                                                                 // struct, union
    20122029        aggregate_key attribute_list_opt
     
    20142031          '{' field_declaration_list_opt '}' type_parameters_opt
    20152032                { $$ = DeclarationNode::newAggregate( $1, nullptr, $7, $5, true )->addQualifiers( $2 ); }
    2016         | aggregate_key attribute_list_opt identifier fred
     2033        | aggregate_key attribute_list_opt identifier
    20172034                {
    20182035                        typedefTable.makeTypedef( *$3, forall || typedefTable.getEnclForall() ? TYPEGENname : TYPEDEFname ); // create typedef
     
    20202037                }
    20212038          '{' field_declaration_list_opt '}' type_parameters_opt
    2022                 { $$ = DeclarationNode::newAggregate( $1, $3, $9, $7, true )->addQualifiers( $2 ); }
    2023         | aggregate_key attribute_list_opt type_name fred
     2039                { $$ = DeclarationNode::newAggregate( $1, $3, $8, $6, true )->addQualifiers( $2 ); }
     2040        | aggregate_key attribute_list_opt type_name
    20242041                {
    20252042                        // for type_name can be a qualified type name S.T, in which case only the last name in the chain needs a typedef (other names in the chain should already have one)
     
    20282045                }
    20292046          '{' field_declaration_list_opt '}' type_parameters_opt
    2030                 { $$ = DeclarationNode::newAggregate( $1, $3->type->symbolic.name, $9, $7, true )->addQualifiers( $2 ); }
     2047                { $$ = DeclarationNode::newAggregate( $1, $3->type->symbolic.name, $8, $6, true )->addQualifiers( $2 ); }
    20312048        | aggregate_type_nobody
    20322049        ;
     
    20402057
    20412058aggregate_type_nobody:                                                                  // struct, union - {...}
    2042         aggregate_key attribute_list_opt identifier fred
     2059        aggregate_key attribute_list_opt identifier
    20432060                {
    20442061                        typedefTable.makeTypedef( *$3, forall || typedefTable.getEnclForall() ? TYPEGENname : TYPEDEFname );
     
    20462063                        $$ = DeclarationNode::newAggregate( $1, $3, nullptr, nullptr, false )->addQualifiers( $2 );
    20472064                }
    2048         | aggregate_key attribute_list_opt type_name fred
     2065        | aggregate_key attribute_list_opt type_name
    20492066                {
    20502067                        forall = false;                                                         // reset
     
    21842201        ;
    21852202
     2203// Cannot use attribute_list_opt because of ambiguity with enum_specifier_nobody, which already parses attribute.
     2204// Hence, only a single attribute is allowed after the "ENUM".
    21862205enum_type:                                                                                              // enum
    2187         ENUM attribute_list_opt '{' enumerator_list comma_opt '}'
     2206        ENUM attribute_opt '{' enumerator_list comma_opt '}'
    21882207                { $$ = DeclarationNode::newEnum( nullptr, $4, true )->addQualifiers( $2 ); }
    2189         | ENUM attribute_list_opt identifier
     2208        | ENUM attribute_opt identifier
    21902209                { typedefTable.makeTypedef( *$3 ); }
    21912210          '{' enumerator_list comma_opt '}'
    21922211                { $$ = DeclarationNode::newEnum( $3, $6, true )->addQualifiers( $2 ); }
    2193         | ENUM attribute_list_opt type_name
     2212        | ENUM attribute_opt typedef                                            // enum cannot be generic
    21942213          '{' enumerator_list comma_opt '}'
    2195                 { $$ = DeclarationNode::newEnum( $3->type->symbolic.name, $5, true )->addQualifiers( $2 ); }
     2214                { $$ = DeclarationNode::newEnum( $3->name, $5, true )->addQualifiers( $2 ); }
     2215        | ENUM enum_specifier_nobody '{' enumerator_list comma_opt '}'
     2216                // { $$ = DeclarationNode::newEnum( nullptr, $4, true ); }
     2217                { SemanticError( yylloc, "Typed enumeration is currently unimplemented." ); $$ = nullptr; }
     2218        | ENUM enum_specifier_nobody declarator '{' enumerator_list comma_opt '}'
     2219                // {
     2220                //      typedefTable.makeTypedef( *$3->name );
     2221                //      $$ = DeclarationNode::newEnum( nullptr, $5, true );
     2222                // }
     2223                { SemanticError( yylloc, "Typed enumeration is currently unimplemented." ); $$ = nullptr; }
    21962224        | enum_type_nobody
    21972225        ;
    21982226
    21992227enum_type_nobody:                                                                               // enum - {...}
    2200         ENUM attribute_list_opt identifier
     2228        ENUM attribute_opt identifier
    22012229                {
    22022230                        typedefTable.makeTypedef( *$3 );
    22032231                        $$ = DeclarationNode::newEnum( $3, 0, false )->addQualifiers( $2 );
    22042232                }
    2205         | ENUM attribute_list_opt type_name
     2233        | ENUM attribute_opt type_name                                          // enum cannot be generic
    22062234                {
    22072235                        typedefTable.makeTypedef( *$3->type->symbolic.name );
     
    22202248        // empty
    22212249                { $$ = nullptr; }
    2222         | '=' constant_expression
    2223                 { $$ = $2; }
     2250        // | '=' constant_expression
     2251        //      { $$ = $2; }
     2252        | '=' initializer
     2253                { $$ = $2->get_expression(); }                                  // FIX ME: enum only deals with constant_expression
    22242254        ;
    22252255
     
    24032433                { $$ = $3; }
    24042434        | '[' push constant_expression ELLIPSIS constant_expression pop ']' // GCC, multiple array elements
    2405                 { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild< Expression >( $3 ), maybeMoveBuild< Expression >( $5 ) ) ); }
     2435                { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild<Expression>( $3 ), maybeMoveBuild<Expression>( $5 ) ) ); }
    24062436        | '.' '[' push field_name_list pop ']'                          // CFA, tuple field selector
    24072437                { $$ = $4; }
     
    24412471type_parameter:                                                                                 // CFA
    24422472        type_class identifier_or_type_name
    2443                 { typedefTable.addToScope( *$2, TYPEDEFname, "9" ); }
     2473                {
     2474                        typedefTable.addToScope( *$2, TYPEDEFname, "9" );
     2475                        if ( $1 == TypeDecl::Otype ) { SemanticError( yylloc, "otype keyword is deprecated, use T " ); }
     2476                        if ( $1 == TypeDecl::Dtype ) { SemanticError( yylloc, "dtype keyword is deprecated, use T &" ); }
     2477                        if ( $1 == TypeDecl::Ttype ) { SemanticError( yylloc, "ttype keyword is deprecated, use T ..." ); }
     2478                }
    24442479          type_initializer_opt assertion_list_opt
    24452480                { $$ = DeclarationNode::newTypeParam( $1, $2 )->addTypeInitializer( $4 )->addAssertions( $5 ); }
     
    27342769subrange:
    27352770        constant_expression '~' constant_expression                     // CFA, integer subrange
    2736                 { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild< Expression >( $1 ), maybeMoveBuild< Expression >( $3 ) ) ); }
     2771                { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild<Expression>( $1 ), maybeMoveBuild<Expression>( $3 ) ) ); }
    27372772        ;
    27382773
     
    27582793        | attribute_list attribute
    27592794                { $$ = $2->addQualifiers( $1 ); }
     2795        ;
     2796
     2797attribute_opt:
     2798        // empty
     2799                { $$ = nullptr; }
     2800        | attribute
    27602801        ;
    27612802
     
    31623203        | '[' ']' multi_array_dimension
    31633204                { $$ = DeclarationNode::newArray( 0, 0, false )->addArray( $3 ); }
     3205        | '[' push assignment_expression pop ',' comma_expression ']'
     3206                { $$ = DeclarationNode::newArray( $3, 0, false )->addArray( DeclarationNode::newArray( $6, 0, false ) ); }
     3207                // { SemanticError( yylloc, "New array dimension is currently unimplemented." ); $$ = nullptr; }
    31643208        | multi_array_dimension
    31653209        ;
  • src/ResolvExpr/PolyCost.cc

    r342af53 r8e4aa05  
    3535                PassVisitor<PolyCost> coster( env, indexer );
    3636                type->accept( coster );
    37                 return coster.pass.result;
     37                return (coster.pass.result > 0) ? 1 : 0;
    3838        }
    3939
     
    8787        ast::Pass<PolyCost_new> costing( symtab, env );
    8888        type->accept( costing );
    89         return costing.core.result;
     89        return (costing.core.result > 0) ? 1 : 0;
    9090}
    9191
  • src/ResolvExpr/SpecCost.cc

    r342af53 r8e4aa05  
    4343                // mark specialization of base type
    4444                void postvisit(ReferenceType*) { if ( count >= 0 ) ++count; }
     45
     46                void postvisit(StructInstType*) { if ( count >= 0 ) ++count; }
     47                void postvisit(UnionInstType*) { if ( count >= 0 ) ++count; }
    4548
    4649        private:
     
    8285                void previsit(StructInstType* sty) {
    8386                        count = minover( sty->parameters );
    84                         visit_children = false;
    8587                }
    8688
     
    8890                void previsit(UnionInstType* uty) {
    8991                        count = minover( uty->parameters );
    90                         visit_children = false;
    9192                }
    9293
     
    174175                void postvisit( const ast::ArrayType * ) { if ( count >= 0 ) ++count; }
    175176                void postvisit( const ast::ReferenceType * ) { if ( count >= 0 ) ++count; }
     177
     178                void postvisit( const ast::StructInstType * ) { if ( count >= 0 ) ++count; }
     179                void postvisit( const ast::UnionInstType * ) { if ( count >= 0 ) ++count; }
    176180
    177181                // Use the minimal specialization value over returns and params.
     
    189193                void previsit( const ast::StructInstType * sty ) {
    190194                        count = minimumPresent( sty->params, expr_result );
    191                         visit_children = false;
    192195                }
    193196
     
    195198                void previsit( const ast::UnionInstType * uty ) {
    196199                        count = minimumPresent( uty->params, expr_result );
    197                         visit_children = false;
    198200                }
    199201
  • src/main.cc

    r342af53 r8e4aa05  
    1010// Created On       : Fri May 15 23:12:02 2015
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Mon Dec  7 15:29:00 2020
    13 // Update Count     : 639
     12// Last Modified On : Fri Feb 19 14:59:00 2021
     13// Update Count     : 643
    1414//
    1515
     
    104104static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit = false );
    105105static void dump( list< Declaration * > & translationUnit, ostream & out = cout );
     106static void dump( ast::TranslationUnit && transUnit, ostream & out = cout );
    106107
    107108static void backtrace( int start ) {                                    // skip first N stack frames
     
    349350                        PASS( "Resolve", ResolvExpr::resolve( transUnit ) );
    350351                        if ( exprp ) {
    351                                 translationUnit = convert( move( transUnit ) );
    352                                 dump( translationUnit );
     352                                dump( move( transUnit ) );
    353353                                return EXIT_SUCCESS;
    354354                        } // if
     
    492492
    493493static const char * description[] = {
    494         "diagnostic color: never, always, or auto.",            // -c
     494        "diagnostic color: never, always, auto",                        // -c
    495495        "wait for gdb to attach",                                                       // -g
    496         "print help message",                                                           // -h
     496        "print translator help message",                                        // -h
    497497        "generate libcfa.c",                                                            // -l
    498498        "generate line marks",                                                          // -L
     
    500500        "do not generate line marks",                                           // -N
    501501        "do not read prelude",                                                          // -n
    502         "generate prototypes for prelude functions",            // -p
     502        "do not generate prelude prototypes => prelude not printed", // -p
    503503        "only print deterministic output",                  // -d
    504504        "Use the old-ast",                                                                      // -O
     
    506506        "print",                                                                                        // -P
    507507        "<directory> prelude directory for debug/nodebug",      // no flag
    508         "<option-list> enable profiling information:\n          counters,heap,time,all,none", // -S
     508        "<option-list> enable profiling information: counters, heap, time, all, none", // -S
    509509        "building cfa standard lib",                                            // -t
    510510        "",                                                                                                     // -w
     
    732732} // dump
    733733
     734static void dump( ast::TranslationUnit && transUnit, ostream & out ) {
     735        std::list< Declaration * > translationUnit = convert( move( transUnit ) );
     736        dump( translationUnit, out );
     737}
     738
    734739// Local Variables: //
    735740// tab-width: 4 //
  • tests/.expect/attributes.nast.x64.txt

    r342af53 r8e4aa05  
    66
    77}
    8 struct __attribute__ ((unused)) __anonymous0 {
     8struct __anonymous0 {
    99};
    1010static inline void _X12_constructorFv_S12__anonymous0_autogen___1(struct __anonymous0 *_X4_dstS12__anonymous0_1);
     
    2626    return _X4_retS12__anonymous0_1;
    2727}
     28__attribute__ ((unused)) struct __anonymous0 _X5DummyS12__anonymous0_1;
    2829struct __attribute__ ((unused)) Agn1;
    2930struct __attribute__ ((unused)) Agn2 {
  • tests/.expect/attributes.nast.x86.txt

    r342af53 r8e4aa05  
    66
    77}
    8 struct __attribute__ ((unused)) __anonymous0 {
     8struct __anonymous0 {
    99};
    1010static inline void _X12_constructorFv_S12__anonymous0_autogen___1(struct __anonymous0 *_X4_dstS12__anonymous0_1);
     
    2626    return _X4_retS12__anonymous0_1;
    2727}
     28__attribute__ ((unused)) struct __anonymous0 _X5DummyS12__anonymous0_1;
    2829struct __attribute__ ((unused)) Agn1;
    2930struct __attribute__ ((unused)) Agn2 {
  • tests/.expect/attributes.oast.x64.txt

    r342af53 r8e4aa05  
    66
    77}
    8 struct __attribute__ ((unused)) __anonymous0 {
     8struct __anonymous0 {
    99};
    1010static inline void _X12_constructorFv_S12__anonymous0_autogen___1(struct __anonymous0 *_X4_dstS12__anonymous0_1);
     
    2626    return _X4_retS12__anonymous0_1;
    2727}
     28__attribute__ ((unused)) struct __anonymous0 _X5DummyS12__anonymous0_1;
    2829struct __attribute__ ((unused)) Agn1;
    2930struct __attribute__ ((unused)) Agn2 {
  • tests/.expect/attributes.oast.x86.txt

    r342af53 r8e4aa05  
    66
    77}
    8 struct __attribute__ ((unused)) __anonymous0 {
     8struct __anonymous0 {
    99};
    1010static inline void _X12_constructorFv_S12__anonymous0_autogen___1(struct __anonymous0 *_X4_dstS12__anonymous0_1);
     
    2626    return _X4_retS12__anonymous0_1;
    2727}
     28__attribute__ ((unused)) struct __anonymous0 _X5DummyS12__anonymous0_1;
    2829struct __attribute__ ((unused)) Agn1;
    2930struct __attribute__ ((unused)) Agn2 {
  • tests/Makefile.am

    r342af53 r8e4aa05  
    1111## Created On       : Sun May 31 09:08:15 2015
    1212## Last Modified By : Peter A. Buhr
    13 ## Last Modified On : Fri Oct  9 23:13:07 2020
    14 ## Update Count     : 86
     13## Last Modified On : Tue Mar  2 21:39:01 2021
     14## Update Count     : 90
    1515###############################################################################
    1616
     
    4444        -Wall \
    4545        -Wno-unused-function \
    46         -quiet @CFA_FLAGS@ \
    47         -DIN_DIR="${abs_srcdir}/.in/"
     46        -quiet @CFA_FLAGS@
    4847
    4948AM_CFAFLAGS = -XCFA --deterministic-out
     
    7675        long_tests.hfa \
    7776        .in/io.data \
     77        io/.in/io.data \
    7878        avltree/avl.h \
    7979        avltree/avl-private.h \
     
    142142# don't use distcc to do the linking because distcc doesn't do linking
    143143% : %.cfa $(CFACCBIN)
    144         $(CFACOMPILETEST) -c -o $(abspath ${@}).o
     144        $(CFACOMPILETEST) -c -o $(abspath ${@}).o -DIN_DIR="$(abspath $(dir ${<}))/.in/"
    145145        $(CFACCLINK) ${@}.o -o $(abspath ${@})
    146146        rm $(abspath ${@}).o
     
    170170
    171171SYNTAX_ONLY_CODE = expression typedefRedef variableDeclarator switch numericConstants identFuncDeclarator forall \
    172         init1 limits nested-types stdincludes cast labelledExit array builtins/sync warnings/self-assignment
     172        init1 limits nested-types stdincludes cast labelledExit array quasiKeyword include/includes builtins/sync warnings/self-assignment
    173173$(SYNTAX_ONLY_CODE): % : %.cfa $(CFACCBIN)
    174174        $(CFACOMPILE_SYNTAX)
  • tests/alloc2.cfa

    r342af53 r8e4aa05  
    1616        bool passed = (malloc_size(ip) == size) && (malloc_usable_size(ip) >= size) && (malloc_alignment(ip) == align) && ((uintptr_t)ip % align  == 0);
    1717        if (!passed) {
    18                 printf("failed test %3d: %4lu %4lu but got %4lu ( %3lu ) %4lu\n", tests_total, size, align, malloc_size(ip), malloc_usable_size(ip), malloc_alignment(ip));
     18                printf("failed test %3d: %4zu %4zu but got %4zu ( %3zu ) %4zu\n", tests_total, size, align, malloc_size(ip), malloc_usable_size(ip), malloc_alignment(ip));
    1919                tests_failed += 1;
    2020        }
  • tests/attributes.cfa

    r342af53 r8e4aa05  
    1010// Created On       : Mon Feb  6 16:07:02 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Nov  6 17:51:12 2018
    13 // Update Count     : 17
     12// Last Modified On : Mon Jan 25 21:26:41 2021
     13// Update Count     : 20
    1414//
    1515
     
    2222
    2323// aggregate_name
    24 struct __attribute__(( unused )) {};
     24struct __attribute__(( unused )) {} Dummy;
    2525struct __attribute__(( unused )) Agn1;
    2626struct __attribute__(( unused )) Agn2 {};
  • tests/avltree/avl-private.cfa

    r342af53 r8e4aa05  
    1111// an AVL tree's height is easy to compute
    1212// just follow path with the larger balance
    13 forall(otype K | Comparable(K), otype V)
     13forall(K | Comparable(K), V)
    1414int height(tree(K, V) * t){
    1515  int helper(tree(K, V) * t, int ht){
     
    2727}
    2828
    29 forall(otype K | Comparable(K), otype V)
     29forall(K | Comparable(K), V)
    3030int calcBalance(tree(K, V) * t){
    3131  int l = height(t->left);
     
    3636
    3737// re-establish the link between parent and child
    38 forall(otype K | Comparable(K), otype V)
     38forall(K | Comparable(K), V)
    3939void relinkToParent(tree(K, V) * t){
    4040  tree(K, V) * parent = t->parent; // FIX ME!!
     
    4949
    5050// rotate left from t
    51 forall(otype K | Comparable(K), otype V)
     51forall(K | Comparable(K), V)
    5252tree(K, V) * rotateLeft(tree(K, V) * t){
    5353  tree(K, V) * newRoot = t->right;
     
    6868
    6969// rotate right from t
    70 forall(otype K | Comparable(K), otype V)
     70forall(K | Comparable(K), V)
    7171tree(K, V) * rotateRight(tree(K, V) * t){
    7272  tree(K, V) * newRoot = t->left;
     
    8787
    8888// balances a node that has balance factor -2 or 2
    89 forall(otype K | Comparable(K), otype V)
     89forall(K | Comparable(K), V)
    9090tree(K, V) * fix(tree(K, V) * t){
    9191  // ensure that t's balance factor is one of
     
    113113
    114114// attempt to fix the tree, if necessary
    115 forall(otype K | Comparable(K), otype V)
     115forall(K | Comparable(K), V)
    116116tree(K, V) * tryFix(tree(K, V) * t){
    117117  int b = calcBalance(t);
     
    126126
    127127// sets parent field of c to be p
    128 forall(otype K | Comparable(K), otype V)
     128forall(K | Comparable(K), V)
    129129void setParent(tree(K, V) * c, tree(K, V) * p){
    130130  if (! empty(c)){
  • tests/avltree/avl-private.h

    r342af53 r8e4aa05  
    55
    66// attempt to fix the tree, if necessary
    7 forall(otype K | Comparable(K), otype V)
     7forall(K | Comparable(K), V)
    88tree(K, V) * tryFix(tree(K, V) * t);
    99
    1010// sets parent field of c to be p
    11 forall(otype K | Comparable(K), otype V)
     11forall(K | Comparable(K), V)
    1212void setParent(tree(K, V) * c, tree(K, V) * p);
    1313
    14 forall(otype K | Comparable(K), otype V)
     14forall(K | Comparable(K), V)
    1515int height(tree(K, V) * t);
  • tests/avltree/avl.h

    r342af53 r8e4aa05  
    99// #include <lib.h>
    1010
    11 trait Comparable(otype T) {
     11trait Comparable(T) {
    1212  int ?<?(T, T);
    1313};
    1414
    15 forall(otype T | Comparable(T))
     15forall(T | Comparable(T))
    1616int ?==?(T t1, T t2);
    1717
    18 forall(otype T | Comparable(T))
     18forall(T | Comparable(T))
    1919int ?>?(T t1, T t2);
    2020
     
    4141
    4242// temporary: need forward decl to get around typedef problem
    43 forall(otype K | Comparable(K), otype V)
     43forall(K | Comparable(K), V)
    4444struct tree;
    4545
    46 forall(otype K | Comparable(K), otype V)
     46forall(K | Comparable(K), V)
    4747struct tree {
    4848  K key;
     
    5454};
    5555
    56 forall(otype K | Comparable(K), otype V)
     56forall(K | Comparable(K), V)
    5757void ?{}(tree(K, V) &t, K key, V value);
    5858
    59 forall(otype K, otype V)
     59forall(K | Comparable(K), V)
    6060void ^?{}(tree(K, V) & t);
    6161
    62 forall(otype K | Comparable(K), otype V)
     62forall(K | Comparable(K), V)
    6363tree(K, V) * create(K key, V value);
    6464
    65 forall(otype K | Comparable(K), otype V)
     65forall(K | Comparable(K), V)
    6666V * find(tree(K, V) * t, K key);
    6767
    68 forall(otype K | Comparable(K), otype V)
     68forall(K | Comparable(K), V)
    6969int empty(tree(K, V) * t);
    7070
    7171// returns the root of the tree
    72 forall(otype K | Comparable(K), otype V)
     72forall(K | Comparable(K), V)
    7373int insert(tree(K, V) ** t, K key, V value);
    7474
    75 forall(otype K | Comparable(K), otype V)
     75forall(K | Comparable(K), V)
    7676int remove(tree(K, V) ** t, K key);
    7777
    78 forall(otype K | Comparable(K), otype V)
     78forall(K | Comparable(K), V)
    7979void copy(tree(K, V) * src, tree(K, V) ** ret);
    8080
    81 forall(otype K | Comparable(K), otype V)
     81forall(K | Comparable(K), V)
    8282void for_each(tree(K, V) * t, void (*func)(V));
    8383
  • tests/avltree/avl0.cfa

    r342af53 r8e4aa05  
    11#include "avl.h"
    22
    3 forall(otype T | Comparable(T))
     3forall(T | Comparable(T))
    44int ?==?(T t1, T t2) {
    55  return !(t1 < t2) && !(t2 < t1);
    66}
    77
    8 forall(otype T | Comparable(T))
     8forall(T | Comparable(T))
    99int ?>?(T t1, T t2) {
    1010  return t2 < t1;
  • tests/avltree/avl1.cfa

    r342af53 r8e4aa05  
    33#include <stdlib.hfa>
    44
    5 forall(otype K | Comparable(K), otype V)
     5forall(K | Comparable(K), V)
    66void ?{}(tree(K, V) &t, K key, V value){
    77  (t.key) { key };
     
    1313}
    1414
    15 forall(otype K, otype V)
     15forall(K| Comparable(K), V)
    1616void ^?{}(tree(K, V) & t){
    1717  delete(t.left);
     
    2121}
    2222
    23 forall(otype K | Comparable(K), otype V)
     23forall(K | Comparable(K), V)
    2424tree(K, V) * create(K key, V value) {
    2525  // infinite loop trying to resolve ... t = malloc();
  • tests/avltree/avl2.cfa

    r342af53 r8e4aa05  
    22#include "avl-private.h"
    33
    4 forall(otype K | Comparable(K), otype V)
     4forall(K | Comparable(K), V)
    55V * find(tree(K, V) * t, K key){
    66  if (empty(t)){
     
    1818}
    1919
    20 forall(otype K | Comparable(K), otype V)
     20forall(K | Comparable(K), V)
    2121int empty(tree(K, V) * t){
    2222  return t == NULL;
     
    2424
    2525// returns the root of the tree
    26 forall(otype K | Comparable(K), otype V)
     26forall(K | Comparable(K), V)
    2727int insert(tree(K, V) ** t, K key, V value) {
    2828  // handles a non-empty tree
  • tests/avltree/avl3.cfa

    r342af53 r8e4aa05  
    44
    55// swaps the data within two tree nodes
    6 forall(otype K | Comparable(K), otype V)
     6forall(K | Comparable(K), V)
    77void node_swap(tree(K, V) * t, tree(K, V) * t2){
    88        swap( t->key,  t2->key);
     
    1111
    1212// go left as deep as possible from within the right subtree
    13 forall(otype K | Comparable(K), otype V)
     13forall(K | Comparable(K), V)
    1414tree(K, V) * find_successor(tree(K, V) * t){
    1515        tree(K, V) * find_successor_helper(tree(K, V) * t){
     
    2525
    2626// cleanup - don't want to deep delete, so set children to NULL first.
    27 forall(otype K | Comparable(K), otype V)
     27forall(K | Comparable(K), V)
    2828void deleteSingleNode(tree(K, V) * t) {
    2929        t->left = NULL;
     
    3333
    3434// does the actual remove operation once we've found the node in question
    35 forall(otype K | Comparable(K), otype V)
     35forall(K | Comparable(K), V)
    3636tree(K, V) * remove_node(tree(K, V) * t){
    3737        // is the node a leaf?
     
    8585
    8686// finds the node that needs to be removed
    87 forall(otype K | Comparable(K), otype V)
     87forall(K | Comparable(K), V)
    8888tree(K, V) * remove_helper(tree(K, V) * t, K key, int * worked){
    8989        if (empty(t)){
     
    106106}
    107107
    108 forall(otype K | Comparable(K), otype V)
     108forall(K | Comparable(K), V)
    109109int remove(tree(K, V) ** t, K key){
    110110        int worked = 0;
  • tests/avltree/avl4.cfa

    r342af53 r8e4aa05  
    44// Perform a shallow copy of src, return the
    55// new tree in ret
    6 forall(otype K | Comparable(K), otype V)
     6forall(K | Comparable(K), V)
    77int copy(tree(K, V) * src, tree(K, V) ** ret){
    88  tree(K, V) * helper(tree(K, V) * t, int * worked){
     
    3535
    3636// Apply func to every value element in t, using an in order traversal
    37 forall(otype K | Comparable(K), otype V)
     37forall(K | Comparable(K), V)
    3838void for_each(tree(K, V) * t, int (*func)(V)) {
    3939  if (t == NULL) {
  • tests/bugs/10.cfa

    r342af53 r8e4aa05  
    22// https://cforall.uwaterloo.ca/trac/ticket/10
    33
    4 forall(otype T)
     4forall(T)
    55struct result {
    66      union {
  • tests/bugs/104.cfa

    r342af53 r8e4aa05  
    44[ float, float ] modf_( float x );
    55
    6 forall(otype T | { [T, T] modf_(T); })
     6forall(T | { [T, T] modf_(T); })
    77void modf(T);
    88
  • tests/bugs/194.cfa

    r342af53 r8e4aa05  
    22// https://cforall.uwaterloo.ca/trac/ticket/194
    33
    4 forall( dtype T | sized(T) ) T * foo( void ) {
     4forall( T & | sized(T) ) T * foo( void ) {
    55      printf( "foo1\n" );
    66        return (T *)0;
    77}
    8 forall( dtype T | sized(T) ) T & foo( void ) {
     8forall( T & | sized(T) ) T & foo( void ) {
    99        printf( "foo2\n" );
    1010        return (T &)*(T *)0;
  • tests/bugs/196.cfa

    r342af53 r8e4aa05  
    22// https://cforall.uwaterloo.ca/trac/ticket/196
    33
    4 forall(dtype T)
     4forall(T &)
    55struct link;
    66
    7 forall(dtype T)
     7forall(T &)
    88struct link {
    99        link(T) * next;
     
    1212// -----
    1313
    14 forall(dtype T)
     14forall(T &)
    1515struct foo;
    1616
    17 forall(dtype U)
     17forall(U &)
    1818struct bar {
    1919        foo(U) * data;
    2020};
    2121
    22 forall(dtype T)
     22forall(T &)
    2323struct foo {};
    2424
  • tests/bugs/203-2.cfa

    r342af53 r8e4aa05  
    11// Trac ticket: https://cforall.uwaterloo.ca/trac/ticket/203
    22
    3 forall(dtype A)
     3forall(A &)
    44struct empty {
    55        // Nothing.
    66};
    77
    8 forall(dtype C)
     8forall(C &)
    99struct wrap_e {
    1010        empty(C) field;
  • tests/bugs/203-7.cfa

    r342af53 r8e4aa05  
    11// Trac ticket: https://cforall.uwaterloo.ca/trac/ticket/203
    22
    3 forall(dtype A)
     3forall(A &)
    44struct empty {
    55        // Nothing.
    66};
    77
    8 forall(dtype C)
     8forall(C &)
    99struct wrap_e {
    1010        empty(C) field;
  • tests/bugs/203-9.cfa

    r342af53 r8e4aa05  
    11// Trac ticket: https://cforall.uwaterloo.ca/trac/ticket/203
    22
    3 forall(dtype A)
     3forall(A &)
    44struct empty {
    55        // Nothing.
    66};
    77
    8 forall(dtype C)
     8forall(C &)
    99struct wrap_e {
    1010        empty(C) field;
  • tests/bugs/7.cfa

    r342af53 r8e4aa05  
    88
    99// (Bug 1 unresolved as of this test.)
    10 forall(otype T)
     10forall(T)
    1111struct stack_node;
    1212
    13 forall(otype T)
     13forall(T)
    1414struct stack_node {
    1515    stack_node(T) * next;
     
    1717};
    1818
    19 forall(otype T)
     19forall(T)
    2020struct stack {
    2121    stack_node(T) * head;
    2222};
    2323
    24 trait stack_errors(otype T) {
     24trait stack_errors(T) {
    2525    T emptyStackHandler (stack(T) * this);
    2626};
    2727
    28 forall(otype T | stack_errors(T))
     28forall(T | stack_errors(T))
    2929T pop (stack(T) * this) {
    3030    return (T){};
  • tests/castError.cfa

    r342af53 r8e4aa05  
    1414//
    1515
    16 forall(otype T) struct S { T p; };
     16forall(T) struct S { T p; };
    1717int f;
    1818S(int) sint;
  • tests/concurrent/examples/boundedBufferEXT.cfa

    r342af53 r8e4aa05  
    2424enum { BufferSize = 50 };
    2525
    26 forall( otype T ) {
     26forall( T ) {
    2727        monitor Buffer {
    2828                int front, back, count;
  • tests/concurrent/examples/boundedBufferINT.cfa

    r342af53 r8e4aa05  
    2424enum { BufferSize = 50 };
    2525
    26 forall( otype T ) {
     26forall( T ) {
    2727        monitor Buffer {
    2828                condition full, empty;
  • tests/concurrent/examples/quickSort.generic.cfa

    r342af53 r8e4aa05  
    2121#include <string.h>                                                                             // strcmp
    2222
    23 forall( otype T | { int ?<?( T, T ); } ) {
     23forall( T | { int ?<?( T, T ); } ) {
    2424        thread Quicksort {
    2525                T * values;                                                                             // communication variables
  • tests/concurrent/multi-monitor.cfa

    r342af53 r8e4aa05  
    3838}
    3939
    40 forall(dtype T | sized(T) | { void ^?{}(T & mutex); })
     40forall(T & | sized(T) | { void ^?{}(T & mutex); })
    4141void delete_mutex(T * x) {
    4242        ^(*x){};
  • tests/concurrent/thread.cfa

    r342af53 r8e4aa05  
    11#include <fstream.hfa>
    22#include <kernel.hfa>
     3#include <locks.hfa>
    34#include <stdlib.hfa>
    45#include <thread.hfa>
  • tests/errors/completeType.cfa

    r342af53 r8e4aa05  
    11void foo(int *) {}
    22void bar(void *) {}
    3 forall(otype T) void baz(T *);
    4 forall(dtype T) void qux(T *);
    5 forall(dtype T | sized(T)) void quux(T *);
     3forall(T) void baz(T *);
     4forall(T &) void qux(T *);
     5forall(T & | sized(T)) void quux(T *);
    66
    77struct A;       // incomplete
     
    3939
    4040
    41 forall(otype T)
     41forall(T)
    4242void baz(T * x) {
    4343        // okay
     
    4949}
    5050
    51 forall(dtype T)
     51forall(T &)
    5252void qux(T * y) {
    5353        // okay
     
    6161}
    6262
    63 forall(dtype T | sized(T))
     63forall(T & | sized(T))
    6464void quux(T * z) {
    6565        // okay
  • tests/exceptions/defaults.cfa

    r342af53 r8e4aa05  
    5555
    5656void unhandled_test(void) {
    57         forall(dtype T, dtype V | is_exception(T, V))
     57        forall(T &, V & | is_exception(T, V))
    5858        void defaultTerminationHandler(T &) {
    5959                throw (unhandled_exception){};
  • tests/exceptions/polymorphic.cfa

    r342af53 r8e4aa05  
    33#include <exception.hfa>
    44
    5 FORALL_TRIVIAL_EXCEPTION(proxy, (otype T), (T));
    6 FORALL_TRIVIAL_INSTANCE(proxy, (otype U), (U))
     5FORALL_TRIVIAL_EXCEPTION(proxy, (T), (T));
     6FORALL_TRIVIAL_INSTANCE(proxy, (U), (U))
    77
    88const char * msg(proxy(int) * this) { return "proxy(int)"; }
     
    3333}
    3434
    35 FORALL_DATA_EXCEPTION(cell, (otype T), (T))(
     35FORALL_DATA_EXCEPTION(cell, (T), (T))(
    3636        T data;
    3737);
    3838
    39 FORALL_DATA_INSTANCE(cell, (otype T), (T))
     39FORALL_DATA_INSTANCE(cell, (T), (T))
    4040
    4141const char * msg(cell(int) * this) { return "cell(int)"; }
  • tests/exceptions/virtual-poly.cfa

    r342af53 r8e4aa05  
    1616};
    1717
    18 forall(otype T)
     18forall(T)
    1919struct mono_child_vtable {
    2020        mono_base_vtable const * const parent;
    2121};
    2222
    23 forall(otype T)
     23forall(T)
    2424struct mono_child {
    2525        mono_child_vtable(T) const * virtual_table;
     
    3737}
    3838
    39 forall(otype U)
     39forall(U)
    4040struct poly_base_vtable {
    4141        poly_base_vtable(U) const * const parent;
    4242};
    4343
    44 forall(otype U)
     44forall(U)
    4545struct poly_base {
    4646        poly_base_vtable(U) const * virtual_table;
    4747};
    4848
    49 forall(otype V)
     49forall(V)
    5050struct poly_child_vtable {
    5151        poly_base_vtable(V) const * const parent;
    5252};
    5353
    54 forall(otype V)
     54forall(V)
    5555struct poly_child {
    5656        poly_child_vtable(V) const * virtual_table;
  • tests/forall.cfa

    r342af53 r8e4aa05  
    1515
    1616void g1() {
    17         forall( otype T ) T f( T ) {};
     17        forall( T ) T f( T ) {};
    1818        void f( int ) {};
    1919        void h( void (*p)(void) ) {};
     
    3232
    3333void g2() {
    34         forall( otype T ) void f( T, T ) {}
    35         forall( otype T, otype U ) void f( T, U ) {}
     34        forall( T ) void f( T, T ) {}
     35        forall( T, U ) void f( T, U ) {}
    3636
    3737        int x;
     
    4545}
    4646
    47 typedef forall ( otype T ) int (* f)( int );
    48 
    49 forall( otype T )
     47typedef forall ( T ) int (* f)( int );
     48
     49forall( T )
    5050void swap( T left, T right ) {
    5151        T temp = left;
     
    5454}
    5555
    56 trait sumable( otype T ) {
     56trait sumable( T ) {
    5757        void ?{}( T &, zero_t );                                                        // 0 literal constructor
    5858        T ?+?( T, T );                                                                          // assortment of additions
     
    6262}; // sumable
    6363
    64 forall( otype T | sumable( T ) )                                                // use trait
     64forall( T | sumable( T ) )                                              // use trait
    6565T sum( size_t size, T a[] ) {
    6666        T total = 0;                                                                            // initialize by 0 constructor
     
    7070} // sum
    7171
    72 forall( otype T | { T ?+?( T, T ); T ?++( T & ); [T] ?+=?( T &,T ); } )
     72forall( T | { T ?+?( T, T ); T ?++( T & ); [T] ?+=?( T &,T ); } )
    7373T twice( T t ) {
    7474        return t + t;
    7575}
    7676
    77 forall( otype T | { int ?<?(T, T); } )
     77forall( T | { int ?<?(T, T); } )
    7878T min( T t1, T t2 ) {
    7979        return t1 < t2 ? t1 : t2;
     
    9191
    9292// Multiple forall
    93 forall( otype T ) forall( otype S ) struct { int i; };
    94 forall( otype T ) struct { int i; } forall( otype S );
    95 struct { int i; } forall( otype T ) forall( otype S );
    96 forall( otype W ) struct { int i; } forall( otype T ) forall( otype S );
     93forall( T ) forall( S ) struct { int i; };
     94forall( T ) struct { int i; } forall( S );
     95struct { int i; } forall( T ) forall( S );
     96forall( W ) struct { int i; } forall( T ) forall( S );
    9797
    9898// Distribution
    9999struct P { int i; };
    100 forall( otype T ) struct Q { T i; };
    101 forall( otype T ) struct { int i; };
     100forall( T ) struct Q { T i; };
     101forall( T ) struct { int i; };
    102102struct KK { int i; };
    103103inline static {
    104104        void RT1() {}
    105105}
    106 forall( otype T ) {
     106forall( T ) {
    107107        T RT2( T ) {
    108108                typedef int TD1;
    109109                struct S1 { T t; };
    110110        }
    111         forall( otype X ) {
     111        forall( X ) {
    112112                typedef int TD2;
    113113                struct S2 {};
     
    117117        }
    118118        extern "C" {
    119                 forall( otype W ) {
     119                forall( W ) {
    120120                        W RT3( W ) {}
    121121                        struct S3 {};
     
    123123        }
    124124        void RT4() {
    125                 forall( otype W ) struct S4 {};
     125                forall( W ) struct S4 {};
    126126                typedef int TD3;
    127127        }
     
    147147
    148148static inline {
    149         forall( otype T ) {
     149        forall( T ) {
    150150                int RT6( T p );
    151151        }
    152         forall( otype T, otype U ) {
     152        forall( T, U ) {
    153153                int RT7( T, U );
    154154        }
    155155}
    156 static forall( otype T ) {
     156static forall( T ) {
    157157        int RT8( T );
    158158}
    159 forall( otype T ) inline static {
     159forall( T ) inline static {
    160160        int RT9( T ) { T t; return 3; }
    161161}
    162162
    163 forall( otype T | { T ?+?( T, T ); } ) {
    164         forall( otype S | { T ?+?( T, S ); } ) {
    165                 forall( otype W ) T bar( T t, S s ) { return t + s; }
    166                 forall( otype W | { W ?+?( T, W ); } ) W baz( T t, S s, W w ) { return t + s + w; }
     163forall( T | { T ?+?( T, T ); } ) {
     164        forall( S | { T ?+?( T, S ); } ) {
     165                forall( W ) T bar( T t, S s ) { return t + s; }
     166                forall( W | { W ?+?( T, W ); } ) W baz( T t, S s, W w ) { return t + s + w; }
    167167                struct W { T t; } (int,int) ww;
    168168                struct P pp;
     
    170170}
    171171
    172 forall( otype T | { T ?+?( T, T ); } ) forall( otype S | { T ?+?( T, S ); } )
     172forall( T | { T ?+?( T, T ); } ) forall( S | { T ?+?( T, S ); } )
    173173struct XW { T t; };
    174174XW(int,int) xww;
    175175
    176 forall( otype T ) struct S { T t; } (int) x, y, z;
    177 forall( otype T ) struct { T t; } (int) a, b, c;
    178 
    179 forall( otype T ) static forall( otype S ) {
    180     forall( otype X ) struct U {
     176forall( T ) struct S { T t; } (int) x, y, z;
     177forall( T ) struct { T t; } (int) a, b, c;
     178
     179forall( T ) static forall( S ) {
     180    forall( X ) struct U {
    181181                T x;
    182182    };
    183183}
    184184
    185 forall( otype T ) {
     185forall( T ) {
    186186        extern "C" {
    187187                struct SS { T t; };
  • tests/function-operator.cfa

    r342af53 r8e4aa05  
    2222
    2323// STL-like Algorithms
    24 trait Assignable(dtype T, dtype U) { T ?=?(T &, U); };
    25 trait Copyable(dtype T) { void ?{}(T &, T); };
    26 trait Destructable(dtype T) { void ^?{}(T &); };
     24trait Assignable(T &, U &) { T ?=?(T &, U); };
     25trait Copyable(T &) { void ?{}(T &, T); };
     26trait Destructable(T &) { void ^?{}(T &); };
    2727
    28 trait Iterator(dtype iter | sized(iter) | Copyable(iter) | Destructable(iter), otype T) {
     28trait Iterator(iter & | sized(iter) | Copyable(iter) | Destructable(iter), T) {
    2929        T & *?(iter);
    3030        iter ++?(iter &);
     
    3232};
    3333
    34 forall(otype Tin, dtype Input | Iterator(Input, Tin), otype Tout, dtype Output | Iterator(Output, Tout) | Assignable(Tout, Tin))
     34forall(Tin, Input & | Iterator(Input, Tin), Tout, Output & | Iterator(Output, Tout) | Assignable(Tout, Tin))
    3535Output copy(Input first, Input last, Output result) {
    3636        while (first != last) {
     
    4242
    4343// test ?()(T *, ...) -- ?() with function call-by-pointer
    44 forall(otype Tin, dtype Input | Iterator(Input, Tin), otype Tout, dtype Output | Iterator(Output, Tout), otype FuncRet, dtype Func | { FuncRet ?()(Func *, Tin); } | Assignable(Tout, FuncRet))
     44forall(Tin, Input & | Iterator(Input, Tin), Tout, Output & | Iterator(Output, Tout), FuncRet, Func & | { FuncRet ?()(Func *, Tin); } | Assignable(Tout, FuncRet))
    4545Output transform (Input first, Input last, Output result, Func * op) {
    4646        while (first != last) {
     
    5252
    5353// test ?()(T, ...) -- ?() with function call-by-value
    54 forall(dtype Iter, otype T | Iterator(Iter, T), otype Pred | { int ?()(Pred, T); })
     54forall(Iter &, T | Iterator(Iter, T), Pred | { int ?()(Pred, T); })
    5555Iter find_if (Iter first, Iter last, Pred pred) {
    5656        while (first != last) {
     
    6262
    6363// test ?()(T, ...) -- ?() with function call-by-reference
    64 forall(otype Generator, otype GenRet | { GenRet ?()(Generator &); }, dtype Iter, otype T | Iterator(Iter, T) | Assignable(T, GenRet))
     64forall(Generator, GenRet | { GenRet ?()(Generator &); }, Iter &, T | Iterator(Iter, T) | Assignable(T, GenRet))
    6565void generate(Iter first, Iter last, Generator & gen) {
    6666        int i = 0;
     
    108108}
    109109
    110 forall(otype T | { int ?==?(T, T); })
     110forall(T | { int ?==?(T, T); })
    111111struct Equals {
    112112        T val;
    113113};
    114114
    115 forall(otype T | { int ?==?(T, T); })
     115forall(T | { int ?==?(T, T); })
    116116int ?()(Equals(T) eq, T x) {
    117117        return eq.val == x;
    118118}
    119119
    120 forall(otype T | { T ?*?(T, T); })
     120forall(T | { T ?*?(T, T); })
    121121struct Multiply {
    122122        T val;
    123123};
    124124
    125 forall(otype T | { T ?*?(T, T); })
     125forall(T | { T ?*?(T, T); })
    126126T ?()(Multiply(T) * mult, T x) {
    127127        return mult->val * x;
     
    130130// TODO: generalize to ttype return; doesn't work yet
    131131// like std::function
    132 forall(otype Return, ttype Args)
     132forall(Return, Args...)
    133133struct function {
    134134        Return (*f)(Args);
  • tests/genericUnion.cfa

    r342af53 r8e4aa05  
    1616#include <limits.hfa>
    1717
    18 forall(otype T)
     18forall(T)
    1919union ByteView {
    2020        T val;
     
    2222};
    2323
    24 forall(otype T)
     24forall(T)
    2525void print(ByteView(T) x) {
    2626        for (int i = 0; i < sizeof(int); i++) {                         // want to change to sizeof(T)
     
    2929}
    3030
    31 forall(otype T)
     31forall(T)
    3232void f(ByteView(T) x, T val) {
    3333        print(x);
  • tests/global-monomorph.cfa

    r342af53 r8e4aa05  
    11// Create monomorphic instances of polymorphic types at global scope.
    22
    3 forall(dtype T)
     3forall(T &)
    44void poly0(T &) {}
    55
    6 forall(dtype T | sized(T))
     6forall(T & | sized(T))
    77void poly1(T &) {}
    88
    9 forall(otype T)
     9forall(T)
    1010void poly2(T &) {}
    1111
  • tests/identity.cfa

    r342af53 r8e4aa05  
    1616#include <fstream.hfa>
    1717
    18 forall( otype T )
     18forall( T )
    1919T identity( T t ) {
    2020        return t;
  • tests/init1.cfa

    r342af53 r8e4aa05  
    120120}
    121121
    122 forall (dtype T, dtype S)
     122forall (T &, S &)
    123123T & anycvt( S & s ) {
    124124    return s;               // mismatched referenced type
    125125}
    126126
    127 forall (dtype T, dtype S)
     127forall (T &, S &)
    128128T * anycvt( S * s ) {
    129129    return s;               // mismatched referenced type
  • tests/io/.expect/io1.oast.txt

    r342af53 r8e4aa05  
    1 9 6 28 0 7 1 2
    2 0 1 2 3
    3 0123
    4 0123
    5 
    61opening delimiters
    72x (1 x [2 x {3 x =4 x $5 x £6 x ¥7 x ¡8 x ¿9 x «10
    83
    94closing delimiters
    10 1, x 2. x 3; x 4! x 5? x 6% x 7 ¢ x 8 » x 9) x 10] x 11} x
     51, x 2. x 3; x 4! x 5? x 6% x 7¢ x 8» x 9) x 10] x 11} x
    116
    127opening/closing delimiters
     
    2419x ( 1 ) x 2 , x 3 :x: 4
    2520
     21spacing
     220 1 2 3
     230123
     240123
     25
     26expressions
     279 6 28 0 7 1 2
  • tests/io/io1.cfa

    r342af53 r8e4aa05  
    1010// Created On       : Wed Mar  2 16:56:02 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Mar  4 21:42:47 2019
    13 // Update Count     : 115
     12// Last Modified On : Sun Feb 21 10:07:07 2021
     13// Update Count     : 119
    1414//
    1515
     
    1717
    1818int main() {
    19         int x = 3, y = 5, z = 7;
    20         sout | x * 3 | y + 1 | z << 2 | x == y | (x | y) | (x || y) | (x > z ? 1 : 2);
    21         sout | 0 | 1 | 2 | 3;
    22         sout | '0' | '1' | '2' | '3';
    23         sout | 0 | "" | 1 | "" | 2 | "" | 3;
    24         sout | nl;
     19        sout | nlOff;                                                                           // auto nl off
    2520
    26         sout | nlOff;
    2721        sout | "opening delimiters" | nl;
    2822        sout | "x (" | 1;
     
    6155        sout | nl | nl;
    6256
    63         sout | nlOn;
     57        sout | nlOn;                                                                            // auto nl on
     58
    6459        sout | "override opening/closing delimiters";
    6560        sout | "x ( " | 1 | " ) x" | 2 | " , x" | 3 | " :x: " | 4;
    6661        sout | nl;
     62
     63        sout | "spacing";
     64        sout | 0 | 1 | 2 | 3;
     65        sout | '0' | '1' | '2' | '3';
     66        sout | 0 | "" | 1 | "" | 2 | "" | 3;
     67        sout | nl;
     68
     69        sout | "expressions";
     70        int x = 3, y = 5, z = 7;
     71        sout | x * 3 | y + 1 | z << 2 | x == y | (x | y) | (x || y) | (x > z ? 1 : 2);
    6772}
    6873
  • tests/nested-types.cfa

    r342af53 r8e4aa05  
    1616typedef int N;
    1717struct A {
    18         forall(otype T)
     18        forall(T)
    1919        struct N {
    2020                T x;
  • tests/poly-d-cycle.cfa

    r342af53 r8e4aa05  
    11// Check that a cycle of polymorphic dtype structures can be instancated.
    22
    3 forall(dtype T)
     3forall(T &)
    44struct func_table;
    55
    6 forall(dtype U)
     6forall(U &)
    77struct object {
    88        func_table(U) * virtual_table;
    99};
    1010
    11 forall(dtype T)
     11forall(T &)
    1212struct func_table {
    1313        void (*object_func)(object(T) *);
  • tests/poly-o-cycle.cfa

    r342af53 r8e4aa05  
    11// Check that a cycle of polymorphic otype structures can be instancated.
    22
    3 forall(otype T)
     3forall(T)
    44struct func_table;
    55
    6 forall(otype U)
     6forall(U)
    77struct object {
    88        func_table(U) * virtual_table;
    99};
    1010
    11 forall(otype T)
     11forall(T)
    1212struct func_table {
    1313        void (*object_func)(object(T) *);
  • tests/polymorphism.cfa

    r342af53 r8e4aa05  
    1818#include <fstream.hfa>
    1919
    20 forall(otype T)
     20forall(T)
    2121T f(T x, T y) {
    2222        x = y;
     
    2424}
    2525
    26 forall(otype T) T ident(T x) {
     26forall(T) T ident(T x) {
    2727        return x;
    2828}
    2929
    30 forall( otype T, otype U )
     30forall( T, U )
    3131size_t struct_size( T i, U j ) {
    3232        struct S { T i; U j; };
     
    3434}
    3535
    36 forall( otype T, otype U )
     36forall( T, U )
    3737size_t union_size( T i, U j ) {
    3838        union B { T i; U j; };
     
    4141
    4242// perform some simple operations on aggregates of T and U
    43 forall( otype T | { void print(T); int ?==?(T, T); }, otype U | { void print(U); U ?=?(U&, zero_t); } )
     43forall( T | { void print(T); int ?==?(T, T); }, U | { void print(U); U ?=?(U&, zero_t); } )
    4444U foo(T i, U j) {
    4545        struct S { T i; U j; };
  • tests/raii/ctor-autogen.cfa

    r342af53 r8e4aa05  
    3333
    3434// dtype-static generic type is otype
    35 forall(dtype T)
     35forall(T &)
    3636struct DtypeStaticStruct {
    3737  T * data;
     
    3939};
    4040
    41 forall(dtype T)
     41forall(T &)
    4242union DtypeStaticUnion {
    4343  T * data;
     
    4646
    4747// dynamic generic type is otype
    48 forall(otype T)
     48forall(T)
    4949struct DynamicStruct {
    5050        T x;
    5151};
    5252
    53 forall(otype T)
     53forall(T)
    5454union DynamicUnion {
    5555        T x;
     
    8080
    8181
    82 forall(otype T)
     82forall(T)
    8383T identity(T x) { return x; }
    8484
  • tests/simpleGenericTriple.cfa

    r342af53 r8e4aa05  
    1414//
    1515
    16 forall(otype T)
     16forall(T)
    1717struct T3 {
    1818        T f0, f1, f2;
    1919};
    2020
    21 forall(otype T | { T ?+?(T, T); })
     21forall(T | { T ?+?(T, T); })
    2222T3(T) ?+?(T3(T) x, T3(T) y) {
    2323        T3(T) z = { x.f0+y.f0, x.f1+y.f1, x.f2+y.f2 };
  • tests/smart-pointers.cfa

    r342af53 r8e4aa05  
    22
    33#include <memory.hfa>
    4 #include <stdlib.hfa>
     4#include <assert.h>
    55
    66void counter_test(void) {
     
    5353}
    5454
     55void declare_test(void) {
     56        counter_ptr(int) ptr_i0 = 3;
     57        counter_ptr(char) ptr_c0 = 'a';
     58        counter_ptr(float) ptr_f0 = 3.5f;
     59        counter_ptr(double) ptr_d0 = 3.5;
     60
     61        unique_ptr(int) ptr_i1 = 3;
     62        unique_ptr(char) ptr_c1 = 'a';
     63        unique_ptr(float) ptr_f1 = 3.5f;
     64        unique_ptr(double) ptr_d1 = 3.5;
     65}
     66
    5567int main(int argc, char * argv[]) {
    5668        counter_test();
    5769        unique_test();
    5870        pointer_equality();
     71
     72        printf("done\n");
    5973}
  • tests/sum.cfa

    r342af53 r8e4aa05  
    1818#include <stdlib.hfa>
    1919
    20 trait sumable( otype T ) {
     20trait sumable( T ) {
    2121        void ?{}( T &, zero_t );                                                        // 0 literal constructor
    2222        T ?+?( T, T );                                                                          // assortment of additions
     
    2626}; // sumable
    2727
    28 forall( otype T | sumable( T ) )                                                // use trait
     28forall( T | sumable( T ) )                                              // use trait
    2929T sum( size_t size, T a[] ) {
    3030        T total = 0;                                                                            // initialize by 0 constructor
     
    107107                 | sum( size, (S *)a ) | ", check" | (S)s;
    108108
    109         forall( otype Impl | sumable( Impl ) )
     109        forall( Impl | sumable( Impl ) )
    110110        struct GS {
    111111                Impl * x, * y;
     
    194194                 sum( size, (S *)a ).[i, j], s.[i, j] );
    195195
    196         forall( otype Impl | sumable( Impl ) )
     196        forall( Impl | sumable( Impl ) )
    197197        struct GS {
    198198                Impl * x, * y;
  • tests/tuple/tuplePolymorphism.cfa

    r342af53 r8e4aa05  
    2929// ensure that f is a viable candidate for g, even though its parameter structure does not exactly match
    3030[A] f([A, B] x, B y) { printf("%g %c %g %lld %c %lld %lld %c %lld\n", x.0.[x,y,z], x.1.[x,y,z], y.[x,y,z]); return x.0; }
    31 forall(otype T, otype U | { T f(T, U, U); })
     31forall(T, U | { T f(T, U, U); })
    3232void g(T x, U y) { f(x, y, y); }
    3333
    3434// add two triples
    35 forall(otype T | { T ?+?(T, T); })
     35forall(T | { T ?+?(T, T); })
    3636[T, T, T] ?+?([T, T, T] x, [T, T, T] y) {
    3737        return [x.0+y.0, x.1+y.1, x.2+y.2];
     
    6464}
    6565
    66 forall(otype T)
     66forall(T)
    6767[T, T] foo([T, T] y) {
    6868        [T, T] x;
  • tests/tuple/tupleVariadic.cfa

    r342af53 r8e4aa05  
    1919        printf("called func(void)\n");
    2020}
    21 forall(otype T, ttype Params | { void process(T); void func(Params); })
     21forall(T, Params... | { void process(T); void func(Params); })
    2222void func(T arg1, Params p) {
    2323        process(arg1);
     
    9292}
    9393
    94 forall(otype T)
     94forall(T)
    9595T * copy(T x) {
    9696        // test calling new inside a polymorphic function
     
    9898}
    9999
    100 forall(ttype T | { void foo(T); }) void bar(T x) {}
     100forall(T... | { void foo(T); }) void bar(T x) {}
    101101void foo(int) {}
    102102
  • tests/zombies/ArrayN.c

    r342af53 r8e4aa05  
    66// }
    77
    8 forall(otype index_t)
     8forall(index_t)
    99index_t offset_to_index(unsigned offset, index_t size) {
    1010    return [offset / size.0, offset % size.1];
  • tests/zombies/Members.c

    r342af53 r8e4aa05  
    22int ?=?( int*, int );
    33float ?=?( float*, float );
    4 forall( dtype DT ) DT * ?=?( DT**, DT* );
    5 forall(otype T) lvalue T *?( T* );
     4forall( DT & ) DT * ?=?( DT**, DT* );
     5forall(T) lvalue T *?( T* );
    66char *__builtin_memcpy();
    77
  • tests/zombies/Rank2.c

    r342af53 r8e4aa05  
    11int ?=?( int &, int );
    2 forall(dtype DT) DT * ?=?( DT *&, DT * );
     2forall(DT &) DT * ?=?( DT *&, DT * );
    33
    44void a() {
    5         forall( otype T ) void f( T );
    6         void g( forall( otype U ) void p( U ) );
     5        forall( T ) void f( T );
     6        void g( forall( U ) void p( U ) );
    77        g( f );
    88}
     
    1010void g() {
    1111        void h( int *null );
    12         forall( otype T ) T id( T );
     12        forall( T ) T id( T );
    1313//      forall( dtype T ) T *0;
    1414//      int 0;
  • tests/zombies/abstype.c

    r342af53 r8e4aa05  
    2121}
    2222
    23 forall( otype T ) T *?( T * );
     23forall( T ) T *?( T * );
    2424int ?++( int * );
    2525int ?=?( int *, int );
    26 forall( dtype DT ) DT * ?=?( DT **, DT * );
     26forall( DT & ) DT * ?=?( DT **, DT * );
    2727
    2828otype U = int *;
  • tests/zombies/context.cfa

    r342af53 r8e4aa05  
    11// trait declaration
    22
    3 trait has_q( otype T ) {
     3trait has_q( T ) {
    44        T q( T );
    55};
    66
    7 forall( otype z | has_q( z ) ) void f() {
    8         trait has_r( otype T, otype U ) {
     7forall( z | has_q( z ) ) void f() {
     8        trait has_r( T, U ) {
    99                T r( T, T (T,U) );
    1010        };
  • tests/zombies/gc_no_raii/bug-repro/blockers/explicit_cast.c

    r342af53 r8e4aa05  
    99};
    1010
    11 forall(otype T)
     11forall(T)
    1212struct gcpointer
    1313{
     
    1515};
    1616
    17 forall(otype T)
     17forall(T)
    1818static inline gcpointer(T) gcmalloc()
    1919{
  • tests/zombies/gc_no_raii/bug-repro/blockers/recursive_realloc.c

    r342af53 r8e4aa05  
    33#include <stdlib.hfa>
    44
    5 trait allocator_c(otype T, otype allocator_t)
     5trait allocator_c(T, allocator_t)
    66{
    77        void realloc(allocator_t* const, size_t);
    88};
    99
    10 forall(otype T)
     10forall(T)
    1111struct heap_allocator
    1212{
     
    1515};
    1616
    17 forall(otype T)
     17forall(T)
    1818inline void realloc(heap_allocator(T) *const this, size_t size)
    1919{
  • tests/zombies/gc_no_raii/bug-repro/deref.c

    r342af53 r8e4aa05  
    1     forall(otype T)
     1    forall(T)
    22    struct wrap
    33    {
     
    55    };
    66
    7     forall(otype T)
     7    forall(T)
    88    T *? (wrap(T) rhs)
    99    {
  • tests/zombies/gc_no_raii/bug-repro/field.c

    r342af53 r8e4aa05  
    88//------------------------------------------------------------------------------
    99//Declaration
    10 trait allocator_c(otype T, otype allocator_t)
     10trait allocator_c(T, allocator_t)
    1111{
    1212        void ctor(allocator_t* const);
     
    1616};
    1717
    18 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     18forall(T, allocator_t | allocator_c(T, allocator_t))
    1919struct vector
    2020{
  • tests/zombies/gc_no_raii/bug-repro/malloc.c

    r342af53 r8e4aa05  
    1 forall(otype T)
     1forall(T)
    22struct wrapper
    33{
     
    55};
    66
    7 forall(otype T)
     7forall(T)
    88void ctor(wrapper(T)* this)
    99{
     
    1111}
    1212
    13 forall(otype T)
     13forall(T)
    1414wrapper(T) gcmalloc()
    1515{
     
    1919}
    2020
    21 forall(otype T)
     21forall(T)
    2222wrapper(T)* ?=? (wrapper(T)* lhs, wrapper(T)* rhs)
    2323{
  • tests/zombies/gc_no_raii/bug-repro/oddtype.c

    r342af53 r8e4aa05  
    1 forall(dtype T)
     1forall(T &)
    22struct wrap {
    33        int i;
    44};
    55
    6 forall(otype T) void ?{}(wrap(T)* this) {}
    7 forall(otype T) void ?=?(wrap(T)* this) {}
    8 forall(otype T) void ^?{}(wrap(T)* this) {}
     6forall(T) void ?{}(wrap(T)* this) {}
     7forall(T) void ?=?(wrap(T)* this) {}
     8forall(T) void ^?{}(wrap(T)* this) {}
    99
    1010struct List_t {
  • tests/zombies/gc_no_raii/bug-repro/push_back.h

    r342af53 r8e4aa05  
    11//------------------------------------------------------------------------------
    22//Declaration
    3 trait allocator_c(otype T, otype allocator_t) {
     3trait allocator_c(T, allocator_t) {
    44        void ctor(allocator_t* const);
    55        void dtor(allocator_t* const);
     
    88};
    99
    10 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     10forall(T, allocator_t | allocator_c(T, allocator_t))
    1111struct vector
    1212{
     
    1717//------------------------------------------------------------------------------
    1818//Initialization
    19 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     19forall(T, allocator_t | allocator_c(T, allocator_t))
    2020void vector_ctor(vector(T, allocator_t) *const this);
    2121
    22 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     22forall(T, allocator_t | allocator_c(T, allocator_t))
    2323void dtor(vector(T, allocator_t) *const this);
    2424
    2525//------------------------------------------------------------------------------
    2626//Allocator
    27 forall(otype T)
     27forall(T)
    2828struct heap_allocator
    2929{
     
    3232};
    3333
    34 forall(otype T)
     34forall(T)
    3535void ctor(heap_allocator(T) *const this);
    3636
    37 forall(otype T)
     37forall(T)
    3838void dtor(heap_allocator(T) *const this);
    3939
    40 forall(otype T)
     40forall(T)
    4141void realloc(heap_allocator(T) *const this, size_t size);
    4242
    43 forall(otype T)
     43forall(T)
    4444inline T* data(heap_allocator(T) *const this)
    4545{
     
    4949//------------------------------------------------------------------------------
    5050//Capacity
    51 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     51forall(T, allocator_t | allocator_c(T, allocator_t))
    5252inline bool empty(vector(T, allocator_t) *const this)
    5353{
     
    5555}
    5656
    57 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     57forall(T, allocator_t | allocator_c(T, allocator_t))
    5858inline bool size(vector(T, allocator_t) *const this)
    5959{
     
    6161}
    6262
    63 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     63forall(T, allocator_t | allocator_c(T, allocator_t))
    6464inline void reserve(vector(T, allocator_t) *const this, size_t size)
    6565{
     
    6969//------------------------------------------------------------------------------
    7070//Modifiers
    71 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     71forall(T, allocator_t | allocator_c(T, allocator_t))
    7272void push_back(vector(T, allocator_t) *const this, T value);
  • tests/zombies/gc_no_raii/bug-repro/realloc.c

    r342af53 r8e4aa05  
    11void* realloc(void*, unsigned long int);
    22
    3 forall(otype T)
     3forall(T)
    44struct wrap
    55{
     
    77};
    88
    9 forall(otype T)
     9forall(T)
    1010static inline void realloc(wrap(T) *const this, unsigned long int size)
    1111{
  • tests/zombies/gc_no_raii/bug-repro/return.c

    r342af53 r8e4aa05  
    1 forall(otype T)
     1forall(T)
    22struct wrapper
    33{
     
    55};
    66
    7 forall(otype T)
     7forall(T)
    88wrapper(T) create()
    99{
     
    1212}
    1313
    14 forall(otype T)
     14forall(T)
    1515wrapper(T)* ?=?(wrapper(T)* lhs, wrapper(T)* rhs)
    1616{
  • tests/zombies/gc_no_raii/bug-repro/return_template.c

    r342af53 r8e4aa05  
    1 forall(otype T)
     1forall(T)
    22struct wrap
    33{
     
    55};
    66
    7 forall(otype T) void ?{}(wrap(T)* this);
    8 forall(otype T) void ?{}(wrap(T)* this, wrap(T)* rhs);
    9 forall(otype T) void ^?{}(wrap(T)* this);
    10 forall(otype T) void ?=?(wrap(T)* this, wrap(T)* rhs);
     7forall(T) void ?{}(wrap(T)* this);
     8forall(T) void ?{}(wrap(T)* this, wrap(T)* rhs);
     9forall(T) void ^?{}(wrap(T)* this);
     10forall(T) void ?=?(wrap(T)* this, wrap(T)* rhs);
    1111
    12 forall(otype T)
     12forall(T)
    1313wrap(T) test()
    1414{
  • tests/zombies/gc_no_raii/bug-repro/slow_malloc.c

    r342af53 r8e4aa05  
    11#include <stdlib.hfa>
    22
    3 forall(otype T)
     3forall(T)
    44struct heap_allocator
    55{
  • tests/zombies/gc_no_raii/bug-repro/zero.c

    r342af53 r8e4aa05  
    1 forall(otype T)
     1forall(T)
    22struct wrap
    33{
     
    55};
    66
    7 forall(otype T)
     7forall(T)
    88int ?==? (wrap(T) lhs, wrap(T) rhs)
    99{
     
    1414struct wrap(int) 0;
    1515/*/
    16 forall(otype T)
     16forall(T)
    1717struct wrap(T) 0;
    1818//*/
  • tests/zombies/gc_no_raii/src/gc.h

    r342af53 r8e4aa05  
    1313// }
    1414
    15 forall(otype T)
     15forall(T)
    1616static inline void gcmalloc(gcpointer(T)* ptr)
    1717{
  • tests/zombies/gc_no_raii/src/gcpointers.c

    r342af53 r8e4aa05  
    113113#endif
    114114
    115 forall(otype T) void ?{}(gcpointer(T)* this) {
     115forall(T) void ?{}(gcpointer(T)* this) {
    116116        (&this->internal) {};
    117117}
    118118
    119 forall(otype T) void ?{}(gcpointer(T)* this, void* address) {
     119forall(T) void ?{}(gcpointer(T)* this, void* address) {
    120120        (&this->internal) { address };
    121121}
    122122
    123 forall(otype T) void ?{}(gcpointer(T)* this, gcpointer(T) other) {
     123forall(T) void ?{}(gcpointer(T)* this, gcpointer(T) other) {
    124124        (&this->internal) { other.internal };
    125125}
    126126
    127 forall(otype T) void ^?{}(gcpointer(T)* this) {
     127forall(T) void ^?{}(gcpointer(T)* this) {
    128128        ^?{}(&this->internal);
    129129}
    130130
    131 forall(otype T) gcpointer(T) ?=?(gcpointer(T)* this, gcpointer(T) rhs) {
     131forall(T) gcpointer(T) ?=?(gcpointer(T)* this, gcpointer(T) rhs) {
    132132        this->internal = rhs.internal;
    133133        return *this;
     
    136136// forall(otype T) T *?(gcpointer(T) this);
    137137
    138 forall(otype T) T* get(gcpointer(T)* this) {
     138forall(T) T* get(gcpointer(T)* this) {
    139139        return (T*)this->internal.ptr;
    140140}
    141141//
    142142// //Logical operators
    143 forall(otype T) int ?!=?(gcpointer(T) this, int zero) {
     143forall(T) int ?!=?(gcpointer(T) this, int zero) {
    144144        return this.internal.ptr != 0;
    145145}
  • tests/zombies/gc_no_raii/src/gcpointers.h

    r342af53 r8e4aa05  
    44#include <stdint.h>
    55
    6 forall(dtype T)
     6forall(T &)
    77struct gcpointer;
    88
     
    2929#endif
    3030
    31 forall(dtype T)
     31forall(T &)
    3232struct gcpointer
    3333{
     
    3636
    3737//
    38 forall(otype T) void ?{}(gcpointer(T)* this);
    39 forall(otype T) void ?{}(gcpointer(T)* this, void* address);
    40 forall(otype T) void ?{}(gcpointer(T)* this, gcpointer(T) other);
    41 forall(otype T) void ^?{}(gcpointer(T)* this);
    42 forall(otype T) gcpointer(T) ?=?(gcpointer(T)* this, gcpointer(T) rhs);
     38forall(T) void ?{}(gcpointer(T)* this);
     39forall(T) void ?{}(gcpointer(T)* this, void* address);
     40forall(T) void ?{}(gcpointer(T)* this, gcpointer(T) other);
     41forall(T) void ^?{}(gcpointer(T)* this);
     42forall(T) gcpointer(T) ?=?(gcpointer(T)* this, gcpointer(T) rhs);
    4343
    4444
    4545// forall(otype T) T *?(gcpointer(T) this);
    46 forall(otype T) T* get(gcpointer(T)* this);
     46forall(T) T* get(gcpointer(T)* this);
    4747
    4848//Logical operators
    49 forall(otype T) int ?!=?(gcpointer(T) this, int zero);
    50 forall(otype T) int ?!=?(gcpointer(T) this, gcpointer(T) rhs);
    51 forall(otype T) int ?==?(gcpointer(T) this, gcpointer(T) rhs);
     49forall(T) int ?!=?(gcpointer(T) this, int zero);
     50forall(T) int ?!=?(gcpointer(T) this, gcpointer(T) rhs);
     51forall(T) int ?==?(gcpointer(T) this, gcpointer(T) rhs);
  • tests/zombies/gc_no_raii/src/tools.h

    r342af53 r8e4aa05  
    1212// }
    1313
    14 trait has_equal(otype T)
     14trait has_equal(T)
    1515{
    1616        signed int ?==?(T a, T b);
    1717};
    1818
    19 trait InputIterator_t(otype T, otype InputIterator)
     19trait InputIterator_t(T, InputIterator)
    2020{
    2121        signed int ?==?(InputIterator a, InputIterator b);
     
    2626};
    2727
    28 forall(otype T | has_equal(T), otype InputIterator | InputIterator_t(T, InputIterator))
     28forall(T | has_equal(T), InputIterator | InputIterator_t(T, InputIterator))
    2929static inline InputIterator find( InputIterator first, const InputIterator* const last, T val)
    3030{
  • tests/zombies/hashtable.cfa

    r342af53 r8e4aa05  
    1414
    1515
    16 trait has_hash( otype K ) {
     16trait has_hash( K ) {
    1717    size_t hash(K);
    1818    int ?==?( K, K );
    1919};
    2020
    21 trait hkey( otype K, dtype tN | has_hash(K) ) {
     21trait hkey( K, tN & | has_hash(K) ) {
    2222    K key(tN &);
    2323};
    2424
    25 forall( otype K, dtype tN, dtype tE | $dlistable(tN, tE) | hkey(K, tN) ) {
     25forall( K, tN &, tE & | $dlistable(tN, tE) | hkey(K, tN) ) {
    2626
    2727    struct hashtable {
     
    3939}
    4040
    41 forall( otype K, dtype tN, dtype tE | $dlistable(tN, tE) | hkey(K, tN) | { void defaultResumptionHandler(ht_fill_limit_crossed &); } ) {
     41forall( K, tN &, tE & | $dlistable(tN, tE) | hkey(K, tN) | { void defaultResumptionHandler(ht_fill_limit_crossed &); } ) {
    4242
    4343    void ?{}( hashtable(K, tN, tE) & this, size_t n_buckets, dlist(tN, tE) *buckets ) {
     
    5757}
    5858
    59 forall( otype K, dtype tN, dtype tE | $dlistable(tN, tE) | hkey(K, tN) ) {
     59forall( K, tN &, tE & | $dlistable(tN, tE) | hkey(K, tN) ) {
    6060
    6161    float fill_frac( hashtable(K, tN, tE) & this ) with(this) {
     
    124124
    125125
    126 trait heaped(dtype T) {
     126trait heaped(T &) {
    127127    T * alloc( size_t );
    128128    void free( void * );
     
    133133}
    134134
    135 forall( otype K, dtype tN, dtype tE | $dlistable(tN, tE) | hkey(K, tN) | heaped( dlist(tN, tE) ) ) {
     135forall( K, tN &, tE & | $dlistable(tN, tE) | hkey(K, tN) | heaped( dlist(tN, tE) ) ) {
    136136
    137137    struct hashtable_dynamic {
  • tests/zombies/hashtable2.cfa

    r342af53 r8e4aa05  
    6969
    7070
    71 trait pretendsToMatter( dtype TTT ) {
     71trait pretendsToMatter( TTT & ) {
    7272    void actsmart(TTT &);
    7373};
    7474
    75 forall( dtype TTTx )
     75forall( TTTx & )
    7676void actsmart(TTTx &) {}
    7777
     
    8686//   2. shows up in -CFA output as hashtable_rbs(), which is bad C; expecting hashtable_rbs*
    8787
    88 forall( otype Tt_unused | pretendsToMatter(Tt_unused) ) {
     88forall( Tt_unused | pretendsToMatter(Tt_unused) ) {
    8989
    9090    // hashtable of request by source
     
    104104}
    105105
    106 forall( otype Tt_unused | pretendsToMatter(Tt_unused) | { void defaultResumptionHandler(ht_fill_limit_crossed &); } ) {
     106forall( Tt_unused | pretendsToMatter(Tt_unused) | { void defaultResumptionHandler(ht_fill_limit_crossed &); } ) {
    107107
    108108    void ?{}( hashtable_rbs(Tt_unused) & this, size_t n_buckets, dlist(request_in_ht_by_src, request) *buckets,
     
    135135void defaultResumptionHandler( ht_auto_resize_pending & ex );
    136136
    137 forall( otype Tt_unused | pretendsToMatter(Tt_unused) ) {
     137forall( Tt_unused | pretendsToMatter(Tt_unused) ) {
    138138
    139139    float fill_frac( hashtable_rbs(Tt_unused) & this ) with(this) {
     
    221221
    222222
    223 trait heaped(dtype T) {
     223trait heaped(T &) {
    224224    T * alloc( size_t );
    225225    void free( void * );
     
    228228void __dynamic_defaultResumptionHandler(ht_fill_limit_crossed &);
    229229
    230 forall( otype Tt_unused ) {
     230forall( Tt_unused ) {
    231231
    232232    struct hashtable_rbs_dynamic {
     
    263263
    264264
    265 forall( otype Tt_unused | heaped( dlist(request_in_ht_by_src, request) ) ) {
     265forall( Tt_unused | heaped( dlist(request_in_ht_by_src, request) ) ) {
    266266
    267267    void ?{}( hashtable_rbs_dynamic(Tt_unused).resize_policy & this, size_t nbuckets_floor ) {
     
    325325}
    326326
    327 forall( otype Tt_unused ) {
     327forall( Tt_unused ) {
    328328    void rehashToLarger_STEP( hashtable_rbs_dynamic(Tt_unused) & this, size_t new_n_buckets ) with (this) {
    329329        rehashToLarger( this, new_n_buckets );
  • tests/zombies/huge.c

    r342af53 r8e4aa05  
    1414//
    1515
    16 int huge( int n, forall( otype T ) T (*f)( T ) ) {
     16int huge( int n, forall( T ) T (*f)( T ) ) {
    1717        if ( n <= 0 )
    1818                return f( 0 );
  • tests/zombies/it_out.c

    r342af53 r8e4aa05  
    1616typedef unsigned long streamsize_type;
    1717
    18 trait ostream( dtype os_type ) {
     18trait ostream( os_type & ) {
    1919        os_type *write( os_type *, const char *, streamsize_type );
    2020        int fail( os_type * );
    2121};
    2222
    23 trait writeable( otype T ) {
    24         forall( dtype os_type | ostream( os_type ) ) os_type * ?<<?( os_type *, T );
     23trait writeable( T ) {
     24        forall( os_type & | ostream( os_type ) ) os_type * ?<<?( os_type *, T );
    2525};
    2626
    27 forall( dtype os_type | ostream( os_type ) ) os_type * ?<<?( os_type *, char );
    28 forall( dtype os_type | ostream( os_type ) ) os_type * ?<<?( os_type *, int );
    29 forall( dtype os_type | ostream( os_type ) ) os_type * ?<<?( os_type *, const char * );
     27forall( os_type & | ostream( os_type ) ) os_type * ?<<?( os_type *, char );
     28forall( os_type & | ostream( os_type ) ) os_type * ?<<?( os_type *, int );
     29forall( os_type & | ostream( os_type ) ) os_type * ?<<?( os_type *, const char * );
    3030
    31 trait istream( dtype is_type ) {
     31trait istream( is_type & ) {
    3232        is_type *read( is_type *, char *, streamsize_type );
    3333        is_type *unread( is_type *, char );
     
    3636};
    3737
    38 trait readable( otype T ) {
    39         forall( dtype is_type | istream( is_type ) ) is_type * ?<<?( is_type *, T );
     38trait readable( T ) {
     39        forall( is_type & | istream( is_type ) ) is_type * ?<<?( is_type *, T );
    4040};
    4141
    42 forall( dtype is_type | istream( is_type ) ) is_type * ?>>?( is_type *, char* );
    43 forall( dtype is_type | istream( is_type ) ) is_type * ?>>?( is_type *, int* );
     42forall( is_type & | istream( is_type ) ) is_type * ?>>?( is_type *, char* );
     43forall( is_type & | istream( is_type ) ) is_type * ?>>?( is_type *, int* );
    4444
    45 trait iterator( otype iterator_type, otype elt_type ) {
     45trait iterator( iterator_type, elt_type ) {
    4646        iterator_type ?++( iterator_type* );
    4747        iterator_type ++?( iterator_type* );
     
    5252};
    5353
    54 forall( otype elt_type | writeable( elt_type ),
    55                 otype iterator_type | iterator( iterator_type, elt_type ),
    56                 dtype os_type | ostream( os_type ) )
     54forall( elt_type | writeable( elt_type ),
     55                iterator_type | iterator( iterator_type, elt_type ),
     56                os_type & | ostream( os_type ) )
    5757void write_all( iterator_type begin, iterator_type end, os_type *os );
    5858
    59 forall( otype elt_type | writeable( elt_type ),
    60                 otype iterator_type | iterator( iterator_type, elt_type ),
    61                 dtype os_type | ostream( os_type ) )
     59forall( elt_type | writeable( elt_type ),
     60                iterator_type | iterator( iterator_type, elt_type ),
     61                os_type & | ostream( os_type ) )
    6262void write_all( elt_type begin, iterator_type end, os_type *os ) {
    6363        os << begin;
  • tests/zombies/new.c

    r342af53 r8e4aa05  
    1414//
    1515
    16 forall( otype T )
     16forall( T )
    1717void f( T *t ) {
    1818        t--;
  • tests/zombies/occursError.cfa

    r342af53 r8e4aa05  
    1 forall( otype T ) void f( void (*)( T, T * ) );
    2 forall( otype U ) void g( U,  U * );
    3 forall( otype U ) void h( U *, U );
     1forall( T ) void f( void (*)( T, T * ) );
     2forall( U ) void g( U,  U * );
     3forall( U ) void h( U *, U );
    44
    55void test() {
  • tests/zombies/prolog.c

    r342af53 r8e4aa05  
    2525void is_integer( int x ) {}
    2626
    27 trait ArithmeticType( otype T ) {
     27trait ArithmeticType( T ) {
    2828        void is_arithmetic( T );
    2929};
    3030
    31 trait IntegralType( otype T | ArithmeticType( T ) ) {
     31trait IntegralType( T | ArithmeticType( T ) ) {
    3232        void is_integer( T );
    3333};
    3434
    35 forall( otype T | IntegralType( T ) | { void printResult( T ); } )
     35forall( T | IntegralType( T ) | { void printResult( T ); } )
    3636void hornclause( T param ) {
    3737        printResult( param );
  • tests/zombies/quad.c

    r342af53 r8e4aa05  
    1616#include <fstream.hfa>
    1717
    18 forall( otype T | { T ?*?( T, T ); } )
     18forall( T | { T ?*?( T, T ); } )
    1919T square( T t ) {
    2020        return t * t;
    2121}
    2222
    23 forall( otype U | { U square( U ); } )
     23forall( U | { U square( U ); } )
    2424U quad( U u ) {
    2525        return square( square( u ) );
  • tests/zombies/scope.cfa

    r342af53 r8e4aa05  
    2020y p;
    2121
    22 trait has_u( otype z ) {
     22trait has_u( z ) {
    2323        z u(z);
    2424};
    2525
    26 forall( otype t | has_u( t ) )
     26forall( t | has_u( t ) )
    2727y q( t the_t ) {
    2828        t y = u( the_t );
  • tests/zombies/simplePoly.c

    r342af53 r8e4aa05  
    1414//
    1515
    16 forall( otype T, otype U | { T f( T, U ); } )
     16forall( T, U | { T f( T, U ); } )
    1717T q( T t, U u ) {
    1818        return f( t, u );
  • tests/zombies/simpler.c

    r342af53 r8e4aa05  
    1414//
    1515
    16 forall( otype T ) T id( T, T );
     16forall( T ) T id( T, T );
    1717
    1818int main() {
  • tests/zombies/specialize.c

    r342af53 r8e4aa05  
    3939}
    4040
    41 forall( otype T ) T f( T t )
     41forall( T ) T f( T t )
    4242{
    4343        printf( "in f; sizeof T is %d\n", sizeof( T ) );
  • tests/zombies/square.c

    r342af53 r8e4aa05  
    1616#include <fstream.hfa>
    1717
    18 forall( otype T | { T ?*?( T, T ); } )
     18forall( T | { T ?*?( T, T ); } )
    1919T square( T t ) {
    2020        return t * t;
  • tests/zombies/structMember.cfa

    r342af53 r8e4aa05  
    6666        S.T;
    6767        .S.T;
    68         forall( otype S, otype T ) struct W {
     68        forall( S, T ) struct W {
    6969                struct X {};
    7070        };
  • tests/zombies/subrange.cfa

    r342af53 r8e4aa05  
    11// A small context defining the notion of an ordered otype.  (The standard
    22// library should probably contain a context for this purpose.)
    3 trait ordered(otype T) {
     3trait ordered(T) {
    44    int ?<?(T, T), ?<=?(T, T);
    55};
     
    77// A subrange otype resembling an Ada subotype with a base otype and a range
    88// constraint.
    9 otype subrange(otype base_t | ordered(base_t), base_t low = 0, base_t high = 8) = base_t;
     9otype subrange(base_t | ordered(base_t), base_t low = 0, base_t high = 8) = base_t;
    1010
    1111// Note that subrange() can be applied to floating-point and pointer otypes, not
     
    2828
    2929// Convenient access to subrange bounds, for instance for iteration:
    30 forall (otype T, T low, T high)
     30forall (T, T low, T high)
    3131T lbound( subrange(T, low, high) v) {
    3232    return low;
    3333}
    3434
    35 forall (otype T, T low, T high)
     35forall (T, T low, T high)
    3636T hbound( subrange(T, low, high) v) {
    3737    return high;
     
    4444// of exception handling here.  Inlining allows the compiler to eliminate
    4545// bounds checks.
    46 forall (otype T | ordered(T), T low, T high)
     46forall (T | ordered(T), T low, T high)
    4747inline subrange(T, low, high) ?=?(subrange(T, low, high)* target, T source) {
    4848    if (low <= source && source <= high) *((T*)target) = source;
     
    5454// compares range bounds so that the compiler can optimize checks away when the
    5555// ranges are known to overlap.
    56 forall (otype T | ordered(T), T t_low, T t_high, T s_low, T s_high)
     56forall (T | ordered(T), T t_low, T t_high, T s_low, T s_high)
    5757inline subrange(T, t_low, t_high) ?=?(subrange(T, t_low, t_high)* target,
    5858                                      subrange(T, s_low, s_high) source) {
  • tests/zombies/twice.c

    r342af53 r8e4aa05  
    1616#include <fstream.hfa>
    1717
    18 forall( otype T | { T ?+?( T, T ); } )
     18forall( T | { T ?+?( T, T ); } )
    1919T twice( const T t ) {
    2020        return t + t;
  • tests/zombies/typeGenerator.cfa

    r342af53 r8e4aa05  
    1 context addable( otype T ) {
     1context addable( T ) {
    22        T ?+?( T,T );
    33        T ?=?( T*, T);
    44};
    55
    6 otype List1( otype T | addable( T ) ) = struct { T data; List1( T ) *next; } *;
     6otype List1( T | addable( T ) ) = struct { T data; List1( T ) *next; } *;
    77typedef List1( int ) ListOfIntegers;
    88//List1( int ) li;
     
    1111[int] h( * List1( int ) p );                                                    // new declaration syntax
    1212
    13 struct( otype T ) S2 { T i; };                                                  // actual definition
     13struct( T ) S2 { T i; };                                                        // actual definition
    1414struct( int ) S3 v1, *p;                                                                // expansion and instantiation
    15 struct( otype T )( int ) S24 { T i; } v2;                               // actual definition, expansion and instantiation
    16 struct( otype T )( int ) { T i; } v2;                                   // anonymous actual definition, expansion and instantiation
     15struct( T )( int ) S24 { T i; } v2;                             // actual definition, expansion and instantiation
     16struct( T )( int ) { T i; } v2;                                 // anonymous actual definition, expansion and instantiation
    1717
    18 struct( otype T | addable( T ) ) node { T data; struct( T ) node *next; };
    19 otype List( otype T ) = struct( T ) node *;
     18struct( T | addable( T ) ) node { T data; struct( T ) node *next; };
     19otype List( T ) = struct( T ) node *;
    2020List( int ) my_list;
    2121
  • tests/zombies/withStatement.cfa

    r342af53 r8e4aa05  
    5454}
    5555
    56 forall( otype T )
     56forall( T )
    5757struct Box {
    5858        T x;
    5959};
    6060
    61 forall( otype T )
     61forall( T )
    6262void ?{}( Box(T) & this ) with( this ) { // with clause in polymorphic function
    6363        x{};
     
    6666void print( int i ) { sout | i; }
    6767
    68 forall( otype T | { void print( T ); })
     68forall( T | { void print( T ); })
    6969void foo( T t ) {
    7070        Box( T ) b = { t };
  • tests/zombies/wrapper/src/pointer.h

    r342af53 r8e4aa05  
    88// type safe malloc / free
    99
    10 forall(otype T)
     10forall(T)
    1111T* new()
    1212{
     
    1616}
    1717
    18 forall(otype T)
     18forall(T)
    1919void delete(T* p)
    2020{
  • tools/prettyprinter/Makefile.am

    r342af53 r8e4aa05  
    1111## Created On       : Wed Jun 28 12:07:10 2017
    1212## Last Modified By : Peter A. Buhr
    13 ## Last Modified On : Mon Apr 16 09:43:23 2018
    14 ## Update Count     : 20
     13## Last Modified On : Thu Jan 28 08:48:22 2021
     14## Update Count     : 23
    1515###############################################################################
    1616
     
    2020BUILT_SOURCES = parser.hh
    2121
    22 AM_YFLAGS = -d -t -v
     22AM_YFLAGS = -d -t -v -Wno-yacc
    2323
    2424SRC = lex.ll \
     
    3434pretty_CXXFLAGS = -Wno-deprecated -Wall -DYY_NO_INPUT -O2 -g -std=c++14
    3535
    36 MAINTAINERCLEANFILES = parser.output
     36MOSTLYCLEANFILES = parser.output
  • tools/prettyprinter/ParserTypes.h

    r342af53 r8e4aa05  
    1313// Created On       : Sun Dec 16 15:00:49 2001
    1414// Last Modified By : Peter A. Buhr
    15 // Last Modified On : Sat Jul 22 10:13:09 2017
    16 // Update Count     : 175
     15// Last Modified On : Tue Jan 26 23:05:34 2021
     16// Update Count     : 176
    1717//
    1818
    1919#pragma once
    2020
    21 int yylex();
     21extern "C" int yylex();
    2222
    2323#include <string>
  • tools/prettyprinter/parser.yy

    r342af53 r8e4aa05  
    1010// Created On       : Sat Dec 15 13:44:21 2001
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Apr 15 21:40:30 2018
    13 // Update Count     : 1052
     12// Last Modified On : Tue Jan 26 22:50:03 2021
     13// Update Count     : 1053
    1414//
    1515
     
    1717#define YYDEBUG_LEXER_TEXT( yylval )                                    // lexer loads this up each time
    1818#define YYDEBUG 1                                                                               // get the pretty debugging code to compile
     19#define YYERROR_VERBOSE                                                                 // more information in syntax errors
    1920
    2021#include <iostream>
Note: See TracChangeset for help on using the changeset viewer.