aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorMichael Forney <mforney@mforney.org>2021-09-04 13:53:09 -0700
committerMichael Forney <mforney@mforney.org>2021-09-04 13:53:09 -0700
commite07ea69d611991a24d67076df906866c198252c0 (patch)
tree61f0df94a91aa221ba394d15738b2504591ff404 /test
parent16c718729adfaf40cf22aa4efeafb0d92ef5ee3c (diff)
Use architecture-specific va_list type
Previously, cproc effectively used used typedef struct { /* 32 bytes, 8-byte aligned */ } __builtin_va_list[1]; However, this is not quite correct for x86_64 nor aarch64, though it was close enough for both to work in most cases. In actuality, for x86_64 we want typedef struct { /* 24 bytes, 8-byte aligned */ } __builtin_va_list[1]; and for aarch64 we want typedef struct { /* 32 bytes, 8-byte aligned */ } __builtin_va_list; The difference only appears when the size of va_list matters, or when va_list is passed as a parameter. However, the former is not often the case, and the aarch64 ABI replaces aggregate arguments with pointers to caller-allocated memory, which is quite similar to arrays decaying to pointers in C except that the struct is not copied. Additionally, riscv64 simply uses typedef void *__builtin_va_list; which again has a different size and calling convention. To fix this, make the __builtin_va_list type architecture-specific and use architecture-specific tests for varargs-related functionality.
Diffstat (limited to 'test')
-rw-r--r--test/builtin-va-copy+aarch64.c (renamed from test/builtin-va-copy.c)0
-rw-r--r--test/builtin-va-copy+aarch64.qbe (renamed from test/builtin-va-copy.qbe)0
-rw-r--r--test/builtin-va-copy+x86_64.c4
-rw-r--r--test/builtin-va-copy+x86_64.qbe18
-rw-r--r--test/varargs+aarch64.c (renamed from test/varargs.c)0
-rw-r--r--test/varargs+aarch64.qbe (renamed from test/varargs.qbe)0
-rw-r--r--test/varargs+riscv64.c12
-rw-r--r--test/varargs+riscv64.qbe22
-rw-r--r--test/varargs+x86_64.c12
-rw-r--r--test/varargs+x86_64.qbe22
-rw-r--r--test/varargs-pass-valist+aarch64.c5
-rw-r--r--test/varargs-pass-valist+aarch64.qbe9
-rw-r--r--test/varargs-pass-valist+riscv64.c5
-rw-r--r--test/varargs-pass-valist+riscv64.qbe9
-rw-r--r--test/varargs-pass-valist+x86_64.c5
-rw-r--r--test/varargs-pass-valist+x86_64.qbe8
16 files changed, 131 insertions, 0 deletions
diff --git a/test/builtin-va-copy.c b/test/builtin-va-copy+aarch64.c
index 7a69596..7a69596 100644
--- a/test/builtin-va-copy.c
+++ b/test/builtin-va-copy+aarch64.c
diff --git a/test/builtin-va-copy.qbe b/test/builtin-va-copy+aarch64.qbe
index 6c8d288..6c8d288 100644
--- a/test/builtin-va-copy.qbe
+++ b/test/builtin-va-copy+aarch64.qbe
diff --git a/test/builtin-va-copy+x86_64.c b/test/builtin-va-copy+x86_64.c
new file mode 100644
index 0000000..7a69596
--- /dev/null
+++ b/test/builtin-va-copy+x86_64.c
@@ -0,0 +1,4 @@
+void f(void) {
+ static __builtin_va_list a, b;
+ __builtin_va_copy(a, b);
+}
diff --git a/test/builtin-va-copy+x86_64.qbe b/test/builtin-va-copy+x86_64.qbe
new file mode 100644
index 0000000..47e1d9e
--- /dev/null
+++ b/test/builtin-va-copy+x86_64.qbe
@@ -0,0 +1,18 @@
+data $.La.2 = align 8 { z 24 }
+data $.Lb.3 = align 8 { z 24 }
+export
+function $f() {
+@start.1
+@body.2
+ %.1 =l loadl $.Lb.3
+ storel %.1, $.La.2
+ %.2 =l add $.Lb.3, 8
+ %.3 =l add $.La.2, 8
+ %.4 =l loadl %.2
+ storel %.4, %.3
+ %.5 =l add %.2, 8
+ %.6 =l add %.3, 8
+ %.7 =l loadl %.5
+ storel %.7, %.6
+ ret
+}
diff --git a/test/varargs.c b/test/varargs+aarch64.c
index 0b5a73d..0b5a73d 100644
--- a/test/varargs.c
+++ b/test/varargs+aarch64.c
diff --git a/test/varargs.qbe b/test/varargs+aarch64.qbe
index 512a8c7..512a8c7 100644
--- a/test/varargs.qbe
+++ b/test/varargs+aarch64.qbe
diff --git a/test/varargs+riscv64.c b/test/varargs+riscv64.c
new file mode 100644
index 0000000..0b5a73d
--- /dev/null
+++ b/test/varargs+riscv64.c
@@ -0,0 +1,12 @@
+void f(int n, ...) {
+ __builtin_va_list ap;
+
+ __builtin_va_start(ap, n);
+ while (n) {
+ __builtin_va_arg(ap, int);
+ __builtin_va_arg(ap, float);
+ __builtin_va_arg(ap, char *);
+ --n;
+ }
+ __builtin_va_end(ap);
+}
diff --git a/test/varargs+riscv64.qbe b/test/varargs+riscv64.qbe
new file mode 100644
index 0000000..7fcda8d
--- /dev/null
+++ b/test/varargs+riscv64.qbe
@@ -0,0 +1,22 @@
+export
+function $f(w %.1, ...) {
+@start.1
+ %.2 =l alloc4 4
+ storew %.1, %.2
+ %.3 =l alloc8 8
+@body.2
+ vastart %.3
+@while_cond.3
+ %.4 =w loadw %.2
+ jnz %.4, @while_body.4, @while_join.5
+@while_body.4
+ %.5 =w vaarg %.3
+ %.6 =s vaarg %.3
+ %.7 =l vaarg %.3
+ %.8 =w loadw %.2
+ %.9 =w sub %.8, 1
+ storew %.9, %.2
+ jmp @while_cond.3
+@while_join.5
+ ret
+}
diff --git a/test/varargs+x86_64.c b/test/varargs+x86_64.c
new file mode 100644
index 0000000..0b5a73d
--- /dev/null
+++ b/test/varargs+x86_64.c
@@ -0,0 +1,12 @@
+void f(int n, ...) {
+ __builtin_va_list ap;
+
+ __builtin_va_start(ap, n);
+ while (n) {
+ __builtin_va_arg(ap, int);
+ __builtin_va_arg(ap, float);
+ __builtin_va_arg(ap, char *);
+ --n;
+ }
+ __builtin_va_end(ap);
+}
diff --git a/test/varargs+x86_64.qbe b/test/varargs+x86_64.qbe
new file mode 100644
index 0000000..641afa1
--- /dev/null
+++ b/test/varargs+x86_64.qbe
@@ -0,0 +1,22 @@
+export
+function $f(w %.1, ...) {
+@start.1
+ %.2 =l alloc4 4
+ storew %.1, %.2
+ %.3 =l alloc8 24
+@body.2
+ vastart %.3
+@while_cond.3
+ %.4 =w loadw %.2
+ jnz %.4, @while_body.4, @while_join.5
+@while_body.4
+ %.5 =w vaarg %.3
+ %.6 =s vaarg %.3
+ %.7 =l vaarg %.3
+ %.8 =w loadw %.2
+ %.9 =w sub %.8, 1
+ storew %.9, %.2
+ jmp @while_cond.3
+@while_join.5
+ ret
+}
diff --git a/test/varargs-pass-valist+aarch64.c b/test/varargs-pass-valist+aarch64.c
new file mode 100644
index 0000000..c1ede84
--- /dev/null
+++ b/test/varargs-pass-valist+aarch64.c
@@ -0,0 +1,5 @@
+void f(__builtin_va_list ap);
+void g(void) {
+ static __builtin_va_list ap;
+ f(ap);
+}
diff --git a/test/varargs-pass-valist+aarch64.qbe b/test/varargs-pass-valist+aarch64.qbe
new file mode 100644
index 0000000..67cef8a
--- /dev/null
+++ b/test/varargs-pass-valist+aarch64.qbe
@@ -0,0 +1,9 @@
+data $.Lap.2 = align 8 { z 32 }
+type :va_list.1 = align 8 { 32 }
+export
+function $g() {
+@start.1
+@body.2
+ call $f(:va_list.1 $.Lap.2)
+ ret
+}
diff --git a/test/varargs-pass-valist+riscv64.c b/test/varargs-pass-valist+riscv64.c
new file mode 100644
index 0000000..c1ede84
--- /dev/null
+++ b/test/varargs-pass-valist+riscv64.c
@@ -0,0 +1,5 @@
+void f(__builtin_va_list ap);
+void g(void) {
+ static __builtin_va_list ap;
+ f(ap);
+}
diff --git a/test/varargs-pass-valist+riscv64.qbe b/test/varargs-pass-valist+riscv64.qbe
new file mode 100644
index 0000000..937f50a
--- /dev/null
+++ b/test/varargs-pass-valist+riscv64.qbe
@@ -0,0 +1,9 @@
+data $.Lap.2 = align 8 { z 8 }
+export
+function $g() {
+@start.1
+@body.2
+ %.1 =l loadl $.Lap.2
+ call $f(l %.1)
+ ret
+}
diff --git a/test/varargs-pass-valist+x86_64.c b/test/varargs-pass-valist+x86_64.c
new file mode 100644
index 0000000..c1ede84
--- /dev/null
+++ b/test/varargs-pass-valist+x86_64.c
@@ -0,0 +1,5 @@
+void f(__builtin_va_list ap);
+void g(void) {
+ static __builtin_va_list ap;
+ f(ap);
+}
diff --git a/test/varargs-pass-valist+x86_64.qbe b/test/varargs-pass-valist+x86_64.qbe
new file mode 100644
index 0000000..50d9283
--- /dev/null
+++ b/test/varargs-pass-valist+x86_64.qbe
@@ -0,0 +1,8 @@
+data $.Lap.2 = align 8 { z 24 }
+export
+function $g() {
+@start.1
+@body.2
+ call $f(l $.Lap.2)
+ ret
+}