Skip to main content

B.2 The Package Interfaces

danger

This Reference Manual output has not been verified, and may contain omissions or errors. Report any problems on the tracking issue

1

Package Interfaces is the parent of several library packages that declare types and other entities useful for interfacing to foreign languages. It also contains some implementation-defined types that are useful across more than one language (in particular for interfacing to assembly language).

1.a
implementation defined

The contents of the visible part of package Interfaces and its language-defined descendants.

Static Semantics

2

The library package Interfaces has the following skeletal declaration:

3/5

package Interfaces with Pure is 4 type Integer_n is range -2**(n-1) .. 2**(n-1) - 1; --2's complement 5 type Unsigned_n is mod 2**n; 6 function Shift_Left (Value : Unsigned_n; Amount : Natural) return Unsigned_n; function Shift_Right (Value : Unsigned_n; Amount : Natural) return Unsigned_n; function Shift_Right_Arithmetic (Value : Unsigned_n; Amount : Natural) return Unsigned_n; function Rotate_Left (Value : Unsigned_n; Amount : Natural) return Unsigned_n; function Rotate_Right (Value : Unsigned_n; Amount : Natural) return Unsigned_n; ... end Interfaces;

Implementation Requirements

7

An implementation shall provide the following declarations in the visible part of package Interfaces:

8
  • Signed and modular integer types of n bits, if supported by the target architecture, for each n that is at least the size of a storage element and that is a factor of the word size. The names of these types are of the form Integer_n for the signed types, and Unsigned_n for the modular types;
8.a
ramification

For example, for a typical 32-bit machine the corresponding types might be Integer_8, Unsigned_8, Integer_16, Unsigned_16, Integer_32, and Unsigned_32.

8.b

The wording above implies, for example, that Integer_16'Size = Unsigned_16'Size = 16. Unchecked conversions between same-Sized types will work as expected.

9
  • For each such modular type in Interfaces, shifting and rotating subprograms as specified in the declaration of Interfaces above. These subprograms are Intrinsic. They operate on a bit-by-bit basis, using the binary representation of the value of the operands to yield a binary representation for the result. The Amount parameter gives the number of bits by which to shift or rotate. For shifting, zero bits are shifted in, except in the case of Shift_Right_Arithmetic, where one bits are shifted in if Value is at least half the modulus.
9.a
reason

We considered making shifting and rotating be primitive operations of all modular types. However, it is a design principle of Ada that all predefined operations should be operators (not functions named by identifiers). (Note that an early version of Ada had "abs" as an identifier, but it was changed to a reserved word operator before standardization of Ada 83.) This is important because the implicit declarations would hide nonoverloadable declarations with the same name, whereas operators are always overloadable. Therefore, we would have had to make shift and rotate into reserved words, which would have been upward incompatible, or else invent new operator symbols, which seemed like too much mechanism.

9.b/5

To be honest: “Shifting” and “rotating” have the conventional meaning. Neither of these terms is usefully defined by the usual normative references of the Reference Manual, so we provide pseudo-code here to describe the intended semantics of the above wording (all operations in these examples are using modular semantics).

9.c/5

function Rotate_Left (Value : Unsigned_n; Amount : Natural) return Unsigned_n is Result : Unsigned_n := Value; Bit : Unsigned_n range 0 .. 1; begin for Count in 1 .. Amount loop Bit := Result/2**(n-1); -- High-bit of Result Result := Result*2 + Bit; end loop; return Result; end Rotate_Left; 9.d/5 function Rotate_Right (Value : Unsigned_n; Amount : Natural) return Unsigned_n is Result : Unsigned_n := Value; Bit : Unsigned_n range 0 .. 1; begin for Count in 1 .. Amount loop Bit := Result mod 2; -- Low-bit of Result Result := Result/2 + (Bit * 2**(n-1)); end loop; return Result; end Rotate_Right; 9.e/5 function Shift_Left (Value : Unsigned_n; Amount : Natural) return Unsigned_n is Result : Unsigned_n := Value; begin for Count in 1 .. Amount loop Result := Result * 2; end loop; return Result; end Shift_Left; 9.f/5 function Shift_Right (Value : Unsigned_n; Amount : Natural) return Unsigned_n is Result : Unsigned_n := Value; begin for Count in 1 .. Amount loop Result := Result / 2; end loop; return Result; end Shift_Right; 9.g/5 function Shift_Right_Arithmetic (Value : Unsigned_n; Amount : Natural) return Unsigned_n is Result : Unsigned_n := Value; Neg : constant Boolean := Result/2**(n-1) = 1; -- High-bit of Result begin for Count in 1 .. Amount loop if Neg then Result := Result / 2 + 2**(n-1); else Result := Result / 2; end if; end loop; return Result; end Shift_Right_Arithmetic;

9.h/5

These generally correspond to machine instructions, although there may not be an exact match in terms of boundary conditions, as Ada requires the correct result to be produced for all values of Amount.

10
  • Floating point types corresponding to each floating point format fully supported by the hardware.
10.a
implementation note

The names for these floating point types are not specified. However, if IEEE arithmetic is supported, then the names should be IEEE_Float_32 and IEEE_Float_64 for single and double precision, respectively.

Implementation Permissions

11

An implementation may provide implementation-defined library units that are children of Interfaces, and may add declarations to the visible part of Interfaces in addition to the ones defined above.

11.a/2
implementation defined

Implementation-defined children of package Interfaces.

11.1/3

A child package of package Interfaces with the name of a convention may be provided independently of whether the convention is supported by the Convention aspect and vice versa. Such a child package should contain any declarations that would be useful for interfacing to the language (implementation) represented by the convention. Any declarations useful for interfacing to any language on the given hardware architecture should be provided directly in Interfaces.

11.b/2
ramification

For example, package Interfaces.XYZ_Pascal might contain declarations of types that match the data types provided by the XYZ implementation of Pascal, so that it will be more convenient to pass parameters to a subprogram whose convention is XYZ_Pascal.

Implementation Advice

12/2

This paragraph was deleted.

12.a/2
This paragraph was deleted.
13/3

An implementation supporting an interface to C, COBOL, or Fortran should provide the corresponding package or packages described in the following subclauses.

13.a.1/2
implementation advice

If an interface to C, COBOL, or Fortran is provided, the corresponding package or packages described in Annex B, “Interface to Other Languages” should also be provided.

13.a
implementation note

The intention is that an implementation might support several implementations of the foreign language: Interfaces.This_Fortran and Interfaces.That_Fortran might both exist. The “default” implementation, overridable by the user, should be declared as a renaming:

13.b

package Interfaces.Fortran renames Interfaces.This_Fortran;

Wording Changes from Ada 95

13.c/2

Clarified that interfacing to foreign languages is optional and has the same restrictions as a Specialized Needs Annex.

Wording Changes from Ada 2005

13.d/3

Move the restrictions on implementations of optional features to the start of this Annex.