blob: 096d2e8ad0c4aa9b7de931b4d9242a1e87fc8bdd [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* );
sewardjf13a16a2004-07-05 17:10:14 +0000102
sewardjc9a65702004-07-07 16:32:57 +0000103 Bool host_is_bigendian = False;
sewardjf13a16a2004-07-05 17:10:14 +0000104 IRBB* irbb;
105 HInstrArray* vcode;
106 HInstrArray* rcode;
sewardj58800ff2004-07-28 01:51:10 +0000107 Int i, j, k, out_used, saved_verbosity;
sewardj2e56f9f2004-07-24 01:24:38 +0000108 UChar insn_bytes[32];
sewardjf13a16a2004-07-05 17:10:14 +0000109
sewardj36ca5132004-07-24 13:12:23 +0000110 available_real_regs = NULL;
111 n_available_real_regs = 0;
112 isMove = NULL;
113 getRegUsage = NULL;
114 mapRegs = NULL;
115 genSpill = NULL;
116 genReload = NULL;
117 ppInstr = NULL;
118 ppReg = NULL;
119 iselBB = NULL;
120 bbToIR = NULL;
121 emit = NULL;
122 findHelper = NULL;
123
sewardj58800ff2004-07-28 01:51:10 +0000124 saved_verbosity = vex_verbosity;
125 if (bb_verbosity > 0)
126 vex_verbosity = bb_verbosity;
127
sewardj35421a32004-07-05 13:12:34 +0000128 vassert(vex_initdone);
sewardj443cd9d2004-07-18 23:06:45 +0000129 LibVEX_ClearTemporary(False);
sewardjf13a16a2004-07-05 17:10:14 +0000130
131 /* First off, check that the guest and host insn sets
132 are supported. */
133 switch (iset_host) {
134 case InsnSetX86:
135 getAllocableRegs_X86 ( &n_available_real_regs,
136 &available_real_regs );
137 isMove = (Bool(*)(HInstr*,HReg*,HReg*)) isMove_X86Instr;
138 getRegUsage = (void(*)(HRegUsage*,HInstr*)) getRegUsage_X86Instr;
139 mapRegs = (void(*)(HRegRemap*,HInstr*)) mapRegs_X86Instr;
140 genSpill = (HInstr*(*)(HReg,Int)) genSpill_X86;
141 genReload = (HInstr*(*)(HReg,Int)) genReload_X86;
sewardj2b515872004-07-05 20:50:45 +0000142 ppInstr = (void(*)(HInstr*)) ppX86Instr;
143 ppReg = (void(*)(HReg)) ppHRegX86;
sewardjf13a16a2004-07-05 17:10:14 +0000144 iselBB = iselBB_X86;
sewardj81bd5502004-07-21 18:49:27 +0000145 emit = (Int(*)(UChar*,Int,HInstr*)) emit_X86Instr;
sewardjc9a65702004-07-07 16:32:57 +0000146 host_is_bigendian = False;
sewardjf13a16a2004-07-05 17:10:14 +0000147 break;
148 default:
sewardj887a11a2004-07-05 17:26:47 +0000149 vpanic("LibVEX_Translate: unsupported target insn set");
sewardjf13a16a2004-07-05 17:10:14 +0000150 }
151
152 switch (iset_guest) {
153 case InsnSetX86:
sewardj36ca5132004-07-24 13:12:23 +0000154 bbToIR = bbToIR_X86Instr;
155 findHelper = x86guest_findhelper;
sewardjf13a16a2004-07-05 17:10:14 +0000156 break;
157 default:
sewardj887a11a2004-07-05 17:26:47 +0000158 vpanic("LibVEX_Translate: unsupported guest insn set");
sewardjf13a16a2004-07-05 17:10:14 +0000159 }
160
161 irbb = bbToIR ( guest_bytes,
162 guest_bytes_addr,
163 guest_bytes_read,
sewardjc9a65702004-07-07 16:32:57 +0000164 byte_accessible,
165 host_is_bigendian );
sewardjf13a16a2004-07-05 17:10:14 +0000166
167 if (irbb == NULL) {
168 /* Access failure. */
sewardj443cd9d2004-07-18 23:06:45 +0000169 LibVEX_ClearTemporary(False);
sewardj58800ff2004-07-28 01:51:10 +0000170 vex_verbosity = saved_verbosity;
sewardjf13a16a2004-07-05 17:10:14 +0000171 return TransAccessFail;
172 }
sewardj35439212004-07-14 22:36:10 +0000173 sanityCheckIRBB(irbb, Ity_I32);
sewardje8e9d732004-07-16 21:03:45 +0000174
sewardjedf4d692004-08-17 13:52:58 +0000175 /* Clean it up, hopefully a lot. */
176 irbb = do_iropt_BB ( irbb );
177
178 if (vex_verbosity > 0) {
179 vex_printf("\n-------- After IR optimisation --------\n");
180 ppIRBB ( irbb );
181 vex_printf("\n");
182 }
183
sewardjf13a16a2004-07-05 17:10:14 +0000184 /* Get the thing instrumented. */
185 if (instrument)
186 irbb = (*instrument)(irbb);
187
188 /* Turn it into virtual-registerised code. */
sewardj36ca5132004-07-24 13:12:23 +0000189 vcode = iselBB ( irbb, findHelper );
sewardjf13a16a2004-07-05 17:10:14 +0000190
sewardj1f40a0a2004-07-21 12:28:07 +0000191 if (vex_verbosity > 0) {
192 vex_printf("\n-------- Virtual registerised code --------\n");
193 for (i = 0; i < vcode->arr_used; i++) {
194 vex_printf("%3d ", i);
195 ppInstr(vcode->arr[i]);
196 vex_printf("\n");
197 }
sewardjfbcaf332004-07-08 01:46:01 +0000198 vex_printf("\n");
199 }
sewardjfbcaf332004-07-08 01:46:01 +0000200
sewardjf13a16a2004-07-05 17:10:14 +0000201 /* Register allocate. */
202 rcode = doRegisterAllocation ( vcode, available_real_regs,
203 n_available_real_regs,
204 isMove, getRegUsage, mapRegs,
sewardj2b515872004-07-05 20:50:45 +0000205 genSpill, genReload,
206 ppInstr, ppReg );
sewardjf13a16a2004-07-05 17:10:14 +0000207
sewardj1f40a0a2004-07-21 12:28:07 +0000208 if (vex_verbosity > 0) {
209 vex_printf("\n-------- Post-regalloc code --------\n");
210 for (i = 0; i < rcode->arr_used; i++) {
211 vex_printf("%3d ", i);
212 ppInstr(rcode->arr[i]);
213 vex_printf("\n");
214 }
sewardjfbcaf332004-07-08 01:46:01 +0000215 vex_printf("\n");
216 }
sewardjfbcaf332004-07-08 01:46:01 +0000217
sewardj81bd5502004-07-21 18:49:27 +0000218 /* Assemble */
sewardj81bd5502004-07-21 18:49:27 +0000219 out_used = 0; /* tracks along the host_bytes array */
220 for (i = 0; i < rcode->arr_used; i++) {
sewardj14731f22004-07-25 01:24:28 +0000221 if (vex_verbosity > 1) {
sewardjbad34a92004-07-22 01:14:11 +0000222 ppInstr(rcode->arr[i]);
223 vex_printf("\n");
224 }
sewardj81bd5502004-07-21 18:49:27 +0000225 j = (*emit)( insn_bytes, 32, rcode->arr[i] );
sewardj14731f22004-07-25 01:24:28 +0000226 if (vex_verbosity > 1) {
sewardjbad34a92004-07-22 01:14:11 +0000227 for (k = 0; k < j; k++)
sewardj86898e82004-07-22 17:26:12 +0000228 if (insn_bytes[k] < 16)
229 vex_printf("0%x ", (UInt)insn_bytes[k]);
230 else
231 vex_printf("%x ", (UInt)insn_bytes[k]);
sewardjbad34a92004-07-22 01:14:11 +0000232 vex_printf("\n\n");
233 }
sewardj81bd5502004-07-21 18:49:27 +0000234 if (out_used + j > host_bytes_size) {
235 LibVEX_ClearTemporary(False);
sewardj58800ff2004-07-28 01:51:10 +0000236 vex_verbosity = saved_verbosity;
sewardj81bd5502004-07-21 18:49:27 +0000237 return TransOutputFull;
238 }
239 for (k = 0; k < j; k++) {
240 host_bytes[out_used] = insn_bytes[k];
241 out_used++;
242 }
243 vassert(out_used <= host_bytes_size);
244 }
245 *host_bytes_used = out_used;
246
sewardj1f40a0a2004-07-21 12:28:07 +0000247 // LibVEX_ClearTemporary(True);
248 LibVEX_ClearTemporary(False);
sewardjf13a16a2004-07-05 17:10:14 +0000249
sewardj58800ff2004-07-28 01:51:10 +0000250 vex_verbosity = saved_verbosity;
sewardj35421a32004-07-05 13:12:34 +0000251 return TransOK;
252}
253
254
255
256/*---------------------------------------------------------------*/
sewardjc0ee2ed2004-07-27 10:29:41 +0000257/*--- end main/vex_main.c ---*/
sewardj35421a32004-07-05 13:12:34 +0000258/*---------------------------------------------------------------*/