Make minor improvements

main
Justine Tunney 2020-12-19 11:21:04 -08:00
parent 1fc91f3580
commit b562d6fdb3
41 changed files with 1948 additions and 92 deletions

View File

@ -137,8 +137,6 @@ include dsp/tty/tty.mk # ├──online
include libc/dns/dns.mk # │ include libc/dns/dns.mk # │
include libc/crypto/crypto.mk # │ include libc/crypto/crypto.mk # │
include net/http/http.mk #─┘ include net/http/http.mk #─┘
include third_party/chibicc/chibicc.mk
include third_party/chibicc/test/test.mk
include third_party/lemon/lemon.mk include third_party/lemon/lemon.mk
include third_party/linenoise/linenoise.mk include third_party/linenoise/linenoise.mk
include third_party/editline/editline.mk include third_party/editline/editline.mk
@ -155,6 +153,8 @@ include third_party/m4/m4.mk
include third_party/lz4cli/lz4cli.mk include third_party/lz4cli/lz4cli.mk
include third_party/bzip2/bzip2.mk include third_party/bzip2/bzip2.mk
include tool/build/lib/buildlib.mk include tool/build/lib/buildlib.mk
include third_party/chibicc/chibicc.mk
include third_party/chibicc/test/test.mk
include tool/build/emucrt/emucrt.mk include tool/build/emucrt/emucrt.mk
include tool/build/emubin/emubin.mk include tool/build/emubin/emubin.mk
include tool/build/build.mk include tool/build/build.mk

View File

@ -0,0 +1,28 @@
/*-*- 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
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
/**
* Creates session and sets the process group id.
*/
uint32_t getsid(int pid) {
return getsid$sysv(pid);
}

View File

@ -180,8 +180,10 @@ i64 write$sysv(i32, const void *, u64) hidden;
int getitimer$sysv(i32, struct itimerval *) hidden; int getitimer$sysv(i32, struct itimerval *) hidden;
int setresgid$sysv(uint32_t, uint32_t, uint32_t) hidden; int setresgid$sysv(uint32_t, uint32_t, uint32_t) hidden;
int setresuid$sysv(uint32_t, uint32_t, uint32_t) hidden; int setresuid$sysv(uint32_t, uint32_t, uint32_t) hidden;
int setsid$sysv(void) hidden;
u32 getgid$sysv(void) hidden; u32 getgid$sysv(void) hidden;
u32 getpid$sysv(void) hidden; u32 getpid$sysv(void) hidden;
u32 getsid$sysv(int) hidden;
u32 gettid$sysv(void) hidden; u32 gettid$sysv(void) hidden;
u32 getuid$sysv(void) hidden; u32 getuid$sysv(void) hidden;
void *mmap$sysv(void *, u64, u32, u32, i64, i64) hidden; void *mmap$sysv(void *, u64, u32, u32, i64, i64) hidden;

View File

@ -26,6 +26,7 @@
/ @see setgid(), getauxval(AT_SECURE) / @see setgid(), getauxval(AT_SECURE)
setegid:push %rbp setegid:push %rbp
mov %rsp,%rbp mov %rsp,%rbp
.profilable
mov %edi,%esi mov %edi,%esi
mov $-1,%edi mov $-1,%edi
call setregid call setregid

View File

@ -26,6 +26,7 @@
/ @see setuid(), getauxval(AT_SECURE) / @see setuid(), getauxval(AT_SECURE)
seteuid:push %rbp seteuid:push %rbp
mov %rsp,%rbp mov %rsp,%rbp
.profilable
mov %edi,%esi mov %edi,%esi
mov $-1,%edi mov $-1,%edi
call setreuid call setreuid

View File

@ -1,5 +1,5 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│ /*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney Copyright 2020 Justine Alexandra Roberts Tunney
@ -17,8 +17,12 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA 02110-1301 USA
*/ */
#include "libc/macros.h" #include "libc/calls/calls.h"
#include "libc/calls/internal.h"
__strncpy: /**
jmp strncpy * Creates session and sets the process group id.
.endfn __strncpy,globl */
int setsid(void) {
return setsid$sysv();
}

View File

@ -34,20 +34,15 @@
__sigenter: __sigenter:
push %rbp push %rbp
mov %rsp,%rbp mov %rsp,%rbp
push %rbx
push %rbx
ezlea _base,bx
.profilable .profilable
ezlea g_sighandrvas,ax
and $NSIG-1,%edi and $NSIG-1,%edi
mov (%rax,%rdi,4),%eax mov g_sighandrvas(,%rdi,4),%eax
cmp $kSigactionMinRva,%eax cmp $kSigactionMinRva,%eax
jl 2f jl 2f
add %rbx,%rax lea _base(%rax),%eax
call *%rax call *%rax
mov $1,%eax mov $1,%eax
1: pop %rbx 1: leave
leave
ret ret
2: xor %eax,%eax 2: xor %eax,%eax
jmp 1b jmp 1b

View File

