blob: 2ba4e477da760bbadb6bdad6ea97730a31a984f2 [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,
sewardj35421a32004-07-05 13:12:34 +000047 /* Are we supporting valgrind checking? */
48 Bool valgrind_support,
sewardj08613742004-10-25 13:01:45 +000049 /* Control ... */
50 /*READONLY*/VexControl* vcon
sewardj35421a32004-07-05 13:12:34 +000051)
52{
sewardj08613742004-10-25 13:01:45 +000053 /* First off, do enough minimal setup so that the following
54 assertions can fail in a sane fashion, if need be. */
sewardjea602bc2004-10-14 21:40:12 +000055 vex_failure_exit = failure_exit;
56 vex_log_bytes = log_bytes;
57
58 /* Now it's safe to check parameters for sanity. */
sewardj35421a32004-07-05 13:12:34 +000059 vassert(!vex_initdone);
60 vassert(failure_exit);
sewardj35421a32004-07-05 13:12:34 +000061 vassert(log_bytes);
sewardj35421a32004-07-05 13:12:34 +000062 vassert(debuglevel >= 0);
sewardj08613742004-10-25 13:01:45 +000063
64 vassert(vcon->iropt_verbosity >= 0);
65 vassert(vcon->iropt_level >= 0);
66 vassert(vcon->iropt_level <= 2);
67 vassert(vcon->iropt_unroll_thresh >= 0);
68 vassert(vcon->iropt_unroll_thresh <= 400);
69 vassert(vcon->guest_max_insns >= 1);
70 vassert(vcon->guest_max_insns <= 100);
71 vassert(vcon->guest_chase_thresh >= 0);
72 vassert(vcon->guest_chase_thresh < vcon->guest_max_insns);
sewardj443cd9d2004-07-18 23:06:45 +000073
sewardj81ec4182004-10-25 23:15:52 +000074 /* All the guest state structs must have an 8-aligned size. */
75 vassert(0 == sizeof(VexGuestX86State) % 8);
76
sewardjea602bc2004-10-14 21:40:12 +000077 /* Check that Vex has been built with sizes of basic types as
78 stated in priv/libvex_basictypes.h. Failure of any of these is
79 a serious configuration error and should be corrected
80 immediately. If any of these assertions fail you can fully
81 expect Vex not to work properly, if at all. */
82
83 vassert(1 == sizeof(UChar));
84 vassert(1 == sizeof(Char));
85 vassert(2 == sizeof(UShort));
86 vassert(2 == sizeof(Short));
87 vassert(4 == sizeof(UInt));
88 vassert(4 == sizeof(Int));
89 vassert(8 == sizeof(ULong));
90 vassert(8 == sizeof(Long));
91 vassert(4 == sizeof(Float));
92 vassert(8 == sizeof(Double));
93 vassert(1 == sizeof(Bool));
94 vassert(4 == sizeof(Addr32));
95 vassert(8 == sizeof(Addr64));
96
97 vassert(sizeof(void*) == 4 || sizeof(void*) == 8);
98 vassert(sizeof(void*) == sizeof(int*));
99 vassert(sizeof(void*) == sizeof(HWord));
100
101 /* Really start up .. */
sewardj443cd9d2004-07-18 23:06:45 +0000102 vex_debuglevel = debuglevel;
sewardj443cd9d2004-07-18 23:06:45 +0000103 vex_valgrind_support = valgrind_support;
sewardj08613742004-10-25 13:01:45 +0000104 vex_control = *vcon;
sewardj443cd9d2004-07-18 23:06:45 +0000105 vex_initdone = True;
106 LibVEX_SetAllocMode ( AllocModeTEMPORARY );
sewardj35421a32004-07-05 13:12:34 +0000107}
108
109
110/* --------- Make a translation. --------- */
111
112/* Exported to library client. */
113
sewardj887a11a2004-07-05 17:26:47 +0000114TranslateResult LibVEX_Translate (
sewardj35421a32004-07-05 13:12:34 +0000115 /* The instruction sets we are translating from and to. */
116 InsnSet iset_guest,
117 InsnSet iset_host,
118 /* IN: the block to translate, and its guest address. */
sewardj81bd5502004-07-21 18:49:27 +0000119 UChar* guest_bytes,
sewardj35421a32004-07-05 13:12:34 +0000120 Addr64 guest_bytes_addr,
121 /* OUT: the number of bytes actually read */
122 Int* guest_bytes_read,
123 /* IN: a place to put the resulting code, and its size */
sewardj81bd5502004-07-21 18:49:27 +0000124 UChar* host_bytes,
125 Int host_bytes_size,
sewardj35421a32004-07-05 13:12:34 +0000126 /* OUT: how much of the output area is used. */
127 Int* host_bytes_used,
sewardj49651f42004-10-28 22:11:04 +0000128 /* IN: optionally, two instrumentation functions. */
sewardjcf787902004-11-03 09:08:33 +0000129 IRBB* (*instrument1) ( IRBB*, VexGuestLayout*, IRType hWordTy ),
130 IRBB* (*instrument2) ( IRBB*, VexGuestLayout*, IRType hWordTy ),
sewardj9578a8b2004-11-04 19:44:48 +0000131 Bool cleanup_after_instrumentation,
sewardj35421a32004-07-05 13:12:34 +0000132 /* IN: optionally, an access check function for guest code. */
sewardj58800ff2004-07-28 01:51:10 +0000133 Bool (*byte_accessible) ( Addr64 ),
sewardjf48ac192004-10-29 00:41:29 +0000134 /* IN: debug: trace vex activity at various points */
135 Int traceflags
sewardj35421a32004-07-05 13:12:34 +0000136)
137{
sewardj81bd5502004-07-21 18:49:27 +0000138 /* This the bundle of functions we need to do the back-end stuff
139 (insn selection, reg-alloc, assembly) whilst being insulated
140 from the target instruction set. */
sewardjf13a16a2004-07-05 17:10:14 +0000141 HReg* available_real_regs;
142 Int n_available_real_regs;
sewardj443cd9d2004-07-18 23:06:45 +0000143 Bool (*isMove) (HInstr*, HReg*, HReg*);
144 void (*getRegUsage) (HRegUsage*, HInstr*);
145 void (*mapRegs) (HRegRemap*, HInstr*);
146 HInstr* (*genSpill) ( HReg, Int );
147 HInstr* (*genReload) ( HReg, Int );
148 void (*ppInstr) ( HInstr* );
149 void (*ppReg) ( HReg );
sewardj8ea867b2004-10-30 19:03:02 +0000150 HInstrArray* (*iselBB) ( IRBB* );
sewardj443cd9d2004-07-18 23:06:45 +0000151 IRBB* (*bbToIR) ( UChar*, Addr64, Int*,
152 Bool(*)(Addr64), Bool );
sewardj81bd5502004-07-21 18:49:27 +0000153 Int (*emit) ( UChar*, Int, HInstr* );
sewardj84ff0652004-08-23 16:16:08 +0000154 IRExpr* (*specHelper) ( Char*, IRExpr** );
sewardj8d2291c2004-10-25 14:50:21 +0000155 Bool (*preciseMemExnsFn) ( Int, Int );
sewardjf13a16a2004-07-05 17:10:14 +0000156
sewardjeeac8412004-11-02 00:26:55 +0000157 VexGuestLayout* guest_layout;
158 Bool host_is_bigendian = False;
159 IRBB* irbb;
160 HInstrArray* vcode;
161 HInstrArray* rcode;
162 Int i, j, k, out_used, guest_sizeB;
163 UChar insn_bytes[32];
sewardjcf787902004-11-03 09:08:33 +0000164 IRType guest_word_type;
165 IRType host_word_type;
sewardjf13a16a2004-07-05 17:10:14 +0000166
sewardj49651f42004-10-28 22:11:04 +0000167 guest_layout = NULL;
sewardj36ca5132004-07-24 13:12:23 +0000168 available_real_regs = NULL;
169 n_available_real_regs = 0;
170 isMove = NULL;
171 getRegUsage = NULL;
172 mapRegs = NULL;
173 genSpill = NULL;
174 genReload = NULL;
175 ppInstr = NULL;
176 ppReg = NULL;
177 iselBB = NULL;
178 bbToIR = NULL;
179 emit = NULL;
sewardj84ff0652004-08-23 16:16:08 +0000180 specHelper = NULL;
sewardj8d2291c2004-10-25 14:50:21 +0000181 preciseMemExnsFn = NULL;
sewardjcf787902004-11-03 09:08:33 +0000182 guest_word_type = Ity_INVALID;
183 host_word_type = Ity_INVALID;
sewardj36ca5132004-07-24 13:12:23 +0000184
sewardjf48ac192004-10-29 00:41:29 +0000185 vex_traceflags = traceflags;
sewardj58800ff2004-07-28 01:51:10 +0000186
sewardj35421a32004-07-05 13:12:34 +0000187 vassert(vex_initdone);
sewardj443cd9d2004-07-18 23:06:45 +0000188 LibVEX_ClearTemporary(False);
sewardjf13a16a2004-07-05 17:10:14 +0000189
190 /* First off, check that the guest and host insn sets
191 are supported. */
192 switch (iset_host) {
193 case InsnSetX86:
194 getAllocableRegs_X86 ( &n_available_real_regs,
195 &available_real_regs );
196 isMove = (Bool(*)(HInstr*,HReg*,HReg*)) isMove_X86Instr;
197 getRegUsage = (void(*)(HRegUsage*,HInstr*)) getRegUsage_X86Instr;
198 mapRegs = (void(*)(HRegRemap*,HInstr*)) mapRegs_X86Instr;
199 genSpill = (HInstr*(*)(HReg,Int)) genSpill_X86;
200 genReload = (HInstr*(*)(HReg,Int)) genReload_X86;
sewardj2b515872004-07-05 20:50:45 +0000201 ppInstr = (void(*)(HInstr*)) ppX86Instr;
202 ppReg = (void(*)(HReg)) ppHRegX86;
sewardjf13a16a2004-07-05 17:10:14 +0000203 iselBB = iselBB_X86;
sewardj81bd5502004-07-21 18:49:27 +0000204 emit = (Int(*)(UChar*,Int,HInstr*)) emit_X86Instr;
sewardjc9a65702004-07-07 16:32:57 +0000205 host_is_bigendian = False;
sewardjcf787902004-11-03 09:08:33 +0000206 host_word_type = Ity_I32;
sewardjf13a16a2004-07-05 17:10:14 +0000207 break;
208 default:
sewardj887a11a2004-07-05 17:26:47 +0000209 vpanic("LibVEX_Translate: unsupported target insn set");
sewardjf13a16a2004-07-05 17:10:14 +0000210 }
211
212 switch (iset_guest) {
213 case InsnSetX86:
sewardj8d2291c2004-10-25 14:50:21 +0000214 preciseMemExnsFn = guest_x86_state_requires_precise_mem_exns;
215 bbToIR = bbToIR_X86Instr;
sewardj8d2291c2004-10-25 14:50:21 +0000216 specHelper = x86guest_spechelper;
sewardj81ec4182004-10-25 23:15:52 +0000217 guest_sizeB = sizeof(VexGuestX86State);
sewardjcf787902004-11-03 09:08:33 +0000218 guest_word_type = Ity_I32;
sewardj49651f42004-10-28 22:11:04 +0000219 guest_layout = &x86guest_layout;
sewardjf13a16a2004-07-05 17:10:14 +0000220 break;
221 default:
sewardj887a11a2004-07-05 17:26:47 +0000222 vpanic("LibVEX_Translate: unsupported guest insn set");
sewardjf13a16a2004-07-05 17:10:14 +0000223 }
224
sewardjf48ac192004-10-29 00:41:29 +0000225 if (vex_traceflags & VEX_TRACE_FE)
226 vex_printf("\n------------------------"
227 " Front end "
228 "------------------------\n\n");
229
sewardjf13a16a2004-07-05 17:10:14 +0000230 irbb = bbToIR ( guest_bytes,
231 guest_bytes_addr,
232 guest_bytes_read,
sewardjc9a65702004-07-07 16:32:57 +0000233 byte_accessible,
234 host_is_bigendian );
sewardjf13a16a2004-07-05 17:10:14 +0000235
236 if (irbb == NULL) {
237 /* Access failure. */
sewardj443cd9d2004-07-18 23:06:45 +0000238 LibVEX_ClearTemporary(False);
sewardjf48ac192004-10-29 00:41:29 +0000239 vex_traceflags = 0;
sewardjf13a16a2004-07-05 17:10:14 +0000240 return TransAccessFail;
241 }
sewardjaa59f942004-10-09 09:34:36 +0000242
243 /* If debugging, show the raw guest bytes for this bb. */
sewardjf48ac192004-10-29 00:41:29 +0000244 if (vex_traceflags & VEX_TRACE_FE) {
sewardjaa59f942004-10-09 09:34:36 +0000245 UChar* p = guest_bytes;
sewardjaa59f942004-10-09 09:34:36 +0000246 vex_printf(". 0 %llx %d\n.", guest_bytes_addr, *guest_bytes_read );
247 for (i = 0; i < *guest_bytes_read; i++)
248 vex_printf(" %02x", (Int)p[i] );
sewardjf48ac192004-10-29 00:41:29 +0000249 vex_printf("\n\n");
sewardjaa59f942004-10-09 09:34:36 +0000250 }
251
252 /* Sanity check the initial IR. */
sewardjcf787902004-11-03 09:08:33 +0000253 sanityCheckIRBB(irbb, guest_word_type);
sewardje8e9d732004-07-16 21:03:45 +0000254
sewardjedf4d692004-08-17 13:52:58 +0000255 /* Clean it up, hopefully a lot. */
sewardj8d2291c2004-10-25 14:50:21 +0000256 irbb = do_iropt_BB ( irbb, specHelper, preciseMemExnsFn,
257 guest_bytes_addr );
sewardjcf787902004-11-03 09:08:33 +0000258 sanityCheckIRBB(irbb, guest_word_type);
sewardjedf4d692004-08-17 13:52:58 +0000259
sewardjf48ac192004-10-29 00:41:29 +0000260 if (vex_traceflags & VEX_TRACE_OPT1) {
261 vex_printf("\n------------------------"
262 " After pre-instr IR optimisation "
263 "------------------------\n\n");
sewardjedf4d692004-08-17 13:52:58 +0000264 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)
sewardjcf787902004-11-03 09:08:33 +0000270 irbb = (*instrument1)(irbb, guest_layout, host_word_type);
sewardj49651f42004-10-28 22:11:04 +0000271 if (instrument2)
sewardjcf787902004-11-03 09:08:33 +0000272 irbb = (*instrument2)(irbb, guest_layout, host_word_type);
sewardj49651f42004-10-28 22:11:04 +0000273
sewardjf48ac192004-10-29 00:41:29 +0000274 if (vex_traceflags & VEX_TRACE_INST) {
275 vex_printf("\n------------------------"
276 " After instrumentation "
277 "------------------------\n\n");
278 ppIRBB ( irbb );
279 vex_printf("\n");
280 }
281
sewardj49651f42004-10-28 22:11:04 +0000282 if (instrument1 || instrument2)
sewardjcf787902004-11-03 09:08:33 +0000283 sanityCheckIRBB(irbb, guest_word_type);
sewardjf13a16a2004-07-05 17:10:14 +0000284
sewardj9578a8b2004-11-04 19:44:48 +0000285 /* Do a post-instrumentation cleanup pass. */
286 if (cleanup_after_instrumentation) {
287 do_deadcode_BB( irbb );
288 irbb = cprop_BB( irbb );
289 do_deadcode_BB( irbb );
290 sanityCheckIRBB(irbb, guest_word_type);
291 }
292
293 if (vex_traceflags & VEX_TRACE_OPT2) {
294 vex_printf("\n------------------------"
295 " After post-instr IR optimisation "
296 "------------------------\n\n");
297 ppIRBB ( irbb );
298 vex_printf("\n");
299 }
300
sewardjf13a16a2004-07-05 17:10:14 +0000301 /* Turn it into virtual-registerised code. */
sewardj49651f42004-10-28 22:11:04 +0000302 do_deadcode_BB( irbb );
303 do_treebuild_BB( irbb );
sewardjf48ac192004-10-29 00:41:29 +0000304
305 if (vex_traceflags & VEX_TRACE_TREES) {
306 vex_printf("\n------------------------"
307 " After tree-building "
308 "------------------------\n\n");
309 ppIRBB ( irbb );
310 vex_printf("\n");
311 }
312
313 if (vex_traceflags & VEX_TRACE_VCODE)
314 vex_printf("\n------------------------"
315 " Instruction selection "
316 "------------------------\n");
317
sewardj8ea867b2004-10-30 19:03:02 +0000318 vcode = iselBB ( irbb );
sewardjf13a16a2004-07-05 17:10:14 +0000319
sewardjf48ac192004-10-29 00:41:29 +0000320 if (vex_traceflags & VEX_TRACE_VCODE)
321 vex_printf("\n");
322
sewardjf48ac192004-10-29 00:41:29 +0000323 if (vex_traceflags & VEX_TRACE_VCODE) {
sewardj1f40a0a2004-07-21 12:28:07 +0000324 for (i = 0; i < vcode->arr_used; i++) {
325 vex_printf("%3d ", i);
326 ppInstr(vcode->arr[i]);
327 vex_printf("\n");
328 }
sewardjfbcaf332004-07-08 01:46:01 +0000329 vex_printf("\n");
330 }
sewardjfbcaf332004-07-08 01:46:01 +0000331
sewardjf13a16a2004-07-05 17:10:14 +0000332 /* Register allocate. */
333 rcode = doRegisterAllocation ( vcode, available_real_regs,
334 n_available_real_regs,
335 isMove, getRegUsage, mapRegs,
sewardj81ec4182004-10-25 23:15:52 +0000336 genSpill, genReload, guest_sizeB,
sewardj2b515872004-07-05 20:50:45 +0000337 ppInstr, ppReg );
sewardjf13a16a2004-07-05 17:10:14 +0000338
sewardjf48ac192004-10-29 00:41:29 +0000339 if (vex_traceflags & VEX_TRACE_RCODE) {
340 vex_printf("\n------------------------"
341 " Register-allocated code "
342 "------------------------\n\n");
sewardj1f40a0a2004-07-21 12:28:07 +0000343 for (i = 0; i < rcode->arr_used; i++) {
344 vex_printf("%3d ", i);
345 ppInstr(rcode->arr[i]);
346 vex_printf("\n");
347 }
sewardjfbcaf332004-07-08 01:46:01 +0000348 vex_printf("\n");
349 }
sewardjfbcaf332004-07-08 01:46:01 +0000350
sewardj81bd5502004-07-21 18:49:27 +0000351 /* Assemble */
sewardjf48ac192004-10-29 00:41:29 +0000352 if (vex_traceflags & VEX_TRACE_ASM) {
353 vex_printf("\n------------------------"
354 " Assembly "
355 "------------------------\n\n");
356 }
357
sewardj81bd5502004-07-21 18:49:27 +0000358 out_used = 0; /* tracks along the host_bytes array */
359 for (i = 0; i < rcode->arr_used; i++) {
sewardjf48ac192004-10-29 00:41:29 +0000360 if (vex_traceflags & VEX_TRACE_ASM) {
sewardjbad34a92004-07-22 01:14:11 +0000361 ppInstr(rcode->arr[i]);
362 vex_printf("\n");
363 }
sewardj81bd5502004-07-21 18:49:27 +0000364 j = (*emit)( insn_bytes, 32, rcode->arr[i] );
sewardjf48ac192004-10-29 00:41:29 +0000365 if (vex_traceflags & VEX_TRACE_ASM) {
sewardjbad34a92004-07-22 01:14:11 +0000366 for (k = 0; k < j; k++)
sewardj86898e82004-07-22 17:26:12 +0000367 if (insn_bytes[k] < 16)
368 vex_printf("0%x ", (UInt)insn_bytes[k]);
369 else
370 vex_printf("%x ", (UInt)insn_bytes[k]);
sewardjbad34a92004-07-22 01:14:11 +0000371 vex_printf("\n\n");
372 }
sewardj81bd5502004-07-21 18:49:27 +0000373 if (out_used + j > host_bytes_size) {
374 LibVEX_ClearTemporary(False);
sewardjf48ac192004-10-29 00:41:29 +0000375 vex_traceflags = 0;
sewardj81bd5502004-07-21 18:49:27 +0000376 return TransOutputFull;
377 }
378 for (k = 0; k < j; k++) {
379 host_bytes[out_used] = insn_bytes[k];
380 out_used++;
381 }
382 vassert(out_used <= host_bytes_size);
383 }
384 *host_bytes_used = out_used;
385
sewardj1f40a0a2004-07-21 12:28:07 +0000386 LibVEX_ClearTemporary(False);
sewardjf13a16a2004-07-05 17:10:14 +0000387
sewardjf48ac192004-10-29 00:41:29 +0000388 vex_traceflags = 0;
sewardj35421a32004-07-05 13:12:34 +0000389 return TransOK;
390}
391
392
393
394/*---------------------------------------------------------------*/
sewardjc0ee2ed2004-07-27 10:29:41 +0000395/*--- end main/vex_main.c ---*/
sewardj35421a32004-07-05 13:12:34 +0000396/*---------------------------------------------------------------*/