Merge branch 'new-preproc'
[platform/upstream/nasm.git] / nasmlib.h
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.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following
9  *   conditions are met:
10  *
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.
17  *     
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.
31  *
32  * ----------------------------------------------------------------------- */
33
34 /* 
35  * nasmlib.h    header file for nasmlib.c
36  */
37
38 #ifndef NASM_NASMLIB_H
39 #define NASM_NASMLIB_H
40
41 #include "compiler.h"
42
43 #include <inttypes.h>
44 #include <stdio.h>
45 #include <string.h>
46 #ifdef HAVE_STRINGS_H
47 #include <strings.h>
48 #endif
49
50 /*
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.
54  */
55 void tolower_init(void);
56 extern unsigned char nasm_tolower_tab[256];
57 #define nasm_tolower(x) nasm_tolower_tab[(unsigned char)(x)]
58
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))
66
67 /*
68  * If this is defined, the wrappers around malloc et al will
69  * transform into logging variants, which will cause NASM to create
70  * a file called `malloc.log' when run, and spew details of all its
71  * memory management into that. That can then be analysed to detect
72  * memory leaks and potentially other problems too.
73  */
74 /* #define LOGALLOC */
75
76 /*
77  * -------------------------
78  * Error reporting functions
79  * -------------------------
80  */
81
82 /*
83  * An error reporting function should look like this.
84  */
85 typedef void (*efunc) (int severity, const char *fmt, ...);
86 typedef void (*vefunc) (int severity, const char *fmt, va_list ap);
87 void nasm_error(int severity, const char *fmt, ...);
88 void nasm_set_verror(vefunc);
89
90 /*
91  * These are the error severity codes which get passed as the first
92  * argument to an efunc.
93  */
94
95 #define ERR_DEBUG       0x00000008      /* put out debugging message */
96 #define ERR_WARNING     0x00000000      /* warn only: no further action */
97 #define ERR_NONFATAL    0x00000001      /* terminate assembly after phase */
98 #define ERR_FATAL       0x00000002      /* instantly fatal: exit with error */
99 #define ERR_PANIC       0x00000003      /* internal error: panic instantly
100                                          * and dump core for reference */
101 #define ERR_MASK        0x0000000F      /* mask off the above codes */
102 #define ERR_NOFILE      0x00000010      /* don't give source file name/line */
103 #define ERR_USAGE       0x00000020      /* print a usage message */
104 #define ERR_PASS1       0x00000040      /* only print this error on pass one */
105 #define ERR_PASS2       0x00000080
106 #define ERR_NO_SEVERITY 0x00000100      /* suppress printing severity */
107
108 /*
109  * These codes define specific types of suppressible warning.
110  */
111
112 #define ERR_WARN_MASK   0xFFFFF000      /* the mask for this feature */
113 #define ERR_WARN_SHR    12               /* how far to shift right */
114
115 #define WARN(x) ((x) << ERR_WARN_SHR)
116
117 #define ERR_WARN_MNP            WARN( 1) /* macro-num-parameters warning */
118 #define ERR_WARN_MSR            WARN( 2) /* macro self-reference */
119 #define ERR_WARN_MDP            WARN( 3) /* macro default parameters check */
120 #define ERR_WARN_OL             WARN( 4) /* orphan label (no colon, and
121                                           * alone on line) */
122 #define ERR_WARN_NOV            WARN( 5) /* numeric overflow */
123 #define ERR_WARN_GNUELF         WARN( 6) /* using GNU ELF extensions */
124 #define ERR_WARN_FL_OVERFLOW    WARN( 7) /* FP overflow */
125 #define ERR_WARN_FL_DENORM      WARN( 8) /* FP denormal */
126 #define ERR_WARN_FL_UNDERFLOW   WARN( 9) /* FP underflow */
127 #define ERR_WARN_FL_TOOLONG     WARN(10) /* FP too many digits */
128 #define ERR_WARN_USER           WARN(11) /* %warning directives */
129 #define ERR_WARN_MAX            11       /* the highest numbered one */
130
131 /*
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.
137  */
138 void nasm_init_malloc_error(void);
139 #ifndef LOGALLOC
140 void *nasm_malloc(size_t);
141 void *nasm_zalloc(size_t);
142 void *nasm_realloc(void *, size_t);
143 void nasm_free(void *);
144 char *nasm_strdup(const char *);
145 char *nasm_strndup(const char *, size_t);
146 #else
147 void *nasm_malloc_log(const char *, int, size_t);
148 void *nasm_zalloc_log(const char *, int, size_t);
149 void *nasm_realloc_log(const char *, int, void *, size_t);
150 void nasm_free_log(const char *, int, void *);
151 char *nasm_strdup_log(const char *, int, const char *);
152 char *nasm_strndup_log(const char *, int, const char *, size_t);
153 #define nasm_malloc(x) nasm_malloc_log(__FILE__,__LINE__,x)
154 #define nasm_zalloc(x) nasm_zalloc_log(__FILE__,__LINE__,x)
155 #define nasm_realloc(x,y) nasm_realloc_log(__FILE__,__LINE__,x,y)
156 #define nasm_free(x) nasm_free_log(__FILE__,__LINE__,x)
157 #define nasm_strdup(x) nasm_strdup_log(__FILE__,__LINE__,x)
158 #define nasm_strndup(x,y) nasm_strndup_log(__FILE__,__LINE__,x,y)
159 #endif
160
161 /*
162  * NASM assert failure
163  */
164 no_return nasm_assert_failed(const char *, int, const char *);
165 #define nasm_assert(x)                                          \
166     do {                                                        \
167         if (unlikely(!(x)))                                     \
168             nasm_assert_failed(__FILE__,__LINE__,#x);           \
169     } while (0)
170
171 /*
172  * ANSI doesn't guarantee the presence of `stricmp' or
173  * `strcasecmp'.
174  */
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
182
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
190
191 int nasm_memicmp(const char *, const char *, size_t);
192
193 #if defined(HAVE_STRSEP)
194 #define nasm_strsep strsep
195 #else
196 char *nasm_strsep(char **stringp, const char *delim);
197 #endif
198
199
200 /*
201  * Convert a string into a number, using NASM number rules. Sets
202  * `*error' to true if an error occurs, and false otherwise.
203  */
204 int64_t readnum(char *str, bool *error);
205
206 /*
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.
211  */
212 int64_t readstrnum(char *str, int length, bool *warn);
213
214 /*
215  * seg_init: Initialise the segment-number allocator.
216  * seg_alloc: allocate a hitherto unused segment number.
217  */
218 void seg_init(void);
219 int32_t seg_alloc(void);
220
221 /*
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
224  */
225 void standard_extension(char *inname, char *outname, char *extension);
226
227 /*
228  * Utility macros...
229  *
230  * This is a useful #define which I keep meaning to use more often:
231  * the number of elements of a statically defined array.
232  */
233
234 #define elements(x)     ( sizeof(x) / sizeof(*(x)) )
235
236 /*
237  * some handy macros that will probably be of use in more than one
238  * output format: convert integers into little-endian byte packed
239  * format in memory
240  */
241
242 #if X86_MEMORY
243
244 #define WRITECHAR(p,v)                          \
245     do {                                        \
246         *(uint8_t *)(p) = (v);                  \
247         (p) += 1;                               \
248     } while (0)
249
250 #define WRITESHORT(p,v)                         \
251     do {                                        \
252         *(uint16_t *)(p) = (v);                 \
253         (p) += 2;                               \
254     } while (0)
255
256 #define WRITELONG(p,v)                          \
257     do {                                        \
258         *(uint32_t *)(p) = (v);                 \
259         (p) += 4;                               \
260     } while (0)
261
262 #define WRITEDLONG(p,v)                         \
263     do {                                        \
264         *(uint64_t *)(p) = (v);                 \
265         (p) += 8;                               \
266     } while (0)
267
268 #define WRITEADDR(p,v,s)                        \
269     do {                                        \
270         uint64_t _wa_v = (v);                   \
271         memcpy((p), &_wa_v, (s));               \
272         (p) += (s);                             \
273     } while (0)
274
275 #else /* !X86_MEMORY */
276
277 #define WRITECHAR(p,v)                          \
278     do {                                        \
279         uint8_t *_wc_p = (uint8_t *)(p);        \
280         uint8_t _wc_v = (v);                    \
281         _wc_p[0] = _wc_v;                       \
282         (p) = (void *)(_wc_p + 1);              \
283     } while (0)
284
285 #define WRITESHORT(p,v)                         \
286     do {                                        \
287         uint8_t *_ws_p = (uint8_t *)(p);        \
288         uint16_t _ws_v = (v);                   \
289         _ws_p[0] = _ws_v;                       \
290         _ws_p[1] = _ws_v >> 8;                  \
291         (p) = (void *)(_ws_p + 2);              \
292     } while (0)
293
294 #define WRITELONG(p,v)                          \
295     do {                                        \
296         uint8_t *_wl_p = (uint8_t *)(p);        \
297         uint32_t _wl_v = (v);                   \
298         _wl_p[0] = _wl_v;                       \
299         _wl_p[1] = _wl_v >> 8;                  \
300         _wl_p[2] = _wl_v >> 16;                 \
301         _wl_p[3] = _wl_v >> 24;                 \
302         (p) = (void *)(_wl_p + 4);              \
303     } while (0)
304
305 #define WRITEDLONG(p,v)                         \
306     do {                                        \
307         uint8_t *_wq_p = (uint8_t *)(p);        \
308         uint64_t _wq_v = (v);                   \
309         _wq_p[0] = _wq_v;                       \
310         _wq_p[1] = _wq_v >> 8;                  \
311         _wq_p[2] = _wq_v >> 16;                 \
312         _wq_p[3] = _wq_v >> 24;                 \
313         _wq_p[4] = _wq_v >> 32;                 \
314         _wq_p[5] = _wq_v >> 40;                 \
315         _wq_p[6] = _wq_v >> 48;                 \
316         _wq_p[7] = _wq_v >> 56;                 \
317         (p) = (void *)(_wq_p + 8);              \
318     } while (0)
319
320 #define WRITEADDR(p,v,s)                        \
321     do {                                        \
322         int _wa_s = (s);                        \
323         uint64_t _wa_v = (v);                   \
324         while (_wa_s--) {                       \
325             WRITECHAR(p,_wa_v);                 \
326             _wa_v >>= 8;                        \
327         }                                       \
328     } while(0)
329
330 #endif
331
332 /*
333  * and routines to do the same thing to a file
334  */
335 #define fwriteint8_t(d,f) putc(d,f)
336 void fwriteint16_t(uint16_t data, FILE * fp);
337 void fwriteint32_t(uint32_t data, FILE * fp);
338 void fwriteint64_t(uint64_t data, FILE * fp);
339 void fwriteaddr(uint64_t data, int size, FILE * fp);
340
341 /*
342  * Binary search routine. Returns index into `array' of an entry
343  * matching `string', or <0 if no match. `array' is taken to
344  * contain `size' elements.
345  *
346  * bsi() is case sensitive, bsii() is case insensitive.
347  */
348 int bsi(const char *string, const char **array, int size);
349 int bsii(const char *string, const char **array, int size);
350
351 char *src_set_fname(char *newname);
352 int32_t src_set_linnum(int32_t newline);
353 int32_t src_get_linnum(void);
354 /*
355  * src_get may be used if you simply want to know the source file and line.
356  * It is also used if you maintain private status about the source location
357  * It return 0 if the information was the same as the last time you
358  * checked, -1 if the name changed and (new-old) if just the line changed.
359  */
360 int src_get(int32_t *xline, char **xname);
361
362 char *nasm_strcat(const char *one, const char *two);
363
364 const char *prefix_name(int);
365
366 #define ZERO_BUF_SIZE   4096
367 extern const uint8_t zero_buffer[ZERO_BUF_SIZE];
368 size_t fwritezero(size_t bytes, FILE *fp);
369
370 #endif