Skip to content

Commit

Permalink
refactor: set up for self-host testing
Browse files Browse the repository at this point in the history
  • Loading branch information
diohabara committed Aug 2, 2023
1 parent f80b4e7 commit ff63908
Show file tree
Hide file tree
Showing 8 changed files with 325 additions and 608 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ tmp*
*.vsix

# for debug
test
.vscode/
tests
ext
test.*
test.s
ext.*
.gdb_history

# stages
stage1/
stage2/
stage3/
Makefile
14 changes: 0 additions & 14 deletions .vscode/settings.json

This file was deleted.

73 changes: 41 additions & 32 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,12 @@ lint:
clang-format -i ./*.c
clang-format -i ./*.h

lintx:
docker run --platform=linux/amd64 -v "${PWD}:/src" -w/src ccc make lint

test: ccc
./ccc tests > tmp.s
echo 'int char_fn() { return 257; }' | gcc -xc -c -o tmp2.o -
echo 'int ext1 = 5; int *ext2 = &ext1;' | gcc -xc -c -o tmp3.o -
echo 'int ext_fn1(int x) { return x; }; int ext_fn2(int x) { return x;}' | gcc -xc -c -o tmp4.o -
gcc -static -o tmp tmp.s tmp2.o tmp3.o tmp4.o
./ccc test/test.c > tmp.s
cat test/helper.c | gcc -xc -c -o tmp2.o -
gcc -static -o tmp tmp.s tmp2.o
./tmp

testx:
docker run --platform=linux/amd64 -v "${PWD}:/src" -w/src ccc make test

clean:
rm -f ccc *.o *~ tmp* stage*/*

Expand All @@ -43,25 +35,42 @@ clean:
# stage2: ccc on X86-64 machine generated by stage1
# stage3: ccc on X86-64 machine generated by stage2

STAGES = stage1 stage2 stage3

define PROCESS_STAGE_RULE
$(1)/%.c: %.c
mkdir -p $(1)
gcc -E -P $$< -o $$@
endef

$(foreach stage,$(STAGES),$(eval $(call PROCESS_STAGE_RULE,$(stage))))

PROCESSED_SRCS = $(foreach stage,$(STAGES),$(addprefix $(stage)/,$(SRCS)))

process: $(PROCESSED_SRCS)

ccc-stage1: $(OBJS)
$(CC) -o $@ $(OBJS) $(CFLAGS)

ccc-stage2:
@echo TODO

ccc-stage3:
@echo TODO
@rm -rf stage1
@mkdir stage1
$(CC) -o stage1/$@ $(OBJS) $(CFLAGS)

ccc-stage2: ccc-stage1 $(SRCS)
@rm -rf stage2
@mkdir stage2
$(CC) -D__ccc_self__ -E -P codegen.c -o stage2/codegen.c
$(CC) -D__ccc_self__ -E -P main.c -o stage2/main.c
$(CC) -D__ccc_self__ -E -P parse.c -o stage2/parse.c
$(CC) -D__ccc_self__ -E -P tokenize.c -o stage2/tokenize.c
$(CC) -D__ccc_self__ -E -P type.c -o stage2/type.c
stage1/ccc-stage1 stage2/codegen.c > stage2/codegen.s
stage1/ccc-stage1 stage2/main.c > stage2/main.s
stage1/ccc-stage1 stage2/parse.c > stage2/parse.s
stage1/ccc-stage1 stage2/tokenize.c > stage2/tokenize.s
stage1/ccc-stage1 stage2/type.c > stage2/type.s
$(CC) -o stage2/$@

ccc-stage3: ccc-stage2 $(SRCS)
@rm -rf stage3
@mkdir stage3
$(CC) -D__ccc_self__ -E -P type.c -o stage3/type.c
$(CC) -D__ccc_self__ -E -P codegen.c -o stage3/codegen.c
$(CC) -D__ccc_self__ -E -P main.c -o stage3/main.c
$(CC) -D__ccc_self__ -E -P parse.c -o stage3/parse.c
$(CC) -D__ccc_self__ -E -P tokenize.c -o stage3/tokenize.c
stage2/ccc-stage2 stage3/codegen.c > stage3/codegen.s
stage2/ccc-stage2 stage3/main.c > stage3/main.s
stage2/ccc-stage2 stage3/parse.c > stage3/parse.s
stage2/ccc-stage2 stage3/tokenize.c > stage3/tokenize.s
stage2/ccc-stage2 stage3/type.c > stage3/type.s
$(CC) -o stage3/$@

test-self-host: ccc-stage1 ccc-stage2 ccc-stage3
strip stage2/ccc-stage2
strip stage3/ccc-stage3
diff -s stage2/ccc-stage2 stage3/ccc-stage3
63 changes: 63 additions & 0 deletions ccc.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,64 @@
#ifdef __ccc_self__

typedef int bool;
typedef int size_t;
typedef void FILE;
typedef void *va_list;

#define SEEK_SET 0
#define SEEK_END 2

struct _reent {
int _errno;
void *_stdin;
void *_stdout;
void *_stderr;
};

extern struct _reent *_impure_ptr;

#define stdin (_impure_ptr->_stdin)
#define stderr (_impure_ptr->_stderr)

extern int errno;

#define __attribute__(x)
#define noreturn

#define NULL (0)
#define true (1)
#define false (0)
#define static

#define assert(x) 1

int printf();
int fopen();
int strncmp();
int strlen();
int fprintf();
char *strerror();
int vsnprintf();
int vfprintf();
void *calloc();
int isalnum();
int isspace();
int snprintf();
int strstr();
int strchr();
int isdigit();
int strtol();
void exit();
int fseek();
int ftell();
int fread();
int fclose();
int realloc();
int ferror();
int feof();

#else

#include <assert.h>
#include <ctype.h>
#include <errno.h>
Expand All @@ -7,6 +68,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#endif
typedef struct Type Type;
typedef struct Member Member;
typedef struct Initializer Initializer;
Expand Down
Loading

0 comments on commit ff63908

Please sign in to comment.