public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/38295]  New: Support pointer difference as constant in static initializer
@ 2008-11-27 18:20 gnu at behdad dot org
  2008-11-27 18:30 ` [Bug c/38295] " pinskia at gcc dot gnu dot org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: gnu at behdad dot org @ 2008-11-27 18:20 UTC (permalink / raw)
  To: gcc-bugs

The feature I'm proposing is not supported by the C standard, so I'm proposing
a gcc extension.

I am wondering if it's possible to make the difference of two function pointer
be a constant value if the two functions are defined as static and in the same
file.  That would allow for function pointer tables that live in .rodata
instead of .data.  Something like:

#include <stdio.h>

typedef int (*func_t) (void);

static int func_base (void)
{
  return 0;
}

static int func0 (void)
{
  return 0;
}

static int func1 (void)
{
  return 1;
}

size_t funcs[] = {
  func0 - func_base,
  func1 - func_base
};

int
main (void)
{
  int i;

  for (i = 0; i < 2; i++)
    printf ("%d: %d\n", i, ((func_t)(funcs[i] + func_base))());
}


-- 
           Summary: Support pointer difference as constant in static
                    initializer
           Product: gcc
           Version: 4.3.2
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: gnu at behdad dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38295


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c/38295] Support pointer difference as constant in static initializer
  2008-11-27 18:20 [Bug c/38295] New: Support pointer difference as constant in static initializer gnu at behdad dot org
@ 2008-11-27 18:30 ` pinskia at gcc dot gnu dot org
  2008-11-27 18:32 ` gnu at behdad dot org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-11-27 18:30 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2008-11-27 18:28 -------
Differences between two different arrays don't make sense really.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38295


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c/38295] Support pointer difference as constant in static initializer
  2008-11-27 18:20 [Bug c/38295] New: Support pointer difference as constant in static initializer gnu at behdad dot org
  2008-11-27 18:30 ` [Bug c/38295] " pinskia at gcc dot gnu dot org
@ 2008-11-27 18:32 ` gnu at behdad dot org
  2008-11-27 18:33 ` gnu at behdad dot org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: gnu at behdad dot org @ 2008-11-27 18:32 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from gnu at behdad dot org  2008-11-27 18:31 -------
I'm not following.  Why arrays?  Those are pointers, and their difference is
known at compile time.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38295


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c/38295] Support pointer difference as constant in static initializer
  2008-11-27 18:20 [Bug c/38295] New: Support pointer difference as constant in static initializer gnu at behdad dot org
  2008-11-27 18:30 ` [Bug c/38295] " pinskia at gcc dot gnu dot org
  2008-11-27 18:32 ` gnu at behdad dot org
@ 2008-11-27 18:33 ` gnu at behdad dot org
  2008-11-27 18:34 ` pinskia at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: gnu at behdad dot org @ 2008-11-27 18:33 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from gnu at behdad dot org  2008-11-27 18:32 -------
Oh, I see what you mean.  Yes, I said in my report that this is
undefined/unsupported/... according to the C standard.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38295


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c/38295] Support pointer difference as constant in static initializer
  2008-11-27 18:20 [Bug c/38295] New: Support pointer difference as constant in static initializer gnu at behdad dot org
                   ` (2 preceding siblings ...)
  2008-11-27 18:33 ` gnu at behdad dot org
@ 2008-11-27 18:34 ` pinskia at gcc dot gnu dot org
  2008-11-27 18:36 ` gnu at behdad dot org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-11-27 18:34 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from pinskia at gcc dot gnu dot org  2008-11-27 18:33 -------
(In reply to comment #2)
> I'm not following.  Why arrays?  Those are pointers, and their difference is
> known at compile time.

Not at compile time really,  the difference is known at link time.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38295


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c/38295] Support pointer difference as constant in static initializer
  2008-11-27 18:20 [Bug c/38295] New: Support pointer difference as constant in static initializer gnu at behdad dot org
                   ` (3 preceding siblings ...)
  2008-11-27 18:34 ` pinskia at gcc dot gnu dot org
@ 2008-11-27 18:36 ` gnu at behdad dot org
  2008-11-27 18:53 ` pinskia at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: gnu at behdad dot org @ 2008-11-27 18:36 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from gnu at behdad dot org  2008-11-27 18:35 -------
If the two functions are in the same compilation unit (and static), it's known
at compile time, isn't it?


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38295


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c/38295] Support pointer difference as constant in static initializer
  2008-11-27 18:20 [Bug c/38295] New: Support pointer difference as constant in static initializer gnu at behdad dot org
                   ` (4 preceding siblings ...)
  2008-11-27 18:36 ` gnu at behdad dot org
@ 2008-11-27 18:53 ` pinskia at gcc dot gnu dot org
  2008-11-27 18:54 ` pinskia at gcc dot gnu dot org
  2008-11-27 18:56 ` gnu at behdad dot org
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-11-27 18:53 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from pinskia at gcc dot gnu dot org  2008-11-27 18:52 -------
(In reply to comment #5)
> If the two functions are in the same compilation unit (and static), it's known
> at compile time, isn't it?

Not always since they could be in different sections via -ffunction-sections or
the user put them into different sections.  The layout of the sections is not
known until link time.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38295


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c/38295] Support pointer difference as constant in static initializer
  2008-11-27 18:20 [Bug c/38295] New: Support pointer difference as constant in static initializer gnu at behdad dot org
                   ` (5 preceding siblings ...)
  2008-11-27 18:53 ` pinskia at gcc dot gnu dot org
@ 2008-11-27 18:54 ` pinskia at gcc dot gnu dot org
  2008-11-27 18:56 ` gnu at behdad dot org
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-11-27 18:54 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from pinskia at gcc dot gnu dot org  2008-11-27 18:53 -------
Also the linker could do some branch relaxation which causes the size to be
different based on the layout of the functions.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38295


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c/38295] Support pointer difference as constant in static initializer
  2008-11-27 18:20 [Bug c/38295] New: Support pointer difference as constant in static initializer gnu at behdad dot org
                   ` (6 preceding siblings ...)
  2008-11-27 18:54 ` pinskia at gcc dot gnu dot org
@ 2008-11-27 18:56 ` gnu at behdad dot org
  7 siblings, 0 replies; 9+ messages in thread
From: gnu at behdad dot org @ 2008-11-27 18:56 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from gnu at behdad dot org  2008-11-27 18:55 -------
If they are asked to be put in different sections, sure, it will err.  But
doesn't gcc already use relative calls for many static functions in the same
unit?

Let me back out:  my request is: add gcc extension to support some way to
implement vtables that do not need (many) relocations.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38295


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2008-11-27 18:56 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-11-27 18:20 [Bug c/38295] New: Support pointer difference as constant in static initializer gnu at behdad dot org
2008-11-27 18:30 ` [Bug c/38295] " pinskia at gcc dot gnu dot org
2008-11-27 18:32 ` gnu at behdad dot org
2008-11-27 18:33 ` gnu at behdad dot org
2008-11-27 18:34 ` pinskia at gcc dot gnu dot org
2008-11-27 18:36 ` gnu at behdad dot org
2008-11-27 18:53 ` pinskia at gcc dot gnu dot org
2008-11-27 18:54 ` pinskia at gcc dot gnu dot org
2008-11-27 18:56 ` gnu at behdad dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).