CoreErlang:Erlang的Core中间表示


随着erlang的不断发展,它的语法越来越复杂,不便于诸如分析器,调试器此类程序在源码层次直接进行解析,而CORE Erlang旨在为Erlang提供一个人类可读可改的中间表示(Intermediate representation),这样就能方便面向程序源码的工具的开发。 千言万语不如代码一句。这里我们通过一个例子来直观的了解究竟什么是CORE Erlang。在helloworld.erl中输入如下代码:

-module (helloworld).
-export([simple/0, complicated/2]).

simple() ->
 atom_to_list(hello_world).

complicated(Type,List) ->
 List = [{Type,Elem} || Elem<-List,is_atom(Elem)],
 case Type of
  bool when bool =:= true -> R1 = bool_true;
  bool -> R1 = bool_false;
  integer -> R1 = 1+2*3/4
 end.

Erlang/OTP R10以及之后发行的版本在编译helloworld.erl时传入to_core标志: c(helloworld,to_core). 将会生成CORE Erlang文件helloworld.core:

module 'helloworld' ['complicated'/2,
           'module_info'/0,
           'module_info'/1,
           'simple'/0]
    attributes [%% Line 1
      'file' =
          %% Line 1
          [{[104|[101|[108|[108|[111|[119|[111|[114|[108|[100|[46|[101|[114|[108]]]]]]]]]]]]]],1}]]
'simple'/0 =
    %% Line 4
    fun () ->
   %% Line 5
   [104|[101|[108|[108|[111|[95|[119|[111|[114|[108|[100]]]]]]]]]]]

可以看到,模块头部信息和helloworld:simple()函数在CORE Erlang中还是很清楚的,但是对于helloworld:complicated(Type,List)它就完全背离了自己的初衷:

'complicated'/2 =
    %% Line 7
    fun (_@c1,_@c0) ->
   let <_@c6> =
       letrec
      'lc$^0'/1 =
          %% Line 8
          fun (_@c4) ->
         case _@c4 of
           <[Elem|_@c3]>
               when call 'erlang':'is_atom'
                (Elem) ->
               let <_@c5> =
              apply 'lc$^0'/1
                  (_@c3)
               in  ( [{_@c1,Elem}|_@c5]
                -| ['compiler_generated'] )
           ( <[Elem|_@c3]> when 'true' ->
            apply 'lc$^0'/1
                (_@c3)
             -| ['compiler_generated'] )
           <[]> when 'true' ->
               []
           ( <_@c4> when 'true' ->
            ( primop 'match_fail'
                  ({'function_clause',_@c4})
              -| [{'function_name',{'lc$^0',1}}] )
             -| ['compiler_generated'] )
         end
       in  %% Line 8
      apply 'lc$^0'/1
          (_@c0)
   in  %% Line 8
       case _@c6 of
         <_@c16>
        when call 'erlang':'=:='
         (_@c6,
          _@c0) ->
        %% Line 9
        case _@c1 of
          %% Line 11
          <'bool'> when 'true' ->
         'bool_false'
          %% Line 12
          <'integer'> when 'true' ->
         2.50000000000000000000e+00
          ( <_@c13> when 'true' ->
           primop 'match_fail'
               ({'case_clause',_@c13})
            -| ['compiler_generated'] )
        end
         ( <_@c7> when 'true' ->
          primop 'match_fail'
         ({'badmatch',_@c7})
      -| ['compiler_generated'] )
       end
'module_info'/0 =
    fun () ->
   call 'erlang':'get_module_info'
       ('helloworld')
'module_info'/1 =
    fun (_@c0) ->
   call 'erlang':'get_module_info'
       ('helloworld', _@c0)
end

不过话又说回来,CORE Erlang的受众终究是代码分析器,调试器之类的工具,人类可读可改更像是一个崇高追求,如果我们观察会发现CORE Erlang的中间表示的确降低了语法的解析难度,它将源码的各种语法用let in,call ,case of,fun等几个简单的结构来表示,从这点来看CORE Erlang算是到达了它的初衷。

[+] core erlang project index : http://www.it.uu.se/research/group/hipe/cerl/`

优质内容筛选与推荐>>
1、Java学习笔记-枚举类
2、深入探索C语言之结构体(struct)
3、windows使用命令行,提高效率
4、tar等
5、Monkey如何使用


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号