blob: 3ea1b73cf5a1c7973874bf19a2a6d24b9bb71d47 [file] [log] [blame]
sewardjc97096c2004-06-30 09:28:04 +00001
2/*---------------------------------------------------------------*/
3/*--- ---*/
4/*--- This file (host_regs.h) is ---*/
5/*--- Copyright (c) 2004 OpenWorks LLP. All rights reserved. ---*/
6/*--- ---*/
7/*---------------------------------------------------------------*/
8
sewardjf8ed9d82004-11-12 17:40:23 +00009/*
10 This file is part of LibVEX, a library for dynamic binary
11 instrumentation and translation.
12
13 Copyright (C) 2004 OpenWorks, LLP.
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; Version 2 dated June 1991 of the
18 license.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or liability
23 for damages. See the GNU General Public License for more details.
24
25 Neither the names of the U.S. Department of Energy nor the
26 University of California nor the names of its contributors may be
27 used to endorse or promote products derived from this software
28 without prior written permission.
29
30 You should have received a copy of the GNU General Public License
31 along with this program; if not, write to the Free Software
32 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 USA.
34*/
35
sewardjc97096c2004-06-30 09:28:04 +000036#ifndef __HOST_REGS_H
37#define __HOST_REGS_H
38
sewardj887a11a2004-07-05 17:26:47 +000039#include "libvex_basictypes.h"
sewardj35421a32004-07-05 13:12:34 +000040
sewardjc97096c2004-06-30 09:28:04 +000041
sewardj8bc26ee2004-07-01 18:30:32 +000042/*---------------------------------------------------------*/
43/*--- Representing HOST REGISTERS ---*/
44/*---------------------------------------------------------*/
45
sewardjc97096c2004-06-30 09:28:04 +000046/* Host registers. Stuff to represent:
47
48 - The register number
49 - The register class
50 - Whether or not the register is a virtual reg.
51
52 Registers are a 32-bit Int, thusly:
53
sewardjaf5a5222004-07-01 23:14:42 +000054 bits 31-28 are the register class.
55 bits 27-23 are 0000b for real register, 0001b for virtual register
56 bits 23-0 register number
57
58 Note (importantly) that by arranging that the class field is never
59 0000b, any valid register looks like an extremely large int -- at
60 least 2^28 -- and so there is little chance of confusing it with an
61 integer array index in the register allocator.
62
63 Note further that since the class field is never 1111b, no valid
64 register can have the value INVALID_HREG.
sewardjc97096c2004-06-30 09:28:04 +000065
66 There are currently 3 register classes:
67
68 int
69 floating
sewardjaf5a5222004-07-01 23:14:42 +000070 vector
sewardjc97096c2004-06-30 09:28:04 +000071*/
72
73typedef UInt HReg;
74
sewardjaf5a5222004-07-01 23:14:42 +000075/* When extending this, do not use any value > 14 or < 0. */
sewardj4a31b262004-12-01 02:24:44 +000076/* HRegClass describes host register classes which the instruction
77 selectors can speak about. We would not expect all of them to be
78 available on any specific host. For example on x86, the available
79 classes are: Int32, Flt64, Vec128 only.
80*/
sewardjc97096c2004-06-30 09:28:04 +000081typedef
sewardj4a31b262004-12-01 02:24:44 +000082 enum {
83 HRcINVALID=1, /* NOT A VALID REGISTER CLASS */
84 HRcInt32=4, /* 32-bit int */
85 HRcInt64=5, /* 64-bit int */
86 HRcFlt64=6, /* 64-bit float */
87 HRcVec64=7, /* 64-bit SIMD */
88 HRcVec128=8 /* 128-bit SIMD */
sewardjb3d4ce72004-07-02 07:09:23 +000089 }
sewardjc97096c2004-06-30 09:28:04 +000090 HRegClass;
91
sewardj35421a32004-07-05 13:12:34 +000092extern void ppHRegClass ( HRegClass );
sewardjaf5a5222004-07-01 23:14:42 +000093
sewardjc97096c2004-06-30 09:28:04 +000094
95/* Print an HReg in a generic (non-target-specific) way. */
sewardj35421a32004-07-05 13:12:34 +000096extern void ppHReg ( HReg );
sewardjc97096c2004-06-30 09:28:04 +000097
98/* Construct/destruct. */
sewardj666fbac2004-10-14 10:42:15 +000099static inline HReg mkHReg ( UInt regno, HRegClass rc, Bool virtual ) {
100 UInt r24 = regno & 0x00FFFFFF;
101 /* This is critical. The register number field may only
102 occupy 24 bits. */
103 if (r24 != regno)
104 vpanic("mkHReg: regno exceeds 2^24");
105 return regno | (((UInt)rc) << 28) | (virtual ? (1<<24) : 0);
106}
sewardjc97096c2004-06-30 09:28:04 +0000107
sewardj1f40a0a2004-07-21 12:28:07 +0000108static inline HRegClass hregClass ( HReg r ) {
109 UInt rc = r;
110 rc = (rc >> 28) & 0x0F;
sewardj4a31b262004-12-01 02:24:44 +0000111 vassert(rc >= HRcInt32 || rc <= HRcVec128);
sewardj1f40a0a2004-07-21 12:28:07 +0000112 return (HRegClass)rc;
113}
114
115static inline UInt hregNumber ( HReg r ) {
116 return ((UInt)r) & 0x00FFFFFF;
117}
118
119static inline Bool hregIsVirtual ( HReg r ) {
120 return (((UInt)r) & (1<<24)) ? True : False;
121}
122
123
sewardjc97096c2004-06-30 09:28:04 +0000124
sewardjaf5a5222004-07-01 23:14:42 +0000125
sewardj8bc26ee2004-07-01 18:30:32 +0000126#define INVALID_HREG ((HReg)0xFFFFFFFF)
127
128
129/*---------------------------------------------------------*/
130/*--- Recording register usage (for reg-alloc) ---*/
131/*---------------------------------------------------------*/
132
133typedef
134 enum { HRmRead, HRmWrite, HRmModify }
135 HRegMode;
136
137
138/* A struct for recording the usage of registers in instructions.
139 This can get quite large, but we don't expect to allocate them
140 dynamically, so there's no problem.
141*/
142#define N_HREG_USAGE 4
143
144typedef
145 struct {
146 HReg hreg[N_HREG_USAGE];
147 HRegMode mode[N_HREG_USAGE];
148 Int n_used;
149 }
150 HRegUsage;
151
sewardj35421a32004-07-05 13:12:34 +0000152extern void ppHRegUsage ( HRegUsage* );
sewardj8bc26ee2004-07-01 18:30:32 +0000153
sewardj666fbac2004-10-14 10:42:15 +0000154static inline void initHRegUsage ( HRegUsage* tab ) {
155 tab->n_used = 0;
156}
sewardj8bc26ee2004-07-01 18:30:32 +0000157
158/* Add a register to a usage table. Combine incoming read uses with
159 existing write uses into a modify use, and vice versa. Do not
160 create duplicate entries -- each reg should only be mentioned once.
161*/
sewardj53f85a92004-07-02 13:45:17 +0000162extern void addHRegUse ( HRegUsage*, HRegMode, HReg );
sewardj8bc26ee2004-07-01 18:30:32 +0000163
164
165
166/*---------------------------------------------------------*/
167/*--- Indicating register remappings (for reg-alloc) ---*/
168/*---------------------------------------------------------*/
169
sewardj0ec33252004-07-03 13:30:00 +0000170/* Note that such maps can only map virtual regs to real regs.
171 addToHRegRenap will barf if given a pair not of that form. As a
172 result, no valid HRegRemap will bind a real reg to anything, and so
173 if lookupHRegMap is given a real reg, it returns it unchanged.
174 This is precisely the behaviour that the register allocator needs
175 to impose its decisions on the instructions it processes. */
176
sewardj8bc26ee2004-07-01 18:30:32 +0000177#define N_HREG_REMAP 4
178
179typedef
180 struct {
181 HReg orig [N_HREG_REMAP];
182 HReg replacement[N_HREG_REMAP];
183 Int n_used;
184 }
185 HRegRemap;
186
sewardj35421a32004-07-05 13:12:34 +0000187extern void ppHRegRemap ( HRegRemap* );
sewardj8bc26ee2004-07-01 18:30:32 +0000188extern void initHRegRemap ( HRegRemap* );
189extern void addToHRegRemap ( HRegRemap*, HReg, HReg );
190extern HReg lookupHRegRemap ( HRegRemap*, HReg );
191
sewardjc97096c2004-06-30 09:28:04 +0000192
sewardjaf5a5222004-07-01 23:14:42 +0000193/*---------------------------------------------------------*/
194/*--- Abstract instructions ---*/
195/*---------------------------------------------------------*/
196
197/* A type is needed to refer to pointers to instructions of any
198 target. Defining it like this means that HInstr* can stand in for
199 X86Instr*, ArmInstr*, etc. */
200
201typedef void HInstr;
202
203
sewardj194d54a2004-07-03 19:08:18 +0000204/* An expandable array of HInstr*'s. Handy for insn selection and
205 register allocation. n_vregs indicates the number of virtual
206 registers mentioned in the code, something that reg-alloc needs to
207 know. These are required to be numbered 0 .. n_vregs-1.
208*/
sewardj2cd80dc2004-07-02 15:20:40 +0000209typedef
210 struct {
211 HInstr** arr;
212 Int arr_size;
213 Int arr_used;
sewardj194d54a2004-07-03 19:08:18 +0000214 Int n_vregs;
sewardj2cd80dc2004-07-02 15:20:40 +0000215 }
216 HInstrArray;
217
218extern HInstrArray* newHInstrArray ( void );
sewardj2cd80dc2004-07-02 15:20:40 +0000219extern void addHInstr ( HInstrArray*, HInstr* );
220
sewardjaf5a5222004-07-01 23:14:42 +0000221
sewardjc97096c2004-06-30 09:28:04 +0000222#endif /* ndef __HOST_REGS_H */
sewardj8bc26ee2004-07-01 18:30:32 +0000223
sewardj2cd80dc2004-07-02 15:20:40 +0000224
225/*---------------------------------------------------------*/
226/*--- Reg alloc: TODO: move somewhere else ---*/
227/*---------------------------------------------------------*/
228
229extern
230HInstrArray* doRegisterAllocation (
231
232 /* Incoming virtual-registerised code. */
233 HInstrArray* instrs_in,
sewardj2cd80dc2004-07-02 15:20:40 +0000234
235 /* An array listing all the real registers the allocator may use,
236 in no particular order. */
237 HReg* available_real_regs,
238 Int n_available_real_regs,
239
240 /* Return True iff the given insn is a reg-reg move, in which
241 case also return the src and dst regs. */
242 Bool (*isMove) (HInstr*, HReg*, HReg*),
243
244 /* Get info about register usage in this insn. */
245 void (*getRegUsage) (HRegUsage*, HInstr*),
246
247 /* Apply a reg-reg mapping to an insn. */
248 void (*mapRegs) (HRegRemap*, HInstr*),
249
250 /* Return an insn to spill/restore a real reg to a spill slot
251 offset. */
252 HInstr* (*genSpill) ( HReg, Int ),
sewardj2b515872004-07-05 20:50:45 +0000253 HInstr* (*genReload) ( HReg, Int ),
sewardj81ec4182004-10-25 23:15:52 +0000254 Int guest_sizeB,
sewardj2b515872004-07-05 20:50:45 +0000255
256 /* For debug printing only. */
257 void (*ppInstr) ( HInstr* ),
258 void (*ppReg) ( HReg )
sewardj2cd80dc2004-07-02 15:20:40 +0000259);
260
261
262
sewardj8bc26ee2004-07-01 18:30:32 +0000263/*---------------------------------------------------------------*/
264/*--- host_regs.h ---*/
265/*---------------------------------------------------------------*/