Re: Generic thinbinding toolkit for C

From: Warren W. Gay VE3WWG (ve3wwg_at_cogeco.ca)
Date: 11/21/03


Date: Fri, 21 Nov 2003 13:02:19 -0500

Lutz Donnerhacke wrote:

> * Warren W. Gay VE3WWG wrote:
>
>>Lutz Donnerhacke wrote:
>>
>>>* Warren Gay wrote:
>>>
>>>>What thinbind would be required to do, is to preprocess the header
>>>>file somewhat differently than the C's preprocessor. Instead of
>>>
>>>This destroys most of the original C API in a unusable way.
>>
>>Rubbish.
>
> I beg to differ. You have to write down an example C source using the
> API for the requested thinbinding. Then you preprocess this source code and
> now you can parse the result in order to find out what really happend.
>
> Example: Thinbinding to getc(3).
> #include <stdio.c>
> return getc(stdin);

Well, first of all, you're focusing on the program
that is _using_ getc, that was defined in stdio.h.

I am only interested in processing the stdio.h file
(not the client code). So within stdio.h (or nested
include), there is a #define for getc, if it is
a macro. That is what thinbind will be inspecting
closely.

> results in
> extern struct _IO_FILE *stdin;
> return _IO_getc (stdin);
> where
> struct _IO_FILE {
> int _flags;
> char* _IO_read_ptr;
...
> char _unused2[15 * sizeof (int) - 2 * sizeof (void *)];
> };
>
> struct _IO_marker {
> struct _IO_marker *_next;
> struct _IO_FILE *_sbuf;
> int _pos;
> };
>
> typedef long int __off_t;
> typedef long long int __off64_t;
> typedef void _IO_lock_t;
>
> You will notice, that the original API vanished by preprocessing.

Which is to be expected: all macros disappear by preprocessing.

Thin bind would have to be CPP syntax aware, and C aware
at various stages (in some stages, it will be wearing two
hats).

> introduces a huge structure and a different name. What do you expect as the
> generated Ada-API?

I agree that there will still be some situations that will
be too difficult to solve (at least in the early stages
of such a tool at least). But this is where the hints
file comes in:

stdio.h.hints:
--------------
   wrapper: getc

or
   #ifdef FreeBSD_HOST
      wrapper: getc -- Create C wrapper routine
   #else
      function returns int: getc -- Use native function call
   ...

may be all that is necessary to get thinbind to compile a
little C wrapper routine, that Ada can easily call, and
that the C wrapper can invoke the real C macro. This is
not ideal from a performance perspective, but it works.

>>The whole purpose of preprocessing it in thinbind is to compile
>>a package for Ada use. It need _NOT_ be the same. You only
>>need to arrive at the correct internal answers. There are
>>many ways to skin a cat (pardon the expression to cat lovers
>>out there).
>
> You wrote about preprocessing and then searching the API. This does not work.
> You have to specify the API and preprocessing it to find the real C code.

You can do both at the same time. You have to manage a lot
more state information this way, but it can be done.

I have to end this post here, because I must attend a meeting
right now. Stay tuned. ;-)

-- 
Warren W. Gay VE3WWG
http://home.cogeco.ca/~ve3wwg
[Remove nospam from the email address:
the worms made me do it!]