Operating System - OpenVMS
1751779 Members
4551 Online
108781 Solutions
New Discussion юеВ

CALL decc$bsearch from FORTRAN

 
Michael Menge
Frequent Advisor

CALL decc$bsearch from FORTRAN

VMS 8.3 FORTRAN X8.0n
In a Fortran application I call the C-routine decc$bsearch using the HP specific POINTER statement: POINTER (pointer,pointee) (ex. test_bsearch1.for, o.k.).
Another way is to use an interface block to define the function and its parameter (ex. test_bsearch2.for). Example 2 runs with accvio and I think some dummy parameters are not defined correctly. You can see the correct C-format of the interface block with
$ help crtl bsearch.

An interface block in Fortran is type specific I think, there is no "void" pointer as in C. What is to do if you want to call decc$bsearch twice in a routine with different table-types and keys? With the Pointer statement as in example 1 it's easy, but using interface blocks, do I have to write two special routines with different interface blocks each?
2 REPLIES 2
Hoff
Honored Contributor

Re: CALL decc$bsearch from FORTRAN

Probably not the answer you want...

Do you need to call the innards of a C standard library function directly from Fortran?

Under the theory of "if it hurts, don't do it", I'd tend to either jacket the code with C code that has tailored the underlying interface to meet Fortran norms, or move to the lib$ tree-related calls, including:

LIB$INSERT_TREE, LIB$LOOKUP_TREE, LIB$TRAVERSE_TREE_64, etc.

I personally find that arbitrary API jacketing is easier in C than in Fortran.

And if this is a case of not using C, you can also use a Macro32 jacket. Or the LIB$ RTL calls.

Rules for pointees (which you've probably already found) are here:

http://h71000.www7.hp.com/DOC/82final/6324/6324pro_060.html#df_pointer_stmt
John Gillings
Honored Contributor

Re: CALL decc$bsearch from FORTRAN

Michael,

If you want your arguments type checked, and have different types, then you'll need to distinguish between different instances of the routine. FORTRAN doesn't do polymorphism very well.

I don't have a compiler handy to check, but it's possible the function name doesn't need to be the same as the C function name. So this might work:

INTERFACE
FUNCTION decc$bsearch_TYPE1 (key, tab, tabSize, elemSize, cmpFunc)
USE keyidx
IMPLICIT NONE
!DEC$ ATTRIBUTES C :: decc$bsearch
...
END INTERFACE

INTERFACE
FUNCTION decc$bsearch_TYPE2 (key, tab, tabSize, elemSize, cmpFunc)
USE keyidx
IMPLICIT NONE
!DEC$ ATTRIBUTES C :: decc$bsearch
...
END INTERFACE

if not you can implement a simple jacket routine. Hoff finds C easier, but I think MACRO32 is the easiest of all:

.title DECC$BSEARCH_JACKETS
.psect $code,rd,nowrt,exe
.CALL_ENTRY LABEL=decc$bsearch_type1,MAX_ARGS=5,HOME_ARGS=TRUE
CALLG (AP),G^DECC$BSEARCH
RET
.CALL_ENTRY LABEL=decc$bsearch_type2,MAX_ARGS=5,HOME_ARGS=TRUE
CALLG (AP),G^DECC$BSEARCH
RET
.END

On the other hand, if you use a builtin function on your argument %VAL, %REF you disable type checking for that argument. So, using a builtin when you don't actually need to will make that argument polymorphic (but the compiler won't help spot your bugs)
A crucible of informative mistakes