diff options
author | Michael Forney <mforney@mforney.org> | 2019-02-13 15:28:40 -0800 |
---|---|---|
committer | Michael Forney <mforney@mforney.org> | 2019-02-13 19:42:25 -0800 |
commit | a0c394bdd0f0c554f504ee8119e8304551653562 (patch) | |
tree | e11fe7fd0679b7a1b47717aed632f918d888c797 | |
parent | abccebea439d961c39e4a6f7d54f72e0b24e9100 (diff) |
Implement __builtin_va_arg
-rw-r--r-- | decl.c | 1 | ||||
-rw-r--r-- | decl.h | 2 | ||||
-rw-r--r-- | expr.c | 7 | ||||
-rw-r--r-- | expr.h | 1 | ||||
-rw-r--r-- | main.c | 1 | ||||
-rw-r--r-- | qbe.c | 3 | ||||
-rw-r--r-- | tests/varargs.c | 12 | ||||
-rw-r--r-- | tests/varargs.qbe | 22 |
8 files changed, 48 insertions, 1 deletions
@@ -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}; @@ -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 *); @@ -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; @@ -79,6 +79,7 @@ struct expression { struct { enum { BUILTINVASTART, + BUILTINVAARG, BUILTINVAEND, } kind; struct expression *arg; @@ -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) { @@ -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 new file mode 100644 index 0000000..0b5a73d --- /dev/null +++ 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 new file mode 100644 index 0000000..16db877 --- /dev/null +++ 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 +} |