Changeset 565acf59
- Timestamp:
- Feb 12, 2021, 12:27:38 PM (5 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- eb24cec0
- Parents:
- da3963a (diff), 52f6250 (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. - Files:
-
- 6 added
- 30 edited
Legend:
- Unmodified
- Added
- Removed
-
benchmark/io/http/http_ring.cpp
rda3963a r565acf59 9 9 #include <liburing.h> 10 10 11 typedef enum { 12 EVENT_END, 13 EVENT_ACCEPT, 14 EVENT_REQUEST, 15 EVENT_ANSWER 16 } event_t; 17 18 struct __attribute__((aligned(128))) request_t { 19 event_t type; 20 int fd; 21 size_t length; 22 char * buff; 23 char data[0]; 24 25 static struct request_t * create(event_t type, size_t extra) { 26 auto ret = (struct request_t *)malloc(sizeof(struct request_t) + extra); 27 ret->type = type; 28 ret->length = extra; 29 ret->buff = ret->data; 30 return ret; 31 } 32 33 static struct request_t * create(event_t type) { 34 return create(type, 0); 35 } 36 }; 37 11 // #define NOBATCHING 12 // #define USE_ASYNC 13 14 // Options passed to each threads 38 15 struct __attribute__((aligned(128))) options_t { 16 // Data passed to accept 39 17 struct { 40 18 int sockfd; … … 44 22 } acpt; 45 23 24 // Termination notification 46 25 int endfd; 26 27 // The ring to use for io 47 28 struct io_uring * ring; 48 29 }; 30 31 //========================================================= 32 // General statistics 33 struct __attribute__((aligned(128))) stats_block_t { 49 34 struct { 50 size_t subs = 0; 51 size_t cnts = 0; 52 } result; 35 volatile size_t conns = 0; 36 volatile size_t reads = 0; 37 volatile size_t writes = 0; 38 volatile size_t full_writes = 0; 39 } completions; 40 41 struct { 42 volatile size_t conns = 0; 43 struct { 44 volatile size_t pipes = 0; 45 volatile size_t reset = 0; 46 volatile size_t other = 0; 47 } requests; 48 49 struct { 50 volatile size_t pipes = 0; 51 volatile size_t reset = 0; 52 volatile size_t other = 0; 53 } answers; 54 } errors; 55 56 struct { 57 volatile size_t current = 0; 58 volatile size_t max = 0; 59 volatile size_t used = 0; 60 } conns; 61 62 volatile size_t recycle_errors = 0; 53 63 }; 54 64 65 // Each thread gets its own block of stats 66 // and there is a global block for tallying at the end 67 thread_local stats_block_t stats; 68 stats_block_t global_stats; 69 70 // Get an array of current connections 71 // This is just for debugging, to make sure 72 // no two state-machines get the same fd 73 const size_t array_max = 25000; 74 class connection * volatile conns[array_max] = { 0 }; 75 76 // Max fd we've seen, keep track so it's convenient to adjust the array size after 77 volatile int max_fd = 0; 78 55 79 //========================================================= 80 // Some small wrappers for ring operations used outside the connection state machine 81 // get sqe + error handling 56 82 static struct io_uring_sqe * get_sqe(struct io_uring * ring) { 57 83 struct io_uring_sqe * sqe = io_uring_get_sqe(ring); … … 63 89 } 64 90 65 static void submit(struct io_uring * ) { 66 // io_uring_submit(ring); 67 } 68 69 //========================================================= 91 // read of the event fd is not done by a connection 92 // use nullptr as the user data 70 93 static void ring_end(struct io_uring * ring, int fd, char * buffer, size_t len) { 71 94 struct io_uring_sqe * sqe = get_sqe(ring); 72 95 io_uring_prep_read(sqe, fd, buffer, len, 0); 73 io_uring_sqe_set_data(sqe, request_t::create(EVENT_END));74 submit(ring);96 io_uring_sqe_set_data(sqe, nullptr); 97 io_uring_submit(ring); 75 98 } 76 99 77 static void ring_accept(struct io_uring * ring, int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) {78 auto req = request_t::create(EVENT_ACCEPT);79 struct io_uring_sqe * sqe = get_sqe(ring);80 io_uring_prep_accept(sqe, sockfd, addr, addrlen, flags);81 io_uring_sqe_set_data(sqe, req);82 submit(ring);83 // std::cout << "Submitted accept: " << req << std::endl;84 }85 86 static void ring_request(struct io_uring * ring, int fd) {87 size_t size = 1024;88 auto req = request_t::create(EVENT_REQUEST, size);89 req->fd = fd;90 91 struct io_uring_sqe * sqe = get_sqe(ring);92 io_uring_prep_read(sqe, fd, req->buff, size, 0);93 io_uring_sqe_set_data(sqe, req);94 submit(ring);95 // std::cout << "Submitted request: " << req << " (" << (void*)req->buffer << ")"<<std::endl;96 }97 98 100 //========================================================= 101 // All answers are fixed and determined by the return code 99 102 enum HttpCode { 100 103 OK200 = 0, … … 108 111 }; 109 112 113 // Get a fix reply based on the return code 110 114 const char * http_msgs[] = { 111 "HTTP/1.1 200 OK\ nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: %zu \n\n%s",112 "HTTP/1.1 400 Bad Request\ nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",113 "HTTP/1.1 404 Not Found\ nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",114 "HTTP/1.1 405 Method Not Allowed\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",115 "HTTP/1.1 408 Request Timeout\ nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",116 "HTTP/1.1 413 Payload Too Large\ nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",117 "HTTP/1.1 414 URI Too Long\ nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",115 "HTTP/1.1 200 OK\r\nServer: HttoForall\r\nContent-Type: text/plain\r\nContent-Length: 15\r\nConnection: keep-alive\r\n\r\nHello, World!\r\n", 116 "HTTP/1.1 400 Bad Request\r\nServer: HttoForall\r\nContent-Type: text/plain\r\nContent-Length: 0 \r\n\r\n", 117 "HTTP/1.1 404 Not Found\r\nServer: HttoForall\r\nContent-Type: text/plain\r\nContent-Length: 0 \r\n\r\n", 118 "HTTP/1.1 405 Method Not \r\nServer: HttoForall\r\nContent-Type: text/plain\r\nContent-Length: 0 \r\n\r\n", 119 "HTTP/1.1 408 Request Timeout\r\nServer: HttoForall\r\nContent-Type: text/plain\r\nContent-Length: 0 \r\n\r\n", 120 "HTTP/1.1 413 Payload Too Large\r\nServer: HttoForall\r\nContent-Type: text/plain\r\nContent-Length: 0 \r\n\r\n", 121 "HTTP/1.1 414 URI Too Long\r\nServer: HttoForall\r\nContent-Type: text/plain\r\nContent-Length: 0 \r\n\r\n", 118 122 }; 119 120 static_assert( KNOWN_CODES == (sizeof(http_msgs ) / sizeof(http_msgs [0]))); 121 122 const int http_codes[] = {123 200,124 400,125 404,126 405,127 408,128 413,129 414,123 static_assert( KNOWN_CODES == (sizeof(http_msgs) / sizeof(http_msgs[0])) ); 124 125 // Pre-compute the length of these replys 126 const size_t http_lens[] = { 127 strlen(http_msgs[0]), 128 strlen(http_msgs[1]), 129 strlen(http_msgs[2]), 130 strlen(http_msgs[3]), 131 strlen(http_msgs[4]), 132 strlen(http_msgs[5]), 133 strlen(http_msgs[6]), 130 134 }; 131 132 static_assert( KNOWN_CODES == (sizeof(http_codes) / sizeof(http_codes[0]))); 133 134 int code_val(HttpCode code) { 135 return http_codes[code]; 136 } 137 138 static void ring_answer(struct io_uring * ring, int fd, HttpCode code) { 139 size_t size = 256; 140 auto req = request_t::create(EVENT_ANSWER, size); 141 req->fd = fd; 142 143 const char * fmt = http_msgs[code]; 144 const char * date = ""; 145 size = snprintf(req->buff, size, fmt, date, size); 146 147 struct io_uring_sqe * sqe = get_sqe(ring); 148 io_uring_prep_write(sqe, fd, req->buff, size, 0); 149 io_uring_sqe_set_data(sqe, req); 150 submit(ring); 151 // std::cout << "Submitted good answer: " << req << " (" << (void*)req->buffer << ")"<<std::endl; 152 } 153 154 static void ring_answer(struct io_uring * ring, int fd, const std::string &) { 155 // size_t size = 256; 156 // auto req = request_t::create(EVENT_ANSWER, size); 157 // req->fd = fd; 158 159 // const char * fmt = http_msgs[OK200]; 160 // const char * date = ""; 161 // size_t len = snprintf(req->buffer, size, fmt, date, ans.size(), ans.c_str()); 162 // req->length = len; 163 164 // struct io_uring_sqe * sqe = get_sqe(ring); 165 // io_uring_prep_write(sqe, fd, req->buffer, len, 0); 166 // io_uring_sqe_set_data(sqe, req); 167 // submit(ring); 168 // std::cout << "Submitted good answer: " << req << " (" << (void*)req->buffer << ")"<<std::endl; 169 170 171 static const char* RESPONSE = "HTTP/1.1 200 OK\r\n" \ 172 "Content-Length: 15\r\n" \ 173 "Content-Type: text/html\r\n" \ 174 "Connection: keep-alive\r\n" \ 175 "Server: testserver\r\n" \ 176 "\r\n" \ 177 "Hello, World!\r\n"; 178 179 static const size_t RLEN = strlen(RESPONSE); 180 181 size_t size = 256; 182 auto req = request_t::create(EVENT_ANSWER, size); 183 req->fd = fd; 184 req->buff = (char*)RESPONSE; 185 req->length = RLEN; 186 187 // const char * fmt = http_msgs[OK200]; 188 // const char * date = ""; 189 // size_t len = snprintf(req->buffer, size, fmt, date, ans.size(), ans.c_str()); 190 // req->length = len; 191 192 struct io_uring_sqe * sqe = get_sqe(ring); 193 io_uring_prep_write(sqe, fd, RESPONSE, RLEN, 0); 194 io_uring_sqe_set_data(sqe, req); 195 submit(ring); 196 } 135 static_assert( KNOWN_CODES == (sizeof(http_lens) / sizeof(http_lens[0])) ); 197 136 198 137 //========================================================= 199 static void handle_new_conn(struct io_uring * ring, int fd) { 200 if( fd < 0 ) { 201 int err = -fd; 202 if( err == ECONNABORTED ) return; 203 std::cerr << "accept error: (" << errno << ") " << strerror(errno) << std::endl; 204 exit(EXIT_FAILURE); 205 } 206 207 ring_request(ring, fd); 208 } 209 210 static void handle_request(struct io_uring * ring, struct request_t * in, int res) { 211 if( res < 0 ) { 212 int err = -res; 213 switch(err) { 214 case EPIPE: 215 case ECONNRESET: 216 close(in->fd); 217 free(in); 138 // Finate state machine responsible for handling each connection 139 class __attribute__((aligned(128))) connection { 140 private: 141 // The state of the machine 142 enum { 143 ACCEPTING, // Accept sent waiting for connection 144 REQUESTING, // Waiting for new request 145 ANSWERING, // Either request received submitting answer or short answer sent, need to submit rest 146 } state; 147 148 // The file descriptor of the connection 149 int fd; 150 151 // request data 152 static const size_t buffer_size = 1024; // Size of the read buffer 153 const char * buffer; // Buffer into which requests are read 154 155 // send data 156 size_t to_send; // Data left to send 157 const char * iterator; // Pointer to rest of the message to send 158 159 // stats 160 // how many requests/answers were complete, that is, a valid cqe was obtained 161 struct { 162 size_t requests = 0; 163 size_t answers = 0; 164 } stats; 165 166 private: 167 connection() 168 : state(ACCEPTING) 169 , fd(0) 170 , buffer( new char[buffer_size]) 171 , iterator(nullptr) 172 {} 173 174 ~connection() { 175 delete [] buffer; 176 ::stats.conns.current--; 177 } 178 179 // Close the current connection 180 void close(int err) { 181 // std::cout << "(" << this->stats.requests << "," << this->stats.answers << ", e" << err << ") "; 182 conns[fd] = nullptr; 183 184 if(fd != 0) { 185 ::close(fd); 186 } 187 delete this; 188 } 189 190 //-------------------------------------------------- 191 // Wrappers for submit so we can tweak it more easily 192 static void submit(struct io_uring * ring, struct io_uring_sqe * sqe, connection * conn) { 193 (void)ring; 194 #ifdef USE_ASYNC 195 io_uring_sqe_set_flags(sqe, IOSQE_ASYNC); 196 #endif 197 io_uring_sqe_set_data(sqe, conn); 198 #ifdef NOBATCHING 199 io_uring_submit(ring); 200 #endif 201 } 202 203 void submit(struct io_uring * ring, struct io_uring_sqe * sqe) { 204 submit(ring, sqe, this); 205 } 206 207 //-------------------------------------------------- 208 // get a new request from the client 209 void request(struct io_uring * ring) { 210 state = REQUESTING; 211 struct io_uring_sqe * sqe = get_sqe(ring); 212 io_uring_prep_recv(sqe, fd, (void*)buffer, buffer_size, 0); 213 submit(ring, sqe); 214 } 215 216 //-------------------------------------------------- 217 // Send a new answer based on a return code 218 void answer(struct io_uring * ring, HttpCode code) { 219 iterator = http_msgs[code]; 220 to_send = http_lens[code]; 221 if(to_send != 124) { 222 std::cerr << "Answer has weird size: " << to_send << " (" << (int)code << ")" << std::endl; 223 } 224 answer(ring); 225 } 226 227 // send a new answer to the client 228 // Reused for incomplete writes 229 void answer(struct io_uring * ring) { 230 state = ANSWERING; 231 struct io_uring_sqe * sqe = get_sqe(ring); 232 io_uring_prep_send(sqe, fd, iterator, to_send, 0); 233 submit(ring, sqe); 234 } 235 236 //-------------------------------------------------- 237 // Handle a new connection, results for getting an cqe while in the ACCEPTING state 238 void newconn(struct io_uring * ring, int ret) { 239 // Check errors 240 if( ret < 0 ) { 241 int err = -ret; 242 if( err == ECONNABORTED ) { 243 ::stats.errors.conns++; 244 this->close(err); 218 245 return; 219 default: 220 std::cerr << "request error: (" << err << ") " << strerror(err) << std::endl; 221 exit(EXIT_FAILURE); 222 } 223 } 224 225 if(res == 0) { 226 close(in->fd); 227 free(in); 228 return; 229 } 230 231 const char * it = in->buff; 232 if( !strstr( it, "\r\n\r\n" ) ) { 233 std::cout << "Incomplete request" << std::endl; 234 close(in->fd); 235 free(in); 236 return; 237 } 238 239 it = in->buff; 240 const std::string reply = "Hello, World!\n"; 241 int ret = memcmp(it, "GET ", 4); 242 if( ret != 0 ) { 243 ring_answer(ring, in->fd, E400); 244 goto NEXT; 245 } 246 247 it += 4; 248 ret = memcmp(it, "/plaintext", 10); 249 if( ret != 0 ) { 250 ring_answer(ring, in->fd, E404); 251 goto NEXT; 252 } 253 254 ring_answer(ring, in->fd, reply); 255 256 NEXT: 257 ring_request(ring, in->fd); 258 return; 259 } 260 261 static void handle_answer(struct io_uring * ring, struct request_t * in, int res) { 262 if( res < 0 ) { 263 int err = -res; 264 switch(err) { 265 case EPIPE: 266 case ECONNRESET: 267 close(in->fd); 268 free(in); 269 return; 270 default: 271 std::cerr << "answer error: (" << err << ") " << strerror(err) << std::endl; 272 exit(EXIT_FAILURE); 273 } 274 } 275 276 if( res >= in->length ) { 277 free(in); 278 return; 279 } 280 281 struct io_uring_sqe * sqe = get_sqe(ring); 282 io_uring_prep_write(sqe, in->fd, in->buff + res, in->length - res, 0); 283 io_uring_sqe_set_data(sqe, in); 284 submit(ring); 285 // std::cout << "Re-Submitted request: " << in << " (" << (void*)in->buffer << ")"<<std::endl; 286 287 ring_request(ring, in->fd); 288 } 246 } 247 std::cerr << "accept error: (" << errno << ") " << strerror(errno) << std::endl; 248 exit(EXIT_FAILURE); 249 } 250 251 // Count the connections 252 ::stats.completions.conns++; 253 ::stats.conns.current++; 254 if(::stats.conns.current > ::stats.conns.max) { 255 ::stats.conns.max = ::stats.conns.current; 256 } 257 258 // Read on the data 259 fd = ret; 260 request(ring); 261 262 // check the max fd so we know if we exceeded the array 263 for(;;) { 264 int expected = max_fd; 265 if(expected >= fd) return; 266 if( __atomic_compare_exchange_n(&max_fd, &expected, fd, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ) return; 267 } 268 269 // check if we have enough space to fit inside the array 270 if(fd >= array_max) { 271 std::cerr << "accept error: fd " << fd << " is too high" << std::endl; 272 return; 273 } 274 275 // Put our connection into the global array 276 // No one else should be using it so if they are that's a bug 277 auto exist = __atomic_exchange_n( &conns[fd], this, __ATOMIC_SEQ_CST); 278 if( exist ) { 279 size_t first = __atomic_fetch_add(&global_stats.recycle_errors, 1, __ATOMIC_SEQ_CST); 280 if( first == 0 ) { 281 std::cerr << "First: accept has existing connection " << std::endl; 282 } 283 } 284 } 285 286 // Handle a new request, results for getting an cqe while in the REQUESTING state 287 void newrequest(struct io_uring * ring, int res) { 288 // Check errors 289 if( res < 0 ) { 290 int err = -res; 291 switch(err) { 292 case EPIPE: 293 ::stats.errors.requests.pipes++; 294 break; 295 // Don't fall through the get better stats 296 case ECONNRESET: 297 ::stats.errors.requests.reset++; 298 break; 299 default: 300 ::stats.errors.requests.other++; 301 std::cerr << "request error: (" << err << ") " << strerror(err) << std::endl; 302 exit(EXIT_FAILURE); 303 } 304 305 // Connection failed, close it 306 this->close(err); 307 return; 308 } 309 310 // Update stats 311 ::stats.completions.reads++; 312 313 // Is this an EOF 314 if(res == 0) { 315 // Yes, close the connection 316 this->close(0); 317 return; 318 } 319 320 // Find the end of the request header 321 const char * it = buffer; 322 if( !strstr( it, "\r\n\r\n" ) ) { 323 // This state machine doesn't support incomplete reads 324 // Print them to output so it's clear there is an issue 325 std::cout << "Incomplete request" << std::endl; 326 this->close(EBADR); 327 return; 328 } 329 330 // Find the method to use 331 it = buffer; 332 int ret = memcmp(it, "GET ", 4); 333 if( ret != 0 ) { 334 // We only support get, answer with an error 335 answer(ring, E400); 336 return; 337 } 338 339 // Find the target 340 it += 4; 341 ret = memcmp(it, "/plaintext", 10); 342 if( ret != 0 ) { 343 // We only support /plaintext, answer with an error 344 answer(ring, E404); 345 return; 346 } 347 348 // Correct request, answer with the payload 349 this->stats.requests++; 350 answer(ring, OK200); 351 } 352 353 // Handle a partial or full answer sent, results for getting an cqe while in the ANSWERING state 354 void writedone(struct io_uring * ring, int res) { 355 // Check errors 356 if( res < 0 ) { 357 int err = -res; 358 switch(err) { 359 case EPIPE: 360 ::stats.errors.answers.pipes++; 361 break; 362 // Don't fall through the get better stats 363 case ECONNRESET: 364 ::stats.errors.answers.reset++; 365 break; 366 default: 367 ::stats.errors.answers.other++; 368 std::cerr << "answer error: (" << err << ") " << strerror(err) << std::endl; 369 exit(EXIT_FAILURE); 370 } 371 372 this->close(err); 373 return; 374 } 375 376 // Update stats 377 ::stats.completions.writes++; 378 if(res == 124) ::stats.completions.full_writes++; 379 380 // Is this write completed 381 if( res == to_send ) { 382 // Yes, more stats 383 this->stats.answers++; 384 if(this->stats.answers == 1) ::stats.conns.used++; 385 // Then read a new request 386 request(ring); 387 return; 388 } 389 390 // Not a completed read, push the rest 391 to_send -= res; 392 iterator += res; 393 answer(ring); 394 } 395 public: 396 // Submit a call to accept and create a new connection object 397 static void accept(struct io_uring * ring, const struct options_t & opt) { 398 struct io_uring_sqe * sqe = get_sqe(ring); 399 io_uring_prep_accept(sqe, opt.acpt.sockfd, opt.acpt.addr, opt.acpt.addrlen, opt.acpt.flags); 400 submit(ring, sqe, new connection()); 401 // std::cout << "Submitted accept: " << req << std::endl; 402 } 403 404 // Handle a new cqe 405 void handle(struct io_uring * ring, int res, const struct options_t & opt) { 406 switch(state) { 407 case ACCEPTING: 408 connection::accept(ring, opt); 409 newconn(ring, res); 410 break; 411 case REQUESTING: 412 newrequest(ring, res); 413 break; 414 case ANSWERING: 415 writedone(ring, res); 416 break; 417 } 418 } 419 }; 289 420 290 421 //========================================================= 291 extern "C" { 292 extern int __io_uring_flush_sq(struct io_uring *ring); 293 } 294 422 // Main loop of the WebServer 423 // Effectively uses one thread_local copy of everything per kernel thread 295 424 void * proc_loop(void * arg) { 296 size_t count = 0;425 // Get the thread local argument 297 426 struct options_t & opt = *(struct options_t *)arg; 298 299 427 struct io_uring * ring = opt.ring; 300 428 429 // Track the shutdown using a event_fd 301 430 char endfd_buf[8]; 302 431 ring_end(ring, opt.endfd, endfd_buf, 8); 303 432 304 ring_accept(ring, opt.acpt.sockfd, opt.acpt.addr, opt.acpt.addrlen, opt.acpt.flags); 305 306 bool done = false; 433 // Accept our first connection 434 // May not take effect until io_uring_submit_and_wait 435 connection::accept(ring, opt); 436 437 int reset = 1; // Counter to print stats once in a while 438 bool done = false; // Are we done 439 size_t sqes = 0; // Number of sqes we submitted 440 size_t call = 0; // Number of submits we made 307 441 while(!done) { 308 struct io_uring_cqe *cqe; 309 int ret; 310 while(-EAGAIN == (ret = io_uring_wait_cqe_nr(ring, &cqe, 0))) { 311 ret = io_uring_submit_and_wait(ring, 1); 312 if (ret < 0) { 313 fprintf( stderr, "io_uring get error: (%d) %s\n", (int)-ret, strerror(-ret) ); 314 exit(EXIT_FAILURE); 315 } 316 opt.result.subs += ret; 317 opt.result.cnts++; 318 } 319 320 if (ret < 0 && -EAGAIN != ret) { 321 fprintf( stderr, "io_uring peek error: (%d) %s\n", (int)-ret, strerror(-ret) ); 442 // Submit all the answers we have and wait for responses 443 int ret = io_uring_submit_and_wait(ring, 1); 444 445 // check errors 446 if (ret < 0) { 447 fprintf( stderr, "io_uring S&W error: (%d) %s\n", (int)-ret, strerror(-ret) ); 322 448 exit(EXIT_FAILURE); 323 449 } 324 450 325 auto req = (struct request_t *)cqe->user_data; 326 // std::cout << req << " completed with " << cqe->res << std::endl; 327 328 switch(req->type) { 329 case EVENT_END: 451 // Check how good we are at batching sqes 452 sqes += ret; 453 call++; 454 455 struct io_uring_cqe *cqe; 456 unsigned head; 457 unsigned count = 0; 458 459 // go through all cqes 460 io_uring_for_each_cqe(ring, head, cqe) { 461 if (0 == cqe->user_data) { 330 462 done = true; 331 463 break; 332 case EVENT_ACCEPT: 333 handle_new_conn(ring, cqe->res); 334 free(req); 335 ring_accept(ring, opt.acpt.sockfd, opt.acpt.addr, opt.acpt.addrlen, opt.acpt.flags); 336 break; 337 case EVENT_REQUEST: 338 handle_request(ring, req, cqe->res); 339 break; 340 case EVENT_ANSWER: 341 handle_answer(ring, req, cqe->res); 342 break; 343 } 344 345 io_uring_cqe_seen(ring, cqe); 346 } 347 348 return (void*)count; 464 } 465 466 auto req = (class connection *)cqe->user_data; 467 req->handle( ring, cqe->res, opt ); 468 469 // Every now and then, print some stats 470 reset--; 471 if(reset == 0) { 472 std::cout << "Submit average: " << sqes << "/" << call << "(" << (((double)sqes) / call) << ")" << std::endl; 473 // Reset to some random number of completions 474 // use the ring_fd in the number of threads don't all print at once 475 reset = 100000 + (100000 * (ring->ring_fd % 5)); 476 } 477 478 // Keep track of how many cqes we have seen 479 count++; 480 } 481 482 // Mark the cqes as seen 483 io_uring_cq_advance(ring, count); 484 } 485 486 // Tally all the thread local statistics 487 __atomic_fetch_add( &global_stats.completions.conns, ::stats.completions.conns, __ATOMIC_SEQ_CST ); 488 __atomic_fetch_add( &global_stats.completions.reads, ::stats.completions.reads, __ATOMIC_SEQ_CST ); 489 __atomic_fetch_add( &global_stats.completions.writes, ::stats.completions.writes, __ATOMIC_SEQ_CST ); 490 __atomic_fetch_add( &global_stats.completions.full_writes, ::stats.completions.full_writes, __ATOMIC_SEQ_CST ); 491 __atomic_fetch_add( &global_stats.errors.conns, ::stats.errors.conns, __ATOMIC_SEQ_CST ); 492 __atomic_fetch_add( &global_stats.errors.requests.pipes, ::stats.errors.requests.pipes, __ATOMIC_SEQ_CST ); 493 __atomic_fetch_add( &global_stats.errors.requests.reset, ::stats.errors.requests.reset, __ATOMIC_SEQ_CST ); 494 __atomic_fetch_add( &global_stats.errors.requests.other, ::stats.errors.requests.other, __ATOMIC_SEQ_CST ); 495 __atomic_fetch_add( &global_stats.errors.answers.pipes, ::stats.errors.answers.pipes, __ATOMIC_SEQ_CST ); 496 __atomic_fetch_add( &global_stats.errors.answers.reset, ::stats.errors.answers.reset, __ATOMIC_SEQ_CST ); 497 __atomic_fetch_add( &global_stats.errors.answers.other, ::stats.errors.answers.other, __ATOMIC_SEQ_CST ); 498 __atomic_fetch_add( &global_stats.conns.current, ::stats.conns.current, __ATOMIC_SEQ_CST ); 499 __atomic_fetch_add( &global_stats.conns.max, ::stats.conns.max, __ATOMIC_SEQ_CST ); 500 __atomic_fetch_add( &global_stats.conns.used, ::stats.conns.used, __ATOMIC_SEQ_CST ); 501 502 return nullptr; 349 503 } 350 504 351 505 //========================================================= 352 struct __attribute__((aligned(128))) aligned_ring { 353 struct io_uring storage; 354 }; 355 356 #include <bit> 357 358 #include <pthread.h> 506 #include <bit> // for ispow2 507 359 508 extern "C" { 360 #include <signal.h> 361 #include <sys/eventfd.h> 362 #include <sys/socket.h> 363 #include <netinet/in.h> 509 #include <pthread.h> // for pthreads 510 #include <signal.h> // for signal(SIGPIPE, SIG_IGN); 511 #include <sys/eventfd.h> // use for termination 512 #include <sys/socket.h> // for sockets in general 513 #include <netinet/in.h> // for sockaddr_in, AF_INET 364 514 } 365 515 366 516 int main(int argc, char * argv[]) { 517 // Initialize the array of connection-fd associations 518 for(int i = 0; i < array_max; i++) { 519 conns[i] = nullptr; 520 } 521 522 // Make sure we ignore all sigpipes 367 523 signal(SIGPIPE, SIG_IGN); 368 524 369 unsigned nthreads = 1; 370 unsigned port = 8800; 371 unsigned entries = 256; 372 unsigned backlog = 10; 373 bool attach = false; 525 // Default command line arguments 526 unsigned nthreads = 1; // number of kernel threads 527 unsigned port = 8800; // which port to listen on 528 unsigned entries = 256; // number of entries per ring/kernel thread 529 unsigned backlog = 262144; // backlog argument to listen 530 bool attach = false; // Whether or not to attach all the rings 531 bool sqpoll = false; // Whether or not to use SQ Polling 374 532 375 533 //=================== 376 // Arguments 534 // Arguments Parsing 377 535 int c; 378 while ((c = getopt (argc, argv, "t:p:e:b:a ")) != -1) {536 while ((c = getopt (argc, argv, "t:p:e:b:aS")) != -1) { 379 537 switch (c) 380 538 { … … 394 552 attach = true; 395 553 break; 554 case 'S': 555 sqpoll = true; 556 break; 396 557 case '?': 397 558 default: 398 std::cerr << "Usage: -t <threads> -p <port> -e <entries> -b <backlog> -a " << std::endl;559 std::cerr << "Usage: -t <threads> -p <port> -e <entries> -b <backlog> -aS" << std::endl; 399 560 return EXIT_FAILURE; 400 561 } … … 416 577 //=================== 417 578 // End FD 579 // Create a single event fd to notify the kernel threads when the server shutsdown 418 580 int efd = eventfd(0, EFD_SEMAPHORE); 419 581 if (efd < 0) { … … 424 586 //=================== 425 587 // Open Socket 588 // Listen on specified port 426 589 std::cout << getpid() << " : Listening on port " << port << std::endl; 427 590 int server_fd = socket(AF_INET, SOCK_STREAM, 0); … … 439 602 address.sin_port = htons( port ); 440 603 604 // In case the port is already in use, don't just return an error 605 // Linux is very slow at reclaiming port so just retry regularly 441 606 int waited = 0; 442 607 while(true) { … … 444 609 if(ret < 0) { 445 610 if(errno == EADDRINUSE) { 611 // Port is in used let's retry later 446 612 if(waited == 0) { 447 613 std::cerr << "Waiting for port" << std::endl; 448 614 } else { 615 // To be cure, print how long we have been waiting 449 616 std::cerr << "\r" << waited; 450 617 std::cerr.flush(); 451 618 } 452 619 waited ++; 453 usleep( 1000000 ); 620 usleep( 1000000 ); // Wait and retry 454 621 continue; 455 622 } 623 // Some other error occured, this is a real error 456 624 std::cerr << "bind error: (" << errno << ") " << strerror(errno) << std::endl; 457 625 exit(EXIT_FAILURE); … … 474 642 std::cout << std::endl; 475 643 644 // Create the desired number of kernel-threads and for each 645 // create a ring. Create the rings in the main so we can attach them 646 // Since the rings are all in a dense VLA, aligned them so we don't get false sharing 647 // it's unlikely but better safe than sorry 648 struct __attribute__((aligned(128))) aligned_ring { 649 struct io_uring storage; 650 }; 476 651 aligned_ring thrd_rings[nthreads]; 477 652 pthread_t thrd_hdls[nthreads]; 478 653 options_t thrd_opts[nthreads]; 654 bool no_drops = true; 655 bool fast_poll = true; 656 bool nfix_sqpl = true; 479 657 for(unsigned i = 0; i < nthreads; i++) { 480 if(!attach || i == 0) { 481 io_uring_queue_init(entries, &thrd_rings[i].storage, 0); 482 } 483 else { 484 struct io_uring_params p; 485 memset(&p, 0, sizeof(p)); 486 p.flags = IORING_SETUP_ATTACH_WQ; 658 struct io_uring_params p = { }; 659 660 if(sqpoll) { // If sqpoll is on, add the flag 661 p.flags |= IORING_SETUP_SQPOLL; 662 p.sq_thread_idle = 100; 663 } 664 665 if (attach && i != 0) { // If attach is on, add the flag, except for the first ring 666 p.flags |= IORING_SETUP_ATTACH_WQ; 487 667 p.wq_fd = thrd_rings[0].storage.ring_fd; 488 io_uring_queue_init_params(entries, &thrd_rings[i].storage, &p); 489 } 490 668 } 669 670 // Create the ring 671 io_uring_queue_init_params(entries, &thrd_rings[i].storage, &p); 672 673 // Check if some of the note-worthy features are there 674 if(0 == (p.features & IORING_FEAT_NODROP )) { no_drops = false; } 675 if(0 == (p.features & IORING_FEAT_FAST_POLL )) { fast_poll = false; } 676 if(0 == (p.features & IORING_FEAT_SQPOLL_NONFIXED)) { nfix_sqpl = false; } 677 678 // Write the socket options we want to the options we pass to the threads 491 679 thrd_opts[i].acpt.sockfd = server_fd; 492 680 thrd_opts[i].acpt.addr = (struct sockaddr *)&address; … … 502 690 } 503 691 } 692 693 // Tell the user if the features are present 694 if( no_drops ) std::cout << "No Drop Present" << std::endl; 695 if( fast_poll) std::cout << "Fast Poll Present" << std::endl; 696 if(!nfix_sqpl) std::cout << "Non-Fixed SQ Poll not Present" << std::endl; 504 697 505 698 //=================== … … 510 703 int ret; 511 704 do { 705 // Wait for a Ctrl-D to close the server 512 706 ret = read(STDIN_FILENO, buffer, 128); 513 707 if(ret < 0) { … … 526 720 527 721 //=================== 722 // Use eventfd_write to tell the threads we are closing 528 723 (std::cout << "Sending Shutdown to Threads... ").flush(); 529 724 ret = eventfd_write(efd, nthreads); … … 535 730 536 731 //=================== 732 // Join all the threads and close the rings 537 733 (std::cout << "Stopping Threads Done... ").flush(); 538 size_t total = 0;539 size_t count = 0;540 734 for(unsigned i = 0; i < nthreads; i++) { 541 735 void * retval; … … 545 739 exit(EXIT_FAILURE); 546 740 } 547 // total += (size_t)retval;548 total += thrd_opts[i].result.subs;549 count += thrd_opts[i].result.cnts;550 741 551 742 io_uring_queue_exit(thrd_opts[i].ring); 552 743 } 553 744 std::cout << "done" << std::endl; 554 std::cout << "Submit average: " << total << "/" << count << "(" << (((double)total) / count) << ")" << std::endl;555 745 556 746 //=================== 747 // Close the sockets 557 748 (std::cout << "Closing Socket... ").flush(); 558 749 ret = shutdown( server_fd, SHUT_RD ); … … 567 758 exit(EXIT_FAILURE); 568 759 } 569 std::cout << "done" << std::endl; 760 std::cout << "done" << std::endl << std::endl; 761 762 // Print stats and exit 763 std::cout << "Errors: " << global_stats.errors.conns << "c, (" << global_stats.errors.requests.pipes << "p, " << global_stats.errors.requests.reset << "r, " << global_stats.errors.requests.other << "o" << ")r, (" << global_stats.errors.answers.pipes << "p, " << global_stats.errors.answers.reset << "r, " << global_stats.errors.answers.other << "o" << ")a" << std::endl; 764 std::cout << "Completions: " << global_stats.completions.conns << "c, " << global_stats.completions.reads << "r, " << global_stats.completions.writes << "w" << std::endl; 765 std::cout << "Full Writes: " << global_stats.completions.full_writes << std::endl; 766 std::cout << "Max FD: " << max_fd << std::endl; 767 std::cout << "Successful connections: " << global_stats.conns.used << std::endl; 768 std::cout << "Max concurrent connections: " << global_stats.conns.max << std::endl; 769 std::cout << "Accepts on non-zeros: " << global_stats.recycle_errors << std::endl; 770 std::cout << "Leaked conn objects: " << global_stats.conns.current << std::endl; 570 771 } 772 773 // compile-command: "g++ http_ring.cpp -std=c++2a -pthread -luring -O3" // -
doc/LaTeXmacros/common.tex
rda3963a r565acf59 11 11 %% Created On : Sat Apr 9 10:06:17 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : Sat Jan 23 09:06:39202114 %% Update Count : 49113 %% Last Modified On : Mon Feb 8 21:45:41 2021 14 %% Update Count : 522 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 32 32 \setlist[enumerate]{listparindent=\parindent}% global 33 33 \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} 35 35 36 36 % Names used in the document. 37 37 38 38 \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 48 50 \newcommand{\Csharp}{C\raisebox{-0.7ex}{\Large$^\sharp$}\xspace} % C# symbolic name 49 51 50 52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 51 53 54 % remove special-character warning in PDF side-bar names 52 55 \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 53 70 % parindent is relative, i.e., toggled on/off in environments like itemize, so store the value for 54 71 % use rather than use \parident directly. … … 81 98 \vskip 50\p@ 82 99 }} 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}} 85 102 \renewcommand\subsubsection{\@startsection{subsubsection}{3}{\z@}{-2.5ex \@plus -1ex \@minus -.2ex}{1.0ex \@plus .2ex}{\normalfont\normalsize\bfseries}} 86 103 \renewcommand\paragraph{\@startsection{paragraph}{4}{\z@}{-2.0ex \@plus -1ex \@minus -.2ex}{-1em}{\normalfont\normalsize\bfseries}} … … 89 106 \newcommand{\italic}[1]{\emph{\hyperpage{#1}}} 90 107 \newcommand{\Definition}[1]{\textbf{\hyperpage{#1}}} 91 \newcommand{\see}[1]{ \emph{see}~#1}108 \newcommand{\see}[1]{(see #1)} 92 109 93 110 % Define some commands that produce formatted index entries suitable for cross-references. … … 235 252 \newcommand{\LstKeywordStyle}[1]{{\lst@basicstyle{\lst@keywordstyle{#1}}}} 236 253 \newcommand{\LstCommentStyle}[1]{{\lst@basicstyle{\lst@commentstyle{#1}}}} 254 \newcommand{\LstStringStyle}[1]{{\lst@basicstyle{\lst@stringstyle{#1}}}} 237 255 238 256 \newlength{\gcolumnposn} % temporary hack because lstlisting does not handle tabs correctly … … 260 278 xleftmargin=\parindentlnth, % indent code to paragraph indentation 261 279 extendedchars=true, % allow ASCII characters in the range 128-255 262 escapechar= §, % LaTeX escape in CFA code §...§ (section symbol), emacs: C-q M-'263 mathescape= true, % LaTeX math escape in CFA code $...$280 escapechar=\$, % LaTeX escape in CFA code §...§ (section symbol), emacs: C-q M-' 281 mathescape=false, % LaTeX math escape in CFA code $...$ 264 282 keepspaces=true, % 265 283 showstringspaces=false, % do not show spaces with cup 266 284 showlines=true, % show blank lines at end of code 267 285 aboveskip=4pt, % spacing above/below code block 268 belowskip= -2pt,286 belowskip=0pt, 269 287 numberstyle=\footnotesize\sf, % numbering style 270 288 % replace/adjust listing characters that look bad in sanserif … … 279 297 \lstset{ 280 298 language=CFA, 281 moredelim=**[is][\color{red}]{®}{®}, % red highlighting ®...® (registered trademark symbol) emacs: C-q M-. 282 moredelim=**[is][\color{blue}]{ß}{ß}, % blue highlighting ß...ß (sharp s symbol) emacs: C-q M-_ 283 moredelim=**[is][\color{OliveGreen}]{¢}{¢}, % green highlighting ¢...¢ (cent symbol) emacs: C-q M-" 284 moredelim=[is][\lstset{keywords={}}]{¶}{¶}, % keyword escape ¶...¶ (pilcrow symbol) emacs: C-q M-^ 285 % replace/adjust listing characters that look bad in sanserif 286 add to literate={`}{\ttfamily\upshape\hspace*{-0.1ex}`}1 299 moredelim=**[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-^ 287 304 }% lstset 288 305 \lstset{#1} -
doc/bibliography/pl.bib
rda3963a r565acf59 1797 1797 } 1798 1798 1799 @article{Delisle 19,1799 @article{Delisle20, 1800 1800 keywords = {concurrency, Cforall}, 1801 1801 contributer = {pabuhr@plg}, 1802 1802 author = {Thierry Delisle and Peter A. Buhr}, 1803 1803 title = {Advanced Control-flow and Concurrency in \textsf{C}$\mathbf{\forall}$}, 1804 year = 20 19,1804 year = 2020, 1805 1805 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 = {}, 1808 1809 } 1809 1810 -
doc/theses/andrew_beach_MMath/existing.tex
rda3963a r565acf59 1 \chapter{\ texorpdfstring{\CFA Existing Features}{Cforall Existing Features}}1 \chapter{\CFA Existing Features} 2 2 3 3 \CFA (C-for-all)~\cite{Cforall} is an open-source project extending ISO C with … … 12 12 obvious to the reader. 13 13 14 \section{ \texorpdfstring{Overloading and \lstinline|extern|}{Overloading andextern}}14 \section{Overloading and \lstinline{extern}} 15 15 \CFA has extensive overloading, allowing multiple definitions of the same name 16 16 to be defined.~\cite{Moss18} … … 42 42 43 43 \section{Reference Type} 44 \CFA adds a rebindable reference type to C, but more expressive than the \C C44 \CFA adds a rebindable reference type to C, but more expressive than the \Cpp 45 45 reference. Multi-level references are allowed and act like auto-dereferenced 46 46 pointers using the ampersand (@&@) instead of the pointer asterisk (@*@). \CFA … … 59 59 60 60 Both constructors and destructors are operators, which means they are just 61 functions with special operator names rather than type names in \C C. The61 functions with special operator names rather than type names in \Cpp. The 62 62 special operator names may be used to call the functions explicitly (not 63 allowed in \C Cfor constructors).63 allowed in \Cpp for constructors). 64 64 65 65 In general, operator names in \CFA are constructed by bracketing an operator … … 88 88 matching overloaded destructor @void ^?{}(T &);@ is called. Without explicit 89 89 definition, \CFA creates a default and copy constructor, destructor and 90 assignment (like \C C). It is possible to define constructors/destructors for90 assignment (like \Cpp). It is possible to define constructors/destructors for 91 91 basic and existing types. 92 92 … … 94 94 \CFA uses parametric polymorphism to create functions and types that are 95 95 defined over multiple types. \CFA polymorphic declarations serve the same role 96 as \C Ctemplates or Java generics. The ``parametric'' means the polymorphism is96 as \Cpp templates or Java generics. The ``parametric'' means the polymorphism is 97 97 accomplished by passing argument operations to associate \emph{parameters} at 98 98 the call site, and these parameters are used in the function to differentiate … … 134 134 135 135 Note, a function named @do_once@ is not required in the scope of @do_twice@ to 136 compile it, unlike \C Ctemplate expansion. Furthermore, call-site inferencing136 compile it, unlike \Cpp template expansion. Furthermore, call-site inferencing 137 137 allows local replacement of the most specific parametric functions needs for a 138 138 call. … … 178 178 } 179 179 \end{cfa} 180 The generic type @node(T)@ is an example of a polymorphic-type usage. Like \C C180 The generic type @node(T)@ is an example of a polymorphic-type usage. Like \Cpp 181 181 templates usage, a polymorphic-type usage must specify a type parameter. 182 182 -
doc/theses/andrew_beach_MMath/features.tex
rda3963a r565acf59 5 5 6 6 \section{Virtuals} 7 Virtual types and casts are not part of the exception system nor are they 8 required for an exception system. But an object-oriented style hierarchy is a 9 great way of organizing exceptions so a minimal virtual system has been added 10 to \CFA. 11 12 The pattern of a simple hierarchy was borrowed from object-oriented 13 programming was chosen for several reasons. 14 The first is that it allows new exceptions to be added in user code 15 and in libraries independently of each other. Another is it allows for 16 different levels of exception grouping (all exceptions, all IO exceptions or 17 a particular IO exception). Also it also provides a simple way of passing 18 data back and forth across the throw. 19 7 20 Virtual types and casts are not required for a basic exception-system but are 8 21 useful for advanced exception features. However, \CFA is not object-oriented so 9 there is no obvious concept of virtuals. 10 features for this work, I needed to design ed and implementeda virtual-like22 there is no obvious concept of virtuals. Hence, to create advanced exception 23 features for this work, I needed to design and implement a virtual-like 11 24 system for \CFA. 12 25 26 % NOTE: Maybe we should but less of the rational here. 13 27 Object-oriented languages often organized exceptions into a simple hierarchy, 14 28 \eg Java. … … 30 44 \end{center} 31 45 The hierarchy provides the ability to handle an exception at different degrees 32 of specificity (left to right). 46 of specificity (left to right). Hence, it is possible to catch a more general 33 47 exception-type in higher-level code where the implementation details are 34 48 unknown, which reduces tight coupling to the lower-level implementation. … … 61 75 While much of the virtual infrastructure is created, it is currently only used 62 76 internally for exception handling. The only user-level feature is the virtual 63 cast, which is the same as the \CC \lstinline[language=C++]|dynamic_cast|. 77 cast, which is the same as the \Cpp \lstinline[language=C++]|dynamic_cast|. 78 \label{p:VirtualCast} 64 79 \begin{cfa} 65 80 (virtual TYPE)EXPRESSION 66 81 \end{cfa} 67 Note, the syntax and semantics matches a C-cast, rather than the unusual \CC 68 syntax for special casts. Both the type of @EXPRESSION@ and @TYPE@ must be a 69 pointer to a virtual type. The cast dynamically checks if the @EXPRESSION@ type 70 is the same or a subtype of @TYPE@, and if true, returns a pointer to the 82 Note, 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 84 a pointer to a virtual type. 85 The cast dynamically checks if the @EXPRESSION@ type is the same or a subtype 86 of @TYPE@, and if true, returns a pointer to the 71 87 @EXPRESSION@ object, otherwise it returns @0p@ (null pointer). 72 88 … … 77 93 78 94 Exceptions are defined by the trait system; there are a series of traits, and 79 if a type satisfies them, then it can be used as an exception. 95 if a type satisfies them, then it can be used as an exception. The following 80 96 is the base trait all exceptions need to match. 81 97 \begin{cfa} 82 98 trait is_exception(exceptT &, virtualT &) { 83 virtualT const & @get_exception_vtable@(exceptT *);99 virtualT const & get_exception_vtable(exceptT *); 84 100 }; 85 101 \end{cfa} 86 The function takes any pointer, including the null pointer, and returns a 87 reference to the virtual-table object. Defining this function also establishes 88 the virtual type and a virtual-table pair to the \CFA type-resolver and 89 promises @exceptT@ is a virtual type and a child of the base exception-type. 90 91 {\color{blue} PAB: I do not understand this paragraph.} 92 One odd thing about @get_exception_vtable@ is that it should always be a 93 constant function, returning the same value regardless of its argument. A 94 pointer or reference to the virtual table instance could be used instead, 95 however using a function has some ease of implementation advantages and allows 96 for easier disambiguation because the virtual type name (or the address of an 97 instance that is in scope) can be used instead of the mangled virtual table 98 name. Also note the use of the word ``promise'' in the trait 99 description. Currently, \CFA cannot check to see if either @exceptT@ or 100 @virtualT@ match the layout requirements. This is considered part of 101 @get_exception_vtable@'s correct implementation. 102 The trait is defined over two types, the exception type and the virtual table 103 type. This should be one-to-one, each exception type has only one virtual 104 table type and vice versa. The only assertion in the trait is 105 @get_exception_vtable@, which takes a pointer of the exception type and 106 returns a reference to the virtual table type instance. 107 108 The function @get_exception_vtable@ is actually a constant function. 109 Recardless of the value passed in (including the null pointer) it should 110 return a reference to the virtual table instance for that type. 111 The reason it is a function instead of a constant is that it make type 112 annotations easier to write as you can use the exception type instead of the 113 virtual 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 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. 102 120 103 121 \section{Raise} 104 \CFA provides two kinds of exception raise: termination (see105 \ VRef{s:Termination}) and resumption (see \VRef{s:Resumption}), which are122 \CFA provides two kinds of exception raise: termination 123 \see{\VRef{s:Termination}} and resumption \see{\VRef{s:Resumption}}, which are 106 124 specified with the following traits. 107 125 \begin{cfa} 108 126 trait is_termination_exception( 109 127 exceptT &, virtualT & | is_exception(exceptT, virtualT)) { 110 void @defaultTerminationHandler@(exceptT &);128 void defaultTerminationHandler(exceptT &); 111 129 }; 112 130 \end{cfa} … … 118 136 trait is_resumption_exception( 119 137 exceptT &, virtualT & | is_exception(exceptT, virtualT)) { 120 void @defaultResumptionHandler@(exceptT &);138 void defaultResumptionHandler(exceptT &); 121 139 }; 122 140 \end{cfa} … … 125 143 126 144 Finally there are three convenience macros for referring to the these traits: 127 @IS_EXCEPTION@, @IS_TERMINATION_EXCEPTION@ and @IS_RESUMPTION_EXCEPTION@. Each 128 takes the virtual type's name, and for polymorphic types only, the 129 parenthesized list of polymorphic arguments. These macros do the name mangling 130 to get the virtual-table name and provide the arguments to both sides 131 {\color{blue}(PAB: What's a ``side''?)} 145 @IS_EXCEPTION@, @IS_TERMINATION_EXCEPTION@ and @IS_RESUMPTION_EXCEPTION@. 146 All three traits are hard to use while naming the virtual table as it has an 147 internal mangled name. These macros take the exception name as their first 148 argument and do the mangling. They all take a second argument for polymorphic 149 types which is the parenthesized list of polymorphic arguments. These 150 arguments are passed to both the exception type and the virtual table type as 151 the arguments do have to match. 152 153 For example consider a function that is polymorphic over types that have a 154 defined arithmetic exception: 155 \begin{cfa} 156 forall(Num | IS_EXCEPTION(Arithmetic, (Num))) 157 void some_math_function(Num & left, Num & right); 158 \end{cfa} 132 159 133 160 \subsection{Termination} … … 146 173 throw EXPRESSION; 147 174 \end{cfa} 148 The expression must return a termination-exception reference, where the 149 termination exception has a type with a @void defaultTerminationHandler(T &)@ 150 (default handler) defined. The handler is found at the call site using \CFA's 151 trait system and passed into the exception system along with the exception 152 itself. 153 154 At runtime, a representation of the exception type and an instance of the 155 exception type is copied into managed memory (heap) to ensure it remains in 175 The expression must return a reference to a termination exception, where the 176 termination exception is any type that satifies @is_termination_exception@ 177 at the call site. 178 Through \CFA's trait system the functions in the traits are passed into the 179 throw code. A new @defaultTerminationHandler@ can be defined in any scope to 180 change the throw's behavior (see below). 181 182 At runtime, the exception returned by the expression 183 is copied into managed memory (heap) to ensure it remains in 156 184 scope during unwinding. It is the user's responsibility to ensure the original 157 185 exception object at the throw is freed when it goes out of scope. Being … … 165 193 try { 166 194 GUARDED_BLOCK 167 } @catch (EXCEPTION_TYPE$\(_1\)$ * NAME)@{ // termination handler 1195 } catch (EXCEPTION_TYPE$\(_1\)$ * NAME$\(_1\)$) { // termination handler 1 168 196 HANDLER_BLOCK$\(_1\)$ 169 } @catch (EXCEPTION_TYPE$\(_2\)$ * NAME)@{ // termination handler 2197 } catch (EXCEPTION_TYPE$\(_2\)$ * NAME$\(_2\)$) { // termination handler 2 170 198 HANDLER_BLOCK$\(_2\)$ 171 199 } … … 178 206 Exception matching checks the representation of the thrown exception-type is 179 207 the same or a descendant type of the exception types in the handler clauses. If 180 there is a match, a pointer to the exception object created at the throwis181 bound to @NAME@ and the statements in the associated @HANDLER_BLOCK@ are182 executed. If control reaches the end of the handler, the exception is freed, 183 and control continues after the try statement.208 it is the same of a descendent of @EXCEPTION_TYPE@$_i$ then @NAME@$_i$ is 209 bound to a pointer to the exception and the statements in @HANDLER_BLOCK@$_i$ 210 are executed. If control reaches the end of the handler, the exception is 211 freed and control continues after the try statement. 184 212 185 213 The default handler visible at the throw statement is used if no matching 186 214 termination handler is found after the entire stack is searched. At that point, 187 215 the default handler is called with a reference to the exception object 188 generated at the throw. If the default handler returns, the system default189 action is executed, which often terminates the program. This feature allows216 generated at the throw. If the default handler returns, control continues 217 from after the throw statement. This feature allows 190 218 each exception type to define its own action, such as printing an informative 191 219 error message, when an exception is not handled in the program. 220 However the default handler for all exception types triggers a cancellation 221 using the exception. 192 222 193 223 \subsection{Resumption} … … 196 226 Resumption raise, called ``resume'', is as old as termination 197 227 raise~\cite{Goodenough75} but is less popular. In many ways, resumption is 198 simpler and easier to understand, as it is simply a dynamic call (as in199 Lisp).The semantics of resumption is: search the stack for a matching handler,228 simpler and easier to understand, as it is simply a dynamic call. 229 The semantics of resumption is: search the stack for a matching handler, 200 230 execute the handler, and continue execution after the resume. Notice, the stack 201 231 cannot be unwound because execution returns to the raise point. Resumption is … … 209 239 \end{cfa} 210 240 The semantics of the @throwResume@ statement are like the @throw@, but the 211 expression has a type with a @void defaultResumptionHandler(T &)@ (default 212 handler) defined, where the handler is found at the call site by the type 213 system. At runtime, a representation of the exception type and an instance of 214 the exception type is \emph{not} copied because the stack is maintained during 215 the handler search. 241 expression has return a reference a type that satifies the trait 242 @is_resumption_exception@. Like with termination the exception system can 243 use these assertions while (throwing/raising/handling) the exception. 244 245 At runtime, no copies are made. As the stack is not unwound the exception and 246 any values on the stack will remain in scope while the resumption is handled. 216 247 217 248 Then the exception system searches the stack starting from the resume and 218 proceeding to wardsthe base of the stack, from callee to caller. At each stack249 proceeding to the base of the stack, from callee to caller. At each stack 219 250 frame, a check is made for resumption handlers defined by the @catchResume@ 220 251 clauses of a @try@ statement. … … 222 253 try { 223 254 GUARDED_BLOCK 224 } @catchResume (EXCEPTION_TYPE$\(_1\)$ * NAME)@ { // resumption handler 1255 } catchResume (EXCEPTION_TYPE$\(_1\)$ * NAME$\(_1\)$) { 225 256 HANDLER_BLOCK$\(_1\)$ 226 } @catchResume (EXCEPTION_TYPE$\(_2\)$ * NAME)@ { // resumption handler 2257 } catchResume (EXCEPTION_TYPE$\(_2\)$ * NAME$\(_2\)$) { 227 258 HANDLER_BLOCK$\(_2\)$ 228 259 } … … 253 284 current point on the stack because new try statements may have been pushed by 254 285 the handler or functions called from the handler. If there is no match back to 255 the point of the current handler, the search skips the stack frames already256 s earched by the first resume and continues after the try statement. The default257 handler always continues from default handler associated with the point where 258 the exception is created.286 the point of the current handler, the search skips\label{p:searchskip} the 287 stack frames already searched by the first resume and continues after 288 the try statement. The default handler always continues from default 289 handler associated with the point where the exception is created. 259 290 260 291 % This might need a diagram. But it is an important part of the justification … … 275 306 \end{verbatim} 276 307 277 This resumption search-pattern reflect the one for termination, which matches 278 with programmer expectations. However, it avoids the \emph{recursive 279 resumption} problem. If parts of the stack are searched multiple times, loops 308 This resumption search pattern reflects the one for termination, and so 309 should come naturally to most programmers. 310 However, it avoids the \emph{recursive resumption} problem. 311 If parts of the stack are searched multiple times, loops 280 312 can easily form resulting in infinite recursion. 281 313 … … 283 315 \begin{cfa} 284 316 try { 285 throwResume$\(_1\)$ (E &){}; 286 } catch( E * ) { 287 throwResume; 288 } 289 \end{cfa} 290 Based on termination semantics, programmer expectation is for the re-resume to 291 continue searching the stack frames after the try statement. However, the 292 current try statement is still on the stack below the handler issuing the 293 reresume (see \VRef{s:Reraise}). Hence, the try statement catches the re-raise 294 again and does another re-raise \emph{ad infinitum}, which is confusing and 295 difficult to debug. The \CFA resumption search-pattern skips the try statement 296 so the reresume search continues after the try, mathcing programmer 297 expectation. 317 throwResume (E &){}; // first 318 } catchResume(E *) { 319 throwResume (E &){}; // second 320 } 321 \end{cfa} 322 If this handler is ever used it will be placed on top of the stack above the 323 try statement. If the stack was not masked than the @throwResume@ in the 324 handler would always be caught by the handler, leading to an infinite loop. 325 Masking avoids this problem and other more complex versions of it involving 326 multiple handlers and exception types. 327 328 Other masking stratagies could be used; such as masking the handlers that 329 have caught an exception. This one was choosen because it creates a symmetry 330 with termination (masked sections of the stack would be unwound with 331 termination) and having only one pattern to learn is easier. 298 332 299 333 \section{Conditional Catch} 300 Both termination and resumption handler-clauses may perform conditional matching: 334 Both termination and resumption handler clauses can be given an additional 335 condition to further control which exceptions they handle: 301 336 \begin{cfa} 302 337 catch (EXCEPTION_TYPE * NAME ; @CONDITION@) … … 305 340 exception matches, @CONDITION@ is executed. The condition expression may 306 341 reference all names in scope at the beginning of the try block and @NAME@ 307 introduced in the handler clause. 342 introduced in the handler clause. If the condition is true, then the handler 308 343 matches. Otherwise, the exception search continues at the next appropriate kind 309 344 of handler clause in the try block. … … 322 357 323 358 \section{Reraise} 359 \color{red}{From Andrew: I recomend we talk about why the language doesn't 360 have rethrows/reraises instead.} 361 324 362 \label{s:Reraise} 325 363 Within the handler block or functions called from the handler block, it is … … 327 365 @throwResume@, respective. 328 366 \begin{cfa} 329 catch( ... ) { 367 try { 368 ... 369 } catch( ... ) { 330 370 ... throw; // rethrow 331 371 } catchResume( ... ) { … … 340 380 handler is generated that does a program-level abort. 341 381 342 343 382 \section{Finally Clauses} 344 383 A @finally@ clause may be placed at the end of a @try@ statement. … … 346 385 try { 347 386 GUARDED_BLOCK 348 } ... 349 }finally {387 } ... // any number or kind of handler clauses 388 ... finally { 350 389 FINALLY_BLOCK 351 390 } 352 391 \end{cfa} 353 The @FINALLY_BLOCK@ is executed when the try statement is unwound from the 354 stack, \ie when the @GUARDED_BLOCK@ or any handler clause finishes. Hence, the 355 finally block is always executed. 392 The @FINALLY_BLOCK@ is executed when the try statement is removed from the 393 stack, including when the @GUARDED_BLOCK@ or any handler clause finishes or 394 during an unwind. 395 The only time the block is not executed is if the program is exited before 396 that happens. 356 397 357 398 Execution of the finally block should always finish, meaning control runs off 358 399 the end of the block. This requirement ensures always continues as if the 359 400 finally clause is not present, \ie finally is for cleanup not changing control 360 flow. 361 is forbidden. 401 flow. Because of this requirement, local control flow out of the finally block 402 is forbidden. The compiler precludes any @break@, @continue@, @fallthru@ or 362 403 @return@ that causes control to leave the finally block. Other ways to leave 363 404 the finally block, such as a long jump or termination are much harder to check, … … 369 410 possible forwards the cancellation exception to a different stack. 370 411 412 Cancellation is not an exception operation like termination or resumption. 371 413 There is no special statement for starting a cancellation; instead the standard 372 library function @cancel_stack@ is called passing an exception. 414 library function @cancel_stack@ is called passing an exception. Unlike a 373 415 raise, this exception is not used in matching only to pass information about 374 416 the cause of the cancellation. … … 377 419 \begin{description} 378 420 \item[Main Stack:] 379 380 421 The main stack is the one used by the program main at the start of execution, 381 and is the only stack in a sequential program. Hence, when cancellation is 382 forwarded to the main stack, there is no other forwarding stack, so after the 383 stack is unwound, there is a program-level abort. 422 and is the only stack in a sequential program. Even in a concurrent program 423 the main stack is only dependent on the environment that started the program. 424 Hence, when the main stack is cancelled there is nowhere else in the program 425 to notify. After the stack is unwound, there is a program-level abort. 384 426 385 427 \item[Thread Stack:] 386 428 A thread stack is created for a @thread@ object or object that satisfies the 387 @is_thread@ trait. 429 @is_thread@ trait. A thread only has two points of communication that must 388 430 happen: start and join. As the thread must be running to perform a 389 cancellation, it must occur after start and before join, so join is a 390 cancellation point. After the stack is unwound, the thread halts and waits for 391 another thread to join with it. The joining thread, checks for a cancellation, 431 cancellation, it must occur after start and before join, so join is used 432 for communication here. 433 After the stack is unwound, the thread halts and waits for 434 another thread to join with it. The joining thread checks for a cancellation, 392 435 and if present, resumes exception @ThreadCancelled@. 393 436 … … 397 440 the exception is not caught. The implicit join does a program abort instead. 398 441 399 This semantics is for safety. One difficult problem for any exception system is 400 defining semantics when an exception is raised during an exception search: 401 which exception has priority, the original or new exception? No matter which 402 exception is selected, it is possible for the selected one to disrupt or 403 destroy the context required for the other. {\color{blue} PAB: I do not 404 understand the following sentences.} This loss of information can happen with 405 join but as the thread destructor is always run when the stack is being unwound 406 and one termination/cancellation is already active. Also since they are 407 implicit they are easier to forget about. 442 This semantics is for safety. If an unwind is triggered while another unwind 443 is underway only one of them can proceed as they both want to ``consume'' the 444 stack. Letting both try to proceed leads to very undefined behaviour. 445 Both termination and cancellation involve unwinding and, since the default 446 @defaultResumptionHandler@ preforms a termination that could more easily 447 happen in an implicate join inside a destructor. So there is an error message 448 and an abort instead. 449 450 The recommended way to avoid the abort is to handle the intial resumption 451 from the implicate join. If required you may put an explicate join inside a 452 finally clause to disable the check and use the local 453 @defaultResumptionHandler@ instead. 408 454 409 455 \item[Coroutine Stack:] A coroutine stack is created for a @coroutine@ object 410 or object that satisfies the @is_coroutine@ trait. 411 two other coroutines, its starter and its last resumer. 412 the tightest coupling to the coroutine it activated. 456 or object that satisfies the @is_coroutine@ trait. A coroutine only knows of 457 two other coroutines, its starter and its last resumer. The last resumer has 458 the tightest coupling to the coroutine it activated. Hence, cancellation of 413 459 the active coroutine is forwarded to the last resumer after the stack is 414 460 unwound, as the last resumer has the most precise knowledge about the current -
doc/theses/andrew_beach_MMath/future.tex
rda3963a r565acf59 1 1 \chapter{Future Work} 2 2 3 \section{Language Improvements} 4 \CFA is a developing programming language. As such, there are partially or 5 unimplemented features of the language (including several broken components) 6 that I had to workaround while building an exception handling system largely in 7 the \CFA language (some C components). The following are a few of these 8 issues, and once implemented/fixed, how this would affect the exception system. 9 \begin{itemize} 10 \item 11 The implementation of termination is not portable because it includes 12 hand-crafted assembly statements. These sections must be ported by hand to 13 support more hardware architectures, such as the ARM processor. 14 \item 15 Due to a type-system problem, the catch clause cannot bind the exception to a 16 reference instead of a pointer. Since \CFA has a very general reference 17 capability, programmers will want to use it. Once fixed, this capability should 18 result in little or no change in the exception system. 19 \item 20 Termination handlers cannot use local control-flow transfers, \eg by @break@, 21 @return@, \etc. The reason is that current code generation hoists a handler 22 into 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 24 scope of the @try@ statement, where the local control-flow transfers are 25 meaningful. 26 \item 27 There is no detection of colliding unwinds. It is possible for clean-up code 28 run during an unwind to trigger another unwind that escapes the clean-up code 29 itself; such as a termination exception caught further down the stack or a 30 cancellation. There do exist ways to handle this but currently they are not 31 even detected and the first unwind will simply be forgotten, often leaving 32 it in a bad state. 33 \item 34 Also the exception system did not have a lot of time to be tried and tested. 35 So just letting people use the exception system more will reveal new 36 quality of life upgrades that can be made with time. 37 \end{itemize} 38 3 39 \section{Complete Virtual System} 4 The virtual system should be completed. It was n ever supposed to be part of5 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 usethe current version.40 The virtual system should be completed. It was not supposed to be part of this 41 project, but was thrust upon it to do exception inheritance; hence, only 42 minimal work was done. A draft for a complete virtual system is available but 43 it is not finalized. A future \CFA project is to complete that work and then 44 update the exception system that uses the current version. 9 45 10 There are several improvements to the virtual system that would improve 11 the exception traits. The biggest one is an assertion that checks that one 12 virtual type is a child of another virtual type. This would capture many of 13 the requirements much more precisely.46 There are several improvements to the virtual system that would improve the 47 exception traits. The most important one is an assertion to check one virtual 48 type is a child of another. This check precisely captures many of the 49 correctness requirements. 14 50 15 51 The full virtual system might also include other improvement like associated 16 types. This is a proposed feature that would allow traits to refer to types 17 not listed in their header. This would allow the exception traits to not 18 refer to the virtual table type explicatly which would remove the need for 19 the interface macros. 52 types to allow traits to refer to types not listed in their header. This 53 feature allows exception traits to not refer to the virtual-table type 54 explicitly, removing the need for the current interface macros. 20 55 21 \section{Additional Throws} 22 Several other kinds of throws, beyond the termination throw (@throw@), 23 the resumption throw (@throwResume@) and the re-throws, were considered. 24 None were as useful as the core throws but they would likely be worth 25 revising. 56 \section{Additional Raises} 57 Several other kinds of exception raises were considered beyond termination 58 (@throw@), resumption (@throwResume@), and reraise. 26 59 27 The first ones are throws for asynchronous exceptions, throwing exceptions 28 from one stack to another. These act like signals allowing for communication 29 between the stacks. This is usually used with resumption as it allows the 30 target stack to continue execution normally after the exception has been 31 handled. 60 The first is a non-local/concurrent raise providing asynchronous exceptions, 61 \ie raising an exception on another stack. This semantics acts like signals 62 allowing for out-of-band communication among coroutines and threads. This kind 63 of raise is often restricted to resumption to allow the target stack to 64 continue execution normally after the exception has been handled. That is, 65 allowing one coroutine/thread to unwind the stack of another via termination is 66 bad software engineering. 32 67 33 This would much more coordination between the concurrency system and the 34 exception system to handle. Most of the interesting design decisions around 35 a pplying asynchronous exceptions appear to be around masking (controlling36 w hich exceptions may be thrown at a stack). It would likely require more of37 the virtual system and would also effect howdefault handlers are set.68 Non-local/concurrent requires more coordination between the concurrency system 69 and the exception system. Many of the interesting design decisions centre 70 around masking (controlling which exceptions may be thrown at a stack). It 71 would likely require more of the virtual system and would also effect how 72 default handlers are set. 38 73 39 The other throws were designed to mimic bidirectional algebraic effects.40 Algebraic effects are used in some functional languages a nd allow afunction74 Other raises were considered to mimic bidirectional algebraic effects. 75 Algebraic effects are used in some functional languages allowing one function 41 76 to have another function on the stack resolve an effect (which is defined with 42 a function-like interface). 43 These can be mimiced with resumptions and the the new throws were designed 44 to try and mimic bidirectional algebraic effects, where control can go back 45 and forth between the function effect caller and handler while the effect 46 is underway. 77 a functional-like interface). This semantics can be mimicked with resumptions 78 and new raises were discussed to mimic bidirectional algebraic-effects, where 79 control can go back and forth between the function-effect caller and handler 80 while the effect is underway. 47 81 % resume-top & resume-reply 82 These raises would be like the resumption raise except using different search 83 patterns to find the handler. 48 84 49 These throws would likely be just like the resumption throw except they would 50 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 87 code rather than assembler code \see{\VPageref{p:zero-cost}}. When the compiler 88 does create its own assembly (or LLVM byte-code), then zero-cost try-statements 89 are possible. The downside of zero-cost try-statements is the LSDA complexity, 90 its size (program bloat), and the high cost of raising an exception. 51 91 52 \section{Zero-Cost Exceptions} 53 \CFA does not have zero-cost exceptions because it does not generate assembly 54 but instead generates C code. See the implementation section. When the 55 compiler does start to create its own assembly (or LLVM byte code) then 56 zero-cost exceptions could be implemented. 92 Alternatively, some research could be done into the simpler alternative method 93 with a non-zero-cost try-statement but much lower cost exception raise. For 94 example, programs are starting to use exception in the normal control path, so 95 more exceptions are thrown. In these cases, the cost balance switches towards 96 low-cost raise. Unfortunately, while exceptions remain exceptional, the 97 libunwind model will probably remain the most effective option. 57 98 58 Now in zero-cost exceptions the only part that is zero-cost are the try 59 blocks. Some research could be done into the alternative methods for systems 60 that expect a lot more exceptions to be thrown, allowing some overhead in 61 entering and leaving try blocks to make throws faster. But while exceptions 62 remain exceptional the libunwind model will probably remain the most effective 63 option. 99 Zero-cost resumptions is still an open problem. First, because libunwind does 100 not support a successful-exiting stack-search without doing an unwind. 101 Workarounds are possible but awkward. Ideally an extension to libunwind could 102 be made, but that would either require separate maintenance or gain enough 103 support to have it folded into the standard. 64 104 65 Zero-cost resumptions have more problems to solve. First because libunwind 66 does not support a successful exiting stack search without doing an unwind. 67 There are several ways to hack that functionality in. Ideally an extension to 68 libunwind could be made, but that would either require seperate maintenance 69 or gain enough support to have it folded into the standard. 70 71 Also new techniques to skip previously searched parts of the stack will have 72 to be developed. The recursive resume problem still remains and ideally the 73 same pattern of ignoring sections of the stack. 105 Also new techniques to skip previously searched parts of the stack need to be 106 developed to handle the recursive resume problem and support advanced algebraic 107 effects. 74 108 75 109 \section{Signal Exceptions} 76 Exception Handling: Issues and a Proposed Notation suggests there are three 77 types of exceptions: escape, notify and signal. 78 Escape exceptions are our termination exceptions, notify exceptions are 79 resumption exceptions and that leaves signal exception unimplemented. 110 Goodenough~\cite{Goodenough75} suggests three types of exceptions: escape, 111 notify and signal. Escape are termination exceptions, notify are resumption 112 exceptions, leaving signal unimplemented. 80 113 81 Signal exceptions allow either behaviour, that is after the exception is 82 handled control can either return to the throw or from where the handler is 83 defined. 114 A signal exception allows either behaviour, \ie after an exception is handled, 115 the handler has the option of returning to the raise or after the @try@ 116 statement. Currently, \CFA fixes the semantics of the handler return 117 syntactically by the @catch@ or @catchResume@ clause. 84 118 85 The design should be rexamined and be updated for \CFA. A very direct 86 translation would perhaps have a new throw and catch pair and astatement87 (or statements) could be used to decide if the handler returns to the throw88 or continues where it is, but there are other options.119 Signal exception should be reexamined and possibly be supported in \CFA. A very 120 direct 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 122 where it is; but there may be other options. 89 123 90 For instance resumption could be extended to cover this use by allowing91 local control flow out of it. Thiswould require an unwind as part of the92 transition as there are stack frames that have to be removed. 93 This would mean there is no notify like throw but because \CFA does not have 94 exception signatures a termination can be thrown from any resumption handler 95 already so there are already ways one could try to dothis in existing \CFA.124 For instance, resumption could be extended to cover this use by allowing local 125 control flow out of it. This approach would require an unwind as part of the 126 transition as there are stack frames that have to be removed. This approach 127 means there is no notify raise, but because \CFA does not have exception 128 signatures, a termination can be thrown from within any resumption handler so 129 there is already a way to do mimic this in existing \CFA. 96 130 97 131 % Maybe talk about the escape; and escape CONTROL_STMT; statements or how 98 132 % if we could choose if _Unwind_Resume proceeded to the clean-up stage this 99 133 % would be much easier to implement. 100 101 \section{Language Improvements}102 There is also a lot of work that are not follow ups to this work in terms of103 research, some have no interesting research to be done at all, but would104 improve \CFA as a programming language. The full list of these would105 naturally be quite extensive but here are a few examples that involve106 exceptions:107 108 \begin{itemize}109 \item The implementation of termination is not portable because it includes110 some assembly statements. These sections will have to be re-written to so111 \CFA has full support on more machines.112 \item Allowing exception handler to bind the exception to a reference instead113 of a pointer. This should actually result in no change in behaviour so there114 is no reason not to allow it. It is however a small improvement; giving a bit115 of flexibility to the user in what style they want to use.116 \item Enabling local control flow (by @break@, @return@ and117 similar statements) out of a termination handler. The current set-up makes118 this very difficult but the catch function that runs the handler after it has119 been matched could be inlined into the function's body, which would make this120 much easier. (To do the same for try blocks would probably wait for zero-cost121 exceptions, which would allow the try block to be inlined as well.)122 \end{itemize} -
doc/theses/andrew_beach_MMath/implement.tex
rda3963a r565acf59 2 2 % Goes over how all the features are implemented. 3 3 4 The implementation work for this thesis covers two components: the virtual 5 system and exceptions. Each component is discussed in detail. 6 4 7 \section{Virtual System} 8 \label{s:VirtualSystem} 5 9 % 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 @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. 10 While the \CFA virtual system currently has only one public feature, virtual 11 cast \see{\VPageref{p:VirtualCast}}, substantial structure is required to 12 support it, and provide features for exception handling and the standard 13 library. 14 15 \subsection{Virtual Table} 16 The virtual system is accessed through a private constant field inserted at the 17 beginning of every virtual type, called the virtual-table pointer. This field 18 points at a type's virtual table and is assigned during the object's 19 construction. The address of a virtual table acts as the unique identifier for 20 the virtual type, and the first field of a virtual table is a pointer to the 21 parent virtual-table or @0p@. The remaining fields are duplicated from the 22 parent tables in this type's inheritance chain, followed by any fields this type 23 introduces. Parent fields are duplicated so they can be changed (\CC 24 \lstinline[language=c++]|override|), so that references to the dispatched type 25 are replaced with the current virtual type. 26 \PAB{Can you create a simple diagram of the layout?} 27 % These are always taken by pointer or reference. 28 29 % For each virtual type, a virtual table is constructed. This is both a new type 30 % and an instance of that type. Other instances of the type could be created 31 % but the system doesn't use them. So this section will go over the creation of 32 % the type and the instance. 33 34 A virtual table is created when the virtual type is created. The name of the 35 type is created by mangling the name of the base type. The name of the instance 36 is also generated by name mangling. The fields are initialized automatically. 39 37 The parent field is initialized by getting the type of the parent field and 40 38 using that to calculate the mangled name of the parent's virtual table type. 41 39 There are two special fields that are included like normal fields but have 42 40 special initialization rules: the @size@ field is the type's size and is 43 initialized with a sizeof expression, the @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 41 initialized with a @sizeof@ expression, the @align@ field is the type's 42 alignment and uses an @alignof@ expression. The remaining fields are resolved 43 to a name matching the field's name and type using the normal visibility and 44 overload resolution rules of the type system. 45 46 These operations are split up into several groups depending on where they take 47 place which varies for monomorphic and polymorphic types. The first devision is 48 between the declarations and the definitions. Declarations, such as a function 49 signature or a aggregate's name, must always be visible but may be repeated in 50 the form of forward declarations in headers. Definitions, such as function 51 bodies and a aggregate's layout, can be separately compiled but must occur 52 exactly once in a source file. 53 54 \begin{sloppypar} 56 55 The declarations include the virtual type definition and forward declarations 57 56 of the virtual table instance, constructor, message function and 58 @get_exception_vtable@. The definition includes the storage and 59 initialization of the virtual table instance and the bodies of the three 60 functions. 57 @get_exception_vtable@. The definition includes the storage and initialization 58 of the virtual table instance and the bodies of the three functions. 59 \end{sloppypar} 61 60 62 61 Monomorphic instances put all of these two groups in one place each. 63 64 Polymorphic instances also split out the core declarations and definitions65 from the per-instance information. The virtual table type and most of the66 functions are polymorphic so they are all part of the core. The virtual table 67 instance and the @get_exception_vtable@ function. 68 62 Polymorphic instances also split out the core declarations and definitions from 63 the per-instance information. The virtual table type and most of the functions 64 are polymorphic so they are all part of the core. The virtual table instance 65 and the @get_exception_vtable@ function. 66 67 \begin{sloppypar} 69 68 Coroutines and threads need instances of @CoroutineCancelled@ and 70 @ThreadCancelled@ respectively to use all of their functionality. 71 When a new data type is declared with @coroutine@ or @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. 69 @ThreadCancelled@ respectively to use all of their functionality. When a new 70 data type is declared with @coroutine@ or @thread@ the forward declaration for 71 the instance is created as well. The definition of the virtual table is created 72 at the definition of the main function. 73 \end{sloppypar} 74 74 75 75 \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 @__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 @__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. 76 Virtual casts are implemented as a function call that does the subtype check 77 and a C coercion-cast to do the type conversion. 78 % The C-cast is just to make sure the generated code is correct so the rest of 79 % the section is about that function. 80 The function is 81 \begin{cfa} 82 void * __cfa__virtual_cast( struct __cfa__parent_vtable const * parent, 83 struct __cfa__parent_vtable const * const * child ); 84 } 85 \end{cfa} 86 and it is implemented in the standard library. It takes a pointer to the target 87 type's virtual table and the object pointer being cast. The function performs a 88 linear search starting at the object's virtual-table and walking through the 89 the parent pointers, checking to if it or any of its ancestors are the same as 90 the target-type virtual table-pointer. 91 92 For the generated code, a forward declaration of the virtual works as follows. 93 There is a forward declaration of @__cfa__virtual_cast@ in every \CFA file so 94 it can just be used. The object argument is the expression being cast so that 95 is just placed in the argument list. 96 97 To build the target type parameter, the compiler creates a mapping from 98 concrete type-name -- so for polymorphic types the parameters are filled in -- 99 to virtual table address. Every virtual table declaration is added to the this 100 table; repeats are ignored unless they have conflicting definitions. Note, 101 these declarations do not have to be in scope, but they should usually be 102 introduced as part of the type definition. 103 104 \PAB{I do not understood all of \VRef{s:VirtualSystem}. I think you need to 105 write more to make it clear.} 106 99 107 100 108 \section{Exceptions} … … 106 114 % resumption doesn't as well. 107 115 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 @setjmp@ \& @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 @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 @-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); 116 % Many modern languages work with an interal stack that function push and pop 117 % their local data to. Stack unwinding removes large sections of the stack, 118 % often across functions. 119 120 Stack unwinding is the process of removing stack frames (activations) from the 121 stack. On function entry and return, unwinding is handled directly by the code 122 embedded in the function. Usually, the stack-frame size is known statically 123 based on parameter and local variable declarations. For dynamically-sized 124 local variables, a runtime computation is necessary to know the frame 125 size. Finally, a function's frame-size may change during execution as local 126 variables (static or dynamic sized) go in and out of scope. 127 Allocating/deallocating stack space is usually an $O(1)$ operation achieved by 128 bumping the hardware stack-pointer up or down as needed. 129 130 Unwinding across multiple stack frames is more complex because individual stack 131 management code associated with each frame is bypassed. That is, the location 132 of a function's frame-management code is largely unknown and dispersed 133 throughout the function, hence the current frame size managed by that code is 134 also unknown. Hence, code unwinding across frames does not have direct 135 knowledge about what is on the stack, and hence, how much of the stack needs to 136 be removed. 137 138 % At a very basic level this can be done with @setjmp@ \& @longjmp@ which simply 139 % move the top of the stack, discarding everything on the stack above a certain 140 % point. However this ignores all the cleanup code that should be run when 141 % certain sections of the stack are removed (for \CFA these are from destructors 142 % and finally clauses) and also requires that the point to which the stack is 143 % being unwound is known ahead of time. libunwind is used to address both of 144 % these problems. 145 146 The traditional unwinding mechanism for C is implemented by saving a snap-shot 147 of a function's state with @setjmp@ and restoring that snap-shot with 148 @longjmp@. This approach bypasses the need to know stack details by simply 149 reseting to a snap-shot of an arbitrary but existing function frame on the 150 stack. It is up to the programmer to ensure the snap-shot is valid when it is 151 reset, making this unwinding approach fragile with potential errors that are 152 difficult to debug because the stack becomes corrupted. 153 154 However, many languages define cleanup actions that must be taken when objects 155 are deallocated from the stack or blocks end, such as running a variable's 156 destructor or a @try@ statement's @finally@ clause. Handling these mechanisms 157 requires walking the stack and checking each stack frame for these potential 158 actions. 159 160 For exceptions, it must be possible to walk the stack frames in search of @try@ 161 statements to match and execute a handler. For termination exceptions, it must 162 also be possible to unwind all stack frames from the throw to the matching 163 catch, and each of these frames must be checked for cleanup actions. Stack 164 walking is where most of the complexity and expense of exception handling 165 appears. 166 167 One of the most popular tools for stack management is libunwind, a low-level 168 library that provides tools for stack walking, handler execution, and 169 unwinding. What follows is an overview of all the relevant features of 170 libunwind needed for this work, and how \CFA uses them to implement exception 171 handling. 172 173 \subsection{libunwind Usage} 174 Libunwind, accessed through @unwind.h@ on most platforms, is a C library that 175 provides \CC-style stack-unwinding. Its operation is divided into two phases: 176 search and cleanup. The dynamic target search -- phase 1 -- is used to scan the 177 stack and decide where unwinding should stop (but no unwinding occurs). The 178 cleanup -- phase 2 -- does the unwinding and also runs any cleanup code. 179 180 To use libunwind, each function must have a personality function and a Language 181 Specific Data Area (LSDA). The LSDA has the unique information for each 182 function to tell the personality function where a function is executing, its 183 current stack frame, and what handlers should be checked. Theoretically, the 184 LSDA can contain any information but conventionally it is a table with entries 185 representing regions of the function and what has to be done there during 186 unwinding. These regions are bracketed by the instruction pointer. If the 187 instruction pointer is within a region's start/end, then execution is currently 188 executing in that region. Regions are used to mark out the scopes of objects 189 with destructors and try blocks. 190 191 % Libunwind actually does very little, it simply moves down the stack from 192 % function to function. Most of the actions are implemented by the personality 193 % function which libunwind calls on every function. Since this is shared across 194 % many functions or even every function in a language it will need a bit more 195 % information. 196 197 The GCC compilation flag @-fexceptions@ causes the generation of an LSDA and 198 attaches its personality function. \PAB{to what is it attached?} However, this 199 flag only handles the cleanup attribute 200 \begin{cfa} 201 void clean_up( int * var ) { ... } 202 int avar __attribute__(( __cleanup(clean_up) )); 203 \end{cfa} 204 which is used on a variable and specifies a function, \eg @clean_up@, run when 205 the variable goes out of scope. The function is passed a pointer to the object 206 so it can be used to mimic destructors. However, this feature cannot be used to 207 mimic @try@ statements. 208 209 \subsection{Personality Functions} 210 Personality functions have a complex interface specified by libunwind. This 211 section covers some of the important parts of the interface. 212 213 A personality function performs four tasks, although not all have to be 214 present. 215 \begin{lstlisting}[language=C,{moredelim=**[is][\color{red}]{@}{@}}] 216 typedef _Unwind_Reason_Code (*@_Unwind_Personality_Fn@) ( 217 _Unwind_Action @action@, 218 _Unwind_Exception_Class @exception_class@, 219 _Unwind_Exception * @exception@, 220 struct _Unwind_Context * @context@ 221 ); 161 222 \end{lstlisting} 162 163 The return value, the reason code, is an enumeration of possible messages 223 The @action@ argument is a bitmask of possible actions: 224 \begin{enumerate} 225 \item 226 @_UA_SEARCH_PHASE@ specifies a search phase and tells the personality function 227 to check for handlers. If there is a handler in a stack frame, as defined by 228 the language, the personality function returns @_URC_HANDLER_FOUND@; otherwise 229 it return @_URC_CONTINUE_UNWIND@. 230 231 \item 232 @_UA_CLEANUP_PHASE@ specifies a cleanup phase, where the entire frame is 233 unwound and all cleanup code is run. The personality function does whatever 234 cleanup the language defines (such as running destructors/finalizers) and then 235 generally returns @_URC_CONTINUE_UNWIND@. 236 237 \item 238 \begin{sloppypar} 239 @_UA_HANDLER_FRAME@ specifies a cleanup phase on a function frame that found a 240 handler. The personality function must prepare to return to normal code 241 execution and return @_URC_INSTALL_CONTEXT@. 242 \end{sloppypar} 243 244 \item 245 @_UA_FORCE_UNWIND@ specifies a forced unwind call. Forced unwind only performs 246 the cleanup phase and uses a different means to decide when to stop 247 \see{\VRef{s:ForcedUnwind}}. 248 \end{enumerate} 249 250 The @exception_class@ argument is a copy of the 251 \lstinline[language=C]|exception|'s @exception_class@ field. 252 253 The \lstinline[language=C]|exception| argument is a pointer to the user 254 provided storage object. It has two public fields, the exception class, which 255 is actually just a number, identifying the exception handling mechanism that 256 created it, and the cleanup function. The cleanup function is called if 257 required by the exception. 258 259 The @context@ argument is a pointer to an opaque type passed to helper 260 functions called inside the personality function. 261 262 The return value, @_Unwind_Reason_Code@, is an enumeration of possible messages 164 263 that can be passed several places in libunwind. It includes a number of 165 264 messages for special cases (some of which should never be used by the … … 167 266 personality function should always return @_URC_CONTINUE_UNWIND@. 168 267 169 The @version@ argument is the verson of the implementation that is170 calling the personality function. At this point it appears to always be 1 and171 it will likely stay that way until a new version of the API is updated.172 173 The @action@ argument is set of flags that tell the personality174 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@_UA_SEARCH_PHASE@: This flag is set whenever the personality178 function is called during the search phase. The personality function should179 decide if unwinding will stop in this function or not. If it does then the180 personality function should return @_URC_HANDLER_FOUND@.181 \item@_UA_CLEANUP_PHASE@: This flag is set whenever the personality182 function is called during the cleanup phase. If no other flags are set this183 means the entire frame will be unwound and all cleanup code should be run.184 \item@_UA_HANDLER_FRAME@: This flag is set during the cleanup phase185 on the function frame that found the handler. The personality function must186 prepare to return to normal code execution and return187 @_URC_INSTALL_CONTEXT@.188 \item@_UA_FORCE_UNWIND@: This flag is set if the personality function189 is called through a forced unwind call. Forced unwind only performs the190 cleanup phase and uses a different means to decide when to stop. See its191 section below.192 \end{itemize}193 194 The @exception_class@ argument is a copy of the @exception@'s195 @exception_class@ field.196 197 The @exception@ argument is a pointer to the user provided storage198 object. It has two public fields, the exception class which is actually just199 a number that identifies the exception handling mechanism that created it and200 the other is the clean-up function. The clean-up function is called if the201 exception needs to202 203 The @context@ argument is a pointer to an opaque type. This is passed204 to the many helper functions that can be called inside the personality205 function.206 207 268 \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} 269 Raising an exception is the central function of libunwind and it performs a 270 two-staged unwinding. 271 \begin{cfa} 213 272 _Unwind_Reason_Code _Unwind_RaiseException(_Unwind_Exception *); 273 \end{cfa} 274 First, the function begins the search phase, calling the personality function 275 of the most recent stack frame. It continues to call personality functions 276 traversing the stack from newest to oldest until a function finds a handler or 277 the end of the stack is reached. In the latter case, raise exception returns 278 @_URC_END_OF_STACK@. 279 280 Second, when a handler is matched, raise exception continues onto the cleanup 281 phase. 282 Once again, it calls the personality functions of each stack frame from newest 283 to oldest. This pass stops at the stack frame containing the matching handler. 284 If that personality function has not install a handler, it is an error. 285 286 If an error is encountered, raise exception returns either 287 @_URC_FATAL_PHASE1_ERROR@ or @_URC_FATAL_PHASE2_ERROR@ depending on when the 288 error occurred. 289 290 \subsection{Forced Unwind} 291 \label{s:ForcedUnwind} 292 Forced Unwind is the other central function in libunwind. 293 \begin{cfa} 294 _Unwind_Reason_Code _Unwind_ForcedUnwind( _Unwind_Exception *, 295 _Unwind_Stop_Fn, void *); 296 \end{cfa} 297 It also unwinds the stack but it does not use the search phase. Instead another 298 function, the stop function, is used to stop searching. The exception is the 299 same as the one passed to raise exception. The extra arguments are the stop 300 function and the stop parameter. The stop function has a similar interface as a 301 personality function, except it is also passed the stop parameter. 302 \begin{lstlisting}[language=C,{moredelim=**[is][\color{red}]{@}{@}}] 303 typedef _Unwind_Reason_Code (*@_Unwind_Stop_Fn@)( 304 _Unwind_Action @action@, 305 _Unwind_Exception_Class @exception_class@, 306 _Unwind_Exception * @exception@, 307 struct _Unwind_Context * @context@, 308 void * @stop_parameter@); 214 309 \end{lstlisting} 215 310 216 When called the function begins the search phase, calling the personality217 function of the most recent stack frame. It will continue to call personality218 functions traversing the stack new-to-old until a function finds a handler or219 the end of the stack is reached. In the latter case raise exception will220 return with @_URC_END_OF_STACK@.221 222 Once a handler has been found raise exception continues onto the the cleanup223 phase. Once again it will call the personality functins of each stack frame224 from newest to oldest. This pass will stop at the stack frame that found the225 handler last time, if that personality function does not install the handler226 it is an error.227 228 If an error is encountered raise exception will return either229 @_URC_FATAL_PHASE1_ERROR@ or @_URC_FATAL_PHASE2_ERROR@ depending230 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 it234 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 extra243 arguments are the stop function and the stop parameter. The stop function has244 a similar interface as a personality function, except it is also passed the245 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 257 311 The 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 @_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 @_UA_CLEANUP_PHASE@ and @_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 @_UA_END_OF_STACK@. 312 function is called and then once more after all frames of the stack are 313 unwound. 314 315 Each time it is called, the stop function should return @_URC_NO_REASON@ or 316 transfer control directly to other code outside of libunwind. The framework 317 does not provide any assistance here. 318 319 \begin{sloppypar} 320 Its arguments are the same as the paired personality function. The actions 321 @_UA_CLEANUP_PHASE@ and @_UA_FORCE_UNWIND@ are always set when it is 322 called. Beyond the libunwind standard, both GCC and Clang add an extra action 323 on the last call at the end of the stack: @_UA_END_OF_STACK@. 324 \end{sloppypar} 270 325 271 326 \section{Exception Context} 272 327 % Should I have another independent section? 273 328 % 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 @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 @this_exception_context@. The main exception handling code is in 287 @libcfa@ and that library also defines the function as a weak symbol 288 so it acts as a default. Meanwhile in @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 @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 @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 @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. 329 % stored changes depending on whether or not the thread-library is linked. 330 331 The exception context is global storage used to maintain data across different 332 exception operations and to communicate among different components. 333 334 Each stack must have its own exception context. In a sequential \CFA program, 335 there is only one stack with a single global exception-context. However, when 336 the library @libcfathread@ is linked, there are multiple stacks where each 337 needs its own exception context. 338 339 General access to the exception context is provided by function 340 @this_exception_context@. For sequential execution, this function is defined as 341 a weak symbol in the \CFA system-library, @libcfa@. When a \CFA program is 342 concurrent, it links with @libcfathread@, where this function is defined with a 343 strong symbol replacing the sequential version. 344 345 % The version of the function defined in @libcfa@ is very simple. It returns a 346 % pointer to a global static variable. With only one stack this global instance 347 % is associated with the only stack. 348 349 For coroutines, @this_exception_context@ accesses the exception context stored 350 at the base of the stack. For threads, @this_exception_context@ uses the 351 concurrency library to access the current stack of the thread or coroutine 352 being executed by the thread, and then accesses the exception context stored at 353 the base of this stack. 303 354 304 355 \section{Termination} … … 306 357 % catches. Talk about GCC nested functions. 307 358 308 Termination exceptions use libunwind quite heavily because it matches the309 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 toform the LSDA for try blocks or destructors.359 Termination exceptions use libunwind heavily because it matches the intended 360 use from \CC exceptions closely. The main complication for \CFA is that the 361 compiler generates C code, making it very difficult to generate the assembly to 362 form the LSDA for try blocks or destructors. 312 363 313 364 \subsection{Memory Management} 314 The first step of termination is to copy the exception into memory managed by315 the exception system. Currently the system just uses malloc, without reserved 316 memory or and ``small allocation" optimizations. The exception handling 317 me chanism manages memory for the exception as well as memory for libunwind318 and the system's ownper-exception storage.319 320 Exceptions are stored in variable sized block. The first component is a fixed321 sized data structure that contains the information for libunwind andthe322 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 embedded365 The first step of a termination raise is to copy the exception into memory 366 managed by the exception system. Currently, the system uses @malloc@, rather 367 than reserved memory or the stack top. The exception handling mechanism manages 368 memory for the exception as well as memory for libunwind and the system's own 369 per-exception storage. 370 371 Exceptions are stored in variable-sized blocks. \PAB{Show a memory layout 372 figure.} The first component is a fixed sized data structure that contains the 373 information for libunwind and the exception system. The second component is an 374 area of memory big enough to store the exception. Macros with pointer arthritic 375 and type cast are used to move between the components or go from the embedded 325 376 @_Unwind_Exception@ to the entire node. 326 377 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 @__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. 378 All of these nodes are linked together in a list, one list per stack, with the 379 list head stored in the exception context. Within each linked list, the most 380 recently thrown exception is at the head followed by older thrown 381 exceptions. This format allows exceptions to be thrown, while a different 382 exception is being handled. The exception at the head of the list is currently 383 being handled, while other exceptions wait for the exceptions before them to be 384 removed. 385 386 The virtual members in the exception's virtual table provide the size of the 387 exception, the copy function, and the free function, so they are specific to an 388 exception type. The size and copy function are used immediately to copy an 389 exception into managed memory. After the exception is handled the free function 390 is used to clean up the exception and then the entire node is passed to free. 391 392 \subsection{Try Statements and Catch Clauses} 393 The try statement with termination handlers is complex because it must 394 compensate for the lack of assembly-code generated from \CFA. Libunwind 395 requires an LSDA and personality function for control to unwind across a 396 function. The LSDA in particular is hard to mimic in generated C code. 397 398 The workaround is a function called @__cfaehm_try_terminate@ in the standard 399 library. The contents of a try block and the termination handlers are converted 400 into functions. These are then passed to the try terminate function and it 401 calls them. This approach puts a try statement in its own functions so that no 402 function has to deal with both termination handlers and destructors. \PAB{I do 403 not understand the previous sentence.} 404 405 This function has some custom embedded assembly that defines \emph{its} 406 personality function and LSDA. The assembly is created with handcrafted C @asm@ 407 statements, which is why there is only one version of it. The personality 408 function is structured so that it can be expanded, but currently it only 409 handles this one function. Notably, it does not handle any destructors so the 410 function is constructed so that it does need to run it. \PAB{I do not 411 understand the previous sentence.} 362 412 363 413 The three functions passed to try terminate are: 364 \begin{ itemize}365 \item The try function: This function is the try block, all the code inside366 t he try block is placed inside the try function. It takes no parameters and367 has no return value. This function is called during regular execution to run 368 the tryblock.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 @__cfaehm_try_terminate@ in \CFA use the GCC personality function and 388 the @-fexceptions@ flag to generate the LSDA. This allows destructors 389 t o be implemented with the cleanup attribute.414 \begin{description} 415 \item[try function:] This function is the try block, all the code inside the 416 try block is placed inside the try function. It takes no parameters and has no 417 return value. This function is called during regular execution to run the try 418 block. 419 420 \item[match function:] This function is called during the search phase and 421 decides if a catch clause matches the termination exception. It is constructed 422 from the conditional part of each handler and runs each check, top to bottom, 423 in turn, first checking to see if the exception type matches and then if the 424 condition is true. It takes a pointer to the exception and returns 0 if the 425 exception is not handled here. Otherwise the return value is the id of the 426 handler that matches the exception. 427 428 \item[handler function:] This function handles the exception. It takes a 429 pointer to the exception and the handler's id and returns nothing. It is called 430 after the cleanup phase. It is constructed by stitching together the bodies of 431 each handler and dispatches to the selected handler. 432 \end{description} 433 All three functions are created with GCC nested functions. GCC nested functions 434 can be used to create closures, functions that can refer to the state of other 435 functions on the stack. This approach allows the functions to refer to all the 436 variables in scope for the function containing the @try@ statement. These 437 nested functions and all other functions besides @__cfaehm_try_terminate@ in 438 \CFA use the GCC personality function and the @-fexceptions@ flag to generate 439 the LSDA. This allows destructors to be implemented with the cleanup attribute. 390 440 391 441 \section{Resumption} 392 442 % The stack-local data, the linked list of nodes. 393 443 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 444 Resumption simple to implement because there is no stack unwinding. The 445 resumption raise uses a list of nodes for its stack traversal. The head of the 446 list is stored in the exception context. The nodes in the list have a pointer 396 447 to the next node and a pointer to the handler function. 397 448 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 returns400 true if the exception washandled and false otherwise.401 402 The handler function does both the matching and catching. It tries each403 the condition of @catchResume@ in order, top-to-bottom and until it 404 finds ahandler that matches. If no handler matches then the function returns405 false. Otherwise the matching handler is run , if it completes successfully406 the function returns true. Rethrows, through the @throwResume;@ 407 statement, causethe function to return true.449 A resumption raise traverses this list. At each node the handler function is 450 called, passing the exception by pointer. It returns true if the exception is 451 handled and false otherwise. 452 453 The handler function does both the matching and handling. It computes the 454 condition of each @catchResume@ in top-to-bottom order, until it finds a 455 handler that matches. If no handler matches then the function returns 456 false. Otherwise the matching handler is run; if it completes successfully, the 457 function returns true. Reresume, through the @throwResume;@ statement, cause 458 the function to return true. 408 459 409 460 % Recursive Resumption Stuff: 410 Blocking out part of the stack is accomplished by updating the front of the 411 list as the search continues. Before the handler at a node is called the head 412 of the list is updated to the next node of the current node. After the search 413 is complete, successful or not, the head of the list is reset. 414 415 This means the current handler and every handler that has already been 416 checked are not on the list while a handler is run. If a resumption is thrown 417 during the handling of another resumption the active handlers and all the 418 other handler checked up to this point will not be checked again. 461 Search skipping \see{\VPageref{p:searchskip}}, which ignores parts of the stack 462 already examined, is accomplished by updating the front of the list as the 463 search continues. Before the handler at a node is called the head of the list 464 is updated to the next node of the current node. After the search is complete, 465 successful or not, the head of the list is reset. 466 467 This mechanism means the current handler and every handler that has already 468 been checked are not on the list while a handler is run. If a resumption is 469 thrown during the handling of another resumption the active handlers and all 470 the other handler checked up to this point are not checked again. 419 471 420 472 This structure also supports new handler added while the resumption is being 421 473 handled. These are added to the front of the list, pointing back along the 422 stack -- the first one will point over all the checked handlers -- and the 423 ordering is maintained. 424 425 \subsection{Libunwind Compatibility} 426 Resumption does not use libunwind for two simple reasons. The first is that 427 it does not have to unwind anything so would never need to use the clean-up 428 phase. Still the search phase could be used to make it free to enter or exit 429 a try statement with resumption handlers in the same way termination handlers 430 are for the same trade off in the cost of the throw. This is where the second 431 reason comes in, there is no way to return from a search without installing 432 a handler or raising an error. 433 434 Although work arounds could be created none seemed to be worth it for the 435 prototype. This implementation has no difference in behaviour and is much 436 simpler. 474 stack -- the first one points over all the checked handlers -- and the ordering 475 is maintained. 476 477 \label{p:zero-cost} 478 Note, the resumption implementation has a cost for entering/exiting a @try@ 479 statement with @catchResume@ clauses, whereas a @try@ statement with @catch@ 480 clauses has zero-cost entry/exit. While resumption does not need the stack 481 unwinding and cleanup provided by libunwind, it could use the search phase to 482 providing zero-cost enter/exit using the LSDA. Unfortunately, there is no way 483 to return from a libunwind search without installing a handler or raising an 484 error. Although workarounds might be possible, they are beyond the scope of 485 this thesis. The current resumption implementation has simplicity in its 486 favour. 437 487 % Seriously, just compare the size of the two chapters and then consider 438 488 % that unwind is required knowledge for that chapter. … … 440 490 \section{Finally} 441 491 % Uses destructors and GCC nested functions. 442 Finally clauses are a simple decomposition to some of the existing features. 443 The code in the block is placed into a GCC nested function with a unique name, 444 no arguments or return values. This nested function is then set as the 445 clean-up function of an empty object that is declared at the beginning of a 446 block placed around the contexts of the try statement. 492 Finally clauses is placed into a GCC nested-function with a unique name, and no 493 arguments or return values. This nested function is then set as the cleanup 494 function of an empty object that is declared at the beginning of a block placed 495 around the context of the associated @try@ statement. 447 496 448 497 The rest is handled by GCC. The try block and all handlers are inside the 449 block. When they are complete control exits the block and the empty object450 is cleanedup, which runs the function that contains the finally code.498 block. At completion, control exits the block and the empty object is cleaned 499 up, which runs the function that contains the finally code. 451 500 452 501 \section{Cancellation} … … 454 503 455 504 Cancellation also uses libunwind to do its stack traversal and unwinding, 456 however it uses a different primary function @_Unwind_ForcedUnwind@. 457 Details of its interface can be found in the unwind section.458 459 The first step of cancellation is to find the stack was cancelled and which460 type of stack it is. Luckily the threadslibrary stores the main thread461 pointer and the current thread pointer and every thread stores a pointer to505 however it uses a different primary function @_Unwind_ForcedUnwind@. Details 506 of its interface can be found in the \VRef{s:ForcedUnwind}. 507 508 The first step of cancellation is to find the cancelled stack and its type: 509 coroutine or thread. Fortunately, the thread library stores the main thread 510 pointer and the current thread pointer, and every thread stores a pointer to 462 511 its main coroutine and the coroutine it is currently executing. 463 512 464 So if the the current thread's main and current coroutine do not match, it is 465 a coroutine cancellation. Otherwise if the main and current thread do not 466 match, it is a thread cancellation. Otherwise it is a main thread 467 cancellation. 468 469 However if the threading library is not linked then execution must be on the 470 main stack as that is the only one that exists. So the entire check is skipped 471 using the linker and weak symbols. Instead the main thread cancellation is 472 unconditionally preformed. 473 474 Regardless of how they are choosen afterwords the stop function and the stop 475 parameter are passed to the forced unwind functon. The general pattern of all 476 three stop functions is the same, they continue unwinding until the end of 477 stack when they do there primary work. 478 479 Main stack cancellation it is very simple. The ``transfer" is just an abort, 480 the program stops executing. 481 482 The coroutine cancellation stores the exception on the coroutine and then 483 does a coroutine context switch. The rest is handled inside resume. Every time 484 control returns from a resumed thread there is a check to see if it is 485 cancelled. If it is the exception is retrieved and the CoroutineCancelled 486 exception is constructed and loaded. It is then thrown as a regular exception 487 with the default handler coming from the context of the resumption call. 488 489 The thread cancellation stores the exception on the thread's main stack and 490 then returns to the scheduler. The rest is handled by the joiner. The wait 491 for the joined thread to finish works the same but after that it checks 492 to see if there was a cancellation. If there was the exception is retrieved 493 and the ThreadCancelled exception is constructed. The default handler is 494 passed in as a function pointer. If it is null (as it is for the 495 auto-generated joins on destructor call) it a default is used that simply 496 calls abort; which gives the required handling on implicate join. 513 The first check is if the current thread's main and current coroutine do not 514 match, implying a coroutine cancellation; otherwise, it is a thread 515 cancellation. Otherwise it is a main thread cancellation. \PAB{Previous 516 sentence does not make sense.} 517 518 However, if the threading library is not linked, the sequential execution is on 519 the main stack. Hence, the entire check is skipped because the weak-symbol 520 function is loaded. Therefore, a main thread cancellation is unconditionally 521 performed. 522 523 Regardless of how the stack is chosen, the stop function and parameter are 524 passed to the forced-unwind function. The general pattern of all three stop 525 functions is the same: they continue unwinding until the end of stack when they 526 do there primary work. 527 528 For main stack cancellation, the transfer is just a program abort. 529 530 For coroutine cancellation, the exception is stored on the coroutine's stack, 531 and the coroutine context switches to its last resumer. The rest is handled on 532 the backside of the resume, which check if the resumed coroutine is 533 cancelled. If cancelled, the exception is retrieved from the resumed coroutine, 534 and a @CoroutineCancelled@ exception is constructed and loaded with the 535 cancelled exception. It is then resumed as a regular exception with the default 536 handler coming from the context of the resumption call. 537 538 For thread cancellation, the exception is stored on the thread's main stack and 539 then context switched to the scheduler. The rest is handled by the thread 540 joiner. When the join is complete, the joiner checks if the joined thread is 541 cancelled. If cancelled, the exception is retrieved and the joined thread, and 542 a @ThreadCancelled@ exception is constructed and loaded with the cancelled 543 exception. The default handler is passed in as a function pointer. If it is 544 null (as it is for the auto-generated joins on destructor call), the default is 545 used, which is a program abort. 546 %; which gives the required handling on implicate join. -
doc/theses/andrew_beach_MMath/thesis-frontpgs.tex
rda3963a r565acf59 36 36 37 37 A thesis \\ 38 presented to the University of Waterloo \\ 38 presented to the University of Waterloo \\ 39 39 in fulfillment of the \\ 40 40 thesis requirement for the degree of \\ … … 64 64 \cleardoublepage 65 65 66 66 67 67 %---------------------------------------------------------------------- 68 68 % EXAMINING COMMITTEE (Required for Ph.D. theses only) … … 71 71 \begin{center}\textbf{Examining Committee Membership}\end{center} 72 72 \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 \\ 73 The following served on the Examining Committee for this thesis. The decision 74 of the Examining Committee is by majority vote. 75 \bigskip 76 77 \noindent 78 \begin{tabbing} 79 Internal-External Member: \= \kill % using longest text to define tab length 80 External Examiner: \> Bruce Bruce \\ 80 81 \> Professor, Dept. of Philosophy of Zoology, University of Wallamaloo \\ 81 \end{tabbing} 82 \bigskip 83 82 \end{tabbing} 83 \bigskip 84 84 85 \noindent 85 86 \begin{tabbing} … … 91 92 \end{tabbing} 92 93 \bigskip 93 94 94 95 \noindent 95 96 \begin{tabbing} … … 99 100 \end{tabbing} 100 101 \bigskip 101 102 102 103 \noindent 103 104 \begin{tabbing} … … 107 108 \end{tabbing} 108 109 \bigskip 109 110 110 111 \noindent 111 112 \begin{tabbing} … … 123 124 % December 13th, 2006. It is designed for an electronic thesis. 124 125 \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 126 I hereby declare that I am the sole author of this thesis. This is a true copy 127 of the thesis, including any required final revisions, as accepted by my 128 examiners. 129 130 \bigskip 131 129 132 \noindent 130 133 I understand that my thesis may be made electronically available to the public. -
doc/theses/andrew_beach_MMath/thesis.tex
rda3963a r565acf59 45 45 % FRONT MATERIAL 46 46 %---------------------------------------------------------------------- 47 \input{thesis-frontpgs} 47 \input{thesis-frontpgs} 48 48 49 49 %---------------------------------------------------------------------- … … 65 65 A \gls{computer} could compute $\pi$ all day long. In fact, subsets of digits 66 66 of $\pi$'s decimal approximation would make a good source for psuedo-random 67 vectors, \gls{rvec} . 67 vectors, \gls{rvec} . 68 68 69 69 %---------------------------------------------------------------------- … … 96 96 97 97 \begin{itemize} 98 \item A well-prepared PDF should be 98 \item A well-prepared PDF should be 99 99 \begin{enumerate} 100 100 \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} 103 103 \item Photos must be bit maps, and so are not scaleable by definition. TIFF and 104 104 BMP are uncompressed formats, while JPEG is compressed. Most photos can be 105 105 compressed 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} 107 107 bit 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). 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). 113 113 Programs such as GSView (a Ghostscript GUI) can create both EPS and PDF from 114 114 PS files. Appendix~\ref{AppendixA} shows how to generate properly sized Matlab 115 115 plots and save them as PDF. 116 116 \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 117 you want to appear in your thesis. Scaling photos with the 118 includegraphics command will cause loss of resolution. And scaling down 119 119 drawings may cause any text annotations to become too small. 120 120 \end{itemize} 121 121 122 122 For 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}. 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}. 124 124 \footnote{ 125 125 Note 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} 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} 131 131 commands in your logical document are no longer defined. 132 132 A 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, 134 before the \package{hyperref} package is included. 135 135 The dummy definition is then redifined by the 136 136 \package{hyperref} package when it is included. … … 138 138 139 139 The 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 140 worth a look too, and the many available add-on packages are described by 141 141 Goossens \textit{et al} \cite{goossens.book}. 142 142 … … 180 180 Export Setup button in the figure Property Editor. 181 181 182 \section{From the Command Line} 182 \section{From the Command Line} 183 183 All figure properties can also be manipulated from the command line. Here's an 184 example: 184 example: 185 185 \begin{verbatim} 186 186 x=[0:0.1:pi]; -
doc/theses/andrew_beach_MMath/unwinding.tex
rda3963a r565acf59 1 \chapter{ \texorpdfstring{Unwinding in \CFA}{Unwinding in Cforall}}1 \chapter{Unwinding in \CFA} 2 2 3 3 Stack unwinding is the process of removing stack frames (activations) from the … … 110 110 alternate transfers of control. 111 111 112 \section{\ texorpdfstring{\CFA Implementation}{Cforall Implementation}}112 \section{\CFA Implementation} 113 113 114 114 To use libunwind, \CFA provides several wrappers, its own storage, personality … … 182 182 control has returned to normal control flow. 183 183 184 {\color{blue}PAB:Maybe a diagram would be helpful?}184 \PAB{Maybe a diagram would be helpful?} -
doc/theses/andrew_beach_MMath/uw-ethesis-frontpgs.tex
rda3963a r565acf59 13 13 \vspace*{1.0cm} 14 14 15 \Huge 16 {\bf Exception Handling in \CFA} 15 {\Huge\bf Exception Handling in \CFA} 17 16 18 17 \vspace*{1.0cm} 19 18 20 \normalsize21 19 by \\ 22 20 23 21 \vspace*{1.0cm} 24 22 25 \Large 26 Andrew James Beach \\ 23 {\Large Andrew James Beach} \\ 27 24 28 25 \vspace*{3.0cm} 29 26 30 \normalsize31 27 A thesis \\ 32 presented to the University of Waterloo \\ 28 presented to the University of Waterloo \\ 33 29 in fulfillment of the \\ 34 30 thesis requirement for the degree of \\ … … 43 39 \vspace*{1.0cm} 44 40 45 \copyright \Andrew James Beach \the\year \\41 \copyright{} Andrew James Beach \the\year \\ 46 42 \end{center} 47 43 \end{titlepage} 48 44 49 % The rest of the front pages should contain no headers and be numbered using Roman numerals starting with `ii' 45 % The rest of the front pages should contain no headers and be numbered using 46 % Roman numerals starting with `ii'. 50 47 \pagestyle{plain} 51 48 \setcounter{page}{2} 52 49 53 \cleardoublepage % Ends the current page and causes all figures and tables that have so far appeared in the input to be printed. 54 % In a two-sided printing style, it also makes the next page a right-hand (odd-numbered) page, producing a blank page if necessary. 50 \cleardoublepage % Ends the current page and causes all figures and tables 51 % that have so far appeared in the input to be printed. In a two-sided 52 % printing style, it also makes the next page a right-hand (odd-numbered) 53 % page, producing a blank page if necessary. 55 54 56 \begin{comment} 55 \begin{comment} 57 56 % E X A M I N I N G C O M M I T T E E (Required for Ph.D. theses only) 58 57 % Remove or comment out the lines below to remove this page 59 58 \begin{center}\textbf{Examining Committee Membership}\end{center} 60 59 \noindent 61 The following served on the Examining Committee for this thesis. The decision of the Examining Committee is by majority vote. 60 The following served on the Examining Committee for this thesis. 61 The decision of the Examining Committee is by majority vote. 62 62 \bigskip 63 63 64 64 \noindent 65 65 \begin{tabbing} 66 66 Internal-External Member: \= \kill % using longest text to define tab length 67 External Examiner: \> Bruce Bruce \\ 67 External Examiner: \> Bruce Bruce \\ 68 68 \> Professor, Dept. of Philosophy of Zoology, University of Wallamaloo \\ 69 \end{tabbing} 69 \end{tabbing} 70 70 \bigskip 71 71 72 72 \noindent 73 73 \begin{tabbing} … … 79 79 \end{tabbing} 80 80 \bigskip 81 81 82 82 \noindent 83 83 \begin{tabbing} … … 87 87 \end{tabbing} 88 88 \bigskip 89 89 90 90 \noindent 91 91 \begin{tabbing} … … 95 95 \end{tabbing} 96 96 \bigskip 97 97 98 98 \noindent 99 99 \begin{tabbing} … … 111 111 % December 13th, 2006. It is designed for an electronic thesis. 112 112 \begin{center}\textbf{Author's Declaration}\end{center} 113 113 114 114 \noindent 115 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. 115 I hereby declare that I am the sole author of this thesis. This is a true copy 116 of the thesis, including any required final revisions, as accepted by my 117 examiners. 116 118 117 119 \bigskip 118 120 119 121 \noindent 120 122 I understand that my thesis may be made electronically available to the public. -
doc/theses/andrew_beach_MMath/uw-ethesis.tex
rda3963a r565acf59 1 1 %====================================================================== 2 % University of Waterloo Thesis Template for LaTeX 3 % Last Updated November, 2020 4 % by Stephen Carr, IST Client Services, 2 % University of Waterloo Thesis Template for LaTeX 3 % Last Updated November, 2020 4 % by Stephen Carr, IST Client Services, 5 5 % University of Waterloo, 200 University Ave. W., Waterloo, Ontario, Canada 6 6 % FOR ASSISTANCE, please send mail to request@uwaterloo.ca 7 7 8 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. 9 % To the best of our knowledge, this template satisfies the current uWaterloo 10 % thesis requirements. However, it is your responsibility to assure that you 11 % have met all requirements of the University and your particular department. 12 13 % Many thanks for the feedback from many graduates who assisted the 14 % development of this template. Also note that there are explanatory comments 15 % and tips throughout this template. 14 16 %====================================================================== 15 17 % Some important notes on using this template and making it your own... 16 18 17 % The University of Waterloo has required electronic thesis submission since October 2006. 18 % See the uWaterloo thesis regulations at 19 % https://uwaterloo.ca/graduate-studies/thesis. 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. 19 % The University of Waterloo has required electronic thesis submission since 20 % October 2006. See the uWaterloo thesis regulations at: 21 % https://uwaterloo.ca/graduate-studies/thesis. 22 % This thesis template is geared towards generating a PDF version optimized 23 % for viewing on an electronic display, including hyperlinks within the PDF. 24 25 % DON'T FORGET TO ADD YOUR OWN NAME AND TITLE in the "hyperref" package 26 % configuration below. THIS INFORMATION GETS EMBEDDED IN THE FINAL PDF 27 % DOCUMENT. You can view the information if you view properties of the PDF. 28 29 % Many faculties/departments also require one or more printed copies. 30 % This template attempts to satisfy both types of output. 28 31 % 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 32 % It is based on the standard "book" document class which provides all 33 % necessary sectioning structures and allows multi-part theses. 34 35 % If you are using this template in Overleaf (cloud-based collaboration 36 % service), then it is automatically processed and previewed for you as you 37 % edit. 38 39 % For people who prefer to install their own LaTeX distributions on their own 40 % computers, and process the source files manually, the following notes 41 % provide the sequence of tasks: 42 35 43 % E.g. to process a thesis called "mythesis.tex" based on this template, run: 36 44 37 45 % pdflatex mythesis -- first pass of the pdflatex processor 38 46 % bibtex mythesis -- generates bibliography from .bib data file(s) 39 % makeindex -- should be run only if an index is used 40 % pdflatex mythesis -- fixes numbering in cross-references, bibliographic references, glossaries, index, etc. 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 46 % the last two times. 47 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. 47 % makeindex -- should be run only if an index is used 48 % pdflatex mythesis -- fixes numbering in cross-references, bibliographic 49 % references, glossaries, index, etc. 50 % pdflatex mythesis -- it takes a couple of passes to completely process all 51 % cross-references 52 53 % If you use the recommended LaTeX editor, Texmaker, you would open the 54 % mythesis.tex file, then click the PDFLaTeX button. Then run BibTeX (under 55 % the Tools menu). Then click the PDFLaTeX button two more times. 56 % If you have an index as well, you'll need to run MakeIndex from the Tools 57 % menu as well, before running pdflatex the last two times. 58 59 % N.B. The "pdftex" program allows graphics in the following formats to be 60 % included with the "\includegraphics" command: PNG, PDF, JPEG, TIFF 61 % Tip: Generate your figures and photos in the size you want them to appear 62 % in your thesis, rather than scaling them with \includegraphics options. 63 % Tip: Any drawings you do should be in scalable vector graphic formats: SVG, 64 % PNG, WMF, EPS and then converted to PNG or PDF, so they are scalable in the 65 % final PDF as well. 51 66 % Tip: Photographs should be cropped and compressed so as not to be too large. 52 67 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 %====================================================================== 68 % To create a PDF output that is optimized for double-sided printing: 69 % 1) comment-out the \documentclass statement in the preamble below, and 70 % un-comment the second \documentclass line. 71 % 2) change the value assigned below to the boolean variable "PrintVersion" 72 % from "false" to "true". 73 74 % ====================================================================== 58 75 % D O C U M E N T P R E A M B L E 59 % Specify the document class, default style attributes, andpage dimensions, etc.76 % Specify the document class, default style attributes, page dimensions, etc. 60 77 % For hyperlinked PDF, suitable for viewing on a computer, use this: 61 78 \documentclass[letterpaper,12pt,titlepage,oneside,final]{book} 62 79 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: 80 % For PDF, suitable for double-sided printing, change the PrintVersion 81 % variable below to "true" and use this \documentclass line instead of the 82 % one above: 64 83 %\documentclass[letterpaper,12pt,titlepage,openright,twoside,final]{book} 65 84 85 \usepackage{etoolbox} 86 66 87 % 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! 88 % If you have to, it's easier to make changes to nomenclature once here than 89 % in a million places throughout your thesis! 68 90 \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 command91 \newcommand{\cmmd}[1]{\textbackslash\texttt{#1}} % command name in tt font 92 \newcommand{\href}[1]{#1} % does nothing, but defines the command so the 93 % print-optimized version will ignore \href tags (redefined by hyperref pkg). 72 94 % Anything defined here may be redefined by packages added below... 73 95 … … 76 98 \newboolean{PrintVersion} 77 99 \setboolean{PrintVersion}{false} 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. 100 % CHANGE THIS VALUE TO "true" as necessary, to improve printed results for 101 % hard copies by overriding some options of the hyperref package, called below. 79 102 80 103 %\usepackage{nomencl} % For a nomenclature (optional; available from ctan.org) 81 \usepackage{amsmath,amssymb,amstext} % Lots of math symbols and environments 82 \usepackage[pdftex]{graphicx} % For including graphics N.B. pdftex graphics driver 104 % Lots of math symbols and environments 105 \usepackage{amsmath,amssymb,amstext} 106 % For including graphics N.B. pdftex graphics driver 107 \usepackage[pdftex]{graphicx} 108 % Removes large sections of the document. 109 \usepackage{comment} 83 110 84 111 % Hyperlinks make it very easy to navigate an electronic document. 85 % In addition, this is where you should specify the thesis title and author as they appear in the properties of the PDF document. 112 % In addition, this is where you should specify the thesis title and author as 113 % they appear in the properties of the PDF document. 86 114 % Use the "hyperref" package 87 115 % N.B. HYPERREF MUST BE THE LAST PACKAGE LOADED; ADD ADDITIONAL PKGS ABOVE 88 116 \usepackage[pdftex,pagebackref=true]{hyperref} % with basic options 89 117 %\usepackage[pdftex,pagebackref=true]{hyperref} 90 % N.B. pagebackref=true provides links back from the References to the body text. This can cause trouble for printing. 118 % N.B. pagebackref=true provides links back from the References to the body 119 % text. This can cause trouble for printing. 91 120 \hypersetup{ 92 121 plainpages=false, % needed if Roman numbers in frontpages 93 unicode=false, % non-Latin characters in Acrobat ’s bookmarks94 pdftoolbar=true, % show Acrobat ’s toolbar?95 pdfmenubar=true, % show Acrobat ’s menu?122 unicode=false, % non-Latin characters in Acrobat's bookmarks 123 pdftoolbar=true, % show Acrobat's toolbar? 124 pdfmenubar=true, % show Acrobat's menu? 96 125 pdffitwindow=false, % window fit to page when opened 97 126 pdfstartview={FitH}, % fits the width of the page to the window 98 % pdftitle={uWaterloo\ LaTeX\ Thesis\ Template}, 127 % pdftitle={uWaterloo\ LaTeX\ Thesis\ Template}, % title: CHANGE THIS TEXT! 99 128 % pdfauthor={Author}, % author: CHANGE THIS TEXT! and uncomment this line 100 129 % pdfsubject={Subject}, % subject: CHANGE THIS TEXT! and uncomment this line 101 % pdfkeywords={keyword1} {key2} {key3}, % list of keywords, and uncomment this line if desired130 % pdfkeywords={keyword1} {key2} {key3}, % optional list of keywords 102 131 pdfnewwindow=true, % links in new window 103 132 colorlinks=true, % false: boxed links; true: colored links … … 107 136 urlcolor=cyan % color of external links 108 137 } 109 \ifthenelse{\boolean{PrintVersion}}{ % for improved print quality, change some hyperref options 138 % for improved print quality, change some hyperref options 139 \ifthenelse{\boolean{PrintVersion}}{ 110 140 \hypersetup{ % override some previously defined hyperref options 111 141 % colorlinks,% … … 116 146 }{} % end of ifthenelse (no else) 117 147 118 \usepackage[automake,toc,abbreviations]{glossaries-extra} % Exception to the rule of hyperref being the last add-on package 119 % If glossaries-extra is not in your LaTeX distribution, get it from CTAN (http://ctan.org/pkg/glossaries-extra), 120 % although it's supposed to be in both the TeX Live and MikTeX distributions. There are also documentation and 121 % installation instructions there. 148 % Exception to the rule of hyperref being the last add-on package 149 \usepackage[automake,toc,abbreviations]{glossaries-extra} 150 % If glossaries-extra is not in your LaTeX distribution, get it from CTAN 151 % (http://ctan.org/pkg/glossaries-extra), although it's supposed to be in 152 % both the TeX Live and MikTeX distributions. There are also documentation 153 % and installation instructions there. 122 154 123 155 % Setting up the page margins... 124 \setlength{\textheight}{9in}\setlength{\topmargin}{-0.45in}\setlength{\headsep}{0.25in} 125 % uWaterloo thesis requirements specify a minimum of 1 inch (72pt) margin at the 126 % top, bottom, and outside page edges and a 1.125 in. (81pt) gutter margin (on binding side). 127 % 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. 128 % Set margins to minimum permitted by uWaterloo thesis regulations: 156 \setlength{\textheight}{9in} 157 \setlength{\topmargin}{-0.45in} 158 \setlength{\headsep}{0.25in} 159 % uWaterloo thesis requirements specify a minimum of 1 inch (72pt) margin at 160 % the top, bottom, and outside page edges and a 1.125 in. (81pt) gutter margin 161 % (on binding side). While this is not an issue for electronic viewing, a PDF 162 % may be printed, and so we have the same page layout for both printed and 163 % electronic versions, we leave the gutter margin in. Set margins to minimum 164 % permitted by uWaterloo thesis regulations: 129 165 \setlength{\marginparwidth}{0pt} % width of margin notes 130 166 % N.B. If margin notes are used, you must adjust \textwidth, \marginparwidth 131 167 % and \marginparsep so that the space left between the margin notes and page 132 168 % edge is less than 15 mm (0.6 in.) 133 \setlength{\marginparsep}{0pt} % width of space between body text and margin notes 134 \setlength{\evensidemargin}{0.125in} % Adds 1/8 in. to binding side of all 169 % width of space between body text and margin notes 170 \setlength{\marginparsep}{0pt} 171 % Adds 1/8 in. to binding side of all 135 172 % even-numbered pages when the "twoside" printing option is selected 136 \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 137 \setlength{\textwidth}{6.375in} % assuming US letter paper (8.5 in. x 11 in.) and side margins as above 173 \setlength{\evensidemargin}{0.125in} 174 % Adds 1/8 in. to the left of all pages when "oneside" printing is selected, 175 % and to the left of all odd-numbered pages when "twoside" printing is selected 176 \setlength{\oddsidemargin}{0.125in} 177 % assuming US letter paper (8.5 in. x 11 in.) and side margins as above 178 \setlength{\textwidth}{6.375in} 138 179 \raggedbottom 139 180 140 % The following statement specifies the amount of space between paragraphs. Other reasonable specifications are \bigskipamount and \smallskipamount. 181 % The following statement specifies the amount of space between paragraphs. 182 % Other reasonable specifications are \bigskipamount and \smallskipamount. 141 183 \setlength{\parskip}{\medskipamount} 142 184 143 % The following statement controls the line spacing. 144 % The default spacing corresponds to good typographic conventions and only slight changes (e.g., perhaps "1.2"), if any, should be made. 185 % The following statement controls the line spacing. 186 % The default spacing corresponds to good typographic conventions and only 187 % slight changes (e.g., perhaps "1.2"), if any, should be made. 145 188 \renewcommand{\baselinestretch}{1} % this is the default line space setting 146 189 147 190 % By default, each chapter will start on a recto (right-hand side) page. 148 % We also force each section of the front pages to start on a recto page by inserting \cleardoublepage commands. 149 % 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. 150 % The following statements ensure a page number is not printed on an otherwise blank verso page. 191 % We also force each section of the front pages to start on a recto page by 192 % inserting \cleardoublepage commands. In many cases, this will require that 193 % the verso (left-hand) page be blank, and while it should be counted, a page 194 % number should not be printed. The following statements ensure a page number 195 % is not printed on an otherwise blank verso page. 151 196 \let\origdoublepage\cleardoublepage 152 197 \newcommand{\clearemptydoublepage}{% … … 154 199 \let\cleardoublepage\clearemptydoublepage 155 200 156 % Define Glossary terms (This is properly done here, in the preamble and could also be \input{} from a separate file...) 201 % Define Glossary terms (This is properly done here, in the preamble and 202 % could also be \input{} from a separate file...) 157 203 \input{glossaries} 158 204 \makeglossaries 159 205 160 \usepackage{comment}161 206 % cfa macros used in the document 162 207 %\usepackage{cfalab} 208 % I'm going to bring back eventually. 209 \makeatletter 210 % Combines all \CC* commands: 211 \newrobustcmd*\Cpp[1][\xspace]{\cfalab@Cpp#1} 212 \newcommand\cfalab@Cpp{C\kern-.1em\hbox{+\kern-.25em+}} 213 % Optional arguments do not work with pdf string. (Some fix-up required.) 214 \pdfstringdefDisableCommands{\def\Cpp{C++}} 215 \makeatother 216 163 217 \input{common} 164 \CFAStyle % CFA code-style for all languages 165 \lstset{language=CFA,basicstyle=\linespread{0.9}\tt} % CFA default lnaguage 218 % CFA code-style for all languages 219 \CFAStyle 220 % CFA default lnaguage 221 \lstset{language=CFA,basicstyle=\linespread{0.9}\tt} 222 % Annotations from Peter: 223 \newcommand{\PAB}[1]{{\color{blue}PAB: #1}} 224 % Change the style of abbreviations: 225 \renewcommand{\abbrevFont}{} 166 226 167 227 %====================================================================== 168 228 % L O G I C A L D O C U M E N T 169 229 % The logical document contains the main content of your thesis. 170 % 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. 230 % Being a large document, it is a good idea to divide your thesis into several 231 % files, each one containing one chapter or other significant chunk of content, 232 % so you can easily shuffle things around later if desired. 171 233 %====================================================================== 172 234 \begin{document} … … 175 237 % FRONT MATERIAL 176 238 % title page,declaration, borrowers' page, abstract, acknowledgements, 177 % dedication, table of contents, list of tables, list of figures, nomenclature, etc. 178 %---------------------------------------------------------------------- 179 \input{uw-ethesis-frontpgs} 239 % dedication, table of contents, list of tables, list of figures, 240 % nomenclature, etc. 241 %---------------------------------------------------------------------- 242 \input{uw-ethesis-frontpgs} 180 243 181 244 %---------------------------------------------------------------------- 182 245 % MAIN BODY 183 246 % We suggest using a separate file for each chapter of your thesis. 184 % Start each chapter file with the \chapter command. 185 % Only use \documentclass or\begin{document} and \end{document} commands in this master document.247 % Start each chapter file with the \chapter command. Only use \documentclass, 248 % \begin{document} and \end{document} commands in this master document. 186 249 % Tip: Putting each sentence on a new line is a way to simplify later editing. 187 250 %---------------------------------------------------------------------- 188 251 \input{existing} 189 252 \input{features} 190 \input{unwinding} 253 \input{implement} 254 %\input{unwinding} 191 255 \input{future} 192 256 … … 198 262 % Bibliography 199 263 200 % The following statement selects the style to use for references. 201 % It controls the sort order of the entries in the bibliography and also the formatting for the in-text labels. 264 % The following statement selects the style to use for references. 265 % It controls the sort order of the entries in the bibliography and also the 266 % formatting for the in-text labels. 202 267 \bibliographystyle{plain} 203 % This specifies the location of the file containing the bibliographic information. 204 % It assumes you're using BibTeX to manage your references (if not, why not?). 205 \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. 206 % Use \clearpage instead if the document class uses the "oneside" argument 207 \phantomsection % With hyperref package, enables hyperlinking from the table of contents to bibliography 208 % The following statement causes the title "References" to be used for the bibliography section: 268 % This specifies the location of the file containing the bibliographic 269 % information. It assumes you're using BibTeX to manage your references (if 270 % not, why not?). 271 \cleardoublepage % This is needed if the "book" document class is used, to 272 % place the anchor in the correct page, because the bibliography will start 273 % on its own page. 274 % Use \clearpage instead if the document class uses the "oneside" argument. 275 \phantomsection % With hyperref package, enables hyperlinking from the table 276 % of contents to bibliography. 277 % The following statement causes the title "References" to be used for the 278 % bibliography section: 209 279 \renewcommand*{\bibname}{References} 210 280 … … 213 283 214 284 \bibliography{uw-ethesis,pl} 215 % Tip: You can create multiple .bib files to organize your references. 216 % Just list them all in the \bibliogaphy command, separated by commas (no spaces). 217 218 % The following statement causes the specified references to be added to the bibliography even if they were not cited in the text. 219 % The asterisk is a wildcard that causes all entries in the bibliographic database to be included (optional). 285 % Tip: You can create multiple .bib files to organize your references. Just 286 % list them all in the \bibliogaphy command, separated by commas (no spaces). 287 288 % The following statement causes the specified references to be added to the 289 % bibliography even if they were not cited in the text. The asterisk is a 290 % wildcard that causes all entries in the bibliographic database to be 291 % included (optional). 220 292 % \nocite{*} 221 293 %---------------------------------------------------------------------- … … 225 297 % The \appendix statement indicates the beginning of the appendices. 226 298 \appendix 227 % Add an un-numbered title page before the appendices and a line in the Table of Contents 299 % Add an un-numbered title page before the appendices and a line in the Table 300 % of Contents 228 301 % \chapter*{APPENDICES} 229 302 % \addcontentsline{toc}{chapter}{APPENDICES} 230 % Appendices are just more chapters, with different labeling (letters instead of numbers). 303 % Appendices are just more chapters, with different labeling (letters instead 304 % of numbers). 231 305 % \input{appendix-matlab_plots.tex} 232 306 233 % GLOSSARIES (Lists of definitions, abbreviations, symbols, etc. provided by the glossaries-extra package) 307 % GLOSSARIES (Lists of definitions, abbreviations, symbols, etc. 308 % provided by the glossaries-extra package) 234 309 % ----------------------------- 235 310 \printglossaries -
doc/theses/fangren_yu_COOP_F20/Report.tex
rda3963a r565acf59 102 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 103 104 The 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 amultiple 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.104 The 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 105 106 106 This 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. … … 122 122 \end{itemize} 123 123 124 The resolver algorithm, designed for overload resolution, uses a significant amount ofreused, and hence copying, for the intermediate representations, especially in the following two places:124 The 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 125 \begin{itemize} 126 126 \item … … 301 301 forall( dtype T | sized( T ) ) 302 302 T * malloc( void ) { return (T *)malloc( sizeof(T) ); } // call C malloc 303 int * i = malloc(); // type deduced from left-hand size $\ Rightarrow$ no size argument or return cast303 int * i = malloc(); // type deduced from left-hand size $\(\Rightarrow\)$ no size argument or return cast 304 304 \end{cfa} 305 305 An 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}): … … 432 432 \begin{cfa} 433 433 void f( int ); 434 double g$ _1$( int );435 int g$ _2$( long );434 double g$\(_1\)$( int ); 435 int g$\(_2\)$( long ); 436 436 f( g( 42 ) ); 437 437 \end{cfa} -
doc/theses/thierry_delisle_PhD/thesis/Makefile
rda3963a r565acf59 32 32 emptytree \ 33 33 fairness \ 34 io_uring \ 35 pivot_ring \ 34 36 system \ 35 37 } … … 43 45 ## Define the documents that need to be made. 44 46 all: thesis.pdf 45 thesis.pdf: ${TEXTS} ${FIGURES} ${PICTURES} glossary.tex local.bib47 thesis.pdf: ${TEXTS} ${FIGURES} ${PICTURES} thesis.tex glossary.tex local.bib 46 48 47 49 DOCUMENT = thesis.pdf … … 105 107 sed -i 's/$@/${Build}\/$@/g' ${Build}/$@_t 106 108 109 build/fairness.svg: fig/fairness.py | ${Build} 110 python3 fig/fairness.py build/fairness.svg 111 107 112 ## pstex with inverted colors 108 113 %.dark.pstex : fig/%.fig Makefile | ${Build} -
doc/theses/thierry_delisle_PhD/thesis/local.bib
rda3963a r565acf59 512 512 } 513 513 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 514 521 % Apple's MAC OS X 515 522 @manual{MAN:apple/scheduler, … … 577 584 578 585 % -------------------------------------------------- 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 % -------------------------------------------------- 579 639 % Wikipedia Entries 580 640 @misc{wiki:taskparallel, … … 617 677 note = "[Online; accessed 2-January-2021]" 618 678 } 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
rda3963a r565acf59 49 49 50 50 \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.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. 52 52 53 53 \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 celltimestamp. 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.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. 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. 55 55 56 56 \begin{figure} … … 100 100 \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. 101 101 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 assharded 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.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. 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. 103 103 104 104 \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.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 $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. 106 106 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.107 To 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. 108 108 109 109 The algorithm works as follows: -
doc/theses/thierry_delisle_PhD/thesis/text/intro.tex
rda3963a r565acf59 7 7 While 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}. 8 8 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 tha tthe latest version is not a goal of this work.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 than the latest version is not a goal of this work. -
doc/theses/thierry_delisle_PhD/thesis/text/io.tex
rda3963a r565acf59 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} 2 As mentionned 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. Various operating systems offer various forms of asynchronous operations and as mentioned in Chapter~\ref{intro}, this work is exclusively focuesd on Linux. 3 3 4 \section{ Existing options}5 Since \glsxtrshort{io} operations are generally handled by the4 \section{Kernel Interface} 5 Since 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 \io subsystem. 6 6 7 \subsection{\lstinline|epoll|, \lstinline|poll| and \lstinline|select|} 7 \subsection{\lstinline|O_NONBLOCK|} 8 In 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 9 process to wait.'' This feature can be used as the foundation for the \io subsystem. However, for the subsystem to be able to block \glspl{thrd} until an 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 will not cause the process to wait\footnote{In this context, ready means to \emph{some} operation can be performed without blocking. It does not mean that the last operation that return \lstinline|EAGAIN| will succeed on the next try. A file that is ready to read but has only 1 byte available would be an example of this distinction.}. 8 10 9 \subsection{Linux's AIO} 11 There are three options to monitor file descriptors in Linux\footnote{For simplicity, this section omits to mention \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 descriptor becomes ready. The group of file descriptors being waited on is often referred to as the \newterm{interest set}. 10 12 13 \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 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| in this case is possible but requires a distinct kernel thread, and as a result is not a acceptable multiplexing solution when the interest set is large and highly dynamic unless the number of parallel calls to select can be strictly bounded.}. 11 14 15 \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. 16 17 \paragraph{\lstinline|epoll|} further improves on these two functions, by allowing the interest set to be dynamically added to and removed from while a \gls{kthrd} is blocked on a call to @epoll@. This is done by creating an \emph{epoll instance} with a persistent intereset set and that is used across multiple calls. This advantage 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@. 18 19 However, all three of these system calls suffer from generality problems to some extent. 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 some 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. 20 21 \subsection{The POSIX asynchronous I/O (AIO)} 22 An alternative to using @O_NONBLOCK@ is to use 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 sending a Linux signal, spawning a new \gls{kthrd} or by polling for completion of one or more operation. For the purpose multiplexing operations, spawning a new \gls{kthrd} is counter-productive but a related solution is discussed in Section~\ref{io:morethreads}. Since using interrupts handlers can also lead to fairly complicated interactions between subsystems, I will concentrate on the different polling methods. AIO only supports read and write operations to file descriptors and those do not have the same limitation as @O_NONBLOCK@, \ie, the file descriptors can be regular files and blocked devices. It also supports batching more than one of these operations in a single system call. 23 24 AIO 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 intended interface. 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. Unlike @select@ and @poll@ however, it also suffers from the limitation that it does not specify which requests have completed, meaning programmers then have to poll each request in the interest set using @aio_error@ to identify which requests have completed. This means that, like @select@ and @poll@ but not @epoll@, the time needed to examine polling results increases based in the total number of requests monitored, not the number of completed requests. 25 26 AIO does not seem to be a particularly popular interface, which I believe is in part due to this less than ideal polling interface. Linus Torvalds talks about this interface as follows : 12 27 13 28 \begin{displayquote} … … 30 45 in 31 46 ``some kind of arbitrary \textit{queue up asynchronous system call} model''. 32 This description is actually quite close to the interface of the interfacedescribed in the next section.47 This description is actually quite close to the interface described in the next section. 33 48 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|} 50 A very recent addition to Linux, @io_uring@\cite{MAN:io_uring} is a framework that aims to solve many of the problems listed with the above mentioned interfaces. Like AIO, it represents \io operations as entries added on 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) as its core, a submit ring to which programmers push \io requests and a completion buffer which programmers poll for completion. 51 52 One of the big advantages over the interfaces listed above is that it 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 54 On top of these, @io_uring@ adds many ``bells and whistles'' like avoiding copies between the kernel and user-space with shared memory, allowing different mechanisms to communicate with device drivers and supporting chains of requests, \ie, requests that automatically trigger followup requests on completion. 36 55 37 56 \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}.57 Finally, if the operating system does not offer any satisfying forms of asynchronous \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}. The is a compromise on 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 an homogenous \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. 39 58 40 59 \subsection{Discussion} 60 These options effectively fall into two broad camps of solutions, waiting for \io to be ready versus waiting for \io to be completed. All operating systems that support asynchronous \io must offer an interface along one of these lines, but the details can vary drastically. For example, Free BSD offers @kqueue@~\cite{MAN:bsd/kqueue} which behaves similarly to @epoll@ but with some small quality of life 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@. 41 61 62 For this project, I have chosen to use @io_uring@, in large parts due to its generality. While @epoll@ has been shown to be a good solution to 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. 42 63 43 64 \section{Event-Engine} 44 65 66 The event engines reponsibility is to use the kernel interface to multiplex many \io operations onto few \glspl{kthrd}. In concrete terms, this means that \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. 67 68 \subsection{\lstinline|io_uring| in depth} 69 Before going into details on the design of the event engine, I will present some more details on the usage of @io_uring@ which are important for the design of the engine. 70 71 \begin{figure} 72 \centering 73 \input{io_uring.pstex_t} 74 \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.} 75 \label{fig:iouring} 76 \end{figure} 77 78 Figure~\ref{fig:iouring} shows an overview of an @io_uring@ instance. Multiple @io_uring@ instances can be created, in which case they each have a copy of the data structures in the figure. New \io operations are submitted to the kernel following 4 steps which use the components shown in the figure. 79 80 \paragraph{First} an @sqe@ must be 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, which means it is both visible by the kernel and the application, and has a fixed size determined at creation. How these entries are allocated is not important for the functionning of @io_uring@, the only requirement is that no entry is reused before the kernel has consumed it. 81 82 \paragraph{Secondly} the @sqe@ must be filled according to the desired operation. This step is straight forward, the only detail worth mentionning is that @sqe@s have a @user_data@ field that must be filled in order to match submission and completion entries. 83 84 \paragraph{Thirdly} the @sqe@ must be submitted to the submission ring, this requires 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 can be updated only after the entire batch has been updated. 85 86 \paragraph{Finally} the kernel must be 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 that this step is not required, but this requires elevated privilege and early version of @io_uring@ had additionnal restrictions. 87 88 The 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 @sqe@s 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, the kernel can directly modify the completion ring, the system call is only needed if the application wants to block waiting on operations to complete. 89 90 The @io_uring_enter@ system call is protected by a lock inside the kernel. This means that concurrent call to @io_uring_enter@ using the same instance are possible, but there is can be 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. 91 92 @io_uring@ also introduces some constraints on what the number of operations that can be ``in flight'' at the same time. Obviously, @sqe@s are allocated from a fixed-size array, meaning that there is a hard limit to how many @sqe@s 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 requirement means that it can be required to handle bursts of \io requests by holding back some of the requests so they can be submitted at a later time. 93 94 \subsection{Multiplexing \io: Submission} 95 The submission side is the most complicated aspect of @io_uring@ and from the design decisions made in the submission side, the completion side effectively follows. 96 97 While 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 of this is that how much parallelism can be used to prepare submissions for the next system call is limited. Beyond this limit, the length of the system call will be the throughput limiting factor. I have 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}. 98 99 \subsubsection{Pool of Instances} 100 One 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 continously\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. 101 102 Allocation in this scheme can be handled fairly easily. Free @sqe@s, \ie, @sqe@s 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 @cqe@s. Allocation also requires no ordering guarantee as all free @sqe@s are interchangeable. This requires a simple concurrent bag. The only added complexity is that the number of @sqe@s 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 @sqe@s. Ideally, the routing algorithm would block operations up-front if none of the instances have available @sqe@s. 103 104 Once an @sqe@ is allocated, \glspl{thrd} can fill them normally, they simply need to keep trac of the @sqe@ index and which instance it belongs to. 105 106 Once 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 @sqe@s 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 @sqe@s or by having some other party regularly submitting all ready @sqe@s, \eg, the poller \gls{thrd} mentionned later in this section. 107 108 In 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 indefinately 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 @sqe@s so that the allocator can reused them. 109 110 Finally, 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 @cqe@s and communicate the result back to the originating \glspl{thrd}. Since @cqe@s 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 @sqe@s 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 convinient for reasons explained in Chapter~\ref{practice}. 111 112 With 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 ressources, @sqe@s 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 @sqe@s, block incoming requests if no instance is available, prevent barging if \glspl{thrd} are already queued up waiting for @sqe@s and handle @sqe@s being freed. The submission side needs to safely append @sqe@s to the ring buffer, make sure no @sqe@ is dropped or left pending forever, notify the allocation side when @sqe@s 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. 113 114 \subsubsection{Private Instances} 115 Another 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 @sqe@s that are allocated must be submitted to the same ring, on the same \gls{proc}, this effectively forces the application to submit @sqe@s 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 partitionned 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. 116 117 \begin{figure} 118 \centering 119 \input{pivot_ring.pstex_t} 120 \caption[Partitionned ring buffer]{Partitionned 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.} 121 \label{fig:pring} 122 \end{figure} 123 124 This 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 @sqe@s 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. 125 126 45 127 46 128 \section{Interface} 129 Finally, 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 : 130 131 @ssize_t read(int fd, void *buf, size_t count);@. 132 133 \subsection{Replacement} 134 Replacing the C \glsxtrshort{api} 135 136 \subsection{Synchronous Extension} 137 138 \subsection{Asynchronous Extension} 139 140 \subsection{Interface directly to \lstinline|io_uring|} -
doc/theses/thierry_delisle_PhD/thesis/text/runtime.tex
rda3963a r565acf59 11 11 12 12 \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 bescheduled 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.}. 14 14 15 15 \begin{figure} … … 25 25 26 26 \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: 27 Prior 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 29 29 \begin{quote} 30 30 Given 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.}. 31 31 \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.33 32 34 \section{Interoperating with C} 33 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 \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}} 35 36 While \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}: 36 37 \begin{quote} … … 44 45 \begin{enumerate} 45 46 \item Precisely identifying blocking C calls is difficult. 46 \item Introducing newcode can have a significant impact on general performance.47 \item Introducing control points code can have a significant impact on general performance. 47 48 \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 toblock 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.49 Because 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
rda3963a r565acf59 81 81 %\usepackage{nomencl} % For a nomenclature (optional; available from ctan.org) 82 82 \usepackage{amsmath,amssymb,amstext} % Lots of math symbols and environments 83 \usepackage{xcolor} 83 84 \usepackage{graphicx} % For including graphics 84 85 … … 120 121 % although it's supposed to be in both the TeX Live and MikTeX distributions. There are also documentation and 121 122 % installation instructions there. 122 \renewcommand*{\glstextformat}[1]{\textsf{#1}} 123 \makeatletter 124 \newcommand*{\glsplainhyperlink}[2]{% 125 \colorlet{currenttext}{.}% store current text color 126 \colorlet{currentlink}{\@linkcolor}% store current link color 127 \hypersetup{linkcolor=currenttext}% set link color 128 \hyperlink{#1}{#2}% 129 \hypersetup{linkcolor=currentlink}% reset to default 130 } 131 \let\@glslink\glsplainhyperlink 132 \makeatother 123 133 124 134 \usepackage{csquotes} … … 200 210 \makeindex 201 211 212 \newcommand\io{\glsxtrshort{io}}% 213 202 214 %====================================================================== 203 215 % L O G I C A L D O C U M E N T -- the content of your thesis … … 232 244 \part{Design} 233 245 \input{text/core.tex} 246 \input{text/io.tex} 234 247 \input{text/practice.tex} 235 \input{text/io.tex}236 248 \part{Evaluation} 237 249 \label{Evaluation} -
doc/user/figures/Cdecl.fig
rda3963a r565acf59 19 19 2 2 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 5 20 20 2850 1200 3600 1200 3600 1350 2850 1350 2850 1200 21 4 1 0 50 -1 4 1 00.0000 2 120 90 2925 1325 0\00122 4 1 0 50 -1 4 1 00.0000 2 120 90 3075 1325 1\00123 4 1 0 50 -1 4 1 00.0000 2 120 90 3225 1325 2\00124 4 1 0 50 -1 4 1 00.0000 2 120 90 3375 1325 3\00125 4 1 0 50 -1 4 1 00.0000 2 120 90 3525 1325 4\00121 4 1 0 50 -1 4 11 0.0000 2 120 90 2925 1325 0\001 22 4 1 0 50 -1 4 11 0.0000 2 120 90 3075 1325 1\001 23 4 1 0 50 -1 4 11 0.0000 2 120 90 3225 1325 2\001 24 4 1 0 50 -1 4 11 0.0000 2 120 90 3375 1325 3\001 25 4 1 0 50 -1 4 11 0.0000 2 120 90 3525 1325 4\001 26 26 -6 27 27 2 2 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 5 … … 55 55 1 1 1.00 45.00 60.00 56 56 2550 1275 2850 1275 57 4 1 0 50 -1 4 1 00.0000 2 120 90 1350 1650 0\00158 4 1 0 50 -1 4 1 00.0000 2 120 90 1500 1650 1\00159 4 1 0 50 -1 4 1 00.0000 2 120 90 1650 1650 2\00160 4 1 0 50 -1 4 1 00.0000 2 120 90 1800 1650 3\00161 4 1 0 50 -1 4 1 00.0000 2 120 90 1950 1650 4\00162 4 1 0 50 -1 4 1 00.0000 2 90 90 1200 1325 x\00163 4 1 0 50 -1 4 1 00.0000 2 90 90 2400 1325 x\00157 4 1 0 50 -1 4 11 0.0000 2 120 90 1350 1650 0\001 58 4 1 0 50 -1 4 11 0.0000 2 120 90 1500 1650 1\001 59 4 1 0 50 -1 4 11 0.0000 2 120 90 1650 1650 2\001 60 4 1 0 50 -1 4 11 0.0000 2 120 90 1800 1650 3\001 61 4 1 0 50 -1 4 11 0.0000 2 120 90 1950 1650 4\001 62 4 1 0 50 -1 4 11 0.0000 2 90 90 1200 1325 x\001 63 4 1 0 50 -1 4 11 0.0000 2 90 90 2400 1325 x\001 -
doc/user/user.tex
rda3963a r565acf59 11 11 %% Created On : Wed Apr 6 14:53:29 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : Mon Oct 5 08:57:29 202014 %% Update Count : 399813 %% Last Modified On : Mon Feb 8 21:53:31 2021 14 %% Update Count : 4327 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 37 37 \usepackage{mathptmx} % better math font with "times" 38 38 \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 39 59 \newcommand{\CFALatin}{} 40 60 % inline code ©...© (copyright symbol) emacs: C-q M-) … … 46 66 % math escape $...$ (dollar symbol) 47 67 \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 underscore59 % removes it as a variable-name character so keywords in variables are highlighted. MUST APPEAR60 % AFTER HYPERREF.61 \renewcommand{\textunderscore}{\leavevmode\makebox[1.2ex][c]{\rule{1ex}{0.075ex}}}62 63 \setlength{\topmargin}{-0.45in} % move running title into header64 \setlength{\headsep}{0.25in}65 66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%67 68 68 \CFAStyle % use default CFA format-style 69 \lstset{language=CFA} % CFA default lnaguage 69 70 \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}} 71 72 {} 72 73 … … 81 82 \newcommand{\Emph}[2][red]{{\color{#1}\textbf{\emph{#2}}}} 82 83 \newcommand{\R}[1]{\Textbf{#1}} 84 \newcommand{\RC}[1]{\Textbf{\LstBasicStyle{#1}}} 83 85 \newcommand{\B}[1]{{\Textbf[blue]{#1}}} 84 86 \newcommand{\G}[1]{{\Textbf[OliveGreen]{#1}}} … … 104 106 \author{ 105 107 \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 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 108 111 }% author 109 112 … … 144 147 \section{Introduction} 145 148 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. 147 150 The syntax of \CFA builds from C and should look immediately familiar to C/\Index*[C++]{\CC{}} programmers. 148 151 % Any language feature that is not described here can be assumed to be using the standard \Celeven syntax. 149 \CFA adds many modern programming-languagefeatures 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. 150 153 Like 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. 151 154 The primary new features include polymorphic routines and types, exceptions, concurrency, and modules. … … 157 160 instead, a programmer evolves a legacy program into \CFA by incrementally incorporating \CFA features. 158 161 As well, new programs can be written in \CFA using a combination of C and \CFA features. 162 In 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. 159 163 160 164 \Index*[C++]{\CC{}}~\cite{c++:v1} had a similar goal 30 years ago, allowing object-oriented programming to be incrementally added to C. … … 165 169 For example, the following programs compare the C, \CFA, and \CC I/O mechanisms, where the programs output the same result. 166 170 \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}$ 171 175 172 176 int main( void ) { 173 177 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 );@ 175 179 } 176 180 \end{cfa} 177 181 & 178 182 \begin{cfa} 179 #include <fstream> §\indexc{fstream}§183 #include <fstream>$\indexc{fstream}$ 180 184 181 185 int main( void ) { 182 186 int x = 0, y = 1, z = 2; 183 ®sout | x | y | z;®§\indexc{sout}§187 @sout | x | y | z;@$\indexc{sout}$ 184 188 } 185 189 \end{cfa} 186 190 & 187 191 \begin{cfa} 188 #include <iostream> §\indexc{iostream}§192 #include <iostream>$\indexc{iostream}$ 189 193 using namespace std; 190 194 int main() { 191 195 int x = 0, y = 1, z = 2; 192 ®cout<<x<<" "<<y<<" "<<z<<endl;®196 @cout<<x<<" "<<y<<" "<<z<<endl;@ 193 197 } 194 198 \end{cfa} 195 199 \end{tabular} 196 200 \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}).201 While \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}. 198 202 199 203 … … 210 214 \section{Why fix C?} 211 215 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.216 The 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. 213 217 This installation base and the programmers producing it represent a massive software-engineering investment spanning decades and likely to continue for decades more. 214 218 Even 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 usuallythe only language of choice.216 The TIOBE index~\cite{TIOBE} for February 202 0 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.219 For system programming, where direct access to hardware, storage management, and real-time issues are a requirement, C is the only language of choice. 220 The 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. 217 221 The top 4 rankings over the past 35 years are: 218 222 \begin{center} 219 223 \setlength{\tabcolsep}{10pt} 220 224 \begin{tabular}{@{}rcccccccc@{}} 221 & 202 0 & 2015 & 2010 & 2005 & 2000 & 1995 & 1990 & 1985\\ \hline222 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} \\ 227 Java & 2 & 1 & 1 & 2 & 3 & 28 & - & - \\ 228 Python & 3 & 5 & 6 & 7 & 23 & 13 & - & - \\ 229 \CC & 4 & 3 & 3 & 3 & 2 & 2 & 2 & 8 \\ 226 230 \end{tabular} 227 231 \end{center} … … 232 236 As stated, the goal of the \CFA project is to engineer modern language-features into C in an evolutionary rather than revolutionary way. 233 237 \CC~\cite{C++14,C++} is an example of a similar project; 234 however, it largely extended the C language, and did not address m ostof C's existing problems.\footnote{%238 however, it largely extended the C language, and did not address many of C's existing problems.\footnote{% 235 239 Two important existing problems addressed were changing the type of character literals from ©int© to ©char© and enumerator from ©int© to the type of its enumerators.} 236 240 \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. … … 241 245 242 246 The 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. 247 To achieve these goals required a significant engineering exercise, \ie ``thinking \emph{inside} the C box''. 248 Considering the large body of existing C code and programmers, there is significant impetus to ensure C is transformed into a modern language. 247 249 While \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. 248 250 While 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. … … 251 253 \section{History} 252 254 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{}}.) 255 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 \see{\cite{Werther96} for similar work in \Index*[C++]{\CC{}}}. 255 256 The first \CFA implementation of these extensions was by \Index*{Rodolfo Esteves}\index{Esteves, Rodolfo}~\cite{Esteves04}. 256 257 257 258 The 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): 258 259 \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; } 261 int forty_two = identity( 42 ); $\C{// T is bound to int, forty\_two == 42}$ 261 262 \end{cfa} 262 263 % extending the C type system with parametric polymorphism and overloading, as opposed to the \Index*[C++]{\CC{}} approach of object-oriented extensions. 263 264 \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}. 264 265 However, 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.266 As 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. 266 267 267 268 … … 273 274 This feature allows \CFA programmers to take advantage of the existing panoply of C libraries to access thousands of external software features. 274 275 Language 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.276 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 zero or very low cost. 276 277 Hence, \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. 277 278 … … 286 287 287 288 double 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}§289 double * val = (double *)bsearch( &key, vals, 10, sizeof(vals[0]), comp ); $\C{// search sorted array}$ 289 290 \end{cfa} 290 291 which can be augmented simply with a polymorphic, type-safe, \CFA-overloaded wrappers: … … 295 296 296 297 forall( 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 301 double * val = bsearch( 5.0, vals, 10 ); $\C{// selection based on return type}$ 301 302 int posn = bsearch( 5.0, vals, 10 ); 302 303 \end{cfa} … … 310 311 \begin{cfa} 311 312 forall( 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}§313 int * ip = malloc(); $\C{// select type and size from left-hand side}$ 313 314 double * dp = malloc(); 314 315 struct S {...} * sp = malloc(); … … 319 320 However, it is necessary to differentiate between C and \CFA code because of name \Index{overload}ing, as for \CC. 320 321 For example, the C math-library provides the following routines for computing the absolute value of the basic types: ©abs©, ©labs©, ©llabs©, ©fabs©, ©fabsf©, ©fabsl©, ©cabsf©, ©cabs©, and ©cabsl©. 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}}. 322 Whereas, \CFA wraps each of these routines into one overloaded name ©abs©: 323 \begin{cfa} 324 char @abs@( char ); 325 extern "C" { int @abs@( int ); } $\C{// use default C routine for int}$ 326 long int @abs@( long int ); 327 long long int @abs@( long long int ); 328 float @abs@( float ); 329 double @abs@( double ); 330 long double @abs@( long double ); 331 float _Complex @abs@( float _Complex ); 332 double _Complex @abs@( double _Complex ); 333 long double _Complex @abs@( long double _Complex ); 334 \end{cfa} 335 The 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). 336 Overloaded names must use \newterm{name mangling}\index{mangling!name} to create unique names that are different from unmangled C names. 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 The only way around this problem is C's approach of creating unique names for each pairing of operation and type. 339 340 This example illustrates a core idea in \CFA: \emph{the \Index{power of a name}}. 341 341 The name ``©abs©'' evokes the notion of absolute value, and many mathematical types provide the notion of absolute value. 342 342 Hence, knowing the name ©abs© is sufficient to apply it to any type where it is applicable. … … 344 344 345 345 346 \section [Compiling a CFA Program]{Compiling a \CFA Program}346 \section{\CFA Compilation} 347 347 348 348 The command ©cfa© is used to compile a \CFA program and is based on the \Index{GNU} \Indexc{gcc} command, \eg: 349 349 \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} 350 cfa$\indexc{cfa}\index{compilation!cfa@©cfa©}$ [ gcc/$\CFA{}$-options ] [ C/$\CFA{}$ source-files ] [ assembler/loader files ] 351 \end{cfa} 352 There 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] 354 356 \item 355 357 \Indexc{-std=gnu11}\index{compilation option!-std=gnu11@{©-std=gnu11©}} … … 359 361 Use the traditional GNU semantics for inline routines in C11 mode, which allows inline routines in header files. 360 362 \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] 363 366 \item 364 367 \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.368 Only 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. 366 369 The generated code starts with the standard \CFA \Index{prelude}. 370 371 \item 372 \Indexc{-XCFA}\index{compilation option!-XCFA@©-XCFA©} 373 Pass next flag as-is to the ©cfa-cpp© translator (see details below). 367 374 368 375 \item 369 376 \Indexc{-debug}\index{compilation option!-debug@©-debug©} 370 377 The program is linked with the debugging version of the runtime system. 371 The debug version performs runtime checks to help duringthe debugging phase of a \CFA program, but can substantially slow program execution.378 The debug version performs runtime checks to aid the debugging phase of a \CFA program, but can substantially slow program execution. 372 379 The runtime checks should only be removed after the program is completely debugged. 373 380 \textbf{This option is the default.} … … 399 406 \item 400 407 \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}).408 Do not supply ©extern "C"© wrappers for \Celeven standard include files \see{\VRef{s:StandardHeaders}}. 402 409 \textbf{This option is \emph{not} the default.} 403 410 \end{comment} … … 430 437 \begin{cfa} 431 438 #ifndef __CFORALL__ 432 #include <stdio.h> §\indexc{stdio.h}§ §\C{// C header file}§439 #include <stdio.h>$\indexc{stdio.h}$ $\C{// C header file}$ 433 440 #else 434 #include <fstream> §\indexc{fstream}§ §\C{// \CFA header file}§441 #include <fstream>$\indexc{fstream}$ $\C{// \CFA header file}$ 435 442 #endif 436 443 \end{cfa} … … 438 445 439 446 The \CFA translator has multiple steps. 440 The following flags control how the tran lator works, the stages run, and printing within a stage.447 The following flags control how the translator works, the stages run, and printing within a stage. 441 448 The majority of these flags are used by \CFA developers, but some are occasionally useful to programmers. 449 Each 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] 451 cfa $test$.cfa -CFA -XCFA -p # print translated code without printing the standard prelude 452 cfa $test$.cfa -XCFA -P -XCFA parse -XCFA -n # show program parse without prelude 453 \end{lstlisting} 442 454 \begin{description}[topsep=5pt,itemsep=0pt,parsep=0pt] 443 455 \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© 447 463 \item 448 464 \Indexc{-L}\index{translator option!-L@{©-L©}}, \Indexc{--linemarks}\index{translator option!--linemarks@{©--linemarks©}} \, generate line marks … … 454 470 \Indexc{-n}\index{translator option!-n@{©-n©}}, \Indexc{--no-prelude}\index{translator option!--no-prelude@{©--no-prelude©}} \, do not read prelude 455 471 \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 457 475 \item 458 476 \Indexc{-P}\index{translator option!-P@{©-P©}}, \Indexc{--print}\index{translator option!--print@{©--print©}} \, one of: 459 477 \begin{description}[topsep=0pt,itemsep=0pt,parsep=0pt] 460 478 \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 461 499 \Indexc{altexpr}\index{translator option!-P@{©-P©}!©altexpr©}\index{translator option!--print@{©-print©}!©altexpr©} \, alternatives for expressions 462 500 \item 463 \Indexc{ascodegen}\index{translator option!-P@{©-P©}!©ascodegen©}\index{translator option!--print@{©-print©}!©ascodegen©} \, as codegen rather than AST464 \item465 \Indexc{ast}\index{translator option!-P@{©-P©}!©ast©}\index{translator option!--print@{©-print©}!©ast©} \, AST after parsing466 \item467 501 \Indexc{astdecl}\index{translator option!-P@{©-P©}!©astdecl©}\index{translator option!--print@{©-print©}!©astdecl©} \, AST after declaration validation pass 468 502 \item 469 \Indexc{ asterr}\index{translator option!-P@{©-P©}!©asterr©}\index{translator option!--print@{©-print©}!©asterr©} \, AST on error503 \Indexc{resolver}\index{translator option!-P@{©-P©}!©resolver©}\index{translator option!--print@{©-print©}!©resolver©} \, before resolver step 470 504 \item 471 505 \Indexc{astexpr}\index{translator option!-P@{©-P©}!©astexpr©}\index{translator option!--print@{©-print©}!©altexpr©} \, AST after expression analysis 472 506 \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 473 511 \Indexc{astgen}\index{translator option!-P@{©-P©}!©astgen©}\index{translator option!--print@{©-print©}!©astgen©} \, AST after instantiate generics 474 512 \item 475 513 \Indexc{box}\index{translator option!-P@{©-P©}!©box©}\index{translator option!--print@{©-print©}!©box©} \, before box step 476 514 \item 477 \Indexc{ctordtor}\index{translator option!-P@{©-P©}!©ctordtor©}\index{translator option!--print@{©-print©}!©ctordtor©} \, after ctor/dtor are replaced478 \item479 515 \Indexc{codegen}\index{translator option!-P@{©-P©}!©codegen©}\index{translator option!--print@{©-print©}!©codegen©} \, before code generation 480 \item481 \Indexc{declstats}\index{translator option!-P@{©-P©}!©declstats©}\index{translator option!--print@{©-print©}!©declstats©} \, code property statistics482 \item483 \Indexc{parse}\index{translator option!-P@{©-P©}!©parse©}\index{translator option!--print@{©-print©}!©parse©} \, yacc (parsing) debug information484 \item485 \Indexc{pretty}\index{translator option!-P@{©-P©}!©pretty©}\index{translator option!--print@{©-print©}!©pretty©} \, prettyprint for ascodegen flag486 \item487 \Indexc{resolver}\index{translator option!-P@{©-P©}!©resolver©}\index{translator option!--print@{©-print©}!©resolver©} \, before resolver step488 \item489 \Indexc{rproto}\index{translator option!-P@{©-P©}!©rproto©}\index{translator option!--print@{©-print©}!©rproto©} \, resolver-proto instance490 \item491 \Indexc{rsteps}\index{translator option!-P@{©-P©}!©rsteps©}\index{translator option!--print@{©-print©}!©rsteps©} \, resolver steps492 \item493 \Indexc{symevt}\index{translator option!-P@{©-P©}!©symevt©}\index{translator option!--print@{©-print©}!©symevt©} \, symbol table events494 \item495 \Indexc{tree}\index{translator option!-P@{©-P©}!©tree©}\index{translator option!--print@{©-print©}!©tree©} \, parse tree496 \item497 \Indexc{tuple}\index{translator option!-P@{©-P©}!©tuple©}\index{translator option!--print@{©-print©}!©tuple©} \, after tuple expansion498 516 \end{description} 499 517 \item 500 518 \Indexc{--prelude-dir} <directory> \, prelude directory for debug/nodebug 501 519 \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© 507 521 \item 508 522 \Indexc{-t}\index{translator option!-t@{©-t©}}, \Indexc{--tree}\index{translator option!--tree@{©--tree©}} build in tree … … 513 527 \label{s:BackquoteIdentifiers} 514 528 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. 516 530 Keyword clashes are accommodated by syntactic transformations using the \CFA backquote escape-mechanism: 517 531 \begin{cfa} 518 int ®``®otype = 3; §\C{// make keyword an identifier}§519 double ®``®forall = 3.5;532 int @``@otype = 3; $\C{// make keyword an identifier}$ 533 double @``@forall = 3.5; 520 534 \end{cfa} 521 535 522 536 Existing 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©. 524 538 Several common C header-files with keyword clashes are fixed in the standard \CFA header-library, so there is a seamless programming-experience. 525 539 … … 527 541 \begin{cfa} 528 542 // 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}$ 531 545 #define __CFA_BFD_H__ 532 546 #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}$ 535 549 #undef with 536 550 #undef __CFA_BFD_H__ … … 544 558 \section{Constant Underscores} 545 559 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}§560 Numeric constants are extended to allow \Index{underscore}s\index{constant!underscore} as a separator, \eg: 561 \begin{cfa} 562 2@_@147@_@483@_@648; $\C{// decimal constant}$ 563 56@_@ul; $\C{// decimal unsigned long constant}$ 564 0@_@377; $\C{// octal constant}$ 565 0x@_@ff@_@ff; $\C{// hexadecimal constant}$ 566 0x@_@ef3d@_@aa5c; $\C{// hexadecimal constant}$ 567 3.141@_@592@_@654; $\C{// floating constant}$ 568 10@_@e@_@+1@_@00; $\C{// floating constant}$ 569 0x@_@ff@_@ff@_@p@_@3; $\C{// hexadecimal floating}$ 570 0x@_@1.ffff@_@ffff@_@p@_@128@_@l; $\C{// hexadecimal floating long constant}$ 571 L@_@$"\texttt{\textbackslash{x}}$@_@$\texttt{ff}$@_@$\texttt{ee}"$; $\C{// wide character constant}$ 558 572 \end{cfa} 559 573 The rules for placement of underscores are: … … 574 588 It 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). 575 589 This 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©. 576 591 577 592 578 593 \section{Exponentiation Operator} 579 594 580 C, \CC, and Java (and manyother 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)©.595 C, \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$. 597 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©. 583 598 584 599 There are exponentiation operators for integral and floating types, including the builtin \Index{complex} types. … … 587 602 Floating exponentiation\index{exponentiation!floating} is performed using \Index{logarithm}s\index{exponentiation!logarithm}, so the exponent cannot be negative. 588 603 \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.1590 | (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.1922i604 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 605 | (1.0f+2.0fi) @\@ (3.0f+2.0fi); 606 1 1 256 -64 125 @0@ 3273344365508751233 @0@ @0@ -0.015625 18.3791736799526 0.264715-1.1922i 592 607 \end{cfa} 593 608 Note, ©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©. 609 Because 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 595 611 The exponentiation operator is available for all the basic types, but for user-defined types, only the integral-computation version is available. 596 612 \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 );613 forall( otype T | { void ?{}( T & this, one_t ); T ?*?( T, T ); } ) 614 T ?@\@?( T ep, unsigned int y ); 615 forall( otype T | { void ?{}( T & this, one_t ); T ?*?( T, T ); } ) 616 T ?@\@?( T ep, unsigned long int y ); 601 617 \end{cfa} 602 618 The user type ©T© must define multiplication, one (©1©), and ©*©. … … 609 625 610 626 %\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 629 The ©if©/©while© expression allows declarations, similar to ©for© declaration expression.\footnote{ 630 Declarations in the ©do©-©while© condition are not useful because they appear after the loop body.} 631 \begin{cfa} 632 if ( @int x = f()@ ) ... $\C{// x != 0}$ 633 if ( @int x = f(), y = g()@ ) ... $\C{// x != 0 \&\& y != 0}$ 634 if ( @int x = f(), y = g(); x < y@ ) ... $\C{// relational expression}$ 635 if ( @struct S { int i; } x = { f() }; x.i < 4@ ) $\C{// relational expression}$ 636 637 while ( @int x = f()@ ) ... $\C{// x != 0}$ 638 while ( @int x = f(), y = g()@ ) ... $\C{// x != 0 \&\& y != 0}$ 639 while ( @int x = f(), y = g(); x < y@ ) ... $\C{// relational expression}$ 640 while ( @struct S { int i; } x = { f() }; x.i < 4@ ) ... $\C{// relational expression}$ 641 \end{cfa} 642 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. 643 The 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. 628 645 629 646 630 647 %\section{\texorpdfstring{\protect\lstinline@case@ Clause}{case Clause}} 631 648 \subsection{\texorpdfstring{\LstKeywordStyle{case} Clause}{case Clause}} 649 \label{s:caseClause} 632 650 633 651 C restricts the ©case© clause of a ©switch© statement to a single value. … … 640 658 \begin{cfa} 641 659 switch ( i ) { 642 case ®1, 3, 5®:660 case @1, 3, 5@: 643 661 ... 644 case ®2, 4, 6®:662 case @2, 4, 6@: 645 663 ... 646 664 } … … 670 688 \begin{cfa} 671 689 switch ( i ) { 672 case ®1~5:® §\C{// 1, 2, 3, 4, 5}§690 case @1~5:@ $\C{// 1, 2, 3, 4, 5}$ 673 691 ... 674 case ®10~15:® §\C{// 10, 11, 12, 13, 14, 15}§692 case @10~15:@ $\C{// 10, 11, 12, 13, 14, 15}$ 675 693 ... 676 694 } … … 678 696 Lists of subranges are also allowed. 679 697 \begin{cfa} 680 case ®1~5, 12~21, 35~42®:698 case @1~5, 12~21, 35~42@: 681 699 \end{cfa} 682 700 … … 722 740 if ( argc == 3 ) { 723 741 // open output file 724 ®// open input file725 ®} 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 { 729 747 // usage message 730 748 } … … 733 751 \end{cquote} 734 752 In this example, case 2 is always done if case 3 is done. 735 This control flow is difficult to simulate with ifstatements or a ©switch© statement without fall-through as code must be duplicated or placed in a separate routine.753 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. 736 754 C also uses fall-through to handle multiple case-values resulting in the same action: 737 755 \begin{cfa} 738 756 switch ( i ) { 739 ®case 1: case 3: case 5:®// odd values757 @case 1: case 3: case 5:@ // odd values 740 758 // odd action 741 759 break; 742 ®case 2: case 4: case 6:®// even values760 @case 2: case 4: case 6:@ // even values 743 761 // even action 744 762 break; 745 763 } 746 764 \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 otherprogramming languages with a ©switch© statement.765 This situation better handled without fall-through by allowing a list of case values \see{\VRef{s:caseClause}}. 766 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 most programming languages with a ©switch© statement. 749 767 Hence, 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. 750 768 … … 756 774 if ( j < k ) { 757 775 ... 758 ®case 1:®// transfer into "if" statement776 @case 1:@ // transfer into "if" statement 759 777 ... 760 778 } // if … … 762 780 while ( j < 5 ) { 763 781 ... 764 ®case 3:®// transfer into "while" statement782 @case 3:@ // transfer into "while" statement 765 783 ... 766 784 } // while 767 785 } // switch 768 786 \end{cfa} 769 Th e problem with this usage is branchinginto 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.787 This usage branches into control structures, which is known to cause both comprehension and technical difficulties. 788 The comprehension problem results from the inability to determine how control reaches a particular point due to the number of branches leading to it. 771 789 The 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 positivearguments for this kind of control flow, and therefore, there is a strong impetus to eliminate it.790 There are few arguments for this kind of control flow, and therefore, there is a strong impetus to eliminate it. 773 791 Nevertheless, C does have an idiom where this capability is used, known as ``\Index*{Duff's device}''~\cite{Duff83}: 774 792 \begin{cfa} … … 794 812 \item 795 813 It is possible to place the ©default© clause anywhere in the list of labelled clauses for a ©switch© statement, rather than only at the end. 796 Virtually allprogramming languages with a ©switch© statement require the ©default© clause to appear last in the case-clause list.814 Most programming languages with a ©switch© statement require the ©default© clause to appear last in the case-clause list. 797 815 The logic for this semantics is that after checking all the ©case© clauses without success, the ©default© clause is selected; 798 816 hence, physically placing the ©default© clause at the end of the ©case© clause list matches with this semantics. … … 803 821 \begin{cfa} 804 822 switch ( 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}$ 807 825 case 0: ... 808 826 ... 809 ®int z = 0;® §\C{// unreachable initialization, cannot appear after case}§827 @int z = 0;@ $\C{// unreachable initialization, cannot appear after case}$ 810 828 z = 2; 811 829 case 1: 812 ®x = z;® §\C{// without fall through, z is uninitialized}§830 @x = z;@ $\C{// without fall through, z is uninitialized}$ 813 831 } 814 832 \end{cfa} 815 833 While the declaration of the local variable ©y© is useful with a scope across all ©case© clauses, the initialization for such a variable is defined to never be executed because control always transfers over it. 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 818 The key observation is that the ©switch© statement branches into control structure, \ie there are multiple entry points into its statement body.834 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©, where both are problematic. 835 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. 836 The key observation is that the ©switch© statement branches into a control structure, \ie there are multiple entry points into its statement body. 819 837 \end{enumerate} 820 838 … … 842 860 Therefore, to preserve backwards compatibility, it is necessary to introduce a new kind of ©switch© statement, called ©choose©, with no implicit fall-through semantics and an explicit fall-through if the last statement of a case-clause ends with the new keyword ©fallthrough©/©fallthru©, \eg: 843 861 \begin{cfa} 844 ®choose®( i ) {862 @choose@ ( i ) { 845 863 case 1: case 2: case 3: 846 864 ... 847 ®// implicit end of switch (break)848 ®case 5:865 @// implicit end of switch (break) 866 @case 5: 849 867 ... 850 ®fallthru®; §\C{// explicit fall through}§868 @fallthru@; $\C{// explicit fall through}$ 851 869 case 7: 852 870 ... 853 ®break® §\C{// explicit end of switch (redundant)}§871 @break@ $\C{// explicit end of switch (redundant)}$ 854 872 default: 855 873 j = 3; 856 874 } 857 875 \end{cfa} 858 Like the ©switch© statement, the ©choose© statement retains the fall-through semantics for a list of ©case© clauses ;876 Like the ©switch© statement, the ©choose© statement retains the fall-through semantics for a list of ©case© clauses. 859 877 An implicit ©break© is applied only at the end of the \emph{statements} following a ©case© clause. 860 878 An 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. … … 872 890 \begin{cfa} 873 891 switch ( x ) { 874 ®int i = 0;® §\C{// allowed only at start}§892 @int i = 0;@ $\C{// allowed only at start}$ 875 893 case 0: 876 894 ... 877 ®int j = 0;® §\C{// disallowed}§895 @int j = 0;@ $\C{// disallowed}$ 878 896 case 1: 879 897 { 880 ®int k = 0;® §\C{// allowed at different nesting levels}§898 @int k = 0;@ $\C{// allowed at different nesting levels}$ 881 899 ... 882 ®case 2:® §\C{// disallow case in nested statements}§900 @case 2:@ $\C{// disallow case in nested statements}$ 883 901 } 884 902 ... … … 897 915 case 3: 898 916 if ( ... ) { 899 ... ®fallthru;®// goto case 4917 ... @fallthru;@ // goto case 4 900 918 } else { 901 919 ... … … 912 930 choose ( ... ) { 913 931 case 3: 914 ... ®fallthrough common;®932 ... @fallthrough common;@ 915 933 case 4: 916 ... ®fallthrough common;®917 918 ®common:®// below fallthrough934 ... @fallthrough common;@ 935 936 @common:@ // below fallthrough 919 937 // at case-clause level 920 938 ... // common code for cases 3/4 … … 932 950 for ( ... ) { 933 951 // multi-level transfer 934 ... ®fallthru common;®952 ... @fallthru common;@ 935 953 } 936 954 ... 937 955 } 938 956 ... 939 ®common:®// below fallthrough957 @common:@ // below fallthrough 940 958 // at case-clause level 941 959 \end{cfa} … … 948 966 949 967 \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} \\ 952 970 \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} 972 while @()@ { sout | "empty"; break; } 973 do { sout | "empty"; break; } while @()@; 974 for @()@ { sout | "empty"; break; } 975 for ( @0@ ) { sout | "A"; } sout | "zero"; 976 for ( @1@ ) { sout | "A"; } 977 for ( @10@ ) { sout | "A"; } 978 for ( @= 10@ ) { sout | "A"; } 979 for ( @1 ~= 10 ~ 2@ ) { sout | "B"; } 980 for ( @10 -~= 1 ~ 2@ ) { sout | "C"; } 981 for ( @0.5 ~ 5.5@ ) { sout | "D"; } 982 for ( @5.5 -~ 0.5@ ) { sout | "E"; } 983 for ( @i; 10@ ) { sout | i; } 984 for ( @i; = 10@ ) { sout | i; } 985 for ( @i; 1 ~= 10 ~ 2@ ) { sout | i; } 986 for ( @i; 10 -~= 1 ~ 2@ ) { sout | i; } 987 for ( @i; 0.5 ~ 5.5@ ) { sout | i; } 988 for ( @i; 5.5 -~ 0.5@ ) { sout | i; } 989 for ( @ui; 2u ~= 10u ~ 2u@ ) { sout | ui; } 990 for ( @ui; 10u -~= 2u ~ 2u@ ) { sout | ui; } 973 991 enum { N = 10 }; 974 for ( ®N®) { sout | "N"; }975 for ( ®i; N®) { sout | i; }976 for ( ®i; N -~ 0®) { sout | i; }992 for ( @N@ ) { sout | "N"; } 993 for ( @i; N@ ) { sout | i; } 994 for ( @i; N -~ 0@ ) { sout | i; } 977 995 const 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; }996 for ( @i; start ~ comp ~ inc + 1@ ) { sout | i; } 997 for ( i; 1 ~ $\R{@}$ ) { if ( i > 10 ) break; sout | i; } 998 for ( i; 10 -~ $\R{@}$ ) { if ( i < 0 ) break; sout | i; } 999 for ( i; 2 ~ $\R{@}$ ~ 2 ) { if ( i > 10 ) break; sout | i; } 1000 for ( i; 2.1 ~ $\R{@}$ ~ $\R{@}$ ) { if ( i > 10.5 ) break; sout | i; i += 1.7; } 1001 for ( i; 10 -~ $\R{@}$ ~ 2 ) { if ( i < 0 ) break; sout | i; } 1002 for ( i; 12.1 ~ $\R{@}$ ~ $\R{@}$ ) { if ( i < 2.5 ) break; sout | i; i -= 1.7; } 1003 for ( i; 5 @:@ j; -5 ~ $@$ ) { sout | i | j; } 1004 for ( i; 5 @:@ j; -5 -~ $@$ ) { sout | i | j; } 1005 for ( i; 5 @:@ j; -5 ~ $@$ ~ 2 ) { sout | i | j; } 1006 for ( i; 5 @:@ j; -5 -~ $@$ ~ 2 ) { sout | i | j; } 1007 for ( i; 5 @:@ j; -5 ~ $@$ ) { sout | i | j; } 1008 for ( i; 5 @:@ j; -5 -~ $@$ ) { sout | i | j; } 1009 for ( i; 5 @:@ j; -5 ~ $@$ ~ 2 ) { sout | i | j; } 1010 for ( i; 5 @:@ j; -5 -~ $@$ ~ 2 ) { sout | i | j; } 1011 for ( i; 5 @:@ j; -5 -~ $@$ ~ 2 @:@ k; 1.5 ~ $@$ ) { sout | i | j | k; } 1012 for ( i; 5 @:@ j; -5 -~ $@$ ~ 2 @:@ k; 1.5 ~ $@$ ) { sout | i | j | k; } 1013 for ( i; 5 @:@ k; 1.5 ~ $@$ @:@ j; -5 -~ $@$ ~ 2 ) { sout | i | j | k; } 996 1014 \end{cfa} 997 1015 & … … 1056 1074 \subsection{Loop Control} 1057 1075 1058 The ©for©/©while©/©do-while© loop-control allows empty or simplified ranges (see Figure~\ref{f:LoopControlExamples}). 1059 \begin{itemize} 1076 Looping a fixed number of times, possibly with a loop index, occurs frequently. 1077 \CFA condenses simply looping to facilitate coding speed and safety. 1078 The ©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 1085 The up-to range uses operator ©+=© for increment; 1086 \item 1087 The down-to range uses operator ©-=© for decrement. 1060 1088 \item 1061 1089 The 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} 1091 for ( i; @5@ ) $\C[2.5in]{// typeof(5) i; 5 is comparison value}$ 1092 for ( i; @1.5@~5.5~0.5 ) $\C{// typeof(1.5) i; 1.5 is start value}$ 1093 \end{cfa} 1062 1094 \item 1063 1095 An 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} 1097 while ( $\R{/*empty*/}$ ) $\C{// while ( true )}$ 1098 for ( $\R{/*empty*/}$ ) $\C{// for ( ; true; )}$ 1099 do ... while ( $\R{/*empty*/}$ ) $\C{// do ... while ( true )}$ 1100 \end{cfa} 1101 \item 1102 A comparison N is implicit up-to exclusive range [0,N\R{)}. 1103 \begin{cfa} 1104 for ( @5@ ) $\C{// for ( typeof(5) i; i < 5; i += 1 )}$ 1105 \end{cfa} 1106 \item 1107 A comparison ©=© N is implicit up-to inclusive range [0,N\R{]}. 1108 \begin{cfa} 1109 for ( @=@5 ) $\C{// for ( typeof(5) i; i <= 5; i += 1 )}$ 1110 \end{cfa} 1111 \item 1112 The up-to range M ©~©\index{~@©~©} N means exclusive range [M,N\R{)}. 1113 \begin{cfa} 1114 for ( 1@~@5 ) $\C{// for ( typeof(1) i = 1; i < 5; i += 1 )}$ 1115 \end{cfa} 1116 \item 1117 The up-to range M ©~=©\index{~=@©~=©} N means inclusive range [M,N\R{]}. 1118 \begin{cfa} 1119 for ( 1@~=@5 ) $\C{// for ( typeof(1) i = 1; i <= 5; i += 1 )}$ 1120 \end{cfa} 1121 \item 1122 The down-to range M ©-~©\index{-~@©-~©} N means exclusive range [N,M\R{)}. 1123 \begin{cfa} 1124 for ( 1@-~@5 ) $\C{// for ( typeof(1) i = 5; i > 0; i -= 1 )}$ 1125 \end{cfa} 1126 \item 1127 The down-to range M ©-~=©\index{-~=@©-~=©} N means inclusive range [N,M\R{]}. 1128 \begin{cfa} 1129 for ( 1@-~=@5 ) $\C{// for ( typeof(1) i = 5; i >= 0; i -= 1 )}$ 1130 \end{cfa} 1084 1131 \item 1085 1132 ©@© means put nothing in this field. 1133 \begin{cfa} 1134 for ( 1~$\R{@}$~2 ) $\C{// for ( typeof(1) i = 1; /*empty*/; i += 2 )}$ 1135 \end{cfa} 1086 1136 \item 1087 1137 ©:© means start another index. 1138 \begin{cfa} 1139 for ( 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} 1088 1141 \end{itemize} 1089 1142 … … 1104 1157 \begin{lrbox}{\myboxA} 1105 1158 \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 ( ... ) { 1113 1166 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@; 1121 1174 } // switch 1122 1175 } else { 1123 ... ®break If®; ... // terminate if1176 ... @break If@; ... // terminate if 1124 1177 } // if 1125 1178 } while ( ... ); // do 1126 1179 } // while 1127 1180 } // for 1128 } ®finally®{ // always executed1181 } @finally@ { // always executed 1129 1182 } // try 1130 1183 } // compound … … 1136 1189 { 1137 1190 1138 ®ForC:®for ( ... ) {1139 ®WhileC:®while ( ... ) {1140 ®DoC:®do {1191 @ForC:@ for ( ... ) { 1192 @WhileC:@ while ( ... ) { 1193 @DoC:@ do { 1141 1194 if ( ... ) { 1142 1195 switch ( ... ) { 1143 1196 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:@ ; 1152 1205 } else { 1153 ... ®goto If®; ... // terminate if1154 } ®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:@ ; 1161 1214 \end{cfa} 1162 1215 \end{lrbox} 1163 1216 1217 \hspace*{-10pt} 1164 1218 \subfloat[\CFA]{\label{f:CFibonacci}\usebox\myboxA} 1165 1219 \hspace{2pt} 1166 1220 \vrule 1167 \hspace{2pt}1168 1221 \subfloat[C]{\label{f:CFAFibonacciGen}\usebox\myboxB} 1169 1222 \caption{Multi-level Exit} … … 1193 1246 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 1247 \begin{cfa} 1195 struct S { §\C{// aggregate}§1196 char c; §\C{// fields}§1248 struct S { $\C{// aggregate}$ 1249 char c; $\C{// fields}$ 1197 1250 int i; 1198 1251 double d; … … 1203 1256 \begin{cfa} 1204 1257 void f( S s ) { 1205 ®s.®c; ®s.®i; ®s.®d; §\C{// access containing fields}§1258 @s.@c; @s.@i; @s.@d; $\C{// access containing fields}$ 1206 1259 } 1207 1260 \end{cfa} … … 1210 1263 \begin{C++} 1211 1264 struct S { 1212 char c; §\C{// fields}§1265 char c; $\C{// fields}$ 1213 1266 int i; 1214 1267 double d; 1215 void f() { §\C{// implicit ``this'' aggregate}§1216 ®this->®c; ®this->®i; ®this->®d; §\C{// access containing fields}§1268 void f() { $\C{// implicit ``this'' aggregate}$ 1269 @this->@c; @this->@i; @this->@d; $\C{// access containing fields}$ 1217 1270 } 1218 1271 } … … 1222 1275 \begin{cfa} 1223 1276 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.1277 int S::f( T & t ) { $\C{// multiple aggregate parameters}$ 1278 c; i; d; $\C{\R{// this--{\textgreater}c, this--{\textgreater}i, this--{\textgreater}d}}$ 1279 @t.@m; @t.@n; $\C{// must qualify}$ 1280 } 1281 \end{cfa} 1282 1283 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 1284 Hence, the qualified fields become variables with the side-effect that it is easier to optimizing field references in a block. 1232 1285 \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}§1286 void f( S & this ) @with ( this )@ { $\C{// with statement}$ 1287 c; i; d; $\C{\R{// this.c, this.i, this.d}}$ 1235 1288 } 1236 1289 \end{cfa} 1237 1290 with the generality of opening multiple aggregate-parameters: 1238 1291 \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}§1292 void f( S & s, T & t ) @with ( s, t )@ { $\C{// multiple aggregate parameters}$ 1293 c; i; d; $\C{\R{// s.c, s.i, s.d}}$ 1294 m; n; $\C{\R{// t.m, t.n}}$ 1242 1295 } 1243 1296 \end{cfa} … … 1245 1298 In detail, the ©with© statement has the form: 1246 1299 \begin{cfa} 1247 §\emph{with-statement}§:1248 'with' '(' §\emph{expression-list}§ ')' §\emph{compound-statement}§1300 $\emph{with-statement}$: 1301 'with' '(' $\emph{expression-list}$ ')' $\emph{compound-statement}$ 1249 1302 \end{cfa} 1250 1303 and may appear as the body of a function or nested within a function body. … … 1258 1311 The difference between parallel and nesting occurs for fields with the same name and type: 1259 1312 \begin{cfa} 1260 struct S { int ®i®; int j; double m; } s, w;1261 struct T { int ®i®; int k; int m; } t, w;1313 struct S { int @i@; int j; double m; } s, w; 1314 struct T { int @i@; int k; int m; } t, w; 1262 1315 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}§1316 j + k; $\C{// unambiguous, s.j + t.k}$ 1317 m = 5.0; $\C{// unambiguous, t.m = 5.0}$ 1318 m = 1; $\C{// unambiguous, s.m = 1}$ 1319 int a = m; $\C{// unambiguous, a = s.i }$ 1320 double b = m; $\C{// unambiguous, b = t.m}$ 1321 int c = s.i + t.i; $\C{// unambiguous, qualification}$ 1322 (double)m; $\C{// unambiguous, cast}$ 1270 1323 } 1271 1324 \end{cfa} … … 1277 1330 There is an interesting problem between parameters and the function-body ©with©, \eg: 1278 1331 \begin{cfa} 1279 void ?{}( S & s, int i ) with ( s ) { §\C{// constructor}§1280 ®s.i = i;® j = 3; m = 5.5; §\C{// initialize fields}§1332 void ?{}( S & s, int i ) with ( s ) { $\C{// constructor}$ 1333 @s.i = i;@ j = 3; m = 5.5; $\C{// initialize fields}$ 1281 1334 } 1282 1335 \end{cfa} … … 1291 1344 and implicitly opened \emph{after} a function-body open, to give them higher priority: 1292 1345 \begin{cfa} 1293 void ?{}( S & s, int ®i® ) with ( s ) ®with( §\emph{\color{red}params}§ )®{1294 s.i = ®i®; j = 3; m = 5.5;1346 void ?{}( S & s, int @i@ ) with ( s ) @with( $\emph{\R{params}}$ )@ { 1347 s.i = @i@; j = 3; m = 5.5; 1295 1348 } 1296 1349 \end{cfa} 1297 1350 Finally, a cast may be used to disambiguate among overload variables in a ©with© expression: 1298 1351 \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:1352 with ( w ) { ... } $\C{// ambiguous, same name and no context}$ 1353 with ( (S)w ) { ... } $\C{// unambiguous, cast}$ 1354 \end{cfa} 1355 and ©with© expressions may be complex expressions with type reference \see{\VRef{s:References}} to aggregate: 1303 1356 % \begin{cfa} 1304 1357 % struct S { int i, j; } sv; 1305 % with ( sv ) { §\C{// implicit reference}§1358 % with ( sv ) { $\C{// implicit reference}$ 1306 1359 % S & sr = sv; 1307 % with ( sr ) { §\C{// explicit reference}§1360 % with ( sr ) { $\C{// explicit reference}$ 1308 1361 % S * sp = &sv; 1309 % with ( *sp ) { §\C{// computed reference}§1310 % i = 3; j = 4; §\C{\color{red}// sp--{\textgreater}i, sp--{\textgreater}j}§1362 % with ( *sp ) { $\C{// computed reference}$ 1363 % i = 3; j = 4; $\C{\color{red}// sp--{\textgreater}i, sp--{\textgreater}j}$ 1311 1364 % } 1312 % i = 2; j = 3; §\C{\color{red}// sr.i, sr.j}§1365 % i = 2; j = 3; $\C{\color{red}// sr.i, sr.j}$ 1313 1366 % } 1314 % i = 1; j = 2; §\C{\color{red}// sv.i, sv.j}§1367 % i = 1; j = 2; $\C{\color{red}// sv.i, sv.j}$ 1315 1368 % } 1316 1369 % \end{cfa} … … 1320 1373 class C { 1321 1374 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}§1375 int mem() { $\C{\R{// implicit "this" parameter}}$ 1376 i = 1; $\C{\R{// this->i}}$ 1377 j = 2; $\C{\R{// this->j}}$ 1325 1378 } 1326 1379 } … … 1329 1382 \begin{cfa} 1330 1383 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;1384 int mem( S & @this@ ) { $\C{// explicit "this" parameter}$ 1385 @this.@i = 1; $\C{// "this" is not elided}$ 1386 @this.@j = 2; 1334 1387 } 1335 1388 \end{cfa} 1336 1389 but it is cumbersome having to write ``©this.©'' many times in a member. 1337 1390 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}§1391 \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. 1392 \begin{cfa} 1393 int mem( S & this ) @with( this )@ { $\C{// with clause}$ 1394 i = 1; $\C{\R{// this.i}}$ 1395 j = 2; $\C{\R{// this.j}}$ 1343 1396 } 1344 1397 \end{cfa} … … 1346 1399 \begin{cfa} 1347 1400 struct T { double m, n; }; 1348 int mem2( S & this1, T & this2 ) ®with( this1, this2 )®{1401 int mem2( S & this1, T & this2 ) @with( this1, this2 )@ { 1349 1402 i = 1; j = 2; 1350 1403 m = 1.0; n = 2.0; … … 1357 1410 struct S1 { ... } s1; 1358 1411 struct S2 { ... } s2; 1359 ®with( s1 )® { §\C{// with statement}§1412 @with( s1 )@ { $\C{// with statement}$ 1360 1413 // access fields of s1 without qualification 1361 ®with s2® { §\C{// nesting}§1414 @with s2@ { $\C{// nesting}$ 1362 1415 // access fields of s1 and s2 without qualification 1363 1416 } 1364 1417 } 1365 ®with s1, s2®{1418 @with s1, s2@ { 1366 1419 // access unambiguous fields of s1 and s2 without qualification 1367 1420 } … … 1414 1467 Non-local transfer can cause stack unwinding, \ie non-local routine termination, depending on the kind of raise. 1415 1468 \begin{cfa} 1416 exception_t E {}; §\C{// exception type}§1469 exception_t E {}; $\C{// exception type}$ 1417 1470 void f(...) { 1418 ... throw E{}; ... §\C{// termination}§1419 ... throwResume E{}; ... §\C{// resumption}§1471 ... throw E{}; ... $\C{// termination}$ 1472 ... throwResume E{}; ... $\C{// resumption}$ 1420 1473 } 1421 1474 try { 1422 1475 f(...); 1423 } catch( E e ; §boolean-predicate§ ) { §\C{// termination handler}§1476 } catch( E e ; $boolean-predicate$ ) { $\C{// termination handler}$ 1424 1477 // recover and continue 1425 } catchResume( E e ; §boolean-predicate§ ) { §\C{// resumption handler}§1478 } catchResume( E e ; $boolean-predicate$ ) { $\C{// resumption handler}$ 1426 1479 // repair and return 1427 1480 } finally { … … 1430 1483 \end{cfa} 1431 1484 The kind of raise and handler match: ©throw© with ©catch© and ©throwResume© with ©catchResume©. 1432 Then the exception type must match along with any addit onal predicate must be true.1485 Then the exception type must match along with any additional predicate must be true. 1433 1486 The ©catch© and ©catchResume© handlers may appear in any oder. 1434 1487 However, the ©finally© clause must appear at the end of the ©try© statement. … … 1483 1536 For example, a routine returning a \Index{pointer} to an array of integers is defined and used in the following way: 1484 1537 \begin{cfa} 1485 int ®(*®f®())[®5®]® {...}; §\C{// definition}§1486 ... ®(*®f®())[®3®]® += 1; §\C{// usage}§1538 int @(*@f@())[@5@]@ {...}; $\C{// definition}$ 1539 ... @(*@f@())[@3@]@ += 1; $\C{// usage}$ 1487 1540 \end{cfa} 1488 1541 Essentially, the return type is wrapped around the routine name in successive layers (like an \Index{onion}). … … 1499 1552 \begin{tabular}{@{}l@{\hspace{3em}}l@{}} 1500 1553 \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 )®;1554 \begin{cfa}[moredelim={**[is][\color{blue}]{\#}{\#}}] 1555 #[5] *# @int@ x1; 1556 #* [5]# @int@ x2; 1557 #[* [5] int]# f@( int p )@; 1505 1558 \end{cfa} 1506 1559 & 1507 \begin{cfa} 1508 ®int® ß*ß x1 ß[5]ß;1509 ®int® ß(*ßx2ß)[5]ß;1510 ßint (*ßf®( int p )®ß)[5]ß;1560 \begin{cfa}[moredelim={**[is][\color{blue}]{\#}{\#}}] 1561 @int@ #*# x1 #[5]#; 1562 @int@ #(*#x2#)[5]#; 1563 #int (*#f@( int p )@#)[5]#; 1511 1564 \end{cfa} 1512 1565 \end{tabular} … … 1520 1573 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1521 1574 \begin{cfa} 1522 ®*®int x, y;1575 @*@ int x, y; 1523 1576 \end{cfa} 1524 1577 & 1525 1578 \begin{cfa} 1526 int ®*®x, ®*®y;1579 int @*@x, @*@y; 1527 1580 \end{cfa} 1528 1581 \end{tabular} … … 1533 1586 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1534 1587 \begin{cfa} 1535 ®*®int x;1588 @*@ int x; 1536 1589 int y; 1537 1590 \end{cfa} 1538 1591 & 1539 1592 \begin{cfa} 1540 int ®*®x, y;1593 int @*@x, y; 1541 1594 1542 1595 \end{cfa} … … 1647 1700 1648 1701 \section{Pointer / Reference} 1702 \label{s:PointerReference} 1649 1703 1650 1704 C provides a \newterm{pointer type}; … … 1673 1727 & 1674 1728 \begin{cfa} 1675 int * ®const®x = (int *)1001729 int * @const@ x = (int *)100 1676 1730 *x = 3; // implicit dereference 1677 int * ®const®y = (int *)104;1731 int * @const@ y = (int *)104; 1678 1732 *y = *x; // implicit dereference 1679 1733 \end{cfa} … … 1713 1767 \begin{tabular}{@{}l@{\hspace{2em}}l@{}} 1714 1768 \begin{cfa} 1715 int x, y, ®*® p1, ®*® p2, ®**®p3;1716 p1 = ®&®x; // p1 points to x1769 int x, y, @*@ p1, @*@ p2, @**@ p3; 1770 p1 = @&@x; // p1 points to x 1717 1771 p2 = p1; // p2 points to x 1718 p1 = ®&®y; // p1 points to y1772 p1 = @&@y; // p1 points to y 1719 1773 p3 = &p2; // p3 points to p2 1720 1774 \end{cfa} … … 1728 1782 For example, \Index*{Algol68}~\cite{Algol68} infers pointer dereferencing to select the best meaning for each pointer usage 1729 1783 \begin{cfa} 1730 p2 = p1 + x; §\C{// compiler infers *p2 = *p1 + x;}§1784 p2 = p1 + x; $\C{// compiler infers *p2 = *p1 + x;}$ 1731 1785 \end{cfa} 1732 1786 Algol68 infers the following dereferencing ©*p2 = *p1 + x©, because adding the arbitrary integer value in ©x© to the address of ©p1© and storing the resulting address into ©p2© is an unlikely operation. … … 1736 1790 In C, objects of pointer type always manipulate the pointer object's address: 1737 1791 \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}§1792 p1 = p2; $\C{// p1 = p2\ \ rather than\ \ *p1 = *p2}$ 1793 p2 = p1 + x; $\C{// p2 = p1 + x\ \ rather than\ \ *p2 = *p1 + x}$ 1740 1794 \end{cfa} 1741 1795 even though the assignment to ©p2© is likely incorrect, and the programmer probably meant: 1742 1796 \begin{cfa} 1743 p1 = p2; §\C{// pointer address assignment}§1744 ®*®p2 = ®*®p1 + x; §\C{// pointed-to value assignment / operation}§ 1797 p1 = p2; $\C{// pointer address assignment}$ 1798 @*@p2 = @*@p1 + x; $\C{// pointed-to value assignment / operation}$ 1745 1799 \end{cfa} 1746 1800 The C semantics work well for situations where manipulation of addresses is the primary meaning and data is rarely accessed, such as storage management (©malloc©/©free©). … … 1758 1812 To support this common case, a reference type is introduced in \CFA, denoted by ©&©, which is the opposite dereference semantics to a pointer type, making the value at the pointed-to location the implicit semantics for dereferencing (similar but not the same as \CC \Index{reference type}s). 1759 1813 \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}§1814 int x, y, @&@ r1, @&@ r2, @&&@ r3; 1815 @&@r1 = &x; $\C{// r1 points to x}$ 1816 @&@r2 = &r1; $\C{// r2 points to x}$ 1817 @&@r1 = &y; $\C{// r1 points to y}$ 1818 @&&@r3 = @&@&r2; $\C{// r3 points to r2}$ 1819 r2 = ((r1 + r2) * (r3 - r1)) / (r3 - 15); $\C{// implicit dereferencing}$ 1766 1820 \end{cfa} 1767 1821 Except for auto-dereferencing by the compiler, this reference example is the same as the previous pointer example. … … 1769 1823 One 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: 1770 1824 \begin{cfa} 1771 ®*®r2 = ((®*®r1 + ®*®r2) ®*® (®**®r3 - ®*®r1)) / (®**®r3 - 15);1825 @*@r2 = ((@*@r1 + @*@r2) @*@ (@**@r3 - @*@r1)) / (@**@r3 - 15); 1772 1826 \end{cfa} 1773 1827 When a reference operation appears beside a dereference operation, \eg ©&*©, they cancel out. … … 1778 1832 For a \CFA reference type, the cancellation on the left-hand side of assignment leaves the reference as an address (\Index{lvalue}): 1779 1833 \begin{cfa} 1780 (& ®*®)r1 = &x; §\C{// (\&*) cancel giving address in r1 not variable pointed-to by r1}§1834 (&@*@)r1 = &x; $\C{// (\&*) cancel giving address in r1 not variable pointed-to by r1}$ 1781 1835 \end{cfa} 1782 1836 Similarly, the address of a reference can be obtained for assignment or computation (\Index{rvalue}): 1783 1837 \begin{cfa} 1784 (&(& ®*®)®*®)r3 = &(&®*®)r2; §\C{// (\&*) cancel giving address in r2, (\&(\&*)*) cancel giving address in r3}§1838 (&(&@*@)@*@)r3 = &(&@*@)r2; $\C{// (\&*) cancel giving address in r2, (\&(\&*)*) cancel giving address in r3}$ 1785 1839 \end{cfa} 1786 1840 Cancellation\index{cancellation!pointer/reference}\index{pointer!cancellation} works to arbitrary depth. … … 1790 1844 int x, *p1 = &x, **p2 = &p1, ***p3 = &p2, 1791 1845 &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}§1846 ***p3 = 3; $\C{// change x}$ 1847 r3 = 3; $\C{// change x, ***r3}$ 1848 **p3 = ...; $\C{// change p1}$ 1849 &r3 = ...; $\C{// change r1, (\&*)**r3, 1 cancellation}$ 1850 *p3 = ...; $\C{// change p2}$ 1851 &&r3 = ...; $\C{// change r2, (\&(\&*)*)*r3, 2 cancellations}$ 1852 &&&r3 = p3; $\C{// change r3 to p3, (\&(\&(\&*)*)*)r3, 3 cancellations}$ 1799 1853 \end{cfa} 1800 1854 Furthermore, both types are equally performant, as the same amount of dereferencing occurs for both types. … … 1803 1857 As for a pointer type, a reference type may have qualifiers: 1804 1858 \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}§ 1859 const int cx = 5; $\C{// cannot change cx;}$ 1860 const int & cr = cx; $\C{// cannot change what cr points to}$ 1861 @&@cr = &cx; $\C{// can change cr}$ 1862 cr = 7; $\C{// error, cannot change cx}$ 1863 int & const rc = x; $\C{// must be initialized}$ 1864 @&@rc = &x; $\C{// error, cannot change rc}$ 1865 const int & const crc = cx; $\C{// must be initialized}$ 1866 crc = 7; $\C{// error, cannot change cx}$ 1867 @&@crc = &cx; $\C{// error, cannot change crc}$ 1814 1868 \end{cfa} 1815 1869 Hence, for type ©& const©, there is no pointer assignment, so ©&rc = &x© is disallowed, and \emph{the address value cannot be the null pointer unless an arbitrary pointer is coerced\index{coercion} into the reference}: 1816 1870 \begin{cfa} 1817 int & const cr = *0; §\C{// where 0 is the int * zero}§1871 int & const cr = *0; $\C{// where 0 is the int * zero}$ 1818 1872 \end{cfa} 1819 1873 Note, constant reference-types do not prevent \Index{addressing errors} because of explicit storage-management: … … 1822 1876 cr = 5; 1823 1877 free( &cr ); 1824 cr = 7; §\C{// unsound pointer dereference}§1878 cr = 7; $\C{// unsound pointer dereference}$ 1825 1879 \end{cfa} 1826 1880 1827 1881 The position of the ©const© qualifier \emph{after} the pointer/reference qualifier causes confuse for C programmers. 1828 1882 The ©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:1883 \CFA-style declarations \see{\VRef{s:AlternativeDeclarations}} attempt to address this issue: 1830 1884 \begin{cquote} 1831 1885 \begin{tabular}{@{}l@{\hspace{3em}}l@{}} 1832 1886 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1833 1887 \begin{cfa} 1834 ®const® * ®const®* const int ccp;1835 ®const® & ®const®& const int ccr;1888 @const@ * @const@ * const int ccp; 1889 @const@ & @const@ & const int ccr; 1836 1890 \end{cfa} 1837 1891 & 1838 1892 \begin{cfa} 1839 const int * ®const® * ®const®ccp;1893 const int * @const@ * @const@ ccp; 1840 1894 1841 1895 \end{cfa} … … 1846 1900 Finally, like pointers, references are usable and composable with other type operators and generators. 1847 1901 \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}§1902 int w, x, y, z, & ar[3] = { x, y, z }; $\C{// initialize array of references}$ 1903 &ar[1] = &w; $\C{// change reference array element}$ 1904 typeof( ar[1] ) p; $\C{// (gcc) is int, \ie the type of referenced object}$ 1905 typeof( &ar[1] ) q; $\C{// (gcc) is int \&, \ie the type of reference}$ 1906 sizeof( ar[1] ) == sizeof( int ); $\C{// is true, \ie the size of referenced object}$ 1907 sizeof( &ar[1] ) == sizeof( int *) $\C{// is true, \ie the size of a reference}$ 1854 1908 \end{cfa} 1855 1909 1856 1910 In contrast to \CFA reference types, \Index*[C++]{\CC{}}'s reference types are all ©const© references, preventing changes to the reference address, so only value assignment is possible, which eliminates half of the \Index{address duality}. 1857 1911 Also, \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 refer ant object.}1912 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 referent object.} 1859 1913 \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. 1860 1914 … … 1868 1922 Therefore, for pointer/reference initialization, the initializing value must be an address not a value. 1869 1923 \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}§1924 int * p = &x; $\C{// assign address of x}$ 1925 @int * p = x;@ $\C{// assign value of x}$ 1926 int & r = x; $\C{// must have address of x}$ 1873 1927 \end{cfa} 1874 1928 Like the previous example with C pointer-arithmetic, it is unlikely assigning the value of ©x© into a pointer is meaningful (again, a warning is usually given). … … 1879 1933 Similarly, 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. 1880 1934 \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}§1935 int & f( int & r ); $\C{// reference parameter and return}$ 1936 z = f( x ) + f( y ); $\C{// reference operator added, temporaries needed for call results}$ 1883 1937 \end{cfa} 1884 1938 Within routine ©f©, it is possible to change the argument by changing the corresponding parameter, and parameter ©r© can be locally reassigned within ©f©. … … 1893 1947 When a pointer/reference parameter has a ©const© value (immutable), it is possible to pass literals and expressions. 1894 1948 \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) );1949 void f( @const@ int & cr ); 1950 void g( @const@ int * cp ); 1951 f( 3 ); g( @&@3 ); 1952 f( x + y ); g( @&@(x + y) ); 1899 1953 \end{cfa} 1900 1954 Here, the compiler passes the address to the literal 3 or the temporary for the expression ©x + y©, knowing the argument cannot be changed through the parameter. … … 1907 1961 void f( int & r ); 1908 1962 void 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}§1963 f( 3 ); g( @&@3 ); $\C{// compiler implicit generates temporaries}$ 1964 f( x + y ); g( @&@(x + y) ); $\C{// compiler implicit generates temporaries}$ 1911 1965 \end{cfa} 1912 1966 Essentially, there is an implicit \Index{rvalue} to \Index{lvalue} conversion in this case.\footnote{ … … 1919 1973 \begin{cfa} 1920 1974 void 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}§1975 void (* fp)( int ); $\C{// routine pointer}$ 1976 fp = f; $\C{// reference initialization}$ 1977 fp = &f; $\C{// pointer initialization}$ 1978 fp = *f; $\C{// reference initialization}$ 1979 fp(3); $\C{// reference invocation}$ 1980 (*fp)(3); $\C{// pointer invocation}$ 1927 1981 \end{cfa} 1928 1982 While C's treatment of routine objects has similarity to inferring a reference type in initialization contexts, the examples are assignment not initialization, and all possible forms of assignment are possible (©f©, ©&f©, ©*f©) without regard for type. 1929 1983 Instead, a routine object should be referenced by a ©const© reference: 1930 1984 \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}§1985 @const@ void (@&@ fr)( int ) = f; $\C{// routine reference}$ 1986 fr = ... $\C{// error, cannot change code}$ 1987 &fr = ...; $\C{// changing routine reference}$ 1988 fr( 3 ); $\C{// reference call to f}$ 1989 (*fr)(3); $\C{// error, incorrect type}$ 1936 1990 \end{cfa} 1937 1991 because the value of the routine object is a routine literal, \ie the routine code is normally immutable during execution.\footnote{ … … 1946 2000 \begin{itemize} 1947 2001 \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).2002 if ©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). 2003 2004 \item 2005 if ©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). 1952 2006 \end{itemize} 1953 2007 The following example shows the first rule applied to different \Index{rvalue} contexts: … … 1955 2009 int x, * px, ** ppx, *** pppx, **** ppppx; 1956 2010 int & 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)}§2011 x = rrrx; $\C[2.0in]{// rrrx is an lvalue with type int \&\&\& (equivalent to x)}$ 2012 px = &rrrx; $\C{// starting from rrrx, \&rrrx is an rvalue with type int *\&\&\& (\&x)}$ 2013 ppx = &&rrrx; $\C{// starting from \&rrrx, \&\&rrrx is an rvalue with type int **\&\& (\&rx)}$ 2014 pppx = &&&rrrx; $\C{// starting from \&\&rrrx, \&\&\&rrrx is an rvalue with type int ***\& (\&rrx)}$ 2015 ppppx = &&&&rrrx; $\C{// starting from \&\&\&rrrx, \&\&\&\&rrrx is an rvalue with type int **** (\&rrrx)}$ 1962 2016 \end{cfa} 1963 2017 The following example shows the second rule applied to different \Index{lvalue} contexts: … … 1965 2019 int x, * px, ** ppx, *** pppx; 1966 2020 int & 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§2021 rrrx = 2; $\C{// rrrx is an lvalue with type int \&\&\& (equivalent to x)}$ 2022 &rrrx = px; $\C{// starting from rrrx, \&rrrx is an rvalue with type int *\&\&\& (rx)}$ 2023 &&rrrx = ppx; $\C{// starting from \&rrrx, \&\&rrrx is an rvalue with type int **\&\& (rrx)}$ 2024 &&&rrrx = pppx; $\C{// starting from \&\&rrrx, \&\&\&rrrx is an rvalue with type int ***\& (rrrx)}\CRT$ 1971 2025 \end{cfa} 1972 2026 … … 1981 2035 \begin{cfa} 1982 2036 int x; 1983 x + 1; §\C[2.0in]{// lvalue variable (int) converts to rvalue for expression}§2037 x + 1; $\C[2.0in]{// lvalue variable (int) converts to rvalue for expression}$ 1984 2038 \end{cfa} 1985 2039 An rvalue has no type qualifiers (©cv©), so the lvalue qualifiers are dropped. … … 1991 2045 \begin{cfa} 1992 2046 int x, &r = x, f( int p ); 1993 x = ®r® + f( ®r® ); §\C{// lvalue reference converts to rvalue}§2047 x = @r@ + f( @r@ ); $\C{// lvalue reference converts to rvalue}$ 1994 2048 \end{cfa} 1995 2049 An rvalue has no type qualifiers (©cv©), so the reference qualifiers are dropped. … … 1998 2052 lvalue to reference conversion: \lstinline[deletekeywords=lvalue]@lvalue-type cv1 T@ converts to ©cv2 T &©, which allows implicitly converting variables to references. 1999 2053 \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 \&)}§2054 int x, &r = @x@, f( int & p ); $\C{// lvalue variable (int) convert to reference (int \&)}$ 2055 f( @x@ ); $\C{// lvalue variable (int) convert to reference (int \&)}$ 2002 2056 \end{cfa} 2003 2057 Conversion can restrict a type, where ©cv1© $\le$ ©cv2©, \eg passing an ©int© to a ©const volatile int &©, which has low cost. … … 2009 2063 \begin{cfa} 2010 2064 int 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§ 2065 f( @x + 3@ ); $\C[1.5in]{// rvalue parameter (int) implicitly converts to lvalue temporary reference (int \&)}$ 2066 @&f@(...) = &x; $\C{// rvalue result (int \&) implicitly converts to lvalue temporary reference (int \&)}\CRT$ 2013 2067 \end{cfa} 2014 2068 In both case, modifications to the temporary are inaccessible (\Index{warning}). … … 2182 2236 The point of the new syntax is to allow returning multiple values from a routine~\cite{Galletly96,CLU}, \eg: 2183 2237 \begin{cfa} 2184 ®[ int o1, int o2, char o3 ]®f( int i1, char i2, char i3 ) {2185 §\emph{routine body}§2238 @[ int o1, int o2, char o3 ]@ f( int i1, char i2, char i3 ) { 2239 $\emph{routine body}$ 2186 2240 } 2187 2241 \end{cfa} … … 2194 2248 Declaration qualifiers can only appear at the start of a routine definition, \eg: 2195 2249 \begin{cfa} 2196 ®extern® [ int x ] g( int y ) {§\,§}2250 @extern@ [ int x ] g( int y ) {$\,$} 2197 2251 \end{cfa} 2198 2252 Lastly, if there are no output parameters or input parameters, the brackets and/or parentheses must still be specified; 2199 2253 in 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: 2200 2254 \begin{cfa} 2201 [ §\,§] g(); §\C{// no input or output parameters}§2202 [ void ] g( void ); §\C{// no input or output parameters}§2255 [$\,$] g(); $\C{// no input or output parameters}$ 2256 [ void ] g( void ); $\C{// no input or output parameters}$ 2203 2257 \end{cfa} 2204 2258 … … 2218 2272 \begin{cfa} 2219 2273 typedef int foo; 2220 int f( int (* foo) ); §\C{// foo is redefined as a parameter name}§2274 int f( int (* foo) ); $\C{// foo is redefined as a parameter name}$ 2221 2275 \end{cfa} 2222 2276 The string ``©int (* foo)©'' declares a C-style named-parameter of type pointer to an integer (the parenthesis are superfluous), while the same string declares a \CFA style unnamed parameter of type routine returning integer with unnamed parameter of type pointer to foo. … … 2226 2280 C-style declarations can be used to declare parameters for \CFA style routine definitions, \eg: 2227 2281 \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}§2282 [ int ] f( * int, int * ); $\C{// returns an integer, accepts 2 pointers to integers}$ 2283 [ * int, int * ] f( int ); $\C{// returns 2 pointers to integers, accepts an integer}$ 2230 2284 \end{cfa} 2231 2285 The 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: 2232 2286 \begin{cfa} 2233 2287 #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 ] )}§2288 int f( ptoa( p, 5 ) ) ... $\C{// expands to int f( int (*p)[ 5 ] )}$ 2289 [ int ] f( ptoa( p, 5 ) ) ... $\C{// expands to [ int ] f( int (*p)[ 5 ] )}$ 2236 2290 \end{cfa} 2237 2291 Again, programmers are highly encouraged to use one declaration form or the other, rather than mixing the forms. … … 2252 2306 \begin{minipage}{\linewidth} 2253 2307 \begin{cfa} 2254 ®[ int x, int y ]®f() {2308 @[ int x, int y ]@ f() { 2255 2309 int z; 2256 2310 ... x = 0; ... y = z; ... 2257 ®return;® §\C{// implicitly return x, y}§2311 @return;@ $\C{// implicitly return x, y}$ 2258 2312 } 2259 2313 \end{cfa} … … 2265 2319 [ int x, int y ] f() { 2266 2320 ... 2267 } §\C{// implicitly return x, y}§2321 } $\C{// implicitly return x, y}$ 2268 2322 \end{cfa} 2269 2323 In this case, the current values of ©x© and ©y© are returned to the calling routine just as if a ©return© had been encountered. … … 2274 2328 [ int x, int y ] f( int, x, int y ) { 2275 2329 ... 2276 } §\C{// implicitly return x, y}§2330 } $\C{// implicitly return x, y}$ 2277 2331 \end{cfa} 2278 2332 This notation allows the compiler to eliminate temporary variables in nested routine calls. 2279 2333 \begin{cfa} 2280 [ int x, int y ] f( int, x, int y ); §\C{// prototype declaration}§2334 [ int x, int y ] f( int, x, int y ); $\C{// prototype declaration}$ 2281 2335 int a, b; 2282 2336 [a, b] = f( f( f( a, b ) ) ); … … 2292 2346 as well, parameter names are optional, \eg: 2293 2347 \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}§2348 [ int x ] f (); $\C{// returning int with no parameters}$ 2349 [ * int ] g (int y); $\C{// returning pointer to int with int parameter}$ 2350 [ ] h ( int, char ); $\C{// returning no result with int and char parameters}$ 2351 [ * int, int ] j ( int ); $\C{// returning pointer to int and int, with int parameter}$ 2298 2352 \end{cfa} 2299 2353 This 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:2354 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: 2301 2355 \begin{cfa} 2302 2356 C : const double bar1(), bar2( int ), bar3( double ); 2303 §\CFA§: [const double] foo(), foo( int ), foo( double ) { return 3.0; }2357 $\CFA$: [const double] foo(), foo( int ), foo( double ) { return 3.0; } 2304 2358 \end{cfa} 2305 2359 \CFA allows the last routine in the list to define its body. … … 2316 2370 The syntax for pointers to \CFA routines specifies the pointer name on the right, \eg: 2317 2371 \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}§2372 * [ int x ] () fp; $\C[2.25in]{// pointer to routine returning int with no parameters}$ 2373 * [ * int ] (int y) gp; $\C{// pointer to routine returning pointer to int with int parameter}$ 2374 * [ ] (int,char) hp; $\C{// pointer to routine returning no result with int and char parameters}$ 2375 * [ * int,int ] ( int ) jp; $\C{// pointer to routine returning pointer to int and int, with int parameter}\CRT$ 2322 2376 \end{cfa} 2323 2377 While parameter names are optional, \emph{a routine name cannot be specified}; 2324 2378 for example, the following is incorrect: 2325 2379 \begin{cfa} 2326 * [ int x ] f () fp; §\C{// routine name "f" is not allowed}§2380 * [ int x ] f () fp; $\C{// routine name "f" is not allowed}$ 2327 2381 \end{cfa} 2328 2382 … … 2347 2401 whereas a named (keyword) call may be: 2348 2402 \begin{cfa} 2349 p( z : 3, x : 4, y : 7 ); §\C{// rewrite $\Rightarrow$ p( 4, 7, 3 )}§2403 p( z : 3, x : 4, y : 7 ); $\C{// rewrite \(\Rightarrow\) p( 4, 7, 3 )}$ 2350 2404 \end{cfa} 2351 2405 Here the order of the arguments is unimportant, and the names of the parameters are used to associate argument values with the corresponding parameters. … … 2364 2418 For example, the following routine prototypes and definition are all valid. 2365 2419 \begin{cfa} 2366 void p( int, int, int ); §\C{// equivalent prototypes}§2420 void p( int, int, int ); $\C{// equivalent prototypes}$ 2367 2421 void p( int x, int y, int z ); 2368 2422 void p( int y, int x, int z ); 2369 2423 void p( int z, int y, int x ); 2370 void p( int q, int r, int s ) {} §\C{// match with this definition}§2424 void p( int q, int r, int s ) {} $\C{// match with this definition}$ 2371 2425 \end{cfa} 2372 2426 Forcing matching parameter names in routine prototypes with corresponding routine definitions is possible, but goes against a strong tradition in C programming. … … 2380 2434 int f( int x, double y ); 2381 2435 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}§2436 f( j : 3, i : 4 ); $\C{// 1st f}$ 2437 f( x : 7, y : 8.1 ); $\C{// 2nd f}$ 2438 f( 4, 5 ); $\C{// ambiguous call}$ 2385 2439 \end{cfa} 2386 2440 However, named arguments compound routine resolution in conjunction with conversions: 2387 2441 \begin{cfa} 2388 f( i : 3, 5.7 ); §\C{// ambiguous call ?}§2442 f( i : 3, 5.7 ); $\C{// ambiguous call ?}$ 2389 2443 \end{cfa} 2390 2444 Depending on the cost associated with named arguments, this call could be resolvable or ambiguous. … … 2400 2454 the allowable positional calls are: 2401 2455 \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 )}§2456 p(); $\C{// rewrite \(\Rightarrow\) p( 1, 2, 3 )}$ 2457 p( 4 ); $\C{// rewrite \(\Rightarrow\) p( 4, 2, 3 )}$ 2458 p( 4, 4 ); $\C{// rewrite \(\Rightarrow\) p( 4, 4, 3 )}$ 2459 p( 4, 4, 4 ); $\C{// rewrite \(\Rightarrow\) p( 4, 4, 4 )}$ 2406 2460 // 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 )}§2461 p( , 4, 4 ); $\C{// rewrite \(\Rightarrow\) p( 1, 4, 4 )}$ 2462 p( 4, , 4 ); $\C{// rewrite \(\Rightarrow\) p( 4, 2, 4 )}$ 2463 p( 4, 4, ); $\C{// rewrite \(\Rightarrow\) p( 4, 4, 3 )}$ 2464 p( 4, , ); $\C{// rewrite \(\Rightarrow\) p( 4, 2, 3 )}$ 2465 p( , 4, ); $\C{// rewrite \(\Rightarrow\) p( 1, 4, 3 )}$ 2466 p( , , 4 ); $\C{// rewrite \(\Rightarrow\) p( 1, 2, 4 )}$ 2467 p( , , ); $\C{// rewrite \(\Rightarrow\) p( 1, 2, 3 )}$ 2414 2468 \end{cfa} 2415 2469 Here the missing arguments are inserted from the default values in the parameter list. … … 2435 2489 Default values may only appear in a prototype versus definition context: 2436 2490 \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}§2491 void p( int x, int y = 2, int z = 3 ); $\C{// prototype: allowed}$ 2492 void p( int, int = 2, int = 3 ); $\C{// prototype: allowed}$ 2493 void p( int x, int y = 2, int z = 3 ) {} $\C{// definition: not allowed}$ 2440 2494 \end{cfa} 2441 2495 The reason for this restriction is to allow separate compilation. … … 2452 2506 \begin{cfa} 2453 2507 p( 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 */, ... );}§2508 p( 1, 4, 5, 6, z : 3, y : 2 ); $\C{// assume p( /* positional */, ... , /* named */ );}$ 2509 p( 1, z : 3, y : 2, 4, 5, 6 ); $\C{// assume p( /* positional */, /* named */, ... );}$ 2456 2510 \end{cfa} 2457 2511 In 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. … … 2462 2516 \begin{cfa} 2463 2517 void 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 */, ... );}§2518 p( 1, 4, 5, 6, z : 3 ); $\C{// assume p( /* positional */, ... , /* named */ );}$ 2519 p( 1, z : 3, 4, 5, 6 ); $\C{// assume p( /* positional */, /* named */, ... );}$ 2466 2520 \end{cfa} 2467 2521 The first call is an error because arguments 4 and 5 are actually positional not ellipse arguments; … … 2469 2523 In 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. 2470 2524 For 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.2525 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{\VRef{s:Overloading}}, making much of this discussion moot. 2526 2527 Default arguments and overloading \see{\VRef{s:Overloading}} are complementary. 2474 2528 While in theory default arguments can be simulated with overloading, as in: 2475 2529 \begin{cquote} … … 2493 2547 Furthermore, overloading cannot handle accessing default arguments in the middle of a positional list, via a missing argument, such as: 2494 2548 \begin{cfa} 2495 p( 1, /* default */, 5 ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 5 )}§2549 p( 1, /* default */, 5 ); $\C{// rewrite \(\Rightarrow\) p( 1, 2, 5 )}$ 2496 2550 \end{cfa} 2497 2551 … … 2506 2560 \begin{cfa} 2507 2561 struct { 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}§2562 int f1; $\C{// named field}$ 2563 int f2 : 4; $\C{// named field with bit field size}$ 2564 int : 3; $\C{// unnamed field for basic type with bit field size}$ 2565 int ; $\C{// disallowed, unnamed field}$ 2566 int *; $\C{// disallowed, unnamed field}$ 2567 int (*)( int ); $\C{// disallowed, unnamed field}$ 2514 2568 }; 2515 2569 \end{cfa} … … 2519 2573 \begin{cfa} 2520 2574 struct { 2521 int , , ; §\C{// 3 unnamed fields}§2575 int , , ; $\C{// 3 unnamed fields}$ 2522 2576 } 2523 2577 \end{cfa} … … 2531 2585 \subsection{Type Nesting} 2532 2586 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.2587 \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. 2534 2588 \begin{figure} 2535 2589 \centering … … 2587 2641 2588 2642 int fred() { 2589 s.t.c = ®S.®R; // type qualification2590 struct ®S.®T t = { ®S.®R, 1, 2 };2591 enum ®S.®C c;2592 union ®S.T.®U u;2643 s.t.c = @S.@R; // type qualification 2644 struct @S.@T t = { @S.@R, 1, 2 }; 2645 enum @S.@C c; 2646 union @S.T.@U u; 2593 2647 } 2594 2648 \end{cfa} … … 2613 2667 const unsigned int size = 5; 2614 2668 int ia[size]; 2615 ... §\C{// assign values to array ia}§2616 qsort( ia, size ); §\C{// sort ascending order using builtin ?<?}§2669 ... $\C{// assign values to array ia}$ 2670 qsort( ia, size ); $\C{// sort ascending order using builtin ?<?}$ 2617 2671 { 2618 ®int ?<?( int x, int y ) { return x > y; }® §\C{// nested routine}§2619 qsort( ia, size ); §\C{// sort descending order by local redefinition}§2672 @int ?<?( int x, int y ) { return x > y; }@ $\C{// nested routine}$ 2673 qsort( ia, size ); $\C{// sort descending order by local redefinition}$ 2620 2674 } 2621 2675 \end{cfa} … … 2625 2679 The following program in undefined in \CFA (and Indexc{gcc}) 2626 2680 \begin{cfa} 2627 [* [int]( int )] foo() { §\C{// int (* foo())( int )}§2628 int ®i®= 7;2681 [* [int]( int )] foo() { $\C{// int (* foo())( int )}$ 2682 int @i@ = 7; 2629 2683 int bar( int p ) { 2630 ®i® += 1; §\C{// dependent on local variable}§2631 sout | ®i®;2684 @i@ += 1; $\C{// dependent on local variable}$ 2685 sout | @i@; 2632 2686 } 2633 return bar; §\C{// undefined because of local dependence}§2687 return bar; $\C{// undefined because of local dependence}$ 2634 2688 } 2635 2689 int main() { 2636 * [int]( int ) fp = foo(); §\C{// int (* fp)( int )}§2690 * [int]( int ) fp = foo(); $\C{// int (* fp)( int )}$ 2637 2691 sout | fp( 3 ); 2638 2692 } … … 2647 2701 In C and \CFA, lists of elements appear in several contexts, such as the parameter list of a routine call. 2648 2702 \begin{cfa} 2649 f( ®2, x, 3 + i® ); §\C{// element list}§2703 f( @2, x, 3 + i@ ); $\C{// element list}$ 2650 2704 \end{cfa} 2651 2705 A list of elements is called a \newterm{tuple}, and is different from a \Index{comma expression}. … … 2656 2710 2657 2711 In C and most programming languages, functions return at most one value; 2658 however, many operations have multiple outcomes, some exceptional (see~\VRef{s:ExceptionHandling}).2712 however, many operations have multiple outcomes, some exceptional \see{\VRef{s:ExceptionHandling}}. 2659 2713 To emulate functions with multiple return values, \emph{\Index{aggregation}} and/or \emph{\Index{aliasing}} is used. 2660 2714 … … 2662 2716 For example, consider C's \Indexc{div} function, which returns the quotient and remainder for a division of an integer value. 2663 2717 \begin{cfa} 2664 typedef struct { int quot, rem; } div_t; §\C[7cm]{// from include stdlib.h}§2718 typedef struct { int quot, rem; } div_t; $\C[7cm]{// from include stdlib.h}$ 2665 2719 div_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}§2720 div_t qr = div( 13, 5 ); $\C{// return quotient/remainder aggregate}$ 2721 printf( "%d %d\n", qr.quot, qr.rem ); $\C{// print quotient/remainder}$ 2668 2722 \end{cfa} 2669 2723 This approach requires a name for the return type and fields, where \Index{naming} is a common programming-language issue. … … 2675 2729 For example, consider C's \Indexc{modf} function, which returns the integral and fractional part of a floating value. 2676 2730 \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}§2731 double modf( double x, double * i ); $\C{// from include math.h}$ 2732 double intp, frac = modf( 13.5, &intp ); $\C{// return integral and fractional components}$ 2733 printf( "%g %g\n", intp, frac ); $\C{// print integral/fractional components}$ 2680 2734 \end{cfa} 2681 2735 This approach requires allocating storage for the return values, which complicates the call site with a sequence of variable declarations leading to the call. … … 2704 2758 When 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. 2705 2759 \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}§2760 void g( int, int ); $\C{// 1}$ 2761 void g( double, double ); $\C{// 2}$ 2762 g( div( 13, 5 ) ); $\C{// select 1}$ 2763 g( modf( 13.5 ) ); $\C{// select 2}$ 2710 2764 \end{cfa} 2711 2765 In this case, there are two overloaded ©g© routines. … … 2716 2770 The previous examples can be rewritten passing the multiple returned-values directly to the ©printf© function call. 2717 2771 \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}§2772 [ int, int ] div( int x, int y ); $\C{// from include stdlib}$ 2773 printf( "%d %d\n", div( 13, 5 ) ); $\C{// print quotient/remainder}$ 2774 2775 [ double, double ] modf( double x ); $\C{// from include math}$ 2776 printf( "%g %g\n", modf( 13.5 ) ); $\C{// print integral/fractional components}$ 2723 2777 \end{cfa} 2724 2778 This 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. … … 2730 2784 \begin{cfa} 2731 2785 int quot, rem; 2732 [ quot, rem ] = div( 13, 5 ); §\C{// assign multiple variables}§2733 printf( "%d %d\n", quot, rem ); §\C{// print quotient/remainder}\CRT§2786 [ quot, rem ] = div( 13, 5 ); $\C{// assign multiple variables}$ 2787 printf( "%d %d\n", quot, rem ); $\C{// print quotient/remainder}\CRT$ 2734 2788 \end{cfa} 2735 2789 Here, the multiple return-values are matched in much the same way as passing multiple return-values to multiple parameters in a call. … … 2760 2814 In \CFA, it is possible to overcome this restriction by declaring a \newterm{tuple variable}. 2761 2815 \begin{cfa} 2762 [int, int] ®qr® = div( 13, 5 ); §\C{// initialize tuple variable}§2763 printf( "%d %d\n", ®qr® ); §\C{// print quotient/remainder}§2816 [int, int] @qr@ = div( 13, 5 ); $\C{// initialize tuple variable}$ 2817 printf( "%d %d\n", @qr@ ); $\C{// print quotient/remainder}$ 2764 2818 \end{cfa} 2765 2819 It is now possible to match the multiple return-values to a single variable, in much the same way as \Index{aggregation}. … … 2767 2821 One way to access the individual components of a tuple variable is with assignment. 2768 2822 \begin{cfa} 2769 [ quot, rem ] = qr; §\C{// assign multiple variables}§2823 [ quot, rem ] = qr; $\C{// assign multiple variables}$ 2770 2824 \end{cfa} 2771 2825 … … 2790 2844 [int, double] * p; 2791 2845 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}§2846 int y = x.0; $\C{// access int component of x}$ 2847 y = f().1; $\C{// access int component of f}$ 2848 p->0 = 5; $\C{// access int component of tuple pointed-to by p}$ 2849 g( x.1, x.0 ); $\C{// rearrange x to pass to g}$ 2850 double z = [ x, f() ].0.1; $\C{// access second component of first component of tuple expression}$ 2797 2851 \end{cfa} 2798 2852 Tuple-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. … … 2801 2855 2802 2856 \subsection{Flattening and Structuring} 2857 \label{s:FlatteningStructuring} 2803 2858 2804 2859 As evident in previous examples, tuples in \CFA do not have a rigid structure. … … 2861 2916 double y; 2862 2917 [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}§2918 [y, x] = 3.14; $\C{// mass assignment}$ 2919 [x, y] = z; $\C{// multiple assignment}$ 2920 z = 10; $\C{// mass assignment}$ 2921 z = [x, y]; $\C{// multiple assignment}$ 2867 2922 \end{cfa} 2868 2923 Let $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. … … 2872 2927 \begin{cfa} 2873 2928 [ int, int ] x, y, z; 2874 [ x, y ] = z; §\C{// multiple assignment, invalid 4 != 2}§2929 [ x, y ] = z; $\C{// multiple assignment, invalid 4 != 2}$ 2875 2930 \end{cfa} 2876 2931 Multiple assignment assigns $R_i$ to $L_i$ for each $i$. … … 2908 2963 double c, d; 2909 2964 [ void ] f( [ int, int ] ); 2910 f( [ c, a ] = [ b, d ] = 1.5 ); §\C{// assignments in parameter list}§2965 f( [ c, a ] = [ b, d ] = 1.5 ); $\C{// assignments in parameter list}$ 2911 2966 \end{cfa} 2912 2967 The 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. … … 2921 2976 \begin{cfa} 2922 2977 struct 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}§2978 void ?{}(S *); $\C{// (1)}$ 2979 void ?{}(S *, int); $\C{// (2)}$ 2980 void ?{}(S * double); $\C{// (3)}$ 2981 void ?{}(S *, S); $\C{// (4)}$ 2982 2983 [S, S] x = [3, 6.28]; $\C{// uses (2), (3), specialized constructors}$ 2984 [S, S] y; $\C{// uses (1), (1), default constructor}$ 2985 [S, S] z = x.0; $\C{// uses (4), (4), copy constructor}$ 2931 2986 \end{cfa} 2932 2987 In 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)©. … … 2969 3024 A member-access tuple may be used anywhere a tuple can be used, \eg: 2970 3025 \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 )}§3026 s.[ y, z, x ] = [ 3, 3.2, 'x' ]; $\C{// equivalent to s.x = 'x', s.y = 3, s.z = 3.2}$ 3027 f( s.[ y, z ] ); $\C{// equivalent to f( s.y, s.z )}$ 2973 3028 \end{cfa} 2974 3029 Note, the fields appearing in a record-field tuple may be specified in any order; … … 2980 3035 void f( double, long ); 2981 3036 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 ]}§3037 f( x.[ 0, 3 ] ); $\C{// f( x.0, x.3 )}$ 3038 x.[ 0, 1 ] = x.[ 1, 0 ]; $\C{// [ x.0, x.1 ] = [ x.1, x.0 ]}$ 2984 3039 [ long, int, long ] y = x.[ 2, 0, 2 ]; 2985 3040 \end{cfa} … … 2998 3053 \begin{cfa} 2999 3054 [ int, float, double ] f(); 3000 [ double, float ] x = f().[ 2, 1 ]; §\C{// f() called once}§3055 [ double, float ] x = f().[ 2, 1 ]; $\C{// f() called once}$ 3001 3056 \end{cfa} 3002 3057 … … 3011 3066 That 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. 3012 3067 \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)}§3068 int f(); $\C{// (1)}$ 3069 double f(); $\C{// (2)}$ 3070 3071 f(); $\C{// ambiguous - (1),(2) both equally viable}$ 3072 (int)f(); $\C{// choose (2)}$ 3018 3073 \end{cfa} 3019 3074 Since casting is a fundamental operation in \CFA, casts need to be given a meaningful interpretation in the context of tuples. … … 3023 3078 void g(); 3024 3079 3025 (void)f(); §\C{// valid, ignore results}§3026 (int)g(); §\C{// invalid, void cannot be converted to int}§3080 (void)f(); $\C{// valid, ignore results}$ 3081 (int)g(); $\C{// invalid, void cannot be converted to int}$ 3027 3082 3028 3083 struct A { int x; }; 3029 (struct A)f(); §\C{// invalid, int cannot be converted to A}§3084 (struct A)f(); $\C{// invalid, int cannot be converted to A}$ 3030 3085 \end{cfa} 3031 3086 In C, line 4 is a valid cast, which calls ©f© and discards its result. … … 3043 3098 [int, [int, int], int] g(); 3044 3099 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}§3100 ([int, double])f(); $\C{// (1) valid}$ 3101 ([int, int, int])g(); $\C{// (2) valid}$ 3102 ([void, [int, int]])g(); $\C{// (3) valid}$ 3103 ([int, int, int, int])g(); $\C{// (4) invalid}$ 3104 ([int, [int, int, int]])g(); $\C{// (5) invalid}$ 3050 3105 \end{cfa} 3051 3106 … … 3107 3162 void f([int, int], int, int); 3108 3163 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}§3164 f([0, 0], 0, 0); $\C{// no cost}$ 3165 f(0, 0, 0, 0); $\C{// cost for structuring}$ 3166 f([0, 0,], [0, 0]); $\C{// cost for flattening}$ 3167 f([0, 0, 0], 0); $\C{// cost for flattening and structuring}$ 3113 3168 \end{cfa} 3114 3169 … … 3146 3201 The general syntax of a lexical list is: 3147 3202 \begin{cfa} 3148 [ §\emph{exprlist}§]3203 [ $\emph{exprlist}$ ] 3149 3204 \end{cfa} 3150 3205 where ©$\emph{exprlist}$© is a list of one or more expressions separated by commas. … … 3158 3213 Tuples are permitted to contain sub-tuples (\ie nesting), such as ©[ [ 14, 21 ], 9 ]©, which is a 2-element tuple whose first element is itself a tuple. 3159 3214 Note, 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).3215 a record denotes a single value with substructure, whereas a tuple is multiple values with no substructure \see{flattening coercion in \VRef{s:FlatteningStructuring}}. 3161 3216 In essence, tuples are largely a compile time phenomenon, having little or no runtime presence. 3162 3217 … … 3166 3221 The general syntax of a tuple type is: 3167 3222 \begin{cfa} 3168 [ §\emph{typelist}§]3223 [ $\emph{typelist}$ ] 3169 3224 \end{cfa} 3170 3225 where ©$\emph{typelist}$© is a list of one or more legal \CFA or C type specifications separated by commas, which may include other tuple type specifications. … … 3173 3228 [ unsigned int, char ] 3174 3229 [ double, double, double ] 3175 [ * int, int * ] §\C{// mix of CFA and ANSI}§3230 [ * int, int * ] $\C{// mix of CFA and ANSI}$ 3176 3231 [ * [ 5 ] int, * * char, * [ [ int, int ] ] (int, int) ] 3177 3232 \end{cfa} … … 3180 3235 Examples of declarations using tuple types are: 3181 3236 \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}§3237 [ int, int ] x; $\C{// 2 element tuple, each element of type int}$ 3238 * [ char, char ] y; $\C{// pointer to a 2 element tuple}$ 3184 3239 [ [ int, int ] ] z ([ int, int ]); 3185 3240 \end{cfa} … … 3198 3253 [ int, int ] w1; 3199 3254 [ 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}§3255 [ void ] f (int, int, int); $\C{// three input parameters of type int}$ 3256 [ void ] g ([ int, int, int ]); $\C{3 element tuple as input}$ 3202 3257 f( [ 1, 2, 3 ] ); 3203 3258 f( w1, 3 ); … … 3279 3334 [ int, int, int, int ] w = [ 1, 2, 3, 4 ]; 3280 3335 int x = 5; 3281 [ x, w ] = [ w, x ]; §\C{// all four tuple coercions}§3336 [ x, w ] = [ w, x ]; $\C{// all four tuple coercions}$ 3282 3337 \end{cfa} 3283 3338 Starting on the right-hand tuple in the last assignment statement, w is opened, producing a tuple of four values; … … 3285 3340 This tuple is then flattened, yielding ©[ 1, 2, 3, 4, 5 ]©, which is structured into ©[ 1, [ 2, 3, 4, 5 ] ]© to match the tuple type of the left-hand side. 3286 3341 The 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).3342 Finally, ©x© is assigned ©1© and ©w© is assigned the tuple value using \Index{multiple assignment} \see{\VRef{s:TupleAssignment}}. 3288 3343 \begin{rationale} 3289 3344 A possible additional language extension is to use the structuring coercion for tuples to initialize a complex record with a tuple. … … 3296 3351 Mass assignment has the following form: 3297 3352 \begin{cfa} 3298 [ §\emph{lvalue}§, ... , §\emph{lvalue}§ ] = §\emph{expr}§;3353 [ $\emph{lvalue}$, ... , $\emph{lvalue}$ ] = $\emph{expr}$; 3299 3354 \end{cfa} 3300 3355 \index{lvalue} … … 3336 3391 Multiple assignment has the following form: 3337 3392 \begin{cfa} 3338 [ §\emph{lvalue}§, ... , §\emph{lvalue}§ ] = [ §\emph{expr}§, ... , §\emph{expr}§];3393 [ $\emph{lvalue}$, ... , $\emph{lvalue}$ ] = [ $\emph{expr}$, ... , $\emph{expr}$ ]; 3339 3394 \end{cfa} 3340 3395 \index{lvalue} … … 3367 3422 both these examples produce indeterminate results: 3368 3423 \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}§3424 f( x++, x++ ); $\C{// C routine call with side effects in arguments}$ 3425 [ v1, v2 ] = [ x++, x++ ]; $\C{// side effects in right-hand side of multiple assignment}$ 3371 3426 \end{cfa} 3372 3427 … … 3377 3432 Cascade assignment has the following form: 3378 3433 \begin{cfa} 3379 §\emph{tuple}§ = §\emph{tuple}§ = ... = §\emph{tuple}§;3434 $\emph{tuple}$ = $\emph{tuple}$ = ... = $\emph{tuple}$; 3380 3435 \end{cfa} 3381 3436 and it has the same parallel semantics as for mass and multiple assignment. … … 3424 3479 \begin{cfa} 3425 3480 int x = 1, y = 2, z = 3; 3426 sout | x ®|® y ®|®z;3481 sout | x @|@ y @|@ z; 3427 3482 \end{cfa} 3428 3483 & 3429 3484 \begin{cfa} 3430 3485 3431 cout << x ®<< " "® << y ®<< " "®<< z << endl;3486 cout << x @<< " "@ << y @<< " "@ << z << endl; 3432 3487 \end{cfa} 3433 3488 & … … 3438 3493 \\ 3439 3494 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3440 1 ® ®2® ®33495 1@ @2@ @3 3441 3496 \end{cfa} 3442 3497 & 3443 3498 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3444 1 ® ®2® ®33499 1@ @2@ @3 3445 3500 \end{cfa} 3446 3501 & 3447 3502 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3448 1 ® ®2® ®33503 1@ @2@ @3 3449 3504 \end{cfa} 3450 3505 \end{tabular} … … 3454 3509 \begin{cfa} 3455 3510 [int, [ int, int ] ] t1 = [ 1, [ 2, 3 ] ], t2 = [ 4, [ 5, 6 ] ]; 3456 sout | t1 | t2; §\C{// print tuples}§3511 sout | t1 | t2; $\C{// print tuples}$ 3457 3512 \end{cfa} 3458 3513 \begin{cfa}[showspaces=true,aboveskip=0pt] 3459 1 ®, ®2®, ®3 4®, ®5®, ®63514 1@, @2@, @3 4@, @5@, @6 3460 3515 \end{cfa} 3461 3516 Finally, \CFA uses the logical-or operator for I/O as it is the lowest-priority \emph{overloadable} operator, other than assignment. … … 3466 3521 & 3467 3522 \begin{cfa} 3468 sout | x * 3 | y + 1 | z << 2 | x == y | ®(®x | y®)® | ®(®x || y®)® | ®(®x > z ? 1 : 2®)®;3523 sout | x * 3 | y + 1 | z << 2 | x == y | @(@x | y@)@ | @(@x || y@)@ | @(@x > z ? 1 : 2@)@; 3469 3524 \end{cfa} 3470 3525 \\ … … 3472 3527 & 3473 3528 \begin{cfa} 3474 cout << x * 3 << y + 1 << ®(®z << 2®)® << ®(®x == y®)® << ®(®x | y®)® << ®(®x || y®)® << ®(®x > z ? 1 : 2®)®<< endl;3529 cout << x * 3 << y + 1 << @(@z << 2@)@ << @(@x == y@)@ << @(@x | y@)@ << @(@x || y@)@ << @(@x > z ? 1 : 2@)@ << endl; 3475 3530 \end{cfa} 3476 3531 \\ … … 3507 3562 \\ 3508 3563 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3509 ®1® ®2.5® ®A® 3564 @1@ @2.5@ @A@ 3510 3565 3511 3566 … … 3513 3568 & 3514 3569 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3515 ®1® ®2.5® ®A® 3570 @1@ @2.5@ @A@ 3516 3571 3517 3572 … … 3519 3574 & 3520 3575 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3521 ®1® 3522 ®2.5® 3523 ®A® 3576 @1@ 3577 @2.5@ 3578 @A@ 3524 3579 \end{cfa} 3525 3580 \end{tabular} … … 3557 3612 3558 3613 \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] 3614 A 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. 3615 \begin{cfa} 3562 3616 sout | 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®}®x3567 \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.3617 | 7 | "$\LstStringStyle{\textcent}$ x" | 8 | "$\LstStringStyle{\guillemotright}$ x" | 9 | ") x" | 10 | "] x" | 11 | "} x"; 3618 \end{cfa} 3619 \begin{cfa}[showspaces=true] 3620 1@,@ 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 3621 \end{cfa} 3622 3623 \item 3624 A 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. 3571 3625 %$ 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;3626 \begin{cfa} 3627 sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x $" | 5 | "x $\LstStringStyle{\textsterling}$" | 6 | "x $\LstStringStyle{\textyen}$" 3628 | 7 | "x $\LstStringStyle{\textexclamdown}$" | 8 | "x $\LstStringStyle{\textquestiondown}$" | 9 | "x $\LstStringStyle{\guillemotleft}$" | 10; 3575 3629 \end{cfa} 3576 3630 %$ 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 ®«®103631 \begin{cfa}[showspaces=true] 3632 x @(@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 3579 3633 \end{cfa} 3580 3634 %$ 3581 3635 3582 3636 \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]3637 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} 3638 \begin{cfa} 3585 3639 sout | "x`" | 1 | "`x'" | 2 | "'x\"" | 3 | "\"x:" | 4 | ":x " | 5 | " x\t" | 6 | "\tx"; 3586 3640 \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® ®x3641 \begin{cfa}[showspaces=true,showtabs=true] 3642 x@`@1@`@x$\R{\texttt{'}}$2$\R{\texttt{'}}$x$\R{\texttt{"}}$3$\R{\texttt{"}}$x@:@4@:@x@ @5@ @x@ @6@ @x 3589 3643 \end{cfa} 3590 3644 3591 3645 \item 3592 3646 If 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:® ®43647 \begin{cfa} 3648 sout | "x ($\R{\texttt{\textvisiblespace}}$" | 1 | "$\R{\texttt{\textvisiblespace}}$) x" | 2 | "$\R{\texttt{\textvisiblespace}}$, x" | 3 | "$\R{\texttt{\textvisiblespace}}$:x:$\R{\texttt{\textvisiblespace}}$" | 4; 3649 \end{cfa} 3650 \begin{cfa}[showspaces=true,showtabs=true] 3651 x (@ @1@ @) x 2@ @, x 3@ @:x:@ @4 3598 3652 \end{cfa} 3599 3653 \end{enumerate} … … 3608 3662 \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. 3609 3663 The 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®| "\"";3664 \begin{cfa}[escapechar=off,belowskip=0pt] 3665 sepSet( sout, ", $" ); $\C{// set separator from " " to ", \$"}$ 3666 sout | 1 | 2 | 3 | " \"" | @sep@ | "\""; 3613 3667 \end{cfa} 3614 3668 %$ 3615 3669 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt] 3616 1 ®, $®2®, $®3 ®", $"®3670 1@, $@2@, $@3 @", $"@ 3617 3671 \end{cfa} 3618 3672 %$ 3619 3673 \begin{cfa}[belowskip=0pt] 3620 sepSet( sout, " " ); §\C{// reset separator to " "}§3621 sout | 1 | 2 | 3 | " \"" | ®sepGet( sout )®| "\"";3674 sepSet( sout, " " ); $\C{// reset separator to " "}$ 3675 sout | 1 | 2 | 3 | " \"" | @sepGet( sout )@ | "\""; 3622 3676 \end{cfa} 3623 3677 \begin{cfa}[showspaces=true,aboveskip=0pt] 3624 1 ® ®2® ®3 ®" "®3678 1@ @2@ @3 @" "@ 3625 3679 \end{cfa} 3626 3680 ©sepGet© can be used to store a separator and then restore it: 3627 3681 \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}§3682 char store[@sepSize@]; $\C{// sepSize is the maximum separator size}$ 3683 strcpy( store, sepGet( sout ) ); $\C{// copy current separator}$ 3684 sepSet( sout, "_" ); $\C{// change separator to underscore}$ 3631 3685 sout | 1 | 2 | 3; 3632 3686 \end{cfa} 3633 3687 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3634 1 ®_®2®_®33688 1@_@2@_@3 3635 3689 \end{cfa} 3636 3690 \begin{cfa}[belowskip=0pt] 3637 sepSet( sout, store ); §\C{// change separator back to original}§3691 sepSet( sout, store ); $\C{// change separator back to original}$ 3638 3692 sout | 1 | 2 | 3; 3639 3693 \end{cfa} 3640 3694 \begin{cfa}[showspaces=true,aboveskip=0pt] 3641 1 ® ®2® ®33695 1@ @2@ @3 3642 3696 \end{cfa} 3643 3697 … … 3646 3700 The tuple separator-string can be at most 16 characters including the ©'\0'© string terminator (15 printable characters). 3647 3701 \begin{cfa}[belowskip=0pt] 3648 sepSetTuple( sout, " " ); §\C{// set tuple separator from ", " to " "}§3649 sout | t1 | t2 | " \"" | ®sepTuple®| "\"";3702 sepSetTuple( sout, " " ); $\C{// set tuple separator from ", " to " "}$ 3703 sout | t1 | t2 | " \"" | @sepTuple@ | "\""; 3650 3704 \end{cfa} 3651 3705 \begin{cfa}[showspaces=true,aboveskip=0pt] 3652 1 2 3 4 5 6 ®" "®3706 1 2 3 4 5 6 @" "@ 3653 3707 \end{cfa} 3654 3708 \begin{cfa}[belowskip=0pt] 3655 sepSetTuple( sout, ", " ); §\C{// reset tuple separator to ", "}§3656 sout | t1 | t2 | " \"" | ®sepGetTuple( sout )®| "\"";3709 sepSetTuple( sout, ", " ); $\C{// reset tuple separator to ", "}$ 3710 sout | t1 | t2 | " \"" | @sepGetTuple( sout )@ | "\""; 3657 3711 \end{cfa} 3658 3712 \begin{cfa}[showspaces=true,aboveskip=0pt] 3659 1, 2, 3 4, 5, 6 ®", "®3713 1, 2, 3 4, 5, 6 @", "@ 3660 3714 \end{cfa} 3661 3715 As for ©sepGet©, ©sepGetTuple© can be use to store a tuple separator and then restore it. … … 3664 3718 \Indexc{sepDisable}\index{manipulator!sepDisable@©sepDisable©} and \Indexc{sepEnable}\index{manipulator!sepEnable@©sepEnable©} toggle printing the separator. 3665 3719 \begin{cfa}[belowskip=0pt] 3666 sout | sepDisable | 1 | 2 | 3; §\C{// turn off implicit separator}§3720 sout | sepDisable | 1 | 2 | 3; $\C{// turn off implicit separator}$ 3667 3721 \end{cfa} 3668 3722 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3670 3724 \end{cfa} 3671 3725 \begin{cfa}[belowskip=0pt] 3672 sout | sepEnable | 1 | 2 | 3; §\C{// turn on implicit separator}§3726 sout | sepEnable | 1 | 2 | 3; $\C{// turn on implicit separator}$ 3673 3727 \end{cfa} 3674 3728 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3679 3733 \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. 3680 3734 \begin{cfa}[belowskip=0pt] 3681 sout | 1 | sepOff | 2 | 3; §\C{// turn off implicit separator for the next item}§3735 sout | 1 | sepOff | 2 | 3; $\C{// turn off implicit separator for the next item}$ 3682 3736 \end{cfa} 3683 3737 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3685 3739 \end{cfa} 3686 3740 \begin{cfa}[belowskip=0pt] 3687 sout | sepDisable | 1 | sepOn | 2 | 3; §\C{// turn on implicit separator for the next item}§3741 sout | sepDisable | 1 | sepOn | 2 | 3; $\C{// turn on implicit separator for the next item}$ 3688 3742 \end{cfa} 3689 3743 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3692 3746 The tuple separator also responses to being turned on and off. 3693 3747 \begin{cfa}[belowskip=0pt] 3694 sout | t1 | sepOff | t2; §\C{// turn off implicit separator for the next item}§3748 sout | t1 | sepOff | t2; $\C{// turn off implicit separator for the next item}$ 3695 3749 \end{cfa} 3696 3750 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3700 3754 use ©sep© to accomplish this functionality. 3701 3755 \begin{cfa}[belowskip=0pt] 3702 sout | sepOn | 1 | 2 | 3 | sepOn; §\C{// sepOn does nothing at start/end of line}§3756 sout | sepOn | 1 | 2 | 3 | sepOn; $\C{// sepOn does nothing at start/end of line}$ 3703 3757 \end{cfa} 3704 3758 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3706 3760 \end{cfa} 3707 3761 \begin{cfa}[belowskip=0pt] 3708 sout | sep | 1 | 2 | 3 | sep ; §\C{// use sep to print separator at start/end of line}§3762 sout | sep | 1 | 2 | 3 | sep ; $\C{// use sep to print separator at start/end of line}$ 3709 3763 \end{cfa} 3710 3764 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3711 ® ®1 2 3® ® 3765 @ @1 2 3@ @ 3712 3766 \end{cfa} 3713 3767 \end{enumerate} … … 3721 3775 \begin{enumerate}[parsep=0pt] 3722 3776 \item 3723 \Indexc{nl}\index{manipulator!nl@©nl©} scans characters until the next newline character, i.e.,ignore the remaining characters in the line.3777 \Indexc{nl}\index{manipulator!nl@©nl©} scans characters until the next newline character, \ie ignore the remaining characters in the line. 3724 3778 \item 3725 3779 \Indexc{nlOn}\index{manipulator!nlOn@©nlOn©} reads the newline character, when reading single characters. … … 3729 3783 For example, in: 3730 3784 \begin{cfa} 3731 sin | i | ®nl®| j;3732 1 ®2®3785 sin | i | @nl@ | j; 3786 1 @2@ 3733 3787 3 3734 3788 \end{cfa} … … 3740 3794 \Indexc{nl}\index{manipulator!nl@©nl©} inserts a newline. 3741 3795 \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}§3796 sout | nl; $\C{// only print newline}$ 3797 sout | 2; $\C{// implicit newline}$ 3798 sout | 3 | nl | 4 | nl; $\C{// terminating nl merged with implicit newline}$ 3799 sout | 5 | nl | nl; $\C{// again terminating nl merged with implicit newline}$ 3800 sout | 6; $\C{// implicit newline}$ 3747 3801 3748 3802 2 … … 3771 3825 0b0 0b11011 0b11011 0b11011 0b11011 3772 3826 sout | bin( -27HH ) | bin( -27H ) | bin( -27 ) | bin( -27L ); 3773 0b11100101 0b1111111111100101 0b11111111111111111111111111100101 0b ®(58 1s)®1001013827 0b11100101 0b1111111111100101 0b11111111111111111111111111100101 0b@(58 1s)@100101 3774 3828 \end{cfa} 3775 3829 … … 3810 3864 \begin{cfa}[belowskip=0pt] 3811 3865 sout | 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®+43866 0@B@11011 0@X@1@B@ 2.75@E@-09 0@X@1.@B@8@P@+4 3813 3867 \end{cfa} 3814 3868 … … 3826 3880 \begin{cfa}[belowskip=0pt] 3827 3881 sout | 0. | nodp( 0. ) | 27.0 | nodp( 27.0 ) | nodp( 27.5 ); 3828 0.0 ®0® 27.0 ®27®27.53882 0.0 @0@ 27.0 @27@ 27.5 3829 3883 \end{cfa} 3830 3884 … … 3833 3887 \begin{cfa}[belowskip=0pt] 3834 3888 sout | sign( 27 ) | sign( -27 ) | sign( 27. ) | sign( -27. ) | sign( 27.5 ) | sign( -27.5 ); 3835 ®+®27 -27 ®+®27.0 -27.0 ®+®27.5 -27.53889 @+@27 -27 @+@27.0 -27.0 @+@27.5 -27.5 3836 3890 \end{cfa} 3837 3891 … … 3846 3900 \end{cfa} 3847 3901 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3848 ® ®34 ® ®34 343849 ® ®4.000000 ® ®4.000000 4.0000003850 ® ®ab ® ®ab ab3902 @ @34 @ @34 34 3903 @ @4.000000 @ @4.000000 4.000000 3904 @ @ab @ @ab ab 3851 3905 \end{cfa} 3852 3906 If the value is larger, it is printed without truncation, ignoring the ©minimum©. … … 3857 3911 \end{cfa} 3858 3912 \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®3913 3456@7@ 345@67@ 34@567@ 3914 3456@.@ 345@6.@ 34@56.@ 3915 abcd@e@ abc@de@ ab@cde@ 3862 3916 \end{cfa} 3863 3917 … … 3868 3922 \end{cfa} 3869 3923 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3870 ®0®34 ®00®34 ®00000000®343924 @0@34 @00@34 @00000000@34 3871 3925 \end{cfa} 3872 3926 If the value is larger, it is printed without truncation, ignoring the ©precision©. … … 3883 3937 \end{cfa} 3884 3938 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3885 ® ® ®00000000®343939 @ @ @00000000@34 3886 3940 \end{cfa} 3887 3941 For floating-point types, ©precision© is the minimum number of digits after the decimal point. … … 3890 3944 \end{cfa} 3891 3945 \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 trunca red if it exceeds the maximum.3946 27.@500@ 27.@5@ 28. 27.@50000000@ 3947 \end{cfa} 3948 For the C-string type, ©precision© is the maximum number of printed characters, so the string is truncated if it exceeds the maximum. 3895 3949 \begin{cfa}[belowskip=0pt] 3896 3950 sout | wd( 6,8, "abcd" ) | wd( 6,8, "abcdefghijk" ) | wd( 6,3, "abcd" ); … … 3908 3962 \end{cfa} 3909 3963 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3910 234.567 234.5 ®7® 234.®6® 23®5®3964 234.567 234.5@7@ 234.@6@ 23@5@ 3911 3965 \end{cfa} 3912 3966 If a value's magnitude is greater than ©significant©, the value is printed in scientific notation with the specified number of significant digits. … … 3915 3969 \end{cfa} 3916 3970 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3917 234567. 2.3457 ®e+05® 2.346®e+05® 2.35®e+05®3971 234567. 2.3457@e+05@ 2.346@e+05@ 2.35@e+05@ 3918 3972 \end{cfa} 3919 3973 If ©significant© is greater than ©minimum©, it defines the number of printed characters. … … 3931 3985 \end{cfa} 3932 3986 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3933 27 ® ® 27.000000 27.500000 027 27.500® ®3987 27@ @ 27.000000 27.500000 027 27.500@ @ 3934 3988 \end{cfa} 3935 3989 … … 3938 3992 \begin{cfa}[belowskip=0pt] 3939 3993 sout | pad0( wd( 4, 27 ) ) | pad0( wd( 4,3, 27 ) ) | pad0( wd( 8,3, 27.5 ) ); 3940 ®00®27 ®0®27 ®00®27.5003994 @00@27 @0@27 @00@27.500 3941 3995 \end{cfa} 3942 3996 \end{enumerate} … … 4034 4088 \end{cfa} 4035 4089 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 4036 ®abc ® 4037 ®abc ® 4038 ®xx® 4090 @abc @ 4091 @abc @ 4092 @xx@ 4039 4093 \end{cfa} 4040 4094 … … 4047 4101 \end{cfa} 4048 4102 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 4049 ®abcd1233.456E+2® 4103 @abcd1233.456E+2@ 4050 4104 \end{cfa} 4051 4105 Note, input ©wdi© cannot be overloaded with output ©wd© because both have the same parameters but return different types. … … 4060 4114 \end{cfa} 4061 4115 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 4062 ® -75.35e-4®254116 @ -75.35e-4@ 25 4063 4117 \end{cfa} 4064 4118 … … 4072 4126 \end{cfa} 4073 4127 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 4074 ®bca®xyz4128 @bca@xyz 4075 4129 \end{cfa} 4076 4130 … … 4084 4138 \end{cfa} 4085 4139 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 4086 ®xyz®bca4140 @xyz@bca 4087 4141 \end{cfa} 4088 4142 \end{enumerate} … … 4101 4155 4102 4156 A 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).4157 This means that users can define distinct function overloads for the new type \see{\VRef{s:Overloading} for more information}. 4104 4158 For example: 4105 4159 … … 4207 4261 \CFA supports C initialization of structures, but it also adds constructors for more advanced initialization. 4208 4262 Additionally, \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).4263 These functions take a reference to the structure as a parameter \see{\VRef{s:PointerReference} for more information}. 4210 4264 4211 4265 \begin{figure} … … 4258 4312 4259 4313 \section{Overloading} 4314 \label{s:Overloading} 4260 4315 4261 4316 Overloading refers to the capability of a programmer to define and use multiple objects in a program with the same name. … … 4468 4523 For example, given 4469 4524 \begin{cfa} 4470 auto j = ®...®4525 auto j = @...@ 4471 4526 \end{cfa} 4472 4527 and the need to write a routine to compute using ©j© 4473 4528 \begin{cfa} 4474 void rtn( ®...®parm );4529 void rtn( @...@ parm ); 4475 4530 rtn( j ); 4476 4531 \end{cfa} … … 4713 4768 4714 4769 coroutine Fibonacci { 4715 int fn; §\C{// used for communication}§4770 int fn; $\C{// used for communication}$ 4716 4771 }; 4717 4772 void ?{}( Fibonacci * this ) { … … 4719 4774 } 4720 4775 void main( Fibonacci * this ) { 4721 int fn1, fn2; §\C{// retained between resumes}§4722 this->fn = 0; §\C{// case 0}§4776 int fn1, fn2; $\C{// retained between resumes}$ 4777 this->fn = 0; $\C{// case 0}$ 4723 4778 fn1 = this->fn; 4724 suspend(); §\C{// return to last resume}§4725 4726 this->fn = 1; §\C{// case 1}§4779 suspend(); $\C{// return to last resume}$ 4780 4781 this->fn = 1; $\C{// case 1}$ 4727 4782 fn2 = fn1; 4728 4783 fn1 = this->fn; 4729 suspend(); §\C{// return to last resume}§4730 4731 for ( ;; ) { §\C{// general case}§4784 suspend(); $\C{// return to last resume}$ 4785 4786 for ( ;; ) { $\C{// general case}$ 4732 4787 this->fn = fn1 + fn2; 4733 4788 fn2 = fn1; 4734 4789 fn1 = this->fn; 4735 suspend(); §\C{// return to last resume}§4790 suspend(); $\C{// return to last resume}$ 4736 4791 } // for 4737 4792 } 4738 4793 int next( Fibonacci * this ) { 4739 resume( this ); §\C{// transfer to last suspend}§4794 resume( this ); $\C{// transfer to last suspend}$ 4740 4795 return this->fn; 4741 4796 } … … 4964 5019 When 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. 4965 5020 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).5021 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{\VRef{s:Interoperability} for more information}. 4967 5022 4968 5023 … … 6282 6337 In \CFA, there are ambiguous cases with dereference and operator identifiers, \eg ©int *?*?()©, where the string ©*?*?© can be interpreted as: 6283 6338 \begin{cfa} 6284 *? §\color{red}\textvisiblespace§*? §\C{// dereference operator, dereference operator}§6285 * §\color{red}\textvisiblespace§?*? §\C{// dereference, multiplication operator}§6339 *?$\R{\textvisiblespace}$*? $\C{// dereference operator, dereference operator}$ 6340 *$\R{\textvisiblespace}$?*? $\C{// dereference, multiplication operator}$ 6286 6341 \end{cfa} 6287 6342 By default, the first interpretation is selected, which does not yield a meaningful parse. … … 6292 6347 The ambiguity occurs when the deference operator has no parameters: 6293 6348 \begin{cfa} 6294 *?() §\color{red}\textvisiblespace...§;6295 *?() §\color{red}\textvisiblespace...§(...) ;6349 *?()$\R{\textvisiblespace...}$ ; 6350 *?()$\R{\textvisiblespace...}$(...) ; 6296 6351 \end{cfa} 6297 6352 requiring arbitrary whitespace look-ahead for the routine-call parameter-list to disambiguate. … … 6301 6356 The remaining cases are with the increment/decrement operators and conditional expression, \eg: 6302 6357 \begin{cfa} 6303 i++? §\color{red}\textvisiblespace...§(...);6304 i?++ §\color{red}\textvisiblespace...§(...);6358 i++?$\R{\textvisiblespace...}$(...); 6359 i?++$\R{\textvisiblespace...}$(...); 6305 6360 \end{cfa} 6306 6361 requiring arbitrary whitespace look-ahead for the operator parameter-list, even though that interpretation is an incorrect expression (juxtaposed identifiers). 6307 6362 Therefore, it is necessary to disambiguate these cases with a space: 6308 6363 \begin{cfa} 6309 i++ §\color{red}\textvisiblespace§? i : 0;6310 i? §\color{red}\textvisiblespace§++i : 0;6364 i++$\R{\textvisiblespace}$? i : 0; 6365 i?$\R{\textvisiblespace}$++i : 0; 6311 6366 \end{cfa} 6312 6367 … … 6321 6376 \begin{description} 6322 6377 \item[Change:] add new keywords \\ 6323 New keywords are added to \CFA (see~\VRef{s:CFAKeywords}).6378 New keywords are added to \CFA \see{\VRef{s:CFAKeywords}}. 6324 6379 \item[Rationale:] keywords added to implement new semantics of \CFA. 6325 6380 \item[Effect on original feature:] change to semantics of well-defined feature. \\ 6326 6381 Any \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}).6382 \item[Difficulty of converting:] keyword clashes are accommodated by syntactic transformations using the \CFA backquote escape-mechanism \see{\VRef{s:BackquoteIdentifiers}}. 6328 6383 \item[How widely used:] clashes among new \CFA keywords and existing identifiers are rare. 6329 6384 \end{description} … … 6335 6390 \eg: 6336 6391 \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 );}§6392 x; $\C{// int x}$ 6393 *y; $\C{// int *y}$ 6394 f( p1, p2 ); $\C{// int f( int p1, int p2 );}$ 6395 g( p1, p2 ) int p1, p2; $\C{// int g( int p1, int p2 );}$ 6341 6396 \end{cfa} 6342 6397 \CFA continues to support K\&R routine definitions: 6343 6398 \begin{cfa} 6344 f( a, b, c ) §\C{// default int return}§6345 int a, b; char c §\C{// K\&R parameter declarations}§6399 f( a, b, c ) $\C{// default int return}$ 6400 int a, b; char c $\C{// K\&R parameter declarations}$ 6346 6401 { 6347 6402 ... … … 6362 6417 int rtn( int i ); 6363 6418 int rtn( char c ); 6364 rtn( 'x' ); §\C{// programmer expects 2nd rtn to be called}§6419 rtn( 'x' ); $\C{// programmer expects 2nd rtn to be called}$ 6365 6420 \end{cfa} 6366 6421 \item[Rationale:] it is more intuitive for the call to ©rtn© to match the second version of definition of ©rtn© rather than the first. … … 6384 6439 \item[Change:] make string literals ©const©: 6385 6440 \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}§6441 char * p = "abc"; $\C{// valid in C, deprecated in \CFA}$ 6442 char * q = expr ? "abc" : "de"; $\C{// valid in C, invalid in \CFA}$ 6388 6443 \end{cfa} 6389 6444 The type of a string literal is changed from ©[] char© to ©const [] char©. … … 6392 6447 \begin{cfa} 6393 6448 char * p = "abc"; 6394 p[0] = 'w'; §\C{// segment fault or change constant literal}§6449 p[0] = 'w'; $\C{// segment fault or change constant literal}$ 6395 6450 \end{cfa} 6396 6451 The same problem occurs when passing a string literal to a routine that changes its argument. … … 6404 6459 \item[Change:] remove \newterm{tentative definitions}, which only occurs at file scope: 6405 6460 \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}§6461 int i; $\C{// forward definition}$ 6462 int *j = @&i@; $\C{// forward reference, valid in C, invalid in \CFA}$ 6463 int i = 0; $\C{// definition}$ 6409 6464 \end{cfa} 6410 6465 is valid in C, and invalid in \CFA because duplicate overloaded object definitions at the same scope level are disallowed. … … 6412 6467 \begin{cfa} 6413 6468 struct 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}§6469 static struct X a; $\C{// forward definition}$ 6470 static struct X b = { 0, @&a@ };$\C{// forward reference, valid in C, invalid in \CFA}$ 6471 static struct X a = { 1, &b }; $\C{// definition}$ 6417 6472 \end{cfa} 6418 6473 \item[Rationale:] avoids having different initialization rules for builtin types and user-defined types. … … 6426 6481 \item[Change:] have ©struct© introduce a scope for nested types: 6427 6482 \begin{cfa} 6428 enum ®Colour®{ R, G, B, Y, C, M };6483 enum @Colour@ { R, G, B, Y, C, M }; 6429 6484 struct 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)}§6485 enum @Colour@ { R, G, B }; $\C[7cm]{// nested type}$ 6486 struct Face { $\C{// nested type}$ 6487 @Colour@ Eyes, Hair; $\C{// type defined outside (1 level)}$ 6433 6488 }; 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}§6489 @.Colour@ shirt; $\C{// type defined outside (top level)}$ 6490 @Colour@ pants; $\C{// type defined same level}$ 6491 Face looks[10]; $\C{// type defined same level}$ 6437 6492 }; 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§6493 @Colour@ c = R; $\C{// type/enum defined same level}$ 6494 Person@.Colour@ pc = Person@.@R;$\C{// type/enum defined inside}$ 6495 Person@.@Face pretty; $\C{// type defined inside}\CRT$ 6441 6496 \end{cfa} 6442 6497 In 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. … … 6455 6510 \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: 6456 6511 \begin{cfa} 6457 struct Y; §\C{// struct Y and struct X are at the same scope}§6512 struct Y; $\C{// struct Y and struct X are at the same scope}$ 6458 6513 struct X { 6459 6514 struct Y { /* ... */ } y; … … 6470 6525 \begin{cfa} 6471 6526 void 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 *}§6527 int * b = malloc( sizeof(int) ); $\C{// implicitly convert void * to int *}$ 6528 char * c = b; $\C{// implicitly convert int * to void *, and then void * to char *}$ 6474 6529 } 6475 6530 \end{cfa} 6476 6531 \item[Rationale:] increase type safety 6477 6532 \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):6533 \item[Difficulty of converting:] requires adding a cast \see{\VRef{s:StorageManagement} for better alternatives}: 6479 6534 \begin{cfa} 6480 6535 int * b = (int *)malloc( sizeof(int) ); … … 6586 6641 \end{cquote} 6587 6642 For 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}).6643 hence, names in these include files are not mangled\index{mangling!name} \see{\VRef{s:Interoperability}}. 6589 6644 All other C header files must be explicitly wrapped in ©extern "C"© to prevent name mangling. 6590 6645 This 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. … … 6649 6704 Type-safe allocation is provided for all C allocation routines and new \CFA allocation routines, \eg in 6650 6705 \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}§6706 int * ip = (int *)malloc( sizeof(int) ); $\C{// C}$ 6707 int * ip = malloc(); $\C{// \CFA type-safe version of C malloc}$ 6708 int * ip = alloc(); $\C{// \CFA type-safe uniform alloc}$ 6654 6709 \end{cfa} 6655 6710 the latter two allocations determine the allocation size from the type of ©p© (©int©) and cast the pointer to the allocated storage to ©int *©. … … 6658 6713 \begin{cfa} 6659 6714 struct S { int i; } __attribute__(( aligned( 128 ) )); // cache-line alignment 6660 S * sp = malloc(); §\C{// honour type alignment}§6715 S * sp = malloc(); $\C{// honour type alignment}$ 6661 6716 \end{cfa} 6662 6717 the storage allocation is implicitly aligned to 128 rather than the default 16. … … 6673 6728 \CFA memory management extends allocation to support constructors for initialization of allocated storage, \eg in 6674 6729 \begin{cfa} 6675 struct S { int i; }; §\C{// cache-line aglinment}§6730 struct S { int i; }; $\C{// cache-line alignment}$ 6676 6731 void ?{}( S & s, int i ) { s.i = i; } 6677 6732 // assume ?|? operator for printing an S 6678 6733 6679 S & sp = * ®new®( 3 ); §\C{// call constructor after allocation}§6734 S & sp = *@new@( 3 ); $\C{// call constructor after allocation}$ 6680 6735 sout | sp.i; 6681 ®delete®( &sp );6682 6683 S * spa = ®anew®( 10, 5 ); §\C{// allocate array and initialize each array element}§6736 @delete@( &sp ); 6737 6738 S * spa = @anew@( 10, 5 ); $\C{// allocate array and initialize each array element}$ 6684 6739 for ( i; 10 ) sout | spa[i] | nonl; 6685 6740 sout | nl; 6686 ®adelete®( 10, spa );6741 @adelete@( 10, spa ); 6687 6742 \end{cfa} 6688 6743 Allocation routines ©new©/©anew© allocate a variable/array and initialize storage using the allocated type's constructor. … … 6693 6748 extern "C" { 6694 6749 // 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}§// CFA6750 void * malloc( size_t size );$\indexc{malloc}$ 6751 void * calloc( size_t dim, size_t size );$\indexc{calloc}$ 6752 void * realloc( void * ptr, size_t size );$\indexc{realloc}$ 6753 void * memalign( size_t align, size_t size );$\indexc{memalign}$ 6754 void * aligned_alloc( size_t align, size_t size );$\indexc{aligned_alloc}$ 6755 int posix_memalign( void ** ptr, size_t align, size_t size );$\indexc{posix_memalign}$ 6756 void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize );$\indexc{cmemalign}$ // CFA 6702 6757 6703 6758 // 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}§6759 void * memset( void * dest, int c, size_t size );$\indexc{memset}$ 6760 void * memcpy( void * dest, const void * src, size_t size );$\indexc{memcpy}$ 6706 6761 } 6707 6762 … … 6709 6764 6710 6765 forall( dtype T | sized(T) ) { 6711 // §\CFA§safe equivalents, i.e., implicit size specification6766 // $\CFA$ safe equivalents, i.e., implicit size specification 6712 6767 T * malloc( void ); 6713 6768 T * calloc( size_t dim ); … … 6718 6773 int posix_memalign( T ** ptr, size_t align ); 6719 6774 6720 // §\CFA§safe general allocation, fill, resize, alignment, array6721 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 specification6745 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 types6775 // $\CFA$ safe general allocation, fill, resize, alignment, array 6776 T * alloc( void );$\indexc{alloc}$ $\C[3.5in]{// variable, T size}$ 6777 T * alloc( size_t dim ); $\C{// array[dim], T size elements}$ 6778 T * alloc( T ptr[], size_t dim ); $\C{// realloc array[dim], T size elements}$ 6779 6780 T * alloc_set( char fill );$\indexc{alloc_set}$ $\C{// variable, T size, fill bytes with value}$ 6781 T * alloc_set( T fill ); $\C{// variable, T size, fill with value}$ 6782 T * alloc_set( size_t dim, char fill ); $\C{// array[dim], T size elements, fill bytes with value}$ 6783 T * alloc_set( size_t dim, T fill ); $\C{// array[dim], T size elements, fill elements with value}$ 6784 T * alloc_set( size_t dim, const T fill[] ); $\C{// array[dim], T size elements, fill elements with array}$ 6785 T * alloc_set( T ptr[], size_t dim, char fill ); $\C{// realloc array[dim], T size elements, fill bytes with value}$ 6786 6787 T * alloc_align( size_t align ); $\C{// aligned variable, T size}$ 6788 T * alloc_align( size_t align, size_t dim ); $\C{// aligned array[dim], T size elements}$ 6789 T * alloc_align( T ptr[], size_t align ); $\C{// realloc new aligned array}$ 6790 T * alloc_align( T ptr[], size_t align, size_t dim ); $\C{// realloc new aligned array[dim]}$ 6791 6792 T * alloc_align_set( size_t align, char fill ); $\C{// aligned variable, T size, fill bytes with value}$ 6793 T * alloc_align_set( size_t align, T fill ); $\C{// aligned variable, T size, fill with value}$ 6794 T * alloc_align_set( size_t align, size_t dim, char fill ); $\C{// aligned array[dim], T size elements, fill bytes with value}$ 6795 T * alloc_align_set( size_t align, size_t dim, T fill ); $\C{// aligned array[dim], T size elements, fill elements with value}$ 6796 T * alloc_align_set( size_t align, size_t dim, const T fill[] ); $\C{// aligned array[dim], T size elements, fill elements with array}$ 6797 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}$ 6798 6799 // $\CFA$ safe initialization/copy, i.e., implicit size specification 6800 T * memset( T * dest, char fill );$\indexc{memset}$ 6801 T * memcpy( T * dest, const T * src );$\indexc{memcpy}$ 6802 6803 // $\CFA$ safe initialization/copy, i.e., implicit size specification, array types 6749 6804 T * amemset( T dest[], char fill, size_t dim ); 6750 6805 T * amemcpy( T dest[], const T src[], size_t dim ); 6751 6806 } 6752 6807 6753 // §\CFA§allocation/deallocation and constructor/destructor, non-array types6754 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}§6808 // $\CFA$ allocation/deallocation and constructor/destructor, non-array types 6809 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );$\indexc{new}$ 6810 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );$\indexc{delete}$ 6756 6811 forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) 6757 6812 void delete( T * ptr, Params rest ); 6758 6813 6759 // §\CFA§allocation/deallocation and constructor/destructor, array types6760 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}§6814 // $\CFA$ allocation/deallocation and constructor/destructor, array types 6815 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );$\indexc{anew}$ 6816 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );$\indexc{adelete}$ 6762 6817 forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) 6763 6818 void adelete( size_t dim, T arr[], Params rest ); … … 6769 6824 \leavevmode 6770 6825 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 6771 int ato( const char * ptr ); §\indexc{ato}§6826 int ato( const char * ptr );$\indexc{ato}$ 6772 6827 unsigned int ato( const char * ptr ); 6773 6828 long int ato( const char * ptr ); … … 6801 6856 \leavevmode 6802 6857 \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}§6858 forall( otype T | { int ?<?( T, T ); } ) $\C{// location}$ 6859 T * bsearch( T key, const T * arr, size_t dim );$\indexc{bsearch}$ 6860 6861 forall( otype T | { int ?<?( T, T ); } ) $\C{// position}$ 6807 6862 unsigned int bsearch( T key, const T * arr, size_t dim ); 6808 6863 6809 6864 forall( otype T | { int ?<?( T, T ); } ) 6810 void qsort( const T * arr, size_t dim ); §\indexc{qsort}§6865 void qsort( const T * arr, size_t dim );$\indexc{qsort}$ 6811 6866 6812 6867 forall( 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}§6868 E * bsearch( E key, const E * vals, size_t dim );$\indexc{bsearch}$ $\C{// location}$ 6869 size_t bsearch( E key, const E * vals, size_t dim );$\C{// position}$ 6870 E * bsearchl( E key, const E * vals, size_t dim );$\indexc{bsearchl}$ 6816 6871 size_t bsearchl( E key, const E * vals, size_t dim ); 6817 E * bsearchu( E key, const E * vals, size_t dim ); §\indexc{bsearchu}§6872 E * bsearchu( E key, const E * vals, size_t dim );$\indexc{bsearchu}$ 6818 6873 size_t bsearchu( E key, const E * vals, size_t dim ); 6819 6874 } … … 6829 6884 6830 6885 forall( otype E | { int ?<?( E, E ); } ) { 6831 void qsort( E * vals, size_t dim ); §\indexc{qsort}§6886 void qsort( E * vals, size_t dim );$\indexc{qsort}$ 6832 6887 } 6833 6888 \end{cfa} … … 6838 6893 \leavevmode 6839 6894 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 6840 unsigned char abs( signed char ); §\indexc{abs}§6895 unsigned char abs( signed char );$\indexc{abs}$ 6841 6896 int abs( int ); 6842 6897 unsigned long int abs( long int ); … … 6857 6912 \leavevmode 6858 6913 \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)}§6914 void srandom( unsigned int seed );$\indexc{srandom}$ 6915 char random( void );$\indexc{random}$ 6916 char random( char u ); $\C{// [0,u)}$ 6917 char random( char l, char u ); $\C{// [l,u)}$ 6863 6918 int random( void ); 6864 int random( int u ); §\C{// [0,u)}§6865 int random( int l, int u ); §\C{// [l,u)}§6919 int random( int u ); $\C{// [0,u)}$ 6920 int random( int l, int u ); $\C{// [l,u)}$ 6866 6921 unsigned 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)}§6922 unsigned int random( unsigned int u ); $\C{// [0,u)}$ 6923 unsigned int random( unsigned int l, unsigned int u ); $\C{// [l,u)}$ 6869 6924 long 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)}§6925 long int random( long int u ); $\C{// [0,u)}$ 6926 long int random( long int l, long int u ); $\C{// [l,u)}$ 6872 6927 unsigned 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}§6928 unsigned long int random( unsigned long int u ); $\C{// [0,u)}$ 6929 unsigned long int random( unsigned long int l, unsigned long int u ); $\C{// [l,u)}$ 6930 float random( void ); $\C{// [0.0, 1.0)}$ 6931 double random( void ); $\C{// [0.0, 1.0)}$ 6932 float _Complex random( void ); $\C{// [0.0, 1.0)+[0.0, 1.0)i}$ 6933 double _Complex random( void ); $\C{// [0.0, 1.0)+[0.0, 1.0)i}$ 6934 long double _Complex random( void ); $\C{// [0.0, 1.0)+[0.0, 1.0)i}$ 6880 6935 \end{cfa} 6881 6936 … … 6885 6940 \leavevmode 6886 6941 \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}§6942 forall( otype T | { int ?<?( T, T ); } ) T min( T t1, T t2 );$\indexc{min}$ 6943 forall( otype T | { int ?>?( T, T ); } ) T max( T t1, T t2 );$\indexc{max}$ 6944 forall( otype T | { T min( T, T ); T max( T, T ); } ) T clamp( T value, T min_val, T max_val );$\indexc{clamp}$ 6945 forall( otype T ) void swap( T * t1, T * t2 );$\indexc{swap}$ 6891 6946 \end{cfa} 6892 6947 … … 6902 6957 \leavevmode 6903 6958 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 6904 float ?%?( float, float ); §\indexc{fmod}§6959 float ?%?( float, float );$\indexc{fmod}$ 6905 6960 float fmod( float, float ); 6906 6961 double ?%?( double, double ); … … 6909 6964 long double fmod( long double, long double ); 6910 6965 6911 float remainder( float, float ); §\indexc{remainder}§6966 float remainder( float, float );$\indexc{remainder}$ 6912 6967 double remainder( double, double ); 6913 6968 long double remainder( long double, long double ); 6914 6969 6915 float remquo( float, float, int * ); §\indexc{remquo}§6970 float remquo( float, float, int * );$\indexc{remquo}$ 6916 6971 double remquo( double, double, int * ); 6917 6972 long double remquo( long double, long double, int * ); … … 6920 6975 [ int, long double ] remquo( long double, long double ); 6921 6976 6922 float div( float, float, int * ); §\indexc{div}§ §\C{// alternative name for remquo}§6977 float div( float, float, int * );$\indexc{div}$ $\C{// alternative name for remquo}$ 6923 6978 double div( double, double, int * ); 6924 6979 long double div( long double, long double, int * ); … … 6927 6982 [ int, long double ] div( long double, long double ); 6928 6983 6929 float fma( float, float, float ); §\indexc{fma}§6984 float fma( float, float, float );$\indexc{fma}$ 6930 6985 double fma( double, double, double ); 6931 6986 long double fma( long double, long double, long double ); 6932 6987 6933 float fdim( float, float ); §\indexc{fdim}§6988 float fdim( float, float );$\indexc{fdim}$ 6934 6989 double fdim( double, double ); 6935 6990 long double fdim( long double, long double ); 6936 6991 6937 float nan( const char * ); §\indexc{nan}§6992 float nan( const char * );$\indexc{nan}$ 6938 6993 double nan( const char * ); 6939 6994 long double nan( const char * ); … … 6945 7000 \leavevmode 6946 7001 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 6947 float exp( float ); §\indexc{exp}§7002 float exp( float );$\indexc{exp}$ 6948 7003 double exp( double ); 6949 7004 long double exp( long double ); … … 6952 7007 long double _Complex exp( long double _Complex ); 6953 7008 6954 float exp2( float ); §\indexc{exp2}§7009 float exp2( float );$\indexc{exp2}$ 6955 7010 double exp2( double ); 6956 7011 long double exp2( long double ); … … 6959 7014 // long double _Complex exp2( long double _Complex ); 6960 7015 6961 float expm1( float ); §\indexc{expm1}§7016 float expm1( float );$\indexc{expm1}$ 6962 7017 double expm1( double ); 6963 7018 long double expm1( long double ); 6964 7019 6965 float pow( float, float ); §\indexc{pow}§7020 float pow( float, float );$\indexc{pow}$ 6966 7021 double pow( double, double ); 6967 7022 long double pow( long double, long double ); … … 6976 7031 \leavevmode 6977 7032 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 6978 float log( float ); §\indexc{log}§7033 float log( float );$\indexc{log}$ 6979 7034 double log( double ); 6980 7035 long double log( long double ); … … 6983 7038 long double _Complex log( long double _Complex ); 6984 7039 6985 float log2( float ); §\indexc{log2}§7040 float log2( float );$\indexc{log2}$ 6986 7041 double log2( double ); 6987 7042 long double log2( long double ); … … 6990 7045 // long double _Complex log2( long double _Complex ); 6991 7046 6992 float log10( float ); §\indexc{log10}§7047 float log10( float );$\indexc{log10}$ 6993 7048 double log10( double ); 6994 7049 long double log10( long double ); … … 6997 7052 // long double _Complex log10( long double _Complex ); 6998 7053 6999 float log1p( float ); §\indexc{log1p}§7054 float log1p( float );$\indexc{log1p}$ 7000 7055 double log1p( double ); 7001 7056 long double log1p( long double ); 7002 7057 7003 int ilogb( float ); §\indexc{ilogb}§7058 int ilogb( float );$\indexc{ilogb}$ 7004 7059 int ilogb( double ); 7005 7060 int ilogb( long double ); 7006 7061 7007 float logb( float ); §\indexc{logb}§7062 float logb( float );$\indexc{logb}$ 7008 7063 double logb( double ); 7009 7064 long double logb( long double ); 7010 7065 7011 float sqrt( float ); §\indexc{sqrt}§7066 float sqrt( float );$\indexc{sqrt}$ 7012 7067 double sqrt( double ); 7013 7068 long double sqrt( long double ); … … 7016 7071 long double _Complex sqrt( long double _Complex ); 7017 7072 7018 float cbrt( float ); §\indexc{cbrt}§7073 float cbrt( float );$\indexc{cbrt}$ 7019 7074 double cbrt( double ); 7020 7075 long double cbrt( long double ); 7021 7076 7022 float hypot( float, float ); §\indexc{hypot}§7077 float hypot( float, float );$\indexc{hypot}$ 7023 7078 double hypot( double, double ); 7024 7079 long double hypot( long double, long double ); … … 7030 7085 \leavevmode 7031 7086 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7032 float sin( float ); §\indexc{sin}§7087 float sin( float );$\indexc{sin}$ 7033 7088 double sin( double ); 7034 7089 long double sin( long double ); … … 7037 7092 long double _Complex sin( long double _Complex ); 7038 7093 7039 float cos( float ); §\indexc{cos}§7094 float cos( float );$\indexc{cos}$ 7040 7095 double cos( double ); 7041 7096 long double cos( long double ); … … 7044 7099 long double _Complex cos( long double _Complex ); 7045 7100 7046 float tan( float ); §\indexc{tan}§7101 float tan( float );$\indexc{tan}$ 7047 7102 double tan( double ); 7048 7103 long double tan( long double ); … … 7051 7106 long double _Complex tan( long double _Complex ); 7052 7107 7053 float asin( float ); §\indexc{asin}§7108 float asin( float );$\indexc{asin}$ 7054 7109 double asin( double ); 7055 7110 long double asin( long double ); … … 7058 7113 long double _Complex asin( long double _Complex ); 7059 7114 7060 float acos( float ); §\indexc{acos}§7115 float acos( float );$\indexc{acos}$ 7061 7116 double acos( double ); 7062 7117 long double acos( long double ); … … 7065 7120 long double _Complex acos( long double _Complex ); 7066 7121 7067 float atan( float ); §\indexc{atan}§7122 float atan( float );$\indexc{atan}$ 7068 7123 double atan( double ); 7069 7124 long double atan( long double ); … … 7072 7127 long double _Complex atan( long double _Complex ); 7073 7128 7074 float atan2( float, float ); §\indexc{atan2}§7129 float atan2( float, float );$\indexc{atan2}$ 7075 7130 double atan2( double, double ); 7076 7131 long double atan2( long double, long double ); 7077 7132 7078 float atan( float, float ); §\C{// alternative name for atan2}§7079 double atan( double, double ); §\indexc{atan}§7133 float atan( float, float ); $\C{// alternative name for atan2}$ 7134 double atan( double, double );$\indexc{atan}$ 7080 7135 long double atan( long double, long double ); 7081 7136 \end{cfa} … … 7086 7141 \leavevmode 7087 7142 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7088 float sinh( float ); §\indexc{sinh}§7143 float sinh( float );$\indexc{sinh}$ 7089 7144 double sinh( double ); 7090 7145 long double sinh( long double ); … … 7093 7148 long double _Complex sinh( long double _Complex ); 7094 7149 7095 float cosh( float ); §\indexc{cosh}§7150 float cosh( float );$\indexc{cosh}$ 7096 7151 double cosh( double ); 7097 7152 long double cosh( long double ); … … 7100 7155 long double _Complex cosh( long double _Complex ); 7101 7156 7102 float tanh( float ); §\indexc{tanh}§7157 float tanh( float );$\indexc{tanh}$ 7103 7158 double tanh( double ); 7104 7159 long double tanh( long double ); … … 7107 7162 long double _Complex tanh( long double _Complex ); 7108 7163 7109 float asinh( float ); §\indexc{asinh}§7164 float asinh( float );$\indexc{asinh}$ 7110 7165 double asinh( double ); 7111 7166 long double asinh( long double ); … … 7114 7169 long double _Complex asinh( long double _Complex ); 7115 7170 7116 float acosh( float ); §\indexc{acosh}§7171 float acosh( float );$\indexc{acosh}$ 7117 7172 double acosh( double ); 7118 7173 long double acosh( long double ); … … 7121 7176 long double _Complex acosh( long double _Complex ); 7122 7177 7123 float atanh( float ); §\indexc{atanh}§7178 float atanh( float );$\indexc{atanh}$ 7124 7179 double atanh( double ); 7125 7180 long double atanh( long double ); … … 7134 7189 \leavevmode 7135 7190 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7136 float erf( float ); §\indexc{erf}§7191 float erf( float );$\indexc{erf}$ 7137 7192 double erf( double ); 7138 7193 long double erf( long double ); … … 7141 7196 long double _Complex erf( long double _Complex ); 7142 7197 7143 float erfc( float ); §\indexc{erfc}§7198 float erfc( float );$\indexc{erfc}$ 7144 7199 double erfc( double ); 7145 7200 long double erfc( long double ); … … 7148 7203 long double _Complex erfc( long double _Complex ); 7149 7204 7150 float lgamma( float ); §\indexc{lgamma}§7205 float lgamma( float );$\indexc{lgamma}$ 7151 7206 double lgamma( double ); 7152 7207 long double lgamma( long double ); … … 7155 7210 long double lgamma( long double, int * ); 7156 7211 7157 float tgamma( float ); §\indexc{tgamma}§7212 float tgamma( float );$\indexc{tgamma}$ 7158 7213 double tgamma( double ); 7159 7214 long double tgamma( long double ); … … 7165 7220 \leavevmode 7166 7221 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7167 float floor( float ); §\indexc{floor}§7222 float floor( float );$\indexc{floor}$ 7168 7223 double floor( double ); 7169 7224 long double floor( long double ); 7170 7225 7171 float ceil( float ); §\indexc{ceil}§7226 float ceil( float );$\indexc{ceil}$ 7172 7227 double ceil( double ); 7173 7228 long double ceil( long double ); 7174 7229 7175 float trunc( float ); §\indexc{trunc}§7230 float trunc( float );$\indexc{trunc}$ 7176 7231 double trunc( double ); 7177 7232 long double trunc( long double ); 7178 7233 7179 float rint( float ); §\indexc{rint}§7234 float rint( float );$\indexc{rint}$ 7180 7235 long double rint( long double ); 7181 7236 long int rint( float ); … … 7186 7241 long long int rint( long double ); 7187 7242 7188 long int lrint( float ); §\indexc{lrint}§7243 long int lrint( float );$\indexc{lrint}$ 7189 7244 long int lrint( double ); 7190 7245 long int lrint( long double ); … … 7193 7248 long long int llrint( long double ); 7194 7249 7195 float nearbyint( float ); §\indexc{nearbyint}§7250 float nearbyint( float );$\indexc{nearbyint}$ 7196 7251 double nearbyint( double ); 7197 7252 long double nearbyint( long double ); 7198 7253 7199 float round( float ); §\indexc{round}§7254 float round( float );$\indexc{round}$ 7200 7255 long double round( long double ); 7201 7256 long int round( float ); … … 7206 7261 long long int round( long double ); 7207 7262 7208 long int lround( float ); §\indexc{lround}§7263 long int lround( float );$\indexc{lround}$ 7209 7264 long int lround( double ); 7210 7265 long int lround( long double ); … … 7219 7274 \leavevmode 7220 7275 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7221 float copysign( float, float ); §\indexc{copysign}§7276 float copysign( float, float );$\indexc{copysign}$ 7222 7277 double copysign( double, double ); 7223 7278 long double copysign( long double, long double ); 7224 7279 7225 float frexp( float, int * ); §\indexc{frexp}§7280 float frexp( float, int * );$\indexc{frexp}$ 7226 7281 double frexp( double, int * ); 7227 7282 long double frexp( long double, int * ); 7228 7283 7229 float ldexp( float, int ); §\indexc{ldexp}§7284 float ldexp( float, int );$\indexc{ldexp}$ 7230 7285 double ldexp( double, int ); 7231 7286 long double ldexp( long double, int ); 7232 7287 7233 [ float, float ] modf( float ); §\indexc{modf}§7288 [ float, float ] modf( float );$\indexc{modf}$ 7234 7289 float modf( float, float * ); 7235 7290 [ double, double ] modf( double ); … … 7238 7293 long double modf( long double, long double * ); 7239 7294 7240 float nextafter( float, float ); §\indexc{nextafter}§7295 float nextafter( float, float );$\indexc{nextafter}$ 7241 7296 double nextafter( double, double ); 7242 7297 long double nextafter( long double, long double ); 7243 7298 7244 float nexttoward( float, long double ); §\indexc{nexttoward}§7299 float nexttoward( float, long double );$\indexc{nexttoward}$ 7245 7300 double nexttoward( double, long double ); 7246 7301 long double nexttoward( long double, long double ); 7247 7302 7248 float scalbn( float, int ); §\indexc{scalbn}§7303 float scalbn( float, int );$\indexc{scalbn}$ 7249 7304 double scalbn( double, int ); 7250 7305 long double scalbn( long double, int ); 7251 7306 7252 float scalbln( float, long int ); §\indexc{scalbln}§7307 float scalbln( float, long int );$\indexc{scalbln}$ 7253 7308 double scalbln( double, long int ); 7254 7309 long double scalbln( long double, long int ); … … 7267 7322 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7268 7323 struct Duration { 7269 int64_t tv; §\C{// nanoseconds}§7324 int64_t tv; $\C{// nanoseconds}$ 7270 7325 }; 7271 7326 … … 7397 7452 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7398 7453 struct Time { 7399 uint64_t tv; §\C{// nanoseconds since UNIX epoch}§7454 uint64_t tv; $\C{// nanoseconds since UNIX epoch}$ 7400 7455 }; 7401 7456 … … 7468 7523 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7469 7524 struct Clock { 7470 Duration offset; §\C{// for virtual clock: contains offset from real-time}§7471 int clocktype; §\C{// implementation only -1 (virtual), CLOCK\_REALTIME}§7525 Duration offset; $\C{// for virtual clock: contains offset from real-time}$ 7526 int clocktype; $\C{// implementation only -1 (virtual), CLOCK\_REALTIME}$ 7472 7527 }; 7473 7528 … … 7477 7532 void ?{}( Clock & clk, Duration adj ); 7478 7533 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}§7534 Duration getResNsec(); $\C{// with nanoseconds}$ 7535 Duration getRes(); $\C{// without nanoseconds}$ 7536 7537 Time getTimeNsec(); $\C{// with nanoseconds}$ 7538 Time getTime(); $\C{// without nanoseconds}$ 7484 7539 Time getTime( Clock & clk ); 7485 7540 Time ?()( Clock & clk ); … … 7497 7552 7498 7553 \begin{cfa} 7499 void ?{}( Int * this ); §\C{// constructor/destructor}§7554 void ?{}( Int * this ); $\C{// constructor/destructor}$ 7500 7555 void ?{}( Int * this, Int init ); 7501 7556 void ?{}( Int * this, zero_t ); … … 7506 7561 void ^?{}( Int * this ); 7507 7562 7508 Int ?=?( Int * lhs, Int rhs ); §\C{// assignment}§7563 Int ?=?( Int * lhs, Int rhs ); $\C{// assignment}$ 7509 7564 Int ?=?( Int * lhs, long int rhs ); 7510 7565 Int ?=?( Int * lhs, unsigned long int rhs ); … … 7523 7578 unsigned long int narrow( Int val ); 7524 7579 7525 int ?==?( Int oper1, Int oper2 ); §\C{// comparison}§7580 int ?==?( Int oper1, Int oper2 ); $\C{// comparison}$ 7526 7581 int ?==?( Int oper1, long int oper2 ); 7527 7582 int ?==?( long int oper2, Int oper1 ); … … 7559 7614 int ?>=?( unsigned long int oper1, Int oper2 ); 7560 7615 7561 Int +?( Int oper ); §\C{// arithmetic}§7616 Int +?( Int oper ); $\C{// arithmetic}$ 7562 7617 Int -?( Int oper ); 7563 7618 Int ~?( Int oper ); … … 7641 7696 Int ?>>=?( Int * lhs, mp_bitcnt_t shift ); 7642 7697 7643 Int abs( Int oper ); §\C{// number functions}§7698 Int abs( Int oper ); $\C{// number functions}$ 7644 7699 Int fact( unsigned long int N ); 7645 7700 Int gcd( Int oper1, Int oper2 ); … … 7653 7708 Int sqrt( Int oper ); 7654 7709 7655 forall( dtype istype | istream( istype ) ) istype * ?|?( istype * is, Int * mp ); §\C{// I/O}§7710 forall( dtype istype | istream( istype ) ) istype * ?|?( istype * is, Int * mp ); $\C{// I/O}$ 7656 7711 forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype * os, Int mp ); 7657 7712 \end{cfa} … … 7664 7719 \hline 7665 7720 \begin{cfa} 7666 #include <gmp> §\indexc{gmp}§7721 #include <gmp>$\indexc{gmp}$ 7667 7722 int main( void ) { 7668 7723 sout | "Factorial Numbers"; … … 7678 7733 & 7679 7734 \begin{cfa} 7680 #include <gmp.h> §\indexc{gmp.h}§7735 #include <gmp.h>$\indexc{gmp.h}$ 7681 7736 int 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 );7737 @gmp_printf@( "Factorial Numbers\n" ); 7738 @mpz_t@ fact; 7739 @mpz_init_set_ui@( fact, 1 ); 7740 @gmp_printf@( "%d %Zd\n", 0, fact ); 7686 7741 for ( unsigned int i = 1; i <= 40; i += 1 ) { 7687 ®mpz_mul_ui®( fact, fact, i );7688 ®gmp_printf®( "%d %Zd\n", i, fact );7742 @mpz_mul_ui@( fact, fact, i ); 7743 @gmp_printf@( "%d %Zd\n", i, fact ); 7689 7744 } 7690 7745 } … … 7751 7806 \begin{cfa}[belowskip=0pt] 7752 7807 // implementation 7753 struct Rational { §\indexc{Rational}§7754 long int numerator, denominator; §\C{// invariant: denominator > 0}§7808 struct Rational {$\indexc{Rational}$ 7809 long int numerator, denominator; $\C{// invariant: denominator > 0}$ 7755 7810 }; // Rational 7756 7811 7757 Rational rational(); §\C{// constructors}§7812 Rational rational(); $\C{// constructors}$ 7758 7813 Rational rational( long int n ); 7759 7814 Rational rational( long int n, long int d ); … … 7761 7816 void ?{}( Rational * r, one_t ); 7762 7817 7763 long int numerator( Rational r ); §\C{// numerator/denominator getter/setter}§7818 long int numerator( Rational r ); $\C{// numerator/denominator getter/setter}$ 7764 7819 long int numerator( Rational r, long int n ); 7765 7820 long int denominator( Rational r ); 7766 7821 long int denominator( Rational r, long int d ); 7767 7822 7768 int ?==?( Rational l, Rational r ); §\C{// comparison}§7823 int ?==?( Rational l, Rational r ); $\C{// comparison}$ 7769 7824 int ?!=?( Rational l, Rational r ); 7770 7825 int ?<?( Rational l, Rational r ); … … 7773 7828 int ?>=?( Rational l, Rational r ); 7774 7829 7775 Rational -?( Rational r ); §\C{// arithmetic}§7830 Rational -?( Rational r ); $\C{// arithmetic}$ 7776 7831 Rational ?+?( Rational l, Rational r ); 7777 7832 Rational ?-?( Rational l, Rational r ); … … 7779 7834 Rational ?/?( Rational l, Rational r ); 7780 7835 7781 double widen( Rational r ); §\C{// conversion}§7836 double widen( Rational r ); $\C{// conversion}$ 7782 7837 Rational narrow( double f, long int md ); 7783 7838 -
libcfa/src/memory.cfa
rda3963a r565acf59 10 10 // Created On : Tue Jun 2 16:48:00 2020 11 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Tue Jun 3 12:30:00 202013 // Update Count : 012 // Last Modified On : Mon Feb 1 16:10:00 2021 13 // Update Count : 1 14 14 // 15 15 … … 56 56 } 57 57 58 forall(T & | sized(T) | { void ^?{}(T &); })58 forall(T & | sized(T)) 59 59 void ?{}(counter_ptr(T) & this, counter_ptr(T) that) { 60 60 // `that` is a copy but it should have neither a constructor 61 61 // nor destructor run on it so it shouldn't need adjustment. 62 internal_decrement(this);63 62 internal_copy(this, that); 64 63 } … … 66 65 forall(T & | sized(T), Args... | { void ?{}(T&, Args); }) 67 66 void ?{}(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}; 69 70 } 70 71 … … 126 127 forall(T & | sized(T), Args... | { void ?{}(T &, Args); }) 127 128 void ?{}(unique_ptr(T) & this, Args args) { 128 this.data = (T *)new(args); 129 this.data = malloc(); 130 (*this.data){args}; 129 131 } 130 132 -
libcfa/src/memory.hfa
rda3963a r565acf59 10 10 // Created On : Tue Jun 2 16:48:00 2020 11 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Tue Jun 3 12:29:00 202013 // Update Count : 012 // Last Modified On : Fri Jan 29 15:52:00 2021 13 // Update Count : 1 14 14 // 15 15 … … 17 17 18 18 // Internal data object. 19 forall(T & | sized(T)) {20 21 22 23 19 forall(T & | sized(T)) 20 struct counter_data { 21 unsigned int counter; 22 T object; 23 }; 24 24 25 forall(Args... | { void ?{}(T &, Args); })26 25 forall(T & | sized(T), Args... | { void ?{}(T &, Args); }) 26 void ?{}(counter_data(T) & this, Args args); 27 27 28 forall( | { void ^?{}(T &); }) 29 void ^?{}(counter_data(T) & this); 30 } 28 forall(T & | sized(T) | { void ^?{}(T &); }) 29 void ^?{}(counter_data(T) & this); 31 30 32 31 // This is one of many pointers keeping this alive. 33 forall(T & | sized(T)) {34 35 36 32 forall(T & | sized(T)) 33 struct counter_ptr { 34 counter_data(T) * data; 35 }; 37 36 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(Args... | { void ?{}(T&, Args); }) 43 void ?{}(counter_ptr(T) & this, Args args); 37 forall(T & | sized(T)) 38 void ?{}(counter_ptr(T) & this); 39 forall(T & | sized(T)) 40 void ?{}(counter_ptr(T) & this, zero_t); 41 forall(T & | sized(T)) 42 void ?{}(counter_ptr(T) & this, counter_ptr(T) that); 43 forall(T & | sized(T), Args... | { void ?{}(T&, Args); }) 44 void ?{}(counter_ptr(T) & this, Args args); 44 45 45 forall(| { void ^?{}(T &); })46 46 forall(T & | sized(T) | { void ^?{}(T &); }) 47 void ^?{}(counter_ptr(T) & this); 47 48 48 T & *?(counter_ptr(T) & this); 49 forall(T & | sized(T)) 50 T & *?(counter_ptr(T) & this); 49 51 50 forall(| { void ^?{}(T &); })51 52 forall(| { void ^?{}(T &); })53 52 forall(T & | sized(T) | { void ^?{}(T &); }) 53 void ?=?(counter_ptr(T) & this, counter_ptr(T) that); 54 forall(T & | sized(T) | { void ^?{}(T &); }) 55 void ?=?(counter_ptr(T) & this, zero_t); 54 56 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 } 57 forall(T & | sized(T)) 58 int ?==?(counter_ptr(T) const & this, counter_ptr(T) const & that); 59 forall(T & | sized(T)) 60 int ?!=?(counter_ptr(T) const & this, counter_ptr(T) const & that); 61 forall(T & | sized(T)) 62 int ?==?(counter_ptr(T) const & this, zero_t); 63 forall(T & | sized(T)) 64 int ?!=?(counter_ptr(T) const & this, zero_t); 60 65 61 66 // This is the only pointer that keeps this alive. 62 forall(T &) {63 64 65 67 forall(T &) 68 struct unique_ptr { 69 T * data; 70 }; 66 71 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), Args... | { void ?{}(T &, Args); }) 71 void ?{}(unique_ptr(T) & this, Args args); 72 forall(T &) 73 void ?{}(unique_ptr(T) & this); 74 forall(T &) 75 void ?{}(unique_ptr(T) & this, zero_t); 76 forall(T &) 77 void ?{}(unique_ptr(T) & this, unique_ptr(T) that) = void; 78 forall(T & | sized(T), Args... | { void ?{}(T &, Args); }) 79 void ?{}(unique_ptr(T) & this, Args args); 72 80 73 forall(| { void ^?{}(T &); })74 81 forall(T & | { void ^?{}(T &); }) 82 void ^?{}(unique_ptr(T) & this); 75 83 76 T & *?(unique_ptr(T) & this); 84 forall(T & ) 85 T & *?(unique_ptr(T) & this); 77 86 78 void ?=?(unique_ptr(T) & this, unique_ptr(T) that) = void; 79 forall( | { void ^?{}(T &); }) 80 void ?=?(unique_ptr(T) & this, zero_t); 87 forall(T &) 88 void ?=?(unique_ptr(T) & this, unique_ptr(T) that) = void; 89 forall(T & | { void ^?{}(T &); }) 90 void ?=?(unique_ptr(T) & this, zero_t); 81 91 82 forall(| { void ^?{}(T &); })83 92 forall(T & | { void ^?{}(T &); }) 93 void move(unique_ptr(T) & this, unique_ptr(T) & that); 84 94 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 } 95 forall(T &) 96 int ?==?(unique_ptr(T) const & this, unique_ptr(T) const & that); 97 forall(T &) 98 int ?!=?(unique_ptr(T) const & this, unique_ptr(T) const & that); 99 forall(T &) 100 int ?==?(unique_ptr(T) const & this, zero_t); 101 forall(T &) 102 int ?!=?(unique_ptr(T) const & this, zero_t); -
src/Parser/parser.yy
rda3963a r565acf59 10 10 // Created On : Sat Sep 1 20:22:55 2001 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue Jan 26 11:18:19202113 // Update Count : 4 67412 // Last Modified On : Wed Feb 3 18:30:12 2021 13 // Update Count : 4700 14 14 // 15 15 … … 41 41 42 42 %{ 43 #define YYDEBUG_LEXER_TEXT (yylval)// lexer loads this up each time43 #define YYDEBUG_LEXER_TEXT( yylval ) // lexer loads this up each time 44 44 #define YYDEBUG 1 // get the pretty debugging code to compile 45 45 #define YYERROR_VERBOSE // more information in syntax errors … … 187 187 ConstantExpr * constant = dynamic_cast<ConstantExpr *>(type->expr.get()); 188 188 if ( constant && (constant->get_constant()->get_value() == "0" || constant->get_constant()->get_value() == "1") ) { 189 189 type = new ExpressionNode( new CastExpr( maybeMoveBuild<Expression>(type), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ) ); 190 190 } // if 191 191 return new ForCtrl( … … 618 618 postfix_expression: 619 619 primary_expression 620 | postfix_expression '[' assignment_expression ',' comma_expression ']' 621 // { $$ = new ExpressionNode( build_binary_val( OperKinds::Index, $1, new ExpressionNode( build_binary_val( OperKinds::Index, $3, $5 ) ) ) ); } 622 { SemanticError( yylloc, "New array subscript is currently unimplemented." ); $$ = nullptr; } 620 623 | postfix_expression '[' assignment_expression ']' 621 624 // CFA, comma_expression disallowed in this context because it results in a common user error: subscripting a … … 1247 1250 { $$ = new StatementNode( build_computedgoto( $3 ) ); } 1248 1251 // A semantic check is required to ensure fallthru appears only in the body of a choose statement. 1249 1252 | fall_through_name ';' // CFA 1250 1253 { $$ = new StatementNode( build_branch( BranchStmt::FallThrough ) ); } 1251 1254 | fall_through_name identifier_or_type_name ';' // CFA 1252 1255 { $$ = new StatementNode( build_branch( $2, BranchStmt::FallThrough ) ); } 1253 1256 | fall_through_name DEFAULT ';' // CFA … … 1739 1742 1740 1743 enum_specifier_nobody: // type specifier - {...} 1741 // Preclude SUE declarations in restricted scopes: 1742 // 1743 // int f( struct S { int i; } s1, Struct S s2 ) { struct S s3; ... } 1744 // 1745 // because it is impossible to call f due to name equivalence. 1744 // Preclude SUE declarations in restricted scopes (see type_specifier_nobody) 1746 1745 basic_type_specifier 1747 1746 | sue_type_specifier_nobody … … 2461 2460 { 2462 2461 typedefTable.addToScope( *$2, TYPEDEFname, "9" ); 2463 if ( $1 == TypeDecl::Otype ) { SemanticError( yylloc, "otype keyword is deprecated " ); }2464 if ( $1 == TypeDecl::Dtype ) { SemanticError( yylloc, "dtype keyword is deprecated " ); }2465 if ( $1 == TypeDecl::Ttype ) { SemanticError( yylloc, "ttype keyword is deprecated " ); }2462 if ( $1 == TypeDecl::Otype ) { SemanticError( yylloc, "otype keyword is deprecated, use T " ); } 2463 if ( $1 == TypeDecl::Dtype ) { SemanticError( yylloc, "dtype keyword is deprecated, use T &" ); } 2464 if ( $1 == TypeDecl::Ttype ) { SemanticError( yylloc, "ttype keyword is deprecated, use T ..." ); } 2466 2465 } 2467 2466 type_initializer_opt assertion_list_opt … … 3191 3190 | '[' ']' multi_array_dimension 3192 3191 { $$ = DeclarationNode::newArray( 0, 0, false )->addArray( $3 ); } 3192 | '[' push assignment_expression pop ',' comma_expression ']' 3193 { $$ = DeclarationNode::newArray( $3, 0, false )->addArray( DeclarationNode::newArray( $6, 0, false ) ); } 3194 // { SemanticError( yylloc, "New array dimension is currently unimplemented." ); $$ = nullptr; } 3193 3195 | multi_array_dimension 3194 3196 ; -
src/main.cc
rda3963a r565acf59 9 9 // Author : Peter Buhr and Rob Schluntz 10 10 // Created On : Fri May 15 23:12:02 2015 11 // Last Modified By : Andrew Beach12 // Last Modified On : Mon Dec 7 15:29:00 202013 // Update Count : 6 3911 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Feb 8 21:10:16 2021 13 // Update Count : 642 14 14 // 15 15 … … 492 492 493 493 static const char * description[] = { 494 "diagnostic color: never, always, or auto.",// -c494 "diagnostic color: never, always, auto", // -c 495 495 "wait for gdb to attach", // -g 496 "print help message",// -h496 "print translator help message", // -h 497 497 "generate libcfa.c", // -l 498 498 "generate line marks", // -L … … 500 500 "do not generate line marks", // -N 501 501 "do not read prelude", // -n 502 " generate prototypes for prelude functions",// -p502 "do not generate prelude prototypes => prelude not printed", // -p 503 503 "only print deterministic output", // -d 504 504 "Use the old-ast", // -O … … 506 506 "print", // -P 507 507 "<directory> prelude directory for debug/nodebug", // no flag 508 "<option-list> enable profiling information: \n counters,heap,time,all,none", // -S508 "<option-list> enable profiling information: counters, heap, time, all, none", // -S 509 509 "building cfa standard lib", // -t 510 510 "", // -w -
tests/smart-pointers.cfa
rda3963a r565acf59 2 2 3 3 #include <memory.hfa> 4 #include < stdlib.hfa>4 #include <assert.h> 5 5 6 6 void counter_test(void) { … … 53 53 } 54 54 55 void 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 55 67 int main(int argc, char * argv[]) { 56 68 counter_test(); 57 69 unique_test(); 58 70 pointer_equality(); 71 72 printf("done\n"); 59 73 } -
tools/prettyprinter/Makefile.am
rda3963a r565acf59 11 11 ## Created On : Wed Jun 28 12:07:10 2017 12 12 ## Last Modified By : Peter A. Buhr 13 ## Last Modified On : Mon Apr 16 09:43:23 201814 ## Update Count : 2 013 ## Last Modified On : Thu Jan 28 08:48:22 2021 14 ## Update Count : 23 15 15 ############################################################################### 16 16 … … 20 20 BUILT_SOURCES = parser.hh 21 21 22 AM_YFLAGS = -d -t -v 22 AM_YFLAGS = -d -t -v -Wno-yacc 23 23 24 24 SRC = lex.ll \ … … 34 34 pretty_CXXFLAGS = -Wno-deprecated -Wall -DYY_NO_INPUT -O2 -g -std=c++14 35 35 36 M AINTAINERCLEANFILES = parser.output36 MOSTLYCLEANFILES = parser.output -
tools/prettyprinter/ParserTypes.h
rda3963a r565acf59 13 13 // Created On : Sun Dec 16 15:00:49 2001 14 14 // Last Modified By : Peter A. Buhr 15 // Last Modified On : Sat Jul 22 10:13:09 201716 // Update Count : 17 515 // Last Modified On : Tue Jan 26 23:05:34 2021 16 // Update Count : 176 17 17 // 18 18 19 19 #pragma once 20 20 21 int yylex();21 extern "C" int yylex(); 22 22 23 23 #include <string> -
tools/prettyprinter/parser.yy
rda3963a r565acf59 10 10 // Created On : Sat Dec 15 13:44:21 2001 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun Apr 15 21:40:30 201813 // Update Count : 105 212 // Last Modified On : Tue Jan 26 22:50:03 2021 13 // Update Count : 1053 14 14 // 15 15 … … 17 17 #define YYDEBUG_LEXER_TEXT( yylval ) // lexer loads this up each time 18 18 #define YYDEBUG 1 // get the pretty debugging code to compile 19 #define YYERROR_VERBOSE // more information in syntax errors 19 20 20 21 #include <iostream>
Note:
See TracChangeset
for help on using the changeset viewer.