@@ -5,81 +5,69 @@ LLVM-libc entrypoints are defined in the entrypoints document. In this document,
55we explain how the entrypoints are implemented. The source layout document
66explains that, within the high level ``src `` directory, there exists one
77directory for every public header file provided by LLVM-libc. The
8- implementations of related group of entrypoints will also live in a directory of
9- their own. This directory will have a name indicative of the related group of
10- entrypoints, and will be under the directory corresponding to the header file of
11- the entrypoints. For example, functions like ``fopen `` and ``fclose `` cannot be
12- tested independent of each other and hence will live in a directory named
13- ``src/stdio/file_operations ``. On the other hand, the implementation of the
14- ``round `` function from ``math.h `` can be tested by itself, so it will live in
15- the directory of its own named ``src/math/round/ ``.
8+ implementations of entrypoints live in the directory for the header they belong
9+ to. Some entrypoints are platform specific, and so their implementations are in
10+ a subdirectory with the name of the platform (e.g. ``stdio/linux/remove.cpp ``).
1611
1712Implementation of entrypoints can span multiple ``.cpp `` and ``.h `` files, but
1813there will be at least one header file with name of the form
19- ``<entrypoint name>.h `` for every entrypoint. This header file is called as the
20- implementation header file. For the ``round `` function, the path to the
21- implementation header file will be ``src/math/round/round.h ``. The rest of this
22- document explains the structure of implementation header files and ``.cpp ``
23- files.
14+ ``<entrypoint name>.h `` for every entrypoint. This header file is called the
15+ implementation header file. For the ``isalpha `` function, the path to the
16+ implementation header file is ``src/ctype/isalpha.h ``.
2417
2518Implementation Header File Structure
2619------------------------------------
2720
28- We will use the ``round `` function from the public ``math .h `` header file as an
29- example. The ``round `` function will be declared in an internal header file
30- ``src/math/round/round .h `` as follows::
21+ We will use the ``isalpha `` function from the public ``ctype .h `` header file as an
22+ example. The ``isalpha `` function will be declared in an internal header file
23+ ``src/ctype/isalpha .h `` as follows::
3124
32- // --- round .h --- //
33- #ifndef LLVM_LIBC_SRC_MATH_ROUND_ROUND_H
34- #define LLVM_LIBC_SRC_MATH_ROUND_ROUND_H
25+ // --- isalpha .h --- //
26+ #ifndef LLVM_LIBC_SRC_CTYPE_ISALPHA_H
27+ #define LLVM_LIBC_SRC_CTYPE_ISALPHA_H
3528
3629 namespace LIBC_NAMESPACE {
3730
38- double round(double );
31+ int isalpha(int c );
3932
4033 } // namespace LIBC_NAMESPACE
4134
42- #endif LLVM_LIBC_SRC_MATH_ROUND_ROUND_H
35+ #endif LLVM_LIBC_SRC_CTYPE_ISALPHA_H
4336
44- Notice that the ``round `` function declaration is nested inside the namespace
45- ``LIBC_NAMESPACE ``. All implementation constructs in LLVM-libc are declared within
46- the namespace ``LIBC_NAMESPACE ``.
37+ Notice that the ``isalpha `` function declaration is nested inside the namespace
38+ ``LIBC_NAMESPACE ``. All implementation constructs in LLVM-libc are declared
39+ within the namespace ``LIBC_NAMESPACE ``.
4740
4841``.cpp `` File Structure
4942-----------------------
5043
51- The implementation can span multiple ``.cpp `` files. However, the signature of
52- the entrypoint function should make use of a special macro. For example, the
53- ``round `` function from ``math.h `` should be defined as follows, say in the file
54- ``src/math/math/round.cpp ``::
44+ The main ``.cpp `` file is named ``<entrypoint name>.cpp `` and is usually in the
45+ same folder as the header. It contains the signature of the entrypoint function,
46+ which must be defined with the ``LLVM_LIBC_FUNCTION `` macro. For example, the
47+ ``isalpha `` function from ``ctype.h `` is defined as follows, in the file
48+ ``src/ctype/isalpha.cpp ``::
5549
56- // --- round .cpp --- //
50+ // --- isalpha .cpp --- //
5751
5852 namespace LIBC_NAMESPACE {
5953
60- double LLVM_LIBC_ENTRYPOINT(round)(double d ) {
54+ LLVM_LIBC_FUNCTION(int, isalpha, (int c) ) {
6155 // ... implementation goes here.
6256 }
6357
6458 } // namespace LIBC_NAMESPACE
6559
66- Notice the use of the macro ``LLVM_LIBC_ENTRYPOINT ``. This macro helps us define
67- an C alias symbol for the C++ implementation. The C alias need not be added by
68- the macro by itself. For example, for ELF targets, the macro is defined as
69- follows::
60+ Notice the use of the macro ``LLVM_LIBC_FUNCTION ``. This macro helps us define
61+ a C alias symbol for the C++ implementation. For example, for a library build,
62+ the macro is defined as follows::
7063
71- #define ENTRYPOINT_SECTION_ATTRIBUTE(name) \
72- __attribute__((section(".llvm.libc.entrypoint."#name)))
73- #define LLVM_LIBC_ENTRYPOINT(name) ENTRYPOINT_SECTION_ATTRIBUTE(name) name
64+ #define LLVM_LIBC_FUNCTION(type, name, arglist)
65+ LLVM_LIBC_FUNCTION_IMPL(type, name, arglist)
66+ #define LLVM_LIBC_FUNCTION_IMPL(type, name, arglist)
67+ LLVM_LIBC_FUNCTION_ATTR decltype(LIBC_NAMESPACE::name)
68+ __##name##_impl__ __asm__(#name);
69+ decltype(LIBC_NAMESPACE::name) name [[gnu::alias(#name)]];
70+ type __##name##_impl__ arglist
7471
75- The macro places the C++ function in a unique section with name
76- ``.llvm.libc.entrypoint.<function name> ``. This allows us to add a C alias using
77- a post build step. For example, for the ``round `` function, one can use
78- ``objcopy `` to add an alias symbol as follows::
79-
80- objcopy --add-symbol round=.llvm.libc.entrypoint.round:0,function round.o
81-
82- NOTE: We use a post build ``objcopy `` step to add an alias instead of using
83- the ``__attribute__((alias)) ``. For C++, this ``alias `` attribute requires
84- mangled names of the referees. Using the post build ``objcopy `` step helps
85- us avoid putting mangled names with ``alias `` attributes.
72+ The LLVM_LIBC_FUNCTION_ATTR macro is normally defined to nothing, but can be
73+ defined by vendors who want to set their own attributes.
0 commit comments