blob: c9f4b4bd887ab9cc888085626da32b8922970e53 [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 */
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
sewardj887a11a2004-07-05 17:26:47 +000058TranslateResult LibVEX_Translate (
sewardj35421a32004-07-05 13:12:34 +000059 /* 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. */
sewardjf13a16a2004-07-05 17:10:14 +000073 IRBB* (*instrument) ( IRBB* ),
sewardj35421a32004-07-05 13:12:34 +000074 /* IN: optionally, an access check function for guest code. */
75 Bool (*byte_accessible) ( Addr64 )
76)
77{
sewardjf13a16a2004-07-05 17:10:14 +000078 /* 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
sewardj35421a32004-07-05 13:12:34 +000093 vassert(vex_initdone);
sewardj887a11a2004-07-05 17:26:47 +000094 LibVEX_Clear(False);
sewardjf13a16a2004-07-05 17:10:14 +000095
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:
sewardj887a11a2004-07-05 17:26:47 +0000110 vpanic("LibVEX_Translate: unsupported target insn set");
sewardjf13a16a2004-07-05 17:10:14 +0000111 }
112
113 switch (iset_guest) {
114 case InsnSetX86:
115 bbToIR = NULL; //bbToIR_X86Instr;
116 break;
117 default:
sewardj887a11a2004-07-05 17:26:47 +0000118 vpanic("LibVEX_Translate: unsupported guest insn set");
sewardjf13a16a2004-07-05 17:10:14 +0000119 }
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. */
sewardj887a11a2004-07-05 17:26:47 +0000128 LibVEX_Clear(False);
sewardjf13a16a2004-07-05 17:10:14 +0000129 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. */
sewardj887a11a2004-07-05 17:26:47 +0000146 LibVEX_Clear(True);
sewardjf13a16a2004-07-05 17:10:14 +0000147
sewardj35421a32004-07-05 13:12:34 +0000148 return TransOK;
149}
150
151
152
153/*---------------------------------------------------------------*/
sewardj887a11a2004-07-05 17:26:47 +0000154/*--- end vex_main.c ---*/
sewardj35421a32004-07-05 13:12:34 +0000155/*---------------------------------------------------------------*/