public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Question regarding the values of labels
@ 2011-11-01 23:52 Matthew Plant
  2011-11-02  0:56 ` Ian Lance Taylor
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Matthew Plant @ 2011-11-01 23:52 UTC (permalink / raw)
  To: gcc-help

Hello there people who are much smarter than me,

A little background on my problem:
I'm trying to implement a simple JIT compiler, and to do this I
decided the best way would be by using the GNU GCC unary && operator.
Instead of having to write target assembly for each platform my
program is going to be on (along with writing a runtime assembler,
which I do not want to do), I figured I'd take sections of C code
wrapped in labels like so:

block_begin:
 {
   <instruction code>
 }
block_end:;

Then I can simply copy the bytes between block_begin and block_end
into an allocated char array, repeat as necessary for as many blocks
as I am wont, finally cast to a function pointer and voilà, I have a
JIT compiler. Of course control flow is incredibly messy (or an offset
has to be added to all the jump addresses), but it's still decently
simple.

Unfortunately, as I expected it would be, it wasn't actually that
simple. Perhaps there is some inherent flaw in my thinking, but for
now, on to the problem that occurs:

For some reason, the location of labels in memory changes depending on
the control flow of the program. I have this test program which just
prints the addresses of the labels in the program:

#include <stdio.h>
#include <stdlib.h>
int main ()
{
  int i;
 lbl1:
  i = 32;
 lbl2:
  i += 4;
 lbl3:
  i += 5;
  printf ("%lu, %lu, %lu, %lu\n", main, &&lbl1, &&lbl2, &&lbl3);
  return 0;
}

A sample run prints out something like 4195524, 4195532, 4195539,
4195543. That's all good and dandy. The labels are all a reasonable
distance from each other and there are no repeat values.
But say we decide to alter the control flow a bit:

#include <stdio.h>
#include <stdlib.h>
int main ()
{
  int i;
  goto lbl3;
 lbl1:
  i = 32;
  goto done;
 lbl2:
  i += 4;
 lbl3:
  i += 5;
  printf ("%lu, %lu, %lu, %lu\n", main, &&lbl1, &&lbl2, &&lbl3);
 done:;
  return 0;
}

A resulting sample output is 4195524, 4195533, 4195532, 4195533. What
the heck?! Two of the labels have the same address, and the second
label comes before the third and first! This was compiled without any
optimizations and the values did not differ upon adding
__attribute__((__noinline__,__noclone__)) or setting the addresses to
static variables.

So my final question is this: what determines the addresses of labels?
Can this problem be fixed with at least semi-readable code?

Any help would be greatly appreciated.

Sincerely,
Matt

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

* Re: Question regarding the values of labels
  2011-11-01 23:52 Question regarding the values of labels Matthew Plant
  2011-11-02  0:56 ` Ian Lance Taylor
@ 2011-11-02  0:56 ` Ian Lance Taylor
  2011-11-02 10:04 ` Andrew Haley
  2 siblings, 0 replies; 4+ messages in thread
From: Ian Lance Taylor @ 2011-11-02  0:56 UTC (permalink / raw)
  To: Matthew Plant; +Cc: gcc-help

Matthew Plant <rookie.mp@gmail.com> writes:

> So my final question is this: what determines the addresses of labels?
> Can this problem be fixed with at least semi-readable code?

Speaking very roughly, gcc separates the code into basic blocks and
builds a control flow graph.  The blocks in the control flow graph are
sorted based on the predicted results of branches.  The blocks are then
output in the order.

Nothing like what you want to do is possible with gcc.  You are assuming
that there is a natural correspondence between your source code and
gcc's internal representation; that is a natural thing assumption, but
in actual fact no such correspondence exists.

Ian

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

* Re: Question regarding the values of labels
  2011-11-01 23:52 Question regarding the values of labels Matthew Plant
@ 2011-11-02  0:56 ` Ian Lance Taylor
  2011-11-02  0:56 ` Ian Lance Taylor
  2011-11-02 10:04 ` Andrew Haley
  2 siblings, 0 replies; 4+ messages in thread
From: Ian Lance Taylor @ 2011-11-02  0:56 UTC (permalink / raw)
  To: Matthew Plant; +Cc: gcc-help

Matthew Plant <rookie.mp@gmail.com> writes:

> So my final question is this: what determines the addresses of labels?
> Can this problem be fixed with at least semi-readable code?

Speaking very roughly, gcc separates the code into basic blocks and
builds a control flow graph.  The blocks in the control flow graph are
sorted based on the predicted results of branches.  The blocks are then
output in the order.

Nothing like what you want to do is possible with gcc.  You are assuming
that there is a natural correspondence between your source code and
gcc's internal representation; that is a natural assumption, but in
actual fact no such correspondence exists.

Ian

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

* Re: Question regarding the values of labels
  2011-11-01 23:52 Question regarding the values of labels Matthew Plant
  2011-11-02  0:56 ` Ian Lance Taylor
  2011-11-02  0:56 ` Ian Lance Taylor
@ 2011-11-02 10:04 ` Andrew Haley
  2 siblings, 0 replies; 4+ messages in thread
From: Andrew Haley @ 2011-11-02 10:04 UTC (permalink / raw)
  To: gcc-help

On 11/01/2011 11:52 PM, Matthew Plant wrote:
> So my final question is this: what determines the addresses of labels?
> Can this problem be fixed with at least semi-readable code?
> 
> Any help would be greatly appreciated.

Have a look at JamVM.  http://jamvm.sourceforge.net/

Andrew.

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

end of thread, other threads:[~2011-11-02 10:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-11-01 23:52 Question regarding the values of labels Matthew Plant
2011-11-02  0:56 ` Ian Lance Taylor
2011-11-02  0:56 ` Ian Lance Taylor
2011-11-02 10:04 ` Andrew Haley

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).