diff --git a/.gitignore b/.gitignore index b27256c..20aa13d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ tmp* a.out chibicc +chibicc-gen* diff --git a/Makefile b/Makefile index 5c7dfaf..5b0986d 100644 --- a/Makefile +++ b/Makefile @@ -7,15 +7,23 @@ chibicc: $(OBJS) $(OBJS): chibi.h -test: chibicc +chibicc-gen2: chibicc $(SRCS) chibi.h + ./self.sh + +extern.o: tests-extern + gcc -xc -c -o extern.o tests-extern + +test: chibicc extern.o ./chibicc tests > tmp.s - echo 'int ext1; int *ext2; int char_fn() { return 257; }' \ - 'int static_fn() { return 5; }' | \ - gcc -xc -c -o tmp2.o - - gcc -static -o tmp tmp.s tmp2.o + gcc -static -o tmp tmp.s extern.o + ./tmp + +test-gen2: chibicc-gen2 extern.o + ./chibicc-gen2 tests > tmp.s + gcc -static -o tmp tmp.s extern.o ./tmp clean: - rm -f chibicc *.o *~ tmp* + rm -rf chibicc chibicc-gen* *.o *~ tmp* .PHONY: test clean diff --git a/self.sh b/self.sh new file mode 100755 index 0000000..a4608e2 --- /dev/null +++ b/self.sh @@ -0,0 +1,44 @@ +#!/bin/bash -x +TMP=tmp-self + +mkdir -p $TMP + +expand() { + file=$1 + cat < $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); +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/, \.\.\.//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 + +gcc -static -o chibicc-gen2 $TMP/*.o diff --git a/tests-extern b/tests-extern new file mode 100644 index 0000000..8b93f1f --- /dev/null +++ b/tests-extern @@ -0,0 +1,5 @@ +// -*- c -*- +int ext1; +int *ext2; + +int static_fn() { return 5; }