Update documentation
[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 licence given in the file "Licence"
6  * distributed in the NASM archive.
7  */
8
9 #ifndef NASM_NASMLIB_H
10 #define NASM_NASMLIB_H
11
12 #include <inttypes.h>
13 #include <stdio.h>
14 #include "compiler.h"
15
16 /*
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.
22  */
23 /* #define LOGALLOC */
24
25 /*
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.
31  */
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
53
54 /*
55  * ANSI doesn't guarantee the presence of `stricmp' or
56  * `strcasecmp'.
57  */
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
67
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
77
78 #if defined(strsep)
79 #define nasm_strsep strsep
80 #else
81 char *nasm_strsep(char **stringp, const char *delim);
82 #endif
83
84
85 /*
86  * Convert a string into a number, using NASM number rules. Sets
87  * `*error' to TRUE if an error occurs, and FALSE otherwise.
88  */
89 int64_t readnum(char *str, int *error);
90
91 /*
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.
96  */
97 int64_t readstrnum(char *str, int length, int *warn);
98
99 /*
100  * seg_init: Initialise the segment-number allocator.
101  * seg_alloc: allocate a hitherto unused segment number.
102  */
103 void seg_init(void);
104 int32_t seg_alloc(void);
105
106 /*
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
109  */
110 #ifdef NASM_NASM_H
111 void standard_extension(char *inname, char *outname, char *extension,
112                         efunc error);
113 #endif
114
115 /*
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
119  */
120
121 #define WRITECHAR(p,v) \
122   do { \
123     *(p)++ = (v) & 0xFF; \
124   } while (0)
125
126 #define WRITESHORT(p,v) \
127   do { \
128     WRITECHAR(p,v); \
129     WRITECHAR(p,(v) >> 8); \
130   } while (0)
131
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)
139   
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)
151
152 /*
153  * and routines to do the same thing to a file
154  */
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);
158
159 /*
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.
163  */
164
165 #define RAA_BLKSIZE 4096        /* this many longs allocated at once */
166 #define RAA_LAYERSIZE 1024      /* this many _pointers_ allocated */
167
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;
172
173 struct RAA {
174     /*
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.
180      */
181     int layers;
182     /*
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.
188      */
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;
198 };
199
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);
204
205 /*
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.
213  */
214
215 struct SAA {
216     /*
217      * members `end' and `elem_len' are only valid in first link in
218      * list; `rptr' and `rpos' are used for reading
219      */
220     struct SAA *next, *end, *rptr;
221     int32_t elem_len, length, posn, start, rpos;
222     char *data;
223 };
224
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 *);
236
237 #ifdef NASM_NASM_H
238 /*
239  * Library routines to manipulate expression data types.
240  */
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
250
251 /*
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.
255  *
256  * bsi() is case sensitive, bsii() is case insensitive.
257  */
258 int bsi(char *string, const char **array, int size);
259 int bsii(char *string, const char **array, int size);
260
261 char *src_set_fname(char *newname);
262 int32_t src_set_linnum(int32_t newline);
263 int32_t src_get_linnum(void);
264 /*
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.
269  */
270 int src_get(int32_t *xline, char **xname);
271
272 void nasm_quote(char **str);
273 char *nasm_strcat(char *one, char *two);
274
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];
278
279 const char *prefix_name(int);
280
281 #endif