blob: 088a5b787bc11cf844a08ab9f685802b9aa6d8a2 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- ConstantWriter.cpp - Functions for writing constants --------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the routines for encoding constants to a bytecode
11// stream.
12//
Chris Lattner00950542001-06-06 20:29:01 +000013//===----------------------------------------------------------------------===//
14
15#include "WriterInternals.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000016#include "llvm/Constants.h"
Chris Lattner00950542001-06-06 20:29:01 +000017#include "llvm/SymbolTable.h"
18#include "llvm/DerivedTypes.h"
Chris Lattnerd6942d72004-01-14 16:54:21 +000019#include "Support/Statistic.h"
Chris Lattner44f549b2004-01-10 18:49:43 +000020using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000021
Chris Lattnerd6942d72004-01-14 16:54:21 +000022static Statistic<>
23TypeBytes("bytecodewriter", "Bytes of types");
24static Statistic<>
25ConstantBytes("bytecodewriter", "Bytes of constants");
26static Statistic<>
27NumConstants("bytecodewriter", "Number of constants");
28
Chris Lattner83bb3d22004-01-14 23:36:54 +000029static Statistic<>
30NumStrConstants("bytecodewriter", "Number of string constants");
31static Statistic<>
32NumStrBytes("bytecodewriter", "Number of string constant bytes");
33
34
Chris Lattner00950542001-06-06 20:29:01 +000035void BytecodeWriter::outputType(const Type *T) {
Chris Lattnerd6942d72004-01-14 16:54:21 +000036 TypeBytes -= Out.size();
Chris Lattner00950542001-06-06 20:29:01 +000037 output_vbr((unsigned)T->getPrimitiveID(), Out);
38
39 // That's all there is to handling primitive types...
Chris Lattnerd6942d72004-01-14 16:54:21 +000040 if (T->isPrimitiveType()) {
41 TypeBytes += Out.size();
Chris Lattner00950542001-06-06 20:29:01 +000042 return; // We might do this if we alias a prim type: %x = type int
Chris Lattnerd6942d72004-01-14 16:54:21 +000043 }
44
Chris Lattner00950542001-06-06 20:29:01 +000045 switch (T->getPrimitiveID()) { // Handle derived types now.
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000046 case Type::FunctionTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000047 const FunctionType *MT = cast<FunctionType>(T);
Alkis Evlogimenos60596382003-10-17 02:02:40 +000048 int Slot = Table.getSlot(MT->getReturnType());
Chris Lattner00950542001-06-06 20:29:01 +000049 assert(Slot != -1 && "Type used but not available!!");
50 output_vbr((unsigned)Slot, Out);
51
Chris Lattnere5a57ee2001-07-25 22:47:55 +000052 // Output the number of arguments to method (+1 if varargs):
Brian Gaekebc839b52003-10-29 20:09:01 +000053 output_vbr((unsigned)MT->getParamTypes().size()+MT->isVarArg(), Out);
Chris Lattnere5a57ee2001-07-25 22:47:55 +000054
Chris Lattner00950542001-06-06 20:29:01 +000055 // Output all of the arguments...
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000056 FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin();
Chris Lattner079df312001-06-27 23:34:01 +000057 for (; I != MT->getParamTypes().end(); ++I) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +000058 Slot = Table.getSlot(*I);
Chris Lattner00950542001-06-06 20:29:01 +000059 assert(Slot != -1 && "Type used but not available!!");
60 output_vbr((unsigned)Slot, Out);
61 }
62
Chris Lattnere5a57ee2001-07-25 22:47:55 +000063 // Terminate list with VoidTy if we are a varargs function...
64 if (MT->isVarArg())
65 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
Chris Lattner00950542001-06-06 20:29:01 +000066 break;
67 }
68
69 case Type::ArrayTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000070 const ArrayType *AT = cast<ArrayType>(T);
Alkis Evlogimenos60596382003-10-17 02:02:40 +000071 int Slot = Table.getSlot(AT->getElementType());
Chris Lattner00950542001-06-06 20:29:01 +000072 assert(Slot != -1 && "Type used but not available!!");
73 output_vbr((unsigned)Slot, Out);
Chris Lattner186a1f72003-03-19 20:56:46 +000074 //std::cerr << "Type slot = " << Slot << " Type = " << T->getName() << endl;
Chris Lattner00950542001-06-06 20:29:01 +000075
76 output_vbr(AT->getNumElements(), Out);
77 break;
78 }
79
80 case Type::StructTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000081 const StructType *ST = cast<StructType>(T);
Chris Lattner00950542001-06-06 20:29:01 +000082
83 // Output all of the element types...
84 StructType::ElementTypes::const_iterator I = ST->getElementTypes().begin();
Chris Lattner079df312001-06-27 23:34:01 +000085 for (; I != ST->getElementTypes().end(); ++I) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +000086 int Slot = Table.getSlot(*I);
Chris Lattner00950542001-06-06 20:29:01 +000087 assert(Slot != -1 && "Type used but not available!!");
88 output_vbr((unsigned)Slot, Out);
89 }
90
91 // Terminate list with VoidTy
92 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
93 break;
94 }
95
96 case Type::PointerTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000097 const PointerType *PT = cast<PointerType>(T);
Alkis Evlogimenos60596382003-10-17 02:02:40 +000098 int Slot = Table.getSlot(PT->getElementType());
Chris Lattner00950542001-06-06 20:29:01 +000099 assert(Slot != -1 && "Type used but not available!!");
100 output_vbr((unsigned)Slot, Out);
101 break;
102 }
103
Chris Lattner5855f0d2001-10-23 01:53:22 +0000104 case Type::OpaqueTyID: {
105 // No need to emit anything, just the count of opaque types is enough.
106 break;
107 }
108
Chris Lattnere8fdde12001-09-07 16:39:41 +0000109 //case Type::PackedTyID:
Chris Lattner00950542001-06-06 20:29:01 +0000110 default:
Chris Lattner186a1f72003-03-19 20:56:46 +0000111 std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
112 << " Type '" << T->getDescription() << "'\n";
Chris Lattner00950542001-06-06 20:29:01 +0000113 break;
114 }
Chris Lattnerd6942d72004-01-14 16:54:21 +0000115 TypeBytes += Out.size();
Chris Lattner00950542001-06-06 20:29:01 +0000116}
117
Chris Lattner83bb3d22004-01-14 23:36:54 +0000118void BytecodeWriter::outputConstant(const Constant *CPV) {
Chris Lattnerd6942d72004-01-14 16:54:21 +0000119 ConstantBytes -= Out.size();
Chris Lattner186a1f72003-03-19 20:56:46 +0000120 assert((CPV->getType()->isPrimitiveType() || !CPV->isNullValue()) &&
121 "Shouldn't output null constants!");
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000122
Chris Lattnerd6942d72004-01-14 16:54:21 +0000123 ++NumConstants;
124
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000125 // We must check for a ConstantExpr before switching by type because
126 // a ConstantExpr can be of any type, and has no explicit value.
127 //
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000128 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
129 // FIXME: Encoding of constant exprs could be much more compact!
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000130 assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
131 output_vbr(CE->getNumOperands(), Out); // flags as an expr
132 output_vbr(CE->getOpcode(), Out); // flags as an expr
133
134 for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000135 int Slot = Table.getSlot(*OI);
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000136 assert(Slot != -1 && "Unknown constant used in ConstantExpr!!");
137 output_vbr((unsigned)Slot, Out);
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000138 Slot = Table.getSlot((*OI)->getType());
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000139 output_vbr((unsigned)Slot, Out);
140 }
Chris Lattnerd6942d72004-01-14 16:54:21 +0000141 ConstantBytes += Out.size();
Chris Lattner83bb3d22004-01-14 23:36:54 +0000142 return;
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000143 } else {
Chris Lattnerd6942d72004-01-14 16:54:21 +0000144 output_vbr(0U, Out); // flag as not a ConstantExpr
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000145 }
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000146
Chris Lattner00950542001-06-06 20:29:01 +0000147 switch (CPV->getType()->getPrimitiveID()) {
148 case Type::BoolTyID: // Boolean Types
Chris Lattner987cb2c2003-07-23 14:54:33 +0000149 if (cast<ConstantBool>(CPV)->getValue())
Chris Lattner186a1f72003-03-19 20:56:46 +0000150 output_vbr(1U, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000151 else
Chris Lattner186a1f72003-03-19 20:56:46 +0000152 output_vbr(0U, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000153 break;
154
155 case Type::UByteTyID: // Unsigned integer types...
156 case Type::UShortTyID:
157 case Type::UIntTyID:
158 case Type::ULongTyID:
Chris Lattner987cb2c2003-07-23 14:54:33 +0000159 output_vbr(cast<ConstantUInt>(CPV)->getValue(), Out);
Chris Lattner00950542001-06-06 20:29:01 +0000160 break;
161
162 case Type::SByteTyID: // Signed integer types...
163 case Type::ShortTyID:
164 case Type::IntTyID:
165 case Type::LongTyID:
Chris Lattner987cb2c2003-07-23 14:54:33 +0000166 output_vbr(cast<ConstantSInt>(CPV)->getValue(), Out);
Chris Lattner00950542001-06-06 20:29:01 +0000167 break;
168
169 case Type::TypeTyID: // Serialize type type
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000170 assert(0 && "Types should not be in the Constant!");
Chris Lattner00950542001-06-06 20:29:01 +0000171 break;
172
173 case Type::ArrayTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +0000174 const ConstantArray *CPA = cast<ConstantArray>(CPV);
Chris Lattner83bb3d22004-01-14 23:36:54 +0000175 assert(!CPA->isString() && "Constant strings should be handled specially!");
176
177 for (unsigned i = 0; i != CPA->getNumOperands(); ++i) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000178 int Slot = Table.getSlot(CPA->getOperand(i));
Chris Lattner00950542001-06-06 20:29:01 +0000179 assert(Slot != -1 && "Constant used but not available!!");
180 output_vbr((unsigned)Slot, Out);
181 }
182 break;
183 }
184
185 case Type::StructTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +0000186 const ConstantStruct *CPS = cast<ConstantStruct>(CPV);
Chris Lattner697954c2002-01-20 22:54:45 +0000187 const std::vector<Use> &Vals = CPS->getValues();
Chris Lattner00950542001-06-06 20:29:01 +0000188
189 for (unsigned i = 0; i < Vals.size(); ++i) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000190 int Slot = Table.getSlot(Vals[i]);
Chris Lattner00950542001-06-06 20:29:01 +0000191 assert(Slot != -1 && "Constant used but not available!!");
192 output_vbr((unsigned)Slot, Out);
Chris Lattnerd6942d72004-01-14 16:54:21 +0000193 }
Chris Lattner00950542001-06-06 20:29:01 +0000194 break;
195 }
196
Chris Lattner7be08bf2004-01-15 18:46:56 +0000197 case Type::PointerTyID:
198 assert(0 && "No non-null, non-constant-expr constants allowed!");
199 abort();
Chris Lattner1a1cb112001-09-30 22:46:54 +0000200
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000201 case Type::FloatTyID: { // Floating point types...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000202 float Tmp = (float)cast<ConstantFP>(CPV)->getValue();
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000203 output_data(&Tmp, &Tmp+1, Out);
204 break;
205 }
206 case Type::DoubleTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000207 double Tmp = cast<ConstantFP>(CPV)->getValue();
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000208 output_data(&Tmp, &Tmp+1, Out);
209 break;
210 }
Chris Lattner00950542001-06-06 20:29:01 +0000211
212 case Type::VoidTyID:
213 case Type::LabelTyID:
214 default:
Chris Lattner186a1f72003-03-19 20:56:46 +0000215 std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
216 << " type '" << CPV->getType()->getName() << "'\n";
Chris Lattner00950542001-06-06 20:29:01 +0000217 break;
218 }
Chris Lattner83bb3d22004-01-14 23:36:54 +0000219 ConstantBytes += Out.size();
220 return;
221}
222
223void BytecodeWriter::outputConstantStrings() {
224 SlotCalculator::string_iterator I = Table.string_begin();
225 SlotCalculator::string_iterator E = Table.string_end();
226 if (I == E) return; // No strings to emit
227
228 // If we have != 0 strings to emit, output them now. Strings are emitted into
229 // the 'void' type plane.
230 output_vbr(unsigned(E-I), Out);
231 output_vbr(Type::VoidTyID, Out);
232
233 ConstantBytes -= Out.size();
234 NumStrBytes -= Out.size();;
235
236 // Emit all of the strings.
237 for (I = Table.string_begin(); I != E; ++I) {
238 const ConstantArray *Str = *I;
239 int Slot = Table.getSlot(Str->getType());
240 assert(Slot != -1 && "Constant string of unknown type?");
241 output_vbr((unsigned)Slot, Out);
242
243 // Now that we emitted the type (which indicates the size of the string),
244 // emit all of the characters.
245 std::string Val = Str->getAsString();
246 output_data(Val.c_str(), Val.c_str()+Val.size(), Out);
247
248 ++NumConstants;
249 ++NumStrConstants;
250 }
251 ConstantBytes += Out.size();
252 NumStrBytes += Out.size();;
Chris Lattner00950542001-06-06 20:29:01 +0000253}