blob: bb8d286899ac4e1a22c2c4d81b553cf7a10c95cd [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 Lattner00950542001-06-06 20:29:01 +000022void BytecodeWriter::outputType(const Type *T) {
23 output_vbr((unsigned)T->getPrimitiveID(), Out);
24
25 // That's all there is to handling primitive types...
Chris Lattnerd6942d72004-01-14 16:54:21 +000026 if (T->isPrimitiveType()) {
Chris Lattner00950542001-06-06 20:29:01 +000027 return; // We might do this if we alias a prim type: %x = type int
Chris Lattnerd6942d72004-01-14 16:54:21 +000028 }
29
Chris Lattner00950542001-06-06 20:29:01 +000030 switch (T->getPrimitiveID()) { // Handle derived types now.
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000031 case Type::FunctionTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000032 const FunctionType *MT = cast<FunctionType>(T);
Alkis Evlogimenos60596382003-10-17 02:02:40 +000033 int Slot = Table.getSlot(MT->getReturnType());
Chris Lattner00950542001-06-06 20:29:01 +000034 assert(Slot != -1 && "Type used but not available!!");
35 output_vbr((unsigned)Slot, Out);
36
Chris Lattnerd5d89962004-02-09 04:14:01 +000037 // Output the number of arguments to function (+1 if varargs):
38 output_vbr((unsigned)MT->getNumParams()+MT->isVarArg(), Out);
Chris Lattnere5a57ee2001-07-25 22:47:55 +000039
Chris Lattner00950542001-06-06 20:29:01 +000040 // Output all of the arguments...
Chris Lattnerd5d89962004-02-09 04:14:01 +000041 FunctionType::param_iterator I = MT->param_begin();
42 for (; I != MT->param_end(); ++I) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +000043 Slot = Table.getSlot(*I);
Chris Lattner00950542001-06-06 20:29:01 +000044 assert(Slot != -1 && "Type used but not available!!");
45 output_vbr((unsigned)Slot, Out);
46 }
47
Chris Lattnere5a57ee2001-07-25 22:47:55 +000048 // Terminate list with VoidTy if we are a varargs function...
49 if (MT->isVarArg())
50 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
Chris Lattner00950542001-06-06 20:29:01 +000051 break;
52 }
53
54 case Type::ArrayTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000055 const ArrayType *AT = cast<ArrayType>(T);
Alkis Evlogimenos60596382003-10-17 02:02:40 +000056 int Slot = Table.getSlot(AT->getElementType());
Chris Lattner00950542001-06-06 20:29:01 +000057 assert(Slot != -1 && "Type used but not available!!");
58 output_vbr((unsigned)Slot, Out);
Chris Lattner186a1f72003-03-19 20:56:46 +000059 //std::cerr << "Type slot = " << Slot << " Type = " << T->getName() << endl;
Chris Lattner00950542001-06-06 20:29:01 +000060
61 output_vbr(AT->getNumElements(), Out);
62 break;
63 }
64
65 case Type::StructTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000066 const StructType *ST = cast<StructType>(T);
Chris Lattner00950542001-06-06 20:29:01 +000067
68 // Output all of the element types...
Chris Lattnerd21cd802004-02-09 04:37:31 +000069 for (StructType::element_iterator I = ST->element_begin(),
70 E = ST->element_end(); I != E; ++I) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +000071 int Slot = Table.getSlot(*I);
Chris Lattner00950542001-06-06 20:29:01 +000072 assert(Slot != -1 && "Type used but not available!!");
73 output_vbr((unsigned)Slot, Out);
74 }
75
76 // Terminate list with VoidTy
77 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
78 break;
79 }
80
81 case Type::PointerTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000082 const PointerType *PT = cast<PointerType>(T);
Alkis Evlogimenos60596382003-10-17 02:02:40 +000083 int Slot = Table.getSlot(PT->getElementType());
Chris Lattner00950542001-06-06 20:29:01 +000084 assert(Slot != -1 && "Type used but not available!!");
85 output_vbr((unsigned)Slot, Out);
86 break;
87 }
88
Chris Lattner5855f0d2001-10-23 01:53:22 +000089 case Type::OpaqueTyID: {
90 // No need to emit anything, just the count of opaque types is enough.
91 break;
92 }
93
Chris Lattnere8fdde12001-09-07 16:39:41 +000094 //case Type::PackedTyID:
Chris Lattner00950542001-06-06 20:29:01 +000095 default:
Chris Lattner186a1f72003-03-19 20:56:46 +000096 std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
97 << " Type '" << T->getDescription() << "'\n";
Chris Lattner00950542001-06-06 20:29:01 +000098 break;
99 }
100}
101
Chris Lattner83bb3d22004-01-14 23:36:54 +0000102void BytecodeWriter::outputConstant(const Constant *CPV) {
Chris Lattner186a1f72003-03-19 20:56:46 +0000103 assert((CPV->getType()->isPrimitiveType() || !CPV->isNullValue()) &&
104 "Shouldn't output null constants!");
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000105
106 // We must check for a ConstantExpr before switching by type because
107 // a ConstantExpr can be of any type, and has no explicit value.
108 //
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000109 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
110 // FIXME: Encoding of constant exprs could be much more compact!
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000111 assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
112 output_vbr(CE->getNumOperands(), Out); // flags as an expr
113 output_vbr(CE->getOpcode(), Out); // flags as an expr
114
115 for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000116 int Slot = Table.getSlot(*OI);
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000117 assert(Slot != -1 && "Unknown constant used in ConstantExpr!!");
118 output_vbr((unsigned)Slot, Out);
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000119 Slot = Table.getSlot((*OI)->getType());
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000120 output_vbr((unsigned)Slot, Out);
121 }
Chris Lattner83bb3d22004-01-14 23:36:54 +0000122 return;
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000123 } else {
Chris Lattnerd6942d72004-01-14 16:54:21 +0000124 output_vbr(0U, Out); // flag as not a ConstantExpr
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000125 }
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000126
Chris Lattner00950542001-06-06 20:29:01 +0000127 switch (CPV->getType()->getPrimitiveID()) {
128 case Type::BoolTyID: // Boolean Types
Chris Lattner987cb2c2003-07-23 14:54:33 +0000129 if (cast<ConstantBool>(CPV)->getValue())
Chris Lattner186a1f72003-03-19 20:56:46 +0000130 output_vbr(1U, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000131 else
Chris Lattner186a1f72003-03-19 20:56:46 +0000132 output_vbr(0U, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000133 break;
134
135 case Type::UByteTyID: // Unsigned integer types...
136 case Type::UShortTyID:
137 case Type::UIntTyID:
138 case Type::ULongTyID:
Chris Lattner987cb2c2003-07-23 14:54:33 +0000139 output_vbr(cast<ConstantUInt>(CPV)->getValue(), Out);
Chris Lattner00950542001-06-06 20:29:01 +0000140 break;
141
142 case Type::SByteTyID: // Signed integer types...
143 case Type::ShortTyID:
144 case Type::IntTyID:
145 case Type::LongTyID:
Chris Lattner987cb2c2003-07-23 14:54:33 +0000146 output_vbr(cast<ConstantSInt>(CPV)->getValue(), Out);
Chris Lattner00950542001-06-06 20:29:01 +0000147 break;
148
149 case Type::TypeTyID: // Serialize type type
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000150 assert(0 && "Types should not be in the Constant!");
Chris Lattner00950542001-06-06 20:29:01 +0000151 break;
152
153 case Type::ArrayTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +0000154 const ConstantArray *CPA = cast<ConstantArray>(CPV);
Chris Lattner83bb3d22004-01-14 23:36:54 +0000155 assert(!CPA->isString() && "Constant strings should be handled specially!");
156
157 for (unsigned i = 0; i != CPA->getNumOperands(); ++i) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000158 int Slot = Table.getSlot(CPA->getOperand(i));
Chris Lattner00950542001-06-06 20:29:01 +0000159 assert(Slot != -1 && "Constant used but not available!!");
160 output_vbr((unsigned)Slot, Out);
161 }
162 break;
163 }
164
165 case Type::StructTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +0000166 const ConstantStruct *CPS = cast<ConstantStruct>(CPV);
Chris Lattner697954c2002-01-20 22:54:45 +0000167 const std::vector<Use> &Vals = CPS->getValues();
Chris Lattner00950542001-06-06 20:29:01 +0000168
169 for (unsigned i = 0; i < Vals.size(); ++i) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000170 int Slot = Table.getSlot(Vals[i]);
Chris Lattner00950542001-06-06 20:29:01 +0000171 assert(Slot != -1 && "Constant used but not available!!");
172 output_vbr((unsigned)Slot, Out);
Chris Lattnerd6942d72004-01-14 16:54:21 +0000173 }
Chris Lattner00950542001-06-06 20:29:01 +0000174 break;
175 }
176
Chris Lattner7be08bf2004-01-15 18:46:56 +0000177 case Type::PointerTyID:
178 assert(0 && "No non-null, non-constant-expr constants allowed!");
179 abort();
Chris Lattner1a1cb112001-09-30 22:46:54 +0000180
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000181 case Type::FloatTyID: { // Floating point types...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000182 float Tmp = (float)cast<ConstantFP>(CPV)->getValue();
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000183 output_data(&Tmp, &Tmp+1, Out);
184 break;
185 }
186 case Type::DoubleTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000187 double Tmp = cast<ConstantFP>(CPV)->getValue();
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000188 output_data(&Tmp, &Tmp+1, Out);
189 break;
190 }
Chris Lattner00950542001-06-06 20:29:01 +0000191
192 case Type::VoidTyID:
193 case Type::LabelTyID:
194 default:
Chris Lattner186a1f72003-03-19 20:56:46 +0000195 std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
196 << " type '" << CPV->getType()->getName() << "'\n";
Chris Lattner00950542001-06-06 20:29:01 +0000197 break;
198 }
Chris Lattner83bb3d22004-01-14 23:36:54 +0000199 return;
200}
201
202void BytecodeWriter::outputConstantStrings() {
203 SlotCalculator::string_iterator I = Table.string_begin();
204 SlotCalculator::string_iterator E = Table.string_end();
205 if (I == E) return; // No strings to emit
206
207 // If we have != 0 strings to emit, output them now. Strings are emitted into
208 // the 'void' type plane.
209 output_vbr(unsigned(E-I), Out);
210 output_vbr(Type::VoidTyID, Out);
211
Chris Lattner83bb3d22004-01-14 23:36:54 +0000212 // Emit all of the strings.
213 for (I = Table.string_begin(); I != E; ++I) {
214 const ConstantArray *Str = *I;
215 int Slot = Table.getSlot(Str->getType());
216 assert(Slot != -1 && "Constant string of unknown type?");
217 output_vbr((unsigned)Slot, Out);
218
219 // Now that we emitted the type (which indicates the size of the string),
220 // emit all of the characters.
221 std::string Val = Str->getAsString();
222 output_data(Val.c_str(), Val.c_str()+Val.size(), Out);
Chris Lattner83bb3d22004-01-14 23:36:54 +0000223 }
Chris Lattner00950542001-06-06 20:29:01 +0000224}