2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Interface for machine code output
23 * @author Matthias Braun
32 #include "beemitter_binary.h"
36 static code_fragment_t *first_fragment;
37 static code_fragment_t *last_fragment;
38 static const unsigned CODE_FRAGMENT_MAGIC = 0x4643414d; /* "CFMA" */
40 struct obstack code_fragment_obst;
42 /** returns current fragment (the address stays only valid until the next
43 be_emit(8/16/32/entity) call!) */
44 code_fragment_t *be_get_current_fragment(void)
46 assert(obstack_object_size(&code_fragment_obst) >= sizeof(code_fragment_t));
47 code_fragment_t *fragment = obstack_base(&code_fragment_obst);
48 assert(fragment->magic == CODE_FRAGMENT_MAGIC);
53 /** allocates a new fragment on the obstack (warning: address is only valid
55 static void alloc_fragment(void)
57 code_fragment_t *fragment;
59 /* shouldn't have any growing fragments */
60 assert(obstack_object_size(&code_fragment_obst) == 0);
62 obstack_blank(&code_fragment_obst, sizeof(*fragment));
63 fragment = obstack_base(&code_fragment_obst);
64 memset(fragment, 0, sizeof(*fragment));
66 fragment->magic = CODE_FRAGMENT_MAGIC;
69 fragment->alignment = 1;
71 fragment->max_offset = UINT_MAX;
74 static code_fragment_t *finish_fragment(void)
76 code_fragment_t *fragment = be_get_current_fragment();
78 = obstack_object_size(&code_fragment_obst) - sizeof(*fragment);
80 fragment = (code_fragment_t*) obstack_finish(&code_fragment_obst);
81 last_fragment = fragment;
83 if (first_fragment == NULL)
84 first_fragment = fragment;
89 void be_start_code_emitter(void)
91 obstack_init(&code_fragment_obst);
92 first_fragment = NULL;
96 void be_start_new_fragment(void)
102 static void emit(FILE *file, const unsigned char *buffer, size_t len)
105 for (i = 0; i < len; ++i) {
107 fputs("\t.byte ", file);
108 for (i2 = i; i2 < i + 30 && i2 < len; ++i2) {
109 fprintf(file, "0x%02X", buffer[i2]);
116 static unsigned align(unsigned offset, unsigned alignment)
118 if (offset % alignment != 0) {
119 offset += alignment - (offset % alignment);
124 static bool determine_jumpsize_pass(const binary_emiter_interface_t *interface)
127 unsigned max_offset = 0;
128 bool changed = false;
129 code_fragment_t *fragment;
131 for (fragment = first_fragment; fragment != NULL;
132 fragment = fragment->next) {
133 unsigned alignment = fragment->alignment;
135 /* assure alignment */
136 offset = align(offset, alignment);
137 max_offset = align(max_offset, alignment);
139 if (offset != fragment->offset) {
141 fragment->offset = offset;
143 fragment->max_offset = max_offset;
146 offset += fragment->len;
147 max_offset += fragment->len;
148 interface->determine_jumpsize(fragment);
149 offset += fragment->jumpsize_min;
150 max_offset += fragment->jumpsize_max;
156 static void determine_offsets(const binary_emiter_interface_t *interface)
160 assert(first_fragment->alignment == 1);
161 first_fragment->offset = 0;
162 first_fragment->max_offset = 0;
164 /* pass1: encode minimum/maximum offsets into fragments */
166 changed = determine_jumpsize_pass(interface);
167 /* TODO: we should have an abort mode for the case when the offsets
168 don't converge fast enough. We could simply use a pessimistic
169 solution after a few iterations... */
173 void be_emit_code(FILE *output, const binary_emiter_interface_t *interface)
177 code_fragment_t *fragment;
181 /* determine near/far jumps */
182 determine_offsets(interface);
186 for (fragment = first_fragment; fragment != NULL;
187 fragment = fragment->next) {
188 unsigned char *jmpbuffer;
190 /* assure alignment by emitting nops */
191 assert(fragment->offset >= offset);
192 unsigned nops = fragment->offset - offset;
194 unsigned char *nopbuffer = obstack_alloc(&code_fragment_obst, nops);
195 interface->create_nops(nopbuffer, nops);
196 emit(output, nopbuffer, nops);
197 offset = fragment->offset;
198 obstack_free(&code_fragment_obst, nopbuffer);
201 /* emit the fragment */
202 emit(output, fragment->data, fragment->len);
203 offset += fragment->len;
206 jmpbuffer = obstack_alloc(&code_fragment_obst, fragment->jumpsize_min);
207 interface->emit_jump(fragment, jmpbuffer);
208 emit(output, jmpbuffer, fragment->jumpsize_min);
209 offset += fragment->jumpsize_min;
210 obstack_free(&code_fragment_obst, jmpbuffer);