#297 value in registers overwritten


Here's some code to reproduce a register allocation bug
where a temporary value overwrites a variable, leading
to wrong results. I have only been able to reproduce
the problem with large memory model.

The variable "next_block" is allocated to R2/R3 and is
used inside a for loop. The loop's execution
overwrites R2/R3 each time the loop is re-run, which
causes the program to assign a wrong value if the
"next_block" variable is used after the first iteration
of the loop.

You can also download a copy of the code here:


#error "This bug only shows up in large memory model"

struct simple_list_struct {
unsigned int next;

static data unsigned int first_block=0;
static xdata at 0x7000 struct simple_list_struct p7;

void test(unsigned int val)
unsigned int block, next_block;

next_block = p7.next + 1234; /* assigned to
r2/r3 */

for (block = first_block; block != 0; block =
p7.next) {
if (p7.next == val) {
p7.next = next_block;
/* BUG: "block = p7.next" overwrites
r2/r3 */


  • Sandeep Dutta

    Sandeep Dutta - 2002-02-20

    Logged In: YES

    Fixed please update SDCCloop.c (fix). SDCCcflow.c (removed
    temp fix)

  • Sandeep Dutta

    Sandeep Dutta - 2002-02-20
    • milestone: --> 100457
    • assigned_to: nobody --> sandeep
    • status: open --> closed-fixed
  • Johan Knol

    Johan Knol - 2003-04-04
    • status: closed-fixed --> open-fixed
  • Johan Knol

    Johan Knol - 2003-04-04
    • labels: 101550 --> Live range problems
    • milestone: 100457 --> 100454
    • priority: 5 --> 9
    • assigned_to: sandeep --> johanknol
    • status: open-fixed --> open-accepted
  • Johan Knol

    Johan Knol - 2003-04-04

    Logged In: YES

    This wasn't a good fix. Infact it creates even bigger problems
    e.g. in gets.c where str[r567] and count[r01] weren't saved
    over the putchar() call after the return in the '\r' case where
    their liverange ended. Getchar() doesn't use these registers
    so it was never noticed, but still ...

    Undid in SDCCloop.c:1.32

    A better fix is on it's way.

  • Erik Petrich

    Erik Petrich - 2003-12-16
    • milestone: 100454 --> fixed
    • status: open-accepted --> closed-fixed
  • Erik Petrich

    Erik Petrich - 2003-12-16

    Logged In: YES

    I have verified that the new live range code (specifically,
    Klaus's 2003/10/28 cvs commit) has fixed this bug.


Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks