Makefile: make it possible to compile with gcc link-time optimization
[nasm.git] / include / nasmlib.h
blob7d712e9747acfbc8022f4da750f3d2bd66596252
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2017 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 <stdio.h>
44 #include <string.h>
45 #ifdef HAVE_STRINGS_H
46 # include <strings.h>
47 #endif
50 * tolower table -- avoids a function call on some platforms.
51 * NOTE: unlike the tolower() function in ctype, EOF is *NOT*
52 * a permitted value, for obvious reasons.
54 void tolower_init(void);
55 extern unsigned char nasm_tolower_tab[256];
56 #define nasm_tolower(x) nasm_tolower_tab[(unsigned char)(x)]
58 /* Wrappers around <ctype.h> functions */
59 /* These are only valid for values that cannot include EOF */
60 #define nasm_isspace(x) isspace((unsigned char)(x))
61 #define nasm_isalpha(x) isalpha((unsigned char)(x))
62 #define nasm_isdigit(x) isdigit((unsigned char)(x))
63 #define nasm_isalnum(x) isalnum((unsigned char)(x))
64 #define nasm_isxdigit(x) isxdigit((unsigned char)(x))
67 * -------------------------
68 * Error reporting functions
69 * -------------------------
73 * An error reporting function should look like this.
75 void printf_func(2, 3) nasm_error(int severity, const char *fmt, ...);
76 no_return printf_func(2, 3) nasm_fatal(int flags, const char *fmt, ...);
77 no_return printf_func(2, 3) nasm_panic(int flags, const char *fmt, ...);
78 no_return nasm_panic_from_macro(const char *file, int line);
79 #define panic() nasm_panic_from_macro(__FILE__, __LINE__);
81 typedef void (*vefunc) (int severity, const char *fmt, va_list ap);
82 extern vefunc nasm_verror;
83 static inline vefunc nasm_set_verror(vefunc ve)
85 vefunc old_verror = nasm_verror;
86 nasm_verror = ve;
87 return old_verror;
91 * These are the error severity codes which get passed as the first
92 * argument to an efunc.
95 #define ERR_DEBUG 0x00000000 /* put out debugging message */
96 #define ERR_WARNING 0x00000001 /* warn only: no further action */
97 #define ERR_NONFATAL 0x00000002 /* terminate assembly after phase */
98 #define ERR_FATAL 0x00000006 /* instantly fatal: exit with error */
99 #define ERR_PANIC 0x00000007 /* internal error: panic instantly
100 * and dump core for reference */
101 #define ERR_MASK 0x00000007 /* mask off the above codes */
102 #define ERR_NOFILE 0x00000010 /* don't give source file name/line */
103 #define ERR_USAGE 0x00000020 /* print a usage message */
104 #define ERR_PASS1 0x00000040 /* only print this error on pass one */
105 #define ERR_PASS2 0x00000080
107 #define ERR_NO_SEVERITY 0x00000100 /* suppress printing severity */
108 #define ERR_PP_PRECOND 0x00000200 /* for preprocessor use */
109 #define ERR_PP_LISTMACRO 0x00000400 /* from preproc->error_list_macros() */
112 * These codes define specific types of suppressible warning.
115 #define ERR_WARN_MASK 0xFFFFF000 /* the mask for this feature */
116 #define ERR_WARN_SHR 12 /* how far to shift right */
118 #define WARN(x) ((x) << ERR_WARN_SHR)
119 #define WARN_IDX(x) (((x) & ERR_WARN_MASK) >> ERR_WARN_SHR)
121 #define ERR_WARN_TERM WARN( 0) /* treat warnings as errors */
122 #define ERR_WARN_MNP WARN( 1) /* macro-num-parameters warning */
123 #define ERR_WARN_MSR WARN( 2) /* macro self-reference */
124 #define ERR_WARN_MDP WARN( 3) /* macro default parameters check */
125 #define ERR_WARN_OL WARN( 4) /* orphan label (no colon, and
126 * alone on line) */
127 #define ERR_WARN_NOV WARN( 5) /* numeric overflow */
128 #define ERR_WARN_GNUELF WARN( 6) /* using GNU ELF extensions */
129 #define ERR_WARN_FL_OVERFLOW WARN( 7) /* FP overflow */
130 #define ERR_WARN_FL_DENORM WARN( 8) /* FP denormal */
131 #define ERR_WARN_FL_UNDERFLOW WARN( 9) /* FP underflow */
132 #define ERR_WARN_FL_TOOLONG WARN(10) /* FP too many digits */
133 #define ERR_WARN_USER WARN(11) /* %warning directives */
134 #define ERR_WARN_LOCK WARN(12) /* bad LOCK prefixes */
135 #define ERR_WARN_HLE WARN(13) /* bad HLE prefixes */
136 #define ERR_WARN_BND WARN(14) /* bad BND prefixes */
137 #define ERR_WARN_ZEXTRELOC WARN(15) /* relocation zero-extended */
138 #define ERR_WARN_PTR WARN(16) /* not a NASM keyword */
139 #define ERR_WARN_MAX 16 /* the highest numbered one */
142 * Wrappers around malloc, realloc and free. nasm_malloc will
143 * fatal-error and die rather than return NULL; nasm_realloc will
144 * do likewise, and will also guarantee to work right on being
145 * passed a NULL pointer; nasm_free will do nothing if it is passed
146 * a NULL pointer.
148 void * safe_malloc(1) nasm_malloc(size_t);
149 void * safe_malloc(1) nasm_zalloc(size_t);
150 void * safe_malloc2(1,2) nasm_calloc(size_t, size_t);
151 void * safe_realloc(2) nasm_realloc(void *, size_t);
152 void nasm_free(void *);
153 char * safe_alloc nasm_strdup(const char *);
154 char * safe_alloc nasm_strndup(const char *, size_t);
156 /* Assert the argument is a pointer without evaluating it */
157 #define nasm_assert_pointer(p) ((void)sizeof(*(p)))
159 #define nasm_new(p) ((p) = nasm_zalloc(sizeof(*(p))))
160 #define nasm_newn(p,n) ((p) = nasm_calloc(sizeof(*(p)),(n)))
162 * This is broken on platforms where there are pointers which don't
163 * match void * in their internal layout. It unfortunately also
164 * loses any "const" part of the argument, although hopefully the
165 * compiler will warn in that case.
167 #define nasm_delete(p) \
168 do { \
169 void **_pp = (void **)&(p); \
170 nasm_assert_pointer(p); \
171 nasm_free(*_pp); \
172 *_pp = NULL; \
173 } while (0)
174 #define nasm_zero(p) (memset((p), 0, sizeof(*(p))))
175 #define nasm_zeron(p,n) (memset((p), 0, (n)*sizeof(*(p))))
178 * Wrapper around fwrite() which fatal-errors on output failure.
180 void nasm_write(const void *, size_t, FILE *);
183 * NASM assert failure
185 no_return nasm_assert_failed(const char *, int, const char *);
186 #define nasm_assert(x) \
187 do { \
188 if (unlikely(!(x))) \
189 nasm_assert_failed(__FILE__,__LINE__,#x); \
190 } while (0)
193 * NASM failure at build time if the argument is false
195 #ifdef static_assert
196 # define nasm_static_assert(x) static_assert(x, #x)
197 #elif defined(HAVE_FUNC_ATTRIBUTE_ERROR) && defined(__OPTIMIZE__)
198 # define nasm_static_assert(x) \
199 if (!(x)) { \
200 extern void __attribute__((error("assertion " #x " failed"))) \
201 _nasm_static_fail(void); \
202 _nasm_static_fail(); \
204 #else
205 /* See http://www.drdobbs.com/compile-time-assertions/184401873 */
206 # define nasm_static_assert(x) \
207 do { enum { _static_assert_failed = 1/(!!(x)) }; } while (0)
208 #endif
211 * ANSI doesn't guarantee the presence of `stricmp' or
212 * `strcasecmp'.
214 #if defined(HAVE_STRCASECMP)
215 #define nasm_stricmp strcasecmp
216 #elif defined(HAVE_STRICMP)
217 #define nasm_stricmp stricmp
218 #else
219 int pure_func nasm_stricmp(const char *, const char *);
220 #endif
222 #if defined(HAVE_STRNCASECMP)
223 #define nasm_strnicmp strncasecmp
224 #elif defined(HAVE_STRNICMP)
225 #define nasm_strnicmp strnicmp
226 #else
227 int pure_func nasm_strnicmp(const char *, const char *, size_t);
228 #endif
230 int pure_func nasm_memicmp(const char *, const char *, size_t);
232 #if defined(HAVE_STRSEP)
233 #define nasm_strsep strsep
234 #else
235 char *nasm_strsep(char **stringp, const char *delim);
236 #endif
238 #ifndef HAVE_DECL_STRNLEN
239 size_t pure_func strnlen(const char *, size_t);
240 #endif
242 /* This returns the numeric value of a given 'digit'. */
243 #define numvalue(c) ((c) >= 'a' ? (c) - 'a' + 10 : (c) >= 'A' ? (c) - 'A' + 10 : (c) - '0')
246 * Convert a string into a number, using NASM number rules. Sets
247 * `*error' to true if an error occurs, and false otherwise.
249 int64_t readnum(char *str, bool *error);
252 * Convert a character constant into a number. Sets
253 * `*warn' to true if an overflow occurs, and false otherwise.
254 * str points to and length covers the middle of the string,
255 * without the quotes.
257 int64_t readstrnum(char *str, int length, bool *warn);
260 * seg_init: Initialise the segment-number allocator.
261 * seg_alloc: allocate a hitherto unused segment number.
263 void pure_func seg_init(void);
264 int32_t pure_func seg_alloc(void);
267 * many output formats will be able to make use of this: a standard
268 * function to add an extension to the name of the input file
270 void standard_extension(char *inname, char *outname, char *extension);
273 * Utility macros...
275 * This is a useful #define which I keep meaning to use more often:
276 * the number of elements of a statically defined array.
278 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
281 * List handling
283 * list_for_each - regular iterator over list
284 * list_for_each_safe - the same but safe against list items removal
285 * list_last - find the last element in a list
287 #define list_for_each(pos, head) \
288 for (pos = head; pos; pos = pos->next)
289 #define list_for_each_safe(pos, n, head) \
290 for (pos = head, n = (pos ? pos->next : NULL); pos; \
291 pos = n, n = (n ? n->next : NULL))
292 #define list_last(pos, head) \
293 for (pos = head; pos && pos->next; pos = pos->next) \
295 #define list_reverse(head, prev, next) \
296 do { \
297 if (!head || !head->next) \
298 break; \
299 prev = NULL; \
300 while (head) { \
301 next = head->next; \
302 head->next = prev; \
303 prev = head; \
304 head = next; \
306 head = prev; \
307 } while (0)
310 * Power of 2 align helpers
312 #undef ALIGN_MASK /* Some BSD flavors define these in system headers */
313 #undef ALIGN
314 #define ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask))
315 #define ALIGN(v, a) ALIGN_MASK(v, (a) - 1)
316 #define IS_ALIGNED(v, a) (((v) & ((a) - 1)) == 0)
319 * some handy macros that will probably be of use in more than one
320 * output format: convert integers into little-endian byte packed
321 * format in memory
324 #if X86_MEMORY
326 #define WRITECHAR(p,v) \
327 do { \
328 *(uint8_t *)(p) = (v); \
329 (p) += 1; \
330 } while (0)
332 #define WRITESHORT(p,v) \
333 do { \
334 *(uint16_t *)(p) = (v); \
335 (p) += 2; \
336 } while (0)
338 #define WRITELONG(p,v) \
339 do { \
340 *(uint32_t *)(p) = (v); \
341 (p) += 4; \
342 } while (0)
344 #define WRITEDLONG(p,v) \
345 do { \
346 *(uint64_t *)(p) = (v); \
347 (p) += 8; \
348 } while (0)
350 #define WRITEADDR(p,v,s) \
351 do { \
352 uint64_t _wa_v = (v); \
353 memcpy((p), &_wa_v, (s)); \
354 (p) += (s); \
355 } while (0)
357 #else /* !X86_MEMORY */
359 #define WRITECHAR(p,v) \
360 do { \
361 uint8_t *_wc_p = (uint8_t *)(p); \
362 uint8_t _wc_v = (v); \
363 _wc_p[0] = _wc_v; \
364 (p) = (void *)(_wc_p + 1); \
365 } while (0)
367 #define WRITESHORT(p,v) \
368 do { \
369 uint8_t *_ws_p = (uint8_t *)(p); \
370 uint16_t _ws_v = (v); \
371 _ws_p[0] = _ws_v; \
372 _ws_p[1] = _ws_v >> 8; \
373 (p) = (void *)(_ws_p + 2); \
374 } while (0)
376 #define WRITELONG(p,v) \
377 do { \
378 uint8_t *_wl_p = (uint8_t *)(p); \
379 uint32_t _wl_v = (v); \
380 _wl_p[0] = _wl_v; \
381 _wl_p[1] = _wl_v >> 8; \
382 _wl_p[2] = _wl_v >> 16; \
383 _wl_p[3] = _wl_v >> 24; \
384 (p) = (void *)(_wl_p + 4); \
385 } while (0)
387 #define WRITEDLONG(p,v) \
388 do { \
389 uint8_t *_wq_p = (uint8_t *)(p); \
390 uint64_t _wq_v = (v); \
391 _wq_p[0] = _wq_v; \
392 _wq_p[1] = _wq_v >> 8; \
393 _wq_p[2] = _wq_v >> 16; \
394 _wq_p[3] = _wq_v >> 24; \
395 _wq_p[4] = _wq_v >> 32; \
396 _wq_p[5] = _wq_v >> 40; \
397 _wq_p[6] = _wq_v >> 48; \
398 _wq_p[7] = _wq_v >> 56; \
399 (p) = (void *)(_wq_p + 8); \
400 } while (0)
402 #define WRITEADDR(p,v,s) \
403 do { \
404 int _wa_s = (s); \
405 uint64_t _wa_v = (v); \
406 while (_wa_s--) { \
407 WRITECHAR(p,_wa_v); \
408 _wa_v >>= 8; \
410 } while(0)
412 #endif
415 * and routines to do the same thing to a file
417 #define fwriteint8_t(d,f) putc(d,f)
418 void fwriteint16_t(uint16_t data, FILE * fp);
419 void fwriteint32_t(uint32_t data, FILE * fp);
420 void fwriteint64_t(uint64_t data, FILE * fp);
421 void fwriteaddr(uint64_t data, int size, FILE * fp);
424 * Binary search routine. Returns index into `array' of an entry
425 * matching `string', or <0 if no match. `array' is taken to
426 * contain `size' elements.
428 * bsi() is case sensitive, bsii() is case insensitive.
430 int bsi(const char *string, const char **array, int size);
431 int bsii(const char *string, const char **array, int size);
434 * These functions are used to keep track of the source code file and name.
436 void src_init(void);
437 void src_free(void);
438 const char *src_set_fname(const char *newname);
439 const char *src_get_fname(void);
440 int32_t src_set_linnum(int32_t newline);
441 int32_t src_get_linnum(void);
442 /* Can be used when there is no need for the old information */
443 void src_set(int32_t line, const char *filename);
445 * src_get gets both the source file name and line.
446 * It is also used if you maintain private status about the source location
447 * It return 0 if the information was the same as the last time you
448 * checked, -2 if the name changed and (new-old) if just the line changed.
450 int32_t src_get(int32_t *xline, const char **xname);
452 char *nasm_strcat(const char *one, const char *two);
454 char *nasm_skip_spaces(const char *p);
455 char *nasm_skip_word(const char *p);
456 char *nasm_zap_spaces_fwd(char *p);
457 char *nasm_zap_spaces_rev(char *p);
458 char *nasm_trim_spaces(char *p);
459 char *nasm_get_word(char *p, char **tail);
460 char *nasm_opt_val(char *p, char **opt, char **val);
463 * Converts a relative pathname rel_path into an absolute path name.
465 * The buffer returned must be freed by the caller
467 char *nasm_realpath(const char *rel_path);
469 const char * pure_func prefix_name(int);
472 * Wrappers around fopen()... for future change to a dedicated structure
474 enum file_flags {
475 NF_BINARY = 0x00000000, /* Binary file (default) */
476 NF_TEXT = 0x00000001, /* Text file */
477 NF_NONFATAL = 0x00000000, /* Don't die on open failure (default) */
478 NF_FATAL = 0x00000002, /* Die on open failure */
479 NF_FORMAP = 0x00000004 /* Intended to use nasm_map_file() */
482 FILE *nasm_open_read(const char *filename, enum file_flags flags);
483 FILE *nasm_open_write(const char *filename, enum file_flags flags);
485 /* Probe for existence of a file */
486 bool nasm_file_exists(const char *filename);
488 #define ZERO_BUF_SIZE 65536 /* Default value */
489 #if defined(BUFSIZ) && (BUFSIZ > ZERO_BUF_SIZE)
490 # undef ZERO_BUF_SIZE
491 # define ZERO_BUF_SIZE BUFSIZ
492 #endif
493 extern const uint8_t zero_buffer[ZERO_BUF_SIZE];
495 /* Missing fseeko/ftello */
496 #ifndef HAVE_FSEEKO
497 # undef off_t /* Just in case it is a macro */
498 # ifdef HAVE__FSEEKI64
499 # define fseeko _fseeki64
500 # define ftello _ftelli64
501 # define off_t int64_t
502 # else
503 # define fseeko fseek
504 # define ftello ftell
505 # define off_t long
506 # endif
507 #endif
509 const void *nasm_map_file(FILE *fp, off_t start, off_t len);
510 void nasm_unmap_file(const void *p, size_t len);
511 off_t nasm_file_size(FILE *f);
512 off_t nasm_file_size_by_path(const char *pathname);
513 void fwritezero(off_t bytes, FILE *fp);
515 static inline bool const_func overflow_general(int64_t value, int bytes)
517 int sbit;
518 int64_t vmax, vmin;
520 if (bytes >= 8)
521 return false;
523 sbit = (bytes << 3) - 1;
524 vmax = ((int64_t)2 << sbit) - 1;
525 vmin = -((int64_t)1 << sbit);
527 return value < vmin || value > vmax;
530 static inline bool const_func overflow_signed(int64_t value, int bytes)
532 int sbit;
533 int64_t vmax, vmin;
535 if (bytes >= 8)
536 return false;
538 sbit = (bytes << 3) - 1;
539 vmax = ((int64_t)1 << sbit) - 1;
540 vmin = -((int64_t)1 << sbit);
542 return value < vmin || value > vmax;
545 static inline bool const_func overflow_unsigned(int64_t value, int bytes)
547 int sbit;
548 int64_t vmax, vmin;
550 if (bytes >= 8)
551 return false;
553 sbit = (bytes << 3) - 1;
554 vmax = ((int64_t)2 << sbit) - 1;
555 vmin = 0;
557 return value < vmin || value > vmax;
560 static inline int64_t const_func signed_bits(int64_t value, int bits)
562 if (bits < 64) {
563 value &= ((int64_t)1 << bits) - 1;
564 if (value & (int64_t)1 << (bits - 1))
565 value |= (int64_t)((uint64_t)-1 << bits);
567 return value;
570 int const_func idata_bytes(int opcode);
572 /* check if value is power of 2 */
573 #define is_power2(v) ((v) && ((v) & ((v) - 1)) == 0)
576 * floor(log2(v))
578 int const_func ilog2_32(uint32_t v);
579 int const_func ilog2_64(uint64_t v);
582 * v == 0 ? 0 : is_power2(x) ? ilog2_X(v) : -1
584 int const_func alignlog2_32(uint32_t v);
585 int const_func alignlog2_64(uint64_t v);
587 #endif