### Install GCC Prerequisites Source: https://gcc-newbies-guide.readthedocs.io/en/latest/_sources/getting-started.rst.txt Installs all necessary development tools and libraries required for building GCC. This should be done once. ```bash sudo apt install perl gawk binutils gcc-multilib \ python3 python3-pip gzip make tar zstd autoconf automake \ gettext gperf dejagnu autogen guile-3.0 expect tcl flex texinfo \ git diffutils patch git-email ``` -------------------------------- ### Example Source Code Source: https://gcc-newbies-guide.readthedocs.io/en/latest/inside-cc1.html This is an example of source code before lexing. ```c return (b - c) * d; ``` -------------------------------- ### Compile a simple C program Source: https://gcc-newbies-guide.readthedocs.io/en/latest/_sources/debugging.rst.txt This is a basic C program used as an example for compilation. ```c #include int main() { printf("Hello world\n"); return 0; } ``` -------------------------------- ### Run Multiple Test Files and ABIs Source: https://gcc-newbies-guide.readthedocs.io/en/latest/_sources/working-with-the-testsuite.rst.txt Execute tests from different .exp files and for multiple ABIs simultaneously. This example runs tests for both 32-bit and 64-bit ABIs. ```bash make check-gcc \ RUNTESTFLAGS="--target_board=unix\{-m32,-m64\} dg.exp='pr10474.c pr15698*.c' tree-ssa.exp=20030530-2.c" ``` -------------------------------- ### C Code Example for Optimization Analysis Source: https://gcc-newbies-guide.readthedocs.io/en/latest/_sources/how-did-my-code-get-optimized.rst.txt This C code serves as an example to demonstrate GCC optimization. It includes a recursive-like structure through function calls and a loop, which are targets for compiler optimizations. ```c static int test (int a, int b, int c, int d) { if (a) return (b - c) * d; else return (c - b) * d; } static int simple_loop (int n, int a, int b, int c, int d) { int result; int i; for (i = 0; i < n) result += test (a, b, c, d); return result; } int fn_with_constants (void) { return simple_loop (20, 1, 7, 4, 3); } ``` -------------------------------- ### Configure and Build GCC Source: https://gcc-newbies-guide.readthedocs.io/en/latest/_sources/getting-started.rst.txt Configures the GCC build with a specified installation prefix and then initiates a parallel bootstrap build. Adjust '-j16' based on your system's core count. ```bash mkdir build && cd build ../sources/configure --prefix "/path/to/gcc-control/install" make -j16 bootstrap ``` -------------------------------- ### Linker Command Line Example Source: https://gcc-newbies-guide.readthedocs.io/en/latest/_sources/binaries-and-processes.rst.txt This command shows the full linker invocation by GCC, including input files, libraries, and various linker options. It is generated after the assembler has created object files. ```sh /usr/libexec/gcc/x86_64-redhat-linux/10/collect2 \ -plugin /usr/libexec/gcc/x86_64-redhat-linux/10/liblto_plugin.so \ -plugin-opt=/usr/libexec/gcc/x86_64-redhat-linux/10/lto-wrapper \ -plugin-opt=-fresolution=/tmp/cchv3sYK.res \ -plugin-opt=-pass-through=-lgcc \ -plugin-opt=-pass-through=-lgcc_s \ -plugin-opt=-pass-through=-lc \ -plugin-opt=-pass-through=-lgcc \ -plugin-opt=-pass-through=-lgcc_s \ --build-id \ --no-add-needed \ --eh-frame-hdr \ --hash-style=gnu \ -m elf_x86_64 \ -dynamic-linker \ /lib64/ld-linux-x86-64.so.2 \ -o hello \ /usr/lib/gcc/x86_64-redhat-linux/10/../../../../lib64/crt1.o \ /usr/lib/gcc/x86_64-redhat-linux/10/../../../../lib64/crti.o \ /usr/lib/gcc/x86_64-redhat-linux/10/crtbegin.o \ -L/usr/lib/gcc/x86_64-redhat-linux/10 \ -L/usr/lib/gcc/x86_64-redhat-linux/10/../../../../lib64 \ -L/lib/../lib64 \ -L/usr/lib/../lib64 \ -L/usr/lib/gcc/x86_64-redhat-linux/10/../../.. \ /tmp/ccXNu6jN.o \ -lgcc \ --push-state \ --as-needed \ -lgcc_s \ --pop-state \ -lc \ -lgcc \ --push-state \ --as-needed \ -lgcc_s \ --pop-state \ /usr/lib/gcc/x86_64-redhat-linux/10/crtend.o \ /usr/lib/gcc/x86_64-redhat-linux/10/../../../../lib64/crtn.o ``` -------------------------------- ### GIMPLE-SSA Form Example Source: https://gcc-newbies-guide.readthedocs.io/en/latest/inside-cc1.html This snippet shows the GIMPLE-SSA form of a simple C function after optimization passes. It illustrates basic blocks, conditional jumps, variable versioning, and return statements. ```c ;; Function test (test, funcdef_no=0, decl_uid=1933, cgraph_uid=1, symbol_order=0) test (int a, int b, int c, int d) { int _1; int _2; int _3; int _8; int _9; [local count: 1073741824]: if (a_4(D) != 0) goto ; [50.00%] else goto ; [50.00%] [local count: 536870913]: _1 = b_6(D) - c_5(D); _9 = _1 * d_7(D); goto ; [100.00%] [local count: 536870913]: _2 = c_5(D) - b_6(D); _8 = _2 * d_7(D); [local count: 1073741824]: # _3 = PHI <_9(3), _8(4)> return _3; } ``` -------------------------------- ### Macro definition for compiler options Source: https://gcc-newbies-guide.readthedocs.io/en/latest/debugging.html Example of autogenerated preprocessor macro definitions for compiler options, showing how they map to internal variables. ```c #ifdef GENERATOR_FILE extern int optimize; #else int x_optimize; #define optimize global_options.x_optimize #endif ``` ```c #ifdef GENERATOR_FILE extern int flag_forward_propagate; #else int x_flag_forward_propagate; #define flag_forward_propagate global_options.x_flag_forward_propagate #endif ``` -------------------------------- ### GCC options.h macro definition Source: https://gcc-newbies-guide.readthedocs.io/en/latest/_sources/debugging.rst.txt Example of how autogenerated preprocessor macros are defined in GCC's options.h. ```c++ #ifdef GENERATOR_FILE extern int optimize; #else int x_optimize; #define optimize global_options.x_optimize #endif ``` -------------------------------- ### GDB output for non-existent symbols Source: https://gcc-newbies-guide.readthedocs.io/en/latest/_sources/debugging.rst.txt Example of GDB output when attempting to print variables that are actually macros. ```console (gdb) print optimize No symbol "optimize" in current context. (gdb) print flag_forward_propagate No symbol "flag_forward_propagate" in current context. ``` -------------------------------- ### Verbose Assembler Output with Comments Source: https://gcc-newbies-guide.readthedocs.io/en/latest/_sources/looking-at-the-generated-asm.rst.txt This is an example of verbose assembler output generated with `-fverbose-asm`. It includes compiler version, options, and comments linking assembler instructions to C code. ```asm .file "hello.c" # GNU C17 (GCC) version 10.3.1 20210422 (Red Hat 10.3.1-1) (x86_64-redhat-linux) # compiled by GNU C version 10.3.1 20210422 (Red Hat 10.3.1-1), GMP version 6.2.0, MPFR version 4.1.0-p9, MPC version 1.1.0, isl version none # GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 # options passed: hello.c -mtune=generic -march=x86-64 -fverbose-asm # options enabled: -faggressive-loop-optimizations -fallocation-dce # -fasynchronous-unwind-tables -fauto-inc-dec -fdelete-null-pointer-checks # -fdwarf2-cfi-asm -fearly-inlining -feliminate-unused-debug-symbols # -feliminate-unused-debug-types -ffp-int-builtin-inexact -ffunction-cse # -fgcse-lm -fgnu-unique -fident -finline-atomics -fipa-stack-alignment # -fira-hoist-pressure -fira-share-save-slots -fira-share-spill-slots # -fivopts -fkeep-static-consts -fleading-underscore -flifetime-dse # -fmath-errno -fmerge-debug-strings -fpeephole -fplt # -fprefetch-loop-arrays -freg-struct-return # -fsched-critical-path-heuristic -fsched-dep-count-heuristic # -fsched-group-heuristic -fsched-interblock -fsched-last-insn-heuristic # -fsched-rank-heuristic -fsched-spec -fsched-spec-insn-heuristic # -fsched-stalled-insns-dep -fschedule-fusion -fsemantic-interposition # -fshow-column -fshrink-wrap-separate -fsigned-zeros # -fsplit-ivs-in-unroller -fssa-backprop -fstdarg-opt # -fstrict-volatile-bitfields -fsync-libcalls -ftrapping-math -ftree-cselim # -ftree-forwprop -ftree-loop-if-convert -ftree-loop-im -ftree-loop-ivcanon # -ftree-loop-optimize -ftree-parallelize-loops= -ftree-phiprop # -ftree-reassoc -ftree-scev-cprop -funit-at-a-time -funwind-tables # -fverbose-asm -fzero-initialized-in-bss -m128bit-long-double -m64 -m80387 # -malign-stringops -mavx256-split-unaligned-load # -mavx256-split-unaligned-store -mfancy-math-387 -mfp-ret-in-387 -mfxsr # -mglibc -mieee-fp -mlong-double-80 -mmmx -mno-sse4 -mpush-args -mred-zone # -msse -msse2 -mstv -mtls-direct-seg-refs -mvzeroupper .text .section .rodata .LC0: .string "hello world" .text .globl main .type main, @function main: .LFB0: .cfi_startproc pushq %rbp # .cfi_def_cfa_offset 16 .cfi_offset 6, -16 ``` -------------------------------- ### GCC Test Case with Show Caret and Multiline Output Source: https://gcc-newbies-guide.readthedocs.io/en/latest/_sources/how-to-improve-the-location-of-a-diagnostic.rst.txt Example of a GCC test case using `-fdiagnostics-show-caret` to capture range information. It demonstrates how to use `dg-begin-multiline-output` and `dg-end-multiline-output` for testing multiline diagnostic output. ```c /* { dg-options "-fdiagnostics-show-caret" } */ void test (void) { enum { c1 = "not int", /* { dg-error "14: enumerator value for .c1. is not an integer constant" } */ /* { dg-begin-multiline-output "" } enum { c1 = "not int", ^~~~~~~~~ { dg-end-multiline-output "" } */ c_end }; } ``` -------------------------------- ### PHI Node Example in SSA Source: https://gcc-newbies-guide.readthedocs.io/en/latest/_sources/inside-cc1.rst.txt Shows a PHI node construct in SSA form, which assigns a value to a temporary based on the control flow path taken. ```c # _3 = PHI <_9(3), _8(4)> ``` -------------------------------- ### GCC RTL 'final' dump example Source: https://gcc-newbies-guide.readthedocs.io/en/latest/_sources/inside-cc1.rst.txt This is a dump of the RTL in its 'final' form, suitable for assembler output. It shows the structure of instructions and control flow after optimization passes. ```lisp ;; Function test (test, funcdef_no=0, decl_uid=1933, cgraph_uid=1, symbol_order=0) test Dataflow summary: ;; fully invalidated by EH 0 [ax] 1 [dx] 2 [cx] 4 [si] 5 [di] 8 [st] 9 [st(1)] 10 [st(2)] 11 [st(3)] 12 [st(4)] 13 [st(5)] 14 [st(6)] 15 [st(7)] 17 [flags] 18 [fpsr] 20 [xmm0] 21 [xmm1] 22 [xmm2] 23 [xmm3] 24 [xmm4] 25 [xmm5] 26 [xmm6] 27 [xmm7] 28 [mm0] 29 [mm1] 30 [mm2] 31 [mm3] 32 [mm4] 33 [mm5] 34 [mm6] 35 [mm7] 36 [r8] 37 [r9] 38 [r10] 39 [r11] 44 [xmm8] 45 [xmm9] 46 [xmm10] 47 [xmm11] 48 [xmm12] 49 [xmm13] 50 [xmm14] 51 [xmm15] 52 [xmm16] 53 [xmm17] 54 [xmm18] 55 [xmm19] 56 [xmm20] 57 [xmm21] 58 [xmm22] 59 [xmm23] 60 [xmm24] 61 [xmm25] 62 [xmm26] 63 [xmm27] 64 [xmm28] 65 [xmm29] 66 [xmm30] 67 [xmm31] 68 [k0] 69 [k1] 70 [k2] 71 [k3] 72 [k4] 73 [k5] 74 [k6] 75 [k7] ;; hardware regs used 7 [sp] ;; regular block artificial uses 7 [sp] ;; eh block artificial uses 7 [sp] 16 [argp] ;; entry block defs 0 [ax] 1 [dx] 2 [cx] 4 [si] 5 [di] 7 [sp] 20 [xmm0] 21 [xmm1] 22 [xmm2] 23 [xmm3] 24 [xmm4] 25 [xmm5] 26 [xmm6] 27 [xmm7] 36 [r8] 37 [r9] ;; exit block uses 0 [ax] 7 [sp] ;; regs ever live 0 [ax] 1 [dx] 2 [cx] 4 [si] 5 [di] 17 [flags] ;; ref usage r0={4d,5u} r1={2d,3u} r2={1d,1u} r4={2d,3u} r5={1d,1u} r7={1d,4u} r17={5d,1u} r20={1d} r21={1d} r22={1d} r23={1d} r24={1d} r25={1d} r26={1d} r27={1d} r36={1d} r37={1d} ;; total ref usage 44{26d,18u,0e} in 11{11 regular + 0 call} insns. (note 1 0 7 NOTE_INSN_DELETED) (note 7 1 48 2 [bb 2] NOTE_INSN_BASIC_BLOCK) (note 48 7 2 2 NOTE_INSN_PROLOGUE_END) (note 2 48 6 2 NOTE_INSN_DELETED) (note 6 2 5 2 NOTE_INSN_FUNCTION_BEG) (insn:TI 5 6 9 2 (set (reg/v:SI 0 ax [orig:88 d ] [88]) (reg:SI 2 cx [97])) "test.c":2:1 67 {*movsi_internal} (expr_list:REG_DEAD (reg:SI 2 cx [97]) (nil))) (insn 9 5 10 2 (set (reg:CCZ 17 flags) (compare:CCZ (reg:SI 5 di [94]) (const_int 0 [0]))) "test.c":3:6 7 {*cmpsi_ccno_1} (expr_list:REG_DEAD (reg:SI 5 di [94]) (nil))) (jump_insn 10 9 11 2 (set (pc) (if_then_else (eq (reg:CCZ 17 flags) (const_int 0 [0])) (label_ref 16) (pc))) "test.c":3:6 736 {*jcc} (expr_list:REG_DEAD (reg:CCZ 17 flags) (int_list:REG_BR_PROB 536870916 (nil)))) -> 16) (note 11 10 12 3 [bb 3] NOTE_INSN_BASIC_BLOCK) (insn:TI 12 11 13 3 (parallel [ (set (reg:SI 4 si [89]) (minus:SI (reg/v:SI 4 si [orig:86 b ] [86]) (reg/v:SI 1 dx [orig:87 c ] [87]))) (clobber (reg:CC 17 flags)) ]) "test.c":4:15 254 {*subsi_1} (expr_list:REG_DEAD (reg/v:SI 1 dx [orig:87 c ] [87]) (expr_list:REG_UNUSED (reg:CC 17 flags) (nil)))) (insn:TI 13 12 52 3 (parallel [ (set (reg:SI 0 ax [orig:84 ] [84]) (mult:SI (reg/v:SI 0 ax [orig:88 d ] [88]) (reg:SI 4 si [89]))) (clobber (reg:CC 17 flags)) ]) "test.c":4:20 378 {*mulsi3_1} (expr_list:REG_DEAD (reg:SI 4 si [89]) (expr_list:REG_UNUSED (reg:CC 17 flags) (nil)))) (insn 52 13 45 3 (use (reg/i:SI 0 ax)) -1 (nil)) (jump_insn:TI 45 52 46 3 (simple_return) "test.c":4:20 767 {simple_return_internal} (nil) -> simple_return) (barrier 46 45 16) (code_label 16 46 17 4 2 (nil) [1 uses]) (note 17 16 18 4 [bb 4] NOTE_INSN_BASIC_BLOCK) (insn:TI 18 17 19 4 (parallel [ (set (reg:SI 1 dx [90]) (minus:SI (reg/v:SI 1 dx [orig:87 c ] [87]) (reg/v:SI 4 si [orig:86 b ] [86]))) (clobber (reg:CC 17 flags)) ]) "test.c":6:15 254 {*subsi_1} (expr_list:REG_DEAD (reg/v:SI 4 si [orig:86 b ] [86]) (expr_list:REG_UNUSED (reg:CC 17 flags) (nil)))) (insn:TI 19 18 26 4 (parallel [ (set (reg:SI 0 ax [orig:84 ] [84]) (mult:SI (reg/v:SI 0 ax [orig:88 d ] [88]) (reg:SI 1 dx [90]))) (clobber (reg:CC 17 flags)) ]) "test.c":6:20 378 {*mulsi3_1} ``` -------------------------------- ### Run executable Source: https://gcc-newbies-guide.readthedocs.io/en/latest/_sources/binaries-and-processes.rst.txt Executes the compiled program, which prints 'hello world' to standard output. ```sh $ ./hello hello world ``` -------------------------------- ### Compile a C program Source: https://gcc-newbies-guide.readthedocs.io/en/latest/debugging.html Standard C code for a 'Hello, world!' program. ```c #include int main (int argc, const char *argv[]) { printf ("Hello world\n"); return 0; } ``` -------------------------------- ### C Program: hello.c Source: https://gcc-newbies-guide.readthedocs.io/en/latest/binaries-and-processes.html A basic C program that prints 'hello world' to the console. ```c #include int main () { printf ("hello world\n"); return 0; } ``` -------------------------------- ### Get Token Location in C++ Source: https://gcc-newbies-guide.readthedocs.io/en/latest/_sources/how-to-improve-the-location-of-a-diagnostic.rst.txt Retrieves the location of the current token being parsed. This is often an implicit source for diagnostics and may be improved. ```c++ value_loc = c_parser_peek_token (parser)->location; ``` -------------------------------- ### SSA Temporaries Assignment Example Source: https://gcc-newbies-guide.readthedocs.io/en/latest/_sources/inside-cc1.rst.txt Illustrates how assignments to a single temporary in an earlier IR form are split into separate temporaries in SSA form. ```c D.1938 = d * _1; ``` ```c D.1938 = d * _2; ``` ```c _9 = d_7(D) * _1; ``` ```c _8 = d_7(D) * _2; ``` -------------------------------- ### Lexing: C Source to Tokens Source: https://gcc-newbies-guide.readthedocs.io/en/latest/_sources/inside-cc1.rst.txt Illustrates the transformation of C source code characters into a stream of tokens during the lexing phase. This is the initial step before parsing. ```none return (b - c) * d; ``` ```none CPP_KEYWORD(RID_RETURN) CPP_OPEN_PAREN CPP_NAME("b") CPP_MINUS CPP_NAME("c") CPP_CLOSE_PAREN CPP_MULT CPP_NAME("d") CPP_SEMICOLON ``` -------------------------------- ### GCC RTL Dump Source: https://gcc-newbies-guide.readthedocs.io/en/latest/inside-cc1.html This is an example of the Register Transfer Language (RTL) generated by GCC after the 'expand' optimization pass. It shows the low-level operations on registers and memory. ```gcc-rtl ;; Function test (test, funcdef_no=0, decl_uid=1933, cgraph_uid=1, symbol_order=0) ;; Generating RTL for gimple basic block 2 ;; Generating RTL for gimple basic block 3 ;; Generating RTL for gimple basic block 4 ;; Generating RTL for gimple basic block 5 try_optimize_cfg iteration 1 Merging block 3 into block 2... Merged blocks 2 and 3. Merged 2 and 3 without moving. Redirecting jump 14 from 6 to 7. Merging block 6 into block 5... Merged blocks 5 and 6. Merged 5 and 6 without moving. Removing jump 22. try_optimize_cfg iteration 2 ;; ;; Full RTL generated for this function: ;; (note 1 0 7 NOTE_INSN_DELETED) (note 7 1 2 2 [bb 2] NOTE_INSN_BASIC_BLOCK) (insn 2 7 3 2 (set (reg/v:SI 85 [ a ]) (reg:SI 5 di [ a ]))) "test.c":2:1 -1 (nil)) (insn 3 2 4 2 (set (reg/v:SI 86 [ b ]) (reg:SI 4 si [ b ]))) "test.c":2:1 -1 (nil)) (insn 4 3 5 2 (set (reg/v:SI 87 [ c ]) (reg:SI 1 dx [ c ]))) "test.c":2:1 -1 (nil)) (insn 5 4 6 2 (set (reg/v:SI 88 [ d ]) (reg:SI 2 cx [ d ]))) "test.c":2:1 -1 (nil)) (note 6 5 9 2 NOTE_INSN_FUNCTION_BEG) (insn 9 6 10 2 (set (reg:CCZ 17 flags) (compare:CCZ (reg/v:SI 85 [ a ]) (const_int 0 [0]))) "test.c":3:6 -1 (nil)) (jump_insn 10 9 11 2 (set (pc) (if_then_else (eq (reg:CCZ 17 flags) (const_int 0 [0])) (label_ref 16) (pc))) "test.c":3:6 -1 (int_list:REG_BR_PROB 536870916 (nil)) -> 16) (note 11 10 12 4 [bb 4] NOTE_INSN_BASIC_BLOCK) (insn 12 11 13 4 (parallel [ (set (reg:SI 89) (minus:SI (reg/v:SI 86 [ b ]) (reg/v:SI 87 [ c ]))) (clobber (reg:CC 17 flags)) ]) "test.c":4:15 -1 (nil)) (insn 13 12 14 4 (parallel [ (set (reg:SI 84 [ ]) (mult:SI (reg:SI 89) (reg/v:SI 88 [ d ]))) (clobber (reg:CC 17 flags)) ]) "test.c":4:20 -1 (nil)) (jump_insn 14 13 15 4 (set (pc) (label_ref:DI 24))) "test.c":4:20 737 {jump} (nil) -> 24) (barrier 15 14 16) (code_label 16 15 17 5 2 (nil) [1 uses]) (note 17 16 18 5 [bb 5] NOTE_INSN_BASIC_BLOCK) (insn 18 17 19 5 (parallel [ (set (reg:SI 90) (minus:SI (reg/v:SI 87 [ c ]) (reg/v:SI 86 [ b ]))) (clobber (reg:CC 17 flags)) ]) "test.c":6:15 -1 (nil)) (insn 19 18 24 5 (parallel [ (set (reg:SI 84 [ ]) (mult:SI (reg:SI 90) (reg/v:SI 88 [ d ]))) (clobber (reg:CC 17 flags)) ]) "test.c":6:20 -1 (nil)) (code_label 24 19 27 7 1 (nil) [1 uses]) (note 27 24 25 7 [bb 7] NOTE_INSN_BASIC_BLOCK) (insn 25 27 26 7 (set (reg/i:SI 0 ax) (reg:SI 84 [ ])) "test.c":7:1 -1 (nil)) (insn 26 25 0 7 (use (reg/i:SI 0 ax)) "test.c":7:1 -1 (nil)) ``` -------------------------------- ### List files in directory Source: https://gcc-newbies-guide.readthedocs.io/en/latest/_sources/binaries-and-processes.rst.txt Lists files in the current directory, showing the generated executable and the source file. ```sh $ ls hello hello.c ``` -------------------------------- ### Get Token Location in C Parser Source: https://gcc-newbies-guide.readthedocs.io/en/latest/how-to-improve-the-location-of-a-diagnostic.html Retrieves the location of the first token within an expression. This is often less informative than the expression's full range. ```c value_loc = c_parser_peek_token (parser)->location; ``` -------------------------------- ### Compile and run a C program Source: https://gcc-newbies-guide.readthedocs.io/en/latest/debugging.html Command-line instructions to compile and execute a C program using GCC. ```bash $ gcc hello.c $ ./a.out Hello world ``` -------------------------------- ### Clone GCC Sources and Download Prerequisites Source: https://gcc-newbies-guide.readthedocs.io/en/latest/_sources/getting-started.rst.txt Clones the GCC source code repository and downloads its specific prerequisites using a provided script. Ensure you are in the desired parent directory. ```bash mkdir -p gcc-control cd gcc-control git clone git://gcc.gnu.org/git/gcc.git sources cd gcc-control ./sources/contrib/download_prerequisites ``` -------------------------------- ### GIMPLE SSA Form Example Source: https://gcc-newbies-guide.readthedocs.io/en/latest/_sources/inside-cc1.rst.txt This GIMPLE code represents a basic block in Static Single Assignment (SSA) form, showing variable assignments and control flow. ```c goto ; [50.00%] else goto ; [50.00%] [local count: 536870913]: _1 = b_6(D) - c_5(D); _9 = _1 * d_7(D); goto ; [100.00%] [local count: 536870913]: _2 = c_5(D) - b_6(D); _8 = _2 * d_7(D); [local count: 1073741824]: # _3 = PHI <_9(3), _8(4)> return _3; } ``` -------------------------------- ### Build GCC with Bootstrap Source: https://gcc-newbies-guide.readthedocs.io/en/latest/getting-started.html Use this command to build GCC for a bootstrap build. Omit the 'bootstrap' target for a regular build. ```bash make -j16 bootstrap ``` -------------------------------- ### Run GCC Tests with Multiple Wildcards and ABIs Source: https://gcc-newbies-guide.readthedocs.io/en/latest/working-with-the-testsuite.html Execute tests from different .exp files using wildcards and specify multiple target ABIs (e.g., 32-bit and 64-bit) within RUNTESTFLAGS. ```bash make check-gcc \ RUNTESTFLAGS="--target_board=unix\{-m32,-m64\} dg.exp='pr10474.c pr15698*.c' tree-ssa.exp=20030530-2.c" ``` -------------------------------- ### Compile C code to executable Source: https://gcc-newbies-guide.readthedocs.io/en/latest/binaries-and-processes.html Compiles a C source file into an executable file named 'hello'. ```bash $ gcc hello.c -o hello ``` -------------------------------- ### Use EXPR_LOC_OR_LOC Macro Source: https://gcc-newbies-guide.readthedocs.io/en/latest/_sources/how-to-improve-the-location-of-a-diagnostic.rst.txt A utility macro to get the location of an expression, providing a fallback if direct location information is unavailable. Useful for emitting multiple locations or fix-it hints. ```c++ EXPR_LOC_OR_LOC (expr, fallback_location) ``` -------------------------------- ### Regstrap Patch and Run Test Suite Source: https://gcc-newbies-guide.readthedocs.io/en/latest/_sources/readying-a-patch.rst.txt This bash script performs the regstrap process for a patched GCC version, including cleaning the build directory, configuring, bootstrapping, and running the full test suite. It's followed by a comparison with the control build. ```bash cd /path/to/gcc-patched # Cleanup your build folder rm -r build && mkdir build cd build # configure WITHOUT disabling bootstrap. ../sources/configure --prefix=/path/to/gcc-patched/install # Run the full testsuite. make -j16 bootstrap && make -k -j16 check # Compare the result of your regstrap with control's # Be sure you understand the output. If you have a doubt, # better ask the mail list. ../sources/contrib/compare_tests /path/to/gcc-control/build . ``` -------------------------------- ### C Code Example for Tree Dump Source: https://gcc-newbies-guide.readthedocs.io/en/latest/inside-cc1.html This C code snippet is used to generate a 'tree' dump, showing the intermediate representation after parsing. It demonstrates a simple conditional return statement. ```c ;; Function test (null) ;; enabled by -tree-original { if (a != 0) { return (b - c) * d; } else { return (c - b) * d; } } ``` -------------------------------- ### Compile C Program with GCC Source: https://gcc-newbies-guide.readthedocs.io/en/latest/binaries-and-processes.html Command to compile a C program using GCC. ```bash gcc hello.c -o hello ``` -------------------------------- ### Use EXPR_LOC_OR_LOC Macro Source: https://gcc-newbies-guide.readthedocs.io/en/latest/how-to-improve-the-location-of-a-diagnostic.html A utility macro to get the location of an expression, falling back to a provided location if the expression itself doesn't have one. Useful for emitting multiple locations or fix-it hints. ```c EXPR_LOC_OR_LOC (expr, fallback_location) ``` -------------------------------- ### Assembler Invocation by GCC Source: https://gcc-newbies-guide.readthedocs.io/en/latest/binaries-and-processes.html Demonstrates how the GCC driver invokes the assembler ('as') with specific options for processing assembly files into object files. ```text COLLECT_GCC_OPTIONS='-o' 'hello' '-v' '-mtune=generic' '-march=x86-64' as -v --64 -o /tmp/ccXNu6jN.o /tmp/cckqYOSJ.s GNU assembler version 2.35 (x86_64-redhat-linux) using BFD version version 2.35-18.fc33 ``` ```text as -v --64 -o /tmp/ccXNu6jN.o /tmp/cckqYOSJ.s ``` -------------------------------- ### Parallel Build with Make Source: https://gcc-newbies-guide.readthedocs.io/en/latest/getting-started.html Builds GCC using parallel processing to speed up the compilation time. Adjust -jN based on your system's cores. ```bash # I have 16 cores on my box, so I use -j16 to parallelize as much as possible make -j16 ``` -------------------------------- ### Compiling C to Assembler with Optimization Source: https://gcc-newbies-guide.readthedocs.io/en/latest/inside-cc1.html Command to compile a C file to assembly, enabling optimization and verbose assembly output. Use this to inspect the generated assembly code. ```bash $ gcc -S test.c -O2 -fverbose-asm ``` -------------------------------- ### GCC Test Case with Diagnostics Show Caret Source: https://gcc-newbies-guide.readthedocs.io/en/latest/how-to-improve-the-location-of-a-diagnostic.html Example of a GCC test case using `-fdiagnostics-show-caret` to capture range information. It includes expected error output and multiline output markers for testing caret placement. ```c /* { dg-options "-fdiagnostics-show-caret" } */ void test (void) { enum { c1 = "not int", /* { dg-error "14: enumerator value for .c1. is not an integer constant" } */ /* { dg-begin-multiline-output "" } enum { c1 = "not int", ^~~~~~~~~ { dg-end-multiline-output "" } */ c_end }; } ``` -------------------------------- ### Configure GCC Build Source: https://gcc-newbies-guide.readthedocs.io/en/latest/getting-started.html Configures the GCC build environment. Use --disable-bootstrap for faster rebuilds during development. ```bash mkdir build && cd build # here you would add --disable-bootstrap for your 'draft' directory ../sources/configure --prefix "/path/to/gcc-control/install" ``` -------------------------------- ### GCC RTL Final Dump Example Source: https://gcc-newbies-guide.readthedocs.io/en/latest/inside-cc1.html This dump file represents the final RTL form of a function after the 'final' optimization pass. It is used as input for assembler generation. Note that this is a representation of intermediate compiler code, not C source. ```gcc-rtl ;; Function test (test, funcdef_no=0, decl_uid=1933, cgraph_uid=1, symbol_order=0) test Dataflow summary: ;; fully invalidated by EH 0 [ax] 1 [dx] 2 [cx] 4 [si] 5 [di] 8 [st] 9 [st(1)] 10 [st(2)] 11 [st(3)] 12 [st(4)] 13 [st(5)] 14 [st(6)] 15 [st(7)] 17 [flags] 18 [fpsr] 20 [xmm0] 21 [xmm1] 22 [xmm2] 23 [xmm3] 24 [xmm4] 25 [xmm5] 26 [xmm6] 27 [xmm7] 28 [mm0] 29 [mm1] 30 [mm2] 31 [mm3] 32 [mm4] 33 [mm5] 34 [mm6] 35 [mm7] 36 [r8] 37 [r9] 38 [r10] 39 [r11] 44 [xmm8] 45 [xmm9] 46 [xmm10] 47 [xmm11] 48 [xmm12] 49 [xmm13] 50 [xmm14] 51 [xmm15] 52 [xmm16] 53 [xmm17] 54 [xmm18] 55 [xmm19] 56 [xmm20] 57 [xmm21] 58 [xmm22] 59 [xmm23] 60 [xmm24] 61 [xmm25] 62 [xmm26] 63 [xmm27] 64 [xmm28] 65 [xmm29] 66 [xmm30] 67 [xmm31] 68 [k0] 69 [k1] 70 [k2] 71 [k3] 72 [k4] 73 [k5] 74 [k6] 75 [k7] ;; hardware regs used 7 [sp] ;; regular block artificial uses 7 [sp] ;; eh block artificial uses 7 [sp] 16 [argp] ;; entry block defs 0 [ax] 1 [dx] 2 [cx] 4 [si] 5 [di] 7 [sp] 20 [xmm0] 21 [xmm1] 22 [xmm2] 23 [xmm3] 24 [xmm4] 25 [xmm5] 26 [xmm6] 27 [xmm7] 36 [r8] 37 [r9] ;; exit block uses 0 [ax] 7 [sp] ;; regs ever live 0 [ax] 1 [dx] 2 [cx] 4 [si] 5 [di] 17 [flags] ;; ref usage r0={4d,5u} r1={2d,3u} r2={1d,1u} r4={2d,3u} r5={1d,1u} r7={1d,4u} r17={5d,1u} r20={1d} r21={1d} r22={1d} r23={1d} r24={1d} r25={1d} r26={1d} r27={1d} r36={1d} r37={1d} ;; total ref usage 44{26d,18u,0e} in 11{11 regular + 0 call} insns. (note 1 0 7 NOTE_INSN_DELETED) (note 7 1 48 2 [bb 2] NOTE_INSN_BASIC_BLOCK) (note 48 7 2 2 NOTE_INSN_PROLOGUE_END) (note 2 48 6 2 NOTE_INSN_DELETED) (note 6 2 5 2 NOTE_INSN_FUNCTION_BEG) (insn:TI 5 6 9 2 (set (reg/v:SI 0 ax [orig:88 d ] [88]) (reg:SI 2 cx [97])) "test.c":2:1 67 {*movsi_internal} (expr_list:REG_DEAD (reg:SI 2 cx [97]) (nil))) (insn 9 5 10 2 (set (reg:CCZ 17 flags) (compare:CCZ (reg:SI 5 di [94]) (const_int 0 [0]))) "test.c":3:6 7 {*cmpsi_ccno_1} (expr_list:REG_DEAD (reg:SI 5 di [94]) (nil))) (jump_insn 10 9 11 2 (set (pc) (if_then_else (eq (reg:CCZ 17 flags) (const_int 0 [0])) (label_ref 16) (pc)))"test.c":3:6 736 {*jcc} (expr_list:REG_DEAD (reg:CCZ 17 flags) (int_list:REG_BR_PROB 536870916 (nil))) -> 16) (note 11 10 12 3 [bb 3] NOTE_INSN_BASIC_BLOCK) (insn:TI 12 11 13 3 (parallel [ (set (reg:SI 4 si [89]) (minus:SI (reg/v:SI 4 si [orig:86 b ] [86]) (reg/v:SI 1 dx [orig:87 c ] [87]))) (clobber (reg:CC 17 flags)) ]) "test.c":4:15 254 {*subsi_1} (expr_list:REG_DEAD (reg/v:SI 1 dx [orig:87 c ] [87]) (expr_list:REG_UNUSED (reg:CC 17 flags) (nil)))) (insn:TI 13 12 52 3 (parallel [ (set (reg:SI 0 ax [orig:84 ] [84]) (mult:SI (reg/v:SI 0 ax [orig:88 d ] [88]) (reg:SI 4 si [89]))) (clobber (reg:CC 17 flags)) ]) "test.c":4:20 378 {*mulsi3_1} (expr_list:REG_DEAD (reg:SI 4 si [89]) (expr_list:REG_UNUSED (reg:CC 17 flags) (nil)))) (insn 52 13 45 3 (use (reg/i:SI 0 ax)) -1 (nil)) (jump_insn:TI 45 52 46 3 (simple_return) "test.c":4:20 767 {simple_return_internal} (nil) -> simple_return) (barrier 46 45 16) (code_label 16 46 17 4 2 (nil) [1 uses]) (note 17 16 18 4 [bb 4] NOTE_INSN_BASIC_BLOCK) (insn:TI 18 17 19 4 (parallel [ (set (reg:SI 1 dx [90]) (minus:SI (reg/v:SI 1 dx [orig:87 c ] [87]) (reg/v:SI 4 si [orig:86 b ] [86]))) (clobber (reg:CC 17 flags)) ]) "test.c":6:15 254 {*subsi_1} (expr_list:REG_DEAD (reg/v:SI 4 si [orig:86 b ] [86]) (expr_list:REG_UNUSED (reg:CC 17 flags) (nil)))) (insn:TI 19 18 26 4 (parallel [ (set (reg:SI 0 ax [orig:84 ] [84]) (mult:SI (reg/v:SI 0 ax [orig:88 d ] [88]) (reg:SI 1 dx [90]))) (clobber (reg:CC 17 flags)) ]) "test.c":6:20 378 {*mulsi3_1} (expr_list:REG_DEAD (reg:SI 1 dx [90]) (expr_list:REG_UNUSED (reg:CC 17 flags) (nil)))) (insn 26 19 55 4 (use (reg/i:SI 0 ax)) "test.c":7:1 -1 (nil)) (note 55 26 50 4 NOTE_INSN_EPILOGUE_BEG) (jump_insn:TI 50 55 51 4 (simple_return) "test.c":7:1 767 {simple_return_internal} (nil) -> simple_return) (barrier 51 50 47) (note 47 51 0 NOTE_INSN_DELETED) ```