blob: c1887dff7a5eded51a1c7e7e2a47c6a63f493022 [file] [log] [blame]
sewardj35421a32004-07-05 13:12:34 +00001
2/*---------------------------------------------------------------*/
3/*--- ---*/
sewardjc0ee2ed2004-07-27 10:29:41 +00004/*--- This file (main/vex_main.c) is ---*/
sewardj35421a32004-07-05 13:12:34 +00005/*--- Copyright (c) 2004 OpenWorks LLP. All rights reserved. ---*/
6/*--- ---*/
7/*---------------------------------------------------------------*/
8
sewardj887a11a2004-07-05 17:26:47 +00009#include "libvex.h"
sewardj81ec4182004-10-25 23:15:52 +000010#include "libvex_guest_x86.h"
sewardjf13a16a2004-07-05 17:10:14 +000011
sewardjc0ee2ed2004-07-27 10:29:41 +000012#include "main/vex_globals.h"
13#include "main/vex_util.h"
14#include "host-generic/h_generic_regs.h"
15#include "host-x86/hdefs.h"
16#include "guest-x86/gdefs.h"
sewardjedf4d692004-08-17 13:52:58 +000017#include "ir/iropt.h"
sewardj35421a32004-07-05 13:12:34 +000018
19
20/* This file contains the top level interface to the library. */
21
22/* --------- Initialise the library. --------- */
23
24/* Exported to library client. */
25
sewardj08613742004-10-25 13:01:45 +000026void LibVEX_default_VexControl ( /*OUT*/ VexControl* vcon )
27{
28 vcon->iropt_verbosity = 0;
29 vcon->iropt_level = 2;
30 vcon->iropt_precise_memory_exns = False;
31 vcon->iropt_unroll_thresh = 120;
32 vcon->guest_max_insns = 50;
33 vcon->guest_chase_thresh = 10;
34}
35
36
37/* Exported to library client. */
38
sewardj887a11a2004-07-05 17:26:47 +000039void LibVEX_Init (
sewardj35421a32004-07-05 13:12:34 +000040 /* failure exit function */
sewardj2b515872004-07-05 20:50:45 +000041 __attribute__ ((noreturn))
sewardj35421a32004-07-05 13:12:34 +000042 void (*failure_exit) ( void ),
43 /* logging output function */
44 void (*log_bytes) ( Char*, Int nbytes ),
45 /* debug paranoia level */
46 Int debuglevel,
sewardj58800ff2004-07-28 01:51:10 +000047 /* initial verbosity level */
sewardj35421a32004-07-05 13:12:34 +000048 Int verbosity,
49 /* Are we supporting valgrind checking? */
50 Bool valgrind_support,
sewardj08613742004-10-25 13:01:45 +000051 /* Control ... */
52 /*READONLY*/VexControl* vcon
sewardj35421a32004-07-05 13:12:34 +000053)
54{
sewardj08613742004-10-25 13:01:45 +000055 /* First off, do enough minimal setup so that the following
56 assertions can fail in a sane fashion, if need be. */
sewardjea602bc2004-10-14 21:40:12 +000057 vex_failure_exit = failure_exit;
58 vex_log_bytes = log_bytes;
59
60 /* Now it's safe to check parameters for sanity. */
sewardj35421a32004-07-05 13:12:34 +000061 vassert(!vex_initdone);
62 vassert(failure_exit);
sewardj35421a32004-07-05 13:12:34 +000063 vassert(log_bytes);
sewardj35421a32004-07-05 13:12:34 +000064 vassert(debuglevel >= 0);
sewardj35421a32004-07-05 13:12:34 +000065 vassert(verbosity >= 0);
sewardj08613742004-10-25 13:01:45 +000066
67 vassert(vcon->iropt_verbosity >= 0);
68 vassert(vcon->iropt_level >= 0);
69 vassert(vcon->iropt_level <= 2);
70 vassert(vcon->iropt_unroll_thresh >= 0);
71 vassert(vcon->iropt_unroll_thresh <= 400);
72 vassert(vcon->guest_max_insns >= 1);
73 vassert(vcon->guest_max_insns <= 100);
74 vassert(vcon->guest_chase_thresh >= 0);
75 vassert(vcon->guest_chase_thresh < vcon->guest_max_insns);
sewardj443cd9d2004-07-18 23:06:45 +000076
sewardj81ec4182004-10-25 23:15:52 +000077 /* All the guest state structs must have an 8-aligned size. */
78 vassert(0 == sizeof(VexGuestX86State) % 8);
79
sewardjea602bc2004-10-14 21:40:12 +000080 /* Check that Vex has been built with sizes of basic types as
81 stated in priv/libvex_basictypes.h. Failure of any of these is
82 a serious configuration error and should be corrected
83 immediately. If any of these assertions fail you can fully
84 expect Vex not to work properly, if at all. */
85
86 vassert(1 == sizeof(UChar));
87 vassert(1 == sizeof(Char));
88 vassert(2 == sizeof(UShort));
89 vassert(2 == sizeof(Short));
90 vassert(4 == sizeof(UInt));
91 vassert(4 == sizeof(Int));
92 vassert(8 == sizeof(ULong));
93 vassert(8 == sizeof(Long));
94 vassert(4 == sizeof(Float));
95 vassert(8 == sizeof(Double));
96 vassert(1 == sizeof(Bool));
97 vassert(4 == sizeof(Addr32));
98 vassert(8 == sizeof(Addr64));
99
100 vassert(sizeof(void*) == 4 || sizeof(void*) == 8);
101 vassert(sizeof(void*) == sizeof(int*));
102 vassert(sizeof(void*) == sizeof(HWord));
103
104 /* Really start up .. */
sewardj443cd9d2004-07-18 23:06:45 +0000105 vex_debuglevel = debuglevel;
106 vex_verbosity = verbosity;
107 vex_valgrind_support = valgrind_support;
sewardj08613742004-10-25 13:01:45 +0000108 vex_control = *vcon;
sewardj443cd9d2004-07-18 23:06:45 +0000109 vex_initdone = True;
110 LibVEX_SetAllocMode ( AllocModeTEMPORARY );
sewardj35421a32004-07-05 13:12:34 +0000111}
112
113
114/* --------- Make a translation. --------- */
115
116/* Exported to library client. */
117
sewardj887a11a2004-07-05 17:26:47 +0000118TranslateResult LibVEX_Translate (
sewardj35421a32004-07-05 13:12:34 +0000119 /* The instruction sets we are translating from and to. */
120 InsnSet iset_guest,
121 InsnSet iset_host,
122 /* IN: the block to translate, and its guest address. */
sewardj81bd5502004-07-21 18:49:27 +0000123 UChar* guest_bytes,
sewardj35421a32004-07-05 13:12:34 +0000124 Addr64 guest_bytes_addr,
125 /* OUT: the number of bytes actually read */
126 Int* guest_bytes_read,
127 /* IN: a place to put the resulting code, and its size */
sewardj81bd5502004-07-21 18:49:27 +0000128 UChar* host_bytes,
129 Int host_bytes_size,
sewardj35421a32004-07-05 13:12:34 +0000130 /* OUT: how much of the output area is used. */
131 Int* host_bytes_used,
sewardj49651f42004-10-28 22:11:04 +0000132 /* IN: optionally, two instrumentation functions. */
133 IRBB* (*instrument1) ( IRBB*, VexGuestLayoutInfo* ),
134 IRBB* (*instrument2) ( IRBB*, VexGuestLayoutInfo* ),
sewardjc5fc7aa2004-10-27 23:00:55 +0000135 HWord (*tool_findhelper) ( Char* ),
sewardj35421a32004-07-05 13:12:34 +0000136 /* IN: optionally, an access check function for guest code. */
sewardj58800ff2004-07-28 01:51:10 +0000137 Bool (*byte_accessible) ( Addr64 ),
138 /* IN: if > 0, use this verbosity for this bb */
139 Int bb_verbosity
sewardj35421a32004-07-05 13:12:34 +0000140)
141{
sewardj81bd5502004-07-21 18:49:27 +0000142 /* This the bundle of functions we need to do the back-end stuff
143 (insn selection, reg-alloc, assembly) whilst being insulated
144 from the target instruction set. */
sewardjf13a16a2004-07-05 17:10:14 +0000145 HReg* available_real_regs;
146 Int n_available_real_regs;
sewardj443cd9d2004-07-18 23:06:45 +0000147 Bool (*isMove) (HInstr*, HReg*, HReg*);
148 void (*getRegUsage) (HRegUsage*, HInstr*);
149 void (*mapRegs) (HRegRemap*, HInstr*);
150 HInstr* (*genSpill) ( HReg, Int );
151 HInstr* (*genReload) ( HReg, Int );
152 void (*ppInstr) ( HInstr* );
153 void (*ppReg) ( HReg );
sewardjc5fc7aa2004-10-27 23:00:55 +0000154 HInstrArray* (*iselBB) ( IRBB*, HWord(*)(Char*), HWord(*)(Char*) );
sewardj443cd9d2004-07-18 23:06:45 +0000155 IRBB* (*bbToIR) ( UChar*, Addr64, Int*,
156 Bool(*)(Addr64), Bool );
sewardj81bd5502004-07-21 18:49:27 +0000157 Int (*emit) ( UChar*, Int, HInstr* );
sewardjc5fc7aa2004-10-27 23:00:55 +0000158 HWord (*findHelper) ( Char* );
sewardj84ff0652004-08-23 16:16:08 +0000159 IRExpr* (*specHelper) ( Char*, IRExpr** );
sewardj8d2291c2004-10-25 14:50:21 +0000160 Bool (*preciseMemExnsFn) ( Int, Int );
sewardjf13a16a2004-07-05 17:10:14 +0000161
sewardj49651f42004-10-28 22:11:04 +0000162 VexGuestLayoutInfo* guest_layout;
163 Bool host_is_bigendian = False;
164 IRBB* irbb;
165 HInstrArray* vcode;
166 HInstrArray* rcode;
167 Int i, j, k, out_used, saved_verbosity, guest_sizeB;
168 UChar insn_bytes[32];
169 IRType guest_word_size;
sewardjf13a16a2004-07-05 17:10:14 +0000170
sewardj49651f42004-10-28 22:11:04 +0000171 guest_layout = NULL;
sewardj36ca5132004-07-24 13:12:23 +0000172 available_real_regs = NULL;
173 n_available_real_regs = 0;
174 isMove = NULL;
175 getRegUsage = NULL;
176 mapRegs = NULL;
177 genSpill = NULL;
178 genReload = NULL;
179 ppInstr = NULL;
180 ppReg = NULL;
181 iselBB = NULL;
182 bbToIR = NULL;
183 emit = NULL;
184 findHelper = NULL;
sewardj84ff0652004-08-23 16:16:08 +0000185 specHelper = NULL;
sewardj8d2291c2004-10-25 14:50:21 +0000186 preciseMemExnsFn = NULL;
sewardjc5fc7aa2004-10-27 23:00:55 +0000187 guest_word_size = Ity_INVALID;
sewardj36ca5132004-07-24 13:12:23 +0000188
sewardj58800ff2004-07-28 01:51:10 +0000189 saved_verbosity = vex_verbosity;
190 if (bb_verbosity > 0)
191 vex_verbosity = bb_verbosity;
192
sewardj35421a32004-07-05 13:12:34 +0000193 vassert(vex_initdone);
sewardj443cd9d2004-07-18 23:06:45 +0000194 LibVEX_ClearTemporary(False);
sewardjf13a16a2004-07-05 17:10:14 +0000195
196 /* First off, check that the guest and host insn sets
197 are supported. */
198 switch (iset_host) {
199 case InsnSetX86:
200 getAllocableRegs_X86 ( &n_available_real_regs,
201 &available_real_regs );
202 isMove = (Bool(*)(HInstr*,HReg*,HReg*)) isMove_X86Instr;
203 getRegUsage = (void(*)(HRegUsage*,HInstr*)) getRegUsage_X86Instr;
204 mapRegs = (void(*)(HRegRemap*,HInstr*)) mapRegs_X86Instr;
205 genSpill = (HInstr*(*)(HReg,Int)) genSpill_X86;
206 genReload = (HInstr*(*)(HReg,Int)) genReload_X86;
sewardj2b515872004-07-05 20:50:45 +0000207 ppInstr = (void(*)(HInstr*)) ppX86Instr;
208 ppReg = (void(*)(HReg)) ppHRegX86;
sewardjf13a16a2004-07-05 17:10:14 +0000209 iselBB = iselBB_X86;
sewardj81bd5502004-07-21 18:49:27 +0000210 emit = (Int(*)(UChar*,Int,HInstr*)) emit_X86Instr;
sewardjc9a65702004-07-07 16:32:57 +0000211 host_is_bigendian = False;
sewardjf13a16a2004-07-05 17:10:14 +0000212 break;
213 default:
sewardj887a11a2004-07-05 17:26:47 +0000214 vpanic("LibVEX_Translate: unsupported target insn set");
sewardjf13a16a2004-07-05 17:10:14 +0000215 }
216
217 switch (iset_guest) {
218 case InsnSetX86:
sewardj8d2291c2004-10-25 14:50:21 +0000219 preciseMemExnsFn = guest_x86_state_requires_precise_mem_exns;
220 bbToIR = bbToIR_X86Instr;
221 findHelper = x86guest_findhelper;
222 specHelper = x86guest_spechelper;
sewardj81ec4182004-10-25 23:15:52 +0000223 guest_sizeB = sizeof(VexGuestX86State);
sewardjc5fc7aa2004-10-27 23:00:55 +0000224 guest_word_size = Ity_I32;
sewardj49651f42004-10-28 22:11:04 +0000225 guest_layout = &x86guest_layout;
sewardjf13a16a2004-07-05 17:10:14 +0000226 break;
227 default:
sewardj887a11a2004-07-05 17:26:47 +0000228 vpanic("LibVEX_Translate: unsupported guest insn set");
sewardjf13a16a2004-07-05 17:10:14 +0000229 }
230
231 irbb = bbToIR ( guest_bytes,
232 guest_bytes_addr,
233 guest_bytes_read,
sewardjc9a65702004-07-07 16:32:57 +0000234 byte_accessible,
235 host_is_bigendian );
sewardjf13a16a2004-07-05 17:10:14 +0000236
237 if (irbb == NULL) {
238 /* Access failure. */
sewardj443cd9d2004-07-18 23:06:45 +0000239 LibVEX_ClearTemporary(False);
sewardj58800ff2004-07-28 01:51:10 +0000240 vex_verbosity = saved_verbosity;
sewardjf13a16a2004-07-05 17:10:14 +0000241 return TransAccessFail;
242 }
sewardjaa59f942004-10-09 09:34:36 +0000243
244 /* If debugging, show the raw guest bytes for this bb. */
245 if (vex_verbosity >= 2) {
246 UChar* p = guest_bytes;
247 vex_printf("\n");
248 vex_printf(". 0 %llx %d\n.", guest_bytes_addr, *guest_bytes_read );
249 for (i = 0; i < *guest_bytes_read; i++)
250 vex_printf(" %02x", (Int)p[i] );
251 vex_printf("\n");
252 }
253
254 /* Sanity check the initial IR. */
sewardjc5fc7aa2004-10-27 23:00:55 +0000255 sanityCheckIRBB(irbb, guest_word_size);
sewardje8e9d732004-07-16 21:03:45 +0000256
sewardjedf4d692004-08-17 13:52:58 +0000257 /* Clean it up, hopefully a lot. */
sewardj8d2291c2004-10-25 14:50:21 +0000258 irbb = do_iropt_BB ( irbb, specHelper, preciseMemExnsFn,
259 guest_bytes_addr );
sewardjc5fc7aa2004-10-27 23:00:55 +0000260 sanityCheckIRBB(irbb, guest_word_size);
sewardjedf4d692004-08-17 13:52:58 +0000261
262 if (vex_verbosity > 0) {
263 vex_printf("\n-------- After IR optimisation --------\n");
264 ppIRBB ( irbb );
265 vex_printf("\n");
266 }
267
sewardjf13a16a2004-07-05 17:10:14 +0000268 /* Get the thing instrumented. */
sewardj49651f42004-10-28 22:11:04 +0000269 if (instrument1)
270 irbb = (*instrument1)(irbb, guest_layout);
271 if (instrument2)
272 irbb = (*instrument2)(irbb, guest_layout);
273
274 if (instrument1 || instrument2)
sewardjc5fc7aa2004-10-27 23:00:55 +0000275 sanityCheckIRBB(irbb, guest_word_size);
sewardjf13a16a2004-07-05 17:10:14 +0000276
277 /* Turn it into virtual-registerised code. */
sewardj49651f42004-10-28 22:11:04 +0000278 do_deadcode_BB( irbb );
279 do_treebuild_BB( irbb );
sewardjc5fc7aa2004-10-27 23:00:55 +0000280 vcode = iselBB ( irbb, findHelper, tool_findhelper );
sewardjf13a16a2004-07-05 17:10:14 +0000281
sewardj1f40a0a2004-07-21 12:28:07 +0000282 if (vex_verbosity > 0) {
283 vex_printf("\n-------- Virtual registerised code --------\n");
284 for (i = 0; i < vcode->arr_used; i++) {
285 vex_printf("%3d ", i);
286 ppInstr(vcode->arr[i]);
287 vex_printf("\n");
288 }
sewardjfbcaf332004-07-08 01:46:01 +0000289 vex_printf("\n");
290 }
sewardjfbcaf332004-07-08 01:46:01 +0000291
sewardjf13a16a2004-07-05 17:10:14 +0000292 /* Register allocate. */
293 rcode = doRegisterAllocation ( vcode, available_real_regs,
294 n_available_real_regs,
295 isMove, getRegUsage, mapRegs,
sewardj81ec4182004-10-25 23:15:52 +0000296 genSpill, genReload, guest_sizeB,
sewardj2b515872004-07-05 20:50:45 +0000297 ppInstr, ppReg );
sewardjf13a16a2004-07-05 17:10:14 +0000298
sewardj1f40a0a2004-07-21 12:28:07 +0000299 if (vex_verbosity > 0) {
300 vex_printf("\n-------- Post-regalloc code --------\n");
301 for (i = 0; i < rcode->arr_used; i++) {
302 vex_printf("%3d ", i);
303 ppInstr(rcode->arr[i]);
304 vex_printf("\n");
305 }
sewardjfbcaf332004-07-08 01:46:01 +0000306 vex_printf("\n");
307 }
sewardjfbcaf332004-07-08 01:46:01 +0000308
sewardj81bd5502004-07-21 18:49:27 +0000309 /* Assemble */
sewardj81bd5502004-07-21 18:49:27 +0000310 out_used = 0; /* tracks along the host_bytes array */
311 for (i = 0; i < rcode->arr_used; i++) {
sewardjc5fc7aa2004-10-27 23:00:55 +0000312 if (vex_verbosity > 2) {
sewardjbad34a92004-07-22 01:14:11 +0000313 ppInstr(rcode->arr[i]);
314 vex_printf("\n");
315 }
sewardj81bd5502004-07-21 18:49:27 +0000316 j = (*emit)( insn_bytes, 32, rcode->arr[i] );
sewardjc5fc7aa2004-10-27 23:00:55 +0000317 if (vex_verbosity > 2) {
sewardjbad34a92004-07-22 01:14:11 +0000318 for (k = 0; k < j; k++)
sewardj86898e82004-07-22 17:26:12 +0000319 if (insn_bytes[k] < 16)
320 vex_printf("0%x ", (UInt)insn_bytes[k]);
321 else
322 vex_printf("%x ", (UInt)insn_bytes[k]);
sewardjbad34a92004-07-22 01:14:11 +0000323 vex_printf("\n\n");
324 }
sewardj81bd5502004-07-21 18:49:27 +0000325 if (out_used + j > host_bytes_size) {
326 LibVEX_ClearTemporary(False);
sewardj58800ff2004-07-28 01:51:10 +0000327 vex_verbosity = saved_verbosity;
sewardj81bd5502004-07-21 18:49:27 +0000328 return TransOutputFull;
329 }
330 for (k = 0; k < j; k++) {
331 host_bytes[out_used] = insn_bytes[k];
332 out_used++;
333 }
334 vassert(out_used <= host_bytes_size);
335 }
336 *host_bytes_used = out_used;
337
sewardj1f40a0a2004-07-21 12:28:07 +0000338 // LibVEX_ClearTemporary(True);
339 LibVEX_ClearTemporary(False);
sewardjf13a16a2004-07-05 17:10:14 +0000340
sewardj58800ff2004-07-28 01:51:10 +0000341 vex_verbosity = saved_verbosity;
sewardj35421a32004-07-05 13:12:34 +0000342 return TransOK;
343}
344
345
346
347/*---------------------------------------------------------------*/
sewardjc0ee2ed2004-07-27 10:29:41 +0000348/*--- end main/vex_main.c ---*/
sewardj35421a32004-07-05 13:12:34 +0000349/*---------------------------------------------------------------*/