blob: 7b704554ffa8a8f361a513f1666a03bdf856979f [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 );
94 HInstrArray* (*iselBB) ( IRBB* );
95 IRBB* (*bbToIR) ( UChar*, Addr64, Int*,
96 Bool(*)(Addr64), Bool );
sewardj81bd5502004-07-21 18:49:27 +000097 Int (*emit) ( UChar*, Int, HInstr* );
sewardjf13a16a2004-07-05 17:10:14 +000098
sewardjc9a65702004-07-07 16:32:57 +000099 Bool host_is_bigendian = False;
sewardjf13a16a2004-07-05 17:10:14 +0000100 IRBB* irbb;
101 HInstrArray* vcode;
102 HInstrArray* rcode;
sewardj81bd5502004-07-21 18:49:27 +0000103 Int i, j, k, out_used;
sewardjf13a16a2004-07-05 17:10:14 +0000104
sewardj35421a32004-07-05 13:12:34 +0000105 vassert(vex_initdone);
sewardj443cd9d2004-07-18 23:06:45 +0000106 LibVEX_ClearTemporary(False);
sewardjf13a16a2004-07-05 17:10:14 +0000107
108 /* First off, check that the guest and host insn sets
109 are supported. */
110 switch (iset_host) {
111 case InsnSetX86:
112 getAllocableRegs_X86 ( &n_available_real_regs,
113 &available_real_regs );
114 isMove = (Bool(*)(HInstr*,HReg*,HReg*)) isMove_X86Instr;
115 getRegUsage = (void(*)(HRegUsage*,HInstr*)) getRegUsage_X86Instr;
116 mapRegs = (void(*)(HRegRemap*,HInstr*)) mapRegs_X86Instr;
117 genSpill = (HInstr*(*)(HReg,Int)) genSpill_X86;
118 genReload = (HInstr*(*)(HReg,Int)) genReload_X86;
sewardj2b515872004-07-05 20:50:45 +0000119 ppInstr = (void(*)(HInstr*)) ppX86Instr;
120 ppReg = (void(*)(HReg)) ppHRegX86;
sewardjf13a16a2004-07-05 17:10:14 +0000121 iselBB = iselBB_X86;
sewardj81bd5502004-07-21 18:49:27 +0000122 emit = (Int(*)(UChar*,Int,HInstr*)) emit_X86Instr;
sewardjc9a65702004-07-07 16:32:57 +0000123 host_is_bigendian = False;
sewardjf13a16a2004-07-05 17:10:14 +0000124 break;
125 default:
sewardj887a11a2004-07-05 17:26:47 +0000126 vpanic("LibVEX_Translate: unsupported target insn set");
sewardjf13a16a2004-07-05 17:10:14 +0000127 }
128
129 switch (iset_guest) {
130 case InsnSetX86:
sewardjc9a65702004-07-07 16:32:57 +0000131 bbToIR = bbToIR_X86Instr;
sewardjf13a16a2004-07-05 17:10:14 +0000132 break;
133 default:
sewardj887a11a2004-07-05 17:26:47 +0000134 vpanic("LibVEX_Translate: unsupported guest insn set");
sewardjf13a16a2004-07-05 17:10:14 +0000135 }
136
137 irbb = bbToIR ( guest_bytes,
138 guest_bytes_addr,
139 guest_bytes_read,
sewardjc9a65702004-07-07 16:32:57 +0000140 byte_accessible,
141 host_is_bigendian );
sewardjf13a16a2004-07-05 17:10:14 +0000142
143 if (irbb == NULL) {
144 /* Access failure. */
sewardj443cd9d2004-07-18 23:06:45 +0000145 LibVEX_ClearTemporary(False);
sewardjf13a16a2004-07-05 17:10:14 +0000146 return TransAccessFail;
147 }
sewardj35439212004-07-14 22:36:10 +0000148 sanityCheckIRBB(irbb, Ity_I32);
sewardje8e9d732004-07-16 21:03:45 +0000149
sewardjf13a16a2004-07-05 17:10:14 +0000150 /* Get the thing instrumented. */
151 if (instrument)
152 irbb = (*instrument)(irbb);
153
154 /* Turn it into virtual-registerised code. */
155 vcode = iselBB ( irbb );
156
sewardj1f40a0a2004-07-21 12:28:07 +0000157 if (vex_verbosity > 0) {
158 vex_printf("\n-------- Virtual registerised code --------\n");
159 for (i = 0; i < vcode->arr_used; i++) {
160 vex_printf("%3d ", i);
161 ppInstr(vcode->arr[i]);
162 vex_printf("\n");
163 }
sewardjfbcaf332004-07-08 01:46:01 +0000164 vex_printf("\n");
165 }
sewardjfbcaf332004-07-08 01:46:01 +0000166
sewardjf13a16a2004-07-05 17:10:14 +0000167 /* Register allocate. */
168 rcode = doRegisterAllocation ( vcode, available_real_regs,
169 n_available_real_regs,
170 isMove, getRegUsage, mapRegs,
sewardj2b515872004-07-05 20:50:45 +0000171 genSpill, genReload,
172 ppInstr, ppReg );
sewardjf13a16a2004-07-05 17:10:14 +0000173
sewardj1f40a0a2004-07-21 12:28:07 +0000174 if (vex_verbosity > 0) {
175 vex_printf("\n-------- Post-regalloc code --------\n");
176 for (i = 0; i < rcode->arr_used; i++) {
177 vex_printf("%3d ", i);
178 ppInstr(rcode->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
sewardj81bd5502004-07-21 18:49:27 +0000184 /* Assemble */
185 UChar insn_bytes[32];
186 out_used = 0; /* tracks along the host_bytes array */
187 for (i = 0; i < rcode->arr_used; i++) {
sewardjbad34a92004-07-22 01:14:11 +0000188 if (vex_verbosity > 0) {
189 ppInstr(rcode->arr[i]);
190 vex_printf("\n");
191 }
sewardj81bd5502004-07-21 18:49:27 +0000192 j = (*emit)( insn_bytes, 32, rcode->arr[i] );
sewardjbad34a92004-07-22 01:14:11 +0000193 if (vex_verbosity > 0) {
194 for (k = 0; k < j; k++)
195 vex_printf("0x%-2x ", (UInt)insn_bytes[k]);
196 vex_printf("\n\n");
197 }
sewardj81bd5502004-07-21 18:49:27 +0000198 if (out_used + j > host_bytes_size) {
199 LibVEX_ClearTemporary(False);
200 return TransOutputFull;
201 }
202 for (k = 0; k < j; k++) {
203 host_bytes[out_used] = insn_bytes[k];
204 out_used++;
205 }
206 vassert(out_used <= host_bytes_size);
207 }
208 *host_bytes_used = out_used;
209
sewardj1f40a0a2004-07-21 12:28:07 +0000210 // LibVEX_ClearTemporary(True);
211 LibVEX_ClearTemporary(False);
sewardjf13a16a2004-07-05 17:10:14 +0000212
sewardj35421a32004-07-05 13:12:34 +0000213 return TransOK;
214}
215
216
217
218/*---------------------------------------------------------------*/
sewardj887a11a2004-07-05 17:26:47 +0000219/*--- end vex_main.c ---*/
sewardj35421a32004-07-05 13:12:34 +0000220/*---------------------------------------------------------------*/