[PATCH] Java: Fix alignment of CNI classes
Andrew Haley
aph@redhat.com
Wed Mar 5 16:30:00 GMT 2003
The new C++ ABI packs non-POD classes more tightly than it used to.
Consider this:
class foo // Please pretend this is non-POD for the sake of the example
{
void *p;
short n;
};
class bar : foo
{
short j;
}
The old ABI packed like this:
class bar
{
void *p;
short n;
// some padding to align to the size of the largest member of class foo
short j;
}
The new ABI omits the padding -- on a 32-bit machine this struct fits
neatly into 8 bytes.
gcj, however, still conforms to the old ABI. It would be good to
convert gcj to the new ABI, because it saves space. Difficult,
though, and impossible to do in the 3.3 timeframe.
So, for the time being I'm going to force CNI headers to align the
"old" way by adding explicit alignment. Like this:
class bar : foo
{
short __attribute__((aligned(__alignof__(foo)))) j;
}
This should work, right?
Andrew.
2003年03月04日 Andrew Haley <aph@redhat.com>
* gjavah.c (is_first_data_member): New global variable.
(print_c_decl): If it's the first data member, align it as the
superclass.
(process_file): Set is_first_data_member.
Index: gjavah.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/gjavah.c,v
retrieving revision 1.104
diff -c -2 -p -r1.104 gjavah.c
*** gjavah.c 1 Feb 2003 01:31:31 -0000 1.104
--- gjavah.c 5 Mar 2003 15:10:28 -0000
*************** static int method_synthetic = 0;
*** 198,201 ****
--- 198,204 ----
static int method_signature = 0;
+ /* Set to 1 while the very first data member of a class is being handled. */
+ static int is_first_data_member = 0;
+
#define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
{ \
*************** print_c_decl (FILE* stream, JCF* jcf, in
*** 1384,1387 ****
--- 1387,1401 ----
}
+ /* Force the alignment of the first data member. This is
+ because the "new" C++ ABI changed the alignemnt of non-POD
+ classes. gcj, however, still uses the "old" alignment. */
+ if (is_first_data_member && ! (flags & ACC_STATIC) && ! is_method)
+ {
+ is_first_data_member = 0;
+ print_cxx_classname (out, " __attribute__((aligned(__alignof__( ",
+ jcf, jcf->super_class, 1);
+ fputs (" )))) ", stream);
+ }
+
/* Now print the name of the thing. */
if (need_space)
*************** process_file (JCF *jcf, FILE *out)
*** 2089,2092 ****
--- 2103,2108 ----
/* Now go back for second pass over methods and fields. */
+ is_first_data_member = 1;
+
JCF_SEEK (jcf, method_start);
method_pass = 1;
More information about the Java
mailing list