[Bug c++/52477] New: Wrong initialization order? __attribute__((constructor)) vs static data access

2012-03-04 Thread przemoc at gmail dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52477

 Bug #: 52477
   Summary: Wrong initialization order?
__attribute__((constructor)) vs static data access
Classification: Unclassified
   Product: gcc
   Version: 4.7.0
Status: UNCONFIRMED
  Severity: major
  Priority: P3
 Component: c++
AssignedTo: unassig...@gcc.gnu.org
ReportedBy: prze...@gmail.com


Created attachment 26821
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=26821
Short exemplary code showing the problem

Attached file compiles flawlessly on 4.7.0, but the output binary segfaults.
Works fine in 4.6.2 though (as expected).

Looks like a serious regression.

g++-4.7 (GCC) 4.7.0 20120304 (prerelease)
built on debian wheezy x64 with:
--enable-languages=c,c++ --prefix=/opt/gcc-4.7 --program-suffix=-4.7

svn+ssh://gcc.gnu.org/svn/gcc/branches/gcc-4_7-branch@184878


[Bug c++/52477] Wrong initialization order? __attribute__((constructor)) vs static data access

2012-03-04 Thread przemoc at gmail dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52477

--- Comment #3 from Przemysław Pawełczyk  2012-03-04 
14:24:10 UTC ---
Thanks for solution, but...

Isn't such order obvious or isn't it at least the most widely used one? I mean
that by default static data initialization should precede
constructor-functions, no? It worked in gcc 4.6 and is there any good reason to
break it?


[Bug c/28575] misleading __builtin_choose_expr documentation error

2010-11-29 Thread przemoc at gmail dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28575

Przemysław Pawełczyk  changed:

   What|Removed |Added

 CC||przemoc at gmail dot com

--- Comment #4 from Przemysław Pawełczyk  2010-11-29 
18:06:57 UTC ---
I'm also curious why the trivial fix has not been applied yet.

--- a/gcc-4.5.1/gcc/doc/extend.texi2010-11-29 18:39:21.0 +0100
+++ b/gcc-4.5.1/gcc/doc/extend.texi2010-11-29 18:39:54.0 +0100
@@ -7026,7 +7026,7 @@
 You can use the built-in function @code{__builtin_choose_expr} to
 evaluate code depending on the value of a constant expression.  This
 built-in function returns @var{exp1} if @var{const_exp}, which is an
-integer constant expression, is nonzero.  Otherwise it returns 0.
+integer constant expression, is nonzero.  Otherwise it returns @var{exp2}.

 This built-in function is analogous to the @samp{? :} operator in C,
 except that the expression returned has its type unaltered by promotion


[Bug c/46711] New: __builtin_choose_expr checks not chosen expression

2010-11-29 Thread przemoc at gmail dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46711

   Summary: __builtin_choose_expr checks not chosen expression
   Product: gcc
   Version: 4.4.5
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c
AssignedTo: unassig...@gcc.gnu.org
ReportedBy: prze...@gmail.com


Created attachment 22569
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=22569
Test case.

Built-in Function: type __builtin_choose_expr (const_exp, exp1, exp2)

Excerpts from doc:

This built-in function is analogous to the `? :' operator in C, except that the
expression returned has its type unaltered by promotion rules. Also, the
built-in function does not evaluate the expression that was not chosen.

Note: This construct is only available for C. Furthermore, the unused
expression (exp1 or exp2 depending on the value of const_exp) may still
generate syntax errors. This may change in future revisions.

-- http://gcc.gnu.org/onlinedocs/gcc-4.5.1/gcc/Other-Builtins.html


#include 

int main()
{
int a = 0;

printf("a is %s\n", 
__builtin_choose_expr(__builtin_constant_p(a),
__builtin_choose_expr((a) == 0,
"constant 0",
__builtin_choose_expr((a) == 1,
"constant 1",
"constant non-{0,1}"
)
),
"non-constant"
)
);

return 0;
}
(attached also as a file)

__builtin_choose_expr.c: In function ‘main’:
__builtin_choose_expr.c:11: error: first argument to ‘__builtin_choose_expr’
not a constant
__builtin_choose_expr.c:9: error: first argument to ‘__builtin_choose_expr’ not
a constant


It's theoretically correct according to the note mentioned before, but it
rather should compile without errors.
[ BTW when you run gcc with optimization, i.e. -On, where n > 0, it shows also
a bug 19449:
__builtin_choose_expr.c:8: error: first argument to ‘__builtin_choose_expr’ not
a constant ]

Expressions are checked in bottom-up order, which is fine, but only for
const_exp part. exp1 or exp2 shall be checked only if const_exp is true or
false respectively, avoiding the not chosen expression.

Example I gave you above is simple and imaginary, but it shows that
__builtin_choose_expr combined with other builtins (such as
__builtin_constant_p) can be useful. Moreover, as it can return lvalue, it's
cannot be easily replaced without introducing function with ifs or switch and
returning pointer to some type.


Better example (linux kernel-space):

#ifdef CONFIG_X86_64
# define PARAM0(regs) (regs)->di
# define PARAM1(regs) (regs)->si
# define PARAM2(regs) (regs)->dx
# define PARAM3(regs) (regs)->r10
# define PARAM4(regs) (regs)->r8
# define PARAM5(regs) (regs)->r9
static inline unsigned long *
_param(struct pt_regs *regs, int num)
{
switch (num) {
case 0: return &PARAM0(regs);
case 1: return &PARAM1(regs);
case 2: return &PARAM2(regs);
case 3: return &PARAM3(regs);
case 4: return &PARAM4(regs);
case 5: return &PARAM5(regs);
}

/* should not happen */
return NULL;
}
# define PARAM(regs, num) \
__builtin_choose_expr(__builtin_constant_p((num)), \
__builtin_choose_expr((num) == 0, PARAM0(regs), \
__builtin_choose_expr((num) == 1, PARAM1(regs), \
__builtin_choose_expr((num) == 2, PARAM2(regs), \
__builtin_choose_expr((num) == 3, PARAM3(regs), \
__builtin_choose_expr((num) == 4, PARAM4(regs), \
__builtin_choose_expr((num) == 5, PARAM5(regs), 0)), \
*_param(regs, num)
)
#endif

PARAM can be even simplified:

# define PARAM(regs, num) \
__builtin_choose_expr(__builtin_constant_p(num), \
__builtin_choose_expr((num) < 6, \
PARAM##num(regs), \
0 \
), \
*_param(regs, num)) \
)

In either case it unfortunately doesn't work now.
Theoretically I can use the function directly, but it isn't inlined with known
result for constant expressions.


[Bug c/46711] __builtin_choose_expr checks not chosen expression

2010-11-29 Thread przemoc at gmail dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46711

--- Comment #3 from Przemysław Pawełczyk  2010-11-29 
20:07:20 UTC ---
Andrew, thanks for the always_inline hint.
But weren't you too fast with marking PR 46711 as duplicate of PR 19449?

Check which lines are marked with errors. 8th line is not mentioned (unless you
turn on optimization), so it's not the same problem, right? Am I missing
something?

The problem here is with ((a) == 0) and ((a) == 1) obviously not being constant
expressions, because a alone is not a constant expression. And this problem
derive from needless checking not chosen expression.

PR 19449 is problematic too, but it's other thing.

(I am not changing the status right now, because I believe that you'll step in
and explain me why I am wrong or admit that you were not careful enough.)