00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 static char *source_file = __FILE__;
00038 static char *rcs_id = "$Source: /depot/CVSROOT/javi/src/sw/cmplr/common/util/util.c,v $ $Revision: 1.1 $";
00039 #include <unistd.h>
00040 #include <errno.h>
00041 #include "defs.h"
00042 #include "erglob.h"
00043 #include "tracing.h"
00044 #include "util.h"
00045
00046 extern pid_t wait(INT *statptr);
00047
00048
00049
00050 #ifndef MONGOOSE_BE
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 INT Execute (
00064 char *cmd,
00065 char **argv,
00066 char *stdoutfile,
00067 BOOL echo
00068 )
00069 {
00070 INT child, t, status;
00071
00072
00073 if ( echo ) {
00074 char **arg = argv+1;
00075
00076 fprintf ( stderr, " %s", cmd);
00077 while ( *arg ) fprintf ( stderr, " %s", *arg++);
00078 if ( stdoutfile != NULL ) fprintf ( stderr, " >%s", stdoutfile );
00079 fprintf ( stderr, "\n" );
00080 }
00081
00082 #ifdef __MINGW32__
00083 if (stdoutfile != NULL) {
00084 if( freopen(stdoutfile, "w", stdout) == NULL) _exit(errno);
00085 }
00086 int spawnRet = _spawnvp(_P_WAIT, cmd, (const char*const*)argv);
00087 if (spawnRet == -1)
00088 return (EAGAIN);
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 if ( (spawnRet&0377) != 0 ) return ( 0400 + t );
00109 return ((spawnRet>>8) & 0377);
00110 #else
00111
00112 child = fork();
00113 if (child == -1) return (EAGAIN);
00114
00115
00116 if (child == 0) {
00117
00118 if (stdoutfile != NULL) {
00119 if( freopen(stdoutfile, "w", stdout) == NULL) _exit(errno);
00120 }
00121
00122
00123 execvp( cmd, argv );
00124
00125
00126 _exit(errno);
00127 }
00128
00129
00130 while (child != wait(&status)) {};
00131 if ( (t=(status&0377)) != 0 ) return ( 0400 + t );
00132 return ((status>>8) & 0377);
00133 #endif
00134
00135 }
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 char *
00147 Get_Environment_Value (
00148 char *name,
00149 char **envp,
00150 char *def
00151 )
00152 {
00153 INT len = strlen(name);
00154 char **env = envp;
00155
00156
00157 while ( *env ) {
00158 if ( strncmp (name, *env, len) == 0 && *(len + *env) == '=' )
00159 return (*env) + len + 1;
00160 env++;
00161 }
00162
00163
00164 return def;
00165 }
00166 #endif
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178 INT
00179 Check_Range (
00180 INT val,
00181 INT lbound,
00182 INT ubound,
00183 INT def
00184 )
00185 {
00186 if ( val >= lbound && val <= ubound ) return val;
00187 return def;
00188 }
00189
00190 #ifndef MONGOOSE_BE
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200 void
00201 Indent (
00202 FILE *f,
00203 INT16 indent
00204 )
00205 {
00206 while ( indent >= 8 ) {
00207 fprintf ( f, "\t" );
00208 indent -= 8;
00209 }
00210 while ( indent-- > 0 ) fprintf ( f, " " );
00211 }
00212 #endif
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228 INT Mod(
00229 INT i,
00230 INT j
00231 )
00232 {
00233 INT rem;
00234
00235 if ( j == 0 )
00236 return i;
00237
00238 rem = i % j;
00239
00240 if ( rem == 0 )
00241 return 0;
00242
00243 if ( (i < 0) != (j < 0) )
00244 return j + rem;
00245 else
00246 return rem;
00247 }
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263 const mUINT8 UINT8_pop_count[256] = {
00264 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
00265 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
00266 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
00267 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00268 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
00269 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00270 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00271 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
00272 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
00273 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00274 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00275 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
00276 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00277 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
00278 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
00279 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
00280 };
00281
00282 TARG_INT
00283 TARG_INT_Pop_Count(
00284 TARG_INT x
00285 )
00286 {
00287 INT i, result=0;
00288
00289
00290
00291
00292 for (i = sizeof(x) - 1; i >= 0; --i ) {
00293 unsigned char y = ((TARG_UINT) x) >> (((UINT) i) * 8);
00294
00295 result += UINT8_pop_count[y];
00296 }
00297 return result;
00298 }
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313 static const mUINT8 UINT8_most_sig_one [256] = {
00314 -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
00315 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
00316 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00317 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00318 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
00319 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
00320 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
00321 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
00322 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
00323 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
00324 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
00325 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
00326 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
00327 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
00328 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
00329 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
00330 };
00331
00332
00333 TARG_INT
00334 TARG_INT_Most_Sig_One(
00335 TARG_INT x
00336 )
00337 {
00338 INT i;
00339
00340
00341
00342
00343 for (i = sizeof(x) - 1; i >= 0; --i ) {
00344 unsigned char y = ((TARG_UINT) x) >> (((UINT) i) * 8);
00345
00346 if (y != 0 )
00347 return i*8 + UINT8_most_sig_one[y];
00348 }
00349
00350 return (TARG_INT) -1;
00351 }
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367 const mUINT8 UINT8_least_sig_one [256] = {
00368 -1, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
00369 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
00370 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
00371 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
00372 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
00373 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
00374 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
00375 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
00376 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
00377 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
00378 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
00379 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
00380 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
00381 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
00382 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
00383 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
00384 };
00385
00386
00387 TARG_INT
00388 TARG_INT_Least_Sig_One(
00389 TARG_INT x
00390 )
00391 {
00392 INT i;
00393
00394
00395
00396
00397 for (i = 0; i < sizeof(x); ++i) {
00398 unsigned char y = ((TARG_UINT) x) >> (((UINT) i) * 8);
00399
00400 if (y != 0)
00401 return i*8 + UINT8_least_sig_one[y];
00402 }
00403
00404 return (TARG_INT) -1;
00405 }
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418 #define IS_POW2(n) (((n) & ((n)-1))==0)
00419
00420 INT32 nearest_power_of_two(INT32 n)
00421 {
00422 INT32 i;
00423
00424 Is_True((n>0), ("nearest_power_of two() not defined for <=0"));
00425
00426 if (IS_POW2(n))
00427 {
00428 return n;
00429 }
00430 for(i=0; (1<<i)<n; i++)
00431 ;
00432 return 1<<i;
00433 }
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444 BOOL Immediate_Has_All_Ones(INT64 imm, INT32 ub, INT32 lb)
00445 {
00446 TARG_UINT field= ~0;
00447 INT32 fieldsize= ub - lb + 1;
00448
00449 Is_True((fieldsize>0), ("nonsensical ub,lb for immediate"));
00450
00451
00452
00453
00454 field >>= (sizeof(TARG_INT)*8) - fieldsize;
00455 imm = (imm >> lb) & field;
00456
00457 if (imm ^ field)
00458 return FALSE;
00459
00460 return TRUE;
00461 }