Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- WriteConst.cpp - Functions for writing constants ---------*- C++ -*--=// |
| 2 | // |
| 3 | // This file implements the routines for encoding constants to a bytecode |
| 4 | // stream. |
| 5 | // |
| 6 | // Note that the performance of this library is not terribly important, because |
| 7 | // it shouldn't be used by JIT type applications... so it is not a huge focus |
| 8 | // at least. :) |
| 9 | // |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
| 12 | #include "WriterInternals.h" |
| 13 | #include "llvm/ConstPoolVals.h" |
| 14 | #include "llvm/SymbolTable.h" |
| 15 | #include "llvm/DerivedTypes.h" |
| 16 | |
| 17 | void BytecodeWriter::outputType(const Type *T) { |
| 18 | output_vbr((unsigned)T->getPrimitiveID(), Out); |
| 19 | |
| 20 | // That's all there is to handling primitive types... |
| 21 | if (T->isPrimitiveType()) |
| 22 | return; // We might do this if we alias a prim type: %x = type int |
| 23 | |
| 24 | switch (T->getPrimitiveID()) { // Handle derived types now. |
| 25 | case Type::MethodTyID: { |
| 26 | const MethodType *MT = (const MethodType*)T; |
| 27 | int Slot = Table.getValSlot(MT->getReturnType()); |
| 28 | assert(Slot != -1 && "Type used but not available!!"); |
| 29 | output_vbr((unsigned)Slot, Out); |
| 30 | |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 31 | // Output the number of arguments to method (+1 if varargs): |
| 32 | output_vbr(MT->getParamTypes().size()+MT->isVarArg(), Out); |
| 33 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 34 | // Output all of the arguments... |
| 35 | MethodType::ParamTypes::const_iterator I = MT->getParamTypes().begin(); |
Chris Lattner | 079df31 | 2001-06-27 23:34:01 +0000 | [diff] [blame] | 36 | for (; I != MT->getParamTypes().end(); ++I) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 37 | Slot = Table.getValSlot(*I); |
| 38 | assert(Slot != -1 && "Type used but not available!!"); |
| 39 | output_vbr((unsigned)Slot, Out); |
| 40 | } |
| 41 | |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 42 | // Terminate list with VoidTy if we are a varargs function... |
| 43 | if (MT->isVarArg()) |
| 44 | output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 45 | break; |
| 46 | } |
| 47 | |
| 48 | case Type::ArrayTyID: { |
| 49 | const ArrayType *AT = (const ArrayType*)T; |
| 50 | int Slot = Table.getValSlot(AT->getElementType()); |
| 51 | assert(Slot != -1 && "Type used but not available!!"); |
| 52 | output_vbr((unsigned)Slot, Out); |
| 53 | //cerr << "Type slot = " << Slot << " Type = " << T->getName() << endl; |
| 54 | |
| 55 | output_vbr(AT->getNumElements(), Out); |
| 56 | break; |
| 57 | } |
| 58 | |
| 59 | case Type::StructTyID: { |
| 60 | const StructType *ST = (const StructType*)T; |
| 61 | |
| 62 | // Output all of the element types... |
| 63 | StructType::ElementTypes::const_iterator I = ST->getElementTypes().begin(); |
Chris Lattner | 079df31 | 2001-06-27 23:34:01 +0000 | [diff] [blame] | 64 | for (; I != ST->getElementTypes().end(); ++I) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 65 | int Slot = Table.getValSlot(*I); |
| 66 | assert(Slot != -1 && "Type used but not available!!"); |
| 67 | output_vbr((unsigned)Slot, Out); |
| 68 | } |
| 69 | |
| 70 | // Terminate list with VoidTy |
| 71 | output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out); |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | case Type::PointerTyID: { |
| 76 | const PointerType *PT = (const PointerType*)T; |
| 77 | int Slot = Table.getValSlot(PT->getValueType()); |
| 78 | assert(Slot != -1 && "Type used but not available!!"); |
| 79 | output_vbr((unsigned)Slot, Out); |
| 80 | break; |
| 81 | } |
| 82 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 83 | //case Type::PackedTyID: |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 84 | default: |
| 85 | cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize" |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 86 | << " Type '" << T->getDescription() << "'\n"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 87 | break; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | bool BytecodeWriter::outputConstant(const ConstPoolVal *CPV) { |
| 92 | switch (CPV->getType()->getPrimitiveID()) { |
| 93 | case Type::BoolTyID: // Boolean Types |
| 94 | if (((const ConstPoolBool*)CPV)->getValue()) |
| 95 | output_vbr((unsigned)1, Out); |
| 96 | else |
| 97 | output_vbr((unsigned)0, Out); |
| 98 | break; |
| 99 | |
| 100 | case Type::UByteTyID: // Unsigned integer types... |
| 101 | case Type::UShortTyID: |
| 102 | case Type::UIntTyID: |
| 103 | case Type::ULongTyID: |
| 104 | output_vbr(((const ConstPoolUInt*)CPV)->getValue(), Out); |
| 105 | break; |
| 106 | |
| 107 | case Type::SByteTyID: // Signed integer types... |
| 108 | case Type::ShortTyID: |
| 109 | case Type::IntTyID: |
| 110 | case Type::LongTyID: |
| 111 | output_vbr(((const ConstPoolSInt*)CPV)->getValue(), Out); |
| 112 | break; |
| 113 | |
| 114 | case Type::TypeTyID: // Serialize type type |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 115 | assert(0 && "Types should not be in the ConstPool!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 116 | break; |
| 117 | |
| 118 | case Type::ArrayTyID: { |
| 119 | const ConstPoolArray *CPA = (const ConstPoolArray *)CPV; |
| 120 | unsigned size = CPA->getValues().size(); |
| 121 | if (!((const ArrayType *)CPA->getType())->isSized()) |
| 122 | output_vbr(size, Out); // Not for sized arrays!!! |
| 123 | |
| 124 | for (unsigned i = 0; i < size; i++) { |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 125 | int Slot = Table.getValSlot(CPA->getOperand(i)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 126 | assert(Slot != -1 && "Constant used but not available!!"); |
| 127 | output_vbr((unsigned)Slot, Out); |
| 128 | } |
| 129 | break; |
| 130 | } |
| 131 | |
| 132 | case Type::StructTyID: { |
| 133 | const ConstPoolStruct *CPS = (const ConstPoolStruct*)CPV; |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 134 | const vector<Use> &Vals = CPS->getValues(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 135 | |
| 136 | for (unsigned i = 0; i < Vals.size(); ++i) { |
| 137 | int Slot = Table.getValSlot(Vals[i]); |
| 138 | assert(Slot != -1 && "Constant used but not available!!"); |
| 139 | output_vbr((unsigned)Slot, Out); |
| 140 | } |
| 141 | break; |
| 142 | } |
| 143 | |
Chris Lattner | 1a1cb11 | 2001-09-30 22:46:54 +0000 | [diff] [blame^] | 144 | case Type::PointerTyID: { |
| 145 | output_vbr((unsigned)0, Out); |
| 146 | break; |
| 147 | } |
| 148 | |
Chris Lattner | ff5ecce | 2001-07-15 00:17:23 +0000 | [diff] [blame] | 149 | case Type::FloatTyID: { // Floating point types... |
| 150 | float Tmp = (float)((const ConstPoolFP*)CPV)->getValue(); |
| 151 | output_data(&Tmp, &Tmp+1, Out); |
| 152 | break; |
| 153 | } |
| 154 | case Type::DoubleTyID: { |
| 155 | double Tmp = ((const ConstPoolFP*)CPV)->getValue(); |
| 156 | output_data(&Tmp, &Tmp+1, Out); |
| 157 | break; |
| 158 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 159 | |
| 160 | case Type::VoidTyID: |
| 161 | case Type::LabelTyID: |
| 162 | default: |
| 163 | cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize" |
| 164 | << " type '" << CPV->getType()->getName() << "'\n"; |
| 165 | break; |
| 166 | } |
| 167 | return false; |
| 168 | } |