Skip to content

Commit 0522e2d

Browse files
committed
Compile an integer to an exectuable that exits with the given number
0 parents  commit 0522e2d

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
**/*~
2+
**/\#*
3+
**/*.o
4+
**/*.s
5+
**/a.out
6+
/tmp*
7+
/chibicc

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CFLAGS=-std=c11 -g -fno-common
2+
3+
chibicc: main.o
4+
$(CC) -o chibicc main.o $(LDFLAGS)
5+
6+
test: chibicc
7+
./test.sh
8+
9+
clean:
10+
rm -f chibicc *.o *~ tmp*
11+
12+
.PHONY: test clean

main.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
int main(int argc, char **argv) {
5+
if (argc != 2) {
6+
fprintf(stderr, "%s: invalid number of arguments\n", argv[0]);
7+
return 1;
8+
}
9+
10+
printf(" .globl main\n");
11+
printf("main:\n");
12+
printf(" mov $%d, %%rax\n", atoi(argv[1]));
13+
printf(" ret\n");
14+
return 0;
15+
}

test.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
assert() {
3+
expected="$1"
4+
input="$2"
5+
6+
./chibicc "$input" > tmp.s || exit
7+
gcc -static -o tmp tmp.s
8+
./tmp
9+
actual="$?"
10+
11+
if [ "$actual" = "$expected" ]; then
12+
echo "$input => $actual"
13+
else
14+
echo "$input => $expected expected, but got $actual"
15+
exit 1
16+
fi
17+
}
18+
19+
assert 0 0
20+
assert 42 42
21+
22+
echo OK

0 commit comments

Comments
 (0)