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.
10 #define NASM_NASMLIB_H
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.
19 /* #define LOGALLOC */
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
28 #ifdef NASM_NASM_H /* need efunc defined for this */
29 void nasm_set_malloc_error(efunc
);
31 void *nasm_malloc(size_t);
32 void *nasm_realloc(void *, size_t);
33 void nasm_free(void *);
34 char *nasm_strdup(const char *);
35 char *nasm_strndup(char *, size_t);
37 void *nasm_malloc_log(char *, int, size_t);
38 void *nasm_realloc_log(char *, int, void *, size_t);
39 void nasm_free_log(char *, int, void *);
40 char *nasm_strdup_log(char *, int, const char *);
41 char *nasm_strndup_log(char *, int, char *, 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)
51 * ANSI doesn't guarantee the presence of `stricmp' or
54 #if defined(stricmp) || defined(strcasecmp)
56 #define nasm_stricmp stricmp
58 #define nasm_stricmp strcasecmp
61 int nasm_stricmp(const char *, const char *);
64 #if defined(strnicmp) || defined(strncasecmp)
66 #define nasm_strnicmp strnicmp
68 #define nasm_strnicmp strncasecmp
71 int nasm_strnicmp(const char *, const char *, int);
75 * Convert a string into a number, using NASM number rules. Sets
76 * `*error' to TRUE if an error occurs, and FALSE otherwise.
78 int64_t readnum(char *str
, int *error
);
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,
86 int64_t readstrnum(char *str
, int length
, int *warn
);
89 * seg_init: Initialise the segment-number allocator.
90 * seg_alloc: allocate a hitherto unused segment number.
93 int32_t seg_alloc(void);
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
100 void standard_extension(char *inname
, char *outname
, char *extension
,
105 * some handy macros that will probably be of use in more than one
106 * output format: convert integers into little-endian byte packed
110 #define WRITECHAR(p,v) \
112 *(p)++ = (v) & 0xFF; \
115 #define WRITESHORT(p,v) \
118 WRITECHAR(p,(v) >> 8); \
121 #define WRITELONG(p,v) \
124 WRITECHAR(p,(v) >> 8); \
125 WRITECHAR(p,(v) >> 16); \
126 WRITECHAR(p,(v) >> 24); \
129 #define WRITEDLONG(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); \
142 * and routines to do the same thing to a file
144 void fwriteint16_t(int data
, FILE * fp
);
145 void fwriteint32_t(int32_t data
, FILE * fp
);
146 void fwriteint64_t(int64_t data
, FILE * fp
);
149 * Routines to manage a dynamic random access array of int32_ts which
150 * may grow in size to be more than the largest single malloc'able
154 #define RAA_BLKSIZE 4096 /* this many longs allocated at once */
155 #define RAA_LAYERSIZE 1024 /* this many _pointers_ allocated */
157 typedef struct RAA RAA
;
158 typedef union RAA_UNION RAA_UNION
;
159 typedef struct RAA_LEAF RAA_LEAF
;
160 typedef struct RAA_BRANCH RAA_BRANCH
;
164 * Number of layers below this one to get to the real data. 0
165 * means this structure is a leaf, holding RAA_BLKSIZE real
166 * data items; 1 and above mean it's a branch, holding
167 * RAA_LAYERSIZE pointers to the next level branch or leaf
172 * Number of real data items spanned by one position in the
173 * `data' array at this level. This number is 1, trivially, for
174 * a leaf (level 0): for a level 1 branch it should be
175 * RAA_BLKSIZE, and for a level 2 branch it's
176 * RAA_LAYERSIZE*RAA_BLKSIZE.
181 int32_t data
[RAA_BLKSIZE
];
184 struct RAA
*data
[RAA_LAYERSIZE
];
189 struct RAA
*raa_init(void);
190 void raa_free(struct RAA
*);
191 int32_t raa_read(struct RAA
*, int32_t);
192 struct RAA
*raa_write(struct RAA
*r
, int32_t posn
, int32_t value
);
195 * Routines to manage a dynamic sequential-access array, under the
196 * same restriction on maximum mallocable block. This array may be
197 * written to in two ways: a contiguous chunk can be reserved of a
198 * given size with a pointer returned OR single-byte data may be
199 * written. The array can also be read back in the same two ways:
200 * as a series of big byte-data blocks or as a list of structures
206 * members `end' and `elem_len' are only valid in first link in
207 * list; `rptr' and `rpos' are used for reading
209 struct SAA
*next
, *end
, *rptr
;
210 int32_t elem_len
, length
, posn
, start
, rpos
;
214 struct SAA
*saa_init(int32_t elem_len
); /* 1 == byte */
215 void saa_free(struct SAA
*);
216 void *saa_wstruct(struct SAA
*); /* return a structure of elem_len */
217 void saa_wbytes(struct SAA
*, const void *, int32_t); /* write arbitrary bytes */
218 void saa_rewind(struct SAA
*); /* for reading from beginning */
219 void *saa_rstruct(struct SAA
*); /* return NULL on EOA */
220 void *saa_rbytes(struct SAA
*, int32_t *); /* return 0 on EOA */
221 void saa_rnbytes(struct SAA
*, void *, int32_t); /* read a given no. of bytes */
222 void saa_fread(struct SAA
*s
, int32_t posn
, void *p
, int32_t len
); /* fixup */
223 void saa_fwrite(struct SAA
*s
, int32_t posn
, void *p
, int32_t len
); /* fixup */
224 void saa_fpwrite(struct SAA
*, FILE *);
230 extern char *stdscan_bufptr
;
231 void stdscan_reset(void);
232 int stdscan(void *private_data
, struct tokenval
*tv
);
237 * Library routines to manipulate expression data types.
239 int is_reloc(expr
*);
240 int is_simple(expr
*);
241 int is_really_simple(expr
*);
242 int is_unknown(expr
*);
243 int is_just_unknown(expr
*);
244 int64_t reloc_value(expr
*);
245 int32_t reloc_seg(expr
*);
246 int32_t reloc_wrt(expr
*);
250 * Binary search routine. Returns index into `array' of an entry
251 * matching `string', or <0 if no match. `array' is taken to
252 * contain `size' elements.
254 int bsi(char *string
, const char **array
, int size
);
256 char *src_set_fname(char *newname
);
257 int32_t src_set_linnum(int32_t newline
);
258 int32_t src_get_linnum(void);
260 * src_get may be used if you simply want to know the source file and line.
261 * It is also used if you maintain private status about the source location
262 * It return 0 if the information was the same as the last time you
263 * checked, -1 if the name changed and (new-old) if just the line changed.
265 int src_get(int32_t *xline
, char **xname
);
267 void nasm_quote(char **str
);
268 char *nasm_strcat(char *one
, char *two
);
269 void nasmlib_cleanup(void);
271 void null_debug_routine(const char *directive
, const char *params
);
272 extern struct dfmt null_debug_form
;
273 extern struct dfmt
*null_debug_arr
[2];
275 const char *prefix_name(int);