\documentclass{article}

\usepackage[letterpaper, landscape, margin=0.7cm]{geometry}

\usepackage{tabularx}
\usepackage{tabulary}
\usepackage{booktabs}
\usepackage{tikz}
\usepackage{setspace}
\usepackage{listofitems}

\renewcommand\tabularxcolumn[1]{m{#1}}

\usetikzlibrary{shapes.multipart,positioning,arrows.meta}

\tikzstyle{stack}=[rectangle split, rectangle split parts=#1,draw,anchor=center]

\newenvironment{opsummary}[2]{%
\begin{tikzpicture}
\newcommand{\opsummaryshort}{#1}
\newcommand{\opsummarylong}{#2}
}{%
\draw[->] (before) -- node[above] {\texttt{\opsummaryshort}} node[text width=1cm,align=center,below] {\texttt{\tiny\begin{spacing}{0.6}\opsummarylong\end{spacing}}} (after);
\end{tikzpicture}
}

\newcommand{\stackdiagram}[2]{%
    \readlist\leftelems{#1}
    \readlist\rightelems{#2}
    \node[stack=\leftelemslen] (before) {%
        \ifnum\leftelemslen>0\nodepart{one}$\leftelems[1]$\fi
        \ifnum\leftelemslen>1\nodepart{two}$\leftelems[2]$\fi
        \ifnum\leftelemslen>2\nodepart{three}$\leftelems[3]$\fi
        \ifnum\leftelemslen>3\nodepart{four}$\leftelems[4]$\fi
        \ifnum\leftelemslen>4\nodepart{five}$\leftelems[5]$\fi
        \ifnum\leftelemslen>5\nodepart{six}$\leftelems[6]$\fi
    };
    \node[stack=\rightelemslen,right=of before] (after) {%
        \ifnum\rightelemslen>0\nodepart{one}$\rightelems[1]$\fi
        \ifnum\rightelemslen>1\nodepart{two}$\rightelems[2]$\fi
        \ifnum\rightelemslen>2\nodepart{three}$\rightelems[3]$\fi
        \ifnum\rightelemslen>3\nodepart{four}$\rightelems[4]$\fi
        \ifnum\rightelemslen>4\nodepart{five}$\rightelems[5]$\fi
        \ifnum\rightelemslen>5\nodepart{six}$\rightelems[6]$\fi
    };
}

\newif\ifnoopdef
\newif\ifopisfun
\newif\ifopisimm
\newif\ifopisgud
\newif\ifopisbad
\newcommand{\optextdescr}{}
\newcommand{\optext}[1]{\renewcommand{\optextdescr}{#1}\noopdeffalse}

\newcommand{\opsumof}[1]{%
\begin{tikzpicture}
    \noopdeftrue
    \opisfunfalse
    \opisimmfalse
    \opisgudfalse
    \opisbadfalse

    % TODO: document and add more metasyntactic variables/functions?

    % So far uncategorized instructions
    \if d#1
        \optext{set x bytes of memory to immediate value at p}
        \opisbadtrue
        \stackdiagram{x,p,\ldots}{\ldots}
    \fi
    \if f#1
        \optext{invoke f, saving return address on stack}
        \opisfuntrue
        \stackdiagram{f,\ldots}{g,\ldots}
    \fi
    \if @#1
        \optext{call immediate name}
        \opisimmtrue
        \opisfuntrue
        \opisgudtrue
        \stackdiagram{\ldots}{f,\ldots}
    \fi
    \if `#1
        \optext{call builtin with given name/value}
        \opisimmtrue
        \opisgudtrue
        \stackdiagram{?,\ldots}{?,\ldots}
    \fi
    \if M#1
        \optext{call math operation with given name/value}
        \opisimmtrue
        \opisgudtrue
        \stackdiagram{?,\ldots}{?,\ldots}
    \fi

    % Memory management functions
    \if F#1
        \optext{free x bytes from p}
        \stackdiagram{x,p,\ldots}{\ldots}
    \fi
    \if R#1
        \optext{reallocate p to x bytes}
        \stackdiagram{x,p,\ldots}{p,\ldots}
    \fi

    % Comparisons
    \if9#1
        \optext{compare and swap x and y at p, push success}
        \stackdiagram{y,x,p,\ldots}{z,\ldots}
    \fi
    \if U#1
        \optext{compare x to y unsigned and set z such that x o y is z o 0}
        \opisgudtrue
        \stackdiagram{y,x,\ldots}{z,\ldots}
    \fi
    \if ?#1
        \optext{compare x to y and set z such that x o y is z o 0}
        \opisgudtrue
        \stackdiagram{x,y,\ldots}{z,\ldots}
    \fi
    \ifx#1\textasciitilde
        \optext{compare a to b within an immediate error}
        \opisgudtrue
        \opisimmtrue
        \stackdiagram{a,b,\ldots}{c,\ldots}
    \fi

    % Syntactical "instructions"
    \ifx#1\textvisiblespace
        \optext{do nothing}
        \opisgudtrue
        \stackdiagram{\ldots}{\ldots}
    \fi
    \ifx#1\#
        \optext{comment}
        \opisgudtrue
        \stackdiagram{ }{ }
    \fi
    \ifx#1\P
        \optext{mark a beat for relative branching}
        \opisgudtrue
        \stackdiagram{\ldots}{\ldots}
    \fi
    \if :#1
        \optext{label line}
        \opisgudtrue
        \stackdiagram{\ldots}{\ldots}
    \fi
    \if '#1
        \optext{breakpoint}
        \opisgudtrue
        \stackdiagram{ }{ }
    \fi
    \if "#1
        \optext{change section}
        \opisgudtrue
        \stackdiagram{ }{ }
    \fi
    \ifx#1\$
        \optext{push constant value}
        \opisimmtrue
        \opisgudtrue
        \stackdiagram{\ldots}{\star{},\ldots}
    \fi

    % Stack manipulation instructions
    \if L#1
        \optext{roll stack by immediate places}
        \opisgudtrue
        \stackdiagram{z,\cdots,y,x,\ldots}{x,z,\cdots,y,\ldots}
        \opisimmtrue
    \fi
    \if T#1
        \optext{push top of stack down by immediate places}
        \opisgudtrue
        \stackdiagram{z,y,\cdots,x,\ldots}{z,\cdots,x,y,\ldots}
        \opisimmtrue
    \fi
    \if e#1
        \optext{rotate stack}
        \opisgudtrue % named based on shape (take bottom to top)
        \stackdiagram{z,y,x,\ldots}{x,z,y,\ldots}
    \fi
    \if h#1
        \optext{stack over}
        \opisgudtrue
        \stackdiagram{y,x,\ldots}{x,y,x,\ldots}
    \fi
    \if k#1
        \optext{stack pop}
        \opisgudtrue
        \stackdiagram{x,\ldots}{\ldots}
    \fi
    \if t#1
        \optext{stack swap}
        \opisgudtrue % named after the Thrush combinator
        \stackdiagram{y,x,\ldots}{x,y,\ldots}
    \fi
    \if w#1
        \optext{stack duplicate}
        \opisgudtrue % named after the Warbler combinator
        \stackdiagram{x,\ldots}{x,x,\ldots}
    \fi
    \if (#1
        \optext{set x to depth of stack}
        \stackdiagram{\ldots}{x,\ldots}
    \fi
    \if )#1
        \optext{pack x elements of stack into array p}
        \stackdiagram{x,p,\ldots}{p,\ldots}
    \fi

    % Integer operations
    \if V#1
        \optext{bitwise OR x and y}
        \opisgudtrue
        \stackdiagram{y,x,\ldots}{x\vee{}y,\ldots}
    \fi
    \if l#1
        \optext{bit clear y from x to z}
        \opisgudtrue
        \stackdiagram{y,x,\ldots}{(x\wedge\neg{}y)}
    \fi
    \ifx#1\_
        \optext{negate x}
        \opisgudtrue
        \stackdiagram{x,\ldots}{-x,\ldots}
    \fi
    \if +#1
        \optext{integer add}
        \opisgudtrue
        \stackdiagram{y,x,\ldots}{x+y,\ldots}
    \fi
    \if -#1
        \optext{integer subtract}
        \opisgudtrue
        \stackdiagram{y,x,\ldots}{x-y,\ldots}
    \fi
    \if *#1
        \optext{integer multiply}
        \opisgudtrue
        \stackdiagram{y,x,\ldots}{x\times{}y,\ldots}
    \fi
    \if /#1
        \optext{integer divide}
        \opisgudtrue
        \stackdiagram{y,x,\ldots}{x\div{}y,\ldots}
    \fi
    \ifx#1\%
        \optext{integer modulo}
        \opisgudtrue
        \stackdiagram{y,x,\ldots}{x\ \textrm{mod}\ y,\ldots}
    \fi
    \ifx#1\textbackslash
        \optext{integer remainder}
        \opisgudtrue
        \stackdiagram{y,x,\ldots}{x\%y,\ldots}
    \fi
    \if |#1
        \optext{integer absolute value}
        \opisgudtrue
        \stackdiagram{x,\ldots}{|x|,\ldots}
    \fi
    \ifx#1\&
        \optext{set z to the bitwise and of x and y}
        \opisgudtrue
        \stackdiagram{x,y,\ldots}{x\wedge{}y}
    \fi
    \ifx#1\textasciicircum
        \optext{set z to the bitwise xor of x and y}
        \opisgudtrue
        \stackdiagram{x,y,\ldots}{x\oplus{}y}
    \fi
    \if !#1
        \optext{set y to the bitwise not of x}
        \opisgudtrue
        \stackdiagram{x,\ldots}{y,\ldots}
    \fi
    \if [#1
        \optext{rotate x right by immediate bits}
        \opisimmtrue
        \stackdiagram{x,\ldots}{x,\ldots}
    \fi
    \if ]#1
        \optext{arithmetic right shift x by immediate bits}
        \opisimmtrue
        \stackdiagram{x,\ldots}{x,\ldots}
    \fi
    \if u#1
        \optext{logical shift x by immediate bits right}
        \opisimmtrue
        \stackdiagram{x,\ldots}{x,\ldots}
    \fi

    % Float operations
    \if j#1
        \optext{negate a}
        \opisgudtrue
        \stackdiagram{a,\ldots}{-a,\ldots}
    \fi
    \if m#1
        \optext{floating point difference}
        \opisgudtrue
        \stackdiagram{b,a,\ldots}{a-b}
    \fi
    \if p#1
        \optext{floating point multiply}
        \opisgudtrue
        \stackdiagram{b,a,\ldots}{a\times{}b,\ldots}
    \fi
    \if q#1
        \optext{floating point divide}
        \opisgudtrue
        \stackdiagram{b,a,\ldots}{b\over{}a,\ldots}
    \fi
    \if s#1
        \optext{floating point add}
        \opisgudtrue
        \stackdiagram{b,a,\ldots}{b+a,\ldots}
    \fi
    \if v#1
        \optext{floating point absolute value}
        \opisgudtrue
        \stackdiagram{a,\ldots}{|a|,\ldots}
    \fi

    % Conversion instructions
    \if .#1
        \optext{convert x to a float}
        \opisgudtrue
        \stackdiagram{x,\ldots}{a,\ldots}
    \fi
    \if E#1
        \optext{round a to integer and store in x}
        \opisgudtrue
        \stackdiagram{a,\ldots}{x,\ldots}
    \fi
    \if W#1
        \optext{truncate x to 32 bits, then sign extend}
        \opisgudtrue
        \stackdiagram{x,\ldots}{x,\ldots}
    \fi
    \if S#1
        \optext{truncate x to 16 bits, then sign extend}
        \opisgudtrue
        \stackdiagram{x,\ldots}{x,\ldots}
    \fi
    \if O#1
        \optext{truncate x to 8 bits, then sign extend}
        \opisgudtrue
        \stackdiagram{x,\ldots}{x,\ldots}
    \fi

    % Load instructions
    \if1#1
        \optext{load byte x from p}
        \opisgudtrue
        \stackdiagram{p,\ldots}{x,\ldots}
    \fi
    \if2#1
        \optext{load short x from p}
        \opisgudtrue
        \stackdiagram{p,\ldots}{x,\ldots}
    \fi
    \if3#1
        \optext{load int x from p}
        \opisgudtrue
        \stackdiagram{p,\ldots}{x,\ldots}
    \fi
    \if4#1
        \optext{load word x from p}
        \opisgudtrue
        \stackdiagram{p,\ldots}{x,\ldots}
    \fi
    \if G#1
        \optext{load int at p plus immediate offset in x}
        \stackdiagram{p,\ldots}{x,\ldots}
        \opisimmtrue
    \fi
    \if H#1
        \optext{load short at p plus immediate offset in x}
        \stackdiagram{p,\ldots}{x,\ldots}
        \opisimmtrue
    \fi
    \if o#1
        \optext{load byte at p plus immediate offset in x}
        \stackdiagram{p,\ldots}{x,\ldots}
        \opisimmtrue
    \fi

    % Store instructions
    \if5#1
        \optext{store byte x to p}
        \opisgudtrue
        \stackdiagram{x,p,\ldots}{\ldots}
    \fi
    \if6#1
        \optext{store short x to p}
        \opisgudtrue
        \stackdiagram{x,p,\ldots}{\ldots}
    \fi
    \if7#1
        \optext{store int x to p}
        \opisgudtrue
        \stackdiagram{x,p,\ldots}{\ldots}
    \fi
    \if8#1
        \optext{store word x to p}
        \opisgudtrue
        \stackdiagram{x,p,\ldots}{\ldots}
    \fi

    % Register instructions
    \if A#1
        \optext{store x in rA}
        \opisgudtrue
        \stackdiagram{x,\ldots}{\ldots}
    \fi
    \if B#1
        \optext{store x in rB}
        \opisgudtrue
        \stackdiagram{x,\ldots}{\ldots}
    \fi
    \if C#1
        \optext{store x in rC}
        \opisgudtrue
        \stackdiagram{x,\ldots}{\ldots}
    \fi
    \if I#1
        \optext{store x in rI}
        \opisgudtrue
        \stackdiagram{x,\ldots}{\ldots}
    \fi
    \if N#1
        \optext{store x in rN}
        \opisgudtrue
        \stackdiagram{x,\ldots}{\ldots}
    \fi
    \if X#1
        \optext{store x in rX}
        \opisgudtrue
        \stackdiagram{x,\ldots}{\ldots}
    \fi
    \if Y#1
        \optext{store x in rY}
        \opisgudtrue
        \stackdiagram{x,\ldots}{\ldots}
    \fi
    \if Z#1
        \optext{store x in rZ}
        \opisgudtrue
        \stackdiagram{x,\ldots}{\ldots}
    \fi
    \if a#1
        \optext{load x from rA}
        \opisgudtrue
        \stackdiagram{\ldots}{x,\ldots}
    \fi
    \if b#1
        \optext{load x from rB}
        \opisgudtrue
        \stackdiagram{\ldots}{x,\ldots}
    \fi
    \if c#1
        \optext{load x from rC}
        \opisgudtrue
        \stackdiagram{\ldots}{x,\ldots}
    \fi
    \if i#1
        \optext{load x from rI}
        \opisgudtrue
        \stackdiagram{\ldots}{x,\ldots}
    \fi
    \if n#1
        \optext{load x from rN}
        \opisgudtrue
        \stackdiagram{\ldots}{x,\ldots}
    \fi
    \if x#1
        \optext{load x from rX}
        \opisgudtrue
        \stackdiagram{\ldots}{x,\ldots}
    \fi
    \if y#1
        \optext{load x from rY}
        \opisgudtrue
        \stackdiagram{\ldots}{x,\ldots}
    \fi
    \if z#1
        \optext{load x from rZ}
        \opisgudtrue
        \stackdiagram{\ldots}{x,\ldots}
    \fi

    % Branch instructions
    \if0#1
        \optext{branch to immediate if x is not zero}
        \opisgudtrue
        \stackdiagram{x,\ldots}{\ldots}
    \fi
    \if <#1
        \optext{branch to immediate if x is less than zero}
        \opisgudtrue
        \opisimmtrue
        \opisfuntrue
        \stackdiagram{x,\ldots}{\ldots}
    \fi
    \ifx#1\{
        \optext{branch to immediate if x is less than or equal to zero}
        \opisgudtrue
        \opisimmtrue
        \opisfuntrue
        \stackdiagram{x,\ldots}{\ldots}
    \fi
    \if =#1
        \optext{branch to immediate if x is zero}
        \opisgudtrue
        \opisimmtrue
        \opisfuntrue
        \stackdiagram{x,\ldots}{\ldots}
    \fi
    \ifx#1\}
        \optext{branch to immediate if x is greater or equal to zero}
        \opisgudtrue
        \opisimmtrue
        \opisfuntrue
        \stackdiagram{x,\ldots}{\ldots}
    \fi
    \if >#1
        \optext{branch to immediate if x is greater than zero}
        \opisgudtrue
        \opisimmtrue
        \opisfuntrue
        \stackdiagram{x,\ldots}{\ldots}
    \fi
    \if ,#1
        \optext{jump to immediate without pushing return address}
        \opisgudtrue
        \opisimmtrue
        \opisfuntrue
        \stackdiagram{\ldots}{\ldots}
    \fi
    \if ;#1
        \optext{return from subroutine}
        \opisgudtrue
        \opisfuntrue
        \stackdiagram{f,\ldots}{\ldots}
    \fi

    \ifnoopdef
        \tikzset{every node/.style={fill=gray}}
        \renewcommand{\optextdescr}{?}
        \stackdiagram{?}{?}
    \fi

    \ifopisgud
        \tikzset{every node/.style={color=black!25!green}}
    \fi

    \ifopisbad
        \tikzset{every node/.style={color=black!25!red}}
    \fi

    \ifopisfun
        \ifopisimm
            \tikzset{->/.style={arrows={Arc Barb[reversed,length=3pt]-Latex[length=5pt]}, double distance=1pt}}
        \else
            \tikzset{->/.style={arrows={-Latex[length=5pt]}, double distance=1pt}}
        \fi
    \else
        \ifopisimm
            \tikzset{->/.style={arrows={Arc Barb[reversed,length=3pt]-Latex[length=5pt]}}}
        \else
            \tikzset{->/.style={arrows={-Latex[length=5pt]}}}
        \fi
    \fi

    \draw[->]
            (before) --
            node[above] {\texttt{#1}}
            node[text width=1cm,align=center,below]
                {\texttt{\tiny\begin{spacing}{0.6}\optextdescr\end{spacing}}}
            (after);
\end{tikzpicture}
}


\begin{document}

\begin{table}[h]
    \renewcommand{\arraystretch}{0}
    \centering
    %\caption{All Code Section Instructions In ASCII Order}
    \begin{tabularx}{\textwidth}{@{}XXXXXXXXXXXXXXXX@{}} %\toprule
        \opsumof{0} & \opsumof{1} & \opsumof{2} & \opsumof{3} &
        \opsumof{4} & \opsumof{5} & \opsumof{6} & \opsumof{7} \\

        \opsumof{8} & \opsumof{9} & \opsumof{A} & \opsumof{B} &
        \opsumof{C} & \opsumof{D} & \opsumof{E} & \opsumof{F} \\

        \opsumof{G} & \opsumof{H} & \opsumof{I} & \opsumof{J} &
        \opsumof{K} & \opsumof{L} & \opsumof{M} & \opsumof{N} \\

        \opsumof{O} & \opsumof{P} & \opsumof{Q} & \opsumof{R} &
        \opsumof{S} & \opsumof{T} & \opsumof{U} & \opsumof{V} \\

        \opsumof{W} & \opsumof{X} & \opsumof{Y} & \opsumof{Z} &
        \opsumof{a} & \opsumof{b} & \opsumof{c} & \opsumof{d} \\

        \opsumof{e} & \opsumof{f} & \opsumof{g} & \opsumof{h} &
        \opsumof{i} & \opsumof{j} & \opsumof{k} & \opsumof{l} \\

        \opsumof{m} & \opsumof{n} & \opsumof{o} & \opsumof{p} &
        \opsumof{q} & \opsumof{r} & \opsumof{s} & \opsumof{t} \\

        \opsumof{u} & \opsumof{v} & \opsumof{w} & \opsumof{x} &
        \opsumof{y} & \opsumof{z} & \opsumof{\_} & \opsumof{.} \\

        \opsumof{+} & \opsumof{-} & \opsumof{*} & \opsumof{/} &
        \opsumof{\%} & \opsumof{\textbackslash} & \opsumof{|} & \opsumof{\$} \\

        \opsumof{@} & \opsumof{<} & \opsumof{\{} & \opsumof{=} &
        \opsumof{\}} & \opsumof{>} & \opsumof{,} & \opsumof{;} \\

        \opsumof{\&} & \opsumof{\textasciicircum} & \opsumof{!} & \opsumof{[} &
        \opsumof{]} & \opsumof{?} & \opsumof{\textasciitilde} & \opsumof{:} \\

        \opsumof{(} & \opsumof{)} & \opsumof{'} & \opsumof{"} &
        \opsumof{`} & \opsumof{\textvisiblespace} & \opsumof{\#} & \opsumof{\P} \\
    \end{tabularx}
\end{table}

\end{document}
