Skip to content

Commit 0aa7ea4

Browse files
committed
[libc][darwin] Add OSUtil for darwin arm64 target so that unit tests can be run.
Currently unit tests cannot be run on macOS due to missing OSUtil. Reviewed By: michaelrj Differential Revision: https://reviews.llvm.org/D151377
1 parent 0bda541 commit 0aa7ea4

File tree

9 files changed

+224
-1
lines changed

9 files changed

+224
-1
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_ARCHITECTURE})
2+
return()
3+
endif()
4+
5+
add_subdirectory(${LIBC_TARGET_ARCHITECTURE})
6+
7+
add_header_library(
8+
darwin_util
9+
HDRS
10+
io.h
11+
quick_exit.h
12+
syscall.h
13+
DEPENDS
14+
.${LIBC_TARGET_ARCHITECTURE}.darwin_util
15+
libc.src.__support.common
16+
libc.src.__support.CPP.string_view
17+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
add_header_library(
2+
darwin_util
3+
HDRS
4+
syscall.h
5+
DEPENDS
6+
libc.src.__support.common
7+
)
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
//===------ inline implementation of Darwin arm64 syscalls --------* C++ *-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_SUPPORT_OSUTIL_DARWIN_ARM_SYSCALL_H
10+
#define LLVM_LIBC_SRC_SUPPORT_OSUTIL_DARWIN_ARM_SYSCALL_H
11+
12+
#include "src/__support/common.h"
13+
14+
#define REGISTER_DECL_0 \
15+
register long x16 __asm__("x16") = number; \
16+
register long x0 __asm__("x0");
17+
#define REGISTER_DECL_1 \
18+
register long x16 __asm__("x16") = number; \
19+
register long x0 __asm__("x0") = arg1;
20+
#define REGISTER_DECL_2 \
21+
REGISTER_DECL_1 \
22+
register long x1 __asm__("x1") = arg2;
23+
#define REGISTER_DECL_3 \
24+
REGISTER_DECL_2 \
25+
register long x2 __asm__("x2") = arg3;
26+
#define REGISTER_DECL_4 \
27+
REGISTER_DECL_3 \
28+
register long x3 __asm__("x3") = arg4;
29+
#define REGISTER_DECL_5 \
30+
REGISTER_DECL_4 \
31+
register long x4 __asm__("x4") = arg5;
32+
#define REGISTER_DECL_6 \
33+
REGISTER_DECL_5 \
34+
register long x5 __asm__("x5") = arg6;
35+
36+
#define REGISTER_CONSTRAINT_0 "r"(x16)
37+
#define REGISTER_CONSTRAINT_1 REGISTER_CONSTRAINT_0, "r"(x0)
38+
#define REGISTER_CONSTRAINT_2 REGISTER_CONSTRAINT_1, "r"(x1)
39+
#define REGISTER_CONSTRAINT_3 REGISTER_CONSTRAINT_2, "r"(x2)
40+
#define REGISTER_CONSTRAINT_4 REGISTER_CONSTRAINT_3, "r"(x3)
41+
#define REGISTER_CONSTRAINT_5 REGISTER_CONSTRAINT_4, "r"(x4)
42+
#define REGISTER_CONSTRAINT_6 REGISTER_CONSTRAINT_5, "r"(x5)
43+
44+
#define SYSCALL_INSTR(input_constraint) \
45+
LIBC_INLINE_ASM("svc 0x80" : "=r"(x0) : input_constraint : "memory", "cc")
46+
47+
namespace __llvm_libc {
48+
49+
LIBC_INLINE long syscall_impl(long number) {
50+
REGISTER_DECL_0;
51+
SYSCALL_INSTR(REGISTER_CONSTRAINT_0);
52+
return x0;
53+
}
54+
55+
LIBC_INLINE long syscall_impl(long number, long arg1) {
56+
REGISTER_DECL_1;
57+
SYSCALL_INSTR(REGISTER_CONSTRAINT_1);
58+
return x0;
59+
}
60+
61+
LIBC_INLINE long syscall_impl(long number, long arg1, long arg2) {
62+
REGISTER_DECL_2;
63+
SYSCALL_INSTR(REGISTER_CONSTRAINT_2);
64+
return x0;
65+
}
66+
67+
LIBC_INLINE long syscall_impl(long number, long arg1, long arg2, long arg3) {
68+
REGISTER_DECL_3;
69+
SYSCALL_INSTR(REGISTER_CONSTRAINT_3);
70+
return x0;
71+
}
72+
73+
LIBC_INLINE long syscall_impl(long number, long arg1, long arg2, long arg3,
74+
long arg4) {
75+
REGISTER_DECL_4;
76+
SYSCALL_INSTR(REGISTER_CONSTRAINT_4);
77+
return x0;
78+
}
79+
80+
LIBC_INLINE long syscall_impl(long number, long arg1, long arg2, long arg3,
81+
long arg4, long arg5) {
82+
REGISTER_DECL_5;
83+
SYSCALL_INSTR(REGISTER_CONSTRAINT_5);
84+
return x0;
85+
}
86+
87+
LIBC_INLINE long syscall_impl(long number, long arg1, long arg2, long arg3,
88+
long arg4, long arg5, long arg6) {
89+
REGISTER_DECL_6;
90+
SYSCALL_INSTR(REGISTER_CONSTRAINT_6);
91+
return x0;
92+
}
93+
94+
} // namespace __llvm_libc
95+
96+
#undef REGISTER_DECL_0
97+
#undef REGISTER_DECL_1
98+
#undef REGISTER_DECL_2
99+
#undef REGISTER_DECL_3
100+
#undef REGISTER_DECL_4
101+
#undef REGISTER_DECL_5
102+
#undef REGISTER_DECL_6
103+
104+
#undef REGISTER_CONSTRAINT_0
105+
#undef REGISTER_CONSTRAINT_1
106+
#undef REGISTER_CONSTRAINT_2
107+
#undef REGISTER_CONSTRAINT_3
108+
#undef REGISTER_CONSTRAINT_4
109+
#undef REGISTER_CONSTRAINT_5
110+
#undef REGISTER_CONSTRAINT_6
111+
112+
#endif // LLVM_LIBC_SRC_SUPPORT_OSUTIL_DARWIN_ARM_SYSCALL_H

libc/src/__support/OSUtil/darwin/io.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===------------- Darwin implementation of IO utils ------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_SUPPORT_OSUTIL_DARWIN_IO_H
10+
#define LLVM_LIBC_SRC_SUPPORT_OSUTIL_DARWIN_IO_H
11+
12+
#include "src/__support/CPP/string_view.h"
13+
#include "syscall.h" // For internal syscall function.
14+
15+
namespace __llvm_libc {
16+
17+
LIBC_INLINE void write_to_stderr(cpp::string_view msg) {
18+
__llvm_libc::syscall_impl(4 /*SYS_write*/, 2 /* stderr */,
19+
reinterpret_cast<long>(msg.data()), msg.size());
20+
}
21+
22+
} // namespace __llvm_libc
23+
24+
#endif // LLVM_LIBC_SRC_SUPPORT_OSUTIL_DARWIN_IO_H
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===--------- Darwin implementation of a quick exit function ---*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_SUPPORT_OSUTIL_DARWIN_QUICK_EXIT_H
10+
#define LLVM_LIBC_SRC_SUPPORT_OSUTIL_DARWIN_QUICK_EXIT_H
11+
12+
#include "syscall.h" // For internal syscall function.
13+
14+
#include "src/__support/common.h"
15+
16+
namespace __llvm_libc {
17+
18+
LIBC_INLINE void quick_exit(int status) {
19+
for (;;) {
20+
__llvm_libc::syscall_impl(1 /* SYS_exit */, status);
21+
}
22+
}
23+
24+
} // namespace __llvm_libc
25+
26+
#endif // LLVM_LIBC_SRC_SUPPORT_OSUTIL_DARWIN_QUICK_EXIT_H
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===---------------------- Darwin syscalls ---------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_SUPPORT_OSUTIL_DARWIN_SYSCALL_H
10+
#define LLVM_LIBC_SRC_SUPPORT_OSUTIL_DARWIN_SYSCALL_H
11+
12+
#include "src/__support/common.h"
13+
#include "src/__support/macros/properties/architectures.h"
14+
15+
#ifdef LIBC_TARGET_ARCH_IS_ANY_ARM
16+
#include "arm/syscall.h"
17+
#else
18+
#error "Unsupported architecture"
19+
#endif
20+
21+
namespace __llvm_libc {
22+
23+
template <typename... Ts>
24+
LIBC_INLINE long syscall_impl(long __number, Ts... ts) {
25+
static_assert(sizeof...(Ts) <= 6, "Too many arguments for syscall");
26+
return syscall_impl(__number, (long)ts...);
27+
}
28+
29+
} // namespace __llvm_libc
30+
31+
#endif // LLVM_LIBC_SRC_SUPPORT_OSUTIL_DARWIN_SYSCALL_H

libc/src/__support/OSUtil/io.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
#if defined(LIBC_TARGET_ARCH_IS_GPU)
1515
#include "gpu/io.h"
16+
#elif defined(__APPLE__)
17+
#include "darwin/io.h"
1618
#elif defined(__unix__)
1719
#include "linux/io.h"
1820
#elif defined(__Fuchsia__)

libc/src/__support/OSUtil/quick_exit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
#if defined(LIBC_TARGET_ARCH_IS_GPU)
1515
#include "gpu/quick_exit.h"
16+
#elif defined(__APPLE__)
17+
#include "darwin/quick_exit.h"
1618
#elif defined(__unix__)
1719
#include "linux/quick_exit.h"
1820
#endif

libc/src/__support/OSUtil/syscall.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#ifndef LLVM_LIBC_SRC_SUPPORT_OSUTIL_SYSCALL_H
1010
#define LLVM_LIBC_SRC_SUPPORT_OSUTIL_SYSCALL_H
1111

12-
#ifdef __unix__
12+
#ifdef __APPLE__
13+
#include "darwin/syscall.h"
14+
#elif defined(__unix__)
1315
#include "linux/syscall.h"
1416
#endif
1517

0 commit comments

Comments
 (0)