XLIBTRAP [ Compiler Library/XL Reference Manual ] MPE/iX 5.0 Documentation
Compiler Library/XL Reference Manual
XLIBTRAP
Using the HP FORTRAN 77/XL compiler, you can override the process
described above by using the ON statement. For more details, see the HP
FORTRAN 77/XL Reference Manual. You can also override the normal error
functions and specify your own error procedure by using the intrinsic
function XLIBTRAP, which has the following format:
PROCEDURE xlibtrap;intrinsic;
or
PROCEDURE xlibtrap (plabel:INTEGER; VAR oldplabel: INTEGER);
EXTERNAL;
where
plabel = external label of the user-written error
procedure to disarm the library trap
mechanisms. If plabel equals zero, control is
not passed to a user error procedure.
oldplabel = original plabel; returned to permit the user to
return to the previous conditions.
Execution proceeds as follows:
1. A library procedure finds an error and calls the library error
handler.
2. The library error handler checks for a user-written error
procedure or checks for an indication to ignore the trap. To
ignore the trap, use the following
HP FORTRAN 77/XL statements:
ON EXTERNAL ERROR IGNORE
ON INTERNAL ERROR IGNORE
(The HP FORTRAN 77/XL ON statement has additional options that
make it easier to use than XLIBTRAP. See the HP FORTRAN 77/XL
Reference Manual for more details.)
3. If a user-written error procedure has not been specified by using
XLIBTRAP or with the HP FORTRAN 77/XL ON statement and the trap is
not to be ignored, the appropriate error report is produced and
the current program is aborted. The library trap mechanism is
disarmed when the user-written procedure is called. If the
compiler finds an indication to ignore the trap, control is
immediately returned to the library procedure.
4. When the user-written error procedure returns control to the
library error handler, the library trap mechanism is rearmed.
5. As defined below, a user-written error procedure can either set a
result or a set a flag. The QUIT parameter in the example below
directs the library error handler to abort the current program or
to return control to the procedure that found the error.
A user-written error procedure (named error in this example) could be
declared as follows:
PROGRAM libtrap_test(output);
A Pascal Example Using XLIBTRAP
The following example shows a user-written HP Pascal error procedure to
be called whenever an error is detected.
{This program tests the ability to invoke library trap handling from Pascal.}
TYPE
word_int = integer;
plabel = word_int;
stack_array = ARRAY [1 .. 4] of word_int;
VAR
Nan_var, {Holds the value -1.}
x,y: REAL; {Used to hold results returned from the library routines.}
oldplabel : plabel;
result : real;
took_trap : Boolean;
{Declare the sqrt library routine.}
FUNCTION sqrt : REAL; INTRINSIC;
{Declare the alog library routine.}
FUNCTION alog : REAL; INTRINSIC;
{Declare XLIBTRAP.}
PROCEDURE xlibtrap; intrinsic;
{Provide the error routine.}
PROCEDURE error (VAR marker: stack_array;
VAR errornum,
VAR quit: word_int);
{Declare constants.}
CONST
sqrt_errornum = 10;
sqrt_default = -999.0;
continue = 0;
abort = 1;
{This procedure returns a result of -999 and continues
the execution of the program if the SQRT routine has
the error. Otherwise, the program aborts.}
BEGIN
IF errornum = sqrt_errornum THEN BEGIN
result := sqrt_default;
took_trap := True;
quit := continue;
END
ELSE quit := abort;
END; {error}
{Start the main body of the program.}
BEGIN
{Set up the -1 value.}
x := -1.0;
Nan_var := sqrt(x);
took_trap := False;
{Arm the trap mechanism by calling XLIBTRAP.}
xlibtrap(waddress(error), oldplabel);
writeln ('Calling sqrt with NaN');
{Call sqrt with a negative value.}
x := sqrt(Nan_var);
if took_trap
then begin
x := result;
took_trap := False;
end;
writeln('X should be -999.0. X = ', x);
writeln('This should be the last line before an abort.');
{Call alog with a negative value.}
y := alog(Nan_var);
writeln('This line should NOT be printed');
END.
where
result = the result of the calculation that produced the
error. The library procedure determines the
size and type of the value returned to result.
If you know that only a subset of the possible
result types occurs in your program, the
extensive variant RECORD structure shown above
is not needed. You can change the value
returned to result in the library procedure and
have the change reflected in the code if
optimization is not being used.
errornum = the error number. (See the "Error Messages"
section in this appendix for a list of the
error messages.)
quit = a flag set by the user-written procedure error.
If quit equals zero, the trap returns to your
program without printing an error message. If
quit does not equal zero, your program is
aborted.
A FORTRAN Example Using XLIBTRAP
The libtrap_test program in the above example can also be written FORTRAN
77/XL using the ON statement:
PROGRAM libtrap_test
IMPLICIT NONE
REAL x,y,NaN
INTEGER INaN
EQUIVALENCE (NaN, INaN)
DATA INaN / Z'7FF0' / ! bit pattern for single- precision NaN
ON EXTERNAL ERROR CALL ERROR ! Set up the trap.
PRINT *, 'Calling sqrt with', NaN
x=sqrt (NaN)
PRINT *, 'x should be -999.0. x=',X
PRINT *, 'This should be the last line before an abort.'
y=alog (NaN)
PRINT *, 'This line should NOT be printed.'
END
SUBROUTINE ERROR (errornum, result, operand 1, operand 2)
IMPLICIT NONE
INTEGER*2 errornum
REAL result, sqrt_default, operand1, operand2
INTEGER sqrt_errornum, abort
!Declare the constants.
PARAMETER (sqrt_default= -999.0)
PARAMETER (sqrt_errornum=10, abort=0)
print *, 'In ERROR, params =', errornum, result, operand1
!Test for SQRT
IF (errornum .eq. sqrt_errornum) THEN
!If so, set the default of -999.0 and continue
result=sqrt_default
ELSE
! Abort if it is not SQRT.
print *, 'Errornum set to ABORT.'
errornum = abort
ENDIF
END
MPE/iX 5.0 Documentation