1d65182f1818d1c19e6f3866ab6e68a262fad5185hbono@chromium.org/* 245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * YASM utility functions. 345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Includes standard headers and defines prototypes for replacement functions 545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * if needed. 645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Copyright (C) 2001-2007 Peter Johnson 845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Redistribution and use in source and binary forms, with or without 1045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * modification, are permitted provided that the following conditions 1145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * are met: 1245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 1. Redistributions of source code must retain the above copyright 1345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * notice, this list of conditions and the following disclaimer. 1445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 2. Redistributions in binary form must reproduce the above copyright 1545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * notice, this list of conditions and the following disclaimer in the 1645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * documentation and/or other materials provided with the distribution. 1745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 1845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS'' 1945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE 2245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * POSSIBILITY OF SUCH DAMAGE. 2945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org */ 3045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifndef YASM_UTIL_H 3145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define YASM_UTIL_H 3245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 3345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifdef HAVE_CONFIG_H 3445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <config.h> 3545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif 3645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 3745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#if defined(HAVE_GNU_C_LIBRARY) || defined(__MINGW32__) || defined(__DJGPP__) 3845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 3945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/* Work around glibc's non-defining of certain things when using gcc -ansi */ 4045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# ifdef __STRICT_ANSI__ 4145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# undef __STRICT_ANSI__ 4245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# endif 4345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 4445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/* Work around glibc's string inlines (in bits/string2.h) if needed */ 4545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# ifdef NO_STRING_INLINES 4645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# define __NO_STRING_INLINES 4745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# endif 4845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 4945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif 5045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 5145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#if !defined(lint) && !defined(NDEBUG) 5245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# define NDEBUG 5345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif 5445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 5545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <stdio.h> 5645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <stdarg.h> 5745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 5845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <stddef.h> 5945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <stdlib.h> 6045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <string.h> 6145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <assert.h> 6245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 6345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifdef HAVE_STRINGS_H 6445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <strings.h> 6545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif 6645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 6745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <libyasm-stdint.h> 6845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <libyasm/coretype.h> 6945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 7045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifdef lint 7145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# define _(String) String 7245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#else 7345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# ifdef HAVE_LOCALE_H 7445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# include <locale.h> 7545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# endif 7645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 7745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# ifdef ENABLE_NLS 7845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# include <libintl.h> 7945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# define _(String) gettext(String) 8045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# else 8145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# define gettext(Msgid) (Msgid) 8245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# define dgettext(Domainname, Msgid) (Msgid) 8345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# define dcgettext(Domainname, Msgid, Category) (Msgid) 8445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# define textdomain(Domainname) while (0) /* nothing */ 8545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# define bindtextdomain(Domainname, Dirname) while (0) /* nothing */ 8645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# define _(String) (String) 8745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# endif 8845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif 8945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 9045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifdef gettext_noop 9145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# define N_(String) gettext_noop(String) 9245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#else 9345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# define N_(String) (String) 9445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif 9545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 9645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifdef HAVE_MERGESORT 9745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define yasm__mergesort(a, b, c, d) mergesort(a, b, c, d) 9845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif 9945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 10045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifdef HAVE_STRSEP 10145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define yasm__strsep(a, b) strsep(a, b) 10245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif 10345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 10445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifdef HAVE_STRCASECMP 10545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# define yasm__strcasecmp(x, y) strcasecmp(x, y) 10645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# define yasm__strncasecmp(x, y, n) strncasecmp(x, y, n) 10745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#elif HAVE_STRICMP 10845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# define yasm__strcasecmp(x, y) stricmp(x, y) 10945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# define yasm__strncasecmp(x, y, n) strnicmp(x, y, n) 11045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#elif HAVE__STRICMP 11145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# define yasm__strcasecmp(x, y) _stricmp(x, y) 11245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# define yasm__strncasecmp(x, y, n) _strnicmp(x, y, n) 11345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#elif HAVE_STRCMPI 11445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# define yasm__strcasecmp(x, y) strcmpi(x, y) 11545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# define yasm__strncasecmp(x, y, n) strncmpi(x, y, n) 11645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#else 11745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# define USE_OUR_OWN_STRCASECMP 11845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif 11945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 12045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <libyasm/compat-queue.h> 12145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 12245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifdef WITH_DMALLOC 12345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# include <dmalloc.h> 12445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# define yasm__xstrdup(str) xstrdup(str) 12545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# define yasm_xmalloc(size) xmalloc(size) 12645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# define yasm_xcalloc(count, size) xcalloc(count, size) 12745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# define yasm_xrealloc(ptr, size) xrealloc(ptr, size) 12845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# define yasm_xfree(ptr) xfree(ptr) 12945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif 13045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 13145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/* Bit-counting: used primarily by HAMT but also in a few other places. */ 13245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define BC_TWO(c) (0x1ul << (c)) 13345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define BC_MSK(c) (((unsigned long)(-1)) / (BC_TWO(BC_TWO(c)) + 1ul)) 13445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define BC_COUNT(x,c) ((x) & BC_MSK(c)) + (((x) >> (BC_TWO(c))) & BC_MSK(c)) 13545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define BitCount(d, s) do { \ 13645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org d = BC_COUNT(s, 0); \ 13745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org d = BC_COUNT(d, 1); \ 13845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org d = BC_COUNT(d, 2); \ 13945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org d = BC_COUNT(d, 3); \ 14045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org d = BC_COUNT(d, 4); \ 14145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } while (0) 14245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 14345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/** Determine if a value is exactly a power of 2. Zero is treated as a power 14445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * of two. 14545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * \param x value 14645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * \return Nonzero if x is a power of 2. 14745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org */ 14845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define is_exp2(x) ((x & (x - 1)) == 0) 14945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 15045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifndef NELEMS 15145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/** Get the number of elements in an array. 15245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * \internal 15345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * \param array array 15445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * \return Number of elements. 15545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org */ 15645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define NELEMS(array) (sizeof(array) / sizeof(array[0])) 15745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif 15845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 15945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif 160