blob: c0c4137db4eb3222a297fb97960ea9096fc658ac [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- ConstantWriter.cpp - Functions for writing constants --------------===//
Chris Lattner00950542001-06-06 20:29:01 +00002//
3// This file implements the routines for encoding constants to a bytecode
4// stream.
5//
Chris Lattner00950542001-06-06 20:29:01 +00006//===----------------------------------------------------------------------===//
7
8#include "WriterInternals.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +00009#include "llvm/Constants.h"
Chris Lattner00950542001-06-06 20:29:01 +000010#include "llvm/SymbolTable.h"
11#include "llvm/DerivedTypes.h"
12
13void BytecodeWriter::outputType(const Type *T) {
14 output_vbr((unsigned)T->getPrimitiveID(), Out);
15
16 // That's all there is to handling primitive types...
17 if (T->isPrimitiveType())
18 return; // We might do this if we alias a prim type: %x = type int
19
20 switch (T->getPrimitiveID()) { // Handle derived types now.
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000021 case Type::FunctionTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000022 const FunctionType *MT = cast<FunctionType>(T);
Alkis Evlogimenos60596382003-10-17 02:02:40 +000023 int Slot = Table.getSlot(MT->getReturnType());
Chris Lattner00950542001-06-06 20:29:01 +000024 assert(Slot != -1 && "Type used but not available!!");
25 output_vbr((unsigned)Slot, Out);
26
Chris Lattnere5a57ee2001-07-25 22:47:55 +000027 // Output the number of arguments to method (+1 if varargs):
28 output_vbr(MT->getParamTypes().size()+MT->isVarArg(), Out);
29
Chris Lattner00950542001-06-06 20:29:01 +000030 // Output all of the arguments...
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000031 FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin();
Chris Lattner079df312001-06-27 23:34:01 +000032 for (; I != MT->getParamTypes().end(); ++I) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +000033 Slot = Table.getSlot(*I);
Chris Lattner00950542001-06-06 20:29:01 +000034 assert(Slot != -1 && "Type used but not available!!");
35 output_vbr((unsigned)Slot, Out);
36 }
37
Chris Lattnere5a57ee2001-07-25 22:47:55 +000038 // Terminate list with VoidTy if we are a varargs function...
39 if (MT->isVarArg())
40 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
Chris Lattner00950542001-06-06 20:29:01 +000041 break;
42 }
43
44 case Type::ArrayTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000045 const ArrayType *AT = cast<ArrayType>(T);
Alkis Evlogimenos60596382003-10-17 02:02:40 +000046 int Slot = Table.getSlot(AT->getElementType());
Chris Lattner00950542001-06-06 20:29:01 +000047 assert(Slot != -1 && "Type used but not available!!");
48 output_vbr((unsigned)Slot, Out);
Chris Lattner186a1f72003-03-19 20:56:46 +000049 //std::cerr << "Type slot = " << Slot << " Type = " << T->getName() << endl;
Chris Lattner00950542001-06-06 20:29:01 +000050
51 output_vbr(AT->getNumElements(), Out);
52 break;
53 }
54
55 case Type::StructTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000056 const StructType *ST = cast<StructType>(T);
Chris Lattner00950542001-06-06 20:29:01 +000057
58 // Output all of the element types...
59 StructType::ElementTypes::const_iterator I = ST->getElementTypes().begin();
Chris Lattner079df312001-06-27 23:34:01 +000060 for (; I != ST->getElementTypes().end(); ++I) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +000061 int Slot = Table.getSlot(*I);
Chris Lattner00950542001-06-06 20:29:01 +000062 assert(Slot != -1 && "Type used but not available!!");
63 output_vbr((unsigned)Slot, Out);
64 }
65
66 // Terminate list with VoidTy
67 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
68 break;
69 }
70
71 case Type::PointerTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000072 const PointerType *PT = cast<PointerType>(T);
Alkis Evlogimenos60596382003-10-17 02:02:40 +000073 int Slot = Table.getSlot(PT->getElementType());
Chris Lattner00950542001-06-06 20:29:01 +000074 assert(Slot != -1 && "Type used but not available!!");
75 output_vbr((unsigned)Slot, Out);
76 break;
77 }
78
Chris Lattner5855f0d2001-10-23 01:53:22 +000079 case Type::OpaqueTyID: {
80 // No need to emit anything, just the count of opaque types is enough.
81 break;
82 }
83
Chris Lattnere8fdde12001-09-07 16:39:41 +000084 //case Type::PackedTyID:
Chris Lattner00950542001-06-06 20:29:01 +000085 default:
Chris Lattner186a1f72003-03-19 20:56:46 +000086 std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
87 << " Type '" << T->getDescription() << "'\n";
Chris Lattner00950542001-06-06 20:29:01 +000088 break;
89 }
90}
91
Chris Lattnere9bb2df2001-12-03 22:26:30 +000092bool BytecodeWriter::outputConstant(const Constant *CPV) {
Chris Lattner186a1f72003-03-19 20:56:46 +000093 assert((CPV->getType()->isPrimitiveType() || !CPV->isNullValue()) &&
94 "Shouldn't output null constants!");
Vikram S. Advea24a0bb2002-07-14 23:08:30 +000095
96 // We must check for a ConstantExpr before switching by type because
97 // a ConstantExpr can be of any type, and has no explicit value.
98 //
Chris Lattnerc188eeb2002-07-30 18:54:25 +000099 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
100 // FIXME: Encoding of constant exprs could be much more compact!
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000101 assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
102 output_vbr(CE->getNumOperands(), Out); // flags as an expr
103 output_vbr(CE->getOpcode(), Out); // flags as an expr
104
105 for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000106 int Slot = Table.getSlot(*OI);
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000107 assert(Slot != -1 && "Unknown constant used in ConstantExpr!!");
108 output_vbr((unsigned)Slot, Out);
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000109 Slot = Table.getSlot((*OI)->getType());
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000110 output_vbr((unsigned)Slot, Out);
111 }
112 return false;
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000113 } else {
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000114 output_vbr((unsigned)0, Out); // flag as not a ConstantExpr
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000115 }
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000116
Chris Lattner00950542001-06-06 20:29:01 +0000117 switch (CPV->getType()->getPrimitiveID()) {
118 case Type::BoolTyID: // Boolean Types
Chris Lattner987cb2c2003-07-23 14:54:33 +0000119 if (cast<ConstantBool>(CPV)->getValue())
Chris Lattner186a1f72003-03-19 20:56:46 +0000120 output_vbr(1U, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000121 else
Chris Lattner186a1f72003-03-19 20:56:46 +0000122 output_vbr(0U, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000123 break;
124
125 case Type::UByteTyID: // Unsigned integer types...
126 case Type::UShortTyID:
127 case Type::UIntTyID:
128 case Type::ULongTyID:
Chris Lattner987cb2c2003-07-23 14:54:33 +0000129 output_vbr(cast<ConstantUInt>(CPV)->getValue(), Out);
Chris Lattner00950542001-06-06 20:29:01 +0000130 break;
131
132 case Type::SByteTyID: // Signed integer types...
133 case Type::ShortTyID:
134 case Type::IntTyID:
135 case Type::LongTyID:
Chris Lattner987cb2c2003-07-23 14:54:33 +0000136 output_vbr(cast<ConstantSInt>(CPV)->getValue(), Out);
Chris Lattner00950542001-06-06 20:29:01 +0000137 break;
138
139 case Type::TypeTyID: // Serialize type type
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000140 assert(0 && "Types should not be in the Constant!");
Chris Lattner00950542001-06-06 20:29:01 +0000141 break;
142
143 case Type::ArrayTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +0000144 const ConstantArray *CPA = cast<ConstantArray>(CPV);
Chris Lattner00950542001-06-06 20:29:01 +0000145 unsigned size = CPA->getValues().size();
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000146 assert(size == cast<ArrayType>(CPA->getType())->getNumElements()
147 && "ConstantArray out of whack!");
Chris Lattner00950542001-06-06 20:29:01 +0000148 for (unsigned i = 0; i < size; i++) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000149 int Slot = Table.getSlot(CPA->getOperand(i));
Chris Lattner00950542001-06-06 20:29:01 +0000150 assert(Slot != -1 && "Constant used but not available!!");
151 output_vbr((unsigned)Slot, Out);
152 }
153 break;
154 }
155
156 case Type::StructTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +0000157 const ConstantStruct *CPS = cast<ConstantStruct>(CPV);
Chris Lattner697954c2002-01-20 22:54:45 +0000158 const std::vector<Use> &Vals = CPS->getValues();
Chris Lattner00950542001-06-06 20:29:01 +0000159
160 for (unsigned i = 0; i < Vals.size(); ++i) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000161 int Slot = Table.getSlot(Vals[i]);
Chris Lattner00950542001-06-06 20:29:01 +0000162 assert(Slot != -1 && "Constant used but not available!!");
163 output_vbr((unsigned)Slot, Out);
164 }
165 break;
166 }
167
Chris Lattner1a1cb112001-09-30 22:46:54 +0000168 case Type::PointerTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +0000169 const ConstantPointer *CPP = cast<ConstantPointer>(CPV);
Chris Lattner186a1f72003-03-19 20:56:46 +0000170 assert(!isa<ConstantPointerNull>(CPP) && "Null should be already emitted!");
171 const ConstantPointerRef *CPR = cast<ConstantPointerRef>(CPP);
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000172 int Slot = Table.getSlot((Value*)CPR->getValue());
Chris Lattner186a1f72003-03-19 20:56:46 +0000173 assert(Slot != -1 && "Global used but not available!!");
174 output_vbr((unsigned)Slot, Out);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000175 break;
176 }
177
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000178 case Type::FloatTyID: { // Floating point types...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000179 float Tmp = (float)cast<ConstantFP>(CPV)->getValue();
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000180 output_data(&Tmp, &Tmp+1, Out);
181 break;
182 }
183 case Type::DoubleTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000184 double Tmp = cast<ConstantFP>(CPV)->getValue();
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000185 output_data(&Tmp, &Tmp+1, Out);
186 break;
187 }
Chris Lattner00950542001-06-06 20:29:01 +0000188
189 case Type::VoidTyID:
190 case Type::LabelTyID:
191 default:
Chris Lattner186a1f72003-03-19 20:56:46 +0000192 std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
193 << " type '" << CPV->getType()->getName() << "'\n";
Chris Lattner00950542001-06-06 20:29:01 +0000194 break;
195 }
196 return false;
197}