NASM 2.03rc7
[nasm/avx512.git] / nasmlib.h
blob7eb5c47f8cd0394ce7f5d8b41cacddb841dcc975
1 /* nasmlib.h header file for nasmlib.c
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the license given in the file "LICENSE"
6 * distributed in the NASM archive.
7 */
9 #ifndef NASM_NASMLIB_H
10 #define NASM_NASMLIB_H
12 #include "compiler.h"
14 #include <inttypes.h>
15 #include <stdio.h>
16 #include <string.h>
17 #ifdef HAVE_STRINGS_H
18 #include <strings.h>
19 #endif
22 * If this is defined, the wrappers around malloc et al will
23 * transform into logging variants, which will cause NASM to create
24 * a file called `malloc.log' when run, and spew details of all its
25 * memory management into that. That can then be analysed to detect
26 * memory leaks and potentially other problems too.
28 /* #define LOGALLOC */
31 * -------------------------
32 * Error reporting functions
33 * -------------------------
37 * An error reporting function should look like this.
39 typedef void (*efunc) (int severity, const char *fmt, ...);
42 * These are the error severity codes which get passed as the first
43 * argument to an efunc.
46 #define ERR_DEBUG 0x00000008 /* put out debugging message */
47 #define ERR_WARNING 0x00000000 /* warn only: no further action */
48 #define ERR_NONFATAL 0x00000001 /* terminate assembly after phase */
49 #define ERR_FATAL 0x00000002 /* instantly fatal: exit with error */
50 #define ERR_PANIC 0x00000003 /* internal error: panic instantly
51 * and dump core for reference */
52 #define ERR_MASK 0x0000000F /* mask off the above codes */
53 #define ERR_NOFILE 0x00000010 /* don't give source file name/line */
54 #define ERR_USAGE 0x00000020 /* print a usage message */
55 #define ERR_PASS1 0x00000040 /* only print this error on pass one */
58 * These codes define specific types of suppressible warning.
61 #define ERR_WARN_MASK 0x0000FF00 /* the mask for this feature */
62 #define ERR_WARN_SHR 8 /* how far to shift right */
64 #define WARN(x) ((x) << ERR_WARN_SHR)
66 #define ERR_WARN_MNP WARN(1) /* macro-num-parameters warning */
67 #define ERR_WARN_MSR WARN(2) /* macro self-reference */
68 #define ERR_WARN_OL WARN(3) /* orphan label (no colon, and
69 * alone on line) */
70 #define ERR_WARN_NOV WARN(4) /* numeric overflow */
71 #define ERR_WARN_GNUELF WARN(5) /* using GNU ELF extensions */
72 #define ERR_WARN_FL_OVERFLOW WARN(6) /* FP overflow */
73 #define ERR_WARN_FL_DENORM WARN(7) /* FP denormal */
74 #define ERR_WARN_FL_UNDERFLOW WARN(8) /* FP underflow */
75 #define ERR_WARN_FL_TOOLONG WARN(9) /* FP too many digits */
76 #define ERR_WARN_MAX 9 /* the highest numbered one */
79 * Wrappers around malloc, realloc and free. nasm_malloc will
80 * fatal-error and die rather than return NULL; nasm_realloc will
81 * do likewise, and will also guarantee to work right on being
82 * passed a NULL pointer; nasm_free will do nothing if it is passed
83 * a NULL pointer.
85 void nasm_set_malloc_error(efunc);
86 #ifndef LOGALLOC
87 void *nasm_malloc(size_t);
88 void *nasm_zalloc(size_t);
89 void *nasm_realloc(void *, size_t);
90 void nasm_free(void *);
91 char *nasm_strdup(const char *);
92 char *nasm_strndup(char *, size_t);
93 #else
94 void *nasm_malloc_log(char *, int, size_t);
95 void *nasm_zalloc_log(char *, int, size_t);
96 void *nasm_realloc_log(char *, int, void *, size_t);
97 void nasm_free_log(char *, int, void *);
98 char *nasm_strdup_log(char *, int, const char *);
99 char *nasm_strndup_log(char *, int, char *, size_t);
100 #define nasm_malloc(x) nasm_malloc_log(__FILE__,__LINE__,x)
101 #define nasm_zalloc(x) nasm_zalloc_log(__FILE__,__LINE__,x)
102 #define nasm_realloc(x,y) nasm_realloc_log(__FILE__,__LINE__,x,y)
103 #define nasm_free(x) nasm_free_log(__FILE__,__LINE__,x)
104 #define nasm_strdup(x) nasm_strdup_log(__FILE__,__LINE__,x)
105 #define nasm_strndup(x,y) nasm_strndup_log(__FILE__,__LINE__,x,y)
106 #endif
109 * ANSI doesn't guarantee the presence of `stricmp' or
110 * `strcasecmp'.
112 #if defined(HAVE_STRCASECMP)
113 #define nasm_stricmp strcasecmp
114 #elif defined(HAVE_STRICMP)
115 #define nasm_stricmp stricmp
116 #else
117 int nasm_stricmp(const char *, const char *);
118 #endif
120 #if defined(HAVE_STRNCASECMP)
121 #define nasm_strnicmp strncasecmp
122 #elif defined(HAVE_STRNICMP)
123 #define nasm_strnicmp strnicmp
124 #else
125 int nasm_strnicmp(const char *, const char *, size_t);
126 #endif
128 int nasm_memicmp(const char *, const char *, size_t);
130 #if defined(HAVE_STRSEP)
131 #define nasm_strsep strsep
132 #else
133 char *nasm_strsep(char **stringp, const char *delim);
134 #endif
138 * Convert a string into a number, using NASM number rules. Sets
139 * `*error' to true if an error occurs, and false otherwise.
141 int64_t readnum(char *str, bool *error);
144 * Convert a character constant into a number. Sets
145 * `*warn' to true if an overflow occurs, and false otherwise.
146 * str points to and length covers the middle of the string,
147 * without the quotes.
149 int64_t readstrnum(char *str, int length, bool *warn);
152 * seg_init: Initialise the segment-number allocator.
153 * seg_alloc: allocate a hitherto unused segment number.
155 void seg_init(void);
156 int32_t seg_alloc(void);
159 * many output formats will be able to make use of this: a standard
160 * function to add an extension to the name of the input file
162 #ifdef NASM_NASM_H
163 void standard_extension(char *inname, char *outname, char *extension,
164 efunc error);
165 #endif
168 * Utility macros...
170 * This is a useful #define which I keep meaning to use more often:
171 * the number of elements of a statically defined array.
174 #define elements(x) ( sizeof(x) / sizeof(*(x)) )
178 * some handy macros that will probably be of use in more than one
179 * output format: convert integers into little-endian byte packed
180 * format in memory
183 #if X86_MEMORY
185 #define WRITECHAR(p,v) \
186 do { \
187 *(uint8_t *)(p) = (v); \
188 (p) += 1; \
189 } while (0)
191 #define WRITESHORT(p,v) \
192 do { \
193 *(uint16_t *)(p) = (v); \
194 (p) += 2; \
195 } while (0)
197 #define WRITELONG(p,v) \
198 do { \
199 *(uint32_t *)(p) = (v); \
200 (p) += 4; \
201 } while (0)
203 #define WRITEDLONG(p,v) \
204 do { \
205 *(uint64_t *)(p) = (v); \
206 (p) += 8; \
207 } while (0)
209 #define WRITEADDR(p,v,s) \
210 do { \
211 uint64_t _wa_v = (v); \
212 memcpy((p), &_wa_v, (s)); \
213 (p) += (s); \
214 } while (0)
216 #else /* !X86_MEMORY */
218 #define WRITECHAR(p,v) \
219 do { \
220 uint8_t *_wc_p = (uint8_t *)(p); \
221 uint8_t _wc_v = (v); \
222 _wc_p[0] = _wc_v; \
223 (p) = (void *)(_wc_p + 1); \
224 } while (0)
226 #define WRITESHORT(p,v) \
227 do { \
228 uint8_t *_ws_p = (uint8_t *)(p); \
229 uint16_t _ws_v = (v); \
230 _ws_p[0] = _ws_v; \
231 _ws_p[1] = _ws_v >> 8; \
232 (p) = (void *)(_ws_p + 2); \
233 } while (0)
235 #define WRITELONG(p,v) \
236 do { \
237 uint8_t *_wl_p = (uint8_t *)(p); \
238 uint32_t _wl_v = (v); \
239 _wl_p[0] = _wl_v; \
240 _wl_p[1] = _wl_v >> 8; \
241 _wl_p[2] = _wl_v >> 16; \
242 _wl_p[3] = _wl_v >> 24; \
243 (p) = (void *)(_wl_p + 4); \
244 } while (0)
246 #define WRITEDLONG(p,v) \
247 do { \
248 uint8_t *_wq_p = (uint8_t *)(p); \
249 uint64_t _wq_v = (v); \
250 _wq_p[0] = _wq_v; \
251 _wq_p[1] = _wq_v >> 8; \
252 _wq_p[2] = _wq_v >> 16; \
253 _wq_p[3] = _wq_v >> 24; \
254 _wq_p[4] = _wq_v >> 32; \
255 _wq_p[5] = _wq_v >> 40; \
256 _wq_p[6] = _wq_v >> 48; \
257 _wq_p[7] = _wq_v >> 56; \
258 (p) = (void *)(_wq_p + 8); \
259 } while (0)
261 #define WRITEADDR(p,v,s) \
262 do { \
263 int _wa_s = (s); \
264 uint64_t _wa_v = (v); \
265 while (_wa_s--) { \
266 WRITECHAR(p,_wa_v); \
267 _wa_v >>= 8; \
269 } while(0)
271 #endif
274 * and routines to do the same thing to a file
276 #define fwriteint8_t(d,f) putc(d,f)
277 void fwriteint16_t(uint16_t data, FILE * fp);
278 void fwriteint32_t(uint32_t data, FILE * fp);
279 void fwriteint64_t(uint64_t data, FILE * fp);
280 void fwriteaddr(uint64_t data, int size, FILE * fp);
283 * Routines to manage a dynamic random access array of int64_ts which
284 * may grow in size to be more than the largest single malloc'able
285 * chunk.
288 #define RAA_BLKSHIFT 15 /* 2**this many longs allocated at once */
289 #define RAA_BLKSIZE (1 << RAA_BLKSHIFT)
290 #define RAA_LAYERSHIFT 15 /* 2**this many _pointers_ allocated */
291 #define RAA_LAYERSIZE (1 << RAA_LAYERSHIFT)
293 typedef struct RAA RAA;
294 typedef union RAA_UNION RAA_UNION;
295 typedef struct RAA_LEAF RAA_LEAF;
296 typedef struct RAA_BRANCH RAA_BRANCH;
298 struct RAA {
300 * Number of layers below this one to get to the real data. 0
301 * means this structure is a leaf, holding RAA_BLKSIZE real
302 * data items; 1 and above mean it's a branch, holding
303 * RAA_LAYERSIZE pointers to the next level branch or leaf
304 * structures.
306 int layers;
309 * Number of real data items spanned by one position in the
310 * `data' array at this level. This number is 0 trivially, for
311 * a leaf (level 0): for a level 1 branch it should be
312 * RAA_BLKSHIFT, and for a level 2 branch it's
313 * RAA_LAYERSHIFT+RAA_BLKSHIFT.
315 int shift;
317 union RAA_UNION {
318 struct RAA_LEAF {
319 int64_t data[RAA_BLKSIZE];
320 } l;
321 struct RAA_BRANCH {
322 struct RAA *data[RAA_LAYERSIZE];
323 } b;
324 } u;
327 struct RAA *raa_init(void);
328 void raa_free(struct RAA *);
329 int64_t raa_read(struct RAA *, int32_t);
330 struct RAA *raa_write(struct RAA *r, int32_t posn, int64_t value);
333 * Routines to manage a dynamic sequential-access array, under the
334 * same restriction on maximum mallocable block. This array may be
335 * written to in two ways: a contiguous chunk can be reserved of a
336 * given size with a pointer returned OR single-byte data may be
337 * written. The array can also be read back in the same two ways:
338 * as a series of big byte-data blocks or as a list of structures
339 * of a given size.
342 struct SAA {
344 * members `end' and `elem_len' are only valid in first link in
345 * list; `rptr' and `rpos' are used for reading
347 size_t elem_len; /* Size of each element */
348 size_t blk_len; /* Size of each allocation block */
349 size_t nblks; /* Total number of allocated blocks */
350 size_t nblkptrs; /* Total number of allocation block pointers */
351 size_t length; /* Total allocated length of the array */
352 size_t datalen; /* Total data length of the array */
353 char **wblk; /* Write block pointer */
354 size_t wpos; /* Write position inside block */
355 size_t wptr; /* Absolute write position */
356 char **rblk; /* Read block pointer */
357 size_t rpos; /* Read position inside block */
358 size_t rptr; /* Absolute read position */
359 char **blk_ptrs; /* Pointer to pointer blocks */
362 struct SAA *saa_init(size_t elem_len); /* 1 == byte */
363 void saa_free(struct SAA *);
364 void *saa_wstruct(struct SAA *); /* return a structure of elem_len */
365 void saa_wbytes(struct SAA *, const void *, size_t); /* write arbitrary bytes */
366 void saa_wleb128u(struct SAA *, int); /* write unsigned LEB128 value */
367 void saa_wleb128s(struct SAA *, int); /* write signed LEB128 value */
368 void saa_rewind(struct SAA *); /* for reading from beginning */
369 void *saa_rstruct(struct SAA *); /* return NULL on EOA */
370 const void *saa_rbytes(struct SAA *, size_t *); /* return 0 on EOA */
371 void saa_rnbytes(struct SAA *, void *, size_t); /* read a given no. of bytes */
372 /* random access */
373 void saa_fread(struct SAA *, size_t, void *, size_t);
374 void saa_fwrite(struct SAA *, size_t, const void *, size_t);
376 /* dump to file */
377 void saa_fpwrite(struct SAA *, FILE *);
380 * Binary search routine. Returns index into `array' of an entry
381 * matching `string', or <0 if no match. `array' is taken to
382 * contain `size' elements.
384 * bsi() is case sensitive, bsii() is case insensitive.
386 int bsi(const char *string, const char **array, int size);
387 int bsii(const char *string, const char **array, int size);
389 char *src_set_fname(char *newname);
390 int32_t src_set_linnum(int32_t newline);
391 int32_t src_get_linnum(void);
393 * src_get may be used if you simply want to know the source file and line.
394 * It is also used if you maintain private status about the source location
395 * It return 0 if the information was the same as the last time you
396 * checked, -1 if the name changed and (new-old) if just the line changed.
398 int src_get(int32_t *xline, char **xname);
400 char *nasm_strcat(char *one, char *two);
402 void null_debug_routine(const char *directive, const char *params);
403 extern struct dfmt null_debug_form;
404 extern struct dfmt *null_debug_arr[2];
406 const char *prefix_name(int);
408 #endif