Ada Bare Bones: Difference between revisions

Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content deleted Content added
No edit summary
Line 50: Line 50:
</source>
</source>


==Files==


===Exception handling===
===gnat.adc===

This file in the root directory of the build tells GNAT there are some configuration pragmas to apply to the build. These pragmas can also be placed at the start of your custom sytem.ads (see below), but we'll place them here for now.

'''Note:''' ''Do not use '''pragma No_Run_Time''' as it is obsolete and has been for a number of years now!''

What these do is to tell GNAT how much of the RTS we can use in our kernel, which is not a lot really.

<source lang="ada">
pragma Discard_Names;
pragma Restrictions (No_Enumeration_Maps);
pragma Normalize_Scalars;
pragma Restrictions (No_Exception_Propagation);
pragma Restrictions (No_Finalization);
pragma Restrictions (No_Tasking);
pragma Restrictions (No_Protected_Types);
pragma Restrictions (No_Delay);
pragma Restrictions (No_Recursion);
pragma Restrictions (No_Allocators);
pragma Restrictions (No_Dispatch);
pragma Restrictions (No_Implicit_Dynamic_Code);
pragma Restrictions (No_Secondary_Stack);
</source>

By passing the '''-r''' flag to the binder (inside the bare_bones.gpr file), the binder will list further restrictions you can apply to enforce further checks.

<source lang="ada">
package Binder is
for Default_Switches ("Ada") use ("-r");
end Binder;
</source>

====Discard_Names====

In Ada, the compiler generates strings for various data types, e.g. enumerations, these strings can then be used in I/O.

<source lang="ada">
type Fruit is (Orange, Banana, Apple);
-- Ada defines the following strings, "Orange", "Banana" and "Apple" in an array.

