2019-08-24 06:08:12 +03:00
|
|
|
#!/bin/bash -x
|
|
|
|
TMP=tmp-self
|
|
|
|
|
|
|
|
mkdir -p $TMP
|
|
|
|
|
|
|
|
expand() {
|
|
|
|
file=$1
|
|
|
|
cat <<EOF > $TMP/$1
|
|
|
|
typedef struct FILE FILE;
|
|
|
|
extern FILE *stdout;
|
|
|
|
extern FILE *stderr;
|
|
|
|
|
|
|
|
void *malloc(long size);
|
|
|
|
void *calloc(long nmemb, long size);
|
|
|
|
int *__errno_location();
|
|
|
|
char *strerror(int errnum);
|
|
|
|
FILE *fopen(char *pathname, char *mode);
|
|
|
|
long fread(void *ptr, long size, long nmemb, FILE *stream);
|
|
|
|
int feof(FILE *stream);
|
|
|
|
static void assert() {}
|
|
|
|
int strcmp(char *s1, char *s2);
|
2019-08-24 10:10:51 +03:00
|
|
|
int printf(char *fmt, ...);
|
|
|
|
int sprintf(char *buf, char *fmt, ...);
|
|
|
|
long strlen(char *p);
|
|
|
|
int strncmp(char *p, char *q);
|
|
|
|
void *memcpy(char *dst, char *src, long n);
|
|
|
|
char *strndup(char *p, long n);
|
|
|
|
int isspace(int c);
|
|
|
|
char *strstr(char *haystack, char *needle);
|
|
|
|
long strtol(char *nptr, char **endptr, int base);
|
2019-08-25 05:48:44 +03:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int gp_offset;
|
|
|
|
int fp_offset;
|
|
|
|
void *overflow_arg_area;
|
|
|
|
void *reg_save_area;
|
|
|
|
} __va_elem;
|
|
|
|
|
|
|
|
typedef __va_elem va_list[1];
|
|
|
|
|
|
|
|
static void va_start(__va_elem *ap) {
|
|
|
|
__builtin_va_start(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void va_end(__va_elem *ap) {}
|
2019-08-24 06:08:12 +03:00
|
|
|
EOF
|
|
|
|
|
|
|
|
grep -v '^#' chibi.h >> $TMP/$1
|
|
|
|
grep -v '^#' $1 >> $TMP/$1
|
|
|
|
sed -i 's/\bbool\b/_Bool/g' $TMP/$1
|
|
|
|
sed -i 's/\berrno\b/*__errno_location()/g' $TMP/$1
|
|
|
|
sed -i 's/\btrue\b/1/g; s/\bfalse\b/0/g;' $TMP/$1
|
|
|
|
sed -i 's/\bNULL\b/0/g' $TMP/$1
|
2019-08-24 10:10:51 +03:00
|
|
|
sed -i 's/INT_MAX/2147483647/g' $TMP/$1
|
2019-08-24 06:08:12 +03:00
|
|
|
|
|
|
|
./chibicc $TMP/$1 > $TMP/${1%.c}.s
|
|
|
|
gcc -c -o $TMP/${1%.c}.o $TMP/${1%.c}.s
|
|
|
|
}
|
|
|
|
|
|
|
|
cp *.c $TMP
|
|
|
|
for i in $TMP/*.c; do
|
|
|
|
gcc -I. -c -o ${i%.c}.o $i
|
|
|
|
done
|
|
|
|
|
|
|
|
expand main.c
|
|
|
|
expand type.c
|
2019-08-24 10:10:51 +03:00
|
|
|
expand parse.c
|
|
|
|
expand codegen.c
|
2019-08-25 12:00:17 +03:00
|
|
|
expand tokenize.c
|
2019-08-24 06:08:12 +03:00
|
|
|
|
|
|
|
gcc -static -o chibicc-gen2 $TMP/*.o
|