On Mon, 13 Jun 2005, Andy Dougherty wrote:

> On Mon, 13 Jun 2005, Leopold Toetsch via RT wrote:
> 
> > Chip Salzenberg wrote:
> > > On Mon, Jun 13, 2005 at 02:57:09PM -0400, Andy Dougherty wrote:
> > 
> > >>Yes.  The compiler does the right thing.  It sensibly reports 
> > >>that sizeof(PMC) = 24 for SPARC.
> > > 
> > > Then I remain puzzled how Parrot could ever misalign a double.
> > 
> > Yes. So I am. Could somone please run this PASM snippet in a debugger so 
> > that we can see, where it fails.
> 
> Here's where it fails:
> 
> (dbx) run ../num2.pasm
> (process id 7721)
> [EMAIL PROTECTED] ([EMAIL PROTECTED]) signal BUS (invalid address alignment) 
> in Parrot_set_n_nc (optimized) at line 149 in file "set.ops"
> 149     goto NEXT();
> (dbx) where
> current thread: [EMAIL PROTECTED]
> =>[1] Parrot_set_n_nc(cur_opcode = ???, interpreter = ???) (optimized), at 
> 0xa9160 (line ~149) in "set.ops"
> [2] runops_slow_core(interpreter = ???, pc = ???) (optimized), at 0xdd2a0 
> (line ~105) in "runops_cores.c"
> [3] runops_int(interpreter = ???, offset = ???) (optimized), at 0xdbc70 (line 
> ~657) in "interpreter.c"
> [4] runops(interpreter = ???, offs = ???) (optimized), at 0xdc6d4 (line ~40) 
> in "inter_run.c"
> [5] Parrot_runcode(interpreter = ???, argc = ???, argv = ???) (optimized), at 
> 0x8482c (line ~757) in "embed.c"
> [6] Parrot_runcode(interpreter = ???, argc = ???, argv = ???) (optimized), at 
> 0x846ac (line ~757) in "embed.c"
> [7] main(argc = ???, argv = ???) (optimized), at 0x68058 (line ~448) in 
> "main.c"
> 
> Alas, since this is an optimized compile, there is no single stepping in the 
> debugger.

The following patch fixes the core dump for me.  The problem appears to be 
in <stacks.h>, where the 'data' segment is declared as a (void *), but is
then assumed to be aligned suitably for holding a double.  I fixed that 
with a union.  The compiler then automatically assures that the elements 
are aligned.  I still need to go through places where the data element is 
assigned to various things through casts and check whether any additional 
members to the union ought to be added.

While hunting for a solution, I saw a lot of places in the code that 
appear to be worried about alignment.  In many cases it was not clear to 
me what needs to be aligned with what and why.  Is it for alignment of 
doubles?  Then we should use a double type where appropriate.  Is it for 
alignment of one structure with another (like perl's sv/av/hv/ 
structures)?  If so, that should be documented carefully somewhere.  Is it 
for alignment with blocks returned by memalign?  I didn't know, so I put 
in questions in the comments.

Gcc currently gives the warning: 
    cast increases required alignment of target type 7,151 times.  One of 
those was actually the key to solving this problem.  Eventually, it'd be 
nice to get rid of the other 7150, even if they are all false alarms.


diff -r -u parrot-orig/include/parrot/debug.h parrot-andy/include/parrot/debug.h
--- parrot-orig/include/parrot/debug.h  Mon Jun  6 09:07:45 2005
+++ parrot-andy/include/parrot/debug.h  Tue Jun 14 14:01:06 2005
@@ -52,8 +52,8 @@
 typedef struct PDB_condition {
     unsigned short          type;
     unsigned char           reg;
-    unsigned char           dummy;     /* For alignment */
-    void                    *value;
+    unsigned char           dummy;     /* For alignment XXX ??  */
+    void                    *value;     /* What neeeds to be aligned with 
what? */
     PDB_condition_ptr       next;
 } PDB_condition_t;
 
