\input 6001mac
%\input psfig
\addtolength{\textheight}{1cm}
%\addtolength{\topmargin}{-1cm}

\newcommand{\comment}[1]{}

\comment{
\renewenvironment{lisp}{%
  \par
  \begin{minipage}[t]{\linewidth}
  \begin{list}{$\bullet$}{%
    \setlength{\topsep}{0in}
    \setlength{\partopsep}{0in}
    \setlength{\itemsep}{0in}
    \setlength{\parsep}{0in}
    \setlength{\leftmargin}{1.5em}
    \setlength{\rightmargin}{0in}
    \setlength{\itemindent}{0in}
  }\item[]
  \obeyspaces
  \obeylines \small\tt}{%
  \end{list}
  \end{minipage}
  \par
  }
}


\begin{document}

\vspace*{-0.5in}\psetheader{Fall Semester, 1996}
{Lecture Notes, December 3 -- Memory Management}

\subsubsection{List-Structured Memory}

A good abstraction for memory hardware is a linear vector or storage
locations with each location indicated by an ``address'' or
``offset'' from the start of memory, and with constant time access to
any location of that memory:

\begin{lisp}
(vector-ref {\it $<$vector$>$} {\it $<$offset$>$})
(vector-set! {\it $<$vector$>$} {\it $<$offset$>$} {\it $<$value$>$})
\end{lisp}

The corresponding register machine primitives are

\begin{lisp}
(assign {\it $<$reg-name$>$} (op vector-ref) (reg {\it $<$vector$>$}) {\it $<$offset$>$})
(perform (op vector-set!) (reg {\it $<$vector$>$}) {\it $<$offset$>$} {\it $<$value$>$})
\end{lisp}

To implement cons cells, we use two special vectors, {\tt the-cars} and {\tt the-cdrs}.
Below, copy a simple example of these vectors:

\framebox[6.5in]{\rule{0in}{2.2in}}

Our notation for typed pointers include the following:

\framebox[6.5in]{\rule{0in}{2.2in}}

An example to put in our memory:

\begin{lisp}
(define a '(1 2 3))
(define b (cons a a))
\end{lisp}

\framebox[6.5in]{\rule{0in}{2.2in}}

\subsubsection{Register Machine Implementation of Pair Abstraction}

To implement pairs, we need to replace the ``higher level''
abstractions we have been using for {\tt cons}, {\tt car},
and {\tt cdr} with the available vector-oriented machinery.
For example,

\begin{lisp}
(assign {\it $<$reg1$>$} (op car) (reg {\it $<$reg2$>$}))
\end{lisp}

becomes

\begin{lisp}
(assign {\it $<$reg1$>$} (op vector-ref) (reg the-cars) (reg {\it $<$reg2$>$}))
\end{lisp}

Similarly, 

\begin{lisp}
(assign {\it $<$reg1$>$} (op cdr) (reg {\it $<$reg2$>$}))
\end{lisp}

becomes

\begin{lisp}
(assign {\it $<$reg1$>$} (op vector-ref) (reg the-cdrs) (reg {\it $<$reg2$>$}))
\end{lisp}

\medskip
Allocation of a cons cell depends upon some additional
conventions, specifically about where to find free or available cons
cells.  Here we assume that {\tt free} points to the first free
location in memory, and that everything else below {\tt free} is also
available for use.  Thus, {\tt cons} can be implemented as in this
example:

\begin{lisp}
(assign {\it $<$reg1$>$} (op cons) (reg {\it $<$reg2$>$}) (const {\it $<$value$>$}))
\end{lisp}

becomes

\begin{lisp}
(perform (op vector-set!) (reg the-cars) (reg free) (reg {\it $<$reg2$>$})) 
(perform (op vector-set!) (reg the-cdrs) (reg free) (const {\it $<$value$>$}))
(assign {\it $<$reg1$>$} (reg free))
(assign free (op +) (reg free) (const 1))
\end{lisp}

\newpage
\subsection{Garbage Collection}

\subsubsection{Method 1: Reference Count}
The basic idea is:

\framebox[6.5in]{\rule{0in}{1.5in}}

An example:

\framebox[6.5in]{\rule{0in}{2.5in}}


\subsubsection{Method 2: Mark-Sweep}
The basic idea is:

\framebox[6.5in]{\rule{0in}{1.5in}}

An example:

\framebox[6.5in]{\rule{0in}{2.5in}}


\subsubsection{Method 3: Stop and Copy}
The basic idea is:

\framebox[6.5in]{\rule{0in}{1.5in}}

An example:

\framebox[6.5in]{\rule{0in}{2.5in}}


\end{document}
