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