Add .gitignore file so "git status" produces something sane
[nasm.git] / nasmlib.h
blob14b28677a159c602e2ed4b7a08bfb3b093e27759
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 "compiler.h"
14 #include <inttypes.h>
15 #include <stdio.h>
16 #include <string.h>
17 #ifdef HAVE_STRINGS_H
18 #include <strings.h>
19 #endif
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.
28 /* #define LOGALLOC */
31 * -------------------------
32 * Error reporting functions
33 * -------------------------
37 * An error reporting function should look like this.
39 typedef void (*efunc) (int severity, const char *fmt, ...);
42 * These are the error severity codes which get passed as the first
43 * argument to an efunc.
46 #define ERR_DEBUG 0x00000008 /* put out debugging message */
47 #define ERR_WARNING 0x00000000 /* warn only: no further action */
48 #define ERR_NONFATAL 0x00000001 /* terminate assembly after phase */
49 #define ERR_FATAL 0x00000002 /* instantly fatal: exit with error */
50 #define ERR_PANIC 0x00000003 /* internal error: panic instantly
51 * and dump core for reference */
52 #define ERR_MASK 0x0000000F /* mask off the above codes */
53 #define ERR_NOFILE 0x00000010 /* don't give source file name/line */
54 #define ERR_USAGE 0x00000020 /* print a usage message */
55 #define ERR_PASS1 0x00000040 /* only print this error on pass one */
58 * These codes define specific types of suppressible warning.
61 #define ERR_WARN_MASK 0x0000FF00 /* the mask for this feature */
62 #define ERR_WARN_SHR 8 /* how far to shift right */
64 #define ERR_WARN_MNP 0x00000100 /* macro-num-parameters warning */
65 #define ERR_WARN_MSR 0x00000200 /* macro self-reference */
66 #define ERR_WARN_OL 0x00000300 /* orphan label (no colon, and
67 * alone on line) */
68 #define ERR_WARN_NOV 0x00000400 /* numeric overflow */
69 #define ERR_WARN_GNUELF 0x00000500 /* using GNU ELF extensions */
70 #define ERR_WARN_MAX 5 /* the highest numbered one */
73 * Wrappers around malloc, realloc and free. nasm_malloc will
74 * fatal-error and die rather than return NULL; nasm_realloc will
75 * do likewise, and will also guarantee to work right on being
76 * passed a NULL pointer; nasm_free will do nothing if it is passed
77 * a NULL pointer.
79 void nasm_set_malloc_error(efunc);
80 #ifndef LOGALLOC
81 void *nasm_malloc(size_t);
82 void *nasm_zalloc(size_t);
83 void *nasm_realloc(void *, size_t);
84 void nasm_free(void *);
85 char *nasm_strdup(const char *);
86 char *nasm_strndup(char *, size_t);
87 #else
88 void *nasm_malloc_log(char *, int, size_t);
89 void *nasm_zalloc_log(char *, int, size_t);
90 void *nasm_realloc_log(char *, int, void *, size_t);
91 void nasm_free_log(char *, int, void *);
92 char *nasm_strdup_log(char *, int, const char *);
93 char *nasm_strndup_log(char *, int, char *, size_t);
94 #define nasm_malloc(x) nasm_malloc_log(__FILE__,__LINE__,x)
95 #define nasm_zalloc(x) nasm_malloc_log(__FILE__,__LINE__,x)
96 #define nasm_realloc(x,y) nasm_realloc_log(__FILE__,__LINE__,x,y)
97 #define nasm_free(x) nasm_free_log(__FILE__,__LINE__,x)
98 #define nasm_strdup(x) nasm_strdup_log(__FILE__,__LINE__,x)
99 #define nasm_strndup(x,y) nasm_strndup_log(__FILE__,__LINE__,x,y)
100 #endif
103 * ANSI doesn't guarantee the presence of `stricmp' or
104 * `strcasecmp'.
106 #if defined(HAVE_STRCASECMP)
107 #define nasm_stricmp strcasecmp
108 #elif defined(HAVE_STRICMP)
109 #define nasm_stricmp stricmp
110 #else
111 int nasm_stricmp(const char *, const char *);
112 #endif
114 #if defined(HAVE_STRNCASECMP)
115 #define nasm_strnicmp strncasecmp
116 #elif defined(HAVE_STRNICMP)
117 #define nasm_strnicmp strnicmp
118 #else
119 int nasm_strnicmp(const char *, const char *, int);
120 #endif
122 #if defined(HAVE_STRSEP)
123 #define nasm_strsep strsep
124 #else
125 char *nasm_strsep(char **stringp, const char *delim);
126 #endif
130 * Convert a string into a number, using NASM number rules. Sets
131 * `*error' to true if an error occurs, and false otherwise.
133 int64_t readnum(char *str, bool *error);
136 * Convert a character constant into a number. Sets
137 * `*warn' to true if an overflow occurs, and false otherwise.
138 * str points to and length covers the middle of the string,
139 * without the quotes.
141 int64_t readstrnum(char *str, int length, bool *warn);
144 * seg_init: Initialise the segment-number allocator.
145 * seg_alloc: allocate a hitherto unused segment number.
147 void seg_init(void);
148 int32_t seg_alloc(void);
151 * many output formats will be able to make use of this: a standard
152 * function to add an extension to the name of the input file
154 #ifdef NASM_NASM_H
155 void standard_extension(char *inname, char *outname, char *extension,
156 efunc error);
157 #endif
160 * some handy macros that will probably be of use in more than one
161 * output format: convert integers into little-endian byte packed
162 * format in memory
165 #define WRITECHAR(p,v) \
166 do { \
167 *(p)++ = (v) & 0xFF; \
168 } while (0)
170 #define WRITESHORT(p,v) \
171 do { \
172 WRITECHAR(p,v); \
173 WRITECHAR(p,(v) >> 8); \
174 } while (0)
176 #define WRITELONG(p,v) \
177 do { \
178 WRITECHAR(p,v); \
179 WRITECHAR(p,(v) >> 8); \
180 WRITECHAR(p,(v) >> 16); \
181 WRITECHAR(p,(v) >> 24); \
182 } while (0)
184 #define WRITEDLONG(p,v) \
185 do { \
186 WRITECHAR(p,v); \
187 WRITECHAR(p,(v) >> 8); \
188 WRITECHAR(p,(v) >> 16); \
189 WRITECHAR(p,(v) >> 24); \
190 WRITECHAR(p,(v) >> 32); \
191 WRITECHAR(p,(v) >> 40); \
192 WRITECHAR(p,(v) >> 48); \
193 WRITECHAR(p,(v) >> 56); \
194 } while (0)
197 * and routines to do the same thing to a file
199 void fwriteint16_t(int data, FILE * fp);
200 void fwriteint32_t(int32_t data, FILE * fp);
201 void fwriteint64_t(int64_t data, FILE * fp);
204 * Routines to manage a dynamic random access array of int32_ts which
205 * may grow in size to be more than the largest single malloc'able
206 * chunk.
209 #define RAA_BLKSIZE 65536 /* this many longs allocated at once */
210 #define RAA_LAYERSIZE 32768 /* this many _pointers_ allocated */
212 typedef struct RAA RAA;
213 typedef union RAA_UNION RAA_UNION;
214 typedef struct RAA_LEAF RAA_LEAF;
215 typedef struct RAA_BRANCH RAA_BRANCH;
217 struct RAA {
219 * Number of layers below this one to get to the real data. 0
220 * means this structure is a leaf, holding RAA_BLKSIZE real
221 * data items; 1 and above mean it's a branch, holding
222 * RAA_LAYERSIZE pointers to the next level branch or leaf
223 * structures.
225 int layers;
227 * Number of real data items spanned by one position in the
228 * `data' array at this level. This number is 1, trivially, for
229 * a leaf (level 0): for a level 1 branch it should be
230 * RAA_BLKSIZE, and for a level 2 branch it's
231 * RAA_LAYERSIZE*RAA_BLKSIZE.
233 int32_t stepsize;
234 union RAA_UNION {
235 struct RAA_LEAF {
236 int32_t data[RAA_BLKSIZE];
237 } l;
238 struct RAA_BRANCH {
239 struct RAA *data[RAA_LAYERSIZE];
240 } b;
241 } u;
244 struct RAA *raa_init(void);
245 void raa_free(struct RAA *);
246 int32_t raa_read(struct RAA *, int32_t);
247 struct RAA *raa_write(struct RAA *r, int32_t posn, int32_t value);
250 * Routines to manage a dynamic sequential-access array, under the
251 * same restriction on maximum mallocable block. This array may be
252 * written to in two ways: a contiguous chunk can be reserved of a
253 * given size with a pointer returned OR single-byte data may be
254 * written. The array can also be read back in the same two ways:
255 * as a series of big byte-data blocks or as a list of structures
256 * of a given size.
259 struct SAA {
261 * members `end' and `elem_len' are only valid in first link in
262 * list; `rptr' and `rpos' are used for reading
264 size_t elem_len; /* Size of each element */
265 size_t blk_len; /* Size of each allocation block */
266 size_t nblks; /* Total number of allocated blocks */
267 size_t nblkptrs; /* Total number of allocation block pointers */
268 size_t length; /* Total allocated length of the array */
269 size_t datalen; /* Total data length of the array */
270 char **wblk; /* Write block pointer */
271 size_t wpos; /* Write position inside block */
272 size_t wptr; /* Absolute write position */
273 char **rblk; /* Read block pointer */
274 size_t rpos; /* Read position inside block */
275 size_t rptr; /* Absolute read position */
276 char **blk_ptrs; /* Pointer to pointer blocks */
279 struct SAA *saa_init(size_t elem_len); /* 1 == byte */
280 void saa_free(struct SAA *);
281 void *saa_wstruct(struct SAA *); /* return a structure of elem_len */
282 void saa_wbytes(struct SAA *, const void *, size_t); /* write arbitrary bytes */
283 void saa_rewind(struct SAA *); /* for reading from beginning */
284 void *saa_rstruct(struct SAA *); /* return NULL on EOA */
285 const void *saa_rbytes(struct SAA *, size_t *); /* return 0 on EOA */
286 void saa_rnbytes(struct SAA *, void *, size_t); /* read a given no. of bytes */
287 /* random access */
288 void saa_fread(struct SAA *, size_t, void *, size_t);
289 void saa_fwrite(struct SAA *, size_t, const void *, size_t);
291 /* dump to file */
292 void saa_fpwrite(struct SAA *, FILE *);
295 * Binary search routine. Returns index into `array' of an entry
296 * matching `string', or <0 if no match. `array' is taken to
297 * contain `size' elements.
299 * bsi() is case sensitive, bsii() is case insensitive.
301 int bsi(char *string, const char **array, int size);
302 int bsii(char *string, const char **array, int size);
304 char *src_set_fname(char *newname);
305 int32_t src_set_linnum(int32_t newline);
306 int32_t src_get_linnum(void);
308 * src_get may be used if you simply want to know the source file and line.
309 * It is also used if you maintain private status about the source location
310 * It return 0 if the information was the same as the last time you
311 * checked, -1 if the name changed and (new-old) if just the line changed.
313 int src_get(int32_t *xline, char **xname);
315 void nasm_quote(char **str);
316 char *nasm_strcat(char *one, char *two);
318 void null_debug_routine(const char *directive, const char *params);
319 extern struct dfmt null_debug_form;
320 extern struct dfmt *null_debug_arr[2];
322 const char *prefix_name(int);
324 #endif