-- These strings can be accessed using the 'Image attribute, as in
Put (Fruit'Image (Orange));
-- Prints "Orange" to the console.
</source>

This pragma tells the compiler not to generate these tables.

====Normalize_Scalars====

Forces all scalars to be initialised, see the latest [http://gcc.gnu.org/onlinedocs/gcc-4.7.0/gnat_rm/Pragma-Normalize_005fScalars.html#Pragma-Normalize_005fScalars GNAT RM:Normalize_Scalars] for more information.

====No_Exception_Propagation====

This forces the compiler to disallow any attempt to raise an exception over a subprogram boundary. All exceptions are caught with the Last_Chance_Handler subprogram. See [http://gcc.gnu.org/onlinedocs/gcc-4.7.0/gnat_rm/No_005fException_005fPropagation.html#No_005fException_005fPropagation GNAT RM:No_Exception_Propagation] for more information.

The [http://docs.adacore.com/gnat-hie-docs/html/gnathie_ug_3.html#SEC8 GNAT High Integrity Edition] documentation states the folowing:

<blockquote>
Exception declarations and raise statements are still permitted under this restriction. A raise statement is compiled into a call of __gnat_last_chance_handler.
</blockquote>

I have been unable, thus far, to raise my own exceptions, although I can declare one at library level. On placing a declaration inside console.ads:

<source lang="ada">
TE : exception; -- A badly named exception.
</source>

and raising inside bare_bones.adb:

<source lang="ada">
raise Console.TE;
exception
when Console.TE =>
Put ("TE caught", 1, 2);
</source>

and upon compiling, I get the following:

<pre>
bare_bones.adb:17:04: construct not allowed in configurable run-time mode
bare_bones.adb:17:04: file a-except.ads not found
bare_bones.adb:17:04: entity "Ada.Exceptions.Raise_Exception" not available
gnatmake: "/home/laguest/src/mine/bare_bones/src/bare_bones.adb" compilation error
make: *** [disk/boot/bare_bones] Error 4
</pre>

so the compiler is looking for the Ada.Exceptions package which defines the normal language standard exception handling, not the cut down version we are using here. Do the same with one of the language defined exceptions works fine. ''I will file a bug at FSF for this as I believe it to be incorrect''.


I have based my knowledge of this kind of work on GNAT's High Integrity Edition manual, in which is hints that at this level of work, a programmer should be able to declare their own exceptions and be able to raise them. I have had it confirmed via a [http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53684 bug I filed] that this is not the case. Any attempt to do this requests that a package from the RTS called Ada.Exceptions be present, I have experimented by creating a version of this package:
I have had it confirmed via a [http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53684 bug I filed] that the programmer is not allowed to declare their own exceptions and catch them. I have experimented by creating a version of the Ada.Exceptions (a-except.ad[sb]) package:


<source lang="ada">
<source lang="ada">
Line 154: Line 241:
So the net effect of this is to not bother with this package and only use the language defined exceptions for bare metal programming.
So the net effect of this is to not bother with this package and only use the language defined exceptions for bare metal programming.


====No_Exception_Registration====
==Files==


Ensures no stream operations are performed on types declared in Ada.Exceptions, see [http://gcc.gnu.org/onlinedocs/gcc-4.7.0/gnat_rm/No_005fException_005fRegistration.html#No_005fException_005fRegistration GNAT RM:No_Exception_Registration] for more information.
===gnat.adc===

This file in the root directory of the build tells GNAT there are some configuration pragmas to apply to the build. These pragmas can also be placed at the start of your custom sytem.ads (see below), but we'll place them here for now.

'''Note:''' ''Do not use '''pragma No_Run_Time''' as it is obsolete and has been for a number of years now!''

What these do is to tell GNAT how much of the RTS we can use in our kernel, which is not a lot really.

<source lang="ada">
pragma Discard_Names;
pragma Restrictions (No_Enumeration_Maps);
pragma Normalize_Scalars;
pragma Restrictions (No_Exception_Propagation);
pragma Restrictions (No_Finalization);
pragma Restrictions (No_Tasking);
pragma Restrictions (No_Protected_Types);
pragma Restrictions (No_Delay);
pragma Restrictions (No_Recursion);
pragma Restrictions (No_Allocators);
pragma Restrictions (No_Dispatch);
pragma Restrictions (No_Implicit_Dynamic_Code);
pragma Restrictions (No_Secondary_Stack);
</source>

By passing the '''-r''' flag to the binder (inside the bare_bones.gpr file), the binder will list further restrictions you can apply to enforce further checks.

<source lang="ada">
package Binder is
for Default_Switches ("Ada") use ("-r");
end Binder;
</source>

====Discard_Names====

In Ada, the compiler generates strings for various data types, e.g. enumerations, these strings can then be used in I/O.

<source lang="ada">
type Fruit is (Orange, Banana, Apple);
-- Ada defines the following strings, "Orange", "Banana" and "Apple" in an array.

-- These strings can be accessed using the 'Image attribute, as in
Put (Fruit'Image (Orange));
-- Prints "Orange" to the console.
</source>

This pragma tells the compiler not to generate these tables.

====Normalize_Scalars====

Forces all scalars to be initialised, see the latest [http://gcc.gnu.org/onlinedocs/gcc-4.7.0/gnat_rm/Pragma-Normalize_005fScalars.html#Pragma-Normalize_005fScalars GNAT RM:Normalize_Scalars] for more information.

====No_Exception_Propagation====

This forces the compiler to disallow any attempt to raise an exception over a subprogram boundary. All exceptions are caught with the Last_Chance_Handler subprogram. See [http://gcc.gnu.org/onlinedocs/gcc-4.7.0/gnat_rm/No_005fException_005fPropagation.html#No_005fException_005fPropagation GNAT RM:No_Exception_Propagation] for more information.

The [http://docs.adacore.com/gnat-hie-docs/html/gnathie_ug_3.html#SEC8 GNAT High Integrity Edition] documentation states the folowing:

<blockquote>
Exception declarations and raise statements are still permitted under this restriction. A raise statement is compiled into a call of __gnat_last_chance_handler.
</blockquote>

I have been unable, thus far, to raise my own exceptions, although I can declare one at library level. On placing a declaration inside console.ads:

<source lang="ada">
TE : exception; -- A badly named exception.
</source>

and raising inside bare_bones.adb:

<source lang="ada">
raise Console.TE;
exception
when Console.TE =>
Put ("TE caught", 1, 2);
</source>

and upon compiling, I get the following:

<pre>
bare_bones.adb:17:04: construct not allowed in configurable run-time mode
bare_bones.adb:17:04: file a-except.ads not found
bare_bones.adb:17:04: entity "Ada.Exceptions.Raise_Exception" not available
gnatmake: "/home/laguest/src/mine/bare_bones/src/bare_bones.adb" compilation error
make: *** [disk/boot/bare_bones] Error 4
</pre>

so the compiler is looking for the Ada.Exceptions package which defines the normal language standard exception handling, not the cut down version we are using here. Do the same with one of the language defined exceptions works fine. ''I will file a bug at FSF for this as I believe it to be incorrect''.


====No_Finalization====
====No_Finalization====