Changes in / [502ff9e:c341b57]
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified doc/theses/mike_brooks_MMath/array.tex ¶
r502ff9e rc341b57 720 720 \newcommand{\falsealarm}{{\color{blue}\small{*}}} 721 721 \newcommand{\allowmisuse}{{\color{red}\textbf{!}}} 722 \newcommand{\cmark}{\ding{51}} % from pifont723 \newcommand{\xmark}{\ding{55}}724 722 725 723 \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 7 7 \section{String Operations} 8 8 9 To prepare for the following discussion, a simple comparison among C, \CC, and \CFA basic string operation is presented. 9 To prepare for the following discussion, comparisons among C, \CC, Java and \CFA strings are presented, beginning in \VRef[Figure]{f:StrApiCompare}. 10 It provides a classic ``cheat sheet'' presentation, summarizing the names of the typical operations. 11 12 \begin{figure} 10 13 \begin{cquote} 11 \begin{tabular}{@{}l|l|l @{}}12 C @char [ ]@ & \CC @string@ & \CFA @string@ \\14 \begin{tabular}{@{}l|l|l|l@{}} 15 C @char [ ]@ & \CC @string@ & Java @String@ & \CFA @string@ \\ 13 16 \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@ \\ 29 n/a & @c_str@, @data@ & n/a & @strcpy@, @strncpy@ \\ 25 30 \end{tabular} 26 31 \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 36 The key commonality is that operations work on groups of characters for assigning, copying, scanning, and updating. 28 37 Because 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. 29 38 Most high-level string libraries use a separate length field and specialized storage management to support group operations. … … 32 41 int open( const char * pathname, int flags ); 33 42 string fname{ "test.cc" ); 34 open( fname.@c_str()@ );43 open( fname.@c_str()@, O_RDONLY ); 35 44 \end{cfa} 36 45 The 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{ 37 46 C functions like \lstinline{strdup} do return allocated storage that must be freed by the caller.} 38 Instead, each \CC string is null terminat orjust in case it might be needed for this purpose.47 Instead, each \CC string is null terminated just in case it might be needed for this purpose. 39 48 Providing this backwards compatibility with C has a ubiquitous performance and storage cost. 49 50 While \VRef[Figure]{f:StrApiCompare} emphasizes cross-language similarities, it elides differences in how a certain function is used. 51 For 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. 52 Generally, Java's @String@ type is immutable. 53 Java 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 55 These 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 62 Type 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 78 Symmetry 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 83 Referent 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 90 Notes 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 105 In C: 106 \begin{cfa} 107 char * s1 = ...; // assumed 108 char * s2 = s1; // alias state, variable-constrained referent 109 char * s3 = s1 + 2; // alias state, variable-constrained referent 110 \end{cfa} 111 The issue of symmetry is trivial for a low-level type, and so, scored as not applicable to C. 112 With 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. 113 While @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 115 In \CC: 116 \begin{cfa} 117 string s1 = ...; // assumed 118 string & s2 = s1; // alias state, lax symmetry, variable-constrained referent 119 string s3 = s1; // NOT fast-initialize (strict symmetry, variable-constrained referent) 120 string s4 = s1.substr(2,4); // NOT fast-initialize (strict symmetry, fragment referent) 121 string & s5 = s1.substr(2,4); // memory-use error 122 \end{cfa} 123 The lax symmetry of the @s2@ technique reflects how the validity of @s2@ depends on the lifetime of @s1@. 124 It 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. 125 So, 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. 126 Exceptions of this pattern are possible, of course, but they represent the programmer taking responsiblity to assure safety where the type system does not. 127 The @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. 128 TODO: address caveat that @s3@ could be done fast by reference counting in the text area. 129 130 131 In Java: 132 \begin{cfa} 133 String s1 = ...; // assumed 134 String s2 = s1; // snapshot state, strict symmetry, variable-constrained referent 135 String s3 = s1.substring(2,4); // snapshot state (possible), strict symmetry, fragment referent 136 \end{cfa} 137 Here, facts about Java's implicit pointers and pointer equality can overcomplicate the picture. 138 The further fact of Java's string immutability means that string variables behave as simple values. 139 The result in @s2@ is the value of @s1@, and their pointer equality certainly assures that no time is spent copying characters. 140 With @s3@, the case for fast-copy is more subtle. 141 Certainly, its value is not pointer-equal to @s1@, implying at least a further allocation. 142 TODO: finish the fast-copy case. 143 Java strings lacking mutation means that aliasing is not possible with the @String@ type. 144 Java's @StringBuffer@ provides aliasing, though without supporting symmetric treatment of a fragment referent; as a result, @StringBuffer@ scores as \CC. 145 The 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 147 Finally, in \CFA, 148 \begin{cfa} 149 string s1 = ...; // assumed 150 string s2 = s1; // snapshot state, strict symmetry, variable-constrained referent 151 string s3 = s1`shareEdits; // alias state, strict symmetry, variable-constrained referent 152 string s4 = s1(2,4); // snapshot state, strict symmetry, fragment referent 153 string s5 = s1(2,4)`shareEdits; // alias state, strict symmetry, fragment referent 154 \end{cfa} 155 all helpful criteria of \VRef[Figure]{f:StrSemanticCompare} are satisfied. 156 The \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). 157 With aliasing, the intuition is that each string is an editor on an open shared document. 158 With fragment aliasing, the intuition is that these editor views have been scolled or zoomed to overlapping, but potentially different, ranges. 159 160 The remainder of this chapter explains how the \CFA string achieves this usage style. 161 40 162 41 163 -
TabularUnified doc/theses/mike_brooks_MMath/uw-ethesis.tex ¶
r502ff9e rc341b57 88 88 \usepackage{tabularx} 89 89 \usepackage{pifont} 90 \newcommand{\cmark}{\ding{51}} 91 \newcommand{\xmark}{\ding{55}} 90 92 \usepackage[labelformat=simple,aboveskip=0pt,farskip=0pt,font=normalsize]{subfig} 93 \usepackage{multirow} 91 94 \renewcommand\thesubfigure{(\alph{subfigure})} 92 95 -
TabularUnified src/AST/Expr.cpp ¶
r502ff9e rc341b57 301 301 // --- OffsetofExpr 302 302 303 OffsetofExpr::OffsetofExpr( const CodeLocation & loc, const Type * ty, const DeclWithType * mem )304 : OffsetofExpr( loc, ty, mem, nullptr ) {}305 306 303 OffsetofExpr::OffsetofExpr( const CodeLocation & loc, const Type * ty, const DeclWithType * mem, const Type * res ) 307 304 : Expr( loc, res ), type( ty ), member( mem ) { … … 324 321 325 322 // --- CommaExpr 323 326 324 bool CommaExpr::get_lvalue() const { 327 325 // This is wrong by C, but the current implementation uses it. -
TabularUnified src/AST/Expr.hpp ¶
r502ff9e rc341b57 539 539 readonly<DeclWithType> member; 540 540 541 OffsetofExpr( const CodeLocation & loc, const Type * ty, const DeclWithType * mem );542 541 OffsetofExpr( const CodeLocation & loc, const Type * ty, const DeclWithType * mem, const Type * res ); 543 542 -
TabularUnified src/GenPoly/Box.cpp ¶
r502ff9e rc341b57 2002 2002 new ast::OffsetofExpr( expr->location, 2003 2003 deepCopy( type ), 2004 memberDecl 2004 memberDecl, 2005 getLayoutType( transUnit() ) 2005 2006 ) 2006 2007 ) ); -
TabularUnified src/ResolvExpr/CandidateFinder.cpp ¶
r502ff9e rc341b57 2150 2150 } 2151 2151 2152 const ast::Expr * CandidateFinder::makeEnumOffsetCast( const ast::EnumInstType * src, 2153 const ast::EnumInstType * dst, const ast::Expr * expr, Cost minCost ) {2152 const ast::Expr * CandidateFinder::makeEnumOffsetCast( const ast::EnumInstType * src, 2153 const ast::EnumInstType * dst, const ast::Expr * expr, Cost minCost ) { 2154 2154 auto srcDecl = src->base; 2155 2155 auto dstDecl = dst->base; 2156 2156 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 ) { 2160 2160 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 ); 2185 2180 } 2186 2181 } -
TabularUnified src/ResolvExpr/ConversionCost.cpp ¶
r502ff9e rc341b57 518 518 auto srcDecl = src->base; 519 519 auto dstDecl = dst->base; 520 if ( srcDecl->name == dstDecl->name) return Cost::safe;520 if ( srcDecl->name == dstDecl->name ) return Cost::safe; 521 521 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; 525 525 } 526 526 return minCost; -
TabularUnified src/ResolvExpr/Cost.hpp ¶
r502ff9e rc341b57 169 169 } 170 170 171 inline bool operator<=( const Cost lhs, const Cost rhs ) { 172 return !(rhs < lhs); 173 } 174 171 175 inline Cost operator+( const Cost lhs, const Cost rhs ) { 172 176 if ( lhs.all == Cost::infinity || rhs.all == Cost::infinity ) return Cost{ Cost::infinity }; -
TabularUnified src/Validate/EnumAndPointerDecay.cpp ¶
r502ff9e rc341b57 48 48 } else if ( auto value = member.as<ast::InlineMemberDecl>() ) { 49 49 auto targetEnum = symtab.lookupEnum( value->name ); 50 // assert( targetEnum );51 50 if (!targetEnum) { 52 51 SemanticError(value, "Only another enum is allowed for enum inline syntax "); 53 52 } 54 53 const ast::EnumInstType * instType = new ast::EnumInstType(targetEnum); 55 mut->inlinedDecl. push_back( std::move(instType));54 mut->inlinedDecl.emplace_back( instType ); 56 55 for ( auto enumMember : targetEnum->members ) { 57 56 auto enumObject = enumMember.strict_as<ast::ObjectDecl>();
Note: See TracChangeset
for help on using the changeset viewer.