blob: 30d93c9624273aaac7f74947acef90ac68aaea2b [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 Lattner44f549b2004-01-10 18:49:43 +000019using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000020
Chris Lattner00950542001-06-06 20:29:01 +000021void BytecodeWriter::outputType(const Type *T) {
22 output_vbr((unsigned)T->getPrimitiveID(), Out);
23
24 // That's all there is to handling primitive types...
25 if (T->isPrimitiveType())
26 return; // We might do this if we alias a prim type: %x = type int
27
28 switch (T->getPrimitiveID()) { // Handle derived types now.
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000029 case Type::FunctionTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000030 const FunctionType *MT = cast<FunctionType>(T);
Alkis Evlogimenos60596382003-10-17 02:02:40 +000031 int Slot = Table.getSlot(MT->getReturnType());
Chris Lattner00950542001-06-06 20:29:01 +000032 assert(Slot != -1 && "Type used but not available!!");
33 output_vbr((unsigned)Slot, Out);
34
Chris Lattnere5a57ee2001-07-25 22:47:55 +000035 // Output the number of arguments to method (+1 if varargs):
Brian Gaekebc839b52003-10-29 20:09:01 +000036 output_vbr((unsigned)MT->getParamTypes().size()+MT->isVarArg(), Out);
Chris Lattnere5a57ee2001-07-25 22:47:55 +000037
Chris Lattner00950542001-06-06 20:29:01 +000038 // Output all of the arguments...
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000039 FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin();
Chris Lattner079df312001-06-27 23:34:01 +000040 for (; I != MT->getParamTypes().end(); ++I) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +000041 Slot = Table.getSlot(*I);
Chris Lattner00950542001-06-06 20:29:01 +000042 assert(Slot != -1 && "Type used but not available!!");
43 output_vbr((unsigned)Slot, Out);
44 }
45
Chris Lattnere5a57ee2001-07-25 22:47:55 +000046 // Terminate list with VoidTy if we are a varargs function...
47 if (MT->isVarArg())
48 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
Chris Lattner00950542001-06-06 20:29:01 +000049 break;
50 }
51
52 case Type::ArrayTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000053 const ArrayType *AT = cast<ArrayType>(T);
Alkis Evlogimenos60596382003-10-17 02:02:40 +000054 int Slot = Table.getSlot(AT->getElementType());
Chris Lattner00950542001-06-06 20:29:01 +000055 assert(Slot != -1 && "Type used but not available!!");
56 output_vbr((unsigned)Slot, Out);
Chris Lattner186a1f72003-03-19 20:56:46 +000057 //std::cerr << "Type slot = " << Slot << " Type = " << T->getName() << endl;
Chris Lattner00950542001-06-06 20:29:01 +000058
59 output_vbr(AT->getNumElements(), Out);
60 break;
61 }
62
63 case Type::StructTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000064 const StructType *ST = cast<StructType>(T);
Chris Lattner00950542001-06-06 20:29:01 +000065
66 // Output all of the element types...
67 StructType::ElementTypes::const_iterator I = ST->getElementTypes().begin();
Chris Lattner079df312001-06-27 23:34:01 +000068 for (; I != ST->getElementTypes().end(); ++I) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +000069 int Slot = Table.getSlot(*I);
Chris Lattner00950542001-06-06 20:29:01 +000070 assert(Slot != -1 && "Type used but not available!!");
71 output_vbr((unsigned)Slot, Out);
72 }
73
74 // Terminate list with VoidTy
75 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
76 break;
77 }
78
79 case Type::PointerTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000080 const PointerType *PT = cast<PointerType>(T);
Alkis Evlogimenos60596382003-10-17 02:02:40 +000081 int Slot = Table.getSlot(PT->getElementType());
Chris Lattner00950542001-06-06 20:29:01 +000082 assert(Slot != -1 && "Type used but not available!!");
83 output_vbr((unsigned)Slot, Out);
84 break;
85 }
86
Chris Lattner5855f0d2001-10-23 01:53:22 +000087 case Type::OpaqueTyID: {
88 // No need to emit anything, just the count of opaque types is enough.
89 break;
90 }
91
Chris Lattnere8fdde12001-09-07 16:39:41 +000092 //case Type::PackedTyID:
Chris Lattner00950542001-06-06 20:29:01 +000093 default:
Chris Lattner186a1f72003-03-19 20:56:46 +000094 std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
95 << " Type '" << T->getDescription() << "'\n";
Chris Lattner00950542001-06-06 20:29:01 +000096 break;
97 }
98}
99
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000100bool BytecodeWriter::outputConstant(const Constant *CPV) {
Chris Lattner186a1f72003-03-19 20:56:46 +0000101 assert((CPV->getType()->isPrimitiveType() || !CPV->isNullValue()) &&
102 "Shouldn't output null constants!");
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000103
104 // We must check for a ConstantExpr before switching by type because
105 // a ConstantExpr can be of any type, and has no explicit value.
106 //
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000107 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
108 // FIXME: Encoding of constant exprs could be much more compact!
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000109 assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
110 output_vbr(CE->getNumOperands(), Out); // flags as an expr
111 output_vbr(CE->getOpcode(), Out); // flags as an expr
112
113 for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000114 int Slot = Table.getSlot(*OI);
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000115 assert(Slot != -1 && "Unknown constant used in ConstantExpr!!");
116 output_vbr((unsigned)Slot, Out);
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000117 Slot = Table.getSlot((*OI)->getType());
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000118 output_vbr((unsigned)Slot, Out);
119 }
120 return false;
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000121 } else {
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000122 output_vbr((unsigned)0, Out); // flag as not a ConstantExpr
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000123 }
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000124
Chris Lattner00950542001-06-06 20:29:01 +0000125 switch (CPV->getType()->getPrimitiveID()) {
126 case Type::BoolTyID: // Boolean Types
Chris Lattner987cb2c2003-07-23 14:54:33 +0000127 if (cast<ConstantBool>(CPV)->getValue())
Chris Lattner186a1f72003-03-19 20:56:46 +0000128 output_vbr(1U, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000129 else
Chris Lattner186a1f72003-03-19 20:56:46 +0000130 output_vbr(0U, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000131 break;
132
133 case Type::UByteTyID: // Unsigned integer types...
134 case Type::UShortTyID:
135 case Type::UIntTyID:
136 case Type::ULongTyID:
Chris Lattner987cb2c2003-07-23 14:54:33 +0000137 output_vbr(cast<ConstantUInt>(CPV)->getValue(), Out);
Chris Lattner00950542001-06-06 20:29:01 +0000138 break;
139
140 case Type::SByteTyID: // Signed integer types...
141 case Type::ShortTyID:
142 case Type::IntTyID:
143 case Type::LongTyID:
Chris Lattner987cb2c2003-07-23 14:54:33 +0000144 output_vbr(cast<ConstantSInt>(CPV)->getValue(), Out);
Chris Lattner00950542001-06-06 20:29:01 +0000145 break;
146
147 case Type::TypeTyID: // Serialize type type
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000148 assert(0 && "Types should not be in the Constant!");
Chris Lattner00950542001-06-06 20:29:01 +0000149 break;
150
151 case Type::ArrayTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +0000152 const ConstantArray *CPA = cast<ConstantArray>(CPV);
Chris Lattner00950542001-06-06 20:29:01 +0000153 unsigned size = CPA->getValues().size();
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000154 assert(size == cast<ArrayType>(CPA->getType())->getNumElements()
155 && "ConstantArray out of whack!");
Chris Lattner00950542001-06-06 20:29:01 +0000156 for (unsigned i = 0; i < size; i++) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000157 int Slot = Table.getSlot(CPA->getOperand(i));
Chris Lattner00950542001-06-06 20:29:01 +0000158 assert(Slot != -1 && "Constant used but not available!!");
159 output_vbr((unsigned)Slot, Out);
160 }
161 break;
162 }
163
164 case Type::StructTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +0000165 const ConstantStruct *CPS = cast<ConstantStruct>(CPV);
Chris Lattner697954c2002-01-20 22:54:45 +0000166 const std::vector<Use> &Vals = CPS->getValues();
Chris Lattner00950542001-06-06 20:29:01 +0000167
168 for (unsigned i = 0; i < Vals.size(); ++i) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000169 int Slot = Table.getSlot(Vals[i]);
Chris Lattner00950542001-06-06 20:29:01 +0000170 assert(Slot != -1 && "Constant used but not available!!");
171 output_vbr((unsigned)Slot, Out);
172 }
173 break;
174 }
175
Chris Lattner1a1cb112001-09-30 22:46:54 +0000176 case Type::PointerTyID: {
Chris Lattner02071d02003-11-17 17:28:29 +0000177 const ConstantPointerRef *CPR = cast<ConstantPointerRef>(CPV);
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000178 int Slot = Table.getSlot((Value*)CPR->getValue());
Chris Lattner186a1f72003-03-19 20:56:46 +0000179 assert(Slot != -1 && "Global used but not available!!");
180 output_vbr((unsigned)Slot, Out);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000181 break;
182 }
183
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000184 case Type::FloatTyID: { // Floating point types...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000185 float Tmp = (float)cast<ConstantFP>(CPV)->getValue();
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000186 output_data(&Tmp, &Tmp+1, Out);
187 break;
188 }
189 case Type::DoubleTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000190 double Tmp = cast<ConstantFP>(CPV)->getValue();
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000191 output_data(&Tmp, &Tmp+1, Out);
192 break;
193 }
Chris Lattner00950542001-06-06 20:29:01 +0000194
195 case Type::VoidTyID:
196 case Type::LabelTyID:
197 default:
Chris Lattner186a1f72003-03-19 20:56:46 +0000198 std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
199 << " type '" << CPV->getType()->getName() << "'\n";
Chris Lattner00950542001-06-06 20:29:01 +0000200 break;
201 }
202 return false;
203}