1 
2 
3 #include <libunwind.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <altivec.h>
9 
10 #include <sys/resource.h>
11 
12 #define panic(args...)	{ fprintf (stderr, args);  abort(); }
13 
14 extern vector signed int vec_init ();
15 extern void vec_print (vector signed int v);
16 
17 vector signed int vec_stack (int count);
18 
19 int
main()20 main ()
21 {
22   printf ("&vec_stack = %016lx\n", (unsigned long) vec_stack);
23   vec_stack (3);
24   return 0;
25 }
26 
27 
28 vector signed int
vec_stack(int count)29 vec_stack (int count)
30 {
31   register vector signed int v1;
32   register vector signed int v2;
33   register vector signed int v3;
34   register vector signed int v4;
35   register vector signed int v5;
36   register vector signed int v6;
37   register vector signed int v7;
38   register vector signed int v8;
39   register vector signed int v9;
40 
41   unw_fpreg_t vr;
42 
43   unw_cursor_t cursor;
44   unw_word_t ip, sp;
45   unw_context_t uc;
46   int ret;
47   int verbose = 1;
48 
49   /* if (count == 0) return vec_init(); */
50 
51   if (count == 0)
52     {
53       unw_getcontext (&uc);
54       if (unw_init_local (&cursor, &uc) < 0)
55 	{
56 	  panic ("unw_init_local failed!\n");
57 	}
58       else
59 	{
60 	  do
61 	    {
62 	      if ((ret = unw_get_reg (&cursor, UNW_REG_IP, &ip)) < 0)
63 		{
64 		  panic ("FAILURE: unw_get_reg returned %d for UNW_REG_IP\n",
65 			 ret);
66 		}
67 	      if ((ret = unw_get_reg (&cursor, UNW_REG_SP, &sp)) < 0)
68 		{
69 		  panic ("FAILURE: unw_get_reg returned %d for UNW_REG_SP\n",
70 			 ret);
71 		}
72 	      if ((ret = unw_get_fpreg (&cursor, UNW_PPC64_V30, &vr)) < 0)
73 		{
74 		  panic
75 		    ("FAILURE: unw_get_vreg returned %d for UNW_PPC64_V30\n",
76 		     ret);
77 		}
78 
79 
80 	      if (verbose)
81 		{
82 		  const char *regname = unw_regname (UNW_PPC64_V30);
83 		  char proc_name_buffer[256];
84 		  unw_word_t offset;
85                   unsigned int * vec_half1, * vec_half2;
86                   vec_half1 = (unsigned int *)&vr;
87                   vec_half2 = vec_half1 + 1;
88 		  printf ("ip = %016lx, sp=%016lx\n", (long) ip, (long) sp);
89 		  printf ("vr30 = %08x %08x %08x %08x\n",
90 			  (unsigned int) (*vec_half1 >> 16),
91 			  (unsigned int) (*vec_half1 & 0xffffffff),
92 			  (unsigned int) (*vec_half2 >> 16),
93 			  (unsigned int) (*vec_half2 & 0xffffffff));
94 		  ret =
95 		    unw_get_proc_name (&cursor, proc_name_buffer,
96 				       sizeof (proc_name_buffer), &offset);
97 		  if (ret == 0)
98 		    {
99 		      printf ("proc name = %s, offset = %lx\n",
100 			      proc_name_buffer, offset);
101 		    }
102 		  else
103 		    {
104 		      panic ("unw_get_proc_name returned %d\n", ret);
105 		    }
106 		  printf ("unw_regname(UNW_PPC_V30) = %s\n\n", regname);
107 		}
108 
109 	      ret = unw_step (&cursor);
110 	      if (ret < 0)
111 		{
112 		  unw_get_reg (&cursor, UNW_REG_IP, &ip);
113 		  panic ("FAILURE: unw_step() returned %d for ip=%lx\n", ret,
114 			 (long) ip);
115 		}
116 	    }
117 	  while (ret > 0);
118 	}
119     }
120 
121   v1 = vec_init ();
122   v2 = vec_init ();
123   v3 = vec_init ();
124   v4 = vec_init ();
125   v5 = vec_init ();
126   v6 = vec_init ();
127 
128   /* make use of all of the registers in some calculation */
129   v7 =
130     vec_nor (v1, vec_add (v2, vec_sub (v3, vec_and (v4, vec_or (v5, v6)))));
131 
132   /*
133    * "force" the registers to be non-volatile by making a call and also
134    * using the registers after the call.
135    */
136   v8 = vec_stack (count - 1);
137 
138   /*
139    * Use the result from the previous call, plus all of the non-volatile
140    * registers in another calculation.
141    */
142   v9 =
143     vec_nor (v1,
144 	     vec_add (v2,
145 		      vec_sub (v3,
146 			       vec_and (v4, vec_or (v5, vec_xor (v6, v8))))));
147 
148   printf ("v1 - ");
149   vec_print (v1);
150   printf ("\n");
151   printf ("v2 - ");
152   vec_print (v2);
153   printf ("\n");
154   printf ("v3 - ");
155   vec_print (v3);
156   printf ("\n");
157   printf ("v4 - ");
158   vec_print (v4);
159   printf ("\n");
160   printf ("v5 - ");
161   vec_print (v5);
162   printf ("\n");
163   printf ("v6 - ");
164   vec_print (v6);
165   printf ("\n");
166   printf ("v7 - ");
167   vec_print (v7);
168   printf ("\n");
169   printf ("v8 - ");
170   vec_print (v8);
171   printf ("\n");
172   printf ("v9 - ");
173   vec_print (v9);
174   printf ("\n");
175 
176   return v9;
177 }
178