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 unsigned char *&Buf,
21 const unsigned char *EndBuf,
24 if (read(Buf, EndBuf, Op)) return true;
26 // bits Instruction format: Common to all formats
27 // --------------------------
28 // 01-00: Opcode type, fixed to 1.
30 Result.NumOperands = (Op >> 0) & 03;
31 Result.Opcode = (Op >> 2) & 63;
33 switch (Result.NumOperands) {
35 // bits Instruction format:
36 // --------------------------
37 // 19-08: Resulting type plane
38 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
40 Result.Ty = getType((Op >> 8) & 4095);
41 Result.Arg1 = (Op >> 20) & 4095;
42 if (Result.Arg1 == 4095) // Handle special encoding for 0 operands...
43 Result.NumOperands = 0;
46 // bits Instruction format:
47 // --------------------------
48 // 15-08: Resulting type plane
52 Result.Ty = getType((Op >> 8) & 255);
53 Result.Arg1 = (Op >> 16) & 255;
54 Result.Arg2 = (Op >> 24) & 255;
57 // bits Instruction format:
58 // --------------------------
59 // 13-08: Resulting type plane
64 Result.Ty = getType((Op >> 8) & 63);
65 Result.Arg1 = (Op >> 14) & 63;
66 Result.Arg2 = (Op >> 20) & 63;
67 Result.Arg3 = (Op >> 26) & 63;
70 Buf -= 4; // Hrm, try this again...
71 if (read_vbr(Buf, EndBuf, Result.Opcode)) return true;
73 if (read_vbr(Buf, EndBuf, Typ)) return true;
74 Result.Ty = getType(Typ);
75 if (Result.Ty == 0) return true;
76 if (read_vbr(Buf, EndBuf, Result.NumOperands)) return true;
78 switch (Result.NumOperands) {
80 std::cerr << "Zero Arg instr found!\n";
81 return true; // This encoding is invalid!
83 if (read_vbr(Buf, EndBuf, Result.Arg1)) return true;
86 if (read_vbr(Buf, EndBuf, Result.Arg1) ||
87 read_vbr(Buf, EndBuf, Result.Arg2)) return true;
90 if (read_vbr(Buf, EndBuf, Result.Arg1) ||
91 read_vbr(Buf, EndBuf, Result.Arg2) ||
92 read_vbr(Buf, EndBuf, Result.Arg3)) return true;
95 if (read_vbr(Buf, EndBuf, Result.Arg1) ||
96 read_vbr(Buf, EndBuf, Result.Arg2)) return true;
98 // Allocate a vector to hold arguments 3, 4, 5, 6 ...
99 Result.VarArgs = new std::vector<unsigned>(Result.NumOperands-2);
100 for (unsigned a = 0; a < Result.NumOperands-2; a++)
101 if (read_vbr(Buf, EndBuf, (*Result.VarArgs)[a])) return true;
104 if (align32(Buf, EndBuf)) return true;
109 std::cerr << "NO: " << Result.NumOperands << " opcode: " << Result.Opcode
110 << " Ty: " << Result.Ty->getDescription() << " arg1: "<< Result.Arg1
111 << " arg2: " << Result.Arg2 << " arg3: " << Result.Arg3 << "\n";
117 bool BytecodeParser::ParseInstruction(const unsigned char *&Buf,
118 const unsigned char *EndBuf,
120 BasicBlock *BB /*HACK*/) {
122 if (ParseRawInst(Buf, EndBuf, Raw))
125 if (Raw.Opcode >= Instruction::BinaryOpsBegin &&
126 Raw.Opcode < Instruction::BinaryOpsEnd && Raw.NumOperands == 2) {
127 Res = BinaryOperator::create((Instruction::BinaryOps)Raw.Opcode,
128 getValue(Raw.Ty, Raw.Arg1),
129 getValue(Raw.Ty, Raw.Arg2));
134 switch (Raw.Opcode) {
135 case Instruction::VarArg:
136 case Instruction::Cast: {
137 V = getValue(Raw.Ty, Raw.Arg1);
138 const Type *Ty = getType(Raw.Arg2);
139 if (V == 0 || Ty == 0) { std::cerr << "Invalid cast!\n"; return true; }
140 if (Raw.Opcode == Instruction::Cast)
141 Res = new CastInst(V, Ty);
143 Res = new VarArgInst(V, Ty);
146 case Instruction::PHINode: {
147 PHINode *PN = new PHINode(Raw.Ty);
148 switch (Raw.NumOperands) {
151 case 3: std::cerr << "Invalid phi node encountered!\n";
154 case 2: PN->addIncoming(getValue(Raw.Ty, Raw.Arg1),
155 cast<BasicBlock>(getValue(Type::LabelTy,Raw.Arg2)));
158 PN->addIncoming(getValue(Raw.Ty, Raw.Arg1),
159 cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2)));
160 if (Raw.VarArgs->size() & 1) {
161 std::cerr << "PHI Node with ODD number of arguments!\n";
165 std::vector<unsigned> &args = *Raw.VarArgs;
166 for (unsigned i = 0; i < args.size(); i+=2)
167 PN->addIncoming(getValue(Raw.Ty, args[i]),
168 cast<BasicBlock>(getValue(Type::LabelTy, args[i+1])));
177 case Instruction::Shl:
178 case Instruction::Shr:
179 Res = new ShiftInst((Instruction::OtherOps)Raw.Opcode,
180 getValue(Raw.Ty, Raw.Arg1),
181 getValue(Type::UByteTy, Raw.Arg2));
183 case Instruction::Ret:
184 if (Raw.NumOperands == 0) {
185 Res = new ReturnInst(); return false;
186 } else if (Raw.NumOperands == 1) {
187 Res = new ReturnInst(getValue(Raw.Ty, Raw.Arg1)); return false;
191 case Instruction::Br:
192 if (Raw.NumOperands == 1) {
193 Res = new BranchInst(cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg1)));
195 } else if (Raw.NumOperands == 3) {
196 Res = new BranchInst(cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg1)),
197 cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2)),
198 getValue(Type::BoolTy , Raw.Arg3));
203 case Instruction::Switch: {
205 new SwitchInst(getValue(Raw.Ty, Raw.Arg1),
206 cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2)));
208 if (Raw.NumOperands < 3) return false; // No destinations? Wierd.
210 if (Raw.NumOperands == 3 || Raw.VarArgs->size() & 1) {
211 std::cerr << "Switch statement with odd number of arguments!\n";
216 std::vector<unsigned> &args = *Raw.VarArgs;
217 for (unsigned i = 0; i < args.size(); i += 2)
218 I->dest_push_back(cast<Constant>(getValue(Raw.Ty, args[i])),
219 cast<BasicBlock>(getValue(Type::LabelTy, args[i+1])));
225 case Instruction::Call: {
226 Value *M = getValue(Raw.Ty, Raw.Arg1);
227 if (M == 0) return true;
229 // Check to make sure we have a pointer to method type
230 const PointerType *PTy = dyn_cast<PointerType>(M->getType());
231 if (PTy == 0) return true;
232 const FunctionType *MTy = dyn_cast<FunctionType>(PTy->getElementType());
233 if (MTy == 0) return true;
235 std::vector<Value *> Params;
236 const FunctionType::ParamTypes &PL = MTy->getParamTypes();
238 if (!MTy->isVarArg()) {
239 FunctionType::ParamTypes::const_iterator It = PL.begin();
241 switch (Raw.NumOperands) {
242 case 0: std::cerr << "Invalid call instruction encountered!\n";
245 case 2: Params.push_back(getValue(*It++, Raw.Arg2)); break;
246 case 3: Params.push_back(getValue(*It++, Raw.Arg2));
247 if (It == PL.end()) return true;
248 Params.push_back(getValue(*It++, Raw.Arg3)); break;
250 Params.push_back(getValue(*It++, Raw.Arg2));
252 std::vector<unsigned> &args = *Raw.VarArgs;
253 for (unsigned i = 0; i < args.size(); i++) {
254 if (It == PL.end()) return true;
255 // TODO: Check getValue for null!
256 Params.push_back(getValue(*It++, args[i]));
261 if (It != PL.end()) return true;
263 if (Raw.NumOperands > 2) {
264 std::vector<unsigned> &args = *Raw.VarArgs;
265 if (args.size() < 1) return true;
267 if ((args.size() & 1) != 0)
268 return true; // Must be pairs of type/value
269 for (unsigned i = 0; i < args.size(); i+=2) {
270 const Type *Ty = getType(args[i]);
274 Value *V = getValue(Ty, args[i+1]);
275 if (V == 0) return true;
282 Res = new CallInst(M, Params);
285 case Instruction::Invoke: {
286 Value *M = getValue(Raw.Ty, Raw.Arg1);
287 if (M == 0) return true;
289 // Check to make sure we have a pointer to method type
290 const PointerType *PTy = dyn_cast<PointerType>(M->getType());
291 if (PTy == 0) return true;
292 const FunctionType *MTy = dyn_cast<FunctionType>(PTy->getElementType());
293 if (MTy == 0) return true;
295 std::vector<Value *> Params;
296 const FunctionType::ParamTypes &PL = MTy->getParamTypes();
297 std::vector<unsigned> &args = *Raw.VarArgs;
299 BasicBlock *Normal, *Except;
301 if (!MTy->isVarArg()) {
302 if (Raw.NumOperands < 3) return true;
304 Normal = cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2));
305 if (Raw.NumOperands == 3)
306 Except = cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg3));
308 Except = cast<BasicBlock>(getValue(Type::LabelTy, args[0]));
310 FunctionType::ParamTypes::const_iterator It = PL.begin();
311 for (unsigned i = 1; i < args.size(); i++) {
312 if (It == PL.end()) return true;
313 // TODO: Check getValue for null!
314 Params.push_back(getValue(*It++, args[i]));
316 if (It != PL.end()) return true;
319 if (args.size() < 4) return true;
321 Normal = cast<BasicBlock>(getValue(Type::LabelTy, args[0]));
322 Except = cast<BasicBlock>(getValue(Type::LabelTy, args[2]));
324 if ((args.size() & 1) != 0)
325 return true; // Must be pairs of type/value
326 for (unsigned i = 4; i < args.size(); i+=2) {
327 // TODO: Check getValue for null!
328 Params.push_back(getValue(getType(args[i]), args[i+1]));
332 if (Raw.NumOperands > 3)
334 Res = new InvokeInst(M, Normal, Except, Params);
337 case Instruction::Malloc:
338 if (Raw.NumOperands > 2) return true;
339 V = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0;
340 if (const PointerType *PTy = dyn_cast<PointerType>(Raw.Ty))
341 Res = new MallocInst(PTy->getElementType(), V);
346 case Instruction::Alloca:
347 if (Raw.NumOperands > 2) return true;
348 V = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0;
349 if (const PointerType *PTy = dyn_cast<PointerType>(Raw.Ty))
350 Res = new AllocaInst(PTy->getElementType(), V);
355 case Instruction::Free:
356 V = getValue(Raw.Ty, Raw.Arg1);
357 if (!isa<PointerType>(V->getType())) return true;
358 Res = new FreeInst(V);
361 case Instruction::Load:
362 case Instruction::GetElementPtr: {
363 std::vector<Value*> Idx;
364 if (!isa<PointerType>(Raw.Ty)) return true;
365 const CompositeType *TopTy = dyn_cast<CompositeType>(Raw.Ty);
367 switch (Raw.NumOperands) {
368 case 0: std::cerr << "Invalid load encountered!\n"; return true;
371 if (!TopTy) return true;
372 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
376 if (!TopTy) return true;
377 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
380 const Type *ETy = GetElementPtrInst::getIndexedType(TopTy, Idx, true);
381 const CompositeType *ElTy = dyn_cast_or_null<CompositeType>(ETy);
382 if (!ElTy) return true;
384 Idx.push_back(V = getValue(ElTy->getIndexType(), Raw.Arg3));
389 if (!TopTy) return true;
390 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
393 std::vector<unsigned> &args = *Raw.VarArgs;
394 for (unsigned i = 0, E = args.size(); i != E; ++i) {
395 const Type *ETy = GetElementPtrInst::getIndexedType(Raw.Ty, Idx, true);
396 const CompositeType *ElTy = dyn_cast_or_null<CompositeType>(ETy);
397 if (!ElTy) return true;
398 Idx.push_back(V = getValue(ElTy->getIndexType(), args[i]));
405 if (Raw.Opcode == Instruction::Load) {
406 Value *Src = getValue(Raw.Ty, Raw.Arg1);
408 std::cerr << "WARNING: Bytecode contains load instruction with indices."
409 << " Replacing with getelementptr/load pair\n";
410 assert(GetElementPtrInst::getIndexedType(Raw.Ty, Idx) &&
411 "Bad indices for Load!");
412 Src = new GetElementPtrInst(Src, Idx);
413 // FIXME: Remove this compatibility code and the BB parameter to this
415 BB->getInstList().push_back(cast<Instruction>(Src));
417 Res = new LoadInst(Src);
418 } else if (Raw.Opcode == Instruction::GetElementPtr)
419 Res = new GetElementPtrInst(getValue(Raw.Ty, Raw.Arg1), Idx);
424 case Instruction::Store: {
425 std::vector<Value*> Idx;
426 if (!isa<PointerType>(Raw.Ty)) return true;
427 const CompositeType *TopTy = dyn_cast<CompositeType>(Raw.Ty);
429 switch (Raw.NumOperands) {
431 case 1: std::cerr << "Invalid store encountered!\n"; return true;
434 if (!TopTy) return true;
435 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg3));
439 std::vector<unsigned> &args = *Raw.VarArgs;
440 const CompositeType *ElTy = TopTy;
442 for (i = 0, E = args.size(); ElTy && i != E; ++i) {
443 Idx.push_back(V = getValue(ElTy->getIndexType(), args[i]));
446 const Type *ETy = GetElementPtrInst::getIndexedType(Raw.Ty, Idx, true);
447 ElTy = dyn_cast_or_null<CompositeType>(ETy);
450 return true; // didn't use up all of the indices!
456 Value *Ptr = getValue(Raw.Ty, Raw.Arg2);
458 std::cerr << "WARNING: Bytecode contains load instruction with indices. "
459 << "Replacing with getelementptr/load pair\n";
461 const Type *ElType = GetElementPtrInst::getIndexedType(Raw.Ty, Idx);
462 if (ElType == 0) return true;
464 Ptr = new GetElementPtrInst(Ptr, Idx);
465 // FIXME: Remove this compatibility code and the BB parameter to this
467 BB->getInstList().push_back(cast<Instruction>(Ptr));
470 const Type *ValTy = cast<PointerType>(Ptr->getType())->getElementType();
471 Res = new StoreInst(getValue(ValTy, Raw.Arg1), Ptr);
474 } // end switch(Raw.Opcode)
476 std::cerr << "Unrecognized instruction! " << Raw.Opcode
477 << " ADDR = 0x" << (void*)Buf << "\n";