1

Is it possible to match numbers, prepended by a %, e.g. variable %101 and surround every occurrence of that number in color-matched boxes? I am trying to make it easier for readers to follow the usages of a numbered variable.

llvm.tex:

\lstinputlisting[language=llvm,style=nasm]{IR/voidandlatecasts.ll}

IR/voidandlatecasts.ll:

  %17 = tail call i8* @kvmalloc_node(i64 1808, i32 3520, i32 %16) #15
  %18 = bitcast i8* %17 to %struct.mapped_device*
  ; ...
  %101 = getelementptr inbounds i8, i8* %17, i64 224
  %102 = bitcast i8* %101 to void (%struct.work_struct*)**
  store void (%struct.work_struct*)* @dm_wq_work, void (%struct.work_struct*)** %102, align 8

Which results in:

code lstlisting

For example: The store instruction uses variable %102 which is defined one line above. Variable %102 uses %101 which is also defined one line above. My goal is to make it more obvious and highlight those variables in the same colors so one can better see where the definitions and the usages are.

The idea of boxed-in variables with the box frame in matching colors is an idea. I'm looking for any possible ways to at least highlight it from the code listing.

1

With moredelim=**[is][\color{blue}]{&}{&}

code:

\documentclass[12pt,a4paper]{report}
\usepackage{geometry} 
\usepackage{xcolor}
\usepackage{listings} 


\definecolor{comment}{RGB}{0,128,0}     % dark green
\definecolor{string}{RGB}{255,0,0}      % red
\definecolor{instruction}{RGB}{0,0,255} % blue
\definecolor{directive}{RGB}{128,0,128} % purple
\definecolor{register}{RGB}{128,0,0}    % dark red

\lstdefinestyle{nasm}{
    commentstyle=\color{comment},
    stringstyle=\color{string},
    keywordstyle=\color{instruction},
    keywordstyle=[2]\color{directive},
    keywordstyle=[3]\color{register},
    basicstyle=\footnotesize\ttfamily,
    numbers=left,
    numberstyle=\tiny,
    numbersep=5pt,
    frame=lines,
    breaklines=true,
    postbreak=\raisebox{0ex}[0ex][0ex]{\space\ensuremath{\hookrightarrow}},
    showstringspaces=false,
    upquote=true,
    tabsize=8,
    linewidth=10.7cm,
    moredelim=**[is][\color{blue}]{&}{&}
}


\begin{document}

\begin{lstlisting}[language=llvm,style=nasm]
  &%17& = tail call i8* @kvmalloc_node(i64 1808, i32 3520, i32 %16) #15
  &%18& = bitcast i8* %17 to %struct.mapped_device*
  ; ...
  &%101& = getelementptr inbounds i8, i8* %17, i64 224
  &%102& = bitcast i8* %101 to void (%struct. work_struct*)**
  store void (%struct.work_struct*)* @dm_wq_work, void (%struct.work_struct*)** %102, align 8
\end{lstlisting}

\end{document}

output:

enter image description here

| improve this answer | |
  • I like the idea. I looked up the moredelim option and it seems suitable for my purposes. – CRoemheld Oct 21 at 0:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.