|
|
Figure 1. Schematic
representation of the Mathemagix
compilation phases.
|
The Mathemagix compilation process is decomposed
in several stages (see figure ?):
-
The Mathemagix input file is first parsed
into a mathemagix program. In the source code of the
compiler, mathemagix programs have the type MMX.
-
The main compilation process consists of finding an unambiguous
meaning for every symbol in the input program. Whenever a variable
var is used in different contexts
(e.g. as a function argument, a local variable, a
class field) or overloaded in a given context, then new symbols
var#1, var#2, etc. are created
which correspond to the different possible meanings of var.
Internally, the compilation of a mathemagix program yields a
typed program of type PRG.
Such a program has a unique “type” (of type TYP) and a unique penalty (of type
Penalty). Since
Mathemagix allows for ambiguous expressions,
the type of the compilation of any subexpression may be a so
called “ambiguous type'. Of course, the overall program is
required to have a single interpretation.
-
After the main compilation, several post-treatments can take
place. These post-treatments are quite independent from the main
compilation process and done inside separate modules. More modules
can be added later. Some useful modules are the following:
-
Inlining of all functions which were declared inline.
-
Nested functions, such as shift(x)(y) == x+y;
are not supported by C++.
A dedicated “unnesting” module transforms nested
function declarations into plain functions, which manipulate
suitable function objects.
All post-treatments are performed on typed programs of type PRG. Libraries are
provided for simple syntactic manipulations of mathemagix, typed
and C++ programs. The typed programs are richer than pure
mathemagix and C++ programs, because all such programs are
required to have types, making their manipulation more rigid and
safe.
-
After compilation, optimization and other post-treatments, we
obtain a typed program which can be transformed into a C++
program in a rather straightforward manner. Such C++ programs are generic expressions
of type CPP.
In principle, other back-ends are possible for the compiler. On
the one hand, we plan to write back-ends for other languages,
starting with C. On the other hand, the
final Mathemagix program is suitable for
efficient interpretation.
-
Mathemagix provides a C++
pretty printer which transforms the C++
program tree into a string. The final object file or binary is
obtained by using a standard C++ compiler.
© 2008 Joris van der Hoeven
Permission is granted to copy, distribute and/or modify this document
under the terms of the
GNU General Public License. If you
don't have this file, write to the Free Software Foundation, Inc., 59
Temple Place - Suite 330, Boston, MA 02111-1307, USA.