Ignore:
Timestamp:
Aug 8, 2024, 10:39:40 PM (13 hours ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
acab1bd
Parents:
c1c0efdb
Message:

Minor update on the thesis (add auto initialization and update future work

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/jiada_liang_MMath/background.tex

    rc1c0efdb r7568e5c  
    11\chapter{Background}
    22
    3 This chapter covers background material for C enumerations and \CFA features used in later discussion.
     3This chapter covers background material for C enumerations and \CFA features used in later discussions.
    44
    55
     
    1414\begin{enumerate}[leftmargin=*]
    1515\item
    16 For @#define@, the programmer has to explicitly manage the constant name and value.
    17 Furthermore, these C preprocessor macro names are outside of the C type-system and can incorrectly change random text in a program.
     16For @#define@, the programmer must explicitly manage the constant name and value.
     17Furthermore, these C preprocessor macro names are outside the C type system and can incorrectly change random text in a program.
    1818\item
    1919The same explicit management is true for the @const@ declaration, and the @const@ variable cannot appear in constant-expression locations, like @case@ labels, array dimensions,\footnote{
    20 C allows variable-length array-declarations (VLA), so this case does work, but it fails in \CC, which does not support VLAs, unless it is \lstinline{g++}.} immediate oper\-ands of assembler instructions, and occupies storage.
     20C allows variable-length array declarations (VLA), so this case does work. Still, it fails in \CC, which does not support VLAs, unless it is \lstinline{g++}.} immediate oper\-ands of assembler instructions and occupies storage.
    2121\begin{clang}
    2222$\$$ nm test.o
     
    8787enum { Size = 20, Max = 10, MaxPlus10 = Max + 10, @Max10Plus1@, Fred = -7 };
    8888\end{clang}
    89 Here, the aliased constants are: 20, 10, 20, 21, and -7.
    90 Direct initialization is by a compile-time expression generating a constant value.
     89Here, the aliased constants are 20, 10, 20, 21, and -7.
     90Direct initialization is achieved by a compile-time expression that generates a constant value.
    9191Indirect initialization (without an initializer, @Max10Plus1@) is called \newterm{auto-initialization}, where enumerators are assigned values from left to right, starting at zero or the next explicitly initialized constant, incrementing by @1@.
    9292Because multiple independent enumerators can be combined, enumerators with the same values can occur.
    93 The enumerators are rvalues, so assignment is disallowed.
     93The enumerators are @rvalues@, so the assignment is disallowed.
    9494Finally, enumerators are \newterm{unscoped}, \ie enumerators declared inside of an @enum@ are visible (projected) outside into the enclosing scope of the @enum@ type.
    95 For unnamed enumerations, this semantic is required because there is no type name for scoped qualification.
    96 
    97 As noted, this kind of aliasing declaration is not an enumeration, even though it is declared using an @enum@ in C.
     95This semantic is required for unnamed enumerations because there is no type name for scoped qualification.
     96
     97As noted, this aliasing declaration is not an enumeration, even though it is declared using an @enum@ in C.
    9898While the semantics is misleading, this enumeration form matches with aggregate types:
    9999\begin{cfa}
     
    121121};
    122122\end{clang}
    123 Note, the comma in the enumerator list can be a terminator or a separator, allowing the list to end with a dangling comma.\footnote{
     123Note the comma in the enumerator list can be a terminator or a separator, allowing the list to end with a dangling comma.\footnote{
    124124A terminating comma appears in other C syntax, \eg the initializer list.}
    125125This feature allows enumerator lines to be interchanged without moving a comma.
     
    130130\label{s:CenumImplementation}
    131131
    132 In theory, a C enumeration \emph{variable} is an implementation-defined integral type large enough to hold all enumerator values.
     132Theoretically, a C enumeration \emph{variable} is an implementation-defined integral type large enough to hold all enumerator values.
    133133In practice, C defines @int@~\cite[\S~6.4.4.3]{C11} as the underlying type for enumeration variables, restricting initialization to integral constants, which have type @int@ (unless qualified with a size suffix).
    134134However, type @int@ is defined as:
     
    137137\end{quote}
    138138However, @int@ means 4 bytes on both 32/64-bit architectures, which does not seem like the ``natural'' size for a 64-bit architecture.
    139 Whereas, @long int@ means 4 bytes on a 32-bit and 8 bytes on 64-bit architectures, and @long long int@ means 8 bytes on both 32/64-bit architectures, where 64-bit operations are simulated on 32-bit architectures.
    140 \VRef[Figure]{f:gccEnumerationStorageSize} shows both @gcc@ and @clang@ partially ignore this specification and type the integral size of an enumerator based its initialization.
    141 Hence, initialization in the range @INT_MIN@..@INT_MAX@ results in a 4-byte enumerator, and outside this range the enumerator is 8 bytes.
     139Whereas @long int@ means 4 bytes on a 32-bit and 8 bytes on 64-bit architectures, and @long long int@ means 8 bytes on both 32/64-bit architectures, where 64-bit operations are simulated on 32-bit architectures.
     140\VRef[Figure]{f:gccEnumerationStorageSize} shows both @gcc@ and @clang@ partially ignore this specification and type the integral size of an enumerator based on its initialization.
     141Hence, initialization in the range @INT_MIN@..@INT_MAX@ results in a 4-byte enumerator, and outside this range, the enumerator is 8 bytes.
    142142Note that @sizeof( typeof( IMin ) ) != sizeof( E )@, making the size of an enumerator different than is containing enumeration type, which seems inconsistent, \eg @sizeof( typeof( 3 ) ) == sizeof( int )@.
    143143
     
    168168\label{s:Usage}
    169169
    170 C proves an implicit \emph{bidirectional} conversion between an enumeration and its integral type, and between two different enumerations.
     170C proves an implicit \emph{bidirectional} conversion between an enumeration and its integral type and between two different enumerations.
    171171\begin{clang}
    172172enum Week week = Mon;                           $\C{// week == 0}$
     
    178178@week = Winter;@                                        $\C{// UNDEFINED! implicit conversion to Week}$
    179179\end{clang}
    180 While converting an enumerator to its underlying type is useful, the implicit conversion from the base or another enumeration type to an enumeration is a common source of error.
     180While converting an enumerator to its underlying type is sound, the implicit conversion from the base or another enumeration type to an enumeration is a common source of error.
    181181
    182182Enumerators can appear in @switch@ and looping statements.
     
    194194\end{cfa}
    195195For iterating using arithmetic to make sense, the enumerator values \emph{must} have a consecutive ordering with a fixed step between values.
    196 For example, a previous gap introduced by @Thu = 10@, results in iterating over the values 0--13, where values 3--9 are not @Week@ values.
    197 Note, it is the bidirectional conversion that allows incrementing @day@: @day@ is converted to @int@, integer @1@ is added, and the result is converted back to @Week@ for the assignment to @day@.
     196For example, a previous gap introduced by @Thu = 10@ results in iterating over the values 0--13, where values 3--9 are not @Week@ values.
     197Note that the bidirectional conversion allows incrementing @day@: @day@ is converted to @int@, integer @1@ is added, and the result is converted back to @Week@ for the assignment to @day@.
    198198For safety, \CC does not support the bidirectional conversion, and hence, an unsafe cast is necessary to increment @day@: @day = (Week)(day + 1)@.
    199199
    200 There is a C idiom to automatically compute the number of enumerators in an enumeration.
     200There is a C idiom that computes the number of enumerators in an enumeration automatically.
    201201\begin{cfa}
    202202enum E { A, B, C, D, @N@ };  // N == 4
    203203for ( enum E e = A; e < @N@; e += 1 ) ...
    204204\end{cfa}
    205 Here, serendipitously the auto-incrementing counts the number of enumerators and puts the total into the last enumerator @N@.
     205Serendipitously, the auto-incrementing counts the number of enumerators and puts the total into the last enumerator @N@.
    206206This @N@ is often used as the dimension for an array associated with the enumeration.
    207207\begin{cfa}
     
    226226\end{cfa}
    227227However, the companion idiom results in the \emph{harmonizing} problem because an update to the enumeration @Integral_Type@ often requires a corresponding update to the companion array \snake{Integral_Name}.
    228 The requirement to harmonize is at best indicated by a comment before the enumeration.
     228The requirement to harmonize is, at best, indicated by a comment before the enumeration.
    229229This issue is exacerbated if enumeration and companion array are in different translation units.
    230230
    231231\bigskip
    232 While C provides a true enumeration, it is restricted, has unsafe semantics, and does not provide useful/advanced enumeration features found in other programming languages.
     232While C provides a true enumeration, it is restricted, has unsafe semantics, and does not provide helpful/advanced enumeration features in other programming languages.
    233233
    234234
    235235\section{\texorpdfstring{\CFA}{Cforall}}
    236236
    237 \CFA in \emph{not} an object-oriented programming-language, \ie functions cannot be nested in aggregate types, and hence, there is no \newterm{receiver} notation for calling functions, \eg @obj.method(...)@, where the first argument proceeds the call and becomes an implicit first (\lstinline[language=C++]{this}) parameter.
     237\CFA in \emph{not} an object-oriented programming language, \ie functions cannot be nested in aggregate types, and hence, there is no \newterm{receiver} notation for calling functions, \eg @obj.method(...)@, where the first argument proceeds the call and becomes an implicit first (\lstinline[language=C++]{this}) parameter.
    238238The following sections provide short descriptions of \CFA features needed further in the thesis.
    239 Other \CFA features are presented in-situ with short explanations, or no explanation because the feature is obvious to C programmers.
     239Other \CFA features are presented in situ with short or no explanation because the feature is obvious to C programmers.
    240240
    241241
    242242\subsection{Overloading}
    243243
    244 Overloading allows programmers to use the most meaningful names without fear of name clashes within a program or from external sources, like include files.
     244Overloading allows programmers to use the most meaningful names without fear of name clashes within a program or from external sources like included files.
    245245\begin{quote}
    246246There are only two hard things in Computer Science: cache invalidation and naming things. --- Phil Karlton
     
    269269\subsection{Function Overloading}
    270270
    271 Both \CFA and \CC allow function names to be overloaded, as long as their prototypes differ in the number and type of parameters and returns.
     271Both \CFA and \CC allow function names to be overloaded as long as their prototypes differ in the number and type of parameters and returns.
    272272\begin{cfa}
    273273void f( void );                 $\C[1.75in]{// (1): no parameter}$
     
    277277\end{cfa}
    278278In this case, the name @f@ is overloaded depending on the number and parameter types.
    279 The type system examines each call size and selects the best match based on the number and types of the arguments.
    280 Here, there is a perfect match for the call, @f( 'A' )@ with the number and parameter type of function (2).
    281 
    282 Ada, Scala, and \CFA type-systems also use the return type in resolving a call, to pinpoint the best overloaded name.
     279The type system examines each call size and selects the best match based on the number and types of arguments.
     280Here, the call @f( 'A' )@ is a perfect match for the number and parameter type of function (2).
     281
     282Ada, Scala, and \CFA type-systems also use the return type to pinpoint the best-overloaded name in resolving a call.
    283283\begin{cfa}
    284284int f( void );                  $\C[1.75in]{// (4); overloaded on return type}$
     
    302302}
    303303\end{cfa}
    304 The \CFA type system simply treats overloaded variables as an overloaded function returning a value with no parameters.
     304The \CFA type system treats overloaded variables as an overloaded function returning a value with no parameters.
    305305Hence, no significant effort is required to support this feature.
    306306
     
    312312
    313313All objects in \CFA are initialized by @constructors@ \emph{after} allocation and de-initialized \emph{before} deallocation.
    314 \CC cannot have constructors for basic-types because they have no aggregate type \lstinline[language=C++]{struct/class} in which to insert a constructor definition.
     314\CC cannot have constructors for basic types because they have no aggregate type \lstinline[language=C++]{struct/class} in which to insert a constructor definition.
    315315Like \CC, \CFA has multiple auto-generated constructors for every type.
    316316
    317317The prototype for the constructor/destructor are @void ?{}( T &, ... )@ and @void ^?{}( T &, ... )@, respectively.
    318 The first parameter is logically the \lstinline[language=C++]{this} or \lstinline[language=Python]{self} in other object-oriented languages, and implicitly passed.
     318The first parameter is logically the \lstinline[language=C++]{this} or \lstinline[language=Python]{self} in other object-oriented languages and implicitly passed.
    319319\VRef[Figure]{f:CFAConstructorDestructor} shows an example of creating and using a constructor and destructor.
    320320Both constructor and destructor can be explicitly called to reuse a variable.
     
    350350
    351351The C constants @0@ and @1@ have special meaning.
    352 @0@ is the null pointer and used in conditional expressions, where @if ( p )@ is rewritten @if ( p != 0 )@;
     352@0@ is the null pointer and is used in conditional expressions, where @if ( p )@ is rewritten @if ( p != 0 )@;
    353353@1@ is an additive identity in unary operators @++@ and @--@.
    354354Aware of their significance, \CFA provides a special type @zero_t@ and @one_t@ for custom types.
     
    385385bar( s );
    386386\end{cfa}
    387 The assertion on @T@ restricts the range of types that can be manipulated by @bar@ to only those that have an implementation of @foo@ with the matching signature, allowing @bar@'s call to @foo@ in its body.
     387The assertion on @T@ restricts the range of types that can be manipulated by @bar@ to only those that implement @foo@ with the matching signature, allowing @bar@'s call to @foo@ in its body.
    388388Unlike templates in \CC, which are macro expansions at the call site, \CFA polymorphic functions are compiled, passing the call-site assertion functions as hidden parameters.
    389389
     
    392392
    393393A @forall@ clause can assert many restrictions on multiple types.
    394 A common practice is to refactor the assertions into a named \newterm{trait}, similar to other languages, like Go and Rust.
     394A common practice is refactoring the assertions into a named \newterm{trait}, similar to other languages like Go and Rust.
    395395\begin{cfa}
    396396forall(T) trait @Bird@ {
     
    407407bird_fly( 23, robin );
    408408\end{cfa}
    409 Grouping type assertions into a named trait effectively creates a reusable interface for parametric-polymorphic types.
     409Grouping type assertions into a named trait effectively creates a reusable interface for parametric polymorphic types.
    410410
    411411
     
    417417
    418418The \CFA resolver attempts to identify the best candidate based on: first, the number of parameters and types, and second, when no exact match exists, the fewest implicit conversions and polymorphic variables.
    419 Finding an exact match is not discussed here, because the mechanism is fairly straightforward, even when the search space is large;
     419Finding an exact match is not discussed here, because the mechanism is fairly straightforward, even when the search space is ample;
    420420only finding a non-exact match is discussed in detail.
    421421
     
    425425
    426426Most programming languages perform some implicit conversions among basic types to facilitate mixed-mode arithmetic;
    427 otherwise, the program becomes littered with many explicit casts, which does not match with programmer's expectations.
    428 C is an aggressive language as it provides conversions among almost all of the basic types, even when the conversion is potentially unsafe or not meaningful, \ie @float@ to @bool@.
     427otherwise, the program becomes littered with many explicit casts which do not match the programmer's expectations.
     428C is an aggressive language, providing conversions among almost all basic types, even when the conversion is potentially unsafe or not meaningful, \ie @float@ to @bool@.
    429429C defines the resolution pattern as ``usual arithmetic conversion''~\cite[\S~6.3.1.8]{C11}, in which C looks for a \newterm{common type} between operands, and converts one or both operands to the common type.
    430 Loosely defined, a common type is the smallest type in terms of the size of representation that both operands can be converted into without losing their precision, called a \newterm{widening} or \newterm{safe conversion}.
     430A common type is the smallest type in terms of the size of representation that both operands can be converted into without losing their precision, called a \newterm{widening} or \newterm{safe conversion}.
    431431
    432432\CFA generalizes ``usual arithmetic conversion'' to \newterm{conversion cost}.
     
    438438@poly@ is the number of polymorphic function parameters, and
    439439\item
    440 @safe@ is sum of the degree of safe (widening) conversions.
     440@safe@ is the sum of the degree of safe (widening) conversions.
    441441\end{enumerate}
    442442Sum of degree is a method to quantify C's integer and floating-point rank.
    443443Every pair of widening conversion types is assigned a \newterm{distance}, and the distance between the two same types is 0.
    444 For example, the distance from @char@ to @int@ is 2, the distance from @int@ to @long@ is 1, and the distance from @int@ to @long long int@ is 2.
     444For example, the distance from @char@ to @int@ is 2, from @int@ to @long@ is 1, and from @int@ to @long long int@ is 2.
    445445This distance does not mirror C's rank system.
    446 For example, the rank of @char@ and @signed char@ are the same in C, but the distance from @char@ to @signed char@ is assigned 1.
     446For example, the @char@ and @signed char@ ranks are the same in C, but the distance from @char@ to @signed char@ is assigned 1.
    447447@safe@ cost is summing all pairs of arguments to parameter safe conversion distances.
    448 Among the three costs in Bilson's model, @unsafe@ is the most significant cost and @safe@ is the least significant, with an implication that \CFA always choose a candidate with the lowest @unsafe@, if possible.
    449 
    450 For example, assume the overloaded function @foo@ is called with two @int@ parameter.
    451 The cost for every overloaded @foo@ has been listed along:
     448Among the three costs in Bilson's model, @unsafe@ is the most significant cost, and @safe@ is the least significant, implying that \CFA always chooses a candidate with the lowest @unsafe@, if possible.
     449
     450For example, assume the overloaded function @foo@ is called with two @int@ parameters.
     451The cost for every overloaded @foo@ has been listed along with the following:
    452452\begin{cfa}
    453453void foo( char, char );                         $\C[2.5in]{// (1) (2, 0, 0)}$
     
    479479\CFA favours candidates with more restrictions on polymorphism, so @forall( T ) void foo( T, T )@ has lower cost.
    480480@specialization@ is an arbitrary count-down value starting at zero.
    481 For every type assertion in @forall@ clause (no assertions in the above example), \CFA subtracts one from @specialization@.
    482 More type assertions means more constraints on argument types, making the function less generic.
     481For every type assertion in the @forall@ clause (no assertions in the above example), \CFA subtracts one from @specialization@.
     482More type assertions mean more constraints on argument types, making the function less generic.
    483483
    484484\CFA defines two special cost values: @zero@ and @infinite@.
    485 A conversion cost is @zero@ when the argument and parameter have an exact match, and a conversion cost is @infinite@ when there is no defined conversion between two types.
     485A conversion cost is @zero@ when the argument and parameter have an exact match, and a conversion cost is @infinite@ when there is no defined conversion between the two types.
    486486For example, the conversion cost from @int@ to a @struct S@ is @infinite@.
    487487
    488488In \CFA, the meaning of a C-style cast is determined by its @Cast Cost@.
    489 For most cast-expression resolutions, a cast cost is equal to a conversion cost.
    490 Cast cost exists as an independent matrix for conversion that cannot happen implicitly, while being possible with an explicit cast.
    491 These conversions are often defined to have a infinite conversion cost and a non-infinite cast cost.
     489For most cast-expression resolutions, a cast cost equals a conversion cost.
     490Cast cost exists as an independent matrix for conversion that cannot happen implicitly while being possible with an explicit cast.
     491These conversions are often defined as having an infinite conversion cost and a non-infinite cast cost.
Note: See TracChangeset for help on using the changeset viewer.