PpHd :
Encore une superbe victoire de canard
!
struct zz {
char *str;
char a,b,c,d;
};
int toto()
{
const struct zz *x = &(const struct zz) {.str= NULL, .a = 1, .b=2, .c=3, .d=4};
return x->a+x->d;
}
mouline par: gcc -O2 -S test.c donne:
.p2align 2,,3
.globl toto
.type toto, @function
toto:
movb $1, %dl
movb $2, %dh
movl %edx, %ecx
andl $-16711681, %ecx
movl %ecx, %edx
orl $196608, %edx
movl %edx, %ecx
andl $16777215, %ecx
pushl %ebp
movl %ecx, %edx
movl %esp, %ebp
orl $67108864, %edx
subl $8, %esp
xorl %eax, %eax
movl %edx, -4(%ebp)
movl %eax, -8(%ebp)
movsbl -1(%ebp),%edx
movsbl -4(%ebp),%eax
addl %edx, %eax
leave
ret
On applaudit bien fort! 
Avec
TIGCC, j'ai:
.LC0:
.long 0
.byte 1
.byte 2
.byte 3
.byte 4
#NO_APP
.text
.even
.globl toto
toto:
lea (.LC0:w,%pc),%a0
move.b 4(%a0),%d0
ext.w %d0
move.b 7(%a0),%d1
ext.w %d1
add.w %d1,%d0
rts
Il faudra que tu appliques ça à ton GCC si tu veux avoir du code convenable:
diff -Naur gcc-3.3.3.orig/gcc/c-decl.c gcc-3.3.3.tigcc1/gcc/c-decl.c
--- gcc-3.3.3.orig/gcc/c-decl.c Sat Sep 6 17:44:14 2003
+++ gcc-3.3.3.tigcc1/gcc/c-decl.c Thu Feb 19 22:20:00 2004
@@ -3279,6 +3353,21 @@
tree type;
tree init;
{
+ /* (TIGCC) If -fglobal-compound-literals (on by default) is given, for
+ constant constructors, the compound literal is just the constructor itself.
+ For variable constructors, we use the new code, since they have to be
+ created on the stack anyway, and since the new code allows taking their
+ address. */
+if (flag_global_compound_literals && TREE_CONSTANT (init)) {
+ if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
+ {
+ int failure = complete_array_type (type, init, 1);
+ if (failure)
+ abort ();
+ }
+
+ return init;
+} else {
/* We do not use start_decl here because we have a type, not a declarator;
and do not use finish_decl because the decl should be stored inside
the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
@@ -3329,6 +3418,7 @@
}
return complit;
+}
}
/* Make TYPE a complete type based on INITIAL_VALUE.
diff -Naur gcc-3.3.3.orig/gcc/c-typeck.c gcc-3.3.3.tigcc1/gcc/c-typeck.c
--- gcc-3.3.3.orig/gcc/c-typeck.c Tue Dec 23 07:23:32 2003
+++ gcc-3.3.3.tigcc1/gcc/c-typeck.c Thu Feb 19 22:20:02 2004
@@ -876,7 +873,10 @@
}
lvalue_array_p = !not_lvalue && lvalue_p (exp);
- if (!flag_isoc99 && !lvalue_array_p)
+
+ if (!flag_isoc99 && !lvalue_array_p
+ && !(flag_global_compound_literals
+ && (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp))) )
{
/* Before C99, non-lvalue arrays do not decay to pointers.
Normally, using such an array would be invalid; but it can
@@ -3033,6 +3026,12 @@
}
#endif
+ /* (TIGCC) In global compound literal mode, allow the address of a
+ constructor if all the elements are constant. */
+ if (flag_global_compound_literals
+ && (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg)))
+ ;
+
/* Anything not already handled and not a true memory reference
or a non-lvalue array is an error. */
else if (typecode != FUNCTION_TYPE && !flag
diff -Naur gcc-3.3.3.orig/gcc/flags.h gcc-3.3.3.tigcc1/gcc/flags.h
--- gcc-3.3.3.orig/gcc/flags.h Sat Jun 21 00:18:40 2003
+++ gcc-3.3.3.tigcc1/gcc/flags.h Thu Feb 19 22:20:02 2004
@@ -690,4 +690,7 @@
#define HONOR_SIGN_DEPENDENT_ROUNDING(MODE) \
(MODE_HAS_SIGN_DEPENDENT_ROUNDING (MODE) && !flag_unsafe_math_optimizations)
+/* (TIGCC) Make compound literals (cast constructors) global for backwards compatibility. */
+extern int flag_global_compound_literals;
+
#endif /* ! GCC_FLAGS_H */
diff -Naur gcc-3.3.3.orig/gcc/toplev.c gcc-3.3.3.tigcc1/gcc/toplev.c
--- gcc-3.3.3.orig/gcc/toplev.c Tue Dec 23 07:28:28 2003
+++ gcc-3.3.3.tigcc1/gcc/toplev.c Thu Feb 19 22:20:04 2004
@@ -883,6 +883,9 @@
int flag_tracer = 0;
+/* (TIGCC) Make compound literals (cast constructors) global for backwards compatibility. */
+int flag_global_compound_literals = 1;
+
/* Values of the -falign-* flags: how much to align labels in code.
0 means `use default', 1 means `don't align'.
For each variable, there is an _log variant which is the power@@ -1184,10 +1187,14 @@
N_("Report time taken by each compiler pass at end of run") },
{"mem-report", &mem_report, 1,
N_("Report on permanent memory allocation at end of run") },
- { "trapv", &flag_trapv, 1,
+ {"trapv", &flag_trapv, 1,
N_("Trap for signed overflow in addition / subtraction / multiplication") },
- { "new-ra", &flag_new_regalloc, 1,
+ {"new-ra", &flag_new_regalloc, 1,
N_("Use graph coloring register allocation.") },
+ {"global-compound-literals", &flag_global_compound_literals, 1,
+ N_("Make compound literals (cast constructors) global for backwards compatibility") },
+ {"global-cast-constructors", &flag_global_compound_literals, 1,
+ N_("Make compound literals (cast constructors) global for backwards compatibility") },
};
/* Table of language-specific options. */
Et si tu veux avoir le tout carrément optimisé en un
return 5;, il faudra attendre la scalarisation des structures qui est implémentée dans la branche de développement
tree-ssa.