e81927c441b13ce166e67f15be69e0fdbab7451f
[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 bool BytecodeParser::ParseRawInst(const unsigned char *&Buf,
21                                   const unsigned char *EndBuf, 
22                                   RawInst &Result) {
23   unsigned Op, Typ;
24   if (read(Buf, EndBuf, Op)) return true;
25
26   // bits   Instruction format:        Common to all formats
27   // --------------------------
28   // 01-00: Opcode type, fixed to 1.
29   // 07-02: Opcode
30   Result.NumOperands = (Op >> 0) & 03;
31   Result.Opcode      = (Op >> 2) & 63;
32
33   switch (Result.NumOperands) {
34   case 1:
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)
39     //
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;
44     break;
45   case 2:
46     // bits   Instruction format:
47     // --------------------------
48     // 15-08: Resulting type plane
49     // 23-16: Operand #1
50     // 31-24: Operand #2  
51     //
52     Result.Ty   = getType((Op >> 8) & 255);
53     Result.Arg1 = (Op >> 16) & 255;
54     Result.Arg2 = (Op >> 24) & 255;
55     break;
56   case 3:
57     // bits   Instruction format:
58     // --------------------------
59     // 13-08: Resulting type plane
60     // 19-14: Operand #1
61     // 25-20: Operand #2
62     // 31-26: Operand #3
63     //
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;
68     break;
69   case 0:
70     Buf -= 4;  // Hrm, try this again...
71     if (read_vbr(Buf, EndBuf, Result.Opcode)) return true;
72     Result.Opcode >>= 2;
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;
77
78     switch (Result.NumOperands) {
79     case 0: 
80       std::cerr << "Zero Arg instr found!\n"; 
81       return true;  // This encoding is invalid!
82     case 1: 
83       if (read_vbr(Buf, EndBuf, Result.Arg1)) return true;
84       break;
85     case 2:
86       if (read_vbr(Buf, EndBuf, Result.Arg1) || 
87           read_vbr(Buf, EndBuf, Result.Arg2)) return true;
88       break;
89     case 3:
90       if (read_vbr(Buf, EndBuf, Result.Arg1) || 
91           read_vbr(Buf, EndBuf, Result.Arg2) ||
92           read_vbr(Buf, EndBuf, Result.Arg3)) return true;
93       break;
94     default:
95       if (read_vbr(Buf, EndBuf, Result.Arg1) || 
96           read_vbr(Buf, EndBuf, Result.Arg2)) return true;
97
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;
102       break;
103     }
104     if (align32(Buf, EndBuf)) return true;
105     break;
106   }
107
108 #if 0
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";
112 #endif
113   return false;
114 }
115
116
117 bool BytecodeParser::ParseInstruction(const unsigned char *&Buf,
118                                       const unsigned char *EndBuf,
119                                       Instruction *&Res,
120                                       BasicBlock *BB /*HACK*/) {
121   RawInst Raw;
122   if (ParseRawInst(Buf, EndBuf, Raw))
123     return true;
124
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));
130     return false;
131   } 
132
133   Value *V;
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);
142     else
143       Res = new VarArgInst(V, Ty);
144     return false;
145   }
146   case Instruction::PHINode: {
147     PHINode *PN = new PHINode(Raw.Ty);
148     switch (Raw.NumOperands) {
149     case 0: 
150     case 1: 
151     case 3: std::cerr << "Invalid phi node encountered!\n"; 
152             delete PN; 
153             return true;
154     case 2: PN->addIncoming(getValue(Raw.Ty, Raw.Arg1),
155                             cast<BasicBlock>(getValue(Type::LabelTy,Raw.Arg2)));
156       break;
157     default:
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";
162         delete PN;
163         return true;
164       } else {
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])));
169       }
170       delete Raw.VarArgs; 
171       break;
172     }
173     Res = PN;
174     return false;
175   }
176
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));
182     return false;
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; 
188     }
189     break;
190
191   case Instruction::Br:
192     if (Raw.NumOperands == 1) {
193       Res = new BranchInst(cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg1)));
194       return false;
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));
199       return false;
200     }
201     break;
202     
203   case Instruction::Switch: {
204     SwitchInst *I = 
205       new SwitchInst(getValue(Raw.Ty, Raw.Arg1), 
206                      cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2)));
207     Res = I;
208     if (Raw.NumOperands < 3) return false;  // No destinations?  Wierd.
209
210     if (Raw.NumOperands == 3 || Raw.VarArgs->size() & 1) {
211       std::cerr << "Switch statement with odd number of arguments!\n";
212       delete I;
213       return true;
214     }      
215     
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])));
220
221     delete Raw.VarArgs;
222     return false;
223   }
224
225   case Instruction::Call: {
226     Value *M = getValue(Raw.Ty, Raw.Arg1);
227     if (M == 0) return true;
228
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;
234
235     std::vector<Value *> Params;
236     const FunctionType::ParamTypes &PL = MTy->getParamTypes();
237
238     if (!MTy->isVarArg()) {
239       FunctionType::ParamTypes::const_iterator It = PL.begin();
240
241       switch (Raw.NumOperands) {
242       case 0: std::cerr << "Invalid call instruction encountered!\n";
243         return true;
244       case 1: break;
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;
249       default:
250         Params.push_back(getValue(*It++, Raw.Arg2));
251         {
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]));
257           }
258         }
259         delete Raw.VarArgs;
260       }
261       if (It != PL.end()) return true;
262     } else {
263       if (Raw.NumOperands > 2) {
264         std::vector<unsigned> &args = *Raw.VarArgs;
265         if (args.size() < 1) return true;
266
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]);
271           if (Ty == 0)
272             return true;
273           
274           Value *V = getValue(Ty, args[i+1]);
275           if (V == 0) return true;
276           Params.push_back(V);
277         }
278         delete Raw.VarArgs;
279       }
280     }
281
282     Res = new CallInst(M, Params);
283     return false;
284   }
285   case Instruction::Invoke: {
286     Value *M = getValue(Raw.Ty, Raw.Arg1);
287     if (M == 0) return true;
288
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;
294
295     std::vector<Value *> Params;
296     const FunctionType::ParamTypes &PL = MTy->getParamTypes();
297     std::vector<unsigned> &args = *Raw.VarArgs;
298
299     BasicBlock *Normal, *Except;
300
301     if (!MTy->isVarArg()) {
302       if (Raw.NumOperands < 3) return true;
303
304       Normal = cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2));
305       if (Raw.NumOperands ==  3)
306         Except = cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg3));
307       else {
308         Except = cast<BasicBlock>(getValue(Type::LabelTy, args[0]));
309
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]));
315         }
316         if (It != PL.end()) return true;
317       }
318     } else {
319       if (args.size() < 4) return true;
320
321       Normal = cast<BasicBlock>(getValue(Type::LabelTy, args[0]));
322       Except = cast<BasicBlock>(getValue(Type::LabelTy, args[2]));
323
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]));
329       }
330     }
331
332     if (Raw.NumOperands > 3)
333       delete Raw.VarArgs;
334     Res = new InvokeInst(M, Normal, Except, Params);
335     return false;
336   }
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);
342     else
343       return true;
344     return false;
345
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);
351     else
352       return true;
353     return false;
354
355   case Instruction::Free:
356     V = getValue(Raw.Ty, Raw.Arg1);
357     if (!isa<PointerType>(V->getType())) return true;
358     Res = new FreeInst(V);
359     return false;
360
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);
366
367     switch (Raw.NumOperands) {
368     case 0: std::cerr << "Invalid load encountered!\n"; return true;
369     case 1: break;
370     case 2:
371       if (!TopTy) return true;
372       Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
373       if (!V) return true;
374       break;
375     case 3: {
376       if (!TopTy) return true;
377       Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
378       if (!V) return true;
379
380       const Type *ETy = GetElementPtrInst::getIndexedType(TopTy, Idx, true);
381       const CompositeType *ElTy = dyn_cast_or_null<CompositeType>(ETy);
382       if (!ElTy) return true;
383
384       Idx.push_back(V = getValue(ElTy->getIndexType(), Raw.Arg3));
385       if (!V) return true;
386       break;
387     }
388     default:
389       if (!TopTy) return true;
390       Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
391       if (!V) return true;
392
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]));
399         if (!V) return true;
400       }
401       delete Raw.VarArgs; 
402       break;
403     }
404
405     if (Raw.Opcode == Instruction::Load) {
406       Value *Src = getValue(Raw.Ty, Raw.Arg1);
407       if (!Idx.empty()) {
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
414         // method.
415         BB->getInstList().push_back(cast<Instruction>(Src));
416       }
417       Res = new LoadInst(Src);
418     } else if (Raw.Opcode == Instruction::GetElementPtr)
419       Res = new GetElementPtrInst(getValue(Raw.Ty, Raw.Arg1), Idx);
420     else
421       abort();
422     return false;
423   }
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);
428
429     switch (Raw.NumOperands) {
430     case 0: 
431     case 1: std::cerr << "Invalid store encountered!\n"; return true;
432     case 2: break;
433     case 3:
434       if (!TopTy) return true;
435       Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg3));
436       if (!V) return true;
437       break;
438     default:
439       std::vector<unsigned> &args = *Raw.VarArgs;
440       const CompositeType *ElTy = TopTy;
441       unsigned i, E;
442       for (i = 0, E = args.size(); ElTy && i != E; ++i) {
443         Idx.push_back(V = getValue(ElTy->getIndexType(), args[i]));
444         if (!V) return true;
445
446         const Type *ETy = GetElementPtrInst::getIndexedType(Raw.Ty, Idx, true);
447         ElTy = dyn_cast_or_null<CompositeType>(ETy);
448       }
449       if (i != E)
450         return true;  // didn't use up all of the indices!
451
452       delete Raw.VarArgs; 
453       break;
454     }
455
456     Value *Ptr = getValue(Raw.Ty, Raw.Arg2);
457     if (!Idx.empty()) {
458       std::cerr << "WARNING: Bytecode contains load instruction with indices.  "
459                 << "Replacing with getelementptr/load pair\n";
460
461       const Type *ElType = GetElementPtrInst::getIndexedType(Raw.Ty, Idx);
462       if (ElType == 0) return true;
463
464       Ptr = new GetElementPtrInst(Ptr, Idx);
465       // FIXME: Remove this compatibility code and the BB parameter to this
466       // method.
467       BB->getInstList().push_back(cast<Instruction>(Ptr));
468     }
469
470     const Type *ValTy = cast<PointerType>(Ptr->getType())->getElementType();
471     Res = new StoreInst(getValue(ValTy, Raw.Arg1), Ptr);
472     return false;
473   }
474   }  // end switch(Raw.Opcode) 
475
476   std::cerr << "Unrecognized instruction! " << Raw.Opcode 
477             << " ADDR = 0x" << (void*)Buf << "\n";
478   return true;
479 }