Ignore:
Timestamp:
Aug 6, 2024, 11:19:56 PM (4 months ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
43ab5fb
Parents:
d7cb0f7
Message:

Merge changes

Location:
doc/theses/jiada_liang_MMath
Files:
2 edited

Legend:

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

    rd7cb0f7 r63d5b9c8  
    236236However, the position of the underlying representation is the order of the enumerator in the new enumeration.
    237237\begin{cfa}
    238 enum() E1 { A };
    239 enum() E2 { B, C };
     238enum() E1 { B };                                                       
     239enum() E2 { C, D };                                                     
    240240enum() E3 { inline E1, inline E2, D };
    241 \end{cfa}
    242 Here, @A@ has position 0 in @E1@ and @E3@.
    243 @B@ has position 0 in @E2@ and 1 in @E3@.
    244 @C@ has position 1 in @E2@ and position 2 in @E3@.
    245 @D@ has position 3 in @E3@.
    246 
    247 A subtype enumeration can be casted, or implicitly converted into its supertype, with a @safe@ cost.
     241enum() E4 { A, inline E3, E };
     242\end{cfa}
     243In the example above, @B@ has the position 0 in @E1@ and @E3@, but it at the position 0 in @E4@ as taking the 0 there.
     244@C@ is at the position 0 in E2, 1 in E3 and 2 in E4. @D@ is at the position 1 in E2, 2 in E3 and 3 in E4.
     245
     246A subtype enumeration can be casted, or implicitly converted into its supertype, with a @safe@ cost. Such conversion is an @enumeration conversion@.
    248247\begin{cfa}
    249248enum E2 e2 = C;
    250 posn( e2 );                     $\C[1.75in]{// 1}$
     249posn( e2 );                     $\C[1.75in]{// 0}$
    251250enum E3 e3 = e2;
    252 posn( e2 );                     $\C{// 2}$
     251posn( e2 );                     $\C{// 1}$
    253252void foo( E3 e );
    254253foo( e2 );
    255 posn( (E3)e2 );         $\C{// 2}$
     254posn( (E3)e2 );         $\C{// 1}$
    256255E3 e31 = B;
    257 posn( e31 );            $\C{// 1}\CRT$
     256posn( e31 );            $\C{// 0}\CRT$
    258257\end{cfa}
    259258The last expression is unambiguous.
    260259While both @E2.B@ and @E3.B@ are valid candidate, @E2.B@ has an associated safe cost and \CFA selects the zero cost candidate @E3.B@.
    261 Hence, as discussed in \VRef{s:OpaqueEnum}, \CFA chooses position as a representation of the \CFA enum.
    262 Therefore, conversion involves both a change of type and possibly position.
    263 
    264 When converting a subtype to a supertype, its position can only be a larger value.
    265 The difference between the position in the subtype and in the supertype is its \newterm{offset}.
    266 \VRef[Figure]{s:OffsetSubtypeSuperType} show the algorithm to determine the offset for an subtype enumerator to its super type.
    267 \PAB{You need to explain the algorithm.}
    268 
    269 \begin{figure}
    270 \begin{cfa}
    271 struct Enumerator;
    272 struct CFAEnum {
    273         vector<variant<CFAEnum, Enumerator>> members;
    274 };
    275 pair<bool, int> calculateEnumOffset( CFAEnum dst, Enumerator e ) {
    276         int offset = 0;
    277         for ( auto v: dst.members ) {
    278                 if ( v.holds_alternative<Enumerator>() ) {
    279                         auto m = v.get<Enumerator>();
    280                         if ( m == e ) return make_pair( true, 0 );
    281                         offset++;
    282                 } else {
    283                         auto p = calculateEnumOffset( v, e );
    284                         if ( p.first ) return make_pair( true, offset + p.second );
    285                         offset += p.second;
    286                 }
    287         }
    288         return make_pair( false, offset );
    289 }
    290 \end{cfa}
    291 \caption{Compute Offset from Subtype Enumerator to Super Type}
    292 \label{s:OffsetSubtypeSuperType}
    293 \end{figure}
    294260
    295261For the given function prototypes, the following calls are valid.
     
    313279Note, the validity of calls is the same for call-by-reference as for call-by-value, and @const@ restrictions are the same as for other types.
    314280
     281\subsection{Offset Calculation}
     282As discussed in \VRef{s:OpaqueEnum}, \CFA chooses position as a representation of a \CFA enumeration variable.
     283Because enumerators has different @position@ between subtype and supertype, \CFA might need to manipulate the representation whenever a cast or
     284implicit conversion involves two \CFA enums. \CFA determine how a position is going to change with an @offset calculation@ function, reflects to
     285enumerator as an arithmetic expression. Casting an enumeration of subtype to a supertype, the position can be unchanged or increase. The change
     286of position is an @offset@. 
     287
     288\begin{figure}
     289\begin{cfa}
     290struct Enumerator;
     291struct CFAEnum { vector<variant<CFAEnum, Enumerator>> members; string name; };
     292inline static bool operator==(CFAEnum& lhs, CFAEnum& rhs) { return lhs.name == rhs.name; }
     293pair<bool, int> calculateEnumOffset(CFAEnum src, CFAEnum dst) {
     294        int offset = 0;
     295        if (src == dst) return make_pair(true, 0);
     296        for (auto v : dst.members) {
     297                if (holds_alternative<Enumerator>(v)) {
     298                        offset++;
     299                } else {
     300                        auto m = get<CFAEnum>(v);
     301                        if (m == src) return make_pair(true, offset);
     302                        auto dist = calculateEnumOffset(src, m);
     303                        if (dist.first) {
     304                                return make_pair(true, offset + dist.second);
     305                        } else {
     306                                offset += dist.second;
     307                        }
     308                }
     309        }
     310        return make_pair(false, offset);
     311}
     312\end{cfa}
     313\caption{Compute Offset from Subtype Enumeration to a Supertype}
     314\label{s:OffsetSubtypeSuperType}
     315\end{figure}
     316
     317Figure~\ref{s:OffsetSubtypeSuperType} shows a minimum of @offset calculation@, written in \CC. CFAEnum represents \CFA enumeration
     318which has a vector of variants of Enumerator or CFAEnum. Two CFAEnums are differentiable by their unquie name.
     319The algorithm takes two CFAEnums as parameters @src@ and @dst@, with @src@ being type of expression the conversion applies on,
     320and @dst@ being type that the expression cast into. The algorithm returns a pair of value: when @src@ is convertible to @dst@ (@src@ is a subtype of @dst@),
     321it returns boolean true and the offset. Otherwise, it returns false and the size of @src@ (total number of enumerators in @src@). The offset between a type and itself is defined
     322as 0.
     323
     324The algorithm iterates over members in @dst@ to find @src@. If a member is an enumerator of @dst@, the positions of all subsequent members
     325increment by one. If the current member is @dst@, the function returns true indicating "found" and the accumulated offset. Otherwise, the algorithm recurse
     326into the current CFAEnum @m@ and find out if the @src@ is convertible to @m@. The @src@ being convertible to the current member @m@ means @src@
     327is a "subtype of subtype" of @dst@. The offset between @src@ and @dst@ is the sum of the offset of @m@ in @dst@ and the offset of
     328@src@ in @m@. If @src@ is not a subtype of @m@, the loop continue but with offset shifted by the size of @m@. The procedure reaches the end
     329of the loop proves @src@ is not convertible to @dst@. It returns size of @src@ in case it is in a recurse innvocation and size is needed for offset update.
    315330
    316331\section{Control Structures}
  • doc/theses/jiada_liang_MMath/background.tex

    rd7cb0f7 r63d5b9c8  
    487487
    488488In \CFA, the meaning of a C style cast is determined by its @Cast Cost@. For most cast expression resolution, a cast cost is equal to a conversion cost.
    489 Cast cost exists as an independent matrix for conversion that cannot happen implcitly, while being possible with an explicit cast. These conversions are often defined to have
     489Cast cost exists as an independent matrix for conversion that cannot happen implicitly, while being possible with an explicit cast. These conversions are often defined to have
    490490infinite conversion cost and non-infinite cast cost.
Note: See TracChangeset for help on using the changeset viewer.