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
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 * Wrappers around malloc, realloc and free. nasm_malloc will
68 * fatal-error and die rather than return NULL; nasm_realloc will
69 * do likewise, and will also guarantee to work right on being
70 * passed a NULL pointer; nasm_free will do nothing if it is passed
73 void * safe_malloc(1) nasm_malloc(size_t);
74 void * safe_malloc(1) nasm_zalloc(size_t);
75 void * safe_malloc2(1,2) nasm_calloc(size_t, size_t);
76 void * safe_realloc(2) nasm_realloc(void *, size_t);
77 void nasm_free(void *);
78 char * safe_alloc
nasm_strdup(const char *);
79 char * safe_alloc
nasm_strndup(const char *, size_t);
81 /* Assert the argument is a pointer without evaluating it */
82 #define nasm_assert_pointer(p) ((void)sizeof(*(p)))
84 #define nasm_new(p) ((p) = nasm_zalloc(sizeof(*(p))))
85 #define nasm_newn(p,n) ((p) = nasm_calloc(sizeof(*(p)),(n)))
87 * This is broken on platforms where there are pointers which don't
88 * match void * in their internal layout. It unfortunately also
89 * loses any "const" part of the argument, although hopefully the
90 * compiler will warn in that case.
92 #define nasm_delete(p) \
94 void **_pp = (void **)&(p); \
95 nasm_assert_pointer(p); \
99 #define nasm_zero(p) (memset((p), 0, sizeof(*(p))))
100 #define nasm_zeron(p,n) (memset((p), 0, (n)*sizeof(*(p))))
103 * Wrapper around fwrite() which fatal-errors on output failure.
105 void nasm_write(const void *, size_t, FILE *);
108 * NASM assert failure
110 no_return
nasm_assert_failed(const char *, int, const char *);
111 #define nasm_assert(x) \
113 if (unlikely(!(x))) \
114 nasm_assert_failed(__FILE__,__LINE__,#x); \
118 * NASM failure at build time if the argument is false
121 # define nasm_static_assert(x) static_assert(x, #x)
122 #elif defined(HAVE_FUNC_ATTRIBUTE_ERROR) && defined(__OPTIMIZE__)
123 # define nasm_static_assert(x) \
125 extern void __attribute__((error("assertion " #x " failed"))) \
126 _nasm_static_fail(void); \
127 _nasm_static_fail(); \
130 /* See http://www.drdobbs.com/compile-time-assertions/184401873 */
131 # define nasm_static_assert(x) \
132 do { enum { _static_assert_failed = 1/(!!(x)) }; } while (0)
136 * ANSI doesn't guarantee the presence of `stricmp' or
139 #if defined(HAVE_STRCASECMP)
140 #define nasm_stricmp strcasecmp
141 #elif defined(HAVE_STRICMP)
142 #define nasm_stricmp stricmp
144 int pure_func
nasm_stricmp(const char *, const char *);
147 #if defined(HAVE_STRNCASECMP)
148 #define nasm_strnicmp strncasecmp
149 #elif defined(HAVE_STRNICMP)
150 #define nasm_strnicmp strnicmp
152 int pure_func
nasm_strnicmp(const char *, const char *, size_t);
155 int pure_func
nasm_memicmp(const char *, const char *, size_t);
157 #if defined(HAVE_STRSEP)
158 #define nasm_strsep strsep
160 char *nasm_strsep(char **stringp
, const char *delim
);
163 #ifndef HAVE_DECL_STRNLEN
164 size_t pure_func
strnlen(const char *, size_t);
167 /* This returns the numeric value of a given 'digit'. */
168 #define numvalue(c) ((c) >= 'a' ? (c) - 'a' + 10 : (c) >= 'A' ? (c) - 'A' + 10 : (c) - '0')
171 * Convert a string into a number, using NASM number rules. Sets
172 * `*error' to true if an error occurs, and false otherwise.
174 int64_t readnum(char *str
, bool *error
);
177 * Convert a character constant into a number. Sets
178 * `*warn' to true if an overflow occurs, and false otherwise.
179 * str points to and length covers the middle of the string,
180 * without the quotes.
182 int64_t readstrnum(char *str
, int length
, bool *warn
);
185 * seg_init: Initialise the segment-number allocator.
186 * seg_alloc: allocate a hitherto unused segment number.
188 void pure_func
seg_init(void);
189 int32_t pure_func
seg_alloc(void);
192 * many output formats will be able to make use of this: a standard
193 * function to add an extension to the name of the input file
195 void standard_extension(char *inname
, char *outname
, char *extension
);
200 * This is a useful #define which I keep meaning to use more often:
201 * the number of elements of a statically defined array.
203 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
208 * list_for_each - regular iterator over list
209 * list_for_each_safe - the same but safe against list items removal
210 * list_last - find the last element in a list
212 #define list_for_each(pos, head) \
213 for (pos = head; pos; pos = pos->next)
214 #define list_for_each_safe(pos, n, head) \
215 for (pos = head, n = (pos ? pos->next : NULL); pos; \
216 pos = n, n = (n ? n->next : NULL))
217 #define list_last(pos, head) \
218 for (pos = head; pos && pos->next; pos = pos->next) \
220 #define list_reverse(head, prev, next) \
222 if (!head || !head->next) \
235 * Power of 2 align helpers
237 #undef ALIGN_MASK /* Some BSD flavors define these in system headers */
239 #define ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask))
240 #define ALIGN(v, a) ALIGN_MASK(v, (a) - 1)
241 #define IS_ALIGNED(v, a) (((v) & ((a) - 1)) == 0)
244 * some handy macros that will probably be of use in more than one
245 * output format: convert integers into little-endian byte packed
251 #define WRITECHAR(p,v) \
253 *(uint8_t *)(p) = (v); \
257 #define WRITESHORT(p,v) \
259 *(uint16_t *)(p) = (v); \
263 #define WRITELONG(p,v) \
265 *(uint32_t *)(p) = (v); \
269 #define WRITEDLONG(p,v) \
271 *(uint64_t *)(p) = (v); \
275 #define WRITEADDR(p,v,s) \
277 uint64_t _wa_v = (v); \
278 memcpy((p), &_wa_v, (s)); \
282 #else /* !X86_MEMORY */
284 #define WRITECHAR(p,v) \
286 uint8_t *_wc_p = (uint8_t *)(p); \
287 uint8_t _wc_v = (v); \
289 (p) = (void *)(_wc_p + 1); \
292 #define WRITESHORT(p,v) \
294 uint8_t *_ws_p = (uint8_t *)(p); \
295 uint16_t _ws_v = (v); \
297 _ws_p[1] = _ws_v >> 8; \
298 (p) = (void *)(_ws_p + 2); \
301 #define WRITELONG(p,v) \
303 uint8_t *_wl_p = (uint8_t *)(p); \
304 uint32_t _wl_v = (v); \
306 _wl_p[1] = _wl_v >> 8; \
307 _wl_p[2] = _wl_v >> 16; \
308 _wl_p[3] = _wl_v >> 24; \
309 (p) = (void *)(_wl_p + 4); \
312 #define WRITEDLONG(p,v) \
314 uint8_t *_wq_p = (uint8_t *)(p); \
315 uint64_t _wq_v = (v); \
317 _wq_p[1] = _wq_v >> 8; \
318 _wq_p[2] = _wq_v >> 16; \
319 _wq_p[3] = _wq_v >> 24; \
320 _wq_p[4] = _wq_v >> 32; \
321 _wq_p[5] = _wq_v >> 40; \
322 _wq_p[6] = _wq_v >> 48; \
323 _wq_p[7] = _wq_v >> 56; \
324 (p) = (void *)(_wq_p + 8); \
327 #define WRITEADDR(p,v,s) \
330 uint64_t _wa_v = (v); \
332 WRITECHAR(p,_wa_v); \
340 * and routines to do the same thing to a file
342 #define fwriteint8_t(d,f) putc(d,f)
343 void fwriteint16_t(uint16_t data
, FILE * fp
);
344 void fwriteint32_t(uint32_t data
, FILE * fp
);
345 void fwriteint64_t(uint64_t data
, FILE * fp
);
346 void fwriteaddr(uint64_t data
, int size
, FILE * fp
);
349 * Binary search routine. Returns index into `array' of an entry
350 * matching `string', or <0 if no match. `array' is taken to
351 * contain `size' elements.
353 * bsi() is case sensitive, bsii() is case insensitive.
355 int bsi(const char *string
, const char **array
, int size
);
356 int bsii(const char *string
, const char **array
, int size
);
359 * These functions are used to keep track of the source code file and name.
363 const char *src_set_fname(const char *newname
);
364 const char *src_get_fname(void);
365 int32_t src_set_linnum(int32_t newline
);
366 int32_t src_get_linnum(void);
367 /* Can be used when there is no need for the old information */
368 void src_set(int32_t line
, const char *filename
);
370 * src_get gets both the source file name and line.
371 * It is also used if you maintain private status about the source location
372 * It return 0 if the information was the same as the last time you
373 * checked, -2 if the name changed and (new-old) if just the line changed.
375 int32_t src_get(int32_t *xline
, const char **xname
);
377 char *nasm_strcat(const char *one
, const char *two
);
379 char *nasm_skip_spaces(const char *p
);
380 char *nasm_skip_word(const char *p
);
381 char *nasm_zap_spaces_fwd(char *p
);
382 char *nasm_zap_spaces_rev(char *p
);
383 char *nasm_trim_spaces(char *p
);
384 char *nasm_get_word(char *p
, char **tail
);
385 char *nasm_opt_val(char *p
, char **opt
, char **val
);
388 * Converts a relative pathname rel_path into an absolute path name.
390 * The buffer returned must be freed by the caller
392 char *nasm_realpath(const char *rel_path
);
394 const char * pure_func
prefix_name(int);
397 * Wrappers around fopen()... for future change to a dedicated structure
400 NF_BINARY
= 0x00000000, /* Binary file (default) */
401 NF_TEXT
= 0x00000001, /* Text file */
402 NF_NONFATAL
= 0x00000000, /* Don't die on open failure (default) */
403 NF_FATAL
= 0x00000002, /* Die on open failure */
404 NF_FORMAP
= 0x00000004 /* Intended to use nasm_map_file() */
407 FILE *nasm_open_read(const char *filename
, enum file_flags flags
);
408 FILE *nasm_open_write(const char *filename
, enum file_flags flags
);
410 /* Probe for existence of a file */
411 bool nasm_file_exists(const char *filename
);
413 #define ZERO_BUF_SIZE 65536 /* Default value */
414 #if defined(BUFSIZ) && (BUFSIZ > ZERO_BUF_SIZE)
415 # undef ZERO_BUF_SIZE
416 # define ZERO_BUF_SIZE BUFSIZ
418 extern const uint8_t zero_buffer
[ZERO_BUF_SIZE
];
420 /* Missing fseeko/ftello */
422 # undef off_t /* Just in case it is a macro */
423 # ifdef HAVE__FSEEKI64
424 # define fseeko _fseeki64
425 # define ftello _ftelli64
426 # define off_t int64_t
428 # define fseeko fseek
429 # define ftello ftell
434 const void *nasm_map_file(FILE *fp
, off_t start
, off_t len
);
435 void nasm_unmap_file(const void *p
, size_t len
);
436 off_t
nasm_file_size(FILE *f
);
437 off_t
nasm_file_size_by_path(const char *pathname
);
438 void fwritezero(off_t bytes
, FILE *fp
);
440 static inline bool const_func
overflow_general(int64_t value
, int bytes
)
448 sbit
= (bytes
<< 3) - 1;
449 vmax
= ((int64_t)2 << sbit
) - 1;
450 vmin
= -((int64_t)1 << sbit
);
452 return value
< vmin
|| value
> vmax
;
455 static inline bool const_func
overflow_signed(int64_t value
, int bytes
)
463 sbit
= (bytes
<< 3) - 1;
464 vmax
= ((int64_t)1 << sbit
) - 1;
465 vmin
= -((int64_t)1 << sbit
);
467 return value
< vmin
|| value
> vmax
;
470 static inline bool const_func
overflow_unsigned(int64_t value
, int bytes
)
478 sbit
= (bytes
<< 3) - 1;
479 vmax
= ((int64_t)2 << sbit
) - 1;
482 return value
< vmin
|| value
> vmax
;
485 static inline int64_t const_func
signed_bits(int64_t value
, int bits
)
488 value
&= ((int64_t)1 << bits
) - 1;
489 if (value
& (int64_t)1 << (bits
- 1))
490 value
|= (int64_t)((uint64_t)-1 << bits
);
495 int const_func
idata_bytes(int opcode
);
497 /* check if value is power of 2 */
498 #define is_power2(v) ((v) && ((v) & ((v) - 1)) == 0)
503 int const_func
ilog2_32(uint32_t v
);
504 int const_func
ilog2_64(uint64_t v
);
507 * v == 0 ? 0 : is_power2(x) ? ilog2_X(v) : -1
509 int const_func
alignlog2_32(uint32_t v
);
510 int const_func
alignlog2_64(uint64_t v
);