Appease compilers that have the -Wcovered-switch-default switch.
[llvm-core.git] / docs / Extensions.rst
blob14fea30204b4cb235a1175f6889daffc4687aa47
1 ===============
2 LLVM Extensions
3 ===============
5 .. contents::
6    :local:
8 .. toctree::
9    :hidden:
11 Introduction
12 ============
14 This document describes extensions to tools and formats LLVM seeks compatibility
15 with.
17 General Assembly Syntax
18 ===========================
20 C99-style Hexadecimal Floating-point Constants
21 ----------------------------------------------
23 LLVM's assemblers allow floating-point constants to be written in C99's
24 hexadecimal format instead of decimal if desired.
26 .. code-block:: gas
28   .section .data
29   .float 0x1c2.2ap3
31 Machine-specific Assembly Syntax
32 ================================
34 X86/COFF-Dependent
35 ------------------
37 Relocations
38 ^^^^^^^^^^^
40 The following additional relocation types are supported:
42 **@IMGREL** (AT&T syntax only) generates an image-relative relocation that
43 corresponds to the COFF relocation types ``IMAGE_REL_I386_DIR32NB`` (32-bit) or
44 ``IMAGE_REL_AMD64_ADDR32NB`` (64-bit).
46 .. code-block:: text
48   .text
49   fun:
50     mov foo@IMGREL(%ebx, %ecx, 4), %eax
52   .section .pdata
53     .long fun@IMGREL
54     .long (fun@imgrel + 0x3F)
55     .long $unwind$fun@imgrel
57 **.secrel32** generates a relocation that corresponds to the COFF relocation
58 types ``IMAGE_REL_I386_SECREL`` (32-bit) or ``IMAGE_REL_AMD64_SECREL`` (64-bit).
60 **.secidx** relocation generates an index of the section that contains
61 the target.  It corresponds to the COFF relocation types
62 ``IMAGE_REL_I386_SECTION`` (32-bit) or ``IMAGE_REL_AMD64_SECTION`` (64-bit).
64 .. code-block:: none
66   .section .debug$S,"rn"
67     .long 4
68     .long 242
69     .long 40
70     .secrel32 _function_name + 0
71     .secidx   _function_name
72     ...
74 ``.linkonce`` Directive
75 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
77 Syntax:
79    ``.linkonce [ comdat type ]``
81 Supported COMDAT types:
83 ``discard``
84    Discards duplicate sections with the same COMDAT symbol. This is the default
85    if no type is specified.
87 ``one_only``
88    If the symbol is defined multiple times, the linker issues an error.
90 ``same_size``
91    Duplicates are discarded, but the linker issues an error if any have
92    different sizes.
94 ``same_contents``
95    Duplicates are discarded, but the linker issues an error if any duplicates
96    do not have exactly the same content.
98 ``largest``
99    Links the largest section from among the duplicates.
101 ``newest``
102    Links the newest section from among the duplicates.
105 .. code-block:: gas
107   .section .text$foo
108   .linkonce
109     ...
111 ``.section`` Directive
112 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
114 MC supports passing the information in ``.linkonce`` at the end of
115 ``.section``. For example,  these two codes are equivalent
117 .. code-block:: gas
119   .section secName, "dr", discard, "Symbol1"
120   .globl Symbol1
121   Symbol1:
122   .long 1
124 .. code-block:: gas
126   .section secName, "dr"
127   .linkonce discard
128   .globl Symbol1
129   Symbol1:
130   .long 1
132 Note that in the combined form the COMDAT symbol is explicit. This
133 extension exists to support multiple sections with the same name in
134 different COMDATs:
137 .. code-block:: gas
139   .section secName, "dr", discard, "Symbol1"
140   .globl Symbol1
141   Symbol1:
142   .long 1
144   .section secName, "dr", discard, "Symbol2"
145   .globl Symbol2
146   Symbol2:
147   .long 1
149 In addition to the types allowed with ``.linkonce``, ``.section`` also accepts
150 ``associative``. The meaning is that the section is linked  if a certain other
151 COMDAT section is linked. This other section is indicated by the comdat symbol
152 in this directive. It can be any symbol defined in the associated section, but
153 is usually the associated section's comdat.
155    The following restrictions apply to the associated section:
157    1. It must be a COMDAT section.
158    2. It cannot be another associative COMDAT section.
160 In the following example the symobl ``sym`` is the comdat symbol of ``.foo``
161 and ``.bar`` is associated to ``.foo``.
163 .. code-block:: gas
165         .section        .foo,"bw",discard, "sym"
166         .section        .bar,"rd",associative, "sym"
168 MC supports these flags in the COFF ``.section`` directive:
170   - ``b``: BSS section (``IMAGE_SCN_CNT_INITIALIZED_DATA``)
171   - ``d``: Data section (``IMAGE_SCN_CNT_UNINITIALIZED_DATA``)
172   - ``n``: Section is not loaded (``IMAGE_SCN_LNK_REMOVE``)
173   - ``r``: Read-only
174   - ``s``: Shared section
175   - ``w``: Writable
176   - ``x``: Executable section
177   - ``y``: Not readable
178   - ``D``: Discardable (``IMAGE_SCN_MEM_DISCARDABLE``)
180 These flags are all compatible with gas, with the exception of the ``D`` flag,
181 which gnu as does not support. For gas compatibility, sections with a name
182 starting with ".debug" are implicitly discardable.
185 ELF-Dependent
186 -------------
188 ``.section`` Directive
189 ^^^^^^^^^^^^^^^^^^^^^^
191 In order to support creating multiple sections with the same name and comdat,
192 it is possible to add an unique number at the end of the ``.seciton`` directive.
193 For example, the following code creates two sections named ``.text``.
195 .. code-block:: gas
197         .section        .text,"ax",@progbits,unique,1
198         nop
200         .section        .text,"ax",@progbits,unique,2
201         nop
204 The unique number is not present in the resulting object at all. It is just used
205 in the assembler to differentiate the sections.
207 The 'o' flag is mapped to SHF_LINK_ORDER. If it is present, a symbol
208 must be given that identifies the section to be placed is the
209 .sh_link.
211 .. code-block:: gas
213         .section .foo,"a",@progbits
214         .Ltmp:
215         .section .bar,"ao",@progbits,.Ltmp
217 which is equivalent to just
219 .. code-block:: gas
221         .section .foo,"a",@progbits
222         .section .bar,"ao",@progbits,.foo
225 Target Specific Behaviour
226 =========================
231 Relocations
232 ^^^^^^^^^^^
234 ``@ABS8`` can be applied to symbols which appear as immediate operands to
235 instructions that have an 8-bit immediate form for that operand. It causes
236 the assembler to use the 8-bit form and an 8-bit relocation (e.g. ``R_386_8``
237 or ``R_X86_64_8``) for the symbol.
239 For example:
241 .. code-block:: gas
243   cmpq $foo@ABS8, %rdi
245 This causes the assembler to select the form of the 64-bit ``cmpq`` instruction
246 that takes an 8-bit immediate operand that is sign extended to 64 bits, as
247 opposed to ``cmpq $foo, %rdi`` which takes a 32-bit immediate operand. This
248 is also not the same as ``cmpb $foo, %dil``, which is an 8-bit comparison.
250 Windows on ARM
251 --------------
253 Stack Probe Emission
254 ^^^^^^^^^^^^^^^^^^^^
256 The reference implementation (Microsoft Visual Studio 2012) emits stack probes
257 in the following fashion:
259 .. code-block:: gas
261   movw r4, #constant
262   bl __chkstk
263   sub.w sp, sp, r4
265 However, this has the limitation of 32 MiB (±16MiB).  In order to accommodate
266 larger binaries, LLVM supports the use of ``-mcode-model=large`` to allow a 4GiB
267 range via a slight deviation.  It will generate an indirect jump as follows:
269 .. code-block:: gas
271   movw r4, #constant
272   movw r12, :lower16:__chkstk
273   movt r12, :upper16:__chkstk
274   blx r12
275   sub.w sp, sp, r4
277 Variable Length Arrays
278 ^^^^^^^^^^^^^^^^^^^^^^
280 The reference implementation (Microsoft Visual Studio 2012) does not permit the
281 emission of Variable Length Arrays (VLAs).
283 The Windows ARM Itanium ABI extends the base ABI by adding support for emitting
284 a dynamic stack allocation.  When emitting a variable stack allocation, a call
285 to ``__chkstk`` is emitted unconditionally to ensure that guard pages are setup
286 properly.  The emission of this stack probe emission is handled similar to the
287 standard stack probe emission.
289 The MSVC environment does not emit code for VLAs currently.