aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Forney <mforney@mforney.org>2019-02-22 13:48:09 -0800
committerMichael Forney <mforney@mforney.org>2019-02-22 13:50:49 -0800
commit3733978d9c7768baaeb6199c05bc98939c15c8cd (patch)
tree3c3de3fc328c69a26a0797896c217204a9f8ff0e
parenta056c878d1df67ff12e506d48653c48e9a1b001d (diff)
Implement __builtin_nanf for empty string literals
-rw-r--r--decl.h1
-rw-r--r--expr.c8
-rw-r--r--scope.c1
-rw-r--r--tests/builtin-nanf.c1
-rw-r--r--tests/builtin-nanf.qbe1
5 files changed, 12 insertions, 0 deletions
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, }