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" |
| 14 | #include "x86h_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. */ |
| 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. */ |
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 | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 82 | /* Stuff we need to know for reg-alloc. */ |
| 83 | HReg* available_real_regs; |
| 84 | Int n_available_real_regs; |
sewardj | 443cd9d | 2004-07-18 23:06:45 +0000 | [diff] [blame^] | 85 | 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 ); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 95 | |
sewardj | c9a6570 | 2004-07-07 16:32:57 +0000 | [diff] [blame] | 96 | Bool host_is_bigendian = False; |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 97 | IRBB* irbb; |
| 98 | HInstrArray* vcode; |
| 99 | HInstrArray* rcode; |
sewardj | fbcaf33 | 2004-07-08 01:46:01 +0000 | [diff] [blame] | 100 | Int i; |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 101 | |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 102 | vassert(vex_initdone); |
sewardj | 443cd9d | 2004-07-18 23:06:45 +0000 | [diff] [blame^] | 103 | LibVEX_ClearTemporary(False); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 104 | |
| 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; |
sewardj | 2b51587 | 2004-07-05 20:50:45 +0000 | [diff] [blame] | 116 | ppInstr = (void(*)(HInstr*)) ppX86Instr; |
| 117 | ppReg = (void(*)(HReg)) ppHRegX86; |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 118 | iselBB = iselBB_X86; |
sewardj | c9a6570 | 2004-07-07 16:32:57 +0000 | [diff] [blame] | 119 | host_is_bigendian = False; |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 120 | break; |
| 121 | default: |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame] | 122 | vpanic("LibVEX_Translate: unsupported target insn set"); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | switch (iset_guest) { |
| 126 | case InsnSetX86: |
sewardj | c9a6570 | 2004-07-07 16:32:57 +0000 | [diff] [blame] | 127 | bbToIR = bbToIR_X86Instr; |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 128 | break; |
| 129 | default: |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame] | 130 | vpanic("LibVEX_Translate: unsupported guest insn set"); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | irbb = bbToIR ( guest_bytes, |
| 134 | guest_bytes_addr, |
| 135 | guest_bytes_read, |
sewardj | c9a6570 | 2004-07-07 16:32:57 +0000 | [diff] [blame] | 136 | byte_accessible, |
| 137 | host_is_bigendian ); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 138 | |
| 139 | if (irbb == NULL) { |
| 140 | /* Access failure. */ |
sewardj | 443cd9d | 2004-07-18 23:06:45 +0000 | [diff] [blame^] | 141 | LibVEX_ClearTemporary(False); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 142 | return TransAccessFail; |
| 143 | } |
sewardj | 3543921 | 2004-07-14 22:36:10 +0000 | [diff] [blame] | 144 | sanityCheckIRBB(irbb, Ity_I32); |
sewardj | e8e9d73 | 2004-07-16 21:03:45 +0000 | [diff] [blame] | 145 | |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 146 | /* Get the thing instrumented. */ |
| 147 | if (instrument) |
| 148 | irbb = (*instrument)(irbb); |
| 149 | |
| 150 | /* Turn it into virtual-registerised code. */ |
| 151 | vcode = iselBB ( irbb ); |
sewardj | 443cd9d | 2004-07-18 23:06:45 +0000 | [diff] [blame^] | 152 | LibVEX_ClearTemporary(True); return TransOK; |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 153 | |
sewardj | fbcaf33 | 2004-07-08 01:46:01 +0000 | [diff] [blame] | 154 | vex_printf("\n-------- Virtual registerised code --------\n"); |
| 155 | for (i = 0; i < vcode->arr_used; i++) { |
| 156 | ppInstr(vcode->arr[i]); |
| 157 | vex_printf("\n"); |
| 158 | } |
| 159 | vex_printf("\n"); |
| 160 | |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 161 | /* Register allocate. */ |
| 162 | rcode = doRegisterAllocation ( vcode, available_real_regs, |
| 163 | n_available_real_regs, |
| 164 | isMove, getRegUsage, mapRegs, |
sewardj | 2b51587 | 2004-07-05 20:50:45 +0000 | [diff] [blame] | 165 | genSpill, genReload, |
| 166 | ppInstr, ppReg ); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 167 | |
sewardj | fbcaf33 | 2004-07-08 01:46:01 +0000 | [diff] [blame] | 168 | vex_printf("\n-------- Post-regalloc code --------\n"); |
| 169 | for (i = 0; i < rcode->arr_used; i++) { |
| 170 | ppInstr(rcode->arr[i]); |
| 171 | vex_printf("\n"); |
| 172 | } |
| 173 | vex_printf("\n"); |
| 174 | |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 175 | /* Assemble, etc. */ |
sewardj | 443cd9d | 2004-07-18 23:06:45 +0000 | [diff] [blame^] | 176 | LibVEX_ClearTemporary(True); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 177 | |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 178 | return TransOK; |
| 179 | } |
| 180 | |
| 181 | |
| 182 | |
| 183 | /*---------------------------------------------------------------*/ |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame] | 184 | /*--- end vex_main.c ---*/ |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 185 | /*---------------------------------------------------------------*/ |