1 //===-- ConstantWriter.cpp - Functions for writing constants --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the routines for encoding constants to a bytecode
13 //===----------------------------------------------------------------------===//
15 #include "WriterInternals.h"
16 #include "llvm/Constants.h"
17 #include "llvm/SymbolTable.h"
18 #include "llvm/DerivedTypes.h"
19 #include "Support/Statistic.h"
22 void BytecodeWriter::outputType(const Type *T) {
23 output_vbr((unsigned)T->getPrimitiveID(), Out);
25 // That's all there is to handling primitive types...
26 if (T->isPrimitiveType()) {
27 return; // We might do this if we alias a prim type: %x = type int
30 switch (T->getPrimitiveID()) { // Handle derived types now.
31 case Type::FunctionTyID: {
32 const FunctionType *MT = cast<FunctionType>(T);
33 int Slot = Table.getSlot(MT->getReturnType());
34 assert(Slot != -1 && "Type used but not available!!");
35 output_vbr((unsigned)Slot, Out);
37 // Output the number of arguments to function (+1 if varargs):
38 output_vbr((unsigned)MT->getNumParams()+MT->isVarArg(), Out);
40 // Output all of the arguments...
41 FunctionType::param_iterator I = MT->param_begin();
42 for (; I != MT->param_end(); ++I) {
43 Slot = Table.getSlot(*I);
44 assert(Slot != -1 && "Type used but not available!!");
45 output_vbr((unsigned)Slot, Out);
48 // Terminate list with VoidTy if we are a varargs function...
50 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
54 case Type::ArrayTyID: {
55 const ArrayType *AT = cast<ArrayType>(T);
56 int Slot = Table.getSlot(AT->getElementType());
57 assert(Slot != -1 && "Type used but not available!!");
58 output_vbr((unsigned)Slot, Out);
59 //std::cerr << "Type slot = " << Slot << " Type = " << T->getName() << endl;
61 output_vbr(AT->getNumElements(), Out);
65 case Type::StructTyID: {
66 const StructType *ST = cast<StructType>(T);
68 // Output all of the element types...
69 StructType::ElementTypes::const_iterator I = ST->getElementTypes().begin();
70 for (; I != ST->getElementTypes().end(); ++I) {
71 int Slot = Table.getSlot(*I);
72 assert(Slot != -1 && "Type used but not available!!");
73 output_vbr((unsigned)Slot, Out);
76 // Terminate list with VoidTy
77 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
81 case Type::PointerTyID: {
82 const PointerType *PT = cast<PointerType>(T);
83 int Slot = Table.getSlot(PT->getElementType());
84 assert(Slot != -1 && "Type used but not available!!");
85 output_vbr((unsigned)Slot, Out);
89 case Type::OpaqueTyID: {
90 // No need to emit anything, just the count of opaque types is enough.
94 //case Type::PackedTyID:
96 std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
97 << " Type '" << T->getDescription() << "'\n";
102 void BytecodeWriter::outputConstant(const Constant *CPV) {
103 assert((CPV->getType()->isPrimitiveType() || !CPV->isNullValue()) &&
104 "Shouldn't output null constants!");
106 // We must check for a ConstantExpr before switching by type because
107 // a ConstantExpr can be of any type, and has no explicit value.
109 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
110 // FIXME: Encoding of constant exprs could be much more compact!
111 assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
112 output_vbr(CE->getNumOperands(), Out); // flags as an expr
113 output_vbr(CE->getOpcode(), Out); // flags as an expr
115 for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
116 int Slot = Table.getSlot(*OI);
117 assert(Slot != -1 && "Unknown constant used in ConstantExpr!!");
118 output_vbr((unsigned)Slot, Out);
119 Slot = Table.getSlot((*OI)->getType());
120 output_vbr((unsigned)Slot, Out);
124 output_vbr(0U, Out); // flag as not a ConstantExpr
127 switch (CPV->getType()->getPrimitiveID()) {
128 case Type::BoolTyID: // Boolean Types
129 if (cast<ConstantBool>(CPV)->getValue())
135 case Type::UByteTyID: // Unsigned integer types...
136 case Type::UShortTyID:
138 case Type::ULongTyID:
139 output_vbr(cast<ConstantUInt>(CPV)->getValue(), Out);
142 case Type::SByteTyID: // Signed integer types...
143 case Type::ShortTyID:
146 output_vbr(cast<ConstantSInt>(CPV)->getValue(), Out);
149 case Type::TypeTyID: // Serialize type type
150 assert(0 && "Types should not be in the Constant!");
153 case Type::ArrayTyID: {
154 const ConstantArray *CPA = cast<ConstantArray>(CPV);
155 assert(!CPA->isString() && "Constant strings should be handled specially!");
157 for (unsigned i = 0; i != CPA->getNumOperands(); ++i) {
158 int Slot = Table.getSlot(CPA->getOperand(i));
159 assert(Slot != -1 && "Constant used but not available!!");
160 output_vbr((unsigned)Slot, Out);
165 case Type::StructTyID: {
166 const ConstantStruct *CPS = cast<ConstantStruct>(CPV);
167 const std::vector<Use> &Vals = CPS->getValues();
169 for (unsigned i = 0; i < Vals.size(); ++i) {
170 int Slot = Table.getSlot(Vals[i]);
171 assert(Slot != -1 && "Constant used but not available!!");
172 output_vbr((unsigned)Slot, Out);
177 case Type::PointerTyID:
178 assert(0 && "No non-null, non-constant-expr constants allowed!");
181 case Type::FloatTyID: { // Floating point types...
182 float Tmp = (float)cast<ConstantFP>(CPV)->getValue();
183 output_data(&Tmp, &Tmp+1, Out);
186 case Type::DoubleTyID: {
187 double Tmp = cast<ConstantFP>(CPV)->getValue();
188 output_data(&Tmp, &Tmp+1, Out);
193 case Type::LabelTyID:
195 std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
196 << " type '" << CPV->getType()->getName() << "'\n";
202 void BytecodeWriter::outputConstantStrings() {
203 SlotCalculator::string_iterator I = Table.string_begin();
204 SlotCalculator::string_iterator E = Table.string_end();
205 if (I == E) return; // No strings to emit
207 // If we have != 0 strings to emit, output them now. Strings are emitted into
208 // the 'void' type plane.
209 output_vbr(unsigned(E-I), Out);
210 output_vbr(Type::VoidTyID, Out);
212 // Emit all of the strings.
213 for (I = Table.string_begin(); I != E; ++I) {
214 const ConstantArray *Str = *I;
215 int Slot = Table.getSlot(Str->getType());
216 assert(Slot != -1 && "Constant string of unknown type?");
217 output_vbr((unsigned)Slot, Out);
219 // Now that we emitted the type (which indicates the size of the string),
220 // emit all of the characters.
221 std::string Val = Str->getAsString();
222 output_data(Val.c_str(), Val.c_str()+Val.size(), Out);