aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorlemon <lsof@mailbox.org>2025-11-23 15:26:55 +0100
committerlemon <lsof@mailbox.org>2025-11-23 15:33:38 +0100
commita9e0174fb69f3ddc85cd9c510b68cec22b6ebf42 (patch)
treec633c8c514bdcc8b729050685adef3e1c4b826f2
parent1f72464c6451fcff16180d00af537225acc9b83c (diff)
c: __builtin_va_copy
-rw-r--r--c/builtin.c29
-rw-r--r--embedfilesdir.c2
-rw-r--r--test/10-varargs.c17
3 files changed, 39 insertions, 9 deletions
diff --git a/c/builtin.c b/c/builtin.c
index 23e1872..29a3191 100644
--- a/c/builtin.c
+++ b/c/builtin.c
@@ -24,11 +24,11 @@ callcheck(const struct span *span, int nparam, const union type *param, int narg
return ok;
}
-#define DEF_FNLIKE_SEMA(name, retty, ...) \
- static bool \
- name##_sema(struct comp *cm, struct expr *ex) { \
- static union type par[] = { {{0}}, __VA_ARGS__ }; \
- ex->ty = retty; \
+#define DEF_FNLIKE_SEMA(name, retty, ...) \
+ static bool \
+ name##_sema(struct comp *cm, struct expr *ex) { \
+ union type par[] = { {{0}}, __VA_ARGS__ }; \
+ ex->ty = retty; \
return callcheck(&ex->span, arraylength(par)-1, par+1, ex->narg, ex->sub+1); \
}
@@ -50,6 +50,8 @@ va_start_comp(struct function *fn, struct expr *ex, bool discard)
return NOREF;
}
+DEF_FNLIKE_SEMA(trap, mktype(TYVOID), )
+
static bool
va_end_sema(struct comp *cm, struct expr *ex)
{
@@ -63,7 +65,19 @@ va_end_comp(struct function *fn, struct expr *ex, bool discard)
return NOREF;
}
-DEF_FNLIKE_SEMA(trap, mktype(TYVOID), )
+DEF_FNLIKE_SEMA(va_copy, mktype(TYVOID), cvalistty, cvalistty)
+
+static union ref
+va_copy_comp(struct function *fn, struct expr *ex, bool discard)
+{
+ union irtype typ = mkirtype(cvalistty.t == TYARRAY ? typechild(cvalistty) : cvalistty);
+ assert(ex->sub[1].ty.bits == cvalistty.bits);
+ assert(ex->sub[2].ty.bits == cvalistty.bits);
+ addinstr(fn, mkarginstr(typ, compileexpr(fn, &ex->sub[1], 0)));
+ addinstr(fn, mkarginstr(typ, compileexpr(fn, &ex->sub[2], 0)));
+ addinstr(fn, mkintrin(INstructcopy, 0, 2));
+ return NOREF;
+}
static union ref
trap_comp(struct function *fn, struct expr *ex, bool discard)
@@ -81,8 +95,7 @@ builtin_va_arg_comp(struct function *fn, const struct expr *ex, bool discard)
}
#define LIST_BUILTINS(_) \
- _(va_start) \
- _(va_end) \
+ _(va_start) _(va_copy) _(va_end) \
_(trap)
static const struct {
diff --git a/embedfilesdir.c b/embedfilesdir.c
index d2670b3..05d65d0 100644
--- a/embedfilesdir.c
+++ b/embedfilesdir.c
@@ -27,7 +27,7 @@ typedef __builtin_va_list __gnuc_va_list;\n\
#endif\n\
#define va_start(ap,n) __builtin_va_start(ap)\n\
#define va_arg(ap,type) __builtin_va_arg(ap, type)\n\
-#define va_copy(dst,src) (void)((dst)=(src))\n\
+#define va_copy(dst,src) __builtin_va_copy(dst, src)\n\
#define va_end(ap) __builtin_va_end(ap)\n\
")},
diff --git a/test/10-varargs.c b/test/10-varargs.c
index a3715ab..a523d97 100644
--- a/test/10-varargs.c
+++ b/test/10-varargs.c
@@ -2,6 +2,8 @@
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36
<1.5>
1.1 + 2.1 + 3.1 + 5.1 + 1.5 + -1.5 + 1.5 + -1.5 + 1.5 + -1 = 11.9
+fwd()/1: Hello World
+fwd()/2: Hello World
*/
@@ -37,9 +39,24 @@ double sumf(double x, ...) {
return x;
}
+void fwd(const char *fmt, ...) {
+ va_list ap, aq;
+ va_start(ap, fmt);
+ va_copy(aq, ap);
+ printf("fwd()/1: ");
+ vprintf(fmt, ap);
+ va_end(ap);
+ printf("\n");
+ printf("fwd()/2: ");
+ vprintf(fmt, aq);
+ printf("\n");
+ va_end(aq);
+}
+
int main() {
printf(" = %d\n", sum(1,2,3,4,5,6,7,8,0,0));
printf("<%g>\n", stkarg(0,0,0,0,0,0,0,0,1.5));
printf(" = %g\n", sumf(1.1, 2.1, 3.1, 5.1, 1.5, -1.5, 1.5, -1.5, 1.5, -1.0, 0.0));
+ fwd("%s %s", "Hello", "World");
}