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: misc.h,v 1.1.1.1 2003/09/18 00:57:54 panalyzer Exp $ 00054 * 00055 * $Log: misc.h,v $ 00056 * Revision 1.1.1.1 2003/09/18 00:57:54 panalyzer 00057 * 00058 * 00059 * Revision 1.1.1.1 2003/09/18 00:18:44 panalyzer 00060 * 00061 * 00062 * Revision 1.1.1.1 2003/09/16 18:48:14 gdm 00063 * 00064 * 00065 * Revision 1.1.1.1 2000/11/29 14:53:54 cu-cs 00066 * Grand unification of arm sources. 00067 * 00068 * 00069 * Revision 1.1.1.1.2.1 2000/09/05 13:56:01 taustin 00070 * SimpleScalar/ARM fuzz buster - random co-simulation tester, very handy! 00071 * 00072 * Revision 1.1.1.1 2000/05/26 15:18:58 taustin 00073 * SimpleScalar Tool Set 00074 * 00075 * 00076 * Revision 1.7 1999/12/31 18:46:11 taustin 00077 * quad_t naming conflicts removed 00078 * 00079 * Revision 1.6 1999/12/13 18:44:49 taustin 00080 * crc generation support added (useful for debugging) 00081 * 00082 * Revision 1.5 1998/08/27 15:45:24 taustin 00083 * implemented host interface description in host.h 00084 * added target interface support 00085 * implemented a more portable random() interface 00086 * disabled calls to sbrk() under malloc(), this breaks some 00087 * malloc() implementation (e.g., newer Linux releases) 00088 * added myprintf() and myatoq() routines for printing and reading 00089 * qword's, respectively 00090 * added gzopen() and gzclose() routines for reading and writing 00091 * compressed files, updated sysprobe to search for GZIP, if found 00092 * support is enabled 00093 * moved host-dependent definitions to host.h 00094 * 00095 * Revision 1.4 1997/03/11 01:18:24 taustin 00096 * updated copyright 00097 * supported added for non-GNU C compilers 00098 * ANSI C compiler check added 00099 * long/int tweaks made for ALPHA target support 00100 * hacks added to make SYMCAT() portable 00101 * 00102 * Revision 1.3 1997/01/06 16:02:01 taustin 00103 * comments updated 00104 * system prototypes deleted (-Wall flag no longer a clean compile) 00105 * 00106 * Revision 1.1 1996/12/05 18:50:23 taustin 00107 * Initial revision 00108 * 00109 * 00110 */ 00111 00112 #ifndef MISC_H 00113 #define MISC_H 00114 00115 #include <stdio.h> 00116 #include <stdlib.h> 00117 #include <stdarg.h> 00118 #include <string.h> 00119 #include <sys/types.h> 00120 #include "host.h" 00121 00122 /* boolean value defs */ 00123 #ifndef TRUE 00124 #define TRUE 1 00125 #endif 00126 #ifndef FALSE 00127 #define FALSE 0 00128 #endif 00129 00130 /* various useful macros */ 00131 #ifndef MAX 00132 #define MAX(a, b) (((a) < (b)) ? (b) : (a)) 00133 #endif 00134 #ifndef MIN 00135 #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 00136 #endif 00137 00138 /* for printing out "long long" vars */ 00139 #define LLHIGH(L) ((int)(((L)>>32) & 0xffffffff)) 00140 #define LLLOW(L) ((int)((L) & 0xffffffff)) 00141 00142 /* size of an array, in elements */ 00143 #define N_ELT(ARR) (sizeof(ARR)/sizeof((ARR)[0])) 00144 00145 /* rounding macros, assumes ALIGN is a power of two */ 00146 #define ROUND_UP(N,ALIGN) (((N) + ((ALIGN)-1)) & ~((ALIGN)-1)) 00147 #define ROUND_DOWN(N,ALIGN) ((N) & ~((ALIGN)-1)) 00148 00149 /* verbose output flag */ 00150 extern int verbose; 00151 00152 #ifdef DEBUG 00153 /* active debug flag */ 00154 extern int debugging; 00155 #endif /* DEBUG */ 00156 00157 /* register a function to be called when an error is detected */ 00158 void 00159 fatal_hook(void (*hook_fn)(FILE *stream)); /* fatal hook function */ 00160 00161 #ifdef __GNUC__ 00162 /* declare a fatal run-time error, calls fatal hook function */ 00163 #define fatal(fmt, args...) \ 00164 _fatal(__FILE__, __FUNCTION__, __LINE__, fmt, ## args) 00165 00166 void 00167 _fatal(char *file, char *func, int line, char *fmt, ...) 00168 __attribute__ ((noreturn)); 00169 #else /* !__GNUC__ */ 00170 void 00171 fatal(char *fmt, ...); 00172 #endif /* !__GNUC__ */ 00173 00174 #ifdef __GNUC__ 00175 /* declare a panic situation, dumps core */ 00176 #define panic(fmt, args...) \ 00177 _panic(__FILE__, __FUNCTION__, __LINE__, fmt, ## args) 00178 00179 void 00180 _panic(char *file, char *func, int line, char *fmt, ...) 00181 __attribute__ ((noreturn)); 00182 #else /* !__GNUC__ */ 00183 void 00184 panic(char *fmt, ...); 00185 #endif /* !__GNUC__ */ 00186 00187 #ifdef __GNUC__ 00188 /* declare a warning */ 00189 #define warn(fmt, args...) \ 00190 _warn(__FILE__, __FUNCTION__, __LINE__, fmt, ## args) 00191 00192 void 00193 _warn(char *file, char *func, int line, char *fmt, ...); 00194 #else /* !__GNUC__ */ 00195 void 00196 warn(char *fmt, ...); 00197 #endif /* !__GNUC__ */ 00198 00199 #ifdef __GNUC__ 00200 /* declare a oneshot warning */ 00201 #define warnonce(fmt, args...) \ 00202 do { \ 00203 static int __first = TRUE; \ 00204 if (__first) _warn(__FILE__, __FUNCTION__, __LINE__, fmt, ## args); \ 00205 __first = FALSE; \ 00206 } while (0) 00207 void 00208 _warn(char *file, char *func, int line, char *fmt, ...); 00209 #else /* !__GNUC__ */ 00210 void 00211 warn(+char *fmt, ...); 00212 #endif /* !__GNUC__ */ 00213 00214 #ifdef __GNUC__ 00215 /* print general information */ 00216 #define info(fmt, args...) \ 00217 _info(__FILE__, __FUNCTION__, __LINE__, fmt, ## args) 00218 00219 void 00220 _info(char *file, char *func, int line, char *fmt, ...); 00221 #else /* !__GNUC__ */ 00222 void 00223 info(char *fmt, ...); 00224 #endif /* !__GNUC__ */ 00225 00226 #ifdef DEBUG 00227 00228 #ifdef __GNUC__ 00229 /* print a debugging message */ 00230 #define debug(fmt, args...) \ 00231 do { \ 00232 if (debugging) \ 00233 _debug(__FILE__, __FUNCTION__, __LINE__, fmt, ## args); \ 00234 } while(0) 00235 00236 void 00237 _debug(char *file, char *func, int line, char *fmt, ...); 00238 #else /* !__GNUC__ */ 00239 void 00240 debug(char *fmt, ...); 00241 #endif /* !__GNUC__ */ 00242 00243 #else /* !DEBUG */ 00244 00245 #ifdef __GNUC__ 00246 #define debug(fmt, args...) 00247 #else /* !__GNUC__ */ 00248 /* the optimizer should eliminate this call! */ 00249 static void debug(char *fmt, ...) {} 00250 #endif /* !__GNUC__ */ 00251 00252 #endif /* !DEBUG */ 00253 00254 /* seed the random number generator */ 00255 void 00256 mysrand(unsigned int seed); /* random number generator seed */ 00257 00258 /* get a random number */ 00259 int myrand(void); /* returns random number */ 00260 00261 /* copy a string to a new storage allocation (NOTE: many machines are missing 00262 this trivial function, so I funcdup() it here...) */ 00263 char * /* duplicated string */ 00264 mystrdup(char *s); /* string to duplicate to heap storage */ 00265 00266 /* find the last occurrence of a character in a string */ 00267 char * 00268 mystrrchr(char *s, char c); 00269 00270 /* case insensitive string compare (NOTE: many machines are missing this 00271 trivial function, so I funcdup() it here...) */ 00272 int /* compare result, see strcmp() */ 00273 mystricmp(char *s1, char *s2); /* strings to compare, case insensitive */ 00274 00275 /* allocate some core, this memory has overhead no larger than a page 00276 in size and it cannot be released. the storage is returned cleared */ 00277 void *getcore(int nbytes); 00278 00279 /* return log of a number to the base 2 */ 00280 int log_base2(int n); 00281 00282 /* return string describing elapsed time, passed in SEC in seconds */ 00283 char *elapsed_time(long sec); 00284 00285 /* assume bit positions numbered 31 to 0 (31 high order bit), extract num bits 00286 from word starting at position pos (with pos as the high order bit of those 00287 to be extracted), result is right justified and zero filled to high order 00288 bit, for example, extractl(word, 6, 3) w/ 8 bit word = 01101011 returns 00289 00000110 */ 00290 unsigned int 00291 extractl(int word, /* the word from which to extract */ 00292 int pos, /* bit positions 31 to 0 */ 00293 int num); /* number of bits to extract */ 00294 00295 #if defined(sparc) && !defined(__svr4__) 00296 #define strtoul strtol 00297 #endif 00298 00299 /* portable 64-bit I/O package */ 00300 00301 /* portable vsprintf with qword support, returns end pointer */ 00302 char *myvsprintf(char *obuf, char *format, va_list v); 00303 00304 /* portable sprintf with qword support, returns end pointer */ 00305 char *mysprintf(char *obuf, char *format, ...); 00306 00307 /* portable vfprintf with qword support, returns end pointer */ 00308 void myvfprintf(FILE *stream, char *format, va_list v); 00309 00310 /* portable fprintf with qword support, returns end pointer */ 00311 void myfprintf(FILE *stream, char *format, ...); 00312 00313 #ifdef HOST_HAS_QWORD 00314 00315 /* convert a string to a signed result */ 00316 sqword_t myatosq(char *nptr, char **endp, int base); 00317 00318 /* convert a string to a unsigned result */ 00319 qword_t myatoq(char *nptr, char **endp, int base); 00320 00321 #endif /* HOST_HAS_QWORD */ 00322 00323 /* same semantics as fopen() except that filenames ending with a ".gz" or ".Z" 00324 will be automagically get compressed */ 00325 FILE *gzopen(char *fname, char *type); 00326 00327 /* close compressed stream */ 00328 void gzclose(FILE *fd); 00329 00330 /* update the CRC on the data block one byte at a time */ 00331 word_t crc(word_t crc_accum, word_t data); 00332 00333 #endif /* MISC_H */