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
52 * tolower table -- avoids a function call on some platforms.
53 * NOTE: unlike the tolower() function in ctype, EOF is *NOT*
54 * a permitted value, for obvious reasons.
56 void tolower_init(void);
57 extern unsigned char nasm_tolower_tab
[256];
58 #define nasm_tolower(x) nasm_tolower_tab[(unsigned char)(x)]
60 /* Wrappers around <ctype.h> functions */
61 /* These are only valid for values that cannot include EOF */
62 #define nasm_isspace(x) isspace((unsigned char)(x))
63 #define nasm_isalpha(x) isalpha((unsigned char)(x))
64 #define nasm_isdigit(x) isdigit((unsigned char)(x))
65 #define nasm_isalnum(x) isalnum((unsigned char)(x))
66 #define nasm_isxdigit(x) isxdigit((unsigned char)(x))
69 * Wrappers around malloc, realloc and free. nasm_malloc will
70 * fatal-error and die rather than return NULL; nasm_realloc will
71 * do likewise, and will also guarantee to work right on being
72 * passed a NULL pointer; nasm_free will do nothing if it is passed
75 void * safe_malloc(1) nasm_malloc(size_t);
76 void * safe_malloc(1) nasm_zalloc(size_t);
77 void * safe_malloc2(1,2) nasm_calloc(size_t, size_t);
78 void * safe_realloc(2) nasm_realloc(void *, size_t);
79 void nasm_free(void *);
80 char * safe_alloc
nasm_strdup(const char *);
81 char * safe_alloc
nasm_strndup(const char *, size_t);
83 /* Assert the argument is a pointer without evaluating it */
84 #define nasm_assert_pointer(p) ((void)sizeof(*(p)))
86 #define nasm_new(p) ((p) = nasm_zalloc(sizeof(*(p))))
87 #define nasm_newn(p,n) ((p) = nasm_calloc(sizeof(*(p)),(n)))
89 * This is broken on platforms where there are pointers which don't
90 * match void * in their internal layout. It unfortunately also
91 * loses any "const" part of the argument, although hopefully the
92 * compiler will warn in that case.
94 #define nasm_delete(p) \
96 void **_pp = (void **)&(p); \
97 nasm_assert_pointer(p); \
101 #define nasm_zero(x) (memset(&(x), 0, sizeof(x)))
102 #define nasm_zeron(p,n) (memset((p), 0, (n)*sizeof(*(p))))
105 * Wrappers around fread()/fwrite() which fatal-errors on failure.
106 * For fread(), only use this if EOF is supposed to be a fatal error!
108 void nasm_read(void *, size_t, FILE *);
109 void nasm_write(const void *, size_t, FILE *);
112 * NASM assert failure
114 no_return
nasm_assert_failed(const char *, int, const char *);
115 #define nasm_assert(x) \
117 if (unlikely(!(x))) \
118 nasm_assert_failed(__FILE__,__LINE__,#x); \
122 * NASM failure at build time if the argument is false
125 # define nasm_static_assert(x) static_assert(x, #x)
126 #elif defined(HAVE_FUNC_ATTRIBUTE_ERROR) && defined(__OPTIMIZE__)
127 # define nasm_static_assert(x) \
129 extern void __attribute__((error("assertion " #x " failed"))) \
130 _nasm_static_fail(void); \
131 _nasm_static_fail(); \
134 /* See http://www.drdobbs.com/compile-time-assertions/184401873 */
135 # define nasm_static_assert(x) \
136 do { enum { _static_assert_failed = 1/(!!(x)) }; } while (0)
139 /* Utility function to generate a string for an invalid enum */
140 const char *invalid_enum_str(int);
143 * ANSI doesn't guarantee the presence of `stricmp' or
146 #if defined(HAVE_STRCASECMP)
147 #define nasm_stricmp strcasecmp
148 #elif defined(HAVE_STRICMP)
149 #define nasm_stricmp stricmp
151 int pure_func
nasm_stricmp(const char *, const char *);
154 #if defined(HAVE_STRNCASECMP)
155 #define nasm_strnicmp strncasecmp
156 #elif defined(HAVE_STRNICMP)
157 #define nasm_strnicmp strnicmp
159 int pure_func
nasm_strnicmp(const char *, const char *, size_t);
162 int pure_func
nasm_memicmp(const char *, const char *, size_t);
164 #if defined(HAVE_STRSEP)
165 #define nasm_strsep strsep
167 char *nasm_strsep(char **stringp
, const char *delim
);
170 #ifndef HAVE_DECL_STRNLEN
171 size_t pure_func
strnlen(const char *, size_t);
174 /* This returns the numeric value of a given 'digit'. */
175 #define numvalue(c) ((c) >= 'a' ? (c) - 'a' + 10 : (c) >= 'A' ? (c) - 'A' + 10 : (c) - '0')
178 * Convert a string into a number, using NASM number rules. Sets
179 * `*error' to true if an error occurs, and false otherwise.
181 int64_t readnum(char *str
, bool *error
);
184 * Convert a character constant into a number. Sets
185 * `*warn' to true if an overflow occurs, and false otherwise.
186 * str points to and length covers the middle of the string,
187 * without the quotes.
189 int64_t readstrnum(char *str
, int length
, bool *warn
);
192 * seg_init: Initialise the segment-number allocator.
193 * seg_alloc: allocate a hitherto unused segment number.
195 void pure_func
seg_init(void);
196 int32_t pure_func
seg_alloc(void);
199 * many output formats will be able to make use of this: a standard
200 * function to add an extension to the name of the input file
202 void standard_extension(char *inname
, char *outname
, char *extension
);
207 * This is a useful #define which I keep meaning to use more often:
208 * the number of elements of a statically defined array.
210 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
215 * list_for_each - regular iterator over list
216 * list_for_each_safe - the same but safe against list items removal
217 * list_last - find the last element in a list
219 #define list_for_each(pos, head) \
220 for (pos = head; pos; pos = pos->next)
221 #define list_for_each_safe(pos, n, head) \
222 for (pos = head, n = (pos ? pos->next : NULL); pos; \
223 pos = n, n = (n ? n->next : NULL))
224 #define list_last(pos, head) \
225 for (pos = head; pos && pos->next; pos = pos->next) \
227 #define list_reverse(head, prev, next) \
229 if (!head || !head->next) \
242 * Power of 2 align helpers
244 #undef ALIGN_MASK /* Some BSD flavors define these in system headers */
246 #define ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask))
247 #define ALIGN(v, a) ALIGN_MASK(v, (a) - 1)
248 #define IS_ALIGNED(v, a) (((v) & ((a) - 1)) == 0)
251 * Routines to write littleendian data to a file
253 #define fwriteint8_t(d,f) putc(d,f)
254 void fwriteint16_t(uint16_t data
, FILE * fp
);
255 void fwriteint32_t(uint32_t data
, FILE * fp
);
256 void fwriteint64_t(uint64_t data
, FILE * fp
);
257 void fwriteaddr(uint64_t data
, int size
, FILE * fp
);
260 * Binary search routine. Returns index into `array' of an entry
261 * matching `string', or <0 if no match. `array' is taken to
262 * contain `size' elements.
264 * bsi() is case sensitive, bsii() is case insensitive.
266 int bsi(const char *string
, const char **array
, int size
);
267 int bsii(const char *string
, const char **array
, int size
);
270 * These functions are used to keep track of the source code file and name.
274 const char *src_set_fname(const char *newname
);
275 const char *src_get_fname(void);
276 int32_t src_set_linnum(int32_t newline
);
277 int32_t src_get_linnum(void);
278 /* Can be used when there is no need for the old information */
279 void src_set(int32_t line
, const char *filename
);
281 * src_get gets both the source file name and line.
282 * It is also used if you maintain private status about the source location
283 * It return 0 if the information was the same as the last time you
284 * checked, -2 if the name changed and (new-old) if just the line changed.
286 int32_t src_get(int32_t *xline
, const char **xname
);
288 char *nasm_strcat(const char *one
, const char *two
);
290 char *nasm_skip_spaces(const char *p
);
291 char *nasm_skip_word(const char *p
);
292 char *nasm_zap_spaces_fwd(char *p
);
293 char *nasm_zap_spaces_rev(char *p
);
294 char *nasm_trim_spaces(char *p
);
295 char *nasm_get_word(char *p
, char **tail
);
296 char *nasm_opt_val(char *p
, char **opt
, char **val
);
299 * Converts a relative pathname rel_path into an absolute path name.
301 * The buffer returned must be freed by the caller
303 char * safe_alloc
nasm_realpath(const char *rel_path
);
306 * Path-splitting and merging functions
308 char * safe_alloc
nasm_dirname(const char *path
);
309 char * safe_alloc
nasm_basename(const char *path
);
310 char * safe_alloc
nasm_catfile(const char *dir
, const char *path
);
312 const char * pure_func
prefix_name(int);
315 * Wrappers around fopen()... for future change to a dedicated structure
318 NF_BINARY
= 0x00000000, /* Binary file (default) */
319 NF_TEXT
= 0x00000001, /* Text file */
320 NF_NONFATAL
= 0x00000000, /* Don't die on open failure (default) */
321 NF_FATAL
= 0x00000002, /* Die on open failure */
322 NF_FORMAP
= 0x00000004 /* Intended to use nasm_map_file() */
325 FILE *nasm_open_read(const char *filename
, enum file_flags flags
);
326 FILE *nasm_open_write(const char *filename
, enum file_flags flags
);
328 /* Probe for existence of a file */
329 bool nasm_file_exists(const char *filename
);
331 #define ZERO_BUF_SIZE 65536 /* Default value */
332 #if defined(BUFSIZ) && (BUFSIZ > ZERO_BUF_SIZE)
333 # undef ZERO_BUF_SIZE
334 # define ZERO_BUF_SIZE BUFSIZ
336 extern const uint8_t zero_buffer
[ZERO_BUF_SIZE
];
338 /* Missing fseeko/ftello */
340 # undef off_t /* Just in case it is a macro */
341 # ifdef HAVE__FSEEKI64
342 # define fseeko _fseeki64
343 # define ftello _ftelli64
344 # define off_t int64_t
346 # define fseeko fseek
347 # define ftello ftell
352 const void *nasm_map_file(FILE *fp
, off_t start
, off_t len
);
353 void nasm_unmap_file(const void *p
, size_t len
);
354 off_t
nasm_file_size(FILE *f
);
355 off_t
nasm_file_size_by_path(const char *pathname
);
356 bool nasm_file_time(time_t *t
, const char *pathname
);
357 void fwritezero(off_t bytes
, FILE *fp
);
359 static inline bool const_func
overflow_general(int64_t value
, int bytes
)
367 sbit
= (bytes
<< 3) - 1;
368 vmax
= ((int64_t)2 << sbit
) - 1;
369 vmin
= -((int64_t)1 << sbit
);
371 return value
< vmin
|| value
> vmax
;
374 static inline bool const_func
overflow_signed(int64_t value
, int bytes
)
382 sbit
= (bytes
<< 3) - 1;
383 vmax
= ((int64_t)1 << sbit
) - 1;
384 vmin
= -((int64_t)1 << sbit
);
386 return value
< vmin
|| value
> vmax
;
389 static inline bool const_func
overflow_unsigned(int64_t value
, int bytes
)
397 sbit
= (bytes
<< 3) - 1;
398 vmax
= ((int64_t)2 << sbit
) - 1;
401 return value
< vmin
|| value
> vmax
;
404 static inline int64_t const_func
signed_bits(int64_t value
, int bits
)
407 value
&= ((int64_t)1 << bits
) - 1;
408 if (value
& (int64_t)1 << (bits
- 1))
409 value
|= (int64_t)((uint64_t)-1 << bits
);
414 /* check if value is power of 2 */
415 #define is_power2(v) ((v) && ((v) & ((v) - 1)) == 0)
420 int const_func
ilog2_32(uint32_t v
);
421 int const_func
ilog2_64(uint64_t v
);
424 * v == 0 ? 0 : is_power2(x) ? ilog2_X(v) : -1
426 int const_func
alignlog2_32(uint32_t v
);
427 int const_func
alignlog2_64(uint64_t v
);