Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===- ReadInst.cpp - Code to read an instruction from bytecode -------------=== |
| 2 | // |
| 3 | // This file defines the mechanism to read an instruction from a bytecode |
| 4 | // stream. |
| 5 | // |
| 6 | // Note that this library should be as fast as possible, reentrant, and |
| 7 | // threadsafe!! |
| 8 | // |
| 9 | // TODO: Change from getValue(Raw.Arg1) etc, to getArg(Raw, 1) |
| 10 | // Make it check type, so that casts are checked. |
| 11 | // |
| 12 | //===------------------------------------------------------------------------=== |
| 13 | |
| 14 | #include "llvm/iOther.h" |
| 15 | #include "llvm/iTerminators.h" |
| 16 | #include "llvm/iMemory.h" |
| 17 | #include "llvm/DerivedTypes.h" |
| 18 | #include "ReaderInternals.h" |
| 19 | |
| 20 | bool BytecodeParser::ParseRawInst(const uchar *&Buf, const uchar *EndBuf, |
| 21 | RawInst &Result) { |
| 22 | unsigned Op, Typ; |
| 23 | if (read(Buf, EndBuf, Op)) return true; |
| 24 | |
| 25 | Result.NumOperands = Op >> 30; |
| 26 | Result.Opcode = (Op >> 24) & 63; |
| 27 | |
| 28 | switch (Result.NumOperands) { |
| 29 | case 1: |
| 30 | Result.Ty = getType((Op >> 12) & 4095); |
| 31 | Result.Arg1 = Op & 4095; |
| 32 | if (Result.Arg1 == 4095) // Handle special encoding for 0 operands... |
| 33 | Result.NumOperands = 0; |
| 34 | break; |
| 35 | case 2: |
| 36 | Result.Ty = getType((Op >> 16) & 255); |
| 37 | Result.Arg1 = (Op >> 8 ) & 255; |
| 38 | Result.Arg2 = (Op >> 0 ) & 255; |
| 39 | break; |
| 40 | case 3: |
| 41 | Result.Ty = getType((Op >> 18) & 63); |
| 42 | Result.Arg1 = (Op >> 12) & 63; |
| 43 | Result.Arg2 = (Op >> 6 ) & 63; |
| 44 | Result.Arg3 = (Op >> 0 ) & 63; |
| 45 | break; |
| 46 | case 0: |
| 47 | Buf -= 4; // Hrm, try this again... |
| 48 | if (read_vbr(Buf, EndBuf, Result.Opcode)) return true; |
| 49 | if (read_vbr(Buf, EndBuf, Typ)) return true; |
| 50 | Result.Ty = getType(Typ); |
| 51 | if (read_vbr(Buf, EndBuf, Result.NumOperands)) return true; |
| 52 | |
| 53 | switch (Result.NumOperands) { |
| 54 | case 0: |
| 55 | cerr << "Zero Arg instr found!\n"; |
| 56 | return true; // This encoding is invalid! |
| 57 | case 1: |
| 58 | if (read_vbr(Buf, EndBuf, Result.Arg1)) return true; |
| 59 | break; |
| 60 | case 2: |
| 61 | if (read_vbr(Buf, EndBuf, Result.Arg1) || |
| 62 | read_vbr(Buf, EndBuf, Result.Arg2)) return true; |
| 63 | break; |
| 64 | case 3: |
| 65 | if (read_vbr(Buf, EndBuf, Result.Arg1) || |
| 66 | read_vbr(Buf, EndBuf, Result.Arg2) || |
| 67 | read_vbr(Buf, EndBuf, Result.Arg3)) return true; |
| 68 | break; |
| 69 | default: |
| 70 | if (read_vbr(Buf, EndBuf, Result.Arg1) || |
| 71 | read_vbr(Buf, EndBuf, Result.Arg2)) return true; |
| 72 | |
| 73 | // Allocate a vector to hold arguments 3, 4, 5, 6 ... |
| 74 | Result.VarArgs = new vector<unsigned>(Result.NumOperands-2); |
| 75 | for (unsigned a = 0; a < Result.NumOperands-2; a++) |
| 76 | if (read_vbr(Buf, EndBuf, (*Result.VarArgs)[a])) return true; |
| 77 | break; |
| 78 | } |
| 79 | if (align32(Buf, EndBuf)) return true; |
| 80 | break; |
| 81 | } |
| 82 | |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 83 | #if 0 |
| 84 | cerr << "NO: " << Result.NumOperands << " opcode: " << Result.Opcode |
| 85 | << " Ty: " << Result.Ty->getName() << " arg1: " << Result.Arg1 |
| 86 | << " arg2: " << Result.Arg2 << " arg3: " << Result.Arg3 << endl; |
| 87 | #endif |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 88 | return false; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | bool BytecodeParser::ParseInstruction(const uchar *&Buf, const uchar *EndBuf, |
| 93 | Instruction *&Res) { |
| 94 | RawInst Raw; |
| 95 | if (ParseRawInst(Buf, EndBuf, Raw)) return true;; |
| 96 | |
| 97 | if (Raw.Opcode >= Instruction::FirstUnaryOp && |
| 98 | Raw.Opcode < Instruction::NumUnaryOps && Raw.NumOperands == 1) { |
Chris Lattner | 9f3d276 | 2001-07-07 20:17:23 +0000 | [diff] [blame] | 99 | Res = UnaryOperator::create((Instruction::UnaryOps)Raw.Opcode, |
| 100 | getValue(Raw.Ty,Raw.Arg1)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 101 | return false; |
| 102 | } else if (Raw.Opcode >= Instruction::FirstBinaryOp && |
| 103 | Raw.Opcode < Instruction::NumBinaryOps && Raw.NumOperands == 2) { |
Chris Lattner | 9f3d276 | 2001-07-07 20:17:23 +0000 | [diff] [blame] | 104 | Res = BinaryOperator::create((Instruction::BinaryOps)Raw.Opcode, |
| 105 | getValue(Raw.Ty, Raw.Arg1), |
| 106 | getValue(Raw.Ty, Raw.Arg2)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 107 | return false; |
Chris Lattner | 027dcc5 | 2001-07-08 21:10:27 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | Value *V; |
| 111 | switch (Raw.Opcode) { |
| 112 | case Instruction::Cast: |
Chris Lattner | 71496b3 | 2001-07-08 19:03:27 +0000 | [diff] [blame] | 113 | Res = new CastInst(getValue(Raw.Ty, Raw.Arg1), getType(Raw.Arg2)); |
Chris Lattner | 0908309 | 2001-07-08 04:57:15 +0000 | [diff] [blame] | 114 | return false; |
Chris Lattner | 027dcc5 | 2001-07-08 21:10:27 +0000 | [diff] [blame] | 115 | |
| 116 | case Instruction::PHINode: { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 117 | PHINode *PN = new PHINode(Raw.Ty); |
| 118 | switch (Raw.NumOperands) { |
Chris Lattner | ee976f3 | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 119 | case 0: |
| 120 | case 1: |
| 121 | case 3: cerr << "Invalid phi node encountered!\n"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 122 | delete PN; |
| 123 | return true; |
Chris Lattner | ee976f3 | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 124 | case 2: PN->addIncoming(getValue(Raw.Ty, Raw.Arg1), |
| 125 | (BasicBlock*)getValue(Type::LabelTy, Raw.Arg2)); |
| 126 | break; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 127 | default: |
Chris Lattner | ee976f3 | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 128 | PN->addIncoming(getValue(Raw.Ty, Raw.Arg1), |
| 129 | (BasicBlock*)getValue(Type::LabelTy, Raw.Arg2)); |
| 130 | if (Raw.VarArgs->size() & 1) { |
| 131 | cerr << "PHI Node with ODD number of arguments!\n"; |
| 132 | delete PN; |
| 133 | return true; |
| 134 | } else { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 135 | vector<unsigned> &args = *Raw.VarArgs; |
Chris Lattner | ee976f3 | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 136 | for (unsigned i = 0; i < args.size(); i+=2) |
| 137 | PN->addIncoming(getValue(Raw.Ty, args[i]), |
| 138 | (BasicBlock*)getValue(Type::LabelTy, args[i+1])); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 139 | } |
Chris Lattner | ee976f3 | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 140 | delete Raw.VarArgs; |
| 141 | break; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 142 | } |
| 143 | Res = PN; |
| 144 | return false; |
Chris Lattner | 027dcc5 | 2001-07-08 21:10:27 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | case Instruction::Shl: |
| 148 | case Instruction::Shr: |
| 149 | Res = new ShiftInst((Instruction::OtherOps)Raw.Opcode, |
| 150 | getValue(Raw.Ty, Raw.Arg1), |
| 151 | getValue(Type::UByteTy, Raw.Arg2)); |
| 152 | return false; |
| 153 | case Instruction::Ret: |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 154 | if (Raw.NumOperands == 0) { |
| 155 | Res = new ReturnInst(); return false; |
| 156 | } else if (Raw.NumOperands == 1) { |
| 157 | Res = new ReturnInst(getValue(Raw.Ty, Raw.Arg1)); return false; |
| 158 | } |
Chris Lattner | 027dcc5 | 2001-07-08 21:10:27 +0000 | [diff] [blame] | 159 | break; |
| 160 | |
| 161 | case Instruction::Br: |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 162 | if (Raw.NumOperands == 1) { |
| 163 | Res = new BranchInst((BasicBlock*)getValue(Type::LabelTy, Raw.Arg1)); |
| 164 | return false; |
| 165 | } else if (Raw.NumOperands == 3) { |
| 166 | Res = new BranchInst((BasicBlock*)getValue(Type::LabelTy, Raw.Arg1), |
| 167 | (BasicBlock*)getValue(Type::LabelTy, Raw.Arg2), |
| 168 | getValue(Type::BoolTy , Raw.Arg3)); |
| 169 | return false; |
| 170 | } |
Chris Lattner | 027dcc5 | 2001-07-08 21:10:27 +0000 | [diff] [blame] | 171 | break; |
| 172 | |
| 173 | case Instruction::Switch: { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 174 | SwitchInst *I = |
| 175 | new SwitchInst(getValue(Raw.Ty, Raw.Arg1), |
| 176 | (BasicBlock*)getValue(Type::LabelTy, Raw.Arg2)); |
| 177 | Res = I; |
| 178 | if (Raw.NumOperands < 3) return false; // No destinations? Wierd. |
| 179 | |
| 180 | if (Raw.NumOperands == 3 || Raw.VarArgs->size() & 1) { |
| 181 | cerr << "Switch statement with odd number of arguments!\n"; |
| 182 | delete I; |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | vector<unsigned> &args = *Raw.VarArgs; |
| 187 | for (unsigned i = 0; i < args.size(); i += 2) |
| 188 | I->dest_push_back((ConstPoolVal*)getValue(Raw.Ty, args[i]), |
| 189 | (BasicBlock*)getValue(Type::LabelTy, args[i+1])); |
| 190 | |
| 191 | delete Raw.VarArgs; |
| 192 | return false; |
Chris Lattner | 027dcc5 | 2001-07-08 21:10:27 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | case Instruction::Call: { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 196 | Method *M = (Method*)getValue(Raw.Ty, Raw.Arg1); |
| 197 | if (M == 0) return true; |
| 198 | |
| 199 | const MethodType::ParamTypes &PL = M->getMethodType()->getParamTypes(); |
| 200 | MethodType::ParamTypes::const_iterator It = PL.begin(); |
| 201 | |
| 202 | vector<Value *> Params; |
| 203 | switch (Raw.NumOperands) { |
| 204 | case 0: cerr << "Invalid call instruction encountered!\n"; |
| 205 | return true; |
| 206 | case 1: break; |
| 207 | case 2: Params.push_back(getValue(*It++, Raw.Arg2)); break; |
| 208 | case 3: Params.push_back(getValue(*It++, Raw.Arg2)); |
| 209 | if (It == PL.end()) return true; |
| 210 | Params.push_back(getValue(*It++, Raw.Arg3)); break; |
| 211 | default: |
| 212 | Params.push_back(getValue(*It++, Raw.Arg2)); |
| 213 | { |
| 214 | vector<unsigned> &args = *Raw.VarArgs; |
| 215 | for (unsigned i = 0; i < args.size(); i++) { |
| 216 | if (It == PL.end()) return true; |
| 217 | Params.push_back(getValue(*It++, args[i])); |
| 218 | } |
| 219 | } |
| 220 | delete Raw.VarArgs; |
| 221 | } |
| 222 | if (It != PL.end()) return true; |
| 223 | |
| 224 | Res = new CallInst(M, Params); |
| 225 | return false; |
Chris Lattner | 027dcc5 | 2001-07-08 21:10:27 +0000 | [diff] [blame] | 226 | } |
| 227 | case Instruction::Malloc: |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 228 | if (Raw.NumOperands > 2) return true; |
Chris Lattner | 027dcc5 | 2001-07-08 21:10:27 +0000 | [diff] [blame] | 229 | V = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0; |
| 230 | Res = new MallocInst(Raw.Ty, V); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 231 | return false; |
Chris Lattner | 027dcc5 | 2001-07-08 21:10:27 +0000 | [diff] [blame] | 232 | |
| 233 | case Instruction::Alloca: |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 234 | if (Raw.NumOperands > 2) return true; |
Chris Lattner | 027dcc5 | 2001-07-08 21:10:27 +0000 | [diff] [blame] | 235 | V = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0; |
| 236 | Res = new AllocaInst(Raw.Ty, V); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 237 | return false; |
Chris Lattner | 027dcc5 | 2001-07-08 21:10:27 +0000 | [diff] [blame] | 238 | |
| 239 | case Instruction::Free: |
| 240 | V = getValue(Raw.Ty, Raw.Arg1); |
| 241 | if (!V->getType()->isPointerType()) return true; |
| 242 | Res = new FreeInst(V); |
| 243 | return false; |
| 244 | |
Chris Lattner | ab5ac6b | 2001-07-08 23:22:50 +0000 | [diff] [blame^] | 245 | case Instruction::Load: |
| 246 | case Instruction::GetElementPtr: { |
Chris Lattner | 027dcc5 | 2001-07-08 21:10:27 +0000 | [diff] [blame] | 247 | vector<ConstPoolVal*> Idx; |
| 248 | switch (Raw.NumOperands) { |
| 249 | case 0: cerr << "Invalid load encountered!\n"; return true; |
| 250 | case 1: break; |
| 251 | case 2: V = getValue(Type::UByteTy, Raw.Arg2); |
| 252 | if (!V->isConstant()) return true; |
| 253 | Idx.push_back(V->castConstant()); |
| 254 | break; |
| 255 | case 3: V = getValue(Type::UByteTy, Raw.Arg2); |
| 256 | if (!V->isConstant()) return true; |
| 257 | Idx.push_back(V->castConstant()); |
| 258 | V = getValue(Type::UByteTy, Raw.Arg3); |
| 259 | if (!V->isConstant()) return true; |
| 260 | Idx.push_back(V->castConstant()); |
| 261 | break; |
| 262 | default: |
| 263 | V = getValue(Type::UByteTy, Raw.Arg2); |
| 264 | if (!V->isConstant()) return true; |
| 265 | Idx.push_back(V->castConstant()); |
| 266 | vector<unsigned> &args = *Raw.VarArgs; |
| 267 | for (unsigned i = 0, E = args.size(); i != E; ++i) { |
| 268 | V = getValue(Type::UByteTy, args[i]); |
| 269 | if (!V->isConstant()) return true; |
| 270 | Idx.push_back(V->castConstant()); |
| 271 | } |
| 272 | delete Raw.VarArgs; |
| 273 | break; |
| 274 | } |
Chris Lattner | ab5ac6b | 2001-07-08 23:22:50 +0000 | [diff] [blame^] | 275 | if (Raw.Opcode == Instruction::Load) |
| 276 | Res = new LoadInst(getValue(Raw.Ty, Raw.Arg1), Idx); |
| 277 | else if (Raw.Opcode == Instruction::GetElementPtr) |
| 278 | Res = new GetElementPtrInst(getValue(Raw.Ty, Raw.Arg1), Idx); |
| 279 | else |
| 280 | abort(); |
| 281 | return false; |
| 282 | } |
| 283 | case Instruction::Store: { |
| 284 | vector<ConstPoolVal*> Idx; |
| 285 | switch (Raw.NumOperands) { |
| 286 | case 0: |
| 287 | case 1: cerr << "Invalid store encountered!\n"; return true; |
| 288 | case 2: break; |
| 289 | case 3: V = getValue(Type::UByteTy, Raw.Arg3); |
| 290 | if (!V->isConstant()) return true; |
| 291 | Idx.push_back(V->castConstant()); |
| 292 | break; |
| 293 | default: |
| 294 | vector<unsigned> &args = *Raw.VarArgs; |
| 295 | for (unsigned i = 0, E = args.size(); i != E; ++i) { |
| 296 | V = getValue(Type::UByteTy, args[i]); |
| 297 | if (!V->isConstant()) return true; |
| 298 | Idx.push_back(V->castConstant()); |
| 299 | } |
| 300 | delete Raw.VarArgs; |
| 301 | break; |
| 302 | } |
| 303 | |
| 304 | const Type *ElType = StoreInst::getIndexedType(Raw.Ty, Idx); |
| 305 | if (ElType == 0) return true; |
| 306 | Res = new StoreInst(getValue(ElType, Raw.Arg1), getValue(Raw.Ty, Raw.Arg2), |
| 307 | Idx); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 308 | return false; |
| 309 | } |
Chris Lattner | 027dcc5 | 2001-07-08 21:10:27 +0000 | [diff] [blame] | 310 | } // end switch(Raw.Opcode) |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 311 | |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 312 | cerr << "Unrecognized instruction! " << Raw.Opcode |
| 313 | << " ADDR = 0x" << (void*)Buf << endl; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 314 | return true; |
| 315 | } |