• Core Erlang: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/`

  • 相关阅读:
    webstorm和git安装后,terminal输入git命令,提示'git' 不是内部或外部命令
    Vue中beforeRouterEnter和beforeRouteLeave的应用
    对象数组,对比多个数组的相同子元素并筛选
    应用canvas绘制动态时钟--每秒自动动态更新时间
    opacity 与rgba区别
    js对象的合并
    小程序wx.navigateTo和wx.redirectTo 都无效
    点赞博客园
    英语学习方法
    JAVA编程思想——分析阅读
  • 原文地址:https://www.cnblogs.com/ysherlock/p/7822280.html
Copyright © 2020-2023  润新知