Poor Man's Compiler Explorer

If you do any sort of C++ programming, you may know about Compiler Explorer. Written by Matthew Godbolt, it has become an indespensible tool in determining what your compiler is doing. If you aren’t comfortable with using the running version he has setup or can’t be bothered with installing the dependancies to run your own, then you have come to the right place! Using some command-line tools that are likely already installed on your system, you can setup an environment that is highly useable and configurable.

A Few One-Liners

You will need a few tools to get this running, but they are more accessible than setting up the Compiler Explorer project on a server or locally. You will need:

First, I will show you the one-liner that will “cat” our generated assembly to the command line.

while inotifywait -e modify main.cpp || true; do gcc -O0 -S main.cpp; cat main.s; done

Now, let’s view the generated assembly using “less”. This will allow us to scroll through a longer output. Closing “less” will not exit the loop. So when we modify “main.cpp”, less will open again with the generated assembly.

while inotifywait -e modify main.cpp || true; do gcc -O0 -S main.cpp; less main.s; done

A More Robust Solution

Now, let’s view the generated assembly using “vim”. We could simply replace “less” with “vim” in the previous snippet, but in order to see the updated assembly, we will have to close vim. This is a little combersome. So let’s use Tmux to setup an easy to use environment.


# poor-mans-compiler-explorer.sh

#!/bin/bash
sname="compiler-explorer"
send_keys="tmux send-keys -t $sname"
working_dir=/tmp/compiler-explorer
working_file=main
compiler_command="g++ -O0 -S"
vim_command="vim $working_file.cpp -c \"vsp $working_file.s\" -c \"au FocusGained,BufEnter * :silent! !\""
watch_command="while inotifywait -e modify $working_file.cpp || true; do $compiler_command $working_file.cpp; done"

###########################################################
# Setup Directory
###########################################################
mkdir -p $working_dir
cd $working_dir
touch $working_file.cpp
touch $working_file.s

###########################################################
# Setup Tmux
###########################################################
tmux start-server;
TMUX= tmux new-session -d -s $sname -n $sname

# Main Watch Loop Window
$send_keys:0.0 "$watch_command" C-m

# Vim Window
tmux new-window -t $sname:1 -n vim
tmux select-layout -t $sname:1 tiled
$send_keys:1.0 "$vim_command" C-m

tmux select-window -t $sname:1
tmux select-pane -t 0

if [ -z "$TMUX" ]; then
    tmux -u attach-session -t $sname
else
    tmux -u switch-client -t $sname
fi

Now let’s make the script executable and see how it runs! ``

$ chmod +x poor-mans-compiler-explorer.sh
$ ./poor-mans-compiler-explorer.sh

When first running the script, the vim splits will both be blank. The right split holds the source file and the left split holds the assembly file. Try writing a function in the right split (the source file). Then switch to the left split and you will see the generated assembly.