| 1 |
.. _Implementation_of_Specific_Ada_Features: |
| 2 |
|
| 3 |
*************************************** |
| 4 |
Implementation of Specific Ada Features |
| 5 |
*************************************** |
| 6 |
|
| 7 |
This chapter describes the GNAT implementation of several Ada language |
| 8 |
facilities. |
| 9 |
|
| 10 |
.. _Machine_Code_Insertions: |
| 11 |
|
| 12 |
Machine Code Insertions |
| 13 |
======================= |
| 14 |
|
| 15 |
.. index:: Machine Code insertions |
| 16 |
|
| 17 |
Package ``Machine_Code`` provides machine code support as described |
| 18 |
in the Ada Reference Manual in two separate forms: |
| 19 |
|
| 20 |
* |
| 21 |
Machine code statements, consisting of qualified expressions that |
| 22 |
fit the requirements of RM section 13.8. |
| 23 |
* |
| 24 |
An intrinsic callable procedure, providing an alternative mechanism of |
| 25 |
including machine instructions in a subprogram. |
| 26 |
|
| 27 |
The two features are similar, and both are closely related to the mechanism |
| 28 |
provided by the asm instruction in the GNU C compiler. Full understanding |
| 29 |
and use of the facilities in this package requires understanding the asm |
| 30 |
instruction, see the section on Extended Asm in |
| 31 |
:title:`Using_the_GNU_Compiler_Collection_(GCC)`. |
| 32 |
|
| 33 |
Calls to the function ``Asm`` and the procedure ``Asm`` have identical |
| 34 |
semantic restrictions and effects as described below. Both are provided so |
| 35 |
that the procedure call can be used as a statement, and the function call |
| 36 |
can be used to form a code_statement. |
| 37 |
|
| 38 |
Consider this C ``asm`` instruction: |
| 39 |
|
| 40 |
:: |
| 41 |
|
| 42 |
asm ("fsinx %1 %0" : "=f" (result) : "f" (angle)); |
| 43 |
|
| 44 |
|
| 45 |
The equivalent can be written for GNAT as: |
| 46 |
|
| 47 |
.. code-block:: ada |
| 48 |
|
| 49 |
Asm ("fsinx %1 %0", |
| 50 |
My_Float'Asm_Output ("=f", result), |
| 51 |
My_Float'Asm_Input ("f", angle)); |
| 52 |
|
| 53 |
|
| 54 |
The first argument to ``Asm`` is the assembler template, and is |
| 55 |
identical to what is used in GNU C. This string must be a static |
| 56 |
expression. The second argument is the output operand list. It is |
| 57 |
either a single ``Asm_Output`` attribute reference, or a list of such |
| 58 |
references enclosed in parentheses (technically an array aggregate of |
| 59 |
such references). |
| 60 |
|
| 61 |
The ``Asm_Output`` attribute denotes a function that takes two |
| 62 |
parameters. The first is a string, the second is the name of a variable |
| 63 |
of the type designated by the attribute prefix. The first (string) |
| 64 |
argument is required to be a static expression and designates the |
| 65 |
constraint (see the section on Constraints in |
| 66 |
:title:`Using_the_GNU_Compiler_Collection_(GCC)`) |
| 67 |
for the parameter; e.g., what kind of register is required. The second |
| 68 |
argument is the variable to be written or updated with the |
| 69 |
result. The possible values for constraint are the same as those used in |
| 70 |
the RTL, and are dependent on the configuration file used to build the |
| 71 |
GCC back end. If there are no output operands, then this argument may |
| 72 |
either be omitted, or explicitly given as ``No_Output_Operands``. |
| 73 |
No support is provided for GNU C's symbolic names for output parameters. |
| 74 |
|
| 75 |
The second argument of ``my_float'Asm_Output`` functions as |
| 76 |
though it were an ``out`` parameter, which is a little curious, but |
| 77 |
all names have the form of expressions, so there is no syntactic |
| 78 |
irregularity, even though normally functions would not be permitted |
| 79 |
``out`` parameters. The third argument is the list of input |
| 80 |
operands. It is either a single ``Asm_Input`` attribute reference, or |
| 81 |
a list of such references enclosed in parentheses (technically an array |
| 82 |
aggregate of such references). |
| 83 |
|
| 84 |
The ``Asm_Input`` attribute denotes a function that takes two |
| 85 |
parameters. The first is a string, the second is an expression of the |
| 86 |
type designated by the prefix. The first (string) argument is required |
| 87 |
to be a static expression, and is the constraint for the parameter, |
| 88 |
(e.g., what kind of register is required). The second argument is the |
| 89 |
value to be used as the input argument. The possible values for the |
| 90 |
constraint are the same as those used in the RTL, and are dependent on |
| 91 |
the configuration file used to built the GCC back end. |
| 92 |
No support is provided for GNU C's symbolic names for input parameters. |
| 93 |
|
| 94 |
If there are no input operands, this argument may either be omitted, or |
| 95 |
explicitly given as ``No_Input_Operands``. The fourth argument, not |
| 96 |
present in the above example, is a list of register names, called the |
| 97 |
*clobber* argument. This argument, if given, must be a static string |
| 98 |
expression, and is a space or comma separated list of names of registers |
| 99 |
that must be considered destroyed as a result of the ``Asm`` call. If |
| 100 |
this argument is the null string (the default value), then the code |
| 101 |
generator assumes that no additional registers are destroyed. |
| 102 |
In addition to registers, the special clobbers ``memory`` and |
| 103 |
``cc`` as described in the GNU C docs are both supported. |
| 104 |
|
| 105 |
The fifth argument, not present in the above example, called the |
| 106 |
*volatile* argument, is by default ``False``. It can be set to |
| 107 |
the literal value ``True`` to indicate to the code generator that all |
| 108 |
optimizations with respect to the instruction specified should be |
| 109 |
suppressed, and in particular an instruction that has outputs |
| 110 |
will still be generated, even if none of the outputs are |
| 111 |
used. See :title:`Using_the_GNU_Compiler_Collection_(GCC)` |
| 112 |
for the full description. |
| 113 |
Generally it is strongly advisable to use Volatile for any ASM statement |
| 114 |
that is missing either input or output operands or to avoid unwanted |
| 115 |
optimizations. A warning is generated if this advice is not followed. |
| 116 |
|
| 117 |
No support is provided for GNU C's ``asm goto`` feature. |
| 118 |
|
| 119 |
The ``Asm`` subprograms may be used in two ways. First the procedure |
| 120 |
forms can be used anywhere a procedure call would be valid, and |
| 121 |
correspond to what the RM calls 'intrinsic' routines. Such calls can |
| 122 |
be used to intersperse machine instructions with other Ada statements. |
| 123 |
Second, the function forms, which return a dummy value of the limited |
| 124 |
private type ``Asm_Insn``, can be used in code statements, and indeed |
| 125 |
this is the only context where such calls are allowed. Code statements |
| 126 |
appear as aggregates of the form: |
| 127 |
|
| 128 |
.. code-block:: ada |
| 129 |
|
| 130 |
Asm_Insn'(Asm (...)); |
| 131 |
Asm_Insn'(Asm_Volatile (...)); |
| 132 |
|
| 133 |
In accordance with RM rules, such code statements are allowed only |
| 134 |
within subprograms whose entire body consists of such statements. It is |
| 135 |
not permissible to intermix such statements with other Ada statements. |
| 136 |
|
| 137 |
Typically the form using intrinsic procedure calls is more convenient |
| 138 |
and more flexible. The code statement form is provided to meet the RM |
| 139 |
suggestion that such a facility should be made available. The following |
| 140 |
is the exact syntax of the call to ``Asm``. As usual, if named notation |
| 141 |
is used, the arguments may be given in arbitrary order, following the |
| 142 |
normal rules for use of positional and named arguments: |
| 143 |
|
| 144 |
:: |
| 145 |
|
| 146 |
ASM_CALL ::= Asm ( |
| 147 |
[Template =>] static_string_EXPRESSION |
| 148 |
[,[Outputs =>] OUTPUT_OPERAND_LIST ] |
| 149 |
[,[Inputs =>] INPUT_OPERAND_LIST ] |
| 150 |
[,[Clobber =>] static_string_EXPRESSION ] |
| 151 |
[,[Volatile =>] static_boolean_EXPRESSION] ) |
| 152 |
|
| 153 |
OUTPUT_OPERAND_LIST ::= |
| 154 |
[PREFIX.]No_Output_Operands |
| 155 |
| OUTPUT_OPERAND_ATTRIBUTE |
| 156 |
| (OUTPUT_OPERAND_ATTRIBUTE {,OUTPUT_OPERAND_ATTRIBUTE}) |
| 157 |
|
| 158 |
OUTPUT_OPERAND_ATTRIBUTE ::= |
| 159 |
SUBTYPE_MARK'Asm_Output (static_string_EXPRESSION, NAME) |
| 160 |
|
| 161 |
INPUT_OPERAND_LIST ::= |
| 162 |
[PREFIX.]No_Input_Operands |
| 163 |
| INPUT_OPERAND_ATTRIBUTE |
| 164 |
| (INPUT_OPERAND_ATTRIBUTE {,INPUT_OPERAND_ATTRIBUTE}) |
| 165 |
|
| 166 |
INPUT_OPERAND_ATTRIBUTE ::= |
| 167 |
SUBTYPE_MARK'Asm_Input (static_string_EXPRESSION, EXPRESSION) |
| 168 |
|
| 169 |
The identifiers ``No_Input_Operands`` and ``No_Output_Operands`` |
| 170 |
are declared in the package ``Machine_Code`` and must be referenced |
| 171 |
according to normal visibility rules. In particular if there is no |
| 172 |
``use`` clause for this package, then appropriate package name |
| 173 |
qualification is required. |
| 174 |
|
| 175 |
.. _GNAT_Implementation_of_Tasking: |
| 176 |
|
| 177 |
GNAT Implementation of Tasking |
| 178 |
============================== |
| 179 |
|
| 180 |
This chapter outlines the basic GNAT approach to tasking (in particular, |
| 181 |
a multi-layered library for portability) and discusses issues related |
| 182 |
to compliance with the Real-Time Systems Annex. |
| 183 |
|
| 184 |
.. _Mapping_Ada_Tasks_onto_the_Underlying_Kernel_Threads: |
| 185 |
|
| 186 |
Mapping Ada Tasks onto the Underlying Kernel Threads |
| 187 |
---------------------------------------------------- |
| 188 |
|
| 189 |
GNAT's run-time support comprises two layers: |
| 190 |
|
| 191 |
* GNARL (GNAT Run-time Layer) |
| 192 |
* GNULL (GNAT Low-level Library) |
| 193 |
|
| 194 |
In GNAT, Ada's tasking services rely on a platform and OS independent |
| 195 |
layer known as GNARL. This code is responsible for implementing the |
| 196 |
correct semantics of Ada's task creation, rendezvous, protected |
| 197 |
operations etc. |
| 198 |
|
| 199 |
GNARL decomposes Ada's tasking semantics into simpler lower level |
| 200 |
operations such as create a thread, set the priority of a thread, |
| 201 |
yield, create a lock, lock/unlock, etc. The spec for these low-level |
| 202 |
operations constitutes GNULLI, the GNULL Interface. This interface is |
| 203 |
directly inspired from the POSIX real-time API. |
| 204 |
|
| 205 |
If the underlying executive or OS implements the POSIX standard |
| 206 |
faithfully, the GNULL Interface maps as is to the services offered by |
| 207 |
the underlying kernel. Otherwise, some target dependent glue code maps |
| 208 |
the services offered by the underlying kernel to the semantics expected |
| 209 |
by GNARL. |
| 210 |
|
| 211 |
Whatever the underlying OS (VxWorks, UNIX, Windows, etc.) the |
| 212 |
key point is that each Ada task is mapped on a thread in the underlying |
| 213 |
kernel. For example, in the case of VxWorks, one Ada task = one VxWorks task. |
| 214 |
|
| 215 |
In addition Ada task priorities map onto the underlying thread priorities. |
| 216 |
Mapping Ada tasks onto the underlying kernel threads has several advantages: |
| 217 |
|
| 218 |
* |
| 219 |
The underlying scheduler is used to schedule the Ada tasks. This |
| 220 |
makes Ada tasks as efficient as kernel threads from a scheduling |
| 221 |
standpoint. |
| 222 |
|
| 223 |
* |
| 224 |
Interaction with code written in C containing threads is eased |
| 225 |
since at the lowest level Ada tasks and C threads map onto the same |
| 226 |
underlying kernel concept. |
| 227 |
|
| 228 |
* |
| 229 |
When an Ada task is blocked during I/O the remaining Ada tasks are |
| 230 |
able to proceed. |
| 231 |
|
| 232 |
* |
| 233 |
On multiprocessor systems Ada tasks can execute in parallel. |
| 234 |
|
| 235 |
Some threads libraries offer a mechanism to fork a new process, with the |
| 236 |
child process duplicating the threads from the parent. |
| 237 |
GNAT does not |
| 238 |
support this functionality when the parent contains more than one task. |
| 239 |
|
| 240 |
.. index:: Forking a new process |
| 241 |
|
| 242 |
.. _Ensuring_Compliance_with_the_Real-Time_Annex: |
| 243 |
|
| 244 |
Ensuring Compliance with the Real-Time Annex |
| 245 |
-------------------------------------------- |
| 246 |
|
| 247 |
.. index:: Real-Time Systems Annex compliance |
| 248 |
|
| 249 |
Although mapping Ada tasks onto |
| 250 |
the underlying threads has significant advantages, it does create some |
| 251 |
complications when it comes to respecting the scheduling semantics |
| 252 |
specified in the real-time annex (Annex D). |
| 253 |
|
| 254 |
For instance the Annex D requirement for the ``FIFO_Within_Priorities`` |
| 255 |
scheduling policy states: |
| 256 |
|
| 257 |
*When the active priority of a ready task that is not running |
| 258 |
changes, or the setting of its base priority takes effect, the |
| 259 |
task is removed from the ready queue for its old active priority |
| 260 |
and is added at the tail of the ready queue for its new active |
| 261 |
priority, except in the case where the active priority is lowered |
| 262 |
due to the loss of inherited priority, in which case the task is |
| 263 |
added at the head of the ready queue for its new active priority.* |
| 264 |
|
| 265 |
While most kernels do put tasks at the end of the priority queue when |
| 266 |
a task changes its priority, (which respects the main |
| 267 |
FIFO_Within_Priorities requirement), almost none keep a thread at the |
| 268 |
beginning of its priority queue when its priority drops from the loss |
| 269 |
of inherited priority. |
| 270 |
|
| 271 |
As a result most vendors have provided incomplete Annex D implementations. |
| 272 |
|
| 273 |
The GNAT run-time, has a nice cooperative solution to this problem |
| 274 |
which ensures that accurate FIFO_Within_Priorities semantics are |
| 275 |
respected. |
| 276 |
|
| 277 |
The principle is as follows. When an Ada task T is about to start |
| 278 |
running, it checks whether some other Ada task R with the same |
| 279 |
priority as T has been suspended due to the loss of priority |
| 280 |
inheritance. If this is the case, T yields and is placed at the end of |
| 281 |
its priority queue. When R arrives at the front of the queue it |
| 282 |
executes. |
| 283 |
|
| 284 |
Note that this simple scheme preserves the relative order of the tasks |
| 285 |
that were ready to execute in the priority queue where R has been |
| 286 |
placed at the end. |
| 287 |
|
| 288 |
.. Support_for_Locking_Policies |
| 289 |
|
| 290 |
Support for Locking Policies |
| 291 |
---------------------------- |
| 292 |
|
| 293 |
This section specifies which policies specified by pragma Locking_Policy |
| 294 |
are supported on which platforms. |
| 295 |
|
| 296 |
GNAT supports the standard ``Ceiling_Locking`` policy, and the |
| 297 |
implementation defined ``Inheritance_Locking`` and |
| 298 |
``Concurrent_Readers_Locking`` policies. |
| 299 |
|
| 300 |
``Ceiling_Locking`` is supported on all platforms if the operating system |
| 301 |
supports it. In particular, ``Ceiling_Locking`` is not supported on |
| 302 |
VxWorks. |
| 303 |
``Inheritance_Locking`` is supported on |
| 304 |
Linux, |
| 305 |
Darwin (Mac OS X), |
| 306 |
LynxOS 178, |
| 307 |
and VxWorks. |
| 308 |
``Concurrent_Readers_Locking`` is supported on Linux. |
| 309 |
|
| 310 |
Notes about ``Ceiling_Locking`` on Linux: |
| 311 |
If the process is running as 'root', ceiling locking is used. |
| 312 |
If the capabilities facility is installed |
| 313 |
("sudo apt-get --assume-yes install libcap-dev" on Ubuntu, |
| 314 |
for example), |
| 315 |
and the program is linked against that library |
| 316 |
("-largs -lcap"), |
| 317 |
and the executable file has the cap_sys_nice capability |
| 318 |
("sudo /sbin/setcap cap_sys_nice=ep executable_file_name"), |
| 319 |
then ceiling locking is used. |
| 320 |
Otherwise, the ``Ceiling_Locking`` policy is ignored. |
| 321 |
|
| 322 |
.. _GNAT_Implementation_of_Shared_Passive_Packages: |
| 323 |
|
| 324 |
GNAT Implementation of Shared Passive Packages |
| 325 |
============================================== |
| 326 |
|
| 327 |
.. index:: Shared passive packages |
| 328 |
|
| 329 |
GNAT fully implements the :index:`pragma <pragma Shared_Passive>` |
| 330 |
``Shared_Passive`` for |
| 331 |
the purpose of designating shared passive packages. |
| 332 |
This allows the use of passive partitions in the |
| 333 |
context described in the Ada Reference Manual; i.e., for communication |
| 334 |
between separate partitions of a distributed application using the |
| 335 |
features in Annex E. |
| 336 |
|
| 337 |
.. index:: Annex E |
| 338 |
|
| 339 |
.. index:: Distribution Systems Annex |
| 340 |
|
| 341 |
However, the implementation approach used by GNAT provides for more |
| 342 |
extensive usage as follows: |
| 343 |
|
| 344 |
*Communication between separate programs* |
| 345 |
This allows separate programs to access the data in passive |
| 346 |
partitions, using protected objects for synchronization where |
| 347 |
needed. The only requirement is that the two programs have a |
| 348 |
common shared file system. It is even possible for programs |
| 349 |
running on different machines with different architectures |
| 350 |
(e.g., different endianness) to communicate via the data in |
| 351 |
a passive partition. |
| 352 |
|
| 353 |
*Persistence between program runs* |
| 354 |
The data in a passive package can persist from one run of a |
| 355 |
program to another, so that a later program sees the final |
| 356 |
values stored by a previous run of the same program. |
| 357 |
|
| 358 |
The implementation approach used is to store the data in files. A |
| 359 |
separate stream file is created for each object in the package, and |
| 360 |
an access to an object causes the corresponding file to be read or |
| 361 |
written. |
| 362 |
|
| 363 |
.. index:: SHARED_MEMORY_DIRECTORY environment variable |
| 364 |
|
| 365 |
The environment variable ``SHARED_MEMORY_DIRECTORY`` should be |
| 366 |
set to the directory to be used for these files. |
| 367 |
The files in this directory |
| 368 |
have names that correspond to their fully qualified names. For |
| 369 |
example, if we have the package |
| 370 |
|
| 371 |
.. code-block:: ada |
| 372 |
|
| 373 |
package X is |
| 374 |
pragma Shared_Passive (X); |
| 375 |
Y : Integer; |
| 376 |
Z : Float; |
| 377 |
end X; |
| 378 |
|
| 379 |
and the environment variable is set to ``/stemp/``, then the files created |
| 380 |
will have the names: |
| 381 |
|
| 382 |
:: |
| 383 |
|
| 384 |
/stemp/x.y |
| 385 |
/stemp/x.z |
| 386 |
|
| 387 |
|
| 388 |
These files are created when a value is initially written to the object, and |
| 389 |
the files are retained until manually deleted. This provides the persistence |
| 390 |
semantics. If no file exists, it means that no partition has assigned a value |
| 391 |
to the variable; in this case the initial value declared in the package |
| 392 |
will be used. This model ensures that there are no issues in synchronizing |
| 393 |
the elaboration process, since elaboration of passive packages elaborates the |
| 394 |
initial values, but does not create the files. |
| 395 |
|
| 396 |
The files are written using normal ``Stream_IO`` access. |
| 397 |
If you want to be able |
| 398 |
to communicate between programs or partitions running on different |
| 399 |
architectures, then you should use the XDR versions of the stream attribute |
| 400 |
routines, since these are architecture independent. |
| 401 |
|
| 402 |
If active synchronization is required for access to the variables in the |
| 403 |
shared passive package, then as described in the Ada Reference Manual, the |
| 404 |
package may contain protected objects used for this purpose. In this case |
| 405 |
a lock file (whose name is :file:`___lock` (three underscores) |
| 406 |
is created in the shared memory directory. |
| 407 |
|
| 408 |
.. index:: ___lock file (for shared passive packages) |
| 409 |
|
| 410 |
This is used to provide the required locking |
| 411 |
semantics for proper protected object synchronization. |
| 412 |
|
| 413 |
.. _Code_Generation_for_Array_Aggregates: |
| 414 |
|
| 415 |
Code Generation for Array Aggregates |
| 416 |
==================================== |
| 417 |
|
| 418 |
Aggregates have a rich syntax and allow the user to specify the values of |
| 419 |
complex data structures by means of a single construct. As a result, the |
| 420 |
code generated for aggregates can be quite complex and involve loops, case |
| 421 |
statements and multiple assignments. In the simplest cases, however, the |
| 422 |
compiler will recognize aggregates whose components and constraints are |
| 423 |
fully static, and in those cases the compiler will generate little or no |
| 424 |
executable code. The following is an outline of the code that GNAT generates |
| 425 |
for various aggregate constructs. For further details, you will find it |
| 426 |
useful to examine the output produced by the -gnatG flag to see the expanded |
| 427 |
source that is input to the code generator. You may also want to examine |
| 428 |
the assembly code generated at various levels of optimization. |
| 429 |
|
| 430 |
The code generated for aggregates depends on the context, the component values, |
| 431 |
and the type. In the context of an object declaration the code generated is |
| 432 |
generally simpler than in the case of an assignment. As a general rule, static |
| 433 |
component values and static subtypes also lead to simpler code. |
| 434 |
|
| 435 |
.. _Static_constant_aggregates_with_static_bounds: |
| 436 |
|
| 437 |
Static constant aggregates with static bounds |
| 438 |
--------------------------------------------- |
| 439 |
|
| 440 |
For the declarations: |
| 441 |
|
| 442 |
.. code-block:: ada |
| 443 |
|
| 444 |
type One_Dim is array (1..10) of integer; |
| 445 |
ar0 : constant One_Dim := (1, 2, 3, 4, 5, 6, 7, 8, 9, 0); |
| 446 |
|
| 447 |
|
| 448 |
GNAT generates no executable code: the constant ar0 is placed in static memory. |
| 449 |
The same is true for constant aggregates with named associations: |
| 450 |
|
| 451 |
|
| 452 |
.. code-block:: ada |
| 453 |
|
| 454 |
Cr1 : constant One_Dim := (4 => 16, 2 => 4, 3 => 9, 1 => 1, 5 .. 10 => 0); |
| 455 |
Cr3 : constant One_Dim := (others => 7777); |
| 456 |
|
| 457 |
|
| 458 |
The same is true for multidimensional constant arrays such as: |
| 459 |
|
| 460 |
.. code-block:: ada |
| 461 |
|
| 462 |
type two_dim is array (1..3, 1..3) of integer; |
| 463 |
Unit : constant two_dim := ( (1,0,0), (0,1,0), (0,0,1)); |
| 464 |
|
| 465 |
|
| 466 |
The same is true for arrays of one-dimensional arrays: the following are |
| 467 |
static: |
| 468 |
|
| 469 |
|
| 470 |
.. code-block:: ada |
| 471 |
|
| 472 |
type ar1b is array (1..3) of boolean; |
| 473 |
type ar_ar is array (1..3) of ar1b; |
| 474 |
None : constant ar1b := (others => false); -- fully static |
| 475 |
None2 : constant ar_ar := (1..3 => None); -- fully static |
| 476 |
|
| 477 |
|
| 478 |
However, for multidimensional aggregates with named associations, GNAT will |
| 479 |
generate assignments and loops, even if all associations are static. The |
| 480 |
following two declarations generate a loop for the first dimension, and |
| 481 |
individual component assignments for the second dimension: |
| 482 |
|
| 483 |
|
| 484 |
.. code-block:: ada |
| 485 |
|
| 486 |
Zero1: constant two_dim := (1..3 => (1..3 => 0)); |
| 487 |
Zero2: constant two_dim := (others => (others => 0)); |
| 488 |
|
| 489 |
|
| 490 |
.. _Constant_aggregates_with_unconstrained_nominal_types: |
| 491 |
|
| 492 |
Constant aggregates with unconstrained nominal types |
| 493 |
---------------------------------------------------- |
| 494 |
|
| 495 |
In such cases the aggregate itself establishes the subtype, so that |
| 496 |
associations with ``others`` cannot be used. GNAT determines the |
| 497 |
bounds for the actual subtype of the aggregate, and allocates the |
| 498 |
aggregate statically as well. No code is generated for the following: |
| 499 |
|
| 500 |
|
| 501 |
.. code-block:: ada |
| 502 |
|
| 503 |
type One_Unc is array (natural range <>) of integer; |
| 504 |
Cr_Unc : constant One_Unc := (12,24,36); |
| 505 |
|
| 506 |
|
| 507 |
.. _Aggregates_with_static_bounds: |
| 508 |
|
| 509 |
Aggregates with static bounds |
| 510 |
----------------------------- |
| 511 |
|
| 512 |
In all previous examples the aggregate was the initial (and immutable) value |
| 513 |
of a constant. If the aggregate initializes a variable, then code is generated |
| 514 |
for it as a combination of individual assignments and loops over the target |
| 515 |
object. The declarations |
| 516 |
|
| 517 |
|
| 518 |
.. code-block:: ada |
| 519 |
|
| 520 |
Cr_Var1 : One_Dim := (2, 5, 7, 11, 0, 0, 0, 0, 0, 0); |
| 521 |
Cr_Var2 : One_Dim := (others > -1); |
| 522 |
|
| 523 |
|
| 524 |
generate the equivalent of |
| 525 |
|
| 526 |
|
| 527 |
.. code-block:: ada |
| 528 |
|
| 529 |
Cr_Var1 (1) := 2; |
| 530 |
Cr_Var1 (2) := 3; |
| 531 |
Cr_Var1 (3) := 5; |
| 532 |
Cr_Var1 (4) := 11; |
| 533 |
|
| 534 |
for I in Cr_Var2'range loop |
| 535 |
Cr_Var2 (I) := -1; |
| 536 |
end loop; |
| 537 |
|
| 538 |
|
| 539 |
.. _Aggregates_with_nonstatic_bounds: |
| 540 |
|
| 541 |
Aggregates with nonstatic bounds |
| 542 |
--------------------------------- |
| 543 |
|
| 544 |
If the bounds of the aggregate are not statically compatible with the bounds |
| 545 |
of the nominal subtype of the target, then constraint checks have to be |
| 546 |
generated on the bounds. For a multidimensional array, constraint checks may |
| 547 |
have to be applied to sub-arrays individually, if they do not have statically |
| 548 |
compatible subtypes. |
| 549 |
|
| 550 |
.. _Aggregates_in_assignment_statements: |
| 551 |
|
| 552 |
Aggregates in assignment statements |
| 553 |
----------------------------------- |
| 554 |
|
| 555 |
In general, aggregate assignment requires the construction of a temporary, |
| 556 |
and a copy from the temporary to the target of the assignment. This is because |
| 557 |
it is not always possible to convert the assignment into a series of individual |
| 558 |
component assignments. For example, consider the simple case: |
| 559 |
|
| 560 |
|
| 561 |
.. code-block:: ada |
| 562 |
|
| 563 |
A := (A(2), A(1)); |
| 564 |
|
| 565 |
|
| 566 |
This cannot be converted into: |
| 567 |
|
| 568 |
|
| 569 |
.. code-block:: ada |
| 570 |
|
| 571 |
A(1) := A(2); |
| 572 |
A(2) := A(1); |
| 573 |
|
| 574 |
|
| 575 |
So the aggregate has to be built first in a separate location, and then |
| 576 |
copied into the target. GNAT recognizes simple cases where this intermediate |
| 577 |
step is not required, and the assignments can be performed in place, directly |
| 578 |
into the target. The following sufficient criteria are applied: |
| 579 |
|
| 580 |
* |
| 581 |
The bounds of the aggregate are static, and the associations are static. |
| 582 |
* |
| 583 |
The components of the aggregate are static constants, names of |
| 584 |
simple variables that are not renamings, or expressions not involving |
| 585 |
indexed components whose operands obey these rules. |
| 586 |
|
| 587 |
If any of these conditions are violated, the aggregate will be built in |
| 588 |
a temporary (created either by the front-end or the code generator) and then |
| 589 |
that temporary will be copied onto the target. |
| 590 |
|
| 591 |
.. _The_Size_of_Discriminated_Records_with_Default_Discriminants: |
| 592 |
|
| 593 |
The Size of Discriminated Records with Default Discriminants |
| 594 |
============================================================ |
| 595 |
|
| 596 |
If a discriminated type ``T`` has discriminants with default values, it is |
| 597 |
possible to declare an object of this type without providing an explicit |
| 598 |
constraint: |
| 599 |
|
| 600 |
|
| 601 |
.. code-block:: ada |
| 602 |
|
| 603 |
type Size is range 1..100; |
| 604 |
|
| 605 |
type Rec (D : Size := 15) is record |
| 606 |
Name : String (1..D); |
| 607 |
end T; |
| 608 |
|
| 609 |
Word : Rec; |
| 610 |
|
| 611 |
|
| 612 |
Such an object is said to be *unconstrained*. |
| 613 |
The discriminant of the object |
| 614 |
can be modified by a full assignment to the object, as long as it preserves the |
| 615 |
relation between the value of the discriminant, and the value of the components |
| 616 |
that depend on it: |
| 617 |
|
| 618 |
|
| 619 |
.. code-block:: ada |
| 620 |
|
| 621 |
Word := (3, "yes"); |
| 622 |
|
| 623 |
Word := (5, "maybe"); |
| 624 |
|
| 625 |
Word := (5, "no"); -- raises Constraint_Error |
| 626 |
|
| 627 |
In order to support this behavior efficiently, an unconstrained object is |
| 628 |
given the maximum size that any value of the type requires. In the case |
| 629 |
above, ``Word`` has storage for the discriminant and for |
| 630 |
a ``String`` of length 100. |
| 631 |
It is important to note that unconstrained objects do not require dynamic |
| 632 |
allocation. It would be an improper implementation to place on the heap those |
| 633 |
components whose size depends on discriminants. (This improper implementation |
| 634 |
was used by some Ada83 compilers, where the ``Name`` component above |
| 635 |
would have |
| 636 |
been stored as a pointer to a dynamic string). Following the principle that |
| 637 |
dynamic storage management should never be introduced implicitly, |
| 638 |
an Ada compiler should reserve the full size for an unconstrained declared |
| 639 |
object, and place it on the stack. |
| 640 |
|
| 641 |
This maximum size approach |
| 642 |
has been a source of surprise to some users, who expect the default |
| 643 |
values of the discriminants to determine the size reserved for an |
| 644 |
unconstrained object: "If the default is 15, why should the object occupy |
| 645 |
a larger size?" |
| 646 |
The answer, of course, is that the discriminant may be later modified, |
| 647 |
and its full range of values must be taken into account. This is why the |
| 648 |
declaration: |
| 649 |
|
| 650 |
|
| 651 |
.. code-block:: ada |
| 652 |
|
| 653 |
type Rec (D : Positive := 15) is record |
| 654 |
Name : String (1..D); |
| 655 |
end record; |
| 656 |
|
| 657 |
Too_Large : Rec; |
| 658 |
|
| 659 |
is flagged by the compiler with a warning: |
| 660 |
an attempt to create ``Too_Large`` will raise ``Storage_Error``, |
| 661 |
because the required size includes ``Positive'Last`` |
| 662 |
bytes. As the first example indicates, the proper approach is to declare an |
| 663 |
index type of 'reasonable' range so that unconstrained objects are not too |
| 664 |
large. |
| 665 |
|
| 666 |
One final wrinkle: if the object is declared to be ``aliased``, or if it is |
| 667 |
created in the heap by means of an allocator, then it is *not* |
| 668 |
unconstrained: |
| 669 |
it is constrained by the default values of the discriminants, and those values |
| 670 |
cannot be modified by full assignment. This is because in the presence of |
| 671 |
aliasing all views of the object (which may be manipulated by different tasks, |
| 672 |
say) must be consistent, so it is imperative that the object, once created, |
| 673 |
remain invariant. |
| 674 |
|
| 675 |
.. _Image_Values_For_Nonscalar_Types: |
| 676 |
|
| 677 |
Image Values For Nonscalar Types |
| 678 |
================================ |
| 679 |
|
| 680 |
Ada 2022 defines the Image, Wide_Image, and Wide_Wide image attributes |
| 681 |
for nonscalar types; earlier Ada versions defined these attributes only |
| 682 |
for scalar types. Ada RM 4.10 provides some general guidance regarding |
| 683 |
the default implementation of these attributes and the GNAT compiler |
| 684 |
follows that guidance. However, beyond that the precise details of the |
| 685 |
image text generated in these cases are deliberately not documented and are |
| 686 |
subject to change. In particular, users should not rely on formatting details |
| 687 |
(such as spaces or line breaking), record field order, image values for access |
| 688 |
types, image values for types that have ancestor or subcomponent types |
| 689 |
declared in non-Ada2022 code, image values for predefined types, or the |
| 690 |
compiler's choices regarding the implementation permissions described in |
| 691 |
Ada RM 4.10. This list is not intended to be exhaustive. If more precise |
| 692 |
control of image text is required for some type T, then T'Put_Image should be |
| 693 |
explicitly specified. |
| 694 |
|
| 695 |
.. _Strict_Conformance_to_the_Ada_Reference_Manual: |
| 696 |
|
| 697 |
Strict Conformance to the Ada Reference Manual |
| 698 |
============================================== |
| 699 |
|
| 700 |
The dynamic semantics defined by the Ada Reference Manual impose a set of |
| 701 |
run-time checks to be generated. By default, the GNAT compiler will insert many |
| 702 |
run-time checks into the compiled code, including most of those required by the |
| 703 |
Ada Reference Manual. However, there are two checks that are not enabled in |
| 704 |
the default mode for efficiency reasons: checks for access before elaboration |
| 705 |
on subprogram calls, and stack overflow checking (most operating systems do not |
| 706 |
perform this check by default). |
| 707 |
|
| 708 |
Strict conformance to the Ada Reference Manual can be achieved by adding two |
| 709 |
compiler options for dynamic checks for access-before-elaboration on subprogram |
| 710 |
calls and generic instantiations (*-gnatE*), and stack overflow checking |
| 711 |
(*-fstack-check*). |
| 712 |
|
| 713 |
Note that the result of a floating point arithmetic operation in overflow and |
| 714 |
invalid situations, when the ``Machine_Overflows`` attribute of the result |
| 715 |
type is ``False``, is to generate IEEE NaN and infinite values. This is the |
| 716 |
case for machines compliant with the IEEE floating-point standard, but on |
| 717 |
machines that are not fully compliant with this standard, such as Alpha, the |
| 718 |
*-mieee* compiler flag must be used for achieving IEEE confirming |
| 719 |
behavior (although at the cost of a significant performance penalty), so |
| 720 |
infinite and NaN values are properly generated. |