sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 1 | |
| 2 | /*---------------------------------------------------------------*/ |
| 3 | /*--- ---*/ |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame] | 4 | /*--- This file (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 | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame] | 11 | #include "vex_globals.h" |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 12 | #include "vex_util.h" |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 13 | #include "host_regs.h" |
sewardj | 8af36f1 | 2004-07-21 18:53:20 +0000 | [diff] [blame] | 14 | #include "x86host_defs.h" |
sewardj | c9a6570 | 2004-07-07 16:32:57 +0000 | [diff] [blame] | 15 | #include "x86guest_defs.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, |
| 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); |
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. */ |
| 79 | Bool (*byte_accessible) ( Addr64 ) |
| 80 | ) |
| 81 | { |
sewardj | 81bd550 | 2004-07-21 18:49:27 +0000 | [diff] [blame] | 82 | /* 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. */ |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 85 | HReg* available_real_regs; |
| 86 | Int n_available_real_regs; |
sewardj | 443cd9d | 2004-07-18 23:06:45 +0000 | [diff] [blame] | 87 | 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 ); |
sewardj | 81bd550 | 2004-07-21 18:49:27 +0000 | [diff] [blame] | 97 | Int (*emit) ( UChar*, Int, HInstr* ); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 98 | |
sewardj | c9a6570 | 2004-07-07 16:32:57 +0000 | [diff] [blame] | 99 | Bool host_is_bigendian = False; |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 100 | IRBB* irbb; |
| 101 | HInstrArray* vcode; |
| 102 | HInstrArray* rcode; |
sewardj | 81bd550 | 2004-07-21 18:49:27 +0000 | [diff] [blame] | 103 | Int i, j, k, out_used; |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 104 | |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 105 | vassert(vex_initdone); |
sewardj | 443cd9d | 2004-07-18 23:06:45 +0000 | [diff] [blame] | 106 | LibVEX_ClearTemporary(False); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 107 | |
| 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; |
sewardj | 2b51587 | 2004-07-05 20:50:45 +0000 | [diff] [blame] | 119 | ppInstr = (void(*)(HInstr*)) ppX86Instr; |
| 120 | ppReg = (void(*)(HReg)) ppHRegX86; |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 121 | iselBB = iselBB_X86; |
sewardj | 81bd550 | 2004-07-21 18:49:27 +0000 | [diff] [blame] | 122 | emit = (Int(*)(UChar*,Int,HInstr*)) emit_X86Instr; |
sewardj | c9a6570 | 2004-07-07 16:32:57 +0000 | [diff] [blame] | 123 | host_is_bigendian = False; |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 124 | break; |
| 125 | default: |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame] | 126 | vpanic("LibVEX_Translate: unsupported target insn set"); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | switch (iset_guest) { |
| 130 | case InsnSetX86: |
sewardj | c9a6570 | 2004-07-07 16:32:57 +0000 | [diff] [blame] | 131 | bbToIR = bbToIR_X86Instr; |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 132 | break; |
| 133 | default: |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame] | 134 | vpanic("LibVEX_Translate: unsupported guest insn set"); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | irbb = bbToIR ( guest_bytes, |
| 138 | guest_bytes_addr, |
| 139 | guest_bytes_read, |
sewardj | c9a6570 | 2004-07-07 16:32:57 +0000 | [diff] [blame] | 140 | byte_accessible, |
| 141 | host_is_bigendian ); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 142 | |
| 143 | if (irbb == NULL) { |
| 144 | /* Access failure. */ |
sewardj | 443cd9d | 2004-07-18 23:06:45 +0000 | [diff] [blame] | 145 | LibVEX_ClearTemporary(False); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 146 | return TransAccessFail; |
| 147 | } |
sewardj | 3543921 | 2004-07-14 22:36:10 +0000 | [diff] [blame] | 148 | sanityCheckIRBB(irbb, Ity_I32); |
sewardj | e8e9d73 | 2004-07-16 21:03:45 +0000 | [diff] [blame] | 149 | |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 150 | /* Get the thing instrumented. */ |
| 151 | if (instrument) |
| 152 | irbb = (*instrument)(irbb); |
| 153 | |
| 154 | /* Turn it into virtual-registerised code. */ |
| 155 | vcode = iselBB ( irbb ); |
| 156 | |
sewardj | 1f40a0a | 2004-07-21 12:28:07 +0000 | [diff] [blame] | 157 | 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 | } |
sewardj | fbcaf33 | 2004-07-08 01:46:01 +0000 | [diff] [blame] | 164 | vex_printf("\n"); |
| 165 | } |
sewardj | fbcaf33 | 2004-07-08 01:46:01 +0000 | [diff] [blame] | 166 | |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 167 | /* Register allocate. */ |
| 168 | rcode = doRegisterAllocation ( vcode, available_real_regs, |
| 169 | n_available_real_regs, |
| 170 | isMove, getRegUsage, mapRegs, |
sewardj | 2b51587 | 2004-07-05 20:50:45 +0000 | [diff] [blame] | 171 | genSpill, genReload, |
| 172 | ppInstr, ppReg ); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 173 | |
sewardj | 1f40a0a | 2004-07-21 12:28:07 +0000 | [diff] [blame] | 174 | 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 | } |
sewardj | fbcaf33 | 2004-07-08 01:46:01 +0000 | [diff] [blame] | 181 | vex_printf("\n"); |
| 182 | } |
sewardj | fbcaf33 | 2004-07-08 01:46:01 +0000 | [diff] [blame] | 183 | |
sewardj | 81bd550 | 2004-07-21 18:49:27 +0000 | [diff] [blame] | 184 | /* 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++) { |
sewardj | bad34a9 | 2004-07-22 01:14:11 +0000 | [diff] [blame] | 188 | if (vex_verbosity > 0) { |
| 189 | ppInstr(rcode->arr[i]); |
| 190 | vex_printf("\n"); |
| 191 | } |
sewardj | 81bd550 | 2004-07-21 18:49:27 +0000 | [diff] [blame] | 192 | j = (*emit)( insn_bytes, 32, rcode->arr[i] ); |
sewardj | bad34a9 | 2004-07-22 01:14:11 +0000 | [diff] [blame] | 193 | if (vex_verbosity > 0) { |
| 194 | for (k = 0; k < j; k++) |
sewardj | 86898e8 | 2004-07-22 17:26:12 +0000 | [diff] [blame^] | 195 | if (insn_bytes[k] < 16) |
| 196 | vex_printf("0%x ", (UInt)insn_bytes[k]); |
| 197 | else |
| 198 | vex_printf("%x ", (UInt)insn_bytes[k]); |
sewardj | bad34a9 | 2004-07-22 01:14:11 +0000 | [diff] [blame] | 199 | vex_printf("\n\n"); |
| 200 | } |
sewardj | 81bd550 | 2004-07-21 18:49:27 +0000 | [diff] [blame] | 201 | if (out_used + j > host_bytes_size) { |
| 202 | LibVEX_ClearTemporary(False); |
| 203 | return TransOutputFull; |
| 204 | } |
| 205 | for (k = 0; k < j; k++) { |
| 206 | host_bytes[out_used] = insn_bytes[k]; |
| 207 | out_used++; |
| 208 | } |
| 209 | vassert(out_used <= host_bytes_size); |
| 210 | } |
| 211 | *host_bytes_used = out_used; |
| 212 | |
sewardj | 1f40a0a | 2004-07-21 12:28:07 +0000 | [diff] [blame] | 213 | // LibVEX_ClearTemporary(True); |
| 214 | LibVEX_ClearTemporary(False); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 215 | |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 216 | return TransOK; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | |
| 221 | /*---------------------------------------------------------------*/ |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame] | 222 | /*--- end vex_main.c ---*/ |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 223 | /*---------------------------------------------------------------*/ |