blob: 73130f3ee87ce0fdea81b4df44abcf2c024f54b3 [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 Lattnere9bb2df2001-12-03 22:26:30 +000013#include "llvm/ConstantVals.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.
27 case Type::MethodTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +000028 const MethodType *MT = cast<const MethodType>(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...
37 MethodType::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) {
Chris Lattner00950542001-06-06 20:29:01 +000099 switch (CPV->getType()->getPrimitiveID()) {
100 case Type::BoolTyID: // Boolean Types
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000101 if (cast<const ConstantBool>(CPV)->getValue())
Chris Lattner00950542001-06-06 20:29:01 +0000102 output_vbr((unsigned)1, Out);
103 else
104 output_vbr((unsigned)0, Out);
105 break;
106
107 case Type::UByteTyID: // Unsigned integer types...
108 case Type::UShortTyID:
109 case Type::UIntTyID:
110 case Type::ULongTyID:
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000111 output_vbr(cast<const ConstantUInt>(CPV)->getValue(), Out);
Chris Lattner00950542001-06-06 20:29:01 +0000112 break;
113
114 case Type::SByteTyID: // Signed integer types...
115 case Type::ShortTyID:
116 case Type::IntTyID:
117 case Type::LongTyID:
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000118 output_vbr(cast<const ConstantSInt>(CPV)->getValue(), Out);
Chris Lattner00950542001-06-06 20:29:01 +0000119 break;
120
121 case Type::TypeTyID: // Serialize type type
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000122 assert(0 && "Types should not be in the Constant!");
Chris Lattner00950542001-06-06 20:29:01 +0000123 break;
124
125 case Type::ArrayTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000126 const ConstantArray *CPA = cast<const ConstantArray>(CPV);
Chris Lattner00950542001-06-06 20:29:01 +0000127 unsigned size = CPA->getValues().size();
Chris Lattner90865152001-12-14 16:30:51 +0000128 assert(size == cast<ArrayType>(CPA->getType())->getNumElements() && "ConstantArray out of whack!");
Chris Lattner00950542001-06-06 20:29:01 +0000129 for (unsigned i = 0; i < size; i++) {
Chris Lattnere8fdde12001-09-07 16:39:41 +0000130 int Slot = Table.getValSlot(CPA->getOperand(i));
Chris Lattner00950542001-06-06 20:29:01 +0000131 assert(Slot != -1 && "Constant used but not available!!");
132 output_vbr((unsigned)Slot, Out);
133 }
134 break;
135 }
136
137 case Type::StructTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000138 const ConstantStruct *CPS = cast<const ConstantStruct>(CPV);
Chris Lattner697954c2002-01-20 22:54:45 +0000139 const std::vector<Use> &Vals = CPS->getValues();
Chris Lattner00950542001-06-06 20:29:01 +0000140
141 for (unsigned i = 0; i < Vals.size(); ++i) {
142 int Slot = Table.getValSlot(Vals[i]);
143 assert(Slot != -1 && "Constant used but not available!!");
144 output_vbr((unsigned)Slot, Out);
145 }
146 break;
147 }
148
Chris Lattner1a1cb112001-09-30 22:46:54 +0000149 case Type::PointerTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000150 const ConstantPointer *CPP = cast<const ConstantPointer>(CPV);
151 if (isa<ConstantPointerNull>(CPP)) {
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000152 output_vbr((unsigned)0, Out);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000153 } else if (const ConstantPointerRef *CPR =
154 dyn_cast<ConstantPointerRef>(CPP)) {
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000155 output_vbr((unsigned)1, Out);
156 int Slot = Table.getValSlot((Value*)CPR->getValue());
157 assert(Slot != -1 && "Global used but not available!!");
158 output_vbr((unsigned)Slot, Out);
159 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000160 assert(0 && "Unknown ConstantPointer Subclass!");
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000161 }
Chris Lattner1a1cb112001-09-30 22:46:54 +0000162 break;
163 }
164
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000165 case Type::FloatTyID: { // Floating point types...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000166 float Tmp = (float)cast<ConstantFP>(CPV)->getValue();
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000167 output_data(&Tmp, &Tmp+1, Out);
168 break;
169 }
170 case Type::DoubleTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000171 double Tmp = cast<ConstantFP>(CPV)->getValue();
Chris Lattnerff5ecce2001-07-15 00:17:23 +0000172 output_data(&Tmp, &Tmp+1, Out);
173 break;
174 }
Chris Lattner00950542001-06-06 20:29:01 +0000175
176 case Type::VoidTyID:
177 case Type::LabelTyID:
178 default:
179 cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
180 << " type '" << CPV->getType()->getName() << "'\n";
181 break;
182 }
183 return false;
184}