blob: dcb034f78398774d7f53187292ba438c920ab84e [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
20void BytecodeWriter::outputType(const Type *T) {
21 output_vbr((unsigned)T->getPrimitiveID(), Out);
22
23 // That's all there is to handling primitive types...
24 if (T->isPrimitiveType())
25 return; // We might do this if we alias a prim type: %x = type int
26
27 switch (T->getPrimitiveID()) { // Handle derived types now.
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000028 case Type::FunctionTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000029 const FunctionType *MT = cast<FunctionType>(T);
Alkis Evlogimenos60596382003-10-17 02:02:40 +000030 int Slot = Table.getSlot(MT->getReturnType());
Chris Lattner00950542001-06-06 20:29:01 +000031 assert(Slot != -1 && "Type used but not available!!");
32 output_vbr((unsigned)Slot, Out);
33
Chris Lattnere5a57ee2001-07-25 22:47:55 +000034 // Output the number of arguments to method (+1 if varargs):
35 output_vbr(MT->getParamTypes().size()+MT->isVarArg(), Out);
36
Chris Lattner00950542001-06-06 20:29:01 +000037 // Output all of the arguments...
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000038 FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin();
Chris Lattner079df312001-06-27 23:34:01 +000039 for (; I != MT->getParamTypes().end(); ++I) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +000040 Slot = Table.getSlot(*I);
Chris Lattner00950542001-06-06 20:29:01 +000041 assert(Slot != -1 && "Type used but not available!!");
42 output_vbr((unsigned)Slot, Out);
43 }
44
Chris Lattnere5a57ee2001-07-25 22:47:55 +000045 // Terminate list with VoidTy if we are a varargs function...
46 if (MT->isVarArg())
47 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
Chris Lattner00950542001-06-06 20:29:01 +000048 break;
49 }
50
51 case Type::ArrayTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000052 const ArrayType *AT = cast<ArrayType>(T);
Alkis Evlogimenos60596382003-10-17 02:02:40 +000053 int Slot = Table.getSlot(AT->getElementType());
Chris Lattner00950542001-06-06 20:29:01 +000054 assert(Slot != -1 && "Type used but not available!!");
55 output_vbr((unsigned)Slot, Out);
Chris Lattner186a1f72003-03-19 20:56:46 +000056 //std::cerr << "Type slot = " << Slot << " Type = " << T->getName() << endl;
Chris Lattner00950542001-06-06 20:29:01 +000057
58 output_vbr(AT->getNumElements(), Out);
59 break;
60 }
61
62 case Type::StructTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000063 const StructType *ST = cast<StructType>(T);
Chris Lattner00950542001-06-06 20:29:01 +000064
65 // Output all of the element types...
66 StructType::ElementTypes::const_iterator I = ST->getElementTypes().begin();
Chris Lattner079df312001-06-27 23:34:01 +000067 for (; I != ST->getElementTypes().end(); ++I) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +000068 int Slot = Table.getSlot(*I);
Chris Lattner00950542001-06-06 20:29:01 +000069 assert(Slot != -1 && "Type used but not available!!");
70 output_vbr((unsigned)Slot, Out);
71 }
72
73 // Terminate list with VoidTy
74 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
75 break;
76 }
77
78 case Type::PointerTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +000079 const PointerType *PT = cast<PointerType>(T);
Alkis Evlogimenos60596382003-10-17 02:02:40 +000080 int Slot = Table.getSlot(PT->getElementType());
Chris Lattner00950542001-06-06 20:29:01 +000081 assert(Slot != -1 && "Type used but not available!!");
82 output_vbr((unsigned)Slot, Out);
83 break;
84 }
85
Chris Lattner5855f0d2001-10-23 01:53:22 +000086 case Type::OpaqueTyID: {
87 // No need to emit anything, just the count of opaque types is enough.
88 break;
89 }
90
Chris Lattnere8fdde12001-09-07 16:39:41 +000091 //case Type::PackedTyID:
Chris Lattner00950542001-06-06 20:29:01 +000092 default:
Chris Lattner186a1f72003-03-19 20:56:46 +000093 std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
94 << " Type '" << T->getDescription() << "'\n";
Chris Lattner00950542001-06-06 20:29:01 +000095 break;
96 }
97}
98
Chris Lattnere9bb2df2001-12-03 22:26:30 +000099bool BytecodeWriter::outputConstant(const Constant *CPV) {
Chris Lattner186a1f72003-03-19 20:56:46 +0000100 assert((CPV->getType()->isPrimitiveType() || !CPV->isNullValue()) &&
101 "Shouldn't output null constants!");
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000102
103 // We must check for a ConstantExpr before switching by type because
104 // a ConstantExpr can be of any type, and has no explicit value.
105 //
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000106 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
107 // FIXME: Encoding of constant exprs could be much more compact!
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000108 assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
109 output_vbr(CE->getNumOperands(), Out); // flags as an expr
110 output_vbr(CE->getOpcode(), Out); // flags as an expr
111
112 for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000113 int Slot = Table.getSlot(*OI);
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000114 assert(Slot != -1 && "Unknown constant used in ConstantExpr!!");
115 output_vbr((unsigned)Slot, Out);
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000116 Slot = Table.getSlot((*OI)->getType());
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000117 output_vbr((unsigned)Slot, Out);
118 }
119 return false;
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000120 } else {
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000121 output_vbr((unsigned)0, Out); // flag as not a ConstantExpr
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000122 }
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000123
Chris Lattner00950542001-06-06 20:29:01 +0000124 switch (CPV->getType()->getPrimitiveID()) {
125 case Type::BoolTyID: // Boolean Types
Chris Lattner987cb2c2003-07-23 14:54:33 +0000126 if (cast<ConstantBool>(CPV)->getValue())
Chris Lattner186a1f72003-03-19 20:56:46 +0000127 output_vbr(1U, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000128 else
Chris Lattner186a1f72003-03-19 20:56:46 +0000129 output_vbr(0U, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000130 break;
131
132 case Type::UByteTyID: // Unsigned integer types...
133 case Type::UShortTyID:
134 case Type::UIntTyID:
135 case Type::ULongTyID:
Chris Lattner987cb2c2003-07-23 14:54:33 +0000136 output_vbr(cast<ConstantUInt>(CPV)->getValue(), Out);
Chris Lattner00950542001-06-06 20:29:01 +0000137 break;
138
139 case Type::SByteTyID: // Signed integer types...
140 case Type::ShortTyID:
141 case Type::IntTyID:
142 case Type::LongTyID:
Chris Lattner987cb2c2003-07-23 14:54:33 +0000143 output_vbr(cast<ConstantSInt>(CPV)->getValue(), Out);
Chris Lattner00950542001-06-06 20:29:01 +0000144 break;
145
146 case Type::TypeTyID: // Serialize type type
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000147 assert(0 && "Types should not be in the Constant!");
Chris Lattner00950542001-06-06 20:29:01 +0000148 break;
149
150 case Type::ArrayTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +0000151 const ConstantArray *CPA = cast<ConstantArray>(CPV);
Chris Lattner00950542001-06-06 20:29:01 +0000152 unsigned size = CPA->getValues().size();
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000153 assert(size == cast<ArrayType>(CPA->getType())->getNumElements()
154 && "ConstantArray out of whack!");
Chris Lattner00950542001-06-06 20:29:01 +0000155 for (unsigned i = 0; i < size; i++) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000156 int Slot = Table.getSlot(CPA->getOperand(i));
Chris Lattner00950542001-06-06 20:29:01 +0000157 assert(Slot != -1 && "Constant used but not available!!");
158 output_vbr((unsigned)Slot, Out);
159 }
160 break;
161 }
162
163 case Type::StructTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +0000164 const ConstantStruct *CPS = cast<ConstantStruct>(CPV);
Chris Lattner697954c2002-01-20 22:54:45 +0000165 const std::vector<Use> &Vals = CPS->getValues();
Chris Lattner00950542001-06-06 20:29:01 +0000166
167 for (unsigned i = 0; i < Vals.size(); ++i) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000168 int Slot = Table.getSlot(Vals[i]);
Chris Lattner00950542001-06-06 20:29:01 +0000169 assert(Slot != -1 && "Constant used but not available!!");
170 output_vbr((unsigned)Slot, Out);
171 }
172 break;
173 }
174
Chris Lattner1a1cb112001-09-30 22:46:54 +0000175 case Type::PointerTyID: {
Chris Lattner987cb2c2003-07-23 14:54:33 +0000176 const ConstantPointer *CPP = cast<ConstantPointer>(CPV);
Chris Lattner186a1f72003-03-19 20:56:46 +0000177 assert(!isa<ConstantPointerNull>(CPP) && "Null should be already emitted!");
178 const ConstantPointerRef *CPR = cast<ConstantPointerRef>(CPP);
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}