
% graph drawing
\newcommand{\digraph}[3][scale=1]{ 
  \newwrite\dotfile 
  \immediate\openout\dotfile=dot_temp_#2.dot 
  \immediate\write\dotfile{digraph #2 { margin=0; pad=0; concentrate=false; \string#3}} 
  \immediate\closeout\dotfile
  \immediate\write18{bash -c "dot -Tpdf dot_temp_#2.dot > dot_temp_#2.pdf"}  
  \IfFileExists{dot_temp_#2.pdf}
  % the pdf exists: include it 
  { \includegraphics[#1]{dot_temp_#2} } 
  % the pdf was not created - show a hint
  { \fbox{ \begin{tabular}{l} 
        The file \texttt{dot_temp_#2.pdf} hasn't been created from 
        \texttt{dot_temp_#2.dot} yet. \\
        We attempted to create it with:\\
        `\texttt{dot -Tpdf dot_temp_#2.dot > dot_temp_#2.pdf}' \\
        but that seems not to have worked. You need to execute `\texttt{pdflatex}' with \\
        the `\texttt{-shell-escape} option.
      \end{tabular}} 
  } 
}

\NewEnviron{digraphenv}[2]{\digraph[#1]{#2}{ nodesep="0.1"; ranksep="0.2"; \BODY }}

\newcommand{\control}[2]{#1:successors:s -> #2:predecessors:n [color=red];}
\newcommand{\controllabel}[2]{#1:s -> #2:predecessors:n [color=red];}
\newcommand{\data}[2]{#2:usages:s -> #1:inputs [color=black,dir=back];}
\newcommand{\datalabel}[2]{#2:usages:s -> #1:n [color=black,dir=back];}
\newcommand{\datalabeltext}[3]{#2:usages:s -> #1:n [color=black,dir=back,label="#3"];}

\newcommand{\textnode}[2]{#1 [shape=plaintext, label="#2"]}
\newcommand{\genericnodestart}[1]{#1 [shape=plaintext, label=< <TABLE BORDER="0" CELLSPACING="0"><TR><TD CELLPADDING="0"><TABLE BORDER="0" CELLSPACING="2" CELLPADDING="0"><TR><TD WIDTH="15" HEIGHT="5" PORT="predecessors" BGCOLOR="rosybrown1"></TD></TR></TABLE></TD><TD COLSPAN="2" CELLPADDING="0" ALIGN="RIGHT"><TABLE BORDER="0" CELLSPACING="2" CELLPADDING="0"><TR>}
\newcommand{\genericnodeend}[0]{</TR></TABLE></TD><TD CELLPADDING="0"><TABLE BORDER="0" CELLSPACING="2" CELLPADDING="0"><TR><TD WIDTH="15" HEIGHT="5" PORT="usages" BGCOLOR="lightgrey"></TD></TR></TABLE></TD></TR></TABLE>>]}
\newcommand{\portinput}[1]{<TD WIDTH="15" HEIGHT="5" PORT="#1" BGCOLOR="lightgrey"></TD>}
\newcommand{\portsuccessor}[1]{<TD WIDTH="15" HEIGHT="5" PORT="#1" BGCOLOR="rosybrown1"></TD>}
\newcommand{\portempty}[0]{<TD WIDTH="15" HEIGHT="5"></TD>}
\newcommand{\genericnodelabel}[2]{</TR></TABLE></TD></TR><TR><TD BORDER="1" COLSPAN="3" BGCOLOR="#2"><FONT POINT-SIZE="12">#1</FONT></TD></TR><TR><TD COLSPAN="2" CELLPADDING="0" ALIGN="RIGHT"><TABLE BORDER="0" CELLSPACING="2" CELLPADDING="0"><TR>}

\newcommand{\nodestart}[0]{start [shape=plaintext, rank=source, label=< <TABLE BORDER="0" CELLSPACING="0"><TR><TD COLSPAN="3"><TABLE BORDER="0"><TR><TD></TD> \genericnodelabel{start}{lightyellow} \portsuccessor{successors} \portempty \genericnodeend }

\newcommand{\nodeend}[0]{\genericnodestart{end} \portempty \portinput{inputs} \genericnodelabel{end}{lightyellow} <TD></TD></TR></TABLE></TD><TD></TD></TR></TABLE>>]}

\newcommand{\nodeconst}[2]{#1 [margin="0.05, 0.05", shape=plaintext, label=< <TABLE BORDER="0" CELLSPACING="0"><TR><TD BORDER="1" BGCOLOR="white"><FONT POINT-SIZE="12">#2</FONT></TD></TR><TR><TD WIDTH="15" HEIGHT="5" PORT="usages" BGCOLOR="lightgrey"></TD></TR></TABLE>>]}
\newcommand{\nodeframestate}[2]{#1 [margin="0.05, 0.05", shape=plaintext, label=< <TABLE BORDER="0" CELLSPACING="0"><TR><TD WIDTH="15" HEIGHT="5" PORT="predecessors" BGCOLOR="rosybrown1"></TD><TD WIDTH="15" HEIGHT="5"></TD><TD HEIGHT="5" PORT="inputs" BGCOLOR="lightgrey"></TD></TR><TR><TD BORDER="1" COLSPAN="3" BGCOLOR="lightblue"><FONT POINT-SIZE="12">#2</FONT></TD></TR></TABLE>>]}


\newcommand{\node}[2]{\genericnodestart{#1} \portempty \portinput{inputs} \genericnodelabel{#2}{white} \portsuccessor{successors} \portempty \genericnodeend }
\newcommand{\nodebi}[2]{\genericnodestart{#1} \portinput{in1} \portinput{in2} \genericnodelabel{#2}{white} \portsuccessor{successors} \portempty \genericnodeend }
\newcommand{\nodetri}[2]{\genericnodestart{#1} \portinput{in1} \portinput{in2} \portinput{in3} \genericnodelabel{#2}{white} \portsuccessor{successors} \portempty \portempty \genericnodeend }
\newcommand{\nodequad}[2]{\genericnodestart{#1} \portinput{in1} \portinput{in2} \portinput{in3} \portinput{in4} \genericnodelabel{#2}{white} \portsuccessor{successors} \portempty \portempty \portempty \genericnodeend }
\newcommand{\nodesplit}[2]{\genericnodestart{#1} \portempty \portinput{inputs} \genericnodelabel{#2}{white} \portsuccessor{succ1} \portsuccessor{succ2} \genericnodeend }
\newcommand{\nodequadsplit}[2]{\genericnodestart{#1} \portinput{in1} \portinput{in2} \portinput{in3} \portinput{in4} \genericnodelabel{#2}{white} \portsuccessor{succ1} \portsuccessor{succ2} \portempty \portempty \genericnodeend }
\newcommand{\nodetrisplit}[2]{\genericnodestart{#1} \portinput{in1} \portinput{in2} \portinput{in3} \genericnodelabel{#2}{white} \portsuccessor{succ1} \portsuccessor{succ2} \portempty \genericnodeend }
\newcommand{\nodesplittri}[2]{\genericnodestart{#1} \portempty \portinput{inputs} \genericnodelabel{#2}{white} \portsuccessor{succ1} \portsuccessor{succ2} \portsuccessor{succ3} \genericnodeend }

\newcommand{\nodetrap}[2]{\cnodebi{#1}{#2}{rosybrown1}}

\newcommand{\cnode}[3]{\genericnodestart{#1} \portempty \portinput{inputs} \genericnodelabel{#2}{#3} \portsuccessor{successors} \portempty \genericnodeend }
\newcommand{\cnodebi}[3]{\genericnodestart{#1} \portinput{in1} \portinput{in2} \genericnodelabel{#2}{#3} \portsuccessor{successors} \portempty \genericnodeend }
\newcommand{\cnodetri}[3]{\genericnodestart{#1} \portinput{in1} \portinput{in2} \portinput{in3} \genericnodelabel{#2}{#3} \portsuccessor{successors} \portempty \portempty \genericnodeend }
\newcommand{\cnodesplit}[3]{\genericnodestart{#1} \portempty \portinput{inputs} \genericnodelabel{#2}{#3} \portsuccessor{succ1} \portsuccessor{succ2} \genericnodeend }

% this doesn't work:
%\newenvironment{graphfigure}[2]{\begin{figure}[h] \label{fig:#1} \centering \begin{digraphenv}{scale=0.5}{#1}}{\end{digraphenv} \caption{#2} \end{figure}}

%%%%%%%%%%%%%% example:

% \begin{digraphenv}{scale=0.5}{MyGraph}
% \node{start}{start}
% \node{end}{end}
% % input projections
% \node{a}{proj:a}
% \node{b}{proj:b}
% \data{a}{start}
% \data{b}{start}
% % if
% \nodebi{cmp1}{&lt;}
% \datalabel{cmp1:in1}{a}
% \datalabel{cmp1:in2}{b}
% \nodesplit{if}{if}
% \data{if}{cmp1}
% \control{start}{if}
% % branches
% \nodebi{add1}{+}
% \datalabel{add1:in1}{a}
% \datalabel{add1:in2}{b}
% \nodebi{sub1}{-}
% \datalabel{sub1:in1}{a}
% \datalabel{sub1:in2}{b}
% \controllabel{if:succ1}{add1}
% \controllabel{if:succ2}{sub1}
% % merge
% \nodebi{merge}{merge}
% \control{add1}{merge}
% \control{sub1}{merge}
% % phi
% \nodebi{phi1}{phi}
% \datalabel{phi1:in1}{add1}
% \datalabel{phi1:in2}{sub1}
% % ret
% \node{ret}{ret}
% \data{ret}{phi1}
% \control{merge}{ret}
% \control{ret}{end}
% \end{digraphenv}

%%%%%%%%%%%%%%%
