Godbolt Compiler Explorer

Godbolt Compiler Explorer

Ever want to see what assembly code your compiler is really generating?

There are a few reasons to want to know this. Firstly is correctness. Sometimes obscure bugs can crop up when you switch from one compiler to another, from one set of optimization settings to another, from debug to release, or even from one compiler version to another.

The second is optimization. What addressing mode(s) are being used, are there data hazards that are slowing things, are simd instructions being used? Game and high performance compute companies still spend time pulling every cycle they can out of a routine.

It used to be you had to compile and then pull the executable into a debugger and examine the code. Now, there’s an online tool that can quickly do the same. Enter the Godbolt compiler explorer.

It allows you to type in C/C++ code, select from a whole host of compilers, enter custom compiler flags, then show you the generated output.

In the case above, I wanted to verify C/C++ short-circuit comparison behavior of a variable against NULL and then dereference in the same conditional. If the compare against NULL doesn’t execute and exit before the dereference, a NULL pointer exception/segfault will result. According to C/C++ rules, there should be an early out after the comparison against NULL – and sure enough – on line 8 and 9 we see the test and exit.

Anyway – it’s a neat online tool you might give a try to learn more about the assembly your C/C++ code turns into.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.