Skip to content

Instantly share code, notes, and snippets.

@ilya-klyuchnikov
Forked from lixen/erlc_compiler.rst
Created January 8, 2021 23:54
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ilya-klyuchnikov/6be9f10fa6fb9a2f214b3f7373b3fe7f to your computer and use it in GitHub Desktop.

Notes on Erlang Compiler

erlc

erts/etc/common/erlc.c

erlc is equivalent to

beam +sbtu +A0 -noinput -mode minimal -boot start_clean -s erl_compile compile_cmdline -extra

erl_compile

lib/stdlib/src/erl_compile.erl

select different passes for different filename extension

compiler(".erl") ->    {compile,         compile};
compiler(".S") ->      {compile,         compile_asm};
compiler(".beam") ->   {compile,         compile_beam};
compiler(".core") ->   {compile,         compile_core};
passes pass function erlc extension format
Standard

parse_module

--------------------------+

transform_module

--------------------------+

makedep

--------------------------+

lint_module

--------------------------+

save_abstract_code

--------------------------+

expand_module

--------------------------+

core_module

epp:parse_file/2

--------------------------------------+

--------------------------------------+

--------------------------------------+

--------------------------------------+

--------------------------------------+

sys_pre_expand:module/2

--------------------------------------+

v3_core:module/2 +

----------------------+

----------------------+

-M

----------------------+

+dpp

----------------------+

-P

----------------------+

+to_pp

----------------------+

+dabstr

----------------------+

+dexp

----------------------+

-E

----------------------+

+to_exp

----------------------+

+dcore

----------------------+

+to_core0

---------------------+

---------------------+

---------------------+

.pp

---------------------+

.P

---------------------+

.P

---------------------+

.abstr

---------------------+

.expand

---------------------+

.E

---------------------+

.E

---------------------+

.core

---------------------+

.core

---------------------+

---------------------+

Makefile

---------------------+

Erlang Form Terms

---------------------+

Erlang Code

---------------------+

Erlang Form Terms

---------------------+

Erlang Form Terms

---------------------+

Erlang Form Terms

---------------------+

Erlang Code

---------------------+

Erlang Form Terms

---------------------+

Core Erlang Code

---------------------+

Core Erlang Code

-------------------------+ parse_transform -------------------------+

-------------------------+

before lint

-------------------------+

-------------------------+

-------------------------+

-------------------------+

add two function module_info/0 module_info/1 to module

-------------------------+

Core

parse_core

--------------------------+

core_lint_module

--------------------------+

core_old_inliner

--------------------------+

core_fold_module

--------------------------+

core_inline_module

--------------------------+

core_fold_after_inlining

--------------------------+

core_transforms

core_scan:string/1, core_parse:parse/1

--------------------------------------+

core_lint:module/2

--------------------------------------+

sys_core_inline:module/2

--------------------------------------+

sys_core_fold:module/2

--------------------------------------+

cerl_inline:core_transform/2

--------------------------------------+

sys_core_fold:module/2

--------------------------------------+

----------------------+

----------------------+

+doldinline

----------------------+

+dcorefold

----------------------+

+inline

----------------------+

----------------------+

+dcopt

----------------------+

to_core

---------------------+

---------------------+

.oldinline

---------------------+

.corefold

---------------------+

.inline

---------------------+

---------------------+

.copt

---------------------+

.core

---------------------+

---------------------+

Core Erlang Code

---------------------+

Core Erlang Code

---------------------+

Core Erlang Code

---------------------+

---------------------+

Core Erlang Code

---------------------+

Core Erlang Code

-------------------------+

-------------------------+

-------------------------+

-------------------------+

-------------------------+

-------------------------+

core_transform

Kernel

core_dsetel_module

--------------------------+

core_lint_module

--------------------------+

save_core_code

--------------------------+

kernel_module

--------------------------+

v3_life

--------------------------+

v3_codegen

sys_core_dsetel:module/2

--------------------------------------+

--------------------------------------+

--------------------------------------+

v3_kernel:module/2 +

--------------------------------------+

v3_life:module/2

--------------------------------------+

v3_codegen:module/2

+dsetel

----------------------+

----------------------+

----------------------+

+dkern

----------------------+

+to_kernel

----------------------+

+dlife

----------------------+

+dcg

