Stop returning bool and pass Instruction by reference;
[oota-llvm.git] / lib / Bytecode / Reader / InstructionReader.cpp
1 //===- ReadInst.cpp - Code to read an instruction from bytecode -----------===//
2 //
3 // This file defines the mechanism to read an instruction from a bytecode 
4 // stream.
5 //
6 // Note that this library should be as fast as possible, reentrant, and 
7 // threadsafe!!
8 //
9 // TODO: Change from getValue(Raw.Arg1) etc, to getArg(Raw, 1)
10 //       Make it check type, so that casts are checked.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "ReaderInternals.h"
15 #include "llvm/iTerminators.h"
16 #include "llvm/iMemory.h"
17 #include "llvm/iPHINode.h"
18 #include "llvm/iOther.h"
19
20 std::auto_ptr<RawInst>
21 BytecodeParser::ParseRawInst(const unsigned char *&Buf,
22                              const unsigned char *EndBuf) { 
23   unsigned Op, Typ;
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.");
27
28   // bits   Instruction format:        Common to all formats
29   // --------------------------
30   // 01-00: Opcode type, fixed to 1.
31   // 07-02: Opcode
32   Result->NumOperands = (Op >> 0) & 03;
33   Result->Opcode      = (Op >> 2) & 63;
34
35   switch (Result->NumOperands) {
36   case 1:
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)
41     //
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;
46     break;
47   case 2:
48     // bits   Instruction format:
49     // --------------------------
50     // 15-08: Resulting type plane
51     // 23-16: Operand #1
52     // 31-24: Operand #2  
53     //
54     Result->Ty   = getType((Op >> 8) & 255);
55     Result->Arg1 = (Op >> 16) & 255;
56     Result->Arg2 = (Op >> 24) & 255;
57     break;
58   case 3:
59     // bits   Instruction format:
60     // --------------------------
61     // 13-08: Resulting type plane
62     // 19-14: Operand #1
63     // 25-20: Operand #2
64     // 31-26: Operand #3
65     //
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;
70     break;
71   case 0:
72     Buf -= 4;  // Hrm, try this again...
73     if (read_vbr(Buf, EndBuf, Result->Opcode))
74       throw std::string("Error reading from buffer.");
75     Result->Opcode >>= 2;
76     if (read_vbr(Buf, EndBuf, Typ))
77       throw std::string("Error reading from buffer.");
78     Result->Ty = getType(Typ);
79     if (Result->Ty == 0) 
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.");
83
84     switch (Result->NumOperands) {
85     case 0: 
86       throw std::string("Zero-argument instruction found; this is invalid.");
87     case 1: 
88       if (read_vbr(Buf, EndBuf, Result->Arg1)) 
89         throw std::string("Error reading from buffer");
90       break;
91     case 2:
92       if (read_vbr(Buf, EndBuf, Result->Arg1) || 
93           read_vbr(Buf, EndBuf, Result->Arg2))
94         throw std::string("Error reading from buffer");
95       break;
96     case 3:
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");
101       break;
102     default:
103       if (read_vbr(Buf, EndBuf, Result->Arg1) || 
104           read_vbr(Buf, EndBuf, Result->Arg2))
105         throw std::string("Error reading from buffer");
106
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");          
112
113       break;
114     }
115     if (align32(Buf, EndBuf)) 
116       throw std::string("Unaligned bytecode buffer.");
117     break;
118   }
119
120 #if 0
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";
124 #endif
125   return Result;
126 }
127
128
129 bool BytecodeParser::ParseInstruction(const unsigned char *&Buf,
130                                       const unsigned char *EndBuf,
131                                       Instruction *&Res) {
132   std::auto_ptr<RawInst> Raw = ParseRawInst(Buf, EndBuf);
133
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));
139     return false;
140   }
141
142   Value *V;
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);
151     else
152       Res = new VarArgInst(V, Ty);
153     return false;
154   }
155   case Instruction::PHINode: {
156     PHINode *PN = new PHINode(Raw->Ty);
157     switch (Raw->NumOperands) {
158     case 0: 
159     case 1: 
160     case 3: std::cerr << "Invalid phi node encountered!\n"; 
161             delete PN; 
162             return true;
163     case 2: PN->addIncoming(getValue(Raw->Ty, Raw->Arg1),
164                             cast<BasicBlock>(getValue(Type::LabelTy,
165                                                       Raw->Arg2)));
166       break;
167     default:
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";
172         delete PN;
173         return true;
174       } else {
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])));
179       }
180       delete Raw->VarArgs; 
181       break;
182     }
183     Res = PN;
184     return false;
185   }
186
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));
192     return false;
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; 
198     }
199     break;
200
201   case Instruction::Br:
202     if (Raw->NumOperands == 1) {
203       Res = new BranchInst(cast<BasicBlock>(getValue(Type::LabelTy,Raw->Arg1)));
204       return false;
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));
209       return false;
210     }
211     break;
212     
213   case Instruction::Switch: {
214     SwitchInst *I = 
215       new SwitchInst(getValue(Raw->Ty, Raw->Arg1), 
216                      cast<BasicBlock>(getValue(Type::LabelTy, Raw->Arg2)));
217     Res = I;
218     if (Raw->NumOperands < 3) return false;  // No destinations?  Weird.
219
220     if (Raw->NumOperands == 3 || Raw->VarArgs->size() & 1) {
221       std::cerr << "Switch statement with odd number of arguments!\n";
222       delete I;
223       return true;
224     }      
225     
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])));
230
231     delete Raw->VarArgs;
232     return false;
233   }
234
235   case Instruction::Call: {
236     Value *F = getValue(Raw->Ty, Raw->Arg1);
237     if (F == 0) return true;
238
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;
244
245     std::vector<Value *> Params;
246     const FunctionType::ParamTypes &PL = FTy->getParamTypes();
247
248     if (!FTy->isVarArg()) {
249       FunctionType::ParamTypes::const_iterator It = PL.begin();
250
251       switch (Raw->NumOperands) {
252       case 0: std::cerr << "Invalid call instruction encountered!\n";
253         return true;
254       case 1: break;
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;
259       default:
260         Params.push_back(getValue(*It++, Raw->Arg2));
261         {
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;
267           }
268         }
269         delete Raw->VarArgs;
270       }
271       if (It != PL.end()) return true;
272     } else {
273       if (Raw->NumOperands > 2) {
274         std::vector<unsigned> &args = *Raw->VarArgs;
275         if (args.size() < 1) return true;
276
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]);
281           if (Ty == 0)
282             return true;
283           
284           Value *V = getValue(Ty, args[i+1]);
285           if (V == 0) return true;
286           Params.push_back(V);
287         }
288         delete Raw->VarArgs;
289       }
290     }
291
292     Res = new CallInst(F, Params);
293     return false;
294   }
295   case Instruction::Invoke: {
296     Value *F = getValue(Raw->Ty, Raw->Arg1);
297     if (F == 0) return true;
298
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;
304
305     std::vector<Value *> Params;
306     const FunctionType::ParamTypes &PL = FTy->getParamTypes();
307     std::vector<unsigned> &args = *Raw->VarArgs;
308
309     BasicBlock *Normal, *Except;
310
311     if (!FTy->isVarArg()) {
312       if (Raw->NumOperands < 3) return true;
313
314       Normal = cast<BasicBlock>(getValue(Type::LabelTy, Raw->Arg2));
315       if (Raw->NumOperands == 3)
316         Except = cast<BasicBlock>(getValue(Type::LabelTy, Raw->Arg3));
317       else {
318         Except = cast<BasicBlock>(getValue(Type::LabelTy, args[0]));
319
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;
325         }
326         if (It != PL.end()) return true;
327       }
328     } else {
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]));
334
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;
340       }
341     }
342
343     if (Raw->NumOperands > 3)
344       delete Raw->VarArgs;
345     Res = new InvokeInst(F, Normal, Except, Params);
346     return false;
347   }
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);
353     else
354       return true;
355     return false;
356
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);
362     else
363       return true;
364     return false;
365
366   case Instruction::Free:
367     V = getValue(Raw->Ty, Raw->Arg1);
368     if (!isa<PointerType>(V->getType())) return true;
369     Res = new FreeInst(V);
370     return false;
371
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);
376
377     switch (Raw->NumOperands) {
378     case 0: std::cerr << "Invalid getelementptr encountered!\n"; return true;
379     case 1: break;
380     case 2:
381       if (!TopTy) return true;
382       Idx.push_back(V = getValue(TopTy->getIndexType(), Raw->Arg2));
383       if (!V) return true;
384       break;
385     case 3: {
386       if (!TopTy) return true;
387       Idx.push_back(V = getValue(TopTy->getIndexType(), Raw->Arg2));
388       if (!V) return true;
389
390       const Type *ETy = GetElementPtrInst::getIndexedType(TopTy, Idx, true);
391       const CompositeType *ElTy = dyn_cast_or_null<CompositeType>(ETy);
392       if (!ElTy) return true;
393
394       Idx.push_back(V = getValue(ElTy->getIndexType(), Raw->Arg3));
395       if (!V) return true;
396       break;
397     }
398     default:
399       if (!TopTy) return true;
400       Idx.push_back(V = getValue(TopTy->getIndexType(), Raw->Arg2));
401       if (!V) return true;
402
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]));
409         if (!V) return true;
410       }
411       delete Raw->VarArgs; 
412       break;
413     }
414
415     Res = new GetElementPtrInst(getValue(Raw->Ty, Raw->Arg1), Idx);
416     return false;
417   }
418
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);
424     return false;
425
426   case 63:   // volatile store 
427   case Instruction::Store: {
428     if (!isa<PointerType>(Raw->Ty) || Raw->NumOperands != 2) return true;
429
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);
433     return false;
434   }
435   case Instruction::Unwind:
436     if (Raw->NumOperands != 0) return true;
437     Res = new UnwindInst();
438     return false;
439   }  // end switch(Raw->Opcode) 
440
441   std::cerr << "Unrecognized instruction! " << Raw->Opcode 
442             << " ADDR = 0x" << (void*)Buf << "\n";
443   return true;
444 }