summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/latex/drawstack/stack-example.tex
blob: 3f87460897aa378cf9d04635121e53c4c54fe448 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
 %% stack-example.tex
  %% Copyright 2010 Matthieu Moy <Matthieu.Moy@imag.fr>
  %
  % This work may be distributed and/or modified under the
  % conditions of the LaTeX Project Public License, either version 1.3
  % of this license or (at your option) any later version.
  % The latest version of this license is in
  %   http://www.latex-project.org/lppl.txt
  % and version 1.3 or later is part of all distributions of LaTeX
  % version 2005/12/01 or later.
  %
  % This work has the LPPL maintenance status `maintained'.
  %
  % The Current Maintainer of this work is M. Matthieu Moy.
  %
  % This work consists of the files drawstack.sty and the example file
  % stack-example.tex.

\documentclass{article}

\usepackage{drawstack}

% Use this instead if you don't want colors.
% \usepackage[nocolor]{drawstack}

\title{{\tt drawstack.sty}: Draw execution stack easily in LaTeX}
\author{Matthieu Moy}

\begin{document}
\maketitle

{\tt drawstack} is a LaTeX package to easily draw execution stack
(typically to illustrate assembly language notions), written on top of
TikZ. This file serves as an example of usage of {\tt drawstack}, and
serves as documentation for this package. Read the source code and
comments to see how to use it.

\section{Minimalistic example}

% The main feature of the package is to define an environment
% drawstack.
\begin{drawstack}
  % Within the environment, draw stack elements with \cell{...}
  \cell{First cell}
  \cell{Second cell}
\end{drawstack}

\section{Grouping cells into stack frames}

\begin{drawstack}
  \startframe
  \cell{First cell}
  \cell{Second cell}
  \finishframe{Some stack frame}
  \cell{Not interesting}
  \startframe
  \cell{Next stack frame}
  \cell{Next stack frame}
  \finishframe{Another stack frame}
\end{drawstack}

\section{Stack and Base pointers}

\begin{drawstack}
  \startframe
  % \cellcom writes something on the right-hand side of a cell.
  \cell{loc2} \cellcom{-8(\%ebp)}
  \cell{loc1} \cellcom{-4(\%ebp)}
  % \esp and \ebp are stack pointer and base pointer in Pentium.
  % These macros are simple shortcuts for \cellptr{...}
  \cell{Sauvegarde \%ebp} \esp \ebp
  \cell{@ retour} \cellcom{4(\%ebp)}
  \finishframe{fonction\\ {\tt f}}
  \startframe
  \cell{} \cellcom{8(\%ebp)}
  \cell{}
  \finishframe{fonction\\ {\tt main}}
\end{drawstack}

\section{Padding}

\begin{drawstack}
  \cell{above padding}
  \padding{3}{nothing here}
  \cell{below padding}
\end{drawstack}

\section{Below/Above stack pointer}

\begin{drawstack}
  \cell{Top}
  \cell{Below top}
  % \bcell is just like \cell, but in a different color.
  \bcell{Above bottom} \cellptr{Stack pointer here}
  \bcell{Bottom}
\end{drawstack}

\section{Highlighting some cell}

\begin{drawstack}
  \cell{Uninteresting cell}
  \cell{Interesting cell} \cellround{Yes, this one!}
\end{drawstack}

\section{Structures without a stack structure}

\begin{tikzpicture}
  \draw (3, -1) node (Otm) {
    \begin{tabular}{c}
      Object\\vtable
    \end{tabular}
  };

  \drawstruct{(0,0)}
  \structcell[freecell]{~} \coordinate (Atm) at (currentcell.east);
  \structcell[freecell]{\texttt{@Object.equals()}}
  \structcell[freecell]{\texttt{@code A.m()}}
  \structcell[freecell]{\texttt{@code A.p()}} \coordinate (A) at (currentcell.west);
  \structname{
    \begin{tabular}{c}
      A's vtable
    \end{tabular}
  }

  \drawstruct{(-4,-3)}
  \structcell[freecell]{} \coordinate (Btm) at (currentcell.east);
  \structcell[freecell]{\texttt{@Object.equals()}}
  \structcell[freecell]{\texttt{@code A.m()}}
  \structcell[freecell]{\texttt{@code B.p()}}
  \structcell[freecell]{\texttt{@code B.q()}}
  \structname{B's vtable}

  \draw[->] (Btm) -- (A);
  \draw[->] (Atm) -- (Otm);
\end{tikzpicture}

\section{Structures and stack together}

\begin{tikzpicture}[scale=.8]

  \stacktop{}
  \separator
  \cell{\texttt{p3}}        \cellcomL{11(GB)} \coordinate (p3) at (currentcell.east);
  \separator
  \cell{\texttt{p2}}        \cellcomL{10(GB)} \coordinate (p2) at (currentcell.east);
  \separator
  \cell{\texttt{p1}}        \cellcomL{ 9(GB)} \coordinate (p1) at (currentcell.east);
  \separator
  \cell{\texttt{@P3D.diag}} \cellcomL{ 8(GB)}
  \cell{\texttt{\footnotesize @Object.equals}} \cellcomL{ 7(GB)}
  \cell{\texttt{3(GB)}}     \cellcomL{ 6(GB)} \coordinate (T1) at (currentcell.east);
  \separator
  \cell{\texttt{@P2D.diag}} \cellcomL{ 5(GB)}
  \cell{\texttt{\footnotesize @Object.equals}} \cellcomL{ 4(GB)}
  \cell{\texttt{1(GB)}}     \cellcomL{ 3(GB)} \coordinate (T2) at (currentcell.east);
  \separator
  \cell{\texttt{\footnotesize @Object.equals}} \cellcomL{ 2(GB)}
  \cell{\texttt{null}}      \cellcomL{ 1(GB)}
  \cell[draw=none]{Stack}


  \drawstruct{(5,1)})
  \structcell{z=2,5}
  \structcell{y=2,5}
  \structcell{x=2,5}
  \structcell{.} \coordinate (O1) at (currentcell.west);
  \coordinate (O1l) at (currentcell.south);

  \drawstruct{(9,-3)}
  \structcell{y=1}
  \structcell{x=1}
  \structcell{.} \coordinate (O2) at (currentcell.west);
  \coordinate (O2l) at (currentcell.south);

  \draw[->] (p3) -- (O1);
  \draw[->] (p2) -- (O1);
  \draw[->] (p1) -- (O2);

  \draw[->] (O1l) .. controls (O1 |- T1) .. (T1);
  \draw[->] (O2l) .. controls (O2 |- T2) .. (T2);

  \draw (10,-10) node{Heap};

\end{tikzpicture}



\section{Using tikzpicture instead of drawstack}

% The environment drawstack is basically a syntactic sugar for
%
% \begin{tikzpicture}[#1]
% \stacktop{}
% ...
% \stackbottom
% \end{tikzpicture}
%
% You can use the above syntax for more flexibility.

\begin{tikzpicture}[scale=0.8]
  \small
  \stacktop{}
  \cell{My cell}
  \stackbottom{}
\end{tikzpicture}

\section{Changing style}

{% tikzstyle will be local to this {...}
\tikzstyle{freecell}=[fill=blue!10,draw=blue!30!black]
\tikzstyle{occupiedcell}=[fill=blue!10!orange!10,draw=blue!30!black]
\tikzstyle{padding}=[fill=yellow!20,draw=blue!30!black]
\tikzstyle{highlight}=[draw=orange!50!black,text=orange!50!black]

\begin{drawstack}
  \cell{Uninteresting cell}
  \cell{Interesting cell} \cellround{Yes, this one!}
  \bcell{bcell}
  \padding{2}{Padding}
\end{drawstack}
}

\section{Example: Computing Factorial}

\begin{drawstack}[scale=0.8]
  \startframe
  \cell{N = 1}
  \cell{...}
  \finishframe{fact(1)}
  \startframe
  \cell{N = 2}
  \cell{...}
  \finishframe{fact(2)}
  \cell{$\vdots$}
  \startframe
  \cell{N = 5}
  \cell{...}
  \finishframe{fact(5)}
\end{drawstack}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End: