sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 1 | |
| 2 | /*---------------------------------------------------------------*/ |
| 3 | /*--- ---*/ |
| 4 | /*--- This file (host_regs.h) is ---*/ |
| 5 | /*--- Copyright (c) 2004 OpenWorks LLP. All rights reserved. ---*/ |
| 6 | /*--- ---*/ |
| 7 | /*---------------------------------------------------------------*/ |
| 8 | |
sewardj | f8ed9d8 | 2004-11-12 17:40:23 +0000 | [diff] [blame] | 9 | /* |
| 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 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 36 | #ifndef __HOST_REGS_H |
| 37 | #define __HOST_REGS_H |
| 38 | |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame] | 39 | #include "libvex_basictypes.h" |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 40 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 41 | |
sewardj | 8bc26ee | 2004-07-01 18:30:32 +0000 | [diff] [blame] | 42 | /*---------------------------------------------------------*/ |
| 43 | /*--- Representing HOST REGISTERS ---*/ |
| 44 | /*---------------------------------------------------------*/ |
| 45 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 46 | /* 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 | |
sewardj | af5a522 | 2004-07-01 23:14:42 +0000 | [diff] [blame] | 54 | 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. |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 65 | |
| 66 | There are currently 3 register classes: |
| 67 | |
| 68 | int |
| 69 | floating |
sewardj | af5a522 | 2004-07-01 23:14:42 +0000 | [diff] [blame] | 70 | vector |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 71 | */ |
| 72 | |
| 73 | typedef UInt HReg; |
| 74 | |
sewardj | af5a522 | 2004-07-01 23:14:42 +0000 | [diff] [blame] | 75 | /* When extending this, do not use any value > 14 or < 0. */ |
sewardj | 4a31b26 | 2004-12-01 02:24:44 +0000 | [diff] [blame^] | 76 | /* 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 | */ |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 81 | typedef |
sewardj | 4a31b26 | 2004-12-01 02:24:44 +0000 | [diff] [blame^] | 82 | 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 */ |
sewardj | b3d4ce7 | 2004-07-02 07:09:23 +0000 | [diff] [blame] | 89 | } |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 90 | HRegClass; |
| 91 | |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 92 | extern void ppHRegClass ( HRegClass ); |
sewardj | af5a522 | 2004-07-01 23:14:42 +0000 | [diff] [blame] | 93 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 94 | |
| 95 | /* Print an HReg in a generic (non-target-specific) way. */ |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 96 | extern void ppHReg ( HReg ); |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 97 | |
| 98 | /* Construct/destruct. */ |
sewardj | 666fbac | 2004-10-14 10:42:15 +0000 | [diff] [blame] | 99 | static 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 | } |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 107 | |
sewardj | 1f40a0a | 2004-07-21 12:28:07 +0000 | [diff] [blame] | 108 | static inline HRegClass hregClass ( HReg r ) { |
| 109 | UInt rc = r; |
| 110 | rc = (rc >> 28) & 0x0F; |
sewardj | 4a31b26 | 2004-12-01 02:24:44 +0000 | [diff] [blame^] | 111 | vassert(rc >= HRcInt32 || rc <= HRcVec128); |
sewardj | 1f40a0a | 2004-07-21 12:28:07 +0000 | [diff] [blame] | 112 | return (HRegClass)rc; |
| 113 | } |
| 114 | |
| 115 | static inline UInt hregNumber ( HReg r ) { |
| 116 | return ((UInt)r) & 0x00FFFFFF; |
| 117 | } |
| 118 | |
| 119 | static inline Bool hregIsVirtual ( HReg r ) { |
| 120 | return (((UInt)r) & (1<<24)) ? True : False; |
| 121 | } |
| 122 | |
| 123 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 124 | |
sewardj | af5a522 | 2004-07-01 23:14:42 +0000 | [diff] [blame] | 125 | |
sewardj | 8bc26ee | 2004-07-01 18:30:32 +0000 | [diff] [blame] | 126 | #define INVALID_HREG ((HReg)0xFFFFFFFF) |
| 127 | |
| 128 | |
| 129 | /*---------------------------------------------------------*/ |
| 130 | /*--- Recording register usage (for reg-alloc) ---*/ |
| 131 | /*---------------------------------------------------------*/ |
| 132 | |
| 133 | typedef |
| 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 | |
| 144 | typedef |
| 145 | struct { |
| 146 | HReg hreg[N_HREG_USAGE]; |
| 147 | HRegMode mode[N_HREG_USAGE]; |
| 148 | Int n_used; |
| 149 | } |
| 150 | HRegUsage; |
| 151 | |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 152 | extern void ppHRegUsage ( HRegUsage* ); |
sewardj | 8bc26ee | 2004-07-01 18:30:32 +0000 | [diff] [blame] | 153 | |
sewardj | 666fbac | 2004-10-14 10:42:15 +0000 | [diff] [blame] | 154 | static inline void initHRegUsage ( HRegUsage* tab ) { |
| 155 | tab->n_used = 0; |
| 156 | } |
sewardj | 8bc26ee | 2004-07-01 18:30:32 +0000 | [diff] [blame] | 157 | |
| 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 | */ |
sewardj | 53f85a9 | 2004-07-02 13:45:17 +0000 | [diff] [blame] | 162 | extern void addHRegUse ( HRegUsage*, HRegMode, HReg ); |
sewardj | 8bc26ee | 2004-07-01 18:30:32 +0000 | [diff] [blame] | 163 | |
| 164 | |
| 165 | |
| 166 | /*---------------------------------------------------------*/ |
| 167 | /*--- Indicating register remappings (for reg-alloc) ---*/ |
| 168 | /*---------------------------------------------------------*/ |
| 169 | |
sewardj | 0ec3325 | 2004-07-03 13:30:00 +0000 | [diff] [blame] | 170 | /* 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 | |
sewardj | 8bc26ee | 2004-07-01 18:30:32 +0000 | [diff] [blame] | 177 | #define N_HREG_REMAP 4 |
| 178 | |
| 179 | typedef |
| 180 | struct { |
| 181 | HReg orig [N_HREG_REMAP]; |
| 182 | HReg replacement[N_HREG_REMAP]; |
| 183 | Int n_used; |
| 184 | } |
| 185 | HRegRemap; |
| 186 | |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 187 | extern void ppHRegRemap ( HRegRemap* ); |
sewardj | 8bc26ee | 2004-07-01 18:30:32 +0000 | [diff] [blame] | 188 | extern void initHRegRemap ( HRegRemap* ); |
| 189 | extern void addToHRegRemap ( HRegRemap*, HReg, HReg ); |
| 190 | extern HReg lookupHRegRemap ( HRegRemap*, HReg ); |
| 191 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 192 | |
sewardj | af5a522 | 2004-07-01 23:14:42 +0000 | [diff] [blame] | 193 | /*---------------------------------------------------------*/ |
| 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 | |
| 201 | typedef void HInstr; |
| 202 | |
| 203 | |
sewardj | 194d54a | 2004-07-03 19:08:18 +0000 | [diff] [blame] | 204 | /* 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 | */ |
sewardj | 2cd80dc | 2004-07-02 15:20:40 +0000 | [diff] [blame] | 209 | typedef |
| 210 | struct { |
| 211 | HInstr** arr; |
| 212 | Int arr_size; |
| 213 | Int arr_used; |
sewardj | 194d54a | 2004-07-03 19:08:18 +0000 | [diff] [blame] | 214 | Int n_vregs; |
sewardj | 2cd80dc | 2004-07-02 15:20:40 +0000 | [diff] [blame] | 215 | } |
| 216 | HInstrArray; |
| 217 | |
| 218 | extern HInstrArray* newHInstrArray ( void ); |
sewardj | 2cd80dc | 2004-07-02 15:20:40 +0000 | [diff] [blame] | 219 | extern void addHInstr ( HInstrArray*, HInstr* ); |
| 220 | |
sewardj | af5a522 | 2004-07-01 23:14:42 +0000 | [diff] [blame] | 221 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 222 | #endif /* ndef __HOST_REGS_H */ |
sewardj | 8bc26ee | 2004-07-01 18:30:32 +0000 | [diff] [blame] | 223 | |
sewardj | 2cd80dc | 2004-07-02 15:20:40 +0000 | [diff] [blame] | 224 | |
| 225 | /*---------------------------------------------------------*/ |
| 226 | /*--- Reg alloc: TODO: move somewhere else ---*/ |
| 227 | /*---------------------------------------------------------*/ |
| 228 | |
| 229 | extern |
| 230 | HInstrArray* doRegisterAllocation ( |
| 231 | |
| 232 | /* Incoming virtual-registerised code. */ |
| 233 | HInstrArray* instrs_in, |
sewardj | 2cd80dc | 2004-07-02 15:20:40 +0000 | [diff] [blame] | 234 | |
| 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 ), |
sewardj | 2b51587 | 2004-07-05 20:50:45 +0000 | [diff] [blame] | 253 | HInstr* (*genReload) ( HReg, Int ), |
sewardj | 81ec418 | 2004-10-25 23:15:52 +0000 | [diff] [blame] | 254 | Int guest_sizeB, |
sewardj | 2b51587 | 2004-07-05 20:50:45 +0000 | [diff] [blame] | 255 | |
| 256 | /* For debug printing only. */ |
| 257 | void (*ppInstr) ( HInstr* ), |
| 258 | void (*ppReg) ( HReg ) |
sewardj | 2cd80dc | 2004-07-02 15:20:40 +0000 | [diff] [blame] | 259 | ); |
| 260 | |
| 261 | |
| 262 | |
sewardj | 8bc26ee | 2004-07-01 18:30:32 +0000 | [diff] [blame] | 263 | /*---------------------------------------------------------------*/ |
| 264 | /*--- host_regs.h ---*/ |
| 265 | /*---------------------------------------------------------------*/ |