<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <HTML> <HEAD> <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <META name="GENERATOR" content="hevea 1.06-7 of 2001-11-14"> <TITLE> Native-code compilation (ocamlopt) </TITLE> </HEAD> <BODY TEXT=black BGCOLOR=white> <A HREF="manual024.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A> <A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A> <A HREF="manual026.html"><IMG SRC ="next_motif.gif" ALT="Next"></A> <HR> <TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#2de52d"><DIV ALIGN=center><TABLE> <TR><TD><A NAME="htoc115"><B><FONT SIZE=6>Chapter 11</FONT></B></A></TD> <TD WIDTH="100%" ALIGN=center><B><FONT SIZE=6>Native-code compilation (ocamlopt)</FONT></B></TD> </TR></TABLE></DIV></TD> </TR></TABLE> <A NAME="c:nativecomp"></A> <BR> This chapter describes the Objective Caml high-performance native-code compiler <TT>ocamlopt</TT>, which compiles Caml source files to native code object files and link these object files to produce standalone executables. <BR> <BR> The native-code compiler is only available on certain platforms. It produces code that runs faster than the bytecode produced by <TT>ocamlc</TT>, at the cost of increased compilation time and executable code size. Compatibility with the bytecode compiler is extremely high: the same source code should run identically when compiled with <TT>ocamlc</TT> and <TT>ocamlopt</TT>.<BR> <BR> It is not possible to mix native-code object files produced by <TT>ocamlc</TT> with bytecode object files produced by <TT>ocamlopt</TT>: a program must be compiled entirely with <TT>ocamlopt</TT> or entirely with <TT>ocamlc</TT>. Native-code object files produced by <TT>ocamlopt</TT> cannot be loaded in the toplevel system <TT>ocaml</TT>.<BR> <BR> <TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE> <TR><TD><A NAME="htoc116"><B><FONT SIZE=5>11.1</FONT></B></A></TD> <TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Overview of the compiler</FONT></B></TD> </TR></TABLE></DIV></TD> </TR></TABLE><BR> The <TT>ocamlopt</TT> command has a command-line interface very close to that of <TT>ocamlc</TT>. It accepts the same types of arguments: <UL><LI> Arguments ending in <TT>.mli</TT> are taken to be source files for compilation unit interfaces. Interfaces specify the names exported by compilation units: they declare value names with their types, define public data types, declare abstract data types, and so on. From the file <I>x</I><TT>.mli</TT>, the <TT>ocamlopt</TT> compiler produces a compiled interface in the file <I>x</I><TT>.cmi</TT>. The interface produced is identical to that produced by the bytecode compiler <TT>ocamlc</TT>.<BR> <BR> <LI>Arguments ending in <TT>.ml</TT> are taken to be source files for compilation unit implementations. Implementations provide definitions for the names exported by the unit, and also contain expressions to be evaluated for their side-effects. From the file <I>x</I><TT>.ml</TT>, the <TT>ocamlopt</TT> compiler produces two files: <I>x</I><TT>.o</TT>, containing native object code, and <I>x</I><TT>.cmx</TT>, containing extra information for linking and optimization of the clients of the unit. The compiled implementation should always be referred to under the name <I>x</I><TT>.cmx</TT> (when given a <TT>.o</TT> file, <TT>ocamlopt</TT> assumes that it contains code compiled from C, not from Caml).<BR> <BR> The implementation is checked against the interface file <I>x</I><TT>.mli</TT> (if it exists) as described in the manual for <TT>ocamlc</TT> (chapter <A HREF="manual022.html#c:camlc">8</A>).<BR> <BR> <LI>Arguments ending in <TT>.cmx</TT> are taken to be compiled object code. These files are linked together, along with the object files obtained by compiling <TT>.ml</TT> arguments (if any), and the Caml standard library, to produce a native-code executable program. The order in which <TT>.cmx</TT> and <TT>.ml</TT> arguments are presented on the command line is relevant: compilation units are initialized in that order at run-time, and it is a link-time error to use a component of a unit before having initialized it. Hence, a given <I>x</I><TT>.cmx</TT> file must come before all <TT>.cmx</TT> files that refer to the unit <I>x</I>.<BR> <BR> <LI>Arguments ending in <TT>.cmxa</TT> are taken to be libraries of object code. Such a library packs in two files (<I>lib</I><TT>.cmxa</TT> and <I>lib</I><TT>.a</TT>) a set of object files (<TT>.cmx</TT>/<TT>.o</TT> files). Libraries are build with <TT>ocamlopt -a</TT> (see the description of the <TT>-a</TT> option below). The object files contained in the library are linked as regular <TT>.cmx</TT> files (see above), in the order specified when the library was built. The only difference is that if an object file contained in a library is not referenced anywhere in the program, then it is not linked in.<BR> <BR> <LI>Arguments ending in <TT>.c</TT> are passed to the C compiler, which generates a <TT>.o</TT> object file. This object file is linked with the program.<BR> <BR> <LI>Arguments ending in <TT>.o</TT>, <TT>.a</TT> or <TT>.so</TT> (<TT>.obj</TT>, <TT>.lib</TT> and <TT>.dll</TT> under Windows) are assumed to be C object files and libraries. They are linked with the program.</UL> The output of the linking phase is a regular Unix executable file. It does not need <TT>ocamlrun</TT> to run.<BR> <BR> <TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE> <TR><TD><A NAME="htoc117"><B><FONT SIZE=5>11.2</FONT></B></A></TD> <TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Options</FONT></B></TD> </TR></TABLE></DIV></TD> </TR></TABLE><BR> The following command-line options are recognized by <TT>ocamlopt</TT>. <DL COMPACT=compact><DT><B><TT>-a</TT></B><DD> Build a library (<TT>.cmxa</TT>/<TT>.a</TT> file) with the object files (<TT>.cmx</TT>/<TT>.o</TT> files) given on the command line, instead of linking them into an executable file. The name of the library can be set with the <TT>-o</TT> option. The default name is <TT>library.cmxa</TT>.<BR> <BR> If <TT>-cclib</TT> or <TT>-ccopt</TT> options are passed on the command line, these options are stored in the resulting <TT>.cmxa</TT> library. Then, linking with this library automatically adds back the <TT>-cclib</TT> and <TT>-ccopt</TT> options as if they had been provided on the command line, unless the <TT>-noautolink</TT> option is given.<BR> <BR> <DT><B><TT>-c</TT></B><DD> Compile only. Suppress the linking phase of the compilation. Source code files are turned into compiled files, but no executable file is produced. This option is useful to compile modules separately.<BR> <BR> <DT><B><TT>-cc</TT> <I>ccomp</I></B><DD> Use <I>ccomp</I> as the C linker called to build the final executable and as the C compiler for compiling <TT>.c</TT> source files.<BR> <BR> <DT><B><TT>-cclib</TT> <TT>-l</TT><I>libname</I></B><DD> Pass the <TT>-l</TT><I>libname</I> option to the linker. This causes the given C library to be linked with the program.<BR> <BR> <DT><B><TT>-ccopt</TT> <I>option</I></B><DD> Pass the given option to the C compiler and linker. For instance, <TT>-ccopt -L</TT><I>dir</I> causes the C linker to search for C libraries in directory <I>dir</I>.<BR> <BR> <DT><B><TT>-compact</TT></B><DD> Optimize the produced code for space rather than for time. This results in slightly smaller but slightly slower programs. The default is to optimize for speed.<BR> <BR> <DT><B><TT>-i</TT></B><DD> Cause the compiler to print all defined names (with their inferred types or their definitions) when compiling an implementation (<TT>.ml</TT> file). This can be useful to check the types inferred by the compiler. Also, since the output follows the syntax of interfaces, it can help in writing an explicit interface (<TT>.mli</TT> file) for a file: just redirect the standard output of the compiler to a <TT>.mli</TT> file, and edit that file to remove all declarations of unexported names.<BR> <BR> <DT><B><TT>-I</TT> <I>directory</I></B><DD> Add the given directory to the list of directories searched for compiled interface files (<TT>.cmi</TT>), compiled object code files (<TT>.cmx</TT>), and libraries (<TT>.cmxa</TT>). By default, the current directory is searched first, then the standard library directory. Directories added with <TT>-I</TT> are searched after the current directory, in the order in which they were given on the command line, but before the standard library directory.<BR> <BR> If the given directory starts with <TT>+</TT>, it is taken relative to the standard library directory. For instance, <TT>-I +labltk</TT> adds the subdirectory <TT>labltk</TT> of the standard library to the search path.<BR> <BR> <DT><B><TT>-inline</TT> <I>n</I></B><DD> Set aggressiveness of inlining to <I>n</I>, where <I>n</I> is a positive integer. Specifying <TT>-inline 0</TT> prevents all functions from being inlined, except those whose body is smaller than the call site. Thus, inlining causes no expansion in code size. The default aggressiveness, <TT>-inline 1</TT>, allows slightly larger functions to be inlined, resulting in a slight expansion in code size. Higher values for the <TT>-inline</TT> option cause larger and larger functions to become candidate for inlining, but can result in a serious increase in code size.<BR> <BR> <DT><B><TT>-linkall</TT></B><DD> Forces all modules contained in libraries to be linked in. If this flag is not given, unreferenced modules are not linked in. When building a library (<TT>-a</TT> flag), setting the <TT>-linkall</TT> flag forces all subsequent links of programs involving that library to link all the modules contained in the library.<BR> <BR> <DT><B><TT>-noassert</TT></B><DD> Turn assertion checking off: assertions are not compiled. This flag has no effect when linking already compiled files.<BR> <BR> <DT><B><TT>-noautolink</TT></B><DD> When linking <TT>.cmxa</TT> libraries, ignore <TT>-cclib</TT> and <TT>-ccopt</TT> options potentially contained in the libraries (if these options were given when building the libraries). This can be useful if a library contains incorrect specifications of C libraries or C options; in this case, during linking, set <TT>-noautolink</TT> and pass the correct C libraries and options on the command line.<BR> <BR> <DT><B><TT>-nolabels</TT></B><DD> Ignore non-optional labels in types. Labels cannot be used in applications, and parameter order becomes strict.<BR> <BR> <DT><B><TT>-o</TT> <I>exec-file</I></B><DD> Specify the name of the output file produced by the linker. The default output name is <TT>a.out</TT>, in keeping with the Unix tradition. If the <TT>-a</TT> option is given, specify the name of the library produced. If the <TT>-output-obj</TT> option is given, specify the name of the output file produced.<BR> <BR> <DT><B><TT>-output-obj</TT></B><DD> Cause the linker to produce a C object file instead of an executable file. This is useful to wrap Caml code as a C library, callable from any C program. See chapter <A HREF="manual032.html#c:intf-c">18</A>, section <A HREF="manual032.html#s:embedded-code">18.7.5</A>. The name of the output object file is <TT>camlprog.o</TT> by default; it can be set with the <TT>-o</TT> option.<BR> <BR> <DT><B><TT>-p</TT></B><DD> Generate extra code to write profile information when the program is executed. The profile information can then be examined with the analysis program <TT>gprof</TT>. (See chapter <A HREF="manual031.html#c:profiler">17</A> for more information on profiling.) The <TT>-p</TT> option must be given both at compile-time and at link-time. Linking object files not compiled with <TT>-p</TT> is possible, but results in less precise profiling.<BR> <BR> <FONT COLOR=purple>Unix:</FONT> <BLOCKQUOTE> See the Unix manual page for <TT>gprof(1)</TT> for more information about the profiles.<BR> <BR> Full support for <TT>gprof</TT> is only available for certain platforms (currently: Intel x86/Linux and Alpha/Digital Unix). On other platforms, the <TT>-p</TT> option will result in a less precise profile (no call graph information, only a time profile). </BLOCKQUOTE> <FONT COLOR=purple>Windows:</FONT> <BLOCKQUOTE> The <TT>-p</TT> option does not work under Windows. </BLOCKQUOTE><BR> <BR> <DT><B><TT>-pack</TT></B><DD> Build an object file (<TT>.cmx</TT>/<TT>.o</TT> file) and its associated compiled interface (<TT>.cmi</TT>) that combines the <TT>.cmx</TT> object files given on the command line, making them appear as sub-modules of the output <TT>.cmx</TT> file. The name of the output <TT>.cmx</TT> file must be given with the <TT>-o</TT> option. For instance, <PRE> ocamlopt -pack -o p.cmx a.cmx b.cmx c.cmx </PRE>generates compiled files <TT>p.cmx</TT>, <TT>p.o</TT> and <TT>p.cmi</TT> describing a compilation unit having three sub-modules <TT>A</TT>, <TT>B</TT> and <TT>C</TT>, corresponding to the contents of the object files <TT>a.cmx</TT>, <TT>b.cmx</TT> and <TT>c.cmx</TT>. These contents can be referenced as <TT>P.A</TT>, <TT>P.B</TT> and <TT>P.C</TT> in the remainder of the program.<BR> <BR> <FONT COLOR=purple>Unix:</FONT> <BLOCKQUOTE> The <TT>-pack</TT> option is available only under platforms that provide the GNU <TT>binutils</TT> tools <TT>nm</TT> and <TT>objcopy</TT>. </BLOCKQUOTE><BR> <BR> <DT><B><TT>-pp</TT> <I>command</I></B><DD> Cause the compiler to call the given <I>command</I> as a preprocessor for each source file. The output of <I>command</I> is redirected to an intermediate file, which is compiled. If there are no compilation errors, the intermediate file is deleted afterwards. The name of this file is built from the basename of the source file with the extension <TT>.ppi</TT> for an interface (<TT>.mli</TT>) file and <TT>.ppo</TT> for an implementation (<TT>.ml</TT>) file.<BR> <BR> <DT><B><TT>-principal</TT></B><DD> Check information path during type-checking, to make sure that all types are derived in a principal way. All programs accepted in <TT>-principal</TT> mode are also accepted in default mode with equivalent types, but different binary signatures.<BR> <BR> <DT><B><TT>-rectypes</TT></B><DD> Allow arbitrary recursive types during type-checking. By default, only recursive types where the recursion goes through an object type are supported.<BR> <BR> <DT><B><TT>-S</TT></B><DD> Keep the assembly code produced during the compilation. The assembly code for the source file <I>x</I><TT>.ml</TT> is saved in the file <I>x</I><TT>.s</TT>.<BR> <BR> <DT><B><TT>-thread</TT></B><DD> Compile or link multithreaded programs, in combination with the <TT>threads</TT> library described in chapter <A HREF="manual038.html#c:threads">24</A>. What this option actually does is select a special, thread-safe version of the standard library.<BR> <BR> <DT><B><TT>-unsafe</TT></B><DD> Turn bound checking off on array and string accesses (the <TT>v.(i)</TT> and <TT>s.[i]</TT> constructs). Programs compiled with <TT>-unsafe</TT> are therefore faster, but unsafe: anything can happen if the program accesses an array or string outside of its bounds.<BR> <BR> <DT><B><TT>-v</TT></B><DD> Print the version number of the compiler and the location of the standard library directory, then exit.<BR> <BR> <DT><B><TT>-verbose</TT></B><DD> Print all external commands before they are executed, in particular invocations of the assembler, C compiler, and linker.<BR> <BR> <DT><B><TT>-version</TT></B><DD> Print the version number of the compiler in short form (e.g. <TT>3.06</TT>), then exit.<BR> <BR> <DT><B><TT>-w</TT> <I>warning-list</I></B><DD> Enable or disable warnings according to the argument <I>warning-list</I>. The argument is a string of one or several characters, with the following meaning for each character: <DL COMPACT=compact><DT> <B><TT>A</TT>/<TT>a</TT></B><DD> enable/disable all warnings. <DT><B><TT>C</TT>/<TT>c</TT></B><DD> enable/disable warnings for suspicious comments. <DT><B><TT>D</TT>/<TT>d</TT></B><DD> enable/disable warnings for deprecated features. <DT><B><TT>F</TT>/<TT>f</TT></B><DD> enable/disable warnings for partially applied functions (i.e. <TT>f x; </TT><I>expr</I> where the application <TT>f x</TT> has a function type). <DT><B><TT>L</TT>/<TT>l</TT></B><DD> enable/disable warnings for labels omitted in application. <DT><B><TT>M</TT>/<TT>m</TT></B><DD> enable/disable warnings for overriden methods. <DT><B><TT>P</TT>/<TT>p</TT></B><DD> enable/disable warnings for partial matches (missing cases in pattern matchings). <DT><B><TT>S</TT>/<TT>s</TT></B><DD> enable/disable warnings for statements that do not have type <TT>unit</TT> (e.g. <I>expr1</I><TT>; </TT><I>expr2</I> when <I>expr1</I> does not have type <TT>unit</TT>). <DT><B><TT>U</TT>/<TT>u</TT></B><DD> enable/disable warnings for unused (redundant) match cases. <DT><B><TT>V</TT>/<TT>v</TT></B><DD> enable/disable warnings for hidden instance variables. <DT><B><TT>X</TT>/<TT>x</TT></B><DD> enable/disable all other warnings. </DL> The default setting is <TT>-w Al</TT> (all warnings but labels enabled).<BR> <BR> <DT><B><TT>-warn-error</TT> <I>warning-list</I></B><DD> Turn the warnings indicated in the argument <I>warning-list</I> into errors. The compiler will stop on an error as soon as one of these warnings is emitted, instead of going on. The <I>warning-list</I> is a string of one or several characters, with the same meaning as for the <TT>-w</TT> option: an uppercase character turns the corresponding warning into an error, a lowercase character leaves it as a warning. The default setting is <TT>-warn-error a</TT> (all warnings are not treated as errors).<BR> <BR> <DT><B><TT>-where</TT></B><DD> Print the location of the standard library. </DL> <TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE> <TR><TD><A NAME="htoc118"><B><FONT SIZE=5>11.3</FONT></B></A></TD> <TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Common errors</FONT></B></TD> </TR></TABLE></DIV></TD> </TR></TABLE><BR> The error messages are almost identical to those of <TT>ocamlc</TT>. See section <A HREF="manual022.html#s:comp-errors">8.4</A>.<BR> <BR> <TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE> <TR><TD><A NAME="htoc119"><B><FONT SIZE=5>11.4</FONT></B></A></TD> <TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Compatibility with the bytecode compiler</FONT></B></TD> </TR></TABLE></DIV></TD> </TR></TABLE> <A NAME="s:compat-native-bytecode"></A><BR> This section lists the known incompatibilities between the bytecode compiler and the native-code compiler. Except on those points, the two compilers should generate code that behave identically. <UL><LI>The following operations abort the program (via an hardware trap or fatal Unix signal) instead of raising an exception: <UL><LI> integer division by zero, modulus by zero; <LI>stack overflow; <LI>on the Alpha processor only, floating-point operations involving infinite or denormalized numbers (all other processors supported by <TT>ocamlopt</TT> treat these numbers correctly, as per the IEEE 754 standard). </UL> In particular, notice that stack overflow caused by excessively deep recursion is reported by most Unix kernels as a ``segmentation violation'' signal.<BR> <BR> <LI>Signals are detected only when the program performs an allocation in the heap. That is, if a signal is delivered while in a piece of code that does not allocate, its handler will not be called until the next heap allocation.</UL> The best way to avoid running into those incompatibilities is to <EM>never</EM> trap the <TT>Division_by_zero</TT> and <TT>Stack_overflow</TT> exceptions, thus also treating them as fatal errors with the bytecode compiler as well as with the native-code compiler. Often, it is feasible to test the divisor before performing the operation, instead of trapping the exception afterwards. <BR> <BR> <HR> <A HREF="manual024.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A> <A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A> <A HREF="manual026.html"><IMG SRC ="next_motif.gif" ALT="Next"></A> </BODY> </HTML>