Euphoria To C Translator
1. Introduction The Euphoria to C Translator will translate any Euphoria program into equivalent C source code. There are versions of the translator for Windows, DOS, Linux and FreeBSD. After translating a Euphoria program to C, you can compile and link using one of the supported C compilers. This will give you an executable file that will typically run much faster than if you used the Euphoria interpreter. It is assumed that you have already installed the Euphoria 2.4 Interpreter package on your system, and that your EUDIR and PATH environment variables are set correctly. For each supported C compiler, on Windows, DOS, Linux or FreeBSD, you'll find a .ZIP file on the RDS site containing these files: 1. the translator (one per platform) ec.exe - DOS ecw.exe - Windows ecu - Linux ecu - FreeBSD 2. a run-time library (one per C compiler) ec.lib - DOS (Watcom) ec.a - DOS (DJGPP) ecw.lib - Windows (Watcom) ecwl.lib - Windows (Lcc) ecwb.lib - Windows (Borland) ecu.a - Linux (GNU) ecu.a - FreeBSD (GNU) 3. (Watcom only) files to support the CauseWay DOS extender cwc.exe - file compressor le23p.exe - format converter cwstub.exe - the DOS extender 4. (DJGPP only) the Allegro graphics library, compiled specially for the translator. Download liballeg.zipTo install the Euphoria To C Translator, put the required .exe's and library files into your euphoria\bin directory. The euphoria\include directory already contains euphoria.h, a C include file needed by all translated programs. Note:
The Translator currently works with GNU C on Linux or FreeBSD, with either Watcom C or DJGPP C on DOS, and with either Watcom C, Lcc or Borland 5.5 on Windows. The Watcom and GNU C implementations are 100% compatible with the Euphoria Interpreter. The others are about 99% compatible. We recommend Borland over Lcc. Borland compiles faster, produces better code, and has fewer bugs compared to Lcc. The Translator has been tested with GNU C and the ncurses library available with Red Hat Linux 5.2 or later, and FreeBSD 4.5 or later. It has been tested with Watcom C/C++ 9.5, 10.6 and 11.0. Watcom 11.0 is open source and free. Look for it at: http://www.openwatcom.org The Watcom DOS32 package includes the CauseWay DOS extender and file compressor. CauseWay is now open source and free. You can find out more about it at: http://www.devoresoftware.com emake.bat and objfiles.lnk will link in the CauseWay extender automatically. Other DOS extenders, such as DOS4GW, do not work well with the Translator. The Translator looks for "WATCOM", "LCC", "BORLAND" or "DJGPP" as either environment variables or directories on your PATH. It will generate an emake.bat file that invokes the appropriate compiler and linker. Notes:
Running the Translator is similar to running the Interpreter. On DOS you would type: ec shell.ex or ec shellbut instead of running the shell.ex program, the Translator will create several C source files. It will also create a file called emake.bat that will compile and link the C files for you. Just type: emakeWhen the C compiling and linking is finished, you will have a file called: shell.exe
When you run shell.exe, it should run the same as if you had typed:
ex shell
Note to Linux and FreeBSD users:
Command-Line Options If you happen to have more than one C compiler for a given platform, you can select the one you want to use with a command-line option:
ecw -bor pretend.exwTo make a Windows .dll file, or Linux or FreeBSD .so file, just add -dll to the command line. e.g. ecw -bor -dll mylib.ewNote:
Simply by adding -dll to the command line, the Translator will build a Windows .dll (Linux/FreeBSD .so) file instead of an executable program. You can translate and compile a set of useful Euphoria routines, and share them with other people, without giving them your source. Furthermore, your routines will likely run much faster when translated and compiled. Both translated/compiled and interpreted programs will be able to use your library. Only the global Euphoria procedures and functions, i.e. those declared with the "global" keyword, will be exported from the .dll (.so). Any Euphoria program, whether translated/compiled or interpreted, can link with a Euphoria .dll (.so) using the same mechanism that lets you link with a .dll (.so) written in C. The program first calls open_dll() to open the .dll or .so file, then it calls define_c_func() or define_c_proc() for any routines that it wants to call. It calls these routines using c_func() and c_proc(). See library.doc for the details. The routine names exported from a Euphoria .dll will vary depending on which C compiler you use. GNU C on Linux or FreeBSD exports the names exactly as they appear in the C code produced by the Translator, e.g. a Euphoria routine global procedure foo(integer x, integer y)would be exported as "_0foo" or maybe "_1foo" etc. The underscore and digit are added to prevent naming conflicts. The digit refers to the Euphoria file where the symbol is defined. The main file is numbered as 0. The include files are numbered in the order they are encountered by the compiler. You should check the C source to be sure. Lcc would export foo() as "__0foo@8", where 8 is the number of parameters (2) times 4. You can check the .def file created by the Translator to see all the exported names. For Borland the Translator also creates a .def file, but this .def file renames the exported symbols back into the same names that you used in your Euphoria source, so foo() would be exported as "foo". For Watcom the same renaming as with Borland occurs, but instead of a .def file, an EXPORT command is added to objfiles.lnk for each exported symbol. With Borland and Watcom you can edit the .def or objfiles.lnk file, and rerun emake.bat, to rename the exported symbols, or remove ones that you don't want to export. With Lcc you can remove symbols but you can't rename them. Having nice exported names is not critical, since the name need only appear once in each Euphoria program that uses the .dll, i.e. in a single define_c_func() or define_c_proc() statement. The author of a .dll should probably provide his users with a Euphoria include file containing the necessary define_c_func() and define_c_proc() statements, and he might even provide a set of Euphoria "wrapper" routines to call the routines in the .dll. When you call open_dll(), any top-level Euphoria statements in the .dll or .so will be executed automatically, just like a normal program. This gives the library a chance to initialize its data structures prior to the first call to a library routine. For many libraries no initialization is required. To pass Euphoria data (atoms and sequences) as arguments, or to receive a Euphoria object as a result, you will need to add the following new data types to euphoria\include\dll.e: -- New Euphoria types for .dll (.so) arguments and return values: global constant E_INTEGER = #06000004, E_ATOM = #07000004, E_SEQUENCE= #08000004, E_OBJECT = #09000004Use these in define_c_proc() and define_c_func() just as you currently use C_INT, C_UINT etc. to call C .dll's and .so's. Currently, file numbers returned by open(), and routine id's returned by routine_id(), can be passed and returned, but the library and the main program each have their own separate ideas of what these numbers mean. Instead of passing the file number of an open file, you could instead pass the file name and let the .dll (.so) open it. Unfortunately there is no simple solution for passing routine id's. This might be fixed in the future. Euphoria .dlls (.so's) can also be used by C programs as long as only 31-bit integer values are exchanged. If a 32-bit pointer or integer must be passed, and you have the source to the C program, you could pass the value in two separate 16-bit integer arguments (upper 16 bits and lower 16 bits), and then combine the values in the Euphoria routine into the desired 32-bit atom.
On DOS32 with Watcom, if the Translator finds the CauseWay files, cwc.exe and le23p.exe in euphoria\bin, it will add commands to emake.bat that will compress your executable file. If you don't want compression, you can edit emake.bat, or remove or rename cwc.exe and/or le23p.exe. On Linux, FreeBSD, Windows, and DOS32 with DJGPP, emake does not include a command to compress your executable file. If you want to do this we suggest you try the free UPX compressor. You can get UPX from: http://upx.sourceforge.net The Translator deletes routines that are not used, including those from the standard Euphoria include files. After deleting unused routines, it checks again for more routines that have now become unused, and so on. This can make a big difference, especially with Win32Lib-based programs where a large file is included, but many of the included routines are not used in a given program. Nevertheless, your compiled executable file will likely be larger than the same Euphoria program bound with the Interpreter. This is partly due to the Interpreter being a compressed executable. Also, Euphoria statements are extremely compact when stored in a bound file. They need more space after being translated to C, and compiled into machine code. Future versions of the Translator will produce faster and smaller executables. All Euphoria programs can be translated to C, and with just a few exceptions noted below, will run the same as with the Interpreter (but hopefully faster). The Interpreter and Translator share the same parser, so you will get the same syntax errors, variable not declared errors etc. with either one. The Translator reads your whole program before trying to do any translation. Occasionally it might catch a syntax error that the Interpreter doesn't see, because the Interpreter starts executing top-level statements immediately without waiting for the end of your program. This also means that if you have top-level statements in your program that modify a file that is later included, you will get a different result with the Translator. Very few programs use this "dynamic include" technique. The Interpreter automatically expands the call stack (until memory is exhausted), so you can have a huge number of levels of nested calls. Most C compilers, on most systems, have a pre-set limit on the size of the stack. Consult your compiler or linker manual if you want to increase the limit, for example if you have a recursive routine that might need thousands of levels of recursion. Modify the link command in emake.bat. For Watcom C, use OPTION STACK=nnnn, where nnnn is the number of bytes of stack space. Note:
You should debug your program with the Interpreter. When C code crashes you'll typically get a very cryptic machine exception. If your translated .exe program does crash, the first thing you should do is run your program with the Interpreter, using the same inputs, and preferably with type_check turned on. Some of the run-time routines are still capable of catching an error and reporting it to the file ex.err. As far as RDS is concerned, any executable programs that you create with this Translator may be distributed royalty-free. You are free to incorporate any Euphoria files provided by RDS into your application. You may not distribute any Complete Edition Translator, or any run-time library file that comes with the Complete Edition for any platform. You may not distribute any hacked or cracked (reverse engineered) versions of any library or Translator, whether it's part of the Public Domain or Complete Edition. In January 2000, the CauseWay DOS extender was donated to the public domain by Michael Devore. He has surrendered his copyright, and encourages anyone to use it freely, including for commercial use. In general, if you wish to use Euphoria code written by 3rd parties, you had better honor any restrictions that apply. If in doubt, you should ask for permission. On Linux, FreeBSD and DJGPP for DOS32, the GNU Library licence will normally not affect programs created with this Translator. Simply compiling with GNU C does not give the Free Software Foundation any jurisdiction over your program. If you statically link their libraries you will be subject to their Library licence, but the standard compile/link procedure in emake does not statically link any FSF libraries, so there should be no problem. The ncurses library is the only one statically linked, and although the Free Software Foundation now holds the copyright, ncurses is not subject to the GNU Library licence, since it was donated to FSF by authors who did not wish the GNU licence to apply to it. See ncurses.h for the copyright notice. Disclaimer:
Programs (including .dll's and .so's) created using the free Public Domain Translator will display a brief message on the screen prior to execution. When you register for the Complete Edition Translator, you'll get:
Many large programs have been successfully translated and compiled using each of the supported C compilers, and the Translator is now quite stable. Note:
In some cases a huge Euphoria routine is translated to C, and it proves to be too large for the C compiler to process. If you run into this problem, make your Euphoria routine smaller and simpler. You can also try turning off C optimization in emake.bat for just the .c file that fails. Breaking up a single constant declaration of many variables into separate constant declarations of a single variable each, may also help. Euphoria has no limits on the size of a routine, or the size of a file, but most C compilers do. The Translator will automatically produce multiple small .c files from a large Euphoria file to avoid stressing the C compiler. It won't however, break a large routine into smaller routines.
Send bug reports to rds@RapidEuphoria.com
|