You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
5.9KB

  1. %% drawstack.sty
  2. %
  3. % v0 (2010) : draw stack.
  4. % v0.1 (2012) : draw structures outside stack.
  5. %
  6. %% Copyright 2010,2012 Matthieu Moy <Matthieu.Moy@imag.fr>
  7. %
  8. % This work may be distributed and/or modified under the
  9. % conditions of the LaTeX Project Public License, either version 1.3
  10. % of this license or (at your option) any later version.
  11. % The latest version of this license is in
  12. % http://www.latex-project.org/lppl.txt
  13. % and version 1.3 or later is part of all distributions of LaTeX
  14. % version 2005/12/01 or later.
  15. %
  16. % This work has the LPPL maintenance status `maintained'.
  17. %
  18. % The Current Maintainer of this work is M. Matthieu Moy.
  19. %
  20. % This work consists of the files drawstack.sty and the example file
  21. % stack-example.tex.
  22. \NeedsTeXFormat{LaTeX2e}
  23. \ProvidesPackage{drawstack}[2010/08/06 Draw Execution Stack in LaTeX]
  24. \RequirePackage{tikz}
  25. \RequirePackage{ifthen}
  26. % Default styles definition.
  27. \tikzstyle{freecell}=[fill=green!10,draw=green!30!black]
  28. \tikzstyle{freestruct}=[fill=blue!10,draw=blue!30!black]
  29. \tikzstyle{occupiedcell}=[fill=green!10!red!10,draw=green!30!black]
  30. \tikzstyle{padding}=[fill=green!10!black!10,draw=green!30!black]
  31. \tikzstyle{highlight}=[draw=red!50!black,text=red!50!black]
  32. \DeclareOption{nocolor}{
  33. \tikzstyle{freecell}=[fill=white!95!black,draw=black]
  34. \tikzstyle{freestruct}=[fill=white!93!black,draw=black]
  35. \tikzstyle{occupiedcell}=[fill=white!60!black,draw=black]
  36. \tikzstyle{padding}=[fill=white!90!black!10,draw=black]
  37. \tikzstyle{highlight}=[draw=black,text=black]
  38. }
  39. \ProcessOptions
  40. \pgfdeclarelayer{background layer}
  41. \pgfdeclarelayer{foreground layer}
  42. \pgfsetlayers{background layer,main,foreground layer}
  43. \usetikzlibrary{shapes} % ellipse
  44. \usetikzlibrary{snakes} % braces
  45. \newcounter{cellnb}
  46. \newcounter{structnb}
  47. \setcounter{cellnb}{0}
  48. \newcommand{\llcell}[3]{
  49. \addtocounter{cellnb}{-#1}
  50. \setcounter{ptrnb}{0}
  51. \draw[#2] (0,\value{cellnb}) +(-2,-.5) rectangle +(2,-.5+#1);
  52. \draw (0,\value{cellnb}+#1/2-0.5) node(currentcell) {#3};
  53. }
  54. \newcommand{\bigcell}[2]{
  55. \llcell{#1}{freecell}{#2}
  56. \foreach \c in {2,...,#1} {
  57. \draw[freecell]
  58. (-2,\value{cellnb}+\c-1.5) -- (-1.7,\value{cellnb}+\c-1.5);
  59. \draw[freecell]
  60. (1.7,\value{cellnb}+\c-1.5) -- (2,\value{cellnb}+\c-1.5);
  61. }
  62. }
  63. \newcommand{\cell}[2][freecell]{%
  64. \llcell{1}{#1}{#2}}
  65. \newcommand{\separator}[1][freecell,very thick]{
  66. \draw[#1] (0,\value{cellnb}) +(-2,-.5) -- +(2,-.5);
  67. }
  68. % Pointer to the next cell, useful if the next cell has size != 0.
  69. \newcommand{\cellptrnext}[1]{
  70. \draw[<-,line width=0.7pt] (0,\value{cellnb}-1) +(2,\value{ptrnb}*0.1) -- +(2.5,\value{ptrnb}*0.45);
  71. \draw (2.5,\value{ptrnb}*0.5+\value{cellnb}-1) node[anchor=west] {#1};
  72. \addtocounter{ptrnb}{1}
  73. }
  74. \newcounter{ptrnb}
  75. \newcommand{\cellptr}[1]{
  76. \draw[<-,line width=0.7pt] (0,\value{cellnb}) +(2,\value{ptrnb}*0.1) -- +(2.5,\value{ptrnb}*0.45);
  77. \draw (2.5,\value{ptrnb}*0.5+\value{cellnb}) node[anchor=west] {#1};
  78. \addtocounter{ptrnb}{1}
  79. }
  80. \newcommand{\esp}{\cellptr{\%esp}}
  81. \newcommand{\ebp}{\cellptr{\%ebp}}
  82. % like cellptr, but without the arrow
  83. \newcommand{\cellcom}[1]{
  84. \draw (2.4,\value{ptrnb}*0.5+\value{cellnb}) node[anchor=west] {#1};
  85. \addtocounter{ptrnb}{1}
  86. }
  87. % like cellcom, but on the left hand side
  88. \newcommand{\cellcomL}[1]{
  89. \draw (-2,\value{ptrnb}*0.5+\value{cellnb}) node[anchor=east] {#1};
  90. \addtocounter{ptrnb}{1}
  91. }
  92. \newcommand{\cellround}[1]{
  93. \begin{pgfonlayer}{foreground layer}
  94. \draw[highlight] (0,\value{cellnb})
  95. node[shape=ellipse,draw, minimum width=3cm, minimum height=0.9cm]
  96. (marked) {};
  97. \draw[highlight] (0,\value{cellnb}) +(3,1.3) node[anchor=west] (textmarked) {#1};
  98. \draw[highlight,->] (marked) -- (textmarked.text);
  99. \end{pgfonlayer}
  100. }
  101. \newcommand{\stacktop}[2][freecell]{
  102. \setcounter{cellnb}{0}
  103. \ifthenelse{\equal{#2}{}}{}{
  104. \draw (0,\value{cellnb}) node {#2};
  105. \addtocounter{cellnb}{-1}
  106. }
  107. \draw[#1] (0,\value{cellnb})
  108. +(-2,.5) -- +(-2,-.5) -- +(2,-.5) -- +(2,.5);
  109. \draw (0,\value{cellnb}) node{...};
  110. }
  111. \newcommand{\stackbottom}[1][freecell]{
  112. \addtocounter{cellnb}{-1}
  113. \draw[#1] (0,\value{cellnb})
  114. +(-2,-.5) -- +(-2,+.5) -- +(2,+.5) -- +(2,-.5);
  115. \draw (0,\value{cellnb}) node{...};
  116. }
  117. \newenvironment{drawstack}[1][]%
  118. {\begin{tikzpicture}[#1]\stacktop{}}%
  119. {\stackbottom\end{tikzpicture}}
  120. \newcommand{\bcell}[1]{\cell[occupiedcell]{#1}}
  121. \newcommand{\padding}[2]{\llcell{#1}{padding}{#2}}
  122. \newcommand{\bstacktop}[0]{\stacktop[occupiedcell]}
  123. \newcommand{\bstackbottom}[0]{\stackbottom[occupiedcell]}
  124. \newcounter{startframe}
  125. \newcommand{\startframe}[0]{
  126. \setcounter{startframe}{\value{cellnb}}
  127. }
  128. \newcommand{\finishframe}[1]{
  129. \draw[snake=brace, line width=0.6pt, segment amplitude=7pt]
  130. (-2,\value{cellnb}-0.5) -- (-2,\value{startframe}-0.5);
  131. \draw (-4.2cm,\value{cellnb}*0.5+\value{startframe}*0.5-0.5) node
  132. {\parbox{2cm}{%
  133. \begin{flushright}
  134. #1
  135. \end{flushright}}};
  136. }
  137. \newcommand{\stackframe}[3]{
  138. \draw[snake=brace] (-2,-#1-0.5) -- (-2,-#2-0.5);
  139. \draw (0, #1) node {#3};
  140. }
  141. \newcommand{\drawstruct}[1]{
  142. \setcounter{structnb}{0}
  143. \path #1 coordinate (structpos);
  144. \path (structpos) ++ (0,\value{structnb}) +(-1.6,-.5) coordinate (structtopleft);
  145. }
  146. \newcommand{\llstructcell}[3]{
  147. \addtocounter{structnb}{-#1}
  148. \path (structpos) ++ (0,\value{structnb}) +(-1.6,-.5) coordinate (topleft);
  149. \path (structpos) ++ (0,\value{structnb}) +(-1.6,-.5+#1) coordinate (bottomleft);
  150. \path (structpos) ++ (0,\value{structnb}) +( 1.6,-.5+#1) coordinate (bottomright);
  151. \draw[#2] (topleft) rectangle (bottomright);
  152. \draw (structpos) ++ (0,\value{structnb}+#1/2-0.5) node (currentcell){#3};
  153. }
  154. \newcommand{\structcell}[2][freestruct]{%
  155. \llstructcell{1}{#1}{#2}
  156. }
  157. \newcommand{\structname}[1]{
  158. \path (structpos) ++ (0,\value{structnb}) +(-1.6,-.5) coordinate (structbottomleft);
  159. \draw (barycentric cs:structtopleft=.5,structbottomleft=.5)
  160. node[rotate=90,anchor=south] {#1};
  161. }
  162. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%