aboutsummaryrefslogtreecommitdiff
path: root/SingleSource/Regression/C/globalrefs.c
blob: 5afdbf5aa16dc6fe2c5defaf3cb8e4fb6741de99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* globalrefs.c - Test symbolic constant expressions constructed from
 * global addresses and index expressions into global addresses.
 * Do this both with global constants and with inline constant.
 * Instead of printing absolute addresses, print out the differences in
 * memory addresses to get output that matches that of the native compiler.
 */

#include <stdio.h>

#define __STDC_LIMIT_MACROS 1
#include <inttypes.h>

struct test {
  int A;
  struct { unsigned X; unsigned Y; } S;
  int B;
  struct test* next;
};

struct test  TestArray[10];
struct test  Test1;

/* Create global symbolic constants from the addresses of the above globals */

struct test* TestArrayPtr = &TestArray[3]; 
long*        Aptr         = &Test1.A;
unsigned*    Yptr         = &Test1.S.Y;
struct test** NextPtr     = &Test1.next;

void
printdiff(void* p1, void* p2)
{
  printf(" %d", (int)((unsigned long) p1 - (unsigned long) p2));
}

int
main(int argc, char** argv)
{
  unsigned long diff1, diff2, diff3, diff4; 

  printdiff(&Test1.S.Y, &Test1.A);
  printdiff(&Test1.next, &Test1.S.Y);
  printf("\n");

  diff1 = (unsigned long) &TestArray[3] - (unsigned long) TestArray;
  diff3 = (unsigned long) &Test1.S.Y - (unsigned long) &Test1.A;
  diff4 = (unsigned long) &Test1.next - (unsigned long) &Test1.S.Y;

  if (diff1 != (diff1 / sizeof(*TestArray)) * sizeof(*TestArray))
    return 1;
  printf("&TestArray[3] - TestArray = 0x%lx\n", diff1 / sizeof(*TestArray));
  printf("Xptr - Aptr          = 0x%lx\n", diff3);
  printf("NextPtr - Xptr       = 0x%lx\n\n", diff4);

  diff1 = (unsigned long) TestArrayPtr - (unsigned long) TestArray;
  diff3 = (unsigned long) Yptr - (unsigned long) Aptr;
  diff4 = (unsigned long) NextPtr - (unsigned long) Yptr;

  if (diff1 != (diff1 / sizeof(*TestArray)) * sizeof(*TestArray))
    return 1;
  printf("&TestArray[3] - TestArray = 0x%lx\n", diff1 / sizeof(*TestArray));
  printf("Xptr - Aptr          = 0x%lx\n", diff3);
  printf("NextPtr - Xptr       = 0x%lx\n\n", diff4);

  return 0;
}