Chapter Contents

Previous

Next
The CMS REXX SAS/C Interface

REXX Concepts and Background

CMS provides an interpretive command and macro processor called the System Product Interpreter to interpret REXX programs. REXX is a high-level, general-purpose programming language. REXX is often used for writing EXECs (command procedures) or XEDIT macros. However, REXX is versatile enough for a wide range of additional programming applications.

As a programming language, REXX contains a large set of built-in functions. Using these functions makes it easier to write programs in REXX. However, in specialized situations, you may find that your program needs a function that REXX does not supply. For example, there is no built-in square root function. If you need a function that REXX does not provide, you can write specialized functions in other languages and group them together under a name recognized by REXX as referring to a function package. In this way you can extend the capabilities of your REXX program by calling these functions. Writing function packages in the C language for REXX programs becomes an efficient way to enhance REXX applications.


Extending REXX with Function Packages

Function packages add flexibility to the REXX language. For example, external functions can access REXX variables and return values to the REXX EXEC. In addition to extending the REXX language, function packages also can boost the performance of a REXX program. Because function packages are usually written in assembler or in a compiled language such as C, more complicated or arithmetically intensive functions can be executed in machine code rather than interpreted word-by-word and line-by-line as for REXX. Also, function packages are loaded from disk only once, thereby avoiding the overhead of reloading each time a function is called.

Function packages are based on the programming concept of grouping sets of instructions (routines), designed to perform some specific task, outside of the mainline program. The REXX language allows calls to routines internal to the program and external to the program. Internal calls cause a branch to a routine identified by a statement label within the program. External calls are made to routines that reside in files outside both the user's program and the interpreter. Grouping similar external routines together into a function package is the focus of this chapter. For example, function packages typically are sets of related functions, such as sin , cos , and other trigonometric functions. Functions in a function package can share common code and data, or each function can be independent of the others.

The REXX interpreter recognizes three function package names. RXSYSFN is supplied with REXX and contains functions that interface with CP and CMS. The other two function packages are called RXUSERFN and RXLOCFN and can be written by any REXX user. Function packages written using the C language can use either of the names RXLOCFN or RXUSERFN. To find and execute an external function, REXX goes through a specific search order. For example, if a REXX statement such as the following refers to a name that is not a label or the name of a built-in function, REXX searches for an external function with the name csqrt :

root = csqrt(100)

REXX searches for an external function by prefixing the function name with RX (RXCSQRT, in our example) and invoking it as a CMS command. If such a program is found, REXX invokes it with the argument list. However, if no such command can be found, REXX then searches for the function in either RXUSERFN or RXLOCFN.

Once called, a function in a function package can access its parameter list, get or set REXX variable values, and return a value to its caller just as any internal function can.

The next section covers the distinction between functions and subroutines, function packages as CMS nucleus extensions, and the parameter lists used between REXX and the function package routines.

Functions and subroutines

Within a REXX program, routines in a function package can be used either as functions or as subroutines. Because the C language does not have subroutines or procedures, this means partitioning functions into two types that REXX recognizes as functions or as subroutines. The distinction is that functions must return a result; subroutines need not return a result. A subroutine is called by the REXX CALL instruction. A function is called with a function call. For example, a function named csqrt would be called as x = csqrt(4) . To use csqrt as a subroutine, the call would be call csqrt(4) .

REXX function packages as nucleus extensions

A REXX function package is an efficient use of code because it is not subject to repetitive reloading. This is because it resides in storage as a nucleus extension. The first time REXX invokes the package, the package copies itself into storage outside of the CMS user program area and identifies itself as a nucleus extension. Once it has been loaded and identified, the function package remains loaded until LOGOFF or until CMS is re-IPLed. Nucleus extensions come before MODULE files in the command search order, so the function package MODULE file is not reloaded as long as the nucleus extension is active. The function package also identifies each separate function as a nucleus extension entry point, thereby enabling REXX to call the function directly (after the first call) without going through the function package search again.

Function parameter lists and variable values

A special parameter list called an Extended Plist is used by REXX for function and subroutine calls. All external routines are invoked using this six-word plist. Word 5 of this plist points to a list of arguments for the function being invoked. These arguments are in the form of address/length pairs known as Adlen pairs. Adlen pairs also are used in the C function that builds and uses the REXX Extended Plist.

The use of Adlen pairs is related to the way REXX handles variable values. REXX keeps all its variable values, even numeric values, as character strings. Each variable value is kept internally as a pointer to a character string coupled with an int containing the length of the string. For example, given the following REXX statement, x is a string of length 3 with the value 100:

x = 100

REXX external functions must accept their parameter lists in this format and return values in this format to REXX. This is why function parameter lists are passed to the function in the form of an array of such Adlen pairs.

For more information about REXX interfaces to external functions, refer to the appropriate IBM documentation for your VM system.


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.