cproc

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.nihaljere.xyz/cproc
Log | Files | Refs | Submodules | README | LICENSE

commit a0c394bdd0f0c554f504ee8119e8304551653562
parent abccebea439d961c39e4a6f7d54f72e0b24e9100
Author: Michael Forney <mforney@mforney.org>
Date:   Wed, 13 Feb 2019 15:28:40 -0800

Implement __builtin_va_arg

Diffstat:
Mdecl.c | 1+
Mdecl.h | 2+-
Mexpr.c | 7+++++++
Mexpr.h | 1+
Mmain.c | 1+
Mqbe.c | 3+++
Atests/varargs.c | 12++++++++++++
Atests/varargs.qbe | 22++++++++++++++++++++++
8 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/decl.c b/decl.c @@ -19,6 +19,7 @@ #include "type.h" struct declaration builtinvastart = {.kind = DECLBUILTIN}; +struct declaration builtinvaarg = {.kind = DECLBUILTIN}; struct declaration builtinvaend = {.kind = DECLBUILTIN}; struct declaration builtinoffsetof = {.kind = DECLBUILTIN}; diff --git a/decl.h b/decl.h @@ -25,7 +25,7 @@ struct declaration { struct scope; struct function; -extern struct declaration builtinvastart, builtinvaend, builtinoffsetof; +extern struct declaration builtinvastart, builtinvaarg, builtinvaend, builtinoffsetof; struct declaration *mkdecl(enum declarationkind, struct type *, enum linkage); _Bool decl(struct scope *, struct function *); diff --git a/expr.c b/expr.c @@ -410,6 +410,13 @@ postfixexpr(struct scope *s, struct expression *r) free(expect(TIDENT, "after ','")); // XXX: check that this was actually a parameter name? expect(TRPAREN, "after parameter identifier"); + } else if (r->ident.decl == &builtinvaarg) { + e = mkexpr(EXPRBUILTIN, NULL, 0); + e->builtin.kind = BUILTINVAARG; + e->builtin.arg = exprconvert(assignexpr(s), &typevalistptr); + expect(TCOMMA, "after va_list"); + e->type = typename(s); + expect(TRPAREN, "after typename"); } else if (r->ident.decl == &builtinvaend) { e = mkexpr(EXPRBUILTIN, &typevoid, 0); e->builtin.kind = BUILTINVAEND; diff --git a/expr.h b/expr.h @@ -79,6 +79,7 @@ struct expression { struct { enum { BUILTINVASTART, + BUILTINVAARG, BUILTINVAEND, } kind; struct expression *arg; diff --git a/main.c b/main.c @@ -54,6 +54,7 @@ main(int argc, char *argv[]) } } else { scopeputdecl(&filescope, "__builtin_va_start", &builtinvastart); + scopeputdecl(&filescope, "__builtin_va_arg", &builtinvaarg); scopeputdecl(&filescope, "__builtin_va_end", &builtinvaend); scopeputdecl(&filescope, "__builtin_offsetof", &builtinoffsetof); while (tok.kind != TEOF) { diff --git a/qbe.c b/qbe.c @@ -777,6 +777,9 @@ funcexpr(struct function *f, struct expression *e) l = funcexpr(f, e->builtin.arg); funcinst(f, IVASTART, NULL, (struct value *[]){l}); break; + case BUILTINVAARG: + l = funcexpr(f, e->builtin.arg); + return funcinst(f, IVAARG, e->type->repr, (struct value *[]){l}); case BUILTINVAEND: /* no-op */ break; diff --git a/tests/varargs.c b/tests/varargs.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/tests/varargs.qbe b/tests/varargs.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 loadsw %.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 loadsw %.2 + %.9 =w sub %.8, 1 + storew %.9, %.2 + jmp @while_cond.3 +@while_join.5 + ret +}