aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/development/gcc.md
blob: d3823f3eb20ba2f985bb9e1949822152a218c3c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# gcc(1)

## CLI

### Preprocessing
While debugging can be helpful to just pre-process files.

```bash
gcc -E [-dM] ...
```
- `-E` run only preprocessor
- `-dM` list only `#define` statements

### Target options
```bash
# List all target options with their description.
gcc --help=target

# Configure for current cpu arch and query (-Q) value of options.
gcc -march=native -Q --help=target
```

## [Builtins][builtins]

### `__builtin_expect(expr, cond)`
Give the compiler a hint which branch is hot, so it can lay out the code
accordingly to reduce number of jump instructions.
See on [compiler explorer](https://godbolt.org/z/MbTHAP).

```bash
echo "
extern void foo();
extern void bar();
void run0(int x) {
  if (__builtin_expect(x,0)) { foo(); }
  else { bar(); }
}
void run1(int x) {
  if (__builtin_expect(x,1)) { foo(); }
  else { bar(); }
}
" | gcc -O2 -S -masm=intel -o /dev/stdout -xc -
```

Will generate something similar to the following.
- `run0`: `bar` is on the path without branch
- `run1`: `foo` is on the path without branch
```x86asm
run0:
        test    edi, edi
        jne     .L4
        xor     eax, eax
        jmp     bar
.L4:
        xor     eax, eax
        jmp     foo
run1:
        test    edi, edi
        je      .L6
        xor     eax, eax
        jmp     foo
.L6:
        xor     eax, eax
        jmp     bar
```

## ABI (Linux)

- C ABI - [SystemV ABI][sysvabi]
- C++ ABI - [C++ Itanium ABI][cppabi]

[builtins]: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
[sysvabi]: https://www.uclibc.org/docs/psABI-x86_64.pdf
[cppabi]: https://itanium-cxx-abi.github.io/cxx-abi