.dsetel

---------------------+

---------------------+

---------------------+

.kernel

---------------------+

.kernel

---------------------+

.life

---------------------+

.codegen

Core Erlang Code

---------------------+

---------------------+

---------------------+

Kernel Erlang Code

---------------------+

Kernel Erlang Code

---------------------+

Kernel Erlang Code

---------------------+

Assembly Terms

-------------------------+

-------------------------+

-------------------------+

-------------------------+

annotated with life

-------------------------+

ASM

beam_consult_asm

--------------------------+

beam_a

--------------------------+

beam_block

--------------------------+

beam_except

--------------------------+

beam_bool

--------------------------+

beam_type

--------------------------+

beam_split

--------------------------+

beam_dead

--------------------------+

beam_jump

--------------------------+

beam_peep

--------------------------+

beam_clean

--------------------------+

beam_bsm

--------------------------+

beam_receive

--------------------------+

beam_trim

--------------------------+

beam_flatten

--------------------------+

beam_z

--------------------------+

beam_validator

--------------------------+

beam_asm

--------------------------------------+ beam_a:module/2 --------------------------------------+ beam_block:module/2 --------------------------------------+ beam_except:module/2 --------------------------------------+ beam_bool:module/2 --------------------------------------+ beam_type:module/2 --------------------------------------+ beam_split:module/2 --------------------------------------+ beam_dead:module/2 --------------------------------------+ beam_jump:module/2 --------------------------------------+ beam_peep:module/2 --------------------------------------+ beam_clean:module/2 --------------------------------------+ beam_bsm:module/2 --------------------------------------+ beam_receive:module/2 --------------------------------------+ beam_trim:module/2 --------------------------------------+ beam_flatten:module/2 --------------------------------------+

beam_z:module/2
--------------------------------------+

beam_validator:module/2

--------------------------------------+

beam_asm:module/2

----------------------+

----------------------+

+dblk

----------------------+

+dexcept

----------------------+

+dbool

----------------------+

+dtype

----------------------+

+dsplit

----------------------+

+ddead

----------------------+

+djmp

----------------------+

+dpeep

----------------------+

+dclean

----------------------+

+dbsm

----------------------+

+drecv

----------------------+

+dtrim

----------------------+

----------------------+

+dopt

----------------------+

-S

----------------------+

+to_asm

----------------------+

----------------------+

---------------------+

---------------------+

.block

---------------------+

.except

---------------------+

.bool

---------------------+

.type

---------------------+

.split

---------------------+

.dead

---------------------+

.jump

---------------------+

.peep

---------------------+

.clean

---------------------+

.bsm

---------------------+

.recv

---------------------+

.trim

---------------------+

---------------------+

.optimize

---------------------+

.S

---------------------+

.S

---------------------+

---------------------+

---------------------+

---------------------+

Assembly Terms

---------------------+

Assembly Terms

---------------------+

Assembly Terms

---------------------+

Assembly Terms

---------------------+

Assembly Terms

---------------------+

Assembly Terms

---------------------+

Assembly Terms

---------------------+

Assembly Terms

---------------------+

Assembly Terms

---------------------+

Assembly Terms

---------------------+

Assembly Terms

---------------------+

Assembly Terms

---------------------+

---------------------+

Assembly Terms

---------------------+

Assembly Terms

---------------------+

Assembly Terms

---------------------+

---------------------+

-------------------------+

-------------------------+

-------------------------+

-------------------------+

-------------------------+

-------------------------+

-------------------------+

-------------------------+

-------------------------+

-------------------------+

-------------------------+

-------------------------+

-------------------------+

-------------------------+

-------------------------+

-------------------------+

-------------------------+

Binary

read_beam_file

--------------------------+

native_compile

--------------------------+

save_binary

--------------------------------------+ hipe:compile/4 --------------------------------------+

----------------------+

----------------------+

---------------------+

---------------------+

---------------------+

---------------------+

-------------------------+

-------------------------+

HiPE

To get Icode of Erlang code

{ok,_,Bin}=compile:file(FileName,[binary]).
{beam_file,_,_,_,_,BeamCode}=beam_disasm:file(Bin).
[{{M,F,A},Icode}|_]=hipe_beam_to_icode:module(BeamCode,[]).

Icode

RTL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment