[pypy-commit] creflect default: float consts
arigo
noreply at buildbot.pypy.org
Tue Nov 18 20:33:08 CET 2014
Author: Armin Rigo <arigo at tunes.org>
Branch:
Changeset: r90:4a2b32da62a3
Date: 2014-11-18 20:33 +0100
http://bitbucket.org/cffi/creflect/changeset/4a2b32da62a3/
Log: float consts
diff --git a/creflect/creflect.h b/creflect/creflect.h
--- a/creflect/creflect.h
+++ b/creflect/creflect.h
@@ -21,7 +21,10 @@
unsigned long as_unsigned_long;
unsigned long long as_unsigned_long_long;
char as_char;
-} crx_int_const_t;
+ float as_float;
+ double as_double;
+ long double as_long_double;
+} crx_num_const_t;
#define CRX_SELF struct _crx_builder_s *
typedef struct _crx_builder_s {
@@ -53,8 +56,8 @@
void *);
void (*define_func)(CRX_SELF, const char *, crx_type_t *,
crx_type_t *[], int, crx_trampoline0_fn, void *);
- void (*define_int_const)(CRX_SELF, const char *, crx_type_t *,
- crx_int_const_t *);
+ void (*define_num_const)(CRX_SELF, const char *, crx_type_t *,
+ crx_num_const_t *);
void (*error)(CRX_SELF, const char *);
} crx_builder_t;
#undef CRX_SELF
@@ -79,6 +82,9 @@
(expr) == (signed char)(expr) || (expr) == (unsigned char)(expr), \
(char)(expr))
+#define CRX_FLOAT_CONST(cb, expr, vp) \
+ _creflect__float_const(cb, vp, sizeof(expr), (long double)(expr))
+
__attribute__((unused))
static crx_type_t *_creflect__int_type(crx_builder_t *cb, int expr_positive,
size_t size_of_expr, int expr_equal_one,
@@ -93,7 +99,7 @@
}
__attribute__((unused))
-static crx_type_t *_creflect__int_const(crx_builder_t *cb, crx_int_const_t *vp,
+static crx_type_t *_creflect__int_const(crx_builder_t *cb, crx_num_const_t *vp,
int preference_number,
const char *toobig,
int fits_int, int fits_long,
@@ -159,7 +165,7 @@
}
__attribute__((unused))
-static void _creflect__char_const(crx_builder_t *cb, crx_int_const_t *vp,
+static void _creflect__char_const(crx_builder_t *cb, crx_num_const_t *vp,
const char *toobig,
int fits_char, char value)
{
@@ -168,3 +174,25 @@
else
vp->as_char = value;
}
+
+__attribute__((unused))
+static crx_type_t *_creflect__float_const(crx_builder_t *cb,
+ crx_num_const_t *vp,
+ size_t size, long double value)
+{
+ const char *name;
+
+ if (size == sizeof(float)) {
+ vp->as_float = (float)value;
+ name = "float";
+ }
+ else if (size == sizeof(double)) {
+ vp->as_double = (double)value;
+ name = "double";
+ }
+ else { /* unknown-sized floats are treated as "long double" */
+ vp->as_long_double = value;
+ name = "long double";
+ }
+ return cb->get_float_type(cb, size, name);
+}
diff --git a/creflect/model.py b/creflect/model.py
--- a/creflect/model.py
+++ b/creflect/model.py
@@ -159,13 +159,17 @@
raise AssertionError
return block.write_crx_type_var(expr)
- def write_int_const_decl(self, block, varname):
- block.writedecl("crx_int_const_t v;")
+ def write_num_const_decl(self, block, varname):
+ block.writedecl("crx_num_const_t v;")
comment = "an integer"
+ op = "<< 1"
if self.is_char_type():
comment += " or char"
- block.writeline("(void)((%s) << 1); /* check that '%s' is %s */" % (
- varname, varname, comment))
+ if self.is_float_type():
+ comment = "a number"
+ op = "/ 2.0"
+ block.writeline("(void)((%s) %s); /* check that '%s' is %s */" % (
+ varname, op, varname, comment))
if self.is_integer_type():
if self.name.endswith('long'):
if self.name.endswith('long long'):
@@ -178,10 +182,12 @@
elif self.is_char_type():
block.writeline('CRX_CHAR_CONST(cb, %s, &v);' % varname)
expr = 'cb->get_char_type(cb)'
+ elif self.is_float_type():
+ expr = "CRX_FLOAT_CONST(cb, %s, &v)" % (varname,)
else:
- xxxxxxx
+ raise AssertionError
t1 = block.write_crx_type_var(expr)
- block.writeline('cb->define_int_const(cb, "%s", %s, &v);' % (
+ block.writeline('cb->define_num_const(cb, "%s", %s, &v);' % (
varname, t1))
@@ -681,7 +687,7 @@
def write_declaration(self, funcblock):
if isinstance(self.type, PrimitiveType):
block = CodeBlock(funcblock)
- self.type.write_int_const_decl(block, self.name)
+ self.type.write_num_const_decl(block, self.name)
funcblock.write_subblock(block)
else:
VarDecl.write_declaration(self, funcblock)
diff --git a/test/cgcompile.c b/test/cgcompile.c
--- a/test/cgcompile.c
+++ b/test/cgcompile.c
@@ -178,8 +178,8 @@
printf("%s\n", ret->text);
}
-static void tst_define_int_const(crx_builder_t *cb, const char *name,
- crx_type_t *t, crx_int_const_t *value)
+static void tst_define_num_const(crx_builder_t *cb, const char *name,
+ crx_type_t *t, crx_num_const_t *value)
{
printf("INTCONST %s = %s ", name, t->text);
if (strcmp(t->text, "int") == 0)
@@ -196,6 +196,12 @@
printf("%llu\n", value->as_unsigned_long_long);
else if (strcmp(t->text, "char") == 0)
printf("'%c'\n", value->as_char);
+ else if (strcmp(t->text, "float") == 0)
+ printf("%f\n", (double)value->as_float);
+ else if (strcmp(t->text, "double") == 0)
+ printf("%f\n", value->as_double);
+ else if (strcmp(t->text, "long double") == 0)
+ printf("%f\n", (double)value->as_double);
else {
printf("???\n");
abort();
@@ -228,7 +234,7 @@
tst_define_type,
tst_define_var,
tst_define_func,
- tst_define_int_const,
+ tst_define_num_const,
tst_error,
};
diff --git a/test/codegen/glob-003.c b/test/codegen/glob-003.c
--- a/test/codegen/glob-003.c
+++ b/test/codegen/glob-003.c
@@ -6,10 +6,10 @@
{
crx_type_t *t1;
{
- crx_int_const_t v;
+ crx_num_const_t v;
(void)((ab) << 1); /* check that 'ab' is an integer */
t1 = CRX_INT_CONST(cb, ab, &v, 1);
- cb->define_int_const(cb, "ab", t1, &v);
+ cb->define_num_const(cb, "ab", t1, &v);
#expect INTCONST ab = int -42
}
}
diff --git a/test/codegen/glob-003b.c b/test/codegen/glob-003b.c
--- a/test/codegen/glob-003b.c
+++ b/test/codegen/glob-003b.c
@@ -10,10 +10,10 @@
{
crx_type_t *t1;
{
- crx_int_const_t v;
+ crx_num_const_t v;
(void)((ab) << 1); /* check that 'ab' is an integer */
t1 = CRX_INT_CONST(cb, ab, &v, 1);
- cb->define_int_const(cb, "ab", t1, &v);
+ cb->define_num_const(cb, "ab", t1, &v);
#expect INTCONST ab = int 42
}
}
diff --git a/test/codegen/glob-003c.c b/test/codegen/glob-003c.c
--- a/test/codegen/glob-003c.c
+++ b/test/codegen/glob-003c.c
@@ -6,10 +6,10 @@
{
crx_type_t *t1;
{
- crx_int_const_t v;
+ crx_num_const_t v;
(void)((ab) << 1); /* check that 'ab' is an integer */
t1 = CRX_INT_CONST(cb, ab, &v, 1);
- cb->define_int_const(cb, "ab", t1, &v);
+ cb->define_num_const(cb, "ab", t1, &v);
#expect INTCONST ab = unsigned int 42
}
}
diff --git a/test/codegen/glob-003d.c b/test/codegen/glob-003d.c
--- a/test/codegen/glob-003d.c
+++ b/test/codegen/glob-003d.c
@@ -6,11 +6,11 @@
{
crx_type_t *t1;
{
- crx_int_const_t v;
+ crx_num_const_t v;
(void)((ab) << 1); /* check that 'ab' is an integer or char */
CRX_CHAR_CONST(cb, ab, &v);
t1 = cb->get_char_type(cb);
- cb->define_int_const(cb, "ab", t1, &v);
+ cb->define_num_const(cb, "ab", t1, &v);
#expect INTCONST ab = char '*'
}
}
diff --git a/test/codegen/glob-003e.c b/test/codegen/glob-003e.c
--- a/test/codegen/glob-003e.c
+++ b/test/codegen/glob-003e.c
@@ -6,10 +6,10 @@
{
crx_type_t *t1;
{
- crx_int_const_t v;
+ crx_num_const_t v;
(void)((ab) << 1); /* check that 'ab' is an integer */
t1 = CRX_INT_CONST(cb, ab, &v, 2);
- cb->define_int_const(cb, "ab", t1, &v);
+ cb->define_num_const(cb, "ab", t1, &v);
#expect INTCONST ab = long -42
}
}
diff --git a/test/codegen/glob-003f.c b/test/codegen/glob-003f.c
--- a/test/codegen/glob-003f.c
+++ b/test/codegen/glob-003f.c
@@ -6,10 +6,10 @@
{
crx_type_t *t1;
{
- crx_int_const_t v;
+ crx_num_const_t v;
(void)((ab) << 1); /* check that 'ab' is an integer */
t1 = CRX_INT_CONST(cb, ab, &v, 3);
- cb->define_int_const(cb, "ab", t1, &v);
+ cb->define_num_const(cb, "ab", t1, &v);
#expect INTCONST ab = long long -42
}
}
diff --git a/test/codegen/glob-003g.c b/test/codegen/glob-003g.c
--- a/test/codegen/glob-003g.c
+++ b/test/codegen/glob-003g.c
@@ -6,10 +6,10 @@
{
crx_type_t *t1;
{
- crx_int_const_t v;
+ crx_num_const_t v;
(void)((ab) << 1); /* check that 'ab' is an integer */
t1 = CRX_INT_CONST(cb, ab, &v, 1);
- cb->define_int_const(cb, "ab", t1, &v);
+ cb->define_num_const(cb, "ab", t1, &v);
#expect INTCONST ab = int -42
}
}
More information about the pypy-commit
mailing list