Elixir代码最终编译成为erlang代码,这个过程是怎样的?本文通过一个小测试做下探索.
 
 
 
 
编译一旦完成,你就看到了真相
 
Elixir代码组织方式一方面和Erlang一样才用非常扁平的代码模块结构,另一方面Elixir同时支持嵌套.Elixir比较方便的一点是可以在Elixir Shell中完成对模块的定义.看下面的方式:
 
iex> defmodule Math do
...> def sum(a, b) do
...> a + b
...> end
...> end iex> Math.sum(1, 2)
3
下面我们把代码放在m.ex模块中,模块的名字和代码文件的名字是可以不一样的,在编译之后文件夹中新增了一个Elixir.Math.beam的文件.换句话说,elixirc已经把m.ex文件编译成Elixir.Math.beam,按照Erlang对模块名称和文件名一致性的要求,我们可以在Erlang的Shell中验证一下:
 
 
[root@nimbus elixir]# elixirc m.ex
[root@nimbus elixir]# ls
Elixir.Math.beam m.ex [root@nimbus elixir]# erl
Erlang R16B01 (erts-5.10.2) [source] [64-bit] [smp:2:2] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V5.10.2 (abort with ^G)
1> 'Elixir.Math':sum(12,23).
35
2>
   是不是和我们预期的一样?之前提到过多次从beam中还原源代码的方法,现在我们动手看下这个Elixir.Math.beam的Erlang代码是怎样的,输出我做了一下排版:
 
> {ok,{_,[{abstract_code,{_,AC}}]}} = beam_lib:chunks("Elixir.Math",[abstract_code]).
{ok,{'Elixir.Math',
[{abstract_code,
{raw_abstract_v1,
[{attribute,,compile, {no_auto_import,[{bitsize,},{apply,},
{spawn,}, {spawn_link,}, {spawn_monitor,}, {spawn_opt,},
{spawn_opt,}, {spawn,},
{spawn_link,}, {spawn_opt,},
{spawn_opt,}, {nodes,...},
{...}|...]}},
{attribute,,file,{"/data2/elixir/m.ex",}},
{attribute,,module,'Elixir.Math'},
{attribute,,export,[{'__info__',},{sum,}]},
{function,,'__info__',,
[{clause,,
[{atom,,functions}],
[],
[{cons,,{...},...}]},
{clause,,[{atom,,macros}],[],[{nil,}]},
{clause,,[{atom,,docs}],[],[{cons,...}]},
{clause,,[{atom,,...}],[],[{...}]},
{clause,,[{atom,...}],[],[...]},
{clause,,[{...}],[],...}]},
{function,,sum,,
[{clause,,
[{var,,a},{var,,b}],
[],
[{op,,...}]}]}]}}]}}
> io:fwrite("~s~n", [erl_prettypr:format(erl_syntax:form_list(AC))]).
-compile({no_auto_import,
[{bitsize, }, {apply, }, {spawn, }, {spawn_link, },
{spawn_monitor, }, {spawn_opt, }, {spawn_opt, },
{spawn, }, {spawn_link, }, {spawn_opt, },
{spawn_opt, }, {nodes, }, {disconnect_node, },
{integer_to_list, }, {integer_to_binary, }, {max, },
{min, }, {port_control, }, {port_connect, },
{port_command, }, {port_command, }, {port_close, },
{spawn_monitor, }, {spawn, }, {load_module, },
{spawn_link, }, {binary_to_float, },
{float_to_binary, }, {float_to_binary, },
{list_to_integer, }, {integer_to_binary, },
{binary_to_integer, }, {binary_to_integer, },
{check_old_code, }, {binary_part, }, {binary_part, },
{binary_to_term, }, {binary_to_existing_atom, },
{binary_to_atom, }, {atom_to_binary, },
{bitstring_to_list, }, {list_to_bitstring, },
{bit_size, }, {byte_size, }, {tuple_size, },
{is_bitstring, }, {list_to_existing_atom, },
{iolist_to_binary, }, {iolist_size, },
{is_boolean, }, {is_record, }, {is_record, },
{is_function, }, {is_function, }, {is_binary, },
{is_reference, }, {is_port, }, {is_pid, },
{is_number, }, {is_integer, }, {is_float, },
{is_tuple, }, {is_list, }, {is_atom, }, {error, },
{error, }, {is_process_alive, }, {demonitor, },
{demonitor, }, {monitor, }, {whereis, },
{unregister, }, {unlink, }, {tuple_to_list, },
{trunc, }, {tl, }, {time, }, {throw, },
{term_to_binary, }, {term_to_binary, },
{statistics, }, {split_binary, }, {spawn_link, },
{spawn, }, {size, }, {setelement, }, {self, },
{round, }, {registered, }, {register, }, {put, },
{purge_module, }, {processes, }, {process_info, },
{process_info, }, {process_flag, }, {process_flag, },
{pre_loaded, }, {pid_to_list, }, {open_port, },
{now, }, {nodes, }, {node, }, {node, },
{monitor_node, }, {module_loaded, }, {make_ref, },
{list_to_tuple, }, {list_to_pid, },
{list_to_integer, }, {list_to_float, },
{list_to_binary, }, {list_to_atom, }, {link, },
{length, }, {is_alive, }, {integer_to_list, },
{hd, }, {halt, }, {halt, }, {halt, },
{group_leader, }, {group_leader, }, {get_keys, },
{get, }, {get, }, {garbage_collect, },
{garbage_collect, }, {float_to_list, },
{float_to_list, }, {float, }, {exit, }, {exit, },
{erase, }, {erase, }, {element, },
{delete_module, }, {date, }, {check_process_code, },
{binary_to_term, }, {binary_to_list, },
{binary_to_list, }, {atom_to_list, }, {apply, },
{abs, }]}). -file("/data2/elixir/m.ex", ). -module('Elixir.Math'). -export(['__info__'/, sum/]). '__info__'(functions) -> [{sum, }];
'__info__'(macros) -> [];
'__info__'(docs) ->
[{{sum, }, , def,
[{a, [{line, }], nil}, {b, [{line, }], nil}], nil}];
'__info__'(moduledoc) -> {, nil};
'__info__'(module) -> 'Elixir.Math';
'__info__'(atom) -> module_info(atom). sum(a, b) -> a + b.
ok
>

