Thu Nov 24 23:32:00 UTC 2022

Command line Compiler Explorer

A Linux command line compiler explorer script inspired by this article by Matt Godbolt. See the repo that generated this page.

Installation

sudo apt update
sudo apt install g++ curl entr gedit

git clone [email protected]:germs-dev/explore
cd explore/

Example usage

Below I’m splitting my terminal with tmux and running the main script when the source changes using entr.

ls main.cxx | entr -c bash explore.sh

But you can also use this wrapper script to edit in gedit.

bash run.sh

The bash script explore.sh

This command really is the core of godbolt.org, edit your compiler flags here.

readonly CXXFLAGS="-std=c++23 -O1"

g++ main.cxx $CXXFLAGS -S -o - \
    | c++filt \
    | grep -vE "\s+\.|^[0-9]:"

The source file main.cxx

#include <cmath>
#include <cstddef>

int func(const size_t max) {

  int sum = 0;

  for (auto i = 0uz; i < max; ++i)
    sum += i;

  return sum;
}

int main() {
  const auto param = std::pow(2, 13);
  return func(param * 3);
}

The generated assembly

Note the call to std::pow has been optimised out even at -O1.

func(unsigned long):
.LFB1178:
    endbr64
    testq   %rdi, %rdi
    movl    $0, %eax
    movl    $0, %edx
.L3:
    addl    %eax, %edx
    addq    $1, %rax
    cmpq    %rax, %rdi
.L1:
    movl    %edx, %eax
    ret
.L4:
    movl    $0, %edx
.LFE1178:
main:
.LFB1179:
    endbr64
    movl    $24576, %edi
    call    func(unsigned long)
    ret
.LFE1179: