Make nasm_malloc() et al available from inside ndisasm
[nasm.git] / nasmlib.h
blob43342096435588e8bf03cffbf3081b67dce4866f
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 licence given in the file "Licence"
6 * distributed in the NASM archive.
7 */
9 #ifndef NASM_NASMLIB_H
10 #define NASM_NASMLIB_H
12 #include <inttypes.h>
13 #include <stdio.h>
14 #include "compiler.h"
17 * If this is defined, the wrappers around malloc et al will
18 * transform into logging variants, which will cause NASM to create
19 * a file called `malloc.log' when run, and spew details of all its
20 * memory management into that. That can then be analysed to detect
21 * memory leaks and potentially other problems too.
23 /* #define LOGALLOC */
26 * -------------------------
27 * Error reporting functions
28 * -------------------------
32 * An error reporting function should look like this.
34 typedef void (*efunc) (int severity, const char *fmt, ...);
37 * These are the error severity codes which get passed as the first
38 * argument to an efunc.
41 #define ERR_DEBUG 0x00000008 /* put out debugging message */
42 #define ERR_WARNING 0x00000000 /* warn only: no further action */
43 #define ERR_NONFATAL 0x00000001 /* terminate assembly after phase */
44 #define ERR_FATAL 0x00000002 /* instantly fatal: exit with error */
45 #define ERR_PANIC 0x00000003 /* internal error: panic instantly
46 * and dump core for reference */
47 #define ERR_MASK 0x0000000F /* mask off the above codes */
48 #define ERR_NOFILE 0x00000010 /* don't give source file name/line */
49 #define ERR_USAGE 0x00000020 /* print a usage message */
50 #define ERR_PASS1 0x00000040 /* only print this error on pass one */
53 * These codes define specific types of suppressible warning.
56 #define ERR_WARN_MASK 0x0000FF00 /* the mask for this feature */
57 #define ERR_WARN_SHR 8 /* how far to shift right */
59 #define ERR_WARN_MNP 0x00000100 /* macro-num-parameters warning */
60 #define ERR_WARN_MSR 0x00000200 /* macro self-reference */
61 #define ERR_WARN_OL 0x00000300 /* orphan label (no colon, and
62 * alone on line) */
63 #define ERR_WARN_NOV 0x00000400 /* numeric overflow */
64 #define ERR_WARN_GNUELF 0x00000500 /* using GNU ELF extensions */
65 #define ERR_WARN_MAX 5 /* the highest numbered one */
68 * Wrappers around malloc, realloc and free. nasm_malloc will
69 * fatal-error and die rather than return NULL; nasm_realloc will
70 * do likewise, and will also guarantee to work right on being
71 * passed a NULL pointer; nasm_free will do nothing if it is passed
72 * a NULL pointer.
74 void nasm_set_malloc_error(efunc);
75 #ifndef LOGALLOC
76 void *nasm_malloc(size_t);
77 void *nasm_realloc(void *, size_t);
78 void nasm_free(void *);
79 char *nasm_strdup(const char *);
80 char *nasm_strndup(char *, size_t);
81 #else
82 void *nasm_malloc_log(char *, int, size_t);
83 void *nasm_realloc_log(char *, int, void *, size_t);
84 void nasm_free_log(char *, int, void *);
85 char *nasm_strdup_log(char *, int, const char *);
86 char *nasm_strndup_log(char *, int, char *, size_t);
87 #define nasm_malloc(x) nasm_malloc_log(__FILE__,__LINE__,x)
88 #define nasm_realloc(x,y) nasm_realloc_log(__FILE__,__LINE__,x,y)
89 #define nasm_free(x) nasm_free_log(__FILE__,__LINE__,x)
90 #define nasm_strdup(x) nasm_strdup_log(__FILE__,__LINE__,x)
91 #define nasm_strndup(x,y) nasm_strndup_log(__FILE__,__LINE__,x,y)
92 #endif
95 * ANSI doesn't guarantee the presence of `stricmp' or
96 * `strcasecmp'.
98 #if defined(stricmp) || defined(strcasecmp)
99 #if defined(stricmp)
100 #define nasm_stricmp stricmp
101 #else
102 #define nasm_stricmp strcasecmp
103 #endif
104 #else
105 int nasm_stricmp(const char *, const char *);
106 #endif
108 #if defined(strnicmp) || defined(strncasecmp)
109 #if defined(strnicmp)
110 #define nasm_strnicmp strnicmp
111 #else
112 #define nasm_strnicmp strncasecmp
113 #endif
114 #else
115 int nasm_strnicmp(const char *, const char *, int);
116 #endif
118 #if defined(strsep)
119 #define nasm_strsep strsep
120 #else
121 char *nasm_strsep(char **stringp, const char *delim);
122 #endif
126 * Convert a string into a number, using NASM number rules. Sets
127 * `*error' to TRUE if an error occurs, and FALSE otherwise.
129 int64_t readnum(char *str, int *error);
132 * Convert a character constant into a number. Sets
133 * `*warn' to TRUE if an overflow occurs, and FALSE otherwise.
134 * str points to and length covers the middle of the string,
135 * without the quotes.
137 int64_t readstrnum(char *str, int length, int *warn);
140 * seg_init: Initialise the segment-number allocator.
141 * seg_alloc: allocate a hitherto unused segment number.
143 void seg_init(void);
144 int32_t seg_alloc(void);
147 * many output formats will be able to make use of this: a standard
148 * function to add an extension to the name of the input file
150 #ifdef NASM_NASM_H
151 void standard_extension(char *inname, char *outname, char *extension,
152 efunc error);
153 #endif
156 * some handy macros that will probably be of use in more than one
157 * output format: convert integers into little-endian byte packed
158 * format in memory
161 #define WRITECHAR(p,v) \
162 do { \
163 *(p)++ = (v) & 0xFF; \
164 } while (0)
166 #define WRITESHORT(p,v) \
167 do { \
168 WRITECHAR(p,v); \
169 WRITECHAR(p,(v) >> 8); \
170 } while (0)
172 #define WRITELONG(p,v) \
173 do { \
174 WRITECHAR(p,v); \
175 WRITECHAR(p,(v) >> 8); \
176 WRITECHAR(p,(v) >> 16); \
177 WRITECHAR(p,(v) >> 24); \
178 } while (0)
180 #define WRITEDLONG(p,v) \
181 do { \
182 WRITECHAR(p,v); \
183 WRITECHAR(p,(v) >> 8); \
184 WRITECHAR(p,(v) >> 16); \
185 WRITECHAR(p,(v) >> 24); \
186 WRITECHAR(p,(v) >> 32); \
187 WRITECHAR(p,(v) >> 40); \
188 WRITECHAR(p,(v) >> 48); \
189 WRITECHAR(p,(v) >> 56); \
190 } while (0)
193 * and routines to do the same thing to a file
195 void fwriteint16_t(int data, FILE * fp);
196 void fwriteint32_t(int32_t data, FILE * fp);
197 void fwriteint64_t(int64_t data, FILE * fp);
200 * Routines to manage a dynamic random access array of int32_ts which
201 * may grow in size to be more than the largest single malloc'able
202 * chunk.
205 #define RAA_BLKSIZE 4096 /* this many longs allocated at once */
206 #define RAA_LAYERSIZE 1024 /* this many _pointers_ allocated */
208 typedef struct RAA RAA;
209 typedef union RAA_UNION RAA_UNION;
210 typedef struct RAA_LEAF RAA_LEAF;
211 typedef struct RAA_BRANCH RAA_BRANCH;
213 struct RAA {
215 * Number of layers below this one to get to the real data. 0
216 * means this structure is a leaf, holding RAA_BLKSIZE real
217 * data items; 1 and above mean it's a branch, holding
218 * RAA_LAYERSIZE pointers to the next level branch or leaf
219 * structures.
221 int layers;
223 * Number of real data items spanned by one position in the
224 * `data' array at this level. This number is 1, trivially, for
225 * a leaf (level 0): for a level 1 branch it should be
226 * RAA_BLKSIZE, and for a level 2 branch it's
227 * RAA_LAYERSIZE*RAA_BLKSIZE.
229 int32_t stepsize;
230 union RAA_UNION {
231 struct RAA_LEAF {
232 int32_t data[RAA_BLKSIZE];
233 } l;
234 struct RAA_BRANCH {
235 struct RAA *data[RAA_LAYERSIZE];
236 } b;
237 } u;
240 struct RAA *raa_init(void);
241 void raa_free(struct RAA *);
242 int32_t raa_read(struct RAA *, int32_t);
243 struct RAA *raa_write(struct RAA *r, int32_t posn, int32_t value);
246 * Routines to manage a dynamic sequential-access array, under the
247 * same restriction on maximum mallocable block. This array may be
248 * written to in two ways: a contiguous chunk can be reserved of a
249 * given size with a pointer returned OR single-byte data may be
250 * written. The array can also be read back in the same two ways:
251 * as a series of big byte-data blocks or as a list of structures
252 * of a given size.
255 struct SAA {
257 * members `end' and `elem_len' are only valid in first link in
258 * list; `rptr' and `rpos' are used for reading
260 struct SAA *next, *end, *rptr;
261 int32_t elem_len, length, posn, start, rpos;
262 char *data;
265 struct SAA *saa_init(int32_t elem_len); /* 1 == byte */
266 void saa_free(struct SAA *);
267 void *saa_wstruct(struct SAA *); /* return a structure of elem_len */
268 void saa_wbytes(struct SAA *, const void *, int32_t); /* write arbitrary bytes */
269 void saa_rewind(struct SAA *); /* for reading from beginning */
270 void *saa_rstruct(struct SAA *); /* return NULL on EOA */
271 void *saa_rbytes(struct SAA *, int32_t *); /* return 0 on EOA */
272 void saa_rnbytes(struct SAA *, void *, int32_t); /* read a given no. of bytes */
273 void saa_fread(struct SAA *s, int32_t posn, void *p, int32_t len); /* fixup */
274 void saa_fwrite(struct SAA *s, int32_t posn, void *p, int32_t len); /* fixup */
275 void saa_fpwrite(struct SAA *, FILE *);
278 * Binary search routine. Returns index into `array' of an entry
279 * matching `string', or <0 if no match. `array' is taken to
280 * contain `size' elements.
282 * bsi() is case sensitive, bsii() is case insensitive.
284 int bsi(char *string, const char **array, int size);
285 int bsii(char *string, const char **array, int size);
287 char *src_set_fname(char *newname);
288 int32_t src_set_linnum(int32_t newline);
289 int32_t src_get_linnum(void);
291 * src_get may be used if you simply want to know the source file and line.
292 * It is also used if you maintain private status about the source location
293 * It return 0 if the information was the same as the last time you
294 * checked, -1 if the name changed and (new-old) if just the line changed.
296 int src_get(int32_t *xline, char **xname);
298 void nasm_quote(char **str);
299 char *nasm_strcat(char *one, char *two);
301 void null_debug_routine(const char *directive, const char *params);
302 extern struct dfmt null_debug_form;
303 extern struct dfmt *null_debug_arr[2];
305 const char *prefix_name(int);
307 #endif