OpenADFortTk (including Open64 and OpenAnalysis references)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
util.c
Go to the documentation of this file.
1 /*
2 
3  Copyright (C) 2000, 2001 Silicon Graphics, Inc. All Rights Reserved.
4 
5  This program is free software; you can redistribute it and/or modify it
6  under the terms of version 2 of the GNU General Public License as
7  published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it would be useful, but
10  WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 
13  Further, this software is distributed without any warranty that it is
14  free of the rightful claim of any third person regarding infringement
15  or the like. Any license provided herein, whether implied or
16  otherwise, applies only to this software file. Patent licenses, if
17  any, provided herein do not apply to combinations of this program with
18  other software, or any other product whatsoever.
19 
20  You should have received a copy of the GNU General Public License along
21  with this program; if not, write the Free Software Foundation, Inc., 59
22  Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 
24  Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pky,
25  Mountain View, CA 94043, or:
26 
27  http://www.sgi.com
28 
29  For further information regarding this notice, see:
30 
31  http://oss.sgi.com/projects/GenInfo/NoticeExplan
32 
33 */
34 
35 
36 
37 static char *source_file = __FILE__;
38 #include <unistd.h>
39 #include <errno.h>
40 #include "defs.h"
41 #include "erglob.h"
42 #include "tracing.h"
43 #include "util.h"
44 
45 #include <sys/wait.h>
46 /*extern pid_t wait(INT *statptr); */ /* Currently not defined with
47  * prototype in sys/wait.h
48  */
49 
50 #ifndef MONGOOSE_BE
51 /* ====================================================================
52  *
53  * Execute
54  *
55  * Execute an external command via fork/execvp.
56  * Returns 0 on success, an error code on failure.
57  * An error code < 0400 is a Unix error code from errno.h.
58  * An error code > 0400 is 0400 plus the signal which killed child.
59  *
60  * ====================================================================
61  */
62 
63 INT Execute (
64  char *cmd, /* The command to execute */
65  char **argv, /* Argument list */
66  char *stdoutfile, /* stdout for new process to use or NULL */
67  BOOL echo /* Echo the command line? */
68 )
69 {
70  INT child, t, status;
71 
72  /* Echo the command line if requested: */
73  if ( echo ) {
74  char **arg = argv+1;
75 
76  fprintf ( stderr, " %s", cmd);
77  while ( *arg ) fprintf ( stderr, " %s", *arg++);
78  if ( stdoutfile != NULL ) fprintf ( stderr, " >%s", stdoutfile );
79  fprintf ( stderr, "\n" );
80  }
81 
82  /* Fork and check for failure: */
83  child = fork();
84  if (child == -1) return (EAGAIN);
85 
86  /* Execute the requested command in the child: */
87  if (child == 0) {
88  /* If a new stdout is given, open it: */
89  if (stdoutfile != NULL) {
90  if( freopen(stdoutfile, "w", stdout) == NULL) _exit(errno);
91  }
92 
93  /* Execute the command: */
94  execvp( cmd, argv );
95 
96  /* If the exec failed, return the system error code: */
97  _exit(errno);
98  }
99 
100  /* Wait for the child in the parent and then check for errors: */
101  while (child != wait(&status)) {};
102  if ( (t=(status&0377)) != 0 ) return ( 0400 + t );
103  return ((status>>8) & 0377);
104 
105 }
106 
107 /* ====================================================================
108  *
109  * Get_Environment_Value
110  *
111  * Get the value of an environment variable.
112  *
113  * ====================================================================
114  */
115 
116 char *
118  char *name, /* The name of the environment variable */
119  char **envp, /* Array of environment variables + NULL */
120  char *def /* Default to return if not found */
121 )
122 {
123  INT len = strlen(name);
124  char **env = envp;
125 
126  /* Search for a match: */
127  while ( *env ) {
128  if ( strncmp (name, *env, len) == 0 && *(len + *env) == '=' )
129  return (*env) + len + 1;
130  env++;
131  }
132 
133  /* Not found: */
134  return def;
135 }
136 #endif /* MONGOOSE_BE */
137 
138 /* ====================================================================
139  *
140  * Check_Range
141  *
142  * Check an integer value against bounds. Return the value if in
143  * range, and a default if out of range.
144  *
145  * ====================================================================
146  */
147 
148 INT
150  INT val, /* Check this value */
151  INT lbound, /* ... against this lower bound */
152  INT ubound, /* ... and this upper bound, */
153  INT def /* ... with this default. */
154 )
155 {
156  if ( val >= lbound && val <= ubound ) return val;
157  return def;
158 }
159 
160 #ifndef MONGOOSE_BE
161 /* ====================================================================
162  *
163  * Indent
164  *
165  * Print tabs and spaces to indent given number of characters.
166  *
167  * ====================================================================
168  */
169 
170 void
171 Indent (
172  FILE *f,
173  INT16 indent
174 )
175 {
176  while ( indent >= 8 ) {
177  fprintf ( f, "\t" );
178  indent -= 8;
179  }
180  while ( indent-- > 0 ) fprintf ( f, " " );
181 }
182 #endif /* MONGOOSE_BE */
183 
184 /* ====================================================================
185  *
186  * Mod
187  *
188  * Mathematically correct integer modulus function. Unlike C's
189  * builtin remainder function, this correctly handles the case where
190  * one of the two arguments is negative.
191  *
192  * Notice the use if INT as the argument and return type. Should
193  * this have been INT64?
194  *
195  * ====================================================================
196  */
197 
199  INT i,
200  INT j
201 )
202 {
203  INT rem;
204 
205  if ( j == 0 )
206  return i;
207 
208  rem = i % j;
209 
210  if ( rem == 0 )
211  return 0;
212 
213  if ( (i < 0) != (j < 0) )
214  return j + rem;
215  else
216  return rem;
217 }
218 
219 /* ====================================================================
220  *
221  * Pop_Count
222  *
223  * Return the count of set bits in 'x'. This could be a lot faster,
224  * but does it matter?
225  *
226  * TODO: Better algorithm.
227  *
228  * ====================================================================
229  */
230 
231 /* Count of bits in all the one byte numbers.
232  */
233 const mUINT8 UINT8_pop_count[256] = {
234  0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
235  1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
236  1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
237  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
238  1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
239  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
240  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
241  3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
242  1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
243  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
244  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
245  3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
246  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
247  3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
248  3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
249  4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
250 };
251 
252 TARG_INT
254  TARG_INT x
255 )
256 {
257  INT i, result=0;
258 
259  /* Find the least significant byte in x that's not zero and look it
260  * up in the above table.
261  */
262  for (i = sizeof(x) - 1; i >= 0; --i ) {
263  unsigned char y = ((TARG_UINT) x) >> (((UINT) i) * 8);
264 
265  result += UINT8_pop_count[y];
266  }
267  return result;
268 }
269 
270 /* ====================================================================
271  *
272  * TARG_INT Most_Sig_One
273  *
274  * Return the index of the most signicant bit in x, or -1 if x is 0.
275  * Bits are labeled [63 .. 0]
276  *
277  * ====================================================================
278  */
279 
280 /* Mapping from unsigned 8 bit integers to the index of their most
281  * significant one. Notice -1 for out of range 0.
282  */
283 static const mUINT8 UINT8_most_sig_one [256] = {
284  (mUINT8)-1,
285  0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
286  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
287  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
288  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
289  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
290  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
291  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
292  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
293  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
294  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
295  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
296  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
297  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
298  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
299  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
300  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
301 };
302 
303 
304 TARG_INT
306  TARG_INT x
307 )
308 {
309  INT i;
310 
311  /* Find the least significant byte in x that's not zero and look it
312  * up in the above table.
313  */
314  for (i = sizeof(x) - 1; i >= 0; --i ) {
315  unsigned char y = ((TARG_UINT) x) >> (((UINT) i) * 8);
316 
317  if (y != 0 )
318  return i*8 + UINT8_most_sig_one[y];
319  }
320 
321  return (TARG_INT) -1;
322 }
323 
324 
325 /* ====================================================================
326  *
327  * TARG_INT_Least_Sig_One
328  *
329  * Return the index of the least signicant bit in x, or -1 if x is 0.
330  * Bits are labeled [63 .. 0]
331  *
332  * ====================================================================
333  */
334 
335 /* Mapping from 8 bit unsigned integers to the index of the first one
336  * bit:
337  */
339  (mUINT8)-1,
340  0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
341  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
342  5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
343  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
344  6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
345  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
346  5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
347  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
348  7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
349  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
350  5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
351  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
352  6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
353  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
354  5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
355  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
356 };
357 
358 
359 TARG_INT
361  TARG_INT x
362 )
363 {
364  INT i;
365 
366  /* Find the least significant byte in x that's not zero and look it
367  * up in the above table.
368  */
369  for (i = 0; i < sizeof(x); ++i) {
370  unsigned char y = ((TARG_UINT) x) >> (((UINT) i) * 8);
371 
372  if (y != 0)
373  return i*8 + UINT8_least_sig_one[y];
374  }
375 
376  return (TARG_INT) -1;
377 }
378 
379 
380 /* ====================================================================
381  *
382  * INT32 nearest_power_of two(INT32 n)
383  *
384  * find the next nearest power of 2
385  * ex.
386  * nearest_power_of two(7) = 8
387  * nearest_power_of two(4) = 4
388  *
389  * ==================================================================== */
390 #define IS_POW2(n) (((n) & ((n)-1))==0)
391 
393 {
394  INT32 i;
395 
396  Is_True((n>0), ("nearest_power_of two() not defined for <=0"));
397 
398  if (IS_POW2(n))
399  {
400  return n;
401  }
402  for(i=0; (1<<i)<n; i++)
403  ;
404  return 1<<i;
405 }
406 
407 
408 
409 /* ====================================================================
410  *
411  * BOOL Immediate_Has_All_Ones(INT64 imm, INT32 ub, INT32 lb)
412  *
413  * return TRUE if imm has all ones from lb to ub
414  *
415  * ==================================================================== */
417 {
418  TARG_UINT field= (TARG_UINT)~0;
419  INT32 fieldsize= ub - lb + 1;
420 
421  Is_True((fieldsize>0), ("nonsensical ub,lb for immediate"));
422 
423  /* create mask of ones
424  * cannonicalize immediate to start at bits zero
425  */
426  field >>= (sizeof(TARG_INT)*8) - fieldsize;
427  imm = (imm >> lb) & field;
428 
429  if (imm ^ field)
430  return FALSE;
431 
432  return TRUE;
433 }