aboutsummaryrefslogtreecommitdiff
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
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.
-rw-r--r--cc.h2
-rw-r--r--expr.c10
-rw-r--r--qbe.c4
-rw-r--r--scope.c5
-rw-r--r--targ.c18
-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
-rw-r--r--type.c9
22 files changed, 163 insertions, 16 deletions
diff --git a/cc.h b/cc.h
index 9c4484b..c4dbee9 100644
--- a/cc.h
+++ b/cc.h
@@ -428,12 +428,12 @@ extern struct type typeint, typeuint;
extern struct type typelong, typeulong;
extern struct type typellong, typeullong;
extern struct type typefloat, typedouble, typeldouble;
-extern struct type typevalist;
/* targ */
struct target {
const char *name;
+ struct type *typevalist;
struct type *typewchar;
int signedchar;
};
diff --git a/expr.c b/expr.c
index 6837adb..1c91556 100644
--- a/expr.c
+++ b/expr.c
@@ -643,7 +643,7 @@ builtinfunc(struct scope *s, enum builtinkind kind)
e = mkexpr(EXPRBUILTIN, NULL);
e->builtin.kind = BUILTINVAARG;
e->base = mkunaryexpr(TBAND, assignexpr(s));
- if (e->base->base->type != &typevalist)
+ if (e->base->base->type != targ->typevalist)
error(&tok.loc, "va_arg argument must have type va_list");
expect(TCOMMA, "after va_list");
e->type = typename(s, &e->qual);
@@ -653,20 +653,20 @@ builtinfunc(struct scope *s, enum builtinkind kind)
e->assign.l = assignexpr(s);
if (e->assign.l->decayed)
e->assign.l = e->assign.l->base;
- if (e->assign.l->type != &typevalist)
+ if (e->assign.l->type != targ->typevalist)
error(&tok.loc, "va_copy destination must have type va_list");
expect(TCOMMA, "after target va_list");
e->assign.r = assignexpr(s);
if (e->assign.r->decayed)
e->assign.r = e->assign.r->base;
- if (e->assign.r->type != &typevalist)
+ if (e->assign.r->type != targ->typevalist)
error(&tok.loc, "va_copy source must have type va_list");
break;
case BUILTINVAEND:
e = assignexpr(s);
if (e->decayed)
e = e->base;
- if (e->type != &typevalist)
+ if (e->type != targ->typevalist)
error(&tok.loc, "va_end argument must have type va_list");
e = mkexpr(EXPRBUILTIN, &typevoid);
e->builtin.kind = BUILTINVAEND;
@@ -675,7 +675,7 @@ builtinfunc(struct scope *s, enum builtinkind kind)
e = mkexpr(EXPRBUILTIN, &typevoid);
e->builtin.kind = BUILTINVASTART;
e->base = mkunaryexpr(TBAND, assignexpr(s));
- if (e->base->base->type != &typevalist)
+ if (e->base->base->type != targ->typevalist)
error(&tok.loc, "va_start argument must have type va_list");
expect(TCOMMA, "after va_list");
param = assignexpr(s);
diff --git a/qbe.c b/qbe.c
index df363c2..be7dce7 100644
--- a/qbe.c
+++ b/qbe.c
@@ -1104,6 +1104,10 @@ emittype(struct type *t)
}
fputs("type ", stdout);
emitvalue(t->value);
+ if (t == targ->typevalist) {
+ printf(" = align %d { %" PRIu64 " }\n", t->align, t->size);
+ return;
+ }
fputs(" = { ", stdout);
for (m = t->structunion.members, off = 0; m;) {
if (t->kind == TYPESTRUCT) {
diff --git a/scope.c b/scope.c
index 72025f5..c116670 100644
--- a/scope.c
+++ b/scope.c
@@ -25,13 +25,16 @@ scopeinit(void)
{"__builtin_va_arg", {.kind = DECLBUILTIN, .builtin = BUILTINVAARG}},
{"__builtin_va_copy", {.kind = DECLBUILTIN, .builtin = BUILTINVACOPY}},
{"__builtin_va_end", {.kind = DECLBUILTIN, .builtin = BUILTINVAEND}},
- {"__builtin_va_list", {.kind = DECLTYPE, .type = &typevalist}},
{"__builtin_va_start", {.kind = DECLBUILTIN, .builtin = BUILTINVASTART}},
};
+ static struct decl valist;
struct builtin *b;
for (b = builtins; b < builtins + LEN(builtins); ++b)
scopeputdecl(&filescope, b->name, &b->decl);
+ valist.kind = DECLTYPE;
+ valist.type = targ->typevalist;
+ scopeputdecl(&filescope, "__builtin_va_list", &valist);
}
struct scope *
diff --git a/targ.c b/targ.c
index 2859975..f2be127 100644
--- a/targ.c
+++ b/targ.c
@@ -9,14 +9,32 @@ static const struct target alltargs[] = {
{
.name = "x86_64",
.typewchar = &typeint,
+ .typevalist = &(struct type){
+ .kind = TYPEARRAY, .prop = PROPOBJECT|PROPDERIVED|PROPAGGR,
+ .align = 8, .size = 24,
+ .array = {1}, .base = &(struct type){
+ .kind = TYPESTRUCT, .prop = PROPOBJECT|PROPAGGR,
+ .align = 8, .size = 24,
+ },
+ },
.signedchar = 1,
},
{
.name = "aarch64",
+ .typevalist = &(struct type){
+ .kind = TYPESTRUCT, .prop = PROPOBJECT|PROPAGGR,
+ .align = 8, .size = 32,
+ .structunion.tag = "va_list",
+ },
.typewchar = &typeuint,
},
{
.name = "riscv64",
+ .typevalist = &(struct type){
+ .kind = TYPEPOINTER, .prop = PROPOBJECT|PROPDERIVED|PROPSCALAR,
+ .align = 8, .size = 8,
+ .base = &typevoid,
+ },
.typewchar = &typeint,
},
};
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
+}
diff --git a/type.c b/type.c
index a97175a..1d28213 100644
--- a/type.c
+++ b/type.c
@@ -39,15 +39,6 @@ struct type typefloat = FLTTYPE(TYPEFLOAT, 4);
struct type typedouble = FLTTYPE(TYPEDOUBLE, 8);
struct type typeldouble = FLTTYPE(TYPELDOUBLE, 16);
-struct type typevalist = {
- .kind = TYPEARRAY, .size = 32, .align = 8, .array = {1},
- .prop = PROPOBJECT|PROPDERIVED|PROPAGGR,
- .base = &(struct type){
- .kind = TYPESTRUCT, .size = 32, .align = 8,
- .prop = PROPOBJECT|PROPAGGR,
- },
-};
-
struct type *
mktype(enum typekind kind, enum typeprop prop)
{