Clause
← All documentation

Builtins

Functions the runtime implements in C++: erlang BIFs and functions of other modules whose OTP counterparts are BIFs, NIFs or need the runtime. Registered by module, function and arity; reached directly, by dynamic calls and as funs. Sources live in runtime/src/builtins/; observable differences from OTP are in differences.

Which builtins exist

How calls reach them

SourcePath
abs(X), X + Y, display, halt, errorInline code or a dedicated service
Catalog builtins without inline service (A -- B, ...)Entered like a function: CLAUSE_builtin_frame_v1, arguments in registers
Direct builtins (never trap, wait or touch another process)CLAUSE_builtin_call_v1 returns the result
Quick builtins (length, binary_to_list, list_to_binary, iolist_to_binary)Bounded fast path (256 cells, 4096 result bytes); else enter the frame
A ++ BShort proper A copied inline; else entered
fun abs/1, fun erlang:'+'/2External fun erlang:F/A bound to the builtin
M:F(Args), apply/3, runtime fun M:F/AModule export first, then builtin

Portions

Builtins whose work grows with a list or binary run in bounded portions, like OTP's trapping BIFs.

Typed builtins

Registration

Source layout

Erlang module

External term format

erlang/term_encoding.cpp, term_decoding.cpp; codec shared with the compiler (external_term.hpp), explicit stacks.

Time

erlang/time.cpp.

System information

erlang/system.cpp, erlang/system_flags.cpp.

Hashing

erlang/hashing.cpp, erlang/md5.cpp.

Code loading

erlang/code.cpp. Every module is linked and stays loaded; dynamic code is deferred.

Lists module

builtins/lists/. OTP's lists is Erlang, so builtins raise what its clauses raise.

Math module

builtins/math.cpp.

Binary module

builtins/binary/; replace/3,4 and list_to_bin/1 are Erlang.

Maps module

builtins/maps.cpp, builtins/os.cpp.

Collections

builtins/collections/: every documented function of ordsets, orddict, proplists, dict, sets, gb_trees, gb_sets, queue, array.

Sets of sets

Graph

Digraph

ETS

builtins/ets/ over the runtime's tables (runtime).

Dets

dets.erl over clause_dets (builtins/dets/): OTP's version 9 file format byte for byte, same file orders.

Encodings

builtins/encoding/.

POSIX messages

builtins/erl_posix_msg.cpp, library/stdlib/erl_posix_msg.erl.

Network names

builtins/net/, library/kernel/clause_net.erl, library/kernel/net.erl.

Filename

builtins/filename/.

Filelib

builtins/filelib.cpp, library/stdlib/filelib.erl, library/stdlib/clause_filelib.erl.

Argparse

builtins/argparse/, library/stdlib/argparse.erl, library/stdlib/clause_argparse.erl.

Qlc pt

builtins/qlc_pt/, library/stdlib/qlc_pt.erl, library/stdlib/clause_qlc_pt.erl: the qlc parse transform at run time, for qlc:string_to_handle/1,2,3 and qlc:q/1,2 in evaluated code, and at compile time for modules including qlc.hrl, in Clause's transform host.

Ms transform

builtins/front_end/ms_transform.hpp, ms_head.cpp, ms_guards.cpp, ms_transform.cpp, library/stdlib/ms_transform.erl, library/stdlib/clause_ms_transform.erl: the translation of literal funs given to ets:fun2ms/1 and dbg:fun2ms/1 into abstract match specifications, in C++ so qlc_pt calls it in place.

Qlc

library/stdlib/qlc.erl: every function in Erlang, since evaluation calls the query's funs. Written from OTP's documentation; the algorithms are OTP's where answers, their order or info/1,2 show them.

String

builtins/string/: every string function.

URI string

builtins/uri/.

Random numbers

builtins/rand/.

Calendar

builtins/calendar.cpp.

Timer

builtins/timer.cpp.

Persistent terms

builtins/persistent_term.cpp over the runtime's table (runtime).

Unicode module

builtins/unicode/: every function is a builtin.

Code module

builtins/code.cpp. No code_server process; answers describe the linked program.

Io module

builtins/terminal.cpp, library/stdlib/clause_io.erl.

Os module

builtins/os.cpp, library/kernel/os.erl.

Init module

builtins/init.cpp, library/erts/init.erl. A started system without a boot script.

Atomics and counters

builtins/atomics.cpp.

Erts internal

builtins/erts_internal.cpp, library/erts/erts_internal.erl: the functions stdlib and kernel call.

Unicode util

unicode_util.erl over clause_unicode builtins and generated tables (unicode/tables.cpp).

io_lib

io_lib.erl, builtins/io_lib/. OTP's deep character lists built in a C++ arena; io:format uses the same engine.

Compiler tables

builtins/front_end/.

Erl scan

