summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/drawstack
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
committerNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
commite0c6872cf40896c7be36b11dcc744620f10adf1d (patch)
tree60335e10d2f4354b0674ec22d7b53f0f8abee672 /macros/latex/contrib/drawstack
Initial commit
Diffstat (limited to 'macros/latex/contrib/drawstack')
-rw-r--r--macros/latex/contrib/drawstack/Makefile9
-rw-r--r--macros/latex/contrib/drawstack/README6
-rw-r--r--macros/latex/contrib/drawstack/drawstack.sty179
-rw-r--r--macros/latex/contrib/drawstack/stack-example.pdfbin0 -> 110321 bytes
-rw-r--r--macros/latex/contrib/drawstack/stack-example.tex246
5 files changed, 440 insertions, 0 deletions
diff --git a/macros/latex/contrib/drawstack/Makefile b/macros/latex/contrib/drawstack/Makefile
new file mode 100644
index 0000000000..8df4845215
--- /dev/null
+++ b/macros/latex/contrib/drawstack/Makefile
@@ -0,0 +1,9 @@
+all: stack-example.pdf drawstack.zip
+
+stack-example.pdf: stack-example.tex drawstack.sty
+ pdflatex stack-example.tex
+
+FILES=stack-example.pdf stack-example.tex drawstack.sty README Makefile
+
+drawstack.zip: $(FILES)
+ cd .. && zip -r $@ $(FILES:%=drawstack/%) && mv drawstack.zip drawstack/
diff --git a/macros/latex/contrib/drawstack/README b/macros/latex/contrib/drawstack/README
new file mode 100644
index 0000000000..64173b882c
--- /dev/null
+++ b/macros/latex/contrib/drawstack/README
@@ -0,0 +1,6 @@
+This is a simple LaTeX package to easily draw execution stack
+(typically to illustrate assembly language notions), written
+on top of TikZ.
+
+Author's name: Matthieu Moy
+License type: lppl
diff --git a/macros/latex/contrib/drawstack/drawstack.sty b/macros/latex/contrib/drawstack/drawstack.sty
new file mode 100644
index 0000000000..b05e81ebb2
--- /dev/null
+++ b/macros/latex/contrib/drawstack/drawstack.sty
@@ -0,0 +1,179 @@
+ %% drawstack.sty
+ %% 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.
+
+\NeedsTeXFormat{LaTeX2e}
+
+\ProvidesPackage{drawstack}[2010/08/06 Draw Execution Stack in LaTeX]
+
+\RequirePackage{tikz}
+\RequirePackage{ifthen}
+
+% Default styles definition.
+\tikzstyle{freecell}=[fill=green!10,draw=green!30!black]
+\tikzstyle{freestruct}=[fill=blue!10,draw=blue!30!black]
+\tikzstyle{occupiedcell}=[fill=green!10!red!10,draw=green!30!black]
+\tikzstyle{padding}=[fill=green!10!black!10,draw=green!30!black]
+\tikzstyle{highlight}=[draw=red!50!black,text=red!50!black]
+
+\DeclareOption{nocolor}{
+ \tikzstyle{freecell}=[fill=white!95!black,draw=black]
+ \tikzstyle{freestruct}=[fill=white!93!black,draw=black]
+ \tikzstyle{occupiedcell}=[fill=white!60!black,draw=black]
+ \tikzstyle{padding}=[fill=white!90!black!10,draw=black]
+ \tikzstyle{highlight}=[draw=black,text=black]
+}
+\ProcessOptions
+
+\pgfdeclarelayer{background layer}
+\pgfdeclarelayer{foreground layer}
+\pgfsetlayers{background layer,main,foreground layer}
+\usetikzlibrary{shapes} % ellipse
+\usetikzlibrary{snakes} % braces
+
+\newcounter{cellnb}
+\newcounter{structnb}
+\setcounter{cellnb}{0}
+
+\newcommand{\llcell}[3]{
+ \addtocounter{cellnb}{-#1}
+ \setcounter{ptrnb}{0}
+ \draw[#2] (0,\value{cellnb}) +(-2,-.5) rectangle +(2,-.5+#1);
+ \draw (0,\value{cellnb}+#1/2-0.5) node(currentcell) {#3};
+}
+\newcommand{\bigcell}[2]{
+ \llcell{#1}{freecell}{#2}
+ \foreach \c in {2,...,#1} {
+ \draw[freecell]
+ (-2,\value{cellnb}+\c-1.5) -- (-1.7,\value{cellnb}+\c-1.5);
+ \draw[freecell]
+ (1.7,\value{cellnb}+\c-1.5) -- (2,\value{cellnb}+\c-1.5);
+ }
+}
+\newcommand{\cell}[2][freecell]{%
+\llcell{1}{#1}{#2}}
+
+\newcommand{\separator}[1][freecell,very thick]{
+ \draw[#1] (0,\value{cellnb}) +(-2,-.5) -- +(2,-.5);
+}
+
+% Pointer to the next cell, useful if the next cell has size != 0.
+\newcommand{\cellptrnext}[1]{
+ \draw[<-,line width=0.7pt] (0,\value{cellnb}-1) +(2,\value{ptrnb}*0.1) -- +(2.5,\value{ptrnb}*0.45);
+ \draw (2.5,\value{ptrnb}*0.5+\value{cellnb}-1) node[anchor=west] {#1};
+ \addtocounter{ptrnb}{1}
+}
+\newcounter{ptrnb}
+\newcommand{\cellptr}[1]{
+ \draw[<-,line width=0.7pt] (0,\value{cellnb}) +(2,\value{ptrnb}*0.1) -- +(2.5,\value{ptrnb}*0.45);
+ \draw (2.5,\value{ptrnb}*0.5+\value{cellnb}) node[anchor=west] {#1};
+ \addtocounter{ptrnb}{1}
+}
+\newcommand{\esp}{\cellptr{\%esp}}
+\newcommand{\ebp}{\cellptr{\%ebp}}
+
+% like cellptr, but without the arrow
+\newcommand{\cellcom}[1]{
+ \draw (2.4,\value{ptrnb}*0.5+\value{cellnb}) node[anchor=west] {#1};
+ \addtocounter{ptrnb}{1}
+}
+% like cellcom, but on the left hand side
+\newcommand{\cellcomL}[1]{
+ \draw (-2,\value{ptrnb}*0.5+\value{cellnb}) node[anchor=east] {#1};
+ \addtocounter{ptrnb}{1}
+}
+
+\newcommand{\cellround}[1]{
+ \begin{pgfonlayer}{foreground layer}
+ \draw[highlight] (0,\value{cellnb})
+ node[shape=ellipse,draw, minimum width=3cm, minimum height=0.9cm]
+ (marked) {};
+ \draw[highlight] (0,\value{cellnb}) +(3,1.3) node[anchor=west] (textmarked) {#1};
+ \draw[highlight,->] (marked) -- (textmarked.text);
+ \end{pgfonlayer}
+}
+
+\newcommand{\stacktop}[2][freecell]{
+ \setcounter{cellnb}{0}
+ \ifthenelse{\equal{#2}{}}{}{
+ \draw (0,\value{cellnb}) node {#2};
+ \addtocounter{cellnb}{-1}
+ }
+
+ \draw[#1] (0,\value{cellnb})
+ +(-2,.5) -- +(-2,-.5) -- +(2,-.5) -- +(2,.5);
+ \draw (0,\value{cellnb}) node{...};
+}
+\newcommand{\stackbottom}[1][freecell]{
+ \addtocounter{cellnb}{-1}
+ \draw[#1] (0,\value{cellnb})
+ +(-2,-.5) -- +(-2,+.5) -- +(2,+.5) -- +(2,-.5);
+ \draw (0,\value{cellnb}) node{...};
+}
+\newenvironment{drawstack}[1][]%
+{\begin{tikzpicture}[#1]\stacktop{}}%
+{\stackbottom\end{tikzpicture}}
+
+\newcommand{\bcell}[1]{\cell[occupiedcell]{#1}}
+\newcommand{\padding}[2]{\llcell{#1}{padding}{#2}}
+\newcommand{\bstacktop}[0]{\stacktop[occupiedcell]}
+\newcommand{\bstackbottom}[0]{\stackbottom[occupiedcell]}
+
+\newcounter{startframe}
+\newcommand{\startframe}[0]{
+ \setcounter{startframe}{\value{cellnb}}
+}
+\newcommand{\finishframe}[1]{
+ \draw[snake=brace, line width=0.6pt, segment amplitude=7pt]
+ (-2,\value{cellnb}-0.5) -- (-2,\value{startframe}-0.5);
+ \draw (-4.2cm,\value{cellnb}*0.5+\value{startframe}*0.5-0.5) node
+ {\parbox{3cm}{%
+\begin{flushright}
+#1
+\end{flushright}}};
+}
+\newcommand{\stackframe}[3]{
+ \draw[snake=brace] (-2,-#1-0.5) -- (-2,-#2-0.5);
+ \draw (0, #1) node {#3};
+}
+
+\newcommand{\drawstruct}[1]{
+ \setcounter{structnb}{0}
+ \path #1 coordinate (structpos);
+ \path (structpos) ++ (0,\value{structnb}) +(-1.6,-.5) coordinate (structtopleft);
+}
+
+\newcommand{\llstructcell}[3]{
+ \addtocounter{structnb}{-#1}
+ \path (structpos) ++ (0,\value{structnb}) +(-1.6,-.5) coordinate (topleft);
+ \path (structpos) ++ (0,\value{structnb}) +(-1.6,-.5+#1) coordinate (bottomleft);
+ \path (structpos) ++ (0,\value{structnb}) +( 1.6,-.5+#1) coordinate (bottomright);
+ \draw[#2] (topleft) rectangle (bottomright);
+ \draw (structpos) ++ (0,\value{structnb}+#1/2-0.5) node (currentcell){#3};
+}
+
+\newcommand{\structcell}[2][freestruct]{%
+ \llstructcell{1}{#1}{#2}
+}
+
+\newcommand{\structname}[1]{
+ \path (structpos) ++ (0,\value{structnb}) +(-1.6,-.5) coordinate (structbottomleft);
+ \draw (barycentric cs:structtopleft=.5,structbottomleft=.5)
+ node[rotate=90,anchor=south] {#1};
+}
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
diff --git a/macros/latex/contrib/drawstack/stack-example.pdf b/macros/latex/contrib/drawstack/stack-example.pdf
new file mode 100644
index 0000000000..4cf88fea57
--- /dev/null
+++ b/macros/latex/contrib/drawstack/stack-example.pdf
Binary files differ
diff --git a/macros/latex/contrib/drawstack/stack-example.tex b/macros/latex/contrib/drawstack/stack-example.tex
new file mode 100644
index 0000000000..3f87460897
--- /dev/null
+++ b/macros/latex/contrib/drawstack/stack-example.tex
@@ -0,0 +1,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: