blob: cac3f77383b79bcab2d4878f3174f5c4af12cbc7 [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"
14#include "x86h_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. */
67 Char* guest_bytes,
68 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 */
72 Char* host_bytes,
73 Int host_bytes_size,
74 /* 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{
sewardjf13a16a2004-07-05 17:10:14 +000082 /* Stuff we need to know for reg-alloc. */
83 HReg* available_real_regs;
84 Int n_available_real_regs;
sewardj443cd9d2004-07-18 23:06:45 +000085 Bool (*isMove) (HInstr*, HReg*, HReg*);
86 void (*getRegUsage) (HRegUsage*, HInstr*);
87 void (*mapRegs) (HRegRemap*, HInstr*);
88 HInstr* (*genSpill) ( HReg, Int );
89 HInstr* (*genReload) ( HReg, Int );
90 void (*ppInstr) ( HInstr* );
91 void (*ppReg) ( HReg );
92 HInstrArray* (*iselBB) ( IRBB* );
93 IRBB* (*bbToIR) ( UChar*, Addr64, Int*,
94 Bool(*)(Addr64), Bool );
sewardjf13a16a2004-07-05 17:10:14 +000095
sewardjc9a65702004-07-07 16:32:57 +000096 Bool host_is_bigendian = False;
sewardjf13a16a2004-07-05 17:10:14 +000097 IRBB* irbb;
98 HInstrArray* vcode;
99 HInstrArray* rcode;
sewardjfbcaf332004-07-08 01:46:01 +0000100 Int i;
sewardjf13a16a2004-07-05 17:10:14 +0000101
sewardj35421a32004-07-05 13:12:34 +0000102 vassert(vex_initdone);
sewardj443cd9d2004-07-18 23:06:45 +0000103 LibVEX_ClearTemporary(False);
sewardjf13a16a2004-07-05 17:10:14 +0000104
105 /* First off, check that the guest and host insn sets
106 are supported. */
107 switch (iset_host) {
108 case InsnSetX86:
109 getAllocableRegs_X86 ( &n_available_real_regs,
110 &available_real_regs );
111 isMove = (Bool(*)(HInstr*,HReg*,HReg*)) isMove_X86Instr;
112 getRegUsage = (void(*)(HRegUsage*,HInstr*)) getRegUsage_X86Instr;
113 mapRegs = (void(*)(HRegRemap*,HInstr*)) mapRegs_X86Instr;
114 genSpill = (HInstr*(*)(HReg,Int)) genSpill_X86;
115 genReload = (HInstr*(*)(HReg,Int)) genReload_X86;
sewardj2b515872004-07-05 20:50:45 +0000116 ppInstr = (void(*)(HInstr*)) ppX86Instr;
117 ppReg = (void(*)(HReg)) ppHRegX86;
sewardjf13a16a2004-07-05 17:10:14 +0000118 iselBB = iselBB_X86;
sewardjc9a65702004-07-07 16:32:57 +0000119 host_is_bigendian = False;
sewardjf13a16a2004-07-05 17:10:14 +0000120 break;
121 default:
sewardj887a11a2004-07-05 17:26:47 +0000122 vpanic("LibVEX_Translate: unsupported target insn set");
sewardjf13a16a2004-07-05 17:10:14 +0000123 }
124
125 switch (iset_guest) {
126 case InsnSetX86:
sewardjc9a65702004-07-07 16:32:57 +0000127 bbToIR = bbToIR_X86Instr;
sewardjf13a16a2004-07-05 17:10:14 +0000128 break;
129 default:
sewardj887a11a2004-07-05 17:26:47 +0000130 vpanic("LibVEX_Translate: unsupported guest insn set");
sewardjf13a16a2004-07-05 17:10:14 +0000131 }
132
133 irbb = bbToIR ( guest_bytes,
134 guest_bytes_addr,
135 guest_bytes_read,
sewardjc9a65702004-07-07 16:32:57 +0000136 byte_accessible,
137 host_is_bigendian );
sewardjf13a16a2004-07-05 17:10:14 +0000138
139 if (irbb == NULL) {
140 /* Access failure. */
sewardj443cd9d2004-07-18 23:06:45 +0000141 LibVEX_ClearTemporary(False);
sewardjf13a16a2004-07-05 17:10:14 +0000142 return TransAccessFail;
143 }
sewardj35439212004-07-14 22:36:10 +0000144 sanityCheckIRBB(irbb, Ity_I32);
sewardje8e9d732004-07-16 21:03:45 +0000145
sewardjf13a16a2004-07-05 17:10:14 +0000146 /* Get the thing instrumented. */
147 if (instrument)
148 irbb = (*instrument)(irbb);
149
150 /* Turn it into virtual-registerised code. */
151 vcode = iselBB ( irbb );
152
sewardj1f40a0a2004-07-21 12:28:07 +0000153 if (vex_verbosity > 0) {
154 vex_printf("\n-------- Virtual registerised code --------\n");
155 for (i = 0; i < vcode->arr_used; i++) {
156 vex_printf("%3d ", i);
157 ppInstr(vcode->arr[i]);
158 vex_printf("\n");
159 }
sewardjfbcaf332004-07-08 01:46:01 +0000160 vex_printf("\n");
161 }
sewardjfbcaf332004-07-08 01:46:01 +0000162
sewardjf13a16a2004-07-05 17:10:14 +0000163 /* Register allocate. */
164 rcode = doRegisterAllocation ( vcode, available_real_regs,
165 n_available_real_regs,
166 isMove, getRegUsage, mapRegs,
sewardj2b515872004-07-05 20:50:45 +0000167 genSpill, genReload,
168 ppInstr, ppReg );
sewardjf13a16a2004-07-05 17:10:14 +0000169
sewardj1f40a0a2004-07-21 12:28:07 +0000170 if (vex_verbosity > 0) {
171 vex_printf("\n-------- Post-regalloc code --------\n");
172 for (i = 0; i < rcode->arr_used; i++) {
173 vex_printf("%3d ", i);
174 ppInstr(rcode->arr[i]);
175 vex_printf("\n");
176 }
sewardjfbcaf332004-07-08 01:46:01 +0000177 vex_printf("\n");
178 }
sewardjfbcaf332004-07-08 01:46:01 +0000179
sewardjf13a16a2004-07-05 17:10:14 +0000180 /* Assemble, etc. */
sewardj1f40a0a2004-07-21 12:28:07 +0000181 // LibVEX_ClearTemporary(True);
182 LibVEX_ClearTemporary(False);
sewardjf13a16a2004-07-05 17:10:14 +0000183
sewardj35421a32004-07-05 13:12:34 +0000184 return TransOK;
185}
186
187
188
189/*---------------------------------------------------------------*/
sewardj887a11a2004-07-05 17:26:47 +0000190/*--- end vex_main.c ---*/
sewardj35421a32004-07-05 13:12:34 +0000191/*---------------------------------------------------------------*/