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 | bebd60d | 2001-06-25 07:31:31 +0000 | [diff] [blame] | 99 | Res = UnaryOperator::create(Raw.Opcode,getValue(Raw.Ty,Raw.Arg1)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 100 | return false; |
| 101 | } else if (Raw.Opcode >= Instruction::FirstBinaryOp && |
| 102 | Raw.Opcode < Instruction::NumBinaryOps && Raw.NumOperands == 2) { |
Chris Lattner | bebd60d | 2001-06-25 07:31:31 +0000 | [diff] [blame] | 103 | Res = BinaryOperator::create(Raw.Opcode, getValue(Raw.Ty, Raw.Arg1), |
| 104 | getValue(Raw.Ty, Raw.Arg2)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 105 | return false; |
| 106 | } else if (Raw.Opcode == Instruction::PHINode) { |
| 107 | PHINode *PN = new PHINode(Raw.Ty); |
| 108 | switch (Raw.NumOperands) { |
Chris Lattner | ee976f3 | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 109 | case 0: |
| 110 | case 1: |
| 111 | case 3: cerr << "Invalid phi node encountered!\n"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 112 | delete PN; |
| 113 | return true; |
Chris Lattner | ee976f3 | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 114 | case 2: PN->addIncoming(getValue(Raw.Ty, Raw.Arg1), |
| 115 | (BasicBlock*)getValue(Type::LabelTy, Raw.Arg2)); |
| 116 | break; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 117 | default: |
Chris Lattner | ee976f3 | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 118 | PN->addIncoming(getValue(Raw.Ty, Raw.Arg1), |
| 119 | (BasicBlock*)getValue(Type::LabelTy, Raw.Arg2)); |
| 120 | if (Raw.VarArgs->size() & 1) { |
| 121 | cerr << "PHI Node with ODD number of arguments!\n"; |
| 122 | delete PN; |
| 123 | return true; |
| 124 | } else { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 125 | vector<unsigned> &args = *Raw.VarArgs; |
Chris Lattner | ee976f3 | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 126 | for (unsigned i = 0; i < args.size(); i+=2) |
| 127 | PN->addIncoming(getValue(Raw.Ty, args[i]), |
| 128 | (BasicBlock*)getValue(Type::LabelTy, args[i+1])); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 129 | } |
Chris Lattner | ee976f3 | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 130 | delete Raw.VarArgs; |
| 131 | break; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 132 | } |
| 133 | Res = PN; |
| 134 | return false; |
| 135 | } else if (Raw.Opcode == Instruction::Ret) { |
| 136 | if (Raw.NumOperands == 0) { |
| 137 | Res = new ReturnInst(); return false; |
| 138 | } else if (Raw.NumOperands == 1) { |
| 139 | Res = new ReturnInst(getValue(Raw.Ty, Raw.Arg1)); return false; |
| 140 | } |
| 141 | } else if (Raw.Opcode == Instruction::Br) { |
| 142 | if (Raw.NumOperands == 1) { |
| 143 | Res = new BranchInst((BasicBlock*)getValue(Type::LabelTy, Raw.Arg1)); |
| 144 | return false; |
| 145 | } else if (Raw.NumOperands == 3) { |
| 146 | Res = new BranchInst((BasicBlock*)getValue(Type::LabelTy, Raw.Arg1), |
| 147 | (BasicBlock*)getValue(Type::LabelTy, Raw.Arg2), |
| 148 | getValue(Type::BoolTy , Raw.Arg3)); |
| 149 | return false; |
| 150 | } |
| 151 | } else if (Raw.Opcode == Instruction::Switch) { |
| 152 | SwitchInst *I = |
| 153 | new SwitchInst(getValue(Raw.Ty, Raw.Arg1), |
| 154 | (BasicBlock*)getValue(Type::LabelTy, Raw.Arg2)); |
| 155 | Res = I; |
| 156 | if (Raw.NumOperands < 3) return false; // No destinations? Wierd. |
| 157 | |
| 158 | if (Raw.NumOperands == 3 || Raw.VarArgs->size() & 1) { |
| 159 | cerr << "Switch statement with odd number of arguments!\n"; |
| 160 | delete I; |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | vector<unsigned> &args = *Raw.VarArgs; |
| 165 | for (unsigned i = 0; i < args.size(); i += 2) |
| 166 | I->dest_push_back((ConstPoolVal*)getValue(Raw.Ty, args[i]), |
| 167 | (BasicBlock*)getValue(Type::LabelTy, args[i+1])); |
| 168 | |
| 169 | delete Raw.VarArgs; |
| 170 | return false; |
| 171 | } else if (Raw.Opcode == Instruction::Call) { |
| 172 | Method *M = (Method*)getValue(Raw.Ty, Raw.Arg1); |
| 173 | if (M == 0) return true; |
| 174 | |
| 175 | const MethodType::ParamTypes &PL = M->getMethodType()->getParamTypes(); |
| 176 | MethodType::ParamTypes::const_iterator It = PL.begin(); |
| 177 | |
| 178 | vector<Value *> Params; |
| 179 | switch (Raw.NumOperands) { |
| 180 | case 0: cerr << "Invalid call instruction encountered!\n"; |
| 181 | return true; |
| 182 | case 1: break; |
| 183 | case 2: Params.push_back(getValue(*It++, Raw.Arg2)); break; |
| 184 | case 3: Params.push_back(getValue(*It++, Raw.Arg2)); |
| 185 | if (It == PL.end()) return true; |
| 186 | Params.push_back(getValue(*It++, Raw.Arg3)); break; |
| 187 | default: |
| 188 | Params.push_back(getValue(*It++, Raw.Arg2)); |
| 189 | { |
| 190 | vector<unsigned> &args = *Raw.VarArgs; |
| 191 | for (unsigned i = 0; i < args.size(); i++) { |
| 192 | if (It == PL.end()) return true; |
| 193 | Params.push_back(getValue(*It++, args[i])); |
| 194 | } |
| 195 | } |
| 196 | delete Raw.VarArgs; |
| 197 | } |
| 198 | if (It != PL.end()) return true; |
| 199 | |
| 200 | Res = new CallInst(M, Params); |
| 201 | return false; |
| 202 | } else if (Raw.Opcode == Instruction::Malloc) { |
| 203 | if (Raw.NumOperands > 2) return true; |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame^] | 204 | Value *Sz = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0; |
| 205 | Res = new MallocInst(Raw.Ty, Sz); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 206 | return false; |
| 207 | } else if (Raw.Opcode == Instruction::Alloca) { |
| 208 | if (Raw.NumOperands > 2) return true; |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame^] | 209 | Value *Sz = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0; |
| 210 | Res = new AllocaInst(Raw.Ty, Sz); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 211 | return false; |
| 212 | } else if (Raw.Opcode == Instruction::Free) { |
| 213 | Value *Val = getValue(Raw.Ty, Raw.Arg1); |
| 214 | if (!Val->getType()->isPointerType()) return true; |
| 215 | Res = new FreeInst(Val); |
| 216 | return false; |
| 217 | } |
| 218 | |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame^] | 219 | cerr << "Unrecognized instruction! " << Raw.Opcode |
| 220 | << " ADDR = 0x" << (void*)Buf << endl; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 221 | return true; |
| 222 | } |