Add %warning, saner unquoting of %error
[platform/upstream/nasm.git] / nasmlib.h
1 /* nasmlib.h    header file for nasmlib.c
2  *
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  */
8
9 #ifndef NASM_NASMLIB_H
10 #define NASM_NASMLIB_H
11
12 #include "compiler.h"
13
14 #include <inttypes.h>
15 #include <stdio.h>
16 #include <string.h>
17 #ifdef HAVE_STRINGS_H
18 #include <strings.h>
19 #endif
20
21 /*
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.
27  */
28 /* #define LOGALLOC */
29
30 /*
31  * -------------------------
32  * Error reporting functions
33  * -------------------------
34  */
35
36 /*
37  * An error reporting function should look like this.
38  */
39 typedef void (*efunc) (int severity, const char *fmt, ...);
40 extern efunc nasm_malloc_error;
41
42 /*
43  * These are the error severity codes which get passed as the first
44  * argument to an efunc.
45  */
46
47 #define ERR_DEBUG       0x00000008      /* put out debugging message */
48 #define ERR_WARNING     0x00000000      /* warn only: no further action */
49 #define ERR_NONFATAL    0x00000001      /* terminate assembly after phase */
50 #define ERR_FATAL       0x00000002      /* instantly fatal: exit with error */
51 #define ERR_PANIC       0x00000003      /* internal error: panic instantly
52                                          * and dump core for reference */
53 #define ERR_MASK        0x0000000F      /* mask off the above codes */
54 #define ERR_NOFILE      0x00000010      /* don't give source file name/line */
55 #define ERR_USAGE       0x00000020      /* print a usage message */
56 #define ERR_PASS1       0x00000040      /* only print this error on pass one */
57 #define ERR_NO_SEVERITY 0x00000080      /* suppress printing severity */
58
59 /*
60  * These codes define specific types of suppressible warning.
61  */
62
63 #define ERR_WARN_MASK   0x0000FF00      /* the mask for this feature */
64 #define ERR_WARN_SHR  8                 /* how far to shift right */
65
66 #define WARN(x) ((x) << ERR_WARN_SHR)
67
68 #define ERR_WARN_MNP            WARN(1) /* macro-num-parameters warning */
69 #define ERR_WARN_MSR            WARN(2) /* macro self-reference */
70 #define ERR_WARN_OL             WARN(3) /* orphan label (no colon, and
71                                          * alone on line) */
72 #define ERR_WARN_NOV            WARN(4) /* numeric overflow */
73 #define ERR_WARN_GNUELF         WARN(5) /* using GNU ELF extensions */
74 #define ERR_WARN_FL_OVERFLOW    WARN(6) /* FP overflow */
75 #define ERR_WARN_FL_DENORM      WARN(7) /* FP denormal */
76 #define ERR_WARN_FL_UNDERFLOW   WARN(8) /* FP underflow */
77 #define ERR_WARN_FL_TOOLONG     WARN(9) /* FP too many digits */
78 #define ERR_WARN_MAX    9               /* the highest numbered one */
79
80 /*
81  * Wrappers around malloc, realloc and free. nasm_malloc will
82  * fatal-error and die rather than return NULL; nasm_realloc will
83  * do likewise, and will also guarantee to work right on being
84  * passed a NULL pointer; nasm_free will do nothing if it is passed
85  * a NULL pointer.
86  */
87 void nasm_set_malloc_error(efunc);
88 #ifndef LOGALLOC
89 void *nasm_malloc(size_t);
90 void *nasm_zalloc(size_t);
91 void *nasm_realloc(void *, size_t);
92 void nasm_free(void *);
93 char *nasm_strdup(const char *);
94 char *nasm_strndup(char *, size_t);
95 #else
96 void *nasm_malloc_log(char *, int, size_t);
97 void *nasm_zalloc_log(char *, int, size_t);
98 void *nasm_realloc_log(char *, int, void *, size_t);
99 void nasm_free_log(char *, int, void *);
100 char *nasm_strdup_log(char *, int, const char *);
101 char *nasm_strndup_log(char *, int, char *, size_t);
102 #define nasm_malloc(x) nasm_malloc_log(__FILE__,__LINE__,x)
103 #define nasm_zalloc(x) nasm_zalloc_log(__FILE__,__LINE__,x)
104 #define nasm_realloc(x,y) nasm_realloc_log(__FILE__,__LINE__,x,y)
105 #define nasm_free(x) nasm_free_log(__FILE__,__LINE__,x)
106 #define nasm_strdup(x) nasm_strdup_log(__FILE__,__LINE__,x)
107 #define nasm_strndup(x,y) nasm_strndup_log(__FILE__,__LINE__,x,y)
108 #endif
109
110 /*
111  * ANSI doesn't guarantee the presence of `stricmp' or
112  * `strcasecmp'.
113  */
114 #if defined(HAVE_STRCASECMP)
115 #define nasm_stricmp strcasecmp
116 #elif defined(HAVE_STRICMP)
117 #define nasm_stricmp stricmp
118 #else
119 int nasm_stricmp(const char *, const char *);
120 #endif
121
122 #if defined(HAVE_STRNCASECMP)
123 #define nasm_strnicmp strncasecmp
124 #elif defined(HAVE_STRNICMP)
125 #define nasm_strnicmp strnicmp
126 #else
127 int nasm_strnicmp(const char *, const char *, size_t);
128 #endif
129
130 int nasm_memicmp(const char *, const char *, size_t);
131
132 #if defined(HAVE_STRSEP)
133 #define nasm_strsep strsep
134 #else
135 char *nasm_strsep(char **stringp, const char *delim);
136 #endif
137
138
139 /*
140  * Convert a string into a number, using NASM number rules. Sets
141  * `*error' to true if an error occurs, and false otherwise.
142  */
143 int64_t readnum(char *str, bool *error);
144
145 /*
146  * Convert a character constant into a number. Sets
147  * `*warn' to true if an overflow occurs, and false otherwise.
148  * str points to and length covers the middle of the string,
149  * without the quotes.
150  */
151 int64_t readstrnum(char *str, int length, bool *warn);
152
153 /*
154  * seg_init: Initialise the segment-number allocator.
155  * seg_alloc: allocate a hitherto unused segment number.
156  */
157 void seg_init(void);
158 int32_t seg_alloc(void);
159
160 /*
161  * many output formats will be able to make use of this: a standard
162  * function to add an extension to the name of the input file
163  */
164 #ifdef NASM_NASM_H
165 void standard_extension(char *inname, char *outname, char *extension,
166                         efunc error);
167 #endif
168
169 /*
170  * Utility macros...
171  *
172  * This is a useful #define which I keep meaning to use more often:
173  * the number of elements of a statically defined array.
174  */
175
176 #define elements(x)     ( sizeof(x) / sizeof(*(x)) )
177
178
179 /*
180  * some handy macros that will probably be of use in more than one
181  * output format: convert integers into little-endian byte packed
182  * format in memory
183  */
184
185 #if X86_MEMORY
186
187 #define WRITECHAR(p,v)                          \
188     do {                                        \
189         *(uint8_t *)(p) = (v);                  \
190         (p) += 1;                               \
191     } while (0)
192
193 #define WRITESHORT(p,v)                         \
194     do {                                        \
195         *(uint16_t *)(p) = (v);                 \
196         (p) += 2;                               \
197     } while (0)
198
199 #define WRITELONG(p,v)                          \
200     do {                                        \
201         *(uint32_t *)(p) = (v);                 \
202         (p) += 4;                               \
203     } while (0)
204
205 #define WRITEDLONG(p,v)                         \
206     do {                                        \
207         *(uint64_t *)(p) = (v);                 \
208         (p) += 8;                               \
209     } while (0)
210
211 #define WRITEADDR(p,v,s)                        \
212     do {                                        \
213         uint64_t _wa_v = (v);                   \
214         memcpy((p), &_wa_v, (s));               \
215         (p) += (s);                             \
216     } while (0)
217
218 #else /* !X86_MEMORY */
219
220 #define WRITECHAR(p,v)                          \
221     do {                                        \
222         uint8_t *_wc_p = (uint8_t *)(p);        \
223         uint8_t _wc_v = (v);                    \
224         _wc_p[0] = _wc_v;                       \
225         (p) = (void *)(_wc_p + 1);              \
226     } while (0)
227
228 #define WRITESHORT(p,v)                         \
229     do {                                        \
230         uint8_t *_ws_p = (uint8_t *)(p);        \
231         uint16_t _ws_v = (v);                   \
232         _ws_p[0] = _ws_v;                       \
233         _ws_p[1] = _ws_v >> 8;                  \
234         (p) = (void *)(_ws_p + 2);              \
235     } while (0)
236
237 #define WRITELONG(p,v)                          \
238     do {                                        \
239         uint8_t *_wl_p = (uint8_t *)(p);        \
240         uint32_t _wl_v = (v);                   \
241         _wl_p[0] = _wl_v;                       \
242         _wl_p[1] = _wl_v >> 8;                  \
243         _wl_p[2] = _wl_v >> 16;                 \
244         _wl_p[3] = _wl_v >> 24;                 \
245         (p) = (void *)(_wl_p + 4);              \
246     } while (0)
247
248 #define WRITEDLONG(p,v)                         \
249     do {                                        \
250         uint8_t *_wq_p = (uint8_t *)(p);        \
251         uint64_t _wq_v = (v);                   \
252         _wq_p[0] = _wq_v;                       \
253         _wq_p[1] = _wq_v >> 8;                  \
254         _wq_p[2] = _wq_v >> 16;                 \
255         _wq_p[3] = _wq_v >> 24;                 \
256         _wq_p[4] = _wq_v >> 32;                 \
257         _wq_p[5] = _wq_v >> 40;                 \
258         _wq_p[6] = _wq_v >> 48;                 \
259         _wq_p[7] = _wq_v >> 56;                 \
260         (p) = (void *)(_wq_p + 8);              \
261     } while (0)
262
263 #define WRITEADDR(p,v,s)                        \
264     do {                                        \
265         int _wa_s = (s);                        \
266         uint64_t _wa_v = (v);                   \
267         while (_wa_s--) {                       \
268             WRITECHAR(p,_wa_v);                 \
269             _wa_v >>= 8;                        \
270         }                                       \
271     } while(0)
272
273 #endif
274
275 /*
276  * and routines to do the same thing to a file
277  */
278 #define fwriteint8_t(d,f) putc(d,f)
279 void fwriteint16_t(uint16_t data, FILE * fp);
280 void fwriteint32_t(uint32_t data, FILE * fp);
281 void fwriteint64_t(uint64_t data, FILE * fp);
282 void fwriteaddr(uint64_t data, int size, FILE * fp);
283
284 /*
285  * Binary search routine. Returns index into `array' of an entry
286  * matching `string', or <0 if no match. `array' is taken to
287  * contain `size' elements.
288  *
289  * bsi() is case sensitive, bsii() is case insensitive.
290  */
291 int bsi(const char *string, const char **array, int size);
292 int bsii(const char *string, const char **array, int size);
293
294 char *src_set_fname(char *newname);
295 int32_t src_set_linnum(int32_t newline);
296 int32_t src_get_linnum(void);
297 /*
298  * src_get may be used if you simply want to know the source file and line.
299  * It is also used if you maintain private status about the source location
300  * It return 0 if the information was the same as the last time you
301  * checked, -1 if the name changed and (new-old) if just the line changed.
302  */
303 int src_get(int32_t *xline, char **xname);
304
305 char *nasm_strcat(char *one, char *two);
306
307 void null_debug_routine(const char *directive, const char *params);
308 extern struct dfmt null_debug_form;
309 extern struct dfmt *null_debug_arr[2];
310
311 const char *prefix_name(int);
312
313 #endif