[pypy-commit] creflect default: pass simple structs again
arigo
noreply at buildbot.pypy.org
Tue Nov 18 19:04:10 CET 2014
Author: Armin Rigo <arigo at tunes.org>
Branch:
Changeset: r80:9a4131bc9166
Date: 2014-11-18 19:04 +0100
http://bitbucket.org/cffi/creflect/changeset/9a4131bc9166/
Log: pass simple structs again
diff --git a/creflect/codegen.py b/creflect/codegen.py
--- a/creflect/codegen.py
+++ b/creflect/codegen.py
@@ -7,10 +7,12 @@
if parent is None:
self.crx_type_vars = []
self.crx_type_cache = {}
+ self.crx_field_vars = []
self.crx_top_level = self
else:
self.crx_type_vars = parent.crx_type_vars
self.crx_type_cache = parent.crx_type_cache
+ self.crx_field_vars = parent.crx_field_vars
self.crx_top_level = parent.crx_top_level
def writedecl(self, line):
@@ -46,6 +48,11 @@
else:
return '0'
+ def write_crx_field_var(self, nfields):
+ d1 = 'd%d' % (len(self.crx_field_vars) + 1)
+ self.crx_field_vars.append('%s[%d]' % (d1, nfields))
+ return d1
+
def transform_cdef(csource, crx_func_name):
outerblock = CodeBlock(parent=None)
@@ -59,6 +66,8 @@
if funcblock.crx_type_vars:
funcblock.writedecl("crx_type_t %s;" % (
', '.join(funcblock.crx_type_vars),))
+ for d1 in funcblock.crx_field_vars:
+ funcblock.writedecl("crx_field_t %s;" % d1)
outerblock.writeline('void %s(crx_builder_t *cb)' % (crx_func_name,))
outerblock.write_subblock(funcblock)
diff --git a/creflect/creflect.h b/creflect/creflect.h
--- a/creflect/creflect.h
+++ b/creflect/creflect.h
@@ -2,17 +2,17 @@
#include <string.h>
-typedef struct {
- const char *name;
- void *type;
- size_t offset;
- int numbits, bitshift;
-} crx_field_t;
-
typedef struct crx_type_s crx_type_t; /* opaque */
typedef void (*crx_trampoline0_fn)(void *args[], void *res);
typedef void (*crx_trampoline1_fn)(void *fn, void *args[], void *res);
+typedef struct {
+ const char *name;
+ crx_type_t *type;
+ size_t offset;
+ int numbits, bitshift;
+} crx_field_t;
+
typedef union {
int as_int;
long as_long;
diff --git a/creflect/model.py b/creflect/model.py
--- a/creflect/model.py
+++ b/creflect/model.py
@@ -559,20 +559,19 @@
insptp = '*%s' % ptrtp
include_alignment = False
#
- extra1 = "(long long)sizeof(%s)" % (sizetp,) # total size
+ extra1 = "sizeof(%s)" % (sizetp,) # total size
if include_alignment:
- extra2 = ("(long long)(((char *)&((struct{char a; %s b;} *)0)->b)"
- " - (char *)0)" % (realtp,)) # alignment
- funcblock.sprintf(tp + r" /*%lld,%lld*/{\n",
- extra="%s, %s" % (extra1, extra2),
- extralength=40)
+ extra2 = ("(((char *)&((struct{char a; %s b;} *)0)->b)"
+ " - (char *)0)" % (realtp,)) # alignment
else:
- funcblock.sprintf(tp + r" /*%lld*/{\n",
- extra=extra1,
- extralength=20)
+ extra2 = '0'
+ d1 = funcblock.write_crx_field_var(len(self.fldnames))
+ t1 = funcblock.write_crx_type_var('cb->get_%s_type(cb, "%s")' % (
+ self.type.kind, self.type.name))
#
- for fldname, fldtype in zip(self.fldnames, self.fldtypes):
- block = CodeBlock(funcblock.tr)
+ for i, (fldname, fldtype) in enumerate(
+ zip(self.fldnames, self.fldtypes)):
+ block = CodeBlock(funcblock)
inspect = TypeInspector(block, insptp, fldname)
inspect.start()
# get the offset of the field
@@ -587,22 +586,27 @@
if arraylevels >= 1:
comment = " (%dx)" % arraylevels
comment = inspect.get_comment(0, False, "an array%s" % comment)
- block.writedecl("long long o = offsetof(%s, %s%s);%s"
+ block.writedecl("size_t o = offsetof(%s, %s%s);%s"
% (realtp, fldname, "[0]" * arraylevels,
comment))
else:
comment = inspect.get_comment(0, False, "not an array")
- block.writedecl("long long o = ((char *)&((%s)0)->%s)"
+ block.writedecl("size_t o = ((char *)&((%s)0)->%s)"
" - (char *)0;%s" % (ptrtp, fldname, comment))
#
- block.sprintf(" /*%lld*/", extra="o", extralength=20)
- block.sprintf_add_right(r';\n')
- fldtype.inspect_type(block, inspect)
+ t2 = fldtype.inspect_type(block, inspect)
inspect.stop()
- block.sprintf_add_left(fldname)
+ block.writeline('%s[%d].name = "%s";\n' % (d1, i, fldname))
+ block.writeline('%s[%d].type = %s;\n' % (d1, i, t2))
+ block.writeline('%s[%d].offset = o;\n' % (d1, i))
+ block.writeline('%s[%d].numbits = -1;\n' % (d1, i))
+ block.writeline('%s[%d].bitshift = -1;\n' % (d1, i))
funcblock.write_subblock(block)
#
- funcblock.sprintf(r"};\n")
+ funcblock.writeline('cb->complete(cb, %s, sizeof(%s),' % (t1, realtp))
+ funcblock.writeline(' ((char *)&((struct{char a; %s b;} *)'
+ '0)->b) - (char *)0,' % realtp)
+ funcblock.writeline(' %s, %d);' % (d1, len(self.fldnames)))
class TypeDefDecl(object):
diff --git a/test/cgcompile.c b/test/cgcompile.c
--- a/test/cgcompile.c
+++ b/test/cgcompile.c
@@ -143,7 +143,11 @@
size_t sz, size_t align,
crx_field_t fields[], int nfields)
{
- abort();
+ int i;
+ printf("%s:\n", t->text);
+ for (i = 0; i < nfields; i++) {
+ printf("| %s: %s\n", fields[i].name, fields[i].type->text);
+ }
}
static void tst_complete_enum(crx_builder_t *cb, crx_type_t *t,
diff --git a/test/codegen/struct-001.c b/test/codegen/struct-001.c
--- a/test/codegen/struct-001.c
+++ b/test/codegen/struct-001.c
@@ -14,7 +14,6 @@
struct foo_s *p1;
size_t o = ((char *)&((struct foo_s *)0)->aa) - (char *)0; /* check that 'struct foo_s::aa' is not an array */
char b[sizeof(p1->aa)];
- r += sprintf(r, " /*%lld*/", o);
p1 = (void *)(((char *)b) - o);
(void)(p1->aa << 1); /* check that 'struct foo_s::aa' is an integer type */
p1->aa = -1; /* check that 'struct foo_s::aa' is not declared 'const' */
@@ -27,20 +26,22 @@
}
{
struct foo_s *p1;
- long long o = ((char *)&((struct foo_s *)0)->bb) - (char *)0; /* check that 'struct foo_s::bb' is not an array */
+ size_t o = ((char *)&((struct foo_s *)0)->bb) - (char *)0; /* check that 'struct foo_s::bb' is not an array */
char b[sizeof(p1->bb)];
- r += sprintf(r, " /*%lld*/", o);
p1 = (void *)(((char *)b) - o);
(void)(p1->bb << 1); /* check that 'struct foo_s::bb' is an integer type */
p1->bb = -1; /* check that 'struct foo_s::bb' is not declared 'const' */
- t3 = CRX_INT_TYPE(cb, p1->bb, "int");
- d1[0].name = "bb";
- d1[0].type = t3;
- d1[0].offset = o;
- d1[0].numbits = -1;
- d1[0].bitshift = -1;
+ t3 = CRX_INT_TYPE(cb, p1->bb, "unsigned int");
+ d1[1].name = "bb";
+ d1[1].type = t3;
+ d1[1].offset = o;
+ d1[1].numbits = -1;
+ d1[1].bitshift = -1;
}
cb->complete(cb, t1, sizeof(struct foo_s),
- offsetof(struct{char a; struct foo_s b;}, b),
+ ((char *)&((struct{char a; struct foo_s b;} *)0)->b) - (char *)0,
d1, 2);
+#expect STRUCT foo_s:
+#expect | aa: int
+#expect | bb: unsigned int
}
More information about the pypy-commit
mailing list