@ -920,6 +920,9 @@ typedef uint64_t uintmax_t;
#pragma GCC diagnostic ignored /* tidy */ "-Wunused-but-set-variable" #pragma GCC diagnostic ignored /* tidy */ "-Wunused-but-set-variable"
#pragma GCC diagnostic ignored /* tidy */ "-Wunused-but-set-parameter" #pragma GCC diagnostic ignored /* tidy */ "-Wunused-but-set-parameter"
#endif /* GCC6+ */ #endif /* GCC6+ */
#if __GNUC__ >= 8
#pragma GCC diagnostic ignored "-Wstringop-truncation"
#endif /* GCC8+ */
#if __GNUC__ + 0 >= 9 #if __GNUC__ + 0 >= 9
#pragma GCC diagnostic ignored /* "always true" breaks dce */ "-Waddress" #pragma GCC diagnostic ignored /* "always true" breaks dce */ "-Waddress"
#endif /* GCC9+ */ #endif /* GCC9+ */

View File

@ -17,16 +17,16 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA 02110-1301 USA
*/ */
#include "libc/macros.h"
#include "libc/runtime/valist.h" #include "libc/runtime/valist.h"
static void *__va_arg_mem(struct __va *ap, unsigned long sz, unsigned align) { static void *__va_arg_mem(struct __va *ap, size_t sz, size_t align) {
void *r = ap->overflow_arg_area; void *r = (void *)ROUNDUP((intptr_t)ap->overflow_arg_area, align);
if (align > 8) r = (void *)(((unsigned long)r + 15) / 16 * 16); ap->overflow_arg_area = (void *)ROUNDUP((intptr_t)r + sz, 8);
ap->overflow_arg_area = (void *)(((unsigned long)r + sz + 7) / 8 * 8);
return r; return r;
} }
void *__va_arg(struct __va *ap, unsigned long sz, unsigned align, unsigned k) { void *__va_arg(struct __va *ap, size_t sz, unsigned align, unsigned k) {
void *r; void *r;
switch (k) { switch (k) {
case 0: case 0:

View File

@ -4,13 +4,13 @@
COSMOPOLITAN_C_START_ COSMOPOLITAN_C_START_
struct __va { struct __va {
unsigned int gp_offset; uint32_t gp_offset;
unsigned int fp_offset; uint32_t fp_offset;
void *overflow_arg_area; void *overflow_arg_area;
void *reg_save_area; void *reg_save_area;
}; };
void *__va_arg(struct __va *, unsigned long, unsigned, unsigned); void *__va_arg(struct __va *, size_t, unsigned, unsigned);
COSMOPOLITAN_C_END_ COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View File

@ -238,9 +238,6 @@ char *strsignal(int) returnsnonnull libcesque;
*/ */
#if defined(__GNUC__) && !defined(__STRICT_ANSI__) #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
char *__strncpy(char *, const char *, size_t) memcpyesque;
#define strncpy(DEST, SRC, N) __strncpy(DEST, SRC, N) /* pacify bad warning */
#define explicit_bzero(STR, BYTES) \ #define explicit_bzero(STR, BYTES) \
do { \ do { \
void *Str; \ void *Str; \

View File

@ -33,10 +33,11 @@
* @see stpncpy(), memccpy() * @see stpncpy(), memccpy()
* @asyncsignalsafe * @asyncsignalsafe
*/ */
char *(strncpy)(char *dest, const char *src, size_t stride) { char *strncpy(char *dest, const char *src, size_t stride) {
char *p; size_t i;
if ((p = memccpy(dest, src, '\0', stride))) { for (i = 0; i < stride; ++i) {
memset(p, 0, dest + stride - p); if (!(dest[i] = src[i])) break;
} }
memset(dest + i, 0, stride - i);
return dest; return dest;
} }

View File

@ -19,6 +19,9 @@
*/ */
#include "libc/str/str.h" #include "libc/str/str.h"
/**
* Converts character to lower case.
*/
int tolower(int c) { int tolower(int c) {
return 'A' <= c && c <= 'Z' ? c + ('a' - 'A') : c; return 'A' <= c && c <= 'Z' ? c + ('a' - 'A') : c;
} }

View File

@ -19,6 +19,9 @@
*/ */
#include "libc/str/str.h" #include "libc/str/str.h"
/**
* Converts character to upper case.
*/
int toupper(int c) { int toupper(int c) {
return 'a' <= c && c <= 'z' ? c - ('a' - 'A') : c; return 'a' <= c && c <= 'z' ? c - ('a' - 'A') : c;
} }

View File

@ -19,6 +19,9 @@
*/ */
#include "libc/str/str.h" #include "libc/str/str.h"
/**
* Converts wide character to lower case.
*/
wint_t towlower(wint_t wc) { wint_t towlower(wint_t wc) {
return tolower(wc); return tolower(wc);
} }

View File

@ -19,6 +19,9 @@
*/ */
#include "libc/str/str.h" #include "libc/str/str.h"
/**
* Converts wide character to upper case.
*/
wint_t towupper(wint_t wc) { wint_t towupper(wint_t wc) {
return toupper(wc); return toupper(wc);
} }

View File

@ -31,6 +31,6 @@ int wcscasecmp(const wchar_t *a, const wchar_t *b) {
size_t i = 0; size_t i = 0;
unsigned x, y; unsigned x, y;
if (a == b) return 0; if (a == b) return 0;
while ((x = tolower(a[i])) == (y = tolower(b[i])) && b[i]) ++i; while ((x = towlower(a[i])) == (y = towlower(b[i])) && b[i]) ++i;
return x - y; return x - y;
} }

View File

@ -0,0 +1,2 @@
.include "o/libc/sysv/macros.inc"
.scall getsid$sysv 0x00ff01362136007c globl hidden

View File

@ -1,2 +0,0 @@
.include "o/libc/sysv/macros.inc"
.scall getsid 0x00ff01362136007c globl

View File

@ -0,0 +1,2 @@
.include "o/libc/sysv/macros.inc"
.scall setsid$sysv 0x0093009320930070 globl hidden

View File

@ -1,2 +0,0 @@
.include "o/libc/sysv/macros.inc"
.scall setsid 0x0093009320930070 globl

View File

@ -145,8 +145,8 @@ scall 'getuid$sysv' 0x0018001820180066 globl hidden
scall 'getgid$sysv' 0x002f002f202f0068 globl hidden scall 'getgid$sysv' 0x002f002f202f0068 globl hidden
scall 'getppid$sysv' 0x002700272027006e globl hidden scall 'getppid$sysv' 0x002700272027006e globl hidden
scall getpgrp 0x005100512051006f globl scall getpgrp 0x005100512051006f globl
scall setsid 0x0093009320930070 globl scall 'setsid$sysv' 0x0093009320930070 globl hidden
scall getsid 0x00ff01362136007c globl scall 'getsid$sysv' 0x00ff01362136007c globl hidden
scall getpgid 0x00cf00cf20970079 globl scall getpgid 0x00cf00cf20970079 globl
scall setpgid 0x005200522052006d globl scall setpgid 0x005200522052006d globl
scall geteuid 0x001900192019006b globl scall geteuid 0x001900192019006b globl

View File

@ -27,6 +27,32 @@ TEST(strcpy, test) {
EXPECT_STREQ("hello there what's up", strcpy(buf, "hello there what's up")); EXPECT_STREQ("hello there what's up", strcpy(buf, "hello there what's up"));
} }
TEST(strncpy, test) {
char b[8] = {0, 1, 2, 3, 4, 5, 6, 7};
strncpy(b, "key", 8);
ASSERT_EQ('k', b[0]);
ASSERT_EQ('e', b[1]);
ASSERT_EQ('y', b[2]);
ASSERT_EQ('\0', b[3]);
ASSERT_EQ('\0', b[4]);
ASSERT_EQ('\0', b[5]);
ASSERT_EQ('\0', b[6]);
ASSERT_EQ('\0', b[7]);
}
TEST(strncpy, testSameLength_isNotNulTerminated) {
char b[8] = {0, 1, 2, 3, 4, 5, 6, 7};
strncpy(b, "keyword", 7);
ASSERT_EQ('k', b[0]);
ASSERT_EQ('e', b[1]);
ASSERT_EQ('y', b[2]);
ASSERT_EQ('w', b[3]);
ASSERT_EQ('o', b[4]);
ASSERT_EQ('r', b[5]);
ASSERT_EQ('d', b[6]);
ASSERT_EQ(7, b[7]);
}
BENCH(strcpy, bench) { BENCH(strcpy, bench) {
extern char *strcpy_(char *, const char *) asm("strcpy"); extern char *strcpy_(char *, const char *) asm("strcpy");
static char buf[1024], buf2[1024]; static char buf[1024], buf2[1024];

1642
third_party/chibicc/as.c vendored 100644

File diff suppressed because it is too large Load Diff

View File

@ -20,6 +20,8 @@
#define PRECIOUS 0b1111000000101000 // bx,bp,r12-r15 #define PRECIOUS 0b1111000000101000 // bx,bp,r12-r15
StaticAsm *staticasms;
static const char kGreg[4][16][5] = { static const char kGreg[4][16][5] = {
{"al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil", "r8b", "r9b", "r10b", {"al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil", "r8b", "r9b", "r10b",
"r11b", "r12b", "r13b", "r14b", "r15b"}, "r11b", "r12b", "r13b", "r14b", "r15b"},
@ -31,8 +33,6 @@ static const char kGreg[4][16][5] = {
"r11", "r12", "r13", "r14", "r15"}, "r11", "r12", "r13", "r14", "r15"},
}; };
StaticAsm *staticasms;
static void DecodeAsmConstraints(AsmOperand *op) { static void DecodeAsmConstraints(AsmOperand *op) {
int i; int i;
char c; char c;

View File

@ -590,7 +590,7 @@ static void run_linker(StringArray *inputs, char *output) {
run_subprocess(arr.data); run_subprocess(arr.data);
} }
int main(int argc, char **argv) { int chibicc(int argc, char **argv) {
showcrashreports(); showcrashreports();
atexit(cleanup); atexit(cleanup);
init_macros(); init_macros();

View File

@ -120,6 +120,10 @@ Token *tokenize_string_literal(Token *, Type *);
bool consume(Token **, Token *, char *, size_t); bool consume(Token **, Token *, char *, size_t);
bool equal(Token *, char *, size_t); bool equal(Token *, char *, size_t);
void convert_pp_tokens(Token *); void convert_pp_tokens(Token *);
void remove_backslash_newline(char *);
void canonicalize_newline(char *);
char *read_file(char *);
int read_escaped_char(char **, char *);
#define UNREACHABLE() error("internal error at %s:%d", __FILE__, __LINE__) #define UNREACHABLE() error("internal error at %s:%d", __FILE__, __LINE__)
#define EQUAL(T, S) equal(T, S, strlen(S)) #define EQUAL(T, S) equal(T, S, strlen(S))
@ -563,6 +567,8 @@ extern bool opt_sse4;
extern bool opt_verbose; extern bool opt_verbose;
extern char *base_file; extern char *base_file;
int chibicc(int, char **);
// //
// alloc.c // alloc.c
// //

View File

@ -20,10 +20,16 @@ PKGS += THIRD_PARTY_CHIBICC
THIRD_PARTY_CHIBICC_ARTIFACTS += THIRD_PARTY_CHIBICC_A THIRD_PARTY_CHIBICC_ARTIFACTS += THIRD_PARTY_CHIBICC_A
THIRD_PARTY_CHIBICC = $(THIRD_PARTY_CHIBICC_A_DEPS) $(THIRD_PARTY_CHIBICC_A) THIRD_PARTY_CHIBICC = $(THIRD_PARTY_CHIBICC_A_DEPS) $(THIRD_PARTY_CHIBICC_A)
THIRD_PARTY_CHIBICC_A = o/$(MODE)/third_party/chibicc/chibicc.a THIRD_PARTY_CHIBICC_A = o/$(MODE)/third_party/chibicc/chibicc.a
THIRD_PARTY_CHIBICC2_A = o/$(MODE)/third_party/chibicc/chibicc2.a
THIRD_PARTY_CHIBICC_A_FILES := $(wildcard third_party/chibicc/*) THIRD_PARTY_CHIBICC_A_FILES := $(wildcard third_party/chibicc/*)
THIRD_PARTY_CHIBICC_A_HDRS = $(filter %.h,$(THIRD_PARTY_CHIBICC_A_FILES)) THIRD_PARTY_CHIBICC_A_HDRS = $(filter %.h,$(THIRD_PARTY_CHIBICC_A_FILES))
THIRD_PARTY_CHIBICC_A_SRCS = $(filter %.c,$(THIRD_PARTY_CHIBICC_A_FILES)) THIRD_PARTY_CHIBICC_A_SRCS = $(filter %.c,$(THIRD_PARTY_CHIBICC_A_FILES))
THIRD_PARTY_CHIBICC_DEFINES = \
-DCRT=\"$(CRT)\" \
-DAPE=\"o/$(MODE)/ape/ape.o\" \
-DLDS=\"o/$(MODE)/ape/ape.lds\"
THIRD_PARTY_CHIBICC_BINS = \ THIRD_PARTY_CHIBICC_BINS = \
o/$(MODE)/third_party/chibicc/chibicc.com.dbg \ o/$(MODE)/third_party/chibicc/chibicc.com.dbg \
o/$(MODE)/third_party/chibicc/chibicc.com \ o/$(MODE)/third_party/chibicc/chibicc.com \
@ -33,9 +39,13 @@ THIRD_PARTY_CHIBICC_BINS = \
THIRD_PARTY_CHIBICC_A_OBJS = \ THIRD_PARTY_CHIBICC_A_OBJS = \
$(THIRD_PARTY_CHIBICC_A_SRCS:%=o/$(MODE)/%.zip.o) \ $(THIRD_PARTY_CHIBICC_A_SRCS:%=o/$(MODE)/%.zip.o) \
$(THIRD_PARTY_CHIBICC_A_SRCS:%.c=o/$(MODE)/%.o) $(THIRD_PARTY_CHIBICC_A_SRCS:%.c=o/$(MODE)/%.o)
THIRD_PARTY_CHIBICC2_A_OBJS = \
$(THIRD_PARTY_CHIBICC_A_SRCS:%=o/$(MODE)/%.zip.o) \
$(THIRD_PARTY_CHIBICC_A_SRCS:%.c=o/$(MODE)/%.chibicc.o)
THIRD_PARTY_CHIBICC_A_CHECKS = \ THIRD_PARTY_CHIBICC_A_CHECKS = \
$(THIRD_PARTY_CHIBICC_A).pkg \ $(THIRD_PARTY_CHIBICC_A).pkg \
$(THIRD_PARTY_CHIBICC2_A).pkg \
$(THIRD_PARTY_CHIBICC_A_HDRS:%=o/$(MODE)/%.ok) $(THIRD_PARTY_CHIBICC_A_HDRS:%=o/$(MODE)/%.ok)
THIRD_PARTY_CHIBICC_A_DIRECTDEPS = \ THIRD_PARTY_CHIBICC_A_DIRECTDEPS = \
@ -53,7 +63,9 @@ THIRD_PARTY_CHIBICC_A_DIRECTDEPS = \
LIBC_STUBS \ LIBC_STUBS \
LIBC_TIME \ LIBC_TIME \
LIBC_UNICODE \ LIBC_UNICODE \
LIBC_SYSV \
LIBC_X \ LIBC_X \
TOOL_BUILD_LIB \
THIRD_PARTY_COMPILER_RT \ THIRD_PARTY_COMPILER_RT \
THIRD_PARTY_DLMALLOC \ THIRD_PARTY_DLMALLOC \
THIRD_PARTY_GDTOA THIRD_PARTY_GDTOA
@ -65,39 +77,49 @@ $(THIRD_PARTY_CHIBICC_A): \
third_party/chibicc/ \ third_party/chibicc/ \
$(THIRD_PARTY_CHIBICC_A).pkg \ $(THIRD_PARTY_CHIBICC_A).pkg \
$(THIRD_PARTY_CHIBICC_A_OBJS) $(THIRD_PARTY_CHIBICC_A_OBJS)
$(THIRD_PARTY_CHIBICC_A).pkg: \ $(THIRD_PARTY_CHIBICC_A).pkg: \
$(THIRD_PARTY_CHIBICC_A_OBJS) \ $(THIRD_PARTY_CHIBICC_A_OBJS) \
$(foreach x,$(THIRD_PARTY_CHIBICC_A_DIRECTDEPS),$($(x)_A).pkg) $(foreach x,$(THIRD_PARTY_CHIBICC_A_DIRECTDEPS),$($(x)_A).pkg)
$(THIRD_PARTY_CHIBICC2_A): \
third_party/chibicc/ \
$(THIRD_PARTY_CHIBICC2_A).pkg \
$(THIRD_PARTY_CHIBICC2_A_OBJS)
$(THIRD_PARTY_CHIBICC2_A).pkg: \
$(THIRD_PARTY_CHIBICC2_A_OBJS) \
$(foreach x,$(THIRD_PARTY_CHIBICC_A_DIRECTDEPS),$($(x)_A).pkg)
o/$(MODE)/third_party/chibicc/chibicc.com.dbg: \ o/$(MODE)/third_party/chibicc/chibicc.com.dbg: \
$(THIRD_PARTY_CHIBICC_A_DEPS) \ $(THIRD_PARTY_CHIBICC_A_DEPS) \
$(THIRD_PARTY_CHIBICC_A) \ $(THIRD_PARTY_CHIBICC_A) \
$(APE) \ $(APE) \
$(CRT) \ $(CRT) \
o/$(MODE)/third_party/chibicc/chibicc.o \ o/$(MODE)/third_party/chibicc/main.o \
$(THIRD_PARTY_CHIBICC_A).pkg
@$(APELINK)
o/$(MODE)/third_party/chibicc/chibicc2.com.dbg: \
$(THIRD_PARTY_CHIBICC_A_DEPS) \
$(THIRD_PARTY_CHIBICC2_A) \
$(APE) \
$(CRT) \
o/$(MODE)/third_party/chibicc/main.chibicc.o \
$(THIRD_PARTY_CHIBICC2_A).pkg
@$(APELINK)
o/$(MODE)/third_party/chibicc/as.com.dbg: \
$(THIRD_PARTY_CHIBICC_A_DEPS) \
$(THIRD_PARTY_CHIBICC_A) \
$(APE) \
$(CRT) \
o/$(MODE)/third_party/chibicc/as.o \
$(THIRD_PARTY_CHIBICC_A).pkg $(THIRD_PARTY_CHIBICC_A).pkg
@$(APELINK) @$(APELINK)
o/$(MODE)/third_party/chibicc/chibicc2.com.dbg: \
$(THIRD_PARTY_CHIBICC_A_DEPS) \
$(THIRD_PARTY_CHIBICC_A_SRCS:%.c=o/$(MODE)/%.chibicc.o) \
$(THIRD_PARTY_CHIBICC_A).pkg \
$(CRT) \
$(APE)
@$(APELINK)
o/$(MODE)/third_party/chibicc/chibicc.o: \ o/$(MODE)/third_party/chibicc/chibicc.o: \
CPPFLAGS += \ CPPFLAGS += $(THIRD_PARTY_CHIBICC_DEFINES)
-DCRT=\"$(CRT)\" \
-DAPE=\"o/$(MODE)/ape/ape.o\" \
-DLDS=\"o/$(MODE)/ape/ape.lds\"
o/$(MODE)/third_party/chibicc/chibicc.chibicc.o: \ o/$(MODE)/third_party/chibicc/chibicc.chibicc.o: \
CHIBICC_FLAGS += \ CHIBICC_FLAGS += $(THIRD_PARTY_CHIBICC_DEFINES)
-DCRT=\"$(CRT)\" \
-DAPE=\"o/$(MODE)/ape/ape.o\" \
-DLDS=\"o/$(MODE)/ape/ape.lds\"
o/$(MODE)/%.chibicc.o: %.c o/$(MODE)/third_party/chibicc/chibicc.com.dbg o/$(MODE)/%.chibicc.o: %.c o/$(MODE)/third_party/chibicc/chibicc.com.dbg
@ACTION=CHIBICC TARGET=$@ build/do $(CHIBICC) $(CHIBICC_FLAGS) -c -o $@ $< @ACTION=CHIBICC TARGET=$@ build/do $(CHIBICC) $(CHIBICC_FLAGS) -c -o $@ $<

View File

@ -1770,9 +1770,9 @@ void gen_expr(Node *node) {
println("\tdiv\t%s", di); println("\tdiv\t%s", di);
} else { } else {
if (node->lhs->ty->size == 8) { if (node->lhs->ty->size == 8) {
emitlin("\tcqo"); emitlin("\tcqto");
} else { } else {
emitlin("\tcdq"); emitlin("\tcltd");
} }
println("\tidiv\t%s", di); println("\tidiv\t%s", di);
} }

5
third_party/chibicc/main.c vendored 100644
View File

@ -0,0 +1,5 @@
#include "third_party/chibicc/chibicc.h"
int main(int argc, char **argv) {
return chibicc(argc, argv);
}

View File

@ -5,6 +5,8 @@ char *main_filename1 = __FILE__;
int main_line1 = __LINE__; int main_line1 = __LINE__;
#define LINE() __LINE__ #define LINE() __LINE__
int main_line2 = LINE(); int main_line2 = LINE();
#define add2 add2
#define add6(a, b, c, d, e, f) add6(a, b, c, d, e, f)
# #

View File

@ -79,7 +79,6 @@ o/$(MODE)/third_party/chibicc/test/%.com.dbg: \
$(THIRD_PARTY_CHIBICC_TEST_A) \ $(THIRD_PARTY_CHIBICC_TEST_A) \
o/$(MODE)/third_party/chibicc/test/%.chibicc.o \ o/$(MODE)/third_party/chibicc/test/%.chibicc.o \
$(THIRD_PARTY_CHIBICC_TEST_A).pkg \ $(THIRD_PARTY_CHIBICC_TEST_A).pkg \
$(LIBC_TESTMAIN) \
$(CRT) \ $(CRT) \
$(APE) $(APE)
@$(APELINK) @$(APELINK)
@ -89,7 +88,6 @@ o/$(MODE)/third_party/chibicc/test/%2.com.dbg: \
$(THIRD_PARTY_CHIBICC_TEST2_A) \ $(THIRD_PARTY_CHIBICC_TEST2_A) \
o/$(MODE)/third_party/chibicc/test/%.chibicc2.o \ o/$(MODE)/third_party/chibicc/test/%.chibicc2.o \
$(THIRD_PARTY_CHIBICC_TEST2_A).pkg \ $(THIRD_PARTY_CHIBICC_TEST2_A).pkg \
$(LIBC_TESTMAIN) \
$(CRT) \ $(CRT) \
$(APE) $(APE)
@$(APELINK) @$(APELINK)

View File

@ -128,7 +128,7 @@ static int read_ident(char *start) {
for (;;) { for (;;) {
char *q; char *q;
c = decode_utf8(&q, p); c = decode_utf8(&q, p);
if (!('a' <= c && c <= 'f') && !is_ident2(c)) { if (!is_ident2(c)) {
return p - start; return p - start;
} }
p = q; p = q;
@ -142,13 +142,14 @@ static int from_hex(char c) {
} }
// Read a punctuator token from p and returns its length. // Read a punctuator token from p and returns its length.
static int read_punct(char *p) { int read_punct(char *p) {
static char *kw[] = {"<<=", ">>=", "...", "==", "!=", "<=", ">=", "->", static char kw[][4] = {"<<=", ">>=", "...", "==", "!=", "<=", ">=", "->",
"+=", "-=", "*=", "/=", "++", "--", "%=", "&=", "+=", "-=", "*=", "/=", "++", "--", "%=", "&=",
"|=", "^=", "&&", "||", "<<", ">>", "##"}; "|=", "^=", "&&", "||", "<<", ">>", "##"};
for (int i = 0; i < sizeof(kw) / sizeof(*kw); i++) { for (int i = 0; i < sizeof(kw) / sizeof(*kw); i++) {
if (startswith(p, kw[i])) { for (int j = 0;;) {
return strlen(kw[i]); if (p[j] != kw[i][j]) break;
if (!kw[i][++j]) return j;
} }
} }
return ispunct(*p) ? 1 : 0; return ispunct(*p) ? 1 : 0;
@ -181,7 +182,7 @@ static bool is_keyword(Token *tok) {
return hashmap_get2(&map, tok->loc, tok->len); return hashmap_get2(&map, tok->loc, tok->len);
} }
static int read_escaped_char(char **new_pos, char *p) { int read_escaped_char(char **new_pos, char *p) {
if ('0' <= *p && *p <= '7') { if ('0' <= *p && *p <= '7') {
// Read an octal number. // Read an octal number.
unsigned c = *p++ - '0'; unsigned c = *p++ - '0';
@ -592,7 +593,7 @@ Token *tokenize(File *file) {
} }
// Returns the contents of a given file. // Returns the contents of a given file.
static char *read_file(char *path) { char *read_file(char *path) {
FILE *fp; FILE *fp;
if (strcmp(path, "-") == 0) { if (strcmp(path, "-") == 0) {
// By convention, read from stdin if a given filename is "-". // By convention, read from stdin if a given filename is "-".
@ -639,7 +640,7 @@ File *new_file(char *name, int file_no, char *contents) {
} }
// Replaces \r or \r\n with \n. // Replaces \r or \r\n with \n.
static void canonicalize_newline(char *p) { void canonicalize_newline(char *p) {
int i = 0, j = 0; int i = 0, j = 0;
while (p[i]) { while (p[i]) {
if (p[i] == '\r' && p[i + 1] == '\n') { if (p[i] == '\r' && p[i + 1] == '\n') {
@ -656,7 +657,7 @@ static void canonicalize_newline(char *p) {
} }
// Removes backslashes followed by a newline. // Removes backslashes followed by a newline.
static void remove_backslash_newline(char *p) { void remove_backslash_newline(char *p) {
int i = 0, j = 0; int i = 0, j = 0;
// We want to keep the number of newline characters so that // We want to keep the number of newline characters so that
// the logical line number matches the physical one. // the logical line number matches the physical one.

View File

@ -61,8 +61,11 @@ uint32_t decode_utf8(char **new_pos, char *p) {
} }
static bool in_range(uint32_t *range, uint32_t c) { static bool in_range(uint32_t *range, uint32_t c) {
for (int i = 0; range[i] != -1; i += 2) for (int i = 0; range[i] != -1; i += 2) {
if (range[i] <= c && c <= range[i + 1]) return true; if (range[i] <= c && c <= range[i + 1]) {
return true;
}
}
return false; return false;
} }
@ -78,7 +81,6 @@ static bool in_range(uint32_t *range, uint32_t c) {
// (U+3000, full-width space) are allowed because they are out of range. // (U+3000, full-width space) are allowed because they are out of range.
bool is_ident1(uint32_t c) { bool is_ident1(uint32_t c) {
static uint32_t range[] = { static uint32_t range[] = {
'a', 'z', 'A', 'Z', '_', '_', '$', '$',
0x00A8, 0x00A8, 0x00AA, 0x00AA, 0x00AD, 0x00AD, 0x00AF, 0x00AF, 0x00A8, 0x00A8, 0x00AA, 0x00AA, 0x00AD, 0x00AD, 0x00AF, 0x00AF,
0x00B2, 0x00B5, 0x00B7, 0x00BA, 0x00BC, 0x00BE, 0x00C0, 0x00D6, 0x00B2, 0x00B5, 0x00B7, 0x00BA, 0x00BC, 0x00BE, 0x00C0, 0x00D6,
0x00D8, 0x00F6, 0x00F8, 0x00FF, 0x0100, 0x02FF, 0x0370, 0x167F, 0x00D8, 0x00F6, 0x00F8, 0x00FF, 0x0100, 0x02FF, 0x0370, 0x167F,
@ -93,17 +95,26 @@ bool is_ident1(uint32_t c) {
0xA0000, 0xAFFFD, 0xB0000, 0xBFFFD, 0xC0000, 0xCFFFD, 0xD0000, 0xDFFFD, 0xA0000, 0xAFFFD, 0xB0000, 0xBFFFD, 0xC0000, 0xCFFFD, 0xD0000, 0xDFFFD,
0xE0000, 0xEFFFD, -1, 0xE0000, 0xEFFFD, -1,
}; };
if (c < 128) {
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '_' ||
c == '$';
} else {
return in_range(range, c); return in_range(range, c);
}
} }
// Returns true if a given character is acceptable as a non-first // Returns true if a given character is acceptable as a non-first
// character of an identifier. // character of an identifier.
bool is_ident2(uint32_t c) { bool is_ident2(uint32_t c) {
static uint32_t range[] = { static uint32_t range[] = {
'0', '9', '$', '$', 0x0300, 0x036F, 0x1DC0, 0x0300, 0x036F, 0x1DC0, 0x1DFF, 0x20D0, 0x20FF, 0xFE20, 0xFE2F, -1,
0x1DFF, 0x20D0, 0x20FF, 0xFE20, 0xFE2F, -1,
}; };
return is_ident1(c) || in_range(range, c); if (is_ident1(c)) return true;
if (c < 128) {
return '0' <= c && c <= '9';
} else {
return in_range(range, c);
}
} }
// Returns the number of columns needed to display a given // Returns the number of columns needed to display a given

84
third_party/musl/tempnam.c vendored 100644
View File

@ -0,0 +1,84 @@
/*-*- 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
Musl Libc
Copyright © 2005-2014 Rich Felker, et al.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/struct/stat.h"
#include "libc/calls/struct/timespec.h"
#include "libc/errno.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/at.h"
#include "libc/sysv/consts/clock.h"
#include "libc/time/time.h"
#include "third_party/musl/tempnam.h"
#define MAXTRIES 100
asm(".ident\t\"\\n\\n\
Musl libc (MIT License)\\n\
Copyright 2005-2014 Rich Felker, et. al.\"");
asm(".include \"libc/disclaimer.inc\"");
static char *__randname(char *template) {
int i;
struct timespec ts;
unsigned long r;
clock_gettime(CLOCK_REALTIME, &ts);
r = ts.tv_nsec * 65537 ^ (uintptr_t)&ts / 16 + (uintptr_t) template;
for (i = 0; i < 6; i++, r >>= 5) template[i] = 'A' + (r & 15) + (r & 16) * 2;
return template;
}
/**
* Creates name for temporary file.
*/
char *tempnam(const char *dir, const char *pfx) {
int i, r;
char s[PATH_MAX];
size_t l, dl, pl;
if (!dir) dir = kTmpPath;
if (!pfx) pfx = "temp";
dl = strlen(dir);
pl = strlen(pfx);
l = dl + 1 + pl + 1 + 6;
if (l >= PATH_MAX) {
errno = ENAMETOOLONG;
return 0;
}
memcpy(s, dir, dl);
s[dl] = '/';
memcpy(s + dl + 1, pfx, pl);
s[dl + 1 + pl] = '_';
s[l] = 0;
for (i = 0; i < MAXTRIES; i++) {
__randname(s + l - 6);
r = fstatat(AT_FDCWD, s, &(struct stat){0}, AT_SYMLINK_NOFOLLOW);
if (r == -ENOENT) return strdup(s);
}
return 0;
}

10
third_party/musl/tempnam.h vendored 100644
View File

@ -0,0 +1,10 @@
#ifndef COSMOPOLITAN_THIRD_PARTY_MUSL_TEMPNAM_H_
#define COSMOPOLITAN_THIRD_PARTY_MUSL_TEMPNAM_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
char *tempnam(const char *, const char *);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_THIRD_PARTY_MUSL_TEMPNAM_H_ */

View File

@ -2497,7 +2497,8 @@ static void Exec(void) {
KeepGoing: KeepGoing:
CheckFramePointer(); CheckFramePointer();
if (action & ALARM) { if (action & ALARM) {
DrawDisplayOnly(&pan.display); /* TODO(jart): Fix me */
/* DrawDisplayOnly(&pan.display); */
action &= ~ALARM; action &= ~ALARM;
} }
if (action & EXIT) { if (action & EXIT) {

View File

@ -53,12 +53,12 @@ TOOL_BUILD_DIRECTDEPS = \
LIBC_TINYMATH \ LIBC_TINYMATH \
LIBC_UNICODE \ LIBC_UNICODE \
LIBC_X \ LIBC_X \
TOOL_BUILD_LIB \
THIRD_PARTY_COMPILER_RT \ THIRD_PARTY_COMPILER_RT \
THIRD_PARTY_GDTOA \ THIRD_PARTY_GDTOA \
THIRD_PARTY_GETOPT \ THIRD_PARTY_GETOPT \
THIRD_PARTY_XED \ THIRD_PARTY_XED \
THIRD_PARTY_ZLIB \ THIRD_PARTY_ZLIB \
TOOL_BUILD_LIB \
THIRD_PARTY_STB THIRD_PARTY_STB
TOOL_BUILD_DEPS := \ TOOL_BUILD_DEPS := \

View File

@ -129,7 +129,7 @@ static size_t FlushStrtab(struct ElfWriter *elf, const char *name,
struct Interner *strtab) { struct Interner *strtab) {
size_t size = strtab->i * sizeof(strtab->p[0]); size_t size = strtab->i * sizeof(strtab->p[0]);
elfwriter_align(elf, 1, 0); elfwriter_align(elf, 1, 0);
AppendSection(elf, ".strtab", SHT_STRTAB, 0); AppendSection(elf, name, SHT_STRTAB, 0);
mempcpy(elfwriter_reserve(elf, size), strtab->p, size); mempcpy(elfwriter_reserve(elf, size), strtab->p, size);
elfwriter_commit(elf, size); elfwriter_commit(elf, size);
return FinishSection(elf); return FinishSection(elf);
@ -172,6 +172,9 @@ struct ElfWriter *elfwriter_open(const char *path, int mode) {
elf->ehdr = memcpy(elf->map, &kObjHeader, (elf->wrote = sizeof(kObjHeader))); elf->ehdr = memcpy(elf->map, &kObjHeader, (elf->wrote = sizeof(kObjHeader)));
elf->strtab = newinterner(); elf->strtab = newinterner();
elf->shstrtab = newinterner(); elf->shstrtab = newinterner();
intern(elf->strtab, "");
intern(elf->shstrtab, "");
intern(elf->shstrtab, ".shstrtab");
return elf; return elf;
} }

View File

@ -22,6 +22,7 @@
void elfwriter_cargoculting(struct ElfWriter *elf) { void elfwriter_cargoculting(struct ElfWriter *elf) {
elfwriter_startsection(elf, "", SHT_NULL, 0); elfwriter_startsection(elf, "", SHT_NULL, 0);
elfwriter_finishsection(elf);
elfwriter_startsection(elf, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR); elfwriter_startsection(elf, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
elfwriter_finishsection(elf); elfwriter_finishsection(elf);
elfwriter_startsection(elf, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE); elfwriter_startsection(elf, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);

View File

@ -1386,7 +1386,7 @@ int main(int argc, char *argv[]) {
return 1; return 1;
} }
} }
if (0 && IsWindows()) { if (IsWindows()) {
Gui(); Gui();
} else { } else {
Tui(); Tui();