blob: b085581c60eae6b0df1d99aaab0fb63eef8b91a4 [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 Lattner927b1852003-10-09 20:22:47 +000017struct RawInst { // The raw fields out of the bytecode stream...
18 unsigned NumOperands;
19 unsigned Opcode;
20 unsigned Type;
21
22 RawInst(const unsigned char *&Buf, const unsigned char *EndBuf,
23 std::vector<unsigned> &Args);
24
25};
26
27
28
29RawInst::RawInst(const unsigned char *&Buf, const unsigned char *EndBuf,
30 std::vector<unsigned> &Args) {
Chris Lattner00950542001-06-06 20:29:01 +000031 unsigned Op, Typ;
Misha Brukmand554ebf2003-09-23 16:17:50 +000032 if (read(Buf, EndBuf, Op))
33 throw std::string("Error reading from buffer.");
Chris Lattner00950542001-06-06 20:29:01 +000034
Chris Lattner2b9f6002001-10-23 03:21:10 +000035 // bits Instruction format: Common to all formats
36 // --------------------------
37 // 01-00: Opcode type, fixed to 1.
38 // 07-02: Opcode
Chris Lattner927b1852003-10-09 20:22:47 +000039 Opcode = (Op >> 2) & 63;
40 Args.resize((Op >> 0) & 03);
Chris Lattner00950542001-06-06 20:29:01 +000041
Chris Lattner927b1852003-10-09 20:22:47 +000042 switch (Args.size()) {
Chris Lattner00950542001-06-06 20:29:01 +000043 case 1:
Chris Lattner2b9f6002001-10-23 03:21:10 +000044 // bits Instruction format:
45 // --------------------------
46 // 19-08: Resulting type plane
47 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
48 //
Chris Lattner927b1852003-10-09 20:22:47 +000049 Type = (Op >> 8) & 4095;
50 Args[0] = (Op >> 20) & 4095;
51 if (Args[0] == 4095) // Handle special encoding for 0 operands...
52 Args.resize(0);
Chris Lattner00950542001-06-06 20:29:01 +000053 break;
54 case 2:
Chris Lattner2b9f6002001-10-23 03:21:10 +000055 // bits Instruction format:
56 // --------------------------
57 // 15-08: Resulting type plane
58 // 23-16: Operand #1
59 // 31-24: Operand #2
60 //
Chris Lattner927b1852003-10-09 20:22:47 +000061 Type = (Op >> 8) & 255;
62 Args[0] = (Op >> 16) & 255;
63 Args[1] = (Op >> 24) & 255;
Chris Lattner00950542001-06-06 20:29:01 +000064 break;
65 case 3:
Chris Lattner2b9f6002001-10-23 03:21:10 +000066 // bits Instruction format:
67 // --------------------------
68 // 13-08: Resulting type plane
69 // 19-14: Operand #1
70 // 25-20: Operand #2
71 // 31-26: Operand #3
72 //
Chris Lattner927b1852003-10-09 20:22:47 +000073 Type = (Op >> 8) & 63;
74 Args[0] = (Op >> 14) & 63;
75 Args[1] = (Op >> 20) & 63;
76 Args[2] = (Op >> 26) & 63;
Chris Lattner00950542001-06-06 20:29:01 +000077 break;
78 case 0:
79 Buf -= 4; // Hrm, try this again...
Chris Lattner927b1852003-10-09 20:22:47 +000080 if (read_vbr(Buf, EndBuf, Opcode))
Misha Brukmand554ebf2003-09-23 16:17:50 +000081 throw std::string("Error reading from buffer.");
Chris Lattner927b1852003-10-09 20:22:47 +000082 Opcode >>= 2;
83 if (read_vbr(Buf, EndBuf, Type))
Misha Brukmand554ebf2003-09-23 16:17:50 +000084 throw std::string("Error reading from buffer.");
Chris Lattner00950542001-06-06 20:29:01 +000085
Chris Lattner927b1852003-10-09 20:22:47 +000086 unsigned NumOperands;
87 if (read_vbr(Buf, EndBuf, NumOperands))
88 throw std::string("Error reading from buffer.");
89 Args.resize(NumOperands);
90
91 if (NumOperands == 0)
Misha Brukmand554ebf2003-09-23 16:17:50 +000092 throw std::string("Zero-argument instruction found; this is invalid.");
Chris Lattner00950542001-06-06 20:29:01 +000093
Chris Lattner927b1852003-10-09 20:22:47 +000094 for (unsigned i = 0; i != NumOperands; ++i)
95 if (read_vbr(Buf, EndBuf, Args[i]))
96 throw std::string("Error reading from buffer");
97 if (align32(Buf, EndBuf))
Misha Brukmand554ebf2003-09-23 16:17:50 +000098 throw std::string("Unaligned bytecode buffer.");
Chris Lattner00950542001-06-06 20:29:01 +000099 break;
100 }
Chris Lattner00950542001-06-06 20:29:01 +0000101}
102
103
Chris Lattner3483f542003-10-09 18:25:19 +0000104Instruction *BytecodeParser::ParseInstruction(const unsigned char *&Buf,
105 const unsigned char *EndBuf) {
Chris Lattner927b1852003-10-09 20:22:47 +0000106 std::vector<unsigned> Args;
107 RawInst RI(Buf, EndBuf, Args);
108 const Type *InstTy = getType(RI.Type);
Chris Lattner00950542001-06-06 20:29:01 +0000109
Chris Lattner927b1852003-10-09 20:22:47 +0000110 if (RI.Opcode >= Instruction::BinaryOpsBegin &&
111 RI.Opcode < Instruction::BinaryOpsEnd && Args.size() == 2)
112 return BinaryOperator::create((Instruction::BinaryOps)RI.Opcode,
113 getValue(InstTy, Args[0]),
114 getValue(InstTy, Args[1]));
Chris Lattner027dcc52001-07-08 21:10:27 +0000115
Chris Lattner927b1852003-10-09 20:22:47 +0000116 switch (RI.Opcode) {
Chris Lattner8f77dae2003-05-08 02:44:12 +0000117 case Instruction::VarArg:
Chris Lattner927b1852003-10-09 20:22:47 +0000118 return new VarArgInst(getValue(InstTy, Args[0]), getType(Args[1]));
119 case Instruction::Cast:
120 return new CastInst(getValue(InstTy, Args[0]), getType(Args[1]));
Chris Lattner027dcc52001-07-08 21:10:27 +0000121 case Instruction::PHINode: {
Chris Lattner927b1852003-10-09 20:22:47 +0000122 if (Args.size() == 0 || (Args.size() & 1))
Chris Lattner3483f542003-10-09 18:25:19 +0000123 throw std::string("Invalid phi node encountered!\n");
Chris Lattner927b1852003-10-09 20:22:47 +0000124
125 PHINode *PN = new PHINode(InstTy);
126 for (unsigned i = 0, e = Args.size(); i != e; i += 2)
127 PN->addIncoming(getValue(InstTy, Args[i]), getBasicBlock(Args[i+1]));
Chris Lattner3483f542003-10-09 18:25:19 +0000128 return PN;
Chris Lattner027dcc52001-07-08 21:10:27 +0000129 }
130
131 case Instruction::Shl:
132 case Instruction::Shr:
Chris Lattner927b1852003-10-09 20:22:47 +0000133 return new ShiftInst((Instruction::OtherOps)RI.Opcode,
134 getValue(InstTy, Args[0]),
135 getValue(Type::UByteTyID, Args[1]));
Chris Lattner027dcc52001-07-08 21:10:27 +0000136 case Instruction::Ret:
Chris Lattner927b1852003-10-09 20:22:47 +0000137 if (Args.size() == 0)
Chris Lattner3483f542003-10-09 18:25:19 +0000138 return new ReturnInst();
Chris Lattner927b1852003-10-09 20:22:47 +0000139 else if (Args.size() == 1)
140 return new ReturnInst(getValue(InstTy, Args[0]));
Chris Lattner027dcc52001-07-08 21:10:27 +0000141 break;
142
143 case Instruction::Br:
Chris Lattner927b1852003-10-09 20:22:47 +0000144 if (Args.size() == 1)
145 return new BranchInst(getBasicBlock(Args[0]));
146 else if (Args.size() == 3)
147 return new BranchInst(getBasicBlock(Args[0]), getBasicBlock(Args[1]),
148 getValue(Type::BoolTyID , Args[2]));
Chris Lattner3483f542003-10-09 18:25:19 +0000149 throw std::string("Invalid number of operands for a 'br' instruction!");
Chris Lattner027dcc52001-07-08 21:10:27 +0000150
151 case Instruction::Switch: {
Chris Lattner927b1852003-10-09 20:22:47 +0000152 if (Args.size() & 1)
Chris Lattner3483f542003-10-09 18:25:19 +0000153 throw std::string("Switch statement with odd number of arguments!");
Chris Lattner00950542001-06-06 20:29:01 +0000154
Chris Lattner927b1852003-10-09 20:22:47 +0000155 SwitchInst *I = new SwitchInst(getValue(InstTy, Args[0]),
156 getBasicBlock(Args[1]));
157 for (unsigned i = 2, e = Args.size(); i != e; i += 2)
158 I->addCase(cast<Constant>(getValue(InstTy, Args[i])),
159 getBasicBlock(Args[i+1]));
Chris Lattner3483f542003-10-09 18:25:19 +0000160 return I;
Chris Lattner027dcc52001-07-08 21:10:27 +0000161 }
162
163 case Instruction::Call: {
Chris Lattner927b1852003-10-09 20:22:47 +0000164 if (Args.size() == 0)
165 throw std::string("Invalid call instruction encountered!");
166
167 Value *F = getValue(InstTy, Args[0]);
Chris Lattner00950542001-06-06 20:29:01 +0000168
Chris Lattner3483f542003-10-09 18:25:19 +0000169 // Check to make sure we have a pointer to function type
Chris Lattner5bea4112003-09-05 18:25:29 +0000170 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
Chris Lattner3483f542003-10-09 18:25:19 +0000171 if (PTy == 0) throw std::string("Call to non function pointer value!");
Chris Lattner5bea4112003-09-05 18:25:29 +0000172 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
Chris Lattner3483f542003-10-09 18:25:19 +0000173 if (FTy == 0) throw std::string("Call to non function pointer value!");
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000174
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000175 std::vector<Value *> Params;
Chris Lattner5bea4112003-09-05 18:25:29 +0000176 const FunctionType::ParamTypes &PL = FTy->getParamTypes();
Chris Lattner05950c32001-10-13 06:47:01 +0000177
Chris Lattner5bea4112003-09-05 18:25:29 +0000178 if (!FTy->isVarArg()) {
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000179 FunctionType::ParamTypes::const_iterator It = PL.begin();
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000180
Chris Lattner927b1852003-10-09 20:22:47 +0000181 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
182 if (It == PL.end()) throw std::string("Invalid call instruction!");
183 Params.push_back(getValue(*It++, Args[i]));
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000184 }
Chris Lattner3483f542003-10-09 18:25:19 +0000185 if (It != PL.end()) throw std::string("Invalid call instruction!");
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000186 } else {
Chris Lattner927b1852003-10-09 20:22:47 +0000187 // FIXME: Args[1] is currently just a dummy padding field!
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000188
Chris Lattner927b1852003-10-09 20:22:47 +0000189 if (Args.size() & 1) // Must be pairs of type/value
190 throw std::string("Invalid call instruction!");
191
192 for (unsigned i = 2, e = Args.size(); i != e; i += 2)
193 Params.push_back(getValue(getType(Args[i]), Args[i+1]));
Chris Lattner00950542001-06-06 20:29:01 +0000194 }
Chris Lattner00950542001-06-06 20:29:01 +0000195
Chris Lattner3483f542003-10-09 18:25:19 +0000196 return new CallInst(F, Params);
Chris Lattner027dcc52001-07-08 21:10:27 +0000197 }
Chris Lattner05950c32001-10-13 06:47:01 +0000198 case Instruction::Invoke: {
Chris Lattner927b1852003-10-09 20:22:47 +0000199 if (Args.size() < 3) throw std::string("Invalid invoke instruction!");
200 Value *F = getValue(InstTy, Args[0]);
Chris Lattner05950c32001-10-13 06:47:01 +0000201
Chris Lattner3483f542003-10-09 18:25:19 +0000202 // Check to make sure we have a pointer to function type
Chris Lattner5bea4112003-09-05 18:25:29 +0000203 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
Chris Lattner3483f542003-10-09 18:25:19 +0000204 if (PTy == 0) throw std::string("Invoke to non function pointer value!");
Chris Lattner5bea4112003-09-05 18:25:29 +0000205 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
Chris Lattner3483f542003-10-09 18:25:19 +0000206 if (FTy == 0) throw std::string("Invoke to non function pointer value!");
Chris Lattner05950c32001-10-13 06:47:01 +0000207
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000208 std::vector<Value *> Params;
Chris Lattner05950c32001-10-13 06:47:01 +0000209 BasicBlock *Normal, *Except;
210
Chris Lattner927b1852003-10-09 20:22:47 +0000211 const FunctionType::ParamTypes &PL = FTy->getParamTypes();
212
Chris Lattner5bea4112003-09-05 18:25:29 +0000213 if (!FTy->isVarArg()) {
Chris Lattner927b1852003-10-09 20:22:47 +0000214 Normal = getBasicBlock(Args[1]);
215 Except = getBasicBlock(Args[2]);
Chris Lattner05950c32001-10-13 06:47:01 +0000216
Chris Lattner927b1852003-10-09 20:22:47 +0000217 FunctionType::ParamTypes::const_iterator It = PL.begin();
218 for (unsigned i = 3, e = Args.size(); i != e; ++i) {
219 if (It == PL.end()) throw std::string("Invalid invoke instruction!");
220 Params.push_back(getValue(*It++, Args[i]));
Chris Lattner05950c32001-10-13 06:47:01 +0000221 }
Chris Lattner927b1852003-10-09 20:22:47 +0000222 if (It != PL.end()) throw std::string("Invalid invoke instruction!");
Chris Lattner05950c32001-10-13 06:47:01 +0000223 } else {
Chris Lattner927b1852003-10-09 20:22:47 +0000224 // FIXME: Args[1] is a dummy padding field
225
226 if (Args.size() < 6) throw std::string("Invalid invoke instruction!");
227 if (Args[2] != Type::LabelTyID || Args[4] != Type::LabelTyID)
Chris Lattner3483f542003-10-09 18:25:19 +0000228 throw std::string("Invalid invoke instruction!");
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000229
Chris Lattner927b1852003-10-09 20:22:47 +0000230 Normal = getBasicBlock(Args[3]);
231 Except = getBasicBlock(Args[5]);
Chris Lattner05950c32001-10-13 06:47:01 +0000232
Chris Lattner927b1852003-10-09 20:22:47 +0000233 if (Args.size() & 1) // Must be pairs of type/value
Chris Lattner3483f542003-10-09 18:25:19 +0000234 throw std::string("Invalid invoke instruction!");
235
Chris Lattner927b1852003-10-09 20:22:47 +0000236 for (unsigned i = 6; i < Args.size(); i += 2)
237 Params.push_back(getValue(Args[i], Args[i+1]));
Chris Lattner05950c32001-10-13 06:47:01 +0000238 }
239
Chris Lattner3483f542003-10-09 18:25:19 +0000240 return new InvokeInst(F, Normal, Except, Params);
Chris Lattner05950c32001-10-13 06:47:01 +0000241 }
Chris Lattner027dcc52001-07-08 21:10:27 +0000242 case Instruction::Malloc:
Chris Lattner927b1852003-10-09 20:22:47 +0000243 if (Args.size() > 2) throw std::string("Invalid malloc instruction!");
244 if (!isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000245 throw std::string("Invalid malloc instruction!");
246
Chris Lattner927b1852003-10-09 20:22:47 +0000247 return new MallocInst(cast<PointerType>(InstTy)->getElementType(),
248 Args.size() ? getValue(Type::UIntTyID,
249 Args[0]) : 0);
Chris Lattner027dcc52001-07-08 21:10:27 +0000250
251 case Instruction::Alloca:
Chris Lattner927b1852003-10-09 20:22:47 +0000252 if (Args.size() > 2) throw std::string("Invalid alloca instruction!");
253 if (!isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000254 throw std::string("Invalid alloca instruction!");
Chris Lattner027dcc52001-07-08 21:10:27 +0000255
Chris Lattner927b1852003-10-09 20:22:47 +0000256 return new AllocaInst(cast<PointerType>(InstTy)->getElementType(),
257 Args.size() ? getValue(Type::UIntTyID,
258 Args[0]) : 0);
Chris Lattner027dcc52001-07-08 21:10:27 +0000259 case Instruction::Free:
Chris Lattner927b1852003-10-09 20:22:47 +0000260 if (!isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000261 throw std::string("Invalid free instruction!");
Chris Lattner927b1852003-10-09 20:22:47 +0000262 return new FreeInst(getValue(InstTy, Args[0]));
Chris Lattner027dcc52001-07-08 21:10:27 +0000263
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000264 case Instruction::GetElementPtr: {
Chris Lattner927b1852003-10-09 20:22:47 +0000265 if (Args.size() == 0 || !isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000266 throw std::string("Invalid getelementptr instruction!");
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000267
Chris Lattner927b1852003-10-09 20:22:47 +0000268 std::vector<Value*> Idx;
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000269
Chris Lattner927b1852003-10-09 20:22:47 +0000270 const Type *NextTy = InstTy;
271 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
272 const CompositeType *TopTy = dyn_cast_or_null<CompositeType>(NextTy);
273 if (!TopTy) throw std::string("Invalid getelementptr instruction!");
274 Idx.push_back(getValue(TopTy->getIndexType(), Args[i]));
275 NextTy = GetElementPtrInst::getIndexedType(InstTy, Idx, true);
Chris Lattner027dcc52001-07-08 21:10:27 +0000276 }
Chris Lattner77a316a2001-11-12 21:48:38 +0000277
Chris Lattner927b1852003-10-09 20:22:47 +0000278 return new GetElementPtrInst(getValue(InstTy, Args[0]), Idx);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000279 }
Chris Lattner09bd0252003-09-08 18:04:16 +0000280
Chris Lattnerdba2b222003-09-08 18:20:14 +0000281 case 62: // volatile load
Chris Lattner09bd0252003-09-08 18:04:16 +0000282 case Instruction::Load:
Chris Lattner927b1852003-10-09 20:22:47 +0000283 if (Args.size() != 1 || !isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000284 throw std::string("Invalid load instruction!");
Chris Lattner927b1852003-10-09 20:22:47 +0000285 return new LoadInst(getValue(InstTy, Args[0]), "", RI.Opcode == 62);
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000286
Chris Lattnerdba2b222003-09-08 18:20:14 +0000287 case 63: // volatile store
Chris Lattner09bd0252003-09-08 18:04:16 +0000288 case Instruction::Store: {
Chris Lattner927b1852003-10-09 20:22:47 +0000289 if (!isa<PointerType>(InstTy) || Args.size() != 2)
Chris Lattner3483f542003-10-09 18:25:19 +0000290 throw std::string("Invalid store instruction!");
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000291
Chris Lattner927b1852003-10-09 20:22:47 +0000292 Value *Ptr = getValue(InstTy, Args[1]);
Chris Lattner352eef72002-08-21 22:55:27 +0000293 const Type *ValTy = cast<PointerType>(Ptr->getType())->getElementType();
Chris Lattner927b1852003-10-09 20:22:47 +0000294 return new StoreInst(getValue(ValTy, Args[0]), Ptr, RI.Opcode == 63);
Chris Lattner00950542001-06-06 20:29:01 +0000295 }
Chris Lattner36143fc2003-09-08 18:54:55 +0000296 case Instruction::Unwind:
Chris Lattner927b1852003-10-09 20:22:47 +0000297 if (Args.size() != 0) throw std::string("Invalid unwind instruction!");
Chris Lattner3483f542003-10-09 18:25:19 +0000298 return new UnwindInst();
Chris Lattner927b1852003-10-09 20:22:47 +0000299 } // end switch(RI.Opcode)
Chris Lattner00950542001-06-06 20:29:01 +0000300
Chris Lattner927b1852003-10-09 20:22:47 +0000301 std::cerr << "Unrecognized instruction! " << RI.Opcode
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000302 << " ADDR = 0x" << (void*)Buf << "\n";
Chris Lattner3483f542003-10-09 18:25:19 +0000303 throw std::string("Unrecognized instruction!");
Chris Lattner00950542001-06-06 20:29:01 +0000304}