labels, outelf: remove casts for allocations
[nasm.git] / include / nasmlib.h
blob0b2bcb17b84bacc91372513151c139bfd9aa7669
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 <ctype.h>
44 #include <stdio.h>
45 #include <string.h>
46 #ifdef HAVE_STRINGS_H
47 # include <strings.h>
48 #endif
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
72 * a NULL pointer.
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) \
94 do { \
95 void **_pp = (void **)&(p); \
96 nasm_assert_pointer(p); \
97 nasm_free(*_pp); \
98 *_pp = NULL; \
99 } while (0)
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) \
115 do { \
116 if (unlikely(!(x))) \
117 nasm_assert_failed(__FILE__,__LINE__,#x); \
118 } while (0)
121 * NASM failure at build time if the argument is false
123 #ifdef static_assert
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) \
127 if (!(x)) { \
128 extern void __attribute__((error("assertion " #x " failed"))) \
129 _nasm_static_fail(void); \
130 _nasm_static_fail(); \
132 #else
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)
136 #endif
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
143 * `strcasecmp'.
145 #if defined(HAVE_STRCASECMP)
146 #define nasm_stricmp strcasecmp
147 #elif defined(HAVE_STRICMP)
148 #define nasm_stricmp stricmp
149 #else
150 int pure_func nasm_stricmp(const char *, const char *);
151 #endif
153 #if defined(HAVE_STRNCASECMP)
154 #define nasm_strnicmp strncasecmp
155 #elif defined(HAVE_STRNICMP)
156 #define nasm_strnicmp strnicmp
157 #else
158 int pure_func nasm_strnicmp(const char *, const char *, size_t);
159 #endif
161 int pure_func nasm_memicmp(const char *, const char *, size_t);
163 #if defined(HAVE_STRSEP)
164 #define nasm_strsep strsep
165 #else
166 char *nasm_strsep(char **stringp, const char *delim);
167 #endif
169 #ifndef HAVE_DECL_STRNLEN
170 size_t pure_func strnlen(const char *, size_t);
171 #endif
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);
204 * Utility macros...
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]))
212 * List handling
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) \
227 do { \
228 if (!head || !head->next) \
229 break; \
230 prev = NULL; \
231 while (head) { \
232 next = head->next; \
233 head->next = prev; \
234 prev = head; \
235 head = next; \
237 head = prev; \
238 } while (0)
241 * Power of 2 align helpers
243 #undef ALIGN_MASK /* Some BSD flavors define these in system headers */
244 #undef ALIGN
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
252 * format in memory
255 #if X86_MEMORY
257 #define WRITECHAR(p,v) \
258 do { \
259 *(uint8_t *)(p) = (v); \
260 (p) += 1; \
261 } while (0)
263 #define WRITESHORT(p,v) \
264 do { \
265 *(uint16_t *)(p) = (v); \
266 (p) += 2; \
267 } while (0)
269 #define WRITELONG(p,v) \
270 do { \
271 *(uint32_t *)(p) = (v); \
272 (p) += 4; \
273 } while (0)
275 #define WRITEDLONG(p,v) \
276 do { \
277 *(uint64_t *)(p) = (v); \
278 (p) += 8; \
279 } while (0)
281 #define WRITEADDR(p,v,s) \
282 do { \
283 uint64_t _wa_v = (v); \
284 memcpy((p), &_wa_v, (s)); \
285 (p) += (s); \
286 } while (0)
288 #else /* !X86_MEMORY */
290 #define WRITECHAR(p,v) \
291 do { \
292 uint8_t *_wc_p = (uint8_t *)(p); \
293 uint8_t _wc_v = (v); \
294 _wc_p[0] = _wc_v; \
295 (p) = (void *)(_wc_p + 1); \
296 } while (0)
298 #define WRITESHORT(p,v) \
299 do { \
300 uint8_t *_ws_p = (uint8_t *)(p); \
301 uint16_t _ws_v = (v); \
302 _ws_p[0] = _ws_v; \
303 _ws_p[1] = _ws_v >> 8; \
304 (p) = (void *)(_ws_p + 2); \
305 } while (0)
307 #define WRITELONG(p,v) \
308 do { \
309 uint8_t *_wl_p = (uint8_t *)(p); \
310 uint32_t _wl_v = (v); \
311 _wl_p[0] = _wl_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); \
316 } while (0)
318 #define WRITEDLONG(p,v) \
319 do { \
320 uint8_t *_wq_p = (uint8_t *)(p); \
321 uint64_t _wq_v = (v); \
322 _wq_p[0] = _wq_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); \
331 } while (0)
333 #define WRITEADDR(p,v,s) \
334 do { \
335 int _wa_s = (s); \
336 uint64_t _wa_v = (v); \
337 while (_wa_s--) { \
338 WRITECHAR(p,_wa_v); \
339 _wa_v >>= 8; \
341 } while(0)
343 #endif
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.
367 void src_init(void);
368 void src_free(void);
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 *nasm_realpath(const char *rel_path);
400 const char * pure_func prefix_name(int);
403 * Wrappers around fopen()... for future change to a dedicated structure
405 enum file_flags {
406 NF_BINARY = 0x00000000, /* Binary file (default) */
407 NF_TEXT = 0x00000001, /* Text file */
408 NF_NONFATAL = 0x00000000, /* Don't die on open failure (default) */
409 NF_FATAL = 0x00000002, /* Die on open failure */
410 NF_FORMAP = 0x00000004 /* Intended to use nasm_map_file() */
413 FILE *nasm_open_read(const char *filename, enum file_flags flags);
414 FILE *nasm_open_write(const char *filename, enum file_flags flags);
416 /* Probe for existence of a file */
417 bool nasm_file_exists(const char *filename);
419 #define ZERO_BUF_SIZE 65536 /* Default value */
420 #if defined(BUFSIZ) && (BUFSIZ > ZERO_BUF_SIZE)
421 # undef ZERO_BUF_SIZE
422 # define ZERO_BUF_SIZE BUFSIZ
423 #endif
424 extern const uint8_t zero_buffer[ZERO_BUF_SIZE];
426 /* Missing fseeko/ftello */
427 #ifndef HAVE_FSEEKO
428 # undef off_t /* Just in case it is a macro */
429 # ifdef HAVE__FSEEKI64
430 # define fseeko _fseeki64
431 # define ftello _ftelli64
432 # define off_t int64_t
433 # else
434 # define fseeko fseek
435 # define ftello ftell
436 # define off_t long
437 # endif
438 #endif
440 const void *nasm_map_file(FILE *fp, off_t start, off_t len);
441 void nasm_unmap_file(const void *p, size_t len);
442 off_t nasm_file_size(FILE *f);
443 off_t nasm_file_size_by_path(const char *pathname);
444 void fwritezero(off_t bytes, FILE *fp);
446 static inline bool const_func overflow_general(int64_t value, int bytes)
448 int sbit;
449 int64_t vmax, vmin;
451 if (bytes >= 8)
452 return false;
454 sbit = (bytes << 3) - 1;
455 vmax = ((int64_t)2 << sbit) - 1;
456 vmin = -((int64_t)1 << sbit);
458 return value < vmin || value > vmax;
461 static inline bool const_func overflow_signed(int64_t value, int bytes)
463 int sbit;
464 int64_t vmax, vmin;
466 if (bytes >= 8)
467 return false;
469 sbit = (bytes << 3) - 1;
470 vmax = ((int64_t)1 << sbit) - 1;
471 vmin = -((int64_t)1 << sbit);
473 return value < vmin || value > vmax;
476 static inline bool const_func overflow_unsigned(int64_t value, int bytes)
478 int sbit;
479 int64_t vmax, vmin;
481 if (bytes >= 8)
482 return false;
484 sbit = (bytes << 3) - 1;
485 vmax = ((int64_t)2 << sbit) - 1;
486 vmin = 0;
488 return value < vmin || value > vmax;
491 static inline int64_t const_func signed_bits(int64_t value, int bits)
493 if (bits < 64) {
494 value &= ((int64_t)1 << bits) - 1;
495 if (value & (int64_t)1 << (bits - 1))
496 value |= (int64_t)((uint64_t)-1 << bits);
498 return value;
501 int const_func idata_bytes(int opcode);
503 /* check if value is power of 2 */
504 #define is_power2(v) ((v) && ((v) & ((v) - 1)) == 0)
507 * floor(log2(v))
509 int const_func ilog2_32(uint32_t v);
510 int const_func ilog2_64(uint64_t v);
513 * v == 0 ? 0 : is_power2(x) ? ilog2_X(v) : -1
515 int const_func alignlog2_32(uint32_t v);
516 int const_func alignlog2_64(uint64_t v);
518 #endif