Changeset fe97a7d8 for doc/user/user.tex
- Timestamp:
- Jul 22, 2017, 11:05:35 AM (7 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 637ff81, 72457b6
- Parents:
- 6b0b624
- git-author:
- Peter A. Buhr <pabuhr@…> (07/22/17 10:59:19)
- git-committer:
- Peter A. Buhr <pabuhr@…> (07/22/17 11:05:35)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/user/user.tex
r6b0b624 rfe97a7d8 11 11 %% Created On : Wed Apr 6 14:53:29 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : Mon Jul 17 13:06:40201714 %% Update Count : 28 2513 %% Last Modified On : Sat Jul 22 11:01:19 2017 14 %% Update Count : 2878 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 251 251 The first \CFA implementation of these extensions was by Esteves~\cite{Esteves04}. 252 252 253 The signature feature of \CFA is \ Index{overload}able\Index{parametric-polymorphic} functions~\cite{forceone:impl,Cormack90,Duggan96} with functions generalized using a ©forall© clause (giving the language its name):253 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): 254 254 \begin{lstlisting} 255 255 ®forall( otype T )® T identity( T val ) { return val; } … … 257 257 \end{lstlisting} 258 258 % extending the C type system with parametric polymorphism and overloading, as opposed to the \Index*[C++]{\CC{}} approach of object-oriented extensions. 259 \CFA{}\hspace{1pt}'s polymorphism was originally formalized by Ditchfi led~\cite{Ditchfield92}, and first implemented by Bilson~\cite{Bilson03}.259 \CFA{}\hspace{1pt}'s polymorphism was originally formalized by Ditchfield~\cite{Ditchfield92}, and first implemented by Bilson~\cite{Bilson03}. 260 260 However, at that time, there was little interesting in extending C, so work did not continue. 261 261 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. … … 270 270 Language developers often state that adequate \Index{library support} takes more work than designing and implementing the language itself. 271 271 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. 272 Hence, \CFA begins by leveraging the large repository of C libraries at little cost.272 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. 273 273 274 274 \begin{comment} … … 318 318 \begin{cfa} 319 319 char abs( char ); 320 ®extern "C" {® 321 int abs( int ); §\C{// use default C routine for int}§ 322 ®}® // extern "C" 320 ®extern "C" {® int abs( int ); ®}® §\C{// use default C routine for int}§ 323 321 long int abs( long int ); 324 322 long long int abs( long long int ); … … 335 333 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. 336 334 There is no way around this problem, other than C's approach of creating unique names for each pairing of operation and type. 335 337 336 This example strongly illustrates a core idea in \CFA: \emph{the \Index{power of a name}}. 338 337 The name ``©abs©'' evokes the notion of absolute value, and many mathematical types provide the notion of absolute value. … … 343 342 \section[Compiling a CFA Program]{Compiling a \CFA Program} 344 343 345 The command ©cfa© is used to compile \CFA program(s), and is based on the GNU\Indexc{gcc} command, \eg:344 The command ©cfa© is used to compile a \CFA program and is based on the \Index{GNU} \Indexc{gcc} command, \eg: 346 345 \begin{cfa} 347 346 cfa§\indexc{cfa}\index{compilation!cfa@©cfa©}§ [ gcc-options ] C/§\CFA{}§-files [ assembler/loader-files ] … … 361 360 \Indexc{-CFA}\index{compilation option!-CFA@©-CFA©} 362 361 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. 363 The generated code start ed with the standard \CFA prelude.362 The generated code starts with the standard \CFA \Index{prelude}. 364 363 365 364 \item … … 373 372 \Indexc{-nodebug}\index{compilation option!-nodebug@©-nodebug©} 374 373 The program is linked with the non-debugging version of the runtime system, so the execution of the program is faster. 375 \Emph{However, no runtime checks or ©assert©s are performed so errors usually result in abnormal program termination.}374 \Emph{However, no runtime checks or ©assert©s are performed so errors usually result in abnormal program behaviour or termination.} 376 375 377 376 \item … … 393 392 \textbf{This option is the default.} 394 393 394 \begin{comment} 395 395 \item 396 396 \Indexc{-no-include-stdhdr}\index{compilation option!-no-include-stdhdr@©-no-include-stdhdr©} 397 397 Do not supply ©extern "C"© wrappers for \Celeven standard include files (see~\VRef{s:StandardHeaders}). 398 398 \textbf{This option is \emph{not} the default.} 399 \end{comment} 399 400 \end{description} 400 401 … … 417 418 \item 418 419 \Indexc{__CFA__}\index{preprocessor variables!__CFA__@©__CFA__©}, 419 \Indexc{__CFORALL__}\index{preprocessor variables!__CFORALL__@©__CFORALL__©} and420 \Indexc{__CFORALL__}\index{preprocessor variables!__CFORALL__@©__CFORALL__©}, and 420 421 \Indexc{__cforall}\index{preprocessor variables!__cforall@©__cforall©} 421 422 are always available during preprocessing and have no value. … … 470 471 \label{s:BackquoteIdentifiers} 471 472 472 \CFA introduces innew keywords (see \VRef{s:CFAKeywords}) that can clash with existing C variable-names in legacy code.473 \CFA introduces several new keywords (see \VRef{s:CFAKeywords}) that can clash with existing C variable-names in legacy code. 473 474 Keyword clashes are accommodated by syntactic transformations using the \CFA backquote escape-mechanism: 474 475 \begin{cfa} … … 476 477 double ®`®forall®`® = 3.5; 477 478 \end{cfa} 479 478 480 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. 479 \VRef[Figure]{f:HeaderFileInterposition} shows how clashes in C header files (see~\VRef{s:StandardHeaders}) can be handled using preprocessor \newterm{interposition}: ©#include_next© and ©-I filename©: 481 \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©. 482 Several common C header-files with keyword clashes are fixed in the standard \CFA header-library, so there is a seamless programming-experience. 480 483 481 484 \begin{figure} 482 485 \begin{cfa} 483 // include file uses the CFA keyword " otype".484 #if ! defined( otype) §\C{// nesting ?}§485 #define otype ®`®otype®`®§\C{// make keyword an identifier}§486 // include file uses the CFA keyword "with". 487 #if ! defined( with ) §\C{// nesting ?}§ 488 #define with ®`®with®`® §\C{// make keyword an identifier}§ 486 489 #define __CFA_BFD_H__ 487 490 #endif 488 491 489 ®#include_next <bfd .h>§\C{// must have internal check for multiple expansion}§492 ®#include_next <bfdlink.h> §\C{// must have internal check for multiple expansion}§ 490 493 ® 491 #if defined( otype) && defined( __CFA_BFD_H__ ) §\C{// reset only if set}§492 #undef otype494 #if defined( with ) && defined( __CFA_BFD_H__ ) §\C{// reset only if set}§ 495 #undef with 493 496 #undef __CFA_BFD_H__ 494 497 #endif … … 499 502 500 503 501 \section{Exponentiation Operator}502 503 C, \CC, and Java (and many other programming languages) have no exponentiation operator\index{exponentiation!operator}\index{operator!exponentiation}, \ie $x^y$, and instead use a routine, like \Indexc{pow}, to perform the exponentiation operation.504 \CFA extends the basic operators with the exponentiation operator ©?\?©\index{?\\?@\lstinline$?\?$} and ©?\=?©\index{?\\=?@\lstinline$?\=?$}, as in, ©x \ y© and ©x \= y©, which means $x^y$ and $x \leftarrow x^y$.505 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)©.506 507 As for \Index{division}, there are exponentiation operators for integral and floating-point types, including the builtin \Index{complex} types.508 Unsigned integral exponentiation\index{exponentiation!unsigned integral} is performed with repeated multiplication (or shifting if the base is 2).509 Signed integral exponentiation\index{exponentiation!signed integral} is performed with repeated multiplication (or shifting if the base is 2), but yields a floating-point result because $b^{-e}=1/b^e$.510 Hence, it is important to designate exponent integral-constants as unsigned or signed: ©3 \ 3u© return an integral result, while ©3 \ 3© returns a floating-point result.511 Floating-point exponentiation\index{exponentiation!floating point} is performed using \Index{logarithm}s\index{exponentiation!logarithm}, so the base cannot be negative.512 \begin{cfa}513 sout | 2 ®\® 8u | 4 ®\® 3u | -4 ®\® 3u | 4 ®\® -3 | -4 ®\® -3 | 4.0 ®\® 2.1 | (1.0f+2.0fi) ®\® (3.0f+2.0fi) | endl;514 256 64 -64 0.015625 -0.015625 18.3791736799526 0.264715-1.1922i515 \end{cfa}516 Parenthesis are necessary for the complex constants or the expresion is parsed as ©1.0f+(2.0fi \ 3.0f)+2.0fi©.517 The exponentiation operator is available for all the basic types, but for user-defined types, only the integral version is available, if the user type defines multiplication, ©*©, and one, ©1©.518 519 520 504 \section{\texorpdfstring{Labelled \LstKeywordStyle{continue} / \LstKeywordStyle{break}}{Labelled continue / break}} 521 505 522 506 While C provides ©continue© and ©break© statements for altering control flow, both are restricted to one level of nesting for a particular control structure. 523 507 Unfortunately, this restriction forces programmers to use \Indexc{goto} to achieve the equivalent control-flow for more than one level of nesting. 524 To prevent having to switch to the ©goto©, \CFA extends the \Indexc{continue}\index{continue@\lstinline $continue$!labelled}\index{labelled!continue@©continue©} and \Indexc{break}\index{break@\lstinline $break$!labelled}\index{labelled!break@©break©} with a target label to support static multi-level exit\index{multi-level exit}\index{static multi-level exit}~\cite{Buhr85} .508 To prevent having to switch to the ©goto©, \CFA extends the \Indexc{continue}\index{continue@\lstinline $continue$!labelled}\index{labelled!continue@©continue©} and \Indexc{break}\index{break@\lstinline $break$!labelled}\index{labelled!break@©break©} with a target label to support static multi-level exit\index{multi-level exit}\index{static multi-level exit}~\cite{Buhr85}, as in Java. 525 509 For both ©continue© and ©break©, the target label must be directly associated with a ©for©, ©while© or ©do© statement; 526 510 for ©break©, the target label can also be associated with a ©switch©, ©if© or compound (©{}©) statement. 527 511 \VRef[Figure]{f:MultiLevelResumeTermination} shows the labelled ©continue© and ©break©, specifying which control structure is the target for exit, and the corresponding C program using only ©goto©. 528 512 The innermost loop has 7 exit points, which cause resumption or termination of one or more of the 7 \Index{nested control-structure}s. 529 Java supports both labelled ©continue© and ©break© statements.530 513 531 514 \begin{figure} … … 540 523 ®LF:® for ( ... ) { 541 524 ®LW:® while ( ... ) { 542 ... break ®LC®; ... 543 ... break ®LS®; ... 544 ... break ®LIF®; ... 545 ... continue ®LF;® ... 546 ... break ®LF®; ... 547 ... continue ®LW®; ... 548 ... break ®LW®; ... 525 ... break ®LC®; ... // terminate compound 526 ... break ®LS®; ... // terminate switch 527 ... break ®LIF®; ... // terminate if 528 ... continue ®LF;® ... // resume loop 529 ... break ®LF®; ... // terminate loop 530 ... continue ®LW®; ... // resume loop 531 ... break ®LW®; ... // terminate loop 549 532 } // while 550 533 } // for 551 534 } else { 552 ... break ®LIF®; ... 535 ... break ®LIF®; ... // terminate if 553 536 } // if 554 537 } // switch … … 592 575 LF: for ( ;; ) { 593 576 LW: while ( 1 ) { 594 break LC; 595 break LS; 596 break LIF; 597 continue LF; 598 break LF; 599 continue LW; 600 break LW; 577 break LC; // terminate compound 578 break LS; // terminate switch 579 break LIF; // terminate if 580 continue LF; // resume loop 581 break LF; // terminate loop 582 continue LW; // resume loop 583 break LW; // terminate loop 601 584 } // while 602 585 } // for 603 586 } else { 604 break LIF; 587 break LIF; // terminate if 605 588 } // if 606 589 } // switch … … 641 624 \item 642 625 They cannot branch into a control structure. 643 This restriction prevents missing initializationat the start of a control structure resulting in undefined behaviour.626 This restriction prevents missing declarations and/or initializations at the start of a control structure resulting in undefined behaviour. 644 627 \end{itemize} 645 628 The advantage of the labelled ©continue©/©break© is allowing static multi-level exits without having to use the ©goto© statement, and tying control flow to the target control structure rather than an arbitrary point in a program. 646 Furthermore, the location of the label at the \emph{beginning} of the target control structure informs the reader that complex control-flow is occurring in the body of the control structure.629 Furthermore, the location of the label at the \emph{beginning} of the target control structure informs the reader (\Index{eye candy}) that complex control-flow is occurring in the body of the control structure. 647 630 With ©goto©, the label is at the end of the control structure, which fails to convey this important clue early enough to the reader. 648 631 Finally, using an explicit target for the transfer instead of an implicit target allows new constructs to be added or removed without affecting existing constructs. … … 736 719 The problem with this usage is branching into control structures, which is known to cause both comprehension and technical difficulties. 737 720 The comprehension problem occurs from the inability to determine how control reaches a particular point due to the number of branches leading to it. 738 The technical problem results from the inability to ensure allocation and initialization of variables when blocks are not entered at the beginning. 739 Often transferring into a block can bypass variable declaration and/or its initialization, which results in subsequent errors. 740 There are virtually no positive arguments for this kind of control flow, and therefore, there is a strong impetus to eliminate it. 721 The technical problem results from the inability to ensure declaration and initialization of variables when blocks are not entered at the beginning. 722 There are no positive arguments for this kind of control flow, and therefore, there is a strong impetus to eliminate it. 741 723 Nevertheless, C does have an idiom where this capability is used, known as ``\Index*{Duff's device}''~\cite{Duff83}: 742 724 \begin{cfa} … … 954 936 but it is cumbersome having to write "©this.©" many times in a member. 955 937 956 \CFA provides a ©with© clause/statement to elided the "©this.©" by opening a scope containing field identifiers and changing the qualified fields into variables, giving an opportunity for optimizing qualified references.\footnote{ 957 The ©with© statement comes from Pascal~\cite[\S~4.F]{Pascal}.} 958 \begin{cfa} 959 int mem( S &this ) ®with this® { 938 \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. 939 \begin{cfa} 940 int mem( S &this ) ®with this® { // with clause 960 941 i = 1; ®// this.i 961 942 ® j = 2; ®// this.j … … 976 957 struct S1 { ... } s1; 977 958 struct S2 { ... } s2; 978 ®with s1® { 959 ®with s1® { // with statement 979 960 // access fields of s1 without qualification 980 961 ®with s2® { // nesting … … 994 975 struct T { int i; int k; int m } b, c; 995 976 ®with a, b® { 996 j + k; §\C{// unambiguous, unique names define unique type }§977 j + k; §\C{// unambiguous, unique names define unique types}§ 997 978 i; §\C{// ambiguous, same name and type}§ 998 a.i + b.i; §\C{// unambiguous, qualification defines unique type}§999 m; §\C{// ambiguous, no context to define unique type}§1000 m = 5.0; §\C{// unambiguous, context defines unique type}§1001 m = 1; §\C{// unambiguous, context defines unique type}§1002 } 1003 ®with c® { ... } §\C{// ambiguous, no context}§1004 ®with (S)c® { ... } §\C{// unambiguous, cast defines unique type}§979 a.i + b.i; §\C{// unambiguous, qualification defines unique names}§ 980 m; §\C{// ambiguous, same name and no context to define unique type}§ 981 m = 5.0; §\C{// unambiguous, same name and context defines unique type}§ 982 m = 1; §\C{// unambiguous, same name and context defines unique type}§ 983 } 984 ®with c® { ... } §\C{// ambiguous, same name and no context}§ 985 ®with (S)c® { ... } §\C{// unambiguous, same name and cast defines unique type}§ 1005 986 \end{cfa} 1006 987 … … 1028 1009 } 1029 1010 \end{cfa} 1030 The kind of raise and handler match: ©throw© with ©catch© and @throwResume@ with @catchResume@.1011 The kind of raise and handler match: ©throw© with ©catch© and ©throwResume© with ©catchResume©. 1031 1012 Then the exception type must match along with any additonal predicate must be true. 1032 1013 The ©catch© and ©catchResume© handlers may appear in any oder. 1033 However, the ©finally© clause must 1014 However, the ©finally© clause must appear at the end of the ©try© statement. 1034 1015 1035 1016 … … 1242 1223 1243 1224 1225 \section{Exponentiation Operator} 1226 1227 C, \CC, and Java (and many other programming languages) have no exponentiation operator\index{exponentiation!operator}\index{operator!exponentiation}, \ie $x^y$, and instead use a routine, like \Indexc{pow}, to perform the exponentiation operation. 1228 \CFA extends the basic operators with the exponentiation operator ©?\?©\index{?\\?@\lstinline$?\?$} and ©?\=?©\index{?\\=?@\lstinline$?\=?$}, as in, ©x \ y© and ©x \= y©, which means $x^y$ and $x \leftarrow x^y$. 1229 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)©. 1230 1231 As for \Index{division}, there are exponentiation operators for integral and floating-point types, including the builtin \Index{complex} types. 1232 Unsigned integral exponentiation\index{exponentiation!unsigned integral} is performed with repeated multiplication (or shifting if the base is 2). 1233 Signed integral exponentiation\index{exponentiation!signed integral} is performed with repeated multiplication (or shifting if the base is 2), but yields a floating-point result because $b^{-e}=1/b^e$. 1234 Hence, it is important to designate exponent integral-constants as unsigned or signed: ©3 \ 3u© return an integral result, while ©3 \ 3© returns a floating-point result. 1235 Floating-point exponentiation\index{exponentiation!floating point} is performed using \Index{logarithm}s\index{exponentiation!logarithm}, so the base cannot be negative. 1236 \begin{cfa} 1237 sout | 2 ®\® 8u | 4 ®\® 3u | -4 ®\® 3u | 4 ®\® -3 | -4 ®\® -3 | 4.0 ®\® 2.1 | (1.0f+2.0fi) ®\® (3.0f+2.0fi) | endl; 1238 256 64 -64 0.015625 -0.015625 18.3791736799526 0.264715-1.1922i 1239 \end{cfa} 1240 Parenthesis are necessary for the complex constants or the expresion is parsed as ©1.0f+(2.0fi \ 3.0f)+2.0fi©. 1241 The exponentiation operator is available for all the basic types, but for user-defined types, only the integral-computation versions are available. 1242 For returning an integral value, the user type ©T© must define multiplication, ©*©, and one, ©1©; 1243 for returning a floating-point value, an additional divide of type ©T© into a ©double© returning a ©double© (©double ?/?( double, T )©) is necessary for negative exponents. 1244 1245 1244 1246 \section{Pointer / Reference} 1245 1247 … … 1250 1252 An integer constant expression with the value 0, or such an expression cast to type ©void *©, is called a \newterm{null-pointer constant}.\footnote{ 1251 1253 One way to conceptualize the null pointer is that no variable is placed at this address, so the null-pointer address can be used to denote an uninitialized pointer/reference object; 1252 \ie the null pointer is guaranteed to compare unequal to a pointer to any object or routine.} 1254 \ie the null pointer is guaranteed to compare unequal to a pointer to any object or routine. 1255 In general, a value with special meaning among a set of values is called a \emph{\Index{sentinel value}}, \eg ©-1© as a return code value.} 1253 1256 An address is \newterm{sound}, if it points to a valid memory location in scope, \ie within the program's execution-environment and has not been freed. 1254 1257 Dereferencing an \newterm{unsound} address, including the null pointer, is \Index{undefined}, often resulting in a \Index{memory fault}. … … 1285 1288 \hline 1286 1289 \begin{cfa} 1287 lda r1,100 1288 ld r2,(r1) 1289 lda r3,104 1290 st r2,(r3) 1290 lda r1,100 // load address of x 1291 ld r2,(r1) // load value of x 1292 lda r3,104 // load address of y 1293 st r2,(r3) // store x into y 1291 1294 \end{cfa} 1292 1295 & 1293 1296 \begin{cfa} 1294 1297 1295 ld r2,(100) 1296 1297 st r2,(104) 1298 ld r2,(100) // load value of x 1299 1300 st r2,(104) // store x into y 1298 1301 \end{cfa} 1299 1302 \end{tabular} … … 3265 3268 \Indexc{gcc} provides ©typeof© to declare a secondary variable from a primary variable. 3266 3269 \CFA also relies heavily on the specification of the left-hand side of assignment for type inferencing, so in many cases it is crucial to specify the type of the left-hand side to select the correct type of the right-hand expression. 3267 Only for overloaded routines with the same return typeis variable type-inferencing possible.3270 Only for overloaded routines \emph{with the same return type} is variable type-inferencing possible. 3268 3271 Finally, ©auto© presents the programming problem of tracking down a type when the type is actually needed. 3269 3272 For example, given … … 5407 5410 ©try© \\ 5408 5411 ©ttype© \\ 5412 ©with© \\ 5409 5413 ©zero_t© \\ 5410 \\5411 5414 \end{tabular} 5412 5415 \end{tabular} … … 5442 5445 g( p1, p2 ) int p1, p2; §\C{// int g( int p1, int p2 );}§ 5443 5446 \end{cfa} 5444 \CFA supportsK\&R routine definitions:5447 \CFA continues to support K\&R routine definitions: 5445 5448 \begin{cfa} 5446 5449 f( a, b, c ) §\C{// default int return}§
Note: See TracChangeset
for help on using the changeset viewer.