| 1 |
%% stack-example.tex |
| 2 |
%% Copyright 2010 Matthieu Moy <[email protected]> |
| 3 |
% |
| 4 |
% This work may be distributed and/or modified under the |
| 5 |
% conditions of the LaTeX Project Public License, either version 1.3 |
| 6 |
% of this license or (at your option) any later version. |
| 7 |
% The latest version of this license is in |
| 8 |
% http://www.latex-project.org/lppl.txt |
| 9 |
% and version 1.3 or later is part of all distributions of LaTeX |
| 10 |
% version 2005/12/01 or later. |
| 11 |
% |
| 12 |
% This work has the LPPL maintenance status `maintained'. |
| 13 |
% |
| 14 |
% The Current Maintainer of this work is M. Matthieu Moy. |
| 15 |
% |
| 16 |
% This work consists of the files drawstack.sty and the example file |
| 17 |
% stack-example.tex. |
| 18 |
|
| 19 |
\documentclass{article} |
| 20 |
|
| 21 |
\usepackage{drawstack} |
| 22 |
|
| 23 |
% Use this instead if you don't want colors. |
| 24 |
% \usepackage[nocolor]{drawstack} |
| 25 |
|
| 26 |
\title{{\tt drawstack.sty}: Draw execution stack easily in LaTeX} |
| 27 |
\author{Matthieu Moy} |
| 28 |
|
| 29 |
\begin{document} |
| 30 |
\maketitle |
| 31 |
|
| 32 |
{\tt drawstack} is a LaTeX package to easily draw execution stack |
| 33 |
(typically to illustrate assembly language notions), written on top of |
| 34 |
TikZ. This file serves as an example of usage of {\tt drawstack}, and |
| 35 |
serves as documentation for this package. Read the source code and |
| 36 |
comments to see how to use it. |
| 37 |
|
| 38 |
\section{Minimalistic example} |
| 39 |
|
| 40 |
% The main feature of the package is to define an environment |
| 41 |
% drawstack. |
| 42 |
\begin{drawstack} |
| 43 |
% Within the environment, draw stack elements with \cell{...} |
| 44 |
\cell{First cell} |
| 45 |
\cell{Second cell} |
| 46 |
\end{drawstack} |
| 47 |
|
| 48 |
\section{Grouping cells into stack frames} |
| 49 |
|
| 50 |
\begin{drawstack} |
| 51 |
\startframe |
| 52 |
\cell{First cell} |
| 53 |
\cell{Second cell} |
| 54 |
\finishframe{Some stack frame} |
| 55 |
\cell{Not interesting} |
| 56 |
\startframe |
| 57 |
\cell{Next stack frame} |
| 58 |
\cell{Next stack frame} |
| 59 |
\finishframe{Another stack frame} |
| 60 |
\end{drawstack} |
| 61 |
|
| 62 |
\section{Stack and Base pointers} |
| 63 |
|
| 64 |
\begin{drawstack} |
| 65 |
\startframe |
| 66 |
% \cellcom writes something on the right-hand side of a cell. |
| 67 |
\cell{loc2} \cellcom{-8(\%ebp)} |
| 68 |
\cell{loc1} \cellcom{-4(\%ebp)} |
| 69 |
% \esp and \ebp are stack pointer and base pointer in Pentium. |
| 70 |
% These macros are simple shortcuts for \cellptr{...} |
| 71 |
\cell{Sauvegarde \%ebp} \esp \ebp |
| 72 |
\cell{@ retour} \cellcom{4(\%ebp)} |
| 73 |
\finishframe{fonction\\ {\tt f}} |
| 74 |
\startframe |
| 75 |
\cell{} \cellcom{8(\%ebp)} |
| 76 |
\cell{} |
| 77 |
\finishframe{fonction\\ {\tt main}} |
| 78 |
\end{drawstack} |
| 79 |
|
| 80 |
\section{Padding} |
| 81 |
|
| 82 |
\begin{drawstack} |
| 83 |
\cell{above padding} |
| 84 |
\padding{3}{nothing here} |
| 85 |
\cell{below padding} |
| 86 |
\end{drawstack} |
| 87 |
|
| 88 |
\section{Below/Above stack pointer} |
| 89 |
|
| 90 |
\begin{drawstack} |
| 91 |
\cell{Top} |
| 92 |
\cell{Below top} |
| 93 |
% \bcell is just like \cell, but in a different color. |
| 94 |
\bcell{Above bottom} \cellptr{Stack pointer here} |
| 95 |
\bcell{Bottom} |
| 96 |
\end{drawstack} |
| 97 |
|
| 98 |
\section{Highlighting some cell} |
| 99 |
|
| 100 |
\begin{drawstack} |
| 101 |
\cell{Uninteresting cell} |
| 102 |
\cell{Interesting cell} \cellround{Yes, this one!} |
| 103 |
\end{drawstack} |
| 104 |
|
| 105 |
\section{Structures without a stack structure} |
| 106 |
|
| 107 |
\begin{tikzpicture} |
| 108 |
\draw (3, -1) node (Otm) { |
| 109 |
\begin{tabular}{c} |
| 110 |
Object\\vtable |
| 111 |
\end{tabular} |
| 112 |
}; |
| 113 |
|
| 114 |
\drawstruct{(0,0)} |
| 115 |
\structcell[freecell]{~} \coordinate (Atm) at (currentcell.east); |
| 116 |
\structcell[freecell]{\texttt{@Object.equals()}} |
| 117 |
\structcell[freecell]{\texttt{@code A.m()}} |
| 118 |
\structcell[freecell]{\texttt{@code A.p()}} \coordinate (A) at (currentcell.west); |
| 119 |
\structname{ |
| 120 |
\begin{tabular}{c} |
| 121 |
A's vtable |
| 122 |
\end{tabular} |
| 123 |
} |
| 124 |
|
| 125 |
\drawstruct{(-4,-3)} |
| 126 |
\structcell[freecell]{} \coordinate (Btm) at (currentcell.east); |
| 127 |
\structcell[freecell]{\texttt{@Object.equals()}} |
| 128 |
\structcell[freecell]{\texttt{@code A.m()}} |
| 129 |
\structcell[freecell]{\texttt{@code B.p()}} |
| 130 |
\structcell[freecell]{\texttt{@code B.q()}} |
| 131 |
\structname{B's vtable} |
| 132 |
|
| 133 |
\draw[->] (Btm) -- (A); |
| 134 |
\draw[->] (Atm) -- (Otm); |
| 135 |
\end{tikzpicture} |
| 136 |
|
| 137 |
\section{Structures and stack together} |
| 138 |
|
| 139 |
\begin{tikzpicture}[scale=.8] |
| 140 |
|
| 141 |
\stacktop{} |
| 142 |
\separator |
| 143 |
\cell{\texttt{p3}} \cellcomL{11(GB)} \coordinate (p3) at (currentcell.east); |
| 144 |
\separator |
| 145 |
\cell{\texttt{p2}} \cellcomL{10(GB)} \coordinate (p2) at (currentcell.east); |
| 146 |
\separator |
| 147 |
\cell{\texttt{p1}} \cellcomL{ 9(GB)} \coordinate (p1) at (currentcell.east); |
| 148 |
\separator |
| 149 |
\cell{\texttt{@P3D.diag}} \cellcomL{ 8(GB)} |
| 150 |
\cell{\texttt{\footnotesize @Object.equals}} \cellcomL{ 7(GB)} |
| 151 |
\cell{\texttt{3(GB)}} \cellcomL{ 6(GB)} \coordinate (T1) at (currentcell.east); |
| 152 |
\separator |
| 153 |
\cell{\texttt{@P2D.diag}} \cellcomL{ 5(GB)} |
| 154 |
\cell{\texttt{\footnotesize @Object.equals}} \cellcomL{ 4(GB)} |
| 155 |
\cell{\texttt{1(GB)}} \cellcomL{ 3(GB)} \coordinate (T2) at (currentcell.east); |
| 156 |
\separator |
| 157 |
\cell{\texttt{\footnotesize @Object.equals}} \cellcomL{ 2(GB)} |
| 158 |
\cell{\texttt{null}} \cellcomL{ 1(GB)} |
| 159 |
\cell[draw=none]{Stack} |
| 160 |
|
| 161 |
|
| 162 |
\drawstruct{(5,1)}) |
| 163 |
\structcell{z=2,5} |
| 164 |
\structcell{y=2,5} |
| 165 |
\structcell{x=2,5} |
| 166 |
\structcell{.} \coordinate (O1) at (currentcell.west); |
| 167 |
\coordinate (O1l) at (currentcell.south); |
| 168 |
|
| 169 |
\drawstruct{(9,-3)} |
| 170 |
\structcell{y=1} |
| 171 |
\structcell{x=1} |
| 172 |
\structcell{.} \coordinate (O2) at (currentcell.west); |
| 173 |
\coordinate (O2l) at (currentcell.south); |
| 174 |
|
| 175 |
\draw[->] (p3) -- (O1); |
| 176 |
\draw[->] (p2) -- (O1); |
| 177 |
\draw[->] (p1) -- (O2); |
| 178 |
|
| 179 |
\draw[->] (O1l) .. controls (O1 |- T1) .. (T1); |
| 180 |
\draw[->] (O2l) .. controls (O2 |- T2) .. (T2); |
| 181 |
|
| 182 |
\draw (10,-10) node{Heap}; |
| 183 |
|
| 184 |
\end{tikzpicture} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
\section{Using tikzpicture instead of drawstack} |
| 189 |
|
| 190 |
% The environment drawstack is basically a syntactic sugar for |
| 191 |
% |
| 192 |
% \begin{tikzpicture}[#1] |
| 193 |
% \stacktop{} |
| 194 |
% ... |
| 195 |
% \stackbottom |
| 196 |
% \end{tikzpicture} |
| 197 |
% |
| 198 |
% You can use the above syntax for more flexibility. |
| 199 |
|
| 200 |
\begin{tikzpicture}[scale=0.8] |
| 201 |
\small |
| 202 |
\stacktop{} |
| 203 |
\cell{My cell} |
| 204 |
\stackbottom{} |
| 205 |
\end{tikzpicture} |
| 206 |
|
| 207 |
\section{Changing style} |
| 208 |
|
| 209 |
{% tikzstyle will be local to this {...} |
| 210 |
\tikzstyle{freecell}=[fill=blue!10,draw=blue!30!black] |
| 211 |
\tikzstyle{occupiedcell}=[fill=blue!10!orange!10,draw=blue!30!black] |
| 212 |
\tikzstyle{padding}=[fill=yellow!20,draw=blue!30!black] |
| 213 |
\tikzstyle{highlight}=[draw=orange!50!black,text=orange!50!black] |
| 214 |
|
| 215 |
\begin{drawstack} |
| 216 |
\cell{Uninteresting cell} |
| 217 |
\cell{Interesting cell} \cellround{Yes, this one!} |
| 218 |
\bcell{bcell} |
| 219 |
\padding{2}{Padding} |
| 220 |
\end{drawstack} |
| 221 |
} |
| 222 |
|
| 223 |
\section{Example: Computing Factorial} |
| 224 |
|
| 225 |
\begin{drawstack}[scale=0.8] |
| 226 |
\startframe |
| 227 |
\cell{N = 1} |
| 228 |
\cell{...} |
| 229 |
\finishframe{fact(1)} |
| 230 |
\startframe |
| 231 |
\cell{N = 2} |
| 232 |
\cell{...} |
| 233 |
\finishframe{fact(2)} |
| 234 |
\cell{$\vdots$} |
| 235 |
\startframe |
| 236 |
\cell{N = 5} |
| 237 |
\cell{...} |
| 238 |
\finishframe{fact(5)} |
| 239 |
\end{drawstack} |
| 240 |
|
| 241 |
\end{document} |
| 242 |
|
| 243 |
%%% Local Variables: |
| 244 |
%%% mode: latex |
| 245 |
%%% TeX-master: t |
| 246 |
%%% End: |