forked from rui314/chibicc
-
Notifications
You must be signed in to change notification settings - Fork 5
/
self.sh
executable file
·73 lines (61 loc) · 1.72 KB
/
self.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/bin/bash -x
TMP=tmp-self
mkdir -p $TMP
expand() {
file=$1
# FIXME: these definitions won't compile for uxn. is trying to run chibicc
# for uxn in uxn even realistic? hehe
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);
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);
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) {}
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
sed -i 's/INT_MAX/2147483647/g' $TMP/$1
./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
expand parse.c
expand codegen.c
expand tokenize.c
gcc -static -o chibicc-gen2 $TMP/*.o