Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A very simple compiler that comiles an integer.
- Loading branch information
0 parents
commit b32144f
Showing
4 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*~ | ||
*.o | ||
tmp* | ||
9cc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
9cc: 9cc.c | ||
|
||
test: 9cc | ||
./test.sh | ||
|
||
clean: | ||
rm -f 9cc *.o *~ tmp* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |