blob: 1b2ada0d493e29823a4fcb32aece45ce376c3a29 [file] [log] [blame]
Chris Lattner05950c32001-10-13 06:47:01 +00001//===- ReadInst.cpp - Code to read an instruction from bytecode -----------===//
Chris Lattner00950542001-06-06 20:29:01 +00002//
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//
Chris Lattner05950c32001-10-13 06:47:01 +00009//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000010
Chris Lattner7061dc52001-12-03 18:02:31 +000011#include "ReaderInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +000012#include "llvm/iTerminators.h"
13#include "llvm/iMemory.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000014#include "llvm/iPHINode.h"
15#include "llvm/iOther.h"
Chris Lattner00950542001-06-06 20:29:01 +000016
Chris Lattner6fcf5032003-10-09 20:45:42 +000017namespace {
18 struct RawInst { // The raw fields out of the bytecode stream...
19 unsigned NumOperands;
20 unsigned Opcode;
21 unsigned Type;
22
23 RawInst(const unsigned char *&Buf, const unsigned char *EndBuf,
24 std::vector<unsigned> &Args);
25 };
26}
Chris Lattner927b1852003-10-09 20:22:47 +000027
28
29
30RawInst::RawInst(const unsigned char *&Buf, const unsigned char *EndBuf,
31 std::vector<unsigned> &Args) {
Chris Lattner00950542001-06-06 20:29:01 +000032 unsigned Op, Typ;
Misha Brukmand554ebf2003-09-23 16:17:50 +000033 if (read(Buf, EndBuf, Op))
34 throw std::string("Error reading from buffer.");
Chris Lattner00950542001-06-06 20:29:01 +000035
Chris Lattner2b9f6002001-10-23 03:21:10 +000036 // bits Instruction format: Common to all formats
37 // --------------------------
38 // 01-00: Opcode type, fixed to 1.
39 // 07-02: Opcode
Chris Lattner927b1852003-10-09 20:22:47 +000040 Opcode = (Op >> 2) & 63;
41 Args.resize((Op >> 0) & 03);
Chris Lattner00950542001-06-06 20:29:01 +000042
Chris Lattner927b1852003-10-09 20:22:47 +000043 switch (Args.size()) {
Chris Lattner00950542001-06-06 20:29:01 +000044 case 1:
Chris Lattner2b9f6002001-10-23 03:21:10 +000045 // bits Instruction format:
46 // --------------------------
47 // 19-08: Resulting type plane
48 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
49 //
Chris Lattner927b1852003-10-09 20:22:47 +000050 Type = (Op >> 8) & 4095;
51 Args[0] = (Op >> 20) & 4095;
52 if (Args[0] == 4095) // Handle special encoding for 0 operands...
53 Args.resize(0);
Chris Lattner00950542001-06-06 20:29:01 +000054 break;
55 case 2:
Chris Lattner2b9f6002001-10-23 03:21:10 +000056 // bits Instruction format:
57 // --------------------------
58 // 15-08: Resulting type plane
59 // 23-16: Operand #1
60 // 31-24: Operand #2
61 //
Chris Lattner927b1852003-10-09 20:22:47 +000062 Type = (Op >> 8) & 255;
63 Args[0] = (Op >> 16) & 255;
64 Args[1] = (Op >> 24) & 255;
Chris Lattner00950542001-06-06 20:29:01 +000065 break;
66 case 3:
Chris Lattner2b9f6002001-10-23 03:21:10 +000067 // bits Instruction format:
68 // --------------------------
69 // 13-08: Resulting type plane
70 // 19-14: Operand #1
71 // 25-20: Operand #2
72 // 31-26: Operand #3
73 //
Chris Lattner927b1852003-10-09 20:22:47 +000074 Type = (Op >> 8) & 63;
75 Args[0] = (Op >> 14) & 63;
76 Args[1] = (Op >> 20) & 63;
77 Args[2] = (Op >> 26) & 63;
Chris Lattner00950542001-06-06 20:29:01 +000078 break;
79 case 0:
80 Buf -= 4; // Hrm, try this again...
Chris Lattner927b1852003-10-09 20:22:47 +000081 if (read_vbr(Buf, EndBuf, Opcode))
Misha Brukmand554ebf2003-09-23 16:17:50 +000082 throw std::string("Error reading from buffer.");
Chris Lattner927b1852003-10-09 20:22:47 +000083 Opcode >>= 2;
84 if (read_vbr(Buf, EndBuf, Type))
Misha Brukmand554ebf2003-09-23 16:17:50 +000085 throw std::string("Error reading from buffer.");
Chris Lattner00950542001-06-06 20:29:01 +000086
Chris Lattner927b1852003-10-09 20:22:47 +000087 unsigned NumOperands;
88 if (read_vbr(Buf, EndBuf, NumOperands))
89 throw std::string("Error reading from buffer.");
90 Args.resize(NumOperands);
91
92 if (NumOperands == 0)
Misha Brukmand554ebf2003-09-23 16:17:50 +000093 throw std::string("Zero-argument instruction found; this is invalid.");
Chris Lattner00950542001-06-06 20:29:01 +000094
Chris Lattner927b1852003-10-09 20:22:47 +000095 for (unsigned i = 0; i != NumOperands; ++i)
96 if (read_vbr(Buf, EndBuf, Args[i]))
97 throw std::string("Error reading from buffer");
98 if (align32(Buf, EndBuf))
Misha Brukmand554ebf2003-09-23 16:17:50 +000099 throw std::string("Unaligned bytecode buffer.");
Chris Lattner00950542001-06-06 20:29:01 +0000100 break;
101 }
Chris Lattner00950542001-06-06 20:29:01 +0000102}
103
104
Chris Lattner3483f542003-10-09 18:25:19 +0000105Instruction *BytecodeParser::ParseInstruction(const unsigned char *&Buf,
Chris Lattner6fcf5032003-10-09 20:45:42 +0000106 const unsigned char *EndBuf,
107 std::vector<unsigned> &Args) {
108 Args.clear();
Chris Lattner927b1852003-10-09 20:22:47 +0000109 RawInst RI(Buf, EndBuf, Args);
110 const Type *InstTy = getType(RI.Type);
Chris Lattner00950542001-06-06 20:29:01 +0000111
Chris Lattner927b1852003-10-09 20:22:47 +0000112 if (RI.Opcode >= Instruction::BinaryOpsBegin &&
113 RI.Opcode < Instruction::BinaryOpsEnd && Args.size() == 2)
114 return BinaryOperator::create((Instruction::BinaryOps)RI.Opcode,
115 getValue(InstTy, Args[0]),
116 getValue(InstTy, Args[1]));
Chris Lattner027dcc52001-07-08 21:10:27 +0000117
Chris Lattner927b1852003-10-09 20:22:47 +0000118 switch (RI.Opcode) {
Chris Lattner8f77dae2003-05-08 02:44:12 +0000119 case Instruction::VarArg:
Chris Lattner927b1852003-10-09 20:22:47 +0000120 return new VarArgInst(getValue(InstTy, Args[0]), getType(Args[1]));
121 case Instruction::Cast:
122 return new CastInst(getValue(InstTy, Args[0]), getType(Args[1]));
Chris Lattner027dcc52001-07-08 21:10:27 +0000123 case Instruction::PHINode: {
Chris Lattner927b1852003-10-09 20:22:47 +0000124 if (Args.size() == 0 || (Args.size() & 1))
Chris Lattner3483f542003-10-09 18:25:19 +0000125 throw std::string("Invalid phi node encountered!\n");
Chris Lattner927b1852003-10-09 20:22:47 +0000126
127 PHINode *PN = new PHINode(InstTy);
128 for (unsigned i = 0, e = Args.size(); i != e; i += 2)
129 PN->addIncoming(getValue(InstTy, Args[i]), getBasicBlock(Args[i+1]));
Chris Lattner3483f542003-10-09 18:25:19 +0000130 return PN;
Chris Lattner027dcc52001-07-08 21:10:27 +0000131 }
132
133 case Instruction::Shl:
134 case Instruction::Shr:
Chris Lattner927b1852003-10-09 20:22:47 +0000135 return new ShiftInst((Instruction::OtherOps)RI.Opcode,
136 getValue(InstTy, Args[0]),
137 getValue(Type::UByteTyID, Args[1]));
Chris Lattner027dcc52001-07-08 21:10:27 +0000138 case Instruction::Ret:
Chris Lattner927b1852003-10-09 20:22:47 +0000139 if (Args.size() == 0)
Chris Lattner3483f542003-10-09 18:25:19 +0000140 return new ReturnInst();
Chris Lattner927b1852003-10-09 20:22:47 +0000141 else if (Args.size() == 1)
142 return new ReturnInst(getValue(InstTy, Args[0]));
Chris Lattner027dcc52001-07-08 21:10:27 +0000143 break;
144
145 case Instruction::Br:
Chris Lattner927b1852003-10-09 20:22:47 +0000146 if (Args.size() == 1)
147 return new BranchInst(getBasicBlock(Args[0]));
148 else if (Args.size() == 3)
149 return new BranchInst(getBasicBlock(Args[0]), getBasicBlock(Args[1]),
150 getValue(Type::BoolTyID , Args[2]));
Chris Lattner3483f542003-10-09 18:25:19 +0000151 throw std::string("Invalid number of operands for a 'br' instruction!");
Chris Lattner027dcc52001-07-08 21:10:27 +0000152
153 case Instruction::Switch: {
Chris Lattner927b1852003-10-09 20:22:47 +0000154 if (Args.size() & 1)
Chris Lattner3483f542003-10-09 18:25:19 +0000155 throw std::string("Switch statement with odd number of arguments!");
Chris Lattner00950542001-06-06 20:29:01 +0000156
Chris Lattner927b1852003-10-09 20:22:47 +0000157 SwitchInst *I = new SwitchInst(getValue(InstTy, Args[0]),
158 getBasicBlock(Args[1]));
159 for (unsigned i = 2, e = Args.size(); i != e; i += 2)
160 I->addCase(cast<Constant>(getValue(InstTy, Args[i])),
161 getBasicBlock(Args[i+1]));
Chris Lattner3483f542003-10-09 18:25:19 +0000162 return I;
Chris Lattner027dcc52001-07-08 21:10:27 +0000163 }
164
165 case Instruction::Call: {
Chris Lattner927b1852003-10-09 20:22:47 +0000166 if (Args.size() == 0)
167 throw std::string("Invalid call instruction encountered!");
168
169 Value *F = getValue(InstTy, Args[0]);
Chris Lattner00950542001-06-06 20:29:01 +0000170
Chris Lattner3483f542003-10-09 18:25:19 +0000171 // Check to make sure we have a pointer to function type
Chris Lattner5bea4112003-09-05 18:25:29 +0000172 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
Chris Lattner3483f542003-10-09 18:25:19 +0000173 if (PTy == 0) throw std::string("Call to non function pointer value!");
Chris Lattner5bea4112003-09-05 18:25:29 +0000174 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
Chris Lattner3483f542003-10-09 18:25:19 +0000175 if (FTy == 0) throw std::string("Call to non function pointer value!");
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000176
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000177 std::vector<Value *> Params;
Chris Lattner5bea4112003-09-05 18:25:29 +0000178 const FunctionType::ParamTypes &PL = FTy->getParamTypes();
Chris Lattner05950c32001-10-13 06:47:01 +0000179
Chris Lattner5bea4112003-09-05 18:25:29 +0000180 if (!FTy->isVarArg()) {
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000181 FunctionType::ParamTypes::const_iterator It = PL.begin();
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000182
Chris Lattner927b1852003-10-09 20:22:47 +0000183 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
184 if (It == PL.end()) throw std::string("Invalid call instruction!");
185 Params.push_back(getValue(*It++, Args[i]));
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000186 }
Chris Lattner3483f542003-10-09 18:25:19 +0000187 if (It != PL.end()) throw std::string("Invalid call instruction!");
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000188 } else {
Chris Lattner927b1852003-10-09 20:22:47 +0000189 // FIXME: Args[1] is currently just a dummy padding field!
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000190
Chris Lattner927b1852003-10-09 20:22:47 +0000191 if (Args.size() & 1) // Must be pairs of type/value
192 throw std::string("Invalid call instruction!");
193
194 for (unsigned i = 2, e = Args.size(); i != e; i += 2)
195 Params.push_back(getValue(getType(Args[i]), Args[i+1]));
Chris Lattner00950542001-06-06 20:29:01 +0000196 }
Chris Lattner00950542001-06-06 20:29:01 +0000197
Chris Lattner3483f542003-10-09 18:25:19 +0000198 return new CallInst(F, Params);
Chris Lattner027dcc52001-07-08 21:10:27 +0000199 }
Chris Lattner05950c32001-10-13 06:47:01 +0000200 case Instruction::Invoke: {
Chris Lattner927b1852003-10-09 20:22:47 +0000201 if (Args.size() < 3) throw std::string("Invalid invoke instruction!");
202 Value *F = getValue(InstTy, Args[0]);
Chris Lattner05950c32001-10-13 06:47:01 +0000203
Chris Lattner3483f542003-10-09 18:25:19 +0000204 // Check to make sure we have a pointer to function type
Chris Lattner5bea4112003-09-05 18:25:29 +0000205 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
Chris Lattner3483f542003-10-09 18:25:19 +0000206 if (PTy == 0) throw std::string("Invoke to non function pointer value!");
Chris Lattner5bea4112003-09-05 18:25:29 +0000207 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
Chris Lattner3483f542003-10-09 18:25:19 +0000208 if (FTy == 0) throw std::string("Invoke to non function pointer value!");
Chris Lattner05950c32001-10-13 06:47:01 +0000209
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000210 std::vector<Value *> Params;
Chris Lattner05950c32001-10-13 06:47:01 +0000211 BasicBlock *Normal, *Except;
212
Chris Lattner927b1852003-10-09 20:22:47 +0000213 const FunctionType::ParamTypes &PL = FTy->getParamTypes();
214
Chris Lattner5bea4112003-09-05 18:25:29 +0000215 if (!FTy->isVarArg()) {
Chris Lattner927b1852003-10-09 20:22:47 +0000216 Normal = getBasicBlock(Args[1]);
217 Except = getBasicBlock(Args[2]);
Chris Lattner05950c32001-10-13 06:47:01 +0000218
Chris Lattner927b1852003-10-09 20:22:47 +0000219 FunctionType::ParamTypes::const_iterator It = PL.begin();
220 for (unsigned i = 3, e = Args.size(); i != e; ++i) {
221 if (It == PL.end()) throw std::string("Invalid invoke instruction!");
222 Params.push_back(getValue(*It++, Args[i]));
Chris Lattner05950c32001-10-13 06:47:01 +0000223 }
Chris Lattner927b1852003-10-09 20:22:47 +0000224 if (It != PL.end()) throw std::string("Invalid invoke instruction!");
Chris Lattner05950c32001-10-13 06:47:01 +0000225 } else {
Chris Lattner927b1852003-10-09 20:22:47 +0000226 // FIXME: Args[1] is a dummy padding field
227
228 if (Args.size() < 6) throw std::string("Invalid invoke instruction!");
229 if (Args[2] != Type::LabelTyID || Args[4] != Type::LabelTyID)
Chris Lattner3483f542003-10-09 18:25:19 +0000230 throw std::string("Invalid invoke instruction!");
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000231
Chris Lattner927b1852003-10-09 20:22:47 +0000232 Normal = getBasicBlock(Args[3]);
233 Except = getBasicBlock(Args[5]);
Chris Lattner05950c32001-10-13 06:47:01 +0000234
Chris Lattner927b1852003-10-09 20:22:47 +0000235 if (Args.size() & 1) // Must be pairs of type/value
Chris Lattner3483f542003-10-09 18:25:19 +0000236 throw std::string("Invalid invoke instruction!");
237
Chris Lattner927b1852003-10-09 20:22:47 +0000238 for (unsigned i = 6; i < Args.size(); i += 2)
239 Params.push_back(getValue(Args[i], Args[i+1]));
Chris Lattner05950c32001-10-13 06:47:01 +0000240 }
241
Chris Lattner3483f542003-10-09 18:25:19 +0000242 return new InvokeInst(F, Normal, Except, Params);
Chris Lattner05950c32001-10-13 06:47:01 +0000243 }
Chris Lattner027dcc52001-07-08 21:10:27 +0000244 case Instruction::Malloc:
Chris Lattner927b1852003-10-09 20:22:47 +0000245 if (Args.size() > 2) throw std::string("Invalid malloc instruction!");
246 if (!isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000247 throw std::string("Invalid malloc instruction!");
248
Chris Lattner927b1852003-10-09 20:22:47 +0000249 return new MallocInst(cast<PointerType>(InstTy)->getElementType(),
250 Args.size() ? getValue(Type::UIntTyID,
251 Args[0]) : 0);
Chris Lattner027dcc52001-07-08 21:10:27 +0000252
253 case Instruction::Alloca:
Chris Lattner927b1852003-10-09 20:22:47 +0000254 if (Args.size() > 2) throw std::string("Invalid alloca instruction!");
255 if (!isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000256 throw std::string("Invalid alloca instruction!");
Chris Lattner027dcc52001-07-08 21:10:27 +0000257
Chris Lattner927b1852003-10-09 20:22:47 +0000258 return new AllocaInst(cast<PointerType>(InstTy)->getElementType(),
259 Args.size() ? getValue(Type::UIntTyID,
260 Args[0]) : 0);
Chris Lattner027dcc52001-07-08 21:10:27 +0000261 case Instruction::Free:
Chris Lattner927b1852003-10-09 20:22:47 +0000262 if (!isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000263 throw std::string("Invalid free instruction!");
Chris Lattner927b1852003-10-09 20:22:47 +0000264 return new FreeInst(getValue(InstTy, Args[0]));
Chris Lattner027dcc52001-07-08 21:10:27 +0000265
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000266 case Instruction::GetElementPtr: {
Chris Lattner927b1852003-10-09 20:22:47 +0000267 if (Args.size() == 0 || !isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000268 throw std::string("Invalid getelementptr instruction!");
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000269
Chris Lattner927b1852003-10-09 20:22:47 +0000270 std::vector<Value*> Idx;
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000271
Chris Lattner927b1852003-10-09 20:22:47 +0000272 const Type *NextTy = InstTy;
273 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
274 const CompositeType *TopTy = dyn_cast_or_null<CompositeType>(NextTy);
275 if (!TopTy) throw std::string("Invalid getelementptr instruction!");
276 Idx.push_back(getValue(TopTy->getIndexType(), Args[i]));
277 NextTy = GetElementPtrInst::getIndexedType(InstTy, Idx, true);
Chris Lattner027dcc52001-07-08 21:10:27 +0000278 }
Chris Lattner77a316a2001-11-12 21:48:38 +0000279
Chris Lattner927b1852003-10-09 20:22:47 +0000280 return new GetElementPtrInst(getValue(InstTy, Args[0]), Idx);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000281 }
Chris Lattner09bd0252003-09-08 18:04:16 +0000282
Chris Lattnerdba2b222003-09-08 18:20:14 +0000283 case 62: // volatile load
Chris Lattner09bd0252003-09-08 18:04:16 +0000284 case Instruction::Load:
Chris Lattner927b1852003-10-09 20:22:47 +0000285 if (Args.size() != 1 || !isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000286 throw std::string("Invalid load instruction!");
Chris Lattner927b1852003-10-09 20:22:47 +0000287 return new LoadInst(getValue(InstTy, Args[0]), "", RI.Opcode == 62);
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000288
Chris Lattnerdba2b222003-09-08 18:20:14 +0000289 case 63: // volatile store
Chris Lattner09bd0252003-09-08 18:04:16 +0000290 case Instruction::Store: {
Chris Lattner927b1852003-10-09 20:22:47 +0000291 if (!isa<PointerType>(InstTy) || Args.size() != 2)
Chris Lattner3483f542003-10-09 18:25:19 +0000292 throw std::string("Invalid store instruction!");
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000293
Chris Lattner927b1852003-10-09 20:22:47 +0000294 Value *Ptr = getValue(InstTy, Args[1]);
Chris Lattner352eef72002-08-21 22:55:27 +0000295 const Type *ValTy = cast<PointerType>(Ptr->getType())->getElementType();
Chris Lattner927b1852003-10-09 20:22:47 +0000296 return new StoreInst(getValue(ValTy, Args[0]), Ptr, RI.Opcode == 63);
Chris Lattner00950542001-06-06 20:29:01 +0000297 }
Chris Lattner36143fc2003-09-08 18:54:55 +0000298 case Instruction::Unwind:
Chris Lattner927b1852003-10-09 20:22:47 +0000299 if (Args.size() != 0) throw std::string("Invalid unwind instruction!");
Chris Lattner3483f542003-10-09 18:25:19 +0000300 return new UnwindInst();
Chris Lattner927b1852003-10-09 20:22:47 +0000301 } // end switch(RI.Opcode)
Chris Lattner00950542001-06-06 20:29:01 +0000302
Chris Lattner927b1852003-10-09 20:22:47 +0000303 std::cerr << "Unrecognized instruction! " << RI.Opcode
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000304 << " ADDR = 0x" << (void*)Buf << "\n";
Chris Lattner3483f542003-10-09 18:25:19 +0000305 throw std::string("Unrecognized instruction!");
Chris Lattner00950542001-06-06 20:29:01 +0000306}