misc.c
来自「一个很有名的硬件模拟器。可以模拟CPU」· C语言 代码 · 共 1,190 行 · 第 1/2 页
C
1,190 行
/* misc.c - miscellaneous routines *//* SimpleScalar(TM) Tool Suite * Copyright (C) 1994-2003 by Todd M. Austin, Ph.D. and SimpleScalar, LLC. * All Rights Reserved. * * THIS IS A LEGAL DOCUMENT, BY USING SIMPLESCALAR, * YOU ARE AGREEING TO THESE TERMS AND CONDITIONS. * * No portion of this work may be used by any commercial entity, or for any * commercial purpose, without the prior, written permission of SimpleScalar, * LLC (info@simplescalar.com). Nonprofit and noncommercial use is permitted * as described below. * * 1. SimpleScalar is provided AS IS, with no warranty of any kind, express * or implied. The user of the program accepts full responsibility for the * application of the program and the use of any results. * * 2. Nonprofit and noncommercial use is encouraged. SimpleScalar may be * downloaded, compiled, executed, copied, and modified solely for nonprofit, * educational, noncommercial research, and noncommercial scholarship * purposes provided that this notice in its entirety accompanies all copies. * Copies of the modified software can be delivered to persons who use it * solely for nonprofit, educational, noncommercial research, and * noncommercial scholarship purposes provided that this notice in its * entirety accompanies all copies. * * 3. ALL COMMERCIAL USE, AND ALL USE BY FOR PROFIT ENTITIES, IS EXPRESSLY * PROHIBITED WITHOUT A LICENSE FROM SIMPLESCALAR, LLC (info@simplescalar.com). * * 4. No nonprofit user may place any restrictions on the use of this software, * including as modified by the user, by any other authorized user. * * 5. Noncommercial and nonprofit users may distribute copies of SimpleScalar * in compiled or executable form as set forth in Section 2, provided that * either: (A) it is accompanied by the corresponding machine-readable source * code, or (B) it is accompanied by a written offer, with no time limit, to * give anyone a machine-readable copy of the corresponding source code in * return for reimbursement of the cost of distribution. This written offer * must permit verbatim duplication by anyone, or (C) it is distributed by * someone who received only the executable form, and is accompanied by a * copy of the written offer of source code. * * 6. SimpleScalar was developed by Todd M. Austin, Ph.D. The tool suite is * currently maintained by SimpleScalar LLC (info@simplescalar.com). US Mail: * 2395 Timbercrest Court, Ann Arbor, MI 48105. * * Copyright (C) 1994-2003 by Todd M. Austin, Ph.D. and SimpleScalar, LLC. */#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <string.h>#include <ctype.h>#include <errno.h>#if defined(__alpha) || defined(linux)#include <unistd.h>#endif /* __alpha || linux */#include "host.h"#include "misc.h"#include "machine.h"/* verbose output flag */int verbose = FALSE;#ifdef DEBUG/* active debug flag */int debugging = FALSE;#endif /* DEBUG *//* fatal function hook, this function is called just before an exit caused by a fatal error, used to spew stats, etc. */static void (*hook_fn)(FILE *stream) = NULL;/* register a function to be called when an error is detected */voidfatal_hook(void (*fn)(FILE *stream)) /* fatal hook function */{ hook_fn = fn;}/* declare a fatal run-time error, calls fatal hook function */#ifdef __GNUC__void_fatal(char *file, char *func, int line, char *fmt, ...)#else /* !__GNUC__ */voidfatal(char *fmt, ...)#endif /* __GNUC__ */{ va_list v; va_start(v, fmt); fprintf(stderr, "fatal: "); myvfprintf(stderr, fmt, v);#ifdef __GNUC__ if (verbose) fprintf(stderr, " [%s:%s, line %d]", func, file, line);#endif /* __GNUC__ */ fprintf(stderr, "\n"); if (hook_fn) (*hook_fn)(stderr); exit(1);}/* declare a panic situation, dumps core */#ifdef __GNUC__void_panic(char *file, char *func, int line, char *fmt, ...)#else /* !__GNUC__ */voidpanic(char *fmt, ...)#endif /* __GNUC__ */{ va_list v; va_start(v, fmt); fprintf(stderr, "panic: "); myvfprintf(stderr, fmt, v);#ifdef __GNUC__ fprintf(stderr, " [%s:%s, line %d]", func, file, line);#endif /* __GNUC__ */ fprintf(stderr, "\n"); if (hook_fn) (*hook_fn)(stderr); abort();}/* declare a warning */#ifdef __GNUC__void_warn(char *file, char *func, int line, char *fmt, ...)#else /* !__GNUC__ */voidwarn(char *fmt, ...)#endif /* __GNUC__ */{ va_list v; va_start(v, fmt); fprintf(stderr, "warning: "); myvfprintf(stderr, fmt, v);#ifdef __GNUC__ if (verbose) fprintf(stderr, " [%s:%s, line %d]", func, file, line);#endif /* __GNUC__ */ fprintf(stderr, "\n");}/* print general information */#ifdef __GNUC__void_info(char *file, char *func, int line, char *fmt, ...)#else /* !__GNUC__ */voidinfo(char *fmt, ...)#endif /* __GNUC__ */{ va_list v; va_start(v, fmt); myvfprintf(stderr, fmt, v);#ifdef __GNUC__ if (verbose) fprintf(stderr, " [%s:%s, line %d]", func, file, line);#endif /* __GNUC__ */ fprintf(stderr, "\n");}#ifdef DEBUG/* print a debugging message */#ifdef __GNUC__void_debug(char *file, char *func, int line, char *fmt, ...)#else /* !__GNUC__ */voiddebug(char *fmt, ...)#endif /* __GNUC__ */{ va_list v; va_start(v, fmt); if (debugging) { fprintf(stderr, "debug: "); myvfprintf(stderr, fmt, v);#ifdef __GNUC__ fprintf(stderr, " [%s:%s, line %d]", func, file, line);#endif fprintf(stderr, "\n"); }}#endif /* DEBUG *//* seed the random number generator */voidmysrand(unsigned int seed) /* random number generator seed */{#if defined(hpux) || defined(__hpux) || defined(__svr4__) || defined(_MSC_VER) srand(seed);#else srandom(seed);#endif}/* get a random number */intmyrand(void) /* returns random number */{#if !defined(__alpha) && !defined(unix) extern long random(void);#endif#if defined(hpux) || defined(__hpux) || defined(__svr4__) || defined(_MSC_VER) return rand();#else return random();#endif}/* copy a string to a new storage allocation (NOTE: many machines are missing this trivial function, so I funcdup() it here...) */char * /* duplicated string */mystrdup(char *s) /* string to duplicate to heap storage */{ char *buf; if (!(buf = (char *)malloc(strlen(s)+1))) return NULL; strcpy(buf, s); return buf;}/* find the last occurrence of a character in a string */char *mystrrchr(char *s, char c){ char *rtnval = 0; do { if (*s == c) rtnval = s; } while (*s++); return rtnval;}/* case insensitive string compare (NOTE: many machines are missing this trivial function, so I funcdup() it here...) */int /* compare result, see strcmp() */mystricmp(char *s1, char *s2) /* strings to compare, case insensitive */{ unsigned char u1, u2; for (;;) { u1 = (unsigned char)*s1++; u1 = tolower(u1); u2 = (unsigned char)*s2++; u2 = tolower(u2); if (u1 != u2) return u1 - u2; if (u1 == '\0') return 0; }}/* allocate some core, this memory has overhead no larger than a page in size and it cannot be released. the storage is returned cleared */void *getcore(int nbytes){ return calloc(nbytes, 1);#if 0 /* FIXME: sbrk() calls break malloc() on Linux... */#if !defined(PURIFY) && !defined(_MSC_VER) void *p = (void *)sbrk(nbytes); if (p == (void *)-1) return NULL; /* this may be superfluous */#if defined(__svr4__) || defined(_MSC_VER) memset(p, '\0', nbytes);#else /* !defined(__svr4__) */ bzero(p, nbytes);#endif return p;#else return calloc(nbytes, 1);#endif /* PURIFY */#endif}/* return log of a number to the base 2 */intlog_base2(int n){ int power = 0; if (n <= 0 || (n & (n-1)) != 0) panic("log2() only works for positive power of two values"); while (n >>= 1) power++; return power;}/* return string describing elapsed time, passed in SEC in seconds */char *elapsed_time(long sec){ static char tstr[256]; char temp[256]; if (sec <= 0) return "0s"; tstr[0] = '\0'; /* days */ if (sec >= 86400) { sprintf(temp, "%ldD ", sec/86400); strcat(tstr, temp); sec = sec % 86400; } /* hours */ if (sec >= 3600) { sprintf(temp, "%ldh ", sec/3600); strcat(tstr, temp); sec = sec % 3600; } /* mins */ if (sec >= 60) { sprintf(temp, "%ldm ", sec/60); strcat(tstr, temp); sec = sec % 60; } /* secs */ if (sec >= 1) { sprintf(temp, "%lds ", sec); strcat(tstr, temp); } tstr[strlen(tstr)-1] = '\0'; return tstr;}/* assume bit positions numbered 31 to 0 (31 high order bit), extract num bits from word starting at position pos (with pos as the high order bit of those to be extracted), result is right justified and zero filled to high order bit, for example, extractl(word, 6, 3) w/ 8 bit word = 01101011 returns 00000110 */unsigned intextractl(int word, /* the word from which to extract */ int pos, /* bit positions 31 to 0 */ int num) /* number of bits to extract */{ return(((unsigned int) word >> (pos + 1 - num)) & ~(~0 << num));}#define PUT(p, n) \ { \ int nn, cc; \ \ for (nn = 0; nn < n; nn++) \ { \ cc = *(p+nn); \ *obuf++ = cc; \ } \ }#define PAD(s, n) \ { \ int nn, cc; \ \ cc = *s; \ for (nn = n; nn > 0; nn--) \ *obuf++ = cc; \ }#ifdef HOST_HAS_QWORD#define HIBITL LL(0x8000000000000000)typedef sqword_t slargeint_t;typedef qword_t largeint_t;#else /* !HOST_HAS_QWORD */#define HIBITL 0x80000000Ltypedef sword_t slargeint_t;typedef word_t largeint_t;#endif /* HOST_HAS_QWORD */static int_lowdigit(slargeint_t *valptr){ /* this function computes the decimal low-order digit of the number pointed to by valptr, and returns this digit after dividing *valptr by ten; this function is called ONLY to compute the low-order digit of a long whose high-order bit is set */ int lowbit = (int)(*valptr & 1); slargeint_t value = (*valptr >> 1) & ~HIBITL; *valptr = value / 5; return (int)(value % 5 * 2 + lowbit + '0');}/* portable vsprintf with qword support, returns end pointer */char *myvsprintf(char *obuf, char *format, va_list v){ static char _blanks[] = " "; static char _zeroes[] = "00000000000000000000"; /* counts output characters */ int count = 0; /* format code */ int fcode; /* field width and precision */ int width, prec; /* number of padding zeroes required on the left and right */ int lzero; /* length of prefix */ int prefixlength; /* combined length of leading zeroes, trailing zeroes, and suffix */ int otherlength; /* format flags */#define PADZERO 0x0001 /* padding zeroes requested via '0' */#define RZERO 0x0002 /* there will be trailing zeros in output */#define LZERO 0x0004 /* there will be leading zeroes in output */#define DOTSEEN 0x0008 /* dot appeared in format specification */#define LENGTH 0x0010 /* l */ int flagword; /* maximum number of digits in printable number */#define MAXDIGS 22 /* starting and ending points for value to be printed */ char *bp, *p; /* work variables */ int k, lradix, mradix; /* pointer to sign, "0x", "0X", or empty */ char *prefix; /* values are developed in this buffer */ static char buf[MAXDIGS*4], buf1[MAXDIGS*4]; /* pointer to a translate table for digits of whatever radix */ char *tab; /* value being converted, if integer */ slargeint_t val; /* value being converted, if floating point */ dfloat_t fval; for (;;) { int n; while ((fcode = *format) != '\0' && fcode != '%') { *obuf++ = fcode; format++; count++; } if (fcode == '\0') { /* end of format; terminate and return */ *obuf = '\0'; return obuf; } /* % has been found, the following switch is used to parse the format specification and to perform the operation specified by the format letter; the program repeatedly goes back to this switch until the format letter is encountered */ width = prefixlength = otherlength = flagword = 0; format++; charswitch: switch (fcode = *format++) { case '0': /* means pad with leading zeros */ flagword |= PADZERO; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { int num = fcode - '0'; while (isdigit(fcode = *format)) { num = num * 10 + fcode - '0'; format++; } if (flagword & DOTSEEN) prec = num; else width = num; goto charswitch; } case '.': flagword |= DOTSEEN; goto charswitch; case 'l': flagword |= LENGTH; goto charswitch; case 'n': /* host counter */#ifdef HOST_HAS_QWORD flagword |= LENGTH; /* fallthru */#else /* !HOST_HAS_QWORD */ flagword |= DOTSEEN; if (!width) width = 12; prec = 0; goto process_float;#endif /* HOST_HAS_QWORD */ case 'd': /* fetch the argument to be printed */ if (flagword & LENGTH) val = va_arg(v, slargeint_t); else val = (slargeint_t)va_arg(v, sword_t); /* set buffer pointer to last digit */ p = bp = buf + MAXDIGS; /* If signed conversion, make sign */ if (val < 0) { prefix = "-"; prefixlength = 1; /* negate, checking in advance for possible overflow */ if (val != (slargeint_t)HIBITL) val = -val; else { /* number is -HIBITL; convert last digit and get pos num */ *--bp = _lowdigit(&val); } } decimal: { slargeint_t qval = val; if (qval <= 9) *--bp = (int)qval + '0'; else { do { n = (int)qval; qval /= 10; *--bp = n - (int)qval * 10 + '0'; } while (qval > 9); *--bp = (int)qval + '0'; } } break; case 'u': /* fetch the argument to be printed */ if (flagword & LENGTH) val = va_arg(v, largeint_t); else
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?