Improve dead code elimination

This commit is contained in:
Justine Tunney 2021-02-08 04:04:42 -08:00
parent 760db8c5ad
commit 0e36cb3ac4
6606 changed files with 9685 additions and 9854 deletions

View file

@ -39,21 +39,23 @@ int close(int fd) {
if (fd < 0) return einval();
if (fd < g_fds.n && g_fds.p[fd].kind == kFdZip) {
rc = weaken(__zipos_close)(fd);
} else if (fd < g_fds.n && g_fds.p[fd].kind == kFdEpoll) {
rc = weaken(sys_close_epoll)(fd);
} else if (!IsWindows()) {
rc = sys_close(fd);
} else if (fd < g_fds.n && g_fds.p[fd].kind == kFdSocket) {
rc = weaken(sys_closesocket_nt)(g_fds.p + fd);
} else if (fd < g_fds.n &&
(g_fds.p[fd].kind == kFdFile || g_fds.p[fd].kind == kFdConsole ||
g_fds.p[fd].kind == kFdProcess)) {
rc = sys_close_nt(g_fds.p + fd);
} else {
rc = ebadf();
}
if (!__vforked && fd < g_fds.n) {
__releasefd(fd);
if (!IsWindows()) {
rc = sys_close(fd);
} else {
if (fd < g_fds.n && g_fds.p[fd].kind == kFdEpoll) {
rc = weaken(sys_close_epoll_nt)(fd);
} else if (fd < g_fds.n && g_fds.p[fd].kind == kFdSocket) {
rc = weaken(sys_closesocket_nt)(g_fds.p + fd);
} else if (fd < g_fds.n && (g_fds.p[fd].kind == kFdFile ||
g_fds.p[fd].kind == kFdConsole ||
g_fds.p[fd].kind == kFdProcess)) {
rc = sys_close_nt(g_fds.p + fd);
} else {
rc = ebadf();
}
}
}
__releasefd(fd);
return rc;
}

View file

