blob: 6e52369b163f42cffdead4d24a5d6280ebe45da9 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- 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"
Chris Lattner31bcdb82002-04-28 19:55:58 +000013#include "llvm/Constants.h"
Chris Lattner00950542001-06-06 20:29:01 +000014#include "llvm/SymbolTable.h"
15#include "llvm/DerivedTypes.h"
16
17void 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.
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000025 case Type::FunctionTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000026 const FunctionType *MT = cast<FunctionType>(T);
Chris Lattner00950542001-06-06 20:29:01 +000027 int Slot = Table.getValSlot(MT->getReturnType());
28 assert(Slot != -1 && "Type used but not available!!");
29 output_vbr((unsigned)Slot, Out);
30
Chris Lattnere5a57ee2001-07-25 22:47:55 +000031 // Output the number of arguments to method (+1 if varargs):
32 output_vbr(MT->getParamTypes().size()+MT->isVarArg(), Out);
33
Chris Lattner00950542001-06-06 20:29:01 +000034 // Output all of the arguments...
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000035 FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin();
Chris Lattner079df312001-06-27 23:34:01 +000036 for (; I != MT->getParamTypes().end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +000037 Slot = Table.getValSlot(*I);
38 assert(Slot != -1 && "Type used but not available!!");
39 output_vbr((unsigned)Slot, Out);
40 }
41
Chris Lattnere5a57ee2001-07-25 22:47:55 +000042 // Terminate list with VoidTy if we are a varargs function...
43 if (MT->isVarArg())
44 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
Chris Lattner00950542001-06-06 20:29:01 +000045 break;
46 }
47
48 case Type::ArrayTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000049 const ArrayType *AT = cast<ArrayType>(T);
Chris Lattner00950542001-06-06 20:29:01 +000050 int Slot = Table.getValSlot(AT->getElementType());
51 assert(Slot != -1 && "Type used but not available!!");
52 output_vbr((unsigned)Slot, Out);
Chris Lattner186a1f72003-03-19 20:56:46 +000053 //std::cerr << "Type slot = " << Slot << " Type = " << T->getName() << endl;
Chris Lattner00950542001-06-06 20:29:01 +000054
55 output_vbr(AT->getNumElements(), Out);
56 break;
57 }
58
59 case Type::StructTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000060 const StructType *ST = cast<StructType>(T);
Chris Lattner00950542001-06-06 20:29:01 +000061
62 // Output all of the element types...
63 StructType::ElementTypes::const_iterator I = ST->getElementTypes().begin();
Chris Lattner079df312001-06-27 23:34:01 +000064 for (; I != ST->getElementTypes().end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +000065 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: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000076 const PointerType *PT = cast<PointerType>(T);
Chris Lattner7a176752001-12-04 00:03:30 +000077 int Slot = Table.getValSlot(PT->getElementType());
Chris Lattner00950542001-06-06 20:29:01 +000078 assert(Slot != -1 && "Type used but not available!!");
79 output_vbr((unsigned)Slot, Out);
80 break;
81 }
82
Chris Lattner5855f0d2001-10-23 01:53:22 +000083 case Type::OpaqueTyID: {
84 // No need to emit anything, just the count of opaque types is enough.
85 break;
86 }
87
Chris Lattnere8fdde12001-09-07 16:39:41 +000088 //case Type::PackedTyID:
Chris Lattner00950542001-06-06 20:29:01 +000089 default:
Chris Lattner186a1f72003-03-19 20:56:46 +000090 std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
91 << " Type '" << T->getDescription() << "'\n";
Chris Lattner00950542001-06-06 20:29:01 +000092 break;
93 }
94}
95
Chris Lattnere9bb2df2001-12-03 22:26:30 +000096bool BytecodeWriter::outputConstant(const Constant *CPV) {
Chris Lattner186a1f72003-03-19 20:56:46 +000097 assert((CPV->getType()->isPrimitiveType() || !CPV->isNullValue()) &&
98 "Shouldn't output null constants!");
Vikram S. Advea24a0bb2002-07-14 23:08:30 +000099
100 // We must check for a ConstantExpr before switching by type because
101 // a ConstantExpr can be of any type, and has no explicit value.
102 //
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000103 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
104 // FIXME: Encoding of constant exprs could be much more compact!
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000105 assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
106 output_vbr(CE->getNumOperands(), Out); // flags as an expr
107 output_vbr(CE->getOpcode(), Out); // flags as an expr
108
109 for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
110 int Slot = Table.getValSlot(*OI);
111 assert(Slot != -1 && "Unknown constant used in ConstantExpr!!");
112 output_vbr((unsigned)Slot, Out);
113 Slot = Table.getValSlot((*OI)->getType());
114 output_vbr((unsigned)Slot, Out);
115 }
116 return false;
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000117 } else {
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000118 output_vbr((unsigned)0, Out); // flag as not a ConstantExpr
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000119 }
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000120
Chris Lattner00950542001-06-06 20:29:01 +0000121 switch (CPV->getType()->getPrimitiveID()) {
122 case Type::BoolTyID: // Boolean Types
Chris Lattner987cb2c2003-07-23 14:54:33 +0000123 if (cast<ConstantBool>(CPV)->getValue())
Chris Lattner186a1f72003-03-19 20:56:46 +0000124 output_vbr(1U, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000125 else
Chris Lattner186a1f72003-03-19 20:56:46 +0000126 output_vbr(0U, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000127 break;
128
129 case Type::UByteTyID: // Unsigned integer types...
130 case Type::UShortTyID:
131 case Type::UIntTyID:
132 case Type::ULongTyID:
Chris Lattner987cb2c2003-07-23 14:54:33 +0000133 output_vbr(cast<ConstantUInt>(CPV)->getValue(), Out);
Chris Lattner00950542001-06-06 20:29:01 +0000134 break;
135
136 case Type::SByteTyID: // Signed integer types...
137 case Type::ShortTyID:
138 case Type::IntTyID:
139 case Type::LongTyID:
Chris Lattner987cb2c2003-07-23 14:54:33 +0000140 output_vbr(cast<ConstantSInt>(CPV)->getValue(), Out);
Chris Lattner00950542001-06-06 20:29:01 +0000141 break;
142
143 case Type::TypeTyID: // Serialize type type
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000144 assert(0 && "Types should not be in the Constant!");
Chris Lattner00950542001-06-06 20:29:01 +0000145 break;
146
147 case Type::ArrayTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +0000148 const ConstantArray *CPA = cast<ConstantArray>(CPV);
Chris Lattner00950542001-06-06 20:29:01 +0000149 unsigned size = CPA->getValues().size();
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000150 assert(size == cast<ArrayType>(CPA->getType())->getNumElements()
151 && "ConstantArray out of whack!");
Chris Lattner00950542001-06-06 20:29:01 +0000152 for (unsigned i = 0; i < size; i++) {
Chris Lattnere8fdde12001-09-07 16:39:41 +0000153 int Slot = Table.getValSlot(CPA->getOperand(i));
Chris Lattner00950542001-06-06 20:29:01 +0000154 assert(Slot != -1 && "Constant used but not available!!");
155 output_vbr((unsigned)Slot, Out);
156 }
157 break;
158 }
159
160 case Type::StructTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +0000161 const ConstantStruct *CPS = cast<ConstantStruct>(CPV);
Chris Lattner697954c2002-01-20 22:54:45 +0000162 const std::vector<Use> &Vals = CPS->getValues();
Chris Lattner00950542001-06-06 20:29:01 +0000163
164 for (unsigned i = 0; i < Vals.size(); ++i) {
165 int Slot = Table.getValSlot(Vals[i]);
166 assert(Slot != -1 && "Constant used but not available!!");
167 output_vbr((unsigned)Slot, Out);
168 }
169 break;
170 }
171
Chris Lattner1a1cb112001-09-30 22:46:54 +0000172 case Type::PointerTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +0000173 const ConstantPointer *CPP = cast<ConstantPointer>(CPV);
Chris Lattner186a1f72003-03-19 20:56:46 +0000174 assert(!isa<ConstantPointerNull>(CPP) && "Null should be already emitted!");
175 const ConstantPointerRef *CPR = cast<ConstantPointerRef>(CPP);
176 int Slot = Table.getValSlot((Value*)CPR->getValue());
177 assert(Slot != -1 && "Global used but not available!!");
178 output_vbr((unsigned)Slot, Out);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000179 break;
180 }
181
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000182 case Type::FloatTyID: { // Floating point types...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000183 float Tmp = (float)cast<ConstantFP>(CPV)->getValue();
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000184 output_data(&Tmp, &Tmp+1, Out);
185 break;
186 }
187 case Type::DoubleTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000188 double Tmp = cast<ConstantFP>(CPV)->getValue();
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000189 output_data(&Tmp, &Tmp+1, Out);
190 break;
191 }
Chris Lattner00950542001-06-06 20:29:01 +0000192
193 case Type::VoidTyID:
194 case Type::LabelTyID:
195 default:
Chris Lattner186a1f72003-03-19 20:56:46 +0000196 std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
197 << " type '" << CPV->getType()->getName() << "'\n";
Chris Lattner00950542001-06-06 20:29:01 +0000198 break;
199 }
200 return false;
201}