Version 0.99.03
[nasm/avx512.git] / nasmlib.h
blob82a35e30432c4eb42dc051358decb1a05b4640b9
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 * Wrappers around malloc, realloc and free. nasm_malloc will
27 * fatal-error and die rather than return NULL; nasm_realloc will
28 * do likewise, and will also guarantee to work right on being
29 * passed a NULL pointer; nasm_free will do nothing if it is passed
30 * a NULL pointer.
32 #ifdef NASM_NASM_H /* need efunc defined for this */
33 void nasm_set_malloc_error(efunc);
34 #ifndef LOGALLOC
35 void *nasm_malloc(size_t);
36 void *nasm_realloc(void *, size_t);
37 void nasm_free(void *);
38 char *nasm_strdup(const char *);
39 char *nasm_strndup(char *, size_t);
40 #else
41 void *nasm_malloc_log(char *, int, size_t);
42 void *nasm_realloc_log(char *, int, void *, size_t);
43 void nasm_free_log(char *, int, void *);
44 char *nasm_strdup_log(char *, int, const char *);
45 char *nasm_strndup_log(char *, int, char *, size_t);
46 #define nasm_malloc(x) nasm_malloc_log(__FILE__,__LINE__,x)
47 #define nasm_realloc(x,y) nasm_realloc_log(__FILE__,__LINE__,x,y)
48 #define nasm_free(x) nasm_free_log(__FILE__,__LINE__,x)
49 #define nasm_strdup(x) nasm_strdup_log(__FILE__,__LINE__,x)
50 #define nasm_strndup(x,y) nasm_strndup_log(__FILE__,__LINE__,x,y)
51 #endif
52 #endif
55 * ANSI doesn't guarantee the presence of `stricmp' or
56 * `strcasecmp'.
58 #if defined(stricmp) || defined(strcasecmp)
59 #if defined(stricmp)
60 #define nasm_stricmp stricmp
61 #else
62 #define nasm_stricmp strcasecmp
63 #endif
64 #else
65 int nasm_stricmp(const char *, const char *);
66 #endif
68 #if defined(strnicmp) || defined(strncasecmp)
69 #if defined(strnicmp)
70 #define nasm_strnicmp strnicmp
71 #else
72 #define nasm_strnicmp strncasecmp
73 #endif
74 #else
75 int nasm_strnicmp(const char *, const char *, int);
76 #endif
78 #if defined(strsep)
79 #define nasm_strsep strsep
80 #else
81 char *nasm_strsep(char **stringp, const char *delim);
82 #endif
86 * Convert a string into a number, using NASM number rules. Sets
87 * `*error' to TRUE if an error occurs, and FALSE otherwise.
89 int64_t readnum(char *str, int *error);
92 * Convert a character constant into a number. Sets
93 * `*warn' to TRUE if an overflow occurs, and FALSE otherwise.
94 * str points to and length covers the middle of the string,
95 * without the quotes.
97 int64_t readstrnum(char *str, int length, int *warn);
100 * seg_init: Initialise the segment-number allocator.
101 * seg_alloc: allocate a hitherto unused segment number.
103 void seg_init(void);
104 int32_t seg_alloc(void);
107 * many output formats will be able to make use of this: a standard
108 * function to add an extension to the name of the input file
110 #ifdef NASM_NASM_H
111 void standard_extension(char *inname, char *outname, char *extension,
112 efunc error);
113 #endif
116 * some handy macros that will probably be of use in more than one
117 * output format: convert integers into little-endian byte packed
118 * format in memory
121 #define WRITECHAR(p,v) \
122 do { \
123 *(p)++ = (v) & 0xFF; \
124 } while (0)
126 #define WRITESHORT(p,v) \
127 do { \
128 WRITECHAR(p,v); \
129 WRITECHAR(p,(v) >> 8); \
130 } while (0)
132 #define WRITELONG(p,v) \
133 do { \
134 WRITECHAR(p,v); \
135 WRITECHAR(p,(v) >> 8); \
136 WRITECHAR(p,(v) >> 16); \
137 WRITECHAR(p,(v) >> 24); \
138 } while (0)
140 #define WRITEDLONG(p,v) \
141 do { \
142 WRITECHAR(p,v); \
143 WRITECHAR(p,(v) >> 8); \
144 WRITECHAR(p,(v) >> 16); \
145 WRITECHAR(p,(v) >> 24); \
146 WRITECHAR(p,(v) >> 32); \
147 WRITECHAR(p,(v) >> 40); \
148 WRITECHAR(p,(v) >> 48); \
149 WRITECHAR(p,(v) >> 56); \
150 } while (0)
153 * and routines to do the same thing to a file
155 void fwriteint16_t(int data, FILE * fp);
156 void fwriteint32_t(int32_t data, FILE * fp);
157 void fwriteint64_t(int64_t data, FILE * fp);
160 * Routines to manage a dynamic random access array of int32_ts which
161 * may grow in size to be more than the largest single malloc'able
162 * chunk.
165 #define RAA_BLKSIZE 4096 /* this many longs allocated at once */
166 #define RAA_LAYERSIZE 1024 /* this many _pointers_ allocated */
168 typedef struct RAA RAA;
169 typedef union RAA_UNION RAA_UNION;
170 typedef struct RAA_LEAF RAA_LEAF;
171 typedef struct RAA_BRANCH RAA_BRANCH;
173 struct RAA {
175 * Number of layers below this one to get to the real data. 0
176 * means this structure is a leaf, holding RAA_BLKSIZE real
177 * data items; 1 and above mean it's a branch, holding
178 * RAA_LAYERSIZE pointers to the next level branch or leaf
179 * structures.
181 int layers;
183 * Number of real data items spanned by one position in the
184 * `data' array at this level. This number is 1, trivially, for
185 * a leaf (level 0): for a level 1 branch it should be
186 * RAA_BLKSIZE, and for a level 2 branch it's
187 * RAA_LAYERSIZE*RAA_BLKSIZE.
189 int32_t stepsize;
190 union RAA_UNION {
191 struct RAA_LEAF {
192 int32_t data[RAA_BLKSIZE];
193 } l;
194 struct RAA_BRANCH {
195 struct RAA *data[RAA_LAYERSIZE];
196 } b;
197 } u;
200 struct RAA *raa_init(void);
201 void raa_free(struct RAA *);
202 int32_t raa_read(struct RAA *, int32_t);
203 struct RAA *raa_write(struct RAA *r, int32_t posn, int32_t value);
206 * Routines to manage a dynamic sequential-access array, under the
207 * same restriction on maximum mallocable block. This array may be
208 * written to in two ways: a contiguous chunk can be reserved of a
209 * given size with a pointer returned OR single-byte data may be
210 * written. The array can also be read back in the same two ways:
211 * as a series of big byte-data blocks or as a list of structures
212 * of a given size.
215 struct SAA {
217 * members `end' and `elem_len' are only valid in first link in
218 * list; `rptr' and `rpos' are used for reading
220 struct SAA *next, *end, *rptr;
221 int32_t elem_len, length, posn, start, rpos;
222 char *data;
225 struct SAA *saa_init(int32_t elem_len); /* 1 == byte */
226 void saa_free(struct SAA *);
227 void *saa_wstruct(struct SAA *); /* return a structure of elem_len */
228 void saa_wbytes(struct SAA *, const void *, int32_t); /* write arbitrary bytes */
229 void saa_rewind(struct SAA *); /* for reading from beginning */
230 void *saa_rstruct(struct SAA *); /* return NULL on EOA */
231 void *saa_rbytes(struct SAA *, int32_t *); /* return 0 on EOA */
232 void saa_rnbytes(struct SAA *, void *, int32_t); /* read a given no. of bytes */
233 void saa_fread(struct SAA *s, int32_t posn, void *p, int32_t len); /* fixup */
234 void saa_fwrite(struct SAA *s, int32_t posn, void *p, int32_t len); /* fixup */
235 void saa_fpwrite(struct SAA *, FILE *);
237 #ifdef NASM_NASM_H
239 * Library routines to manipulate expression data types.
241 int is_reloc(expr *);
242 int is_simple(expr *);
243 int is_really_simple(expr *);
244 int is_unknown(expr *);
245 int is_just_unknown(expr *);
246 int64_t reloc_value(expr *);
247 int32_t reloc_seg(expr *);
248 int32_t reloc_wrt(expr *);
249 #endif
252 * Binary search routine. Returns index into `array' of an entry
253 * matching `string', or <0 if no match. `array' is taken to
254 * contain `size' elements.
256 * bsi() is case sensitive, bsii() is case insensitive.
258 int bsi(char *string, const char **array, int size);
259 int bsii(char *string, const char **array, int size);
261 char *src_set_fname(char *newname);
262 int32_t src_set_linnum(int32_t newline);
263 int32_t src_get_linnum(void);
265 * src_get may be used if you simply want to know the source file and line.
266 * It is also used if you maintain private status about the source location
267 * It return 0 if the information was the same as the last time you
268 * checked, -1 if the name changed and (new-old) if just the line changed.
270 int src_get(int32_t *xline, char **xname);
272 void nasm_quote(char **str);
273 char *nasm_strcat(char *one, char *two);
275 void null_debug_routine(const char *directive, const char *params);
276 extern struct dfmt null_debug_form;
277 extern struct dfmt *null_debug_arr[2];
279 const char *prefix_name(int);
281 #endif