Call native OS functions from my interpreter? 
Author Message
 Call native OS functions from my interpreter?

Hi,

 I've written a little interpreter for a programming language of my own.
Now, I'd like to be able to call the APIs of my operating system (MacOS
X, aka Darwin aka FreeBSD) from the interpreter. Ideally, this would
work like in C: The programmer specifies the calling conventions of the
OS API function she wants to use to my interpreter, and my interpreter
simply calls it and passes it the parameters.

 The trouble is, how do I do I actually call a function whose signature
I don't know until runtime? Is there an ANSI (or at least POSIX) way of
doing such a call, something like va_arg? Or will I need to write
platform-specific code in assembly? I've only dabbled in 68k asm so
far...

 To clarify: I know what OS-specific calls to make to get a function
pointer to the system API, but how do I get from that and an array
containing the parameters to an actual function call?

Thanks in advance & cheers,
-- M. Uli Kusterer
http://www.*-*-*.com/
--



Tue, 27 Dec 2005 15:52:33 GMT  
 Call native OS functions from my interpreter?

Quote:

>  The trouble is, how do I do I actually call a function whose signature
> I don't know until runtime? Is there an ANSI (or at least POSIX) way of
> doing such a call, something like va_arg?

No, and rightly so.  What you're trying to do is about as impossible
to do portably as it gets.

To make it worse, you don't just have to find out the signature, either,
you won't have the *address* of that function available to you, either.
The mapping from names to actual functions is done at compile time.

--

Even if all the snow were burnt, ashes would remain.
--



Thu, 29 Dec 2005 01:29:28 GMT  
 Call native OS functions from my interpreter?


Quote:

> No, and rightly so.  What you're trying to do is about as impossible
> to do portably as it gets.

Hans-Bernhard,

 that's a pity :-T But thanks for letting me know. I just wanted to
cover all bases before I went and reinvented the wheel.

Quote:
> To make it worse, you don't just have to find out the signature, either,
> you won't have the *address* of that function available to you, either.
> The mapping from names to actual functions is done at compile time.

 As I said, I already have the platform-specific code to get such a
function pointer, so this issue isn't an issue in my case. But
rethinking it from your point of view, I realize that it would have been
pointless to add something like being able to call functions whose
signature is unknown to C if there is no way you can portably produce
such a function pointer.

Thanks,
-- M. Uli Kusterer
http://www.zathras.de
--



Mon, 02 Jan 2006 00:27:16 GMT  
 Call native OS functions from my interpreter?
Hi,

why is it important to know, that the function is called correct?
I think, you should only give the possibility to call a OS function,
but you should not check if it's correct. It is up to you (or user)
to declare a function correct.

Bye, Holger.
--



Fri, 06 Jan 2006 07:55:38 GMT  
 Call native OS functions from my interpreter?

Quote:

> Hi,
> why is it important to know, that the function is called correct?
> I think, you should only give the possibility to call a OS function,
> but you should not check if it's correct. It is up to you (or user)
> to declare a function correct.

You're missing the point, I think, which is that C as a programming
language doesn't offer any way to call *any* function not known and
properly declared and provided already at source code translation
time.  There is no such thing as dynamic linking in standard C.

Some platforms do of course offer this as an extension, but none of
those is part of the C programming language.
--

Even if all the snow were burnt, ashes would remain.
--



Sat, 07 Jan 2006 16:09:21 GMT  
 Call native OS functions from my interpreter?

Quote:
>  The trouble is, how do I do I actually call a function whose signature
> I don't know until runtime? Is there an ANSI (or at least POSIX) way of
> doing such a call, something like va_arg? Or will I need to write
> platform-specific code in assembly? I've only dabbled in 68k asm so
> far...

There isn't any particularly portable way to call arbitrary functions from
your interpreter. You might want to consider whether that's a necessary
feature, though. If you simply provide an interface to (most of) the
POSIX-style API's of your Operating System, that should suffice for most
users.

Alternatively, you could provide a plug-in API that others could use to
extend the set of supported API's. This is how Perl and TCL handle the
issue, for example.

-Mark
--



Sat, 07 Jan 2006 16:10:02 GMT  
 Call native OS functions from my interpreter?
Hi,

Quote:
> You're missing the point, I think, which is that C as a programming
> language doesn't offer any way to call *any* function not known and
> properly declared and provided already at source code translation
> time.  There is no such thing as dynamic linking in standard C.

> Some platforms do of course offer this as an extension, but none of
> those is part of the C programming language.

but it is possible to have this feature inside a scripting language.

Script:
DECLARE INTEGER FunctionXY(Arg1: INTEGER, Arg2: INTEGER) FROM library.bla

Inside Interpreter

typedef int (* T_IntResult)(...);
typedef char (* T_CharResult)(...);

switch(ResultTypeDeclaration)
{
    case INTEGER_FUNCTION:
    {
        T_IntResult Func=(T_IntResult)GetFunction(FunctionName,
LibraryName);
        int Result;
        switch(NumberOfArgs)
        {
            case 0: Result=Func(); break;
            case 1: Result=Func(Arg[0]); break;
            case 2: Result=Func(Arg[0], Arg[1]); break;
            ...
        }
        break;
    }
    case ...

Quote:
}

It's not realy much work to have a typedef for each result type and calling
convention.
And if you support declarations with up to 10 arguments then you have still
no work

In fact, I've done something simmilar (but I've only to support windows, so
I have nothing
to do with calling conventions and I use asm to call the function "direct").

Bye, Holger.
--



Sun, 08 Jan 2006 01:58:53 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Interpreter -- Calling functions

2. Interpreter -- Calling functions

3. COBOL making dynamic call to c function under OS/390

4. Diff between PPC OS / HPC OS/ eXP OS

5. Call constructor from native code

6. how to call native code in DLL?

7. Debugging Native C called by java

8. Trouble compiling with Win2k/NT Native API Calls in code

9. Native C calls using VC++ 4.2

10. native dll calling with structs containing arrays of nested structs

11. Native code call?

12. Condition breakpoints and function calls and function calls in Immediate window

 

 
Powered by phpBB® Forum Software