blob: 5e24018f113194ea1a6b02cd8c79e6a3460d6beb [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- WriteConst.cpp - Functions for writing constants ---------*- C++ -*--=//
2//
3// This file implements the routines for encoding constants to a bytecode
4// stream.
5//
6// Note that the performance of this library is not terribly important, because
7// it shouldn't be used by JIT type applications... so it is not a huge focus
8// at least. :)
9//
10//===----------------------------------------------------------------------===//
11
12#include "WriterInternals.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000013#include "llvm/Constants.h"
Chris Lattner00950542001-06-06 20:29:01 +000014#include "llvm/SymbolTable.h"
15#include "llvm/DerivedTypes.h"
Chris Lattner697954c2002-01-20 22:54:45 +000016#include <iostream>
17using std::cerr;
Chris Lattner00950542001-06-06 20:29:01 +000018
19void BytecodeWriter::outputType(const Type *T) {
20 output_vbr((unsigned)T->getPrimitiveID(), Out);
21
22 // That's all there is to handling primitive types...
23 if (T->isPrimitiveType())
24 return; // We might do this if we alias a prim type: %x = type int
25
26 switch (T->getPrimitiveID()) { // Handle derived types now.
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000027 case Type::FunctionTyID: {
28 const FunctionType *MT = cast<const FunctionType>(T);
Chris Lattner00950542001-06-06 20:29:01 +000029 int Slot = Table.getValSlot(MT->getReturnType());
30 assert(Slot != -1 && "Type used but not available!!");
31 output_vbr((unsigned)Slot, Out);
32
Chris Lattnere5a57ee2001-07-25 22:47:55 +000033 // Output the number of arguments to method (+1 if varargs):
34 output_vbr(MT->getParamTypes().size()+MT->isVarArg(), Out);
35
Chris Lattner00950542001-06-06 20:29:01 +000036 // Output all of the arguments...
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000037 FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin();
Chris Lattner079df312001-06-27 23:34:01 +000038 for (; I != MT->getParamTypes().end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +000039 Slot = Table.getValSlot(*I);
40 assert(Slot != -1 && "Type used but not available!!");
41 output_vbr((unsigned)Slot, Out);
42 }
43
Chris Lattnere5a57ee2001-07-25 22:47:55 +000044 // Terminate list with VoidTy if we are a varargs function...
45 if (MT->isVarArg())
46 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
Chris Lattner00950542001-06-06 20:29:01 +000047 break;
48 }
49
50 case Type::ArrayTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +000051 const ArrayType *AT = cast<const ArrayType>(T);
Chris Lattner00950542001-06-06 20:29:01 +000052 int Slot = Table.getValSlot(AT->getElementType());
53 assert(Slot != -1 && "Type used but not available!!");
54 output_vbr((unsigned)Slot, Out);
55 //cerr << "Type slot = " << Slot << " Type = " << T->getName() << endl;
56
57 output_vbr(AT->getNumElements(), Out);
58 break;
59 }
60
61 case Type::StructTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +000062 const StructType *ST = cast<const StructType>(T);
Chris Lattner00950542001-06-06 20:29:01 +000063
64 // Output all of the element types...
65 StructType::ElementTypes::const_iterator I = ST->getElementTypes().begin();
Chris Lattner079df312001-06-27 23:34:01 +000066 for (; I != ST->getElementTypes().end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +000067 int Slot = Table.getValSlot(*I);
68 assert(Slot != -1 && "Type used but not available!!");
69 output_vbr((unsigned)Slot, Out);
70 }
71
72 // Terminate list with VoidTy
73 output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
74 break;
75 }
76
77 case Type::PointerTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +000078 const PointerType *PT = cast<const PointerType>(T);
Chris Lattner7a176752001-12-04 00:03:30 +000079 int Slot = Table.getValSlot(PT->getElementType());
Chris Lattner00950542001-06-06 20:29:01 +000080 assert(Slot != -1 && "Type used but not available!!");
81 output_vbr((unsigned)Slot, Out);
82 break;
83 }
84
Chris Lattner5855f0d2001-10-23 01:53:22 +000085 case Type::OpaqueTyID: {
86 // No need to emit anything, just the count of opaque types is enough.
87 break;
88 }
89
Chris Lattnere8fdde12001-09-07 16:39:41 +000090 //case Type::PackedTyID:
Chris Lattner00950542001-06-06 20:29:01 +000091 default:
92 cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
Chris Lattnere8fdde12001-09-07 16:39:41 +000093 << " Type '" << T->getDescription() << "'\n";
Chris Lattner00950542001-06-06 20:29:01 +000094 break;
95 }
96}
97
Chris Lattnere9bb2df2001-12-03 22:26:30 +000098bool BytecodeWriter::outputConstant(const Constant *CPV) {
Vikram S. Advea24a0bb2002-07-14 23:08:30 +000099
100 // We must check for a ConstantExpr before switching by type because
101 // a ConstantExpr can be of any type, and has no explicit value.
102 //
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000103 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
104 // FIXME: Encoding of constant exprs could be much more compact!
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000105 assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
106 output_vbr(CE->getNumOperands(), Out); // flags as an expr
107 output_vbr(CE->getOpcode(), Out); // flags as an expr
108
109 for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
110 int Slot = Table.getValSlot(*OI);
111 assert(Slot != -1 && "Unknown constant used in ConstantExpr!!");
112 output_vbr((unsigned)Slot, Out);
113 Slot = Table.getValSlot((*OI)->getType());
114 output_vbr((unsigned)Slot, Out);
115 }
116 return false;
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000117 } else {
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000118 output_vbr((unsigned)0, Out); // flag as not a ConstantExpr
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000119 }
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000120
Chris Lattner00950542001-06-06 20:29:01 +0000121 switch (CPV->getType()->getPrimitiveID()) {
122 case Type::BoolTyID: // Boolean Types
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000123 if (cast<const ConstantBool>(CPV)->getValue())
Chris Lattner00950542001-06-06 20:29:01 +0000124 output_vbr((unsigned)1, Out);
125 else
126 output_vbr((unsigned)0, Out);
127 break;
128
129 case Type::UByteTyID: // Unsigned integer types...
130 case Type::UShortTyID:
131 case Type::UIntTyID:
132 case Type::ULongTyID:
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000133 output_vbr(cast<const ConstantUInt>(CPV)->getValue(), Out);
Chris Lattner00950542001-06-06 20:29:01 +0000134 break;
135
136 case Type::SByteTyID: // Signed integer types...
137 case Type::ShortTyID:
138 case Type::IntTyID:
139 case Type::LongTyID:
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000140 output_vbr(cast<const ConstantSInt>(CPV)->getValue(), Out);
Chris Lattner00950542001-06-06 20:29:01 +0000141 break;
142
143 case Type::TypeTyID: // Serialize type type
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000144 assert(0 && "Types should not be in the Constant!");
Chris Lattner00950542001-06-06 20:29:01 +0000145 break;
146
147 case Type::ArrayTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000148 const ConstantArray *CPA = cast<const ConstantArray>(CPV);
Chris Lattner00950542001-06-06 20:29:01 +0000149 unsigned size = CPA->getValues().size();
Vikram S. Advea24a0bb2002-07-14 23:08:30 +0000150 assert(size == cast<ArrayType>(CPA->getType())->getNumElements()
151 && "ConstantArray out of whack!");
Chris Lattner00950542001-06-06 20:29:01 +0000152 for (unsigned i = 0; i < size; i++) {
Chris Lattnere8fdde12001-09-07 16:39:41 +0000153 int Slot = Table.getValSlot(CPA->getOperand(i));
Chris Lattner00950542001-06-06 20:29:01 +0000154 assert(Slot != -1 && "Constant used but not available!!");
155 output_vbr((unsigned)Slot, Out);
156 }
157 break;
158 }
159
160 case Type::StructTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000161 const ConstantStruct *CPS = cast<const ConstantStruct>(CPV);
Chris Lattner697954c2002-01-20 22:54:45 +0000162 const std::vector<Use> &Vals = CPS->getValues();
Chris Lattner00950542001-06-06 20:29:01 +0000163
164 for (unsigned i = 0; i < Vals.size(); ++i) {
165 int Slot = Table.getValSlot(Vals[i]);
166 assert(Slot != -1 && "Constant used but not available!!");
167 output_vbr((unsigned)Slot, Out);
168 }
169 break;
170 }
171
Chris Lattner1a1cb112001-09-30 22:46:54 +0000172 case Type::PointerTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000173 const ConstantPointer *CPP = cast<const ConstantPointer>(CPV);
174 if (isa<ConstantPointerNull>(CPP)) {
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000175 output_vbr((unsigned)0, Out);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000176 } else if (const ConstantPointerRef *CPR =
177 dyn_cast<ConstantPointerRef>(CPP)) {
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000178 output_vbr((unsigned)1, Out);
179 int Slot = Table.getValSlot((Value*)CPR->getValue());
180 assert(Slot != -1 && "Global used but not available!!");
181 output_vbr((unsigned)Slot, Out);
182 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000183 assert(0 && "Unknown ConstantPointer Subclass!");
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000184 }
Chris Lattner1a1cb112001-09-30 22:46:54 +0000185 break;
186 }
187
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000188 case Type::FloatTyID: { // Floating point types...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000189 float Tmp = (float)cast<ConstantFP>(CPV)->getValue();
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000190 output_data(&Tmp, &Tmp+1, Out);
191 break;
192 }
193 case Type::DoubleTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000194 double Tmp = cast<ConstantFP>(CPV)->getValue();
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000195 output_data(&Tmp, &Tmp+1, Out);
196 break;
197 }
Chris Lattner00950542001-06-06 20:29:01 +0000198
199 case Type::VoidTyID:
200 case Type::LabelTyID:
201 default:
202 cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
203 << " type '" << CPV->getType()->getName() << "'\n";
204 break;
205 }
206 return false;
207}