output: outmac -- Fix few nits during merge
[nasm.git] / nasmlib.h
blob86766e379c4b5de9a59399077674a50e579447e2
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2016 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 <inttypes.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 * -------------------------
69 * Error reporting functions
70 * -------------------------
74 * An error reporting function should look like this.
76 typedef void (*efunc) (int severity, const char *fmt, ...);
77 typedef void (*vefunc) (int severity, const char *fmt, va_list ap);
78 void printf_func(2, 3) nasm_error(int severity, const char *fmt, ...);
79 void nasm_set_verror(vefunc);
80 no_return printf_func(2, 3) nasm_panic(int flags, const char *fmt, ...);
81 no_return nasm_panic_from_macro(const char *file, int line);
82 #define panic() nasm_panic_from_macro(__FILE__, __LINE__);
85 * These are the error severity codes which get passed as the first
86 * argument to an efunc.
89 #define ERR_DEBUG 0x00000008 /* put out debugging message */
90 #define ERR_WARNING 0x00000000 /* warn only: no further action */
91 #define ERR_NONFATAL 0x00000001 /* terminate assembly after phase */
92 #define ERR_FATAL 0x00000002 /* instantly fatal: exit with error */
93 #define ERR_PANIC 0x00000003 /* internal error: panic instantly
94 * and dump core for reference */
95 #define ERR_MASK 0x0000000F /* mask off the above codes */
96 #define ERR_NOFILE 0x00000010 /* don't give source file name/line */
97 #define ERR_USAGE 0x00000020 /* print a usage message */
98 #define ERR_PASS1 0x00000040 /* only print this error on pass one */
99 #define ERR_PASS2 0x00000080
100 #define ERR_NO_SEVERITY 0x00000100 /* suppress printing severity */
103 * These codes define specific types of suppressible warning.
106 #define ERR_WARN_MASK 0xFFFFF000 /* the mask for this feature */
107 #define ERR_WARN_SHR 12 /* how far to shift right */
109 #define WARN(x) ((x) << ERR_WARN_SHR)
110 #define WARN_IDX(x) (((x) & ERR_WARN_MASK) >> ERR_WARN_SHR)
112 #define ERR_WARN_TERM WARN( 0) /* treat warnings as errors */
113 #define ERR_WARN_MNP WARN( 1) /* macro-num-parameters warning */
114 #define ERR_WARN_MSR WARN( 2) /* macro self-reference */
115 #define ERR_WARN_MDP WARN( 3) /* macro default parameters check */
116 #define ERR_WARN_OL WARN( 4) /* orphan label (no colon, and
117 * alone on line) */
118 #define ERR_WARN_NOV WARN( 5) /* numeric overflow */
119 #define ERR_WARN_GNUELF WARN( 6) /* using GNU ELF extensions */
120 #define ERR_WARN_FL_OVERFLOW WARN( 7) /* FP overflow */
121 #define ERR_WARN_FL_DENORM WARN( 8) /* FP denormal */
122 #define ERR_WARN_FL_UNDERFLOW WARN( 9) /* FP underflow */
123 #define ERR_WARN_FL_TOOLONG WARN(10) /* FP too many digits */
124 #define ERR_WARN_USER WARN(11) /* %warning directives */
125 #define ERR_WARN_LOCK WARN(12) /* bad LOCK prefixes */
126 #define ERR_WARN_HLE WARN(13) /* bad HLE prefixes */
127 #define ERR_WARN_BND WARN(14) /* bad BND prefixes */
128 #define ERR_WARN_ZEXTRELOC WARN(15) /* relocation zero-extended */
129 #define ERR_WARN_MAX 15 /* the highest numbered one */
132 * Wrappers around malloc, realloc and free. nasm_malloc will
133 * fatal-error and die rather than return NULL; nasm_realloc will
134 * do likewise, and will also guarantee to work right on being
135 * passed a NULL pointer; nasm_free will do nothing if it is passed
136 * a NULL pointer.
138 void *nasm_malloc(size_t);
139 void *nasm_zalloc(size_t);
140 void *nasm_realloc(void *, size_t);
141 void nasm_free(void *);
142 char *nasm_strdup(const char *);
143 char *nasm_strndup(const char *, size_t);
146 * Wrapper around fwrite() which fatal-errors on output failure.
148 void nasm_write(const void *, size_t, FILE *);
151 * NASM assert failure
153 no_return nasm_assert_failed(const char *, int, const char *);
154 #define nasm_assert(x) \
155 do { \
156 if (unlikely(!(x))) \
157 nasm_assert_failed(__FILE__,__LINE__,#x); \
158 } while (0)
161 * NASM failure at build time if x != 0
163 #define nasm_build_assert(x) (void)(sizeof(char[1-2*!!(x)]))
166 * ANSI doesn't guarantee the presence of `stricmp' or
167 * `strcasecmp'.
169 #if defined(HAVE_STRCASECMP)
170 #define nasm_stricmp strcasecmp
171 #elif defined(HAVE_STRICMP)
172 #define nasm_stricmp stricmp
173 #else
174 int nasm_stricmp(const char *, const char *);
175 #endif
177 #if defined(HAVE_STRNCASECMP)
178 #define nasm_strnicmp strncasecmp
179 #elif defined(HAVE_STRNICMP)
180 #define nasm_strnicmp strnicmp
181 #else
182 int nasm_strnicmp(const char *, const char *, size_t);
183 #endif
185 int nasm_memicmp(const char *, const char *, size_t);
187 #if defined(HAVE_STRSEP)
188 #define nasm_strsep strsep
189 #else
190 char *nasm_strsep(char **stringp, const char *delim);
191 #endif
193 /* This returns the numeric value of a given 'digit'. */
194 #define numvalue(c) ((c) >= 'a' ? (c) - 'a' + 10 : (c) >= 'A' ? (c) - 'A' + 10 : (c) - '0')
197 * Convert a string into a number, using NASM number rules. Sets
198 * `*error' to true if an error occurs, and false otherwise.
200 int64_t readnum(char *str, bool *error);
203 * Convert a character constant into a number. Sets
204 * `*warn' to true if an overflow occurs, and false otherwise.
205 * str points to and length covers the middle of the string,
206 * without the quotes.
208 int64_t readstrnum(char *str, int length, bool *warn);
211 * seg_init: Initialise the segment-number allocator.
212 * seg_alloc: allocate a hitherto unused segment number.
214 void seg_init(void);
215 int32_t seg_alloc(void);
218 * many output formats will be able to make use of this: a standard
219 * function to add an extension to the name of the input file
221 void standard_extension(char *inname, char *outname, char *extension);
224 * Utility macros...
226 * This is a useful #define which I keep meaning to use more often:
227 * the number of elements of a statically defined array.
229 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
232 * List handling
234 * list_for_each - regular iterator over list
235 * list_for_each_safe - the same but safe against list items removal
236 * list_last - find the last element in a list
238 #define list_for_each(pos, head) \
239 for (pos = head; pos; pos = pos->next)
240 #define list_for_each_safe(pos, n, head) \
241 for (pos = head, n = (pos ? pos->next : NULL); pos; \
242 pos = n, n = (n ? n->next : NULL))
243 #define list_last(pos, head) \
244 for (pos = head; pos && pos->next; pos = pos->next) \
246 #define list_reverse(head, prev, next) \
247 do { \
248 if (!head || !head->next) \
249 break; \
250 prev = NULL; \
251 while (head) { \
252 next = head->next; \
253 head->next = prev; \
254 prev = head; \
255 head = next; \
257 head = prev; \
258 } while (0)
261 * Power of 2 align helpers
263 #undef ALIGN_MASK /* Some BSD flavors define these in system headers */
264 #undef ALIGN
265 #define ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask))
266 #define ALIGN(v, a) ALIGN_MASK(v, (a) - 1)
267 #define IS_ALIGNED(v, a) (((v) & ((a) - 1)) == 0)
270 * some handy macros that will probably be of use in more than one
271 * output format: convert integers into little-endian byte packed
272 * format in memory
275 #if X86_MEMORY
277 #define WRITECHAR(p,v) \
278 do { \
279 *(uint8_t *)(p) = (v); \
280 (p) += 1; \
281 } while (0)
283 #define WRITESHORT(p,v) \
284 do { \
285 *(uint16_t *)(p) = (v); \
286 (p) += 2; \
287 } while (0)
289 #define WRITELONG(p,v) \
290 do { \
291 *(uint32_t *)(p) = (v); \
292 (p) += 4; \
293 } while (0)
295 #define WRITEDLONG(p,v) \
296 do { \
297 *(uint64_t *)(p) = (v); \
298 (p) += 8; \
299 } while (0)
301 #define WRITEADDR(p,v,s) \
302 do { \
303 uint64_t _wa_v = (v); \
304 memcpy((p), &_wa_v, (s)); \
305 (p) += (s); \
306 } while (0)
308 #else /* !X86_MEMORY */
310 #define WRITECHAR(p,v) \
311 do { \
312 uint8_t *_wc_p = (uint8_t *)(p); \
313 uint8_t _wc_v = (v); \
314 _wc_p[0] = _wc_v; \
315 (p) = (void *)(_wc_p + 1); \
316 } while (0)
318 #define WRITESHORT(p,v) \
319 do { \
320 uint8_t *_ws_p = (uint8_t *)(p); \
321 uint16_t _ws_v = (v); \
322 _ws_p[0] = _ws_v; \
323 _ws_p[1] = _ws_v >> 8; \
324 (p) = (void *)(_ws_p + 2); \
325 } while (0)
327 #define WRITELONG(p,v) \
328 do { \
329 uint8_t *_wl_p = (uint8_t *)(p); \
330 uint32_t _wl_v = (v); \
331 _wl_p[0] = _wl_v; \
332 _wl_p[1] = _wl_v >> 8; \
333 _wl_p[2] = _wl_v >> 16; \
334 _wl_p[3] = _wl_v >> 24; \
335 (p) = (void *)(_wl_p + 4); \
336 } while (0)
338 #define WRITEDLONG(p,v) \
339 do { \
340 uint8_t *_wq_p = (uint8_t *)(p); \
341 uint64_t _wq_v = (v); \
342 _wq_p[0] = _wq_v; \
343 _wq_p[1] = _wq_v >> 8; \
344 _wq_p[2] = _wq_v >> 16; \
345 _wq_p[3] = _wq_v >> 24; \
346 _wq_p[4] = _wq_v >> 32; \
347 _wq_p[5] = _wq_v >> 40; \
348 _wq_p[6] = _wq_v >> 48; \
349 _wq_p[7] = _wq_v >> 56; \
350 (p) = (void *)(_wq_p + 8); \
351 } while (0)
353 #define WRITEADDR(p,v,s) \
354 do { \
355 int _wa_s = (s); \
356 uint64_t _wa_v = (v); \
357 while (_wa_s--) { \
358 WRITECHAR(p,_wa_v); \
359 _wa_v >>= 8; \
361 } while(0)
363 #endif
366 * and routines to do the same thing to a file
368 #define fwriteint8_t(d,f) putc(d,f)
369 void fwriteint16_t(uint16_t data, FILE * fp);
370 void fwriteint32_t(uint32_t data, FILE * fp);
371 void fwriteint64_t(uint64_t data, FILE * fp);
372 void fwriteaddr(uint64_t data, int size, FILE * fp);
375 * Binary search routine. Returns index into `array' of an entry
376 * matching `string', or <0 if no match. `array' is taken to
377 * contain `size' elements.
379 * bsi() is case sensitive, bsii() is case insensitive.
381 int bsi(const char *string, const char **array, int size);
382 int bsii(const char *string, const char **array, int size);
384 char *src_set_fname(char *newname);
385 int32_t src_set_linnum(int32_t newline);
386 int32_t src_get_linnum(void);
388 * src_get may be used if you simply want to know the source file and line.
389 * It is also used if you maintain private status about the source location
390 * It return 0 if the information was the same as the last time you
391 * checked, -1 if the name changed and (new-old) if just the line changed.
393 int src_get(int32_t *xline, char **xname);
395 char *nasm_strcat(const char *one, const char *two);
397 char *nasm_skip_spaces(const char *p);
398 char *nasm_skip_word(const char *p);
399 char *nasm_zap_spaces_fwd(char *p);
400 char *nasm_zap_spaces_rev(char *p);
401 char *nasm_trim_spaces(char *p);
402 char *nasm_get_word(char *p, char **tail);
403 char *nasm_opt_val(char *p, char **opt, char **val);
406 * Converts a relative pathname rel_path into an absolute path name.
408 * The buffer returned must be freed by the caller
410 char *nasm_realpath(const char *rel_path);
412 const char *prefix_name(int);
414 #define ZERO_BUF_SIZE 4096 /* Default value */
415 #if defined(BUFSIZ) && (BUFSIZ > ZERO_BUF_SIZE)
416 # undef ZERO_BUF_SIZE
417 # define ZERO_BUF_SIZE BUFSIZ
418 #endif
419 extern const uint8_t zero_buffer[ZERO_BUF_SIZE];
420 void fwritezero(size_t bytes, FILE *fp);
422 static inline bool overflow_general(int64_t value, int bytes)
424 int sbit;
425 int64_t vmax, vmin;
427 if (bytes >= 8)
428 return false;
430 sbit = (bytes << 3) - 1;
431 vmax = ((int64_t)2 << sbit) - 1;
432 vmin = -((int64_t)1 << sbit);
434 return value < vmin || value > vmax;
437 static inline bool overflow_signed(int64_t value, int bytes)
439 int sbit;
440 int64_t vmax, vmin;
442 if (bytes >= 8)
443 return false;
445 sbit = (bytes << 3) - 1;
446 vmax = ((int64_t)1 << sbit) - 1;
447 vmin = -((int64_t)1 << sbit);
449 return value < vmin || value > vmax;
452 static inline bool overflow_unsigned(int64_t value, int bytes)
454 int sbit;
455 int64_t vmax, vmin;
457 if (bytes >= 8)
458 return false;
460 sbit = (bytes << 3) - 1;
461 vmax = ((int64_t)2 << sbit) - 1;
462 vmin = 0;
464 return value < vmin || value > vmax;
467 static inline int64_t signed_bits(int64_t value, int bits)
469 if (bits < 64) {
470 value &= ((int64_t)1 << bits) - 1;
471 if (value & (int64_t)1 << (bits - 1))
472 value |= (int64_t)((uint64_t)-1 << bits);
474 return value;
477 int idata_bytes(int opcode);
479 /* check if value is power of 2 */
480 #define is_power2(v) ((v) && ((v) & ((v) - 1)) == 0)
483 * floor(log2(v))
485 int ilog2_32(uint32_t v);
486 int ilog2_64(uint64_t v);
489 * v == 0 ? 0 : is_power2(x) ? ilog2_X(v) : -1
491 int alignlog2_32(uint32_t v);
492 int alignlog2_64(uint64_t v);
494 #endif