1 /* This test script is part of GDB, the GNU debugger.
2 
3    Copyright 1999, 2004,
4    Free Software Foundation, Inc.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19    */
20 
21 /* Test long long expression; test printing in general.
22  *
23  * /CLO/BUILD_ENV/Exports/cc -g +e -o long_long long_long.c
24  *
25  * or
26  *
27  * cc +e +DA2.0 -g -o long_long long_long.c
28  */
29 
30 #include <string.h>
31 
32 enum { MAX_BYTES = 16 };
33 
34 void
35 pack (unsigned char b[MAX_BYTES], int size, int nr)
36 {
37   static long long val[] = { 0x123456789abcdefLL, 01234567123456701234567LL, 12345678901234567890ULL};
38   volatile static int e = 1;
39   int i;
40   for (i = 0; i < nr; i++)
41     {
42       int offset;
43       if (*(char *)&e)
44 	/* Little endian.  */
45 	offset = sizeof (long long) - size;
46       else
47 	/* Big endian endian.  */
48 	offset = 0;
49       memcpy (b + size * i, (char *) val + sizeof (long long) * i + offset, size);
50     }
51 }
52 
53 unsigned char b[MAX_BYTES];
54 unsigned char h[MAX_BYTES];
55 unsigned char w[MAX_BYTES];
56 unsigned char g[MAX_BYTES];
57 
58 unsigned char c[MAX_BYTES];
59 unsigned char s[MAX_BYTES];
60 unsigned char i[MAX_BYTES];
61 unsigned char l[MAX_BYTES];
62 unsigned char ll[MAX_BYTES];
63 
64 int known_types()
65 {
66   /* A union is used here as, hopefully it has well defined packing
67      rules.  */
68   struct {
69     long long bin, oct, dec, hex;
70   } val;
71   memset (&val, 0, sizeof val);
72 
73   /* Known values, filling the full 64 bits.  */
74   val.bin = 0x123456789abcdefLL; /* 64 bits = 16 hex digits */
75   val.oct = 01234567123456701234567LL; /*  = 21+ octal digits */
76   val.dec = 12345678901234567890ULL;    /*  = 19+ decimal digits */
77 
78   /* Stop here and look!  */
79   val.hex = val.bin - val.dec | val.oct;
80 
81   return 0;
82 }
83 
84 int main() {
85 
86    /* Pack Byte, Half, Word and Giant arrays with byte-orderd values.
87       That way "(gdb) x" gives the same output on different
88       architectures.  */
89    pack (b, 1, 2);
90    pack (h, 2, 2);
91    pack (w, 4, 2);
92    pack (g, 8, 2);
93    pack (c, sizeof (char), 2);
94    pack (s, sizeof (short), 2);
95    pack (i, sizeof (int), 2);
96    pack (l, sizeof (long), 2);
97    pack (ll, sizeof (long long), 2);
98 
99    known_types();
100 
101    return 0;
102 }
103