[Ada Information Clearinghouse]
Ada '83 Rationale, Sec 15.7: Interface with Other Languages

"Rationale for the Design of the
Ada® Programming Language"

[Ada '83 Rationale, HTML Version]

Copyright ©1986 owned by the United States Government. All rights reserved.
Direct inquiries to the Ada Information Clearinghouse at adainfo@sw-eng.falls-church.va.us.

CHAPTER 15: Representation Clauses and Machine Dependences

15.7 Interface with Other Languages

A limited and implementation-dependent facility for machine code insertions has been included in Ada. This facility has the advantage of clearly isolating the use of machine language. Furthermore, its use is heavier than direct use of an assembler: the facility offered should be sufficient for cases where there is an actual need, but its style will inhibit overuse.

Each machine instruction appears as a code statement, which is a record aggregate of a record type that defines the corresponding instruction. Such record definitions will generally be available in a system-dependent library package called MACHINE_CODE. This package must also contain the representation of the record that describes the machine instruction format. Code statements may be used only in procedures whose bodies consist entirely of code statements.

The following example illustrates the use of a set system mask instruction on an IBM 370. The package MACHINE_CODE could be as follows

package MACHINE_CODE is
  type REGISTER    is range 0 .. 16#F#;
  type DISPLACEMENT   is range 0 .. 16#FFF#;
  type OPCODE      is ( ... , SSM, ... );

  type RR is ...
  type RX is ...
  type SI is
      CODE  :  OPCODE;
      B     :  REGISTER;
      D     :  DISPLACEMENT;
    end record;
  for OPCODE use ( ... , SSM =>  16#80#, ... );
  for SI use
      CODE at 0 range 0 .. 7;
      B    at 0 range 16 .. 19; -- bits 8 .. 15 unused
      D    at 0 range 20 .. 31;
    end record;

We will assume in what follows that the set system mask operation is defined as part of a package, along with similar operations:

  procedure SET_SYSTEM_MASK(X :  MASK);
package body SPECIAL_COMMANDS is
  procedure SET_SYSTEM_MASK(X :  MASK) is
    SI'(CODE =>  SSM,  B =>  X'BASE_REG,  D =>  X'DISP);

For the body of this package, a with clause mentioning MACHINE_CODE is required: this rule will enable programming support environments to enforce certain management rules concerning the use of machine code insertions. This example also shows the need to use implementation- specific predefined attributes in code statements, such as X'BASE_REG (the base register used for X) and X'DISP (the displacement of X). Additional implementation-specific pragmas may be needed to specify the register and linkage conventions.

Obviously such pragmas cannot be machine-independent; the only order that may be brought by a high level language to such a matter is to standardize the conventions to be used for such specifications, and this is what we achieve in Ada by recourse to pragmas. Finally, pragmas can also be used to specify that a subprogram called from an Ada program is written in another language. Consider for example the reuse of functions defined in the Fortran library, from an Ada program. To achieve this, we declare a package

package FORTRAN_LIB is
  function SQRT  (X :  FLOAT) return FLOAT;
  function EXP (X :  FLOAT) return FLOAT;

For each function needed we have declared an Ada function in the usual manner, and this function can then be called using the normal Ada syntax. However, the bodies of these functions are not written in Ada and we indicate this by INTERFACE pragmas. These inform the compiler about the corresponding linkage conventions, and also to expect the object code to be provided later (at linkage editing time). Of course compilers may impose restrictions on the form of parameters that are allowed (for example, passing two-dimensional arrays may be complex, given the internal representation used by Fortran). Not all compilers need provide this machine code insertion capability.

Address any questions or comments to adainfo@sw-eng.falls-church.va.us.