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