blob: 878224bac94b0b06883f2ffac5d1677454f3c3f1 [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"
sewardj8d2291c2004-10-25 14:50:21 +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
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;
103 vex_verbosity = verbosity;
104 vex_valgrind_support = valgrind_support;
sewardj08613742004-10-25 13:01:45 +0000105 vex_control = *vcon;
sewardj443cd9d2004-07-18 23:06:45 +0000106 vex_initdone = True;
107 LibVEX_SetAllocMode ( AllocModeTEMPORARY );
sewardj35421a32004-07-05 13:12:34 +0000108}
109
110
111/* --------- Make a translation. --------- */
112
113/* Exported to library client. */
114
sewardj887a11a2004-07-05 17:26:47 +0000115TranslateResult LibVEX_Translate (
sewardj35421a32004-07-05 13:12:34 +0000116 /* The instruction sets we are translating from and to. */
117 InsnSet iset_guest,
118 InsnSet iset_host,
119 /* IN: the block to translate, and its guest address. */
sewardj81bd5502004-07-21 18:49:27 +0000120 UChar* guest_bytes,
sewardj35421a32004-07-05 13:12:34 +0000121 Addr64 guest_bytes_addr,
122 /* OUT: the number of bytes actually read */
123 Int* guest_bytes_read,
124 /* IN: a place to put the resulting code, and its size */
sewardj81bd5502004-07-21 18:49:27 +0000125 UChar* host_bytes,
126 Int host_bytes_size,
sewardj35421a32004-07-05 13:12:34 +0000127 /* OUT: how much of the output area is used. */
128 Int* host_bytes_used,
129 /* IN: optionally, an instrumentation function. */
sewardjf13a16a2004-07-05 17:10:14 +0000130 IRBB* (*instrument) ( IRBB* ),
sewardj35421a32004-07-05 13:12:34 +0000131 /* IN: optionally, an access check function for guest code. */
sewardj58800ff2004-07-28 01:51:10 +0000132 Bool (*byte_accessible) ( Addr64 ),
133 /* IN: if > 0, use this verbosity for this bb */
134 Int bb_verbosity
sewardj35421a32004-07-05 13:12:34 +0000135)
136{
sewardj81bd5502004-07-21 18:49:27 +0000137 /* This the bundle of functions we need to do the back-end stuff
138 (insn selection, reg-alloc, assembly) whilst being insulated
139 from the target instruction set. */
sewardjf13a16a2004-07-05 17:10:14 +0000140 HReg* available_real_regs;
141 Int n_available_real_regs;
sewardj443cd9d2004-07-18 23:06:45 +0000142 Bool (*isMove) (HInstr*, HReg*, HReg*);
143 void (*getRegUsage) (HRegUsage*, HInstr*);
144 void (*mapRegs) (HRegRemap*, HInstr*);
145 HInstr* (*genSpill) ( HReg, Int );
146 HInstr* (*genReload) ( HReg, Int );
147 void (*ppInstr) ( HInstr* );
148 void (*ppReg) ( HReg );
sewardj36ca5132004-07-24 13:12:23 +0000149 HInstrArray* (*iselBB) ( IRBB*, Addr64(*)(Char*) );
sewardj443cd9d2004-07-18 23:06:45 +0000150 IRBB* (*bbToIR) ( UChar*, Addr64, Int*,
151 Bool(*)(Addr64), Bool );
sewardj81bd5502004-07-21 18:49:27 +0000152 Int (*emit) ( UChar*, Int, HInstr* );
sewardj36ca5132004-07-24 13:12:23 +0000153 Addr64 (*findHelper) ( Char* );
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
sewardjc9a65702004-07-07 16:32:57 +0000157 Bool host_is_bigendian = False;
sewardjf13a16a2004-07-05 17:10:14 +0000158 IRBB* irbb;
159 HInstrArray* vcode;
160 HInstrArray* rcode;
sewardj58800ff2004-07-28 01:51:10 +0000161 Int i, j, k, out_used, saved_verbosity;
sewardj2e56f9f2004-07-24 01:24:38 +0000162 UChar insn_bytes[32];
sewardjf13a16a2004-07-05 17:10:14 +0000163
sewardj36ca5132004-07-24 13:12:23 +0000164 available_real_regs = NULL;
165 n_available_real_regs = 0;
166 isMove = NULL;
167 getRegUsage = NULL;
168 mapRegs = NULL;
169 genSpill = NULL;
170 genReload = NULL;
171 ppInstr = NULL;
172 ppReg = NULL;
173 iselBB = NULL;
174 bbToIR = NULL;
175 emit = NULL;
176 findHelper = NULL;
sewardj84ff0652004-08-23 16:16:08 +0000177 specHelper = NULL;
sewardj8d2291c2004-10-25 14:50:21 +0000178 preciseMemExnsFn = NULL;
sewardj36ca5132004-07-24 13:12:23 +0000179
sewardj58800ff2004-07-28 01:51:10 +0000180 saved_verbosity = vex_verbosity;
181 if (bb_verbosity > 0)
182 vex_verbosity = bb_verbosity;
183
sewardj35421a32004-07-05 13:12:34 +0000184 vassert(vex_initdone);
sewardj443cd9d2004-07-18 23:06:45 +0000185 LibVEX_ClearTemporary(False);
sewardjf13a16a2004-07-05 17:10:14 +0000186
187 /* First off, check that the guest and host insn sets
188 are supported. */
189 switch (iset_host) {
190 case InsnSetX86:
191 getAllocableRegs_X86 ( &n_available_real_regs,
192 &available_real_regs );
193 isMove = (Bool(*)(HInstr*,HReg*,HReg*)) isMove_X86Instr;
194 getRegUsage = (void(*)(HRegUsage*,HInstr*)) getRegUsage_X86Instr;
195 mapRegs = (void(*)(HRegRemap*,HInstr*)) mapRegs_X86Instr;
196 genSpill = (HInstr*(*)(HReg,Int)) genSpill_X86;
197 genReload = (HInstr*(*)(HReg,Int)) genReload_X86;
sewardj2b515872004-07-05 20:50:45 +0000198 ppInstr = (void(*)(HInstr*)) ppX86Instr;
199 ppReg = (void(*)(HReg)) ppHRegX86;
sewardjf13a16a2004-07-05 17:10:14 +0000200 iselBB = iselBB_X86;
sewardj81bd5502004-07-21 18:49:27 +0000201 emit = (Int(*)(UChar*,Int,HInstr*)) emit_X86Instr;
sewardjc9a65702004-07-07 16:32:57 +0000202 host_is_bigendian = False;
sewardjf13a16a2004-07-05 17:10:14 +0000203 break;
204 default:
sewardj887a11a2004-07-05 17:26:47 +0000205 vpanic("LibVEX_Translate: unsupported target insn set");
sewardjf13a16a2004-07-05 17:10:14 +0000206 }
207
208 switch (iset_guest) {
209 case InsnSetX86:
sewardj8d2291c2004-10-25 14:50:21 +0000210 preciseMemExnsFn = guest_x86_state_requires_precise_mem_exns;
211 bbToIR = bbToIR_X86Instr;
212 findHelper = x86guest_findhelper;
213 specHelper = x86guest_spechelper;
sewardjf13a16a2004-07-05 17:10:14 +0000214 break;
215 default:
sewardj887a11a2004-07-05 17:26:47 +0000216 vpanic("LibVEX_Translate: unsupported guest insn set");
sewardjf13a16a2004-07-05 17:10:14 +0000217 }
218
219 irbb = bbToIR ( guest_bytes,
220 guest_bytes_addr,
221 guest_bytes_read,
sewardjc9a65702004-07-07 16:32:57 +0000222 byte_accessible,
223 host_is_bigendian );
sewardjf13a16a2004-07-05 17:10:14 +0000224
225 if (irbb == NULL) {
226 /* Access failure. */
sewardj443cd9d2004-07-18 23:06:45 +0000227 LibVEX_ClearTemporary(False);
sewardj58800ff2004-07-28 01:51:10 +0000228 vex_verbosity = saved_verbosity;
sewardjf13a16a2004-07-05 17:10:14 +0000229 return TransAccessFail;
230 }
sewardjaa59f942004-10-09 09:34:36 +0000231
232 /* If debugging, show the raw guest bytes for this bb. */
233 if (vex_verbosity >= 2) {
234 UChar* p = guest_bytes;
235 vex_printf("\n");
236 vex_printf(". 0 %llx %d\n.", guest_bytes_addr, *guest_bytes_read );
237 for (i = 0; i < *guest_bytes_read; i++)
238 vex_printf(" %02x", (Int)p[i] );
239 vex_printf("\n");
240 }
241
242 /* Sanity check the initial IR. */
sewardj35439212004-07-14 22:36:10 +0000243 sanityCheckIRBB(irbb, Ity_I32);
sewardje8e9d732004-07-16 21:03:45 +0000244
sewardjedf4d692004-08-17 13:52:58 +0000245 /* Clean it up, hopefully a lot. */
sewardj8d2291c2004-10-25 14:50:21 +0000246 irbb = do_iropt_BB ( irbb, specHelper, preciseMemExnsFn,
247 guest_bytes_addr );
sewardjd7cb8532004-08-17 23:59:23 +0000248 sanityCheckIRBB(irbb, Ity_I32);
sewardjedf4d692004-08-17 13:52:58 +0000249
250 if (vex_verbosity > 0) {
251 vex_printf("\n-------- After IR optimisation --------\n");
252 ppIRBB ( irbb );
253 vex_printf("\n");
254 }
255
sewardjf13a16a2004-07-05 17:10:14 +0000256 /* Get the thing instrumented. */
257 if (instrument)
258 irbb = (*instrument)(irbb);
259
260 /* Turn it into virtual-registerised code. */
sewardj36ca5132004-07-24 13:12:23 +0000261 vcode = iselBB ( irbb, findHelper );
sewardjf13a16a2004-07-05 17:10:14 +0000262
sewardj1f40a0a2004-07-21 12:28:07 +0000263 if (vex_verbosity > 0) {
264 vex_printf("\n-------- Virtual registerised code --------\n");
265 for (i = 0; i < vcode->arr_used; i++) {
266 vex_printf("%3d ", i);
267 ppInstr(vcode->arr[i]);
268 vex_printf("\n");
269 }
sewardjfbcaf332004-07-08 01:46:01 +0000270 vex_printf("\n");
271 }
sewardjfbcaf332004-07-08 01:46:01 +0000272
sewardjf13a16a2004-07-05 17:10:14 +0000273 /* Register allocate. */
274 rcode = doRegisterAllocation ( vcode, available_real_regs,
275 n_available_real_regs,
276 isMove, getRegUsage, mapRegs,
sewardj2b515872004-07-05 20:50:45 +0000277 genSpill, genReload,
278 ppInstr, ppReg );
sewardjf13a16a2004-07-05 17:10:14 +0000279
sewardj1f40a0a2004-07-21 12:28:07 +0000280 if (vex_verbosity > 0) {
281 vex_printf("\n-------- Post-regalloc code --------\n");
282 for (i = 0; i < rcode->arr_used; i++) {
283 vex_printf("%3d ", i);
284 ppInstr(rcode->arr[i]);
285 vex_printf("\n");
286 }
sewardjfbcaf332004-07-08 01:46:01 +0000287 vex_printf("\n");
288 }
sewardjfbcaf332004-07-08 01:46:01 +0000289
sewardj81bd5502004-07-21 18:49:27 +0000290 /* Assemble */
sewardj81bd5502004-07-21 18:49:27 +0000291 out_used = 0; /* tracks along the host_bytes array */
292 for (i = 0; i < rcode->arr_used; i++) {
sewardj14731f22004-07-25 01:24:28 +0000293 if (vex_verbosity > 1) {
sewardjbad34a92004-07-22 01:14:11 +0000294 ppInstr(rcode->arr[i]);
295 vex_printf("\n");
296 }
sewardj81bd5502004-07-21 18:49:27 +0000297 j = (*emit)( insn_bytes, 32, rcode->arr[i] );
sewardj14731f22004-07-25 01:24:28 +0000298 if (vex_verbosity > 1) {
sewardjbad34a92004-07-22 01:14:11 +0000299 for (k = 0; k < j; k++)
sewardj86898e82004-07-22 17:26:12 +0000300 if (insn_bytes[k] < 16)
301 vex_printf("0%x ", (UInt)insn_bytes[k]);
302 else
303 vex_printf("%x ", (UInt)insn_bytes[k]);
sewardjbad34a92004-07-22 01:14:11 +0000304 vex_printf("\n\n");
305 }
sewardj81bd5502004-07-21 18:49:27 +0000306 if (out_used + j > host_bytes_size) {
307 LibVEX_ClearTemporary(False);
sewardj58800ff2004-07-28 01:51:10 +0000308 vex_verbosity = saved_verbosity;
sewardj81bd5502004-07-21 18:49:27 +0000309 return TransOutputFull;
310 }
311 for (k = 0; k < j; k++) {
312 host_bytes[out_used] = insn_bytes[k];
313 out_used++;
314 }
315 vassert(out_used <= host_bytes_size);
316 }
317 *host_bytes_used = out_used;
318
sewardj1f40a0a2004-07-21 12:28:07 +0000319 // LibVEX_ClearTemporary(True);
320 LibVEX_ClearTemporary(False);
sewardjf13a16a2004-07-05 17:10:14 +0000321
sewardj58800ff2004-07-28 01:51:10 +0000322 vex_verbosity = saved_verbosity;
sewardj35421a32004-07-05 13:12:34 +0000323 return TransOK;
324}
325
326
327
328/*---------------------------------------------------------------*/
sewardjc0ee2ed2004-07-27 10:29:41 +0000329/*--- end main/vex_main.c ---*/
sewardj35421a32004-07-05 13:12:34 +0000330/*---------------------------------------------------------------*/