Make improvements

- More timspec_*() and timeval_*() APIs have been introduced.
- The copyfd() function is now simplified thanks to POSIX rules.
- More Cosmo-specific APIs have been moved behind the COSMO define.
- The setitimer() polyfill for Windows NT is now much higher quality.
- Fixed build error for MODE=aarch64 due to -mstringop-strategy=loop.
- This change introduces `make MODE=nox87 toolchain` which makes it
  possible to build programs using your cosmocc toolchain that don't
  have legacy fpu instructions. This is useful, for example, if you
  want to have a ~22kb tinier blink virtual machine.
This commit is contained in:
Justine Tunney 2023-06-15 13:50:42 -07:00
parent 8dc11afcf6
commit c3440d040c
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
132 changed files with 539 additions and 587 deletions

View file

@ -21,6 +21,7 @@
#include "libc/dce.h"
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/bits.h"
#include "libc/intrin/cmpxchg.h"
#include "libc/intrin/directmap.internal.h"
#include "libc/intrin/kmalloc.h"
@ -437,10 +438,7 @@ static struct AsanFault __asan_checka(const signed char *s, long ndiv8) {
if (UNLIKELY(!((intptr_t)s & (FRAMESIZE - 1))) && kisdangerous(s)) {
return (struct AsanFault){kAsanUnmapped, s};
}
if ((w = ((uint64_t)(255 & s[0]) << 000 | (uint64_t)(255 & s[1]) << 010 |
(uint64_t)(255 & s[2]) << 020 | (uint64_t)(255 & s[3]) << 030 |
(uint64_t)(255 & s[4]) << 040 | (uint64_t)(255 & s[5]) << 050 |
(uint64_t)(255 & s[6]) << 060 | (uint64_t)(255 & s[7]) << 070))) {
if ((w = READ64LE(s))) {
s += __asan_bsf(w) >> 3;
return __asan_fault(s, kAsanHeapOverrun);
}

View file

@ -19,6 +19,7 @@
#include "libc/calls/calls.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/sysv/consts/arch.h"
const char *(DescribeArchPrctlCode)(char buf[12], int x) {
if (x == ARCH_SET_FS) return "ARCH_SET_FS";

View file

@ -24,6 +24,7 @@ const char *DescribeFrame(char[32], int);
const char *DescribeFutexOp(char[64], int);
const char *DescribeHow(char[12], int);
const char *DescribeInOutInt64(char[23], ssize_t, int64_t *);
const char *DescribeItimer(char[12], int);
const char *DescribeMapFlags(char[64], int);
const char *DescribeMapping(char[8], int, int);
const char *DescribeNtConsoleInFlags(char[256], uint32_t);
@ -77,6 +78,7 @@ const char *DescribeWhichPrio(char[12], int);
#define DescribeFutexOp(x) DescribeFutexOp(alloca(64), x)
#define DescribeHow(x) DescribeHow(alloca(12), x)
#define DescribeInOutInt64(rc, x) DescribeInOutInt64(alloca(23), rc, x)
#define DescribeItimer(x) DescribeItimer(alloca(12), x)
#define DescribeMapFlags(x) DescribeMapFlags(alloca(64), x)
#define DescribeMapping(x, y) DescribeMapping(alloca(8), x, y)
#define DescribeNtConsoleInFlags(x) DescribeNtConsoleInFlags(alloca(256), x)

View file

@ -0,0 +1,29 @@
/*-*- 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 2023 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/fmt/itoa.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/sysv/consts/itimer.h"
const char *(DescribeItimer)(char buf[12], int which) {
if (which == ITIMER_REAL) return "ITIMER_REAL";
if (which == ITIMER_VIRTUAL) return "ITIMER_VIRTUAL";
if (which == ITIMER_PROF) return "ITIMER_PROF";
FormatInt32(buf, which);
return buf;
}

View file

@ -0,0 +1,41 @@
/*-*- 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 2021 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/struct/itimerval.internal.h"
#include "libc/calls/struct/timeval.h"
#include "libc/calls/struct/timeval.internal.h"
#include "libc/dce.h"
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/kprintf.h"
#define N 90
const char *(DescribeItimerval)(char buf[N], int rc,
const struct itimerval *it) {
if (!it) return "NULL";
if (rc == -1) return "n/a";
if ((!IsAsan() && kisdangerous(it)) ||
(IsAsan() && !__asan_is_valid(it, sizeof(*it)))) {
ksnprintf(buf, N, "%p", it);
} else {
ksnprintf(buf, N, "{%s, %s}", DescribeTimeval(0, &it->it_interval),
DescribeTimeval(0, &it->it_value));
}
return buf;
}

View file

@ -31,7 +31,6 @@ const char *(DescribeMapFlags)(char buf[64], int x) {
{MAP_FIXED, "FIXED"}, //
{MAP_FIXED_NOREPLACE, "FIXED_NOREPLACE"}, //
{MAP_CONCEAL, "CONCEAL"}, //
{MAP_HUGETLB, "HUGETLB"}, //
{MAP_LOCKED, "LOCKED"}, //
{MAP_NORESERVE, "NORESERVE"}, //
{MAP_NONBLOCK, "NONBLOCK"}, //

View file

@ -21,6 +21,8 @@
#include "libc/fmt/magnumstrs.internal.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/macros.internal.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/sol.h"
#define N (PAGESIZE / 2 / sizeof(struct DescribeFlags))
@ -29,17 +31,42 @@
* Describes clock_gettime() clock argument.
*/
const char *(DescribeOpenFlags)(char buf[128], int x) {
char *s;
char *p;
int i, n;
const char *pipe;
struct DescribeFlags d[N];
if (x == -1) return "-1";
// TODO(jart): unify DescribeFlags and MagnumStr data structures
for (n = 0; kOpenFlags[n].x != MAGNUM_TERMINATOR; ++n) {
if (n == N) notpossible;
p = buf;
switch (x & O_ACCMODE) {
case O_RDONLY:
p = stpcpy(p, "O_RDONLY");
x &= ~O_ACCMODE;
pipe = "|";
break;
case O_WRONLY:
p = stpcpy(p, "O_WRONLY");
x &= ~O_ACCMODE;
pipe = "|";
break;
case O_RDWR:
p = stpcpy(p, "O_RDWR");
x &= ~O_ACCMODE;
pipe = "|";
break;
default:
pipe = "";
break;
}
for (i = 0; i < n; ++i) {
d[i].flag = MAGNUM_NUMBER(kOpenFlags, i);
d[i].name = MAGNUM_STRING(kOpenFlags, i);
if (x) {
p = stpcpy(p, pipe);
for (n = 0; kOpenFlags[n].x != MAGNUM_TERMINATOR; ++n) {
if (n == N) notpossible;
}
for (i = 0; i < n; ++i) {
d[i].flag = MAGNUM_NUMBER(kOpenFlags, i);
d[i].name = MAGNUM_STRING(kOpenFlags, i);
}
DescribeFlags(p, 128 - (p - buf), d, n, "O_", x);
}
return DescribeFlags(buf, 128, d, n, "O_", x);
return buf;
}

View file

@ -23,8 +23,8 @@
#include "libc/intrin/kprintf.h"
const char *(DescribeTimeval)(char buf[45], int rc, const struct timeval *tv) {
if (rc == -1) return "n/a";
if (!tv) return "NULL";
if (rc == -1) return "n/a";
if ((!IsAsan() && kisdangerous(tv)) ||
(IsAsan() && !__asan_is_valid(tv, sizeof(*tv)))) {
ksnprintf(buf, 45, "%p", tv);

View file

@ -46,11 +46,14 @@ feclearexcept:
// maintain exceptions in the sse mxcsr, clear x87 exceptions
mov %edi,%ecx
and $0x3f,%ecx
#ifndef NOX87
fnstsw %ax
test %eax,%ecx
jz 1f
fnclex
1: stmxcsr -8(%rsp)
1:
#endif
stmxcsr -8(%rsp)
and $0x3f,%eax
or %eax,-8(%rsp)
test %ecx,-8(%rsp)
@ -96,12 +99,16 @@ fetestexcept:
.ftrace2
#ifdef __x86_64__
and $0x3f,%edi
push %rax
push $0
stmxcsr (%rsp)
#ifdef NOX87
pop %rax
#else
pop %rsi
fnstsw %ax
or %esi,%eax
and %edi,%eax
#endif
ret
#elif defined(__aarch64__)
and w0,w0,#0x1f
@ -138,10 +145,12 @@ __fesetround:
push %rax
xor %eax,%eax
mov %edi,%ecx
#ifndef NOX87
fnstcw (%rsp)
andb $0xf3,1(%rsp)
or %ch,1(%rsp)
fldcw (%rsp)
#endif
stmxcsr (%rsp)
shl $3,%ch
andb $0x9f,1(%rsp)
@ -181,7 +190,9 @@ fegetenv:
.ftrace2
#ifdef __x86_64__
xor %eax,%eax
#ifndef NOX87
fnstenv (%rdi)
#endif
stmxcsr 28(%rdi)
ret
#elif defined(__aarch64__)
@ -200,14 +211,18 @@ fesetenv:
xor %eax,%eax
inc %rdi
jz 1f
#ifndef NOX87
fldenv -1(%rdi)
#endif
ldmxcsr 27(%rdi)
ret
1: push %rax
push %rax
pushq $0xffff
pushq $0x37f
#ifndef NOX87
fldenv (%rsp)
#endif
pushq $0x1f80
ldmxcsr (%rsp)
add $40,%rsp

View file

@ -35,5 +35,5 @@ double fmax(double x, double y) {
}
#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
__strong_reference(fmax, fmaxl);
__weak_reference(fmax, fmaxl);
#endif

View file

@ -69,6 +69,6 @@ double scalbn(double x, int n)
__strong_reference(scalbn, ldexp);
#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
__strong_reference(scalbn, ldexpl);
__strong_reference(scalbn, scalbnl);
__weak_reference(scalbn, ldexpl);
__weak_reference(scalbn, scalbnl);
#endif