mirror of https://github.com/rui314/chibicc
Compile an integer to an exectuable that exits with the given number
This commit is contained in:
commit
82171f9ec3
|
@ -0,0 +1,5 @@
|
|||
*~
|
||||
*.o
|
||||
tmp*
|
||||
a.out
|
||||
chibicc
|
|
@ -0,0 +1,12 @@
|
|||
CFLAGS=-std=c11 -g -static -fno-common
|
||||
|
||||
chibicc: main.o
|
||||
$(CC) -o $@ $? $(LDFLAGS)
|
||||
|
||||
test: chibicc
|
||||
./test.sh
|
||||
|
||||
clean:
|
||||
rm -f chibicc *.o *~ tmp*
|
||||
|
||||
.PHONY: test clean
|
|
@ -0,0 +1,16 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "%s: invalid number of arguments\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf(".intel_syntax noprefix\n");
|
||||
printf(".global main\n");
|
||||
printf("main:\n");
|
||||
printf(" mov rax, %d\n", atoi(argv[1]));
|
||||
printf(" ret\n");
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
#!/bin/bash
|
||||
assert() {
|
||||
expected="$1"
|
||||
input="$2"
|
||||
|
||||
./chibicc "$input" > tmp.s
|
||||
gcc -static -o tmp tmp.s
|
||||
./tmp
|
||||
actual="$?"
|
||||
|
||||
if [ "$actual" = "$expected" ]; then
|
||||
echo "$input => $actual"
|
||||
else
|
||||
echo "$input => $expected expected, but got $actual"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
assert 0 0
|
||||
assert 42 42
|
||||
|
||||
echo OK
|
Loading…
Reference in New Issue