如何编译的?

 
 
  下面我们探究一下Elixir编译的过程,切入点当然是elixirc,打开这个脚本:
 
 
可以看到完成了一些环境变量解析之后,最终是调用了elixir
exec "$SCRIPT_PATH"/elixir +compile "$@"
OK,我们继续跟进elixir,经过一番参数检查,变量解析后,最后执行的命令类似下面:
  
 erl -pa "$SCRIPT_PATH"/../lib/*/ebin -noshell  -s elixir start_cli -extra +compile
简单回顾一下erlang 运行时环境启动的参数,erl的参数分三种:加号+后面跟的是 emulator flags,单连字符"-"后面跟的是flags,init进程会完成这些参数的解析; -extra 后面跟的内容都会被当做是plain arguments. http://erlang.org/doc/man/erl.html
  
% erl +W w -sname arnie +R  -s my_init -extra +bertie
(arnie@host)> init:get_argument(sname).
{ok,[["arnie"]]}
(arnie@host)> init:get_plain_arguments().
["+bertie"]

Here +W w and +R 9 are emulator flags. -s my_init is an init flag, interpreted by init. -sname arnie is a user flag, stored by init. It is read by Kernel and will cause the Erlang runtime system to become distributed. Finally, everything after -extra (that is, +bertie) is considered as plain arguments.

% erl -myflag
> init:get_argument(myflag).
{ok,[[""]]}
> init:get_plain_arguments().
[]

Here the user flag -myflag 1 is passed to and stored by the init process. It is a user defined flag, presumably used by some user defined application.

 
 
 书归正传,elixir代码里面给我们后续跟进的线索"-s elixir start_cli",废话少说,打开文件:
 
 
%% Boot and process given options. Invoked by Elixir's script.
start_cli() ->
application:start(?MODULE), 'Elixir.Kernel.CLI':main(init:get_plain_arguments()).
 
ok,下面我们手工完成m.ex文件的编译过程(为了方便执行你可以去/elixir/lib/elixir/ebin文件夹),我们分两步1.启动elixir 2.调用编译函数 'Elixir.Kernel.CLI':main(["+compile","m.ex"]).之所以要启动elixir,是为了完成类似code_server的职责.
 
[root@nimbus ebin]# erl
Erlang R16B01 (erts-5.10.) [source] [-bit] [smp::] [async-threads:] [hipe] [kernel-poll:false] Eshell V5.10.2 (abort with ^G)
> application:start(elixir).
ok > 'Elixir.Kernel.CLI':main(["+compile","m.ex"]).
检查文件夹中的文件,是不是已经编译好了.
 
{ok,"今天就到这里."}
 
 
如果你要继续跟进代码,你可以看到:
  @doc """
This is the API invoked by Elixir boot process.
"""
def main(argv) do
argv = lc arg inlist argv, do: String.from_char_list!(arg) { config, argv } = process_argv(argv, Kernel.CLI.Config.new)
System.argv(argv) run fn ->
command_results = Enum.map(Enum.reverse(config.commands), &process_command(&1, config))
command_errors = lc { :error, msg } inlist command_results, do: msg
errors = Enum.reverse(config.errors) ++ command_errors if errors != [] do
Enum.each(errors, &IO.puts(:stderr, &1))
System.halt(1)
end
end, config.halt
end

 

 defp process_argv(["+compile"|t], config) do
process_compiler t, config
end

  

 
 
 
最后,于"金蝉脱壳"上映之际小图一张  
 

[Erlang 0109] From Elixir to Erlang Code的更多相关文章

  1. elixir 调用erlang 代码

    备注:    项目比较简单,主要是elixir 混合erlang 代码,elixir 调用erlang 模块方法   1. 初始化项目   mix new erlangelixirdemo 项目结构如 ...

  2. [Erlang 0116] 当我们谈论Erlang Maps时,我们谈论什么 Part 1

         Erlang 增加 Maps数据类型并不是很突然,因为这个提议已经进行了2~3年之久,只不过Joe Armstrong老爷子最近一篇文章Big changes to Erlang掀起不小了风 ...

  3. [Erlang 0117] 当我们谈论Erlang Maps时,我们谈论什么 Part 2

    声明:本文讨论的Erlang Maps是基于17.0-rc2,时间2014-3-4.后续Maps可能会出现语法或函数API上的有所调整,特此说明. 前情提要: [Erlang 0116] 当我们谈论E ...

  4. Erlang基础 -- 介绍 -- 历史及Erlang并发

    前言 最近在总结一些Erlang编程语言的基础知识,拟系统的介绍Erlang编程语言,从基础到进阶,然后再做Erlang编程语言有意思的库的分析. 其实,还是希望越来越多的人关注Erlang,使用Er ...

  5. [Erlang 0125] Know a little Erlang opcode

    Erlang源代码编译为beam文件,代码要经过一系列的过程(见下面的简图),Core Erlang之前已经简单介绍过了Core Erlang,代码转换为Core Erlang,就容易拨开一些语法糖的 ...

  6. [Erlang 0128] Term sharing in Erlang/OTP 下篇

    继续昨天的话题,昨天提到io:format对数据共享的间接影响,如果是下面两种情况恐怕更容易成为"坑", 呃,恰好我都遇到过; 如果是测试代码是下面这样,得到的结果会是怎样?猜! ...

  7. [Erlang 0127] Term sharing in Erlang/OTP 上篇

    之前,在 [Erlang 0126] 我们读过的Erlang论文 提到过下面这篇论文: On Preserving Term Sharing in the Erlang Virtual Machine ...

  8. [Erlang 0121] 当我们谈论Erlang Maps时,我们谈论什么 Part 3

    Erlang/OTP 17.0 has been released  http://www.erlang.org/download/otp_src_17.0.readme     Erlang/OTP ...

  9. [Erlang 0113] Elixir 编译流程梳理

    注意:目前Elixir版本还不稳定,代码调整较大,本文随时失效      之前简单演示过如何从elixir ex代码生成并运行Erlang代码,下面仔细梳理一遍elixir文件的编译过程,书接上文,从 ...

随机推荐

  1. ABP(现代ASP.NET样板开发框架)系列之2、ABP入门教程

    点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之2.ABP入门教程 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)” ...

  2. 电容与EMC-电容不同功能时对整板EMC的作用

    一般我们的pcb板的器件有很多种类,但是值得特别关注的,很多人都会说是BGA.接口.IC.晶振之类,因为这些都是layout功能模块以及设计难点.然而数量上占绝对优势的器件却是阻容器件,之前围殴阻抗时 ...

  3. Spark Shuffle原理、Shuffle操作问题解决和参数调优

    摘要: 1 shuffle原理 1.1 mapreduce的shuffle原理 1.1.1 map task端操作 1.1.2 reduce task端操作 1.2 spark现在的SortShuff ...

  4. MongoDB 安全和访问权限控制

    MongoDB的访问控制能够有效保证数据库的安全,访问控制是指绑定Application监听的IP地址,设置监听端口,使用账户和密码登录 一,访问控制的参数 1,绑定IP地址 mongod 参数:-- ...

  5. 如果你想深刻理解ASP.NET Core请求处理管道,可以试着写一个自定义的Server

    我们在上面对ASP.NET Core默认提供的具有跨平台能力的KestrelServer进行了详细介绍(<聊聊ASP.NET Core默认提供的这个跨平台的服务器——KestrelServer& ...

  6. Oracle基础维护01-常用管理命令总结

    概览: 1.Oracle 内存管理 2.Oracle 数据库启动关闭 3.Oracle 参数文件 4.Oracle 控制文件 5.Oracle redo日志文件 6.Oracle undo表空间管理 ...

  7. 读书笔记--SQL必知必会03--排序检索数据

    3.1 排序数据 子句(clause) SQL语句由子句构成.一个子句通常由一个关键字加上所提供的数据组成. ORDER BY子句可以取一个或多个列的名字,将SELECT语句检索出的数据进行排序. O ...

  8. docker对数据卷进行还原操作

    转载请注明出处 数据卷容器备份数据后,备份数据查看 http://www.cnblogs.com/zhuxiaojie/p/5947138.html   我们可能要把这个备份的数据,还原到另一台的do ...

  9. WebApi系列~StringContent与FormUrlEncodedContent

    回到目录 知识点 本文是一个很另类的文章,在项目中用的比较少,但如果项目中真的出现了这种情况,我们也需要知道如何去解决,对于知识点StringContent和FormUrlEncodedContent ...

  10. 一种开发模式:ajax + ashx + UserControl

    一.ajax+ashx模式的缺点     在web开发过程中,为了提高网站的用户体验,或多或少都会用到ajax技术,甚至有的网站全部采用ajax来实现,大量使用ajax在增强用户体验的同时会带来一些负 ...