[pypy-commit] creflect default: Constant chars
arigo
noreply at buildbot.pypy.org
Tue Nov 18 17:38:09 CET 2014
Author: Armin Rigo <arigo at tunes.org>
Branch:
Changeset: r72:6131fc269b64
Date: 2014-11-18 17:38 +0100
http://bitbucket.org/cffi/creflect/changeset/6131fc269b64/
Log: Constant chars
diff --git a/creflect/creflect.h b/creflect/creflect.h
--- a/creflect/creflect.h
+++ b/creflect/creflect.h
@@ -20,6 +20,7 @@
unsigned int as_unsigned_int;
unsigned long as_unsigned_long;
unsigned long long as_unsigned_long_long;
+ char as_char;
} crx_int_const_t;
#define CRX_SELF struct _crx_builder_s *
@@ -62,15 +63,21 @@
#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_expr(cb, vp, \
- "integer constant '" #expr "' is too large", \
- !(((expr) * 0 + 4) << (sizeof(int)*8-2)), \
- !(((expr) * 0L + 4L) << (sizeof(long)*8-2)), \
- ((expr) * 0 - 1) > 0, \
- (expr) == (unsigned long long)(expr), \
- (expr) == (long long)(expr), \
- (unsigned long long)(expr))
+#define CRX_INT_CONST(cb, expr, vp) \
+ _creflect__int_const(cb, vp, \
+ "integer constant '" #expr "' is too large", \
+ !(((expr) * 0 + 4) << (sizeof(int)*8-2)), \
+ !(((expr) * 0L + 4L) << (sizeof(long)*8-2)), \
+ ((expr) * 0 - 1) > 0, \
+ (expr) == (unsigned long long)(expr), \
+ (expr) == (long long)(expr), \
+ (unsigned long long)(expr))
+
+#define CRX_CHAR_CONST(cb, expr, vp) \
+ _creflect__char_const(cb, vp, \
+ "char constant '" #expr "' is too large", \
+ (expr) == (signed char)(expr) || (expr) == (unsigned char)(expr), \
+ (char)(expr))
__attribute__((unused))
static crx_type_t *_creflect__int_type(crx_builder_t *cb, int expr_positive,
@@ -86,11 +93,11 @@
}
__attribute__((unused))
-static crx_type_t *_creflect__int_expr(crx_builder_t *cb, crx_int_const_t *vp,
- const char *toobig,
- int fits_int, int fits_long,
- int unsign, int fits_ull, int fits_ll,
- unsigned long long value)
+static crx_type_t *_creflect__int_const(crx_builder_t *cb, crx_int_const_t *vp,
+ const char *toobig,
+ int fits_int, int fits_long,
+ int unsign, int fits_ull, int fits_ll,
+ unsigned long long value)
{
size_t size;
const char *name;
@@ -142,3 +149,14 @@
cb->error(cb, toobig);
return 0;
}
+
+__attribute__((unused))
+static void _creflect__char_const(crx_builder_t *cb, crx_int_const_t *vp,
+ const char *toobig,
+ int fits_char, char value)
+{
+ if (!fits_char)
+ cb->error(cb, toobig);
+ else
+ vp->as_char = value;
+}
diff --git a/creflect/model.py b/creflect/model.py
--- a/creflect/model.py
+++ b/creflect/model.py
@@ -138,9 +138,18 @@
def write_int_const_decl(self, block, varname):
block.writedecl("crx_int_const_t v;")
- block.writeline("(void)((%s) << 1); /* check that '%s' is an"
- " integer */" % (varname, varname))
- expr = "CRX_INT_CONST(cb, %s, &v)" % varname
+ comment = "an integer"
+ if self.is_char_type():
+ comment += " or char"
+ 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
+ elif self.is_char_type():
+ block.writeline('CRX_CHAR_CONST(cb, %s, &v);' % varname)
+ expr = 'cb->get_char_type(cb)'
+ else:
+ xxxxxxx
t1 = block.write_crx_type_var(expr)
block.writeline('cb->define_int_const(cb, "%s", %s, &v);' % (
varname, t1))
diff --git a/test/cgcompile.c b/test/cgcompile.c
--- a/test/cgcompile.c
+++ b/test/cgcompile.c
@@ -190,6 +190,8 @@
printf("%lu\n", value->as_unsigned_long);
else if (strcmp(t->text, "unsigned long long") == 0)
printf("%llu\n", value->as_unsigned_long_long);
+ else if (strcmp(t->text, "char") == 0)
+ printf("'%c'\n", value->as_char);
else {
printf("???\n");
abort();
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
@@ -2,57 +2,14 @@
# ____________________________________________________________
-int testglob_003b(char *r)
+void testglob_003b(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 ((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);
+ cb->define_int_const(cb, "ab", t1, &v);
+#expect INTCONST ab = int 42
}
- r += sprintf(r, " const ab = %s;\n", v);
- f1:
- return e;
}
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
@@ -2,57 +2,14 @@
# ____________________________________________________________
-int testglob_003c(char *r)
+void testglob_003c(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 ((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);
+ cb->define_int_const(cb, "ab", t1, &v);
+#expect INTCONST ab = unsigned int 42
}
- r += sprintf(r, " const ab = %s;\n", v);
- f1:
- return e;
}
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
@@ -2,61 +2,15 @@
# ____________________________________________________________
-int testglob_003d(char *r)
+void testglob_003d(crx_builder_t *cb)
{
- int e = 0;
- char v[32];
- if (!r)
- return 52;
+ 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 (ab == (char)ab) {
- r += sprintf(r, "char");
- sprintf(v, "%d", (int)ab);
- }
- else 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 or char */
+ CRX_CHAR_CONST(cb, ab, &v);
+ t1 = cb->get_char_type(cb);
+ cb->define_int_const(cb, "ab", t1, &v);
+#expect INTCONST ab = char '*'
}
- r += sprintf(r, " const ab = %s;\n", v);
- f1:
- return e;
}
More information about the pypy-commit
mailing list