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"
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
29 switch (T->getPrimitiveID()) { // Handle derived types now.
30 case Type::FunctionTyID: {
31 const FunctionType *MT = cast<FunctionType>(T);
32 int Slot = Table.getSlot(MT->getReturnType());
33 assert(Slot != -1 && "Type used but not available!!");
34 output_vbr((unsigned)Slot, Out);
36 // Output the number of arguments to method (+1 if varargs):
37 output_vbr((unsigned)MT->getParamTypes().size()+MT->isVarArg(), Out);
39 // Output all of the arguments...
40 FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin();
41 for (; I != MT->getParamTypes().end(); ++I) {
42 Slot = Table.getSlot(*I);
43 assert(Slot != -1 && "Type used but not available!!");
44 output_vbr((unsigned)Slot, Out);
47 // Terminate list with VoidTy if we are a varargs function...
49 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
53 case Type::ArrayTyID: {
54 const ArrayType *AT = cast<ArrayType>(T);
55 int Slot = Table.getSlot(AT->getElementType());
56 assert(Slot != -1 && "Type used but not available!!");
57 output_vbr((unsigned)Slot, Out);
58 //std::cerr << "Type slot = " << Slot << " Type = " << T->getName() << endl;
60 output_vbr(AT->getNumElements(), Out);
64 case Type::StructTyID: {
65 const StructType *ST = cast<StructType>(T);
67 // Output all of the element types...
68 StructType::ElementTypes::const_iterator I = ST->getElementTypes().begin();
69 for (; I != ST->getElementTypes().end(); ++I) {
70 int Slot = Table.getSlot(*I);
71 assert(Slot != -1 && "Type used but not available!!");
72 output_vbr((unsigned)Slot, Out);
75 // Terminate list with VoidTy
76 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
80 case Type::PointerTyID: {
81 const PointerType *PT = cast<PointerType>(T);
82 int Slot = Table.getSlot(PT->getElementType());
83 assert(Slot != -1 && "Type used but not available!!");
84 output_vbr((unsigned)Slot, Out);
88 case Type::OpaqueTyID: {
89 // No need to emit anything, just the count of opaque types is enough.
93 //case Type::PackedTyID:
95 std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
96 << " Type '" << T->getDescription() << "'\n";
101 bool BytecodeWriter::outputConstant(const Constant *CPV) {
102 assert((CPV->getType()->isPrimitiveType() || !CPV->isNullValue()) &&
103 "Shouldn't output null constants!");
105 // We must check for a ConstantExpr before switching by type because
106 // a ConstantExpr can be of any type, and has no explicit value.
108 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
109 // FIXME: Encoding of constant exprs could be much more compact!
110 assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
111 output_vbr(CE->getNumOperands(), Out); // flags as an expr
112 output_vbr(CE->getOpcode(), Out); // flags as an expr
114 for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
115 int Slot = Table.getSlot(*OI);
116 assert(Slot != -1 && "Unknown constant used in ConstantExpr!!");
117 output_vbr((unsigned)Slot, Out);
118 Slot = Table.getSlot((*OI)->getType());
119 output_vbr((unsigned)Slot, Out);
123 output_vbr((unsigned)0, Out); // flag as not a ConstantExpr
126 switch (CPV->getType()->getPrimitiveID()) {
127 case Type::BoolTyID: // Boolean Types
128 if (cast<ConstantBool>(CPV)->getValue())
134 case Type::UByteTyID: // Unsigned integer types...
135 case Type::UShortTyID:
137 case Type::ULongTyID:
138 output_vbr(cast<ConstantUInt>(CPV)->getValue(), Out);
141 case Type::SByteTyID: // Signed integer types...
142 case Type::ShortTyID:
145 output_vbr(cast<ConstantSInt>(CPV)->getValue(), Out);
148 case Type::TypeTyID: // Serialize type type
149 assert(0 && "Types should not be in the Constant!");
152 case Type::ArrayTyID: {
153 const ConstantArray *CPA = cast<ConstantArray>(CPV);
154 unsigned size = CPA->getValues().size();
155 assert(size == cast<ArrayType>(CPA->getType())->getNumElements()
156 && "ConstantArray out of whack!");
157 for (unsigned i = 0; i < size; 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 const ConstantPointerRef *CPR = cast<ConstantPointerRef>(CPV);
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";
206 } // End llvm namespace