blob: 12043b048a16eb966758de357a8080059a5260b0 [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 Lattner00950542001-06-06 20:29:01 +000029void BytecodeWriter::outputType(const Type *T) {
Chris Lattnerd6942d72004-01-14 16:54:21 +000030 TypeBytes -= Out.size();
Chris Lattner00950542001-06-06 20:29:01 +000031 output_vbr((unsigned)T->getPrimitiveID(), Out);
32
33 // That's all there is to handling primitive types...
Chris Lattnerd6942d72004-01-14 16:54:21 +000034 if (T->isPrimitiveType()) {
35 TypeBytes += Out.size();
Chris Lattner00950542001-06-06 20:29:01 +000036 return; // We might do this if we alias a prim type: %x = type int
Chris Lattnerd6942d72004-01-14 16:54:21 +000037 }
38
Chris Lattner00950542001-06-06 20:29:01 +000039 switch (T->getPrimitiveID()) { // Handle derived types now.
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000040 case Type::FunctionTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000041 const FunctionType *MT = cast<FunctionType>(T);
Alkis Evlogimenos60596382003-10-17 02:02:40 +000042 int Slot = Table.getSlot(MT->getReturnType());
Chris Lattner00950542001-06-06 20:29:01 +000043 assert(Slot != -1 && "Type used but not available!!");
44 output_vbr((unsigned)Slot, Out);
45
Chris Lattnere5a57ee2001-07-25 22:47:55 +000046 // Output the number of arguments to method (+1 if varargs):
Brian Gaekebc839b52003-10-29 20:09:01 +000047 output_vbr((unsigned)MT->getParamTypes().size()+MT->isVarArg(), Out);
Chris Lattnere5a57ee2001-07-25 22:47:55 +000048
Chris Lattner00950542001-06-06 20:29:01 +000049 // Output all of the arguments...
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000050 FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin();
Chris Lattner079df312001-06-27 23:34:01 +000051 for (; I != MT->getParamTypes().end(); ++I) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +000052 Slot = Table.getSlot(*I);
Chris Lattner00950542001-06-06 20:29:01 +000053 assert(Slot != -1 && "Type used but not available!!");
54 output_vbr((unsigned)Slot, Out);
55 }
56
Chris Lattnere5a57ee2001-07-25 22:47:55 +000057 // Terminate list with VoidTy if we are a varargs function...
58 if (MT->isVarArg())
59 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
Chris Lattner00950542001-06-06 20:29:01 +000060 break;
61 }
62
63 case Type::ArrayTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000064 const ArrayType *AT = cast<ArrayType>(T);
Alkis Evlogimenos60596382003-10-17 02:02:40 +000065 int Slot = Table.getSlot(AT->getElementType());
Chris Lattner00950542001-06-06 20:29:01 +000066 assert(Slot != -1 && "Type used but not available!!");
67 output_vbr((unsigned)Slot, Out);
Chris Lattner186a1f72003-03-19 20:56:46 +000068 //std::cerr << "Type slot = " << Slot << " Type = " << T->getName() << endl;
Chris Lattner00950542001-06-06 20:29:01 +000069
70 output_vbr(AT->getNumElements(), Out);
71 break;
72 }
73
74 case Type::StructTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000075 const StructType *ST = cast<StructType>(T);
Chris Lattner00950542001-06-06 20:29:01 +000076
77 // Output all of the element types...
78 StructType::ElementTypes::const_iterator I = ST->getElementTypes().begin();
Chris Lattner079df312001-06-27 23:34:01 +000079 for (; I != ST->getElementTypes().end(); ++I) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +000080 int Slot = Table.getSlot(*I);
Chris Lattner00950542001-06-06 20:29:01 +000081 assert(Slot != -1 && "Type used but not available!!");
82 output_vbr((unsigned)Slot, Out);
83 }
84
85 // Terminate list with VoidTy
86 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
87 break;
88 }
89
90 case Type::PointerTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000091 const PointerType *PT = cast<PointerType>(T);
Alkis Evlogimenos60596382003-10-17 02:02:40 +000092 int Slot = Table.getSlot(PT->getElementType());
Chris Lattner00950542001-06-06 20:29:01 +000093 assert(Slot != -1 && "Type used but not available!!");
94 output_vbr((unsigned)Slot, Out);
95 break;
96 }
97
Chris Lattner5855f0d2001-10-23 01:53:22 +000098 case Type::OpaqueTyID: {
99 // No need to emit anything, just the count of opaque types is enough.
100 break;
101 }
102
Chris Lattnere8fdde12001-09-07 16:39:41 +0000103 //case Type::PackedTyID:
Chris Lattner00950542001-06-06 20:29:01 +0000104 default:
Chris Lattner186a1f72003-03-19 20:56:46 +0000105 std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
106 << " Type '" << T->getDescription() << "'\n";
Chris Lattner00950542001-06-06 20:29:01 +0000107 break;
108 }
Chris Lattnerd6942d72004-01-14 16:54:21 +0000109 TypeBytes += Out.size();
Chris Lattner00950542001-06-06 20:29:01 +0000110}
111
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000112bool BytecodeWriter::outputConstant(const Constant *CPV) {
Chris Lattnerd6942d72004-01-14 16:54:21 +0000113 ConstantBytes -= Out.size();
Chris Lattner186a1f72003-03-19 20:56:46 +0000114 assert((CPV->getType()->isPrimitiveType() || !CPV->isNullValue()) &&
115 "Shouldn't output null constants!");
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000116
Chris Lattnerd6942d72004-01-14 16:54:21 +0000117 ++NumConstants;
118
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000119 // We must check for a ConstantExpr before switching by type because
120 // a ConstantExpr can be of any type, and has no explicit value.
121 //
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000122 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
123 // FIXME: Encoding of constant exprs could be much more compact!
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000124 assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
125 output_vbr(CE->getNumOperands(), Out); // flags as an expr
126 output_vbr(CE->getOpcode(), Out); // flags as an expr
127
128 for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000129 int Slot = Table.getSlot(*OI);
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000130 assert(Slot != -1 && "Unknown constant used in ConstantExpr!!");
131 output_vbr((unsigned)Slot, Out);
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000132 Slot = Table.getSlot((*OI)->getType());
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000133 output_vbr((unsigned)Slot, Out);
134 }
Chris Lattnerd6942d72004-01-14 16:54:21 +0000135 ConstantBytes += Out.size();
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000136 return false;
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000137 } else {
Chris Lattnerd6942d72004-01-14 16:54:21 +0000138 output_vbr(0U, Out); // flag as not a ConstantExpr
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000139 }
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000140
Chris Lattner00950542001-06-06 20:29:01 +0000141 switch (CPV->getType()->getPrimitiveID()) {
142 case Type::BoolTyID: // Boolean Types
Chris Lattner987cb2c2003-07-23 14:54:33 +0000143 if (cast<ConstantBool>(CPV)->getValue())
Chris Lattner186a1f72003-03-19 20:56:46 +0000144 output_vbr(1U, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000145 else
Chris Lattner186a1f72003-03-19 20:56:46 +0000146 output_vbr(0U, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000147 break;
148
149 case Type::UByteTyID: // Unsigned integer types...
150 case Type::UShortTyID:
151 case Type::UIntTyID:
152 case Type::ULongTyID:
Chris Lattner987cb2c2003-07-23 14:54:33 +0000153 output_vbr(cast<ConstantUInt>(CPV)->getValue(), Out);
Chris Lattner00950542001-06-06 20:29:01 +0000154 break;
155
156 case Type::SByteTyID: // Signed integer types...
157 case Type::ShortTyID:
158 case Type::IntTyID:
159 case Type::LongTyID:
Chris Lattner987cb2c2003-07-23 14:54:33 +0000160 output_vbr(cast<ConstantSInt>(CPV)->getValue(), Out);
Chris Lattner00950542001-06-06 20:29:01 +0000161 break;
162
163 case Type::TypeTyID: // Serialize type type
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000164 assert(0 && "Types should not be in the Constant!");
Chris Lattner00950542001-06-06 20:29:01 +0000165 break;
166
167 case Type::ArrayTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +0000168 const ConstantArray *CPA = cast<ConstantArray>(CPV);
Chris Lattner00950542001-06-06 20:29:01 +0000169 unsigned size = CPA->getValues().size();
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000170 assert(size == cast<ArrayType>(CPA->getType())->getNumElements()
171 && "ConstantArray out of whack!");
Chris Lattner00950542001-06-06 20:29:01 +0000172 for (unsigned i = 0; i < size; i++) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000173 int Slot = Table.getSlot(CPA->getOperand(i));
Chris Lattner00950542001-06-06 20:29:01 +0000174 assert(Slot != -1 && "Constant used but not available!!");
175 output_vbr((unsigned)Slot, Out);
176 }
177 break;
178 }
179
180 case Type::StructTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +0000181 const ConstantStruct *CPS = cast<ConstantStruct>(CPV);
Chris Lattner697954c2002-01-20 22:54:45 +0000182 const std::vector<Use> &Vals = CPS->getValues();
Chris Lattner00950542001-06-06 20:29:01 +0000183
184 for (unsigned i = 0; i < Vals.size(); ++i) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000185 int Slot = Table.getSlot(Vals[i]);
Chris Lattner00950542001-06-06 20:29:01 +0000186 assert(Slot != -1 && "Constant used but not available!!");
187 output_vbr((unsigned)Slot, Out);
Chris Lattnerd6942d72004-01-14 16:54:21 +0000188 }
Chris Lattner00950542001-06-06 20:29:01 +0000189 break;
190 }
191
Chris Lattner1a1cb112001-09-30 22:46:54 +0000192 case Type::PointerTyID: {
Chris Lattner02071d02003-11-17 17:28:29 +0000193 const ConstantPointerRef *CPR = cast<ConstantPointerRef>(CPV);
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000194 int Slot = Table.getSlot((Value*)CPR->getValue());
Chris Lattner186a1f72003-03-19 20:56:46 +0000195 assert(Slot != -1 && "Global used but not available!!");
196 output_vbr((unsigned)Slot, Out);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000197 break;
198 }
199
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000200 case Type::FloatTyID: { // Floating point types...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000201 float Tmp = (float)cast<ConstantFP>(CPV)->getValue();
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000202 output_data(&Tmp, &Tmp+1, Out);
203 break;
204 }
205 case Type::DoubleTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000206 double Tmp = cast<ConstantFP>(CPV)->getValue();
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000207 output_data(&Tmp, &Tmp+1, Out);
208 break;
209 }
Chris Lattner00950542001-06-06 20:29:01 +0000210
211 case Type::VoidTyID:
212 case Type::LabelTyID:
213 default:
Chris Lattner186a1f72003-03-19 20:56:46 +0000214 std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
215 << " type '" << CPV->getType()->getName() << "'\n";
Chris Lattner00950542001-06-06 20:29:01 +0000216 break;
217 }
Chris Lattnerd6942d72004-01-14 16:54:21 +0000218 ConstantBytes += Out.size();
Chris Lattner00950542001-06-06 20:29:01 +0000219 return false;
220}