blob: 9d64e81ba0a2b2d5ca3197b154d0a21f32a10c6f [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"
sewardj35421a32004-07-05 13:12:34 +000015
16
17/* This file contains the top level interface to the library. */
18
19/* --------- Initialise the library. --------- */
20
21/* Exported to library client. */
22
sewardj887a11a2004-07-05 17:26:47 +000023void LibVEX_Init (
sewardj35421a32004-07-05 13:12:34 +000024 /* failure exit function */
sewardj2b515872004-07-05 20:50:45 +000025 __attribute__ ((noreturn))
sewardj35421a32004-07-05 13:12:34 +000026 void (*failure_exit) ( void ),
27 /* logging output function */
28 void (*log_bytes) ( Char*, Int nbytes ),
29 /* debug paranoia level */
30 Int debuglevel,
31 /* verbosity level */
32 Int verbosity,
33 /* Are we supporting valgrind checking? */
34 Bool valgrind_support,
35 /* Max # guest insns per bb */
36 Int guest_insns_per_bb
37)
38{
39 vassert(!vex_initdone);
40 vassert(failure_exit);
41 vex_failure_exit = failure_exit;
42 vassert(log_bytes);
43 vex_log_bytes = log_bytes;
44 vassert(debuglevel >= 0);
45 vex_debuglevel = debuglevel;
46 vassert(verbosity >= 0);
47 vex_verbosity = verbosity;
48 vex_valgrind_support = valgrind_support;
49 vassert(guest_insns_per_bb >= 1 && guest_insns_per_bb <= 100);
50 vex_guest_insns_per_bb = guest_insns_per_bb;
51 vex_initdone = True;
52}
53
54
55/* --------- Make a translation. --------- */
56
57/* Exported to library client. */
58
sewardj887a11a2004-07-05 17:26:47 +000059TranslateResult LibVEX_Translate (
sewardj35421a32004-07-05 13:12:34 +000060 /* The instruction sets we are translating from and to. */
61 InsnSet iset_guest,
62 InsnSet iset_host,
63 /* IN: the block to translate, and its guest address. */
64 Char* guest_bytes,
65 Addr64 guest_bytes_addr,
66 /* OUT: the number of bytes actually read */
67 Int* guest_bytes_read,
68 /* IN: a place to put the resulting code, and its size */
69 Char* host_bytes,
70 Int host_bytes_size,
71 /* OUT: how much of the output area is used. */
72 Int* host_bytes_used,
73 /* IN: optionally, an instrumentation function. */
sewardjf13a16a2004-07-05 17:10:14 +000074 IRBB* (*instrument) ( IRBB* ),
sewardj35421a32004-07-05 13:12:34 +000075 /* IN: optionally, an access check function for guest code. */
76 Bool (*byte_accessible) ( Addr64 )
77)
78{
sewardjf13a16a2004-07-05 17:10:14 +000079 /* Stuff we need to know for reg-alloc. */
80 HReg* available_real_regs;
81 Int n_available_real_regs;
82 Bool (*isMove) (HInstr*, HReg*, HReg*);
83 void (*getRegUsage) (HRegUsage*, HInstr*);
84 void (*mapRegs) (HRegRemap*, HInstr*);
85 HInstr* (*genSpill) ( HReg, Int );
86 HInstr* (*genReload) ( HReg, Int );
sewardj2b515872004-07-05 20:50:45 +000087 void (*ppInstr) ( HInstr* );
88 void (*ppReg) ( HReg );
sewardjf13a16a2004-07-05 17:10:14 +000089 HInstrArray* (*iselBB) ( IRBB* );
90 IRBB* (*bbToIR) ( Char*, Addr64, Int*, Bool(*)(Addr64) );
91
92 IRBB* irbb;
93 HInstrArray* vcode;
94 HInstrArray* rcode;
95
sewardj35421a32004-07-05 13:12:34 +000096 vassert(vex_initdone);
sewardj887a11a2004-07-05 17:26:47 +000097 LibVEX_Clear(False);
sewardjf13a16a2004-07-05 17:10:14 +000098
99 /* First off, check that the guest and host insn sets
100 are supported. */
101 switch (iset_host) {
102 case InsnSetX86:
103 getAllocableRegs_X86 ( &n_available_real_regs,
104 &available_real_regs );
105 isMove = (Bool(*)(HInstr*,HReg*,HReg*)) isMove_X86Instr;
106 getRegUsage = (void(*)(HRegUsage*,HInstr*)) getRegUsage_X86Instr;
107 mapRegs = (void(*)(HRegRemap*,HInstr*)) mapRegs_X86Instr;
108 genSpill = (HInstr*(*)(HReg,Int)) genSpill_X86;
109 genReload = (HInstr*(*)(HReg,Int)) genReload_X86;
sewardj2b515872004-07-05 20:50:45 +0000110 ppInstr = (void(*)(HInstr*)) ppX86Instr;
111 ppReg = (void(*)(HReg)) ppHRegX86;
sewardjf13a16a2004-07-05 17:10:14 +0000112 iselBB = iselBB_X86;
113 break;
114 default:
sewardj887a11a2004-07-05 17:26:47 +0000115 vpanic("LibVEX_Translate: unsupported target insn set");
sewardjf13a16a2004-07-05 17:10:14 +0000116 }
117
118 switch (iset_guest) {
119 case InsnSetX86:
120 bbToIR = NULL; //bbToIR_X86Instr;
121 break;
122 default:
sewardj887a11a2004-07-05 17:26:47 +0000123 vpanic("LibVEX_Translate: unsupported guest insn set");
sewardjf13a16a2004-07-05 17:10:14 +0000124 }
125
126 irbb = bbToIR ( guest_bytes,
127 guest_bytes_addr,
128 guest_bytes_read,
129 byte_accessible );
130
131 if (irbb == NULL) {
132 /* Access failure. */
sewardj887a11a2004-07-05 17:26:47 +0000133 LibVEX_Clear(False);
sewardjf13a16a2004-07-05 17:10:14 +0000134 return TransAccessFail;
135 }
136
137 /* Get the thing instrumented. */
138 if (instrument)
139 irbb = (*instrument)(irbb);
140
141 /* Turn it into virtual-registerised code. */
142 vcode = iselBB ( irbb );
143
144 /* Register allocate. */
145 rcode = doRegisterAllocation ( vcode, available_real_regs,
146 n_available_real_regs,
147 isMove, getRegUsage, mapRegs,
sewardj2b515872004-07-05 20:50:45 +0000148 genSpill, genReload,
149 ppInstr, ppReg );
sewardjf13a16a2004-07-05 17:10:14 +0000150
151 /* Assemble, etc. */
sewardj887a11a2004-07-05 17:26:47 +0000152 LibVEX_Clear(True);
sewardjf13a16a2004-07-05 17:10:14 +0000153
sewardj35421a32004-07-05 13:12:34 +0000154 return TransOK;
155}
156
157
158
159/*---------------------------------------------------------------*/
sewardj887a11a2004-07-05 17:26:47 +0000160/*--- end vex_main.c ---*/
sewardj35421a32004-07-05 13:12:34 +0000161/*---------------------------------------------------------------*/