2009-06-29 08:13:04 +08:00
|
|
|
/* ----------------------------------------------------------------------- *
|
2009-10-11 17:17:52 +08:00
|
|
|
*
|
2009-06-29 08:13:04 +08:00
|
|
|
* Copyright 1996-2009 The NASM Authors - All Rights Reserved
|
|
|
|
* See the file AUTHORS included with the NASM distribution for
|
|
|
|
* the specific copyright holders.
|
2002-05-01 04:51:32 +08:00
|
|
|
*
|
2009-06-29 08:13:04 +08:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following
|
|
|
|
* conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials provided
|
|
|
|
* with the distribution.
|
2009-10-11 17:17:52 +08:00
|
|
|
*
|
2009-06-29 08:13:04 +08:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* ----------------------------------------------------------------------- */
|
|
|
|
|
2009-10-11 17:17:52 +08:00
|
|
|
/*
|
|
|
|
* nasmlib.h header file for nasmlib.c
|
2002-05-01 04:51:32 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef NASM_NASMLIB_H
|
|
|
|
#define NASM_NASMLIB_H
|
|
|
|
|
2007-10-03 12:53:51 +08:00
|
|
|
#include "compiler.h"
|
|
|
|
|
2007-09-12 09:29:43 +08:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdio.h>
|
2007-10-03 12:53:51 +08:00
|
|
|
#include <string.h>
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
#include <strings.h>
|
|
|
|
#endif
|
2007-09-12 09:29:43 +08:00
|
|
|
|
2008-06-22 01:23:17 +08:00
|
|
|
/*
|
|
|
|
* tolower table -- avoids a function call on some platforms.
|
|
|
|
* NOTE: unlike the tolower() function in ctype, EOF is *NOT*
|
|
|
|
* a permitted value, for obvious reasons.
|
|
|
|
*/
|
|
|
|
void tolower_init(void);
|
|
|
|
extern unsigned char nasm_tolower_tab[256];
|
|
|
|
#define nasm_tolower(x) nasm_tolower_tab[(unsigned char)(x)]
|
|
|
|
|
|
|
|
/* Wrappers around <ctype.h> functions */
|
|
|
|
/* These are only valid for values that cannot include EOF */
|
2008-06-22 02:03:51 +08:00
|
|
|
#define nasm_isspace(x) isspace((unsigned char)(x))
|
|
|
|
#define nasm_isalpha(x) isalpha((unsigned char)(x))
|
|
|
|
#define nasm_isdigit(x) isdigit((unsigned char)(x))
|
|
|
|
#define nasm_isalnum(x) isalnum((unsigned char)(x))
|
|
|
|
#define nasm_isxdigit(x) isxdigit((unsigned char)(x))
|
2008-06-22 01:23:17 +08:00
|
|
|
|
2007-09-20 12:40:37 +08:00
|
|
|
/*
|
|
|
|
* -------------------------
|
|
|
|
* Error reporting functions
|
|
|
|
* -------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* An error reporting function should look like this.
|
|
|
|
*/
|
|
|
|
typedef void (*efunc) (int severity, const char *fmt, ...);
|
2009-07-19 09:07:17 +08:00
|
|
|
typedef void (*vefunc) (int severity, const char *fmt, va_list ap);
|
2009-08-11 08:10:16 +08:00
|
|
|
#ifdef __GNUC__
|
|
|
|
void nasm_error(int severity, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
|
|
|
|
#else
|
2009-07-19 09:07:17 +08:00
|
|
|
void nasm_error(int severity, const char *fmt, ...);
|
2009-08-11 08:10:16 +08:00
|
|
|
#endif
|
2009-07-19 09:07:17 +08:00
|
|
|
void nasm_set_verror(vefunc);
|
2007-09-20 12:40:37 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* These are the error severity codes which get passed as the first
|
|
|
|
* argument to an efunc.
|
|
|
|
*/
|
|
|
|
|
2009-10-11 17:17:52 +08:00
|
|
|
#define ERR_DEBUG 0x00000008 /* put out debugging message */
|
|
|
|
#define ERR_WARNING 0x00000000 /* warn only: no further action */
|
|
|
|
#define ERR_NONFATAL 0x00000001 /* terminate assembly after phase */
|
|
|
|
#define ERR_FATAL 0x00000002 /* instantly fatal: exit with error */
|
|
|
|
#define ERR_PANIC 0x00000003 /* internal error: panic instantly
|
2007-09-20 12:40:37 +08:00
|
|
|
* and dump core for reference */
|
2009-10-11 17:17:52 +08:00
|
|
|
#define ERR_MASK 0x0000000F /* mask off the above codes */
|
|
|
|
#define ERR_NOFILE 0x00000010 /* don't give source file name/line */
|
|
|
|
#define ERR_USAGE 0x00000020 /* print a usage message */
|
|
|
|
#define ERR_PASS1 0x00000040 /* only print this error on pass one */
|
|
|
|
#define ERR_PASS2 0x00000080
|
|
|
|
#define ERR_NO_SEVERITY 0x00000100 /* suppress printing severity */
|
2007-09-20 12:40:37 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* These codes define specific types of suppressible warning.
|
|
|
|
*/
|
|
|
|
|
2009-10-11 17:17:52 +08:00
|
|
|
#define ERR_WARN_MASK 0xFFFFF000 /* the mask for this feature */
|
|
|
|
#define ERR_WARN_SHR 12 /* how far to shift right */
|
2007-09-20 12:40:37 +08:00
|
|
|
|
2011-12-05 05:01:06 +08:00
|
|
|
#define WARN(x) ((x) << ERR_WARN_SHR)
|
|
|
|
#define WARN_IDX(x) (((x) & ERR_WARN_MASK) >> ERR_WARN_SHR)
|
2007-10-17 02:32:58 +08:00
|
|
|
|
2011-12-05 05:01:06 +08:00
|
|
|
#define ERR_WARN_TERM WARN( 0) /* treat warnings as errors */
|
2009-10-11 17:17:52 +08:00
|
|
|
#define ERR_WARN_MNP WARN( 1) /* macro-num-parameters warning */
|
|
|
|
#define ERR_WARN_MSR WARN( 2) /* macro self-reference */
|
|
|
|
#define ERR_WARN_MDP WARN( 3) /* macro default parameters check */
|
|
|
|
#define ERR_WARN_OL WARN( 4) /* orphan label (no colon, and
|
2008-08-06 20:47:54 +08:00
|
|
|
* alone on line) */
|
2009-10-11 17:17:52 +08:00
|
|
|
#define ERR_WARN_NOV WARN( 5) /* numeric overflow */
|
|
|
|
#define ERR_WARN_GNUELF WARN( 6) /* using GNU ELF extensions */
|
|
|
|
#define ERR_WARN_FL_OVERFLOW WARN( 7) /* FP overflow */
|
|
|
|
#define ERR_WARN_FL_DENORM WARN( 8) /* FP denormal */
|
|
|
|
#define ERR_WARN_FL_UNDERFLOW WARN( 9) /* FP underflow */
|
|
|
|
#define ERR_WARN_FL_TOOLONG WARN(10) /* FP too many digits */
|
|
|
|
#define ERR_WARN_USER WARN(11) /* %warning directives */
|
2012-02-26 07:10:04 +08:00
|
|
|
#define ERR_WARN_LOCK WARN(12) /* bad LOCK prefixes */
|
|
|
|
#define ERR_WARN_HLE WARN(13) /* bad HLE prefixes */
|
2013-11-29 16:38:29 +08:00
|
|
|
#define ERR_WARN_BND WARN(14) /* bad BND prefixes */
|
|
|
|
#define ERR_WARN_MAX 14 /* the highest numbered one */
|
2007-09-20 12:40:37 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
/*
|
|
|
|
* Wrappers around malloc, realloc and free. nasm_malloc will
|
|
|
|
* fatal-error and die rather than return NULL; nasm_realloc will
|
|
|
|
* do likewise, and will also guarantee to work right on being
|
|
|
|
* passed a NULL pointer; nasm_free will do nothing if it is passed
|
|
|
|
* a NULL pointer.
|
|
|
|
*/
|
2005-01-16 06:15:51 +08:00
|
|
|
void *nasm_malloc(size_t);
|
2007-09-26 05:27:34 +08:00
|
|
|
void *nasm_zalloc(size_t);
|
2005-01-16 06:15:51 +08:00
|
|
|
void *nasm_realloc(void *, size_t);
|
|
|
|
void nasm_free(void *);
|
2007-04-14 00:47:53 +08:00
|
|
|
char *nasm_strdup(const char *);
|
2009-07-02 13:02:54 +08:00
|
|
|
char *nasm_strndup(const char *, size_t);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2009-07-02 13:02:54 +08:00
|
|
|
/*
|
|
|
|
* NASM assert failure
|
|
|
|
*/
|
2009-07-06 10:22:28 +08:00
|
|
|
no_return nasm_assert_failed(const char *, int, const char *);
|
2009-10-11 17:17:52 +08:00
|
|
|
#define nasm_assert(x) \
|
|
|
|
do { \
|
|
|
|
if (unlikely(!(x))) \
|
|
|
|
nasm_assert_failed(__FILE__,__LINE__,#x); \
|
2009-07-04 13:55:51 +08:00
|
|
|
} while (0)
|
2009-07-02 13:02:54 +08:00
|
|
|
|
2009-11-06 04:35:04 +08:00
|
|
|
/*
|
|
|
|
* NASM failure at build time if x != 0
|
|
|
|
*/
|
|
|
|
#define nasm_build_assert(x) (void)(sizeof(char[1-2*!!(x)]))
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
/*
|
|
|
|
* ANSI doesn't guarantee the presence of `stricmp' or
|
|
|
|
* `strcasecmp'.
|
|
|
|
*/
|
2007-10-03 12:53:51 +08:00
|
|
|
#if defined(HAVE_STRCASECMP)
|
2002-05-01 04:59:21 +08:00
|
|
|
#define nasm_stricmp strcasecmp
|
2007-10-03 12:53:51 +08:00
|
|
|
#elif defined(HAVE_STRICMP)
|
|
|
|
#define nasm_stricmp stricmp
|
2002-05-01 04:59:21 +08:00
|
|
|
#else
|
2007-04-14 00:47:53 +08:00
|
|
|
int nasm_stricmp(const char *, const char *);
|
2002-05-01 04:59:21 +08:00
|
|
|
#endif
|
|
|
|
|
2007-10-03 12:53:51 +08:00
|
|
|
#if defined(HAVE_STRNCASECMP)
|
2002-05-01 04:59:21 +08:00
|
|
|
#define nasm_strnicmp strncasecmp
|
2007-10-03 12:53:51 +08:00
|
|
|
#elif defined(HAVE_STRNICMP)
|
|
|
|
#define nasm_strnicmp strnicmp
|
2002-05-01 04:59:21 +08:00
|
|
|
#else
|
2008-06-02 12:34:49 +08:00
|
|
|
int nasm_strnicmp(const char *, const char *, size_t);
|
2002-05-01 04:59:21 +08:00
|
|
|
#endif
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2008-06-02 12:34:49 +08:00
|
|
|
int nasm_memicmp(const char *, const char *, size_t);
|
|
|
|
|
2007-10-03 12:53:51 +08:00
|
|
|
#if defined(HAVE_STRSEP)
|
2007-08-26 13:48:54 +08:00
|
|
|
#define nasm_strsep strsep
|
|
|
|
#else
|
|
|
|
char *nasm_strsep(char **stringp, const char *delim);
|
|
|
|
#endif
|
|
|
|
|
2011-04-13 18:11:15 +08:00
|
|
|
/* This returns the numeric value of a given 'digit'. */
|
|
|
|
#define numvalue(c) ((c) >= 'a' ? (c) - 'a' + 10 : (c) >= 'A' ? (c) - 'A' + 10 : (c) - '0')
|
2007-08-26 13:48:54 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
/*
|
|
|
|
* Convert a string into a number, using NASM number rules. Sets
|
2007-10-11 05:58:45 +08:00
|
|
|
* `*error' to true if an error occurs, and false otherwise.
|
2002-05-01 04:51:32 +08:00
|
|
|
*/
|
2007-10-11 15:05:31 +08:00
|
|
|
int64_t readnum(char *str, bool *error);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2002-05-01 04:53:55 +08:00
|
|
|
/*
|
|
|
|
* Convert a character constant into a number. Sets
|
2007-10-11 05:58:45 +08:00
|
|
|
* `*warn' to true if an overflow occurs, and false otherwise.
|
2002-05-01 04:53:55 +08:00
|
|
|
* str points to and length covers the middle of the string,
|
|
|
|
* without the quotes.
|
|
|
|
*/
|
2007-10-11 15:05:31 +08:00
|
|
|
int64_t readstrnum(char *str, int length, bool *warn);
|
2002-05-01 04:53:55 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
/*
|
|
|
|
* seg_init: Initialise the segment-number allocator.
|
|
|
|
* seg_alloc: allocate a hitherto unused segment number.
|
|
|
|
*/
|
|
|
|
void seg_init(void);
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t seg_alloc(void);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* many output formats will be able to make use of this: a standard
|
|
|
|
* function to add an extension to the name of the input file
|
|
|
|
*/
|
2009-07-19 09:07:17 +08:00
|
|
|
void standard_extension(char *inname, char *outname, char *extension);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-11-09 11:30:22 +08:00
|
|
|
/*
|
|
|
|
* Utility macros...
|
|
|
|
*
|
|
|
|
* This is a useful #define which I keep meaning to use more often:
|
|
|
|
* the number of elements of a statically defined array.
|
|
|
|
*/
|
2010-06-04 02:04:36 +08:00
|
|
|
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
|
2007-11-09 11:30:22 +08:00
|
|
|
|
2009-07-11 21:28:42 +08:00
|
|
|
/*
|
|
|
|
* List handling
|
|
|
|
*
|
|
|
|
* list_for_each - regular iterator over list
|
|
|
|
* list_for_each_safe - the same but safe against list items removal
|
2011-04-13 14:19:30 +08:00
|
|
|
* list_last - find the last element in a list
|
2009-07-11 21:28:42 +08:00
|
|
|
*/
|
2009-10-11 17:17:52 +08:00
|
|
|
#define list_for_each(pos, head) \
|
|
|
|
for (pos = head; pos; pos = pos->next)
|
|
|
|
#define list_for_each_safe(pos, n, head) \
|
|
|
|
for (pos = head, n = (pos ? pos->next : NULL); pos; \
|
|
|
|
pos = n, n = (n ? n->next : NULL))
|
2011-04-13 14:19:30 +08:00
|
|
|
#define list_last(pos, head) \
|
|
|
|
for (pos = head; pos && pos->next; pos = pos->next) \
|
|
|
|
;
|
2011-04-13 15:25:49 +08:00
|
|
|
#define list_reverse(head, prev, next) \
|
|
|
|
do { \
|
|
|
|
if (!head || !head->next) \
|
|
|
|
break; \
|
|
|
|
prev = NULL; \
|
|
|
|
while (head) { \
|
|
|
|
next = head->next; \
|
|
|
|
head->next = prev; \
|
|
|
|
prev = head; \
|
|
|
|
head = next; \
|
|
|
|
} \
|
|
|
|
head = prev; \
|
|
|
|
} while (0)
|
2009-07-11 21:28:42 +08:00
|
|
|
|
2010-02-11 14:03:51 +08:00
|
|
|
/*
|
|
|
|
* Power of 2 align helpers
|
|
|
|
*/
|
|
|
|
#define ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask))
|
|
|
|
#define ALIGN(v, a) ALIGN_MASK(v, (a) - 1)
|
|
|
|
#define IS_ALIGNED(v, a) (((v) & ((a) - 1)) == 0)
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
/*
|
|
|
|
* some handy macros that will probably be of use in more than one
|
|
|
|
* output format: convert integers into little-endian byte packed
|
|
|
|
* format in memory
|
|
|
|
*/
|
|
|
|
|
2007-11-14 01:46:38 +08:00
|
|
|
#if X86_MEMORY
|
|
|
|
|
2009-10-11 17:17:52 +08:00
|
|
|
#define WRITECHAR(p,v) \
|
|
|
|
do { \
|
|
|
|
*(uint8_t *)(p) = (v); \
|
|
|
|
(p) += 1; \
|
2007-11-14 01:46:38 +08:00
|
|
|
} while (0)
|
|
|
|
|
2009-10-11 17:17:52 +08:00
|
|
|
#define WRITESHORT(p,v) \
|
|
|
|
do { \
|
|
|
|
*(uint16_t *)(p) = (v); \
|
|
|
|
(p) += 2; \
|
2007-11-14 01:46:38 +08:00
|
|
|
} while (0)
|
|
|
|
|
2009-10-11 17:17:52 +08:00
|
|
|
#define WRITELONG(p,v) \
|
|
|
|
do { \
|
|
|
|
*(uint32_t *)(p) = (v); \
|
|
|
|
(p) += 4; \
|
2007-11-14 01:46:38 +08:00
|
|
|
} while (0)
|
|
|
|
|
2009-10-11 17:17:52 +08:00
|
|
|
#define WRITEDLONG(p,v) \
|
|
|
|
do { \
|
|
|
|
*(uint64_t *)(p) = (v); \
|
|
|
|
(p) += 8; \
|
2007-11-14 01:46:38 +08:00
|
|
|
} while (0)
|
|
|
|
|
2009-10-11 17:17:52 +08:00
|
|
|
#define WRITEADDR(p,v,s) \
|
|
|
|
do { \
|
|
|
|
uint64_t _wa_v = (v); \
|
|
|
|
memcpy((p), &_wa_v, (s)); \
|
|
|
|
(p) += (s); \
|
2007-11-14 01:46:38 +08:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#else /* !X86_MEMORY */
|
|
|
|
|
2009-10-11 17:17:52 +08:00
|
|
|
#define WRITECHAR(p,v) \
|
|
|
|
do { \
|
|
|
|
uint8_t *_wc_p = (uint8_t *)(p); \
|
|
|
|
uint8_t _wc_v = (v); \
|
|
|
|
_wc_p[0] = _wc_v; \
|
|
|
|
(p) = (void *)(_wc_p + 1); \
|
2008-02-17 07:25:02 +08:00
|
|
|
} while (0)
|
|
|
|
|
2009-10-11 17:17:52 +08:00
|
|
|
#define WRITESHORT(p,v) \
|
|
|
|
do { \
|
|
|
|
uint8_t *_ws_p = (uint8_t *)(p); \
|
|
|
|
uint16_t _ws_v = (v); \
|
|
|
|
_ws_p[0] = _ws_v; \
|
|
|
|
_ws_p[1] = _ws_v >> 8; \
|
|
|
|
(p) = (void *)(_ws_p + 2); \
|
2008-02-17 07:25:02 +08:00
|
|
|
} while (0)
|
|
|
|
|
2009-10-11 17:17:52 +08:00
|
|
|
#define WRITELONG(p,v) \
|
|
|
|
do { \
|
|
|
|
uint8_t *_wl_p = (uint8_t *)(p); \
|
|
|
|
uint32_t _wl_v = (v); \
|
|
|
|
_wl_p[0] = _wl_v; \
|
|
|
|
_wl_p[1] = _wl_v >> 8; \
|
|
|
|
_wl_p[2] = _wl_v >> 16; \
|
|
|
|
_wl_p[3] = _wl_v >> 24; \
|
|
|
|
(p) = (void *)(_wl_p + 4); \
|
2008-02-17 07:25:02 +08:00
|
|
|
} while (0)
|
|
|
|
|
2009-10-11 17:17:52 +08:00
|
|
|
#define WRITEDLONG(p,v) \
|
|
|
|
do { \
|
|
|
|
uint8_t *_wq_p = (uint8_t *)(p); \
|
|
|
|
uint64_t _wq_v = (v); \
|
|
|
|
_wq_p[0] = _wq_v; \
|
|
|
|
_wq_p[1] = _wq_v >> 8; \
|
|
|
|
_wq_p[2] = _wq_v >> 16; \
|
|
|
|
_wq_p[3] = _wq_v >> 24; \
|
|
|
|
_wq_p[4] = _wq_v >> 32; \
|
|
|
|
_wq_p[5] = _wq_v >> 40; \
|
|
|
|
_wq_p[6] = _wq_v >> 48; \
|
|
|
|
_wq_p[7] = _wq_v >> 56; \
|
|
|
|
(p) = (void *)(_wq_p + 8); \
|
2008-02-17 07:25:02 +08:00
|
|
|
} while (0)
|
|
|
|
|
2009-10-11 17:17:52 +08:00
|
|
|
#define WRITEADDR(p,v,s) \
|
|
|
|
do { \
|
|
|
|
int _wa_s = (s); \
|
|
|
|
uint64_t _wa_v = (v); \
|
|
|
|
while (_wa_s--) { \
|
|
|
|
WRITECHAR(p,_wa_v); \
|
|
|
|
_wa_v >>= 8; \
|
|
|
|
} \
|
2007-11-14 01:37:59 +08:00
|
|
|
} while(0)
|
|
|
|
|
2007-11-14 01:46:38 +08:00
|
|
|
#endif
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
/*
|
|
|
|
* and routines to do the same thing to a file
|
|
|
|
*/
|
2007-11-14 01:37:59 +08:00
|
|
|
#define fwriteint8_t(d,f) putc(d,f)
|
2007-11-14 02:37:23 +08:00
|
|
|
void fwriteint16_t(uint16_t data, FILE * fp);
|
|
|
|
void fwriteint32_t(uint32_t data, FILE * fp);
|
|
|
|
void fwriteint64_t(uint64_t data, FILE * fp);
|
|
|
|
void fwriteaddr(uint64_t data, int size, FILE * fp);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2002-05-01 04:52:49 +08:00
|
|
|
/*
|
|
|
|
* Binary search routine. Returns index into `array' of an entry
|
|
|
|
* matching `string', or <0 if no match. `array' is taken to
|
|
|
|
* contain `size' elements.
|
2007-08-30 00:25:46 +08:00
|
|
|
*
|
|
|
|
* bsi() is case sensitive, bsii() is case insensitive.
|
2002-05-01 04:52:49 +08:00
|
|
|
*/
|
2007-11-09 11:15:33 +08:00
|
|
|
int bsi(const char *string, const char **array, int size);
|
|
|
|
int bsii(const char *string, const char **array, int size);
|
2002-05-01 04:53:55 +08:00
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
char *src_set_fname(char *newname);
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t src_set_linnum(int32_t newline);
|
|
|
|
int32_t src_get_linnum(void);
|
2002-05-01 04:53:55 +08:00
|
|
|
/*
|
|
|
|
* src_get may be used if you simply want to know the source file and line.
|
|
|
|
* It is also used if you maintain private status about the source location
|
|
|
|
* It return 0 if the information was the same as the last time you
|
|
|
|
* checked, -1 if the name changed and (new-old) if just the line changed.
|
|
|
|
*/
|
2007-04-14 00:47:53 +08:00
|
|
|
int src_get(int32_t *xline, char **xname);
|
2002-05-01 04:53:55 +08:00
|
|
|
|
2009-02-22 09:54:31 +08:00
|
|
|
char *nasm_strcat(const char *one, const char *two);
|
2002-05-01 04:53:55 +08:00
|
|
|
|
2009-10-11 20:51:31 +08:00
|
|
|
char *nasm_skip_spaces(const char *p);
|
|
|
|
char *nasm_skip_word(const char *p);
|
2009-10-14 03:03:37 +08:00
|
|
|
char *nasm_zap_spaces_fwd(char *p);
|
2009-10-11 20:51:31 +08:00
|
|
|
char *nasm_zap_spaces_rev(char *p);
|
2010-04-09 17:17:23 +08:00
|
|
|
char *nasm_trim_spaces(char *p);
|
2010-04-11 05:03:20 +08:00
|
|
|
char *nasm_get_word(char *p, char **tail);
|
2010-04-09 17:17:23 +08:00
|
|
|
char *nasm_opt_val(char *p, char **opt, char **val);
|
2009-10-11 20:51:31 +08:00
|
|
|
|
2007-04-18 04:23:11 +08:00
|
|
|
const char *prefix_name(int);
|
|
|
|
|
2009-10-11 17:17:52 +08:00
|
|
|
#define ZERO_BUF_SIZE 4096
|
2009-02-09 18:03:33 +08:00
|
|
|
extern const uint8_t zero_buffer[ZERO_BUF_SIZE];
|
|
|
|
size_t fwritezero(size_t bytes, FILE *fp);
|
|
|
|
|
2009-10-11 19:01:39 +08:00
|
|
|
static inline bool overflow_general(int64_t value, int bytes)
|
|
|
|
{
|
2009-11-11 14:39:21 +08:00
|
|
|
int sbit;
|
|
|
|
int64_t vmax, vmin;
|
|
|
|
|
|
|
|
if (bytes >= 8)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
sbit = (bytes << 3) - 1;
|
|
|
|
vmax = ((int64_t)2 << sbit) - 1;
|
|
|
|
vmin = -((int64_t)1 << sbit);
|
2009-10-11 19:01:39 +08:00
|
|
|
|
|
|
|
return value < vmin || value > vmax;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool overflow_signed(int64_t value, int bytes)
|
|
|
|
{
|
2009-11-11 14:39:21 +08:00
|
|
|
int sbit;
|
|
|
|
int64_t vmax, vmin;
|
|
|
|
|
|
|
|
if (bytes >= 8)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
sbit = (bytes << 3) - 1;
|
|
|
|
vmax = ((int64_t)1 << sbit) - 1;
|
|
|
|
vmin = -((int64_t)1 << sbit);
|
2009-10-11 19:01:39 +08:00
|
|
|
|
|
|
|
return value < vmin || value > vmax;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool overflow_unsigned(int64_t value, int bytes)
|
|
|
|
{
|
2009-11-11 14:39:21 +08:00
|
|
|
int sbit;
|
|
|
|
int64_t vmax, vmin;
|
|
|
|
|
|
|
|
if (bytes >= 8)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
sbit = (bytes << 3) - 1;
|
|
|
|
vmax = ((int64_t)2 << sbit) - 1;
|
|
|
|
vmin = 0;
|
2009-10-11 19:01:39 +08:00
|
|
|
|
|
|
|
return value < vmin || value > vmax;
|
|
|
|
}
|
|
|
|
|
2010-01-25 04:24:57 +08:00
|
|
|
static inline int64_t signed_bits(int64_t value, int bits)
|
|
|
|
{
|
|
|
|
if (bits < 64) {
|
|
|
|
value &= ((int64_t)1 << bits) - 1;
|
|
|
|
if (value & (int64_t)1 << (bits - 1))
|
|
|
|
value |= (int64_t)-1 << bits;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2009-11-01 01:02:14 +08:00
|
|
|
int idata_bytes(int opcode);
|
|
|
|
|
2009-11-06 14:03:08 +08:00
|
|
|
/* check if value is power of 2 */
|
|
|
|
#define is_power2(v) ((v) && ((v) & ((v) - 1)) == 0)
|
|
|
|
|
2010-04-22 07:43:20 +08:00
|
|
|
/*
|
|
|
|
* floor(log2(v))
|
|
|
|
*/
|
|
|
|
int ilog2_32(uint32_t v);
|
|
|
|
int ilog2_64(uint64_t v);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* v == 0 ? 0 : is_power2(x) ? ilog2_X(v) : -1
|
|
|
|
*/
|
|
|
|
int alignlog2_32(uint32_t v);
|
|
|
|
int alignlog2_64(uint64_t v);
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
#endif
|