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"
23 bool BytecodeParser::ParseRawInst(const uchar *&Buf, const uchar *EndBuf,
26 if (read(Buf, EndBuf, Op)) return failure(true);
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)) return failure(true);
75 if (read_vbr(Buf, EndBuf, Typ)) return failure(true);
76 Result.Ty = getType(Typ);
77 if (Result.Ty == 0) return failure(true);
78 if (read_vbr(Buf, EndBuf, Result.NumOperands)) return failure(true);
80 switch (Result.NumOperands) {
82 cerr << "Zero Arg instr found!\n";
83 return failure(true); // This encoding is invalid!
85 if (read_vbr(Buf, EndBuf, Result.Arg1)) return failure(true);
88 if (read_vbr(Buf, EndBuf, Result.Arg1) ||
89 read_vbr(Buf, EndBuf, Result.Arg2)) return failure(true);
92 if (read_vbr(Buf, EndBuf, Result.Arg1) ||
93 read_vbr(Buf, EndBuf, Result.Arg2) ||
94 read_vbr(Buf, EndBuf, Result.Arg3)) return failure(true);
97 if (read_vbr(Buf, EndBuf, Result.Arg1) ||
98 read_vbr(Buf, EndBuf, Result.Arg2)) return failure(true);
100 // Allocate a vector to hold arguments 3, 4, 5, 6 ...
101 Result.VarArgs = new vector<unsigned>(Result.NumOperands-2);
102 for (unsigned a = 0; a < Result.NumOperands-2; a++)
103 if (read_vbr(Buf, EndBuf, (*Result.VarArgs)[a])) return failure(true);
106 if (align32(Buf, EndBuf)) return failure(true);
111 cerr << "NO: " << Result.NumOperands << " opcode: " << Result.Opcode
112 << " Ty: " << Result.Ty->getDescription() << " arg1: " << Result.Arg1
113 << " arg2: " << Result.Arg2 << " arg3: " << Result.Arg3 << "\n";
119 bool BytecodeParser::ParseInstruction(const uchar *&Buf, const uchar *EndBuf,
122 if (ParseRawInst(Buf, EndBuf, Raw))
123 return failure(true);
125 if (Raw.Opcode >= Instruction::FirstUnaryOp &&
126 Raw.Opcode < Instruction::NumUnaryOps && Raw.NumOperands == 1) {
127 Res = UnaryOperator::create((Instruction::UnaryOps)Raw.Opcode,
128 getValue(Raw.Ty,Raw.Arg1));
130 } else if (Raw.Opcode >= Instruction::FirstBinaryOp &&
131 Raw.Opcode < Instruction::NumBinaryOps && Raw.NumOperands == 2) {
132 Res = BinaryOperator::create((Instruction::BinaryOps)Raw.Opcode,
133 getValue(Raw.Ty, Raw.Arg1),
134 getValue(Raw.Ty, Raw.Arg2));
139 switch (Raw.Opcode) {
140 case Instruction::Cast: {
141 V = getValue(Raw.Ty, Raw.Arg1);
142 const Type *Ty = getType(Raw.Arg2);
143 if (V == 0 || Ty == 0) { cerr << "Invalid cast!\n"; return true; }
144 Res = new CastInst(V, Ty);
147 case Instruction::PHINode: {
148 PHINode *PN = new PHINode(Raw.Ty);
149 switch (Raw.NumOperands) {
152 case 3: cerr << "Invalid phi node encountered!\n";
154 return failure(true);
155 case 2: PN->addIncoming(getValue(Raw.Ty, Raw.Arg1),
156 cast<BasicBlock>(getValue(Type::LabelTy,Raw.Arg2)));
159 PN->addIncoming(getValue(Raw.Ty, Raw.Arg1),
160 cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2)));
161 if (Raw.VarArgs->size() & 1) {
162 cerr << "PHI Node with ODD number of arguments!\n";
164 return failure(true);
166 vector<unsigned> &args = *Raw.VarArgs;
167 for (unsigned i = 0; i < args.size(); i+=2)
168 PN->addIncoming(getValue(Raw.Ty, args[i]),
169 cast<BasicBlock>(getValue(Type::LabelTy, args[i+1])));
178 case Instruction::Shl:
179 case Instruction::Shr:
180 Res = new ShiftInst((Instruction::OtherOps)Raw.Opcode,
181 getValue(Raw.Ty, Raw.Arg1),
182 getValue(Type::UByteTy, Raw.Arg2));
184 case Instruction::Ret:
185 if (Raw.NumOperands == 0) {
186 Res = new ReturnInst(); return false;
187 } else if (Raw.NumOperands == 1) {
188 Res = new ReturnInst(getValue(Raw.Ty, Raw.Arg1)); return false;
192 case Instruction::Br:
193 if (Raw.NumOperands == 1) {
194 Res = new BranchInst(cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg1)));
196 } else if (Raw.NumOperands == 3) {
197 Res = new BranchInst(cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg1)),
198 cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2)),
199 getValue(Type::BoolTy , Raw.Arg3));
204 case Instruction::Switch: {
206 new SwitchInst(getValue(Raw.Ty, Raw.Arg1),
207 cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2)));
209 if (Raw.NumOperands < 3) return false; // No destinations? Wierd.
211 if (Raw.NumOperands == 3 || Raw.VarArgs->size() & 1) {
212 cerr << "Switch statement with odd number of arguments!\n";
214 return failure(true);
217 vector<unsigned> &args = *Raw.VarArgs;
218 for (unsigned i = 0; i < args.size(); i += 2)
219 I->dest_push_back(cast<Constant>(getValue(Raw.Ty, args[i])),
220 cast<BasicBlock>(getValue(Type::LabelTy, args[i+1])));
226 case Instruction::Call: {
227 Value *M = getValue(Raw.Ty, Raw.Arg1);
228 if (M == 0) return failure(true);
230 // Check to make sure we have a pointer to method type
231 PointerType *PTy = dyn_cast<PointerType>(M->getType());
232 if (PTy == 0) return failure(true);
233 MethodType *MTy = dyn_cast<MethodType>(PTy->getElementType());
234 if (MTy == 0) return failure(true);
236 vector<Value *> Params;
237 const MethodType::ParamTypes &PL = MTy->getParamTypes();
239 if (!MTy->isVarArg()) {
240 MethodType::ParamTypes::const_iterator It = PL.begin();
242 switch (Raw.NumOperands) {
243 case 0: cerr << "Invalid call instruction encountered!\n";
244 return failure(true);
246 case 2: Params.push_back(getValue(*It++, Raw.Arg2)); break;
247 case 3: Params.push_back(getValue(*It++, Raw.Arg2));
248 if (It == PL.end()) return failure(true);
249 Params.push_back(getValue(*It++, Raw.Arg3)); break;
251 Params.push_back(getValue(*It++, Raw.Arg2));
253 vector<unsigned> &args = *Raw.VarArgs;
254 for (unsigned i = 0; i < args.size(); i++) {
255 if (It == PL.end()) return failure(true);
256 // TODO: Check getValue for null!
257 Params.push_back(getValue(*It++, args[i]));
262 if (It != PL.end()) return failure(true);
264 if (Raw.NumOperands > 2) {
265 vector<unsigned> &args = *Raw.VarArgs;
266 if (args.size() < 1) return failure(true);
268 if ((args.size() & 1) != 0)
269 return failure(true); // Must be pairs of type/value
270 for (unsigned i = 0; i < args.size(); i+=2) {
271 const Type *Ty = getType(args[i]);
273 return failure(true);
275 Value *V = getValue(Ty, args[i+1]);
276 if (V == 0) return failure(true);
283 Res = new CallInst(M, Params);
286 case Instruction::Invoke: {
287 Value *M = getValue(Raw.Ty, Raw.Arg1);
288 if (M == 0) return failure(true);
290 // Check to make sure we have a pointer to method type
291 PointerType *PTy = dyn_cast<PointerType>(M->getType());
292 if (PTy == 0) return failure(true);
293 MethodType *MTy = dyn_cast<MethodType>(PTy->getElementType());
294 if (MTy == 0) return failure(true);
296 vector<Value *> Params;
297 const MethodType::ParamTypes &PL = MTy->getParamTypes();
298 vector<unsigned> &args = *Raw.VarArgs;
300 BasicBlock *Normal, *Except;
302 if (!MTy->isVarArg()) {
303 if (Raw.NumOperands < 3) return failure(true);
305 Normal = cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2));
306 Except = cast<BasicBlock>(getValue(Type::LabelTy, args[0]));
308 MethodType::ParamTypes::const_iterator It = PL.begin();
309 for (unsigned i = 1; i < args.size(); i++) {
310 if (It == PL.end()) return failure(true);
311 // TODO: Check getValue for null!
312 Params.push_back(getValue(*It++, args[i]));
315 if (It != PL.end()) return failure(true);
317 if (args.size() < 4) return failure(true);
319 Normal = cast<BasicBlock>(getValue(Type::LabelTy, args[0]));
320 Except = cast<BasicBlock>(getValue(Type::LabelTy, args[2]));
322 if ((args.size() & 1) != 0)
323 return failure(true); // Must be pairs of type/value
324 for (unsigned i = 4; i < args.size(); i+=2) {
325 // TODO: Check getValue for null!
326 Params.push_back(getValue(getType(args[i]), args[i+1]));
331 Res = new InvokeInst(M, Normal, Except, Params);
334 case Instruction::Malloc:
335 if (Raw.NumOperands > 2) return failure(true);
336 V = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0;
337 Res = new MallocInst(Raw.Ty, V);
340 case Instruction::Alloca:
341 if (Raw.NumOperands > 2) return failure(true);
342 V = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0;
343 Res = new AllocaInst(Raw.Ty, V);
346 case Instruction::Free:
347 V = getValue(Raw.Ty, Raw.Arg1);
348 if (!V->getType()->isPointerType()) return failure(true);
349 Res = new FreeInst(V);
352 case Instruction::Load:
353 case Instruction::GetElementPtr: {
355 if (!isa<PointerType>(Raw.Ty)) return failure(true);
356 const CompositeType *TopTy = dyn_cast<CompositeType>(Raw.Ty);
358 switch (Raw.NumOperands) {
359 case 0: cerr << "Invalid load encountered!\n"; return failure(true);
362 if (!TopTy) return failure(true);
363 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
364 if (!V) return failure(true);
367 if (!TopTy) return failure(true);
368 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
369 if (!V) return failure(true);
371 const Type *ETy = MemAccessInst::getIndexedType(TopTy, Idx, true);
372 const CompositeType *ElTy = dyn_cast_or_null<CompositeType>(ETy);
373 if (!ElTy) return failure(true);
375 Idx.push_back(V = getValue(ElTy->getIndexType(), Raw.Arg3));
376 if (!V) return failure(true);
380 if (!TopTy) return failure(true);
381 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
382 if (!V) return failure(true);
384 vector<unsigned> &args = *Raw.VarArgs;
385 for (unsigned i = 0, E = args.size(); i != E; ++i) {
386 const Type *ETy = MemAccessInst::getIndexedType(Raw.Ty, Idx, true);
387 const CompositeType *ElTy = dyn_cast_or_null<CompositeType>(ETy);
388 if (!ElTy) return failure(true);
389 Idx.push_back(V = getValue(ElTy->getIndexType(), args[i]));
390 if (!V) return failure(true);
396 if (Raw.Opcode == Instruction::Load) {
397 assert(MemAccessInst::getIndexedType(Raw.Ty, Idx) &&
398 "Bad indices for Load!");
399 Res = new LoadInst(getValue(Raw.Ty, Raw.Arg1), Idx);
400 } else if (Raw.Opcode == Instruction::GetElementPtr)
401 Res = new GetElementPtrInst(getValue(Raw.Ty, Raw.Arg1), Idx);
406 case Instruction::Store: {
408 if (!isa<PointerType>(Raw.Ty)) return failure(true);
409 const CompositeType *TopTy = dyn_cast<CompositeType>(Raw.Ty);
411 switch (Raw.NumOperands) {
413 case 1: cerr << "Invalid store encountered!\n"; return failure(true);
416 if (!TopTy) return failure(true);
417 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg3));
418 if (!V) return failure(true);
421 vector<unsigned> &args = *Raw.VarArgs;
422 const CompositeType *ElTy = TopTy;
424 for (i = 0, E = args.size(); ElTy && i != E; ++i) {
425 Idx.push_back(V = getValue(ElTy->getIndexType(), args[i]));
426 if (!V) return failure(true);
428 const Type *ETy = MemAccessInst::getIndexedType(Raw.Ty, Idx, true);
429 ElTy = dyn_cast_or_null<CompositeType>(ETy);
432 return failure(true); // didn't use up all of the indices!
438 const Type *ElType = StoreInst::getIndexedType(Raw.Ty, Idx);
439 if (ElType == 0) return failure(true);
440 Res = new StoreInst(getValue(ElType, Raw.Arg1), getValue(Raw.Ty, Raw.Arg2),
444 } // end switch(Raw.Opcode)
446 cerr << "Unrecognized instruction! " << Raw.Opcode
447 << " ADDR = 0x" << (void*)Buf << "\n";
448 return failure(true);