00001 /* 00002 * misc.h - miscellaneous interfaces 00003 * 00004 * This file is a part of the SimpleScalar tool suite written by 00005 * Todd M. Austin as a part of the Multiscalar Research Project. 00006 * 00007 * The tool suite is currently maintained by Doug Burger and Todd M. Austin. 00008 * 00009 * Copyright (C) 1994, 1995, 1996, 1997, 1998 by Todd M. Austin 00010 * 00011 * This source file is distributed "as is" in the hope that it will be 00012 * useful. The tool set comes with no warranty, and no author or 00013 * distributor accepts any responsibility for the consequences of its 00014 * use. 00015 * 00016 * Everyone is granted permission to copy, modify and redistribute 00017 * this tool set under the following conditions: 00018 * 00019 * This source code is distributed for non-commercial use only. 00020 * Please contact the maintainer for restrictions applying to 00021 * commercial use. 00022 * 00023 * Permission is granted to anyone to make or distribute copies 00024 * of this source code, either as received or modified, in any 00025 * medium, provided that all copyright notices, permission and 00026 * nonwarranty notices are preserved, and that the distributor 00027 * grants the recipient permission for further redistribution as 00028 * permitted by this document. 00029 * 00030 * Permission is granted to distribute this file in compiled 00031 * or executable form under the same conditions that apply for 00032 * source code, provided that either: 00033 * 00034 * A. it is accompanied by the corresponding machine-readable 00035 * source code, 00036 * B. it is accompanied by a written offer, with no time limit, 00037 * to give anyone a machine-readable copy of the corresponding 00038 * source code in return for reimbursement of the cost of 00039 * distribution. This written offer must permit verbatim 00040 * duplication by anyone, or 00041 * C. it is distributed by someone who received only the 00042 * executable form, and is accompanied by a copy of the 00043 * written offer of source code that they received concurrently. 00044 * 00045 * In other words, you are welcome to use, share and improve this 00046 00047 * source file. You are forbidden to forbid anyone else to use, share 00048 * and improve what you give them. 00049 * 00050 * INTERNET: dburger@cs.wisc.edu 00051 * US Mail: 1210 W. Dayton Street, Madison, WI 53706 00052 * 00053 * $Id: ssb_misc.h,v 1.1.1.1 2006-01-31 16:35:50 afrodri Exp $ 00054 * 00055 * $Log: not supported by cvs2svn $ 00056 * Revision 1.2 2004/08/10 22:55:35 arodrig6 00057 * works, except for the speculative stuff, caches, and multiple procs 00058 * 00059 * Revision 1.1 2004/08/05 23:51:44 arodrig6 00060 * grabed files from SS and broke up some of them 00061 * 00062 * Revision 1.6 2001/06/08 00:47:02 karu 00063 * fixed sim-cheetah. eio does not work because i am still not sure how much state to commit for system calls. 00064 * 00065 * Revision 1.5 2000/04/10 23:46:31 karu 00066 * converted all quad to qword. NO OTHER change made. 00067 * the tag specfp95-before-quad-qword is a snapshow before this change was made 00068 * 00069 * Revision 1.4 2000/04/03 20:03:21 karu 00070 * entire specf95 working . 00071 * 00072 * Revision 1.3 2000/03/15 10:13:00 karu 00073 * made a lot of changes to cror, crand, crnand, crnor, creqv, crandc, crorc 00074 * the original code wasn't generating a proper mask. 00075 * but these changes now affect printf with integere width 00076 * ie printf("%3d", i) does not produce the 3 spaces :-( 00077 * 00078 * Revision 1.2 2000/03/09 02:27:28 karu 00079 * checkpoint support complete. file name passed as env variable FROZENFILE 00080 * 00081 * Revision 1.1.1.1 2000/03/07 05:15:17 karu 00082 * this is the repository created for my own maintanence. 00083 * created when spec95 (lisp and compress worked). 00084 * compress still had the scanf("%i") problem 00085 * DIFF from the repository I am using alongwith ramdass on /projects 00086 * need to merge the two sometime :-) 00087 * 00088 * Revision 1.1.1.1 2000/02/25 21:02:51 karu 00089 * creating cvs repository for ss3-ppc 00090 * 00091 * Revision 1.5 1998/08/27 15:45:24 taustin 00092 * implemented host interface description in host.h 00093 * added target interface support 00094 * implemented a more portable random() interface 00095 * disabled calls to sbrk() under malloc(), this breaks some 00096 * malloc() implementation (e.g., newer Linux releases) 00097 * added myprintf() and myatoq() routines for printing and reading 00098 * quadword's, respectively 00099 * added gzopen() and gzclose() routines for reading and writing 00100 * compressed files, updated sysprobe to search for GZIP, if found 00101 * support is enabled 00102 * moved host-dependent definitions to host.h 00103 * 00104 * Revision 1.4 1997/03/11 01:18:24 taustin 00105 * updated copyright 00106 * supported added for non-GNU C compilers 00107 * ANSI C compiler check added 00108 * long/int tweaks made for ALPHA target support 00109 * hacks added to make SYMCAT() portable 00110 * 00111 * Revision 1.3 1997/01/06 16:02:01 taustin 00112 * comments updated 00113 * system prototypes deleted (-Wall flag no longer a clean compile) 00114 * 00115 * Revision 1.1 1996/12/05 18:50:23 taustin 00116 * Initial revision 00117 * 00118 * 00119 */ 00120 00121 #ifndef MISC_H 00122 #define MISC_H 00123 00124 #include <stdio.h> 00125 #include <stdlib.h> 00126 #include <stdarg.h> 00127 #include <string.h> 00128 #include <sys/types.h> 00129 00130 /* boolean value defs */ 00131 #ifndef TRUE 00132 #define TRUE 1 00133 #endif 00134 #ifndef FALSE 00135 #define FALSE 0 00136 #endif 00137 00138 /* various useful macros */ 00139 #ifndef MAX 00140 #define MAX(a, b) (((a) < (b)) ? (b) : (a)) 00141 #endif 00142 #ifndef MIN 00143 #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 00144 #endif 00145 00146 /* for printing out "long long" vars */ 00147 #define LLHIGH(L) ((int)(((L)>>32) & 0xffffffff)) 00148 #define LLLOW(L) ((int)((L) & 0xffffffff)) 00149 00150 /* size of an array, in elements */ 00151 #define N_ELT(ARR) (sizeof(ARR)/sizeof((ARR)[0])) 00152 00153 /* rounding macros, assumes ALIGN is a power of two */ 00154 #define ROUND_UP(N,ALIGN) (((N) + ((ALIGN)-1)) & ~((ALIGN)-1)) 00155 #define ROUND_DOWN(N,ALIGN) ((N) & ~((ALIGN)-1)) 00156 00157 /* verbose output flag */ 00158 extern int verbose; 00159 00160 #ifdef DEBUG 00161 /* active debug flag */ 00162 extern int debugging; 00163 #endif /* DEBUG */ 00164 00165 /* register a function to be called when an error is detected */ 00166 void 00167 fatal_hook(void (*hook_fn)(FILE *stream)); /* fatal hook function */ 00168 00169 #ifdef __GNUC__ 00170 /* declare a fatal run-time error, calls fatal hook function */ 00171 #define fatal(fmt, args...) \ 00172 _fatal(__FILE__, __FUNCTION__, __LINE__, fmt, ## args) 00173 00174 void 00175 _fatal(const char *file, const char *func, int line, const char *fmt, ...) 00176 __attribute__ ((noreturn)); 00177 #else /* !__GNUC__ */ 00178 void 00179 fatal(char *fmt, ...); 00180 #endif /* !__GNUC__ */ 00181 00182 #ifdef __GNUC__ 00183 /* declare a panic situation, dumps core */ 00184 #define panic(fmt, args...) \ 00185 _panic(__FILE__, __FUNCTION__, __LINE__, fmt, ## args) 00186 00187 00188 void 00189 _panic(const char *file, const char *func, int line, const char *fmt, ...) 00190 __attribute__ ((noreturn)); 00191 #else /* !__GNUC__ */ 00192 void 00193 panic(char *fmt, ...); 00194 #endif /* !__GNUC__ */ 00195 00196 #ifdef __GNUC__ 00197 /* declare a warning */ 00198 #define warn(fmt, args...) \ 00199 _warn(__FILE__, __FUNCTION__, __LINE__, fmt, ## args) 00200 00201 void 00202 _warn(const char *file, const char *func, int line, const char *fmt, ...); 00203 #else /* !__GNUC__ */ 00204 void 00205 warn(char *fmt, ...); 00206 #endif /* !__GNUC__ */ 00207 00208 #ifdef __GNUC__ 00209 /* print general information */ 00210 #define info(fmt, args...) \ 00211 _info(__FILE__, __FUNCTION__, __LINE__, fmt, ## args) 00212 00213 void 00214 _info(const char *file, const char *func, int line, const char *fmt, ...); 00215 #else /* !__GNUC__ */ 00216 void 00217 info(char *fmt, ...); 00218 #endif /* !__GNUC__ */ 00219 00220 #ifdef DEBUG 00221 00222 #ifdef __GNUC__ 00223 /* print a debugging message */ 00224 #define debug(fmt, args...) \ 00225 do { \ 00226 if (debugging) \ 00227 _debug(__FILE__, __FUNCTION__, __LINE__, fmt, ## args); \ 00228 } while(0) 00229 00230 void 00231 _debug(const char *file, const char *func, int line, const char *fmt, ...); 00232 #else /* !__GNUC__ */ 00233 void 00234 debug(char *fmt, ...); 00235 #endif /* !__GNUC__ */ 00236 00237 #else /* !DEBUG */ 00238 00239 #ifdef __GNUC__ 00240 #define debug(fmt, args...) 00241 #else /* !__GNUC__ */ 00242 /* the optimizer should eliminate this call! */ 00243 static void debug(char *fmt, ...) {} 00244 #endif /* !__GNUC__ */ 00245 00246 #endif /* !DEBUG */ 00247 00248 /* seed the random number generator */ 00249 void 00250 mysrand(unsigned int seed); /* random number generator seed */ 00251 00252 /* get a random number */ 00253 int myrand(void); /* returns random number */ 00254 00255 /* copy a string to a new storage allocation (NOTE: many machines are missing 00256 this trivial function, so I funcdup() it here...) */ 00257 char * /* duplicated string */ 00258 mystrdup(const char *s); /* string to duplicate to heap storage */ 00259 00260 /* find the last occurrence of a character in a string */ 00261 char * 00262 mystrrchr(const char *s, char c); 00263 00264 /* case insensitive string compare (NOTE: many machines are missing this 00265 trivial function, so I funcdup() it here...) */ 00266 int /* compare result, see strcmp() */ 00267 mystricmp(const char *s1, const char *s2); /* strings to compare, case insensitive */ 00268 00269 /* allocate some core, this memory has overhead no larger than a page 00270 in size and it cannot be released. the storage is returned cleared */ 00271 void *getcore(int nbytes); 00272 00273 /* return log of a number to the base 2 */ 00274 int log_base2(int n); 00275 00276 /* return string describing elapsed time, passed in SEC in seconds */ 00277 const char *elapsed_time(long sec); 00278 00279 /* assume bit positions numbered 31 to 0 (31 high order bit), extract num bits 00280 from word starting at position pos (with pos as the high order bit of those 00281 to be extracted), result is right justified and zero filled to high order 00282 bit, for example, extractl(word, 6, 3) w/ 8 bit word = 01101011 returns 00283 00000110 */ 00284 unsigned int 00285 extractl(int word, /* the word from which to extract */ 00286 int pos, /* bit positions 31 to 0 */ 00287 int num); /* number of bits to extract */ 00288 00289 #if defined(sparc) && !defined(__svr4__) 00290 #define strtoul strtol 00291 #endif 00292 00293 /* portable 64-bit I/O package */ 00294 00295 /* portable vsprintf with quadword support, returns end pointer */ 00296 char *myvsprintf(char *obuf, const char *format, va_list v); 00297 00298 /* portable sprintf with quadword support, returns end pointer */ 00299 char *mysprintf(char *obuf, const char *format, ...); 00300 00301 /* portable vfprintf with quadword support, returns end pointer */ 00302 void myvfprintf(FILE *stream, const char *format, va_list v); 00303 00304 /* portable fprintf with quadword support, returns end pointer */ 00305 void myfprintf(FILE *stream, const char *format, ...); 00306 00307 #ifdef HOST_HAS_QWORD 00308 00309 /* convert a string to a signed result */ 00310 sqword_t myatosq(char *nptr, char **endp, int base); 00311 00312 /* convert a string to a unsigned result */ 00313 qword_t myatoq(char *nptr, char **endp, int base); 00314 00315 #endif /* HOST_HAS_QWORD */ 00316 00317 /* same semantics as fopen() except that filenames ending with a ".gz" or ".Z" 00318 will be automagically get compressed */ 00319 FILE *gzopen(char *fname, char *type); 00320 00321 /* close compressed stream */ 00322 void gzclose(FILE *fd); 00323 00324 extern int interactive; 00325 00326 /* wedge all stat values into a counter_t */ 00327 #define STATVAL(STAT) \ 00328 ((STAT)->sc == sc_int \ 00329 ? (counter_t)*((STAT)->variant.for_int.var) \ 00330 : ((STAT)->sc == sc_uint \ 00331 ? (counter_t)*((STAT)->variant.for_uint.var) \ 00332 : ((STAT)->sc == sc_counter \ 00333 ? *((STAT)->variant.for_counter.var) \ 00334 : (panic("bad stat class"), 0)))) 00335 00336 00337 #endif /* MISC_H */