Skip to content

Commit

Permalink
A very simple compiler that comiles an integer.
Browse files Browse the repository at this point in the history
  • Loading branch information
moizumi99 committed Nov 10, 2018
0 parents commit b32144f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
*~
*.o
tmp*
9cc
16 changes: 16 additions & 0 deletions 9cc.c
@@ -0,0 +1,16 @@
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Arguments number not right.\n");
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;
}
7 changes: 7 additions & 0 deletions Makefile
@@ -0,0 +1,7 @@
9cc: 9cc.c

test: 9cc
./test.sh

clean:
rm -f 9cc *.o *~ tmp*
21 changes: 21 additions & 0 deletions test.sh
@@ -0,0 +1,21 @@
#!/bin/bash

try() {
expected="$1"
input="$2"

./9cc "$input" > tmp.s
gcc -o tmp tmp.s
./tmp
actual="$?"

if [ "$actual" != "$expected" ]; then
echo "$input expected, but got $actual"
exit 1
fi
}

try 0 0
try 42 42

echo OK

0 comments on commit b32144f

Please sign in to comment.