comp.lang.ada
 help / color / mirror / Atom feed
* how do I do pointers to functions in Ada
@ 1989-12-04  6:40 Michael Hunter
  1989-12-04 15:39 ` how do I do pointers to functions i stt
  0 siblings, 1 reply; 3+ messages in thread
From: Michael Hunter @ 1989-12-04  6:40 UTC (permalink / raw)


I heard a vailed reference the other day of some way of manipulating address
attributes in some way such that you could take the address of a function or
procedure, store it, and latter execute (vector off to) it.  I havn't been
able to figure out how this could be done in Ada.  I realize that it isn't a
normal thing to do in the Ada programming philosophy and could probably turn
out non-protable, but it could be usefull and the answer could be interesting.

                                Michael

Mike Hunter - Box's and CPU's from HELL: iapx80[012]86, PR1ME 50 Series, 1750a
UUCP: {ames!elroy, <routing site>}!gryphon!pnet02!bagpiper
INET: bagpiper@pnet02.gryphon.com

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: how do I do pointers to functions i
  1989-12-04  6:40 how do I do pointers to functions in Ada Michael Hunter
@ 1989-12-04 15:39 ` stt
  1989-12-06 13:44   ` Ed Matthews
  0 siblings, 1 reply; 3+ messages in thread
From: stt @ 1989-12-04 15:39 UTC (permalink / raw)



To call a subprogram, given only its address...

With some Ada compilers, it is possible to provide
an address clause for a pragma-interfaced subprogram
with a non-static address.  This means that the following
might work:

    procedure Real_Proc(B : Integer) is
    begin ... end Real_Proc;

. . .

    Proc_Addr : System.Address;

. . .

    Proc_Addr := Real_Proc'ADDRESS;

. . .

    procedure Dummy_Proc(B : Integer);
    pragma Interface(Ada, Dummy_Proc);
    for Dummy_Proc use at Proc_Addr;

. . .

    Dummy_Proc(42);

However, we have found that with most compilers we need to drop into
assembler to call a procedure given only its address.
This usually only requires a couple of lines of assembler.
For example:

    procedure Call_Ada_Proc(
      Param : Integer;
      Proc : System.Address
    );
    pragma Interface(Assembler, Call_Ada_Proc);

    . . .

    Call_Ada_Proc(42, Proc_Addr);

Depending on parameter passing conventions,
the code for Call_Ada_Proc can sometimes be a single
instruction which passes control to the address passed
as the last parameter.  By putting the address parameter
last, the earlier parameters are often already in the
"right" place (e.g. register or stack offset).

Since the address-clause "trick" is compiler-dependent
anyway, dropping into assembler is not a bad
alternative.  This is one of those
things we can hope will be solved as part of the Ada9X
process, since it seems like a common problem.
Of course, no guarantees...

S. Tucker Taft
Intermetrics, Inc.
Cambridge, MA  02138

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: how do I do pointers to functions i
  1989-12-04 15:39 ` how do I do pointers to functions i stt
@ 1989-12-06 13:44   ` Ed Matthews
  0 siblings, 0 replies; 3+ messages in thread
From: Ed Matthews @ 1989-12-06 13:44 UTC (permalink / raw)


In article <20600024@inmet> stt@inmet.inmet.com writes:
>
>However, we have found that with most compilers we need to drop into
>assembler to call a procedure given only its address.
>This usually only requires a couple of lines of assembler.
>For example:
>
>    procedure Call_Ada_Proc(
>      Param : Integer;
>      Proc : System.Address
>    );
>    pragma Interface(Assembler, Call_Ada_Proc);

The Verdix VADS compiler is an exception to the above.  You can use a
machine code insertion to do the same thing.  We use this trick when
passing function pointers to C code.

with Machine_Code;
with System;
procedure Call(
    Function_Ptr : in System.Address;
    Param        : in Integer_32) is

    procedure Do_Call(Param : in Integer_32) is
        use Machine_Code;
    begin
        -------------------------------------------
        -- Stack is set up correctly so just do  --
        -- a branch to the start of the function --
        -------------------------------------------
        Code_1'(JMP, Function_Ptr'Ref);
	---------------------------------------------------------
	-- JMP is an enumeration literal of type OP_CODE from  --
	-- package Machine_Code.  The 'Ref attribute generates --
	-- a reference to an Ada object.                       --
	---------------------------------------------------------
    end Do_Call;

begin
    Do_Call(Param);
end Call;
-- 

Ed Matthews                                                edm@verdix.com
Verdix Corporation Headquarters                            (703) 378-7600
Chantilly, Virginia

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1989-12-06 13:44 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1989-12-04  6:40 how do I do pointers to functions in Ada Michael Hunter
1989-12-04 15:39 ` how do I do pointers to functions i stt
1989-12-06 13:44   ` Ed Matthews

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox