Erlang 编译器是如何实现模式匹配的? [英] How does the Erlang compiler implement pattern matching?

查看:22
本文介绍了Erlang 编译器是如何实现模式匹配的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道模式匹配通常是如何实现的.例如,在 Erlang 中,您认为它是在字节码级别实现的(它有一个字节码以便它有效地完成)还是由编译器生成为一系列指令(一系列字节码)?

I am wondering how pattern matching is usually implemented. for example in Erlang do you think its implemented at the byte-code level( there's a byte-code for it so that its done efficiently) or is it generated as a series of instructions (series of byte-codes) by the compiler?

这是一个非常有用的东西,我只需要将它放入我正在构建的玩具语言中.

It is such a useful thing that I just have to put it into a toy language I am building.

推荐答案

你可以看看如果编译一些代码会发生什么

You can see what happen if compile some code

-module(match).
-export([match/1]).
match(X) -> {a,Y} = X.

当你想看看看起来像 core

> c(match, to_core).

$ erlc +to_core match.erl

结果是

module 'match' ['match'/1,
                'module_info'/0,
                'module_info'/1]
    attributes []
'match'/1 =
    %% Line 3
    fun (_cor0) ->
        case _cor0 of
          <{'a',Y}> when 'true' ->
              _cor0
          ( <_cor1> when 'true' ->
                primop 'match_fail'
                    ({'badmatch',_cor1})
            -| ['compiler_generated'] )
        end
'module_info'/0 =
    fun () ->
        call 'erlang':'get_module_info'
            ('match')
'module_info'/1 =
    fun (_cor0) ->
        call 'erlang':'get_module_info'
            ('match', _cor0)

如果您想查看梁的 asm 代码,您可以这样做

If you want see asm code of beam you can do

> c(match, 'S').

$ erlc -S match.erl

结果

{module, match}.  %% version = 0

{exports, [{match,1},{module_info,0},{module_info,1}]}.

{attributes, []}.

{labels, 8}.


{function, match, 1, 2}.
  {label,1}.
    {func_info,{atom,match},{atom,match},1}.
  {label,2}.
    {test,is_tuple,{f,3},[{x,0}]}.
    {test,test_arity,{f,3},[{x,0},2]}.
    {get_tuple_element,{x,0},0,{x,1}}.
    {test,is_eq_exact,{f,3},[{x,1},{atom,a}]}.
    return.
  {label,3}.
    {badmatch,{x,0}}.


{function, module_info, 0, 5}.
  {label,4}.
    {func_info,{atom,match},{atom,module_info},0}.
  {label,5}.
    {move,{atom,match},{x,0}}.
    {call_ext_only,1,{extfunc,erlang,get_module_info,1}}.


{function, module_info, 1, 7}.
  {label,6}.
    {func_info,{atom,match},{atom,module_info},1}.
  {label,7}.
    {move,{x,0},{x,1}}.
    {move,{atom,match},{x,0}}.
    {call_ext_only,2,{extfunc,erlang,get_module_info,2}}.

如你所见 {test,is_tuple,..., {test,test_arity,..., {get_tuple_element,...code> 和 {test,is_eq_exact,... 是如何在 beam 中执行匹配并直接转换为 beam 的字节码的说明.

As you can see {test,is_tuple,..., {test,test_arity,..., {get_tuple_element,... and {test,is_eq_exact,... are instruction how this match is performed in beam and it's transformed directly to byte-code of beam.

Erlang 编译器是在 Erlang 本身中实现的,您可以在 编译 模块和依赖模块中的详细信息.

Erlang compiler is implemented in Erlang itself and you can look at each phase of compilation in source code of compile module and details in depend modules.

这篇关于Erlang 编译器是如何实现模式匹配的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