blob: b415025a578b724ec9aaaeb9e855db62c4ff4384 [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,
Chris Lattner35d2ca62003-10-09 22:39:30 +0000115 getValue(RI.Type, Args[0]),
116 getValue(RI.Type, 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 Lattner35d2ca62003-10-09 22:39:30 +0000120 return new VarArgInst(getValue(RI.Type, Args[0]), getType(Args[1]));
Chris Lattner927b1852003-10-09 20:22:47 +0000121 case Instruction::Cast:
Chris Lattner35d2ca62003-10-09 22:39:30 +0000122 return new CastInst(getValue(RI.Type, 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);
Chris Lattner4c523922003-10-09 22:46:58 +0000128 PN->op_reserve(Args.size());
Chris Lattner927b1852003-10-09 20:22:47 +0000129 for (unsigned i = 0, e = Args.size(); i != e; i += 2)
Chris Lattner35d2ca62003-10-09 22:39:30 +0000130 PN->addIncoming(getValue(RI.Type, Args[i]), getBasicBlock(Args[i+1]));
Chris Lattner3483f542003-10-09 18:25:19 +0000131 return PN;
Chris Lattner027dcc52001-07-08 21:10:27 +0000132 }
133
134 case Instruction::Shl:
135 case Instruction::Shr:
Chris Lattner927b1852003-10-09 20:22:47 +0000136 return new ShiftInst((Instruction::OtherOps)RI.Opcode,
Chris Lattner35d2ca62003-10-09 22:39:30 +0000137 getValue(RI.Type, Args[0]),
Chris Lattner927b1852003-10-09 20:22:47 +0000138 getValue(Type::UByteTyID, Args[1]));
Chris Lattner027dcc52001-07-08 21:10:27 +0000139 case Instruction::Ret:
Chris Lattner927b1852003-10-09 20:22:47 +0000140 if (Args.size() == 0)
Chris Lattner3483f542003-10-09 18:25:19 +0000141 return new ReturnInst();
Chris Lattner927b1852003-10-09 20:22:47 +0000142 else if (Args.size() == 1)
Chris Lattner35d2ca62003-10-09 22:39:30 +0000143 return new ReturnInst(getValue(RI.Type, Args[0]));
Chris Lattner027dcc52001-07-08 21:10:27 +0000144 break;
145
146 case Instruction::Br:
Chris Lattner927b1852003-10-09 20:22:47 +0000147 if (Args.size() == 1)
148 return new BranchInst(getBasicBlock(Args[0]));
149 else if (Args.size() == 3)
150 return new BranchInst(getBasicBlock(Args[0]), getBasicBlock(Args[1]),
151 getValue(Type::BoolTyID , Args[2]));
Chris Lattner3483f542003-10-09 18:25:19 +0000152 throw std::string("Invalid number of operands for a 'br' instruction!");
Chris Lattner027dcc52001-07-08 21:10:27 +0000153
154 case Instruction::Switch: {
Chris Lattner927b1852003-10-09 20:22:47 +0000155 if (Args.size() & 1)
Chris Lattner3483f542003-10-09 18:25:19 +0000156 throw std::string("Switch statement with odd number of arguments!");
Chris Lattner00950542001-06-06 20:29:01 +0000157
Chris Lattner35d2ca62003-10-09 22:39:30 +0000158 SwitchInst *I = new SwitchInst(getValue(RI.Type, Args[0]),
Chris Lattner927b1852003-10-09 20:22:47 +0000159 getBasicBlock(Args[1]));
160 for (unsigned i = 2, e = Args.size(); i != e; i += 2)
Chris Lattner35d2ca62003-10-09 22:39:30 +0000161 I->addCase(cast<Constant>(getValue(RI.Type, Args[i])),
Chris Lattner927b1852003-10-09 20:22:47 +0000162 getBasicBlock(Args[i+1]));
Chris Lattner3483f542003-10-09 18:25:19 +0000163 return I;
Chris Lattner027dcc52001-07-08 21:10:27 +0000164 }
165
166 case Instruction::Call: {
Chris Lattner927b1852003-10-09 20:22:47 +0000167 if (Args.size() == 0)
168 throw std::string("Invalid call instruction encountered!");
169
Chris Lattner35d2ca62003-10-09 22:39:30 +0000170 Value *F = getValue(RI.Type, Args[0]);
Chris Lattner00950542001-06-06 20:29:01 +0000171
Chris Lattner3483f542003-10-09 18:25:19 +0000172 // Check to make sure we have a pointer to function type
Chris Lattner5bea4112003-09-05 18:25:29 +0000173 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
Chris Lattner3483f542003-10-09 18:25:19 +0000174 if (PTy == 0) throw std::string("Call to non function pointer value!");
Chris Lattner5bea4112003-09-05 18:25:29 +0000175 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
Chris Lattner3483f542003-10-09 18:25:19 +0000176 if (FTy == 0) throw std::string("Call to non function pointer value!");
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000177
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000178 std::vector<Value *> Params;
Chris Lattner5bea4112003-09-05 18:25:29 +0000179 const FunctionType::ParamTypes &PL = FTy->getParamTypes();
Chris Lattner05950c32001-10-13 06:47:01 +0000180
Chris Lattner5bea4112003-09-05 18:25:29 +0000181 if (!FTy->isVarArg()) {
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000182 FunctionType::ParamTypes::const_iterator It = PL.begin();
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000183
Chris Lattner927b1852003-10-09 20:22:47 +0000184 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
185 if (It == PL.end()) throw std::string("Invalid call instruction!");
186 Params.push_back(getValue(*It++, Args[i]));
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000187 }
Chris Lattner3483f542003-10-09 18:25:19 +0000188 if (It != PL.end()) throw std::string("Invalid call instruction!");
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000189 } else {
Chris Lattner927b1852003-10-09 20:22:47 +0000190 // FIXME: Args[1] is currently just a dummy padding field!
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000191
Chris Lattner927b1852003-10-09 20:22:47 +0000192 if (Args.size() & 1) // Must be pairs of type/value
193 throw std::string("Invalid call instruction!");
194
195 for (unsigned i = 2, e = Args.size(); i != e; i += 2)
Chris Lattner35d2ca62003-10-09 22:39:30 +0000196 Params.push_back(getValue(Args[i], Args[i+1]));
Chris Lattner00950542001-06-06 20:29:01 +0000197 }
Chris Lattner00950542001-06-06 20:29:01 +0000198
Chris Lattner3483f542003-10-09 18:25:19 +0000199 return new CallInst(F, Params);
Chris Lattner027dcc52001-07-08 21:10:27 +0000200 }
Chris Lattner05950c32001-10-13 06:47:01 +0000201 case Instruction::Invoke: {
Chris Lattner927b1852003-10-09 20:22:47 +0000202 if (Args.size() < 3) throw std::string("Invalid invoke instruction!");
Chris Lattner35d2ca62003-10-09 22:39:30 +0000203 Value *F = getValue(RI.Type, Args[0]);
Chris Lattner05950c32001-10-13 06:47:01 +0000204
Chris Lattner3483f542003-10-09 18:25:19 +0000205 // Check to make sure we have a pointer to function type
Chris Lattner5bea4112003-09-05 18:25:29 +0000206 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
Chris Lattner3483f542003-10-09 18:25:19 +0000207 if (PTy == 0) throw std::string("Invoke to non function pointer value!");
Chris Lattner5bea4112003-09-05 18:25:29 +0000208 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
Chris Lattner3483f542003-10-09 18:25:19 +0000209 if (FTy == 0) throw std::string("Invoke to non function pointer value!");
Chris Lattner05950c32001-10-13 06:47:01 +0000210
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000211 std::vector<Value *> Params;
Chris Lattner05950c32001-10-13 06:47:01 +0000212 BasicBlock *Normal, *Except;
213
Chris Lattner927b1852003-10-09 20:22:47 +0000214 const FunctionType::ParamTypes &PL = FTy->getParamTypes();
215
Chris Lattner5bea4112003-09-05 18:25:29 +0000216 if (!FTy->isVarArg()) {
Chris Lattner927b1852003-10-09 20:22:47 +0000217 Normal = getBasicBlock(Args[1]);
218 Except = getBasicBlock(Args[2]);
Chris Lattner05950c32001-10-13 06:47:01 +0000219
Chris Lattner927b1852003-10-09 20:22:47 +0000220 FunctionType::ParamTypes::const_iterator It = PL.begin();
221 for (unsigned i = 3, e = Args.size(); i != e; ++i) {
222 if (It == PL.end()) throw std::string("Invalid invoke instruction!");
223 Params.push_back(getValue(*It++, Args[i]));
Chris Lattner05950c32001-10-13 06:47:01 +0000224 }
Chris Lattner927b1852003-10-09 20:22:47 +0000225 if (It != PL.end()) throw std::string("Invalid invoke instruction!");
Chris Lattner05950c32001-10-13 06:47:01 +0000226 } else {
Chris Lattner927b1852003-10-09 20:22:47 +0000227 // FIXME: Args[1] is a dummy padding field
228
229 if (Args.size() < 6) throw std::string("Invalid invoke instruction!");
230 if (Args[2] != Type::LabelTyID || Args[4] != Type::LabelTyID)
Chris Lattner3483f542003-10-09 18:25:19 +0000231 throw std::string("Invalid invoke instruction!");
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000232
Chris Lattner927b1852003-10-09 20:22:47 +0000233 Normal = getBasicBlock(Args[3]);
234 Except = getBasicBlock(Args[5]);
Chris Lattner05950c32001-10-13 06:47:01 +0000235
Chris Lattner927b1852003-10-09 20:22:47 +0000236 if (Args.size() & 1) // Must be pairs of type/value
Chris Lattner3483f542003-10-09 18:25:19 +0000237 throw std::string("Invalid invoke instruction!");
238
Chris Lattner927b1852003-10-09 20:22:47 +0000239 for (unsigned i = 6; i < Args.size(); i += 2)
240 Params.push_back(getValue(Args[i], Args[i+1]));
Chris Lattner05950c32001-10-13 06:47:01 +0000241 }
242
Chris Lattner3483f542003-10-09 18:25:19 +0000243 return new InvokeInst(F, Normal, Except, Params);
Chris Lattner05950c32001-10-13 06:47:01 +0000244 }
Chris Lattner027dcc52001-07-08 21:10:27 +0000245 case Instruction::Malloc:
Chris Lattner927b1852003-10-09 20:22:47 +0000246 if (Args.size() > 2) throw std::string("Invalid malloc instruction!");
247 if (!isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000248 throw std::string("Invalid malloc instruction!");
249
Chris Lattner927b1852003-10-09 20:22:47 +0000250 return new MallocInst(cast<PointerType>(InstTy)->getElementType(),
251 Args.size() ? getValue(Type::UIntTyID,
252 Args[0]) : 0);
Chris Lattner027dcc52001-07-08 21:10:27 +0000253
254 case Instruction::Alloca:
Chris Lattner927b1852003-10-09 20:22:47 +0000255 if (Args.size() > 2) throw std::string("Invalid alloca instruction!");
256 if (!isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000257 throw std::string("Invalid alloca instruction!");
Chris Lattner027dcc52001-07-08 21:10:27 +0000258
Chris Lattner927b1852003-10-09 20:22:47 +0000259 return new AllocaInst(cast<PointerType>(InstTy)->getElementType(),
260 Args.size() ? getValue(Type::UIntTyID,
261 Args[0]) : 0);
Chris Lattner027dcc52001-07-08 21:10:27 +0000262 case Instruction::Free:
Chris Lattner927b1852003-10-09 20:22:47 +0000263 if (!isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000264 throw std::string("Invalid free instruction!");
Chris Lattner35d2ca62003-10-09 22:39:30 +0000265 return new FreeInst(getValue(RI.Type, Args[0]));
Chris Lattner027dcc52001-07-08 21:10:27 +0000266
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000267 case Instruction::GetElementPtr: {
Chris Lattner927b1852003-10-09 20:22:47 +0000268 if (Args.size() == 0 || !isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000269 throw std::string("Invalid getelementptr instruction!");
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000270
Chris Lattner927b1852003-10-09 20:22:47 +0000271 std::vector<Value*> Idx;
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000272
Chris Lattner927b1852003-10-09 20:22:47 +0000273 const Type *NextTy = InstTy;
274 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
275 const CompositeType *TopTy = dyn_cast_or_null<CompositeType>(NextTy);
276 if (!TopTy) throw std::string("Invalid getelementptr instruction!");
277 Idx.push_back(getValue(TopTy->getIndexType(), Args[i]));
278 NextTy = GetElementPtrInst::getIndexedType(InstTy, Idx, true);
Chris Lattner027dcc52001-07-08 21:10:27 +0000279 }
Chris Lattner77a316a2001-11-12 21:48:38 +0000280
Chris Lattner35d2ca62003-10-09 22:39:30 +0000281 return new GetElementPtrInst(getValue(RI.Type, Args[0]), Idx);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000282 }
Chris Lattner09bd0252003-09-08 18:04:16 +0000283
Chris Lattnerdba2b222003-09-08 18:20:14 +0000284 case 62: // volatile load
Chris Lattner09bd0252003-09-08 18:04:16 +0000285 case Instruction::Load:
Chris Lattner927b1852003-10-09 20:22:47 +0000286 if (Args.size() != 1 || !isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000287 throw std::string("Invalid load instruction!");
Chris Lattner35d2ca62003-10-09 22:39:30 +0000288 return new LoadInst(getValue(RI.Type, Args[0]), "", RI.Opcode == 62);
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000289
Chris Lattnerdba2b222003-09-08 18:20:14 +0000290 case 63: // volatile store
Chris Lattner09bd0252003-09-08 18:04:16 +0000291 case Instruction::Store: {
Chris Lattner927b1852003-10-09 20:22:47 +0000292 if (!isa<PointerType>(InstTy) || Args.size() != 2)
Chris Lattner3483f542003-10-09 18:25:19 +0000293 throw std::string("Invalid store instruction!");
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000294
Chris Lattner35d2ca62003-10-09 22:39:30 +0000295 Value *Ptr = getValue(RI.Type, Args[1]);
Chris Lattner352eef72002-08-21 22:55:27 +0000296 const Type *ValTy = cast<PointerType>(Ptr->getType())->getElementType();
Chris Lattner927b1852003-10-09 20:22:47 +0000297 return new StoreInst(getValue(ValTy, Args[0]), Ptr, RI.Opcode == 63);
Chris Lattner00950542001-06-06 20:29:01 +0000298 }
Chris Lattner36143fc2003-09-08 18:54:55 +0000299 case Instruction::Unwind:
Chris Lattner927b1852003-10-09 20:22:47 +0000300 if (Args.size() != 0) throw std::string("Invalid unwind instruction!");
Chris Lattner3483f542003-10-09 18:25:19 +0000301 return new UnwindInst();
Chris Lattner927b1852003-10-09 20:22:47 +0000302 } // end switch(RI.Opcode)
Chris Lattner00950542001-06-06 20:29:01 +0000303
Chris Lattner927b1852003-10-09 20:22:47 +0000304 std::cerr << "Unrecognized instruction! " << RI.Opcode
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000305 << " ADDR = 0x" << (void*)Buf << "\n";
Chris Lattner3483f542003-10-09 18:25:19 +0000306 throw std::string("Unrecognized instruction!");
Chris Lattner00950542001-06-06 20:29:01 +0000307}