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"
20 void BytecodeWriter::outputType(const Type *T) {
21 output_vbr((unsigned)T->getPrimitiveID(), Out);
23 // That's all there is to handling primitive types...
24 if (T->isPrimitiveType())
25 return; // We might do this if we alias a prim type: %x = type int
27 switch (T->getPrimitiveID()) { // Handle derived types now.
28 case Type::FunctionTyID: {
29 const FunctionType *MT = cast<FunctionType>(T);
30 int Slot = Table.getSlot(MT->getReturnType());
31 assert(Slot != -1 && "Type used but not available!!");
32 output_vbr((unsigned)Slot, Out);
34 // Output the number of arguments to method (+1 if varargs):
35 output_vbr(MT->getParamTypes().size()+MT->isVarArg(), Out);
37 // Output all of the arguments...
38 FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin();
39 for (; I != MT->getParamTypes().end(); ++I) {
40 Slot = Table.getSlot(*I);
41 assert(Slot != -1 && "Type used but not available!!");
42 output_vbr((unsigned)Slot, Out);
45 // Terminate list with VoidTy if we are a varargs function...
47 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
51 case Type::ArrayTyID: {
52 const ArrayType *AT = cast<ArrayType>(T);
53 int Slot = Table.getSlot(AT->getElementType());
54 assert(Slot != -1 && "Type used but not available!!");
55 output_vbr((unsigned)Slot, Out);
56 //std::cerr << "Type slot = " << Slot << " Type = " << T->getName() << endl;
58 output_vbr(AT->getNumElements(), Out);
62 case Type::StructTyID: {
63 const StructType *ST = cast<StructType>(T);
65 // Output all of the element types...
66 StructType::ElementTypes::const_iterator I = ST->getElementTypes().begin();
67 for (; I != ST->getElementTypes().end(); ++I) {
68 int Slot = Table.getSlot(*I);
69 assert(Slot != -1 && "Type used but not available!!");
70 output_vbr((unsigned)Slot, Out);
73 // Terminate list with VoidTy
74 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
78 case Type::PointerTyID: {
79 const PointerType *PT = cast<PointerType>(T);
80 int Slot = Table.getSlot(PT->getElementType());
81 assert(Slot != -1 && "Type used but not available!!");
82 output_vbr((unsigned)Slot, Out);
86 case Type::OpaqueTyID: {
87 // No need to emit anything, just the count of opaque types is enough.
91 //case Type::PackedTyID:
93 std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
94 << " Type '" << T->getDescription() << "'\n";
99 bool BytecodeWriter::outputConstant(const Constant *CPV) {
100 assert((CPV->getType()->isPrimitiveType() || !CPV->isNullValue()) &&
101 "Shouldn't output null constants!");
103 // We must check for a ConstantExpr before switching by type because
104 // a ConstantExpr can be of any type, and has no explicit value.
106 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
107 // FIXME: Encoding of constant exprs could be much more compact!
108 assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
109 output_vbr(CE->getNumOperands(), Out); // flags as an expr
110 output_vbr(CE->getOpcode(), Out); // flags as an expr
112 for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
113 int Slot = Table.getSlot(*OI);
114 assert(Slot != -1 && "Unknown constant used in ConstantExpr!!");
115 output_vbr((unsigned)Slot, Out);
116 Slot = Table.getSlot((*OI)->getType());
117 output_vbr((unsigned)Slot, Out);
121 output_vbr((unsigned)0, Out); // flag as not a ConstantExpr
124 switch (CPV->getType()->getPrimitiveID()) {
125 case Type::BoolTyID: // Boolean Types
126 if (cast<ConstantBool>(CPV)->getValue())
132 case Type::UByteTyID: // Unsigned integer types...
133 case Type::UShortTyID:
135 case Type::ULongTyID:
136 output_vbr(cast<ConstantUInt>(CPV)->getValue(), Out);
139 case Type::SByteTyID: // Signed integer types...
140 case Type::ShortTyID:
143 output_vbr(cast<ConstantSInt>(CPV)->getValue(), Out);
146 case Type::TypeTyID: // Serialize type type
147 assert(0 && "Types should not be in the Constant!");
150 case Type::ArrayTyID: {
151 const ConstantArray *CPA = cast<ConstantArray>(CPV);
152 unsigned size = CPA->getValues().size();
153 assert(size == cast<ArrayType>(CPA->getType())->getNumElements()
154 && "ConstantArray out of whack!");
155 for (unsigned i = 0; i < size; i++) {
156 int Slot = Table.getSlot(CPA->getOperand(i));
157 assert(Slot != -1 && "Constant used but not available!!");
158 output_vbr((unsigned)Slot, Out);
163 case Type::StructTyID: {
164 const ConstantStruct *CPS = cast<ConstantStruct>(CPV);
165 const std::vector<Use> &Vals = CPS->getValues();
167 for (unsigned i = 0; i < Vals.size(); ++i) {
168 int Slot = Table.getSlot(Vals[i]);
169 assert(Slot != -1 && "Constant used but not available!!");
170 output_vbr((unsigned)Slot, Out);
175 case Type::PointerTyID: {
176 const ConstantPointer *CPP = cast<ConstantPointer>(CPV);
177 assert(!isa<ConstantPointerNull>(CPP) && "Null should be already emitted!");
178 const ConstantPointerRef *CPR = cast<ConstantPointerRef>(CPP);
179 int Slot = Table.getSlot((Value*)CPR->getValue());
180 assert(Slot != -1 && "Global used but not available!!");
181 output_vbr((unsigned)Slot, Out);
185 case Type::FloatTyID: { // Floating point types...
186 float Tmp = (float)cast<ConstantFP>(CPV)->getValue();
187 output_data(&Tmp, &Tmp+1, Out);
190 case Type::DoubleTyID: {
191 double Tmp = cast<ConstantFP>(CPV)->getValue();
192 output_data(&Tmp, &Tmp+1, Out);
197 case Type::LabelTyID:
199 std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
200 << " type '" << CPV->getType()->getName() << "'\n";