* Use new style casts more
authorChris Lattner <sabre@nondot.org>
Sat, 13 Oct 2001 06:48:38 +0000 (06:48 +0000)
committerChris Lattner <sabre@nondot.org>
Sat, 13 Oct 2001 06:48:38 +0000 (06:48 +0000)
* Add real support for global variable addresses initializing constants
* Fix encoding/decoding of VarArgs calls
* Support the Invoke instruction

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@761 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Bytecode/Writer/ConstantWriter.cpp
lib/Bytecode/Writer/InstructionWriter.cpp
lib/Bytecode/Writer/Writer.cpp

index d0c58f1ae6d147b1a61ed27c4b67528d6d1287fe..8a252dd251fdf631d94177560d8e5b5ee3e594c2 100644 (file)
@@ -101,14 +101,14 @@ bool BytecodeWriter::outputConstant(const ConstPoolVal *CPV) {
   case Type::UShortTyID:
   case Type::UIntTyID:
   case Type::ULongTyID:
-    output_vbr(((const ConstPoolUInt*)CPV)->getValue(), Out);
+    output_vbr(cast<const ConstPoolUInt>(CPV)->getValue(), Out);
     break;
 
   case Type::SByteTyID:   // Signed integer types...
   case Type::ShortTyID:
   case Type::IntTyID:
   case Type::LongTyID:
-    output_vbr(((const ConstPoolSInt*)CPV)->getValue(), Out);
+    output_vbr(cast<const ConstPoolSInt>(CPV)->getValue(), Out);
     break;
 
   case Type::TypeTyID:     // Serialize type type
@@ -116,7 +116,7 @@ bool BytecodeWriter::outputConstant(const ConstPoolVal *CPV) {
     break;
 
   case Type::ArrayTyID: {
-    const ConstPoolArray *CPA = (const ConstPoolArray *)CPV;
+    const ConstPoolArray *CPA = cast<const ConstPoolArray>(CPV);
     unsigned size = CPA->getValues().size();
     if (!((const ArrayType *)CPA->getType())->isSized())
       output_vbr(size, Out);            // Not for sized arrays!!!
@@ -130,7 +130,7 @@ bool BytecodeWriter::outputConstant(const ConstPoolVal *CPV) {
   }
 
   case Type::StructTyID: {
-    const ConstPoolStruct *CPS = (const ConstPoolStruct*)CPV;
+    const ConstPoolStruct *CPS = cast<const ConstPoolStruct>(CPV);
     const vector<Use> &Vals = CPS->getValues();
 
     for (unsigned i = 0; i < Vals.size(); ++i) {
@@ -142,17 +142,28 @@ bool BytecodeWriter::outputConstant(const ConstPoolVal *CPV) {
   }
 
   case Type::PointerTyID: {
-    output_vbr((unsigned)0, Out);
+    const ConstPoolPointer *CPP = cast<const ConstPoolPointer>(CPV);
+    if (isa<ConstPoolPointerNull>(CPP)) {
+      output_vbr((unsigned)0, Out);
+    } else if (const ConstPoolPointerReference *CPR = 
+                       dyn_cast<ConstPoolPointerReference>(CPP)) {
+      output_vbr((unsigned)1, Out);
+      int Slot = Table.getValSlot((Value*)CPR->getValue());
+      assert(Slot != -1 && "Global used but not available!!");
+      output_vbr((unsigned)Slot, Out);
+    } else {
+      assert(0 && "Unknown ConstPoolPointer Subclass!");
+    }
     break;
   }
 
   case Type::FloatTyID: {   // Floating point types...
-    float Tmp = (float)((const ConstPoolFP*)CPV)->getValue();
+    float Tmp = (float)cast<ConstPoolFP>(CPV)->getValue();
     output_data(&Tmp, &Tmp+1, Out);
     break;
   }
   case Type::DoubleTyID: {
-    double Tmp = ((const ConstPoolFP*)CPV)->getValue();
+    double Tmp = cast<ConstPoolFP>(CPV)->getValue();
     output_data(&Tmp, &Tmp+1, Out);
     break;
   }
index d8e17e25187ca067c8a58098169df4e86fb9426c..c972a7cf7912c609d7887d9d366fa0273cdb7645 100644 (file)
@@ -16,6 +16,7 @@
 #include "llvm/Instruction.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/iOther.h"
+#include "llvm/iTerminators.h"
 #include <algorithm>
 
 typedef unsigned char uchar;
@@ -56,26 +57,29 @@ static void outputInstructionFormat0(const Instruction *I,
 static void outputInstrVarArgsCall(const Instruction *I,
                                   const SlotCalculator &Table, unsigned Type,
                                   deque<uchar> &Out) {
-  assert(I->getOpcode() == Instruction::Call /*|| 
-        I->getOpcode() == Instruction::ICall */);
+  assert(isa<CallInst>(I) || isa<InvokeInst>(I));
   // Opcode must have top two bits clear...
   output_vbr(I->getOpcode(), Out);               // Instruction Opcode ID
   output_vbr(Type, Out);                         // Result type (varargs type)
 
   unsigned NumArgs = I->getNumOperands();
-  output_vbr((NumArgs-2)*2+2, Out); // Don't duplicate method & Arg1 types
+  output_vbr(NumArgs*2, Out);
+  // TODO: Don't need to emit types for the fixed types of the varargs method
+  // prototype...
 
-  // Output the method type without an extra type argument.
+  // The type for the method has already been emitted in the type field of the
+  // instruction.  Just emit the slot # now.
   int Slot = Table.getValSlot(I->getOperand(0));
   assert(Slot >= 0 && "No slot number for value!?!?");      
   output_vbr((unsigned)Slot, Out);
 
-  // VarArgs methods must have at least one specified operand
-  Slot = Table.getValSlot(I->getOperand(1));
-  assert(Slot >= 0 && "No slot number for value!?!?");      
-  output_vbr((unsigned)Slot, Out);
+  // Output a dummy field to fill Arg#2 in the reader that is currently unused
+  // for varargs calls.  This is a gross hack to make the code simpler, but we
+  // aren't really doing very small bytecode for varargs calls anyways.
+  // FIXME in the future: Smaller bytecode for varargs calls
+  output_vbr(0, Out);
 
-  for (unsigned i = 2; i < NumArgs; ++i) {
+  for (unsigned i = 1; i < NumArgs; ++i) {
     // Output Arg Type ID
     Slot = Table.getValSlot(I->getOperand(i)->getType());
     assert(Slot >= 0 && "No slot number for value!?!?");      
@@ -159,8 +163,6 @@ static void outputInstructionFormat3(const Instruction *I,
   output(Opcode, Out);
 }
 
-#include "llvm/Assembly/Writer.h"
-
 void BytecodeWriter::processInstruction(const Instruction *I) {
   assert(I->getOpcode() < 64 && "Opcode too big???");
 
@@ -216,7 +218,14 @@ void BytecodeWriter::processInstruction(const Instruction *I) {
     if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
     NumOperands++;
   } else if (const CallInst *CI = dyn_cast<CallInst>(I)) {// Handle VarArg calls
-    if (CI->getCalledMethod()->getMethodType()->isVarArg()) {
+    PointerType *Ty = cast<PointerType>(CI->getCalledValue()->getType());
+    if (cast<MethodType>(Ty->getValueType())->isVarArg()) {
+      outputInstrVarArgsCall(I, Table, Type, Out);
+      return;
+    }
+  } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) { // ...  & Invokes
+    PointerType *Ty = cast<PointerType>(II->getCalledValue()->getType());
+    if (cast<MethodType>(Ty->getValueType())->isVarArg()) {
       outputInstrVarArgsCall(I, Table, Type, Out);
       return;
     }
index 5df2fdabde5aff860ed495c73ff816c5625847fe..ffe67a32f50126c65da3efa2a793a61b41f86615 100644 (file)
@@ -61,9 +61,6 @@ BytecodeWriter::BytecodeWriter(deque<unsigned char> &o, const Module *M)
     outputSymbolTable(*M->getSymbolTable());
 }
 
-// TODO: REMOVE
-#include "llvm/Assembly/Writer.h"
-
 void BytecodeWriter::outputConstants(bool isMethod) {
   BytecodeBlock CPool(BytecodeFormat::ConstantPool, Out);
 
@@ -109,8 +106,7 @@ void BytecodeWriter::outputConstants(bool isMethod) {
        //     << Out.size() << "\n";
        outputConstant(CPV);
       } else {
-       const Type *Ty = cast<const Type>(V);
-       outputType(Ty);
+       outputType(cast<const Type>(V));
       }
     }
   }
@@ -130,7 +126,7 @@ void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
                         isa<ConstPoolVal>(GV);
     output_vbr(oSlot, Out);
 
-    // If we have an initialized, output it now.
+    // If we have an initializer, output it now.
     if (GV->hasInitializer()) {
       Slot = Table.getValSlot(GV->getInitializer());
       assert(Slot != -1 && "No slot for global var initializer!");