Debian Codegraph @

docs/misra/C-language-toolchain.rst

raw
1 .. SPDX-License-Identifier: CC-BY-4.0
2
3 =============================================
4 C Dialect and Translation Assumptions for Xen
5 =============================================
6
7 This document specifies the C language dialect used by Xen and
8 the assumptions Xen makes on the translation toolchain.
9 It covers, in particular:
10
11 1. the used language extensions;
12 2. the translation limits that the translation toolchains must be able
13 to accommodate;
14 3. the implementation-defined behaviors upon which Xen may depend.
15
16 All points are of course relevant for portability. In addition,
17 programming in C is impossible without a detailed knowledge of the
18 implementation-defined behaviors. For this reason, it is recommended
19 that Xen developers have familiarity with this document and the
20 documentation referenced therein.
21
22 This document needs maintenance and adaptation in the following
23 circumstances:
24
25 - whenever the compiler is changed or updated;
26 - whenever the use of a certain language extension is added or removed;
27 - whenever code modifications cause exceeding the stated translation limits.
28
29
30 Applicable C Language Standard
31 ______________________________
32
33 Xen is written in C99 with extensions. The relevant ISO standard is
34
35 *ISO/IEC 9899:1999/Cor 3:2007*: Programming Languages - C,
36 Technical Corrigendum 3.
37 ISO/IEC, Geneva, Switzerland, 2007.
38
39
40 Reference Documentation
41 _______________________
42
43 The following documents are referred to in the sequel:
44
45 GCC_MANUAL:
46 https://gcc.gnu.org/onlinedocs/gcc-12.1.0/gcc.pdf
47 CPP_MANUAL:
48 https://gcc.gnu.org/onlinedocs/gcc-12.1.0/cpp.pdf
49 ARM64_ABI_MANUAL:
50 https://github.com/ARM-software/abi-aa/blob/60a8eb8c55e999d74dac5e368fc9d7e36e38dda4/aapcs64/aapcs64.rst
51 X86_64_ABI_MANUAL:
52 https://gitlab.com/x86-psABIs/x86-64-ABI/-/jobs/artifacts/master/raw/x86-64-ABI/abi.pdf?job=build
53
54
55 C Language Extensions
56 _____________________
57
58
59 The following table lists the extensions currently used in Xen.
60 The table columns are as follows:
61
62 Extension
63 a terse description of the extension;
64 Architectures
65 a set of Xen architectures making use of the extension;
66 References
67 when available, references to the documentation explaining
68 the syntax and semantics of (each instance of) the extension.
69
70
71 .. list-table::
72 :widths: 30 15 55
73 :header-rows: 1
74
75 * - Extension
76 - Architectures
77 - References
78
79 * - Non-standard tokens
80 - ARM64, X86_64
81 - _Static_assert:
82 see Section "2.1 C Language" of GCC_MANUAL.
83 asm, __asm__:
84 see Sections "6.48 Alternate Keywords" and "6.47 How to Use Inline Assembly Language in C Code" of GCC_MANUAL.
85 __volatile__:
86 see Sections "6.48 Alternate Keywords" and "6.47.2.1 Volatile" of GCC_MANUAL.
87 __const__:
88 see Section "6.48 Alternate Keywords" of GCC_MANUAL.
89 typeof, __typeof__:
90 see Section "6.7 Referring to a Type with typeof" of GCC_MANUAL.
91 __alignof__, __alignof:
92 see Sections "6.48 Alternate Keywords" and "6.44 Determining the Alignment of Functions, Types or Variables" of GCC_MANUAL.
93 __attribute__:
94 see Section "6.39 Attribute Syntax" of GCC_MANUAL.
95 __builtin_types_compatible_p:
96 see Section "6.59 Other Built-in Functions Provided by GCC" of GCC_MANUAL.
97 __builtin_va_arg:
98 non-documented GCC extension.
99 __builtin_offsetof:
100 see Section "6.53 Support for offsetof" of GCC_MANUAL.
101
102 * - Empty initialization list
103 - ARM64, X86_64
104 - Non-documented GCC extension.
105
106 * - Arithmetic operator on pointer to void
107 - ARM64, X86_64
108 - See Section "6.24 Arithmetic on void- and Function-Pointers" of GCC_MANUAL."
109
110 * - Statements and declarations in expressions
111 - ARM64, X86_64
112 - See Section "6.1 Statements and Declarations in Expressions" of GCC_MANUAL.
113
114 * - Structure or union definition with no members
115 - ARM64, X86_64
116 - See Section "6.19 Structures with No Members" of GCC_MANUAL.
117
118 * - Zero size array type
119 - ARM64, X86_64
120 - See Section "6.18 Arrays of Length Zero" of GCC_MANUAL.
121
122 * - Binary conditional expression
123 - ARM64, X86_64
124 - See Section "6.8 Conditionals with Omitted Operands" of GCC_MANUAL.
125
126 * - 'Case' label with upper/lower values
127 - ARM64, X86_64
128 - See Section "6.30 Case Ranges" of GCC_MANUAL.
129
130 * - Unnamed field that is not a bit-field
131 - ARM64, X86_64
132 - See Section "6.63 Unnamed Structure and Union Fields" of GCC_MANUAL.
133
134 * - Empty declaration
135 - ARM64, X86_64
136 - Non-documented GCC extension.
137 Note: an empty declaration is caused by a semicolon at file scope
138 with nothing before it (not to be confused with an empty statement).
139
140 * - Incomplete enum declaration
141 - ARM64
142 - See Section "6.49 Incomplete enum Types" of GCC_MANUAL.
143
144 * - Implicit conversion from a pointer to an incompatible pointer
145 - ARM64, X86_64
146 - Non-documented GCC extension. The documentation for option
147 -Wincompatible-pointer-types in Section
148 "3.8 Options to Request or Suppress Warnings" of GCC_MANUAL
149 is possibly relevant.
150
151 * - Pointer to a function is converted to a pointer to an object or a pointer to an object is converted to a pointer to a function
152 - X86_64
153 - Non-documented GCC extension. The information provided in
154 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83584
155 is possibly relevant.
156
157 * - Token pasting of ',' and __VA_ARGS__
158 - ARM64, X86_64
159 - See Section "6.21 Macros with a Variable Number of Arguments" of GCC_MANUAL.
160
161 * - Named variadic macro arguments
162 - ARM64, X86_64
163 - See Section "6.21 Macros with a Variable Number of Arguments" of GCC_MANUAL.
164
165 * - No arguments for '...' parameter of variadic macro
166 - ARM64, X86_64
167 - See Section "6.21 Macros with a Variable Number of Arguments" of GCC_MANUAL.
168
169 * - void function returning void expression
170 - ARM64, X86_64
171 - See the documentation for -Wreturn-type in Section "3.8 Options to Request or Suppress Warnings" of GCC_MANUAL.
172
173 * - GNU statement expressions from macro expansion
174 - ARM64, X86_64
175 - See Section "6.1 Statements and Declarations in Expressions" of GCC_MANUAL.
176
177 * - Invalid application of sizeof to a void type
178 - ARM64, X86_64
179 - See Section "6.24 Arithmetic on void- and Function-Pointers" of GCC_MANUAL.
180
181 * - Redeclaration of already-defined enum
182 - ARM64, X86_64
183 - See Section "6.49 Incomplete enum Types" of GCC_MANUAL.
184
185 * - struct with flexible array member nested in a struct
186 - ARM64, X86_64
187 - See Section "6.18 Arrays of Length Zero" of GCC_MANUAL.
188
189 * - struct with flexible array member used as an array element
190 - ARM64, X86_64
191 - See Section "6.18 Arrays of Length Zero" of GCC_MANUAL.
192
193 * - enumerator value outside the range of int
194 - ARM64, X86_64
195 - Non-documented GCC extension.
196
197 * - Extended integer types
198 - X86_64
199 - See Section "6.9 128-bit Integers" of GCC_MANUAL.
200
201 * - Designated initializer for a range of elements
202 - ARM64, X86_64
203 - See Section "6.29 Designated Initializers" of GCC_MANUAL
204
205 * - Signed << compiler-defined behavior
206 - All architectures
207 - See Section "4.5 Integers" of GCC_MANUAL. As an extension to the
208 C language, GCC does not use the latitude given in C99 and C11
209 only to treat certain aspects of signed << as undefined.
210
211 * - Signed >> acts on negative numbers by sign extension
212 - All architectures
213 - See Section "4.5 Integers" of GCC_MANUAL.
214
215 Translation Limits
216 __________________
217
218 The following table lists the translation limits that a toolchain has
219 to satisfy in order to translate Xen. The numbers given are a
220 compromise: on the one hand, many modern compilers have very generous
221 limits (in several cases, the only limitation is the amount of
222 available memory); on the other hand we prefer setting limits that are
223 not too high, because compilers do not have any obligation of
224 diagnosing when a limit has been exceeded, and not too low, so as to
225 avoid frequently updating this document. In the table, only the
226 limits that go beyond the minima specified by the relevant C Standard
227 are listed.
228
229 The table columns are as follows:
230
231 Limit
232 a terse description of the translation limit;
233 Architectures
234 a set relevant of Xen architectures;
235 Threshold
236 a value that the Xen project does not wish to exceed for that limit
237 (this is typically below, often much below what the translation
238 toolchain supports);
239 References
240 when available, references to the documentation providing evidence
241 that the translation toolchain honors the threshold (and more).
242
243 .. list-table::
244 :widths: 30 15 10 45
245 :header-rows: 1
246
247 * - Limit
248 - Architectures
249 - Threshold
250 - References
251
252 * - Size of an object
253 - ARM64, X86_64
254 - 8388608
255 - The maximum size of an object is defined in the MAX_SIZE macro, and for a 32 bit architecture is 8MB.
256 The maximum size for an array is defined in the PTRDIFF_MAX and in a 32 bit architecture is 2^30-1.
257 See occurrences of these macros in GCC_MANUAL.
258
259 * - Characters in one logical source line
260 - ARM64
261 - 5000
262 - See Section "11.2 Implementation limits" of CPP_MANUAL.
263
264 * - Characters in one logical source line
265 - X86_64
266 - 12000
267 - See Section "11.2 Implementation limits" of CPP_MANUAL.
268
269 * - Nesting levels for #include files
270 - ARM64
271 - 24
272 - See Section "11.2 Implementation limits" of CPP_MANUAL.
273
274 * - Nesting levels for #include files
275 - X86_64
276 - 32
277 - See Section "11.2 Implementation limits" of CPP_MANUAL.
278
279 * - case labels for a switch statement (excluding those for any nested switch statements)
280 - X86_64
281 - 1500
282 - See Section "4.12 Statements" of GCC_MANUAL.
283
284 * - Number of significant initial characters in an external identifier
285 - ARM64, X86_64
286 - 63
287 - See Section "4.3 Identifiers" of GCC_MANUAL.
288
289
290 Implementation-Defined Behaviors
291 ________________________________
292
293 The following table lists the C language implementation-defined behaviors
294 relevant for MISRA C:2012 Dir 1.1 upon which Xen may possibly depend.
295
296 The table columns are as follows:
297
298 I.-D.B.
299 a terse description of the implementation-defined behavior;
300 Architectures
301 a set relevant of Xen architectures;
302 Value(s)
303 for i.-d.b.'s with values, the values allowed;
304 References
305 when available, references to the documentation providing details
306 about how the i.-d.b. is resolved by the translation toolchain.
307
308 .. list-table::
309 :widths: 30 15 10 45
310 :header-rows: 1
311
312 * - I.-D.B.
313 - Architectures
314 - Value(s)
315 - References
316
317 * - Allowable bit-field types other than _Bool, signed int, and unsigned int
318 - ARM64, X86_64
319 - All explicitly signed integer types, all unsigned integer types,
320 and enumerations.
321 - See Section "4.9 Structures, Unions, Enumerations, and Bit-Fields".
322
323 * - #pragma preprocessing directive that is documented as causing translation failure or some other form of undefined behavior is encountered
324 - ARM64, X86_64
325 - pack, GCC visibility
326 - #pragma pack:
327 see Section "6.62.11 Structure-Layout Pragmas" of GCC_MANUAL.
328 #pragma GCC visibility:
329 see Section "6.62.14 Visibility Pragmas" of GCC_MANUAL.
330
331 * - The number of bits in a byte
332 - ARM64
333 - 8
334 - See Section "4.4 Characters" of GCC_MANUAL and Section "8.1 Data types" of ARM64_ABI_MANUAL.
335
336 * - The number of bits in a byte
337 - X86_64
338 - 8
339 - See Section "4.4 Characters" of GCC_MANUAL and Section "3.1.2 Data Representation" of X86_64_ABI_MANUAL.
340
341 * - Whether signed integer types are represented using sign and magnitude, two's complement, or one's complement, and whether the extraordinary value is a trap representation or an ordinary value
342 - ARM64, X86_64
343 - Two's complement
344 - See Section "4.5 Integers" of GCC_MANUAL.
345
346 * - Any extended integer types that exist in the implementation
347 - X86_64
348 - __uint128_t
349 - See Section "6.9 128-bit Integers" of GCC_MANUAL.
350
351 * - The number, order, and encoding of bytes in any object
352 - ARM64
353 -
354 - See Section "4.15 Architecture" of GCC_MANUAL and Chapter 5 "Data types and alignment" of ARM64_ABI_MANUAL.
355
356 * - The number, order, and encoding of bytes in any object
357 - X86_64
358 -
359 - See Section "4.15 Architecture" of GCC_MANUAL and Section "3.1.2 Data Representation" of X86_64_ABI_MANUAL.
360
361 * - Whether a bit-field can straddle a storage-unit boundary
362 - ARM64
363 -
364 - See Section "4.9 Structures, Unions, Enumerations, and Bit-Fields of GCC_MANUAL and Section "8.1.8 Bit-fields" of ARM64_ABI_MANUAL.
365
366 * - Whether a bit-field can straddle a storage-unit boundary
367 - X86_64
368 -
369 - See Section "4.9 Structures, Unions, Enumerations, and Bit-Fields" of GCC_MANUAL and Section "3.1.2 Data Representation" of X86_64_ABI_MANUAL.
370
371 * - The order of allocation of bit-fields within a unit
372 - ARM64
373 -
374 - See Section "4.9 Structures, Unions, Enumerations, and Bit-Fields of GCC_MANUAL and Section "8.1.8 Bit-fields" of ARM64_ABI_MANUAL.
375
376 * - The order of allocation of bit-fields within a unit
377 - X86_64
378 -
379 - See Section "4.9 Structures, Unions, Enumerations, and Bit-Fields" of GCC_MANUAL and Section "3.1.2 Data Representation" of X86_64_ABI_MANUAL.
380
381 * - What constitutes an access to an object that has volatile-qualified type
382 - ARM64, X86_64
383 -
384 - See Section "4.10 Qualifiers" of GCC_MANUAL.
385
386 * - The values or expressions assigned to the macros specified in the headers <float.h>, <limits.h>, and <stdint.h>
387 - ARM64
388 -
389 - See Section "4.15 Architecture" of GCC_MANUAL and Chapter 5 "Data types and alignment" of ARM64_ABI_MANUAL.
390
391 * - The values or expressions assigned to the macros specified in the headers <float.h>, <limits.h>, and <stdint.h>
392 - X86_64
393 -
394 - See Section "4.15 Architecture" of GCC_MANUAL and Section "3.1.2 Data Representation" of X86_64_ABI_MANUAL.
395
396 * - Character not in the basic source character set is encountered in a source file, except in an identifier, a character constant, a string literal, a header name, a comment, or a preprocessing token that is never converted to a token
397 - ARM64
398 - UTF-8
399 - See Section "1.1 Character sets" of CPP_MANUAL.
400 We assume the locale is not restricting any UTF-8 characters being part of the source character set.
401
402 * - The value of a char object into which has been stored any character other than a member of the basic execution character set
403 - ARM64
404 -
405 - See Section "4.4 Characters" of GCC_MANUAL and Section "8.1 Data types" of ARM64_ABI_MANUAL.
406
407 * - The value of a char object into which has been stored any character other than a member of the basic execution character set
408 - X86_64
409 -
410 - See Section "4.4 Characters" of GCC_MANUAL and Section "3.1.2 Data Representation" of X86_64_ABI_MANUAL.
411
412 * - The value of an integer character constant containing more than one character or containing a character or escape sequence that does not map to a single-byte execution character
413 - ARM64
414 -
415 - See Section "4.4 Characters" of GCC_MANUAL and Section "8.1 Data types" of ARM64_ABI_MANUAL.
416
417 * - The value of an integer character constant containing more than one character or containing a character or escape sequence that does not map to a single-byte execution character
418 - X86_64
419 -
420 - See Section "4.4 Characters" of GCC_MANUAL and Section "3.1.2 Data Representation" of X86_64_ABI_MANUAL.
421
422 * - The mapping of members of the source character set
423 - ARM64, X86_64
424 -
425 - See Section "4.4 Characters" of GCC_MANUAL and the documentation for -finput-charset=charset in the same manual.
426
427 * - The members of the source and execution character sets, except as explicitly specified in the Standard
428 - ARM64, X86_64
429 - UTF-8
430 - See Section "4.4 Characters" of GCC_MANUAL
431
432 * - The values of the members of the execution character set
433 - ARM64, X86_64
434 -
435 - See Section "4.4 Characters" of GCC_MANUAL and the documentation for -fexec-charset=charset in the same manual.
436
437 * - How a diagnostic is identified
438 - ARM64, X86_64
439 -
440 - See Section "4.1 Translation" of GCC_MANUAL.
441
442 * - The places that are searched for an included < > delimited header, and how the places are specified or the header is identified
443 - ARM64, X86_64
444 -
445 - See Chapter "2 Header Files" of CPP_MANUAL.
446
447 * - How the named source file is searched for in an included " " delimited header
448 - ARM64, X86_64
449 -
450 - See Chapter "2 Header Files" of CPP_MANUAL.
451
452 * - How sequences in both forms of header names are mapped to headers or external source file names
453 - ARM64, X86_64
454 -
455 - See Chapter "2 Header Files" of CPP_MANUAL.
456
457 * - Whether the # operator inserts a \ character before the \ character that begins a universal character name in a character constant or string literal
458 - ARM64, X86_64
459 -
460 - See Section "3.4 Stringizing" of CPP_MANUAL.
461
462 * - The current locale used to convert a wide string literal into corresponding wide character codes
463 - ARM64, X86_64
464 -
465 - See Section "4.4 Characters" of GCC_MANUAL and Section "11.1 Implementation-defined behavior" of CPP_MANUAL.
466
467 * - The value of a string literal containing a multibyte character or escape sequence not represented in the execution character set
468 - X86_64
469 -
470 - See Section "4.4 Characters" of GCC_MANUAL and Section "11.1 Implementation-defined behavior" of CPP_MANUAL.
471
472 * - The behavior on each recognized #pragma directive
473 - ARM64, X86_64
474 - pack, GCC visibility
475 - See Section "4.13 Preprocessing Directives" of GCC_MANUAL and Section "7 Pragmas" of CPP_MANUAL.
476
477 * - The method by which preprocessing tokens (possibly resulting from macro expansion) in a #include directive are combined into a header name
478 - X86_64
479 -
480 - See Section "4.13 Preprocessing Directives" of GCC_MANUAL and Section "11.1 Implementation-defined behavior" of CPP_MANUAL.
481
482
483 Sizes of Integer types
484 ______________________
485
486 Xen expects System V ABI on x86_64:
487 https://gitlab.com/x86-psABIs/x86-64-ABI
488
489 Xen expects AAPCS32 on ARMv8-A AArch32 and ARMv7-A:
490 https://github.com/ARM-software/abi-aa/blob/main/aapcs32/aapcs32.rst
491
492 Xen expects AAPCS64 LP64 on ARMv8-A AArch64:
493 https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst
494
495 A summary table of data types, sizes and alignment is below:
496
497 .. list-table::
498 :widths: 10 10 10 45
499 :header-rows: 1
500
501 * - Type
502 - Size
503 - Alignment
504 - Architectures
505
506 * - char
507 - 8 bits
508 - 8 bits
509 - x86_32, ARMv8-A AArch32, ARMv8-R AArch32, ARMv7-A, x86_64,
510 ARMv8-A AArch64, RV64, PPC64
511
512 * - short
513 - 16 bits
514 - 16 bits
515 - x86_32, ARMv8-A AArch32, ARMv8-R AArch32, ARMv7-A, x86_64,
516 ARMv8-A AArch64, RV64, PPC64
517
518 * - int
519 - 32 bits
520 - 32 bits
521 - x86_32, ARMv8-A AArch32, ARMv8-R AArch32, ARMv7-A, x86_64,
522 ARMv8-A AArch64, RV64, PPC64
523
524 * - long
525 - 32 bits
526 - 32 bits
527 - x86_32, ARMv8-A AArch32, ARMv8-R AArch32, ARMv7-A
528
529 * - long
530 - 64 bits
531 - 64 bits
532 - x86_64, ARMv8-A AArch64, RV64, PPC64
533
534 * - long long
535 - 64-bit
536 - 32-bit
537 - x86_32
538
539 * - long long
540 - 64-bit
541 - 64-bit
542 - x86_64, ARMv8-A AArch64, RV64, PPC64, ARMv8-A AArch32, ARMv8-R
543 AArch32, ARMv7-A
544
545 * - pointer
546 - 32-bit
547 - 32-bit
548 - x86_32, ARMv8-A AArch32, ARMv8-R AArch32, ARMv7-A
549
550 * - pointer
551 - 64-bit
552 - 64-bit
553 - x86_64, ARMv8-A AArch64, RV64, PPC64
554
555
556 END OF DOCUMENT.