blob: 656e768135424e569885cafbc613755cf41298aa [file] [log] [blame]
sewardj35421a32004-07-05 13:12:34 +00001
2/*---------------------------------------------------------------*/
3/*--- ---*/
sewardj887a11a2004-07-05 17:26:47 +00004/*--- This file (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
sewardj887a11a2004-07-05 17:26:47 +000011#include "vex_globals.h"
sewardj35421a32004-07-05 13:12:34 +000012#include "vex_util.h"
sewardjf13a16a2004-07-05 17:10:14 +000013#include "host_regs.h"
sewardj8af36f12004-07-21 18:53:20 +000014#include "x86host_defs.h"
sewardjc9a65702004-07-07 16:32:57 +000015#include "x86guest_defs.h"
sewardj35421a32004-07-05 13:12:34 +000016
17
18/* This file contains the top level interface to the library. */
19
20/* --------- Initialise the library. --------- */
21
22/* Exported to library client. */
23
sewardj887a11a2004-07-05 17:26:47 +000024void LibVEX_Init (
sewardj35421a32004-07-05 13:12:34 +000025 /* failure exit function */
sewardj2b515872004-07-05 20:50:45 +000026 __attribute__ ((noreturn))
sewardj35421a32004-07-05 13:12:34 +000027 void (*failure_exit) ( void ),
28 /* logging output function */
29 void (*log_bytes) ( Char*, Int nbytes ),
30 /* debug paranoia level */
31 Int debuglevel,
32 /* verbosity level */
33 Int verbosity,
34 /* Are we supporting valgrind checking? */
35 Bool valgrind_support,
36 /* Max # guest insns per bb */
37 Int guest_insns_per_bb
38)
39{
40 vassert(!vex_initdone);
41 vassert(failure_exit);
sewardj35421a32004-07-05 13:12:34 +000042 vassert(log_bytes);
sewardj35421a32004-07-05 13:12:34 +000043 vassert(debuglevel >= 0);
sewardj35421a32004-07-05 13:12:34 +000044 vassert(verbosity >= 0);
sewardj35421a32004-07-05 13:12:34 +000045 vassert(guest_insns_per_bb >= 1 && guest_insns_per_bb <= 100);
sewardj443cd9d2004-07-18 23:06:45 +000046
47 vex_failure_exit = failure_exit;
48 vex_log_bytes = log_bytes;
49 vex_debuglevel = debuglevel;
50 vex_verbosity = verbosity;
51 vex_valgrind_support = valgrind_support;
sewardj35421a32004-07-05 13:12:34 +000052 vex_guest_insns_per_bb = guest_insns_per_bb;
sewardj443cd9d2004-07-18 23:06:45 +000053 vex_initdone = True;
54 LibVEX_SetAllocMode ( AllocModeTEMPORARY );
sewardj35421a32004-07-05 13:12:34 +000055}
56
57
58/* --------- Make a translation. --------- */
59
60/* Exported to library client. */
61
sewardj887a11a2004-07-05 17:26:47 +000062TranslateResult LibVEX_Translate (
sewardj35421a32004-07-05 13:12:34 +000063 /* The instruction sets we are translating from and to. */
64 InsnSet iset_guest,
65 InsnSet iset_host,
66 /* IN: the block to translate, and its guest address. */
sewardj81bd5502004-07-21 18:49:27 +000067 UChar* guest_bytes,
sewardj35421a32004-07-05 13:12:34 +000068 Addr64 guest_bytes_addr,
69 /* OUT: the number of bytes actually read */
70 Int* guest_bytes_read,
71 /* IN: a place to put the resulting code, and its size */
sewardj81bd5502004-07-21 18:49:27 +000072 UChar* host_bytes,
73 Int host_bytes_size,
sewardj35421a32004-07-05 13:12:34 +000074 /* OUT: how much of the output area is used. */
75 Int* host_bytes_used,
76 /* IN: optionally, an instrumentation function. */
sewardjf13a16a2004-07-05 17:10:14 +000077 IRBB* (*instrument) ( IRBB* ),
sewardj35421a32004-07-05 13:12:34 +000078 /* IN: optionally, an access check function for guest code. */
79 Bool (*byte_accessible) ( Addr64 )
80)
81{
sewardj81bd5502004-07-21 18:49:27 +000082 /* This the bundle of functions we need to do the back-end stuff
83 (insn selection, reg-alloc, assembly) whilst being insulated
84 from the target instruction set. */
sewardjf13a16a2004-07-05 17:10:14 +000085 HReg* available_real_regs;
86 Int n_available_real_regs;
sewardj443cd9d2004-07-18 23:06:45 +000087 Bool (*isMove) (HInstr*, HReg*, HReg*);
88 void (*getRegUsage) (HRegUsage*, HInstr*);
89 void (*mapRegs) (HRegRemap*, HInstr*);
90 HInstr* (*genSpill) ( HReg, Int );
91 HInstr* (*genReload) ( HReg, Int );
92 void (*ppInstr) ( HInstr* );
93 void (*ppReg) ( HReg );
sewardj36ca5132004-07-24 13:12:23 +000094 HInstrArray* (*iselBB) ( IRBB*, Addr64(*)(Char*) );
sewardj443cd9d2004-07-18 23:06:45 +000095 IRBB* (*bbToIR) ( UChar*, Addr64, Int*,
96 Bool(*)(Addr64), Bool );
sewardj81bd5502004-07-21 18:49:27 +000097 Int (*emit) ( UChar*, Int, HInstr* );
sewardj36ca5132004-07-24 13:12:23 +000098 Addr64 (*findHelper) ( Char* );
sewardjf13a16a2004-07-05 17:10:14 +000099
sewardjc9a65702004-07-07 16:32:57 +0000100 Bool host_is_bigendian = False;
sewardjf13a16a2004-07-05 17:10:14 +0000101 IRBB* irbb;
102 HInstrArray* vcode;
103 HInstrArray* rcode;
sewardj81bd5502004-07-21 18:49:27 +0000104 Int i, j, k, out_used;
sewardj2e56f9f2004-07-24 01:24:38 +0000105 UChar insn_bytes[32];
sewardjf13a16a2004-07-05 17:10:14 +0000106
sewardj36ca5132004-07-24 13:12:23 +0000107 available_real_regs = NULL;
108 n_available_real_regs = 0;
109 isMove = NULL;
110 getRegUsage = NULL;
111 mapRegs = NULL;
112 genSpill = NULL;
113 genReload = NULL;
114 ppInstr = NULL;
115 ppReg = NULL;
116 iselBB = NULL;
117 bbToIR = NULL;
118 emit = NULL;
119 findHelper = NULL;
120
sewardj35421a32004-07-05 13:12:34 +0000121 vassert(vex_initdone);
sewardj443cd9d2004-07-18 23:06:45 +0000122 LibVEX_ClearTemporary(False);
sewardjf13a16a2004-07-05 17:10:14 +0000123
124 /* First off, check that the guest and host insn sets
125 are supported. */
126 switch (iset_host) {
127 case InsnSetX86:
128 getAllocableRegs_X86 ( &n_available_real_regs,
129 &available_real_regs );
130 isMove = (Bool(*)(HInstr*,HReg*,HReg*)) isMove_X86Instr;
131 getRegUsage = (void(*)(HRegUsage*,HInstr*)) getRegUsage_X86Instr;
132 mapRegs = (void(*)(HRegRemap*,HInstr*)) mapRegs_X86Instr;
133 genSpill = (HInstr*(*)(HReg,Int)) genSpill_X86;
134 genReload = (HInstr*(*)(HReg,Int)) genReload_X86;
sewardj2b515872004-07-05 20:50:45 +0000135 ppInstr = (void(*)(HInstr*)) ppX86Instr;
136 ppReg = (void(*)(HReg)) ppHRegX86;
sewardjf13a16a2004-07-05 17:10:14 +0000137 iselBB = iselBB_X86;
sewardj81bd5502004-07-21 18:49:27 +0000138 emit = (Int(*)(UChar*,Int,HInstr*)) emit_X86Instr;
sewardjc9a65702004-07-07 16:32:57 +0000139 host_is_bigendian = False;
sewardjf13a16a2004-07-05 17:10:14 +0000140 break;
141 default:
sewardj887a11a2004-07-05 17:26:47 +0000142 vpanic("LibVEX_Translate: unsupported target insn set");
sewardjf13a16a2004-07-05 17:10:14 +0000143 }
144
145 switch (iset_guest) {
146 case InsnSetX86:
sewardj36ca5132004-07-24 13:12:23 +0000147 bbToIR = bbToIR_X86Instr;
148 findHelper = x86guest_findhelper;
sewardjf13a16a2004-07-05 17:10:14 +0000149 break;
150 default:
sewardj887a11a2004-07-05 17:26:47 +0000151 vpanic("LibVEX_Translate: unsupported guest insn set");
sewardjf13a16a2004-07-05 17:10:14 +0000152 }
153
154 irbb = bbToIR ( guest_bytes,
155 guest_bytes_addr,
156 guest_bytes_read,
sewardjc9a65702004-07-07 16:32:57 +0000157 byte_accessible,
158 host_is_bigendian );
sewardjf13a16a2004-07-05 17:10:14 +0000159
160 if (irbb == NULL) {
161 /* Access failure. */
sewardj443cd9d2004-07-18 23:06:45 +0000162 LibVEX_ClearTemporary(False);
sewardjf13a16a2004-07-05 17:10:14 +0000163 return TransAccessFail;
164 }
sewardj35439212004-07-14 22:36:10 +0000165 sanityCheckIRBB(irbb, Ity_I32);
sewardje8e9d732004-07-16 21:03:45 +0000166
sewardjf13a16a2004-07-05 17:10:14 +0000167 /* Get the thing instrumented. */
168 if (instrument)
169 irbb = (*instrument)(irbb);
170
171 /* Turn it into virtual-registerised code. */
sewardj36ca5132004-07-24 13:12:23 +0000172 vcode = iselBB ( irbb, findHelper );
sewardjf13a16a2004-07-05 17:10:14 +0000173
sewardj1f40a0a2004-07-21 12:28:07 +0000174 if (vex_verbosity > 0) {
175 vex_printf("\n-------- Virtual registerised code --------\n");
176 for (i = 0; i < vcode->arr_used; i++) {
177 vex_printf("%3d ", i);
178 ppInstr(vcode->arr[i]);
179 vex_printf("\n");
180 }
sewardjfbcaf332004-07-08 01:46:01 +0000181 vex_printf("\n");
182 }
sewardjfbcaf332004-07-08 01:46:01 +0000183
sewardjf13a16a2004-07-05 17:10:14 +0000184 /* Register allocate. */
185 rcode = doRegisterAllocation ( vcode, available_real_regs,
186 n_available_real_regs,
187 isMove, getRegUsage, mapRegs,
sewardj2b515872004-07-05 20:50:45 +0000188 genSpill, genReload,
189 ppInstr, ppReg );
sewardjf13a16a2004-07-05 17:10:14 +0000190
sewardj1f40a0a2004-07-21 12:28:07 +0000191 if (vex_verbosity > 0) {
192 vex_printf("\n-------- Post-regalloc code --------\n");
193 for (i = 0; i < rcode->arr_used; i++) {
194 vex_printf("%3d ", i);
195 ppInstr(rcode->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
sewardj81bd5502004-07-21 18:49:27 +0000201 /* Assemble */
sewardj81bd5502004-07-21 18:49:27 +0000202 out_used = 0; /* tracks along the host_bytes array */
203 for (i = 0; i < rcode->arr_used; i++) {
sewardjbad34a92004-07-22 01:14:11 +0000204 if (vex_verbosity > 0) {
205 ppInstr(rcode->arr[i]);
206 vex_printf("\n");
207 }
sewardj81bd5502004-07-21 18:49:27 +0000208 j = (*emit)( insn_bytes, 32, rcode->arr[i] );
sewardjbad34a92004-07-22 01:14:11 +0000209 if (vex_verbosity > 0) {
210 for (k = 0; k < j; k++)
sewardj86898e82004-07-22 17:26:12 +0000211 if (insn_bytes[k] < 16)
212 vex_printf("0%x ", (UInt)insn_bytes[k]);
213 else
214 vex_printf("%x ", (UInt)insn_bytes[k]);
sewardjbad34a92004-07-22 01:14:11 +0000215 vex_printf("\n\n");
216 }
sewardj81bd5502004-07-21 18:49:27 +0000217 if (out_used + j > host_bytes_size) {
218 LibVEX_ClearTemporary(False);
219 return TransOutputFull;
220 }
221 for (k = 0; k < j; k++) {
222 host_bytes[out_used] = insn_bytes[k];
223 out_used++;
224 }
225 vassert(out_used <= host_bytes_size);
226 }
227 *host_bytes_used = out_used;
228
sewardj1f40a0a2004-07-21 12:28:07 +0000229 // LibVEX_ClearTemporary(True);
230 LibVEX_ClearTemporary(False);
sewardjf13a16a2004-07-05 17:10:14 +0000231
sewardj35421a32004-07-05 13:12:34 +0000232 return TransOK;
233}
234
235
236
237/*---------------------------------------------------------------*/
sewardj887a11a2004-07-05 17:26:47 +0000238/*--- end vex_main.c ---*/
sewardj35421a32004-07-05 13:12:34 +0000239/*---------------------------------------------------------------*/