hipe.erl
来自「OTP是开放电信平台的简称」· ERL 代码 · 共 1,594 行 · 第 1/4 页
ERL
1,594 行
%% -*- erlang-indent-level: 2 -*-%% ====================================================================%% Copyright (c) 1998 by Erik Johansson. All Rights Reserved %% ====================================================================%% Filename : hipe.erl%% Module : hipe%% Purpose : %% Notes : %% History : * 1998-01-28 Erik Johansson (happi@csd.uu.se): Created.%% CVS : $Id$%% ====================================================================%% @doc This is the direct interface to the HiPE compiler.%%%% <h3>Normal use</h3>%%%% <p>The normal way to native-compile an Erlang module using HiPE is to%% include the atom <code>native</code> in the Erlang compiler options,%% as in:%%%% <pre> 1> c(my_module, [native]).</pre></p>%%%% <p>Options to the HiPE compiler are then passed as follows:%%%% <pre> 1> c(my_module, [native,{hipe,Options}]).</pre></p>%%%% <p>For on-line help in the Erlang shell, call <a%% href="#help-0"><code>hipe:help()</code></a>. Details on HiPE compiler%% options are given by <a%% href="#help_options-0"><code>hipe:help_options()</code></a>.</p>%%%% <h3>Using the direct interface - for advanced users only</h3>%%%% To compile a module or a specific function to native code and%% automatically load the code into memory, call <a%% href="#c-1"><code>hipe:c(Module)</code></a> or <a%% href="#c-2"><code>hipe:c(Module, Options)</code></a>. Note that all%% options are specific to the HiPE compiler. See the <a%% href="#index">function index</a> for other compiler functions.%%%% <h3>Main Options</h3>%%%% Options are processed in the order they appear in the list; an%% early option will shadow a later one.%% <dl>%% <dt><code>o0, 'O0', o1, 'O1', o2, 'O2', o3, 'O3'</code></dt>%% <dd>Set optimization level (default 2).</dd>%%%% <dt><code>{'O', N}</code></dt>%% <dd>Set optimization level to <code>N</code>.</dd>%%%% <dt><code>load</code></dt>%% <dd>Automatically load the code into memory after compiling.</dd>%%%% <dt><code>time</code></dt>%% <dd>Reports the compilation times for the different stages%% of the compiler. Call <a%% href="#help_option-1"><code>hipe:help_option(time)</code></a> for%% details.</dd>%%%% <dt><code>{timeout, Time}</code></dt>%% <dd>Sets the time the compiler is allowed to use for the%% compilation. <code>Time</code> is time in ms or the atom%% <code>infinity</code> (the default).</dd>%%%% <dt><code>verbose</code></dt>%% <dd>Make the HiPE compiler output information about what it is%% being done.</dd>%% </dl>%% %% <h3>Advanced Options</h3>%%%% Note: You can also specify <code>{Option, false}</code> to turn a%% particular option off, or <code>{Option, true}</code> to force it on.%% Boolean-valued (<code>true</code>/<code>false</code>) options also%% have negative-form aliases, e.g. <code>no_load</code> = <code>{load,%% false}</code>.%%%% <p><dl>%% <dt><code>debug</code></dt>%% <dd>Outputs internal debugging information during%% compilation.</dd>%%%% <dt><code>icode_ssa_copy_prop</code></dt>%% <dd>Performs copy propagation on the SSA form on the Icode%% level.</dd>%%%% <dt><code>icode_ssa_const_prop</code></dt>%% <dd>Performs sparse conditional constant propagation on the SSA%% form on the Icode level.</dd>%%%% <dt><code>icode_ssa_struct_reuse</code></dt>%% <dd>Tries to factor out identical structure and list constructions%% on the Icode level.</dd>%%%% <dt><code>icode_type</code></dt>%% <dd>A type propagator on the Icode level.</dd>%%%% <dt><code>icode_range</code></dt>%% <dd>Performs integer range analysis on the Icode level.</dd>%%%% <dt><code>pp_all</code></dt>%% <dd>Equivalent to <code>[pp_beam, pp_icode, pp_rtl,%% pp_native]</code>.</dd>%%%% <dt><code>pp_asm</code></dt>%% <dd>Prints the assembly listing with addresses and bytecode.%% Currently available for x86 only.</dd>%%%% <dt><code>pp_beam, {pp_beam, {file, File}}</code></dt>%% <dd>Display the input Beam code to stdout or file.</dd>%%%% <dt><code>pp_icode, {pp_icode, {file, File}},%% {pp_icode, {only, Functions}}</code></dt>%% <dd>Pretty-print Icode intermediate code to stdout or file.</dd>%%%% <dt><code>pp_native, {pp_native, {file, File}},%% {pp_native, {only, Functions}}</code></dt>%% <dd>Pretty-print native code to stdout or file.</dd>%%%% <dt><code>pp_opt_icode, {pp_opt_icode, {file, File}},%% {pp_opt_icode, {only, Functions}}</code></dt>%% <dd>Pretty-print optimized Icode to stdout or file.</dd>%%%% <dt><code>pp_rtl, {pp_rtl, {file, File}},%% {pp_rtl, {only, Functions}}</code></dt>%% <dd>Pretty-print RTL intermediate code to stdout or file.</dd>%%%% <dt><code>regalloc</code></dt>%% <dd>Select register allocation algorithm. Used as%% <code>{regalloc, Method}</code>.%%%% <p><code>Method</code> is one of the following:%% <ul>%% <li><code>naive</code>: spills everything (for debugging and%% testing only).</li>%% <li><code>linear_scan</code>: fast compilation; not so good if%% only few registers available.</li>%% <li><code>graph_color</code>: slower, but gives better%% performance.</li>%% <li><code>coalescing</code>: tries hard to use registers; can be%% very slow, but typically results in code with best performance.</li>%% </ul></p></dd>%%%% <dt><code>remove_comments</code></dt>%% <dd>Remove comments from intermediate code.</dd>%%%% <dt><code>rtl_ssa_const_prop</code></dt>%% <dd>Performs sparse conditional constant propagation on the SSA%% form on the RTL level. </dd>%%%% <dt><code>rtl_lcm</code></dt>%% <dd>Lazy Code Motion on RTL.</dd>%%%% <dt><code>rtl_ssapre</code></dt>%% <dd>Lazy Partial Redundancy Elimination on RTL (SSA level).</dd>%%%% <dt><code>sparc_estimate_block_times</code></dt>%% <dd>Do not perform instruction scheduling, but annotate with cycle%% estimates</dd>%%%% <dt><code>sparc_post_schedule</code></dt>%% <dd>Perform instruction scheduling after register allocation as well.%% There are two reasons why this might be beneficial:%% <ol>%% <li>Spill code (rare).</li>%% <li>Register allocation may reuse registers in nearby%% instructions, which can destroy our careful schedule;%% post-scheduling tries to repair the damage (if any; no difference%% has been observed so far).</li>%% </ol></dd>%%%% <dt><code>sparc_profile</code></dt>%% <dd>Inserts profiling counters into code. You get an annotated%% CFG by <code>hipe_profile:annot(MFA)</code> (see also%% <code>misc/hipe_profile.erl</code> for more info). Also%% <code>{sparc_profile, Prof_type}</code>.%%%% <p><code>Prof_type</code> is one of the following:%% <ul>%% <li><code>true</code>/<code>false</code>: normal%% profiling/no profiling</li>%% <li><code>block</code>: </li>%% <li><code>arc</code>: </li>%% </ul></p>%% </dd>%%%% <dt><code>sparc_schedule</code></dt>%% <dd>Perform ILP scheduling on code (also annotates code with%% cycle estimates). Also <code>{sparc_schedule, Sched_type}</code>.%%%% <p><code>Sched_type</code> is one of are the following:%% <ul>%% <li><code>true</code>/<code>false</code>: normal%% scheduling/no scheduling</li>%% <li><code>ultra</code>:</li>%% </ul></p>%% </dd>%%%% <dt><code>use_indexing</code></dt>%% <dd>Use indexing for multiple-choice branch selection.</dd>%%%% <dt><code>use_callgraph</code></dt>%% <dd>Use a static call-graph for determining the order in which%% the functions of a module should be compiled (in reversed %% topological sort order).</dd>%% </dl></p>%%%% <h3>Debugging Options</h3>%% (May require that some modules have been%% compiled with the <code>DEBUG</code> flag.)%% <dl>%% <dt><code>rtl_show_translation</code></dt>%% <dd>Prints each step in the translation from Icode to RTL</dd>%% </dl>%%%% @end%% ====================================================================-module(hipe).-export([c/1, c/2, f/1, f/2, compile/1, compile/2, compile/4, compile_core/4, file/1, file/2, load/1, help/0, help_hiper/0, help_options/0, help_option/1, help_debug_options/0, version/0, has_hipe_code/1, set_architecture/1, init/1, pre_init/1]).-ifndef(DEBUG).-define(DEBUG,true).-endif.-include("hipe.hrl").-include("../rtl/hipe_literals.hrl").-define(COMPILE_DEFAULTS, [o2]).-define(DEFAULT_TIMEOUT, infinity).%% @spec load(Mod) -> {module, Mod} | {error, Reason}%% Mod = mod()%% Reason = term()%% %% @doc Like load/2, but tries to locate a BEAM file automatically.%%%% @see load/2load(Mod) -> load(Mod, beam_file(Mod)).%% @spec load(Mod, Bin) -> {module, Mod} | {error, Reason}%% Mod = mod()%% Reason = term()%% Bin = binary() | filename()%% filename() = term()%%%% @type mod() = atom(). A module name.%% %% @doc User interface for loading code into memory. The code can be%% given as a native code binary or as the file name of a BEAM file%% which should contain a native-code chunk. If only the module name is%% given (see <code>load/1</code>), the BEAM file is located%% automatically.%%%% @see load/1%% load(Mod, Bin) when is_binary(Bin) ->%% do_load(Mod, Bin, false);load(Mod, File) when is_atom(File) -> Architecture = erlang:system_info(hipe_architecture), ChunkName = hipe_unified_loader:chunk_name(Architecture), case beam_lib:chunks(File, [ChunkName]) of {ok,{_,[{_,Bin}]}} -> do_load(Mod, Bin, File); Error -> {error, Error} end.-define(USER_DEFAULTS, [load]).%% @spec c(Name) -> {ok, Name} | {error, Reason}%% Name = mod() | mfa()%% Reason = term()%%%% @equiv c(Name, [])c(Name) -> c(Name, []).%% @spec c(Name, options()) -> {ok, Name} | {error, Reason}%% Name = mod() | mfa()%% options() = [option()]%% option() = term()%% Reason = term()%%%% @type mfa() = {M::mod(),F::fun(),A::arity()}.%% A fully qualified function name.%%%% @type fun() = atom(). A function identifier.%%%% @type arity() = integer(). A function arity; always nonnegative.%% %% @doc User-friendly native code compiler interface. Reads BEAM code%% from the corresponding "Module<code>.beam</code>" file in the system%% path, and compiles either a single function or the whole module to%% native code. By default, the compiled code is loaded directly. See%% above for documentation of options.%%%% @see c/1%% @see c/3%% @see f/2%% @see compile/2c(Name, Options) -> c(Name, beam_file(Name), Options).%% @spec c(Name, File, options()) -> {ok, Name} | {error, Reason}%% Name = mod() | mfa()%% File = filename() | binary()%% Reason = term()%%%% @doc Like <code>c/2</code>, but reads BEAM code from the specified%% <code>File</code>.%%%% @see c/2%% @see f/2c(Name, File, Opts) -> %% No server if only one function is compiled case compile(Name, File, user_compile_opts(Opts)) of {ok, Res} -> case proplists:get_bool(dialyzer, Opts) of true -> {ok, Res}; false -> {ok, Name} end; Other -> Other end.%% @spec f(File) -> {ok, Name} | {error, Reason}%% File = filename() | binary()%% Name = mod()%% Reason = term()%%%% @equiv f(File, [])f(File) -> f(File, []).%% @spec f(File, options()) -> {ok, Name} | {error, Reason}%% File = filename() | binary()%% Name = mod()%% Reason = term()%%%% @doc Like <code>c/3</code>, but takes the module name from the%% specified <code>File</code>. This always compiles the whole module;%% there is no possibility to compile just a single function.%%%% @see c/3f(File, Opts) -> case file(File, user_compile_opts(Opts)) of {ok, Name, _} -> {ok, Name}; Other -> Other end.user_compile_opts(Opts) -> Opts ++ ?USER_DEFAULTS.%% @spec compile(Name) -> {ok, Binary} | {error, Reason}%% Name = mod() | mfa()%% Binary = binary()%% Reason = term()%% %% @equiv compile(Name, [])compile(Name) -> compile(Name, []).%% @spec compile(Name, options()) -> {ok, Binary} | {error, Reason}%% Name = mod() | mfa()%% Binary = binary()
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?