[pypy-commit] creflect default: basic case for floats
arigo
noreply at buildbot.pypy.org
Tue Nov 18 20:16:52 CET 2014
Author: Armin Rigo <arigo at tunes.org>
Branch:
Changeset: r88:7056fee0a27a
Date: 2014-11-18 20:13 +0100
http://bitbucket.org/cffi/creflect/changeset/7056fee0a27a/
Log: basic case for floats
diff --git a/creflect/creflect.h b/creflect/creflect.h
--- a/creflect/creflect.h
+++ b/creflect/creflect.h
@@ -29,11 +29,11 @@
crx_type_t *(*get_char_type)(CRX_SELF);
crx_type_t *(*get_bool_type)(CRX_SELF);
crx_type_t *(*get_signed_type)(CRX_SELF, size_t,
- const char *);
+ const char *);
crx_type_t *(*get_unsigned_type)(CRX_SELF, size_t,
+ const char *);
+ crx_type_t *(*get_float_type)(CRX_SELF, size_t,
const char *);
- crx_type_t *(*get_float_type)(CRX_SELF, size_t,
- const char *);
crx_type_t *(*get_function_type)(CRX_SELF, crx_type_t *,
crx_type_t *[], int, crx_trampoline1_fn);
crx_type_t *(*get_ellipsis_function_type)(CRX_SELF, crx_type_t *,
diff --git a/creflect/model.py b/creflect/model.py
--- a/creflect/model.py
+++ b/creflect/model.py
@@ -98,13 +98,22 @@
if isinstance(inspect, TypeInspector):
star_p1 = inspect.fetch_star_p1()
comment1 = inspect.get_comment(0, False, "a valid type", minlevel=1)
- comment2 = inspect.get_comment(0, False, "an integer type")
+ if self.is_float_type():
+ comment2 = inspect.get_comment(0, False, "a numeric type")
+ else:
+ comment2 = inspect.get_comment(0, False, "an integer type")
comment3 = inspect.get_comment(0, False, "not declared 'const'")
block.writedecl("char b[sizeof(%s)];%s" % (star_p1, comment1))
- if self.const:
+ if self.is_float_type():
+ block.writeline("memset(b, 0, sizeof(b));")
+ elif self.const:
block.writeline("memset(b, -1, sizeof(b));")
inspect.assign_to_p1("b")
- block.writeline("(void)(%s << 1);%s" % (star_p1, comment2))
+ if self.is_float_type():
+ op = '/ 2.0'
+ else:
+ op = '<< 1'
+ block.writeline("(void)(%s %s);%s" % (star_p1, op, comment2))
if not self.const:
block.writeline("%s = -1;%s" % (star_p1, comment3))
if self.is_integer_type():
@@ -118,7 +127,14 @@
block.writeline("}")
expr = 'cb->get_char_type(cb)'
elif self.is_float_type():
- xxx
+ errmsg = "numeric type '%s' is an integer, not a float" % (
+ inspect.get_comment_type(0, False),)
+ block.writeline("if ((1 + %s * 0) / 2 == 0) {" % (star_p1,))
+ block.writeline(' cb->error(cb, "%s");' % (errmsg,))
+ block.writeline(" return;")
+ block.writeline("}")
+ expr = 'cb->get_float_type(cb, sizeof(%s), "%s")' % (
+ star_p1, self.name)
else:
raise AssertionError
else:
diff --git a/test/codegen/003g.c b/test/codegen/003g.c
new file mode 100644
--- /dev/null
+++ b/test/codegen/003g.c
@@ -0,0 +1,23 @@
+typedef double num_t;
+
+# ____________________________________________________________
+
+void test003g(crx_builder_t *cb)
+{
+ crx_type_t *t1;
+ {
+ num_t *p1;
+ char b[sizeof(*p1)];
+ memset(b, 0, sizeof(b));
+ p1 = (void *)b;
+ (void)(*p1 / 2.0); /* check that 'num_t' is a numeric type */
+ *p1 = -1; /* check that 'num_t' is not declared 'const' */
+ if ((1 + *p1 * 0) / 2 == 0) {
+ cb->error(cb, "numeric type 'num_t' is an integer, not a float");
+ return;
+ }
+ t1 = cb->get_float_type(cb, sizeof(*p1), "double");
+ cb->define_type(cb, "num_t", t1);
+#expect TYPEDEF num_t = double
+ }
+}
More information about the pypy-commit
mailing list