drmortalwombat / oscar64

Optimizing Small memory C Compiler Assembler and Runtime for C64
GNU General Public License v3.0
263 stars 23 forks source link

On the fly structure instance is recycled? #107

Open AGPX opened 5 days ago

AGPX commented 5 days ago

The following test doesn't pass. Looks like that the struct with "hi" and 1, is created only once and recycled (optimization?) and so the second time foo is invoked, the t is 2. If you replace the function bar with the following, the test passes:

void
bar (void)
{
    struct s x = { "hi", 1 };
  foo (&x);
}

Here the code:

#include "common.h"
struct s { const char *p; int t; };

void bar (void);
void foo (struct s *);

int main(void)
{
  bar ();
  bar ();
  exit (0);
}

void
bar (void)
{
  foo (& (struct s) { "hi", 1 });
}

void foo (struct s *p)
{
  if (p->t != 1)
    abort();
  p->t = 2;
}

with common.h:

#pragma once

#include <stdio.h>
#include <string.h>

#define register

#define double float

#define __attribute__(x)

#define __complex__

#define NULL (void *)0

static void abort()
{
    printf("** TEST FAILED **!\n");
}

static void abort2(const char *msg)
{
    printf("%s\n", msg);
    printf("** TEST FAILED **!\n");
}
AGPX commented 5 days ago

Same issue here:

#include "common.h"

struct s
{
    int value;
    const char *string;
};

int main (void)
{
  int i;
  for (i = 0; i < 4; i++)
    {
      struct s *t = & (struct s) { 3, "hey there" };
      if (t->value != 3)
    abort();
      t->value = 4;
      if (t->value != 4)
    abort();
    }
  exit (0);
}