فهرست منبع

array challenge

Jeremy Siek 3 سال پیش
والد
کامیت
09ad56a3c5
2فایلهای تغییر یافته به همراه34 افزوده شده و 19 حذف شده
  1. 25 18
      book.tex
  2. 9 1
      defs.tex

+ 25 - 18
book.tex

@@ -13168,10 +13168,20 @@ an array:
 In the following subsections we provide hints regarding how to update
 In the following subsections we provide hints regarding how to update
 the passes to handle arrays.
 the passes to handle arrays.
 
 
+\subsection{Shrink}
+
+Translate calls to \racket{\code{vector-length}}\python{\code{len}}
+into a primitive specific to arrays, \racket{\code{vectorof-length}}\python{\code{array\_len}},
+when the argument's type is \CARRAYTY{T}.
+%
+\python{The type checker for \LangArray{} adds a \code{has\_type}
+  field to the argument of \code{len} for this purpose.}
+
+
 \subsection{Bounds Checking}
 \subsection{Bounds Checking}
 
 
 We recommend inserting a new pass named \code{check\_bounds} that
 We recommend inserting a new pass named \code{check\_bounds} that
-inserts code around each the \racket{\code{vector-ref} and \code{vector-set!}}
+inserts code around each \racket{\code{vector-ref} and \code{vector-set!}}
 \python{subscript} operation to ensure that the index is greater than or equal to zero
 \python{subscript} operation to ensure that the index is greater than or equal to zero
 and less than the array's length.
 and less than the array's length.
 
 
@@ -13209,16 +13219,13 @@ and less than the array's length.
 
 
 \subsection{Expose Allocation}
 \subsection{Expose Allocation}
 
 
-This pass should translate the \code{make-vector} operator into
-lower-level operations. In particular, the new AST node
-$\LP\key{AllocateArray}~\Exp~\Type\RP$ is analogous to the
-\code{Allocate} AST node for tuples.  It allocates an array of the
-length specified by the $\Exp$, but does not initialize the elements
-of the array. The $\Type$ argument must be $\LP\key{Vectorof}~T\RP$
-where $T$ is the element type for the array. Regarding the
-initialization of the array, we recommend generated a \code{while}
-loop that uses \code{vector-set!} to put the initializing value into
-every element of the array.
+This pass should translate array creation into lower-level
+operations. In particular, the new AST node \ALLOCARRAY{\Exp}{\Type}
+is analogous to the \code{Allocate} AST node for tuples.  The $\Type$
+argument must be \ARRAYTY{T} where $T$ is the element type for the
+array. The \code{AllocateArray} AST node allocates an array of the
+length specified by the $\Exp$ but does not initialize the elements of
+the array. Generate code in this pass to initialize the elements.
 
 
 \subsection{Remove Complex Operands}
 \subsection{Remove Complex Operands}
 
 
@@ -13238,16 +13245,16 @@ Generate instructions for \code{AllocateArray} similar to those for
 that the tag at the front of the array should instead use the
 that the tag at the front of the array should instead use the
 representation discussed in Section~\ref{sec:array-rep}.
 representation discussed in Section~\ref{sec:array-rep}.
 
 
-Regarding \code{vectorof-length}, extract the length from the tag
-according to the representation discussed in
+Regarding \racket{\code{vectorof-length}}\python{\code{array\_len}},
+extract the length from the tag according to the representation discussed in
 Section~\ref{sec:array-rep}.
 Section~\ref{sec:array-rep}.
 
 
-The instructions generated for \code{vectorof-ref} differ from those
-for \code{vector-ref} (Section~\ref{sec:select-instructions-gc}) in
+The instructions generated for accessing an element of an array differ
+from those for a tuple (Section~\ref{sec:select-instructions-gc}) in
 that the index is not a constant so the offset must be computed at
 that the index is not a constant so the offset must be computed at
-runtime. The same is true for \code{vectorof-set!}.  Also, the
-\code{vectorof-set!} may appear in an assignment and as a stand-alone
-statement, so make sure to handle both situations in this pass.
+runtime. Also, note that assignment to an array element may appear in
+as a stand-alone statement, so make sure to handle that situation in
+this pass.
 
 
 %% Finally, the instructions for \code{any-vectorof-length} should be
 %% Finally, the instructions for \code{any-vectorof-length} should be
 %% similar to those for \code{vectorof-length}, except that one must
 %% similar to those for \code{vectorof-length}, except that one must

+ 9 - 1
defs.tex

@@ -155,7 +155,9 @@
 \newcommand{\INTTY}{{\key{Integer}}}
 \newcommand{\INTTY}{{\key{Integer}}}
 \newcommand{\INTTYPE}{{\key{Integer}}}
 \newcommand{\INTTYPE}{{\key{Integer}}}
 \newcommand{\BOOLTY}{{\key{Boolean}}}
 \newcommand{\BOOLTY}{{\key{Boolean}}}
-\newcommand{\VECTY}[1]{{\LP\key{Vector}~#1\RP}}
+\newcommand{\VECTY}[1]{\LP\key{Vector}~#1\RP}
+\newcommand{\ARRAYTY}[1]{\LP\key{Vectorof}~#1\RP}
+\newcommand{\CARRAYTY}[1]{\LP\key{Vectorof}~#1\RP}
 \newcommand{\ANYTY}{{\key{Any}}}
 \newcommand{\ANYTY}{{\key{Any}}}
 \newcommand{\CANYTY}{{\key{Any}}}
 \newcommand{\CANYTY}{{\key{Any}}}
 \newcommand{\CPROGRAM}[2]{\LP\code{CProgram}~#1~#2\RP}
 \newcommand{\CPROGRAM}[2]{\LP\code{CProgram}~#1~#2\RP}
@@ -166,6 +168,8 @@
 \newcommand{\CCOLLECT}[1]{\LP\key{collect}~#1\RP}
 \newcommand{\CCOLLECT}[1]{\LP\key{collect}~#1\RP}
 \newcommand{\ALLOCATE}[2]{\LP\key{Allocate}~#1~#2\RP}
 \newcommand{\ALLOCATE}[2]{\LP\key{Allocate}~#1~#2\RP}
 \newcommand{\CALLOCATE}[2]{\LP\key{allocate}~#1~#2\RP}
 \newcommand{\CALLOCATE}[2]{\LP\key{allocate}~#1~#2\RP}
+\newcommand{\ALLOCARRAY}[2]{\LP\key{AllocateArray}~#1~#2\RP}
+\newcommand{\CALLOCARRAY}[2]{\LP\key{allocate\_array}~#1~#2\RP}
 \newcommand{\GLOBAL}[1]{\LP\key{Global}~#1\RP}
 \newcommand{\GLOBAL}[1]{\LP\key{Global}~#1\RP}
 \newcommand{\CGLOBAL}[1]{#1\key{(\%rip)}}
 \newcommand{\CGLOBAL}[1]{#1\key{(\%rip)}}
 \newcommand{\GLOBALVALUE}[1]{\LP\key{GlobalValue}~#1\RP}
 \newcommand{\GLOBALVALUE}[1]{\LP\key{GlobalValue}~#1\RP}
@@ -223,11 +227,15 @@
 \newcommand{\ANYTY}{{\key{AnyType}}}
 \newcommand{\ANYTY}{{\key{AnyType}}}
 \newcommand{\CANYTY}{{\key{Any}}}
 \newcommand{\CANYTY}{{\key{Any}}}
 \newcommand{\VECTY}[1]{{\key{TupleType}\LP\LS #1 \RS\RP}}
 \newcommand{\VECTY}[1]{{\key{TupleType}\LP\LS #1 \RS\RP}}
+\newcommand{\ARRAYTY}[1]{\key{ListType}\LP#1\RP}
+\newcommand{\CARRAYTY}[1]{\key{list}\LS#1\RS}
 \newcommand{\INTTYPE}{{\key{IntType}}}
 \newcommand{\INTTYPE}{{\key{IntType}}}
 \newcommand{\COLLECT}[1]{\key{Collect}\LP#1\RP}
 \newcommand{\COLLECT}[1]{\key{Collect}\LP#1\RP}
 \newcommand{\CCOLLECT}[1]{\key{collect}\LP#1\RP}
 \newcommand{\CCOLLECT}[1]{\key{collect}\LP#1\RP}
 \newcommand{\ALLOCATE}[2]{\key{Allocate}\LP#1,#2\RP}
 \newcommand{\ALLOCATE}[2]{\key{Allocate}\LP#1,#2\RP}
 \newcommand{\CALLOCATE}[2]{\key{allocate}\LP#1,#2\RP}
 \newcommand{\CALLOCATE}[2]{\key{allocate}\LP#1,#2\RP}
+\newcommand{\ALLOCARRAY}[2]{\key{AllocateArrow}\LP#1,#2\RP}
+\newcommand{\CALLOCARRAY}[2]{\key{allocate\_arrray}\LP#1,#2\RP}
 \newcommand{\GLOBALVALUE}[1]{\key{GlobalValue}\LP#1\RP}
 \newcommand{\GLOBALVALUE}[1]{\key{GlobalValue}\LP#1\RP}
 \newcommand{\CGLOBALVALUE}[1]{\key{global\_value}\LP#1\RP}
 \newcommand{\CGLOBALVALUE}[1]{\key{global\_value}\LP#1\RP}
 \newcommand{\GLOBAL}[1]{\key{Global}\LP#1\RP}
 \newcommand{\GLOBAL}[1]{\key{Global}\LP#1\RP}