Author: Armin Rigo <[email protected]>
Branch:
Changeset: r74:38fe2925e976
Date: 2014-11-18 17:51 +0100
http://bitbucket.org/cffi/creflect/changeset/38fe2925e976/
Log: Preference number for otherwise-equivalent integer types
diff --git a/creflect/creflect.h b/creflect/creflect.h
--- a/creflect/creflect.h
+++ b/creflect/creflect.h
@@ -63,8 +63,8 @@
#define CRX_INT_TYPE(cb, expr, guessname) \
_creflect__int_type(cb, expr > 0, sizeof(expr), expr == 1, guessname)
-#define CRX_INT_CONST(cb, expr, vp) \
- _creflect__int_const(cb, vp, \
+#define CRX_INT_CONST(cb, expr, vp, pn) \
+ _creflect__int_const(cb, vp, pn, \
"integer constant '" #expr "' is too large", \
!(((expr) * 0 + 4) << (sizeof(int)*8-2)), \
!(((expr) * 0L + 4L) << (sizeof(long)*8-2)), \
@@ -94,6 +94,7 @@
__attribute__((unused))
static crx_type_t *_creflect__int_const(crx_builder_t *cb, crx_int_const_t *vp,
+ int preference_number,
const char *toobig,
int fits_int, int fits_long,
int unsign, int fits_ull, int fits_ll,
@@ -102,6 +103,13 @@
size_t size;
const char *name;
+ if (preference_number >= 2) /* "prefer long" */
+ if (sizeof(int) == sizeof(long))
+ fits_int = 0;
+ if (preference_number >= 3) /* "prefer long long" */
+ if (sizeof(long) == sizeof(long long))
+ fits_long = 0;
+
if (unsign) {
if (!fits_ull) {
goto overflow;
diff --git a/creflect/model.py b/creflect/model.py
--- a/creflect/model.py
+++ b/creflect/model.py
@@ -144,7 +144,14 @@
block.writeline("(void)((%s) << 1); /* check that '%s' is %s */" % (
varname, varname, comment))
if self.is_integer_type():
- expr = "CRX_INT_CONST(cb, %s, &v)" % varname
+ if self.name.endswith('long'):
+ if self.name.endswith('long long'):
+ preference = 3
+ else:
+ preference = 2
+ else:
+ preference = 1
+ expr = "CRX_INT_CONST(cb, %s, &v, %d)" % (varname, preference)
elif self.is_char_type():
block.writeline('CRX_CHAR_CONST(cb, %s, &v);' % varname)
expr = 'cb->get_char_type(cb)'
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
@@ -8,7 +8,7 @@
{
crx_int_const_t v;
(void)((ab) << 1); /* check that 'ab' is an integer */
- t1 = CRX_INT_CONST(cb, ab, &v);
+ t1 = CRX_INT_CONST(cb, ab, &v, 1);
cb->define_int_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
@@ -12,7 +12,7 @@
{
crx_int_const_t v;
(void)((ab) << 1); /* check that 'ab' is an integer */
- t1 = CRX_INT_CONST(cb, ab, &v);
+ t1 = CRX_INT_CONST(cb, ab, &v, 1);
cb->define_int_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
@@ -8,7 +8,7 @@
{
crx_int_const_t v;
(void)((ab) << 1); /* check that 'ab' is an integer */
- t1 = CRX_INT_CONST(cb, ab, &v);
+ t1 = CRX_INT_CONST(cb, ab, &v, 1);
cb->define_int_const(cb, "ab", t1, &v);
#expect INTCONST ab = unsigned int 42
}
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
@@ -2,58 +2,14 @@
# ____________________________________________________________
-int testglob_003e(char *r)
+void testglob_003e(crx_builder_t *cb)
{
- int e = 0;
- char v[32];
- if (!r)
- return 999 + 1;
+ crx_type_t *t1;
{
- int z1, z2;
- (void)(ab << 1); /* check that 'ab' is an integer */
- z1 = !((ab * 0 + 4) << (sizeof(int)*8-2));
- z2 = !((ab * 0L + 4L) << (sizeof(long)*8-2));
- if (sizeof(int) == sizeof(long)) z1 = 0;
- if ((ab * 0 - 1) > 0) { /* unsigned */
- if (ab != (unsigned long long)ab) {
- r += sprintf(r, "#error unsigned integer constant 'ab' is too
large\n");
- e = -1;
- goto f1;
- }
- if (z1) {
- r += sprintf(r, "unsigned int");
- sprintf(v, "%u", (unsigned int)ab);
- }
- else if (z2) {
- r += sprintf(r, "unsigned long");
- sprintf(v, "%lu", (unsigned long)ab);
- }
- else {
- r += sprintf(r, "unsigned long long");
- sprintf(v, "%llu", (unsigned long long)ab);
- }
- }
- else { /* signed */
- if (ab != (long long)ab) {
- r += sprintf(r, "#error integer constant 'ab' is too large\n");
- e = -1;
- goto f1;
- }
- if (z1) {
- r += sprintf(r, "int");
- sprintf(v, "%d", (int)ab);
- }
- else if (z2) {
- r += sprintf(r, "long");
- sprintf(v, "%ld", (long)ab);
- }
- else {
- r += sprintf(r, "long long");
- sprintf(v, "%lld", (long long)ab);
- }
- }
+ crx_int_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);
+#expect INTCONST ab = long -42
}
- r += sprintf(r, " const ab = %s;\n", v);
- f1:
- return e;
}
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
@@ -2,59 +2,14 @@
# ____________________________________________________________
-int testglob_003f(char *r)
+void testglob_003f(crx_builder_t *cb)
{
- int e = 0;
- char v[32];
- if (!r)
- return 999 + 1;
+ crx_type_t *t1;
{
- int z1, z2;
- (void)(ab << 1); /* check that 'ab' is an integer */
- z1 = !((ab * 0 + 4) << (sizeof(int)*8-2));
- z2 = !((ab * 0L + 4L) << (sizeof(long)*8-2));
- if (sizeof(int) == sizeof(long)) z1 = 0;
- if (sizeof(long) == sizeof(long long)) z2 = 0;
- if ((ab * 0 - 1) > 0) { /* unsigned */
- if (ab != (unsigned long long)ab) {
- r += sprintf(r, "#error unsigned integer constant 'ab' is too
large\n");
- e = -1;
- goto f1;
- }
- if (z1) {
- r += sprintf(r, "unsigned int");
- sprintf(v, "%u", (unsigned int)ab);
- }
- else if (z2) {
- r += sprintf(r, "unsigned long");
- sprintf(v, "%lu", (unsigned long)ab);
- }
- else {
- r += sprintf(r, "unsigned long long");
- sprintf(v, "%llu", (unsigned long long)ab);
- }
- }
- else { /* signed */
- if (ab != (long long)ab) {
- r += sprintf(r, "#error integer constant 'ab' is too large\n");
- e = -1;
- goto f1;
- }
- if (z1) {
- r += sprintf(r, "int");
- sprintf(v, "%d", (int)ab);
- }
- else if (z2) {
- r += sprintf(r, "long");
- sprintf(v, "%ld", (long)ab);
- }
- else {
- r += sprintf(r, "long long");
- sprintf(v, "%lld", (long long)ab);
- }
- }
+ crx_int_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);
+#expect INTCONST ab = long long -42
}
- r += sprintf(r, " const ab = %s;\n", v);
- f1:
- return e;
}
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit