-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.h
257 lines (208 loc) · 7.96 KB
/
misc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* misc.h - miscellaneous interfaces */
/* 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.
*/
#ifndef MISC_H
#define MISC_H
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
/* boolean value defs */
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
/* various useful macros */
#ifndef MAX
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
#endif
#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
/* for printing out "long long" vars */
#define LLHIGH(L) ((int)(((L)>>32) & 0xffffffff))
#define LLLOW(L) ((int)((L) & 0xffffffff))
/* size of an array, in elements */
#define N_ELT(ARR) (sizeof(ARR)/sizeof((ARR)[0]))
/* rounding macros, assumes ALIGN is a power of two */
#define ROUND_UP(N,ALIGN) (((N) + ((ALIGN)-1)) & ~((ALIGN)-1))
#define ROUND_DOWN(N,ALIGN) ((N) & ~((ALIGN)-1))
/* verbose output flag */
extern int verbose;
#ifdef DEBUG
/* active debug flag */
extern int debugging;
#endif /* DEBUG */
/* register a function to be called when an error is detected */
void
fatal_hook(void (*hook_fn)(FILE *stream)); /* fatal hook function */
#ifdef __GNUC__
/* declare a fatal run-time error, calls fatal hook function */
#define fatal(fmt, args...) \
_fatal(__FILE__, __FUNCTION__, __LINE__, fmt, ## args)
void
_fatal(char *file, const char *func, int line, char *fmt, ...)
__attribute__ ((noreturn));
#else /* !__GNUC__ */
void
fatal(char *fmt, ...);
#endif /* !__GNUC__ */
#ifdef __GNUC__
/* declare a panic situation, dumps core */
#define panic(fmt, args...) \
_panic(__FILE__, __FUNCTION__, __LINE__, fmt, ## args)
void
_panic(char *file, const char *func, int line, char *fmt, ...)
__attribute__ ((noreturn));
#else /* !__GNUC__ */
void
panic(char *fmt, ...);
#endif /* !__GNUC__ */
#ifdef __GNUC__
/* declare a warning */
#define warn(fmt, args...) \
_warn(__FILE__, __FUNCTION__, __LINE__, fmt, ## args)
void
_warn(char *file, const char *func, int line, char *fmt, ...);
#else /* !__GNUC__ */
void
warn(char *fmt, ...);
#endif /* !__GNUC__ */
#ifdef __GNUC__
/* print general information */
#define info(fmt, args...) \
_info(__FILE__, __FUNCTION__, __LINE__, fmt, ## args)
void
_info(char *file, const char *func, int line, char *fmt, ...);
#else /* !__GNUC__ */
void
info(char *fmt, ...);
#endif /* !__GNUC__ */
#ifdef DEBUG
#ifdef __GNUC__
/* print a debugging message */
#define debug(fmt, args...) \
do { \
if (debugging) \
_debug(__FILE__, __FUNCTION__, __LINE__, fmt, ## args); \
} while(0)
void
_debug(char *file, const char *func, int line, char *fmt, ...);
#else /* !__GNUC__ */
void
debug(char *fmt, ...);
#endif /* !__GNUC__ */
#else /* !DEBUG */
#ifdef __GNUC__
#define debug(fmt, args...)
#else /* !__GNUC__ */
/* the optimizer should eliminate this call! */
static void debug(char *fmt, ...) {}
#endif /* !__GNUC__ */
#endif /* !DEBUG */
/* seed the random number generator */
void
mysrand(unsigned int seed); /* random number generator seed */
/* get a random number */
int myrand(void); /* returns random number */
/* 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 */
/* find the last occurrence of a character in a string */
char *
mystrrchr(char *s, char c);
/* 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 */
/* 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 log of a number to the base 2 */
int log_base2(int n);
/* return string describing elapsed time, passed in SEC in seconds */
char *elapsed_time(long sec);
/* 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 int
extractl(int word, /* the word from which to extract */
int pos, /* bit positions 31 to 0 */
int num); /* number of bits to extract */
#if defined(sparc) && !defined(__svr4__)
#define strtoul strtol
#endif
/* portable 64-bit I/O package */
/* portable vsprintf with qword support, returns end pointer */
char *myvsprintf(char *obuf, char *format, va_list v);
/* portable sprintf with qword support, returns end pointer */
char *mysprintf(char *obuf, char *format, ...);
/* portable vfprintf with qword support, returns end pointer */
void myvfprintf(FILE *stream, char *format, va_list v);
/* portable fprintf with qword support, returns end pointer */
void myfprintf(FILE *stream, char *format, ...);
#ifdef HOST_HAS_QWORD
/* convert a string to a signed result */
sqword_t myatosq(char *nptr, char **endp, int base);
/* convert a string to a unsigned result */
qword_t myatoq(char *nptr, char **endp, int base);
#endif /* HOST_HAS_QWORD */
/* same semantics as fopen() except that filenames ending with a ".gz" or ".Z"
will be automagically get compressed */
FILE *gzopen(char *fname, char *type);
/* close compressed stream */
void gzclose(FILE *fd);
/* update the CRC on the data block one byte at a time */
word_t crc(word_t crc_accum, word_t data);
#endif /* MISC_H */