This answer, for clang, just show how I find implementation of a builtin function.
I'm interested in implementation of std::atomic<T>
. If T
is not a trivial type, clang use a lock to guard its atomicity. Look this answer first, I find a builtin function named __c11_atomic_store
. The question is, how this builtin function implemented in clang?
Searching Builtin
in clang codebase, find in clang/Basic/Builtins.def
:
// Some of our atomics builtins are handled by AtomicExpr rather than
// as normal builtin CallExprs. This macro is used for such builtins.
#ifndef ATOMIC_BUILTIN
#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) BUILTIN(ID, TYPE, ATTRS)
#endif
// C11 _Atomic operations for <stdatomic.h>.
ATOMIC_BUILTIN(__c11_atomic_init, "v.", "t")
ATOMIC_BUILTIN(__c11_atomic_load, "v.", "t")
ATOMIC_BUILTIN(__c11_atomic_store, "v.", "t")
ATOMIC_BUILTIN(__c11_atomic_exchange, "v.", "t")
...
The keyword are AtomicExpr
and CallExpr
. Then I check every caller of AtomicExpr
's constructor, but doesn't find any useful information. So I guess, maybe in parse phase, if parser match an builtin function calling, it will construct an CallExpr
to AST with builtin flag. In code generate phase, it will emit the implementation.
Check CodeGen
, I find the answer in lib/CodeGen/CGBuiltin.cpp
and CodeGen/CGAtomic.cpp
.
You can check CodeGenFunction::EmitVAArg
, I holp that would be useful for you.