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,
121 if (ParseRawInst(Buf, EndBuf, Raw))
124 if (Raw.Opcode >= Instruction::BinaryOpsBegin &&
125 Raw.Opcode < Instruction::BinaryOpsEnd && Raw.NumOperands == 2) {
126 Res = BinaryOperator::create((Instruction::BinaryOps)Raw.Opcode,
127 getValue(Raw.Ty, Raw.Arg1),
128 getValue(Raw.Ty, Raw.Arg2));
133 switch (Raw.Opcode) {
134 case Instruction::VarArg:
135 case Instruction::Cast: {
136 V = getValue(Raw.Ty, Raw.Arg1);
137 const Type *Ty = getType(Raw.Arg2);
138 if (V == 0 || Ty == 0) { std::cerr << "Invalid cast!\n"; return true; }
139 if (Raw.Opcode == Instruction::Cast)
140 Res = new CastInst(V, Ty);
142 Res = new VarArgInst(V, Ty);
145 case Instruction::PHINode: {
146 PHINode *PN = new PHINode(Raw.Ty);
147 switch (Raw.NumOperands) {
150 case 3: std::cerr << "Invalid phi node encountered!\n";
153 case 2: PN->addIncoming(getValue(Raw.Ty, Raw.Arg1),
154 cast<BasicBlock>(getValue(Type::LabelTy,Raw.Arg2)));
157 PN->addIncoming(getValue(Raw.Ty, Raw.Arg1),
158 cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2)));
159 if (Raw.VarArgs->size() & 1) {
160 std::cerr << "PHI Node with ODD number of arguments!\n";
164 std::vector<unsigned> &args = *Raw.VarArgs;
165 for (unsigned i = 0; i < args.size(); i+=2)
166 PN->addIncoming(getValue(Raw.Ty, args[i]),
167 cast<BasicBlock>(getValue(Type::LabelTy, args[i+1])));
176 case Instruction::Shl:
177 case Instruction::Shr:
178 Res = new ShiftInst((Instruction::OtherOps)Raw.Opcode,
179 getValue(Raw.Ty, Raw.Arg1),
180 getValue(Type::UByteTy, Raw.Arg2));
182 case Instruction::Ret:
183 if (Raw.NumOperands == 0) {
184 Res = new ReturnInst(); return false;
185 } else if (Raw.NumOperands == 1) {
186 Res = new ReturnInst(getValue(Raw.Ty, Raw.Arg1)); return false;
190 case Instruction::Br:
191 if (Raw.NumOperands == 1) {
192 Res = new BranchInst(cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg1)));
194 } else if (Raw.NumOperands == 3) {
195 Res = new BranchInst(cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg1)),
196 cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2)),
197 getValue(Type::BoolTy , Raw.Arg3));
202 case Instruction::Switch: {
204 new SwitchInst(getValue(Raw.Ty, Raw.Arg1),
205 cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2)));
207 if (Raw.NumOperands < 3) return false; // No destinations? Wierd.
209 if (Raw.NumOperands == 3 || Raw.VarArgs->size() & 1) {
210 std::cerr << "Switch statement with odd number of arguments!\n";
215 std::vector<unsigned> &args = *Raw.VarArgs;
216 for (unsigned i = 0; i < args.size(); i += 2)
217 I->addCase(cast<Constant>(getValue(Raw.Ty, args[i])),
218 cast<BasicBlock>(getValue(Type::LabelTy, args[i+1])));
224 case Instruction::Call: {
225 Value *F = getValue(Raw.Ty, Raw.Arg1);
226 if (F == 0) return true;
228 // Check to make sure we have a pointer to method type
229 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
230 if (PTy == 0) return true;
231 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
232 if (FTy == 0) return true;
234 std::vector<Value *> Params;
235 const FunctionType::ParamTypes &PL = FTy->getParamTypes();
237 if (!FTy->isVarArg()) {
238 FunctionType::ParamTypes::const_iterator It = PL.begin();
240 switch (Raw.NumOperands) {
241 case 0: std::cerr << "Invalid call instruction encountered!\n";
244 case 2: Params.push_back(getValue(*It++, Raw.Arg2)); break;
245 case 3: Params.push_back(getValue(*It++, Raw.Arg2));
246 if (It == PL.end()) return true;
247 Params.push_back(getValue(*It++, Raw.Arg3)); break;
249 Params.push_back(getValue(*It++, Raw.Arg2));
251 std::vector<unsigned> &args = *Raw.VarArgs;
252 for (unsigned i = 0; i < args.size(); i++) {
253 if (It == PL.end()) return true;
254 Params.push_back(getValue(*It++, args[i]));
255 if (Params.back() == 0) return true;
260 if (It != PL.end()) return true;
262 if (Raw.NumOperands > 2) {
263 std::vector<unsigned> &args = *Raw.VarArgs;
264 if (args.size() < 1) return true;
266 if ((args.size() & 1) != 0)
267 return true; // Must be pairs of type/value
268 for (unsigned i = 0; i < args.size(); i+=2) {
269 const Type *Ty = getType(args[i]);
273 Value *V = getValue(Ty, args[i+1]);
274 if (V == 0) return true;
281 Res = new CallInst(F, Params);
284 case Instruction::Invoke: {
285 Value *F = getValue(Raw.Ty, Raw.Arg1);
286 if (F == 0) return true;
288 // Check to make sure we have a pointer to method type
289 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
290 if (PTy == 0) return true;
291 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
292 if (FTy == 0) return true;
294 std::vector<Value *> Params;
295 const FunctionType::ParamTypes &PL = FTy->getParamTypes();
296 std::vector<unsigned> &args = *Raw.VarArgs;
298 BasicBlock *Normal, *Except;
300 if (!FTy->isVarArg()) {
301 if (Raw.NumOperands < 3) return true;
303 Normal = cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2));
304 if (Raw.NumOperands == 3)
305 Except = cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg3));
307 Except = cast<BasicBlock>(getValue(Type::LabelTy, args[0]));
309 FunctionType::ParamTypes::const_iterator It = PL.begin();
310 for (unsigned i = 1; i < args.size(); i++) {
311 if (It == PL.end()) return true;
312 Params.push_back(getValue(*It++, args[i]));
313 if (Params.back() == 0) return true;
315 if (It != PL.end()) return true;
318 if (args.size() < 4) return true;
319 if (getType(args[0]) != Type::LabelTy ||
320 getType(args[2]) != Type::LabelTy) return true;
321 Normal = cast<BasicBlock>(getValue(Type::LabelTy, args[1]));
322 Except = cast<BasicBlock>(getValue(Type::LabelTy, args[3]));
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 Params.push_back(getValue(getType(args[i]), args[i+1]));
328 if (Params.back() == 0) return true;
332 if (Raw.NumOperands > 3)
334 Res = new InvokeInst(F, 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::GetElementPtr: {
362 std::vector<Value*> Idx;
363 if (!isa<PointerType>(Raw.Ty)) return true;
364 const CompositeType *TopTy = dyn_cast<CompositeType>(Raw.Ty);
366 switch (Raw.NumOperands) {
367 case 0: std::cerr << "Invalid getelementptr encountered!\n"; return true;
370 if (!TopTy) return true;
371 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
375 if (!TopTy) return true;
376 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
379 const Type *ETy = GetElementPtrInst::getIndexedType(TopTy, Idx, true);
380 const CompositeType *ElTy = dyn_cast_or_null<CompositeType>(ETy);
381 if (!ElTy) return true;
383 Idx.push_back(V = getValue(ElTy->getIndexType(), Raw.Arg3));
388 if (!TopTy) return true;
389 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
392 std::vector<unsigned> &args = *Raw.VarArgs;
393 for (unsigned i = 0, E = args.size(); i != E; ++i) {
394 const Type *ETy = GetElementPtrInst::getIndexedType(Raw.Ty, Idx, true);
395 const CompositeType *ElTy = dyn_cast_or_null<CompositeType>(ETy);
396 if (!ElTy) return true;
397 Idx.push_back(V = getValue(ElTy->getIndexType(), args[i]));
404 Res = new GetElementPtrInst(getValue(Raw.Ty, Raw.Arg1), Idx);
408 case Instruction::Load:
409 if (Raw.NumOperands != 1) return true;
410 if (!isa<PointerType>(Raw.Ty)) return true;
411 Res = new LoadInst(getValue(Raw.Ty, Raw.Arg1));
414 case Instruction::Store: {
415 if (!isa<PointerType>(Raw.Ty) || Raw.NumOperands != 2) return true;
417 Value *Ptr = getValue(Raw.Ty, Raw.Arg2);
418 const Type *ValTy = cast<PointerType>(Ptr->getType())->getElementType();
419 Res = new StoreInst(getValue(ValTy, Raw.Arg1), Ptr);
422 } // end switch(Raw.Opcode)
424 std::cerr << "Unrecognized instruction! " << Raw.Opcode
425 << " ADDR = 0x" << (void*)Buf << "\n";