sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 1 | |
| 2 | /*---------------------------------------------------------------*/ |
| 3 | /*--- ---*/ |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame] | 4 | /*--- This file (libvex_ir.h) is ---*/ |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +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 | #ifndef __LIBVEX_IR_H |
| 10 | #define __LIBVEX_IR_H |
sewardj | ac9af02 | 2004-07-05 01:15:34 +0000 | [diff] [blame] | 11 | |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame] | 12 | #include "libvex_basictypes.h" |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 13 | |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 14 | |
| 15 | /*---------------------------------------------------------------*/ |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 16 | /*--- Type definitions for the IR ---*/ |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 17 | /*---------------------------------------------------------------*/ |
| 18 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 19 | /* ------------------ Types ------------------ */ |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 20 | |
| 21 | typedef |
sewardj | 6efd4a1 | 2004-07-15 03:54:23 +0000 | [diff] [blame] | 22 | enum { Ity_INVALID=0x10FFF, |
| 23 | Ity_Bit=0x11000, |
sewardj | d1725d1 | 2004-08-12 20:46:53 +0000 | [diff] [blame] | 24 | Ity_I8, Ity_I16, Ity_I32, Ity_I64, |
| 25 | Ity_F32, Ity_F64 |
| 26 | } |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 27 | IRType; |
| 28 | |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 29 | extern void ppIRType ( IRType ); |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 30 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 31 | |
| 32 | /* ------------------ Constants ------------------ */ |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 33 | |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 34 | typedef |
sewardj | 51cba89 | 2004-08-20 00:09:03 +0000 | [diff] [blame] | 35 | enum { Ico_Bit=0x12000, |
sewardj | 207557a | 2004-08-27 12:00:18 +0000 | [diff] [blame] | 36 | Ico_U8, Ico_U16, Ico_U32, Ico_U64, Ico_F64, |
| 37 | Ico_NaN64 /* 64-bit IEEE NaN. */ |
| 38 | } |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 39 | IRConstTag; |
| 40 | |
| 41 | typedef |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 42 | struct _IRConst { |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 43 | IRConstTag tag; |
| 44 | union { |
sewardj | 51cba89 | 2004-08-20 00:09:03 +0000 | [diff] [blame] | 45 | Bool Bit; |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 46 | UChar U8; |
| 47 | UShort U16; |
| 48 | UInt U32; |
| 49 | ULong U64; |
sewardj | a58ea66 | 2004-08-15 03:12:41 +0000 | [diff] [blame] | 50 | Double F64; |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 51 | } Ico; |
| 52 | } |
| 53 | IRConst; |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 54 | |
sewardj | 207557a | 2004-08-27 12:00:18 +0000 | [diff] [blame] | 55 | extern IRConst* IRConst_Bit ( Bool ); |
| 56 | extern IRConst* IRConst_U8 ( UChar ); |
| 57 | extern IRConst* IRConst_U16 ( UShort ); |
| 58 | extern IRConst* IRConst_U32 ( UInt ); |
| 59 | extern IRConst* IRConst_U64 ( ULong ); |
| 60 | extern IRConst* IRConst_F64 ( Double ); |
| 61 | extern IRConst* IRConst_NaN64 ( void ); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 62 | |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 63 | extern void ppIRConst ( IRConst* ); |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 64 | |
| 65 | |
| 66 | /* ------------------ Temporaries ------------------ */ |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 67 | |
sewardj | fbcaf33 | 2004-07-08 01:46:01 +0000 | [diff] [blame] | 68 | typedef UInt IRTemp; |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 69 | |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 70 | extern void ppIRTemp ( IRTemp ); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 71 | |
sewardj | fbcaf33 | 2004-07-08 01:46:01 +0000 | [diff] [blame] | 72 | #define INVALID_IRTEMP ((IRTemp)0xFFFFFFFF) |
| 73 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 74 | |
| 75 | /* ------------------ Binary and unary ops ------------------ */ |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 76 | |
sewardj | 8f3debf | 2004-09-08 23:42:23 +0000 | [diff] [blame] | 77 | /* Encoding of rounding modes in Float -> Int conversions. This is |
| 78 | the same as the encoding used by Intel IA32 to indicate x87 |
| 79 | rounding mode. */ |
| 80 | typedef |
| 81 | enum { Irrm_NEAREST=0, Irrm_NegINF=1, Irrm_PosINF=2, Irrm_ZERO=3 } |
| 82 | IRRoundingMode; |
| 83 | |
| 84 | /* Floating point comparison result values, as created by Iop_CmpF64. |
| 85 | This is also derived from what IA32 does. */ |
| 86 | typedef |
| 87 | enum { |
| 88 | Ircr_UN = 0x85, |
| 89 | Ircr_LT = 0x01, |
| 90 | Ircr_GT = 0x00, |
| 91 | Ircr_EQ = 0x80 |
| 92 | } |
| 93 | IRCmpF64Result; |
| 94 | |
| 95 | |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 96 | typedef |
sewardj | 41f43bc | 2004-07-08 14:23:22 +0000 | [diff] [blame] | 97 | enum { |
| 98 | /* Do not change this ordering. The IR generators |
| 99 | rely on (eg) Iop_Add64 == IopAdd8 + 3. */ |
sewardj | 66de227 | 2004-07-16 21:19:05 +0000 | [diff] [blame] | 100 | Iop_INVALID=0x13000, |
| 101 | Iop_Add8, Iop_Add16, Iop_Add32, Iop_Add64, |
sewardj | 41f43bc | 2004-07-08 14:23:22 +0000 | [diff] [blame] | 102 | Iop_Sub8, Iop_Sub16, Iop_Sub32, Iop_Sub64, |
sewardj | 41f43bc | 2004-07-08 14:23:22 +0000 | [diff] [blame] | 103 | /* Signless mul. MullS/MullU is elsewhere. */ |
| 104 | Iop_Mul8, Iop_Mul16, Iop_Mul32, Iop_Mul64, |
| 105 | Iop_Or8, Iop_Or16, Iop_Or32, Iop_Or64, |
| 106 | Iop_And8, Iop_And16, Iop_And32, Iop_And64, |
| 107 | Iop_Xor8, Iop_Xor16, Iop_Xor32, Iop_Xor64, |
| 108 | Iop_Shl8, Iop_Shl16, Iop_Shl32, Iop_Shl64, |
| 109 | Iop_Shr8, Iop_Shr16, Iop_Shr32, Iop_Shr64, |
| 110 | Iop_Sar8, Iop_Sar16, Iop_Sar32, Iop_Sar64, |
sewardj | e90ad6a | 2004-07-10 19:02:10 +0000 | [diff] [blame] | 111 | /* Integer comparisons. */ |
| 112 | Iop_CmpEQ8, Iop_CmpEQ16, Iop_CmpEQ32, Iop_CmpEQ64, |
| 113 | Iop_CmpNE8, Iop_CmpNE16, Iop_CmpNE32, Iop_CmpNE64, |
sewardj | 41f43bc | 2004-07-08 14:23:22 +0000 | [diff] [blame] | 114 | /* Tags for unary ops */ |
| 115 | Iop_Not8, Iop_Not16, Iop_Not32, Iop_Not64, |
sewardj | e87b484 | 2004-07-10 12:23:30 +0000 | [diff] [blame] | 116 | Iop_Neg8, Iop_Neg16, Iop_Neg32, Iop_Neg64, |
sewardj | 9690d92 | 2004-07-14 01:39:17 +0000 | [diff] [blame] | 117 | /* Widening multiplies */ |
| 118 | Iop_MullS8, Iop_MullS16, Iop_MullS32, |
| 119 | Iop_MullU8, Iop_MullU16, Iop_MullU32, |
sewardj | 8f3debf | 2004-09-08 23:42:23 +0000 | [diff] [blame] | 120 | |
sewardj | ce646f2 | 2004-08-31 23:55:54 +0000 | [diff] [blame] | 121 | /* Wierdo integer stuff */ |
| 122 | Iop_Clz32, /* count leading zeroes */ |
| 123 | Iop_Ctz32, /* count trailing zeros */ |
sewardj | 8f3debf | 2004-09-08 23:42:23 +0000 | [diff] [blame] | 124 | /* Ctz32/Clz32 are UNDEFINED when given arguments of zero. |
| 125 | You must ensure they are never given a zero argument. |
| 126 | */ |
| 127 | |
sewardj | 9690d92 | 2004-07-14 01:39:17 +0000 | [diff] [blame] | 128 | /* Ordering not important after here. */ |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 129 | Iop_CmpLT32S, |
| 130 | Iop_CmpLE32S, |
| 131 | Iop_CmpLT32U, |
| 132 | Iop_CmpLE32U, |
sewardj | 9690d92 | 2004-07-14 01:39:17 +0000 | [diff] [blame] | 133 | /* Division */ |
sewardj | 8f3debf | 2004-09-08 23:42:23 +0000 | [diff] [blame] | 134 | /* TODO: clarify semantics wrt rounding, negative values, whatever */ |
sewardj | 9690d92 | 2004-07-14 01:39:17 +0000 | [diff] [blame] | 135 | Iop_DivModU64to32, // :: I64,I32 -> I64 |
| 136 | // of which lo half is div and hi half is mod |
| 137 | Iop_DivModS64to32, // ditto, signed |
| 138 | /* Widening conversions */ |
sewardj | e5427e8 | 2004-09-11 19:43:51 +0000 | [diff] [blame] | 139 | Iop_8Uto16, Iop_8Uto32, Iop_16Uto32, Iop_32Uto64, |
sewardj | bb53f8c | 2004-08-14 11:50:01 +0000 | [diff] [blame] | 140 | Iop_8Sto16, Iop_8Sto32, Iop_16Sto32, Iop_32Sto64, |
sewardj | a238471 | 2004-07-29 14:36:40 +0000 | [diff] [blame] | 141 | /* Narrowing conversions */ |
sewardj | 8c7f1ab | 2004-07-29 20:31:09 +0000 | [diff] [blame] | 142 | Iop_32to8, |
sewardj | b81f8b3 | 2004-07-30 10:17:50 +0000 | [diff] [blame] | 143 | /* 8 <-> 16 bit conversions */ |
| 144 | Iop_16to8, // :: I16 -> I8, low half |
| 145 | Iop_16HIto8, // :: I16 -> I8, high half |
| 146 | Iop_8HLto16, // :: (I8,I8) -> I16 |
sewardj | 8c7f1ab | 2004-07-29 20:31:09 +0000 | [diff] [blame] | 147 | /* 16 <-> 32 bit conversions */ |
| 148 | Iop_32to16, // :: I32 -> I16, low half |
| 149 | Iop_32HIto16, // :: I32 -> I16, high half |
| 150 | Iop_16HLto32, // :: (I16,I16) -> I32 |
sewardj | 9690d92 | 2004-07-14 01:39:17 +0000 | [diff] [blame] | 151 | /* 32 <-> 64 bit conversions */ |
sewardj | 8c7f1ab | 2004-07-29 20:31:09 +0000 | [diff] [blame] | 152 | Iop_64to32, // :: I64 -> I32, low half |
sewardj | 9690d92 | 2004-07-14 01:39:17 +0000 | [diff] [blame] | 153 | Iop_64HIto32, // :: I64 -> I32, high half |
| 154 | Iop_32HLto64, // :: (I32,I32) -> I64 |
sewardj | cf780b4 | 2004-07-13 18:42:17 +0000 | [diff] [blame] | 155 | /* 1-bit stuff */ |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 156 | Iop_32to1, /* :: Ity_I32 -> Ity_Bit, just select bit[0] */ |
| 157 | Iop_1Uto8, /* :: Ity_Bit -> Ity_I8, unsigned widen */ |
| 158 | Iop_1Uto32, /* :: Ity_Bit -> Ity_I32, unsigned widen */ |
sewardj | 8f3debf | 2004-09-08 23:42:23 +0000 | [diff] [blame] | 159 | |
| 160 | /* ------ Floating point. We try and be IEEE754 compliant. ------ */ |
| 161 | |
sewardj | 52ace3e | 2004-09-11 17:10:08 +0000 | [diff] [blame] | 162 | /* Binary operations mandated by IEEE754. */ |
sewardj | 46de407 | 2004-09-11 19:23:24 +0000 | [diff] [blame] | 163 | Iop_AddF64, Iop_SubF64, Iop_MulF64, Iop_DivF64, /* Iop_RemF64, */ |
sewardj | cfded9a | 2004-09-09 11:44:16 +0000 | [diff] [blame] | 164 | |
sewardj | 52ace3e | 2004-09-11 17:10:08 +0000 | [diff] [blame] | 165 | /* Binary ops supported by IA32 but not mandated by 754. */ |
sewardj | 46de407 | 2004-09-11 19:23:24 +0000 | [diff] [blame] | 166 | Iop_AtanF64, /* FPATAN, arctan(arg1/arg2) */ |
| 167 | Iop_Yl2xF64, /* FYL2X, arg1 * log2(arg2) */ |
sewardj | 8308aad | 2004-09-12 11:09:54 +0000 | [diff] [blame] | 168 | Iop_Yl2xp1F64, /* FYL2XP1, arg1 * log2(arg2+1.0) */ |
sewardj | 46de407 | 2004-09-11 19:23:24 +0000 | [diff] [blame] | 169 | Iop_PRemF64, /* FPREM, remainder(arg1/arg2) */ |
| 170 | Iop_PRemC3210F64, /* C3210 flags resulting from FPREM, :: I32 */ |
sewardj | 06c32a0 | 2004-09-12 12:07:34 +0000 | [diff] [blame^] | 171 | Iop_ScaleF64, /* FSCALE, arg1 * (2^RoundTowardsZero(arg2)) */ |
sewardj | 52ace3e | 2004-09-11 17:10:08 +0000 | [diff] [blame] | 172 | |
| 173 | /* Unary operations mandated by IEEE754. */ |
| 174 | Iop_NegF64, Iop_SqrtF64, |
| 175 | |
| 176 | /* Unary ops supported by IA32 but not mandated by 754. */ |
| 177 | Iop_AbsF64, /* FABS */ |
| 178 | Iop_SinF64, /* FSIN */ |
| 179 | Iop_CosF64, /* FCOS */ |
sewardj | 06c32a0 | 2004-09-12 12:07:34 +0000 | [diff] [blame^] | 180 | Iop_2xm1F64, /* (2^arg - 1.0) */ |
sewardj | 8f3debf | 2004-09-08 23:42:23 +0000 | [diff] [blame] | 181 | |
| 182 | /* Comparison, yielding GT/LT/EQ/UN(ordered), as per the following: |
sewardj | 883b00b | 2004-09-11 09:30:24 +0000 | [diff] [blame] | 183 | 0x45 Unordered |
sewardj | 8f3debf | 2004-09-08 23:42:23 +0000 | [diff] [blame] | 184 | 0x01 LT |
| 185 | 0x00 GT |
sewardj | 883b00b | 2004-09-11 09:30:24 +0000 | [diff] [blame] | 186 | 0x40 EQ |
sewardj | 8f3debf | 2004-09-08 23:42:23 +0000 | [diff] [blame] | 187 | This just happens to be the Intel encoding. The values |
| 188 | are recorded in the type IRCmpF64Result. |
| 189 | */ |
| 190 | Iop_CmpF64, |
| 191 | |
| 192 | /* int -> double */ |
| 193 | Iop_I32toF64, Iop_I64toF64, |
| 194 | |
| 195 | /* double -> int. These take a first argument :: Ity_I32 |
| 196 | (an IRRoundingMode) which is an indication of the rounding mode, |
| 197 | as per the following encoding: |
| 198 | 00b to nearest (the default) |
| 199 | 01b to -infinity |
| 200 | 10b to +infinity |
| 201 | 11b to zero |
| 202 | This just happens to be the Intel encoding. For reference only, |
| 203 | the PPC encoding is: |
| 204 | 00b to nearest (the default) |
| 205 | 01b to zero |
| 206 | 10b to +infinity |
| 207 | 11b to -infinity |
| 208 | Any PPC -> IR front end will have to translate these PPC |
| 209 | encodings to the standard encodings. |
| 210 | |
| 211 | If one of these conversions gets an out-of-range condition, |
| 212 | or a NaN, as an argument, the result is host-defined. On x86 |
| 213 | the "integer indefinite" value 0x80..00 is produced. |
| 214 | On PPC it is either 0x80..00 or 0x7F..FF depending on the sign |
| 215 | of the argument. |
| 216 | */ |
| 217 | Iop_F64toI64, Iop_F64toI32, Iop_F64toI16, |
sewardj | 52ace3e | 2004-09-11 17:10:08 +0000 | [diff] [blame] | 218 | |
sewardj | e670911 | 2004-09-10 18:37:18 +0000 | [diff] [blame] | 219 | /* F64 -> F64, also takes an I32 first argument encoding the |
| 220 | rounding mode. */ |
| 221 | Iop_RoundF64, |
sewardj | 8f3debf | 2004-09-08 23:42:23 +0000 | [diff] [blame] | 222 | |
| 223 | /* double <-> float. What does this mean -- does it round? */ |
sewardj | 89cd093 | 2004-09-08 18:23:25 +0000 | [diff] [blame] | 224 | Iop_F32toF64, Iop_F64toF32 |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 225 | } |
| 226 | IROp; |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 227 | |
sewardj | ce646f2 | 2004-08-31 23:55:54 +0000 | [diff] [blame] | 228 | |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 229 | extern void ppIROp ( IROp ); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 230 | |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 231 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 232 | /* ------------------ Expressions ------------------ */ |
| 233 | /* |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 234 | data Expr |
sewardj | d1725d1 | 2004-08-12 20:46:53 +0000 | [diff] [blame] | 235 | = GET Int Type -- offset, size |
| 236 | | GETI Expr Type Int Int -- offset, size, minoff, maxoff |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 237 | | TMP Temp -- value of temporary |
| 238 | | BINOP Op Expr Expr -- binary op |
| 239 | | UNOP Op Expr -- unary op |
| 240 | | LDle Type Expr -- load of the given type, Expr:: 32 or 64 |
| 241 | | CONST Const -- 8/16/32/64-bit int constant |
sewardj | d1725d1 | 2004-08-12 20:46:53 +0000 | [diff] [blame] | 242 | |
| 243 | Re GETI. It carries two ints, which give the lowest and highest |
| 244 | possible byte offsets that the GetI can possibly reference. |
| 245 | For example, if the type is Ity_I32, and the Expr may have |
| 246 | a value of M, M+4 or M+8, where M is a translation-time known |
| 247 | constant, then the low and high limits are M and M+11 respectively. |
| 248 | |
| 249 | PUTI carries similar limit values. |
| 250 | |
| 251 | These can be used by IR optimisers to establish aliasing/non-aliasing |
| 252 | between seperate GETI and PUTI terms, which could be used to do |
| 253 | reordering of them, or suchlike things. Clearly it's critical to give |
| 254 | the correct limit values -- this is something that can't be |
| 255 | automatically checked (in general), and so the front-end writers must |
| 256 | be very careful to tell the truth, since not doing so could lead to |
| 257 | obscure IR optimisation bugs. |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 258 | */ |
sewardj | d1725d1 | 2004-08-12 20:46:53 +0000 | [diff] [blame] | 259 | |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 260 | typedef |
sewardj | e670911 | 2004-09-10 18:37:18 +0000 | [diff] [blame] | 261 | enum { Iex_Binder, /* Used only in pattern matching. |
| 262 | Not an expression. */ |
sewardj | d1725d1 | 2004-08-12 20:46:53 +0000 | [diff] [blame] | 263 | Iex_Get, Iex_GetI, Iex_Tmp, Iex_Binop, Iex_Unop, Iex_LDle, |
sewardj | 4042c7e | 2004-07-18 01:28:30 +0000 | [diff] [blame] | 264 | Iex_Const, Iex_CCall, Iex_Mux0X } |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 265 | IRExprTag; |
| 266 | |
| 267 | typedef |
| 268 | struct _IRExpr { |
| 269 | IRExprTag tag; |
| 270 | union { |
sewardj | 443cd9d | 2004-07-18 23:06:45 +0000 | [diff] [blame] | 271 | struct { |
| 272 | Int binder; |
| 273 | } Binder; |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 274 | struct { |
sewardj | fbcaf33 | 2004-07-08 01:46:01 +0000 | [diff] [blame] | 275 | Int offset; |
| 276 | IRType ty; |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 277 | } Get; |
| 278 | struct { |
sewardj | d1725d1 | 2004-08-12 20:46:53 +0000 | [diff] [blame] | 279 | struct _IRExpr* offset; |
| 280 | IRType ty; |
| 281 | UShort minoff; |
| 282 | UShort maxoff; |
| 283 | } GetI; |
| 284 | struct { |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 285 | IRTemp tmp; |
| 286 | } Tmp; |
| 287 | struct { |
| 288 | IROp op; |
| 289 | struct _IRExpr* arg1; |
| 290 | struct _IRExpr* arg2; |
| 291 | } Binop; |
| 292 | struct { |
| 293 | IROp op; |
| 294 | struct _IRExpr* arg; |
| 295 | } Unop; |
| 296 | struct { |
| 297 | IRType ty; |
| 298 | struct _IRExpr* addr; |
| 299 | } LDle; |
| 300 | struct { |
sewardj | 66f2f79 | 2004-06-30 16:37:16 +0000 | [diff] [blame] | 301 | IRConst* con; |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 302 | } Const; |
sewardj | e87b484 | 2004-07-10 12:23:30 +0000 | [diff] [blame] | 303 | struct { |
| 304 | Char* name; |
| 305 | IRType retty; |
| 306 | struct _IRExpr** args; |
| 307 | } CCall; |
sewardj | eeb9ef8 | 2004-07-15 12:39:03 +0000 | [diff] [blame] | 308 | struct { |
| 309 | struct _IRExpr* cond; |
sewardj | eeb9ef8 | 2004-07-15 12:39:03 +0000 | [diff] [blame] | 310 | struct _IRExpr* expr0; |
sewardj | 4042c7e | 2004-07-18 01:28:30 +0000 | [diff] [blame] | 311 | struct _IRExpr* exprX; |
| 312 | } Mux0X; |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 313 | } Iex; |
| 314 | } |
| 315 | IRExpr; |
| 316 | |
sewardj | 443cd9d | 2004-07-18 23:06:45 +0000 | [diff] [blame] | 317 | extern IRExpr* IRExpr_Binder ( Int binder ); |
| 318 | extern IRExpr* IRExpr_Get ( Int off, IRType ty ); |
sewardj | d1725d1 | 2004-08-12 20:46:53 +0000 | [diff] [blame] | 319 | extern IRExpr* IRExpr_GetI ( IRExpr* off, IRType ty, |
| 320 | UShort minoff, UShort maxoff ); |
sewardj | 443cd9d | 2004-07-18 23:06:45 +0000 | [diff] [blame] | 321 | extern IRExpr* IRExpr_Tmp ( IRTemp tmp ); |
| 322 | extern IRExpr* IRExpr_Binop ( IROp op, IRExpr* arg1, IRExpr* arg2 ); |
| 323 | extern IRExpr* IRExpr_Unop ( IROp op, IRExpr* arg ); |
| 324 | extern IRExpr* IRExpr_LDle ( IRType ty, IRExpr* addr ); |
| 325 | extern IRExpr* IRExpr_Const ( IRConst* con ); |
| 326 | extern IRExpr* IRExpr_CCall ( Char* name, IRType retty, IRExpr** args ); |
| 327 | extern IRExpr* IRExpr_Mux0X ( IRExpr* cond, IRExpr* expr0, IRExpr* exprX ); |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 328 | |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 329 | extern void ppIRExpr ( IRExpr* ); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 330 | |
sewardj | e87b484 | 2004-07-10 12:23:30 +0000 | [diff] [blame] | 331 | /* CCall info. The name is the C helper function; the backends |
sewardj | d1725d1 | 2004-08-12 20:46:53 +0000 | [diff] [blame] | 332 | will hand the name to the front ends to get the address of a |
| 333 | host-code helper function to be called. |
sewardj | e87b484 | 2004-07-10 12:23:30 +0000 | [diff] [blame] | 334 | |
| 335 | The args are a NULL-terminated array of arguments. The stated |
| 336 | return IRType, and the implied argument types, must match that |
| 337 | of the function being called well enough so that the back end |
| 338 | can actually generate correct code for the call. (too vague) |
| 339 | |
| 340 | The called function must satisfy the following: |
| 341 | |
| 342 | * no side effects -- must be a pure function |
| 343 | * it may not look at any of the guest state -- must depend |
| 344 | purely on passed parameters |
| 345 | * it may not access guest memory -- since that would |
| 346 | hide guest memory transactions from the instrumenters |
| 347 | */ |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 348 | |
| 349 | /* ------------------ Statements ------------------ */ |
| 350 | /* |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 351 | data Stmt |
| 352 | = PUT Int Int Expr -- offset, size, value |
| 353 | | TMP Temp Expr -- store value in Temp |
| 354 | | STle Expr Expr -- address (32 or 64 bit), value |
sewardj | 64e1d65 | 2004-07-12 14:00:46 +0000 | [diff] [blame] | 355 | | Exit Expr Const -- conditional exit from middle of BB |
| 356 | -- Const is destination guest addr |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 357 | */ |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 358 | typedef |
sewardj | d1725d1 | 2004-08-12 20:46:53 +0000 | [diff] [blame] | 359 | enum { Ist_Put, Ist_PutI, Ist_Tmp, Ist_STle, Ist_Exit } |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 360 | IRStmtTag; |
| 361 | |
| 362 | typedef |
| 363 | struct _IRStmt { |
| 364 | IRStmtTag tag; |
| 365 | union { |
| 366 | struct { |
| 367 | Int offset; |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 368 | IRExpr* expr; |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 369 | } Put; |
| 370 | struct { |
sewardj | d1725d1 | 2004-08-12 20:46:53 +0000 | [diff] [blame] | 371 | IRExpr* offset; |
| 372 | IRExpr* expr; |
| 373 | UShort minoff; |
| 374 | UShort maxoff; |
| 375 | } PutI; |
| 376 | struct { |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 377 | IRTemp tmp; |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 378 | IRExpr* expr; |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 379 | } Tmp; |
| 380 | struct { |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 381 | IRExpr* addr; |
| 382 | IRExpr* data; |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 383 | } STle; |
sewardj | 64e1d65 | 2004-07-12 14:00:46 +0000 | [diff] [blame] | 384 | struct { |
sewardj | 443cd9d | 2004-07-18 23:06:45 +0000 | [diff] [blame] | 385 | IRExpr* cond; |
sewardj | 64e1d65 | 2004-07-12 14:00:46 +0000 | [diff] [blame] | 386 | IRConst* dst; |
| 387 | } Exit; |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 388 | } Ist; |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 389 | } |
| 390 | IRStmt; |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 391 | |
sewardj | eeb9ef8 | 2004-07-15 12:39:03 +0000 | [diff] [blame] | 392 | extern IRStmt* IRStmt_Put ( Int off, IRExpr* value ); |
sewardj | d1725d1 | 2004-08-12 20:46:53 +0000 | [diff] [blame] | 393 | extern IRStmt* IRStmt_PutI ( IRExpr* off, IRExpr* value, |
| 394 | UShort minoff, UShort maxoff ); |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 395 | extern IRStmt* IRStmt_Tmp ( IRTemp tmp, IRExpr* expr ); |
| 396 | extern IRStmt* IRStmt_STle ( IRExpr* addr, IRExpr* value ); |
sewardj | 64e1d65 | 2004-07-12 14:00:46 +0000 | [diff] [blame] | 397 | extern IRStmt* IRStmt_Exit ( IRExpr* cond, IRConst* dst ); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 398 | |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 399 | extern void ppIRStmt ( IRStmt* ); |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 400 | |
sewardj | e90ad6a | 2004-07-10 19:02:10 +0000 | [diff] [blame] | 401 | /* Guards in Put: if NULL, the Put is always done. |
| 402 | If non-NULL, the expr must denote a value of Ity_Bit, and |
| 403 | the Put is only done if this evaluates to 1. The expression |
| 404 | to be stored (expr) will be evaluated regardless of what |
| 405 | the guard is. |
| 406 | */ |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 407 | |
sewardj | e539a40 | 2004-07-14 18:24:17 +0000 | [diff] [blame] | 408 | /* ------------------ Basic Blocks ------------------ */ |
sewardj | 78c19df | 2004-07-12 22:49:27 +0000 | [diff] [blame] | 409 | |
sewardj | e539a40 | 2004-07-14 18:24:17 +0000 | [diff] [blame] | 410 | /* This describes the unconditional jumps which implicitly happen at |
| 411 | the end of each basic block. Conditional jumps -- which can only |
| 412 | be done with the IRStmt_Exit statement -- are implicitly of the |
| 413 | Ijk_Boring kind. */ |
| 414 | |
sewardj | 78c19df | 2004-07-12 22:49:27 +0000 | [diff] [blame] | 415 | typedef |
| 416 | enum { |
sewardj | e8e9d73 | 2004-07-16 21:03:45 +0000 | [diff] [blame] | 417 | Ijk_Boring=0x14000, /* not interesting; just goto next */ |
sewardj | 78c19df | 2004-07-12 22:49:27 +0000 | [diff] [blame] | 418 | Ijk_Call, /* guest is doing a call */ |
| 419 | Ijk_Ret, /* guest is doing a return */ |
| 420 | Ijk_ClientReq, /* do guest client req before continuing */ |
| 421 | Ijk_Syscall, /* do guest syscall before continuing */ |
| 422 | Ijk_Yield /* client is yielding to thread scheduler */ |
| 423 | } |
| 424 | IRJumpKind; |
| 425 | |
| 426 | extern void ppIRJumpKind ( IRJumpKind ); |
| 427 | |
| 428 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 429 | /* A bunch of statements, expressions, etc, are incomplete without an |
| 430 | environment indicating the type of each IRTemp. So this provides |
sewardj | e539a40 | 2004-07-14 18:24:17 +0000 | [diff] [blame] | 431 | one. IR temporaries are really just unsigned ints and so this |
| 432 | provides an array, 0 .. n_types_used-1 of them. |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 433 | */ |
| 434 | typedef |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 435 | struct { |
sewardj | e539a40 | 2004-07-14 18:24:17 +0000 | [diff] [blame] | 436 | IRType* types; |
| 437 | Int types_size; |
| 438 | Int types_used; |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 439 | } |
| 440 | IRTypeEnv; |
| 441 | |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 442 | extern void ppIRTypeEnv ( IRTypeEnv* ); |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 443 | |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 444 | |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 445 | /* Basic blocks contain: |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 446 | - A table giving a type for each temp |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 447 | - An expandable array of statements |
sewardj | e539a40 | 2004-07-14 18:24:17 +0000 | [diff] [blame] | 448 | - An expression of type 32 or 64 bits, depending on the |
| 449 | guest's word size, indicating the next destination. |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 450 | - An indication of any special actions (JumpKind) needed |
| 451 | for this final jump. |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 452 | */ |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 453 | typedef |
| 454 | struct _IRBB { |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 455 | IRTypeEnv* tyenv; |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 456 | IRStmt** stmts; |
| 457 | Int stmts_size; |
| 458 | Int stmts_used; |
sewardj | e539a40 | 2004-07-14 18:24:17 +0000 | [diff] [blame] | 459 | IRExpr* next; |
| 460 | IRJumpKind jumpkind; |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 461 | } |
| 462 | IRBB; |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 463 | |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 464 | extern IRBB* emptyIRBB ( void ); |
| 465 | extern void addStmtToIRBB ( IRBB*, IRStmt* ); |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 466 | |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 467 | extern void ppIRBB ( IRBB* ); |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 468 | |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 469 | |
| 470 | /*---------------------------------------------------------------*/ |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 471 | /*--- Helper functions for the IR ---*/ |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 472 | /*---------------------------------------------------------------*/ |
| 473 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 474 | /* For messing with IR type environments */ |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 475 | extern IRTypeEnv* emptyIRTypeEnv ( void ); |
sewardj | e539a40 | 2004-07-14 18:24:17 +0000 | [diff] [blame] | 476 | extern IRTemp newIRTemp ( IRTypeEnv*, IRType ); |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 477 | extern IRType lookupIRTypeEnv ( IRTypeEnv*, IRTemp ); |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 478 | extern IRTypeEnv* copyIRTypeEnv ( IRTypeEnv* ); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 479 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 480 | /* What is the type of this expression? */ |
sewardj | 6efd4a1 | 2004-07-15 03:54:23 +0000 | [diff] [blame] | 481 | extern IRType typeOfIRConst ( IRConst* ); |
| 482 | extern IRType typeOfIRExpr ( IRTypeEnv*, IRExpr* ); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 483 | |
sewardj | 3543921 | 2004-07-14 22:36:10 +0000 | [diff] [blame] | 484 | /* Sanity check a BB of IR */ |
| 485 | extern void sanityCheckIRBB ( IRBB* bb, IRType guest_word_size ); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 486 | |
sewardj | 6d2638e | 2004-07-15 09:38:27 +0000 | [diff] [blame] | 487 | /* Is this any value actually in the enumeration 'IRType' ? */ |
| 488 | extern Bool isPlausibleType ( IRType ty ); |
| 489 | |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame] | 490 | #endif /* ndef __LIBVEX_IR_H */ |
sewardj | ac9af02 | 2004-07-05 01:15:34 +0000 | [diff] [blame] | 491 | |
| 492 | |
| 493 | /*---------------------------------------------------------------*/ |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame] | 494 | /*--- libvex_ir.h ---*/ |
sewardj | ac9af02 | 2004-07-05 01:15:34 +0000 | [diff] [blame] | 495 | /*---------------------------------------------------------------*/ |