1 //===- ReadInst.cpp - Code to read an instruction from bytecode -----------===//
3 // This file defines the mechanism to read an instruction from a bytecode
6 // Note that this library should be as fast as possible, reentrant, and
9 // TODO: Change from getValue(Raw.Arg1) etc, to getArg(Raw, 1)
10 // Make it check type, so that casts are checked.
12 //===----------------------------------------------------------------------===//
14 #include "ReaderInternals.h"
15 #include "llvm/iTerminators.h"
16 #include "llvm/iMemory.h"
17 #include "llvm/iPHINode.h"
18 #include "llvm/iOther.h"
20 std::auto_ptr<RawInst>
21 BytecodeParser::ParseRawInst(const unsigned char *&Buf,
22 const unsigned char *EndBuf) {
24 std::auto_ptr<RawInst> Result = std::auto_ptr<RawInst>(new RawInst());
25 if (read(Buf, EndBuf, Op))
26 throw std::string("Error reading from buffer.");
28 // bits Instruction format: Common to all formats
29 // --------------------------
30 // 01-00: Opcode type, fixed to 1.
32 Result->NumOperands = (Op >> 0) & 03;
33 Result->Opcode = (Op >> 2) & 63;
35 switch (Result->NumOperands) {
37 // bits Instruction format:
38 // --------------------------
39 // 19-08: Resulting type plane
40 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
42 Result->Ty = getType((Op >> 8) & 4095);
43 Result->Arg1 = (Op >> 20) & 4095;
44 if (Result->Arg1 == 4095) // Handle special encoding for 0 operands...
45 Result->NumOperands = 0;
48 // bits Instruction format:
49 // --------------------------
50 // 15-08: Resulting type plane
54 Result->Ty = getType((Op >> 8) & 255);
55 Result->Arg1 = (Op >> 16) & 255;
56 Result->Arg2 = (Op >> 24) & 255;
59 // bits Instruction format:
60 // --------------------------
61 // 13-08: Resulting type plane
66 Result->Ty = getType((Op >> 8) & 63);
67 Result->Arg1 = (Op >> 14) & 63;
68 Result->Arg2 = (Op >> 20) & 63;
69 Result->Arg3 = (Op >> 26) & 63;
72 Buf -= 4; // Hrm, try this again...
73 if (read_vbr(Buf, EndBuf, Result->Opcode))
74 throw std::string("Error reading from buffer.");
76 if (read_vbr(Buf, EndBuf, Typ))
77 throw std::string("Error reading from buffer.");
78 Result->Ty = getType(Typ);
80 throw std::string("Invalid type read in instruction.");
81 if (read_vbr(Buf, EndBuf, Result->NumOperands))
82 throw std::string("Error reading from buffer.");
84 switch (Result->NumOperands) {
86 throw std::string("Zero-argument instruction found; this is invalid.");
88 if (read_vbr(Buf, EndBuf, Result->Arg1))
89 throw std::string("Error reading from buffer");
92 if (read_vbr(Buf, EndBuf, Result->Arg1) ||
93 read_vbr(Buf, EndBuf, Result->Arg2))
94 throw std::string("Error reading from buffer");
97 if (read_vbr(Buf, EndBuf, Result->Arg1) ||
98 read_vbr(Buf, EndBuf, Result->Arg2) ||
99 read_vbr(Buf, EndBuf, Result->Arg3))
100 throw std::string("Error reading from buffer");
103 if (read_vbr(Buf, EndBuf, Result->Arg1) ||
104 read_vbr(Buf, EndBuf, Result->Arg2))
105 throw std::string("Error reading from buffer");
107 // Allocate a vector to hold arguments 3, 4, 5, 6 ...
108 Result->VarArgs = new std::vector<unsigned>(Result->NumOperands-2);
109 for (unsigned a = 0; a < Result->NumOperands-2; a++)
110 if (read_vbr(Buf, EndBuf, (*Result->VarArgs)[a]))
111 throw std::string("Error reading from buffer");
115 if (align32(Buf, EndBuf))
116 throw std::string("Unaligned bytecode buffer.");
121 std::cerr << "NO: " << Result->NumOperands << " opcode: " << Result->Opcode
122 << " Ty: "<< Result->Ty->getDescription()<< " arg1: "<< Result->Arg1
123 << " arg2: " << Result->Arg2 << " arg3: " << Result->Arg3 << "\n";
129 bool BytecodeParser::ParseInstruction(const unsigned char *&Buf,
130 const unsigned char *EndBuf,
132 std::auto_ptr<RawInst> Raw = ParseRawInst(Buf, EndBuf);
134 if (Raw->Opcode >= Instruction::BinaryOpsBegin &&
135 Raw->Opcode < Instruction::BinaryOpsEnd && Raw->NumOperands == 2) {
136 Res = BinaryOperator::create((Instruction::BinaryOps)Raw->Opcode,
137 getValue(Raw->Ty, Raw->Arg1),
138 getValue(Raw->Ty, Raw->Arg2));
143 switch (Raw->Opcode) {
144 case Instruction::VarArg:
145 case Instruction::Cast: {
146 V = getValue(Raw->Ty, Raw->Arg1);
147 const Type *Ty = getType(Raw->Arg2);
148 if (V == 0 || Ty == 0) { std::cerr << "Invalid cast!\n"; return true; }
149 if (Raw->Opcode == Instruction::Cast)
150 Res = new CastInst(V, Ty);
152 Res = new VarArgInst(V, Ty);
155 case Instruction::PHINode: {
156 PHINode *PN = new PHINode(Raw->Ty);
157 switch (Raw->NumOperands) {
160 case 3: std::cerr << "Invalid phi node encountered!\n";
163 case 2: PN->addIncoming(getValue(Raw->Ty, Raw->Arg1),
164 cast<BasicBlock>(getValue(Type::LabelTy,
168 PN->addIncoming(getValue(Raw->Ty, Raw->Arg1),
169 cast<BasicBlock>(getValue(Type::LabelTy, Raw->Arg2)));
170 if (Raw->VarArgs->size() & 1) {
171 std::cerr << "PHI Node with ODD number of arguments!\n";
175 std::vector<unsigned> &args = *Raw->VarArgs;
176 for (unsigned i = 0; i < args.size(); i+=2)
177 PN->addIncoming(getValue(Raw->Ty, args[i]),
178 cast<BasicBlock>(getValue(Type::LabelTy, args[i+1])));
187 case Instruction::Shl:
188 case Instruction::Shr:
189 Res = new ShiftInst((Instruction::OtherOps)Raw->Opcode,
190 getValue(Raw->Ty, Raw->Arg1),
191 getValue(Type::UByteTy, Raw->Arg2));
193 case Instruction::Ret:
194 if (Raw->NumOperands == 0) {
195 Res = new ReturnInst(); return false;
196 } else if (Raw->NumOperands == 1) {
197 Res = new ReturnInst(getValue(Raw->Ty, Raw->Arg1)); return false;
201 case Instruction::Br:
202 if (Raw->NumOperands == 1) {
203 Res = new BranchInst(cast<BasicBlock>(getValue(Type::LabelTy,Raw->Arg1)));
205 } else if (Raw->NumOperands == 3) {
206 Res = new BranchInst(cast<BasicBlock>(getValue(Type::LabelTy, Raw->Arg1)),
207 cast<BasicBlock>(getValue(Type::LabelTy, Raw->Arg2)),
208 getValue(Type::BoolTy , Raw->Arg3));
213 case Instruction::Switch: {
215 new SwitchInst(getValue(Raw->Ty, Raw->Arg1),
216 cast<BasicBlock>(getValue(Type::LabelTy, Raw->Arg2)));
218 if (Raw->NumOperands < 3) return false; // No destinations? Weird.
220 if (Raw->NumOperands == 3 || Raw->VarArgs->size() & 1) {
221 std::cerr << "Switch statement with odd number of arguments!\n";
226 std::vector<unsigned> &args = *Raw->VarArgs;
227 for (unsigned i = 0; i < args.size(); i += 2)
228 I->addCase(cast<Constant>(getValue(Raw->Ty, args[i])),
229 cast<BasicBlock>(getValue(Type::LabelTy, args[i+1])));
235 case Instruction::Call: {
236 Value *F = getValue(Raw->Ty, Raw->Arg1);
237 if (F == 0) return true;
239 // Check to make sure we have a pointer to method type
240 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
241 if (PTy == 0) return true;
242 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
243 if (FTy == 0) return true;
245 std::vector<Value *> Params;
246 const FunctionType::ParamTypes &PL = FTy->getParamTypes();
248 if (!FTy->isVarArg()) {
249 FunctionType::ParamTypes::const_iterator It = PL.begin();
251 switch (Raw->NumOperands) {
252 case 0: std::cerr << "Invalid call instruction encountered!\n";
255 case 2: Params.push_back(getValue(*It++, Raw->Arg2)); break;
256 case 3: Params.push_back(getValue(*It++, Raw->Arg2));
257 if (It == PL.end()) return true;
258 Params.push_back(getValue(*It++, Raw->Arg3)); break;
260 Params.push_back(getValue(*It++, Raw->Arg2));
262 std::vector<unsigned> &args = *Raw->VarArgs;
263 for (unsigned i = 0; i < args.size(); i++) {
264 if (It == PL.end()) return true;
265 Params.push_back(getValue(*It++, args[i]));
266 if (Params.back() == 0) return true;
271 if (It != PL.end()) return true;
273 if (Raw->NumOperands > 2) {
274 std::vector<unsigned> &args = *Raw->VarArgs;
275 if (args.size() < 1) return true;
277 if ((args.size() & 1) != 0)
278 return true; // Must be pairs of type/value
279 for (unsigned i = 0; i < args.size(); i+=2) {
280 const Type *Ty = getType(args[i]);
284 Value *V = getValue(Ty, args[i+1]);
285 if (V == 0) return true;
292 Res = new CallInst(F, Params);
295 case Instruction::Invoke: {
296 Value *F = getValue(Raw->Ty, Raw->Arg1);
297 if (F == 0) return true;
299 // Check to make sure we have a pointer to method type
300 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
301 if (PTy == 0) return true;
302 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
303 if (FTy == 0) return true;
305 std::vector<Value *> Params;
306 const FunctionType::ParamTypes &PL = FTy->getParamTypes();
307 std::vector<unsigned> &args = *Raw->VarArgs;
309 BasicBlock *Normal, *Except;
311 if (!FTy->isVarArg()) {
312 if (Raw->NumOperands < 3) return true;
314 Normal = cast<BasicBlock>(getValue(Type::LabelTy, Raw->Arg2));
315 if (Raw->NumOperands == 3)
316 Except = cast<BasicBlock>(getValue(Type::LabelTy, Raw->Arg3));
318 Except = cast<BasicBlock>(getValue(Type::LabelTy, args[0]));
320 FunctionType::ParamTypes::const_iterator It = PL.begin();
321 for (unsigned i = 1; i < args.size(); i++) {
322 if (It == PL.end()) return true;
323 Params.push_back(getValue(*It++, args[i]));
324 if (Params.back() == 0) return true;
326 if (It != PL.end()) return true;
329 if (args.size() < 4) return true;
330 if (getType(args[0]) != Type::LabelTy ||
331 getType(args[2]) != Type::LabelTy) return true;
332 Normal = cast<BasicBlock>(getValue(Type::LabelTy, args[1]));
333 Except = cast<BasicBlock>(getValue(Type::LabelTy, args[3]));
335 if ((args.size() & 1) != 0)
336 return true; // Must be pairs of type/value
337 for (unsigned i = 4; i < args.size(); i+=2) {
338 Params.push_back(getValue(getType(args[i]), args[i+1]));
339 if (Params.back() == 0) return true;
343 if (Raw->NumOperands > 3)
345 Res = new InvokeInst(F, Normal, Except, Params);
348 case Instruction::Malloc:
349 if (Raw->NumOperands > 2) return true;
350 V = Raw->NumOperands ? getValue(Type::UIntTy, Raw->Arg1) : 0;
351 if (const PointerType *PTy = dyn_cast<PointerType>(Raw->Ty))
352 Res = new MallocInst(PTy->getElementType(), V);
357 case Instruction::Alloca:
358 if (Raw->NumOperands > 2) return true;
359 V = Raw->NumOperands ? getValue(Type::UIntTy, Raw->Arg1) : 0;
360 if (const PointerType *PTy = dyn_cast<PointerType>(Raw->Ty))
361 Res = new AllocaInst(PTy->getElementType(), V);
366 case Instruction::Free:
367 V = getValue(Raw->Ty, Raw->Arg1);
368 if (!isa<PointerType>(V->getType())) return true;
369 Res = new FreeInst(V);
372 case Instruction::GetElementPtr: {
373 std::vector<Value*> Idx;
374 if (!isa<PointerType>(Raw->Ty)) return true;
375 const CompositeType *TopTy = dyn_cast<CompositeType>(Raw->Ty);
377 switch (Raw->NumOperands) {
378 case 0: std::cerr << "Invalid getelementptr encountered!\n"; return true;
381 if (!TopTy) return true;
382 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw->Arg2));
386 if (!TopTy) return true;
387 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw->Arg2));
390 const Type *ETy = GetElementPtrInst::getIndexedType(TopTy, Idx, true);
391 const CompositeType *ElTy = dyn_cast_or_null<CompositeType>(ETy);
392 if (!ElTy) return true;
394 Idx.push_back(V = getValue(ElTy->getIndexType(), Raw->Arg3));
399 if (!TopTy) return true;
400 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw->Arg2));
403 std::vector<unsigned> &args = *Raw->VarArgs;
404 for (unsigned i = 0, E = args.size(); i != E; ++i) {
405 const Type *ETy = GetElementPtrInst::getIndexedType(Raw->Ty, Idx, true);
406 const CompositeType *ElTy = dyn_cast_or_null<CompositeType>(ETy);
407 if (!ElTy) return true;
408 Idx.push_back(V = getValue(ElTy->getIndexType(), args[i]));
415 Res = new GetElementPtrInst(getValue(Raw->Ty, Raw->Arg1), Idx);
419 case 62: // volatile load
420 case Instruction::Load:
421 if (Raw->NumOperands != 1) return true;
422 if (!isa<PointerType>(Raw->Ty)) return true;
423 Res = new LoadInst(getValue(Raw->Ty, Raw->Arg1), "", Raw->Opcode == 62);
426 case 63: // volatile store
427 case Instruction::Store: {
428 if (!isa<PointerType>(Raw->Ty) || Raw->NumOperands != 2) return true;
430 Value *Ptr = getValue(Raw->Ty, Raw->Arg2);
431 const Type *ValTy = cast<PointerType>(Ptr->getType())->getElementType();
432 Res = new StoreInst(getValue(ValTy, Raw->Arg1), Ptr, Raw->Opcode == 63);
435 case Instruction::Unwind:
436 if (Raw->NumOperands != 0) return true;
437 Res = new UnwindInst();
439 } // end switch(Raw->Opcode)
441 std::cerr << "Unrecognized instruction! " << Raw->Opcode
442 << " ADDR = 0x" << (void*)Buf << "\n";