sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 1 | |
| 2 | /*---------------------------------------------------------------*/ |
| 3 | /*--- ---*/ |
sewardj | c0ee2ed | 2004-07-27 10:29:41 +0000 | [diff] [blame] | 4 | /*--- This file (main/vex_main.c) is ---*/ |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 5 | /*--- Copyright (c) 2004 OpenWorks LLP. All rights reserved. ---*/ |
| 6 | /*--- ---*/ |
| 7 | /*---------------------------------------------------------------*/ |
| 8 | |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame] | 9 | #include "libvex.h" |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 10 | |
sewardj | c0ee2ed | 2004-07-27 10:29:41 +0000 | [diff] [blame] | 11 | #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" |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 16 | |
| 17 | |
| 18 | /* This file contains the top level interface to the library. */ |
| 19 | |
| 20 | /* --------- Initialise the library. --------- */ |
| 21 | |
| 22 | /* Exported to library client. */ |
| 23 | |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame] | 24 | void LibVEX_Init ( |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 25 | /* failure exit function */ |
sewardj | 2b51587 | 2004-07-05 20:50:45 +0000 | [diff] [blame] | 26 | __attribute__ ((noreturn)) |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 27 | void (*failure_exit) ( void ), |
| 28 | /* logging output function */ |
| 29 | void (*log_bytes) ( Char*, Int nbytes ), |
| 30 | /* debug paranoia level */ |
| 31 | Int debuglevel, |
sewardj | 58800ff | 2004-07-28 01:51:10 +0000 | [diff] [blame^] | 32 | /* initial verbosity level */ |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 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); |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 42 | vassert(log_bytes); |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 43 | vassert(debuglevel >= 0); |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 44 | vassert(verbosity >= 0); |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 45 | vassert(guest_insns_per_bb >= 1 && guest_insns_per_bb <= 100); |
sewardj | 443cd9d | 2004-07-18 23:06:45 +0000 | [diff] [blame] | 46 | |
| 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; |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 52 | vex_guest_insns_per_bb = guest_insns_per_bb; |
sewardj | 443cd9d | 2004-07-18 23:06:45 +0000 | [diff] [blame] | 53 | vex_initdone = True; |
| 54 | LibVEX_SetAllocMode ( AllocModeTEMPORARY ); |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | |
| 58 | /* --------- Make a translation. --------- */ |
| 59 | |
| 60 | /* Exported to library client. */ |
| 61 | |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame] | 62 | TranslateResult LibVEX_Translate ( |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 63 | /* 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. */ |
sewardj | 81bd550 | 2004-07-21 18:49:27 +0000 | [diff] [blame] | 67 | UChar* guest_bytes, |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 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 */ |
sewardj | 81bd550 | 2004-07-21 18:49:27 +0000 | [diff] [blame] | 72 | UChar* host_bytes, |
| 73 | Int host_bytes_size, |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 74 | /* OUT: how much of the output area is used. */ |
| 75 | Int* host_bytes_used, |
| 76 | /* IN: optionally, an instrumentation function. */ |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 77 | IRBB* (*instrument) ( IRBB* ), |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 78 | /* IN: optionally, an access check function for guest code. */ |
sewardj | 58800ff | 2004-07-28 01:51:10 +0000 | [diff] [blame^] | 79 | Bool (*byte_accessible) ( Addr64 ), |
| 80 | /* IN: if > 0, use this verbosity for this bb */ |
| 81 | Int bb_verbosity |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 82 | ) |
| 83 | { |
sewardj | 81bd550 | 2004-07-21 18:49:27 +0000 | [diff] [blame] | 84 | /* This the bundle of functions we need to do the back-end stuff |
| 85 | (insn selection, reg-alloc, assembly) whilst being insulated |
| 86 | from the target instruction set. */ |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 87 | HReg* available_real_regs; |
| 88 | Int n_available_real_regs; |
sewardj | 443cd9d | 2004-07-18 23:06:45 +0000 | [diff] [blame] | 89 | Bool (*isMove) (HInstr*, HReg*, HReg*); |
| 90 | void (*getRegUsage) (HRegUsage*, HInstr*); |
| 91 | void (*mapRegs) (HRegRemap*, HInstr*); |
| 92 | HInstr* (*genSpill) ( HReg, Int ); |
| 93 | HInstr* (*genReload) ( HReg, Int ); |
| 94 | void (*ppInstr) ( HInstr* ); |
| 95 | void (*ppReg) ( HReg ); |
sewardj | 36ca513 | 2004-07-24 13:12:23 +0000 | [diff] [blame] | 96 | HInstrArray* (*iselBB) ( IRBB*, Addr64(*)(Char*) ); |
sewardj | 443cd9d | 2004-07-18 23:06:45 +0000 | [diff] [blame] | 97 | IRBB* (*bbToIR) ( UChar*, Addr64, Int*, |
| 98 | Bool(*)(Addr64), Bool ); |
sewardj | 81bd550 | 2004-07-21 18:49:27 +0000 | [diff] [blame] | 99 | Int (*emit) ( UChar*, Int, HInstr* ); |
sewardj | 36ca513 | 2004-07-24 13:12:23 +0000 | [diff] [blame] | 100 | Addr64 (*findHelper) ( Char* ); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 101 | |
sewardj | c9a6570 | 2004-07-07 16:32:57 +0000 | [diff] [blame] | 102 | Bool host_is_bigendian = False; |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 103 | IRBB* irbb; |
| 104 | HInstrArray* vcode; |
| 105 | HInstrArray* rcode; |
sewardj | 58800ff | 2004-07-28 01:51:10 +0000 | [diff] [blame^] | 106 | Int i, j, k, out_used, saved_verbosity; |
sewardj | 2e56f9f | 2004-07-24 01:24:38 +0000 | [diff] [blame] | 107 | UChar insn_bytes[32]; |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 108 | |
sewardj | 36ca513 | 2004-07-24 13:12:23 +0000 | [diff] [blame] | 109 | available_real_regs = NULL; |
| 110 | n_available_real_regs = 0; |
| 111 | isMove = NULL; |
| 112 | getRegUsage = NULL; |
| 113 | mapRegs = NULL; |
| 114 | genSpill = NULL; |
| 115 | genReload = NULL; |
| 116 | ppInstr = NULL; |
| 117 | ppReg = NULL; |
| 118 | iselBB = NULL; |
| 119 | bbToIR = NULL; |
| 120 | emit = NULL; |
| 121 | findHelper = NULL; |
| 122 | |
sewardj | 58800ff | 2004-07-28 01:51:10 +0000 | [diff] [blame^] | 123 | saved_verbosity = vex_verbosity; |
| 124 | if (bb_verbosity > 0) |
| 125 | vex_verbosity = bb_verbosity; |
| 126 | |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 127 | vassert(vex_initdone); |
sewardj | 443cd9d | 2004-07-18 23:06:45 +0000 | [diff] [blame] | 128 | LibVEX_ClearTemporary(False); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 129 | |
| 130 | /* First off, check that the guest and host insn sets |
| 131 | are supported. */ |
| 132 | switch (iset_host) { |
| 133 | case InsnSetX86: |
| 134 | getAllocableRegs_X86 ( &n_available_real_regs, |
| 135 | &available_real_regs ); |
| 136 | isMove = (Bool(*)(HInstr*,HReg*,HReg*)) isMove_X86Instr; |
| 137 | getRegUsage = (void(*)(HRegUsage*,HInstr*)) getRegUsage_X86Instr; |
| 138 | mapRegs = (void(*)(HRegRemap*,HInstr*)) mapRegs_X86Instr; |
| 139 | genSpill = (HInstr*(*)(HReg,Int)) genSpill_X86; |
| 140 | genReload = (HInstr*(*)(HReg,Int)) genReload_X86; |
sewardj | 2b51587 | 2004-07-05 20:50:45 +0000 | [diff] [blame] | 141 | ppInstr = (void(*)(HInstr*)) ppX86Instr; |
| 142 | ppReg = (void(*)(HReg)) ppHRegX86; |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 143 | iselBB = iselBB_X86; |
sewardj | 81bd550 | 2004-07-21 18:49:27 +0000 | [diff] [blame] | 144 | emit = (Int(*)(UChar*,Int,HInstr*)) emit_X86Instr; |
sewardj | c9a6570 | 2004-07-07 16:32:57 +0000 | [diff] [blame] | 145 | host_is_bigendian = False; |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 146 | break; |
| 147 | default: |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame] | 148 | vpanic("LibVEX_Translate: unsupported target insn set"); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | switch (iset_guest) { |
| 152 | case InsnSetX86: |
sewardj | 36ca513 | 2004-07-24 13:12:23 +0000 | [diff] [blame] | 153 | bbToIR = bbToIR_X86Instr; |
| 154 | findHelper = x86guest_findhelper; |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 155 | break; |
| 156 | default: |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame] | 157 | vpanic("LibVEX_Translate: unsupported guest insn set"); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | irbb = bbToIR ( guest_bytes, |
| 161 | guest_bytes_addr, |
| 162 | guest_bytes_read, |
sewardj | c9a6570 | 2004-07-07 16:32:57 +0000 | [diff] [blame] | 163 | byte_accessible, |
| 164 | host_is_bigendian ); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 165 | |
| 166 | if (irbb == NULL) { |
| 167 | /* Access failure. */ |
sewardj | 443cd9d | 2004-07-18 23:06:45 +0000 | [diff] [blame] | 168 | LibVEX_ClearTemporary(False); |
sewardj | 58800ff | 2004-07-28 01:51:10 +0000 | [diff] [blame^] | 169 | vex_verbosity = saved_verbosity; |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 170 | return TransAccessFail; |
| 171 | } |
sewardj | 3543921 | 2004-07-14 22:36:10 +0000 | [diff] [blame] | 172 | sanityCheckIRBB(irbb, Ity_I32); |
sewardj | e8e9d73 | 2004-07-16 21:03:45 +0000 | [diff] [blame] | 173 | |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 174 | /* Get the thing instrumented. */ |
| 175 | if (instrument) |
| 176 | irbb = (*instrument)(irbb); |
| 177 | |
| 178 | /* Turn it into virtual-registerised code. */ |
sewardj | 36ca513 | 2004-07-24 13:12:23 +0000 | [diff] [blame] | 179 | vcode = iselBB ( irbb, findHelper ); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 180 | |
sewardj | 1f40a0a | 2004-07-21 12:28:07 +0000 | [diff] [blame] | 181 | if (vex_verbosity > 0) { |
| 182 | vex_printf("\n-------- Virtual registerised code --------\n"); |
| 183 | for (i = 0; i < vcode->arr_used; i++) { |
| 184 | vex_printf("%3d ", i); |
| 185 | ppInstr(vcode->arr[i]); |
| 186 | vex_printf("\n"); |
| 187 | } |
sewardj | fbcaf33 | 2004-07-08 01:46:01 +0000 | [diff] [blame] | 188 | vex_printf("\n"); |
| 189 | } |
sewardj | fbcaf33 | 2004-07-08 01:46:01 +0000 | [diff] [blame] | 190 | |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 191 | /* Register allocate. */ |
| 192 | rcode = doRegisterAllocation ( vcode, available_real_regs, |
| 193 | n_available_real_regs, |
| 194 | isMove, getRegUsage, mapRegs, |
sewardj | 2b51587 | 2004-07-05 20:50:45 +0000 | [diff] [blame] | 195 | genSpill, genReload, |
| 196 | ppInstr, ppReg ); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 197 | |
sewardj | 1f40a0a | 2004-07-21 12:28:07 +0000 | [diff] [blame] | 198 | if (vex_verbosity > 0) { |
| 199 | vex_printf("\n-------- Post-regalloc code --------\n"); |
| 200 | for (i = 0; i < rcode->arr_used; i++) { |
| 201 | vex_printf("%3d ", i); |
| 202 | ppInstr(rcode->arr[i]); |
| 203 | vex_printf("\n"); |
| 204 | } |
sewardj | fbcaf33 | 2004-07-08 01:46:01 +0000 | [diff] [blame] | 205 | vex_printf("\n"); |
| 206 | } |
sewardj | fbcaf33 | 2004-07-08 01:46:01 +0000 | [diff] [blame] | 207 | |
sewardj | 81bd550 | 2004-07-21 18:49:27 +0000 | [diff] [blame] | 208 | /* Assemble */ |
sewardj | 81bd550 | 2004-07-21 18:49:27 +0000 | [diff] [blame] | 209 | out_used = 0; /* tracks along the host_bytes array */ |
| 210 | for (i = 0; i < rcode->arr_used; i++) { |
sewardj | 14731f2 | 2004-07-25 01:24:28 +0000 | [diff] [blame] | 211 | if (vex_verbosity > 1) { |
sewardj | bad34a9 | 2004-07-22 01:14:11 +0000 | [diff] [blame] | 212 | ppInstr(rcode->arr[i]); |
| 213 | vex_printf("\n"); |
| 214 | } |
sewardj | 81bd550 | 2004-07-21 18:49:27 +0000 | [diff] [blame] | 215 | j = (*emit)( insn_bytes, 32, rcode->arr[i] ); |
sewardj | 14731f2 | 2004-07-25 01:24:28 +0000 | [diff] [blame] | 216 | if (vex_verbosity > 1) { |
sewardj | bad34a9 | 2004-07-22 01:14:11 +0000 | [diff] [blame] | 217 | for (k = 0; k < j; k++) |
sewardj | 86898e8 | 2004-07-22 17:26:12 +0000 | [diff] [blame] | 218 | if (insn_bytes[k] < 16) |
| 219 | vex_printf("0%x ", (UInt)insn_bytes[k]); |
| 220 | else |
| 221 | vex_printf("%x ", (UInt)insn_bytes[k]); |
sewardj | bad34a9 | 2004-07-22 01:14:11 +0000 | [diff] [blame] | 222 | vex_printf("\n\n"); |
| 223 | } |
sewardj | 81bd550 | 2004-07-21 18:49:27 +0000 | [diff] [blame] | 224 | if (out_used + j > host_bytes_size) { |
| 225 | LibVEX_ClearTemporary(False); |
sewardj | 58800ff | 2004-07-28 01:51:10 +0000 | [diff] [blame^] | 226 | vex_verbosity = saved_verbosity; |
sewardj | 81bd550 | 2004-07-21 18:49:27 +0000 | [diff] [blame] | 227 | return TransOutputFull; |
| 228 | } |
| 229 | for (k = 0; k < j; k++) { |
| 230 | host_bytes[out_used] = insn_bytes[k]; |
| 231 | out_used++; |
| 232 | } |
| 233 | vassert(out_used <= host_bytes_size); |
| 234 | } |
| 235 | *host_bytes_used = out_used; |
| 236 | |
sewardj | 1f40a0a | 2004-07-21 12:28:07 +0000 | [diff] [blame] | 237 | // LibVEX_ClearTemporary(True); |
| 238 | LibVEX_ClearTemporary(False); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 239 | |
sewardj | 58800ff | 2004-07-28 01:51:10 +0000 | [diff] [blame^] | 240 | vex_verbosity = saved_verbosity; |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 241 | return TransOK; |
| 242 | } |
| 243 | |
| 244 | |
| 245 | |
| 246 | /*---------------------------------------------------------------*/ |
sewardj | c0ee2ed | 2004-07-27 10:29:41 +0000 | [diff] [blame] | 247 | /*--- end main/vex_main.c ---*/ |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 248 | /*---------------------------------------------------------------*/ |