Forwarded message from marcov@stack.nl (Marco van de Voort):
Subject: Re: c library imports revisited In-Reply-To: 1109917477.4701.80683@goedel.fjf.gnu.de To: Frank Heckenbach ih8mj@fjf.gnu.de Date: Mon, 7 Mar 2005 13:12:30 +0100 (CET) X-Mailer: ELM [version 2.4ME+ PL118 (25)]
: We do offer a limited selection of Unix calls though, but these are targeted : for specific Unix use, and in no way a substitute for the general calls in : e.g. an unit sysutils. They are prototyped roughly as the C calls, but : with "fp" prefixed, so manual pages still apply.
This probably explains what Rick was wondering about. If even glibc interfaces changes frequently (I suppose so, I'm not following them closely myself), other system's libcs are vastly different ...
About half the breakage occurs in startup files, mangling, and dealing with stuff like tls and threadsafe errno workarounds. GPC being gcc based might have less problems with that. OTOH GPC will typically rely heavier on libc, which might also cause different problems. (for FPC, the only thing that matters is the binary api, no C code)
The other half are more definitions changing, and distro maintainers that retire legacy calls earlier than others.
constant, rewriting) AFAICS would be either to have a 100% automatic translator that handles everything that occurs in some libc headers (which is likely to be all of standard C + extensions, via macros and inline functions), or to write wrappers in C that use the libc routines in the C way (thus being able to include headers as designed) and provide a well-defined C/Pascal interface. That's basically what we do in the RTS for a subset of libc (sometimes providing a bare-bones interfaces, sometimes higher-level).
This is extremely difficult (we also thought/tried this). Personally, I have given up on C2pascal like programs being fully automatic, and consider them more as a tool for manual translation. Even if it works, the output is horribly ugly, comments are stripped etc.
For the commctrl windows header,
1) I wrote a simple (200 line) pascal program that did some basic conversion and stripping of microsoft directives etc, sorting constants, structures and procedure defs into separate files, expanding simple macro's (that only were a bunch of typecasts). All done very simple, basic on heuristics, and modifying till output is ok.
2) Then I postedited the individual files with some global search and replace, and ran them through h2pas for the basic conversion (for the structs). The constants I fully converted using block-editor commands and search-n-replace.
3) Then again search and replace time and some simple filter procs (e.g. external clauses in calls)
4) Then manually merging, checking and fixing (days of work, but these are 10000+ lines), converting more complex macro's etc.
(the commctrl macro's where very picky about typing. They contained such systematic typecasting, one could infer the parameters typing, greatly increasing the automatic conversion ratio)
The basic problem with this process is the amount of manual labour (I did it in lost hours in trains etc), and the batchwise operating. Noticing an error in the conversion of (1) can't be corrected by redoing (1) since then all manual editing must be redone.
The trouble with C to pascal parsing is that everybody tries to go for the silver bullet of 100% automatic conversion. They all fail, and nobody is interested long enough to try a second approach, something what is more a construction kit for converting headers or so, or a scriptable or assisted system.
As I wrote the other day, automatic translation for most other C libraries is probably a whole lot easier (as glibc headers contain some of the most complicated macros I've seen so far in external headers, apart from the Linux kernel headers) ...
Try *BSD startup binary sources (crtbegin/crt0 etc) I once had 6 pages (roughly 100-150 lines) that became 12 asm instructions. They have cleaned up a bit since though.
The big funny thing was that it was a portable wrapper for syscalls. However the file was marked as "i386 only". Even if it was portable, it was so complex that I only figured it out by comparing 1) objdumped assembler files, 2) -S output 3) the source
P.s. Jedi also did an attempt at a header convertor, based on an experiment of dr Bob:
http://delphi-jedi.org/TEAM_DARTH_HOME or search for "headconv"
Afaik it is roughly the same principle as the FPC one, but less maintained (and the FPC one is already essentially unmaintained)
Frank Heckenbach wrote:
Forwarded message from marcov@stack.nl (Marco van de Voort):
Again, I don't pretend to be in this level of programmer expertise. A thread at FPC, "trying to advance linux fuel cell controller," gives some background. If the c-library is unmanageable, then everybody is screwed on every computing platform. When I pushed a "fiber optic super-network" in the early 1980s, programmers challenged everything, too. I'm a Biophysicist. And biology organizes itself for efficiency and stability.
Well, I got the new GPC going comfortably. No problems on SuSE8.1. The new docs completely "clarify" C access. Superb job. Time for Rick to play.
On 9 Mar 2005 at 4:50, Frank Heckenbach wrote:
[...]
The trouble with C to pascal parsing is that everybody tries to go for the silver bullet of 100% automatic conversion. They all fail, and nobody is interested long enough to try a second approach, something what is more a construction kit for converting headers or so, or a scriptable or assisted system.
This was my experience in porting the old gcc winapi headers to GPC (a few years ago). Dr Bob's "headconv" program gave me a good head start, but I also had to write a program to process the headconv output and clean it up a bit, after which I had to do some manual editing. It was a long, tedious process, but it did enable me to create a functional "Windows" unit.
It is clear that 100% conversion will not be possible for many C headers, and so another approach is needed (the compiler being able to compile C code? after all, we are already compiling assembler ...).
Best regards, The Chief -------- Prof. Abimbola A. Olowofoyeku (The African Chief) web: http://www.greatchief.plus.com/
Prof A Olowofoyeku (The African Chief) wrote:
It is clear that 100% conversion will not be possible for many C headers, and so another approach is needed (the compiler being able to compile C code? after all, we are already compiling assembler ...).
Not really. The compiler outputs assembler, so when it sees inline assembler, it just passes it through. But the compiler doesn't read, write or process any C code at any stage.
Indeed, for a 100% (or even close) C translation, we'd probably need access to some C features in the language. Some are already present (dubious thanks to Borland ;-), some more could be added in non-invasive ways (e.g., using obscure names).
Another thing is the ability to link compiled Pascal, C, etc. as well as assembler files together. But there are different compilers involved, and it won't help for interfaces, except in the way I described before -- write wrappers in C (or C++ for such libraries) and provide your own C[++]/Pascal interface.
Frank
Prof A Olowofoyeku (The African Chief) wrote:
On 9 Mar 2005 at 4:50, Frank Heckenbach wrote:
[...]
The trouble with C to pascal parsing is that everybody tries to go for the silver bullet of 100% automatic conversion. They all fail, and nobody is interested long enough to try a second approach, something what is more a construction kit for converting headers or so, or a scriptable or assisted system.
This was my experience in porting the old gcc winapi headers to GPC (a few years ago). Dr Bob's "headconv" program gave me a good head start, but I also had to write a program to process the headconv output and clean it up a bit, after which I had to do some manual editing. It was a long, tedious process, but it did enable me to create a functional "Windows" unit.
It is clear that 100% conversion will not be possible for many C headers, and so another approach is needed (the compiler being able to compile C code? after all, we are already compiling assembler ...).
Best regards, The Chief
Prof. Abimbola A. Olowofoyeku (The African Chief) web: http://www.greatchief.plus.com/
I'm glad the GPC and FPC developers are communicating to again provide the world with a robust Pascal programming platform.
There are differences in the languages already, that should be pointed out. For example, GPC uses "OpenHandle()" to apply the c-lib function "open()". But FPC uses "fdOpen()". Some of the parameters for read and write differ as well. "File handles" or "file descriptors" is "six of one, half-dozen of the other". These differences will simply take time. The beauty is that accessing c-libs works so well.
If I may, I encourage GPC installation on linux in a user directory, as the GPC docs suggest. This is a nice feature of GPC, no system directories are touched. Further, I suggest setting up a shell variable;
----------- gpcc='/home/user/../bin/gpc --command-line-options-... ...--unit-path=/home/user/.....' export gpcc -----------
Then, from any working directory at the prompt you just type;
$gpcc program.pas
Thanks for the fun :-)