OLD | NEW |
1 /* | 1 /* |
2 * Bytecode utility functions | 2 * Bytecode utility functions |
3 * | 3 * |
4 * Copyright (C) 2001-2007 Peter Johnson | 4 * Copyright (C) 2001-2007 Peter Johnson |
5 * | 5 * |
6 * Redistribution and use in source and binary forms, with or without | 6 * Redistribution and use in source and binary forms, with or without |
7 * modification, are permitted provided that the following conditions | 7 * modification, are permitted provided that the following conditions |
8 * are met: | 8 * are met: |
9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
11 * 2. Redistributions in binary form must reproduce the above copyright | 11 * 2. Redistributions in binary form must reproduce the above copyright |
12 * notice, this list of conditions and the following disclaimer in the | 12 * notice, this list of conditions and the following disclaimer in the |
13 * documentation and/or other materials provided with the distribution. | 13 * documentation and/or other materials provided with the distribution. |
14 * | 14 * |
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS'' | 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS'' |
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE | 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE |
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
25 * POSSIBILITY OF SUCH DAMAGE. | 25 * POSSIBILITY OF SUCH DAMAGE. |
26 */ | 26 */ |
27 #include "util.h" | 27 #include "util.h" |
28 /*@unused@*/ RCSID("$Id: bytecode.c 2233 2009-10-31 21:45:55Z peter $"); | |
29 | 28 |
30 #include "libyasm-stdint.h" | 29 #include "libyasm-stdint.h" |
31 #include "coretype.h" | 30 #include "coretype.h" |
32 | 31 |
33 #include "errwarn.h" | 32 #include "errwarn.h" |
34 #include "intnum.h" | 33 #include "intnum.h" |
35 #include "expr.h" | 34 #include "expr.h" |
36 #include "value.h" | 35 #include "value.h" |
37 #include "symrec.h" | 36 #include "symrec.h" |
38 | 37 |
(...skipping 27 matching lines...) Expand all Loading... |
66 int | 65 int |
67 yasm_bc_expand_common(yasm_bytecode *bc, int span, long old_val, long new_val, | 66 yasm_bc_expand_common(yasm_bytecode *bc, int span, long old_val, long new_val, |
68 /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres) | 67 /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres) |
69 { | 68 { |
70 yasm_internal_error(N_("bytecode does not have any dependent spans")); | 69 yasm_internal_error(N_("bytecode does not have any dependent spans")); |
71 /*@unreached@*/ | 70 /*@unreached@*/ |
72 return 0; | 71 return 0; |
73 } | 72 } |
74 | 73 |
75 int | 74 int |
76 yasm_bc_tobytes_common(yasm_bytecode *bc, unsigned char **bufp, void *d, | 75 yasm_bc_tobytes_common(yasm_bytecode *bc, unsigned char **buf, |
| 76 unsigned char *bufstart, void *d, |
77 yasm_output_value_func output_value, | 77 yasm_output_value_func output_value, |
78 /*@null@*/ yasm_output_reloc_func output_reloc) | 78 /*@null@*/ yasm_output_reloc_func output_reloc) |
79 { | 79 { |
80 yasm_internal_error(N_("bytecode cannot be converted to bytes")); | 80 yasm_internal_error(N_("bytecode cannot be converted to bytes")); |
81 /*@unreached@*/ | 81 /*@unreached@*/ |
82 return 0; | 82 return 0; |
83 } | 83 } |
84 | 84 |
85 void | 85 void |
86 yasm_bc_transform(yasm_bytecode *bc, const yasm_bytecode_callback *callback, | 86 yasm_bc_transform(yasm_bytecode *bc, const yasm_bytecode_callback *callback, |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 } | 298 } |
299 | 299 |
300 /*@null@*/ /*@only@*/ unsigned char * | 300 /*@null@*/ /*@only@*/ unsigned char * |
301 yasm_bc_tobytes(yasm_bytecode *bc, unsigned char *buf, unsigned long *bufsize, | 301 yasm_bc_tobytes(yasm_bytecode *bc, unsigned char *buf, unsigned long *bufsize, |
302 /*@out@*/ int *gap, void *d, | 302 /*@out@*/ int *gap, void *d, |
303 yasm_output_value_func output_value, | 303 yasm_output_value_func output_value, |
304 /*@null@*/ yasm_output_reloc_func output_reloc) | 304 /*@null@*/ yasm_output_reloc_func output_reloc) |
305 /*@sets *buf@*/ | 305 /*@sets *buf@*/ |
306 { | 306 { |
307 /*@only@*/ /*@null@*/ unsigned char *mybuf = NULL; | 307 /*@only@*/ /*@null@*/ unsigned char *mybuf = NULL; |
| 308 unsigned char *bufstart; |
308 unsigned char *origbuf, *destbuf; | 309 unsigned char *origbuf, *destbuf; |
309 long i; | 310 long i; |
310 int error = 0; | 311 int error = 0; |
311 | 312 |
312 long mult; | 313 long mult; |
313 if (yasm_bc_get_multiple(bc, &mult, 1) || mult == 0) { | 314 if (yasm_bc_get_multiple(bc, &mult, 1) || mult == 0) { |
314 *bufsize = 0; | 315 *bufsize = 0; |
315 return NULL; | 316 return NULL; |
316 } | 317 } |
317 bc->mult_int = mult; | 318 bc->mult_int = mult; |
318 | 319 |
319 /* special case for reserve bytecodes */ | 320 /* special case for reserve bytecodes */ |
320 if (bc->callback->special == YASM_BC_SPECIAL_RESERVE) { | 321 if (bc->callback->special == YASM_BC_SPECIAL_RESERVE) { |
321 *bufsize = bc->len*bc->mult_int; | 322 *bufsize = bc->len*bc->mult_int; |
322 *gap = 1; | 323 *gap = 1; |
323 return NULL; /* we didn't allocate a buffer */ | 324 return NULL; /* we didn't allocate a buffer */ |
324 } | 325 } |
325 *gap = 0; | 326 *gap = 0; |
326 | 327 |
327 if (*bufsize < bc->len*bc->mult_int) { | 328 if (*bufsize < bc->len*bc->mult_int) { |
328 mybuf = yasm_xmalloc(bc->len*bc->mult_int); | 329 mybuf = yasm_xmalloc(bc->len*bc->mult_int); |
329 destbuf = mybuf; | 330 destbuf = mybuf; |
330 } else | 331 } else |
331 destbuf = buf; | 332 destbuf = buf; |
| 333 bufstart = destbuf; |
332 | 334 |
333 *bufsize = bc->len*bc->mult_int; | 335 *bufsize = bc->len*bc->mult_int; |
334 | 336 |
335 if (!bc->callback) | 337 if (!bc->callback) |
336 yasm_internal_error(N_("got empty bytecode in bc_tobytes")); | 338 yasm_internal_error(N_("got empty bytecode in bc_tobytes")); |
337 else for (i=0; i<bc->mult_int; i++) { | 339 else for (i=0; i<bc->mult_int; i++) { |
338 origbuf = destbuf; | 340 origbuf = destbuf; |
339 error = bc->callback->tobytes(bc, &destbuf, d, output_value, | 341 error = bc->callback->tobytes(bc, &destbuf, bufstart, d, output_value, |
340 output_reloc); | 342 output_reloc); |
341 | 343 |
342 if (!error && ((unsigned long)(destbuf - origbuf) != bc->len)) | 344 if (!error && ((unsigned long)(destbuf - origbuf) != bc->len)) |
343 yasm_internal_error( | 345 yasm_internal_error( |
344 N_("written length does not match optimized length")); | 346 N_("written length does not match optimized length")); |
345 } | 347 } |
346 | 348 |
347 return mybuf; | 349 return mybuf; |
348 } | 350 } |
349 | 351 |
(...skipping 25 matching lines...) Expand all Loading... |
375 return bc->multiple; | 377 return bc->multiple; |
376 } | 378 } |
377 | 379 |
378 yasm_insn * | 380 yasm_insn * |
379 yasm_bc_get_insn(yasm_bytecode *bc) | 381 yasm_bc_get_insn(yasm_bytecode *bc) |
380 { | 382 { |
381 if (bc->callback->special != YASM_BC_SPECIAL_INSN) | 383 if (bc->callback->special != YASM_BC_SPECIAL_INSN) |
382 return NULL; | 384 return NULL; |
383 return (yasm_insn *)bc->contents; | 385 return (yasm_insn *)bc->contents; |
384 } | 386 } |
OLD | NEW |