Changeset 9c12dd0 for doc/theses/mike_brooks_MMath/array.tex
- Timestamp:
- Apr 27, 2026, 7:17:34 PM (33 hours ago)
- Branches:
- master
- Children:
- f1ffc47
- Parents:
- 602cec4
- File:
-
- 1 edited
-
doc/theses/mike_brooks_MMath/array.tex (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/mike_brooks_MMath/array.tex
r602cec4 r9c12dd0 30 30 31 31 A function can be polymorphic over @array@ arguments using the \CFA @forall@ declaration prefix. 32 \begin{minipage}{\linewidth} % forbid page splitting, force 32 33 \begin{cfa} 33 34 forall( T, @[N]@ ) … … 40 41 Cforall Runtime error: subscript 1000 exceeds dimension range [0,99) $for$ array 0x555555558020. 41 42 \end{cfa} 43 \end{minipage} 42 44 Function @g@ takes an arbitrary type parameter @T@ and an unsigned integer \emph{dimension} @N@. 43 45 The dimension represents a to-be-determined number of elements, managed by the type system, where 0 represents an empty array. … … 125 127 \lstinput{30-43}{hello-array.cfa} 126 128 \lstinput{45-48}{hello-array.cfa} 127 \caption{Example of calling a dimension-generic function .}129 \caption{Example of calling a dimension-generic function} 128 130 \label{f:fExample} 129 131 \end{figure} … … 243 245 This static check's rules are presented in \VRef[Section]{s:ArrayTypingC}. 244 246 245 Orthogonally, the \CFA array type works within generic \emph{types}, \ie @forall@-on-@struct@. 246 The same argument safety and the associated implicit communication of array length occurs. 247 Orthogonally, the \CFA array type also works inside generic \emph{types}. 248 That is, an @array@ as a generic @struct@'s member and an @[N]@ can parameterize a @struct@. 249 The same argument safety, and the associated implicit length communication, occur. 247 250 Preexisting \CFA allowed aggregate types to be generalized with type parameters, enabling parameterizing of element types. 248 This feature is extended to allow parameterizing by dimension. 249 Doing so gives a refinement of C's ``flexible array member''~\cite[\S~6.7.2.1.18]{C11}: 251 As polymorphism on functions was extended to allow parameterizing over dimensions, so is polymorphism on types. 252 The result has profound consequences for safe memory management. 253 254 C's Flexible Array Member (FAM) pattern~\cite[\S~6.7.2.1.18]{C11} gives an unsafe way to put an array of dynamic length inside a structure. 250 255 \begin{cfa} 251 256 struct S { 252 257 ... 253 double d []; // incomplete array type => flexible array member 254 } * s = malloc( sizeof( struct S ) + sizeof( double [10] ) ); 255 \end{cfa} 256 which creates a VLA of size 10 @double@s at the end of the structure. 257 A C flexible array member can only occur at the end of a structure; 258 \CFA allows length-parameterized array members to be nested at arbitrary locations, with intervening member declarations. 259 \lstinput{10-15}{hello-accordion.cfa} 260 The structure has course- and student-level metadata (their respective field names) and a position-based preferences' matrix. 261 The structure's layout is dynamic; the starting offset of @student_ids@ varies according to the generic parameter @C@; the offset of @preferences@ varies by both. 262 263 \VRef[Figure]{f:checkExample} shows a program main using @School@ and results with different array sizes. 258 double d[]; // incomplete array type => flexible array member 259 }; 260 struct S * s = malloc( sizeof( struct S ) + sizeof( double [10] ) ); 261 \end{cfa} 262 This type-@S@ declaration defines a quasi-VLA named @d@ as the last structure member. 263 The variable-@s@ initialization sizes this particular array at 10 elements. 264 The choice of @malloc@ here is the common-case FAM idiom, but @alloca@ can be substituted, resulting in stack allocation. 265 A \CFA @[N]@-parameterized structure improves on this pattern. 266 267 \begin{figure} 268 \lstinput{10-15}{hello-accordion.cfa} 269 \caption{\CFA Dynamic Array Member (DAM) declaration} 270 \label{DAM-declaration} 271 \end{figure} 272 273 \CFA allows length-parameterized array members to be nested at arbitrary locations, with further member declarations interleaved. 274 The result is a \newterm{Dynamic Array Member (DAM)} 275 % \footnote{ 276 % To keep the names straight, it may help to think of C's flexible array member as ``too flexible,'' where the discussion that follows makes the case that a FAM is weakly typed. 277 % }, 278 as illustrated in \VRef[Figure]{DAM-declaration}. The example's @School@ structure has course- and student-level metadata (in the respectively named fields) and a position-based preferences' matrix. 279 The structure's layout is dynamic; the starting offset of @student_ids@ varies according to the generic parameter @C@, which affects the size of the @course_codes@ field. 280 Similarly, the offset of @preferences@ varies by both parameters. 281 282 \VRef[Figure]{f:DAM-example} shows a program main using the @School@ type, along with results at different array sizes. 264 283 The @school@ variable holds many students' course-preference forms. 265 284 It is on the stack and its initialization does not use any casting or size arithmetic. 266 Both of these points are impossible with a C flexible array member. 267 When heap allocation is preferred, the original pattern still applies. 285 When heap allocation is preferred, the original pattern still applies, with the same qualities. 268 286 \begin{cfa} 269 287 School( classes, students ) * sp = alloc(); 270 288 \end{cfa} 271 This ability to avoid casting and size arithmetic improves safety and usability over C flexible array members.272 289 The example program prints the courses in each student's preferred order, all using the looked-up display names. 273 When a function operates on a @School@ structure, the type system handles its memory layout transparently.274 In the example, function @getPref@ returns, for the student at position @is@, what is the position of their @pref@\textsuperscript{th}-favoured class.275 Finally, inputs and outputs are given on the right for different sized schools.290 In the example, function @getPref@ returns, for the student at position @is@, the position of their @pref@\textsuperscript{th}-favoured class. 291 This function helps the program to output each student's ordered preferences. 292 The program's inputs and outputs are given on the right, for two different school sizes. 276 293 277 294 \begin{figure} … … 304 321 \end{tabular} 305 322 306 \caption{\lstinline{School} Example, Input and Output} 307 \label{f:checkExample} 323 324 \caption[\CFA Dynamic Array Member (DAM) usage]{ 325 \CFA Dynamic Array Member (DAM) usage. 326 Example program (left) with input and output (right).} 327 \label{f:DAM-example} 308 328 \end{figure} 329 330 Much of these \CFA DAM features are impossible with a C FAM. 331 In C, they can only occur at the end of a structure; 332 \CFA allows them anywhere. 333 As a consequence, \CFA allows several of them, while C is limited to one. 334 The \CFA ability to avoid casting and size arithmetic improves safety and usability over C. 335 While C offers the \emph{VLA} language feature, to eliminate casting and size arithmetic for stack-allocated \emph{simple arrays}, this feature does not extend to stack-allocating a FAM-structure, which makes the user fall back on explicit @alloca@. 336 The \CFA DAM, by contrast, treats every stack allocation as a simple variable declaration. 337 When a function operates on a \CFA DAM structure, the type system handles its memory layout transparently. 338 309 339 310 340 … … 343 373 The ``resolver'' compiler pass, which provides argument values for a declaration's type-system parameters, gathered from type information in scope at the usage site. 344 374 \item 345 The ``box'' compiler pass, which encodes information about type parameters into ``extra'' regular parameters on declarations and andarguments on calls.375 The ``box'' compiler pass, which encodes information about type parameters into ``extra'' regular parameters on declarations, and corresponding arguments on calls. 346 376 Notably, it conveys the size of a type @foo@ as a @__sizeof_foo@ parameter, and rewrites the @sizeof(foo)@ expression as @__sizeof_foo@, \ie a use of the parameter. 347 377 \end{itemize} … … 690 720 \end{itemize} 691 721 692 \caption {Case comparison for array type compatibility, given pairs of dimension expressions.}722 \caption[Case comparison for array type compatibility]{Case comparison for array type compatibility, given pairs of dimension expressions.} 693 723 \label{f:DimexprRuleCompare} 694 724 \end{figure} … … 946 976 \begin{figure} 947 977 \includegraphics{measuring-like-layout} 948 \caption{Visualization of subscripting by value and by \lstinline[language=CFA]{all}, for \lstinline{x} of type \lstinline{array( float, 5, 7 )} understood as 5 rows by 7 columns. 949 The horizontal layout represents contiguous memory addresses while the vertical layout is conceptual. 950 The vertical shaded band highlights the location of the targeted element, 2.3. 951 Any such vertical slice contains various interpretations of a single address.} 978 \caption[Visualization of subscripting by value and by `all']{ 979 Visualization of subscripting by value and by `\lstinline[language=CFA]{all}.' 980 Uses example variable \lstinline{x} of type \lstinline{array( float, 5, 7 )}, understood as 5 rows by 7 columns. 981 The horizontal layout represents contiguous memory addresses while the vertical layout is conceptual. 982 The vertical shaded band highlights the location of the targeted element, 2.3. 983 Any such vertical slice contains various interpretations of a single address. 984 } 952 985 \label{fig:subscr-all} 953 986 \end{figure} … … 1007 1040 % \noindent END: Paste looking for a home 1008 1041 1009 The new-array library defines types and operations that ensure proper elements are accessed soundly in spite of the overlapping. 1010 The @arpk@ structure and its @-[i]@ operator are defined as: 1042 \begin{figure} 1011 1043 \begin{cfa} 1012 1044 forall( … … 1026 1058 } 1027 1059 \end{cfa} 1060 \caption{Definition of structure `\lstinline{arpk},' which underlies the \CFA array} 1061 \label{f:arpk} 1062 \end{figure} 1063 1064 The new-array library defines types and operations that ensure proper elements are accessed soundly in spite of the overlapping. 1065 The @arpk@ structure and its @-[i]@ operator are defined in \VRef[Figure]{f:arpk}. 1028 1066 The private @arpk@ structure (array with explicit packing) is generic over four types: dimension length, masquerading-as, ... 1029 1067 This structure's public interface is hidden behind the @array(...)@ macro and the subscript operator. … … 1101 1139 \multicolumn{1}{c}{(i)} 1102 1140 \end{tabular} 1103 \caption{Overhead comparison, control case. 1104 All three programs/compilers generate equally performant code when the programmer ``self-checks'' bounds via a loop's condition. 1105 Yet both \CFA and \CC versions generate code that is slower and safer than C's when the programmer might overrun the bounds.} 1141 \caption[Bound-check overhead comparison, control case]{ 1142 Bound-check overhead comparison, control case. 1143 All three programs/compilers generate equally performant code when the programmer ``self-checks'' bounds via a loop's condition. 1144 Yet both \CFA and \CC versions generate code that is slower and safer than C's when the programmer might overrun the bounds. 1145 } 1106 1146 \label{f:ovhd-ctl} 1107 1147 \end{figure} … … 1344 1384 \end{cfa} 1345 1385 \end{tabular} 1346 \caption{Exponential thunk generation under the otype-recursion pattern. 1386 \caption[Exponential thunk generation under the otype-recursion pattern]{ 1387 Exponential thunk generation under the otype-recursion pattern. 1347 1388 Each time one type's function (\eg ctor) uses another type's function, the \CFA compiler generates a thunk, to capture the used function's dependencies, presented according to the using function's need. 1348 1389 So, each non-leaf line represents a generated thunk and every line represents a search request for the resolver to find a satisfying function.} … … 1640 1681 \hspace{6pt} 1641 1682 1642 \caption{Triangular Matrix}1683 \caption{Triangular matrix in Java and C} 1643 1684 \label{f:JavaVsCTriangularMatrix} 1644 1685 \end{figure} … … 1684 1725 1685 1726 Thus, these options do not offer an allocation with a dynamically given fixed size. 1686 And furthermore, they do not provide any improvement to the C flexible array member pattern, for making a dynamic amount of storage contiguous with its header, as do \CFA's accordions.1727 And furthermore, they do not provide any improvement to the C flexible array member pattern, for making a dynamic amount of storage contiguous with its header, as do \CFA's new dynamic array members. 1687 1728 1688 1729
Note:
See TracChangeset
for help on using the changeset viewer.