A GCC build mis-expands a macro and an inline asm block returns garbage; how do -E, -dD, -save-temps and -S -fverbose-asm show what the compiler received and emitted?
answer
- read what the compiler read
- -E -dM lists final macros
- line markers show the defining header
- keep .ii and .s together
- verbose asm maps operands
basics
~20 sRerun the exact compile line with -E, adding -dD or -dM, to see expanded code and macro definitions, or with -save-temps to keep the .ii and .s. Then read -S -fverbose-asm output for the registers GCC gave the inline asm.
solid answer
~50 sBoth bugs are about a gap between the source and what the compiler saw, so read the compiler's own intermediate files. Take the **exact compile command** of the failing file and add `-E`: the output is the translation unit after expansion, with line markers naming the header each line came from. `-E -dM` lists every macro defined at the end, including predefined ones, and `-E -dD` interleaves the `#define`s with the code so you can see which definition was active where. `-save-temps` keeps the `.ii` and `.s` from a normal build. For the asm, `-S -fverbose-asm` writes assembly with source lines and operand hints as comments, so you can check which registers GCC substituted for each constraint and whether a value it keeps live sits in a register the asm overwrites without declaring a clobber.
code
bash · 5 lines# exact flags of the failing file, plus one of:
g++ -std=c++20 -O2 -Iinclude -DFAST_PATH -E -dD parser.cpp > parser.dD.ii
g++ -std=c++20 -O2 -Iinclude -DFAST_PATH -E -dM parser.cpp | grep FAST_PATH
g++ -std=c++20 -O2 -Iinclude -DFAST_PATH -save-temps=obj -c parser.cpp -o build/parser.o
g++ -std=c++20 -O2 -Iinclude -DFAST_PATH -S -fverbose-asm parser.cpp -o parser.sgo deeper
Know that -E shows the code after macro expansion and -S shows the generated assembly.
Use -E -dM and -E -dD to find which macro definition was active, and -save-temps to keep the intermediate files of a real build.
Reproduce the exact compile line, read -fverbose-asm output for operand and register choices, and recognise an undeclared clobber that only bites at -O2.
Make the exact compile commands and intermediate files easy to obtain in the build, since every hard compiler-level investigation starts from them.
## Read what the compiler actually read A macro bug and an inline asm bug share a shape: the source looks right, but the compiler received or produced something else. Guessing from the source is slow. GCC can show you each intermediate form, as long as you reproduce **the exact command line** of the failing file. Copy it from the build log, or from the compilation database a build system writes, because a different `-D`, `-I` or `-std` produces a different translation unit. ## Macros: the preprocessed translation unit Add `-E` to the command and redirect the output: - `g++ <flags> -E foo.cpp > foo.ii` gives the fully expanded code. Search it for the expression that misbehaves and read what it became. - **Line markers** such as `# 42 "include/config.h" 1` say which file and line each chunk came from. `-P` removes them, which is easier to read but loses that trail. - `-E -dM` prints only `#define` lines for **every macro defined at the end** of preprocessing, including GCC's predefined ones. It answers "is `NDEBUG` set?" or "what is `__cplusplus` here?". - `-E -dD` prints the `#define` directives **and** the preprocessed code, in order, so you can see a later header redefining a macro. - `-H` lists each header as it is opened, indented by depth, to find which copy of a header was used. One trap from the manual: `-dM` **without** `-E` is not the macro dump at all; it is read as a request for an RTL dump. To find **where** a surprising definition came from, search the `-E -dD` output for the macro's `#define`: the nearest line marker above it names the header and line. If the macro appears twice, the later definition is the one in force for the code after it, and GCC normally warns about a redefinition with a different body, unless that header is a system header, where the warning is suppressed. A classic result, from a `SQUARE(x)` macro written without parentheses: ```cpp #define SQUARE(x) x * x int n = 3; int r = SQUARE(n + 1); // after -E: int r = n + 1 * n + 1; so r == 7, not 16 ``` ## Keeping every intermediate: -save-temps `-save-temps` keeps what the driver normally deletes and names the files after the output: `g++ -save-temps -c foo.cpp` leaves `foo.ii`, `foo.s` and `foo.o`. Variants control where they go: 1. `-save-temps=cwd` writes them in the current directory; 2. `-save-temps=obj` writes them beside the object named by `-o`, which keeps out-of-tree builds tidy. Because the files come from the real build line, they are also what you attach to a compiler bug report. ## Inline asm: read the .s file Extended asm hands the compiler a template plus **constraints**; GCC picks registers and pastes them in. When the result is garbage, look at what it pasted: - `g++ <flags> -S -fverbose-asm foo.cpp -o foo.s` adds comments with the compiler version and options, the **source lines** next to the instructions, and **hints about which expressions each operand holds**. - On ELF targets the inline block is bracketed by `#APP` and `#NO_APP` comments, so you can find it quickly. - On x86, `-masm=intel` switches the output to the `intel` dialect; the default is `att`, and Darwin does not support `intel`. The same option changes the dialect GCC expects inside asm templates, so set it for the whole translation unit or not at all. Questions to ask of the output: - Did each output operand land in the register the instruction really writes? An instruction such as `cpuid` overwrites registers you did not name as outputs. - Does the compiler keep a live value in a register the asm writes but does not list as a **clobber**? That is the usual cause of garbage after the block, and it often appears only at `-O2`, when register allocation changes. - Did the optimizer delete or move the block? An asm with outputs that are never used, and without `volatile`, may be removed. ## A short procedure 1. Reproduce the exact compile line. 2. Add `-save-temps` and keep `.ii` and `.s`. 3. For the macro: search the `.ii`, then `-E -dD` to see which definition won. 4. For the asm: read the `.s` with `-fverbose-asm` at the same `-O` level as the failing build. 5. Fix the source, then diff the new `.ii` or `.s` against the old one to confirm.
- Why must you reuse the exact compile command when inspecting -E output?The preprocessed translation unit depends on every `-D`, `-U`, `-I`, `-std` and target flag. A hand-typed command with one missing define can expand the macro correctly and hide the bug. Copy the command from the build log or compilation database.
- Why might an inline asm bug appear at -O2 but not at -O0?At `-O0` most values live in memory between statements, so a register the asm overwrites without declaring a clobber holds nothing important. At `-O2` the register allocator keeps values in registers across the block, and the undeclared clobber destroys one.
- Passing -dM without -E prints the macro list.
- Reading the source carefully is enough; the preprocessed output adds nothing.
- -save-temps changes the generated code, so its files cannot be trusted.
- Inline asm is copied verbatim, so the compiler's register choices never matter.
- An asm bug that disappears at -O0 must be a compiler bug.