@@ -5,81 +5,69 @@ LLVM-libc entrypoints are defined in the entrypoints document. In this document,
5
5
we explain how the entrypoints are implemented. The source layout document
6
6
explains that, within the high level ``src `` directory, there exists one
7
7
directory 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 ``).
16
11
17
12
Implementation of entrypoints can span multiple ``.cpp `` and ``.h `` files, but
18
13
there 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 ``.
24
17
25
18
Implementation Header File Structure
26
19
------------------------------------
27
20
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::
31
24
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
35
28
36
29
namespace LIBC_NAMESPACE {
37
30
38
- double round(double );
31
+ int isalpha(int c );
39
32
40
33
} // namespace LIBC_NAMESPACE
41
34
42
- #endif LLVM_LIBC_SRC_MATH_ROUND_ROUND_H
35
+ #endif LLVM_LIBC_SRC_CTYPE_ISALPHA_H
43
36
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 ``.
47
40
48
41
``.cpp `` File Structure
49
42
-----------------------
50
43
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 ``::
55
49
56
- // --- round .cpp --- //
50
+ // --- isalpha .cpp --- //
57
51
58
52
namespace LIBC_NAMESPACE {
59
53
60
- double LLVM_LIBC_ENTRYPOINT(round)(double d ) {
54
+ LLVM_LIBC_FUNCTION(int, isalpha, (int c) ) {
61
55
// ... implementation goes here.
62
56
}
63
57
64
58
} // namespace LIBC_NAMESPACE
65
59
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::
70
63
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
74
71
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