blob: 5813ea296d82b808d24c6ad9493a6b285aacd572 [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"
19
Brian Gaeked0fde302003-11-11 22:41:34 +000020namespace llvm {
21
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...
26 if (T->isPrimitiveType())
27 return; // We might do this if we alias a prim type: %x = type int
28
29 switch (T->getPrimitiveID()) { // Handle derived types now.
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000030 case Type::FunctionTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000031 const FunctionType *MT = cast<FunctionType>(T);
Alkis Evlogimenos60596382003-10-17 02:02:40 +000032 int Slot = Table.getSlot(MT->getReturnType());
Chris Lattner00950542001-06-06 20:29:01 +000033 assert(Slot != -1 && "Type used but not available!!");
34 output_vbr((unsigned)Slot, Out);
35
Chris Lattnere5a57ee2001-07-25 22:47:55 +000036 // Output the number of arguments to method (+1 if varargs):
Brian Gaekebc839b52003-10-29 20:09:01 +000037 output_vbr((unsigned)MT->getParamTypes().size()+MT->isVarArg(), Out);
Chris Lattnere5a57ee2001-07-25 22:47:55 +000038
Chris Lattner00950542001-06-06 20:29:01 +000039 // Output all of the arguments...
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000040 FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin();
Chris Lattner079df312001-06-27 23:34:01 +000041 for (; I != MT->getParamTypes().end(); ++I) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +000042 Slot = Table.getSlot(*I);
Chris Lattner00950542001-06-06 20:29:01 +000043 assert(Slot != -1 && "Type used but not available!!");
44 output_vbr((unsigned)Slot, Out);
45 }
46
Chris Lattnere5a57ee2001-07-25 22:47:55 +000047 // Terminate list with VoidTy if we are a varargs function...
48 if (MT->isVarArg())
49 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
Chris Lattner00950542001-06-06 20:29:01 +000050 break;
51 }
52
53 case Type::ArrayTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000054 const ArrayType *AT = cast<ArrayType>(T);
Alkis Evlogimenos60596382003-10-17 02:02:40 +000055 int Slot = Table.getSlot(AT->getElementType());
Chris Lattner00950542001-06-06 20:29:01 +000056 assert(Slot != -1 && "Type used but not available!!");
57 output_vbr((unsigned)Slot, Out);
Chris Lattner186a1f72003-03-19 20:56:46 +000058 //std::cerr << "Type slot = " << Slot << " Type = " << T->getName() << endl;
Chris Lattner00950542001-06-06 20:29:01 +000059
60 output_vbr(AT->getNumElements(), Out);
61 break;
62 }
63
64 case Type::StructTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000065 const StructType *ST = cast<StructType>(T);
Chris Lattner00950542001-06-06 20:29:01 +000066
67 // Output all of the element types...
68 StructType::ElementTypes::const_iterator I = ST->getElementTypes().begin();
Chris Lattner079df312001-06-27 23:34:01 +000069 for (; I != ST->getElementTypes().end(); ++I) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +000070 int Slot = Table.getSlot(*I);
Chris Lattner00950542001-06-06 20:29:01 +000071 assert(Slot != -1 && "Type used but not available!!");
72 output_vbr((unsigned)Slot, Out);
73 }
74
75 // Terminate list with VoidTy
76 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
77 break;
78 }
79
80 case Type::PointerTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000081 const PointerType *PT = cast<PointerType>(T);
Alkis Evlogimenos60596382003-10-17 02:02:40 +000082 int Slot = Table.getSlot(PT->getElementType());
Chris Lattner00950542001-06-06 20:29:01 +000083 assert(Slot != -1 && "Type used but not available!!");
84 output_vbr((unsigned)Slot, Out);
85 break;
86 }
87
Chris Lattner5855f0d2001-10-23 01:53:22 +000088 case Type::OpaqueTyID: {
89 // No need to emit anything, just the count of opaque types is enough.
90 break;
91 }
92
Chris Lattnere8fdde12001-09-07 16:39:41 +000093 //case Type::PackedTyID:
Chris Lattner00950542001-06-06 20:29:01 +000094 default:
Chris Lattner186a1f72003-03-19 20:56:46 +000095 std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
96 << " Type '" << T->getDescription() << "'\n";
Chris Lattner00950542001-06-06 20:29:01 +000097 break;
98 }
99}
100
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000101bool BytecodeWriter::outputConstant(const Constant *CPV) {
Chris Lattner186a1f72003-03-19 20:56:46 +0000102 assert((CPV->getType()->isPrimitiveType() || !CPV->isNullValue()) &&
103 "Shouldn't output null constants!");
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000104
105 // We must check for a ConstantExpr before switching by type because
106 // a ConstantExpr can be of any type, and has no explicit value.
107 //
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000108 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
109 // FIXME: Encoding of constant exprs could be much more compact!
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000110 assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
111 output_vbr(CE->getNumOperands(), Out); // flags as an expr
112 output_vbr(CE->getOpcode(), Out); // flags as an expr
113
114 for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000115 int Slot = Table.getSlot(*OI);
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000116 assert(Slot != -1 && "Unknown constant used in ConstantExpr!!");
117 output_vbr((unsigned)Slot, Out);
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000118 Slot = Table.getSlot((*OI)->getType());
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000119 output_vbr((unsigned)Slot, Out);
120 }
121 return false;
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000122 } else {
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000123 output_vbr((unsigned)0, Out); // flag as not a ConstantExpr
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000124 }
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000125
Chris Lattner00950542001-06-06 20:29:01 +0000126 switch (CPV->getType()->getPrimitiveID()) {
127 case Type::BoolTyID: // Boolean Types
Chris Lattner987cb2c2003-07-23 14:54:33 +0000128 if (cast<ConstantBool>(CPV)->getValue())
Chris Lattner186a1f72003-03-19 20:56:46 +0000129 output_vbr(1U, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000130 else
Chris Lattner186a1f72003-03-19 20:56:46 +0000131 output_vbr(0U, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000132 break;
133
134 case Type::UByteTyID: // Unsigned integer types...
135 case Type::UShortTyID:
136 case Type::UIntTyID:
137 case Type::ULongTyID:
Chris Lattner987cb2c2003-07-23 14:54:33 +0000138 output_vbr(cast<ConstantUInt>(CPV)->getValue(), Out);
Chris Lattner00950542001-06-06 20:29:01 +0000139 break;
140
141 case Type::SByteTyID: // Signed integer types...
142 case Type::ShortTyID:
143 case Type::IntTyID:
144 case Type::LongTyID:
Chris Lattner987cb2c2003-07-23 14:54:33 +0000145 output_vbr(cast<ConstantSInt>(CPV)->getValue(), Out);
Chris Lattner00950542001-06-06 20:29:01 +0000146 break;
147
148 case Type::TypeTyID: // Serialize type type
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000149 assert(0 && "Types should not be in the Constant!");
Chris Lattner00950542001-06-06 20:29:01 +0000150 break;
151
152 case Type::ArrayTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +0000153 const ConstantArray *CPA = cast<ConstantArray>(CPV);
Chris Lattner00950542001-06-06 20:29:01 +0000154 unsigned size = CPA->getValues().size();
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000155 assert(size == cast<ArrayType>(CPA->getType())->getNumElements()
156 && "ConstantArray out of whack!");
Chris Lattner00950542001-06-06 20:29:01 +0000157 for (unsigned i = 0; i < size; 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);
173 }
174 break;
175 }
176
Chris Lattner1a1cb112001-09-30 22:46:54 +0000177 case Type::PointerTyID: {
Chris Lattner02071d02003-11-17 17:28:29 +0000178 const ConstantPointerRef *CPR = cast<ConstantPointerRef>(CPV);
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000179 int Slot = Table.getSlot((Value*)CPR->getValue());
Chris Lattner186a1f72003-03-19 20:56:46 +0000180 assert(Slot != -1 && "Global used but not available!!");
181 output_vbr((unsigned)Slot, Out);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000182 break;
183 }
184
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000185 case Type::FloatTyID: { // Floating point types...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000186 float Tmp = (float)cast<ConstantFP>(CPV)->getValue();
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000187 output_data(&Tmp, &Tmp+1, Out);
188 break;
189 }
190 case Type::DoubleTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000191 double Tmp = cast<ConstantFP>(CPV)->getValue();
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000192 output_data(&Tmp, &Tmp+1, Out);
193 break;
194 }
Chris Lattner00950542001-06-06 20:29:01 +0000195
196 case Type::VoidTyID:
197 case Type::LabelTyID:
198 default:
Chris Lattner186a1f72003-03-19 20:56:46 +0000199 std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
200 << " type '" << CPV->getType()->getName() << "'\n";
Chris Lattner00950542001-06-06 20:29:01 +0000201 break;
202 }
203 return false;
204}
Brian Gaeked0fde302003-11-11 22:41:34 +0000205
206} // End llvm namespace