Changes in / [502ff9e:c341b57]


Ignore:
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified doc/theses/mike_brooks_MMath/array.tex

    r502ff9e rc341b57  
    720720        \newcommand{\falsealarm}{{\color{blue}\small{*}}}
    721721        \newcommand{\allowmisuse}{{\color{red}\textbf{!}}}
    722         \newcommand{\cmark}{\ding{51}} % from pifont
    723         \newcommand{\xmark}{\ding{55}}
    724722
    725723        \begin{tabular}{@{}l@{\hspace{16pt}}c@{\hspace{8pt}}c@{\hspace{16pt}}c@{\hspace{8pt}}c@{\hspace{16pt}}c}
  • TabularUnified doc/theses/mike_brooks_MMath/string.tex

    r502ff9e rc341b57  
    77\section{String Operations}
    88
    9 To prepare for the following discussion, a simple comparison among C, \CC, and \CFA basic string operation is presented.
     9To prepare for the following discussion, comparisons among C, \CC, Java and \CFA strings are presented, beginning in \VRef[Figure]{f:StrApiCompare}.
     10It provides a classic ``cheat sheet'' presentation, summarizing the names of the typical operations.
     11
     12\begin{figure}
    1013\begin{cquote}
    11 \begin{tabular}{@{}l|l|l@{}}
    12 C @char [ ]@                    &  \CC @string@                 & \CFA @string@ \\
     14\begin{tabular}{@{}l|l|l|l@{}}
     15C @char [ ]@                    &  \CC @string@                 & Java @String@     & \CFA @string@     \\
    1316\hline
    14 @strcpy@, @strncpy@             & @=@                                   & @=@   \\
    15 @strcat@, @strncat@             & @+@                                   & @+@   \\
    16 @strcmp@, @strncmp@             & @==@, @!=@, @<@, @<=@, @>@, @>=@ & @==@, @!=@, @<@, @<=@, @>@, @>=@ \\
    17 @strlen@                                & @length@                              & @size@        \\
    18 @[ ]@                                   & @[ ]@                                 & @[ ]@ \\
    19                                                 & @substr@                              & @substr@      \\
    20                                                 & @replace@                             & @=@ \emph{(on a substring)}\\
    21 @strstr@                                & @find@, @rfind@               & @find@, MISSING \\
    22 @strcspn@                               & @find_first_of@, @find_last_of@ & @include@, MISSING \\
    23 @strspn@                                & @find_first_not_of@, @find_last_not_of@ & @exclude@, MISSING \\
    24                                                 & @c_str@                               & MISSING \\
     17@strcpy@, @strncpy@             & @=@                                   & @=@               & @=@       \\
     18@strcat@, @strncat@             & @+@                                   & @+@               & @+@       \\
     19@strcmp@, @strncmp@             & @==@, @!=@, @<@, @<=@, @>@, @>=@
     20                                                & @equals@, @compareTo@
     21                                                                                                                    & @==@, @!=@, @<@, @<=@, @>@, @>=@ \\
     22@strlen@                                & @length@, @size@              & @length@                      & @size@        \\
     23@[ ]@                                   & @[ ]@                                 & @charAt@          & @[ ]@     \\
     24@strncpy@                               & @substr@                              & @substring@       & @( )@     \\
     25@strncpy@                               & @replace@                             & @replace@         & @=@ \emph{(on a substring)}\\
     26@strstr@                                & @find@                                & @indexOf@         & @find@ \\
     27@strcspn@                               & @find_first_of@               & @matches@         & @include@ \\
     28@strspn@                                & @find_first_not_of@   & @matches@         & @exclude@ \\
     29n/a                                             & @c_str@, @data@               & n/a               & @strcpy@, @strncpy@ \\
    2530\end{tabular}
    2631\end{cquote}
    27 The key commonality is that operations work on groups of characters for assigning. copying, scanning, and updating.
     32\caption{Comparison of languages' strings, API/``cheat-sheet'' perspective.}
     33\label{f:StrApiCompare}
     34\end{figure}
     35
     36The key commonality is that operations work on groups of characters for assigning, copying, scanning, and updating.
    2837Because a C string is null terminated and requires explicit storage management \see{\VRef{s:String}}, most of its group operations are error prone and expensive.
    2938Most high-level string libraries use a separate length field and specialized storage management to support group operations.
     
    3241int open( const char * pathname, int flags );
    3342string fname{ "test.cc" );
    34 open( fname.@c_str()@ );
     43open( fname.@c_str()@, O_RDONLY );
    3544\end{cfa}
    3645The function @c_str@ does not create a new null-terminated C string from the \CC string, as that requires passing ownership of the C string to the caller for eventual deletion.\footnote{
    3746C functions like \lstinline{strdup} do return allocated storage that must be freed by the caller.}
    38 Instead, each \CC string is null terminator just in case it might be needed for this purpose.
     47Instead, each \CC string is null terminated just in case it might be needed for this purpose.
    3948Providing this backwards compatibility with C has a ubiquitous performance and storage cost.
     49
     50While \VRef[Figure]{f:StrApiCompare} emphasizes cross-language similarities, it elides differences in how a certain function is used.
     51For example, the @replace@ function in \CC performs a modification on its @this@ parameter, while the Java one allocates and returns a new string with the result, leaving @this@ unmodified.
     52Generally, Java's @String@ type is immutable.
     53Java provides a @StringBuffer@ near-analog that is mutable, but the differences are significant; for example, this class's @substring@ functions still return copies rather than mutable selections.
     54
     55These more significant differences are summarized in \VRef[Figure]{f:StrSemanticCompare}.  It calls out the consequences of each language taking a different approach on the ``internal'' issues, like storage management and null-terminator interoperability.  The discussion following justifies the figure's yes/no entries.
     56
     57\begin{figure}
     58\begin{tabular}{@{}p{0.5in}p{2in}p{2in}>{\centering\arraybackslash}p{0.2in}>{\centering\arraybackslash}>{\centering\arraybackslash}p{0.2in}>{\centering\arraybackslash}p{0.2in}>{\centering\arraybackslash}p{0.2in}@{}}
     59                                        &                       &                       & \multicolumn{4}{c}{\underline{Supports Helpful?}} \\
     60                                        & Required      & Helpful       & C                     & \CC           & Java          & \CFA \\
     61\hline
     62Type abst'n
     63                                        & Low-level: A ``string'' type represents a varying amount of text that is communicated with a function as a parameter/return.
     64                                                                & High-level: Using a string-typed variable relieves the user of managing a distinct allocation for the text.
     65                                                                                        & \xmark        & \cmark        & \cmark        & \cmark \\
     66\hline
     67\multirow{2}{0.5in}
     68{State}
     69                                        & \multirow{2}{2in}
     70                                        {Fast Initialize: The target receives the characters of the original, but no time is spent copying characters.  The result is one of Alias or Snapshot.}
     71                                                                & Alias: The target is a further name for the text in the original; changes made in either variable are visible in both.
     72                                                                                        & \cmark        & \cmark        & \xmark        & \cmark \\
     73\cline{3-7}
     74                                        &
     75                                                                & Snapshot: The target (resp.\ source) contains the value of the source at the time of the initialization until the target (resp.\ source) is explicitly changed.
     76                                                                                        & \xmark        & \xmark        & \cmark        & \cmark \\
     77\hline
     78Symmetry
     79                                        & Laxed: The target’s type is anything string-like; it may have a different status concerning ownership.
     80                                                                & Strict: The target’s type is the same as the original; both strings are equivalent peers concerning ownership.
     81                                                                                        & --            & \xmark        & \cmark        & \cmark \\
     82\hline
     83Referent
     84                                        & Variable-Constrained: The target can accept the entire text of the original.
     85                                                                & Fragment: The target can accept an arbitrary substring of the original.
     86                                                                                        & \xmark        & \xmark        & \cmark        & \cmark
     87\end{tabular}
     88
     89\noindent
     90Notes
     91\begin{itemize}
     92\item
     93        All languages support Required in all criteria.
     94\item
     95        A language gets ``Supports Helpful'' in one criterion if it can do so without sacrificing the Required achievement on all other criteria.
     96\item
     97        The C ``string'' is @char *@, under the conventions that @<string.h>@ requires.  Symmetry is not applicable to C.
     98\item
     99        The Java @String@ class is analyzed; its @StringBuffer@ class behaves similarly to @C++@.
     100\end{itemize}
     101\caption{Comparison of languages' strings, assignment-semantics perspective.}
     102\label{f:StrSemanticCompare}
     103\end{figure}
     104
     105In C:
     106\begin{cfa}
     107char * s1 = ...; // assumed
     108char * s2 = s1;  // alias state, variable-constrained referent
     109char * s3 = s1 + 2;  // alias state, variable-constrained referent
     110\end{cfa}
     111The issue of symmetry is trivial for a low-level type, and so, scored as not applicable to C.
     112With the type not managing the text buffer, there is no ownership question, \ie nothing done with the @s1@ or @s2@ variables leads to the memory that their text currently occupies becoming reusable.
     113While @s3@ is a valid C-string that contains a proper substring of @s1@, the @s3@ technique does not constitue having a fragment referent because null termination implies the substring cannot be chosen arbitrarily; the technique works only for suffixes.
     114
     115In \CC:
     116\begin{cfa}
     117string s1 = ...; // assumed
     118string & s2 = s1;  // alias state, lax symmetry, variable-constrained referent
     119string s3 = s1;  // NOT fast-initialize (strict symmetry, variable-constrained referent)
     120string s4 = s1.substr(2,4);  // NOT fast-initialize (strict symmetry, fragment referent)
     121string & s5 = s1.substr(2,4);  // memory-use error
     122\end{cfa}
     123The lax symmetry of the @s2@ technique reflects how the validity of @s2@ depends on the lifetime of @s1@.
     124It is common practice in \CC to use the @s2@ technique for parameter passing, but the safest-bet advice has to be that the callee can only use the referenced string for the duration of the call.
     125So, when the called function is a constructor, it is typical that the implementation is doing an @s3@-style initialization of a string-object-typed member.
     126Exceptions of this pattern are possible, of course, but they represent the programmer taking responsiblity to assure safety where the type system does not.
     127The @s4@ initialization is constrained by specification to copy the substring because of @c_str@ being specified to be a null-terminated character run that is not its own allocation.
     128TODO: address caveat that @s3@ could be done fast by reference counting in the text area.
     129
     130
     131In Java:
     132\begin{cfa}
     133String s1 = ...;  // assumed
     134String s2 = s1;  // snapshot state, strict symmetry, variable-constrained referent
     135String s3 = s1.substring(2,4);  // snapshot state (possible), strict symmetry, fragment referent
     136\end{cfa}
     137Here, facts about Java's implicit pointers and pointer equality can overcomplicate the picture.
     138The further fact of Java's string immutability means that string variables behave as simple values.
     139The result in @s2@ is the value of @s1@, and their pointer equality certainly assures that no time is spent copying characters.
     140With @s3@, the case for fast-copy is more subtle.
     141Certainly, its value is not pointer-equal to @s1@, implying at least a further allocation.
     142TODO: finish the fast-copy case.
     143Java strings lacking mutation means that aliasing is not possible with the @String@ type.
     144Java's @StringBuffer@ provides aliasing, though without supporting symmetric treatment of a fragment referent; as a result, @StringBuffer@ scores as \CC.
     145The easy symmetry that the Java string enjoys is aided by Java's garbage collection; Java's @s2@ is doing effectively the operation of \CC's @s2@, though without the consequence to of complicating memory management.
     146
     147Finally, in \CFA,
     148\begin{cfa}
     149string s1 = ...; // assumed
     150string s2 = s1; // snapshot state, strict symmetry, variable-constrained referent
     151string s3 = s1`shareEdits; // alias state, strict symmetry, variable-constrained referent
     152string s4 = s1(2,4); // snapshot state, strict symmetry, fragment referent
     153string s5 = s1(2,4)`shareEdits; // alias state, strict symmetry, fragment referent
     154\end{cfa}
     155all helpful criteria of \VRef[Figure]{f:StrSemanticCompare} are satisfied.
     156The \CFA string manages storage, handles all assignments, including those of fragment referents, with fast initialization, provides the choice between snapshot and alias semantics, does so symmetrically with one type (which assures text validity according to the lifecycles of the string variables).
     157With aliasing, the intuition is that each string is an editor on an open shared document.
     158With fragment aliasing, the intuition is that these editor views have been scolled or zoomed to overlapping, but potentially different, ranges.
     159
     160The remainder of this chapter explains how the \CFA string achieves this usage style.
     161
    40162
    41163
  • TabularUnified doc/theses/mike_brooks_MMath/uw-ethesis.tex

    r502ff9e rc341b57  
    8888\usepackage{tabularx}
    8989\usepackage{pifont}
     90\newcommand{\cmark}{\ding{51}}
     91\newcommand{\xmark}{\ding{55}}
    9092\usepackage[labelformat=simple,aboveskip=0pt,farskip=0pt,font=normalsize]{subfig}
     93\usepackage{multirow}
    9194\renewcommand\thesubfigure{(\alph{subfigure})}
    9295
  • TabularUnified src/AST/Expr.cpp

    r502ff9e rc341b57  
    301301// --- OffsetofExpr
    302302
    303 OffsetofExpr::OffsetofExpr( const CodeLocation & loc, const Type * ty, const DeclWithType * mem )
    304 : OffsetofExpr( loc, ty, mem, nullptr ) {}
    305 
    306303OffsetofExpr::OffsetofExpr( const CodeLocation & loc, const Type * ty, const DeclWithType * mem, const Type * res )
    307304: Expr( loc, res ), type( ty ), member( mem ) {
     
    324321
    325322// --- CommaExpr
     323
    326324bool CommaExpr::get_lvalue() const {
    327325        // This is wrong by C, but the current implementation uses it.
  • TabularUnified src/AST/Expr.hpp

    r502ff9e rc341b57  
    539539        readonly<DeclWithType> member;
    540540
    541         OffsetofExpr( const CodeLocation & loc, const Type * ty, const DeclWithType * mem );
    542541        OffsetofExpr( const CodeLocation & loc, const Type * ty, const DeclWithType * mem, const Type * res );
    543542
  • TabularUnified src/GenPoly/Box.cpp

    r502ff9e rc341b57  
    20022002                        new ast::OffsetofExpr( expr->location,
    20032003                                deepCopy( type ),
    2004                                 memberDecl
     2004                                memberDecl,
     2005                                getLayoutType( transUnit() )
    20052006                        )
    20062007                ) );
  • TabularUnified src/ResolvExpr/CandidateFinder.cpp

    r502ff9e rc341b57  
    21502150}
    21512151
    2152 const ast::Expr * CandidateFinder::makeEnumOffsetCast( const ast::EnumInstType * src, 
    2153         const ast::EnumInstType * dst, const ast::Expr * expr, Cost minCost ) {
     2152const ast::Expr * CandidateFinder::makeEnumOffsetCast( const ast::EnumInstType * src,
     2153                const ast::EnumInstType * dst, const ast::Expr * expr, Cost minCost ) {
    21542154        auto srcDecl = src->base;
    21552155        auto dstDecl = dst->base;
    21562156
    2157         if (srcDecl->name == dstDecl->name) return expr;
    2158 
    2159         for (auto& dstChild: dstDecl->inlinedDecl) {
     2157        if ( srcDecl->name == dstDecl->name ) return expr;
     2158
     2159        for ( auto& dstChild : dstDecl->inlinedDecl ) {
    21602160                Cost c = castCost(src, dstChild, false, context.symtab, env);
    2161                 ast::CastExpr * castToDst;
    2162                 if (c<minCost) {
    2163                         unsigned offset = findChildOffset( dstDecl, dstChild.get()->base );
    2164                         if (offset > 0) {
    2165                                 auto untyped = ast::UntypedExpr::createCall(
    2166                                         expr->location,
    2167                                         "?+?",
    2168                                         { new ast::CastExpr( expr->location,
    2169                                                 expr,
    2170                                                 new ast::BasicType(ast::BasicKind::SignedInt),
    2171                                                 ast::GeneratedFlag::ExplicitCast ),
    2172                                                 ast::ConstantExpr::from_int(expr->location, offset)} );
    2173                                 CandidateFinder finder(context, env);
    2174                                 finder.find( untyped );
    2175                                 CandidateList winners = findMinCost( finder.candidates );
    2176                                 CandidateRef & choice = winners.front();
    2177                                 choice->expr = new ast::CastExpr(expr->location, choice->expr, dstChild, ast::GeneratedFlag::ExplicitCast);
    2178                                 auto destExpr = makeEnumOffsetCast( src, dstChild, choice->expr, minCost );
    2179                                 if ( !destExpr ) continue;
    2180                                 castToDst = new ast::CastExpr( destExpr, dst );
    2181                         } else {
    2182                                 castToDst = new ast::CastExpr( expr, dst );
    2183                         }
    2184                         return castToDst;
     2161                if ( minCost <= c ) continue;
     2162                unsigned offset = findChildOffset( dstDecl, dstChild.get()->base );
     2163                if ( offset <= 0 ) return new ast::CastExpr( expr, dst );
     2164
     2165                auto untyped = ast::UntypedExpr::createCall(
     2166                        expr->location,
     2167                        "?+?",
     2168                        { new ast::CastExpr( expr->location,
     2169                                expr,
     2170                                new ast::BasicType( ast::BasicKind::SignedInt ),
     2171                                ast::GeneratedFlag::ExplicitCast ),
     2172                                ast::ConstantExpr::from_int( expr->location, offset ) } );
     2173                CandidateFinder finder(context, env);
     2174                finder.find( untyped );
     2175                CandidateList winners = findMinCost( finder.candidates );
     2176                CandidateRef & choice = winners.front();
     2177                choice->expr = new ast::CastExpr( expr->location, choice->expr, dstChild, ast::GeneratedFlag::ExplicitCast );
     2178                if ( auto destExpr = makeEnumOffsetCast( src, dstChild, choice->expr, minCost ) ) {
     2179                        return new ast::CastExpr( destExpr, dst );
    21852180                }
    21862181        }
  • TabularUnified src/ResolvExpr/ConversionCost.cpp

    r502ff9e rc341b57  
    518518        auto srcDecl = src->base;
    519519        auto dstDecl = dst->base;
    520         if (srcDecl->name == dstDecl->name) return Cost::safe;
     520        if ( srcDecl->name == dstDecl->name ) return Cost::safe;
    521521        Cost minCost = Cost::infinity;
    522         for (auto child: dstDecl->inlinedDecl) {
    523                 Cost c = enumCastCost(src, child, symtab, env) + Cost::safe;
    524                 if (c<minCost) minCost = c;
     522        for ( auto child : dstDecl->inlinedDecl ) {
     523                Cost c = enumCastCost( src, child, symtab, env ) + Cost::safe;
     524                if ( c < minCost ) minCost = c;
    525525        }
    526526        return minCost;
  • TabularUnified src/ResolvExpr/Cost.hpp

    r502ff9e rc341b57  
    169169}
    170170
     171inline bool operator<=( const Cost lhs, const Cost rhs ) {
     172        return !(rhs < lhs);
     173}
     174
    171175inline Cost operator+( const Cost lhs, const Cost rhs ) {
    172176        if ( lhs.all == Cost::infinity || rhs.all == Cost::infinity ) return Cost{ Cost::infinity };
  • TabularUnified src/Validate/EnumAndPointerDecay.cpp

    r502ff9e rc341b57  
    4848                } else if ( auto value = member.as<ast::InlineMemberDecl>() ) {
    4949                        auto targetEnum = symtab.lookupEnum( value->name );
    50                         // assert( targetEnum );
    5150                        if (!targetEnum) {
    5251                                SemanticError(value, "Only another enum is allowed for enum inline syntax ");
    5352                        }
    5453                        const ast::EnumInstType * instType = new ast::EnumInstType(targetEnum);
    55                         mut->inlinedDecl.push_back( std::move(instType) );
     54                        mut->inlinedDecl.emplace_back( instType );
    5655                        for ( auto enumMember : targetEnum->members ) {
    5756                                auto enumObject = enumMember.strict_as<ast::ObjectDecl>();
Note: See TracChangeset for help on using the changeset viewer.