1 //===- ReadInst.cpp - Code to read an instruction from bytecode -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the mechanism to read an instruction from a bytecode
13 // Note that this library should be as fast as possible, reentrant, and
16 //===----------------------------------------------------------------------===//
18 #include "ReaderInternals.h"
19 #include "llvm/iTerminators.h"
20 #include "llvm/iMemory.h"
21 #include "llvm/iPHINode.h"
22 #include "llvm/iOther.h"
23 #include "llvm/Module.h"
26 struct RawInst { // The raw fields out of the bytecode stream...
31 RawInst(const unsigned char *&Buf, const unsigned char *EndBuf,
32 std::vector<unsigned> &Args);
38 RawInst::RawInst(const unsigned char *&Buf, const unsigned char *EndBuf,
39 std::vector<unsigned> &Args) {
41 if (read(Buf, EndBuf, Op))
42 throw std::string("Error reading from buffer.");
44 // bits Instruction format: Common to all formats
45 // --------------------------
46 // 01-00: Opcode type, fixed to 1.
48 Opcode = (Op >> 2) & 63;
49 Args.resize((Op >> 0) & 03);
51 switch (Args.size()) {
53 // bits Instruction format:
54 // --------------------------
55 // 19-08: Resulting type plane
56 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
58 Type = (Op >> 8) & 4095;
59 Args[0] = (Op >> 20) & 4095;
60 if (Args[0] == 4095) // Handle special encoding for 0 operands...
64 // bits Instruction format:
65 // --------------------------
66 // 15-08: Resulting type plane
70 Type = (Op >> 8) & 255;
71 Args[0] = (Op >> 16) & 255;
72 Args[1] = (Op >> 24) & 255;
75 // bits Instruction format:
76 // --------------------------
77 // 13-08: Resulting type plane
82 Type = (Op >> 8) & 63;
83 Args[0] = (Op >> 14) & 63;
84 Args[1] = (Op >> 20) & 63;
85 Args[2] = (Op >> 26) & 63;
88 Buf -= 4; // Hrm, try this again...
89 if (read_vbr(Buf, EndBuf, Opcode))
90 throw std::string("Error reading from buffer.");
92 if (read_vbr(Buf, EndBuf, Type))
93 throw std::string("Error reading from buffer.");
96 if (read_vbr(Buf, EndBuf, NumOperands))
97 throw std::string("Error reading from buffer.");
98 Args.resize(NumOperands);
100 if (NumOperands == 0)
101 throw std::string("Zero-argument instruction found; this is invalid.");
103 for (unsigned i = 0; i != NumOperands; ++i)
104 if (read_vbr(Buf, EndBuf, Args[i]))
105 throw std::string("Error reading from buffer");
106 if (align32(Buf, EndBuf))
107 throw std::string("Unaligned bytecode buffer.");
113 void BytecodeParser::ParseInstruction(const unsigned char *&Buf,
114 const unsigned char *EndBuf,
115 std::vector<unsigned> &Args,
118 RawInst RI(Buf, EndBuf, Args);
119 const Type *InstTy = getType(RI.Type);
121 Instruction *Result = 0;
122 if (RI.Opcode >= Instruction::BinaryOpsBegin &&
123 RI.Opcode < Instruction::BinaryOpsEnd && Args.size() == 2)
124 Result = BinaryOperator::create((Instruction::BinaryOps)RI.Opcode,
125 getValue(RI.Type, Args[0]),
126 getValue(RI.Type, Args[1]));
130 if (Result == 0) throw std::string("Illegal instruction read!");
132 case Instruction::VAArg:
133 Result = new VAArgInst(getValue(RI.Type, Args[0]), getType(Args[1]));
135 case Instruction::VANext:
136 if (!hasOldStyleVarargs) {
137 Result = new VANextInst(getValue(RI.Type, Args[0]), getType(Args[1]));
139 // In the old-style varargs scheme, this was the "va_arg" instruction.
140 // Emit emulation code now.
141 if (!usesOldStyleVarargs) {
142 usesOldStyleVarargs = true;
143 std::cerr << "WARNING: this bytecode file uses obsolete features. "
144 << "Disassemble and assemble to update it.\n";
147 Value *VAListPtr = getValue(RI.Type, Args[0]);
148 const Type *ArgTy = getType(Args[1]);
150 // First, load the valist...
151 Instruction *CurVAList = new LoadInst(VAListPtr, "");
152 BB->getInstList().push_back(CurVAList);
154 // Construct the vaarg
155 Result = new VAArgInst(CurVAList, ArgTy);
157 // Now we must advance the pointer and update it in memory.
158 Instruction *TheVANext = new VANextInst(CurVAList, ArgTy);
159 BB->getInstList().push_back(TheVANext);
161 BB->getInstList().push_back(new StoreInst(TheVANext, VAListPtr));
165 case Instruction::Cast:
166 Result = new CastInst(getValue(RI.Type, Args[0]), getType(Args[1]));
168 case Instruction::PHI: {
169 if (Args.size() == 0 || (Args.size() & 1))
170 throw std::string("Invalid phi node encountered!\n");
172 PHINode *PN = new PHINode(InstTy);
173 PN->op_reserve(Args.size());
174 for (unsigned i = 0, e = Args.size(); i != e; i += 2)
175 PN->addIncoming(getValue(RI.Type, Args[i]), getBasicBlock(Args[i+1]));
180 case Instruction::Shl:
181 case Instruction::Shr:
182 Result = new ShiftInst((Instruction::OtherOps)RI.Opcode,
183 getValue(RI.Type, Args[0]),
184 getValue(Type::UByteTyID, Args[1]));
186 case Instruction::Ret:
187 if (Args.size() == 0)
188 Result = new ReturnInst();
189 else if (Args.size() == 1)
190 Result = new ReturnInst(getValue(RI.Type, Args[0]));
192 throw std::string("Unrecognized instruction!");
195 case Instruction::Br:
196 if (Args.size() == 1)
197 Result = new BranchInst(getBasicBlock(Args[0]));
198 else if (Args.size() == 3)
199 Result = new BranchInst(getBasicBlock(Args[0]), getBasicBlock(Args[1]),
200 getValue(Type::BoolTyID , Args[2]));
202 throw std::string("Invalid number of operands for a 'br' instruction!");
204 case Instruction::Switch: {
206 throw std::string("Switch statement with odd number of arguments!");
208 SwitchInst *I = new SwitchInst(getValue(RI.Type, Args[0]),
209 getBasicBlock(Args[1]));
210 for (unsigned i = 2, e = Args.size(); i != e; i += 2)
211 I->addCase(cast<Constant>(getValue(RI.Type, Args[i])),
212 getBasicBlock(Args[i+1]));
217 case Instruction::Call: {
218 if (Args.size() == 0)
219 throw std::string("Invalid call instruction encountered!");
221 Value *F = getValue(RI.Type, Args[0]);
223 // Check to make sure we have a pointer to function type
224 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
225 if (PTy == 0) throw std::string("Call to non function pointer value!");
226 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
227 if (FTy == 0) throw std::string("Call to non function pointer value!");
229 std::vector<Value *> Params;
230 const FunctionType::ParamTypes &PL = FTy->getParamTypes();
232 if (!FTy->isVarArg()) {
233 FunctionType::ParamTypes::const_iterator It = PL.begin();
235 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
236 if (It == PL.end()) throw std::string("Invalid call instruction!");
237 Params.push_back(getValue(*It++, Args[i]));
239 if (It != PL.end()) throw std::string("Invalid call instruction!");
241 Args.erase(Args.begin(), Args.begin()+1+hasVarArgCallPadding);
243 unsigned FirstVariableOperand;
244 if (!hasVarArgCallPadding) {
245 if (Args.size() < FTy->getNumParams())
246 throw std::string("Call instruction missing operands!");
248 // Read all of the fixed arguments
249 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
250 Params.push_back(getValue(FTy->getParamType(i), Args[i]));
252 FirstVariableOperand = FTy->getNumParams();
254 FirstVariableOperand = 0;
257 if ((Args.size()-FirstVariableOperand) & 1) // Must be pairs of type/value
258 throw std::string("Invalid call instruction!");
260 for (unsigned i = FirstVariableOperand, e = Args.size(); i != e; i += 2)
261 Params.push_back(getValue(Args[i], Args[i+1]));
264 Result = new CallInst(F, Params);
267 case Instruction::Invoke: {
268 if (Args.size() < 3) throw std::string("Invalid invoke instruction!");
269 Value *F = getValue(RI.Type, Args[0]);
271 // Check to make sure we have a pointer to function type
272 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
273 if (PTy == 0) throw std::string("Invoke to non function pointer value!");
274 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
275 if (FTy == 0) throw std::string("Invoke to non function pointer value!");
277 std::vector<Value *> Params;
278 BasicBlock *Normal, *Except;
280 const FunctionType::ParamTypes &PL = FTy->getParamTypes();
282 if (!FTy->isVarArg()) {
283 Normal = getBasicBlock(Args[1]);
284 Except = getBasicBlock(Args[2]);
286 FunctionType::ParamTypes::const_iterator It = PL.begin();
287 for (unsigned i = 3, e = Args.size(); i != e; ++i) {
288 if (It == PL.end()) throw std::string("Invalid invoke instruction!");
289 Params.push_back(getValue(*It++, Args[i]));
291 if (It != PL.end()) throw std::string("Invalid invoke instruction!");
293 Args.erase(Args.begin(), Args.begin()+1+hasVarArgCallPadding);
295 unsigned FirstVariableArgument;
296 if (!hasVarArgCallPadding) {
297 Normal = getBasicBlock(Args[0]);
298 Except = getBasicBlock(Args[1]);
300 FirstVariableArgument = FTy->getNumParams()+2;
301 for (unsigned i = 2; i != FirstVariableArgument; ++i)
302 Params.push_back(getValue(FTy->getParamType(i-2), Args[i]));
305 if (Args.size() < 4) throw std::string("Invalid invoke instruction!");
306 if (Args[0] != Type::LabelTyID || Args[2] != Type::LabelTyID)
307 throw std::string("Invalid invoke instruction!");
308 Normal = getBasicBlock(Args[1]);
309 Except = getBasicBlock(Args[3]);
311 FirstVariableArgument = 4;
314 if (Args.size()-FirstVariableArgument & 1) // Must be pairs of type/value
315 throw std::string("Invalid invoke instruction!");
317 for (unsigned i = FirstVariableArgument; i < Args.size(); i += 2)
318 Params.push_back(getValue(Args[i], Args[i+1]));
321 Result = new InvokeInst(F, Normal, Except, Params);
324 case Instruction::Malloc:
325 if (Args.size() > 2) throw std::string("Invalid malloc instruction!");
326 if (!isa<PointerType>(InstTy))
327 throw std::string("Invalid malloc instruction!");
329 Result = new MallocInst(cast<PointerType>(InstTy)->getElementType(),
330 Args.size() ? getValue(Type::UIntTyID,
334 case Instruction::Alloca:
335 if (Args.size() > 2) throw std::string("Invalid alloca instruction!");
336 if (!isa<PointerType>(InstTy))
337 throw std::string("Invalid alloca instruction!");
339 Result = new AllocaInst(cast<PointerType>(InstTy)->getElementType(),
340 Args.size() ? getValue(Type::UIntTyID, Args[0]) :0);
342 case Instruction::Free:
343 if (!isa<PointerType>(InstTy))
344 throw std::string("Invalid free instruction!");
345 Result = new FreeInst(getValue(RI.Type, Args[0]));
347 case Instruction::GetElementPtr: {
348 if (Args.size() == 0 || !isa<PointerType>(InstTy))
349 throw std::string("Invalid getelementptr instruction!");
351 std::vector<Value*> Idx;
353 const Type *NextTy = InstTy;
354 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
355 const CompositeType *TopTy = dyn_cast_or_null<CompositeType>(NextTy);
356 if (!TopTy) throw std::string("Invalid getelementptr instruction!");
357 Idx.push_back(getValue(TopTy->getIndexType(), Args[i]));
358 NextTy = GetElementPtrInst::getIndexedType(InstTy, Idx, true);
361 Result = new GetElementPtrInst(getValue(RI.Type, Args[0]), Idx);
365 case 62: // volatile load
366 case Instruction::Load:
367 if (Args.size() != 1 || !isa<PointerType>(InstTy))
368 throw std::string("Invalid load instruction!");
369 Result = new LoadInst(getValue(RI.Type, Args[0]), "", RI.Opcode == 62);
372 case 63: // volatile store
373 case Instruction::Store: {
374 if (!isa<PointerType>(InstTy) || Args.size() != 2)
375 throw std::string("Invalid store instruction!");
377 Value *Ptr = getValue(RI.Type, Args[1]);
378 const Type *ValTy = cast<PointerType>(Ptr->getType())->getElementType();
379 Result = new StoreInst(getValue(ValTy, Args[0]), Ptr, RI.Opcode == 63);
382 case Instruction::Unwind:
383 if (Args.size() != 0) throw std::string("Invalid unwind instruction!");
384 Result = new UnwindInst();
386 } // end switch(RI.Opcode)
388 insertValue(Result, Values);
389 BB->getInstList().push_back(Result);
390 BCR_TRACE(4, *Result);