Mat Hostetter <[EMAIL PROTECTED]> writes:

> I'm using x86 RedHat 6.1, both egcs-2.91.66 and gcc-2.95.2,
> and the binutils-2.9.1.0.23-6 RPM.
> 
> I'm using C as the back end for a compiler.  One of the things this
> compiler generates is a jump table consisting of an array of x86 `jmp'
> instructions.  Machine-generated C code calls through this table like
> this:
> 
>   extern const char jump_table[];
>   typedef void (*func_ptr)(void);
>   ....
>   ((func_ptr)&jump_table[256])();
> 
> This works fine unless I both optimize and compile with -fPIC.  When I
> compile that way (with both the call and the jump table are in the
> same file, FWIW), the above code compiles to this:

Write valid C and you have a right to complain.
As is this is invalid C, and the compiler can do anything it feels like.

In particular you should write your code more like:
typdef void (*func_ptr)(void);
extern func_ptr jump_table[];
jump_table[256]();

I can't say for this exact situation but there are optimizations
in gcc 2.95 and possibly before that take the type of the argument into
consideration.  

I know of no garantees in standard C that allow you to assume you can
cast a character pointer into the address of code.

To me the code just looks silly and bad.
If it has a usefulness you might try asking how to do X.
Instead of saying when I rely on non portable code the 
compiler doesn't do what I expect.

Eric

Reply via email to