aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-05-03 20:12:55 +0900
committernsfisis <nsfisis@gmail.com>2025-05-03 20:12:55 +0900
commit9de8ac56f23bb3a8a0adb40c359f0a83c839f9f2 (patch)
tree8955f4246bb3b2c2471eaf2bd8fedb3e425d4616
parent7570e8a0af6b4d8ea28d422cd8e4645fb23300af (diff)
downloadP4Dcc-9de8ac56f23bb3a8a0adb40c359f0a83c839f9f2.tar.gz
P4Dcc-9de8ac56f23bb3a8a0adb40c359f0a83c839f9f2.tar.zst
P4Dcc-9de8ac56f23bb3a8a0adb40c359f0a83c839f9f2.zip
fizzbuzz
-rw-r--r--main.c4
-rw-r--r--tests/020.sh123
-rw-r--r--tests/test_diff.sh17
3 files changed, 142 insertions, 2 deletions
diff --git a/main.c b/main.c
index 5c86d53..13bd3ac 100644
--- a/main.c
+++ b/main.c
@@ -979,6 +979,7 @@ void gen_func_call(CODEGEN* g, AST* ast) {
}
}
// TODO: rsp align
+ printf(" mov rax, 0\n");
printf(" call %s\n", func_name);
printf(" push rax\n");
}
@@ -1141,10 +1142,9 @@ void gen(CODEGEN* g, AST* ast) {
printf(".intel_syntax noprefix\n\n");
- int str_index = 1;
char** str_lit = ast->str_literals;
while (*str_lit) {
- printf(".Lstr__%d:\n", str_index);
+ printf(".Lstr__%d:\n", str_lit - ast->str_literals + 1);
printf(" .string \"%s\"\n\n", *str_lit);
str_lit += 1;
}
diff --git a/tests/020.sh b/tests/020.sh
new file mode 100644
index 0000000..146f725
--- /dev/null
+++ b/tests/020.sh
@@ -0,0 +1,123 @@
+set -e
+
+cat <<'EOF' > expected
+1
+2
+Fizz
+4
+Buzz
+Fizz
+7
+8
+Fizz
+Buzz
+11
+Fizz
+13
+14
+FizzBuzz
+16
+17
+Fizz
+19
+Buzz
+Fizz
+22
+23
+Fizz
+Buzz
+26
+Fizz
+28
+29
+FizzBuzz
+31
+32
+Fizz
+34
+Buzz
+Fizz
+37
+38
+Fizz
+Buzz
+41
+Fizz
+43
+44
+FizzBuzz
+46
+47
+Fizz
+49
+Buzz
+Fizz
+52
+53
+Fizz
+Buzz
+56
+Fizz
+58
+59
+FizzBuzz
+61
+62
+Fizz
+64
+Buzz
+Fizz
+67
+68
+Fizz
+Buzz
+71
+Fizz
+73
+74
+FizzBuzz
+76
+77
+Fizz
+79
+Buzz
+Fizz
+82
+83
+Fizz
+Buzz
+86
+Fizz
+88
+89
+FizzBuzz
+91
+92
+Fizz
+94
+Buzz
+Fizz
+97
+98
+Fizz
+Buzz
+EOF
+bash ../../test_diff.sh <<'EOF'
+int printf();
+
+int main() {
+ int i;
+ for (i = 1; i <= 100; i = i + 1) {
+ if (i % 15 == 0) {
+ printf("FizzBuzz\n");
+ } else if (i % 3 == 0) {
+ printf("Fizz\n");
+ } else if (i % 5 == 0) {
+ printf("Buzz\n");
+ } else {
+ printf("%d\n", i);
+ }
+ }
+ return 0;
+}
+EOF
diff --git a/tests/test_diff.sh b/tests/test_diff.sh
new file mode 100644
index 0000000..a66639e
--- /dev/null
+++ b/tests/test_diff.sh
@@ -0,0 +1,17 @@
+cat > main.c
+
+"$p4dcc" < main.c > main.s
+if [[ $? -ne 0 ]]; then
+ cat main.s >&2
+ exit 1
+fi
+gcc -Wl,-z,noexecstack -o a.out main.s
+./a.out > output
+exit_code=$?
+
+if [[ $exit_code -ne 0 ]]; then
+ echo "invalid exit code: $exit_code" >&2
+ exit 1
+fi
+
+diff -u expected output