erl_scan.erl over clause_erl_scan:scan/6 (front_end/scanner*.cpp), its own scanner following OTP's scan1/5.

Erl parse

erl_parse.erl over clause_erl_parse:parse/1 (front_end/parser*.cpp), a recursive-descent parser of OTP's grammar.

Erl eval

erl_eval.erl interprets the abstract format in Erlang; clause_erl_eval builtins handle bit syntax segments (front_end/erl_eval.cpp) and pattern matching (front_end/eval_match.cpp), and erl_eval:partial_eval/1 and is_constant_expr/1 are builtins too.

Module interpreter

builtins/interpreted.cpp is the runtime half of clause_interp: the code server's registry of interpreted modules (modules/interpreted_modules.cpp), with each module's file and exports.

Compile

builtins/compile_messages.cpp quotes sources for the reports of compile: clause_compile:quote_source(Source, Encoding, Location) returns the line of Location from the source binary (latin1, or UTF-8 up to its first invalid character) as "% Line| Text ", the marker line "% | ^ " when the location has a column, and an empty line, as OTP's sys_messages prints them, with OTP's quirks: tabs before the column stay tabs and a column past the end of the line widens the marker. <<>> when the source has no such line. compile.erl reads the file and its coding comment and formats the message.

Erl syntax

builtins/syntax/ implements erl_syntax over OTP's node representation. The syntax.hpp tables drive most of it:

Accessors with several erl_parse forms (record_expr_type/1, attribute_arguments/1, ...), constructors that normalize their arguments (clause/3's guards, list/2, cons/2, ...), list skeletons, comments and attributes (nodes.cpp, attrs.cpp), abstract/1, concrete/1, is_literal/1 and meta/1 (literals.cpp) are written out.

concrete/1 of a binary reverts each field and builds it as erl_parse:normalise/1 does. abstract/1 takes a list for a string when Clause's io_lib:printable_list/1 accepts it.

Prettypr

builtins/prettypr/ is the layout chooser of prettypr: prettypr:best/3 is OTP's rewrite/2, John Hughes' strict context-passing pretty printer with OTP's paragraphs, empty documents, floats and null texts, rule for rule, so it returns OTP's very layout documents, joined strings included ([W1 + W2 | [Chars1 | Chars2]]).

Erl syntax lib

builtins/syntax_lib/ has the functions of erl_syntax_lib that take no funs. They are written over builtins/syntax/'s C++ accessors and constructors:

Comments

builtins/comments/ has erl_comment_scan's scanning and erl_recomment:

Epp dodger

builtins/epp_dodger/ has the rewriting of epp_dodger, as clause_epp_dodger's builtins:

Merl

builtins/merl/ has the template functions of merl:

Erl lint

erl_lint's checks are builtins (front_end/lint*.cpp) written after OTP's erl_lint clause for clause; erl_lint.erl keeps what calls funs or other modules.

Erl pp

erl_pp's printing is a builtin (front_end/pp*.cpp), clause_erl_pp:print/2, ported from the two passes of the earlier Erlang module; erl_pp.erl keeps the hooks and legalize_vars/1.

Epp

epp's -if and -elif conditions are evaluated by a builtin, clause_epp:eval_if/1 (front_end/epp_eval.cpp), so a program that preprocesses files does not link erl_eval.

Erl expand records

erl_expand_records:module/2 is a builtin (front_end/expand_records.cpp): the expansion walks forms in C++ instead of a large Erlang clause dispatch, so programs that call it compile no extra code.

Zlib module

builtins/zlib/streams.cpp, library/erts/zlib.erl.

Zstd module

builtins/zstd/contexts.cpp, library/stdlib/zstd.erl.

ASN.1 codec

builtins/asn1/ in clause_runtime, library/asn1/clause_asn1.erl (ASN.1).

Public key

builtins/public_key/ in clause_runtime, library/public_key/clause_public_key.erl (public_key): the record walks and tables of OTP's pubkey_translation and pubkey_cert_records, pubkey_pem's text and pubkey_pbe's tables, which clause_public_key_codec calls.

Ssl

builtins/ssl/ in clause_runtime (ssl): the ssl module's cipher suite table and ssl_cipher_format's name walks, as catalog builtins of ssl itself.

Ssl engine

builtins/ssl_engine/ in the separate clause_runtime_ssl archive, library/ssl/clause_ssl_engine.erl (ssl): one TLS connection over OpenSSL's libssl and memory buffers. Each step takes the bytes received from the transport and returns the bytes to send to it, so the connection process does all the I/O.

Crypto module

builtins/crypto/ in the separate clause_runtime_crypto archive, library/crypto/crypto.erl and clause_crypto.erl.

Re module

builtins/re/, re.erl over bundled PCRE2 (PCRE2).

Prim eval

process/receive.cpp, library/erts/prim_eval.erl.