changeset 0:7d266b61ec2a

initial commit return valueなコンパイラを作成
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Wed, 24 Jul 2019 13:58:02 +0900
parents
children 59c56be5222e
files .hgignore 9cc.c Makefile test.sh
diffstat 4 files changed, 55 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore	Wed Jul 24 13:58:02 2019 +0900
@@ -0,0 +1,6 @@
+syntax: glob
+*~
+*.o
+tmp*
+9cc
+.gitignore
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/9cc.c	Wed Jul 24 13:58:02 2019 +0900
@@ -0,0 +1,16 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+int main(int argc, char **argv) {
+  if (argc != 2) {
+    fprintf(stderr,"invalid arguments number\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;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Makefile	Wed Jul 24 13:58:02 2019 +0900
@@ -0,0 +1,11 @@
+CFLAGS=-std=c11 -g -static
+
+9cc: 9cc.c
+
+test: 9cc
+	./test.sh
+
+clean:
+	rm -f 9cc *.o *~ tmp*
+
+.PHONY: test clean
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test.sh	Wed Jul 24 13:58:02 2019 +0900
@@ -0,0 +1,22 @@
+#!/bin/bash
+try() {
+  expected="$1"
+  input="$2"
+
+  ./9cc "$input" > tmp.s
+  gcc -o tmp tmp.s
+  ./tmp
+  actual="$?"
+
+  if [ "$actual" = "$expected" ]; then
+    echo "$input => $actual"
+  else
+    echo "$input => $expected expected, but got $actual"
+    exit 1
+  fi
+}
+
+try 0 0
+try 42 42
+
+echo OK