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