From 3733978d9c7768baaeb6199c05bc98939c15c8cd Mon Sep 17 00:00:00 2001 From: Michael Forney Date: Fri, 22 Feb 2019 13:48:09 -0800 Subject: Implement __builtin_nanf for empty string literals --- decl.h | 1 + expr.c | 8 ++++++++ scope.c | 1 + tests/builtin-nanf.c | 1 + tests/builtin-nanf.qbe | 1 + 5 files changed, 12 insertions(+) create mode 100644 tests/builtin-nanf.c create mode 100644 tests/builtin-nanf.qbe diff --git a/decl.h b/decl.h index db6a28c..314926b 100644 --- a/decl.h +++ b/decl.h @@ -20,6 +20,7 @@ enum builtinkind { BUILTINVAEND, BUILTINOFFSETOF, BUILTINALLOCA, + BUILTINNANF, BUILTININFF, }; diff --git a/expr.c b/expr.c index 8058523..009e05f 100644 --- a/expr.c +++ b/expr.c @@ -450,6 +450,14 @@ builtinfunc(struct scope *s, enum builtinkind kind) e->builtin.kind = BUILTINALLOCA; e->builtin.arg = exprconvert(assignexpr(s), &typeulong); break; + case BUILTINNANF: + e = assignexpr(s); + if (!(e->flags & EXPRFLAG_DECAYED) || e->unary.base->kind != EXPRSTRING || e->unary.base->string.size > 0) + error(&tok.loc, "__builtin_nanf currently only supports empty string literals"); + e = mkexpr(EXPRCONST, &typefloat, 0); + /* TODO: use NAN here when we can handle musl's math.h */ + e->constant.f = strtod("nan", NULL); + break; case BUILTININFF: e = mkexpr(EXPRCONST, &typefloat, 0); /* TODO: use INFINITY here when we can handle musl's math.h */ diff --git a/scope.c b/scope.c index 00569b9..be561f5 100644 --- a/scope.c +++ b/scope.c @@ -24,6 +24,7 @@ scopeinit(void) {"__builtin_va_end", {.kind = DECLBUILTIN, .builtin = BUILTINVAEND}}, {"__builtin_offsetof", {.kind = DECLBUILTIN, .builtin = BUILTINOFFSETOF}}, {"__builtin_alloca", {.kind = DECLBUILTIN, .builtin = BUILTINALLOCA}}, + {"__builtin_nanf", {.kind = DECLBUILTIN, .builtin = BUILTINNANF}}, {"__builtin_inff", {.kind = DECLBUILTIN, .builtin = BUILTININFF}}, }; struct builtin *b; diff --git a/tests/builtin-nanf.c b/tests/builtin-nanf.c new file mode 100644 index 0000000..d239a46 --- /dev/null +++ b/tests/builtin-nanf.c @@ -0,0 +1 @@ +float x = __builtin_nanf(""); diff --git a/tests/builtin-nanf.qbe b/tests/builtin-nanf.qbe new file mode 100644 index 0000000..5fd7ac2 --- /dev/null +++ b/tests/builtin-nanf.qbe @@ -0,0 +1 @@ +export data $x = align 4 { s s_nan, } -- cgit v1.2.3