B - calling B routines from Pascal programs.

Description:

In the single segment environment, you can call a routine written in the B programming language from your Pascal program. To do this, put

extern b;

immediately after the declaration of the subprogram in the calling program. For example, suppose you wished to call the B library routine DATEV (see "expl b lib datev"). This would be declared with the following statements.

type
    daterec = record
        year, month, day, hour, minute, second, pulse
            : integer
    end;
var
    d : daterec;
    procedure datev(var res:daterec);
              extern b;
begin
    {various steps}
    datev(d);
    {and so on}
end;

The Pascal compiler takes care of adjusting pointers to var arguments. When the B routine receives its arguments from the Pascal program, pointer arguments will be in the lower half of the machine word, just as B expects them to be.

Some B library routines contain the "." character in their names (e.g. ".ABBRV"). Unfortunately, the dot is not a legal character for Pascal identifiers, and therefore a statement like

procedure .abbrv

would result in an error. Pascal solves this problem by allowing you to specify a SYMREF of one to six characters on the "extern" statement. For example,

procedure abbrev({declarations}):integer;
          extern b '.abbrv';

declares a procedure that the Pascal program will call "abbrev". However, when the loader searches for the procedure among the B library routines, it will look for a routine named ".abbrv". In this way, a legal Pascal name is associated with a routine whose name is illegal. (Note that the "symref" construction on the "extern" statement is also valid when referencing Fortran and Pascal routines.)

When calling B routines that perform I/O, it is necessary to associate the Pascal program's file variables with the B language's concept of unit numbers. This is done by using the Pascal library routine named IOUNIT. IOUNIT is a pre-declared function that accepts a file variable as an argument and returns a B unit number. For further information, see "expl pasc lib iounit".

See Also:

expl pascal manua

Copyright © 1996, Thinkage Ltd.