blob: 3857e44c686e894a8eb9269a595ed42a0844ed26 [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"
sewardjf13a16a2004-07-05 17:10:14 +000010
sewardjc0ee2ed2004-07-27 10:29:41 +000011#include "main/vex_globals.h"
12#include "main/vex_util.h"
13#include "host-generic/h_generic_regs.h"
14#include "host-x86/hdefs.h"
15#include "guest-x86/gdefs.h"
sewardjedf4d692004-08-17 13:52:58 +000016#include "ir/iropt.h"
sewardj35421a32004-07-05 13:12:34 +000017
18
19/* This file contains the top level interface to the library. */
20
21/* --------- Initialise the library. --------- */
22
23/* Exported to library client. */
24
sewardj887a11a2004-07-05 17:26:47 +000025void LibVEX_Init (
sewardj35421a32004-07-05 13:12:34 +000026 /* failure exit function */
sewardj2b515872004-07-05 20:50:45 +000027 __attribute__ ((noreturn))
sewardj35421a32004-07-05 13:12:34 +000028 void (*failure_exit) ( void ),
29 /* logging output function */
30 void (*log_bytes) ( Char*, Int nbytes ),
31 /* debug paranoia level */
32 Int debuglevel,
sewardj58800ff2004-07-28 01:51:10 +000033 /* initial verbosity level */
sewardj35421a32004-07-05 13:12:34 +000034 Int verbosity,
35 /* Are we supporting valgrind checking? */
36 Bool valgrind_support,
37 /* Max # guest insns per bb */
38 Int guest_insns_per_bb
39)
40{
41 vassert(!vex_initdone);
42 vassert(failure_exit);
sewardj35421a32004-07-05 13:12:34 +000043 vassert(log_bytes);
sewardj35421a32004-07-05 13:12:34 +000044 vassert(debuglevel >= 0);
sewardj35421a32004-07-05 13:12:34 +000045 vassert(verbosity >= 0);
sewardj35421a32004-07-05 13:12:34 +000046 vassert(guest_insns_per_bb >= 1 && guest_insns_per_bb <= 100);
sewardj443cd9d2004-07-18 23:06:45 +000047
48 vex_failure_exit = failure_exit;
49 vex_log_bytes = log_bytes;
50 vex_debuglevel = debuglevel;
51 vex_verbosity = verbosity;
52 vex_valgrind_support = valgrind_support;
sewardj35421a32004-07-05 13:12:34 +000053 vex_guest_insns_per_bb = guest_insns_per_bb;
sewardj443cd9d2004-07-18 23:06:45 +000054 vex_initdone = True;
55 LibVEX_SetAllocMode ( AllocModeTEMPORARY );
sewardj35421a32004-07-05 13:12:34 +000056}
57
58
59/* --------- Make a translation. --------- */
60
61/* Exported to library client. */
62
sewardj887a11a2004-07-05 17:26:47 +000063TranslateResult LibVEX_Translate (
sewardj35421a32004-07-05 13:12:34 +000064 /* The instruction sets we are translating from and to. */
65 InsnSet iset_guest,
66 InsnSet iset_host,
67 /* IN: the block to translate, and its guest address. */
sewardj81bd5502004-07-21 18:49:27 +000068 UChar* guest_bytes,
sewardj35421a32004-07-05 13:12:34 +000069 Addr64 guest_bytes_addr,
70 /* OUT: the number of bytes actually read */
71 Int* guest_bytes_read,
72 /* IN: a place to put the resulting code, and its size */
sewardj81bd5502004-07-21 18:49:27 +000073 UChar* host_bytes,
74 Int host_bytes_size,
sewardj35421a32004-07-05 13:12:34 +000075 /* OUT: how much of the output area is used. */
76 Int* host_bytes_used,
77 /* IN: optionally, an instrumentation function. */
sewardjf13a16a2004-07-05 17:10:14 +000078 IRBB* (*instrument) ( IRBB* ),
sewardj35421a32004-07-05 13:12:34 +000079 /* IN: optionally, an access check function for guest code. */
sewardj58800ff2004-07-28 01:51:10 +000080 Bool (*byte_accessible) ( Addr64 ),
81 /* IN: if > 0, use this verbosity for this bb */
82 Int bb_verbosity
sewardj35421a32004-07-05 13:12:34 +000083)
84{
sewardj81bd5502004-07-21 18:49:27 +000085 /* This the bundle of functions we need to do the back-end stuff
86 (insn selection, reg-alloc, assembly) whilst being insulated
87 from the target instruction set. */
sewardjf13a16a2004-07-05 17:10:14 +000088 HReg* available_real_regs;
89 Int n_available_real_regs;
sewardj443cd9d2004-07-18 23:06:45 +000090 Bool (*isMove) (HInstr*, HReg*, HReg*);
91 void (*getRegUsage) (HRegUsage*, HInstr*);
92 void (*mapRegs) (HRegRemap*, HInstr*);
93 HInstr* (*genSpill) ( HReg, Int );
94 HInstr* (*genReload) ( HReg, Int );
95 void (*ppInstr) ( HInstr* );
96 void (*ppReg) ( HReg );
sewardj36ca5132004-07-24 13:12:23 +000097 HInstrArray* (*iselBB) ( IRBB*, Addr64(*)(Char*) );
sewardj443cd9d2004-07-18 23:06:45 +000098 IRBB* (*bbToIR) ( UChar*, Addr64, Int*,
99 Bool(*)(Addr64), Bool );
sewardj81bd5502004-07-21 18:49:27 +0000100 Int (*emit) ( UChar*, Int, HInstr* );
sewardj36ca5132004-07-24 13:12:23 +0000101 Addr64 (*findHelper) ( Char* );
sewardj84ff0652004-08-23 16:16:08 +0000102 IRExpr* (*specHelper) ( Char*, IRExpr** );
sewardjf13a16a2004-07-05 17:10:14 +0000103
sewardjc9a65702004-07-07 16:32:57 +0000104 Bool host_is_bigendian = False;
sewardjf13a16a2004-07-05 17:10:14 +0000105 IRBB* irbb;
106 HInstrArray* vcode;
107 HInstrArray* rcode;
sewardj58800ff2004-07-28 01:51:10 +0000108 Int i, j, k, out_used, saved_verbosity;
sewardj2e56f9f2004-07-24 01:24:38 +0000109 UChar insn_bytes[32];
sewardjf13a16a2004-07-05 17:10:14 +0000110
sewardj36ca5132004-07-24 13:12:23 +0000111 available_real_regs = NULL;
112 n_available_real_regs = 0;
113 isMove = NULL;
114 getRegUsage = NULL;
115 mapRegs = NULL;
116 genSpill = NULL;
117 genReload = NULL;
118 ppInstr = NULL;
119 ppReg = NULL;
120 iselBB = NULL;
121 bbToIR = NULL;
122 emit = NULL;
123 findHelper = NULL;
sewardj84ff0652004-08-23 16:16:08 +0000124 specHelper = NULL;
sewardj36ca5132004-07-24 13:12:23 +0000125
sewardj58800ff2004-07-28 01:51:10 +0000126 saved_verbosity = vex_verbosity;
127 if (bb_verbosity > 0)
128 vex_verbosity = bb_verbosity;
129
sewardj35421a32004-07-05 13:12:34 +0000130 vassert(vex_initdone);
sewardj443cd9d2004-07-18 23:06:45 +0000131 LibVEX_ClearTemporary(False);
sewardjf13a16a2004-07-05 17:10:14 +0000132
133 /* First off, check that the guest and host insn sets
134 are supported. */
135 switch (iset_host) {
136 case InsnSetX86:
137 getAllocableRegs_X86 ( &n_available_real_regs,
138 &available_real_regs );
139 isMove = (Bool(*)(HInstr*,HReg*,HReg*)) isMove_X86Instr;
140 getRegUsage = (void(*)(HRegUsage*,HInstr*)) getRegUsage_X86Instr;
141 mapRegs = (void(*)(HRegRemap*,HInstr*)) mapRegs_X86Instr;
142 genSpill = (HInstr*(*)(HReg,Int)) genSpill_X86;
143 genReload = (HInstr*(*)(HReg,Int)) genReload_X86;
sewardj2b515872004-07-05 20:50:45 +0000144 ppInstr = (void(*)(HInstr*)) ppX86Instr;
145 ppReg = (void(*)(HReg)) ppHRegX86;
sewardjf13a16a2004-07-05 17:10:14 +0000146 iselBB = iselBB_X86;
sewardj81bd5502004-07-21 18:49:27 +0000147 emit = (Int(*)(UChar*,Int,HInstr*)) emit_X86Instr;
sewardjc9a65702004-07-07 16:32:57 +0000148 host_is_bigendian = False;
sewardjf13a16a2004-07-05 17:10:14 +0000149 break;
150 default:
sewardj887a11a2004-07-05 17:26:47 +0000151 vpanic("LibVEX_Translate: unsupported target insn set");
sewardjf13a16a2004-07-05 17:10:14 +0000152 }
153
154 switch (iset_guest) {
155 case InsnSetX86:
sewardj36ca5132004-07-24 13:12:23 +0000156 bbToIR = bbToIR_X86Instr;
157 findHelper = x86guest_findhelper;
sewardj84ff0652004-08-23 16:16:08 +0000158 specHelper = x86guest_spechelper;
sewardjf13a16a2004-07-05 17:10:14 +0000159 break;
160 default:
sewardj887a11a2004-07-05 17:26:47 +0000161 vpanic("LibVEX_Translate: unsupported guest insn set");
sewardjf13a16a2004-07-05 17:10:14 +0000162 }
163
164 irbb = bbToIR ( guest_bytes,
165 guest_bytes_addr,
166 guest_bytes_read,
sewardjc9a65702004-07-07 16:32:57 +0000167 byte_accessible,
168 host_is_bigendian );
sewardjf13a16a2004-07-05 17:10:14 +0000169
170 if (irbb == NULL) {
171 /* Access failure. */
sewardj443cd9d2004-07-18 23:06:45 +0000172 LibVEX_ClearTemporary(False);
sewardj58800ff2004-07-28 01:51:10 +0000173 vex_verbosity = saved_verbosity;
sewardjf13a16a2004-07-05 17:10:14 +0000174 return TransAccessFail;
175 }
sewardjaa59f942004-10-09 09:34:36 +0000176
177 /* If debugging, show the raw guest bytes for this bb. */
178 if (vex_verbosity >= 2) {
179 UChar* p = guest_bytes;
180 vex_printf("\n");
181 vex_printf(". 0 %llx %d\n.", guest_bytes_addr, *guest_bytes_read );
182 for (i = 0; i < *guest_bytes_read; i++)
183 vex_printf(" %02x", (Int)p[i] );
184 vex_printf("\n");
185 }
186
187 /* Sanity check the initial IR. */
sewardj35439212004-07-14 22:36:10 +0000188 sanityCheckIRBB(irbb, Ity_I32);
sewardje8e9d732004-07-16 21:03:45 +0000189
sewardjedf4d692004-08-17 13:52:58 +0000190 /* Clean it up, hopefully a lot. */
sewardj84ff0652004-08-23 16:16:08 +0000191 irbb = do_iropt_BB ( irbb, specHelper );
sewardjd7cb8532004-08-17 23:59:23 +0000192 sanityCheckIRBB(irbb, Ity_I32);
sewardjedf4d692004-08-17 13:52:58 +0000193
194 if (vex_verbosity > 0) {
195 vex_printf("\n-------- After IR optimisation --------\n");
196 ppIRBB ( irbb );
197 vex_printf("\n");
198 }
199
sewardjf13a16a2004-07-05 17:10:14 +0000200 /* Get the thing instrumented. */
201 if (instrument)
202 irbb = (*instrument)(irbb);
203
204 /* Turn it into virtual-registerised code. */
sewardj36ca5132004-07-24 13:12:23 +0000205 vcode = iselBB ( irbb, findHelper );
sewardjf13a16a2004-07-05 17:10:14 +0000206
sewardj1f40a0a2004-07-21 12:28:07 +0000207 if (vex_verbosity > 0) {
208 vex_printf("\n-------- Virtual registerised code --------\n");
209 for (i = 0; i < vcode->arr_used; i++) {
210 vex_printf("%3d ", i);
211 ppInstr(vcode->arr[i]);
212 vex_printf("\n");
213 }
sewardjfbcaf332004-07-08 01:46:01 +0000214 vex_printf("\n");
215 }
sewardjfbcaf332004-07-08 01:46:01 +0000216
sewardjf13a16a2004-07-05 17:10:14 +0000217 /* Register allocate. */
218 rcode = doRegisterAllocation ( vcode, available_real_regs,
219 n_available_real_regs,
220 isMove, getRegUsage, mapRegs,
sewardj2b515872004-07-05 20:50:45 +0000221 genSpill, genReload,
222 ppInstr, ppReg );
sewardjf13a16a2004-07-05 17:10:14 +0000223
sewardj1f40a0a2004-07-21 12:28:07 +0000224 if (vex_verbosity > 0) {
225 vex_printf("\n-------- Post-regalloc code --------\n");
226 for (i = 0; i < rcode->arr_used; i++) {
227 vex_printf("%3d ", i);
228 ppInstr(rcode->arr[i]);
229 vex_printf("\n");
230 }
sewardjfbcaf332004-07-08 01:46:01 +0000231 vex_printf("\n");
232 }
sewardjfbcaf332004-07-08 01:46:01 +0000233
sewardj81bd5502004-07-21 18:49:27 +0000234 /* Assemble */
sewardj81bd5502004-07-21 18:49:27 +0000235 out_used = 0; /* tracks along the host_bytes array */
236 for (i = 0; i < rcode->arr_used; i++) {
sewardj14731f22004-07-25 01:24:28 +0000237 if (vex_verbosity > 1) {
sewardjbad34a92004-07-22 01:14:11 +0000238 ppInstr(rcode->arr[i]);
239 vex_printf("\n");
240 }
sewardj81bd5502004-07-21 18:49:27 +0000241 j = (*emit)( insn_bytes, 32, rcode->arr[i] );
sewardj14731f22004-07-25 01:24:28 +0000242 if (vex_verbosity > 1) {
sewardjbad34a92004-07-22 01:14:11 +0000243 for (k = 0; k < j; k++)
sewardj86898e82004-07-22 17:26:12 +0000244 if (insn_bytes[k] < 16)
245 vex_printf("0%x ", (UInt)insn_bytes[k]);
246 else
247 vex_printf("%x ", (UInt)insn_bytes[k]);
sewardjbad34a92004-07-22 01:14:11 +0000248 vex_printf("\n\n");
249 }
sewardj81bd5502004-07-21 18:49:27 +0000250 if (out_used + j > host_bytes_size) {
251 LibVEX_ClearTemporary(False);
sewardj58800ff2004-07-28 01:51:10 +0000252 vex_verbosity = saved_verbosity;
sewardj81bd5502004-07-21 18:49:27 +0000253 return TransOutputFull;
254 }
255 for (k = 0; k < j; k++) {
256 host_bytes[out_used] = insn_bytes[k];
257 out_used++;
258 }
259 vassert(out_used <= host_bytes_size);
260 }
261 *host_bytes_used = out_used;
262
sewardj1f40a0a2004-07-21 12:28:07 +0000263 // LibVEX_ClearTemporary(True);
264 LibVEX_ClearTemporary(False);
sewardjf13a16a2004-07-05 17:10:14 +0000265
sewardj58800ff2004-07-28 01:51:10 +0000266 vex_verbosity = saved_verbosity;
sewardj35421a32004-07-05 13:12:34 +0000267 return TransOK;
268}
269
270
271
272/*---------------------------------------------------------------*/
sewardjc0ee2ed2004-07-27 10:29:41 +0000273/*--- end main/vex_main.c ---*/
sewardj35421a32004-07-05 13:12:34 +0000274/*---------------------------------------------------------------*/