headers/doc: copyright header for documentation files
[nasm/avx512.git] / nasmlib.h
blob995c9612a32d24dcb4777242a9feb76d09393194
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation, Inc.,
10 * 51 Franklin St, Fifth Floor, Boston MA 02110-1301, USA; version 2.1,
11 * or, at your option, any later version, incorporated herein by
12 * reference.
14 * Patches submitted to this file are required to be dual licensed
15 * under the LGPL 2.1+ and the 2-clause BSD license:
17 * Copyright 1996-2009 the NASM Authors - All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following
21 * conditions are met:
23 * * Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * * Redistributions in binary form must reproduce the above
26 * copyright notice, this list of conditions and the following
27 * disclaimer in the documentation and/or other materials provided
28 * with the distribution.
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
41 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 * ----------------------------------------------------------------------- */
46 /*
47 * nasmlib.h header file for nasmlib.c
50 #ifndef NASM_NASMLIB_H
51 #define NASM_NASMLIB_H
53 #include "compiler.h"
55 #include <inttypes.h>
56 #include <stdio.h>
57 #include <string.h>
58 #ifdef HAVE_STRINGS_H
59 #include <strings.h>
60 #endif
63 * tolower table -- avoids a function call on some platforms.
64 * NOTE: unlike the tolower() function in ctype, EOF is *NOT*
65 * a permitted value, for obvious reasons.
67 void tolower_init(void);
68 extern unsigned char nasm_tolower_tab[256];
69 #define nasm_tolower(x) nasm_tolower_tab[(unsigned char)(x)]
71 /* Wrappers around <ctype.h> functions */
72 /* These are only valid for values that cannot include EOF */
73 #define nasm_isspace(x) isspace((unsigned char)(x))
74 #define nasm_isalpha(x) isalpha((unsigned char)(x))
75 #define nasm_isdigit(x) isdigit((unsigned char)(x))
76 #define nasm_isalnum(x) isalnum((unsigned char)(x))
77 #define nasm_isxdigit(x) isxdigit((unsigned char)(x))
80 * If this is defined, the wrappers around malloc et al will
81 * transform into logging variants, which will cause NASM to create
82 * a file called `malloc.log' when run, and spew details of all its
83 * memory management into that. That can then be analysed to detect
84 * memory leaks and potentially other problems too.
86 /* #define LOGALLOC */
89 * -------------------------
90 * Error reporting functions
91 * -------------------------
95 * An error reporting function should look like this.
97 typedef void (*efunc) (int severity, const char *fmt, ...);
98 extern efunc nasm_malloc_error;
101 * These are the error severity codes which get passed as the first
102 * argument to an efunc.
105 #define ERR_DEBUG 0x00000008 /* put out debugging message */
106 #define ERR_WARNING 0x00000000 /* warn only: no further action */
107 #define ERR_NONFATAL 0x00000001 /* terminate assembly after phase */
108 #define ERR_FATAL 0x00000002 /* instantly fatal: exit with error */
109 #define ERR_PANIC 0x00000003 /* internal error: panic instantly
110 * and dump core for reference */
111 #define ERR_MASK 0x0000000F /* mask off the above codes */
112 #define ERR_NOFILE 0x00000010 /* don't give source file name/line */
113 #define ERR_USAGE 0x00000020 /* print a usage message */
114 #define ERR_PASS1 0x00000040 /* only print this error on pass one */
115 #define ERR_PASS2 0x00000080
116 #define ERR_NO_SEVERITY 0x00000100 /* suppress printing severity */
119 * These codes define specific types of suppressible warning.
122 #define ERR_WARN_MASK 0xFFFFF000 /* the mask for this feature */
123 #define ERR_WARN_SHR 12 /* how far to shift right */
125 #define WARN(x) ((x) << ERR_WARN_SHR)
127 #define ERR_WARN_MNP WARN( 1) /* macro-num-parameters warning */
128 #define ERR_WARN_MSR WARN( 2) /* macro self-reference */
129 #define ERR_WARN_MDP WARN( 3) /* macro default parameters check */
130 #define ERR_WARN_OL WARN( 4) /* orphan label (no colon, and
131 * alone on line) */
132 #define ERR_WARN_NOV WARN( 5) /* numeric overflow */
133 #define ERR_WARN_GNUELF WARN( 6) /* using GNU ELF extensions */
134 #define ERR_WARN_FL_OVERFLOW WARN( 7) /* FP overflow */
135 #define ERR_WARN_FL_DENORM WARN( 8) /* FP denormal */
136 #define ERR_WARN_FL_UNDERFLOW WARN( 9) /* FP underflow */
137 #define ERR_WARN_FL_TOOLONG WARN(10) /* FP too many digits */
138 #define ERR_WARN_USER WARN(11) /* %warning directives */
139 #define ERR_WARN_MAX 11 /* the highest numbered one */
142 * Wrappers around malloc, realloc and free. nasm_malloc will
143 * fatal-error and die rather than return NULL; nasm_realloc will
144 * do likewise, and will also guarantee to work right on being
145 * passed a NULL pointer; nasm_free will do nothing if it is passed
146 * a NULL pointer.
148 void nasm_set_malloc_error(efunc);
149 #ifndef LOGALLOC
150 void *nasm_malloc(size_t);
151 void *nasm_zalloc(size_t);
152 void *nasm_realloc(void *, size_t);
153 void nasm_free(void *);
154 char *nasm_strdup(const char *);
155 char *nasm_strndup(char *, size_t);
156 #else
157 void *nasm_malloc_log(char *, int, size_t);
158 void *nasm_zalloc_log(char *, int, size_t);
159 void *nasm_realloc_log(char *, int, void *, size_t);
160 void nasm_free_log(char *, int, void *);
161 char *nasm_strdup_log(char *, int, const char *);
162 char *nasm_strndup_log(char *, int, char *, size_t);
163 #define nasm_malloc(x) nasm_malloc_log(__FILE__,__LINE__,x)
164 #define nasm_zalloc(x) nasm_zalloc_log(__FILE__,__LINE__,x)
165 #define nasm_realloc(x,y) nasm_realloc_log(__FILE__,__LINE__,x,y)
166 #define nasm_free(x) nasm_free_log(__FILE__,__LINE__,x)
167 #define nasm_strdup(x) nasm_strdup_log(__FILE__,__LINE__,x)
168 #define nasm_strndup(x,y) nasm_strndup_log(__FILE__,__LINE__,x,y)
169 #endif
172 * ANSI doesn't guarantee the presence of `stricmp' or
173 * `strcasecmp'.
175 #if defined(HAVE_STRCASECMP)
176 #define nasm_stricmp strcasecmp
177 #elif defined(HAVE_STRICMP)
178 #define nasm_stricmp stricmp
179 #else
180 int nasm_stricmp(const char *, const char *);
181 #endif
183 #if defined(HAVE_STRNCASECMP)
184 #define nasm_strnicmp strncasecmp
185 #elif defined(HAVE_STRNICMP)
186 #define nasm_strnicmp strnicmp
187 #else
188 int nasm_strnicmp(const char *, const char *, size_t);
189 #endif
191 int nasm_memicmp(const char *, const char *, size_t);
193 #if defined(HAVE_STRSEP)
194 #define nasm_strsep strsep
195 #else
196 char *nasm_strsep(char **stringp, const char *delim);
197 #endif
201 * Convert a string into a number, using NASM number rules. Sets
202 * `*error' to true if an error occurs, and false otherwise.
204 int64_t readnum(char *str, bool *error);
207 * Convert a character constant into a number. Sets
208 * `*warn' to true if an overflow occurs, and false otherwise.
209 * str points to and length covers the middle of the string,
210 * without the quotes.
212 int64_t readstrnum(char *str, int length, bool *warn);
215 * seg_init: Initialise the segment-number allocator.
216 * seg_alloc: allocate a hitherto unused segment number.
218 void seg_init(void);
219 int32_t seg_alloc(void);
222 * many output formats will be able to make use of this: a standard
223 * function to add an extension to the name of the input file
225 #ifdef NASM_NASM_H
226 void standard_extension(char *inname, char *outname, char *extension,
227 efunc error);
228 #endif
231 * Utility macros...
233 * This is a useful #define which I keep meaning to use more often:
234 * the number of elements of a statically defined array.
237 #define elements(x) ( sizeof(x) / sizeof(*(x)) )
240 * some handy macros that will probably be of use in more than one
241 * output format: convert integers into little-endian byte packed
242 * format in memory
245 #if X86_MEMORY
247 #define WRITECHAR(p,v) \
248 do { \
249 *(uint8_t *)(p) = (v); \
250 (p) += 1; \
251 } while (0)
253 #define WRITESHORT(p,v) \
254 do { \
255 *(uint16_t *)(p) = (v); \
256 (p) += 2; \
257 } while (0)
259 #define WRITELONG(p,v) \
260 do { \
261 *(uint32_t *)(p) = (v); \
262 (p) += 4; \
263 } while (0)
265 #define WRITEDLONG(p,v) \
266 do { \
267 *(uint64_t *)(p) = (v); \
268 (p) += 8; \
269 } while (0)
271 #define WRITEADDR(p,v,s) \
272 do { \
273 uint64_t _wa_v = (v); \
274 memcpy((p), &_wa_v, (s)); \
275 (p) += (s); \
276 } while (0)
278 #else /* !X86_MEMORY */
280 #define WRITECHAR(p,v) \
281 do { \
282 uint8_t *_wc_p = (uint8_t *)(p); \
283 uint8_t _wc_v = (v); \
284 _wc_p[0] = _wc_v; \
285 (p) = (void *)(_wc_p + 1); \
286 } while (0)
288 #define WRITESHORT(p,v) \
289 do { \
290 uint8_t *_ws_p = (uint8_t *)(p); \
291 uint16_t _ws_v = (v); \
292 _ws_p[0] = _ws_v; \
293 _ws_p[1] = _ws_v >> 8; \
294 (p) = (void *)(_ws_p + 2); \
295 } while (0)
297 #define WRITELONG(p,v) \
298 do { \
299 uint8_t *_wl_p = (uint8_t *)(p); \
300 uint32_t _wl_v = (v); \
301 _wl_p[0] = _wl_v; \
302 _wl_p[1] = _wl_v >> 8; \
303 _wl_p[2] = _wl_v >> 16; \
304 _wl_p[3] = _wl_v >> 24; \
305 (p) = (void *)(_wl_p + 4); \
306 } while (0)
308 #define WRITEDLONG(p,v) \
309 do { \
310 uint8_t *_wq_p = (uint8_t *)(p); \
311 uint64_t _wq_v = (v); \
312 _wq_p[0] = _wq_v; \
313 _wq_p[1] = _wq_v >> 8; \
314 _wq_p[2] = _wq_v >> 16; \
315 _wq_p[3] = _wq_v >> 24; \
316 _wq_p[4] = _wq_v >> 32; \
317 _wq_p[5] = _wq_v >> 40; \
318 _wq_p[6] = _wq_v >> 48; \
319 _wq_p[7] = _wq_v >> 56; \
320 (p) = (void *)(_wq_p + 8); \
321 } while (0)
323 #define WRITEADDR(p,v,s) \
324 do { \
325 int _wa_s = (s); \
326 uint64_t _wa_v = (v); \
327 while (_wa_s--) { \
328 WRITECHAR(p,_wa_v); \
329 _wa_v >>= 8; \
331 } while(0)
333 #endif
336 * and routines to do the same thing to a file
338 #define fwriteint8_t(d,f) putc(d,f)
339 void fwriteint16_t(uint16_t data, FILE * fp);
340 void fwriteint32_t(uint32_t data, FILE * fp);
341 void fwriteint64_t(uint64_t data, FILE * fp);
342 void fwriteaddr(uint64_t data, int size, FILE * fp);
345 * Binary search routine. Returns index into `array' of an entry
346 * matching `string', or <0 if no match. `array' is taken to
347 * contain `size' elements.
349 * bsi() is case sensitive, bsii() is case insensitive.
351 int bsi(const char *string, const char **array, int size);
352 int bsii(const char *string, const char **array, int size);
354 char *src_set_fname(char *newname);
355 int32_t src_set_linnum(int32_t newline);
356 int32_t src_get_linnum(void);
358 * src_get may be used if you simply want to know the source file and line.
359 * It is also used if you maintain private status about the source location
360 * It return 0 if the information was the same as the last time you
361 * checked, -1 if the name changed and (new-old) if just the line changed.
363 int src_get(int32_t *xline, char **xname);
365 char *nasm_strcat(const char *one, const char *two);
367 const char *prefix_name(int);
369 #define ZERO_BUF_SIZE 4096
370 extern const uint8_t zero_buffer[ZERO_BUF_SIZE];
371 size_t fwritezero(size_t bytes, FILE *fp);
373 #endif