Avoiding Pitfalls in Multi-Language Programming

Use parameters to pass shared data rather than global data items.
Extensible parameter lists should be avoided, when possible.
System Intrinsics (as opposed to language intrinsics) should always be externally defined via the
automatic language specification that uses the SYSINTR.PUB.SYS intrinsic specification file.
Details:
Procedure names (except intrinsics) are
downshifted by default for all languages except C (which
uses mixed-case names). In C, you must write external names exactly
as they are to be seen by the
linkeditor or loader. For example, the Pascal procedure name 'ProcessNextRecord' would
be
downshifted by the compiler. To call it from C, you would
have to write it in lower case
'processnextrecord'. By default, Cobol external names have hyphens converted to
underscores. This
convention can be defeated by preceding the
routine name in a Cobol CALL statement by a
backslash. In Pascal and Fortran, the ALIAS option may be used to provide
access to names that are
invalid within the language (eg. names with apostrophes). The LITERAL_ALIAS and UPPERCASE
compiler options may be used to reference mixed-case and uppercase routine names.
The default external routine name in Basic
can be overridden by providing an alias on the
EXTERNAL or GLOBAL EXTERNAL statement. It is not possible in C to create
or to call a function
with a special character in its name,
except for the underscore. RPG uses the EXIT operator to
invoke external routines with up to a six character routine name placed in the factor 2 field -
columns
33-42, left-adjusted.
Parameters may be passed by value, by reference, or by descriptor. C passes parameters by value.
This is part of the simplicity of design of the C language.
Passing by reference involves dealing with
pointers, and the C programmer is expected to code pointer operations explicitly.
To simulate passing
a parameter by reference, explicitly pass the address of the desired object. A Cobol program normally
passes parameters by reference, although an HP extension does
allow passing by value. A Cobol
routine expects its parameters to be passed by reference. Transact uses the "%" option to
pass a
given parameter by byte address; "#" to pass a parameter by value; "&" to indicate the parameter is a
function value return. Fortran passes parameters by reference
unless told otherwise. Pointers from
another language may be
passed by value to Fortran which can treat them as addresses of reference parameters or simply as
identifiers to be passed to other languages' routines. Passing
function references as parameters is a
complex area and should be avoided unless required for traps and sorting.
In Basic and Pascal, the EXTERNAL directive allows for the specification of the language in which an
external procedure was written. The language specifier will
cause certain language specific
adjustments to the procedure calling code to be made, thus facilitating such procedure
calls. For
example, PASCAL will convert its string format to
that of C during a procedure call, and will convert
the string back to the Pascal format upon return, thereby updating the
string length. In Basic, for
BYTE String$ parameters, the address passed is a pointer to the start of the string data.
However for
String$, if the language specifier is BASIC, the
address passed is a pointer to the maximum length.
For a non-Basic specifier, the address passed is a pointer to the
current length, and the value of the
maximum length is passed as a hidden parameter immediately following the string
address. For
Basic, if a called routine written in another
language changes the length of a string parameter, the
current length of the string must be updated. This can be done by the
called routine or by the
SETLEN statement, or automatically (for the Pascal specifier) after returning to the Basic
routine. In
Fortran, the ALIAS directive includes the external
language specifier, and allows specification of
individual parameter passing modes of value, reference, and descriptor.
Arrays are normally passed by reference.
Within Basic the address passed will be a pointer to the
start of the array information block. Otherwise, for other languages
the address passed will be a
pointer to the start of the array data.
Arrays of greater than one dimension are frequently a source of parameter passing problems. Fortran
stores arrays in column-major order, while other languages use row-
major order. When passed as
reference parameters, the compilers often add hidden parameters that contain the maximum
indexes
Page
5
of
14
Avoiding Pitfalls in Multi
-
7/18/2008
http://www.hp.com/cgi
-
bin/pf
-
new.cgi?IN=http://jazz.external.hp.com/papers/lang/pgm_pi
...