sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 1 | |
| 2 | /*---------------------------------------------------------------*/ |
| 3 | /*--- ---*/ |
| 4 | /*--- This file (ir_defs.c) is ---*/ |
| 5 | /*--- Copyright (c) 2004 OpenWorks LLP. All rights reserved. ---*/ |
| 6 | /*--- ---*/ |
| 7 | /*---------------------------------------------------------------*/ |
| 8 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 9 | #include <stdio.h> |
| 10 | #include <malloc.h> |
| 11 | |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 12 | #include "basictypes.h" |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 13 | #include "ir_defs.h" |
| 14 | |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 15 | |
| 16 | /*---------------------------------------------------------------*/ |
| 17 | /*--- Printing the IR ---*/ |
| 18 | /*---------------------------------------------------------------*/ |
| 19 | |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 20 | void ppIRType ( FILE* f, IRType ty ) |
| 21 | { |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 22 | switch (ty) { |
| 23 | case Ity_Bit: fprintf(f, "Bit"); break; |
| 24 | case Ity_I8: fprintf(f, "I8"); break; |
| 25 | case Ity_I16: fprintf(f, "I16"); break; |
| 26 | case Ity_I32: fprintf(f, "I32"); break; |
| 27 | case Ity_I64: fprintf(f, "I64"); break; |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 28 | default: panic("ppIRType"); |
| 29 | } |
| 30 | } |
| 31 | |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 32 | void ppIRConst ( FILE* f, IRConst* con ) |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 33 | { |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 34 | switch (con->tag) { |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 35 | case Ico_U8: fprintf(f, "0x%x", (UInt)(con->Ico.U8)); break; |
| 36 | case Ico_U16: fprintf(f, "0x%x", (UInt)(con->Ico.U16)); break; |
| 37 | case Ico_U32: fprintf(f, "0x%x", (UInt)(con->Ico.U32)); break; |
| 38 | case Ico_U64: fprintf(f, "0x%llx", (ULong)(con->Ico.U64)); break; |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 39 | default: panic("ppIRConst"); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | void ppIRTemp ( FILE* f, IRTemp tmp ) |
| 44 | { |
| 45 | fprintf(f, "t%d", tmp); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | void ppIROp ( FILE* f, IROp op ) |
| 50 | { |
| 51 | switch (op) { |
| 52 | case Iop_Add32: fprintf(f, "Add32"); break; |
| 53 | case Iop_Sub32: fprintf(f, "Sub32"); break; |
| 54 | case Iop_Mul32: fprintf(f, "Mul32"); break; |
| 55 | case Iop_Or32: fprintf(f, "Or32"); break; |
| 56 | case Iop_And32: fprintf(f, "And32"); break; |
| 57 | case Iop_Xor32: fprintf(f, "Xor32"); break; |
| 58 | case Iop_Shl32: fprintf(f, "Shl32"); break; |
| 59 | case Iop_Shr32: fprintf(f, "Shr32"); break; |
| 60 | case Iop_Sar32: fprintf(f, "Sar32"); break; |
| 61 | case Iop_Not32: fprintf(f, "Not32"); break; |
| 62 | case Iop_Neg32: fprintf(f, "Neg32"); break; |
| 63 | default: panic("ppIROp"); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void ppIRExpr ( FILE* f, IRExpr* e ) |
| 68 | { |
| 69 | switch (e->tag) { |
| 70 | case Iex_Get: |
| 71 | fprintf(f, "GET(%d,%d)", e->Iex.Get.offset, e->Iex.Get.size); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 72 | break; |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 73 | case Iex_Tmp: |
| 74 | ppIRTemp(f, e->Iex.Tmp.tmp); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 75 | break; |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 76 | case Iex_Binop: |
| 77 | ppIROp(f, e->Iex.Binop.op); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 78 | fprintf(f, "("); |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 79 | ppIRExpr(f, e->Iex.Binop.arg1); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 80 | fprintf(f, ","); |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 81 | ppIRExpr(f, e->Iex.Binop.arg2); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 82 | fprintf(f, ")"); |
| 83 | break; |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 84 | case Iex_Unop: |
| 85 | ppIROp(f, e->Iex.Unop.op); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 86 | fprintf(f, "("); |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 87 | ppIRExpr(f, e->Iex.Unop.arg); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 88 | fprintf(f, ")"); |
| 89 | break; |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 90 | case Iex_LDle: |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 91 | fprintf(f, "LDle<"); |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 92 | ppIRType(f, e->Iex.LDle.ty); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 93 | fprintf(f,">("); |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 94 | ppIRExpr(f, e->Iex.LDle.addr); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 95 | fprintf(f,")"); |
| 96 | break; |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 97 | case Iex_Const: |
| 98 | ppIRConst(f, e->Iex.Const.con); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 99 | break; |
| 100 | default: |
| 101 | panic("ppIExpr"); |
| 102 | } |
| 103 | } |
| 104 | |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 105 | void ppIRStmt ( FILE* f, IRStmt* s ) |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 106 | { |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 107 | switch (s->tag) { |
| 108 | case Ist_Put: |
| 109 | fprintf(f, "Put(%d,%d) = ", s->Ist.Put.offset, s->Ist.Put.size); |
| 110 | ppIRExpr(f, s->Ist.Put.expr); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 111 | break; |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 112 | case Ist_Tmp: |
| 113 | ppIRTemp(f, s->Ist.Tmp.tmp); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 114 | fprintf(f," = "); |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 115 | ppIRExpr(f, s->Ist.Tmp.expr); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 116 | break; |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 117 | case Ist_STle: |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 118 | fprintf(f, "STle("); |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 119 | ppIRExpr(f, s->Ist.STle.addr); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 120 | fprintf(f, ") = "); |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 121 | ppIRExpr(f, s->Ist.STle.data); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 122 | break; |
| 123 | default: |
| 124 | panic("ppIRStmt"); |
| 125 | } |
| 126 | } |
| 127 | |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 128 | void ppIRNext ( FILE* f, IRNext* nx ) |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 129 | { |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 130 | switch (nx->tag) { |
| 131 | case Inx_UJump: |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 132 | fprintf(f, "UJump "); |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 133 | ppIRConst(f, nx->Inx.UJump.dst); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 134 | break; |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 135 | case Inx_CJump01: |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 136 | fprintf(f,"CJump01 ("); |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 137 | ppIRExpr(f, nx->Inx.CJump01.cond); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 138 | fprintf(f, ") "); |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 139 | ppIRConst(f, nx->Inx.CJump01.dst0); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 140 | fprintf(f, " "); |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 141 | ppIRConst(f, nx->Inx.CJump01.dst1); |
| 142 | break; |
| 143 | case Inx_IJump: |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 144 | fprintf(f, "IJump "); |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 145 | ppIRExpr(f, nx->Inx.IJump.dst); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 146 | break; |
| 147 | default: |
| 148 | panic("ppIRNext"); |
| 149 | } |
| 150 | } |
| 151 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 152 | void ppIRTypeEnv ( FILE* f, IRTypeEnv* env ) { |
| 153 | UInt i; |
| 154 | for (i = 0; i < env->map_used; i++) { |
| 155 | if (i % 8 == 0) |
| 156 | fprintf(f, " "); |
| 157 | ppIRTemp(f, env->map[i].name); |
| 158 | fprintf(f, ":"); |
| 159 | ppIRType(f, env->map[i].type); |
| 160 | if (i % 8 == 7) |
| 161 | fprintf(f, "\n"); |
| 162 | else |
| 163 | fprintf(f, " "); |
| 164 | } |
| 165 | if (env->map_used > 0 && env->map_used % 8 != 7) |
| 166 | fprintf(f, "\n"); |
| 167 | } |
| 168 | |
| 169 | |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 170 | void ppIRBB ( FILE* f, IRBB* bb ) |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 171 | { |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 172 | IRStmt* s; |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 173 | ppIRTypeEnv(f, bb->tyenv); |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 174 | for (s = bb->stmts; s; s = s->link) { |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 175 | fprintf(f, " "); |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 176 | ppIRStmt(f, s); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 177 | fprintf(f, "\n"); |
| 178 | } |
| 179 | fprintf(f, " "); |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 180 | ppIRNext(f, bb->next); |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 181 | fprintf(f, "\n"); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | /*---------------------------------------------------------------*/ |
| 186 | /*--- Constructors ---*/ |
| 187 | /*---------------------------------------------------------------*/ |
| 188 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 189 | |
| 190 | /* Constructors -- IRConst */ |
| 191 | |
| 192 | IRConst* IRConst_U8 ( UChar u8 ) |
| 193 | { |
| 194 | IRConst* c = malloc(sizeof(IRConst)); |
| 195 | c->tag = Ico_U8; |
| 196 | c->Ico.U8 = u8; |
| 197 | return c; |
| 198 | } |
| 199 | IRConst* IRConst_U16 ( UShort u16 ) |
| 200 | { |
| 201 | IRConst* c = malloc(sizeof(IRConst)); |
| 202 | c->tag = Ico_U16; |
| 203 | c->Ico.U16 = u16; |
| 204 | return c; |
| 205 | } |
| 206 | IRConst* IRConst_U32 ( UInt u32 ) |
| 207 | { |
| 208 | IRConst* c = malloc(sizeof(IRConst)); |
| 209 | c->tag = Ico_U32; |
| 210 | c->Ico.U32 = u32; |
| 211 | return c; |
| 212 | } |
| 213 | IRConst* IRConst_U64 ( ULong u64 ) |
| 214 | { |
| 215 | IRConst* c = malloc(sizeof(IRConst)); |
| 216 | c->tag = Ico_U64; |
| 217 | c->Ico.U64 = u64; |
| 218 | return c; |
| 219 | } |
| 220 | |
| 221 | |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 222 | /* Constructors -- IRExpr */ |
| 223 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 224 | IRExpr* IRExpr_Get ( Int off, Int sz ) { |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 225 | IRExpr* e = malloc(sizeof(IRExpr)); |
| 226 | e->tag = Iex_Get; |
| 227 | e->Iex.Get.offset = off; |
| 228 | e->Iex.Get.size = sz; |
| 229 | return e; |
| 230 | } |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 231 | IRExpr* IRExpr_Tmp ( IRTemp tmp ) { |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 232 | IRExpr* e = malloc(sizeof(IRExpr)); |
| 233 | e->tag = Iex_Tmp; |
| 234 | e->Iex.Tmp.tmp = tmp; |
| 235 | return e; |
| 236 | } |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 237 | IRExpr* IRExpr_Binop ( IROp op, IRExpr* arg1, IRExpr* arg2 ) { |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 238 | IRExpr* e = malloc(sizeof(IRExpr)); |
| 239 | e->tag = Iex_Binop; |
| 240 | e->Iex.Binop.op = op; |
| 241 | e->Iex.Binop.arg1 = arg1; |
| 242 | e->Iex.Binop.arg2 = arg2; |
| 243 | return e; |
| 244 | } |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 245 | IRExpr* IRExpr_Unop ( IROp op, IRExpr* arg ) { |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 246 | IRExpr* e = malloc(sizeof(IRExpr)); |
| 247 | e->tag = Iex_Unop; |
| 248 | e->Iex.Unop.op = op; |
| 249 | e->Iex.Unop.arg = arg; |
| 250 | return e; |
| 251 | } |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 252 | IRExpr* IRExpr_LDle ( IRType ty, IRExpr* addr ) { |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 253 | IRExpr* e = malloc(sizeof(IRExpr)); |
| 254 | e->tag = Iex_LDle; |
| 255 | e->Iex.LDle.ty = ty; |
| 256 | e->Iex.LDle.addr = addr; |
| 257 | return e; |
| 258 | } |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 259 | IRExpr* IRExpr_Const ( IRConst* con ) { |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 260 | IRExpr* e = malloc(sizeof(IRExpr)); |
| 261 | e->tag = Iex_Const; |
| 262 | e->Iex.Const.con = con; |
| 263 | return e; |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 264 | } |
| 265 | |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 266 | |
| 267 | /* Constructors -- IRStmt */ |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 268 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 269 | IRStmt* IRStmt_Put ( Int off, Int sz, IRExpr* value ) { |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 270 | IRStmt* s = malloc(sizeof(IRStmt)); |
| 271 | s->tag = Ist_Put; |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 272 | s->link = NULL; |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 273 | s->Ist.Put.offset = off; |
| 274 | s->Ist.Put.size = sz; |
| 275 | s->Ist.Put.expr = value; |
| 276 | return s; |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 277 | } |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 278 | IRStmt* IRStmt_Tmp ( IRTemp tmp, IRExpr* expr ) { |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 279 | IRStmt* s = malloc(sizeof(IRStmt)); |
| 280 | s->tag = Ist_Tmp; |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 281 | s->link = NULL; |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 282 | s->Ist.Tmp.tmp = tmp; |
| 283 | s->Ist.Tmp.expr = expr; |
| 284 | return s; |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 285 | } |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 286 | IRStmt* IRStmt_STle ( IRExpr* addr, IRExpr* value ) { |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 287 | IRStmt* s = malloc(sizeof(IRStmt)); |
| 288 | s->tag = Ist_STle; |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 289 | s->link = NULL; |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 290 | s->Ist.STle.addr = addr; |
| 291 | s->Ist.STle.data = value; |
| 292 | return s; |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 293 | } |
| 294 | |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 295 | |
| 296 | /* Constructors -- IRNext */ |
| 297 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 298 | IRNext* IRNext_UJump ( IRConst* dst ) { |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 299 | IRNext* nx = malloc(sizeof(IRNext)); |
| 300 | nx->tag = Inx_UJump; |
| 301 | nx->Inx.UJump.dst = dst; |
| 302 | return nx; |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 303 | } |
| 304 | |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 305 | |
| 306 | /* Constructors -- IRBB */ |
| 307 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 308 | IRBB* mk_IRBB ( IRTypeEnv* env, IRStmt* stmts, IRNext* next ) { |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 309 | IRBB* bb = malloc(sizeof(IRBB)); |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 310 | bb->tyenv = env; |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 311 | bb->stmts = stmts; |
| 312 | bb->next = next; |
| 313 | return bb; |
sewardj | ec6ad59 | 2004-06-20 12:26:53 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 317 | /*---------------------------------------------------------------*/ |
| 318 | /*--- Helper functions for the IR ---*/ |
| 319 | /*---------------------------------------------------------------*/ |
| 320 | |
| 321 | IRTypeEnv* newIRTypeEnv ( void ) |
| 322 | { |
| 323 | IRTypeEnv* env = malloc(sizeof(IRTypeEnv)); |
| 324 | env->map = NULL; |
| 325 | env->map_size = 0; |
| 326 | env->map_used = 0; |
| 327 | return env; |
| 328 | } |
sewardj | e3d0d2e | 2004-06-27 10:42:44 +0000 | [diff] [blame] | 329 | |
| 330 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 331 | void addToIRTypeEnv ( IRTypeEnv* env, IRTemp tmp, IRType ty ) |
| 332 | { |
| 333 | assert(env); |
| 334 | assert(env->map_used >= 0); |
| 335 | assert(env->map_size >= 0); |
| 336 | assert(env->map_used <= env->map_size); |
| 337 | if (env->map_used < env->map_size) { |
| 338 | env->map[env->map_used].name = tmp; |
| 339 | env->map[env->map_used].type = ty; |
| 340 | env->map_used++; |
| 341 | } else { |
| 342 | Int i; |
| 343 | Int new_size = env->map_size==0 ? 8 : 2*env->map_size; |
| 344 | IRTypeEnvMaplet* new_map = malloc(new_size * sizeof(IRTypeEnvMaplet)); |
| 345 | for (i = 0; i < env->map_used; i++) |
| 346 | new_map[i] = env->map[i]; |
| 347 | env->map = new_map; |
| 348 | env->map_size = new_size; |
| 349 | return addToIRTypeEnv(env, tmp, ty); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | |
| 354 | IRType lookupIRTypeEnv ( IRTypeEnv* env, IRTemp tmp ) |
| 355 | { |
| 356 | Int i; |
| 357 | for (i = 0; i < env->map_used; i++) |
| 358 | if (env->map[i].name == tmp) |
| 359 | return env->map[i].type; |
| 360 | panic("lookupIRTypeEnv"); |
| 361 | } |
| 362 | |
| 363 | |
| 364 | IRType typeOfIRExpr ( IRTypeEnv* tyenv, IRExpr* e ) |
| 365 | { |
| 366 | switch (e->tag) { |
| 367 | case Iex_Tmp: |
| 368 | return lookupIRTypeEnv(tyenv, e->Iex.Tmp.tmp); |
| 369 | case Iex_Const: |
| 370 | switch (e->Iex.Const.con->tag) { |
| 371 | case Ico_U8: return Ity_I8; |
| 372 | case Ico_U16: return Ity_I16; |
| 373 | case Ico_U32: return Ity_I32; |
| 374 | case Ico_U64: return Ity_I64; |
| 375 | default: panic("typeOfIRExpr:Iex_Const"); |
| 376 | } |
| 377 | break; |
| 378 | case Iex_Binop: |
| 379 | switch (e->Iex.Binop.op) { |
| 380 | case Iop_Add32: case Iop_Sub32: case Iop_Mul32: |
| 381 | case Iop_Or32: case Iop_And32: case Iop_Xor32: |
| 382 | case Iop_Shl32: case Iop_Shr32: case Iop_Sar32: |
| 383 | return Ity_I32; |
| 384 | default: break; |
| 385 | } |
| 386 | break; |
| 387 | default: |
| 388 | break; |
| 389 | } |
| 390 | ppIRExpr(stderr,e); |
| 391 | panic("typeOfIRExpr"); |
| 392 | } |