Perform some code cleanup

This commit is contained in:
Justine Tunney 2023-05-28 19:42:00 -07:00
parent 992a4638ae
commit 72f8bd10b7
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
8 changed files with 55 additions and 265 deletions

View file

@ -1,6 +1,7 @@
#ifndef COSMOPOLITAN_LIBC_LOG_LIBFATAL_INTERNAL_H_
#define COSMOPOLITAN_LIBC_LOG_LIBFATAL_INTERNAL_H_
#include "libc/calls/calls.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/macros.internal.h"
#include "libc/nt/runtime.h"
@ -10,191 +11,6 @@ COSMOPOLITAN_C_START_
#define __ToUpper(c) ((c) >= 'a' && (c) <= 'z' ? (c) - 'a' + 'A' : (c))
forceinline long __sysv_exit(long rc) {
long ax;
#if defined(__MNO_RED_ZONE__) && defined(__GNUC__) && !defined(__STRICT_ANSI__)
asm volatile("call\t__syscall__"
: "=a"(ax)
: "0"(__NR_exit_group), "D"(rc)
: "memory", "cc");
#elif defined(__aarch64__)
register long r0 asm("x0") = rc;
register long r8 asm("x8") = __NR_exit_group & 0x7ff;
register long r16 asm("x16") = __NR_exit_group & 0x7ff;
register long res_x0 asm("x0");
asm volatile("svc\t0" : "=r"(res_x0) : "r"(r0), "r"(r8), "r"(r16) : "memory");
ax = res_x0;
#else
ax = syscall(__NR_exit_group & 0x7ff, rc);
#endif
return ax;
}
forceinline int __sysv_close(long fd) {
long ax;
#if defined(__MNO_RED_ZONE__) && defined(__GNUC__) && !defined(__STRICT_ANSI__)
asm volatile("call\t__syscall__"
: "=a"(ax)
: "0"(__NR_close), "D"(fd)
: "rdx", "memory", "cc");
#elif defined(__aarch64__)
register long r0 asm("x0") = fd;
register long r8 asm("x8") = __NR_close & 0x7ff;
register long r16 asm("x16") = __NR_close & 0x7ff;
register long res_x0 asm("x0");
asm volatile("mov\tx8,%1\n\t"
"svc\t0"
: "=r"(res_x0)
: "r"(r0), "r"(r8), "r"(r16)
: "memory");
ax = res_x0;
#else
ax = syscall(__NR_close & 0x7ff, fd);
#endif
return ax;
}
forceinline int __sysv_open(const char *path, long flags, long mode) {
long ax, dx;
#if defined(__MNO_RED_ZONE__) && defined(__GNUC__) && !defined(__STRICT_ANSI__)
asm volatile("call\t__syscall__"
: "=a"(ax), "=d"(dx)
: "0"(__NR_open), "D"(path), "S"(flags), "1"(mode)
: "memory", "cc");
#elif defined(__aarch64__)
register long r0 asm("x0") = -100;
register long r1 asm("x1") = (long)path;
register long r2 asm("x2") = (long)flags;
register long r3 asm("x3") = (long)mode;
register long r8 asm("x8") = (long)__NR_open & 0x7ff;
register long r16 asm("x16") = (long)__NR_open & 0x7ff;
register long res_x0 asm("x0");
asm volatile("svc\t0"
: "=r"(res_x0)
: "r"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r8), "r"(r16)
: "memory");
ax = res_x0;
#else
ax = syscall(__NR_open & 0x7ff, path, flags, mode);
#endif
return ax;
}
forceinline long __sysv_read(long fd, void *data, unsigned long size) {
long ax, dx;
#if defined(__MNO_RED_ZONE__) && defined(__GNUC__) && !defined(__STRICT_ANSI__)
asm volatile("call\t__syscall__"
: "=a"(ax), "=d"(dx)
: "0"(__NR_read), "D"(fd), "S"(data), "1"(size)
: "memory", "cc");
#elif defined(__aarch64__)
register long r0 asm("x0") = (long)fd;
register long r1 asm("x1") = (long)data;
register long r2 asm("x2") = (long)size;
register long r8 asm("x8") = (long)__NR_read & 0x7ff;
register long r16 asm("x16") = (long)__NR_read & 0x7ff;
register long res_x0 asm("x0");
asm volatile("svc\t0"
: "=r"(res_x0)
: "r"(r0), "r"(r1), "r"(r2), "r"(r8), "r"(r16)
: "memory");
ax = res_x0;
#else
ax = syscall(__NR_read & 0x7ff, fd, data, size);
#endif
return ax;
}
forceinline long __sysv_write(long fd, const void *data, unsigned long size) {
long ax, dx;
#if defined(__MNO_RED_ZONE__) && defined(__GNUC__) && !defined(__STRICT_ANSI__)
asm volatile("call\t__syscall__"
: "=a"(ax), "=d"(dx)
: "0"(__NR_write), "D"(fd), "S"(data), "1"(size)
: "memory", "cc");
#elif defined(__aarch64__)
register long r0 asm("x0") = (long)fd;
register long r1 asm("x1") = (long)data;
register long r2 asm("x2") = (long)size;
register long r8 asm("x8") = (long)__NR_write & 0x7ff;
register long r16 asm("x16") = (long)__NR_write & 0x7ff;
register long res_x0 asm("x0");
asm volatile("svc\t0"
: "=r"(res_x0)
: "i"(64), "r"(r0), "r"(r1), "r"(r2), "r"(r8), "r"(r16)
: "memory");
ax = res_x0;
#else
ax = syscall(__NR_write & 0x7ff, fd, data, size);
#endif
return ax;
}
forceinline long __sysv_mprotect(void *addr, size_t size, long prot) {
long ax, dx;
#if defined(__MNO_RED_ZONE__) && defined(__GNUC__) && !defined(__STRICT_ANSI__)
asm volatile("call\t__syscall__"
: "=a"(ax), "=d"(dx)
: "0"(__NR_mprotect), "D"(addr), "S"(size), "1"(prot)
: "memory", "cc");
#elif defined(__aarch64__)
register long r0 asm("x0") = (long)addr;
register long r1 asm("x1") = (long)size;
register long r2 asm("x2") = (long)prot;
register long r8 asm("x8") = (long)__NR_mprotect & 0x7ff;
register long r16 asm("x16") = (long)__NR_mprotect & 0x7ff;
register long res_x0 asm("x0");
asm volatile("svc\t0"
: "=r"(res_x0)
: "r"(r0), "r"(r1), "r"(r2), "r"(r8), "r"(r16)
: "memory");
ax = res_x0;
#else
ax = syscall(__NR_mprotect & 0x7ff, addr, size, prot);
#endif
return ax;
}
forceinline int __sysv_getpid(void) {
long ax;
#if defined(__MNO_RED_ZONE__) && defined(__GNUC__) && !defined(__STRICT_ANSI__)
asm volatile("call\t__syscall__"
: "=a"(ax)
: "0"(__NR_getpid)
: "rdx", "memory", "cc");
#elif defined(__aarch64__)
register long r8 asm("x8") = (long)__NR_getpid & 0x7ff;
register long r16 asm("x16") = (long)__NR_getpid & 0x7ff;
register long res_x0 asm("x0");
asm volatile("svc\t0" : "=r"(res_x0) : "r"(r8), "r"(r16) : "memory");
ax = res_x0;
#else
ax = syscall(__NR_getpid & 0x7ff);
#endif
return ax;
}
forceinline ssize_t __write(const void *p, size_t n) {
uint32_t wrote;
if (!IsWindows()) {
return __sysv_write(2, p, n);
} else if (WriteFile(GetStdHandle(kNtStdErrorHandle), p, n, &wrote, 0)) {
return wrote;
} else {
return -GetLastError();
}
}
forceinline size_t __strlen(const char *s) {
size_t i = 0;
while (s[i]) ++i;
return i;
}
forceinline ssize_t __write_str(const char *s) {
return __write(s, __strlen(s));
}
forceinline int __strcmp(const char *l, const char *r) {
size_t i = 0;
while (l[i] == r[i] && r[i]) ++i;