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 | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 15 | |
| 16 | |
| 17 | /* This file contains the top level interface to the library. */ |
| 18 | |
| 19 | /* --------- Initialise the library. --------- */ |
| 20 | |
| 21 | /* Exported to library client. */ |
| 22 | |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame^] | 23 | void LibVEX_Init ( |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 24 | /* failure exit function */ |
| 25 | void (*failure_exit) ( void ), |
| 26 | /* logging output function */ |
| 27 | void (*log_bytes) ( Char*, Int nbytes ), |
| 28 | /* debug paranoia level */ |
| 29 | Int debuglevel, |
| 30 | /* verbosity level */ |
| 31 | Int verbosity, |
| 32 | /* Are we supporting valgrind checking? */ |
| 33 | Bool valgrind_support, |
| 34 | /* Max # guest insns per bb */ |
| 35 | Int guest_insns_per_bb |
| 36 | ) |
| 37 | { |
| 38 | vassert(!vex_initdone); |
| 39 | vassert(failure_exit); |
| 40 | vex_failure_exit = failure_exit; |
| 41 | vassert(log_bytes); |
| 42 | vex_log_bytes = log_bytes; |
| 43 | vassert(debuglevel >= 0); |
| 44 | vex_debuglevel = debuglevel; |
| 45 | vassert(verbosity >= 0); |
| 46 | vex_verbosity = verbosity; |
| 47 | vex_valgrind_support = valgrind_support; |
| 48 | vassert(guest_insns_per_bb >= 1 && guest_insns_per_bb <= 100); |
| 49 | vex_guest_insns_per_bb = guest_insns_per_bb; |
| 50 | vex_initdone = True; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | /* --------- Make a translation. --------- */ |
| 55 | |
| 56 | /* Exported to library client. */ |
| 57 | |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame^] | 58 | TranslateResult LibVEX_Translate ( |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 59 | /* The instruction sets we are translating from and to. */ |
| 60 | InsnSet iset_guest, |
| 61 | InsnSet iset_host, |
| 62 | /* IN: the block to translate, and its guest address. */ |
| 63 | Char* guest_bytes, |
| 64 | Addr64 guest_bytes_addr, |
| 65 | /* OUT: the number of bytes actually read */ |
| 66 | Int* guest_bytes_read, |
| 67 | /* IN: a place to put the resulting code, and its size */ |
| 68 | Char* host_bytes, |
| 69 | Int host_bytes_size, |
| 70 | /* OUT: how much of the output area is used. */ |
| 71 | Int* host_bytes_used, |
| 72 | /* IN: optionally, an instrumentation function. */ |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 73 | IRBB* (*instrument) ( IRBB* ), |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 74 | /* IN: optionally, an access check function for guest code. */ |
| 75 | Bool (*byte_accessible) ( Addr64 ) |
| 76 | ) |
| 77 | { |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 78 | /* Stuff we need to know for reg-alloc. */ |
| 79 | HReg* available_real_regs; |
| 80 | Int n_available_real_regs; |
| 81 | Bool (*isMove) (HInstr*, HReg*, HReg*); |
| 82 | void (*getRegUsage) (HRegUsage*, HInstr*); |
| 83 | void (*mapRegs) (HRegRemap*, HInstr*); |
| 84 | HInstr* (*genSpill) ( HReg, Int ); |
| 85 | HInstr* (*genReload) ( HReg, Int ); |
| 86 | HInstrArray* (*iselBB) ( IRBB* ); |
| 87 | IRBB* (*bbToIR) ( Char*, Addr64, Int*, Bool(*)(Addr64) ); |
| 88 | |
| 89 | IRBB* irbb; |
| 90 | HInstrArray* vcode; |
| 91 | HInstrArray* rcode; |
| 92 | |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 93 | vassert(vex_initdone); |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame^] | 94 | LibVEX_Clear(False); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 95 | |
| 96 | /* First off, check that the guest and host insn sets |
| 97 | are supported. */ |
| 98 | switch (iset_host) { |
| 99 | case InsnSetX86: |
| 100 | getAllocableRegs_X86 ( &n_available_real_regs, |
| 101 | &available_real_regs ); |
| 102 | isMove = (Bool(*)(HInstr*,HReg*,HReg*)) isMove_X86Instr; |
| 103 | getRegUsage = (void(*)(HRegUsage*,HInstr*)) getRegUsage_X86Instr; |
| 104 | mapRegs = (void(*)(HRegRemap*,HInstr*)) mapRegs_X86Instr; |
| 105 | genSpill = (HInstr*(*)(HReg,Int)) genSpill_X86; |
| 106 | genReload = (HInstr*(*)(HReg,Int)) genReload_X86; |
| 107 | iselBB = iselBB_X86; |
| 108 | break; |
| 109 | default: |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame^] | 110 | vpanic("LibVEX_Translate: unsupported target insn set"); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | switch (iset_guest) { |
| 114 | case InsnSetX86: |
| 115 | bbToIR = NULL; //bbToIR_X86Instr; |
| 116 | break; |
| 117 | default: |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame^] | 118 | vpanic("LibVEX_Translate: unsupported guest insn set"); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | irbb = bbToIR ( guest_bytes, |
| 122 | guest_bytes_addr, |
| 123 | guest_bytes_read, |
| 124 | byte_accessible ); |
| 125 | |
| 126 | if (irbb == NULL) { |
| 127 | /* Access failure. */ |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame^] | 128 | LibVEX_Clear(False); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 129 | return TransAccessFail; |
| 130 | } |
| 131 | |
| 132 | /* Get the thing instrumented. */ |
| 133 | if (instrument) |
| 134 | irbb = (*instrument)(irbb); |
| 135 | |
| 136 | /* Turn it into virtual-registerised code. */ |
| 137 | vcode = iselBB ( irbb ); |
| 138 | |
| 139 | /* Register allocate. */ |
| 140 | rcode = doRegisterAllocation ( vcode, available_real_regs, |
| 141 | n_available_real_regs, |
| 142 | isMove, getRegUsage, mapRegs, |
| 143 | genSpill, genReload ); |
| 144 | |
| 145 | /* Assemble, etc. */ |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame^] | 146 | LibVEX_Clear(True); |
sewardj | f13a16a | 2004-07-05 17:10:14 +0000 | [diff] [blame] | 147 | |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 148 | return TransOK; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | |
| 153 | /*---------------------------------------------------------------*/ |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame^] | 154 | /*--- end vex_main.c ---*/ |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 155 | /*---------------------------------------------------------------*/ |