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 * Wrapper around fwrite() which fatal-errors on output failure.
106 void nasm_write(const void *, size_t, FILE *);
109 * NASM assert failure
111 no_return
nasm_assert_failed(const char *, int, const char *);
112 #define nasm_assert(x) \
114 if (unlikely(!(x))) \
115 nasm_assert_failed(__FILE__,__LINE__,#x); \
119 * NASM failure at build time if the argument is false
122 # define nasm_static_assert(x) static_assert(x, #x)
123 #elif defined(HAVE_FUNC_ATTRIBUTE_ERROR) && defined(__OPTIMIZE__)
124 # define nasm_static_assert(x) \
126 extern void __attribute__((error("assertion " #x " failed"))) \
127 _nasm_static_fail(void); \
128 _nasm_static_fail(); \
131 /* See http://www.drdobbs.com/compile-time-assertions/184401873 */
132 # define nasm_static_assert(x) \
133 do { enum { _static_assert_failed = 1/(!!(x)) }; } while (0)
136 /* Utility function to generate a string for an invalid enum */
137 const char *invalid_enum_str(int);
140 * ANSI doesn't guarantee the presence of `stricmp' or
143 #if defined(HAVE_STRCASECMP)
144 #define nasm_stricmp strcasecmp
145 #elif defined(HAVE_STRICMP)
146 #define nasm_stricmp stricmp
148 int pure_func
nasm_stricmp(const char *, const char *);
151 #if defined(HAVE_STRNCASECMP)
152 #define nasm_strnicmp strncasecmp
153 #elif defined(HAVE_STRNICMP)
154 #define nasm_strnicmp strnicmp
156 int pure_func
nasm_strnicmp(const char *, const char *, size_t);
159 int pure_func
nasm_memicmp(const char *, const char *, size_t);
161 #if defined(HAVE_STRSEP)
162 #define nasm_strsep strsep
164 char *nasm_strsep(char **stringp
, const char *delim
);
167 #ifndef HAVE_DECL_STRNLEN
168 size_t pure_func
strnlen(const char *, size_t);
171 /* This returns the numeric value of a given 'digit'. */
172 #define numvalue(c) ((c) >= 'a' ? (c) - 'a' + 10 : (c) >= 'A' ? (c) - 'A' + 10 : (c) - '0')
175 * Convert a string into a number, using NASM number rules. Sets
176 * `*error' to true if an error occurs, and false otherwise.
178 int64_t readnum(char *str
, bool *error
);
181 * Convert a character constant into a number. Sets
182 * `*warn' to true if an overflow occurs, and false otherwise.
183 * str points to and length covers the middle of the string,
184 * without the quotes.
186 int64_t readstrnum(char *str
, int length
, bool *warn
);
189 * seg_init: Initialise the segment-number allocator.
190 * seg_alloc: allocate a hitherto unused segment number.
192 void pure_func
seg_init(void);
193 int32_t pure_func
seg_alloc(void);
196 * many output formats will be able to make use of this: a standard
197 * function to add an extension to the name of the input file
199 void standard_extension(char *inname
, char *outname
, char *extension
);
204 * This is a useful #define which I keep meaning to use more often:
205 * the number of elements of a statically defined array.
207 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
212 * list_for_each - regular iterator over list
213 * list_for_each_safe - the same but safe against list items removal
214 * list_last - find the last element in a list
216 #define list_for_each(pos, head) \
217 for (pos = head; pos; pos = pos->next)
218 #define list_for_each_safe(pos, n, head) \
219 for (pos = head, n = (pos ? pos->next : NULL); pos; \
220 pos = n, n = (n ? n->next : NULL))
221 #define list_last(pos, head) \
222 for (pos = head; pos && pos->next; pos = pos->next) \
224 #define list_reverse(head, prev, next) \
226 if (!head || !head->next) \
239 * Power of 2 align helpers
241 #undef ALIGN_MASK /* Some BSD flavors define these in system headers */
243 #define ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask))
244 #define ALIGN(v, a) ALIGN_MASK(v, (a) - 1)
245 #define IS_ALIGNED(v, a) (((v) & ((a) - 1)) == 0)
248 * some handy macros that will probably be of use in more than one
249 * output format: convert integers into little-endian byte packed
255 #define WRITECHAR(p,v) \
257 *(uint8_t *)(p) = (v); \
261 #define WRITESHORT(p,v) \
263 *(uint16_t *)(p) = (v); \
267 #define WRITELONG(p,v) \
269 *(uint32_t *)(p) = (v); \
273 #define WRITEDLONG(p,v) \
275 *(uint64_t *)(p) = (v); \
279 #define WRITEADDR(p,v,s) \
281 uint64_t _wa_v = (v); \
282 memcpy((p), &_wa_v, (s)); \
286 #else /* !X86_MEMORY */
288 #define WRITECHAR(p,v) \
290 uint8_t *_wc_p = (uint8_t *)(p); \
291 uint8_t _wc_v = (v); \
293 (p) = (void *)(_wc_p + 1); \
296 #define WRITESHORT(p,v) \
298 uint8_t *_ws_p = (uint8_t *)(p); \
299 uint16_t _ws_v = (v); \
301 _ws_p[1] = _ws_v >> 8; \
302 (p) = (void *)(_ws_p + 2); \
305 #define WRITELONG(p,v) \
307 uint8_t *_wl_p = (uint8_t *)(p); \
308 uint32_t _wl_v = (v); \
310 _wl_p[1] = _wl_v >> 8; \
311 _wl_p[2] = _wl_v >> 16; \
312 _wl_p[3] = _wl_v >> 24; \
313 (p) = (void *)(_wl_p + 4); \
316 #define WRITEDLONG(p,v) \
318 uint8_t *_wq_p = (uint8_t *)(p); \
319 uint64_t _wq_v = (v); \
321 _wq_p[1] = _wq_v >> 8; \
322 _wq_p[2] = _wq_v >> 16; \
323 _wq_p[3] = _wq_v >> 24; \
324 _wq_p[4] = _wq_v >> 32; \
325 _wq_p[5] = _wq_v >> 40; \
326 _wq_p[6] = _wq_v >> 48; \
327 _wq_p[7] = _wq_v >> 56; \
328 (p) = (void *)(_wq_p + 8); \
331 #define WRITEADDR(p,v,s) \
334 uint64_t _wa_v = (v); \
336 WRITECHAR(p,_wa_v); \
344 * and routines to do the same thing to a file
346 #define fwriteint8_t(d,f) putc(d,f)
347 void fwriteint16_t(uint16_t data
, FILE * fp
);
348 void fwriteint32_t(uint32_t data
, FILE * fp
);
349 void fwriteint64_t(uint64_t data
, FILE * fp
);
350 void fwriteaddr(uint64_t data
, int size
, FILE * fp
);
353 * Binary search routine. Returns index into `array' of an entry
354 * matching `string', or <0 if no match. `array' is taken to
355 * contain `size' elements.
357 * bsi() is case sensitive, bsii() is case insensitive.
359 int bsi(const char *string
, const char **array
, int size
);
360 int bsii(const char *string
, const char **array
, int size
);
363 * These functions are used to keep track of the source code file and name.
367 const char *src_set_fname(const char *newname
);
368 const char *src_get_fname(void);
369 int32_t src_set_linnum(int32_t newline
);
370 int32_t src_get_linnum(void);
371 /* Can be used when there is no need for the old information */
372 void src_set(int32_t line
, const char *filename
);
374 * src_get gets both the source file name and line.
375 * It is also used if you maintain private status about the source location
376 * It return 0 if the information was the same as the last time you
377 * checked, -2 if the name changed and (new-old) if just the line changed.
379 int32_t src_get(int32_t *xline
, const char **xname
);
381 char *nasm_strcat(const char *one
, const char *two
);
383 char *nasm_skip_spaces(const char *p
);
384 char *nasm_skip_word(const char *p
);
385 char *nasm_zap_spaces_fwd(char *p
);
386 char *nasm_zap_spaces_rev(char *p
);
387 char *nasm_trim_spaces(char *p
);
388 char *nasm_get_word(char *p
, char **tail
);
389 char *nasm_opt_val(char *p
, char **opt
, char **val
);
392 * Converts a relative pathname rel_path into an absolute path name.
394 * The buffer returned must be freed by the caller
396 char *nasm_realpath(const char *rel_path
);
398 const char * pure_func
prefix_name(int);
401 * Wrappers around fopen()... for future change to a dedicated structure
404 NF_BINARY
= 0x00000000, /* Binary file (default) */
405 NF_TEXT
= 0x00000001, /* Text file */
406 NF_NONFATAL
= 0x00000000, /* Don't die on open failure (default) */
407 NF_FATAL
= 0x00000002, /* Die on open failure */
408 NF_FORMAP
= 0x00000004 /* Intended to use nasm_map_file() */
411 FILE *nasm_open_read(const char *filename
, enum file_flags flags
);
412 FILE *nasm_open_write(const char *filename
, enum file_flags flags
);
414 /* Probe for existence of a file */
415 bool nasm_file_exists(const char *filename
);
417 #define ZERO_BUF_SIZE 65536 /* Default value */
418 #if defined(BUFSIZ) && (BUFSIZ > ZERO_BUF_SIZE)
419 # undef ZERO_BUF_SIZE
420 # define ZERO_BUF_SIZE BUFSIZ
422 extern const uint8_t zero_buffer
[ZERO_BUF_SIZE
];
424 /* Missing fseeko/ftello */
426 # undef off_t /* Just in case it is a macro */
427 # ifdef HAVE__FSEEKI64
428 # define fseeko _fseeki64
429 # define ftello _ftelli64
430 # define off_t int64_t
432 # define fseeko fseek
433 # define ftello ftell
438 const void *nasm_map_file(FILE *fp
, off_t start
, off_t len
);
439 void nasm_unmap_file(const void *p
, size_t len
);
440 off_t
nasm_file_size(FILE *f
);
441 off_t
nasm_file_size_by_path(const char *pathname
);
442 void fwritezero(off_t bytes
, FILE *fp
);
444 static inline bool const_func
overflow_general(int64_t value
, int bytes
)
452 sbit
= (bytes
<< 3) - 1;
453 vmax
= ((int64_t)2 << sbit
) - 1;
454 vmin
= -((int64_t)1 << sbit
);
456 return value
< vmin
|| value
> vmax
;
459 static inline bool const_func
overflow_signed(int64_t value
, int bytes
)
467 sbit
= (bytes
<< 3) - 1;
468 vmax
= ((int64_t)1 << sbit
) - 1;
469 vmin
= -((int64_t)1 << sbit
);
471 return value
< vmin
|| value
> vmax
;
474 static inline bool const_func
overflow_unsigned(int64_t value
, int bytes
)
482 sbit
= (bytes
<< 3) - 1;
483 vmax
= ((int64_t)2 << sbit
) - 1;
486 return value
< vmin
|| value
> vmax
;
489 static inline int64_t const_func
signed_bits(int64_t value
, int bits
)
492 value
&= ((int64_t)1 << bits
) - 1;
493 if (value
& (int64_t)1 << (bits
- 1))
494 value
|= (int64_t)((uint64_t)-1 << bits
);
499 int const_func
idata_bytes(int opcode
);
501 /* check if value is power of 2 */
502 #define is_power2(v) ((v) && ((v) & ((v) - 1)) == 0)
507 int const_func
ilog2_32(uint32_t v
);
508 int const_func
ilog2_64(uint64_t v
);
511 * v == 0 ? 0 : is_power2(x) ? ilog2_X(v) : -1
513 int const_func
alignlog2_32(uint32_t v
);
514 int const_func
alignlog2_64(uint64_t v
);