Add stage2 build

This commit is contained in:
Rui Ueyama 2019-08-24 12:08:12 +09:00
parent 9bf96124ba
commit 5d15431df1
4 changed files with 119 additions and 4 deletions

1
.gitignore vendored
View File

@ -6,3 +6,4 @@
/tmp*
/chibicc
/test/*.exe
/stage2

View File

@ -6,6 +6,8 @@ OBJS=$(SRCS:.c=.o)
TEST_SRCS=$(wildcard test/*.c)
TESTS=$(TEST_SRCS:.c=.exe)
# Stage 1
chibicc: $(OBJS)
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
@ -17,10 +19,33 @@ test/%.exe: chibicc test/%.c
test: $(TESTS)
for i in $^; do echo $$i; ./$$i || exit 1; echo; done
test/driver.sh
test/driver.sh ./chibicc
test-all: test test-stage2
# Stage 2
stage2/chibicc: $(OBJS:%=stage2/%)
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
stage2/%.s: chibicc self.py %.c
mkdir -p stage2/test
./self.py chibicc.h $*.c > stage2/$*.c
./chibicc -o stage2/$*.s stage2/$*.c
stage2/test/%.exe: stage2/chibicc test/%.c
mkdir -p stage2/test
$(CC) -o- -E -P -C test/$*.c | ./stage2/chibicc -o stage2/test/$*.s -
$(CC) -o $@ stage2/test/$*.s -xc test/common
test-stage2: $(TESTS:test/%=stage2/test/%)
for i in $^; do echo $$i; ./$$i || exit 1; echo; done
test/driver.sh ./stage2/chibicc
# Misc.
clean:
rm -rf chibicc tmp* $(TESTS) test/*.s test/*.exe
rm -rf chibicc tmp* $(TESTS) test/*.s test/*.exe stage2
find * -type f '(' -name '*~' -o -name '*.o' ')' -exec rm {} ';'
.PHONY: test clean
.PHONY: test clean test-stage2

87
self.py Executable file
View File

@ -0,0 +1,87 @@
#!/usr/bin/python3
import re
import sys
print("""
typedef signed char int8_t;
typedef short int16_t;
typedef int int32_t;
typedef long int64_t;
typedef unsigned long size_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long uint64_t;
typedef struct FILE FILE;
extern FILE *stdin;
extern FILE *stdout;
extern FILE *stderr;
typedef struct {
int gp_offset;
int fp_offset;
void *overflow_arg_area;
void *reg_save_area;
} __va_elem;
typedef __va_elem va_list[1];
struct stat {
char _[512];
};
void *malloc(long size);
void *calloc(long nmemb, long size);
void *realloc(void *buf, long size);
int *__errno_location();
char *strerror(int errnum);
FILE *fopen(char *pathname, char *mode);
FILE *open_memstream(char **ptr, size_t *sizeloc);
long fread(void *ptr, long size, long nmemb, FILE *stream);
size_t fwrite(void *ptr, size_t size, size_t nmemb, FILE *stream);
int fflush(FILE *stream);
int fclose(FILE *fp);
int fputc(int c, FILE *stream);
int feof(FILE *stream);
static void assert() {}
int strcmp(char *s1, char *s2);
int strncasecmp(char *s1, char *s2, long n);
int memcmp(char *s1, char *s2, long n);
int printf(char *fmt, ...);
int sprintf(char *buf, char *fmt, ...);
int fprintf(FILE *fp, char *fmt, ...);
int vfprintf(FILE *fp, char *fmt, va_list ap);
long strlen(char *p);
int strncmp(char *p, char *q, long n);
void *memcpy(char *dst, char *src, long n);
char *strdup(char *p);
char *strndup(char *p, long n);
int isspace(int c);
int ispunct(int c);
int isdigit(int c);
int isxdigit(int c);
char *strstr(char *haystack, char *needle);
char *strchr(char *s, int c);
double strtod(char *nptr, char **endptr);
static void va_end(va_list ap) {}
long strtoul(char *nptr, char **endptr, int base);
void exit(int code);
""")
for path in sys.argv[1:]:
with open(path) as file:
s = file.read()
s = re.sub(r'\\\n', '', s)
s = re.sub(r'^\s*#.*', '', s, flags=re.MULTILINE)
s = re.sub(r'"\n\s*"', '', s)
s = re.sub(r'\bbool\b', '_Bool', s)
s = re.sub(r'\berrno\b', '*__errno_location()', s)
s = re.sub(r'\btrue\b', '1', s)
s = re.sub(r'\bfalse\b', '0', s)
s = re.sub(r'\bNULL\b', '0', s)
s = re.sub(r'\bva_start\(([^)]*),([^)]*)\)', '*(\\1)=*(__va_elem*)__va_area__', s)
s = re.sub(r'\bunreachable\(\)', 'error("unreachable")', s)
s = re.sub(r'\bMIN\(([^)]*),([^)]*)\)', '((\\1)<(\\2)?(\\1):(\\2))', s)
print(s)

View File

@ -1,4 +1,6 @@
#!/bin/bash
chibicc=$1
tmp=`mktemp -d /tmp/chibicc-test-XXXXXX`
trap 'rm -rf $tmp' INT TERM HUP EXIT
echo > $tmp/empty.c
@ -19,7 +21,7 @@ rm -f $tmp/out
check -o
# --help
./chibicc --help 2>&1 | grep -q chibicc
$chibicc --help 2>&1 | grep -q chibicc
check --help
echo OK