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