BR2907058: insn_size - close file handle before returning
[nasm/avx512.git] / saa.c
blob157aba34cb7142a4ffb3a7ee35354a482f64a849
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
34 #include "compiler.h"
35 #include "nasmlib.h"
36 #include "saa.h"
38 /* Aggregate SAA components smaller than this */
39 #define SAA_BLKSHIFT 16
40 #define SAA_BLKLEN ((size_t)1 << SAA_BLKSHIFT)
42 struct SAA *saa_init(size_t elem_len)
44 struct SAA *s;
45 char *data;
47 s = nasm_zalloc(sizeof(struct SAA));
49 if (elem_len >= SAA_BLKLEN)
50 s->blk_len = elem_len;
51 else
52 s->blk_len = SAA_BLKLEN - (SAA_BLKLEN % elem_len);
54 s->elem_len = elem_len;
55 s->length = s->blk_len;
56 data = nasm_malloc(s->blk_len);
57 s->nblkptrs = s->nblks = 1;
58 s->blk_ptrs = nasm_malloc(sizeof(char *));
59 s->blk_ptrs[0] = data;
60 s->wblk = s->rblk = &s->blk_ptrs[0];
62 return s;
65 void saa_free(struct SAA *s)
67 char **p;
68 size_t n;
70 for (p = s->blk_ptrs, n = s->nblks; n; p++, n--)
71 nasm_free(*p);
73 nasm_free(s->blk_ptrs);
74 nasm_free(s);
77 /* Add one allocation block to an SAA */
78 static void saa_extend(struct SAA *s)
80 size_t blkn = s->nblks++;
82 if (blkn >= s->nblkptrs) {
83 size_t rindex = s->rblk - s->blk_ptrs;
84 size_t windex = s->wblk - s->blk_ptrs;
86 s->nblkptrs <<= 1;
87 s->blk_ptrs =
88 nasm_realloc(s->blk_ptrs, s->nblkptrs * sizeof(char *));
90 s->rblk = s->blk_ptrs + rindex;
91 s->wblk = s->blk_ptrs + windex;
94 s->blk_ptrs[blkn] = nasm_malloc(s->blk_len);
95 s->length += s->blk_len;
98 void *saa_wstruct(struct SAA *s)
100 void *p;
102 nasm_assert((s->wpos % s->elem_len) == 0);
104 if (s->wpos + s->elem_len > s->blk_len) {
105 nasm_assert(s->wpos == s->blk_len);
106 if (s->wptr + s->elem_len > s->length)
107 saa_extend(s);
108 s->wblk++;
109 s->wpos = 0;
112 p = *s->wblk + s->wpos;
113 s->wpos += s->elem_len;
114 s->wptr += s->elem_len;
116 if (s->wptr > s->datalen)
117 s->datalen = s->wptr;
119 return p;
122 void saa_wbytes(struct SAA *s, const void *data, size_t len)
124 const char *d = data;
126 while (len) {
127 size_t l = s->blk_len - s->wpos;
128 if (l > len)
129 l = len;
130 if (l) {
131 if (d) {
132 memcpy(*s->wblk + s->wpos, d, l);
133 d += l;
134 } else
135 memset(*s->wblk + s->wpos, 0, l);
136 s->wpos += l;
137 s->wptr += l;
138 len -= l;
140 if (s->datalen < s->wptr)
141 s->datalen = s->wptr;
143 if (len) {
144 if (s->wptr >= s->length)
145 saa_extend(s);
146 s->wblk++;
147 s->wpos = 0;
152 void saa_rewind(struct SAA *s)
154 s->rblk = s->blk_ptrs;
155 s->rpos = s->rptr = 0;
158 void *saa_rstruct(struct SAA *s)
160 void *p;
162 if (s->rptr + s->elem_len > s->datalen)
163 return NULL;
165 nasm_assert((s->rpos % s->elem_len) == 0);
167 if (s->rpos + s->elem_len > s->blk_len) {
168 s->rblk++;
169 s->rpos = 0;
172 p = *s->rblk + s->rpos;
173 s->rpos += s->elem_len;
174 s->rptr += s->elem_len;
176 return p;
179 const void *saa_rbytes(struct SAA *s, size_t * lenp)
181 const void *p;
182 size_t len;
184 if (s->rptr >= s->datalen) {
185 *lenp = 0;
186 return NULL;
189 if (s->rpos >= s->blk_len) {
190 s->rblk++;
191 s->rpos = 0;
194 len = *lenp;
195 if (len > s->datalen - s->rptr)
196 len = s->datalen - s->rptr;
197 if (len > s->blk_len - s->rpos)
198 len = s->blk_len - s->rpos;
200 *lenp = len;
201 p = *s->rblk + s->rpos;
203 s->rpos += len;
204 s->rptr += len;
206 return p;
209 void saa_rnbytes(struct SAA *s, void *data, size_t len)
211 char *d = data;
213 nasm_assert(s->rptr + len <= s->datalen);
215 while (len) {
216 size_t l;
217 const void *p;
219 l = len;
220 p = saa_rbytes(s, &l);
222 memcpy(d, p, l);
223 d += l;
224 len -= l;
228 /* Same as saa_rnbytes, except position the counter first */
229 void saa_fread(struct SAA *s, size_t posn, void *data, size_t len)
231 size_t ix;
233 nasm_assert(posn + len <= s->datalen);
235 if (likely(s->blk_len == SAA_BLKLEN)) {
236 ix = posn >> SAA_BLKSHIFT;
237 s->rpos = posn & (SAA_BLKLEN - 1);
238 } else {
239 ix = posn / s->blk_len;
240 s->rpos = posn % s->blk_len;
242 s->rptr = posn;
243 s->rblk = &s->blk_ptrs[ix];
245 saa_rnbytes(s, data, len);
248 /* Same as saa_wbytes, except position the counter first */
249 void saa_fwrite(struct SAA *s, size_t posn, const void *data, size_t len)
251 size_t ix;
253 /* Seek beyond the end of the existing array not supported */
254 nasm_assert(posn <= s->datalen);
256 if (likely(s->blk_len == SAA_BLKLEN)) {
257 ix = posn >> SAA_BLKSHIFT;
258 s->wpos = posn & (SAA_BLKLEN - 1);
259 } else {
260 ix = posn / s->blk_len;
261 s->wpos = posn % s->blk_len;
263 s->wptr = posn;
264 s->wblk = &s->blk_ptrs[ix];
266 if (!s->wpos) {
267 s->wpos = s->blk_len;
268 s->wblk--;
271 saa_wbytes(s, data, len);
274 void saa_fpwrite(struct SAA *s, FILE * fp)
276 const char *data;
277 size_t len;
279 saa_rewind(s);
280 while (len = s->datalen, (data = saa_rbytes(s, &len)) != NULL)
281 fwrite(data, 1, len, fp);
284 void saa_write8(struct SAA *s, uint8_t v)
286 saa_wbytes(s, &v, 1);
289 #ifdef WORDS_LITTEENDIAN
291 void saa_write16(struct SAA *s, uint16_t v)
293 saa_wbytes(s, &v, 2);
296 void saa_write32(struct SAA *s, uint32_t v)
298 saa_wbytes(s, &v, 4);
301 void saa_write64(struct SAA *s, uint64_t v)
303 saa_wbytes(s, &v, 8);
306 void saa_writeaddr(struct SAA *s, uint64_t v, size_t len)
308 saa_wbytes(s, &v, len);
311 #else /* not WORDS_LITTLEENDIAN */
313 void saa_write16(struct SAA *s, uint16_t v)
315 uint8_t b[2];
317 b[0] = v;
318 b[1] = v >> 8;
319 saa_wbytes(s, b, 2);
322 void saa_write32(struct SAA *s, uint32_t v)
324 uint8_t b[4];
326 b[0] = v;
327 b[1] = v >> 8;
328 b[2] = v >> 16;
329 b[3] = v >> 24;
330 saa_wbytes(s, b, 4);
333 void saa_write64(struct SAA *s, uint64_t v)
335 uint8_t b[8];
337 b[0] = v;
338 b[1] = v >> 8;
339 b[2] = v >> 16;
340 b[3] = v >> 24;
341 b[4] = v >> 32;
342 b[5] = v >> 40;
343 b[6] = v >> 48;
344 b[7] = v >> 56;
345 saa_wbytes(s, b, 8);
348 void saa_writeaddr(struct SAA *s, uint64_t v, size_t len)
350 uint8_t b[8];
352 b[0] = v;
353 b[1] = v >> 8;
354 b[2] = v >> 16;
355 b[3] = v >> 24;
356 b[4] = v >> 32;
357 b[5] = v >> 40;
358 b[6] = v >> 48;
359 b[7] = v >> 56;
361 saa_wbytes(s, &v, len);
364 #endif /* WORDS_LITTLEENDIAN */
366 /* write unsigned LEB128 value to SAA */
367 void saa_wleb128u(struct SAA *psaa, int value)
369 char temp[64], *ptemp;
370 uint8_t byte;
371 int len;
373 ptemp = temp;
374 len = 0;
375 do {
376 byte = value & 127;
377 value >>= 7;
378 if (value != 0) /* more bytes to come */
379 byte |= 0x80;
380 *ptemp = byte;
381 ptemp++;
382 len++;
383 } while (value != 0);
384 saa_wbytes(psaa, temp, len);
387 /* write signed LEB128 value to SAA */
388 void saa_wleb128s(struct SAA *psaa, int value)
390 char temp[64], *ptemp;
391 uint8_t byte;
392 bool more, negative;
393 int size, len;
395 ptemp = temp;
396 more = 1;
397 negative = (value < 0);
398 size = sizeof(int) * 8;
399 len = 0;
400 while (more) {
401 byte = value & 0x7f;
402 value >>= 7;
403 if (negative)
404 /* sign extend */
405 value |= -(1 << (size - 7));
406 /* sign bit of byte is second high order bit (0x40) */
407 if ((value == 0 && !(byte & 0x40)) ||
408 ((value == -1) && (byte & 0x40)))
409 more = 0;
410 else
411 byte |= 0x80;
412 *ptemp = byte;
413 ptemp++;
414 len++;
416 saa_wbytes(psaa, temp, len);