Using Intrinsics [ HP Pascal/iX Programmer's Guide ] MPE/iX 5.0 Documentation
HP Pascal/iX Programmer's Guide
Using Intrinsics
To use an intrinsic, your program must specify the intrinsic file in
which its definition resides and declare the intrinsic with the
INTRINSIC directive. How your program can declare the intrinsic as a
routine--specifying all, part, or none of its formal parameters--depends
upon its definition in the intrinsic file.
This section explains:
* How to specify intrinsic files.
* How to declare an intrinsic with the INTRINSIC directive.
* Actual and intrinsic parameter compatibility.
* How to declare formal function types for an intrinsic.
* How to declare formal parameters for an intrinsic to ensure
stricter type checking for actual parameters.
* How to use an intrinsic function as a procedure.
Specifying Intrinsic Files
When compiling a program that references an intrinsic, the compiler reads
the intrinsic definition from an intrinsic file. The intrinsic file can
be the default intrinsic file for the system, or it can be one that you
or another programmer built (see "How to Build or Change an Intrinsic
File" ). The program can specify different intrinsic files for
different intrinsics.
The SYSINTR compiler option determines the intrinsic file. If the
program does not contain a SYSINTR option, or if the SYSINTR option does
not specify a file name, the compiler reads intrinsic definitions
from the default intrinsic file. (The default intrinsic file is
system-dependent. See Appendix A for the MPE/iX operating system;
Appendix B for the HP-UX operating system.) Otherwise, the compiler
reads intrinsic definitions from the file that the SYSINTR option
specifies, until another SYSINTR option specifies another file. (See the
HP Pascal/iX Reference Manual or the HP Pascal/HP-UX Reference Manual,
depending on your implementation, for more information on the SYSINTR
compiler option.)
To list an intrinsic file, use the LISTINTR compiler option (refer to the
HP Pascal/iX Reference Manual the HP Pascal/HP-UX Reference Manual,
depending on your implementation, for more information on the LISTINTR
compiler option).
NOTE The compiler options LITERAL_ALIAS and UPPERCASE apply to all
external routine names, including intrinsic names. When either of
these options is set, the compiler performs a case-sensitive search
of the intrinsic file for the intrinsic names.
INTRINSIC Directive
The INTRINSIC directive allows a program to access an intrinsic routine.
It follows the routine declaration.
Example
PROGRAM p;
VAR
f,m : shortint;
PROCEDURE FSETMODE; INTRINSIC;
BEGIN
FSETMODE(f,m);
END.
The program p can call the intrinsic procedure FSETMODE because it
declares it with the INTRINSIC directive.
The system name of an intrinsic is the name by which the operating system
recognizes it, the name that it has in the intrinsic file.
The system names of some intrinsics are illegal in HP Pascal. If you
want to use such an intrinsic in your program, give it a legal name in
your program and specify its system name with the ALIAS compiler option
(refer to the HP PascaliX Reference Manual the HP Pascal/HP-UX Reference
Manual, depending on your implementation, for more information on ALIAS).
Example
$SYSINTR 'myintr'$ {myintr contains the intrinsic P'F'INFO}
PROGRAM q (output);
PROCEDURE pfileinfo $ALIAS 'P''F''INFO'$; INTRINSIC;
BEGIN
pfileinfo;
END.
The name P'F'INFO is illegal in HP Pascal because it contains single
quotes. The program q can call the intrinsic procedure P'F'INFO by the
name pfileinfo because it declares it with the INTRINSIC directive and
specifies its system name with the ALIAS compiler option.
Actual and Intrinsic Parameter Compatibility
An intrinsic's actual parameters are those with which your program calls
it. Its intrinsic parameters are those in its definition, in the
intrinsic file. Its formal parameters are those that your program
declares for it.
Formal parameters are optional. If you do not declare them, you can pass
the intrinsic actual parameters of types that would otherwise be
incompatible. Usually, programmers want this flexibility; therefore,
they rarely declare formal parameters.
If you do not declare a formal parameter, its actual parameters are
type-checked against their corresponding intrinsic parameters. Type
checking depends upon whether the intrinsic parameter is a reference,
value, or function or procedure parameter. The following subsections
explain these three cases, using these terms:
alignment- An actual and intrinsic parameter are
compatible alignment-compatible if the actual parameter is
aligned on the same or a larger boundary than
the intrinsic parameter. For example, a
2- or 4-byte-aligned actual parameter is
alignment-compatible with a 2-byte-aligned intrinsic
parameter. A byte-aligned actual parameter is not
alignment-compatible with a 2-byte-aligned intrinsic
parameter.
size-compatible An actual and intrinsic parameter are size-compatible
if the actual parameter is allocated more or the same
amount of space as the intrinsic parameter. For
example, a 2- or 4-byte actual parameter is
size-compatible with a 2-byte intrinsic parameter. A
1-byte actual parameter is not size-compatible with a
2-byte intrinsic parameter.
intrinsic- See Table 10-1 for reference parameters; Table
compatible 10-2 for value parameters.
Reference Parameter Compatibility.
A reference parameter is a parameter that is passed by reference. VAR,
ANYVAR, and READONLY parameters are reference parameters.
All actual reference parameters must be alignment-compatible with their
corresponding intrinsic parameters. Actual VAR and READONLY parameters
must also be size-compatible and intrinsic-compatible with their
corresponding intrinsic parameters.
An intrinsic and an actual reference parameter are intrinsic-compatible
if their types are in the same row of Table 10-1 . The intrinsic
parameter type is the type of the intrinsic parameter, as the intrinsic
file declares it. The actual parameter type is the type of the actual
parameter.
Table 10-1. Intrinsic-Compatible Intrinsic and Actual Reference Parameter Types
-----------------------------------------------------------------------------------------------
| | |
| Intrinsic Parameter Type | Actual Parameter Type |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Array | Any type |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Boolean | Boolean |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Char | Char |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Integer | Integer |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Integer | Integer subrange m..n with either |
| | m < 0 or m >= 0 and n >65535 |
| | |
-----------------------------------------------------------------------------------------------
| | | |
| Integer subrange | m < 0, or m >= 0, | Integer, or integer subrange m..n with |
| m..n | n > 65535 | either |
| | | m < 0 or m >= 0 and n >65535 |
| | | |
-----------------------------------------------------------------------------------------------
| | |
| Integer | Bit32 |
| | |
-----------------------------------------------------------------------------------------------
| | | |
| Integer subrange | m >= 0 and n <= 65535 | Integer subrange m..n with m >= 0 and |
| m..n | | n <= 65535 |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Integer subrange | m >= 0 and n <= 65535 | Bit16 |
| m..n | | |
| | | |
-----------------------------------------------------------------------------------------------
| | |
| Longreal | Longreal |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Real | Real |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Record | Any type |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Set | Any type |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Shortint | Bit16 |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Shortint | Shortint |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Shortint | Integer |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Shortint | Integer subrange m..n (except where m>=0 |
| | and n<=255) |
| | |
-----------------------------------------------------------------------------------------------
Value Parameter Compatibility.
A value parameter is a parameter that is passed by value. All parameters
except VAR, ANYVAR, READONLY, function, and procedure parameters are
value parameters.
An actual value parameter of a structured type (array, record,
or set) must be the same size as its corresponding intrinsic
parameter. An actual value parameter of an unstructured type must be
assignment-compatible with its corresponding intrinsic parameter.
Table 10-2 shows which intrinsic and actual value parameter types are
intrinsic-compatible. It also shows, for each intrinsic parameter type,
which of the compatible actual parameter types are converted to that
intrinsic parameter type, and which are not. The intrinsic parameter
type is the type of the parameter as the intrinsic file declares it. The
actual parameter type is the type of the actual parameter.
Table 10-2. Intrinsic-Compatible Intrinsic and Actual Value Parameter Types
-----------------------------------------------------------------------------------------------
| | |
| Intrinsic | Actual Parameter Type |
| Parameter Type | |
| | |
-----------------------------------------------------------------------------------------------
| | | |
| | Not Converted to Intrinsic | Converted to Intrinsic Type |
| | Type | |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Array | Any type | |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Boolean | Boolean | |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Char | Char | |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Integer or | Array | |
| Integer Subrange | Bit16 | |
| | Bit32 | |
| | Bit52 | |
| | Integer | |
| | Integer Subrange | |
| | Longint | |
| | Record | |
| | Set | |
| | Shortint | |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Longreal | Longreal | Bit16 |
| | | Bit32 |
| | | Bit52 |
| | | Integer |
| | | Integer Subrange |
| | | Longint |
| | | Real |
| | | Shortint |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Real | Real | Bit16 |
| | | Bit32 |
| | | Bit52 |
| | | Integer |
| | | Integer Subrange |
| | | Longint |
| | | Longreal |
| | | Shortint |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Record | Any Type | |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Set | Any Type | |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Shortint | Array | |
| | Bit16 | |
| | Bit32 | |
| | Bit52 | |
| | Integer | |
| | Integer Subrange | |
| | Longint | |
| | Record | |
| | Set | |
| | Shortint | |
| | | |
-----------------------------------------------------------------------------------------------
Function and Procedure Parameter Compatibility.
A function or procedure parameter is a parameter that is a routine. The
compiler only checks that the actual parameter for a function or
procedure parameter is a routine. You are responsible for making sure
that the actual parameter is what the intrinsic expects.
Using Strings as Actual Parameters.
If you use a string variable as an actual value parameter to an intrinsic
routine, HP Pascal passes a copy of the data portion only of the string.
The length portion is ignored.
If you use a string variable as an actual reference parameter to an
intrinsic routine, HP Pascal passes the address of the data portion of
the string, and not the string length. If the intrinsic returns data in
the string variable, you must determine and update the length of the
string when the intrinsic returns control to your program.
There are a number of ways to obtain and update the string length:
* If the intrinsic returns the correct length as a parameter or
function return, use the setstrlen procedure with the returned
value.
* If the length is defined in documentation of the intrinsic, use
the setstrlen procedure with that value.
* If the intrinsic appends some end-of-string character (such as
NUL), scan for the character and set the string length with the
setstrlen procedure to one less than the character's position.
* If the intrinsic does not provide any length indication, you can
use the strrpt function to fill the string with blanks to its full
physical length, call the intrinsic, and then use the strrtrim
function to get rid of the trailing blanks and update the string
length.
Example
This example demonstrates the sequence of filling a string with blanks,
calling an intrinsic that returns a value in the string, and updating the
string length.
PROGRAM TestIntrin ;
VAR
Str : string [80] ;
PROCEDURE Dateline ; INTRINSIC ;
BEGIN { main program }
...
Str := strrpt ( ' ' , 80 ) ; { fill string with blanks }
Dateline ( Str ) ; { call intrinsic }
Str := strrtrim ( Str ) ; { remove trailing blanks }
...
END .
Formal and Intrinsic Function Type Compatibility
A function type must be specified when using the intrinsic directive with
functions. A formal function type is compatible with an intrinsic
function type as long as the size of the formal type matches the size of
the intrinsic type.
NOTE In general, the formal type and the intrinsic type should match the
function return type. If the types do not match, they are the same
as a free union type coercion. This can cause problems for signed
versus unsigned types.
Example
program m(output);
var a,b:shortint;
buf:packed array[1..16] of char;
i:integer;
function calendar:shortint; intrinsic;
function cal_16 $alias 'calendar'$:bit16; intrinsic;
function neg:shortint;
begin
neg:=-1;
end;
begin
a := calendar;
b := calendar;
writeln(a = calendar,' ',a = b);
end.
Assuming the date did not change, the output is unexpected:
FALSE TRUE
Function cal_16 shows the correct definition; a and b should be declared
as bit16.
User-Defined Formal Parameters
If you want stricter type checking for an intrinsic's actual parameters,
you can declare formal parameters for some or all of its intrinsic
parameters. Then, actual parameter types are compared to their
corresponding formal parameter types, not to their corresponding
intrinsic parameter types. This type checking is as strict as that for
the parameter of a nonintrinsic routine: if the actual parameter is a
reference parameter, it must be of the same type as the formal
parameter; if the actual parameter is a value parameter, it must be
assignment-compatible with the formal parameter.
If an intrinsic is defined without an extensible parameter list, you
cannot declare it with one.
If an intrinsic is defined with an extensible parameter list, you can
declare it with or without one. If you declare the intrinsic with an
extensible parameter list, you must declare at least as many
nonextensible (required) parameters as the definition does. If you
declare the intrinsic without an extensible parameter list, you must
declare all of its nonextensible (required) parameters.
Example 1
The intrinsic file defines the intrinsic Pascal procedure intr this way:
PROCEDURE intr (a, b, c, d, e : integer)
OPTION EXTENSIBLE 2;
The program can declare intr in any of these ways:
PROCEDURE intr (a, b, c, d, e : integer); {All parameters}
INTRINSIC;
PROCEDURE intr (a, b : integer); {Required parameters only}
INTRINSIC;
PROCEDURE intr (a, b, c : integer); {First extensible parameter}
INTRINSIC;
PROCEDURE intr (a, b, c, d : integer); {Extensible parameters}
INTRINSIC;
The program cannot declare intr in any of these ways:
PROCEDURE intr (a : integer); {Without second nonextensible parameter}
INTRINSIC;
PROCEDURE intr (a, b, c, d : integer) {Fewer required parameters than}
OPTION EXTENSIBLE 1; {in the intrinsic definition}
INTRINSIC;
If you supply default values for the formal parameters that you declare,
your default values override those supplied by the intrinsic definition.
Example 2
The intrinsic file defines the intrinsic Pascal procedure intr this way:
PROCEDURE intr (a, b : integer)
OPTION EXTENSIBLE 2
DEFAULT_PARMS (a := 10, b := 20);
If the program declares intr this way
PROCEDURE intr (a, b: integer)
OPTION EXTENSIBLE 2
DEFAULT_PARMS (a := 35, b := 60);
INTRINSIC;
Then the default value of a is 35 (not 10) and the default value of b is
60 (not 20).
If you declare a formal parameter, you must give it a type that is
compatible with the type of its corresponding intrinsic parameter.
Compatibility rules are different for reference and value parameters.
Reference Parameter Compatibility.
A formal reference parameter is compatible with its corresponding
intrinsic parameter if any of the following is true:
* Their types (Boolean, integer, etc.) are intrinsic-compatible
(see Table 10-3 ).
* They are alignment-compatible.
* Their types (VAR, ANYVAR, UNCHECKABLE_ANYVAR, READONLY) are
compatible.
* If the intrinsic parameter is a VAR or READONLY array, record, or
set, then:
sizeof (formal_parameter) <= sizeof (intrinsic_parameter)
An intrinsic and a formal reference parameter are intrinsic-compatible if
their types are in the same row of Table 10-3 . The intrinsic
parameter type is the type of the intrinsic parameter, as the intrinsic
file declares it. The formal parameter type is the type of the formal
parameter in your program.
Table 10-3. Intrinsic-Compatible Intrinsic and Formal Reference Parameter Types
-----------------------------------------------------------------------------------------------
| | |
| Intrinsic Parameter Type | Formal Parameter Type |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Array | Any type |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Boolean | Boolean |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Char | Char |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Integer | Integer |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Integer | Bit32 |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Integer | Integer subrange m..n with either |
| | m < 0 or m >= 0 and n >65535 |
| | |
-----------------------------------------------------------------------------------------------
| | | |
| Integer subrange | m < 0, or m >= 0, | Integer, or integer subrange m..n with |
| m..n | n > 65535 | either |
| | | m < 0 or m >= 0 and n >65535 |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Integer subrange | m >= 0 and n <= 65535 | Integer subrange m..n with m >= 0 and |
| m..n | | n <= 65535 |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Integer subrange | m >= 0 and n <= 65535 | Bit16 |
| m..n | | |
| | | |
-----------------------------------------------------------------------------------------------
| | |
| Longreal | Longreal |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Real | Real |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Record | Any type |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Set | Any type |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Shortint | Bit16 |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Shortint | Shortint |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Shortint | Integer |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Shortint | Integer subrange m..n |
| | (except where m>=0 and n<=255) |
| | |
-----------------------------------------------------------------------------------------------
Table 10-4 shows which intrinsic and formal reference parameter types
are compatible. The intrinsic parameter type is the type that the
intrinsic parameter has in the intrinsic file; the formal parameter types
are the types that you can give the formal parameter when you declare it
in your program.
Table 10-4. Compatible Intrinsic and Formal Reference Parameter Types
-------------------------------------------------------------------
| | |
| Intrinsic Parameter Type | Formal Parameter Type |
| | |
-------------------------------------------------------------------
| | |
| VAR | VAR |
| | |
-------------------------------------------------------------------
| | |
| ANYVAR | ANYVAR |
| | VAR |
| | |
-------------------------------------------------------------------
| | |
| UNCHECKABLE_ANYVAR | UNCHECKABLE_ANYVAR |
| | VAR |
| | |
-------------------------------------------------------------------
| | |
| READONLY | READONLY |
| | VAR |
| | |
-------------------------------------------------------------------
Value Parameter Compatibility.
A formal value parameter is compatible with its corresponding intrinsic
parameter if any of the following is true:
* They are intrinsic-compatible (see Table 10-5 ).
* If the intrinsic parameter is an array, record, or set, then:
sizeof (formal_parameter) = sizeof (intrinsic_parameter)
An intrinsic and formal value parameter are intrinsic-compatible if their
types are in the same row of Table 10-5 . The intrinsic parameter
type is the type of the intrinsic parameter, as the intrinsic file
declares it. The formal parameter type is the type of the formal
parameter.
Table 10-5. Intrinsic-Compatible Intrinsic and Formal Value Parameter Types
---------------------------------------------------------------------------
| | |
| Intrinsic Parameter Type | Formal Parameter Type |
| | |
---------------------------------------------------------------------------
| | |
| Array | Array |
| | Record |
| | |
---------------------------------------------------------------------------
| | |
| Boolean | Boolean |
| | |
---------------------------------------------------------------------------
| | |
| Char | Char |
| | |
---------------------------------------------------------------------------
| | |
| Integer | Bit16 |
| | Bit32 |
| | Bit52 |
| | Integer |
| | Integer subrange |
| | Longint |
| | Shortint |
| | |
---------------------------------------------------------------------------
| | |
| Integer subrange | Bit16 |
| | Bit32 |
| | Bit52 |
| | Integer |
| | Integer subrange |
| | Longint |
| | Shortint |
| | |
---------------------------------------------------------------------------
| | |
| Longreal | Longreal |
| | |
---------------------------------------------------------------------------
| | |
| Real | Real |
| | |
---------------------------------------------------------------------------
| | |
| Record | Record |
| | Array |
| | |
---------------------------------------------------------------------------
| | |
| Set | Set |
| | |
---------------------------------------------------------------------------
| | |
| Shortint | Bit16 |
| | Bit32 |
| | Bit52 |
| | Integer |
| | Integer subrange |
| | Longint |
| | Shortint |
| | |
---------------------------------------------------------------------------
Using Intrinsic Functions as Procedures
Your program must use an intrinsic procedure as a procedure, but it can
use an intrinsic function as a function, a procedure, or both.
To use an intrinsic function as a function, declare it as a function in
your program, including its result type in the declaration. To use an
intrinsic function as a procedure, declare it as a procedure in your
program, omitting the result type. To use an intrinsic function as both
a function and a procedure, declare it both ways, giving the routine
different names in your program. Use the ALIAS compiler option to
associate the intrinsic's system name with the names you have given it.
If you declare an intrinsic function as a procedure only, you cannot call
it as a function.
Example
The intrinsic file defines the intrinsic Pascal functions f1 and f2 this
way:
FUNCTION f1 (i1 : integer) : integer;
FUNCTION f2 (i1,i2 : integer) : Boolean;
The Pascal program prog declares the function f1 as a procedure. It
cannot call it as a function. It declares the function f2 as a function
(which it calls ffunc) and as a procedure (which it calls fproc), using
the compiler option ALIAS to associate them with the system name f2. The
program cannot call fproc as a function.
PROGRAM prog;
VAR
x : Boolean;
y,z : integer;
PROCEDURE f1 (a : integer) INTRINSIC;
FUNCTION $ALIAS 'f2'$ ffunc : Boolean; INTRINSIC;
PROCEDURE $ALIAS 'f2'$ fproc; INTRINSIC;
BEGIN
f1(y);
x := ffunc(y,z);
fproc(y,z);
z := f1(y); {illegal -- declared as a procedure}
END.
MPE/iX 5.0 Documentation