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 bool BytecodeParser::ParseRawInst(const uchar *&Buf, const uchar *EndBuf,
23 if (read(Buf, EndBuf, Op)) return true;
25 // bits Instruction format: Common to all formats
26 // --------------------------
27 // 01-00: Opcode type, fixed to 1.
29 Result.NumOperands = (Op >> 0) & 03;
30 Result.Opcode = (Op >> 2) & 63;
32 switch (Result.NumOperands) {
34 // bits Instruction format:
35 // --------------------------
36 // 19-08: Resulting type plane
37 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
39 Result.Ty = getType((Op >> 8) & 4095);
40 Result.Arg1 = (Op >> 20) & 4095;
41 if (Result.Arg1 == 4095) // Handle special encoding for 0 operands...
42 Result.NumOperands = 0;
45 // bits Instruction format:
46 // --------------------------
47 // 15-08: Resulting type plane
51 Result.Ty = getType((Op >> 8) & 255);
52 Result.Arg1 = (Op >> 16) & 255;
53 Result.Arg2 = (Op >> 24) & 255;
56 // bits Instruction format:
57 // --------------------------
58 // 13-08: Resulting type plane
63 Result.Ty = getType((Op >> 8) & 63);
64 Result.Arg1 = (Op >> 14) & 63;
65 Result.Arg2 = (Op >> 20) & 63;
66 Result.Arg3 = (Op >> 26) & 63;
69 Buf -= 4; // Hrm, try this again...
70 if (read_vbr(Buf, EndBuf, Result.Opcode)) return true;
72 if (read_vbr(Buf, EndBuf, Typ)) return true;
73 Result.Ty = getType(Typ);
74 if (Result.Ty == 0) return true;
75 if (read_vbr(Buf, EndBuf, Result.NumOperands)) return true;
77 switch (Result.NumOperands) {
79 std::cerr << "Zero Arg instr found!\n";
80 return true; // This encoding is invalid!
82 if (read_vbr(Buf, EndBuf, Result.Arg1)) return true;
85 if (read_vbr(Buf, EndBuf, Result.Arg1) ||
86 read_vbr(Buf, EndBuf, Result.Arg2)) return true;
89 if (read_vbr(Buf, EndBuf, Result.Arg1) ||
90 read_vbr(Buf, EndBuf, Result.Arg2) ||
91 read_vbr(Buf, EndBuf, Result.Arg3)) return true;
94 if (read_vbr(Buf, EndBuf, Result.Arg1) ||
95 read_vbr(Buf, EndBuf, Result.Arg2)) return true;
97 // Allocate a vector to hold arguments 3, 4, 5, 6 ...
98 Result.VarArgs = new std::vector<unsigned>(Result.NumOperands-2);
99 for (unsigned a = 0; a < Result.NumOperands-2; a++)
100 if (read_vbr(Buf, EndBuf, (*Result.VarArgs)[a])) return true;
103 if (align32(Buf, EndBuf)) return true;
108 std::cerr << "NO: " << Result.NumOperands << " opcode: " << Result.Opcode
109 << " Ty: " << Result.Ty->getDescription() << " arg1: "<< Result.Arg1
110 << " arg2: " << Result.Arg2 << " arg3: " << Result.Arg3 << "\n";
116 bool BytecodeParser::ParseInstruction(const uchar *&Buf, const uchar *EndBuf,
118 BasicBlock *BB /*HACK*/) {
120 if (ParseRawInst(Buf, EndBuf, Raw))
123 if (Raw.Opcode >= Instruction::BinaryOpsBegin &&
124 Raw.Opcode < Instruction::BinaryOpsEnd && Raw.NumOperands == 2) {
125 Res = BinaryOperator::create((Instruction::BinaryOps)Raw.Opcode,
126 getValue(Raw.Ty, Raw.Arg1),
127 getValue(Raw.Ty, Raw.Arg2));
132 switch (Raw.Opcode) {
133 case Instruction::Cast: {
134 V = getValue(Raw.Ty, Raw.Arg1);
135 const Type *Ty = getType(Raw.Arg2);
136 if (V == 0 || Ty == 0) { std::cerr << "Invalid cast!\n"; return true; }
137 Res = new CastInst(V, Ty);
140 case Instruction::PHINode: {
141 PHINode *PN = new PHINode(Raw.Ty);
142 switch (Raw.NumOperands) {
145 case 3: std::cerr << "Invalid phi node encountered!\n";
148 case 2: PN->addIncoming(getValue(Raw.Ty, Raw.Arg1),
149 cast<BasicBlock>(getValue(Type::LabelTy,Raw.Arg2)));
152 PN->addIncoming(getValue(Raw.Ty, Raw.Arg1),
153 cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2)));
154 if (Raw.VarArgs->size() & 1) {
155 std::cerr << "PHI Node with ODD number of arguments!\n";
159 std::vector<unsigned> &args = *Raw.VarArgs;
160 for (unsigned i = 0; i < args.size(); i+=2)
161 PN->addIncoming(getValue(Raw.Ty, args[i]),
162 cast<BasicBlock>(getValue(Type::LabelTy, args[i+1])));
171 case Instruction::Shl:
172 case Instruction::Shr:
173 Res = new ShiftInst((Instruction::OtherOps)Raw.Opcode,
174 getValue(Raw.Ty, Raw.Arg1),
175 getValue(Type::UByteTy, Raw.Arg2));
177 case Instruction::Ret:
178 if (Raw.NumOperands == 0) {
179 Res = new ReturnInst(); return false;
180 } else if (Raw.NumOperands == 1) {
181 Res = new ReturnInst(getValue(Raw.Ty, Raw.Arg1)); return false;
185 case Instruction::Br:
186 if (Raw.NumOperands == 1) {
187 Res = new BranchInst(cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg1)));
189 } else if (Raw.NumOperands == 3) {
190 Res = new BranchInst(cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg1)),
191 cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2)),
192 getValue(Type::BoolTy , Raw.Arg3));
197 case Instruction::Switch: {
199 new SwitchInst(getValue(Raw.Ty, Raw.Arg1),
200 cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2)));
202 if (Raw.NumOperands < 3) return false; // No destinations? Wierd.
204 if (Raw.NumOperands == 3 || Raw.VarArgs->size() & 1) {
205 std::cerr << "Switch statement with odd number of arguments!\n";
210 std::vector<unsigned> &args = *Raw.VarArgs;
211 for (unsigned i = 0; i < args.size(); i += 2)
212 I->dest_push_back(cast<Constant>(getValue(Raw.Ty, args[i])),
213 cast<BasicBlock>(getValue(Type::LabelTy, args[i+1])));
219 case Instruction::Call: {
220 Value *M = getValue(Raw.Ty, Raw.Arg1);
221 if (M == 0) return true;
223 // Check to make sure we have a pointer to method type
224 const PointerType *PTy = dyn_cast<PointerType>(M->getType());
225 if (PTy == 0) return true;
226 const FunctionType *MTy = dyn_cast<FunctionType>(PTy->getElementType());
227 if (MTy == 0) return true;
229 std::vector<Value *> Params;
230 const FunctionType::ParamTypes &PL = MTy->getParamTypes();
232 if (!MTy->isVarArg()) {
233 FunctionType::ParamTypes::const_iterator It = PL.begin();
235 switch (Raw.NumOperands) {
236 case 0: std::cerr << "Invalid call instruction encountered!\n";
239 case 2: Params.push_back(getValue(*It++, Raw.Arg2)); break;
240 case 3: Params.push_back(getValue(*It++, Raw.Arg2));
241 if (It == PL.end()) return true;
242 Params.push_back(getValue(*It++, Raw.Arg3)); break;
244 Params.push_back(getValue(*It++, Raw.Arg2));
246 std::vector<unsigned> &args = *Raw.VarArgs;
247 for (unsigned i = 0; i < args.size(); i++) {
248 if (It == PL.end()) return true;
249 // TODO: Check getValue for null!
250 Params.push_back(getValue(*It++, args[i]));
255 if (It != PL.end()) return true;
257 if (Raw.NumOperands > 2) {
258 std::vector<unsigned> &args = *Raw.VarArgs;
259 if (args.size() < 1) return true;
261 if ((args.size() & 1) != 0)
262 return true; // Must be pairs of type/value
263 for (unsigned i = 0; i < args.size(); i+=2) {
264 const Type *Ty = getType(args[i]);
268 Value *V = getValue(Ty, args[i+1]);
269 if (V == 0) return true;
276 Res = new CallInst(M, Params);
279 case Instruction::Invoke: {
280 Value *M = getValue(Raw.Ty, Raw.Arg1);
281 if (M == 0) return true;
283 // Check to make sure we have a pointer to method type
284 const PointerType *PTy = dyn_cast<PointerType>(M->getType());
285 if (PTy == 0) return true;
286 const FunctionType *MTy = dyn_cast<FunctionType>(PTy->getElementType());
287 if (MTy == 0) return true;
289 std::vector<Value *> Params;
290 const FunctionType::ParamTypes &PL = MTy->getParamTypes();
291 std::vector<unsigned> &args = *Raw.VarArgs;
293 BasicBlock *Normal, *Except;
295 if (!MTy->isVarArg()) {
296 if (Raw.NumOperands < 3) return true;
298 Normal = cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2));
299 Except = cast<BasicBlock>(getValue(Type::LabelTy, args[0]));
301 FunctionType::ParamTypes::const_iterator It = PL.begin();
302 for (unsigned i = 1; i < args.size(); i++) {
303 if (It == PL.end()) return true;
304 // TODO: Check getValue for null!
305 Params.push_back(getValue(*It++, args[i]));
308 if (It != PL.end()) return true;
310 if (args.size() < 4) return true;
312 Normal = cast<BasicBlock>(getValue(Type::LabelTy, args[0]));
313 Except = cast<BasicBlock>(getValue(Type::LabelTy, args[2]));
315 if ((args.size() & 1) != 0)
316 return true; // Must be pairs of type/value
317 for (unsigned i = 4; i < args.size(); i+=2) {
318 // TODO: Check getValue for null!
319 Params.push_back(getValue(getType(args[i]), args[i+1]));
324 Res = new InvokeInst(M, Normal, Except, Params);
327 case Instruction::Malloc:
328 if (Raw.NumOperands > 2) return true;
329 V = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0;
330 if (const PointerType *PTy = dyn_cast<PointerType>(Raw.Ty))
331 Res = new MallocInst(PTy->getElementType(), V);
336 case Instruction::Alloca:
337 if (Raw.NumOperands > 2) return true;
338 V = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0;
339 if (const PointerType *PTy = dyn_cast<PointerType>(Raw.Ty))
340 Res = new AllocaInst(PTy->getElementType(), V);
345 case Instruction::Free:
346 V = getValue(Raw.Ty, Raw.Arg1);
347 if (!isa<PointerType>(V->getType())) return true;
348 Res = new FreeInst(V);
351 case Instruction::Load:
352 case Instruction::GetElementPtr: {
353 std::vector<Value*> Idx;
354 if (!isa<PointerType>(Raw.Ty)) return true;
355 const CompositeType *TopTy = dyn_cast<CompositeType>(Raw.Ty);
357 switch (Raw.NumOperands) {
358 case 0: std::cerr << "Invalid load encountered!\n"; return true;
361 if (!TopTy) return true;
362 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
366 if (!TopTy) return true;
367 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
370 const Type *ETy = GetElementPtrInst::getIndexedType(TopTy, Idx, true);
371 const CompositeType *ElTy = dyn_cast_or_null<CompositeType>(ETy);
372 if (!ElTy) return true;
374 Idx.push_back(V = getValue(ElTy->getIndexType(), Raw.Arg3));
379 if (!TopTy) return true;
380 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
383 std::vector<unsigned> &args = *Raw.VarArgs;
384 for (unsigned i = 0, E = args.size(); i != E; ++i) {
385 const Type *ETy = GetElementPtrInst::getIndexedType(Raw.Ty, Idx, true);
386 const CompositeType *ElTy = dyn_cast_or_null<CompositeType>(ETy);
387 if (!ElTy) return true;
388 Idx.push_back(V = getValue(ElTy->getIndexType(), args[i]));
395 if (Raw.Opcode == Instruction::Load) {
396 Value *Src = getValue(Raw.Ty, Raw.Arg1);
398 std::cerr << "WARNING: Bytecode contains load instruction with indices."
399 << " Replacing with getelementptr/load pair\n";
400 assert(GetElementPtrInst::getIndexedType(Raw.Ty, Idx) &&
401 "Bad indices for Load!");
402 Src = new GetElementPtrInst(Src, Idx);
403 // FIXME: Remove this compatibility code and the BB parameter to this
405 BB->getInstList().push_back(cast<Instruction>(Src));
407 Res = new LoadInst(Src);
408 } else if (Raw.Opcode == Instruction::GetElementPtr)
409 Res = new GetElementPtrInst(getValue(Raw.Ty, Raw.Arg1), Idx);
414 case Instruction::Store: {
415 std::vector<Value*> Idx;
416 if (!isa<PointerType>(Raw.Ty)) return true;
417 const CompositeType *TopTy = dyn_cast<CompositeType>(Raw.Ty);
419 switch (Raw.NumOperands) {
421 case 1: std::cerr << "Invalid store encountered!\n"; return true;
424 if (!TopTy) return true;
425 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg3));
429 std::vector<unsigned> &args = *Raw.VarArgs;
430 const CompositeType *ElTy = TopTy;
432 for (i = 0, E = args.size(); ElTy && i != E; ++i) {
433 Idx.push_back(V = getValue(ElTy->getIndexType(), args[i]));
436 const Type *ETy = GetElementPtrInst::getIndexedType(Raw.Ty, Idx, true);
437 ElTy = dyn_cast_or_null<CompositeType>(ETy);
440 return true; // didn't use up all of the indices!
446 Value *Ptr = getValue(Raw.Ty, Raw.Arg2);
448 std::cerr << "WARNING: Bytecode contains load instruction with indices. "
449 << "Replacing with getelementptr/load pair\n";
451 const Type *ElType = GetElementPtrInst::getIndexedType(Raw.Ty, Idx);
452 if (ElType == 0) return true;
454 Ptr = new GetElementPtrInst(Ptr, Idx);
455 // FIXME: Remove this compatibility code and the BB parameter to this
457 BB->getInstList().push_back(cast<Instruction>(Ptr));
460 const Type *ValTy = cast<PointerType>(Ptr->getType())->getElementType();
461 Res = new StoreInst(getValue(ValTy, Raw.Arg1), Ptr);
464 } // end switch(Raw.Opcode)
466 std::cerr << "Unrecognized instruction! " << Raw.Opcode
467 << " ADDR = 0x" << (void*)Buf << "\n";