### Initialize global variables using +initialize Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Executing-code-before-main.html An example showing the problematic approach of using +initialize for global variable setup, which may occur too late. ```objective-c FileStream *Stdin = nil; FileStream *Stdout = nil; FileStream *Stderr = nil; @implementation FileStream + (void)initialize { Stdin = [[FileStream new] initWithFd:0]; Stdout = [[FileStream new] initWithFd:1]; Stderr = [[FileStream new] initWithFd:2]; } /* Other methods here */ @end ``` -------------------------------- ### Insert Bits Examples Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/AVR-Built-in-Functions.html Examples demonstrating how to use __builtin_avr_insert_bits to manipulate bit fields and port layouts. ```c // same as val, bits is unused __builtin_avr_insert_bits (0xffffffff, bits, val); ``` ```c // same as bits, val is unused __builtin_avr_insert_bits (0x76543210, bits, val); ``` ```c // same as rotating bits by 4 __builtin_avr_insert_bits (0x32107654, bits, 0); ``` ```c // high nibble of result is the high nibble of val // low nibble of result is the low nibble of bits __builtin_avr_insert_bits (0xffff3210, bits, val); ``` ```c // reverse the bit order of bits __builtin_avr_insert_bits (0x01234567, bits, 0); ``` -------------------------------- ### Compile with command-line option Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/LoongArch-Attributes.html Command-line example demonstrating how to disable lasx via command-line options. ```bash $ gcc test.c -o test.s -O2 -mlasx -mno-lasx ``` -------------------------------- ### Compile with target attribute Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/LoongArch-Attributes.html Command-line example for compiling the test.c file with the WITH_ATTR macro defined. ```bash $ gcc test.c -o test.s -O2 -mlasx -DWITH_ATTR ``` -------------------------------- ### Define structures with zero-length bit-field alignment Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/x86-Options.html Examples demonstrating how zero-length bit-fields affect the alignment and size of subsequent structure members. ```c struct { char foo : 4; short : 0; char bar; } t2; struct { char foo : 4; short : 0; double bar; } t3; ``` -------------------------------- ### Define structure with trailing zero-length bit-field Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/x86-Options.html Example showing how a zero-length bit-field can affect the alignment of the entire structure. ```c struct { char foo : 6; long : 0; } t4; ``` -------------------------------- ### Declare a structure for compound literal examples Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Compound-Literals.html Defines the structure used in subsequent examples. ```c struct foo {int a; char b[2];} structure; ``` -------------------------------- ### Initialize EIND in .init3 section Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/AVR-Options.html Example of setting the EIND register early in the startup sequence using a naked function in the .init3 section. ```c #include static void __attribute__((section(".init3"),naked,used,no_instrument_function)) init3_set_eind (void) { __asm volatile ("ldi r24,pm_hh8(__trampolines_start)\n\t" "out %i0,r24" :: "n" (&EIND) : "r24","memory"); } ``` -------------------------------- ### Define structure with zero-length bit-field Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/x86-Options.html Example of using a zero-length bit-field to prevent coalescing of adjacent bit-fields. ```c struct { unsigned long bf_1 : 12; unsigned long : 0; unsigned long bf_2 : 12; } t1; ``` -------------------------------- ### Example of --prime-paths-lines output Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Invoking-Gcov.html Sample output showing path coverage statistics and instructions for covering specific paths. ```text paths covered 12 of 15 path 2 not covered: lines 8 8(false) 11(true) 11 13(true) 13(true) 14 17 path 3 not covered: lines 8 8(false) 11(true) 11 13(true) 13(false) 16 17 path 4 not covered: lines 8 8(false) 11(true) 11 13(false) 16 17 ``` -------------------------------- ### Conditional Branching Example Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Directly-mapped-SX-Builtin-Functions.html Example usage of __lsx_bz_v for conditional branching based on vector state. ```c #include extern __m128i a; void test (void) { if (__lsx_bz_v (a)) printf ("1\n"); else printf ("2\n"); } ``` -------------------------------- ### Initializing Static Storage with __memx Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Named-Address-Spaces.html Shows how to initialize a static pointer variable with a 24-bit address using the __memx address space. ```C extern const __memx char foo; const __memx void *pfoo = &foo; ``` -------------------------------- ### Zero-Check Intrinsics Usage Example Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Directly-mapped-ASX-Builtin-Functions.html Example demonstrating the use of the __lasx_xbz_v intrinsic for conditional branching. ```c eg: #include extern __m256i a; void test (void) { if (__lasx_xbz_v (a)) printf ("1\n"); else printf ("2\n"); } ``` -------------------------------- ### Zero-Length Array Initialization Examples Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Zero-Length.html Demonstrates valid and invalid initialization patterns for structures containing zero-length arrays. ```c struct foo { int x; int y[]; }; struct bar { struct foo z; }; struct foo a = { 1, { 2, 3, 4 } }; // Valid. struct bar b = { { 1, { 2, 3, 4 } } }; // Invalid. struct bar c = { { 1, { } } }; // Valid. struct foo d[1] = { { 1, { 2, 3, 4 } } }; // Invalid. ``` -------------------------------- ### Dangling reference examples Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/C_002b_002b-Dialect-Options.html Examples of code that triggers -Wdangling-reference warnings due to temporary lifetime issues. ```cpp int n = 1; const int& r = std::max(n - 1, n + 1); // r is dangling ``` ```cpp const int& f(const int& i) { return i; } struct S { const int &r; // r is dangling S() : r(f(10)) { } }; ``` ```cpp struct S { const S& self () { return *this; } }; const S& s = S().self(); // s is dangling ``` ```cpp auto p = std::minmax(1, 2); ``` -------------------------------- ### Loop Distribution Transformation Example Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Optimize-Options.html Demonstrates how loop distribution splits a single loop body into multiple loops to improve cache performance. ```Fortran DO I = 1, N A(I) = B(I) + C D(I) = E(I) * F ENDDO ``` ```Fortran DO I = 1, N A(I) = B(I) + C ENDDO DO I = 1, N D(I) = E(I) * F ENDDO ``` -------------------------------- ### x86 Assembler Dialect Example Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Extended-Asm.html An example showing how dialect-specific instructions are expanded based on the target assembler dialect. ```asm "bt{l %[Offset],%[Base] | %[Base],%[Offset]}; jc %l2" ``` ```asm "btl %[Offset],%[Base] ; jc %l2" /* att dialect */ "bt %[Base],%[Offset]; jc %l2" /* intel dialect */ ``` -------------------------------- ### Compile an Objective-C file Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Objective-C-and-Objective-C_002b_002b-Dialect-Options.html Example command for compiling an Objective-C source file using the GNU runtime. ```bash gcc -g -fgnu-runtime -O -c some_class.m ``` -------------------------------- ### HP-UX assembler warning example Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Interoperation.html Example of a warning message emitted by the HP-UX assembler when processing GCC-compiled code. ```text (warning) Use of GR3 when frame >= 8192 may cause conflict. ``` -------------------------------- ### Demonstrating __FUNCTION__ and __PRETTY_FUNCTION__ in C++ Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Function-Names.html Example showing the difference between __FUNCTION__ and __PRETTY_FUNCTION__ within a class method. ```cpp extern "C" int printf (const char *, ...); class a { public: void sub (int i) { printf ("__FUNCTION__ = %s\n", __FUNCTION__); printf ("__PRETTY_FUNCTION__ = %s\n", __PRETTY_FUNCTION__); } }; int main (void) { a ax; ax.sub (0); return 0; } ``` -------------------------------- ### Mask Rotation Examples Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/AVR-Built-in-Functions.html Examples showing how __builtin_avr_mask1 provides an efficient alternative to standard bitwise shift expressions. ```c __builtin_avr_mask1 (1, offs) // same like 1 << offs __builtin_avr_mask1 (~1, offs) // same like ~(1 << offs) __builtin_avr_mask1 (0x80, offs) // same like 0x80 >> offs __builtin_avr_mask1 (~0x80, offs) // same like ~(0x80 >> offs) ``` -------------------------------- ### Loop Distribution Pattern Transformation Example Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Optimize-Options.html Shows how loop distribution identifies patterns like initialization loops and replaces them with library calls such as memset. ```Fortran DO I = 1, N A(I) = 0 B(I) = A(I) + I ENDDO ``` ```Fortran DO I = 1, N A(I) = 0 ENDDO DO I = 1, N B(I) = A(I) + I ENDDO ``` -------------------------------- ### Intent Operand Usage Example Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Directly-mapped-SX-Builtin-Functions.html Example showing that the first parameter is used as the intent operand when modifying part of a bitfield. ```c #include extern __m128i dst; extern int src; void test (void) { dst = __lsx_vinsgr2vr_b (dst, src, 3); } ``` -------------------------------- ### Compile and Link with LTO Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Optimize-Options.html Demonstrates the standard workflow for using link-time optimization by specifying flags during both compilation and linking stages. ```bash gcc -c -O2 -flto foo.c gcc -c -O2 -flto bar.c gcc -o myprog -flto -O2 foo.o bar.o ``` -------------------------------- ### Intent Operand Intrinsics Usage Example Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Directly-mapped-ASX-Builtin-Functions.html Example showing how to use intrinsics where the first parameter serves as the intent operand. ```c eg: #include extern __m256i dst; int src; void test (void) { dst = __lasx_xvinsgr2vr_w (dst, src, 3); } ``` -------------------------------- ### Loop Optimization Example Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Non-bugs.html An example of a loop that GCC may remove entirely if the expression does not affect global state and the result is unused. ```c { int sum = 0; int ix; for (ix = 0; ix != 10000; ix++) sum += some_expression; } ``` -------------------------------- ### Defining Multiple Alternative Constraints Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Multi-Alternative.html Example showing how to combine multiple alternatives for output and input operands using comma-separated constraint strings. ```c "+m,r" (output) : "ir,irm" (input) ``` -------------------------------- ### Example of machine-parseable fix-it hints Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Diagnostic-Message-Formatting-Options.html Format of fix-it hints emitted for IDE consumption, showing the replacement range and string. ```text fix-it:"test.c":{45:3-45:21}:"gtk_widget_show_all" ``` ```text 00000000011111111112222222222 12345678901234567890123456789 gtk_widget_showall (dlg); ^^^^^^^^^^^^^^^^^^ gtk_widget_show_all ``` -------------------------------- ### Redundant std::move warning examples Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/C_002b_002b-Dialect-Options.html Examples demonstrating scenarios where std::move is redundant due to compiler-enforced rvalue treatment. ```cpp struct T { … }; T fn(T t) { … return std::move (t); } ``` ```cpp struct T { // convertible to U … }; struct U { … }; U fn() { T t; … return std::move (t); } ``` -------------------------------- ### Diagnostic Escape Format Examples Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Diagnostic-Message-Formatting-Options.html Examples showing how different escape formats represent non-printable ASCII characters in source lines. ```text beforeafter ``` ```text before<80>after ``` -------------------------------- ### Interactive Mode Startup Notice Template Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Copying.html Short notice to display when a program starts in interactive mode to inform users of license terms and warranty status. ```text program Copyright (C) year name of author This program comes with ABSOLUTELY NO WARRANTY; for details type ‘show w’. This is free software, and you are welcome to redistribute it under certain conditions; type ‘show c’ for details. ``` -------------------------------- ### Infinite Loop Detection Example Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Static-Analyzer-Options.html Example of code structure that triggers an infinite loop warning due to an implicit default case in a switch statement. ```c while (1) { char opcode = *cpu_state.pc; switch (opcode) { case OPCODE_FOO: handle_opcode_foo (&cpu_state); break; case OPCODE_BAR: handle_opcode_bar (&cpu_state); break; } } ``` -------------------------------- ### Display Predefined Macros Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Preprocessor-Options.html Example command to list all macros defined during preprocessor execution using the -dM flag. ```bash touch foo.h; cpp -dM foo.h ``` -------------------------------- ### Example of --prime-paths-source output Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Invoking-Gcov.html Sample output providing a detailed breakdown of basic blocks and line numbers for uncovered paths. ```text path 10 not covered: BB 3: 8: for (i = 0; i < 10; i++) BB 3: 9: total += i; BB 4: (false) 8: for (i = 0; i < 10; i++) BB 5: (true) 11: int v = total > 100 ? 1 : 2; BB 6: 11: int v = total > 100 ? 1 : 2; BB 8: (false) 13: if (total != 45 && v == 1) BB 11: 16: printf ("Success\n"); BB 12: 17: return 0; ``` -------------------------------- ### x86 Floating-Point asm Operand Examples Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Extended-Asm.html Examples demonstrating the use of stack-like registers, early-clobber modifiers, and clobbering for stack management in x86 asm. ```c asm ("foo" : "=t" (a) : "f" (b)); ``` ```c asm ("foo" : "=&t" (a) : "f" (b)); ``` ```c asm ("fsincos" : "=t" (cos), "=u" (sin) : "0" (inp)); ``` ```c asm ("fyl2xp1" : "=t" (result) : "0" (x), "u" (y) : "st(1)"); ``` -------------------------------- ### __builtin_tbeginc Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/S_002f390-System-z-Built-in-Functions.html Starts a constrained hardware transaction. ```APIDOC ## void __builtin_tbeginc(void) ### Description Generates the tbeginc machine instruction starting a constrained hardware transaction. ``` -------------------------------- ### Configure Offload Target Options Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/OpenMP-and-OpenACC-Options.html Examples of passing specific compiler options to offload targets, either globally or for specific target architectures. ```bash -foffload-options='-fno-math-errno -ffinite-math-only' \ -foffload-options=nvptx-none=-latomic -foffload-options=amdgcn-amdhsa=-march=gfx906 ``` -------------------------------- ### Vector Type Encoding Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Type-encoding.html Example of encoding a vector type with a specified size. ```Objective-C int a __attribute__ ((vector_size (16))); ``` -------------------------------- ### Dump profiling information in freestanding environments Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Instrumentation-Options.html Example C code demonstrating how to access and dump profiling information registered in a linker set for embedded systems. ```c #include #include #include extern const struct gcov_info *const __gcov_info_start[]; extern const struct gcov_info *const __gcov_info_end[]; static void dump (const void *d, unsigned n, void *arg) { const unsigned char *c = d; for (unsigned i = 0; i < n; ++i) printf ("%02x", c[i]); } static void filename (const char *f, void *arg) { __gcov_filename_to_gcfn (f, dump, arg ); } static void * allocate (unsigned length, void *arg) { return malloc (length); } static void dump_gcov_info (void) { const struct gcov_info *const *info = __gcov_info_start; const struct gcov_info *const *end = __gcov_info_end; /* Obfuscate variable to prevent compiler optimizations. */ __asm__ ("" : "+r" (info)); while (info != end) { void *arg = NULL; __gcov_info_to_gcda (*info, filename, dump, allocate, arg); putchar ('\n'); ++info; } } int main (void) { dump_gcov_info (); return 0; } ``` -------------------------------- ### Array Type Encoding Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Type-encoding.html Example of encoding an integer array of size 10. ```Objective-C int a[10]; ``` -------------------------------- ### Sample gcov output file Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Invoking-Gcov.html An example of the contents of a generated .gcov file. ```text -: 0:Source:tmp.cpp -: 0:Working directory:/home/gcc/testcase -: 0:Graph:tmp.gcno -: 0:Data:tmp.gcda -: 0:Runs:1 -: 0:Programs:1 -: 1:#include -: 2: -: 3:template -: 4:class Foo -: 5:{ -: 6: public: 1*: 7: Foo(): b (1000) {} ------------------ Foo::Foo(): #####: 7: Foo(): b (1000) {} ------------------ Foo::Foo(): 1: 7: Foo(): b (1000) {} ------------------ 2*: 8: void inc () { b++; } ------------------ Foo::inc(): #####: 8: void inc () { b++; } ------------------ Foo::inc(): 2: 8: void inc () { b++; } ------------------ -: 9: -: 10: private: -: 11: int b; -: 12:}; -: 13: -: 14:template class Foo; -: 15:template class Foo; -: 16: -: 17:int 1: 18:main (void) -: 19:{ -: 20: int i, total; 1: 21: Foo counter; -: 22: 1: 23: counter.inc(); 1: 24: counter.inc(); 1: 25: total = 0; -: 26: 11: 27: for (i = 0; i < 10; i++) 10: 28: total += i; -: 29: 1*: 30: int v = total > 100 ? 1 : 2; -: 31: 1: 32: if (total != 45) #####: 33: printf ("Failure\n"); -: 34: else 1: 35: printf ("Success\n"); 1: 36: return 0; -: 37:} ``` -------------------------------- ### Example C source file for assembly generation Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Code-Gen-Options.html A simple C function used to demonstrate the effect of assembly generation options. ```c int test (int n) { int i; int total = 0; for (i = 0; i < n; i++) total += i * i; return total; } ``` -------------------------------- ### Retrieve Source Location and Function Name Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Other-Builtins.html Demonstrates using __builtin_FUNCTION, __builtin_FILE, and __builtin_LINE to capture context information. ```C const char* function (const char *func = __builtin_FUNCTION ()) { return func; } void foo (void) { printf ("%s:%i: %s: message\n", file (), line (), function ()); } ``` -------------------------------- ### __builtin_tbegin_nofloat Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/S_002f390-System-z-Built-in-Functions.html Starts a non-constrained hardware transaction without FPR saves and restores. ```APIDOC ## int __builtin_tbegin_nofloat(void*) ### Description Same as __builtin_tbegin but without FPR saves and restores. Using this variant in code making use of FPRs will leave the FPRs in undefined state when entering the transaction abort handler code. ``` -------------------------------- ### __builtin_tbegin Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/S_002f390-System-z-Built-in-Functions.html Generates the tbegin machine instruction to start a non-constrained hardware transaction. ```APIDOC ## int __builtin_tbegin(void*) ### Description Generates the tbegin machine instruction starting a non-constrained hardware transaction. If the parameter is non-NULL, the memory area is used to store the transaction diagnostic buffer (TDB). ### Parameters - **void*** (pointer) - Optional - Pointer to a transaction diagnostic buffer (TDB) or constant zero to prevent TDB writing. ``` -------------------------------- ### Demonstrate Strub Mode Compatibility and Calls Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Common-Attributes.html Shows how to assign functions with different strub modes to pointers and the resulting compatibility behavior. ```C void __attribute__ ((strub)) bap (void) { /* Assign a callable function to pointer-to-disabled. Flagged as not quite compatible with -Wpedantic. */ int __attribute__ ((strub ("disabled"))) (*d_p) (void) = bac; /* Not allowed: calls disabled type in a strub context. */ d_p (); /* Assign a disabled function to pointer-to-callable. Flagged as not quite compatible with -Wpedantic. */ int __attribute__ ((strub ("callable"))) (*c_p) (void) = bad; /* Ok, safe. */ c_p (); /* Assign an internal function to pointer-to-callable. Flagged as not quite compatible with -Wpedantic. */ c_p = bar; /* Ok, safe. */ c_p (); /* Assign an at-calls function to pointer-to-callable. Flaggged as incompatible. */ c_p = bal; /* The call through an interface-incompatible type will not use the modified interface expected by the at-calls function, so it is likely to misbehave at runtime. */ c_p (); } ``` -------------------------------- ### Preamble line format Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Invoking-Gcov.html The format for preamble lines at the start of gcov output. ```text -:0:tag:value ``` -------------------------------- ### Perform Simple Arithmetic with Extended Asm Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/Extended-Asm.html An example for i386 demonstrating how to move a value between C variables and perform an addition using assembly. ```c int src = 1; int dst; asm ("mov %1, %0\n\t" "add $1, %0" : "=r" (dst) : "r" (src)); printf("%d\n", dst); ``` -------------------------------- ### Initialize v2sf values Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/MIPS-Paired-Single-Support.html Demonstrates initialization and assignment of v2sf vector types using aggregate syntax. ```c v2sf a = {1.5, 9.1}; v2sf b; float e, f; b = (v2sf) {e, f}; ```