Size of an asm (Using the GNU Compiler Collection (GCC))

#### 6.11.7 Size of an `asm` ¶

Some targets require that GCC track the size of each instruction used in order to generate correct code. Because the final length of the code produced by an `asm` statement is only known by the assembler, GCC must make an estimate as to how big it will be. It does this by counting the number of instructions in the pattern of the `asm` and multiplying that by the length of the longest instruction supported by that processor. (When working out the number of instructions, it assumes that any occurrence of a newline or of whatever statement separator character is supported by the assembler — typically ‘`;`’ — indicates the end of an instruction.)

Normally, GCC’s estimate is adequate to ensure that correct code is generated, but it is possible to confuse the compiler if you use pseudo instructions or assembler macros that expand into multiple real instructions, or if you use assembler directives that expand to more space in the object file than is needed for a single instruction. If this happens then the assembler may produce a diagnostic saying that a label is unreachable.

This size is also used for inlining decisions. If you use `asm inline` instead of just `asm`, then for inlining purposes the size of the asm is taken as the minimum size, ignoring how many instructions GCC thinks it is.

#### Specifying the size of an `asm` on AVR ¶

Apart from the default size calculation explained above, since GCC 17 there is a way to specify the exact size of an `asm` by means of `[[len=spec]]` annotations. The annotations must be placed in assembly comments in the `asm` template: In a `/* ... */` block comment, or after a ‘`;`’ that starts a single-line comment.

The following `spec`’s are supported. They specify the size of the `asm` — or parts of it — in units of 16-bit words:

`nl`

  The number of lines in the code template, which is half the default size. It suits code templates that only use 1-word instructions with no superfuous physical ‘`\n`’ or logical ‘`$`’ line breaks.

`[0-9]+`

  The decimal representation of a non-negative integer.

`%[0-9]+`
`%[name]`

  The value of the referred operand, which must evaluate to a compile-time integer constant.

`%~`
`%~jmp`
`%~call`

  The length of a `%~call` or `%~jmp` instruction before relaxing.

`lds`
`sts`

  The length of a `lds` resp. `sts` instruction.

The following rules apply:

- All such annotations will be ignored with option `-mno-asm-len-notes`. Diagnosing can be adjusted with option `-Wasm-len-notes`.
- The length of the `asm` is the sum over all `[[len=spec]]` notes in the `asm` template. When an invalid or unrecognizable note is found, then the default length (as calculated from the number of physical and logical line breaks) applies.
- No white-spaces are recognized inside `[[...]]` notes, and all letters must be lower case. Otherwise, all notes in that asm are ignored, and the default length of the `asm` is used.

The actual instruction lengths can be inferred from ‘`;; ADDR = ...`’ assembly comments as generated with, say `-save-temps -mlog=insn_addresses`. Length calculations are also shown in the RTL dump file(s) as generated with `-fdump-rtl-shorten`.

Lengths annotations can solve two issues:

- Cases where the expanded assembly code is longer than determined from the number of physical and logical line breaks. Such cases can lead to errors when a jump that uses a too optimistic jump offset is crossing such assembly code.
- Better code generation for jumps that are crossing an `asm`. The default length in words is twice the number of lines in the code template, though almost all AVR instructions occupy only one 16-bit word.