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
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
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 * Wrappers around malloc, realloc and free. nasm_malloc will
69 * fatal-error and die rather than return NULL; nasm_realloc will
70 * do likewise, and will also guarantee to work right on being
71 * passed a NULL pointer; nasm_free will do nothing if it is passed
74 void * safe_malloc(1) nasm_malloc(size_t);
75 void * safe_malloc(1) nasm_zalloc(size_t);
76 void * safe_malloc2(1,2) nasm_calloc(size_t, size_t);
77 void * safe_realloc(2) nasm_realloc(void *, size_t);
78 void nasm_free(void *);
79 char * safe_alloc
nasm_strdup(const char *);
80 char * safe_alloc
nasm_strndup(const char *, size_t);
82 /* Assert the argument is a pointer without evaluating it */
83 #define nasm_assert_pointer(p) ((void)sizeof(*(p)))
85 #define nasm_new(p) ((p) = nasm_zalloc(sizeof(*(p))))
86 #define nasm_newn(p,n) ((p) = nasm_calloc(sizeof(*(p)),(n)))
88 * This is broken on platforms where there are pointers which don't
89 * match void * in their internal layout. It unfortunately also
90 * loses any "const" part of the argument, although hopefully the
91 * compiler will warn in that case.
93 #define nasm_delete(p) \
95 void **_pp = (void **)&(p); \
96 nasm_assert_pointer(p); \
100 #define nasm_zero(x) (memset(&(x), 0, sizeof(x)))
101 #define nasm_zeron(p,n) (memset((p), 0, (n)*sizeof(*(p))))
104 * Wrappers around fread()/fwrite() which fatal-errors on failure.
105 * For fread(), only use this if EOF is supposed to be a fatal error!
107 void nasm_read(void *, size_t, FILE *);
108 void nasm_write(const void *, size_t, FILE *);
111 * NASM assert failure
113 no_return
nasm_assert_failed(const char *, int, const char *);
114 #define nasm_assert(x) \
116 if (unlikely(!(x))) \
117 nasm_assert_failed(__FILE__,__LINE__,#x); \
121 * NASM failure at build time if the argument is false
124 # define nasm_static_assert(x) static_assert(x, #x)
125 #elif defined(HAVE_FUNC_ATTRIBUTE_ERROR) && defined(__OPTIMIZE__)
126 # define nasm_static_assert(x) \
128 extern void __attribute__((error("assertion " #x " failed"))) \
129 _nasm_static_fail(void); \
130 _nasm_static_fail(); \
133 /* See http://www.drdobbs.com/compile-time-assertions/184401873 */
134 # define nasm_static_assert(x) \
135 do { enum { _static_assert_failed = 1/(!!(x)) }; } while (0)
138 /* Utility function to generate a string for an invalid enum */
139 const char *invalid_enum_str(int);
142 * ANSI doesn't guarantee the presence of `stricmp' or
145 #if defined(HAVE_STRCASECMP)
146 #define nasm_stricmp strcasecmp
147 #elif defined(HAVE_STRICMP)
148 #define nasm_stricmp stricmp
150 int pure_func
nasm_stricmp(const char *, const char *);
153 #if defined(HAVE_STRNCASECMP)
154 #define nasm_strnicmp strncasecmp
155 #elif defined(HAVE_STRNICMP)
156 #define nasm_strnicmp strnicmp
158 int pure_func
nasm_strnicmp(const char *, const char *, size_t);
161 int pure_func
nasm_memicmp(const char *, const char *, size_t);
163 #if defined(HAVE_STRSEP)
164 #define nasm_strsep strsep
166 char *nasm_strsep(char **stringp
, const char *delim
);
169 #ifndef HAVE_DECL_STRNLEN
170 size_t pure_func
strnlen(const char *, size_t);
173 /* This returns the numeric value of a given 'digit'. */
174 #define numvalue(c) ((c) >= 'a' ? (c) - 'a' + 10 : (c) >= 'A' ? (c) - 'A' + 10 : (c) - '0')
177 * Convert a string into a number, using NASM number rules. Sets
178 * `*error' to true if an error occurs, and false otherwise.
180 int64_t readnum(char *str
, bool *error
);
183 * Convert a character constant into a number. Sets
184 * `*warn' to true if an overflow occurs, and false otherwise.
185 * str points to and length covers the middle of the string,
186 * without the quotes.
188 int64_t readstrnum(char *str
, int length
, bool *warn
);
191 * seg_init: Initialise the segment-number allocator.
192 * seg_alloc: allocate a hitherto unused segment number.
194 void pure_func
seg_init(void);
195 int32_t pure_func
seg_alloc(void);
198 * many output formats will be able to make use of this: a standard
199 * function to add an extension to the name of the input file
201 void standard_extension(char *inname
, char *outname
, char *extension
);
206 * This is a useful #define which I keep meaning to use more often:
207 * the number of elements of a statically defined array.
209 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
214 * list_for_each - regular iterator over list
215 * list_for_each_safe - the same but safe against list items removal
216 * list_last - find the last element in a list
218 #define list_for_each(pos, head) \
219 for (pos = head; pos; pos = pos->next)
220 #define list_for_each_safe(pos, n, head) \
221 for (pos = head, n = (pos ? pos->next : NULL); pos; \
222 pos = n, n = (n ? n->next : NULL))
223 #define list_last(pos, head) \
224 for (pos = head; pos && pos->next; pos = pos->next) \
226 #define list_reverse(head, prev, next) \
228 if (!head || !head->next) \
241 * Power of 2 align helpers
243 #undef ALIGN_MASK /* Some BSD flavors define these in system headers */
245 #define ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask))
246 #define ALIGN(v, a) ALIGN_MASK(v, (a) - 1)
247 #define IS_ALIGNED(v, a) (((v) & ((a) - 1)) == 0)
250 * some handy macros that will probably be of use in more than one
251 * output format: convert integers into little-endian byte packed
257 #define WRITECHAR(p,v) \
259 *(uint8_t *)(p) = (v); \
263 #define WRITESHORT(p,v) \
265 *(uint16_t *)(p) = (v); \
269 #define WRITELONG(p,v) \
271 *(uint32_t *)(p) = (v); \
275 #define WRITEDLONG(p,v) \
277 *(uint64_t *)(p) = (v); \
281 #define WRITEADDR(p,v,s) \
283 uint64_t _wa_v = (v); \
284 memcpy((p), &_wa_v, (s)); \
288 #else /* !X86_MEMORY */
290 #define WRITECHAR(p,v) \
292 uint8_t *_wc_p = (uint8_t *)(p); \
293 uint8_t _wc_v = (v); \
295 (p) = (void *)(_wc_p + 1); \
298 #define WRITESHORT(p,v) \
300 uint8_t *_ws_p = (uint8_t *)(p); \
301 uint16_t _ws_v = (v); \
303 _ws_p[1] = _ws_v >> 8; \
304 (p) = (void *)(_ws_p + 2); \
307 #define WRITELONG(p,v) \
309 uint8_t *_wl_p = (uint8_t *)(p); \
310 uint32_t _wl_v = (v); \
312 _wl_p[1] = _wl_v >> 8; \
313 _wl_p[2] = _wl_v >> 16; \
314 _wl_p[3] = _wl_v >> 24; \
315 (p) = (void *)(_wl_p + 4); \
318 #define WRITEDLONG(p,v) \
320 uint8_t *_wq_p = (uint8_t *)(p); \
321 uint64_t _wq_v = (v); \
323 _wq_p[1] = _wq_v >> 8; \
324 _wq_p[2] = _wq_v >> 16; \
325 _wq_p[3] = _wq_v >> 24; \
326 _wq_p[4] = _wq_v >> 32; \
327 _wq_p[5] = _wq_v >> 40; \
328 _wq_p[6] = _wq_v >> 48; \
329 _wq_p[7] = _wq_v >> 56; \
330 (p) = (void *)(_wq_p + 8); \
333 #define WRITEADDR(p,v,s) \
336 uint64_t _wa_v = (v); \
338 WRITECHAR(p,_wa_v); \
346 * and routines to do the same thing to a file
348 #define fwriteint8_t(d,f) putc(d,f)
349 void fwriteint16_t(uint16_t data
, FILE * fp
);
350 void fwriteint32_t(uint32_t data
, FILE * fp
);
351 void fwriteint64_t(uint64_t data
, FILE * fp
);
352 void fwriteaddr(uint64_t data
, int size
, FILE * fp
);
355 * Binary search routine. Returns index into `array' of an entry
356 * matching `string', or <0 if no match. `array' is taken to
357 * contain `size' elements.
359 * bsi() is case sensitive, bsii() is case insensitive.
361 int bsi(const char *string
, const char **array
, int size
);
362 int bsii(const char *string
, const char **array
, int size
);
365 * These functions are used to keep track of the source code file and name.
369 const char *src_set_fname(const char *newname
);
370 const char *src_get_fname(void);
371 int32_t src_set_linnum(int32_t newline
);
372 int32_t src_get_linnum(void);
373 /* Can be used when there is no need for the old information */
374 void src_set(int32_t line
, const char *filename
);
376 * src_get gets both the source file name and line.
377 * It is also used if you maintain private status about the source location
378 * It return 0 if the information was the same as the last time you
379 * checked, -2 if the name changed and (new-old) if just the line changed.
381 int32_t src_get(int32_t *xline
, const char **xname
);
383 char *nasm_strcat(const char *one
, const char *two
);
385 char *nasm_skip_spaces(const char *p
);
386 char *nasm_skip_word(const char *p
);
387 char *nasm_zap_spaces_fwd(char *p
);
388 char *nasm_zap_spaces_rev(char *p
);
389 char *nasm_trim_spaces(char *p
);
390 char *nasm_get_word(char *p
, char **tail
);
391 char *nasm_opt_val(char *p
, char **opt
, char **val
);
394 * Converts a relative pathname rel_path into an absolute path name.
396 * The buffer returned must be freed by the caller
398 char * safe_alloc
nasm_realpath(const char *rel_path
);
401 * Path-splitting and merging functions
403 char * safe_alloc
nasm_dirname(const char *path
);
404 char * safe_alloc
nasm_basename(const char *path
);
405 char * safe_alloc
nasm_catfile(const char *dir
, const char *path
);
407 const char * pure_func
prefix_name(int);
410 * Wrappers around fopen()... for future change to a dedicated structure
413 NF_BINARY
= 0x00000000, /* Binary file (default) */
414 NF_TEXT
= 0x00000001, /* Text file */
415 NF_NONFATAL
= 0x00000000, /* Don't die on open failure (default) */
416 NF_FATAL
= 0x00000002, /* Die on open failure */
417 NF_FORMAP
= 0x00000004 /* Intended to use nasm_map_file() */
420 FILE *nasm_open_read(const char *filename
, enum file_flags flags
);
421 FILE *nasm_open_write(const char *filename
, enum file_flags flags
);
423 /* Probe for existence of a file */
424 bool nasm_file_exists(const char *filename
);
426 #define ZERO_BUF_SIZE 65536 /* Default value */
427 #if defined(BUFSIZ) && (BUFSIZ > ZERO_BUF_SIZE)
428 # undef ZERO_BUF_SIZE
429 # define ZERO_BUF_SIZE BUFSIZ
431 extern const uint8_t zero_buffer
[ZERO_BUF_SIZE
];
433 /* Missing fseeko/ftello */
435 # undef off_t /* Just in case it is a macro */
436 # ifdef HAVE__FSEEKI64
437 # define fseeko _fseeki64
438 # define ftello _ftelli64
439 # define off_t int64_t
441 # define fseeko fseek
442 # define ftello ftell
447 const void *nasm_map_file(FILE *fp
, off_t start
, off_t len
);
448 void nasm_unmap_file(const void *p
, size_t len
);
449 off_t
nasm_file_size(FILE *f
);
450 off_t
nasm_file_size_by_path(const char *pathname
);
451 bool nasm_file_time(time_t *t
, const char *pathname
);
452 void fwritezero(off_t bytes
, FILE *fp
);
454 static inline bool const_func
overflow_general(int64_t value
, int bytes
)
462 sbit
= (bytes
<< 3) - 1;
463 vmax
= ((int64_t)2 << sbit
) - 1;
464 vmin
= -((int64_t)1 << sbit
);
466 return value
< vmin
|| value
> vmax
;
469 static inline bool const_func
overflow_signed(int64_t value
, int bytes
)
477 sbit
= (bytes
<< 3) - 1;
478 vmax
= ((int64_t)1 << sbit
) - 1;
479 vmin
= -((int64_t)1 << sbit
);
481 return value
< vmin
|| value
> vmax
;
484 static inline bool const_func
overflow_unsigned(int64_t value
, int bytes
)
492 sbit
= (bytes
<< 3) - 1;
493 vmax
= ((int64_t)2 << sbit
) - 1;
496 return value
< vmin
|| value
> vmax
;
499 static inline int64_t const_func
signed_bits(int64_t value
, int bits
)
502 value
&= ((int64_t)1 << bits
) - 1;
503 if (value
& (int64_t)1 << (bits
- 1))
504 value
|= (int64_t)((uint64_t)-1 << bits
);
509 /* check if value is power of 2 */
510 #define is_power2(v) ((v) && ((v) & ((v) - 1)) == 0)
515 int const_func
ilog2_32(uint32_t v
);
516 int const_func
ilog2_64(uint64_t v
);
519 * v == 0 ? 0 : is_power2(x) ? ilog2_X(v) : -1
521 int const_func
alignlog2_32(uint32_t v
);
522 int const_func
alignlog2_64(uint64_t v
);