Fix Makefile for MSVC++ 2005, delete obsolete Makefiles
[nasm.git] / nasmlib.h
blobcf7040ca026e9dce2b91d2a8d37e4577d2a26e7a
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>
16 * If this is defined, the wrappers around malloc et al will
17 * transform into logging variants, which will cause NASM to create
18 * a file called `malloc.log' when run, and spew details of all its
19 * memory management into that. That can then be analysed to detect
20 * memory leaks and potentially other problems too.
22 /* #define LOGALLOC */
25 * Wrappers around malloc, realloc and free. nasm_malloc will
26 * fatal-error and die rather than return NULL; nasm_realloc will
27 * do likewise, and will also guarantee to work right on being
28 * passed a NULL pointer; nasm_free will do nothing if it is passed
29 * a NULL pointer.
31 #ifdef NASM_NASM_H /* need efunc defined for this */
32 void nasm_set_malloc_error(efunc);
33 #ifndef LOGALLOC
34 void *nasm_malloc(size_t);
35 void *nasm_realloc(void *, size_t);
36 void nasm_free(void *);
37 char *nasm_strdup(const char *);
38 char *nasm_strndup(char *, size_t);
39 #else
40 void *nasm_malloc_log(char *, int, size_t);
41 void *nasm_realloc_log(char *, int, void *, size_t);
42 void nasm_free_log(char *, int, void *);
43 char *nasm_strdup_log(char *, int, const char *);
44 char *nasm_strndup_log(char *, int, char *, size_t);
45 #define nasm_malloc(x) nasm_malloc_log(__FILE__,__LINE__,x)
46 #define nasm_realloc(x,y) nasm_realloc_log(__FILE__,__LINE__,x,y)
47 #define nasm_free(x) nasm_free_log(__FILE__,__LINE__,x)
48 #define nasm_strdup(x) nasm_strdup_log(__FILE__,__LINE__,x)
49 #define nasm_strndup(x,y) nasm_strndup_log(__FILE__,__LINE__,x,y)
50 #endif
51 #endif
54 * ANSI doesn't guarantee the presence of `stricmp' or
55 * `strcasecmp'.
57 #if defined(stricmp) || defined(strcasecmp)
58 #if defined(stricmp)
59 #define nasm_stricmp stricmp
60 #else
61 #define nasm_stricmp strcasecmp
62 #endif
63 #else
64 int nasm_stricmp(const char *, const char *);
65 #endif
67 #if defined(strnicmp) || defined(strncasecmp)
68 #if defined(strnicmp)
69 #define nasm_strnicmp strnicmp
70 #else
71 #define nasm_strnicmp strncasecmp
72 #endif
73 #else
74 int nasm_strnicmp(const char *, const char *, int);
75 #endif
77 #if defined(strsep)
78 #define nasm_strsep strsep
79 #else
80 char *nasm_strsep(char **stringp, const char *delim);
81 #endif
85 * Convert a string into a number, using NASM number rules. Sets
86 * `*error' to TRUE if an error occurs, and FALSE otherwise.
88 int64_t readnum(char *str, int *error);
91 * Convert a character constant into a number. Sets
92 * `*warn' to TRUE if an overflow occurs, and FALSE otherwise.
93 * str points to and length covers the middle of the string,
94 * without the quotes.
96 int64_t readstrnum(char *str, int length, int *warn);
99 * seg_init: Initialise the segment-number allocator.
100 * seg_alloc: allocate a hitherto unused segment number.
102 void seg_init(void);
103 int32_t seg_alloc(void);
106 * many output formats will be able to make use of this: a standard
107 * function to add an extension to the name of the input file
109 #ifdef NASM_NASM_H
110 void standard_extension(char *inname, char *outname, char *extension,
111 efunc error);
112 #endif
115 * some handy macros that will probably be of use in more than one
116 * output format: convert integers into little-endian byte packed
117 * format in memory
120 #define WRITECHAR(p,v) \
121 do { \
122 *(p)++ = (v) & 0xFF; \
123 } while (0)
125 #define WRITESHORT(p,v) \
126 do { \
127 WRITECHAR(p,v); \
128 WRITECHAR(p,(v) >> 8); \
129 } while (0)
131 #define WRITELONG(p,v) \
132 do { \
133 WRITECHAR(p,v); \
134 WRITECHAR(p,(v) >> 8); \
135 WRITECHAR(p,(v) >> 16); \
136 WRITECHAR(p,(v) >> 24); \
137 } while (0)
139 #define WRITEDLONG(p,v) \
140 do { \
141 WRITECHAR(p,v); \
142 WRITECHAR(p,(v) >> 8); \
143 WRITECHAR(p,(v) >> 16); \
144 WRITECHAR(p,(v) >> 24); \
145 WRITECHAR(p,(v) >> 32); \
146 WRITECHAR(p,(v) >> 40); \
147 WRITECHAR(p,(v) >> 48); \
148 WRITECHAR(p,(v) >> 56); \
149 } while (0)
152 * and routines to do the same thing to a file
154 void fwriteint16_t(int data, FILE * fp);
155 void fwriteint32_t(int32_t data, FILE * fp);
156 void fwriteint64_t(int64_t data, FILE * fp);
159 * Routines to manage a dynamic random access array of int32_ts which
160 * may grow in size to be more than the largest single malloc'able
161 * chunk.
164 #define RAA_BLKSIZE 4096 /* this many longs allocated at once */
165 #define RAA_LAYERSIZE 1024 /* this many _pointers_ allocated */
167 typedef struct RAA RAA;
168 typedef union RAA_UNION RAA_UNION;
169 typedef struct RAA_LEAF RAA_LEAF;
170 typedef struct RAA_BRANCH RAA_BRANCH;
172 struct RAA {
174 * Number of layers below this one to get to the real data. 0
175 * means this structure is a leaf, holding RAA_BLKSIZE real
176 * data items; 1 and above mean it's a branch, holding
177 * RAA_LAYERSIZE pointers to the next level branch or leaf
178 * structures.
180 int layers;
182 * Number of real data items spanned by one position in the
183 * `data' array at this level. This number is 1, trivially, for
184 * a leaf (level 0): for a level 1 branch it should be
185 * RAA_BLKSIZE, and for a level 2 branch it's
186 * RAA_LAYERSIZE*RAA_BLKSIZE.
188 int32_t stepsize;
189 union RAA_UNION {
190 struct RAA_LEAF {
191 int32_t data[RAA_BLKSIZE];
192 } l;
193 struct RAA_BRANCH {
194 struct RAA *data[RAA_LAYERSIZE];
195 } b;
196 } u;
199 struct RAA *raa_init(void);
200 void raa_free(struct RAA *);
201 int32_t raa_read(struct RAA *, int32_t);
202 struct RAA *raa_write(struct RAA *r, int32_t posn, int32_t value);
205 * Routines to manage a dynamic sequential-access array, under the
206 * same restriction on maximum mallocable block. This array may be
207 * written to in two ways: a contiguous chunk can be reserved of a
208 * given size with a pointer returned OR single-byte data may be
209 * written. The array can also be read back in the same two ways:
210 * as a series of big byte-data blocks or as a list of structures
211 * of a given size.
214 struct SAA {
216 * members `end' and `elem_len' are only valid in first link in
217 * list; `rptr' and `rpos' are used for reading
219 struct SAA *next, *end, *rptr;
220 int32_t elem_len, length, posn, start, rpos;
221 char *data;
224 struct SAA *saa_init(int32_t elem_len); /* 1 == byte */
225 void saa_free(struct SAA *);
226 void *saa_wstruct(struct SAA *); /* return a structure of elem_len */
227 void saa_wbytes(struct SAA *, const void *, int32_t); /* write arbitrary bytes */
228 void saa_rewind(struct SAA *); /* for reading from beginning */
229 void *saa_rstruct(struct SAA *); /* return NULL on EOA */
230 void *saa_rbytes(struct SAA *, int32_t *); /* return 0 on EOA */
231 void saa_rnbytes(struct SAA *, void *, int32_t); /* read a given no. of bytes */
232 void saa_fread(struct SAA *s, int32_t posn, void *p, int32_t len); /* fixup */
233 void saa_fwrite(struct SAA *s, int32_t posn, void *p, int32_t len); /* fixup */
234 void saa_fpwrite(struct SAA *, FILE *);
236 #ifdef NASM_NASM_H
238 * Library routines to manipulate expression data types.
240 int is_reloc(expr *);
241 int is_simple(expr *);
242 int is_really_simple(expr *);
243 int is_unknown(expr *);
244 int is_just_unknown(expr *);
245 int64_t reloc_value(expr *);
246 int32_t reloc_seg(expr *);
247 int32_t reloc_wrt(expr *);
248 #endif
251 * Binary search routine. Returns index into `array' of an entry
252 * matching `string', or <0 if no match. `array' is taken to
253 * contain `size' elements.
255 * bsi() is case sensitive, bsii() is case insensitive.
257 int bsi(char *string, const char **array, int size);
258 int bsii(char *string, const char **array, int size);
260 char *src_set_fname(char *newname);
261 int32_t src_set_linnum(int32_t newline);
262 int32_t src_get_linnum(void);
264 * src_get may be used if you simply want to know the source file and line.
265 * It is also used if you maintain private status about the source location
266 * It return 0 if the information was the same as the last time you
267 * checked, -1 if the name changed and (new-old) if just the line changed.
269 int src_get(int32_t *xline, char **xname);
271 void nasm_quote(char **str);
272 char *nasm_strcat(char *one, char *two);
274 void null_debug_routine(const char *directive, const char *params);
275 extern struct dfmt null_debug_form;
276 extern struct dfmt *null_debug_arr[2];
278 const char *prefix_name(int);
280 #endif