Add support for extern varargs methods & varargs method calls
[oota-llvm.git] / lib / Bytecode / Reader / InstructionReader.cpp
index 667e144673cf78243ecf7f14e2675f7b9842df21..f1dcf08e793215c970e44cc8785c6e9e92005c16 100644 (file)
@@ -80,8 +80,11 @@ bool BytecodeParser::ParseRawInst(const uchar *&Buf, const uchar *EndBuf,
     break;
   }
 
-  //cerr << "NO: "  << Result.NumOperands   << " opcode: " << Result.Opcode 
-  //   << " Ty: " << Result.Ty->getName() << " arg1: "   << Result.Arg1 << endl;
+#if 0
+  cerr << "NO: "  << Result.NumOperands   << " opcode: " << Result.Opcode 
+       << " Ty: " << Result.Ty->getName() << " arg1: "   << Result.Arg1 
+       << " arg2: "   << Result.Arg2 << " arg3: "   << Result.Arg3 << endl;
+#endif
   return false;
 }
 
@@ -93,44 +96,69 @@ bool BytecodeParser::ParseInstruction(const uchar *&Buf, const uchar *EndBuf,
 
   if (Raw.Opcode >= Instruction::FirstUnaryOp && 
       Raw.Opcode <  Instruction::NumUnaryOps  && Raw.NumOperands == 1) {
-    Res = Instruction::getUnaryOperator(Raw.Opcode, getValue(Raw.Ty, Raw.Arg1));
+    Res = UnaryOperator::create((Instruction::UnaryOps)Raw.Opcode,
+                               getValue(Raw.Ty,Raw.Arg1));
     return false;
   } else if (Raw.Opcode >= Instruction::FirstBinaryOp &&
             Raw.Opcode <  Instruction::NumBinaryOps  && Raw.NumOperands == 2) {
-    Res = Instruction::getBinaryOperator(Raw.Opcode, getValue(Raw.Ty, Raw.Arg1),
-                                        getValue(Raw.Ty, Raw.Arg2));
+    Res = BinaryOperator::create((Instruction::BinaryOps)Raw.Opcode,
+                                getValue(Raw.Ty, Raw.Arg1),
+                                getValue(Raw.Ty, Raw.Arg2));
     return false;
-  } else if (Raw.Opcode == Instruction::PHINode) {
+  } 
+
+  Value *V;
+  switch (Raw.Opcode) {
+  case Instruction::Cast:
+    Res = new CastInst(getValue(Raw.Ty, Raw.Arg1), getType(Raw.Arg2));
+    return false;
+
+  case Instruction::PHINode: {
     PHINode *PN = new PHINode(Raw.Ty);
     switch (Raw.NumOperands) {
-    case 0: cerr << "Invalid phi node encountered!\n"; 
+    case 0: 
+    case 1: 
+    case 3: cerr << "Invalid phi node encountered!\n"; 
             delete PN; 
            return true;
-    case 1: PN->addIncoming(getValue(Raw.Ty, Raw.Arg1)); break;
-    case 2: PN->addIncoming(getValue(Raw.Ty, Raw.Arg1)); 
-            PN->addIncoming(getValue(Raw.Ty, Raw.Arg2)); break;
-    case 3: PN->addIncoming(getValue(Raw.Ty, Raw.Arg1)); 
-            PN->addIncoming(getValue(Raw.Ty, Raw.Arg2)); 
-            PN->addIncoming(getValue(Raw.Ty, Raw.Arg3)); break;
+    case 2: PN->addIncoming(getValue(Raw.Ty, Raw.Arg1),
+                           (BasicBlock*)getValue(Type::LabelTy, Raw.Arg2)); 
+      break;
     default:
-      PN->addIncoming(getValue(Raw.Ty, Raw.Arg1)); 
-      PN->addIncoming(getValue(Raw.Ty, Raw.Arg2));
-      {
+      PN->addIncoming(getValue(Raw.Ty, Raw.Arg1), 
+                     (BasicBlock*)getValue(Type::LabelTy, Raw.Arg2));
+      if (Raw.VarArgs->size() & 1) {
+       cerr << "PHI Node with ODD number of arguments!\n";
+       delete PN;
+       return true;
+      } else {
         vector<unsigned> &args = *Raw.VarArgs;
-        for (unsigned i = 0; i < args.size(); i++)
-          PN->addIncoming(getValue(Raw.Ty, args[i]));
+        for (unsigned i = 0; i < args.size(); i+=2)
+          PN->addIncoming(getValue(Raw.Ty, args[i]),
+                         (BasicBlock*)getValue(Type::LabelTy, args[i+1]));
       }
-      delete Raw.VarArgs;
+      delete Raw.VarArgs; 
+      break;
     }
     Res = PN;
     return false;
-  } else if (Raw.Opcode == Instruction::Ret) {
+  }
+
+  case Instruction::Shl:
+  case Instruction::Shr:
+    Res = new ShiftInst((Instruction::OtherOps)Raw.Opcode,
+                       getValue(Raw.Ty, Raw.Arg1),
+                       getValue(Type::UByteTy, Raw.Arg2));
+    return false;
+  case Instruction::Ret:
     if (Raw.NumOperands == 0) {
       Res = new ReturnInst(); return false; 
     } else if (Raw.NumOperands == 1) {
       Res = new ReturnInst(getValue(Raw.Ty, Raw.Arg1)); return false; 
     }
-  } else if (Raw.Opcode == Instruction::Br) {
+    break;
+
+  case Instruction::Br:
     if (Raw.NumOperands == 1) {
       Res = new BranchInst((BasicBlock*)getValue(Type::LabelTy, Raw.Arg1));
       return false;
@@ -140,7 +168,9 @@ bool BytecodeParser::ParseInstruction(const uchar *&Buf, const uchar *EndBuf,
                                        getValue(Type::BoolTy , Raw.Arg3));
       return false;
     }
-  } else if (Raw.Opcode == Instruction::Switch) {
+    break;
+    
+  case Instruction::Switch: {
     SwitchInst *I = 
       new SwitchInst(getValue(Raw.Ty, Raw.Arg1), 
                      (BasicBlock*)getValue(Type::LabelTy, Raw.Arg2));
@@ -160,54 +190,144 @@ bool BytecodeParser::ParseInstruction(const uchar *&Buf, const uchar *EndBuf,
 
     delete Raw.VarArgs;
     return false;
-  } else if (Raw.Opcode == Instruction::Call) {
+  }
+
+  case Instruction::Call: {
     Method *M = (Method*)getValue(Raw.Ty, Raw.Arg1);
     if (M == 0) return true;
 
+    vector<Value *> Params;
     const MethodType::ParamTypes &PL = M->getMethodType()->getParamTypes();
-    MethodType::ParamTypes::const_iterator It = PL.begin();
 
-    vector<Value *> Params;
-    switch (Raw.NumOperands) {
-    case 0: cerr << "Invalid call instruction encountered!\n";
-           return true;
-    case 1: break;
-    case 2: Params.push_back(getValue(*It++, Raw.Arg2)); break;
-    case 3: Params.push_back(getValue(*It++, Raw.Arg2)); 
-            if (It == PL.end()) return true;
-            Params.push_back(getValue(*It++, Raw.Arg3)); break;
-    default:
-      Params.push_back(getValue(*It++, Raw.Arg2));
-      {
-        vector<unsigned> &args = *Raw.VarArgs;
-        for (unsigned i = 0; i < args.size(); i++) {
-         if (It == PL.end()) return true;
-          Params.push_back(getValue(*It++, args[i]));
+    if (!M->getType()->isMethodType()->isVarArg()) {
+      MethodType::ParamTypes::const_iterator It = PL.begin();
+
+      switch (Raw.NumOperands) {
+      case 0: cerr << "Invalid call instruction encountered!\n";
+       return true;
+      case 1: break;
+      case 2: Params.push_back(getValue(*It++, Raw.Arg2)); break;
+      case 3: Params.push_back(getValue(*It++, Raw.Arg2)); 
+       if (It == PL.end()) return true;
+       Params.push_back(getValue(*It++, Raw.Arg3)); break;
+      default:
+       Params.push_back(getValue(*It++, Raw.Arg2));
+       {
+         vector<unsigned> &args = *Raw.VarArgs;
+         for (unsigned i = 0; i < args.size(); i++) {
+           if (It == PL.end()) return true;
+           // TODO: Check getValue for null!
+           Params.push_back(getValue(*It++, args[i]));
+         }
        }
+       delete Raw.VarArgs;
+      }
+      if (It != PL.end()) return true;
+    } else {
+      // The first parameter does not have a type specifier... because there
+      // must be at least one concrete argument to a vararg type...
+      Params.push_back(getValue(PL.front(), Raw.Arg2));
+
+      vector<unsigned> &args = *Raw.VarArgs;
+      if ((args.size() & 1) != 0) return true;  // Must be pairs of type/value
+      for (unsigned i = 0; i < args.size(); i+=2) {
+       Value *Ty = getValue(Type::TypeTy, args[i]);
+       if (!Ty) return true;
+       // TODO: Check getValue for null!
+       Params.push_back(getValue(Ty->castTypeAsserting(), args[i+1]));
       }
       delete Raw.VarArgs;
     }
-    if (It != PL.end()) return true;
 
     Res = new CallInst(M, Params);
     return false;
-  } else if (Raw.Opcode == Instruction::Malloc) {
+  }
+  case Instruction::Malloc:
     if (Raw.NumOperands > 2) return true;
-    Value *Sz = (Raw.NumOperands == 2) ? getValue(Type::UIntTy, Raw.Arg2) : 0;
-    Res = new MallocInst((ConstPoolType*)getValue(Type::TypeTy, Raw.Arg1), Sz);
+    V = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0;
+    Res = new MallocInst(Raw.Ty, V);
     return false;
-  } else if (Raw.Opcode == Instruction::Alloca) {
+
+  case Instruction::Alloca:
     if (Raw.NumOperands > 2) return true;
-    Value *Sz = (Raw.NumOperands == 2) ? getValue(Type::UIntTy, Raw.Arg2) : 0;
-    Res = new AllocaInst((ConstPoolType*)getValue(Type::TypeTy, Raw.Arg1), Sz);
+    V = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0;
+    Res = new AllocaInst(Raw.Ty, V);
+    return false;
+
+  case Instruction::Free:
+    V = getValue(Raw.Ty, Raw.Arg1);
+    if (!V->getType()->isPointerType()) return true;
+    Res = new FreeInst(V);
     return false;
-  } else if (Raw.Opcode == Instruction::Free) {
-    Value *Val = getValue(Raw.Ty, Raw.Arg1);
-    if (!Val->getType()->isPointerType()) return true;
-    Res = new FreeInst(Val);
+
+  case Instruction::Load:
+  case Instruction::GetElementPtr: {
+    vector<ConstPoolVal*> Idx;
+    switch (Raw.NumOperands) {
+    case 0: cerr << "Invalid load encountered!\n"; return true;
+    case 1: break;
+    case 2: V = getValue(Type::UByteTy, Raw.Arg2);
+            if (!V->isConstant()) return true;
+            Idx.push_back(V->castConstant());
+            break;
+    case 3: V = getValue(Type::UByteTy, Raw.Arg2);
+            if (!V->isConstant()) return true;
+            Idx.push_back(V->castConstant());
+           V = getValue(Type::UByteTy, Raw.Arg3);
+            if (!V->isConstant()) return true;
+            Idx.push_back(V->castConstant());
+            break;
+    default:
+      V = getValue(Type::UByteTy, Raw.Arg2);
+      if (!V->isConstant()) return true;
+      Idx.push_back(V->castConstant());
+      vector<unsigned> &args = *Raw.VarArgs;
+      for (unsigned i = 0, E = args.size(); i != E; ++i) {
+       V = getValue(Type::UByteTy, args[i]);
+       if (!V->isConstant()) return true;
+       Idx.push_back(V->castConstant());
+      }
+      delete Raw.VarArgs; 
+      break;
+    }
+    if (Raw.Opcode == Instruction::Load)
+      Res = new LoadInst(getValue(Raw.Ty, Raw.Arg1), Idx);
+    else if (Raw.Opcode == Instruction::GetElementPtr)
+      Res = new GetElementPtrInst(getValue(Raw.Ty, Raw.Arg1), Idx);
+    else
+      abort();
+    return false;
+  }
+  case Instruction::Store: {
+    vector<ConstPoolVal*> Idx;
+    switch (Raw.NumOperands) {
+    case 0: 
+    case 1: cerr << "Invalid store encountered!\n"; return true;
+    case 2: break;
+    case 3: V = getValue(Type::UByteTy, Raw.Arg3);
+            if (!V->isConstant()) return true;
+            Idx.push_back(V->castConstant());
+            break;
+    default:
+      vector<unsigned> &args = *Raw.VarArgs;
+      for (unsigned i = 0, E = args.size(); i != E; ++i) {
+       V = getValue(Type::UByteTy, args[i]);
+       if (!V->isConstant()) return true;
+       Idx.push_back(V->castConstant());
+      }
+      delete Raw.VarArgs; 
+      break;
+    }
+
+    const Type *ElType = StoreInst::getIndexedType(Raw.Ty, Idx);
+    if (ElType == 0) return true;
+    Res = new StoreInst(getValue(ElType, Raw.Arg1), getValue(Raw.Ty, Raw.Arg2),
+                       Idx);
     return false;
   }
+  }  // end switch(Raw.Opcode) 
 
-  cerr << "Unrecognized instruction! " << Raw.Opcode << endl;
+  cerr << "Unrecognized instruction! " << Raw.Opcode 
+       << " ADDR = 0x" << (void*)Buf << endl;
   return true;
 }