Make minor improvements

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

1642
third_party/chibicc/as.c vendored Normal file

File diff suppressed because it is too large Load Diff

View File

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

View File

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

View File

@ -120,6 +120,10 @@ Token *tokenize_string_literal(Token *, Type *);
bool consume(Token **, Token *, char *, size_t);
bool equal(Token *, char *, size_t);
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 EQUAL(T, S) equal(T, S, strlen(S))
@ -563,6 +567,8 @@ extern bool opt_sse4;
extern bool opt_verbose;
extern char *base_file;
int chibicc(int, char **);
//
// alloc.c
//

View File

@ -20,10 +20,16 @@ PKGS += THIRD_PARTY_CHIBICC
THIRD_PARTY_CHIBICC_ARTIFACTS += 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_CHIBICC2_A = o/$(MODE)/third_party/chibicc/chibicc2.a
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_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 = \
o/$(MODE)/third_party/chibicc/chibicc.com.dbg \
o/$(MODE)/third_party/chibicc/chibicc.com \
@ -33,9 +39,13 @@ THIRD_PARTY_CHIBICC_BINS = \
THIRD_PARTY_CHIBICC_A_OBJS = \
$(THIRD_PARTY_CHIBICC_A_SRCS:%=o/$(MODE)/%.zip.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).pkg \
$(THIRD_PARTY_CHIBICC2_A).pkg \
$(THIRD_PARTY_CHIBICC_A_HDRS:%=o/$(MODE)/%.ok)
THIRD_PARTY_CHIBICC_A_DIRECTDEPS = \
@ -53,7 +63,9 @@ THIRD_PARTY_CHIBICC_A_DIRECTDEPS = \
LIBC_STUBS \
LIBC_TIME \
LIBC_UNICODE \
LIBC_SYSV \
LIBC_X \
TOOL_BUILD_LIB \
THIRD_PARTY_COMPILER_RT \
THIRD_PARTY_DLMALLOC \
THIRD_PARTY_GDTOA
@ -65,39 +77,49 @@ $(THIRD_PARTY_CHIBICC_A): \
third_party/chibicc/ \
$(THIRD_PARTY_CHIBICC_A).pkg \
$(THIRD_PARTY_CHIBICC_A_OBJS)
$(THIRD_PARTY_CHIBICC_A).pkg: \
$(THIRD_PARTY_CHIBICC_A_OBJS) \
$(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: \
$(THIRD_PARTY_CHIBICC_A_DEPS) \
$(THIRD_PARTY_CHIBICC_A) \
$(APE) \
$(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
@$(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: \
CPPFLAGS += \
-DCRT=\"$(CRT)\" \
-DAPE=\"o/$(MODE)/ape/ape.o\" \
-DLDS=\"o/$(MODE)/ape/ape.lds\"
CPPFLAGS += $(THIRD_PARTY_CHIBICC_DEFINES)
o/$(MODE)/third_party/chibicc/chibicc.chibicc.o: \
CHIBICC_FLAGS += \
-DCRT=\"$(CRT)\" \
-DAPE=\"o/$(MODE)/ape/ape.o\" \
-DLDS=\"o/$(MODE)/ape/ape.lds\"
CHIBICC_FLAGS += $(THIRD_PARTY_CHIBICC_DEFINES)
o/$(MODE)/%.chibicc.o: %.c o/$(MODE)/third_party/chibicc/chibicc.com.dbg
@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);
} else {
if (node->lhs->ty->size == 8) {
emitlin("\tcqo");
emitlin("\tcqto");
} else {
emitlin("\tcdq");
emitlin("\tcltd");
}
println("\tidiv\t%s", di);
}

5
third_party/chibicc/main.c vendored Normal file
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__;
#define LINE() __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) \
o/$(MODE)/third_party/chibicc/test/%.chibicc.o \
$(THIRD_PARTY_CHIBICC_TEST_A).pkg \
$(LIBC_TESTMAIN) \
$(CRT) \
$(APE)
@$(APELINK)
@ -89,7 +88,6 @@ o/$(MODE)/third_party/chibicc/test/%2.com.dbg: \
$(THIRD_PARTY_CHIBICC_TEST2_A) \
o/$(MODE)/third_party/chibicc/test/%.chibicc2.o \
$(THIRD_PARTY_CHIBICC_TEST2_A).pkg \
$(LIBC_TESTMAIN) \
$(CRT) \
$(APE)
@$(APELINK)

View File

@ -128,7 +128,7 @@ static int read_ident(char *start) {
for (;;) {
char *q;
c = decode_utf8(&q, p);
if (!('a' <= c && c <= 'f') && !is_ident2(c)) {
if (!is_ident2(c)) {
return p - start;
}
p = q;
@ -142,13 +142,14 @@ static int from_hex(char c) {
}
// Read a punctuator token from p and returns its length.
static int read_punct(char *p) {
static char *kw[] = {"<<=", ">>=", "...", "==", "!=", "<=", ">=", "->",
"+=", "-=", "*=", "/=", "++", "--", "%=", "&=",
"|=", "^=", "&&", "||", "<<", ">>", "##"};
int read_punct(char *p) {
static char kw[][4] = {"<<=", ">>=", "...", "==", "!=", "<=", ">=", "->",
"+=", "-=", "*=", "/=", "++", "--", "%=", "&=",
"|=", "^=", "&&", "||", "<<", ">>", "##"};
for (int i = 0; i < sizeof(kw) / sizeof(*kw); i++) {
if (startswith(p, kw[i])) {
return strlen(kw[i]);
for (int j = 0;;) {
if (p[j] != kw[i][j]) break;
if (!kw[i][++j]) return j;
}
}
return ispunct(*p) ? 1 : 0;
@ -181,7 +182,7 @@ static bool is_keyword(Token *tok) {
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') {
// Read an octal number.
unsigned c = *p++ - '0';
@ -592,7 +593,7 @@ Token *tokenize(File *file) {
}
// Returns the contents of a given file.
static char *read_file(char *path) {
char *read_file(char *path) {
FILE *fp;
if (strcmp(path, "-") == 0) {
// 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.
static void canonicalize_newline(char *p) {
void canonicalize_newline(char *p) {
int i = 0, j = 0;
while (p[i]) {
if (p[i] == '\r' && p[i + 1] == '\n') {
@ -656,7 +657,7 @@ static void canonicalize_newline(char *p) {
}
// Removes backslashes followed by a newline.
static void remove_backslash_newline(char *p) {
void remove_backslash_newline(char *p) {
int i = 0, j = 0;
// We want to keep the number of newline characters so that
// 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) {
for (int i = 0; range[i] != -1; i += 2)
if (range[i] <= c && c <= range[i + 1]) return true;
for (int i = 0; range[i] != -1; i += 2) {
if (range[i] <= c && c <= range[i + 1]) {
return true;
}
}
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.
bool is_ident1(uint32_t c) {
static uint32_t range[] = {
'a', 'z', 'A', 'Z', '_', '_', '$', '$',
0x00A8, 0x00A8, 0x00AA, 0x00AA, 0x00AD, 0x00AD, 0x00AF, 0x00AF,
0x00B2, 0x00B5, 0x00B7, 0x00BA, 0x00BC, 0x00BE, 0x00C0, 0x00D6,
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,
0xE0000, 0xEFFFD, -1,
};
return in_range(range, c);
if (c < 128) {
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '_' ||
c == '$';
} else {
return in_range(range, c);
}
}
// Returns true if a given character is acceptable as a non-first
// character of an identifier.
bool is_ident2(uint32_t c) {
static uint32_t range[] = {
'0', '9', '$', '$', 0x0300, 0x036F, 0x1DC0,
0x1DFF, 0x20D0, 0x20FF, 0xFE20, 0xFE2F, -1,
0x0300, 0x036F, 0x1DC0, 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

84
third_party/musl/tempnam.c vendored Normal file
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 Normal file
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_ */