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 2006-01-31 16:35:46 afrodri Exp $ 00054 * 00055 * $Log: not supported by cvs2svn $ 00056 * Revision 1.1 2004/04/22 16:37:36 arodrig6 00057 * ppc backend 00058 * 00059 * Revision 1.6 2001/06/08 00:47:02 karu 00060 * fixed sim-cheetah. eio does not work because i am still not sure how much state to commit for system calls. 00061 * 00062 * Revision 1.5 2000/04/10 23:46:31 karu 00063 * converted all quad to qword. NO OTHER change made. 00064 * the tag specfp95-before-quad-qword is a snapshow before this change was made 00065 * 00066 * Revision 1.4 2000/04/03 20:03:21 karu 00067 * entire specf95 working . 00068 * 00069 * Revision 1.3 2000/03/15 10:13:00 karu 00070 * made a lot of changes to cror, crand, crnand, crnor, creqv, crandc, crorc 00071 * the original code wasn't generating a proper mask. 00072 * but these changes now affect printf with integere width 00073 * ie printf("%3d", i) does not produce the 3 spaces :-( 00074 * 00075 * Revision 1.2 2000/03/09 02:27:28 karu 00076 * checkpoint support complete. file name passed as env variable FROZENFILE 00077 * 00078 * Revision 1.1.1.1 2000/03/07 05:15:17 karu 00079 * this is the repository created for my own maintanence. 00080 * created when spec95 (lisp and compress worked). 00081 * compress still had the scanf("%i") problem 00082 * DIFF from the repository I am using alongwith ramdass on /projects 00083 * need to merge the two sometime :-) 00084 * 00085 * Revision 1.1.1.1 2000/02/25 21:02:51 karu 00086 * creating cvs repository for ss3-ppc 00087 * 00088 * Revision 1.5 1998/08/27 15:45:24 taustin 00089 * implemented host interface description in host.h 00090 * added target interface support 00091 * implemented a more portable random() interface 00092 * disabled calls to sbrk() under malloc(), this breaks some 00093 * malloc() implementation (e.g., newer Linux releases) 00094 * added myprintf() and myatoq() routines for printing and reading 00095 * quadword's, respectively 00096 * added gzopen() and gzclose() routines for reading and writing 00097 * compressed files, updated sysprobe to search for GZIP, if found 00098 * support is enabled 00099 * moved host-dependent definitions to host.h 00100 * 00101 * Revision 1.4 1997/03/11 01:18:24 taustin 00102 * updated copyright 00103 * supported added for non-GNU C compilers 00104 * ANSI C compiler check added 00105 * long/int tweaks made for ALPHA target support 00106 * hacks added to make SYMCAT() portable 00107 * 00108 * Revision 1.3 1997/01/06 16:02:01 taustin 00109 * comments updated 00110 * system prototypes deleted (-Wall flag no longer a clean compile) 00111 * 00112 * Revision 1.1 1996/12/05 18:50:23 taustin 00113 * Initial revision 00114 * 00115 * 00116 */ 00117 00118 #ifndef MISC_H 00119 #define MISC_H 00120 00121 #include <stdio.h> 00122 #include <stdlib.h> 00123 #include <stdarg.h> 00124 #include <string.h> 00125 #include <sys/types.h> 00126 00127 /* boolean value defs */ 00128 #ifndef TRUE 00129 #define TRUE 1 00130 #endif 00131 #ifndef FALSE 00132 #define FALSE 0 00133 #endif 00134 00135 /* various useful macros */ 00136 #ifndef MAX 00137 #define MAX(a, b) (((a) < (b)) ? (b) : (a)) 00138 #endif 00139 #ifndef MIN 00140 #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 00141 #endif 00142 00143 /* for printing out "long long" vars */ 00144 #define LLHIGH(L) ((int)(((L)>>32) & 0xffffffff)) 00145 #define LLLOW(L) ((int)((L) & 0xffffffff)) 00146 00147 /* size of an array, in elements */ 00148 #define N_ELT(ARR) (sizeof(ARR)/sizeof((ARR)[0])) 00149 00150 /* rounding macros, assumes ALIGN is a power of two */ 00151 #define ROUND_UP(N,ALIGN) (((N) + ((ALIGN)-1)) & ~((ALIGN)-1)) 00152 #define ROUND_DOWN(N,ALIGN) ((N) & ~((ALIGN)-1)) 00153 00154 /* verbose output flag */ 00155 extern int verbose; 00156 00157 #ifdef DEBUG 00158 /* active debug flag */ 00159 extern int debugging; 00160 #endif /* DEBUG */ 00161 00162 /* register a function to be called when an error is detected */ 00163 void 00164 fatal_hook(void (*hook_fn)(FILE *stream)); /* fatal hook function */ 00165 00166 #ifdef __GNUC__ 00167 /* declare a fatal run-time error, calls fatal hook function */ 00168 #define fatal(fmt, args...) \ 00169 _fatal(__FILE__, __FUNCTION__, __LINE__, fmt, ## args) 00170 00171 void 00172 _fatal(char *file, char *func, int line, char *fmt, ...) 00173 __attribute__ ((noreturn)); 00174 #else /* !__GNUC__ */ 00175 void 00176 fatal(char *fmt, ...); 00177 #endif /* !__GNUC__ */ 00178 00179 #ifdef __GNUC__ 00180 /* declare a panic situation, dumps core */ 00181 #define panic(fmt, args...) \ 00182 _panic(__FILE__, __FUNCTION__, __LINE__, fmt, ## args) 00183 00184 void 00185 _panic(char *file, char *func, int line, char *fmt, ...) 00186 __attribute__ ((noreturn)); 00187 #else /* !__GNUC__ */ 00188 void 00189 panic(char *fmt, ...); 00190 #endif /* !__GNUC__ */ 00191 00192 #ifdef __GNUC__ 00193 /* declare a warning */ 00194 #define warn(fmt, args...) \ 00195 _warn(__FILE__, __FUNCTION__, __LINE__, fmt, ## args) 00196 00197 void 00198 _warn(char *file, char *func, int line, char *fmt, ...); 00199 #else /* !__GNUC__ */ 00200 void 00201 warn(char *fmt, ...); 00202 #endif /* !__GNUC__ */ 00203 00204 #ifdef __GNUC__ 00205 /* print general information */ 00206 #define info(fmt, args...) \ 00207 _info(__FILE__, __FUNCTION__, __LINE__, fmt, ## args) 00208 00209 void 00210 _info(char *file, char *func, int line, char *fmt, ...); 00211 #else /* !__GNUC__ */ 00212 void 00213 info(char *fmt, ...); 00214 #endif /* !__GNUC__ */ 00215 00216 #ifdef DEBUG 00217 00218 #ifdef __GNUC__ 00219 /* print a debugging message */ 00220 #define debug(fmt, args...) \ 00221 do { \ 00222 if (debugging) \ 00223 _debug(__FILE__, __FUNCTION__, __LINE__, fmt, ## args); \ 00224 } while(0) 00225 00226 void 00227 _debug(char *file, char *func, int line, char *fmt, ...); 00228 #else /* !__GNUC__ */ 00229 void 00230 debug(char *fmt, ...); 00231 #endif /* !__GNUC__ */ 00232 00233 #else /* !DEBUG */ 00234 00235 #ifdef __GNUC__ 00236 #define debug(fmt, args...) 00237 #else /* !__GNUC__ */ 00238 /* the optimizer should eliminate this call! */ 00239 static void debug(char *fmt, ...) {} 00240 #endif /* !__GNUC__ */ 00241 00242 #endif /* !DEBUG */ 00243 00244 /* seed the random number generator */ 00245 void 00246 mysrand(unsigned int seed); /* random number generator seed */ 00247 00248 /* get a random number */ 00249 int myrand(void); /* returns random number */ 00250 00251 /* copy a string to a new storage allocation (NOTE: many machines are missing 00252 this trivial function, so I funcdup() it here...) */ 00253 char * /* duplicated string */ 00254 mystrdup(char *s); /* string to duplicate to heap storage */ 00255 00256 /* find the last occurrence of a character in a string */ 00257 char * 00258 mystrrchr(char *s, char c); 00259 00260 /* case insensitive string compare (NOTE: many machines are missing this 00261 trivial function, so I funcdup() it here...) */ 00262 int /* compare result, see strcmp() */ 00263 mystricmp(const char *s1, const char *s2); /* strings to compare, case insensitive */ 00264 00265 /* allocate some core, this memory has overhead no larger than a page 00266 in size and it cannot be released. the storage is returned cleared */ 00267 void *getcore(int nbytes); 00268 00269 /* return log of a number to the base 2 */ 00270 int log_base2(int n); 00271 00272 /* return string describing elapsed time, passed in SEC in seconds */ 00273 char *elapsed_time(long sec); 00274 00275 /* assume bit positions numbered 31 to 0 (31 high order bit), extract num bits 00276 from word starting at position pos (with pos as the high order bit of those 00277 to be extracted), result is right justified and zero filled to high order 00278 bit, for example, extractl(word, 6, 3) w/ 8 bit word = 01101011 returns 00279 00000110 */ 00280 //unsigned int 00281 //extractl(int word, /* the word from which to extract */ 00282 // int pos, /* bit positions 31 to 0 */ 00283 // int num); /* number of bits to extract */ 00284 00285 #if defined(sparc) && !defined(__svr4__) 00286 #define strtoul strtol 00287 #endif 00288 00289 /* portable 64-bit I/O package */ 00290 00291 /* portable vsprintf with quadword support, returns end pointer */ 00292 char *myvsprintf(char *obuf, const char *format, va_list v); 00293 00294 /* portable sprintf with quadword support, returns end pointer */ 00295 char *mysprintf(char *obuf, const char *format, ...); 00296 00297 /* portable vfprintf with quadword support, returns end pointer */ 00298 void myvfprintf(FILE *stream, const char *format, va_list v); 00299 00300 /* portable fprintf with quadword support, returns end pointer */ 00301 void myfprintf(FILE *stream, const char *format, ...); 00302 00303 #ifdef HOST_HAS_QWORD 00304 00305 /* convert a string to a signed result */ 00306 sqword_t myatosq(char *nptr, char **endp, int base); 00307 00308 /* convert a string to a unsigned result */ 00309 qword_t myatoq(char *nptr, char **endp, int base); 00310 00311 #endif /* HOST_HAS_QWORD */ 00312 00313 /* same semantics as fopen() except that filenames ending with a ".gz" or ".Z" 00314 will be automagically get compressed */ 00315 FILE *gzopen(char *fname, char *type); 00316 00317 /* close compressed stream */ 00318 void gzclose(FILE *fd); 00319 00320 extern int interactive; 00321 00322 #endif /* MISC_H */