Changeset eba74ba for doc/papers/general/Paper.tex
- Timestamp:
- May 25, 2018, 2:51:06 PM (6 years ago)
- Branches:
- new-env, with_gc
- Children:
- cdc4d43
- Parents:
- 3ef35bd (diff), 58e822a (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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/papers/general/Paper.tex
r3ef35bd reba74ba 243 243 Nevertheless, C, first standardized almost forty years ago~\cite{ANSI89:C}, lacks many features that make programming in more modern languages safer and more productive. 244 244 245 \CFA (pronounced ``C-for-all'', and written \CFA or Cforall) is an evolutionary extension of the C programming language that adds modern language-features to C, while maintaining both source and runtime compatibility with C and a familiar programming model for programmers.245 \CFA (pronounced ``C-for-all'', and written \CFA or Cforall) is an evolutionary extension of the C programming language that adds modern language-features to C, while maintaining source and runtime compatibility in the familiar C programming model. 246 246 The four key design goals for \CFA~\cite{Bilson03} are: 247 247 (1) The behaviour of standard C code must remain the same when translated by a \CFA compiler as when translated by a C compiler; … … 273 273 Starting with a translator versus a compiler makes it easier and faster to generate and debug C object-code rather than intermediate, assembler or machine code. 274 274 The translator design is based on the \emph{visitor pattern}, allowing multiple passes over the abstract code-tree, which works well for incrementally adding new feature through additional visitor passes. 275 At the heart of the translator is the type resolver, which handles the polymorphic routine/type overload-resolution.275 At the heart of the translator is the type resolver, which handles the polymorphic function/type overload-resolution. 276 276 % @plg2[8]% cd cfa-cc/src; cloc libcfa 277 277 % ------------------------------------------------------------------------------- … … 310 310 311 311 Finally, it is impossible to describe a programming language without usages before definitions. 312 Therefore, syntax and semantics appear before explanations ;313 hence, patience is necessary until details are presented.312 Therefore, syntax and semantics appear before explanations, and related work (Section~\ref{s:RelatedWork}) is deferred until \CFA is presented; 313 hence, patience is necessary until details are discussed. 314 314 315 315 … … 329 329 \end{quote} 330 330 \vspace{-9pt} 331 C already has a limited form of ad-hoc polymorphism in the form ofits basic arithmetic operators, which apply to a variety of different types using identical syntax.331 C already has a limited form of ad-hoc polymorphism in its basic arithmetic operators, which apply to a variety of different types using identical syntax. 332 332 \CFA extends the built-in operator overloading by allowing users to define overloads for any function, not just operators, and even any variable; 333 333 Section~\ref{sec:libraries} includes a number of examples of how this overloading simplifies \CFA programming relative to C. … … 653 653 } 654 654 \end{cfa} 655 Since @pair( T *, T * )@ is a concrete type, there are no implicit parameters passed to @lexcmp@, so the generated code is identical to a function written in standard C using @void *@, yet the \CFA version is type-checked to ensure the fields of both pairs and the arguments to the comparison function match in type.655 Since @pair( T *, T * )@ is a concrete type, there are no implicit parameters passed to @lexcmp@, so the generated code is identical to a function written in standard C using @void *@, yet the \CFA version is type-checked to ensure the members of both pairs and the arguments to the comparison function match in type. 656 656 657 657 Another useful pattern enabled by reused dtype-static type instantiations is zero-cost \newterm{tag-structures}. … … 815 815 \subsection{Member Access} 816 816 817 It is also possible to access multiple fields from a single expression using a \newterm{member-access}.817 It is also possible to access multiple members from a single expression using a \newterm{member-access}. 818 818 The result is a single tuple-valued expression whose type is the tuple of the types of the members, \eg: 819 819 \begin{cfa} … … 1020 1020 \begin{cfa} 1021 1021 forall( dtype T0, dtype T1 | sized(T0) | sized(T1) ) struct _tuple2 { 1022 T0 field_0; T1 field_1; $\C{// generated before the first 2-tuple}$1022 T0 member_0; T1 member_1; $\C{// generated before the first 2-tuple}$ 1023 1023 }; 1024 1024 _tuple2(int, int) f() { 1025 1025 _tuple2(double, double) x; 1026 1026 forall( dtype T0, dtype T1, dtype T2 | sized(T0) | sized(T1) | sized(T2) ) struct _tuple3 { 1027 T0 field_0; T1 field_1; T2 field_2; $\C{// generated before the first 3-tuple}$1027 T0 member_0; T1 member_1; T2 member_2; $\C{// generated before the first 3-tuple}$ 1028 1028 }; 1029 1029 _tuple3(int, double, int) y; … … 1033 1033 1034 1034 \begin{comment} 1035 Since tuples are essentially structures, tuple indexing expressions are just fieldaccesses:1035 Since tuples are essentially structures, tuple indexing expressions are just member accesses: 1036 1036 \begin{cfa} 1037 1037 void f(int, [double, char]); … … 1047 1047 _tuple2(int, double) x; 1048 1048 1049 x. field_0+x.field_1;1050 printf("%d %g\n", x. field_0, x.field_1);1051 f(x. field_0, (_tuple2){ x.field_1, 'z' });1052 \end{cfa} 1053 Note that due to flattening, @x@ used in the argument position is converted into the list of its fields.1049 x.member_0+x.member_1; 1050 printf("%d %g\n", x.member_0, x.member_1); 1051 f(x.member_0, (_tuple2){ x.member_1, 'z' }); 1052 \end{cfa} 1053 Note that due to flattening, @x@ used in the argument position is converted into the list of its members. 1054 1054 In the call to @f@, the second and third argument components are structured into a tuple argument. 1055 1055 Similarly, tuple member expressions are recursively expanded into a list of member access expressions. … … 1083 1083 1084 1084 The various kinds of tuple assignment, constructors, and destructors generate GNU C statement expressions. 1085 A variable is generated to store the value produced by a statement expression, since its fields may need to be constructed with a non-trivial constructor and it may need to be referred to multiple time, \eg in a unique expression.1085 A variable is generated to store the value produced by a statement expression, since its members may need to be constructed with a non-trivial constructor and it may need to be referred to multiple time, \eg in a unique expression. 1086 1086 The use of statement expressions allows the translator to arbitrarily generate additional temporary variables as needed, but binds the implementation to a non-standard extension of the C language. 1087 1087 However, there are other places where the \CFA translator makes use of GNU C extensions, such as its use of nested functions, so this restriction is not new. … … 1493 1493 1494 1494 Heterogeneous data is often aggregated into a structure/union. 1495 To reduce syntactic noise, \CFA provides a @with@ statement (see Pascal~\cite[\S~4.F]{Pascal}) to elide aggregate field-qualification by opening a scope containing the fieldidentifiers.1495 To reduce syntactic noise, \CFA provides a @with@ statement (see Pascal~\cite[\S~4.F]{Pascal}) to elide aggregate member-qualification by opening a scope containing the member identifiers. 1496 1496 \begin{cquote} 1497 1497 \vspace*{-\baselineskip}%??? … … 1530 1530 The type must be an aggregate type. 1531 1531 (Enumerations are already opened.) 1532 The object is the implicit qualifier for the open structure- fields.1532 The object is the implicit qualifier for the open structure-members. 1533 1533 1534 1534 All expressions in the expression list are open in parallel within the compound statement, which is different from Pascal, which nests the openings from left to right. 1535 The difference between parallel and nesting occurs for fields with the same name and type:1536 \begin{cfa} 1537 struct S { int `i`; int j; double m; } s, w; 1535 The difference between parallel and nesting occurs for members with the same name and type: 1536 \begin{cfa} 1537 struct S { int `i`; int j; double m; } s, w; $\C{// member i has same type in structure types S and T}$ 1538 1538 struct T { int `i`; int k; int m; } t, w; 1539 with ( s, t ) { 1539 with ( s, t ) { $\C{// open structure variables s and t in parallel}$ 1540 1540 j + k; $\C{// unambiguous, s.j + t.k}$ 1541 1541 m = 5.0; $\C{// unambiguous, s.m = 5.0}$ … … 1549 1549 For parallel semantics, both @s.i@ and @t.i@ are visible, so @i@ is ambiguous without qualification; 1550 1550 for nested semantics, @t.i@ hides @s.i@, so @i@ implies @t.i@. 1551 \CFA's ability to overload variables means fields with the same name but different types are automatically disambiguated, eliminating most qualification when opening multiple aggregates.1551 \CFA's ability to overload variables means members with the same name but different types are automatically disambiguated, eliminating most qualification when opening multiple aggregates. 1552 1552 Qualification or a cast is used to disambiguate. 1553 1553 … … 1555 1555 \begin{cfa} 1556 1556 void ?{}( S & s, int i ) with ( s ) { $\C{// constructor}$ 1557 `s.i = i;` j = 3; m = 5.5; $\C{// initialize fields}$1557 `s.i = i;` j = 3; m = 5.5; $\C{// initialize members}$ 1558 1558 } 1559 1559 \end{cfa} … … 1659 1659 \lstMakeShortInline@% 1660 1660 \end{cquote} 1661 The only exception is bit 1661 The only exception is bit-field specification, which always appear to the right of the base type. 1662 1662 % Specifically, the character @*@ is used to indicate a pointer, square brackets @[@\,@]@ are used to represent an array or function return value, and parentheses @()@ are used to indicate a function parameter. 1663 1663 However, unlike C, \CFA type declaration tokens are distributed across all variables in the declaration list. … … 1715 1715 // pointer to array of 5 doubles 1716 1716 1717 // common bit 1717 // common bit-field syntax 1718 1718 1719 1719 … … 1911 1911 \subsection{Type Nesting} 1912 1912 1913 Nested types provide a mechanism to organize associated types and refactor a subset of fields into a named aggregate (\eg sub-aggregates @name@, @address@, @department@, within aggregate @employe@).1913 Nested types provide a mechanism to organize associated types and refactor a subset of members into a named aggregate (\eg sub-aggregates @name@, @address@, @department@, within aggregate @employe@). 1914 1914 Java nested types are dynamic (apply to objects), \CC are static (apply to the \lstinline[language=C++]@class@), and C hoists (refactors) nested types into the enclosing scope, meaning there is no need for type qualification. 1915 1915 Since \CFA in not object-oriented, adopting dynamic scoping does not make sense; 1916 instead \CFA adopts \CC static nesting, using the field-selection operator ``@.@'' for type qualification, as does Java, rather than the \CC type-selection operator ``@::@'' (see Figure~\ref{f:TypeNestingQualification}).1916 instead \CFA adopts \CC static nesting, using the member-selection operator ``@.@'' for type qualification, as does Java, rather than the \CC type-selection operator ``@::@'' (see Figure~\ref{f:TypeNestingQualification}). 1917 1917 \begin{figure} 1918 1918 \centering … … 2005 2005 Destruction parameters are useful for specifying storage-management actions, such as de-initialize but not deallocate.}. 2006 2006 \begin{cfa} 2007 struct VLA { int len, * data; }; $\C{// variable length array of integers}$2008 void ?{}( VLA & vla ) with ( vla ) { len = 10; data = alloc( len); } $\C{// default constructor}$2007 struct VLA { int size, * data; }; $\C{// variable length array of integers}$ 2008 void ?{}( VLA & vla ) with ( vla ) { size = 10; data = alloc( size ); } $\C{// default constructor}$ 2009 2009 void ^?{}( VLA & vla ) with ( vla ) { free( data ); } $\C{// destructor}$ 2010 2010 { … … 2013 2013 \end{cfa} 2014 2014 @VLA@ is a \newterm{managed type}\footnote{ 2015 A managed type affects the runtime environment versus a self-contained type.}: a type requiring a non-trivial constructor or destructor, or with a fieldof a managed type.2015 A managed type affects the runtime environment versus a self-contained type.}: a type requiring a non-trivial constructor or destructor, or with a member of a managed type. 2016 2016 A managed type is implicitly constructed at allocation and destructed at deallocation to ensure proper interaction with runtime resources, in this case, the @data@ array in the heap. 2017 2017 For details of the code-generation placement of implicit constructor and destructor calls among complex executable statements see~\cite[\S~2.2]{Schluntz17}. … … 2019 2019 \CFA also provides syntax for \newterm{initialization} and \newterm{copy}: 2020 2020 \begin{cfa} 2021 void ?{}( VLA & vla, int size, char fill ) with ( vla) { $\C{// initialization}$2022 len = size; data = alloc( len, fill );2021 void ?{}( VLA & vla, int size, char fill = '\0' ) { $\C{// initialization}$ 2022 vla.[ size, data ] = [ size, alloc( size, fill ) ]; 2023 2023 } 2024 2024 void ?{}( VLA & vla, VLA other ) { $\C{// copy, shallow}$ 2025 vla .len = other.len; vla.data = other.data;2025 vla = other; 2026 2026 } 2027 2027 \end{cfa} … … 2036 2036 2037 2037 \CFA constructors may be explicitly called, like Java, and destructors may be explicitly called, like \CC. 2038 Explicit calls to constructors double as a \CC-style \emph{placement syntax}, useful for construction of member fields in user-defined constructors and reuse of existing storage allocations.2038 Explicit calls to constructors double as a \CC-style \emph{placement syntax}, useful for construction of members in user-defined constructors and reuse of existing storage allocations. 2039 2039 Like the other operators in \CFA, there is a concise syntax for constructor/destructor function calls: 2040 2040 \begin{cfa} … … 2048 2048 y{ x }; $\C{// reallocate y, points to x}$ 2049 2049 x{}; $\C{// reallocate x, not pointing to y}$ 2050 // ^z{}; ^y{}; ^x{}; 2051 } 2050 } // ^z{}; ^y{}; ^x{}; 2052 2051 \end{cfa} 2053 2052 … … 2060 2059 For compatibility with C, a copy constructor from the first union member type is also defined. 2061 2060 For @struct@ types, each of the four functions are implicitly defined to call their corresponding functions on each member of the struct. 2062 To better simulate the behaviour of C initializers, a set of \newterm{ fieldconstructors} is also generated for structures.2061 To better simulate the behaviour of C initializers, a set of \newterm{member constructors} is also generated for structures. 2063 2062 A constructor is generated for each non-empty prefix of a structure's member-list to copy-construct the members passed as parameters and default-construct the remaining members. 2064 To allow users to limit the set of constructors available for a type, when a user declares any constructor or destructor, the corresponding generated function and all fieldconstructors for that type are hidden from expression resolution;2063 To allow users to limit the set of constructors available for a type, when a user declares any constructor or destructor, the corresponding generated function and all member constructors for that type are hidden from expression resolution; 2065 2064 similarly, the generated default constructor is hidden upon declaration of any constructor. 2066 2065 These semantics closely mirror the rule for implicit declaration of constructors in \CC\cite[p.~186]{ANSI98:C++}. … … 2740 2739 2741 2740 \section{Related Work} 2741 \label{s:RelatedWork} 2742 2742 2743 2743 … … 2793 2793 C provides variadic functions through @va_list@ objects, but the programmer is responsible for managing the number of arguments and their types, so the mechanism is type unsafe. 2794 2794 KW-C~\cite{Buhr94a}, a predecessor of \CFA, introduced tuples to C as an extension of the C syntax, taking much of its inspiration from SETL. 2795 The main contributions of that work were adding MRVF, tuple mass and multiple assignment, and record- fieldaccess.2795 The main contributions of that work were adding MRVF, tuple mass and multiple assignment, and record-member access. 2796 2796 \CCeleven introduced @std::tuple@ as a library variadic template structure. 2797 2797 Tuples are a generalization of @std::pair@, in that they allow for arbitrary length, fixed-size aggregation of heterogeneous values.
Note: See TracChangeset
for help on using the changeset viewer.