diff -r -u parrot-orig/include/parrot/jit.h parrot-andy/include/parrot/jit.h
--- parrot-orig/include/parrot/jit.h    Mon Jun  6 09:08:05 2005
+++ parrot-andy/include/parrot/jit.h    Tue Jun 14 14:02:54 2005
@@ -30,8 +30,8 @@
     int                         type;
     ptrdiff_t                   native_offset;
     char                        skip;
-    char                        dummy[3]; /* For alignment */
-    union {
+    char                        dummy[3]; /* For alignment ??? XXX */
+    union {                               /* What has to align with what? */
         opcode_t                opcode;
         void                    (*fptr)(void);
     } param;
@@ -124,8 +124,8 @@
     char                                 isjit;
     char                                 done;
     char                                 ins_count;
-    char                                 dummy; /* For alignment */
-    int                                  block;
+    char                                 dummy; /* For alignment ??? XXX */
+    int                                  block; /* What has to align with 
what? */
     Parrot_jit_optimizer_section_ptr     branch_target;
     Parrot_jit_optimizer_section_ptr     prev;
     Parrot_jit_optimizer_section_ptr     next;
@@ -151,8 +151,8 @@
     char                            *map_branch;
     opcode_t                       **branch_list;
     unsigned char                    has_unpredictable_jump;
-    unsigned char                    dummy[3]; /* For alignment */
-} Parrot_jit_optimizer_t;
+    unsigned char                    dummy[3]; /* For alignment ??? XXX */
+} Parrot_jit_optimizer_t;                      /* What has to align with what? 
*/
 
 /*  Parrot_jit_constant_pool_t
  *      Constants pool information.
diff -r -u parrot-orig/include/parrot/smallobject.h 
parrot-andy/include/parrot/smallobject.h
--- parrot-orig/include/parrot/smallobject.h    Mon Jun  6 09:07:38 2005
+++ parrot-andy/include/parrot/smallobject.h    Tue Jun 14 14:04:34 2005
@@ -52,6 +52,7 @@
  *
  * XXX this could lead to unaligned FLOATVALs in the adjacent PMC
  *     if that's true either insert a dummy or reorder PMC members
+ *     ??? How is that possible? 
  */
 typedef struct _gc_gms_hdr {
     struct _gc_gms_hdr *prev;
diff -r -u parrot-orig/include/parrot/stacks.h 
parrot-andy/include/parrot/stacks.h
--- parrot-orig/include/parrot/stacks.h Mon Jun  6 09:08:06 2005
+++ parrot-andy/include/parrot/stacks.h Tue Jun 14 14:00:11 2005
@@ -28,13 +28,13 @@
     int size;
     const char * name;
     struct Stack_Chunk *prev;
-#if ! DISABLE_GC_DEBUG
-    void * dummy;   /* force 8 byte align */
-#endif
-    void *data;
+    union { /* force appropriate alignment of 'data' */
+       void *data;
+       double d_dummy; 
+    } u;
 } Stack_Chunk_t;
 
-#define STACK_DATAP(chunk)    &chunk->data
+#define STACK_DATAP(chunk)    &chunk->u.data
 /* #define STACK_ITEMSIZE(chunk) PObj_buflen(chunk) */
 
 
diff -r -u parrot-orig/src/stack_common.c parrot-andy/src/stack_common.c
--- parrot-orig/src/stack_common.c      Mon Jun  6 09:02:00 2005
+++ parrot-andy/src/stack_common.c      Tue Jun 14 13:15:13 2005
@@ -58,7 +58,7 @@
 {
     Stack_Chunk_t *chunk;
 
-    item_size += offsetof(Stack_Chunk_t, data);
+    item_size += offsetof(Stack_Chunk_t, u.data);
     item_size += 7;
     item_size &= ~7;    /* round up to 8 so that the chunk is aligned at
                            the same size - the aligned MMX memcpy needs it */

-- 
    Andy Dougherty              [EMAIL PROTECTED]


Reply via email to