@ -16,7 +16,6 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/progn.internal.h"
#include "libc/bits/safemacros.h"
#include "libc/calls/calls.h"
#include "libc/runtime/runtime.h"
@ -74,8 +73,14 @@ char *commandv(const char *name, char pathbuf[hasatleast PATH_MAX]) {
char *p;
size_t namelen;
int rc, olderr;
if (!(namelen = strlen(name))) return PROGN(enoent(), NULL);
if (namelen + 1 > PATH_MAX) return PROGN(enametoolong(), NULL);
if (!(namelen = strlen(name))) {
enoent();
return NULL;
}
if (namelen + 1 > PATH_MAX) {
enametoolong();
return NULL;
}
if (strchr(name, '/') || strchr(name, '\\')) {
if (AccessCommand(strcpy(pathbuf, ""), name, namelen, 0)) {
return pathbuf;

View file

@ -50,7 +50,7 @@ int fallocate(int fd, int32_t mode, int64_t offset, int64_t length) {
return rc;
} else if (!IsWindows()) {
return sys_posix_fallocate(fd, offset, length);
} else if (IsWindows()) {
} else {
if (!__isfdkind(fd, kFdFile)) return ebadf();
if (mode == FALLOC_FL_ZERO_RANGE) {
if (DeviceIoControl(
@ -70,7 +70,5 @@ int fallocate(int fd, int32_t mode, int64_t offset, int64_t length) {
} else {
return enosys();
}
} else {
return enosys();
}
}

View file

@ -31,12 +31,10 @@ int fstat(int fd, struct stat *st) {
if (__isfdkind(fd, kFdZip)) {
return weaken(__zipos_fstat)(
(struct ZiposHandle *)(intptr_t)g_fds.p[fd].handle, st);
} else if (!IsWindows()) {
if (!IsMetal()) {
return sys_fstat(fd, st);
} else {
return fstat_metal(fd, st);
}
} else if (!IsWindows() && !IsMetal()) {
return sys_fstat(fd, st);
} else if (IsMetal()) {
return fstat_metal(fd, st);
} else {
if (!__isfdkind(fd, kFdFile)) return ebadf();
return sys_fstat_nt(g_fds.p[fd].handle, st);

View file

@ -16,7 +16,6 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/initializer.internal.h"
#include "libc/bits/pushpop.h"
#include "libc/calls/internal.h"
#include "libc/nt/runtime.h"
@ -26,13 +25,21 @@ STATIC_YOINK("_init_g_fds");
hidden struct Fds g_fds;
hidden void InitializeFileDescriptors(void) {
hidden textstartup void InitializeFileDescriptors(void) {
struct Fds *fds;
fds = VEIL("r", &g_fds);
pushmov(&fds->f, 3ul);
pushmov(&fds->n, ARRAYLEN(fds->__init_p));
fds->p = fds->__init_p;
if (!IsMetal()) {
if (IsMetal()) {
pushmov(&fds->f, 3ull);
fds->__init_p[STDIN_FILENO].kind = pushpop(kFdSerial);
fds->__init_p[STDOUT_FILENO].kind = pushpop(kFdSerial);
fds->__init_p[STDERR_FILENO].kind = pushpop(kFdSerial);
fds->__init_p[STDIN_FILENO].handle = VEIL("r", 0x3F8ull);
fds->__init_p[STDOUT_FILENO].handle = VEIL("r", 0x3F8ull);
fds->__init_p[STDERR_FILENO].handle = VEIL("r", 0x3F8ull);
} else if (IsWindows()) {
pushmov(&fds->f, 3ull);
fds->__init_p[STDIN_FILENO].kind = pushpop(kFdFile);
fds->__init_p[STDOUT_FILENO].kind = pushpop(kFdFile);
fds->__init_p[STDERR_FILENO].kind = pushpop(kFdFile);
@ -42,12 +49,5 @@ hidden void InitializeFileDescriptors(void) {
GetStdHandle(pushpop(kNtStdOutputHandle));
fds->__init_p[STDERR_FILENO].handle =
GetStdHandle(pushpop(kNtStdErrorHandle));
} else {
fds->__init_p[STDIN_FILENO].kind = pushpop(kFdSerial);
fds->__init_p[STDOUT_FILENO].kind = pushpop(kFdSerial);
fds->__init_p[STDERR_FILENO].kind = pushpop(kFdSerial);
fds->__init_p[STDIN_FILENO].handle = 0x3F8;
fds->__init_p[STDOUT_FILENO].handle = 0x3F8;
fds->__init_p[STDERR_FILENO].handle = 0x3F8;
}
}

View file

@ -1,25 +0,0 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/internal.h"
#include "libc/nt/startupinfo.h"
#include "libc/nt/struct/startupinfo.h"
hidden struct NtStartupInfo __nt_startupinfo;
STATIC_YOINK("_init___nt_startupinfo");

View file

@ -1,26 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
.init.start 400,_init___nt_startupinfo
ezlea __nt_startupinfo,cx
mov %rsp,%rbp
ntcall __imp_GetStartupInfoW
.init.end 400,_init___nt_startupinfo,globl,hidden

View file

@ -1,25 +0,0 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/internal.h"
#include "libc/nt/struct/systeminfo.h"
#include "libc/nt/systeminfo.h"
hidden struct NtSystemInfo __nt_systeminfo;
STATIC_YOINK("_init___nt_systeminfo");

View file

@ -1,26 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
.init.start 400,_init___nt_systeminfo
ezlea __nt_systeminfo,cx
mov %rsp,%rbp
ntcall __imp_GetSystemInfo
.init.end 400,_init___nt_systeminfo,globl,hidden

View file

@ -47,10 +47,10 @@ enum FdKind {
};
struct Fd {
enum FdKind kind;
unsigned flags;
int64_t handle;
int64_t extra;
int kind;
unsigned flags;
};
struct Fds {
@ -66,8 +66,6 @@ hidden extern volatile bool __interrupted;
hidden extern int __vforked;
hidden extern unsigned __sighandrvas[NSIG];
hidden extern struct Fds g_fds;
hidden extern struct NtSystemInfo __nt_systeminfo;
hidden extern struct NtStartupInfo __nt_startupinfo;
hidden extern const struct NtSecurityAttributes kNtIsInheritable;
int __reservefd(void) hidden;
@ -232,6 +230,7 @@ bool32 sys_isatty_nt(int) hidden;
char *sys_getcwd_nt(char *, size_t) hidden;
i64 sys_lseek_nt(int, i64, int) hidden;
int sys_chdir_nt(const char *) hidden;
int sys_close_epoll_nt(int) hidden;
int sys_close_nt(struct Fd *) hidden;
int sys_dup_nt(int, int, int) hidden;
int sys_execve_nt(const char *, char *const[], char *const[]) hidden;

View file

@ -22,6 +22,7 @@
#include "libc/calls/struct/winsize.h"
#include "libc/nt/console.h"
#include "libc/nt/enum/startf.h"
#include "libc/nt/startupinfo.h"
#include "libc/nt/struct/consolescreenbufferinfoex.h"
#include "libc/str/str.h"
#include "libc/sysv/errfuns.h"
@ -29,8 +30,10 @@
textwindows int ioctl_tiocgwinsz_nt(int fd, struct winsize *ws) {
int i, fds[3];
uint32_t mode;
struct NtStartupInfo startinfo;
struct NtConsoleScreenBufferInfoEx sbinfo;
fds[0] = fd, fds[1] = 1, fds[2] = 0;
GetStartupInfo(&startinfo);
for (i = 0; i < ARRAYLEN(fds); ++i) {
if (__isfdkind(fds[i], kFdFile) || __isfdkind(fds[i], kFdConsole)) {
if (GetConsoleMode(g_fds.p[fds[i]].handle, &mode)) {
@ -42,9 +45,9 @@ textwindows int ioctl_tiocgwinsz_nt(int fd, struct winsize *ws) {
ws->ws_xpixel = 0;
ws->ws_ypixel = 0;
return 0;
} else if (__nt_startupinfo.dwFlags & kNtStartfUsecountchars) {
ws->ws_col = __nt_startupinfo.dwXCountChars;
ws->ws_row = __nt_startupinfo.dwYCountChars;
} else if (startinfo.dwFlags & kNtStartfUsecountchars) {
ws->ws_col = startinfo.dwXCountChars;
ws->ws_row = startinfo.dwYCountChars;
ws->ws_xpixel = 0;
ws->ws_ypixel = 0;
return 0;

View file

@ -16,6 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/dce.h"
#include "libc/macros.h"
.source __FILE__
@ -31,7 +32,11 @@ kNtSystemDirectory:
.previous
.init.start 300,_init_kNtSystemDirectory
#if SupportsWindows()
pushpop BYTES,%rdx
mov __imp_GetSystemDirectoryA(%rip),%rax
call __getntsyspath
#else
add $BYTES,%rdi
#endif
.init.end 300,_init_kNtSystemDirectory

View file

@ -32,7 +32,11 @@ kNtWindowsDirectory:
.previous
.init.start 300,_init_kNtWindowsDirectory
#if SupportsWindows()
pushpop BYTES,%rdx
mov __imp_GetWindowsDirectoryA(%rip),%rax
call __getntsyspath
#else
add $BYTES,%rdi
#endif
.init.end 300,_init_kNtWindowsDirectory

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/runtime/runtime.h"
#include "libc/dce.h"
#include "libc/macros.h"
#define kTmpPathMax 80
@ -34,8 +35,12 @@ kTmpPath:
.init.start 300,_init_kTmpPath
movl $'/|'t<<010|'m<<020|'p<<030,(%rdi)
movw $'/,4(%rdi)
#if SupportsWindows()
pushpop kTmpPathMax,%rdx
ezlea GetTempPathA_flunk,ax
call __getntsyspath
#else
add $kTmpPathMax,%rdi
#endif
.init.end 300,_init_kTmpPath
.source __FILE__

View file

@ -36,7 +36,7 @@ ssize_t readv(int fd, const struct iovec *iov, int iovlen) {
if (fd < g_fds.n && g_fds.p[fd].kind == kFdZip) {
return weaken(__zipos_read)(
(struct ZiposHandle *)(intptr_t)g_fds.p[fd].handle, iov, iovlen, -1);
} else if (fd < g_fds.n && g_fds.p[fd].kind == kFdSerial) {
} else if (SupportsMetal() && fd < g_fds.n && g_fds.p[fd].kind == kFdSerial) {
return readv_serial(&g_fds.p[fd], iov, iovlen);
} else if (!IsWindows()) {
return sys_readv(fd, iov, iovlen);

View file

@ -21,9 +21,11 @@
void __releasefd(int fd) {
int x;
g_fds.p[fd].kind = kFdEmpty;
do {
x = g_fds.f;
if (fd >= x) break;
} while (!cmpxchg(&g_fds.f, x, fd));
if (!__vforked && 0 <= fd && fd < g_fds.n) {
g_fds.p[fd].kind = kFdEmpty;
do {
x = g_fds.f;
if (fd >= x) break;
} while (!cmpxchg(&g_fds.f, x, fd));
}
}

View file

@ -25,11 +25,13 @@
textwindows int sys_sysinfo_nt(struct sysinfo *info) {
struct NtMemoryStatusEx memstat;
struct NtSystemInfo sysinfo;
GetSystemInfo(&sysinfo);
memstat.dwLength = sizeof(struct NtMemoryStatusEx);
if (GlobalMemoryStatusEx(&memstat)) {
info->totalram = memstat.ullTotalPhys;
info->freeram = memstat.ullAvailPhys;
info->procs = __nt_systeminfo.dwNumberOfProcessors;
info->procs = sysinfo.dwNumberOfProcessors;
info->mem_unit = 1;
return 0;
} else {

View file

@ -39,7 +39,7 @@ ssize_t writev(int fd, const struct iovec *iov, int iovlen) {
if (fd < g_fds.n && g_fds.p[fd].kind == kFdZip) {
return weaken(__zipos_write)(
(struct ZiposHandle *)(intptr_t)g_fds.p[fd].handle, iov, iovlen, -1);
} else if (fd < g_fds.n && g_fds.p[fd].kind == kFdSerial) {
} else if (SupportsMetal() && fd < g_fds.n && g_fds.p[fd].kind == kFdSerial) {
return writev_serial(&g_fds.p[fd], iov, iovlen);
} else if (!IsWindows()) {
return sys_writev(fd, iov, iovlen);