nasmlib: added string replace (strrep) utility function
[nasm/nasm.git] / nasmlib.h
blob12f9ca122863ca8f1d5065046dee4697b460f521
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * nasmlib.h header file for nasmlib.c
38 #ifndef NASM_NASMLIB_H
39 #define NASM_NASMLIB_H
41 #include "compiler.h"
43 #include <inttypes.h>
44 #include <stdio.h>
45 #include <string.h>
46 #ifdef HAVE_STRINGS_H
47 #include <strings.h>
48 #endif
51 * tolower table -- avoids a function call on some platforms.
52 * NOTE: unlike the tolower() function in ctype, EOF is *NOT*
53 * a permitted value, for obvious reasons.
55 void tolower_init(void);
56 extern unsigned char nasm_tolower_tab[256];
57 #define nasm_tolower(x) nasm_tolower_tab[(unsigned char)(x)]
59 /* Wrappers around <ctype.h> functions */
60 /* These are only valid for values that cannot include EOF */
61 #define nasm_isspace(x) isspace((unsigned char)(x))
62 #define nasm_isalpha(x) isalpha((unsigned char)(x))
63 #define nasm_isdigit(x) isdigit((unsigned char)(x))
64 #define nasm_isalnum(x) isalnum((unsigned char)(x))
65 #define nasm_isxdigit(x) isxdigit((unsigned char)(x))
68 * If this is defined, the wrappers around malloc et al will
69 * transform into logging variants, which will cause NASM to create
70 * a file called `malloc.log' when run, and spew details of all its
71 * memory management into that. That can then be analysed to detect
72 * memory leaks and potentially other problems too.
74 /* #define LOGALLOC */
77 * -------------------------
78 * Error reporting functions
79 * -------------------------
83 * An error reporting function should look like this.
85 typedef void (*efunc) (int severity, const char *fmt, ...);
86 typedef void (*vefunc) (int severity, const char *fmt, va_list ap);
87 #ifdef __GNUC__
88 void nasm_error(int severity, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
89 #else
90 void nasm_error(int severity, const char *fmt, ...);
91 #endif
92 void nasm_set_verror(vefunc);
95 * These are the error severity codes which get passed as the first
96 * argument to an efunc.
99 #define ERR_DEBUG 0x00000008 /* put out debugging message */
100 #define ERR_WARNING 0x00000000 /* warn only: no further action */
101 #define ERR_NONFATAL 0x00000001 /* terminate assembly after phase */
102 #define ERR_FATAL 0x00000002 /* instantly fatal: exit with error */
103 #define ERR_PANIC 0x00000003 /* internal error: panic instantly
104 * and dump core for reference */
105 #define ERR_MASK 0x0000000F /* mask off the above codes */
106 #define ERR_NOFILE 0x00000010 /* don't give source file name/line */
107 #define ERR_USAGE 0x00000020 /* print a usage message */
108 #define ERR_PASS1 0x00000040 /* only print this error on pass one */
109 #define ERR_PASS2 0x00000080
110 #define ERR_NO_SEVERITY 0x00000100 /* suppress printing severity */
113 * These codes define specific types of suppressible warning.
116 #define ERR_WARN_MASK 0xFFFFF000 /* the mask for this feature */
117 #define ERR_WARN_SHR 12 /* how far to shift right */
119 #define WARN(x) ((x) << ERR_WARN_SHR)
121 #define ERR_WARN_MNP WARN( 1) /* macro-num-parameters warning */
122 #define ERR_WARN_MSR WARN( 2) /* macro self-reference */
123 #define ERR_WARN_MDP WARN( 3) /* macro default parameters check */
124 #define ERR_WARN_OL WARN( 4) /* orphan label (no colon, and
125 * alone on line) */
126 #define ERR_WARN_NOV WARN( 5) /* numeric overflow */
127 #define ERR_WARN_GNUELF WARN( 6) /* using GNU ELF extensions */
128 #define ERR_WARN_FL_OVERFLOW WARN( 7) /* FP overflow */
129 #define ERR_WARN_FL_DENORM WARN( 8) /* FP denormal */
130 #define ERR_WARN_FL_UNDERFLOW WARN( 9) /* FP underflow */
131 #define ERR_WARN_FL_TOOLONG WARN(10) /* FP too many digits */
132 #define ERR_WARN_USER WARN(11) /* %warning directives */
133 #define ERR_WARN_MAX 11 /* the highest numbered one */
136 * Wrappers around malloc, realloc and free. nasm_malloc will
137 * fatal-error and die rather than return NULL; nasm_realloc will
138 * do likewise, and will also guarantee to work right on being
139 * passed a NULL pointer; nasm_free will do nothing if it is passed
140 * a NULL pointer.
142 void nasm_init_malloc_error(void);
143 #ifndef LOGALLOC
144 void *nasm_malloc(size_t);
145 void *nasm_zalloc(size_t);
146 void *nasm_realloc(void *, size_t);
147 void nasm_free(void *);
148 char *nasm_strdup(const char *);
149 char *nasm_strndup(const char *, size_t);
150 #else
151 void *nasm_malloc_log(const char *, int, size_t);
152 void *nasm_zalloc_log(const char *, int, size_t);
153 void *nasm_realloc_log(const char *, int, void *, size_t);
154 void nasm_free_log(const char *, int, void *);
155 char *nasm_strdup_log(const char *, int, const char *);
156 char *nasm_strndup_log(const char *, int, const char *, size_t);
157 #define nasm_malloc(x) nasm_malloc_log(__FILE__,__LINE__,x)
158 #define nasm_zalloc(x) nasm_zalloc_log(__FILE__,__LINE__,x)
159 #define nasm_realloc(x,y) nasm_realloc_log(__FILE__,__LINE__,x,y)
160 #define nasm_free(x) nasm_free_log(__FILE__,__LINE__,x)
161 #define nasm_strdup(x) nasm_strdup_log(__FILE__,__LINE__,x)
162 #define nasm_strndup(x,y) nasm_strndup_log(__FILE__,__LINE__,x,y)
163 #endif
166 * NASM assert failure
168 no_return nasm_assert_failed(const char *, int, const char *);
169 #define nasm_assert(x) \
170 do { \
171 if (unlikely(!(x))) \
172 nasm_assert_failed(__FILE__,__LINE__,#x); \
173 } while (0)
176 * NASM failure at build time if x != 0
178 #define nasm_build_assert(x) (void)(sizeof(char[1-2*!!(x)]))
181 * ANSI doesn't guarantee the presence of `stricmp' or
182 * `strcasecmp'.
184 #if defined(HAVE_STRCASECMP)
185 #define nasm_stricmp strcasecmp
186 #elif defined(HAVE_STRICMP)
187 #define nasm_stricmp stricmp
188 #else
189 int nasm_stricmp(const char *, const char *);
190 #endif
192 #if defined(HAVE_STRNCASECMP)
193 #define nasm_strnicmp strncasecmp
194 #elif defined(HAVE_STRNICMP)
195 #define nasm_strnicmp strnicmp
196 #else
197 int nasm_strnicmp(const char *, const char *, size_t);
198 #endif
200 int nasm_memicmp(const char *, const char *, size_t);
202 #if defined(HAVE_STRSEP)
203 #define nasm_strsep strsep
204 #else
205 char *nasm_strsep(char **stringp, const char *delim);
206 #endif
209 * Replace all instances of `str` with `sub` in `lin`
211 char *nasm_strrep(const char *str, const char *sub, char *lin, bool casesense);
215 * Convert a string into a number, using NASM number rules. Sets
216 * `*error' to true if an error occurs, and false otherwise.
218 int64_t readnum(char *str, bool *error);
221 * Convert a character constant into a number. Sets
222 * `*warn' to true if an overflow occurs, and false otherwise.
223 * str points to and length covers the middle of the string,
224 * without the quotes.
226 int64_t readstrnum(char *str, int length, bool *warn);
229 * seg_init: Initialise the segment-number allocator.
230 * seg_alloc: allocate a hitherto unused segment number.
232 void seg_init(void);
233 int32_t seg_alloc(void);
236 * many output formats will be able to make use of this: a standard
237 * function to add an extension to the name of the input file
239 void standard_extension(char *inname, char *outname, char *extension);
242 * Utility macros...
244 * This is a useful #define which I keep meaning to use more often:
245 * the number of elements of a statically defined array.
247 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
250 * List handling
252 * list_for_each - regular iterator over list
253 * list_for_each_safe - the same but safe against list items removal
255 #define list_for_each(pos, head) \
256 for (pos = head; pos; pos = pos->next)
257 #define list_for_each_safe(pos, n, head) \
258 for (pos = head, n = (pos ? pos->next : NULL); pos; \
259 pos = n, n = (n ? n->next : NULL))
262 * Power of 2 align helpers
264 #define ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask))
265 #define ALIGN(v, a) ALIGN_MASK(v, (a) - 1)
266 #define IS_ALIGNED(v, a) (((v) & ((a) - 1)) == 0)
269 * some handy macros that will probably be of use in more than one
270 * output format: convert integers into little-endian byte packed
271 * format in memory
274 #if X86_MEMORY
276 #define WRITECHAR(p,v) \
277 do { \
278 *(uint8_t *)(p) = (v); \
279 (p) += 1; \
280 } while (0)
282 #define WRITESHORT(p,v) \
283 do { \
284 *(uint16_t *)(p) = (v); \
285 (p) += 2; \
286 } while (0)
288 #define WRITELONG(p,v) \
289 do { \
290 *(uint32_t *)(p) = (v); \
291 (p) += 4; \
292 } while (0)
294 #define WRITEDLONG(p,v) \
295 do { \
296 *(uint64_t *)(p) = (v); \
297 (p) += 8; \
298 } while (0)
300 #define WRITEADDR(p,v,s) \
301 do { \
302 uint64_t _wa_v = (v); \
303 memcpy((p), &_wa_v, (s)); \
304 (p) += (s); \
305 } while (0)
307 #else /* !X86_MEMORY */
309 #define WRITECHAR(p,v) \
310 do { \
311 uint8_t *_wc_p = (uint8_t *)(p); \
312 uint8_t _wc_v = (v); \
313 _wc_p[0] = _wc_v; \
314 (p) = (void *)(_wc_p + 1); \
315 } while (0)
317 #define WRITESHORT(p,v) \
318 do { \
319 uint8_t *_ws_p = (uint8_t *)(p); \
320 uint16_t _ws_v = (v); \
321 _ws_p[0] = _ws_v; \
322 _ws_p[1] = _ws_v >> 8; \
323 (p) = (void *)(_ws_p + 2); \
324 } while (0)
326 #define WRITELONG(p,v) \
327 do { \
328 uint8_t *_wl_p = (uint8_t *)(p); \
329 uint32_t _wl_v = (v); \
330 _wl_p[0] = _wl_v; \
331 _wl_p[1] = _wl_v >> 8; \
332 _wl_p[2] = _wl_v >> 16; \
333 _wl_p[3] = _wl_v >> 24; \
334 (p) = (void *)(_wl_p + 4); \
335 } while (0)
337 #define WRITEDLONG(p,v) \
338 do { \
339 uint8_t *_wq_p = (uint8_t *)(p); \
340 uint64_t _wq_v = (v); \
341 _wq_p[0] = _wq_v; \
342 _wq_p[1] = _wq_v >> 8; \
343 _wq_p[2] = _wq_v >> 16; \
344 _wq_p[3] = _wq_v >> 24; \
345 _wq_p[4] = _wq_v >> 32; \
346 _wq_p[5] = _wq_v >> 40; \
347 _wq_p[6] = _wq_v >> 48; \
348 _wq_p[7] = _wq_v >> 56; \
349 (p) = (void *)(_wq_p + 8); \
350 } while (0)
352 #define WRITEADDR(p,v,s) \
353 do { \
354 int _wa_s = (s); \
355 uint64_t _wa_v = (v); \
356 while (_wa_s--) { \
357 WRITECHAR(p,_wa_v); \
358 _wa_v >>= 8; \
360 } while(0)
362 #endif
365 * and routines to do the same thing to a file
367 #define fwriteint8_t(d,f) putc(d,f)
368 void fwriteint16_t(uint16_t data, FILE * fp);
369 void fwriteint32_t(uint32_t data, FILE * fp);
370 void fwriteint64_t(uint64_t data, FILE * fp);
371 void fwriteaddr(uint64_t data, int size, FILE * fp);
374 * Binary search routine. Returns index into `array' of an entry
375 * matching `string', or <0 if no match. `array' is taken to
376 * contain `size' elements.
378 * bsi() is case sensitive, bsii() is case insensitive.
380 int bsi(const char *string, const char **array, int size);
381 int bsii(const char *string, const char **array, int size);
383 char *src_set_fname(char *newname);
384 int32_t src_set_linnum(int32_t newline);
385 int32_t src_get_linnum(void);
387 * src_get may be used if you simply want to know the source file and line.
388 * It is also used if you maintain private status about the source location
389 * It return 0 if the information was the same as the last time you
390 * checked, -1 if the name changed and (new-old) if just the line changed.
392 int src_get(int32_t *xline, char **xname);
394 char *nasm_strcat(const char *one, const char *two);
396 char *nasm_skip_spaces(const char *p);
397 char *nasm_skip_word(const char *p);
398 char *nasm_zap_spaces_fwd(char *p);
399 char *nasm_zap_spaces_rev(char *p);
400 char *nasm_trim_spaces(char *p);
401 char *nasm_get_word(char *p, char **tail);
402 char *nasm_opt_val(char *p, char **opt, char **val);
404 const char *prefix_name(int);
406 #define ZERO_BUF_SIZE 4096
407 extern const uint8_t zero_buffer[ZERO_BUF_SIZE];
408 size_t fwritezero(size_t bytes, FILE *fp);
410 static inline bool overflow_general(int64_t value, int bytes)
412 int sbit;
413 int64_t vmax, vmin;
415 if (bytes >= 8)
416 return false;
418 sbit = (bytes << 3) - 1;
419 vmax = ((int64_t)2 << sbit) - 1;
420 vmin = -((int64_t)1 << sbit);
422 return value < vmin || value > vmax;
425 static inline bool overflow_signed(int64_t value, int bytes)
427 int sbit;
428 int64_t vmax, vmin;
430 if (bytes >= 8)
431 return false;
433 sbit = (bytes << 3) - 1;
434 vmax = ((int64_t)1 << sbit) - 1;
435 vmin = -((int64_t)1 << sbit);
437 return value < vmin || value > vmax;
440 static inline bool overflow_unsigned(int64_t value, int bytes)
442 int sbit;
443 int64_t vmax, vmin;
445 if (bytes >= 8)
446 return false;
448 sbit = (bytes << 3) - 1;
449 vmax = ((int64_t)2 << sbit) - 1;
450 vmin = 0;
452 return value < vmin || value > vmax;
455 static inline int64_t signed_bits(int64_t value, int bits)
457 if (bits < 64) {
458 value &= ((int64_t)1 << bits) - 1;
459 if (value & (int64_t)1 << (bits - 1))
460 value |= (int64_t)-1 << bits;
462 return value;
465 int idata_bytes(int opcode);
467 /* check if value is power of 2 */
468 #define is_power2(v) ((v) && ((v) & ((v) - 1)) == 0)
471 * floor(log2(v))
473 int ilog2_32(uint32_t v);
474 int ilog2_64(uint64_t v);
477 * v == 0 ? 0 : is_power2(x) ? ilog2_X(v) : -1
479 int alignlog2_32(uint32_t v);
480 int alignlog2_64(uint64_t v);
482 #endif