This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Calling compiler from C program...
- From: Basile Starynkevitch <basile at starynkevitch dot net>
- To: Johann Klammer <klammerj at a1 dot net>
- Cc: gcc at gcc dot gnu dot org
- Date: Thu, 8 Nov 2012 18:31:14 +0100
- Subject: Re: Calling compiler from C program...
- References: <509BE0A0.9000803@a1.net>
On Thu, Nov 08, 2012 at 05:41:04PM +0100, Johann Klammer wrote:
> Hello,
> Some compilers feature function call interfaces for on-the-fly
> compilation (C# and lcc).
And most importantly, LLVM/Clang. (And also tinycc). LLVM is mostly a library
to generate quite good machine code from some internal representation.
> Now I wonder if this is possible with gcc. Is there a
> documented/stable interface for compiling snippets of C code
> programatically from inside a C program? If not, how much work would
> it involve? Would it be possible to do certain initialisation steps
> only once? Does gcc leak memory? Would it be able to run for
> extended periods of time, without having to end the process? Which
> parts of the compiler are reentrant/thread safe? Is the state nicely
> encapsulated or strewn across loads of globals/statics?
I believe it is not easily doable for GCC, but it might not be important, because you can definitely fork a gcc command to make the compilation (or even a make command to drive the gcc command).
It is not easily doable, because GCC has a lot of global state, and may have some small memory leaks.
And GCC is definitely not re-entrant.
There has been some past exploratory efforts around the idea of a GCC compiler server.
And it might not be very important in practice, because GCC is mostly useful to
compile optimized code from C (or C++, Ada, Fortran....) code (if you just want
to translate quickly generated C code to slow x86 machine code, you might use
tinycc); whatever technique or tool you use generating optimized code
(i.e. code comparable in quality to what gcc -O2 produces) will take a significant
amount of CPU time, so the overhead of starting a new process is practically negligible.
However GCC does have a plugin ability. Perhaps you might bent your program to make it
a GCC plugin.
FWIW, MELT on http://gcc-melt.org/ is a big GCC plugin
translating a domain specific language called MELT to some kind of extension for GCC,
and MELT uses exactly the technique I am suggesting: generating C code on the fly,
forking a compiler (thru make -> gcc), and dlopen-ing the generated *.so module. This works
surprisingly well in practice, and is even compatible on current computers
-laptops or desktops running GNU/Linux- with interactive use (you can type a sequence of MELT expressions which are translated to a .c file, then compiled to a .so file, then dlopen-ed, and all this happens in a fraction of a second).
So if you want to use GCC from inside a compiler, a practical way is to generate a C file and fork the compilation. This works very well, and many software products are doing that.
If you insist of having a reentrant easily usable library with a well defined interface to compile code to machine code or to object files, LLVM is today more suitable for this usage than GCC is. (but that might change in a few years).
Regards.
--
Basile STARYNKEVITCH http://starynkevitch.net/Basile/
email: basile<at>starynkevitch<dot>net mobile: +33 6 8501 2359
8, rue de la Faiencerie, 92340 Bourg La Reine, France
*** opinions {are only mines, sont seulement les miennes} ***