Chris Lattner | 1499934 | 2004-01-10 19:07:06 +0000 | [diff] [blame] | 1 | //===-- Writer.cpp - Library for writing LLVM bytecode files --------------===// |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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. |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
| 10 | // This library implements the functionality defined in llvm/Bytecode/Writer.h |
| 11 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 12 | // Note that this file uses an unusual technique of outputting all the bytecode |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 13 | // to a vector of unsigned char, then copies the vector to an ostream. The |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 14 | // reason for this is that we must do "seeking" in the stream to do back- |
| 15 | // patching, and some very important ostreams that we want to support (like |
| 16 | // pipes) do not support seeking. :( :( :( |
| 17 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 18 | //===----------------------------------------------------------------------===// |
| 19 | |
| 20 | #include "WriterInternals.h" |
Chris Lattner | 635cd93 | 2002-07-23 19:56:44 +0000 | [diff] [blame] | 21 | #include "llvm/Bytecode/WriteBytecodePass.h" |
Chris Lattner | dee199f | 2005-05-06 22:34:01 +0000 | [diff] [blame] | 22 | #include "llvm/CallingConv.h" |
Chris Lattner | 83bb3d2 | 2004-01-14 23:36:54 +0000 | [diff] [blame] | 23 | #include "llvm/Constants.h" |
| 24 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 3bc5a60 | 2006-01-25 23:08:15 +0000 | [diff] [blame] | 25 | #include "llvm/InlineAsm.h" |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 26 | #include "llvm/Instructions.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 27 | #include "llvm/Module.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 28 | #include "llvm/SymbolTable.h" |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 29 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Reid Spencer | 17f52c5 | 2004-11-06 23:17:23 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Compressor.h" |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 31 | #include "llvm/Support/MathExtras.h" |
Bill Wendling | 68fe61d | 2006-11-29 00:19:40 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Streams.h" |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 33 | #include "llvm/System/Program.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/STLExtras.h" |
| 35 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 32abce6 | 2004-01-10 19:10:01 +0000 | [diff] [blame] | 36 | #include <cstring> |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 37 | #include <algorithm> |
Chris Lattner | 44f549b | 2004-01-10 18:49:43 +0000 | [diff] [blame] | 38 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 39 | |
Reid Spencer | 38d54be | 2004-08-17 07:45:14 +0000 | [diff] [blame] | 40 | /// This value needs to be incremented every time the bytecode format changes |
| 41 | /// so that the reader can distinguish which format of the bytecode file has |
| 42 | /// been written. |
| 43 | /// @brief The bytecode version number |
Reid Spencer | 6996feb | 2006-11-08 21:27:54 +0000 | [diff] [blame] | 44 | const unsigned BCVersionNum = 7; |
Reid Spencer | 38d54be | 2004-08-17 07:45:14 +0000 | [diff] [blame] | 45 | |
Chris Lattner | 635cd93 | 2002-07-23 19:56:44 +0000 | [diff] [blame] | 46 | static RegisterPass<WriteBytecodePass> X("emitbytecode", "Bytecode Writer"); |
| 47 | |
Chris Lattner | ac0b6ae | 2006-12-06 17:46:33 +0000 | [diff] [blame] | 48 | static Statistic |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 49 | BytesWritten("bytecodewriter", "Number of bytecode bytes written"); |
Chris Lattner | 635cd93 | 2002-07-23 19:56:44 +0000 | [diff] [blame] | 50 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 51 | //===----------------------------------------------------------------------===// |
| 52 | //=== Output Primitives ===// |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | |
| 55 | // output - If a position is specified, it must be in the valid portion of the |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 56 | // string... note that this should be inlined always so only the relevant IF |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 57 | // body should be included. |
| 58 | inline void BytecodeWriter::output(unsigned i, int pos) { |
| 59 | if (pos == -1) { // Be endian clean, little endian is our friend |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 60 | Out.push_back((unsigned char)i); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 61 | Out.push_back((unsigned char)(i >> 8)); |
| 62 | Out.push_back((unsigned char)(i >> 16)); |
| 63 | Out.push_back((unsigned char)(i >> 24)); |
| 64 | } else { |
| 65 | Out[pos ] = (unsigned char)i; |
| 66 | Out[pos+1] = (unsigned char)(i >> 8); |
| 67 | Out[pos+2] = (unsigned char)(i >> 16); |
| 68 | Out[pos+3] = (unsigned char)(i >> 24); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | inline void BytecodeWriter::output(int i) { |
| 73 | output((unsigned)i); |
| 74 | } |
| 75 | |
| 76 | /// output_vbr - Output an unsigned value, by using the least number of bytes |
| 77 | /// possible. This is useful because many of our "infinite" values are really |
| 78 | /// very small most of the time; but can be large a few times. |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 79 | /// Data format used: If you read a byte with the high bit set, use the low |
| 80 | /// seven bits as data and then read another byte. |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 81 | inline void BytecodeWriter::output_vbr(uint64_t i) { |
| 82 | while (1) { |
| 83 | if (i < 0x80) { // done? |
| 84 | Out.push_back((unsigned char)i); // We know the high bit is clear... |
| 85 | return; |
| 86 | } |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 87 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 88 | // Nope, we are bigger than a character, output the next 7 bits and set the |
| 89 | // high bit to say that there is more coming... |
| 90 | Out.push_back(0x80 | ((unsigned char)i & 0x7F)); |
| 91 | i >>= 7; // Shift out 7 bits now... |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | inline void BytecodeWriter::output_vbr(unsigned i) { |
| 96 | while (1) { |
| 97 | if (i < 0x80) { // done? |
| 98 | Out.push_back((unsigned char)i); // We know the high bit is clear... |
| 99 | return; |
| 100 | } |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 101 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 102 | // Nope, we are bigger than a character, output the next 7 bits and set the |
| 103 | // high bit to say that there is more coming... |
| 104 | Out.push_back(0x80 | ((unsigned char)i & 0x7F)); |
| 105 | i >>= 7; // Shift out 7 bits now... |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | inline void BytecodeWriter::output_typeid(unsigned i) { |
| 110 | if (i <= 0x00FFFFFF) |
| 111 | this->output_vbr(i); |
| 112 | else { |
| 113 | this->output_vbr(0x00FFFFFF); |
| 114 | this->output_vbr(i); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | inline void BytecodeWriter::output_vbr(int64_t i) { |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 119 | if (i < 0) |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 120 | output_vbr(((uint64_t)(-i) << 1) | 1); // Set low order sign bit... |
| 121 | else |
| 122 | output_vbr((uint64_t)i << 1); // Low order bit is clear. |
| 123 | } |
| 124 | |
| 125 | |
| 126 | inline void BytecodeWriter::output_vbr(int i) { |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 127 | if (i < 0) |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 128 | output_vbr(((unsigned)(-i) << 1) | 1); // Set low order sign bit... |
| 129 | else |
| 130 | output_vbr((unsigned)i << 1); // Low order bit is clear. |
| 131 | } |
| 132 | |
Reid Spencer | 38d54be | 2004-08-17 07:45:14 +0000 | [diff] [blame] | 133 | inline void BytecodeWriter::output(const std::string &s) { |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 134 | unsigned Len = s.length(); |
Chris Lattner | c847f7c | 2006-07-28 22:07:54 +0000 | [diff] [blame] | 135 | output_vbr(Len); // Strings may have an arbitrary length. |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 136 | Out.insert(Out.end(), s.begin(), s.end()); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | inline void BytecodeWriter::output_data(const void *Ptr, const void *End) { |
| 140 | Out.insert(Out.end(), (const unsigned char*)Ptr, (const unsigned char*)End); |
| 141 | } |
| 142 | |
| 143 | inline void BytecodeWriter::output_float(float& FloatVal) { |
| 144 | /// FIXME: This isn't optimal, it has size problems on some platforms |
| 145 | /// where FP is not IEEE. |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 146 | uint32_t i = FloatToBits(FloatVal); |
Chris Lattner | c847f7c | 2006-07-28 22:07:54 +0000 | [diff] [blame] | 147 | Out.push_back( static_cast<unsigned char>( (i ) & 0xFF)); |
| 148 | Out.push_back( static_cast<unsigned char>( (i >> 8 ) & 0xFF)); |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 149 | Out.push_back( static_cast<unsigned char>( (i >> 16) & 0xFF)); |
| 150 | Out.push_back( static_cast<unsigned char>( (i >> 24) & 0xFF)); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | inline void BytecodeWriter::output_double(double& DoubleVal) { |
| 154 | /// FIXME: This isn't optimal, it has size problems on some platforms |
| 155 | /// where FP is not IEEE. |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 156 | uint64_t i = DoubleToBits(DoubleVal); |
Chris Lattner | c847f7c | 2006-07-28 22:07:54 +0000 | [diff] [blame] | 157 | Out.push_back( static_cast<unsigned char>( (i ) & 0xFF)); |
| 158 | Out.push_back( static_cast<unsigned char>( (i >> 8 ) & 0xFF)); |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 159 | Out.push_back( static_cast<unsigned char>( (i >> 16) & 0xFF)); |
| 160 | Out.push_back( static_cast<unsigned char>( (i >> 24) & 0xFF)); |
| 161 | Out.push_back( static_cast<unsigned char>( (i >> 32) & 0xFF)); |
| 162 | Out.push_back( static_cast<unsigned char>( (i >> 40) & 0xFF)); |
| 163 | Out.push_back( static_cast<unsigned char>( (i >> 48) & 0xFF)); |
| 164 | Out.push_back( static_cast<unsigned char>( (i >> 56) & 0xFF)); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Chris Lattner | c76ea43 | 2005-11-12 18:34:09 +0000 | [diff] [blame] | 167 | inline BytecodeBlock::BytecodeBlock(unsigned ID, BytecodeWriter &w, |
| 168 | bool elideIfEmpty, bool hasLongFormat) |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 169 | : Id(ID), Writer(w), ElideIfEmpty(elideIfEmpty), HasLongFormat(hasLongFormat){ |
| 170 | |
| 171 | if (HasLongFormat) { |
| 172 | w.output(ID); |
| 173 | w.output(0U); // For length in long format |
| 174 | } else { |
| 175 | w.output(0U); /// Place holder for ID and length for this block |
| 176 | } |
| 177 | Loc = w.size(); |
| 178 | } |
| 179 | |
Chris Lattner | b0bf664 | 2004-10-14 01:35:17 +0000 | [diff] [blame] | 180 | inline BytecodeBlock::~BytecodeBlock() { // Do backpatch when block goes out |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 181 | // of scope... |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 182 | if (Loc == Writer.size() && ElideIfEmpty) { |
| 183 | // If the block is empty, and we are allowed to, do not emit the block at |
| 184 | // all! |
| 185 | Writer.resize(Writer.size()-(HasLongFormat?8:4)); |
| 186 | return; |
| 187 | } |
| 188 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 189 | if (HasLongFormat) |
| 190 | Writer.output(unsigned(Writer.size()-Loc), int(Loc-4)); |
| 191 | else |
| 192 | Writer.output(unsigned(Writer.size()-Loc) << 5 | (Id & 0x1F), int(Loc-4)); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | //===----------------------------------------------------------------------===// |
| 196 | //=== Constant Output ===// |
| 197 | //===----------------------------------------------------------------------===// |
| 198 | |
| 199 | void BytecodeWriter::outputType(const Type *T) { |
| 200 | output_vbr((unsigned)T->getTypeID()); |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 201 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 202 | // That's all there is to handling primitive types... |
| 203 | if (T->isPrimitiveType()) { |
| 204 | return; // We might do this if we alias a prim type: %x = type int |
| 205 | } |
| 206 | |
| 207 | switch (T->getTypeID()) { // Handle derived types now. |
| 208 | case Type::FunctionTyID: { |
| 209 | const FunctionType *MT = cast<FunctionType>(T); |
| 210 | int Slot = Table.getSlot(MT->getReturnType()); |
| 211 | assert(Slot != -1 && "Type used but not available!!"); |
| 212 | output_typeid((unsigned)Slot); |
| 213 | |
| 214 | // Output the number of arguments to function (+1 if varargs): |
| 215 | output_vbr((unsigned)MT->getNumParams()+MT->isVarArg()); |
| 216 | |
| 217 | // Output all of the arguments... |
| 218 | FunctionType::param_iterator I = MT->param_begin(); |
| 219 | for (; I != MT->param_end(); ++I) { |
| 220 | Slot = Table.getSlot(*I); |
| 221 | assert(Slot != -1 && "Type used but not available!!"); |
| 222 | output_typeid((unsigned)Slot); |
| 223 | } |
| 224 | |
| 225 | // Terminate list with VoidTy if we are a varargs function... |
| 226 | if (MT->isVarArg()) |
| 227 | output_typeid((unsigned)Type::VoidTyID); |
| 228 | break; |
| 229 | } |
| 230 | |
| 231 | case Type::ArrayTyID: { |
| 232 | const ArrayType *AT = cast<ArrayType>(T); |
| 233 | int Slot = Table.getSlot(AT->getElementType()); |
| 234 | assert(Slot != -1 && "Type used but not available!!"); |
| 235 | output_typeid((unsigned)Slot); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 236 | output_vbr(AT->getNumElements()); |
| 237 | break; |
| 238 | } |
| 239 | |
Brian Gaeke | 715c90b | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 240 | case Type::PackedTyID: { |
| 241 | const PackedType *PT = cast<PackedType>(T); |
| 242 | int Slot = Table.getSlot(PT->getElementType()); |
| 243 | assert(Slot != -1 && "Type used but not available!!"); |
| 244 | output_typeid((unsigned)Slot); |
| 245 | output_vbr(PT->getNumElements()); |
| 246 | break; |
| 247 | } |
| 248 | |
| 249 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 250 | case Type::StructTyID: { |
| 251 | const StructType *ST = cast<StructType>(T); |
| 252 | |
| 253 | // Output all of the element types... |
| 254 | for (StructType::element_iterator I = ST->element_begin(), |
| 255 | E = ST->element_end(); I != E; ++I) { |
| 256 | int Slot = Table.getSlot(*I); |
| 257 | assert(Slot != -1 && "Type used but not available!!"); |
| 258 | output_typeid((unsigned)Slot); |
| 259 | } |
| 260 | |
| 261 | // Terminate list with VoidTy |
| 262 | output_typeid((unsigned)Type::VoidTyID); |
| 263 | break; |
| 264 | } |
| 265 | |
| 266 | case Type::PointerTyID: { |
| 267 | const PointerType *PT = cast<PointerType>(T); |
| 268 | int Slot = Table.getSlot(PT->getElementType()); |
| 269 | assert(Slot != -1 && "Type used but not available!!"); |
| 270 | output_typeid((unsigned)Slot); |
| 271 | break; |
| 272 | } |
| 273 | |
Chris Lattner | b0bf664 | 2004-10-14 01:35:17 +0000 | [diff] [blame] | 274 | case Type::OpaqueTyID: |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 275 | // No need to emit anything, just the count of opaque types is enough. |
| 276 | break; |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 277 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 278 | default: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame^] | 279 | cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize" |
| 280 | << " Type '" << T->getDescription() << "'\n"; |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 281 | break; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | void BytecodeWriter::outputConstant(const Constant *CPV) { |
| 286 | assert((CPV->getType()->isPrimitiveType() || !CPV->isNullValue()) && |
| 287 | "Shouldn't output null constants!"); |
| 288 | |
| 289 | // We must check for a ConstantExpr before switching by type because |
| 290 | // a ConstantExpr can be of any type, and has no explicit value. |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 291 | // |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 292 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) { |
| 293 | // FIXME: Encoding of constant exprs could be much more compact! |
| 294 | assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 295 | assert(CE->getNumOperands() != 1 || CE->isCast()); |
Chris Lattner | a79e7cc | 2004-10-16 18:18:16 +0000 | [diff] [blame] | 296 | output_vbr(1+CE->getNumOperands()); // flags as an expr |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 297 | output_vbr(CE->getOpcode()); // Put out the CE op code |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 298 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 299 | for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){ |
| 300 | int Slot = Table.getSlot(*OI); |
| 301 | assert(Slot != -1 && "Unknown constant used in ConstantExpr!!"); |
| 302 | output_vbr((unsigned)Slot); |
| 303 | Slot = Table.getSlot((*OI)->getType()); |
| 304 | output_typeid((unsigned)Slot); |
| 305 | } |
Reid Spencer | 595b477 | 2006-12-04 05:23:49 +0000 | [diff] [blame] | 306 | if (CE->isCompare()) |
| 307 | output_vbr((unsigned)CE->getPredicate()); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 308 | return; |
Chris Lattner | a79e7cc | 2004-10-16 18:18:16 +0000 | [diff] [blame] | 309 | } else if (isa<UndefValue>(CPV)) { |
| 310 | output_vbr(1U); // 1 -> UndefValue constant. |
| 311 | return; |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 312 | } else { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 313 | output_vbr(0U); // flag as not a ConstantExpr (i.e. 0 operands) |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 314 | } |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 315 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 316 | switch (CPV->getType()->getTypeID()) { |
| 317 | case Type::BoolTyID: // Boolean Types |
| 318 | if (cast<ConstantBool>(CPV)->getValue()) |
| 319 | output_vbr(1U); |
| 320 | else |
| 321 | output_vbr(0U); |
| 322 | break; |
| 323 | |
| 324 | case Type::UByteTyID: // Unsigned integer types... |
| 325 | case Type::UShortTyID: |
| 326 | case Type::UIntTyID: |
| 327 | case Type::ULongTyID: |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 328 | output_vbr(cast<ConstantInt>(CPV)->getZExtValue()); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 329 | break; |
| 330 | |
| 331 | case Type::SByteTyID: // Signed integer types... |
| 332 | case Type::ShortTyID: |
| 333 | case Type::IntTyID: |
| 334 | case Type::LongTyID: |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 335 | output_vbr(cast<ConstantInt>(CPV)->getSExtValue()); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 336 | break; |
| 337 | |
| 338 | case Type::ArrayTyID: { |
| 339 | const ConstantArray *CPA = cast<ConstantArray>(CPV); |
| 340 | assert(!CPA->isString() && "Constant strings should be handled specially!"); |
| 341 | |
Alkis Evlogimenos | 15876bb | 2004-08-04 08:44:43 +0000 | [diff] [blame] | 342 | for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) { |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 343 | int Slot = Table.getSlot(CPA->getOperand(i)); |
| 344 | assert(Slot != -1 && "Constant used but not available!!"); |
| 345 | output_vbr((unsigned)Slot); |
| 346 | } |
| 347 | break; |
| 348 | } |
| 349 | |
Brian Gaeke | 715c90b | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 350 | case Type::PackedTyID: { |
| 351 | const ConstantPacked *CP = cast<ConstantPacked>(CPV); |
| 352 | |
| 353 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) { |
| 354 | int Slot = Table.getSlot(CP->getOperand(i)); |
| 355 | assert(Slot != -1 && "Constant used but not available!!"); |
| 356 | output_vbr((unsigned)Slot); |
| 357 | } |
| 358 | break; |
| 359 | } |
| 360 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 361 | case Type::StructTyID: { |
| 362 | const ConstantStruct *CPS = cast<ConstantStruct>(CPV); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 363 | |
Alkis Evlogimenos | 15876bb | 2004-08-04 08:44:43 +0000 | [diff] [blame] | 364 | for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) { |
| 365 | int Slot = Table.getSlot(CPS->getOperand(i)); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 366 | assert(Slot != -1 && "Constant used but not available!!"); |
| 367 | output_vbr((unsigned)Slot); |
| 368 | } |
| 369 | break; |
| 370 | } |
| 371 | |
| 372 | case Type::PointerTyID: |
| 373 | assert(0 && "No non-null, non-constant-expr constants allowed!"); |
| 374 | abort(); |
| 375 | |
| 376 | case Type::FloatTyID: { // Floating point types... |
| 377 | float Tmp = (float)cast<ConstantFP>(CPV)->getValue(); |
| 378 | output_float(Tmp); |
| 379 | break; |
| 380 | } |
| 381 | case Type::DoubleTyID: { |
| 382 | double Tmp = cast<ConstantFP>(CPV)->getValue(); |
| 383 | output_double(Tmp); |
| 384 | break; |
| 385 | } |
| 386 | |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 387 | case Type::VoidTyID: |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 388 | case Type::LabelTyID: |
| 389 | default: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame^] | 390 | cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize" |
| 391 | << " type '" << *CPV->getType() << "'\n"; |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 392 | break; |
| 393 | } |
| 394 | return; |
| 395 | } |
| 396 | |
Chris Lattner | 3bc5a60 | 2006-01-25 23:08:15 +0000 | [diff] [blame] | 397 | /// outputInlineAsm - InlineAsm's get emitted to the constant pool, so they can |
| 398 | /// be shared by multiple uses. |
| 399 | void BytecodeWriter::outputInlineAsm(const InlineAsm *IA) { |
| 400 | // Output a marker, so we know when we have one one parsing the constant pool. |
| 401 | // Note that this encoding is 5 bytes: not very efficient for a marker. Since |
| 402 | // unique inline asms are rare, this should hardly matter. |
| 403 | output_vbr(~0U); |
| 404 | |
| 405 | output(IA->getAsmString()); |
| 406 | output(IA->getConstraintString()); |
| 407 | output_vbr(unsigned(IA->hasSideEffects())); |
| 408 | } |
| 409 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 410 | void BytecodeWriter::outputConstantStrings() { |
| 411 | SlotCalculator::string_iterator I = Table.string_begin(); |
| 412 | SlotCalculator::string_iterator E = Table.string_end(); |
| 413 | if (I == E) return; // No strings to emit |
| 414 | |
| 415 | // If we have != 0 strings to emit, output them now. Strings are emitted into |
| 416 | // the 'void' type plane. |
| 417 | output_vbr(unsigned(E-I)); |
| 418 | output_typeid(Type::VoidTyID); |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 419 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 420 | // Emit all of the strings. |
| 421 | for (I = Table.string_begin(); I != E; ++I) { |
| 422 | const ConstantArray *Str = *I; |
| 423 | int Slot = Table.getSlot(Str->getType()); |
| 424 | assert(Slot != -1 && "Constant string of unknown type?"); |
| 425 | output_typeid((unsigned)Slot); |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 426 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 427 | // Now that we emitted the type (which indicates the size of the string), |
| 428 | // emit all of the characters. |
| 429 | std::string Val = Str->getAsString(); |
| 430 | output_data(Val.c_str(), Val.c_str()+Val.size()); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | //===----------------------------------------------------------------------===// |
| 435 | //=== Instruction Output ===// |
| 436 | //===----------------------------------------------------------------------===// |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 437 | |
Chris Lattner | da895d6 | 2005-02-27 06:18:25 +0000 | [diff] [blame] | 438 | // outputInstructionFormat0 - Output those weird instructions that have a large |
Chris Lattner | dee199f | 2005-05-06 22:34:01 +0000 | [diff] [blame] | 439 | // number of operands or have large operands themselves. |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 440 | // |
| 441 | // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>] |
| 442 | // |
Chris Lattner | f9d7178 | 2004-10-14 01:46:07 +0000 | [diff] [blame] | 443 | void BytecodeWriter::outputInstructionFormat0(const Instruction *I, |
| 444 | unsigned Opcode, |
| 445 | const SlotCalculator &Table, |
| 446 | unsigned Type) { |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 447 | // Opcode must have top two bits clear... |
| 448 | output_vbr(Opcode << 2); // Instruction Opcode ID |
| 449 | output_typeid(Type); // Result type |
| 450 | |
| 451 | unsigned NumArgs = I->getNumOperands(); |
Reid Spencer | cae6053 | 2006-12-06 04:27:07 +0000 | [diff] [blame] | 452 | output_vbr(NumArgs + (isa<CastInst>(I) || isa<InvokeInst>(I) || |
| 453 | isa<CmpInst>(I) || isa<VAArgInst>(I) || Opcode == 58)); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 454 | |
| 455 | if (!isa<GetElementPtrInst>(&I)) { |
| 456 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 457 | int Slot = Table.getSlot(I->getOperand(i)); |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 458 | assert(Slot >= 0 && "No slot number for value!?!?"); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 459 | output_vbr((unsigned)Slot); |
| 460 | } |
| 461 | |
| 462 | if (isa<CastInst>(I) || isa<VAArgInst>(I)) { |
| 463 | int Slot = Table.getSlot(I->getType()); |
| 464 | assert(Slot != -1 && "Cast return type unknown?"); |
| 465 | output_typeid((unsigned)Slot); |
Reid Spencer | cae6053 | 2006-12-06 04:27:07 +0000 | [diff] [blame] | 466 | } else if (isa<CmpInst>(I)) { |
| 467 | output_vbr(unsigned(cast<CmpInst>(I)->getPredicate())); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 468 | } else if (isa<InvokeInst>(I)) { |
Chris Lattner | dee199f | 2005-05-06 22:34:01 +0000 | [diff] [blame] | 469 | output_vbr(cast<InvokeInst>(I)->getCallingConv()); |
| 470 | } else if (Opcode == 58) { // Call escape sequence |
| 471 | output_vbr((cast<CallInst>(I)->getCallingConv() << 1) | |
Jeff Cohen | 39cef60 | 2005-05-07 02:44:04 +0000 | [diff] [blame] | 472 | unsigned(cast<CallInst>(I)->isTailCall())); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 473 | } |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 474 | } else { |
| 475 | int Slot = Table.getSlot(I->getOperand(0)); |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 476 | assert(Slot >= 0 && "No slot number for value!?!?"); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 477 | output_vbr(unsigned(Slot)); |
| 478 | |
| 479 | // We need to encode the type of sequential type indices into their slot # |
| 480 | unsigned Idx = 1; |
| 481 | for (gep_type_iterator TI = gep_type_begin(I), E = gep_type_end(I); |
| 482 | Idx != NumArgs; ++TI, ++Idx) { |
| 483 | Slot = Table.getSlot(I->getOperand(Idx)); |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 484 | assert(Slot >= 0 && "No slot number for value!?!?"); |
| 485 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 486 | if (isa<SequentialType>(*TI)) { |
| 487 | unsigned IdxId; |
| 488 | switch (I->getOperand(Idx)->getType()->getTypeID()) { |
| 489 | default: assert(0 && "Unknown index type!"); |
| 490 | case Type::UIntTyID: IdxId = 0; break; |
| 491 | case Type::IntTyID: IdxId = 1; break; |
| 492 | case Type::ULongTyID: IdxId = 2; break; |
| 493 | case Type::LongTyID: IdxId = 3; break; |
| 494 | } |
| 495 | Slot = (Slot << 2) | IdxId; |
| 496 | } |
| 497 | output_vbr(unsigned(Slot)); |
| 498 | } |
| 499 | } |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | |
| 503 | // outputInstrVarArgsCall - Output the absurdly annoying varargs function calls. |
| 504 | // This are more annoying than most because the signature of the call does not |
| 505 | // tell us anything about the types of the arguments in the varargs portion. |
| 506 | // Because of this, we encode (as type 0) all of the argument types explicitly |
| 507 | // before the argument value. This really sucks, but you shouldn't be using |
| 508 | // varargs functions in your code! *death to printf*! |
| 509 | // |
| 510 | // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>] |
| 511 | // |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 512 | void BytecodeWriter::outputInstrVarArgsCall(const Instruction *I, |
| 513 | unsigned Opcode, |
| 514 | const SlotCalculator &Table, |
| 515 | unsigned Type) { |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 516 | assert(isa<CallInst>(I) || isa<InvokeInst>(I)); |
| 517 | // Opcode must have top two bits clear... |
| 518 | output_vbr(Opcode << 2); // Instruction Opcode ID |
| 519 | output_typeid(Type); // Result type (varargs type) |
| 520 | |
| 521 | const PointerType *PTy = cast<PointerType>(I->getOperand(0)->getType()); |
| 522 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 523 | unsigned NumParams = FTy->getNumParams(); |
| 524 | |
| 525 | unsigned NumFixedOperands; |
| 526 | if (isa<CallInst>(I)) { |
| 527 | // Output an operand for the callee and each fixed argument, then two for |
| 528 | // each variable argument. |
| 529 | NumFixedOperands = 1+NumParams; |
| 530 | } else { |
| 531 | assert(isa<InvokeInst>(I) && "Not call or invoke??"); |
| 532 | // Output an operand for the callee and destinations, then two for each |
| 533 | // variable argument. |
| 534 | NumFixedOperands = 3+NumParams; |
| 535 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 536 | output_vbr(2 * I->getNumOperands()-NumFixedOperands + |
| 537 | unsigned(Opcode == 58 || isa<InvokeInst>(I))); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 538 | |
| 539 | // The type for the function has already been emitted in the type field of the |
| 540 | // instruction. Just emit the slot # now. |
| 541 | for (unsigned i = 0; i != NumFixedOperands; ++i) { |
| 542 | int Slot = Table.getSlot(I->getOperand(i)); |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 543 | assert(Slot >= 0 && "No slot number for value!?!?"); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 544 | output_vbr((unsigned)Slot); |
| 545 | } |
| 546 | |
| 547 | for (unsigned i = NumFixedOperands, e = I->getNumOperands(); i != e; ++i) { |
| 548 | // Output Arg Type ID |
| 549 | int Slot = Table.getSlot(I->getOperand(i)->getType()); |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 550 | assert(Slot >= 0 && "No slot number for value!?!?"); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 551 | output_typeid((unsigned)Slot); |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 552 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 553 | // Output arg ID itself |
| 554 | Slot = Table.getSlot(I->getOperand(i)); |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 555 | assert(Slot >= 0 && "No slot number for value!?!?"); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 556 | output_vbr((unsigned)Slot); |
| 557 | } |
Chris Lattner | a65371e | 2006-05-26 18:42:34 +0000 | [diff] [blame] | 558 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 559 | if (isa<InvokeInst>(I)) { |
| 560 | // Emit the tail call/calling conv for invoke instructions |
| 561 | output_vbr(cast<InvokeInst>(I)->getCallingConv()); |
| 562 | } else if (Opcode == 58) { |
Chris Lattner | a65371e | 2006-05-26 18:42:34 +0000 | [diff] [blame] | 563 | const CallInst *CI = cast<CallInst>(I); |
| 564 | output_vbr((CI->getCallingConv() << 1) | unsigned(CI->isTailCall())); |
Chris Lattner | a65371e | 2006-05-26 18:42:34 +0000 | [diff] [blame] | 565 | } |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | |
| 569 | // outputInstructionFormat1 - Output one operand instructions, knowing that no |
| 570 | // operand index is >= 2^12. |
| 571 | // |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 572 | inline void BytecodeWriter::outputInstructionFormat1(const Instruction *I, |
| 573 | unsigned Opcode, |
| 574 | unsigned *Slots, |
| 575 | unsigned Type) { |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 576 | // bits Instruction format: |
| 577 | // -------------------------- |
| 578 | // 01-00: Opcode type, fixed to 1. |
| 579 | // 07-02: Opcode |
| 580 | // 19-08: Resulting type plane |
| 581 | // 31-20: Operand #1 (if set to (2^12-1), then zero operands) |
| 582 | // |
Chris Lattner | f9d7178 | 2004-10-14 01:46:07 +0000 | [diff] [blame] | 583 | output(1 | (Opcode << 2) | (Type << 8) | (Slots[0] << 20)); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | |
| 587 | // outputInstructionFormat2 - Output two operand instructions, knowing that no |
| 588 | // operand index is >= 2^8. |
| 589 | // |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 590 | inline void BytecodeWriter::outputInstructionFormat2(const Instruction *I, |
| 591 | unsigned Opcode, |
| 592 | unsigned *Slots, |
| 593 | unsigned Type) { |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 594 | // bits Instruction format: |
| 595 | // -------------------------- |
| 596 | // 01-00: Opcode type, fixed to 2. |
| 597 | // 07-02: Opcode |
| 598 | // 15-08: Resulting type plane |
| 599 | // 23-16: Operand #1 |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 600 | // 31-24: Operand #2 |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 601 | // |
Chris Lattner | f9d7178 | 2004-10-14 01:46:07 +0000 | [diff] [blame] | 602 | output(2 | (Opcode << 2) | (Type << 8) | (Slots[0] << 16) | (Slots[1] << 24)); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | |
| 606 | // outputInstructionFormat3 - Output three operand instructions, knowing that no |
| 607 | // operand index is >= 2^6. |
| 608 | // |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 609 | inline void BytecodeWriter::outputInstructionFormat3(const Instruction *I, |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 610 | unsigned Opcode, |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 611 | unsigned *Slots, |
| 612 | unsigned Type) { |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 613 | // bits Instruction format: |
| 614 | // -------------------------- |
| 615 | // 01-00: Opcode type, fixed to 3. |
| 616 | // 07-02: Opcode |
| 617 | // 13-08: Resulting type plane |
| 618 | // 19-14: Operand #1 |
| 619 | // 25-20: Operand #2 |
| 620 | // 31-26: Operand #3 |
| 621 | // |
Chris Lattner | f9d7178 | 2004-10-14 01:46:07 +0000 | [diff] [blame] | 622 | output(3 | (Opcode << 2) | (Type << 8) | |
Chris Lattner | 84d1ced | 2004-10-14 01:57:28 +0000 | [diff] [blame] | 623 | (Slots[0] << 14) | (Slots[1] << 20) | (Slots[2] << 26)); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | void BytecodeWriter::outputInstruction(const Instruction &I) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 627 | assert(I.getOpcode() < 57 && "Opcode too big???"); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 628 | unsigned Opcode = I.getOpcode(); |
| 629 | unsigned NumOperands = I.getNumOperands(); |
| 630 | |
Chris Lattner | 38287bd | 2005-05-06 06:13:34 +0000 | [diff] [blame] | 631 | // Encode 'tail call' as 61, 'volatile load' as 62, and 'volatile store' as |
| 632 | // 63. |
Chris Lattner | dee199f | 2005-05-06 22:34:01 +0000 | [diff] [blame] | 633 | if (const CallInst *CI = dyn_cast<CallInst>(&I)) { |
| 634 | if (CI->getCallingConv() == CallingConv::C) { |
| 635 | if (CI->isTailCall()) |
| 636 | Opcode = 61; // CCC + Tail Call |
| 637 | else |
| 638 | ; // Opcode = Instruction::Call |
| 639 | } else if (CI->getCallingConv() == CallingConv::Fast) { |
| 640 | if (CI->isTailCall()) |
| 641 | Opcode = 59; // FastCC + TailCall |
| 642 | else |
| 643 | Opcode = 60; // FastCC + Not Tail Call |
| 644 | } else { |
| 645 | Opcode = 58; // Call escape sequence. |
| 646 | } |
Chris Lattner | dee199f | 2005-05-06 22:34:01 +0000 | [diff] [blame] | 647 | } else if (isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) { |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 648 | Opcode = 62; |
Chris Lattner | dee199f | 2005-05-06 22:34:01 +0000 | [diff] [blame] | 649 | } else if (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) { |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 650 | Opcode = 63; |
Chris Lattner | dee199f | 2005-05-06 22:34:01 +0000 | [diff] [blame] | 651 | } |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 652 | |
| 653 | // Figure out which type to encode with the instruction. Typically we want |
| 654 | // the type of the first parameter, as opposed to the type of the instruction |
| 655 | // (for example, with setcc, we always know it returns bool, but the type of |
| 656 | // the first param is actually interesting). But if we have no arguments |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 657 | // we take the type of the instruction itself. |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 658 | // |
| 659 | const Type *Ty; |
| 660 | switch (I.getOpcode()) { |
| 661 | case Instruction::Select: |
| 662 | case Instruction::Malloc: |
| 663 | case Instruction::Alloca: |
| 664 | Ty = I.getType(); // These ALWAYS want to encode the return type |
| 665 | break; |
| 666 | case Instruction::Store: |
| 667 | Ty = I.getOperand(1)->getType(); // Encode the pointer type... |
| 668 | assert(isa<PointerType>(Ty) && "Store to nonpointer type!?!?"); |
| 669 | break; |
| 670 | default: // Otherwise use the default behavior... |
| 671 | Ty = NumOperands ? I.getOperand(0)->getType() : I.getType(); |
| 672 | break; |
| 673 | } |
| 674 | |
| 675 | unsigned Type; |
| 676 | int Slot = Table.getSlot(Ty); |
| 677 | assert(Slot != -1 && "Type not available!!?!"); |
| 678 | Type = (unsigned)Slot; |
| 679 | |
| 680 | // Varargs calls and invokes are encoded entirely different from any other |
| 681 | // instructions. |
| 682 | if (const CallInst *CI = dyn_cast<CallInst>(&I)){ |
| 683 | const PointerType *Ty =cast<PointerType>(CI->getCalledValue()->getType()); |
| 684 | if (cast<FunctionType>(Ty->getElementType())->isVarArg()) { |
| 685 | outputInstrVarArgsCall(CI, Opcode, Table, Type); |
| 686 | return; |
| 687 | } |
| 688 | } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) { |
| 689 | const PointerType *Ty =cast<PointerType>(II->getCalledValue()->getType()); |
| 690 | if (cast<FunctionType>(Ty->getElementType())->isVarArg()) { |
| 691 | outputInstrVarArgsCall(II, Opcode, Table, Type); |
| 692 | return; |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | if (NumOperands <= 3) { |
| 697 | // Make sure that we take the type number into consideration. We don't want |
| 698 | // to overflow the field size for the instruction format we select. |
| 699 | // |
| 700 | unsigned MaxOpSlot = Type; |
| 701 | unsigned Slots[3]; Slots[0] = (1 << 12)-1; // Marker to signify 0 operands |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 702 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 703 | for (unsigned i = 0; i != NumOperands; ++i) { |
| 704 | int slot = Table.getSlot(I.getOperand(i)); |
| 705 | assert(slot != -1 && "Broken bytecode!"); |
| 706 | if (unsigned(slot) > MaxOpSlot) MaxOpSlot = unsigned(slot); |
| 707 | Slots[i] = unsigned(slot); |
| 708 | } |
| 709 | |
| 710 | // Handle the special cases for various instructions... |
| 711 | if (isa<CastInst>(I) || isa<VAArgInst>(I)) { |
| 712 | // Cast has to encode the destination type as the second argument in the |
| 713 | // packet, or else we won't know what type to cast to! |
| 714 | Slots[1] = Table.getSlot(I.getType()); |
| 715 | assert(Slots[1] != ~0U && "Cast return type unknown?"); |
| 716 | if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1]; |
| 717 | NumOperands++; |
Chris Lattner | 42ba6b4 | 2005-11-05 22:08:14 +0000 | [diff] [blame] | 718 | } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) { |
| 719 | assert(NumOperands == 1 && "Bogus allocation!"); |
| 720 | if (AI->getAlignment()) { |
| 721 | Slots[1] = Log2_32(AI->getAlignment())+1; |
| 722 | if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1]; |
| 723 | NumOperands = 2; |
| 724 | } |
Reid Spencer | c8dab49 | 2006-12-03 06:28:54 +0000 | [diff] [blame] | 725 | } else if (isa<ICmpInst>(I) || isa<FCmpInst>(I)) { |
| 726 | // We need to encode the compare instruction's predicate as the third |
| 727 | // operand. Its not really a slot, but we don't want to break the |
| 728 | // instruction format for these instructions. |
| 729 | NumOperands++; |
| 730 | assert(NumOperands == 3 && "CmpInst with wrong number of operands?"); |
| 731 | Slots[2] = unsigned(cast<CmpInst>(&I)->getPredicate()); |
| 732 | if (Slots[2] > MaxOpSlot) |
| 733 | MaxOpSlot = Slots[2]; |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 734 | } else if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I)) { |
| 735 | // We need to encode the type of sequential type indices into their slot # |
| 736 | unsigned Idx = 1; |
| 737 | for (gep_type_iterator I = gep_type_begin(GEP), E = gep_type_end(GEP); |
| 738 | I != E; ++I, ++Idx) |
| 739 | if (isa<SequentialType>(*I)) { |
| 740 | unsigned IdxId; |
| 741 | switch (GEP->getOperand(Idx)->getType()->getTypeID()) { |
| 742 | default: assert(0 && "Unknown index type!"); |
| 743 | case Type::UIntTyID: IdxId = 0; break; |
| 744 | case Type::IntTyID: IdxId = 1; break; |
| 745 | case Type::ULongTyID: IdxId = 2; break; |
| 746 | case Type::LongTyID: IdxId = 3; break; |
| 747 | } |
| 748 | Slots[Idx] = (Slots[Idx] << 2) | IdxId; |
| 749 | if (Slots[Idx] > MaxOpSlot) MaxOpSlot = Slots[Idx]; |
| 750 | } |
Chris Lattner | dee199f | 2005-05-06 22:34:01 +0000 | [diff] [blame] | 751 | } else if (Opcode == 58) { |
| 752 | // If this is the escape sequence for call, emit the tailcall/cc info. |
| 753 | const CallInst &CI = cast<CallInst>(I); |
| 754 | ++NumOperands; |
Chris Lattner | ebf8e6c | 2006-05-19 21:57:37 +0000 | [diff] [blame] | 755 | if (NumOperands <= 3) { |
| 756 | Slots[NumOperands-1] = |
| 757 | (CI.getCallingConv() << 1)|unsigned(CI.isTailCall()); |
Chris Lattner | dee199f | 2005-05-06 22:34:01 +0000 | [diff] [blame] | 758 | if (Slots[NumOperands-1] > MaxOpSlot) |
| 759 | MaxOpSlot = Slots[NumOperands-1]; |
| 760 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 761 | } else if (isa<InvokeInst>(I)) { |
Chris Lattner | dee199f | 2005-05-06 22:34:01 +0000 | [diff] [blame] | 762 | // Invoke escape seq has at least 4 operands to encode. |
| 763 | ++NumOperands; |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | // Decide which instruction encoding to use. This is determined primarily |
| 767 | // by the number of operands, and secondarily by whether or not the max |
| 768 | // operand will fit into the instruction encoding. More operands == fewer |
| 769 | // bits per operand. |
| 770 | // |
| 771 | switch (NumOperands) { |
| 772 | case 0: |
| 773 | case 1: |
| 774 | if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops |
| 775 | outputInstructionFormat1(&I, Opcode, Slots, Type); |
| 776 | return; |
| 777 | } |
| 778 | break; |
| 779 | |
| 780 | case 2: |
| 781 | if (MaxOpSlot < (1 << 8)) { |
| 782 | outputInstructionFormat2(&I, Opcode, Slots, Type); |
| 783 | return; |
| 784 | } |
| 785 | break; |
| 786 | |
| 787 | case 3: |
| 788 | if (MaxOpSlot < (1 << 6)) { |
| 789 | outputInstructionFormat3(&I, Opcode, Slots, Type); |
| 790 | return; |
| 791 | } |
| 792 | break; |
| 793 | default: |
| 794 | break; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | // If we weren't handled before here, we either have a large number of |
| 799 | // operands or a large operand index that we are referring to. |
| 800 | outputInstructionFormat0(&I, Opcode, Table, Type); |
| 801 | } |
| 802 | |
| 803 | //===----------------------------------------------------------------------===// |
| 804 | //=== Block Output ===// |
| 805 | //===----------------------------------------------------------------------===// |
| 806 | |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 807 | BytecodeWriter::BytecodeWriter(std::vector<unsigned char> &o, const Module *M) |
Reid Spencer | 798ff64 | 2004-05-26 07:37:11 +0000 | [diff] [blame] | 808 | : Out(o), Table(M) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 809 | |
Chris Lattner | 83bb3d2 | 2004-01-14 23:36:54 +0000 | [diff] [blame] | 810 | // Emit the signature... |
Chris Lattner | c847f7c | 2006-07-28 22:07:54 +0000 | [diff] [blame] | 811 | static const unsigned char *Sig = (const unsigned char*)"llvm"; |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 812 | output_data(Sig, Sig+4); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 813 | |
| 814 | // Emit the top level CLASS block. |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 815 | BytecodeBlock ModuleBlock(BytecodeFormat::ModuleBlockID, *this, false, true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 816 | |
Chris Lattner | d445c6b | 2003-08-24 13:47:36 +0000 | [diff] [blame] | 817 | bool isBigEndian = M->getEndianness() == Module::BigEndian; |
| 818 | bool hasLongPointers = M->getPointerSize() == Module::Pointer64; |
| 819 | bool hasNoEndianness = M->getEndianness() == Module::AnyEndianness; |
| 820 | bool hasNoPointerSize = M->getPointerSize() == Module::AnyPointerSize; |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 821 | |
Chris Lattner | a79e7cc | 2004-10-16 18:18:16 +0000 | [diff] [blame] | 822 | // Output the version identifier and other information. |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 823 | unsigned Version = (BCVersionNum << 4) | |
Reid Spencer | 38d54be | 2004-08-17 07:45:14 +0000 | [diff] [blame] | 824 | (unsigned)isBigEndian | (hasLongPointers << 1) | |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 825 | (hasNoEndianness << 2) | |
Reid Spencer | 38d54be | 2004-08-17 07:45:14 +0000 | [diff] [blame] | 826 | (hasNoPointerSize << 3); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 827 | output_vbr(Version); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 828 | |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 829 | // The Global type plane comes first |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 830 | { |
Chris Lattner | c847f7c | 2006-07-28 22:07:54 +0000 | [diff] [blame] | 831 | BytecodeBlock CPool(BytecodeFormat::GlobalTypePlaneBlockID, *this); |
| 832 | outputTypes(Type::FirstDerivedTyID); |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 833 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 834 | |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 835 | // The ModuleInfoBlock follows directly after the type information |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 836 | outputModuleInfoBlock(M); |
| 837 | |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 838 | // Output module level constants, used for global variable initializers |
| 839 | outputConstants(false); |
| 840 | |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 841 | // Do the whole module now! Process each function at a time... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 842 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 843 | outputFunction(I); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 844 | |
| 845 | // If needed, output the symbol table for the module... |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 846 | outputSymbolTable(M->getSymbolTable()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Chris Lattner | f9d7178 | 2004-10-14 01:46:07 +0000 | [diff] [blame] | 849 | void BytecodeWriter::outputTypes(unsigned TypeNum) { |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 850 | // Write the type plane for types first because earlier planes (e.g. for a |
| 851 | // primitive type like float) may have constants constructed using types |
| 852 | // coming later (e.g., via getelementptr from a pointer type). The type |
| 853 | // plane is needed before types can be fwd or bkwd referenced. |
| 854 | const std::vector<const Type*>& Types = Table.getTypes(); |
| 855 | assert(!Types.empty() && "No types at all?"); |
| 856 | assert(TypeNum <= Types.size() && "Invalid TypeNo index"); |
| 857 | |
| 858 | unsigned NumEntries = Types.size() - TypeNum; |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 859 | |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 860 | // Output type header: [num entries] |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 861 | output_vbr(NumEntries); |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 862 | |
| 863 | for (unsigned i = TypeNum; i < TypeNum+NumEntries; ++i) |
| 864 | outputType(Types[i]); |
| 865 | } |
| 866 | |
Vikram S. Adve | a7dac3d | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 867 | // Helper function for outputConstants(). |
| 868 | // Writes out all the constants in the plane Plane starting at entry StartNo. |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 869 | // |
Vikram S. Adve | a7dac3d | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 870 | void BytecodeWriter::outputConstantsInPlane(const std::vector<const Value*> |
| 871 | &Plane, unsigned StartNo) { |
| 872 | unsigned ValNo = StartNo; |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 873 | |
Chris Lattner | 83bb3d2 | 2004-01-14 23:36:54 +0000 | [diff] [blame] | 874 | // Scan through and ignore function arguments, global values, and constant |
| 875 | // strings. |
| 876 | for (; ValNo < Plane.size() && |
| 877 | (isa<Argument>(Plane[ValNo]) || isa<GlobalValue>(Plane[ValNo]) || |
| 878 | (isa<ConstantArray>(Plane[ValNo]) && |
| 879 | cast<ConstantArray>(Plane[ValNo])->isString())); ValNo++) |
Vikram S. Adve | a7dac3d | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 880 | /*empty*/; |
| 881 | |
| 882 | unsigned NC = ValNo; // Number of constants |
Chris Lattner | 3bc5a60 | 2006-01-25 23:08:15 +0000 | [diff] [blame] | 883 | for (; NC < Plane.size() && (isa<Constant>(Plane[NC]) || |
| 884 | isa<InlineAsm>(Plane[NC])); NC++) |
Vikram S. Adve | a7dac3d | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 885 | /*empty*/; |
| 886 | NC -= ValNo; // Convert from index into count |
| 887 | if (NC == 0) return; // Skip empty type planes... |
| 888 | |
Chris Lattner | d6942d7 | 2004-01-14 16:54:21 +0000 | [diff] [blame] | 889 | // FIXME: Most slabs only have 1 or 2 entries! We should encode this much |
| 890 | // more compactly. |
| 891 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 892 | // Put out type header: [num entries][type id number] |
Vikram S. Adve | a7dac3d | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 893 | // |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 894 | output_vbr(NC); |
Vikram S. Adve | a7dac3d | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 895 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 896 | // Put out the Type ID Number... |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 897 | int Slot = Table.getSlot(Plane.front()->getType()); |
Vikram S. Adve | a7dac3d | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 898 | assert (Slot != -1 && "Type in constant pool but not in function!!"); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 899 | output_typeid((unsigned)Slot); |
Vikram S. Adve | a7dac3d | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 900 | |
Vikram S. Adve | a7dac3d | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 901 | for (unsigned i = ValNo; i < ValNo+NC; ++i) { |
| 902 | const Value *V = Plane[i]; |
Chris Lattner | 3bc5a60 | 2006-01-25 23:08:15 +0000 | [diff] [blame] | 903 | if (const Constant *C = dyn_cast<Constant>(V)) |
Reid Spencer | e0125b6 | 2004-07-18 00:16:21 +0000 | [diff] [blame] | 904 | outputConstant(C); |
Chris Lattner | 3bc5a60 | 2006-01-25 23:08:15 +0000 | [diff] [blame] | 905 | else |
| 906 | outputInlineAsm(cast<InlineAsm>(V)); |
Vikram S. Adve | a7dac3d | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | |
Chris Lattner | 9e60d8d | 2005-05-05 22:21:19 +0000 | [diff] [blame] | 910 | static inline bool hasNullValue(const Type *Ty) { |
| 911 | return Ty != Type::LabelTy && Ty != Type::VoidTy && !isa<OpaqueType>(Ty); |
Chris Lattner | 80b9734 | 2004-01-17 23:25:43 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 914 | void BytecodeWriter::outputConstants(bool isFunction) { |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 915 | BytecodeBlock CPool(BytecodeFormat::ConstantPoolBlockID, *this, |
Chris Lattner | 0baa0af | 2004-01-15 21:06:57 +0000 | [diff] [blame] | 916 | true /* Elide block if empty */); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 917 | |
| 918 | unsigned NumPlanes = Table.getNumPlanes(); |
Chris Lattner | f69315b | 2003-05-22 18:35:38 +0000 | [diff] [blame] | 919 | |
Reid Spencer | e0125b6 | 2004-07-18 00:16:21 +0000 | [diff] [blame] | 920 | if (isFunction) |
| 921 | // Output the type plane before any constants! |
Chris Lattner | a79e7cc | 2004-10-16 18:18:16 +0000 | [diff] [blame] | 922 | outputTypes(Table.getModuleTypeLevel()); |
Reid Spencer | e0125b6 | 2004-07-18 00:16:21 +0000 | [diff] [blame] | 923 | else |
Chris Lattner | f9d7178 | 2004-10-14 01:46:07 +0000 | [diff] [blame] | 924 | // Output module-level string constants before any other constants. |
Chris Lattner | 83bb3d2 | 2004-01-14 23:36:54 +0000 | [diff] [blame] | 925 | outputConstantStrings(); |
| 926 | |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 927 | for (unsigned pno = 0; pno != NumPlanes; pno++) { |
| 928 | const std::vector<const Value*> &Plane = Table.getPlane(pno); |
| 929 | if (!Plane.empty()) { // Skip empty type planes... |
| 930 | unsigned ValNo = 0; |
| 931 | if (isFunction) // Don't re-emit module constants |
Reid Spencer | 0852c80 | 2004-07-04 11:46:15 +0000 | [diff] [blame] | 932 | ValNo += Table.getModuleLevel(pno); |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 933 | |
Chris Lattner | 9e60d8d | 2005-05-05 22:21:19 +0000 | [diff] [blame] | 934 | if (hasNullValue(Plane[0]->getType())) { |
Reid Spencer | 0852c80 | 2004-07-04 11:46:15 +0000 | [diff] [blame] | 935 | // Skip zero initializer |
| 936 | if (ValNo == 0) |
| 937 | ValNo = 1; |
Chris Lattner | f69315b | 2003-05-22 18:35:38 +0000 | [diff] [blame] | 938 | } |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 939 | |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 940 | // Write out constants in the plane |
| 941 | outputConstantsInPlane(Plane, ValNo); |
Chris Lattner | f69315b | 2003-05-22 18:35:38 +0000 | [diff] [blame] | 942 | } |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 943 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Chris Lattner | 6b25242 | 2003-10-16 18:28:50 +0000 | [diff] [blame] | 946 | static unsigned getEncodedLinkage(const GlobalValue *GV) { |
| 947 | switch (GV->getLinkage()) { |
| 948 | default: assert(0 && "Invalid linkage!"); |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 949 | case GlobalValue::ExternalLinkage: return 0; |
| 950 | case GlobalValue::WeakLinkage: return 1; |
| 951 | case GlobalValue::AppendingLinkage: return 2; |
| 952 | case GlobalValue::InternalLinkage: return 3; |
| 953 | case GlobalValue::LinkOnceLinkage: return 4; |
| 954 | case GlobalValue::DLLImportLinkage: return 5; |
| 955 | case GlobalValue::DLLExportLinkage: return 6; |
| 956 | case GlobalValue::ExternalWeakLinkage: return 7; |
Chris Lattner | 6b25242 | 2003-10-16 18:28:50 +0000 | [diff] [blame] | 957 | } |
| 958 | } |
| 959 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 960 | void BytecodeWriter::outputModuleInfoBlock(const Module *M) { |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 961 | BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfoBlockID, *this); |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 962 | |
Chris Lattner | 404cddf | 2005-11-12 01:33:40 +0000 | [diff] [blame] | 963 | // Give numbers to sections as we encounter them. |
| 964 | unsigned SectionIDCounter = 0; |
| 965 | std::vector<std::string> SectionNames; |
| 966 | std::map<std::string, unsigned> SectionID; |
| 967 | |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 968 | // Output the types for the global variables in the module... |
Chris Lattner | 28caccf | 2005-05-06 20:27:03 +0000 | [diff] [blame] | 969 | for (Module::const_global_iterator I = M->global_begin(), |
Chris Lattner | 8eb52dd | 2005-11-06 07:11:04 +0000 | [diff] [blame] | 970 | End = M->global_end(); I != End; ++I) { |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 971 | int Slot = Table.getSlot(I->getType()); |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 972 | assert(Slot != -1 && "Module global vars is broken!"); |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 973 | |
Chris Lattner | 8eb52dd | 2005-11-06 07:11:04 +0000 | [diff] [blame] | 974 | assert((I->hasInitializer() || !I->hasInternalLinkage()) && |
| 975 | "Global must have an initializer or have external linkage!"); |
| 976 | |
Chris Lattner | 22482a1 | 2003-10-18 06:30:21 +0000 | [diff] [blame] | 977 | // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2-4=Linkage, |
Chris Lattner | 8eb52dd | 2005-11-06 07:11:04 +0000 | [diff] [blame] | 978 | // bit5+ = Slot # for type. |
Chris Lattner | 404cddf | 2005-11-12 01:33:40 +0000 | [diff] [blame] | 979 | bool HasExtensionWord = (I->getAlignment() != 0) || I->hasSection(); |
Chris Lattner | 8eb52dd | 2005-11-06 07:11:04 +0000 | [diff] [blame] | 980 | |
| 981 | // If we need to use the extension byte, set linkage=3(internal) and |
| 982 | // initializer = 0 (impossible!). |
| 983 | if (!HasExtensionWord) { |
| 984 | unsigned oSlot = ((unsigned)Slot << 5) | (getEncodedLinkage(I) << 2) | |
| 985 | (I->hasInitializer() << 1) | (unsigned)I->isConstant(); |
| 986 | output_vbr(oSlot); |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 987 | } else { |
Chris Lattner | 8eb52dd | 2005-11-06 07:11:04 +0000 | [diff] [blame] | 988 | unsigned oSlot = ((unsigned)Slot << 5) | (3 << 2) | |
| 989 | (0 << 1) | (unsigned)I->isConstant(); |
| 990 | output_vbr(oSlot); |
| 991 | |
| 992 | // The extension word has this format: bit 0 = has initializer, bit 1-3 = |
Chris Lattner | 404cddf | 2005-11-12 01:33:40 +0000 | [diff] [blame] | 993 | // linkage, bit 4-8 = alignment (log2), bit 9 = has SectionID, |
| 994 | // bits 10+ = future use. |
Jeff Cohen | ba0ffcc | 2005-11-12 01:01:50 +0000 | [diff] [blame] | 995 | unsigned ExtWord = (unsigned)I->hasInitializer() | |
| 996 | (getEncodedLinkage(I) << 1) | |
Chris Lattner | 404cddf | 2005-11-12 01:33:40 +0000 | [diff] [blame] | 997 | ((Log2_32(I->getAlignment())+1) << 4) | |
| 998 | ((unsigned)I->hasSection() << 9); |
Chris Lattner | 8eb52dd | 2005-11-06 07:11:04 +0000 | [diff] [blame] | 999 | output_vbr(ExtWord); |
Chris Lattner | 404cddf | 2005-11-12 01:33:40 +0000 | [diff] [blame] | 1000 | if (I->hasSection()) { |
| 1001 | // Give section names unique ID's. |
| 1002 | unsigned &Entry = SectionID[I->getSection()]; |
| 1003 | if (Entry == 0) { |
| 1004 | Entry = ++SectionIDCounter; |
| 1005 | SectionNames.push_back(I->getSection()); |
| 1006 | } |
| 1007 | output_vbr(Entry); |
| 1008 | } |
Chris Lattner | 8eb52dd | 2005-11-06 07:11:04 +0000 | [diff] [blame] | 1009 | } |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 1010 | |
Chris Lattner | 1b98c5c | 2001-10-13 06:48:38 +0000 | [diff] [blame] | 1011 | // If we have an initializer, output it now. |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1012 | if (I->hasInitializer()) { |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 1013 | Slot = Table.getSlot((Value*)I->getInitializer()); |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 1014 | assert(Slot != -1 && "No slot for global var initializer!"); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 1015 | output_vbr((unsigned)Slot); |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 1016 | } |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 1017 | } |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 1018 | output_typeid((unsigned)Table.getSlot(Type::VoidTy)); |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 1019 | |
Chris Lattner | a79e7cc | 2004-10-16 18:18:16 +0000 | [diff] [blame] | 1020 | // Output the types of the functions in this module. |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 1021 | for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) { |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 1022 | int Slot = Table.getSlot(I->getType()); |
Chris Lattner | a79e7cc | 2004-10-16 18:18:16 +0000 | [diff] [blame] | 1023 | assert(Slot != -1 && "Module slot calculator is broken!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1024 | assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!"); |
Chris Lattner | e73bd45 | 2005-11-06 07:43:39 +0000 | [diff] [blame] | 1025 | assert(((Slot << 6) >> 6) == Slot && "Slot # too big!"); |
Chris Lattner | 54b369e | 2005-11-06 07:46:13 +0000 | [diff] [blame] | 1026 | unsigned CC = I->getCallingConv()+1; |
| 1027 | unsigned ID = (Slot << 5) | (CC & 15); |
Chris Lattner | 479ffeb | 2005-05-06 20:42:57 +0000 | [diff] [blame] | 1028 | |
Chris Lattner | d6e431f | 2004-11-15 22:39:49 +0000 | [diff] [blame] | 1029 | if (I->isExternal()) // If external, we don't have an FunctionInfo block. |
| 1030 | ID |= 1 << 4; |
Chris Lattner | e73bd45 | 2005-11-06 07:43:39 +0000 | [diff] [blame] | 1031 | |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 1032 | if (I->getAlignment() || I->hasSection() || (CC & ~15) != 0 || |
| 1033 | (I->isExternal() && I->hasDLLImportLinkage()) || |
| 1034 | (I->isExternal() && I->hasExternalWeakLinkage()) |
| 1035 | ) |
Chris Lattner | e73bd45 | 2005-11-06 07:43:39 +0000 | [diff] [blame] | 1036 | ID |= 1 << 31; // Do we need an extension word? |
| 1037 | |
Chris Lattner | a79e7cc | 2004-10-16 18:18:16 +0000 | [diff] [blame] | 1038 | output_vbr(ID); |
Chris Lattner | e73bd45 | 2005-11-06 07:43:39 +0000 | [diff] [blame] | 1039 | |
| 1040 | if (ID & (1 << 31)) { |
| 1041 | // Extension byte: bits 0-4 = alignment, bits 5-9 = top nibble of calling |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 1042 | // convention, bit 10 = hasSectionID., bits 11-12 = external linkage type |
| 1043 | unsigned extLinkage = 0; |
| 1044 | |
| 1045 | if (I->isExternal()) { |
| 1046 | if (I->hasDLLImportLinkage()) { |
| 1047 | extLinkage = 1; |
| 1048 | } else if (I->hasExternalWeakLinkage()) { |
| 1049 | extLinkage = 2; |
| 1050 | } |
| 1051 | } |
| 1052 | |
Chris Lattner | 404cddf | 2005-11-12 01:33:40 +0000 | [diff] [blame] | 1053 | ID = (Log2_32(I->getAlignment())+1) | ((CC >> 4) << 5) | |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 1054 | (I->hasSection() << 10) | |
| 1055 | ((extLinkage & 3) << 11); |
Chris Lattner | e73bd45 | 2005-11-06 07:43:39 +0000 | [diff] [blame] | 1056 | output_vbr(ID); |
Chris Lattner | 404cddf | 2005-11-12 01:33:40 +0000 | [diff] [blame] | 1057 | |
| 1058 | // Give section names unique ID's. |
| 1059 | if (I->hasSection()) { |
| 1060 | unsigned &Entry = SectionID[I->getSection()]; |
| 1061 | if (Entry == 0) { |
| 1062 | Entry = ++SectionIDCounter; |
| 1063 | SectionNames.push_back(I->getSection()); |
| 1064 | } |
| 1065 | output_vbr(Entry); |
| 1066 | } |
Chris Lattner | e73bd45 | 2005-11-06 07:43:39 +0000 | [diff] [blame] | 1067 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1068 | } |
Chris Lattner | a79e7cc | 2004-10-16 18:18:16 +0000 | [diff] [blame] | 1069 | output_vbr((unsigned)Table.getSlot(Type::VoidTy) << 5); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 1070 | |
Chris Lattner | a79e7cc | 2004-10-16 18:18:16 +0000 | [diff] [blame] | 1071 | // Emit the list of dependent libraries for the Module. |
Reid Spencer | 5ac8812 | 2004-07-25 21:32:02 +0000 | [diff] [blame] | 1072 | Module::lib_iterator LI = M->lib_begin(); |
| 1073 | Module::lib_iterator LE = M->lib_end(); |
Chris Lattner | a79e7cc | 2004-10-16 18:18:16 +0000 | [diff] [blame] | 1074 | output_vbr(unsigned(LE - LI)); // Emit the number of dependent libraries. |
| 1075 | for (; LI != LE; ++LI) |
Reid Spencer | 38d54be | 2004-08-17 07:45:14 +0000 | [diff] [blame] | 1076 | output(*LI); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 1077 | |
| 1078 | // Output the target triple from the module |
Reid Spencer | 38d54be | 2004-08-17 07:45:14 +0000 | [diff] [blame] | 1079 | output(M->getTargetTriple()); |
Chris Lattner | 404cddf | 2005-11-12 01:33:40 +0000 | [diff] [blame] | 1080 | |
| 1081 | // Emit the table of section names. |
| 1082 | output_vbr((unsigned)SectionNames.size()); |
| 1083 | for (unsigned i = 0, e = SectionNames.size(); i != e; ++i) |
| 1084 | output(SectionNames[i]); |
Chris Lattner | 7e6db76 | 2006-01-23 23:43:17 +0000 | [diff] [blame] | 1085 | |
| 1086 | // Output the inline asm string. |
Chris Lattner | 6631601 | 2006-01-24 04:14:29 +0000 | [diff] [blame] | 1087 | output(M->getModuleInlineAsm()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 1090 | void BytecodeWriter::outputInstructions(const Function *F) { |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 1091 | BytecodeBlock ILBlock(BytecodeFormat::InstructionListBlockID, *this); |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 1092 | for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 1093 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) |
| 1094 | outputInstruction(*I); |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 1097 | void BytecodeWriter::outputFunction(const Function *F) { |
Chris Lattner | fd7f8fe | 2004-11-15 21:56:33 +0000 | [diff] [blame] | 1098 | // If this is an external function, there is nothing else to emit! |
| 1099 | if (F->isExternal()) return; |
| 1100 | |
Chris Lattner | d6e431f | 2004-11-15 22:39:49 +0000 | [diff] [blame] | 1101 | BytecodeBlock FunctionBlock(BytecodeFormat::FunctionBlockID, *this); |
| 1102 | output_vbr(getEncodedLinkage(F)); |
| 1103 | |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 1104 | // Get slot information about the function... |
| 1105 | Table.incorporateFunction(F); |
| 1106 | |
| 1107 | if (Table.getCompactionTable().empty()) { |
| 1108 | // Output information about the constants in the function if the compaction |
| 1109 | // table is not being used. |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 1110 | outputConstants(true); |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 1111 | } else { |
| 1112 | // Otherwise, emit the compaction table. |
| 1113 | outputCompactionTable(); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 1114 | } |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 1115 | |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 1116 | // Output all of the instructions in the body of the function |
| 1117 | outputInstructions(F); |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 1118 | |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 1119 | // If needed, output the symbol table for the function... |
| 1120 | outputSymbolTable(F->getSymbolTable()); |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 1121 | |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 1122 | Table.purgeFunction(); |
| 1123 | } |
| 1124 | |
| 1125 | void BytecodeWriter::outputCompactionTablePlane(unsigned PlaneNo, |
| 1126 | const std::vector<const Value*> &Plane, |
| 1127 | unsigned StartNo) { |
| 1128 | unsigned End = Table.getModuleLevel(PlaneNo); |
Chris Lattner | 52f86d6 | 2004-01-20 00:54:06 +0000 | [diff] [blame] | 1129 | if (Plane.empty() || StartNo == End || End == 0) return; // Nothing to emit |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 1130 | assert(StartNo < End && "Cannot emit negative range!"); |
| 1131 | assert(StartNo < Plane.size() && End <= Plane.size()); |
| 1132 | |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 1133 | // Do not emit the null initializer! |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 1134 | ++StartNo; |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 1135 | |
Chris Lattner | 2410243 | 2004-01-18 22:35:34 +0000 | [diff] [blame] | 1136 | // Figure out which encoding to use. By far the most common case we have is |
| 1137 | // to emit 0-2 entries in a compaction table plane. |
| 1138 | switch (End-StartNo) { |
| 1139 | case 0: // Avoid emitting two vbr's if possible. |
| 1140 | case 1: |
| 1141 | case 2: |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 1142 | output_vbr((PlaneNo << 2) | End-StartNo); |
Chris Lattner | 2410243 | 2004-01-18 22:35:34 +0000 | [diff] [blame] | 1143 | break; |
| 1144 | default: |
| 1145 | // Output the number of things. |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 1146 | output_vbr((unsigned(End-StartNo) << 2) | 3); |
| 1147 | output_typeid(PlaneNo); // Emit the type plane this is |
Chris Lattner | 2410243 | 2004-01-18 22:35:34 +0000 | [diff] [blame] | 1148 | break; |
| 1149 | } |
| 1150 | |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 1151 | for (unsigned i = StartNo; i != End; ++i) |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 1152 | output_vbr(Table.getGlobalSlot(Plane[i])); |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 1155 | void BytecodeWriter::outputCompactionTypes(unsigned StartNo) { |
| 1156 | // Get the compaction type table from the slot calculator |
| 1157 | const std::vector<const Type*> &CTypes = Table.getCompactionTypes(); |
| 1158 | |
| 1159 | // The compaction types may have been uncompactified back to the |
| 1160 | // global types. If so, we just write an empty table |
Chris Lattner | c847f7c | 2006-07-28 22:07:54 +0000 | [diff] [blame] | 1161 | if (CTypes.size() == 0) { |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 1162 | output_vbr(0U); |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 1163 | return; |
| 1164 | } |
| 1165 | |
| 1166 | assert(CTypes.size() >= StartNo && "Invalid compaction types start index"); |
| 1167 | |
| 1168 | // Determine how many types to write |
| 1169 | unsigned NumTypes = CTypes.size() - StartNo; |
| 1170 | |
| 1171 | // Output the number of types. |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 1172 | output_vbr(NumTypes); |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 1173 | |
| 1174 | for (unsigned i = StartNo; i < StartNo+NumTypes; ++i) |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 1175 | output_typeid(Table.getGlobalSlot(CTypes[i])); |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 1178 | void BytecodeWriter::outputCompactionTable() { |
Reid Spencer | 0033c18 | 2004-08-27 00:38:44 +0000 | [diff] [blame] | 1179 | // Avoid writing the compaction table at all if there is no content. |
| 1180 | if (Table.getCompactionTypes().size() >= Type::FirstDerivedTyID || |
| 1181 | (!Table.CompactionTableIsEmpty())) { |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 1182 | BytecodeBlock CTB(BytecodeFormat::CompactionTableBlockID, *this, |
Reid Spencer | 0033c18 | 2004-08-27 00:38:44 +0000 | [diff] [blame] | 1183 | true/*ElideIfEmpty*/); |
Chris Lattner | f9d7178 | 2004-10-14 01:46:07 +0000 | [diff] [blame] | 1184 | const std::vector<std::vector<const Value*> > &CT = |
| 1185 | Table.getCompactionTable(); |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 1186 | |
Reid Spencer | 0033c18 | 2004-08-27 00:38:44 +0000 | [diff] [blame] | 1187 | // First things first, emit the type compaction table if there is one. |
| 1188 | outputCompactionTypes(Type::FirstDerivedTyID); |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 1189 | |
Reid Spencer | 0033c18 | 2004-08-27 00:38:44 +0000 | [diff] [blame] | 1190 | for (unsigned i = 0, e = CT.size(); i != e; ++i) |
| 1191 | outputCompactionTablePlane(i, CT[i], 0); |
| 1192 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1195 | void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) { |
Chris Lattner | 737d3cd | 2004-01-10 19:56:59 +0000 | [diff] [blame] | 1196 | // Do not output the Bytecode block for an empty symbol table, it just wastes |
| 1197 | // space! |
Chris Lattner | f9d7178 | 2004-10-14 01:46:07 +0000 | [diff] [blame] | 1198 | if (MST.isEmpty()) return; |
Chris Lattner | 737d3cd | 2004-01-10 19:56:59 +0000 | [diff] [blame] | 1199 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 1200 | BytecodeBlock SymTabBlock(BytecodeFormat::SymbolTableBlockID, *this, |
Chris Lattner | f9d7178 | 2004-10-14 01:46:07 +0000 | [diff] [blame] | 1201 | true/*ElideIfEmpty*/); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1202 | |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 1203 | // Write the number of types |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 1204 | output_vbr(MST.num_types()); |
Reid Spencer | 250c418 | 2004-08-17 02:59:02 +0000 | [diff] [blame] | 1205 | |
| 1206 | // Write each of the types |
Reid Spencer | 94f2df2 | 2004-05-25 17:29:59 +0000 | [diff] [blame] | 1207 | for (SymbolTable::type_const_iterator TI = MST.type_begin(), |
Chris Lattner | c847f7c | 2006-07-28 22:07:54 +0000 | [diff] [blame] | 1208 | TE = MST.type_end(); TI != TE; ++TI) { |
Reid Spencer | 250c418 | 2004-08-17 02:59:02 +0000 | [diff] [blame] | 1209 | // Symtab entry:[def slot #][name] |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 1210 | output_typeid((unsigned)Table.getSlot(TI->second)); |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 1211 | output(TI->first); |
Reid Spencer | 94f2df2 | 2004-05-25 17:29:59 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | // Now do each of the type planes in order. |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 1215 | for (SymbolTable::plane_const_iterator PI = MST.plane_begin(), |
Reid Spencer | 94f2df2 | 2004-05-25 17:29:59 +0000 | [diff] [blame] | 1216 | PE = MST.plane_end(); PI != PE; ++PI) { |
| 1217 | SymbolTable::value_const_iterator I = MST.value_begin(PI->first); |
| 1218 | SymbolTable::value_const_iterator End = MST.value_end(PI->first); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1219 | int Slot; |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 1220 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1221 | if (I == End) continue; // Don't mess with an absent type... |
| 1222 | |
Reid Spencer | 250c418 | 2004-08-17 02:59:02 +0000 | [diff] [blame] | 1223 | // Write the number of values in this plane |
Chris Lattner | 001d16a | 2005-03-07 02:59:36 +0000 | [diff] [blame] | 1224 | output_vbr((unsigned)PI->second.size()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1225 | |
Reid Spencer | 250c418 | 2004-08-17 02:59:02 +0000 | [diff] [blame] | 1226 | // Write the slot number of the type for this plane |
Reid Spencer | 94f2df2 | 2004-05-25 17:29:59 +0000 | [diff] [blame] | 1227 | Slot = Table.getSlot(PI->first); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1228 | assert(Slot != -1 && "Type in symtab, but not in table!"); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 1229 | output_typeid((unsigned)Slot); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1230 | |
Reid Spencer | 250c418 | 2004-08-17 02:59:02 +0000 | [diff] [blame] | 1231 | // Write each of the values in this plane |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 1232 | for (; I != End; ++I) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1233 | // Symtab entry: [def slot #][name] |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 1234 | Slot = Table.getSlot(I->second); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 1235 | assert(Slot != -1 && "Value in symtab but has no slot number!!"); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 1236 | output_vbr((unsigned)Slot); |
Reid Spencer | 38d54be | 2004-08-17 07:45:14 +0000 | [diff] [blame] | 1237 | output(I->first); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1238 | } |
| 1239 | } |
| 1240 | } |
| 1241 | |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame^] | 1242 | void llvm::WriteBytecodeToFile(const Module *M, OStream &Out, |
Chris Lattner | c847f7c | 2006-07-28 22:07:54 +0000 | [diff] [blame] | 1243 | bool compress) { |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 1244 | assert(M && "You can't write a null module!!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1245 | |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 1246 | // Make sure that std::cout is put into binary mode for systems |
| 1247 | // that care. |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame^] | 1248 | if (Out == cout) |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 1249 | sys::Program::ChangeStdoutToBinary(); |
| 1250 | |
Reid Spencer | 17f52c5 | 2004-11-06 23:17:23 +0000 | [diff] [blame] | 1251 | // Create a vector of unsigned char for the bytecode output. We |
| 1252 | // reserve 256KBytes of space in the vector so that we avoid doing |
| 1253 | // lots of little allocations. 256KBytes is sufficient for a large |
| 1254 | // proportion of the bytecode files we will encounter. Larger files |
| 1255 | // will be automatically doubled in size as needed (std::vector |
| 1256 | // behavior). |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 1257 | std::vector<unsigned char> Buffer; |
Reid Spencer | 17f52c5 | 2004-11-06 23:17:23 +0000 | [diff] [blame] | 1258 | Buffer.reserve(256 * 1024); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1259 | |
Reid Spencer | 17f52c5 | 2004-11-06 23:17:23 +0000 | [diff] [blame] | 1260 | // The BytecodeWriter populates Buffer for us. |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 1261 | BytecodeWriter BCW(Buffer, M); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1262 | |
Reid Spencer | 17f52c5 | 2004-11-06 23:17:23 +0000 | [diff] [blame] | 1263 | // Keep track of how much we've written |
Chris Lattner | ce6ef11 | 2002-07-26 18:40:14 +0000 | [diff] [blame] | 1264 | BytesWritten += Buffer.size(); |
| 1265 | |
Reid Spencer | 17f52c5 | 2004-11-06 23:17:23 +0000 | [diff] [blame] | 1266 | // Determine start and end points of the Buffer |
Reid Spencer | 83296f5 | 2004-11-07 18:17:38 +0000 | [diff] [blame] | 1267 | const unsigned char *FirstByte = &Buffer.front(); |
Reid Spencer | 17f52c5 | 2004-11-06 23:17:23 +0000 | [diff] [blame] | 1268 | |
| 1269 | // If we're supposed to compress this mess ... |
| 1270 | if (compress) { |
| 1271 | |
| 1272 | // We signal compression by using an alternate magic number for the |
Reid Spencer | 83296f5 | 2004-11-07 18:17:38 +0000 | [diff] [blame] | 1273 | // file. The compressed bytecode file's magic number is "llvc" instead |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 1274 | // of "llvm". |
Reid Spencer | 83296f5 | 2004-11-07 18:17:38 +0000 | [diff] [blame] | 1275 | char compressed_magic[4]; |
| 1276 | compressed_magic[0] = 'l'; |
| 1277 | compressed_magic[1] = 'l'; |
| 1278 | compressed_magic[2] = 'v'; |
| 1279 | compressed_magic[3] = 'c'; |
Reid Spencer | 17f52c5 | 2004-11-06 23:17:23 +0000 | [diff] [blame] | 1280 | |
Bill Wendling | 68fe61d | 2006-11-29 00:19:40 +0000 | [diff] [blame] | 1281 | Out.stream()->write(compressed_magic,4); |
Reid Spencer | 17f52c5 | 2004-11-06 23:17:23 +0000 | [diff] [blame] | 1282 | |
Reid Spencer | a70d84d | 2004-11-14 22:01:41 +0000 | [diff] [blame] | 1283 | // Compress everything after the magic number (which we altered) |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 1284 | Compressor::compressToStream( |
Reid Spencer | 17f52c5 | 2004-11-06 23:17:23 +0000 | [diff] [blame] | 1285 | (char*)(FirstByte+4), // Skip the magic number |
| 1286 | Buffer.size()-4, // Skip the magic number |
Bill Wendling | 68fe61d | 2006-11-29 00:19:40 +0000 | [diff] [blame] | 1287 | *Out.stream() // Where to write compressed data |
Reid Spencer | 17f52c5 | 2004-11-06 23:17:23 +0000 | [diff] [blame] | 1288 | ); |
| 1289 | |
Reid Spencer | 17f52c5 | 2004-11-06 23:17:23 +0000 | [diff] [blame] | 1290 | } else { |
| 1291 | |
| 1292 | // We're not compressing, so just write the entire block. |
Bill Wendling | 68fe61d | 2006-11-29 00:19:40 +0000 | [diff] [blame] | 1293 | Out.stream()->write((char*)FirstByte, Buffer.size()); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 1294 | } |
Reid Spencer | 17f52c5 | 2004-11-06 23:17:23 +0000 | [diff] [blame] | 1295 | |
| 1296 | // make sure it hits disk now |
Bill Wendling | 68fe61d | 2006-11-29 00:19:40 +0000 | [diff] [blame] | 1297 | Out.stream()->flush(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1298 | } |