4 * Copyright (C) 2005-2007 Peter Johnson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
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
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
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
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
25 * POSSIBILITY OF SUCH DAMAGE.
29 #include "libyasm-stdint.h"
39 typedef struct bytecode_align {
40 /*@only@*/ yasm_expr *boundary; /* alignment boundary */
42 /* What to fill intervening locations with, NULL if using code_fill */
43 /*@only@*/ /*@null@*/ yasm_expr *fill;
45 /* Maximum number of bytes to skip, NULL if no maximum. */
46 /*@only@*/ /*@null@*/ yasm_expr *maxskip;
48 /* Code fill, NULL if using 0 fill */
49 /*@null@*/ const unsigned char **code_fill;
52 static void bc_align_destroy(void *contents);
53 static void bc_align_print(const void *contents, FILE *f, int indent_level);
54 static void bc_align_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc);
55 static int bc_align_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span,
57 static int bc_align_expand(yasm_bytecode *bc, int span, long old_val,
58 long new_val, /*@out@*/ long *neg_thres,
59 /*@out@*/ long *pos_thres);
60 static int bc_align_tobytes(yasm_bytecode *bc, unsigned char **bufp,
61 unsigned char *bufstart, void *d,
62 yasm_output_value_func output_value,
63 /*@null@*/ yasm_output_reloc_func output_reloc);
65 static const yasm_bytecode_callback bc_align_callback = {
73 YASM_BC_SPECIAL_OFFSET
78 bc_align_destroy(void *contents)
80 bytecode_align *align = (bytecode_align *)contents;
82 yasm_expr_destroy(align->boundary);
84 yasm_expr_destroy(align->fill);
86 yasm_expr_destroy(align->maxskip);
91 bc_align_print(const void *contents, FILE *f, int indent_level)
93 const bytecode_align *align = (const bytecode_align *)contents;
94 fprintf(f, "%*s_Align_\n", indent_level, "");
95 fprintf(f, "%*sBoundary=", indent_level, "");
96 yasm_expr_print(align->boundary, f);
97 fprintf(f, "\n%*sFill=", indent_level, "");
98 yasm_expr_print(align->fill, f);
99 fprintf(f, "\n%*sMax Skip=", indent_level, "");
100 yasm_expr_print(align->maxskip, f);
105 bc_align_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc)
107 bytecode_align *align = (bytecode_align *)bc->contents;
108 if (!yasm_expr_get_intnum(&align->boundary, 0))
109 yasm_error_set(YASM_ERROR_NOT_CONSTANT,
110 N_("align boundary must be a constant"));
111 if (align->fill && !yasm_expr_get_intnum(&align->fill, 0))
112 yasm_error_set(YASM_ERROR_NOT_CONSTANT,
113 N_("align fill must be a constant"));
114 if (align->maxskip && !yasm_expr_get_intnum(&align->maxskip, 0))
115 yasm_error_set(YASM_ERROR_NOT_CONSTANT,
116 N_("align maximum skip must be a constant"));
120 bc_align_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span,
126 if (bc_align_expand(bc, 0, 0, (long)bc->offset, &neg_thres,
134 bc_align_expand(yasm_bytecode *bc, int span, long old_val, long new_val,
135 /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres)
137 bytecode_align *align = (bytecode_align *)bc->contents;
139 unsigned long boundary =
140 yasm_intnum_get_uint(yasm_expr_get_intnum(&align->boundary, 0));
144 *pos_thres = new_val;
148 end = (unsigned long)new_val;
149 if ((unsigned long)new_val & (boundary-1))
150 end = ((unsigned long)new_val & ~(boundary-1)) + boundary;
152 *pos_thres = (long)end;
153 bc->len = end - (unsigned long)new_val;
155 if (align->maxskip) {
156 unsigned long maxskip =
157 yasm_intnum_get_uint(yasm_expr_get_intnum(&align->maxskip, 0));
158 if (bc->len > maxskip) {
159 *pos_thres = (long)end-maxskip-1;
167 bc_align_tobytes(yasm_bytecode *bc, unsigned char **bufp,
168 unsigned char *bufstart, void *d,
169 yasm_output_value_func output_value,
170 /*@unused@*/ yasm_output_reloc_func output_reloc)
172 bytecode_align *align = (bytecode_align *)bc->contents;
174 unsigned long boundary =
175 yasm_intnum_get_uint(yasm_expr_get_intnum(&align->boundary, 0));
180 unsigned long end = bc->offset;
181 if (bc->offset & (boundary-1))
182 end = (bc->offset & ~(boundary-1)) + boundary;
183 len = end - bc->offset;
186 if (align->maxskip) {
187 unsigned long maxskip =
188 yasm_intnum_get_uint(yasm_expr_get_intnum(&align->maxskip, 0));
196 v = yasm_intnum_get_uint(yasm_expr_get_intnum(&align->fill, 0));
197 memset(*bufp, (int)v, len);
199 } else if (align->code_fill) {
200 unsigned long maxlen = 15;
201 while (!align->code_fill[maxlen] && maxlen>0)
204 yasm_error_set(YASM_ERROR_GENERAL,
205 N_("could not find any code alignment size"));
209 /* Fill with maximum code fill as much as possible */
210 while (len > maxlen) {
211 memcpy(*bufp, align->code_fill[maxlen], maxlen);
216 if (!align->code_fill[len]) {
217 yasm_error_set(YASM_ERROR_VALUE,
218 N_("invalid alignment size %d"), len);
221 /* Handle rest of code fill */
222 memcpy(*bufp, align->code_fill[len], len);
225 /* Just fill with 0 */
226 memset(*bufp, 0, len);
233 yasm_bc_create_align(yasm_expr *boundary, yasm_expr *fill,
234 yasm_expr *maxskip, const unsigned char **code_fill,
237 bytecode_align *align = yasm_xmalloc(sizeof(bytecode_align));
239 align->boundary = boundary;
241 align->maxskip = maxskip;
242 align->code_fill = code_fill;
244 return yasm_bc_create_common(&bc_align_callback, align, line);