mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-27 04:50:28 +00:00
Make minor improvements
This commit is contained in:
parent
1fc91f3580
commit
b562d6fdb3
41 changed files with 1948 additions and 92 deletions
1642
third_party/chibicc/as.c
vendored
Normal file
1642
third_party/chibicc/as.c
vendored
Normal file
File diff suppressed because it is too large
Load diff
4
third_party/chibicc/asm.c
vendored
4
third_party/chibicc/asm.c
vendored
|
@ -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;
|
||||
|
|
2
third_party/chibicc/chibicc.c
vendored
2
third_party/chibicc/chibicc.c
vendored
|
@ -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();
|
||||
|
|
6
third_party/chibicc/chibicc.h
vendored
6
third_party/chibicc/chibicc.h
vendored
|
@ -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
|
||||
//
|
||||
|
|
58
third_party/chibicc/chibicc.mk
vendored
58
third_party/chibicc/chibicc.mk
vendored
|
@ -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 $@ $<
|
||||
|
|
4
third_party/chibicc/codegen.c
vendored
4
third_party/chibicc/codegen.c
vendored
|
@ -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
5
third_party/chibicc/main.c
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
#include "third_party/chibicc/chibicc.h"
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
return chibicc(argc, argv);
|
||||
}
|
2
third_party/chibicc/test/macro_test.c
vendored
2
third_party/chibicc/test/macro_test.c
vendored
|
@ -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)
|
||||
|
||||
#
|
||||
|
||||
|
|
2
third_party/chibicc/test/test.mk
vendored
2
third_party/chibicc/test/test.mk
vendored
|
@ -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)
|
||||
|
|
23
third_party/chibicc/tokenize.c
vendored
23
third_party/chibicc/tokenize.c
vendored
|
@ -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.
|
||||
|
|
25
third_party/chibicc/unicode.c
vendored
25
third_party/chibicc/unicode.c
vendored
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue