sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 1 | |
| 2 | /*---------------------------------------------------------------*/ |
| 3 | /*--- ---*/ |
| 4 | /*--- This file (ir_defs.h) is ---*/ |
| 5 | /*--- Copyright (c) 2004 OpenWorks LLP. All rights reserved. ---*/ |
| 6 | /*--- ---*/ |
| 7 | /*---------------------------------------------------------------*/ |
| 8 | |
| 9 | #ifndef __IR_DEFS_H |
| 10 | #define __IR_DEFS_H |
| 11 | |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 12 | |
| 13 | /*---------------------------------------------------------------*/ |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 14 | /*--- Type definitions for the IR ---*/ |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 15 | /*---------------------------------------------------------------*/ |
| 16 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 17 | /* ------------------ Types ------------------ */ |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 18 | |
| 19 | typedef |
| 20 | enum { Ity_Bit, Ity_I8, Ity_I16, Ity_I32, Ity_I64 } |
| 21 | IRType; |
| 22 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 23 | extern void ppIRType ( FILE* f, IRType ); |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 24 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 25 | |
| 26 | /* ------------------ Constants ------------------ */ |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 27 | |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 28 | typedef |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 29 | enum { Ico_U8, Ico_U16, Ico_U32, Ico_U64 } |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 30 | IRConstTag; |
| 31 | |
| 32 | typedef |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 33 | struct _IRConst { |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 34 | IRConstTag tag; |
| 35 | union { |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 36 | UChar U8; |
| 37 | UShort U16; |
| 38 | UInt U32; |
| 39 | ULong U64; |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 40 | } Ico; |
| 41 | } |
| 42 | IRConst; |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 43 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 44 | extern IRConst* IRConst_U8 ( UChar ); |
| 45 | extern IRConst* IRConst_U16 ( UShort ); |
| 46 | extern IRConst* IRConst_U32 ( UInt ); |
| 47 | extern IRConst* IRConst_U64 ( ULong ); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 48 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 49 | extern void ppIRConst ( FILE* f, IRConst* ); |
| 50 | |
| 51 | |
| 52 | /* ------------------ Temporaries ------------------ */ |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 53 | |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 54 | typedef int IRTemp; |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 55 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 56 | extern void ppIRTemp ( FILE* f, IRTemp ); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 57 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 58 | |
| 59 | /* ------------------ Binary and unary ops ------------------ */ |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 60 | |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 61 | typedef |
| 62 | enum { Iop_Add32, |
| 63 | Iop_Sub32, |
| 64 | Iop_Mul32, |
| 65 | Iop_Or32, |
| 66 | Iop_And32, |
| 67 | Iop_Xor32, |
| 68 | Iop_Shl32, |
| 69 | Iop_Shr32, |
| 70 | Iop_Sar32, |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 71 | /* Tags for unary ops */ |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 72 | Iop_Not32, |
| 73 | Iop_Neg32 |
| 74 | } |
| 75 | IROp; |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 76 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 77 | extern void ppIROp ( FILE* f, IROp ); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 78 | |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 79 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 80 | /* ------------------ Expressions ------------------ */ |
| 81 | /* |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 82 | data Expr |
| 83 | = GET Int Int -- offset, size |
| 84 | | TMP Temp -- value of temporary |
| 85 | | BINOP Op Expr Expr -- binary op |
| 86 | | UNOP Op Expr -- unary op |
| 87 | | LDle Type Expr -- load of the given type, Expr:: 32 or 64 |
| 88 | | CONST Const -- 8/16/32/64-bit int constant |
| 89 | */ |
| 90 | typedef |
| 91 | enum { Iex_Get, Iex_Tmp, Iex_Binop, Iex_Unop, Iex_LDle, Iex_Const } |
| 92 | IRExprTag; |
| 93 | |
| 94 | typedef |
| 95 | struct _IRExpr { |
| 96 | IRExprTag tag; |
| 97 | union { |
| 98 | struct { |
| 99 | Int offset; |
| 100 | Int size; |
| 101 | } Get; |
| 102 | struct { |
| 103 | IRTemp tmp; |
| 104 | } Tmp; |
| 105 | struct { |
| 106 | IROp op; |
| 107 | struct _IRExpr* arg1; |
| 108 | struct _IRExpr* arg2; |
| 109 | } Binop; |
| 110 | struct { |
| 111 | IROp op; |
| 112 | struct _IRExpr* arg; |
| 113 | } Unop; |
| 114 | struct { |
| 115 | IRType ty; |
| 116 | struct _IRExpr* addr; |
| 117 | } LDle; |
| 118 | struct { |
sewardj | 66f2f79 | 2004-06-30 16:37:16 +0000 | [diff] [blame^] | 119 | IRConst* con; |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 120 | } Const; |
| 121 | } Iex; |
| 122 | } |
| 123 | IRExpr; |
| 124 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 125 | extern IRExpr* IRExpr_Get ( Int off, Int sz ); |
| 126 | extern IRExpr* IRExpr_Tmp ( IRTemp tmp ); |
| 127 | extern IRExpr* IRExpr_Binop ( IROp op, IRExpr* arg1, IRExpr* arg2 ); |
| 128 | extern IRExpr* IRExpr_Unop ( IROp op, IRExpr* arg ); |
| 129 | extern IRExpr* IRExpr_LDle ( IRType ty, IRExpr* addr ); |
| 130 | extern IRExpr* IRExpr_Const ( IRConst* con ); |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 131 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 132 | extern void ppIRExpr ( FILE* f, IRExpr* ); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 133 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 134 | |
| 135 | /* ------------------ Statements ------------------ */ |
| 136 | /* |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 137 | data Stmt |
| 138 | = PUT Int Int Expr -- offset, size, value |
| 139 | | TMP Temp Expr -- store value in Temp |
| 140 | | STle Expr Expr -- address (32 or 64 bit), value |
| 141 | */ |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 142 | typedef |
| 143 | enum { Ist_Put, Ist_Tmp, Ist_STle } |
| 144 | IRStmtTag; |
| 145 | |
| 146 | typedef |
| 147 | struct _IRStmt { |
| 148 | IRStmtTag tag; |
| 149 | union { |
| 150 | struct { |
| 151 | Int offset; |
| 152 | Int size; |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 153 | IRExpr* expr; |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 154 | } Put; |
| 155 | struct { |
| 156 | IRTemp tmp; |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 157 | IRExpr* expr; |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 158 | } Tmp; |
| 159 | struct { |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 160 | IRExpr* addr; |
| 161 | IRExpr* data; |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 162 | } STle; |
| 163 | } Ist; |
| 164 | struct _IRStmt* link; |
| 165 | } |
| 166 | IRStmt; |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 167 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 168 | extern IRStmt* IRStmt_Put ( Int off, Int sz, IRExpr* value ); |
| 169 | extern IRStmt* IRStmt_Tmp ( IRTemp tmp, IRExpr* expr ); |
| 170 | extern IRStmt* IRStmt_STle ( IRExpr* addr, IRExpr* value ); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 171 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 172 | extern void ppIRStmt ( FILE* f, IRStmt* ); |
| 173 | |
| 174 | |
| 175 | /* ------------------ Basic block enders. ------------------ */ |
| 176 | /* |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 177 | IRConst represents a guest address, which is either a |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 178 | 32 or 64 bit integer, depending on the architecture we're simulating. |
| 179 | |
| 180 | data Next |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 181 | = UJump Const -- unconditional jump |
| 182 | | CJump01 Expr Const Const -- conditional jump, Expr::TY_Bit |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 183 | | IJump Expr -- jump to unknown address |
| 184 | */ |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 185 | typedef |
| 186 | enum { Inx_UJump, Inx_CJump01, Inx_IJump } |
| 187 | IRNextTag; |
| 188 | |
| 189 | typedef |
sewardj | 66f2f79 | 2004-06-30 16:37:16 +0000 | [diff] [blame^] | 190 | struct { |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 191 | IRNextTag tag; |
| 192 | union { |
| 193 | struct { |
sewardj | 66f2f79 | 2004-06-30 16:37:16 +0000 | [diff] [blame^] | 194 | IRConst* dst; |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 195 | } UJump; |
| 196 | struct { |
sewardj | 66f2f79 | 2004-06-30 16:37:16 +0000 | [diff] [blame^] | 197 | IRExpr* cond; |
| 198 | IRConst* dst0; |
| 199 | IRConst* dst1; |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 200 | } CJump01; |
| 201 | struct { |
sewardj | 66f2f79 | 2004-06-30 16:37:16 +0000 | [diff] [blame^] | 202 | IRExpr* dst; |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 203 | } IJump; |
| 204 | } Inx; |
| 205 | } |
| 206 | IRNext; |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 207 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 208 | extern IRNext* IRNext_UJump ( IRConst* dst ); |
| 209 | |
| 210 | extern void ppIRNext ( FILE* f, IRNext* ); |
| 211 | |
| 212 | |
| 213 | /* ------------------ Basic Blocks ------------------ */ |
| 214 | |
| 215 | /* A bunch of statements, expressions, etc, are incomplete without an |
| 216 | environment indicating the type of each IRTemp. So this provides |
| 217 | one. |
| 218 | */ |
| 219 | typedef |
| 220 | struct { |
| 221 | IRTemp name; |
| 222 | IRType type; |
| 223 | } |
| 224 | IRTypeEnvMaplet; |
| 225 | |
| 226 | typedef |
| 227 | struct { |
| 228 | IRTypeEnvMaplet* map; |
| 229 | Int map_size; |
| 230 | Int map_used; |
| 231 | } |
| 232 | IRTypeEnv; |
| 233 | |
| 234 | extern void ppIRTypeEnv ( FILE* f, IRTypeEnv* ); |
| 235 | |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 236 | |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 237 | /* Basic blocks contain 3 fields: |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 238 | - A table giving a type for each temp |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 239 | - A list of statements |
| 240 | - A Next |
| 241 | */ |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 242 | typedef |
| 243 | struct _IRBB { |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 244 | IRTypeEnv* tyenv; |
| 245 | IRStmt* stmts; |
| 246 | IRNext* next; |
sewardj | ac6b712 | 2004-06-27 01:03:57 +0000 | [diff] [blame] | 247 | } |
| 248 | IRBB; |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 249 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 250 | extern IRBB* mk_IRBB ( IRTypeEnv*, IRStmt*, IRNext* ); |
| 251 | |
| 252 | extern void ppIRBB ( FILE* f, IRBB* ); |
| 253 | |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 254 | |
| 255 | /*---------------------------------------------------------------*/ |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 256 | /*--- Helper functions for the IR ---*/ |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 257 | /*---------------------------------------------------------------*/ |
| 258 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 259 | /* For messing with IR type environments */ |
| 260 | extern IRTypeEnv* newIRTypeEnv ( void ); |
| 261 | extern void addToIRTypeEnv ( IRTypeEnv*, IRTemp, IRType ); |
| 262 | extern IRType lookupIRTypeEnv ( IRTypeEnv*, IRTemp ); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 263 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 264 | /* What is the type of this expression? */ |
| 265 | extern IRType typeOfIRExpr ( IRTypeEnv*, IRExpr* ); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 266 | |
| 267 | |
| 268 | #endif /* ndef __IR_DEFS_H */ |