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