1 //===-- Writer.cpp - Library for writing LLVM bytecode files --------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This library implements the functionality defined in llvm/Bytecode/Writer.h
12 // Note that this file uses an unusual technique of outputting all the bytecode
13 // to a vector of unsigned char, then copies the vector to an ostream. The
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. :( :( :(
18 //===----------------------------------------------------------------------===//
20 #include "WriterInternals.h"
21 #include "llvm/Bytecode/WriteBytecodePass.h"
22 #include "llvm/Constants.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Instructions.h"
25 #include "llvm/Module.h"
26 #include "llvm/SymbolTable.h"
27 #include "llvm/Support/GetElementPtrTypeIterator.h"
28 #include "llvm/Support/Compressor.h"
29 #include "llvm/ADT/STLExtras.h"
30 #include "llvm/ADT/Statistic.h"
35 /// This value needs to be incremented every time the bytecode format changes
36 /// so that the reader can distinguish which format of the bytecode file has
38 /// @brief The bytecode version number
39 const unsigned BCVersionNum = 5;
41 static RegisterPass<WriteBytecodePass> X("emitbytecode", "Bytecode Writer");
44 BytesWritten("bytecodewriter", "Number of bytecode bytes written");
46 //===----------------------------------------------------------------------===//
47 //=== Output Primitives ===//
48 //===----------------------------------------------------------------------===//
50 // output - If a position is specified, it must be in the valid portion of the
51 // string... note that this should be inlined always so only the relevant IF
52 // body should be included.
53 inline void BytecodeWriter::output(unsigned i, int pos) {
54 if (pos == -1) { // Be endian clean, little endian is our friend
55 Out.push_back((unsigned char)i);
56 Out.push_back((unsigned char)(i >> 8));
57 Out.push_back((unsigned char)(i >> 16));
58 Out.push_back((unsigned char)(i >> 24));
60 Out[pos ] = (unsigned char)i;
61 Out[pos+1] = (unsigned char)(i >> 8);
62 Out[pos+2] = (unsigned char)(i >> 16);
63 Out[pos+3] = (unsigned char)(i >> 24);
67 inline void BytecodeWriter::output(int i) {
71 /// output_vbr - Output an unsigned value, by using the least number of bytes
72 /// possible. This is useful because many of our "infinite" values are really
73 /// very small most of the time; but can be large a few times.
74 /// Data format used: If you read a byte with the high bit set, use the low
75 /// seven bits as data and then read another byte.
76 inline void BytecodeWriter::output_vbr(uint64_t i) {
78 if (i < 0x80) { // done?
79 Out.push_back((unsigned char)i); // We know the high bit is clear...
83 // Nope, we are bigger than a character, output the next 7 bits and set the
84 // high bit to say that there is more coming...
85 Out.push_back(0x80 | ((unsigned char)i & 0x7F));
86 i >>= 7; // Shift out 7 bits now...
90 inline void BytecodeWriter::output_vbr(unsigned i) {
92 if (i < 0x80) { // done?
93 Out.push_back((unsigned char)i); // We know the high bit is clear...
97 // Nope, we are bigger than a character, output the next 7 bits and set the
98 // high bit to say that there is more coming...
99 Out.push_back(0x80 | ((unsigned char)i & 0x7F));
100 i >>= 7; // Shift out 7 bits now...
104 inline void BytecodeWriter::output_typeid(unsigned i) {
108 this->output_vbr(0x00FFFFFF);
113 inline void BytecodeWriter::output_vbr(int64_t i) {
115 output_vbr(((uint64_t)(-i) << 1) | 1); // Set low order sign bit...
117 output_vbr((uint64_t)i << 1); // Low order bit is clear.
121 inline void BytecodeWriter::output_vbr(int i) {
123 output_vbr(((unsigned)(-i) << 1) | 1); // Set low order sign bit...
125 output_vbr((unsigned)i << 1); // Low order bit is clear.
128 inline void BytecodeWriter::output(const std::string &s) {
129 unsigned Len = s.length();
130 output_vbr(Len ); // Strings may have an arbitrary length...
131 Out.insert(Out.end(), s.begin(), s.end());
134 inline void BytecodeWriter::output_data(const void *Ptr, const void *End) {
135 Out.insert(Out.end(), (const unsigned char*)Ptr, (const unsigned char*)End);
138 inline void BytecodeWriter::output_float(float& FloatVal) {
139 /// FIXME: This isn't optimal, it has size problems on some platforms
140 /// where FP is not IEEE.
145 FloatUnion.f = FloatVal;
146 Out.push_back( static_cast<unsigned char>( (FloatUnion.i & 0xFF )));
147 Out.push_back( static_cast<unsigned char>( (FloatUnion.i >> 8) & 0xFF));
148 Out.push_back( static_cast<unsigned char>( (FloatUnion.i >> 16) & 0xFF));
149 Out.push_back( static_cast<unsigned char>( (FloatUnion.i >> 24) & 0xFF));
152 inline void BytecodeWriter::output_double(double& DoubleVal) {
153 /// FIXME: This isn't optimal, it has size problems on some platforms
154 /// where FP is not IEEE.
159 DoubleUnion.d = DoubleVal;
160 Out.push_back( static_cast<unsigned char>( (DoubleUnion.i & 0xFF )));
161 Out.push_back( static_cast<unsigned char>( (DoubleUnion.i >> 8) & 0xFF));
162 Out.push_back( static_cast<unsigned char>( (DoubleUnion.i >> 16) & 0xFF));
163 Out.push_back( static_cast<unsigned char>( (DoubleUnion.i >> 24) & 0xFF));
164 Out.push_back( static_cast<unsigned char>( (DoubleUnion.i >> 32) & 0xFF));
165 Out.push_back( static_cast<unsigned char>( (DoubleUnion.i >> 40) & 0xFF));
166 Out.push_back( static_cast<unsigned char>( (DoubleUnion.i >> 48) & 0xFF));
167 Out.push_back( static_cast<unsigned char>( (DoubleUnion.i >> 56) & 0xFF));
170 inline BytecodeBlock::BytecodeBlock(unsigned ID, BytecodeWriter& w,
171 bool elideIfEmpty, bool hasLongFormat )
172 : Id(ID), Writer(w), ElideIfEmpty(elideIfEmpty), HasLongFormat(hasLongFormat){
176 w.output(0U); // For length in long format
178 w.output(0U); /// Place holder for ID and length for this block
183 inline BytecodeBlock::~BytecodeBlock() { // Do backpatch when block goes out
185 if (Loc == Writer.size() && ElideIfEmpty) {
186 // If the block is empty, and we are allowed to, do not emit the block at
188 Writer.resize(Writer.size()-(HasLongFormat?8:4));
193 Writer.output(unsigned(Writer.size()-Loc), int(Loc-4));
195 Writer.output(unsigned(Writer.size()-Loc) << 5 | (Id & 0x1F), int(Loc-4));
198 //===----------------------------------------------------------------------===//
199 //=== Constant Output ===//
200 //===----------------------------------------------------------------------===//
202 void BytecodeWriter::outputType(const Type *T) {
203 output_vbr((unsigned)T->getTypeID());
205 // That's all there is to handling primitive types...
206 if (T->isPrimitiveType()) {
207 return; // We might do this if we alias a prim type: %x = type int
210 switch (T->getTypeID()) { // Handle derived types now.
211 case Type::FunctionTyID: {
212 const FunctionType *MT = cast<FunctionType>(T);
213 int Slot = Table.getSlot(MT->getReturnType());
214 assert(Slot != -1 && "Type used but not available!!");
215 output_typeid((unsigned)Slot);
217 // Output the number of arguments to function (+1 if varargs):
218 output_vbr((unsigned)MT->getNumParams()+MT->isVarArg());
220 // Output all of the arguments...
221 FunctionType::param_iterator I = MT->param_begin();
222 for (; I != MT->param_end(); ++I) {
223 Slot = Table.getSlot(*I);
224 assert(Slot != -1 && "Type used but not available!!");
225 output_typeid((unsigned)Slot);
228 // Terminate list with VoidTy if we are a varargs function...
230 output_typeid((unsigned)Type::VoidTyID);
234 case Type::ArrayTyID: {
235 const ArrayType *AT = cast<ArrayType>(T);
236 int Slot = Table.getSlot(AT->getElementType());
237 assert(Slot != -1 && "Type used but not available!!");
238 output_typeid((unsigned)Slot);
239 output_vbr(AT->getNumElements());
243 case Type::PackedTyID: {
244 const PackedType *PT = cast<PackedType>(T);
245 int Slot = Table.getSlot(PT->getElementType());
246 assert(Slot != -1 && "Type used but not available!!");
247 output_typeid((unsigned)Slot);
248 output_vbr(PT->getNumElements());
253 case Type::StructTyID: {
254 const StructType *ST = cast<StructType>(T);
256 // Output all of the element types...
257 for (StructType::element_iterator I = ST->element_begin(),
258 E = ST->element_end(); I != E; ++I) {
259 int Slot = Table.getSlot(*I);
260 assert(Slot != -1 && "Type used but not available!!");
261 output_typeid((unsigned)Slot);
264 // Terminate list with VoidTy
265 output_typeid((unsigned)Type::VoidTyID);
269 case Type::PointerTyID: {
270 const PointerType *PT = cast<PointerType>(T);
271 int Slot = Table.getSlot(PT->getElementType());
272 assert(Slot != -1 && "Type used but not available!!");
273 output_typeid((unsigned)Slot);
277 case Type::OpaqueTyID:
278 // No need to emit anything, just the count of opaque types is enough.
282 std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
283 << " Type '" << T->getDescription() << "'\n";
288 void BytecodeWriter::outputConstant(const Constant *CPV) {
289 assert((CPV->getType()->isPrimitiveType() || !CPV->isNullValue()) &&
290 "Shouldn't output null constants!");
292 // We must check for a ConstantExpr before switching by type because
293 // a ConstantExpr can be of any type, and has no explicit value.
295 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
296 // FIXME: Encoding of constant exprs could be much more compact!
297 assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
298 output_vbr(1+CE->getNumOperands()); // flags as an expr
299 output_vbr(CE->getOpcode()); // flags as an expr
301 for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
302 int Slot = Table.getSlot(*OI);
303 assert(Slot != -1 && "Unknown constant used in ConstantExpr!!");
304 output_vbr((unsigned)Slot);
305 Slot = Table.getSlot((*OI)->getType());
306 output_typeid((unsigned)Slot);
309 } else if (isa<UndefValue>(CPV)) {
310 output_vbr(1U); // 1 -> UndefValue constant.
313 output_vbr(0U); // flag as not a ConstantExpr
316 switch (CPV->getType()->getTypeID()) {
317 case Type::BoolTyID: // Boolean Types
318 if (cast<ConstantBool>(CPV)->getValue())
324 case Type::UByteTyID: // Unsigned integer types...
325 case Type::UShortTyID:
327 case Type::ULongTyID:
328 output_vbr(cast<ConstantUInt>(CPV)->getValue());
331 case Type::SByteTyID: // Signed integer types...
332 case Type::ShortTyID:
335 output_vbr(cast<ConstantSInt>(CPV)->getValue());
338 case Type::ArrayTyID: {
339 const ConstantArray *CPA = cast<ConstantArray>(CPV);
340 assert(!CPA->isString() && "Constant strings should be handled specially!");
342 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) {
343 int Slot = Table.getSlot(CPA->getOperand(i));
344 assert(Slot != -1 && "Constant used but not available!!");
345 output_vbr((unsigned)Slot);
350 case Type::PackedTyID: {
351 const ConstantPacked *CP = cast<ConstantPacked>(CPV);
353 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) {
354 int Slot = Table.getSlot(CP->getOperand(i));
355 assert(Slot != -1 && "Constant used but not available!!");
356 output_vbr((unsigned)Slot);
361 case Type::StructTyID: {
362 const ConstantStruct *CPS = cast<ConstantStruct>(CPV);
364 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) {
365 int Slot = Table.getSlot(CPS->getOperand(i));
366 assert(Slot != -1 && "Constant used but not available!!");
367 output_vbr((unsigned)Slot);
372 case Type::PointerTyID:
373 assert(0 && "No non-null, non-constant-expr constants allowed!");
376 case Type::FloatTyID: { // Floating point types...
377 float Tmp = (float)cast<ConstantFP>(CPV)->getValue();
381 case Type::DoubleTyID: {
382 double Tmp = cast<ConstantFP>(CPV)->getValue();
388 case Type::LabelTyID:
390 std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
391 << " type '" << *CPV->getType() << "'\n";
397 void BytecodeWriter::outputConstantStrings() {
398 SlotCalculator::string_iterator I = Table.string_begin();
399 SlotCalculator::string_iterator E = Table.string_end();
400 if (I == E) return; // No strings to emit
402 // If we have != 0 strings to emit, output them now. Strings are emitted into
403 // the 'void' type plane.
404 output_vbr(unsigned(E-I));
405 output_typeid(Type::VoidTyID);
407 // Emit all of the strings.
408 for (I = Table.string_begin(); I != E; ++I) {
409 const ConstantArray *Str = *I;
410 int Slot = Table.getSlot(Str->getType());
411 assert(Slot != -1 && "Constant string of unknown type?");
412 output_typeid((unsigned)Slot);
414 // Now that we emitted the type (which indicates the size of the string),
415 // emit all of the characters.
416 std::string Val = Str->getAsString();
417 output_data(Val.c_str(), Val.c_str()+Val.size());
421 //===----------------------------------------------------------------------===//
422 //=== Instruction Output ===//
423 //===----------------------------------------------------------------------===//
424 typedef unsigned char uchar;
426 // outputInstructionFormat0 - Output those wierd instructions that have a large
427 // number of operands or have large operands themselves...
429 // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
431 void BytecodeWriter::outputInstructionFormat0(const Instruction *I,
433 const SlotCalculator &Table,
435 // Opcode must have top two bits clear...
436 output_vbr(Opcode << 2); // Instruction Opcode ID
437 output_typeid(Type); // Result type
439 unsigned NumArgs = I->getNumOperands();
440 output_vbr(NumArgs + (isa<CastInst>(I) || isa<VANextInst>(I) ||
443 if (!isa<GetElementPtrInst>(&I)) {
444 for (unsigned i = 0; i < NumArgs; ++i) {
445 int Slot = Table.getSlot(I->getOperand(i));
446 assert(Slot >= 0 && "No slot number for value!?!?");
447 output_vbr((unsigned)Slot);
450 if (isa<CastInst>(I) || isa<VAArgInst>(I)) {
451 int Slot = Table.getSlot(I->getType());
452 assert(Slot != -1 && "Cast return type unknown?");
453 output_typeid((unsigned)Slot);
454 } else if (const VANextInst *VAI = dyn_cast<VANextInst>(I)) {
455 int Slot = Table.getSlot(VAI->getArgType());
456 assert(Slot != -1 && "VarArg argument type unknown?");
457 output_typeid((unsigned)Slot);
461 int Slot = Table.getSlot(I->getOperand(0));
462 assert(Slot >= 0 && "No slot number for value!?!?");
463 output_vbr(unsigned(Slot));
465 // We need to encode the type of sequential type indices into their slot #
467 for (gep_type_iterator TI = gep_type_begin(I), E = gep_type_end(I);
468 Idx != NumArgs; ++TI, ++Idx) {
469 Slot = Table.getSlot(I->getOperand(Idx));
470 assert(Slot >= 0 && "No slot number for value!?!?");
472 if (isa<SequentialType>(*TI)) {
474 switch (I->getOperand(Idx)->getType()->getTypeID()) {
475 default: assert(0 && "Unknown index type!");
476 case Type::UIntTyID: IdxId = 0; break;
477 case Type::IntTyID: IdxId = 1; break;
478 case Type::ULongTyID: IdxId = 2; break;
479 case Type::LongTyID: IdxId = 3; break;
481 Slot = (Slot << 2) | IdxId;
483 output_vbr(unsigned(Slot));
489 // outputInstrVarArgsCall - Output the absurdly annoying varargs function calls.
490 // This are more annoying than most because the signature of the call does not
491 // tell us anything about the types of the arguments in the varargs portion.
492 // Because of this, we encode (as type 0) all of the argument types explicitly
493 // before the argument value. This really sucks, but you shouldn't be using
494 // varargs functions in your code! *death to printf*!
496 // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
498 void BytecodeWriter::outputInstrVarArgsCall(const Instruction *I,
500 const SlotCalculator &Table,
502 assert(isa<CallInst>(I) || isa<InvokeInst>(I));
503 // Opcode must have top two bits clear...
504 output_vbr(Opcode << 2); // Instruction Opcode ID
505 output_typeid(Type); // Result type (varargs type)
507 const PointerType *PTy = cast<PointerType>(I->getOperand(0)->getType());
508 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
509 unsigned NumParams = FTy->getNumParams();
511 unsigned NumFixedOperands;
512 if (isa<CallInst>(I)) {
513 // Output an operand for the callee and each fixed argument, then two for
514 // each variable argument.
515 NumFixedOperands = 1+NumParams;
517 assert(isa<InvokeInst>(I) && "Not call or invoke??");
518 // Output an operand for the callee and destinations, then two for each
519 // variable argument.
520 NumFixedOperands = 3+NumParams;
522 output_vbr(2 * I->getNumOperands()-NumFixedOperands);
524 // The type for the function has already been emitted in the type field of the
525 // instruction. Just emit the slot # now.
526 for (unsigned i = 0; i != NumFixedOperands; ++i) {
527 int Slot = Table.getSlot(I->getOperand(i));
528 assert(Slot >= 0 && "No slot number for value!?!?");
529 output_vbr((unsigned)Slot);
532 for (unsigned i = NumFixedOperands, e = I->getNumOperands(); i != e; ++i) {
533 // Output Arg Type ID
534 int Slot = Table.getSlot(I->getOperand(i)->getType());
535 assert(Slot >= 0 && "No slot number for value!?!?");
536 output_typeid((unsigned)Slot);
538 // Output arg ID itself
539 Slot = Table.getSlot(I->getOperand(i));
540 assert(Slot >= 0 && "No slot number for value!?!?");
541 output_vbr((unsigned)Slot);
546 // outputInstructionFormat1 - Output one operand instructions, knowing that no
547 // operand index is >= 2^12.
549 inline void BytecodeWriter::outputInstructionFormat1(const Instruction *I,
553 // bits Instruction format:
554 // --------------------------
555 // 01-00: Opcode type, fixed to 1.
557 // 19-08: Resulting type plane
558 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
560 output(1 | (Opcode << 2) | (Type << 8) | (Slots[0] << 20));
564 // outputInstructionFormat2 - Output two operand instructions, knowing that no
565 // operand index is >= 2^8.
567 inline void BytecodeWriter::outputInstructionFormat2(const Instruction *I,
571 // bits Instruction format:
572 // --------------------------
573 // 01-00: Opcode type, fixed to 2.
575 // 15-08: Resulting type plane
579 output(2 | (Opcode << 2) | (Type << 8) | (Slots[0] << 16) | (Slots[1] << 24));
583 // outputInstructionFormat3 - Output three operand instructions, knowing that no
584 // operand index is >= 2^6.
586 inline void BytecodeWriter::outputInstructionFormat3(const Instruction *I,
590 // bits Instruction format:
591 // --------------------------
592 // 01-00: Opcode type, fixed to 3.
594 // 13-08: Resulting type plane
599 output(3 | (Opcode << 2) | (Type << 8) |
600 (Slots[0] << 14) | (Slots[1] << 20) | (Slots[2] << 26));
603 void BytecodeWriter::outputInstruction(const Instruction &I) {
604 assert(I.getOpcode() < 62 && "Opcode too big???");
605 unsigned Opcode = I.getOpcode();
606 unsigned NumOperands = I.getNumOperands();
608 // Encode 'volatile load' as 62 and 'volatile store' as 63.
609 if (isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile())
611 if (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())
614 // Figure out which type to encode with the instruction. Typically we want
615 // the type of the first parameter, as opposed to the type of the instruction
616 // (for example, with setcc, we always know it returns bool, but the type of
617 // the first param is actually interesting). But if we have no arguments
618 // we take the type of the instruction itself.
621 switch (I.getOpcode()) {
622 case Instruction::Select:
623 case Instruction::Malloc:
624 case Instruction::Alloca:
625 Ty = I.getType(); // These ALWAYS want to encode the return type
627 case Instruction::Store:
628 Ty = I.getOperand(1)->getType(); // Encode the pointer type...
629 assert(isa<PointerType>(Ty) && "Store to nonpointer type!?!?");
631 default: // Otherwise use the default behavior...
632 Ty = NumOperands ? I.getOperand(0)->getType() : I.getType();
637 int Slot = Table.getSlot(Ty);
638 assert(Slot != -1 && "Type not available!!?!");
639 Type = (unsigned)Slot;
641 // Varargs calls and invokes are encoded entirely different from any other
643 if (const CallInst *CI = dyn_cast<CallInst>(&I)){
644 const PointerType *Ty =cast<PointerType>(CI->getCalledValue()->getType());
645 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
646 outputInstrVarArgsCall(CI, Opcode, Table, Type);
649 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
650 const PointerType *Ty =cast<PointerType>(II->getCalledValue()->getType());
651 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
652 outputInstrVarArgsCall(II, Opcode, Table, Type);
657 if (NumOperands <= 3) {
658 // Make sure that we take the type number into consideration. We don't want
659 // to overflow the field size for the instruction format we select.
661 unsigned MaxOpSlot = Type;
662 unsigned Slots[3]; Slots[0] = (1 << 12)-1; // Marker to signify 0 operands
664 for (unsigned i = 0; i != NumOperands; ++i) {
665 int slot = Table.getSlot(I.getOperand(i));
666 assert(slot != -1 && "Broken bytecode!");
667 if (unsigned(slot) > MaxOpSlot) MaxOpSlot = unsigned(slot);
668 Slots[i] = unsigned(slot);
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!
675 Slots[1] = Table.getSlot(I.getType());
676 assert(Slots[1] != ~0U && "Cast return type unknown?");
677 if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
679 } else if (const VANextInst *VANI = dyn_cast<VANextInst>(&I)) {
680 Slots[1] = Table.getSlot(VANI->getArgType());
681 assert(Slots[1] != ~0U && "va_next return type unknown?");
682 if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
684 } else if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I)) {
685 // We need to encode the type of sequential type indices into their slot #
687 for (gep_type_iterator I = gep_type_begin(GEP), E = gep_type_end(GEP);
689 if (isa<SequentialType>(*I)) {
691 switch (GEP->getOperand(Idx)->getType()->getTypeID()) {
692 default: assert(0 && "Unknown index type!");
693 case Type::UIntTyID: IdxId = 0; break;
694 case Type::IntTyID: IdxId = 1; break;
695 case Type::ULongTyID: IdxId = 2; break;
696 case Type::LongTyID: IdxId = 3; break;
698 Slots[Idx] = (Slots[Idx] << 2) | IdxId;
699 if (Slots[Idx] > MaxOpSlot) MaxOpSlot = Slots[Idx];
703 // Decide which instruction encoding to use. This is determined primarily
704 // by the number of operands, and secondarily by whether or not the max
705 // operand will fit into the instruction encoding. More operands == fewer
708 switch (NumOperands) {
711 if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops
712 outputInstructionFormat1(&I, Opcode, Slots, Type);
718 if (MaxOpSlot < (1 << 8)) {
719 outputInstructionFormat2(&I, Opcode, Slots, Type);
725 if (MaxOpSlot < (1 << 6)) {
726 outputInstructionFormat3(&I, Opcode, Slots, Type);
735 // If we weren't handled before here, we either have a large number of
736 // operands or a large operand index that we are referring to.
737 outputInstructionFormat0(&I, Opcode, Table, Type);
740 //===----------------------------------------------------------------------===//
741 //=== Block Output ===//
742 //===----------------------------------------------------------------------===//
744 BytecodeWriter::BytecodeWriter(std::vector<unsigned char> &o, const Module *M)
747 // Emit the signature...
748 static const unsigned char *Sig = (const unsigned char*)"llvm";
749 output_data(Sig, Sig+4);
751 // Emit the top level CLASS block.
752 BytecodeBlock ModuleBlock(BytecodeFormat::ModuleBlockID, *this, false, true);
754 bool isBigEndian = M->getEndianness() == Module::BigEndian;
755 bool hasLongPointers = M->getPointerSize() == Module::Pointer64;
756 bool hasNoEndianness = M->getEndianness() == Module::AnyEndianness;
757 bool hasNoPointerSize = M->getPointerSize() == Module::AnyPointerSize;
759 // Output the version identifier and other information.
760 unsigned Version = (BCVersionNum << 4) |
761 (unsigned)isBigEndian | (hasLongPointers << 1) |
762 (hasNoEndianness << 2) |
763 (hasNoPointerSize << 3);
766 // The Global type plane comes first
768 BytecodeBlock CPool(BytecodeFormat::GlobalTypePlaneBlockID, *this );
769 outputTypes(Type::FirstDerivedTyID);
772 // The ModuleInfoBlock follows directly after the type information
773 outputModuleInfoBlock(M);
775 // Output module level constants, used for global variable initializers
776 outputConstants(false);
778 // Do the whole module now! Process each function at a time...
779 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
782 // If needed, output the symbol table for the module...
783 outputSymbolTable(M->getSymbolTable());
786 void BytecodeWriter::outputTypes(unsigned TypeNum) {
787 // Write the type plane for types first because earlier planes (e.g. for a
788 // primitive type like float) may have constants constructed using types
789 // coming later (e.g., via getelementptr from a pointer type). The type
790 // plane is needed before types can be fwd or bkwd referenced.
791 const std::vector<const Type*>& Types = Table.getTypes();
792 assert(!Types.empty() && "No types at all?");
793 assert(TypeNum <= Types.size() && "Invalid TypeNo index");
795 unsigned NumEntries = Types.size() - TypeNum;
797 // Output type header: [num entries]
798 output_vbr(NumEntries);
800 for (unsigned i = TypeNum; i < TypeNum+NumEntries; ++i)
801 outputType(Types[i]);
804 // Helper function for outputConstants().
805 // Writes out all the constants in the plane Plane starting at entry StartNo.
807 void BytecodeWriter::outputConstantsInPlane(const std::vector<const Value*>
808 &Plane, unsigned StartNo) {
809 unsigned ValNo = StartNo;
811 // Scan through and ignore function arguments, global values, and constant
813 for (; ValNo < Plane.size() &&
814 (isa<Argument>(Plane[ValNo]) || isa<GlobalValue>(Plane[ValNo]) ||
815 (isa<ConstantArray>(Plane[ValNo]) &&
816 cast<ConstantArray>(Plane[ValNo])->isString())); ValNo++)
819 unsigned NC = ValNo; // Number of constants
820 for (; NC < Plane.size() && (isa<Constant>(Plane[NC])); NC++)
822 NC -= ValNo; // Convert from index into count
823 if (NC == 0) return; // Skip empty type planes...
825 // FIXME: Most slabs only have 1 or 2 entries! We should encode this much
828 // Output type header: [num entries][type id number]
832 // Output the Type ID Number...
833 int Slot = Table.getSlot(Plane.front()->getType());
834 assert (Slot != -1 && "Type in constant pool but not in function!!");
835 output_typeid((unsigned)Slot);
837 for (unsigned i = ValNo; i < ValNo+NC; ++i) {
838 const Value *V = Plane[i];
839 if (const Constant *C = dyn_cast<Constant>(V)) {
845 static inline bool hasNullValue(unsigned TyID) {
846 return TyID != Type::LabelTyID && TyID != Type::VoidTyID;
849 void BytecodeWriter::outputConstants(bool isFunction) {
850 BytecodeBlock CPool(BytecodeFormat::ConstantPoolBlockID, *this,
851 true /* Elide block if empty */);
853 unsigned NumPlanes = Table.getNumPlanes();
856 // Output the type plane before any constants!
857 outputTypes(Table.getModuleTypeLevel());
859 // Output module-level string constants before any other constants.
860 outputConstantStrings();
862 for (unsigned pno = 0; pno != NumPlanes; pno++) {
863 const std::vector<const Value*> &Plane = Table.getPlane(pno);
864 if (!Plane.empty()) { // Skip empty type planes...
866 if (isFunction) // Don't re-emit module constants
867 ValNo += Table.getModuleLevel(pno);
869 if (hasNullValue(pno)) {
870 // Skip zero initializer
875 // Write out constants in the plane
876 outputConstantsInPlane(Plane, ValNo);
881 static unsigned getEncodedLinkage(const GlobalValue *GV) {
882 switch (GV->getLinkage()) {
883 default: assert(0 && "Invalid linkage!");
884 case GlobalValue::ExternalLinkage: return 0;
885 case GlobalValue::WeakLinkage: return 1;
886 case GlobalValue::AppendingLinkage: return 2;
887 case GlobalValue::InternalLinkage: return 3;
888 case GlobalValue::LinkOnceLinkage: return 4;
892 void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
893 BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfoBlockID, *this);
895 // Output the types for the global variables in the module...
896 for (Module::const_giterator I = M->gbegin(), End = M->gend(); I != End;++I) {
897 int Slot = Table.getSlot(I->getType());
898 assert(Slot != -1 && "Module global vars is broken!");
900 // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2-4=Linkage,
901 // bit5+ = Slot # for type
902 unsigned oSlot = ((unsigned)Slot << 5) | (getEncodedLinkage(I) << 2) |
903 (I->hasInitializer() << 1) | (unsigned)I->isConstant();
906 // If we have an initializer, output it now.
907 if (I->hasInitializer()) {
908 Slot = Table.getSlot((Value*)I->getInitializer());
909 assert(Slot != -1 && "No slot for global var initializer!");
910 output_vbr((unsigned)Slot);
913 output_typeid((unsigned)Table.getSlot(Type::VoidTy));
915 // Output the types of the functions in this module.
916 for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) {
917 int Slot = Table.getSlot(I->getType());
918 assert(Slot != -1 && "Module slot calculator is broken!");
919 assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!");
920 assert(((Slot << 5) >> 5) == Slot && "Slot # too big!");
921 unsigned ID = (Slot << 5) + 1;
922 if (I->isExternal()) // If external, we don't have an FunctionInfo block.
926 output_vbr((unsigned)Table.getSlot(Type::VoidTy) << 5);
928 // Emit the list of dependent libraries for the Module.
929 Module::lib_iterator LI = M->lib_begin();
930 Module::lib_iterator LE = M->lib_end();
931 output_vbr(unsigned(LE - LI)); // Emit the number of dependent libraries.
932 for (; LI != LE; ++LI)
935 // Output the target triple from the module
936 output(M->getTargetTriple());
939 void BytecodeWriter::outputInstructions(const Function *F) {
940 BytecodeBlock ILBlock(BytecodeFormat::InstructionListBlockID, *this);
941 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
942 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
943 outputInstruction(*I);
946 void BytecodeWriter::outputFunction(const Function *F) {
947 // If this is an external function, there is nothing else to emit!
948 if (F->isExternal()) return;
950 BytecodeBlock FunctionBlock(BytecodeFormat::FunctionBlockID, *this);
951 output_vbr(getEncodedLinkage(F));
953 // Get slot information about the function...
954 Table.incorporateFunction(F);
956 if (Table.getCompactionTable().empty()) {
957 // Output information about the constants in the function if the compaction
958 // table is not being used.
959 outputConstants(true);
961 // Otherwise, emit the compaction table.
962 outputCompactionTable();
965 // Output all of the instructions in the body of the function
966 outputInstructions(F);
968 // If needed, output the symbol table for the function...
969 outputSymbolTable(F->getSymbolTable());
971 Table.purgeFunction();
974 void BytecodeWriter::outputCompactionTablePlane(unsigned PlaneNo,
975 const std::vector<const Value*> &Plane,
977 unsigned End = Table.getModuleLevel(PlaneNo);
978 if (Plane.empty() || StartNo == End || End == 0) return; // Nothing to emit
979 assert(StartNo < End && "Cannot emit negative range!");
980 assert(StartNo < Plane.size() && End <= Plane.size());
982 // Do not emit the null initializer!
985 // Figure out which encoding to use. By far the most common case we have is
986 // to emit 0-2 entries in a compaction table plane.
987 switch (End-StartNo) {
988 case 0: // Avoid emitting two vbr's if possible.
991 output_vbr((PlaneNo << 2) | End-StartNo);
994 // Output the number of things.
995 output_vbr((unsigned(End-StartNo) << 2) | 3);
996 output_typeid(PlaneNo); // Emit the type plane this is
1000 for (unsigned i = StartNo; i != End; ++i)
1001 output_vbr(Table.getGlobalSlot(Plane[i]));
1004 void BytecodeWriter::outputCompactionTypes(unsigned StartNo) {
1005 // Get the compaction type table from the slot calculator
1006 const std::vector<const Type*> &CTypes = Table.getCompactionTypes();
1008 // The compaction types may have been uncompactified back to the
1009 // global types. If so, we just write an empty table
1010 if (CTypes.size() == 0 ) {
1015 assert(CTypes.size() >= StartNo && "Invalid compaction types start index");
1017 // Determine how many types to write
1018 unsigned NumTypes = CTypes.size() - StartNo;
1020 // Output the number of types.
1021 output_vbr(NumTypes);
1023 for (unsigned i = StartNo; i < StartNo+NumTypes; ++i)
1024 output_typeid(Table.getGlobalSlot(CTypes[i]));
1027 void BytecodeWriter::outputCompactionTable() {
1028 // Avoid writing the compaction table at all if there is no content.
1029 if (Table.getCompactionTypes().size() >= Type::FirstDerivedTyID ||
1030 (!Table.CompactionTableIsEmpty())) {
1031 BytecodeBlock CTB(BytecodeFormat::CompactionTableBlockID, *this,
1032 true/*ElideIfEmpty*/);
1033 const std::vector<std::vector<const Value*> > &CT =
1034 Table.getCompactionTable();
1036 // First things first, emit the type compaction table if there is one.
1037 outputCompactionTypes(Type::FirstDerivedTyID);
1039 for (unsigned i = 0, e = CT.size(); i != e; ++i)
1040 outputCompactionTablePlane(i, CT[i], 0);
1044 void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
1045 // Do not output the Bytecode block for an empty symbol table, it just wastes
1047 if (MST.isEmpty()) return;
1049 BytecodeBlock SymTabBlock(BytecodeFormat::SymbolTableBlockID, *this,
1050 true/*ElideIfEmpty*/);
1052 // Write the number of types
1053 output_vbr(MST.num_types());
1055 // Write each of the types
1056 for (SymbolTable::type_const_iterator TI = MST.type_begin(),
1057 TE = MST.type_end(); TI != TE; ++TI ) {
1058 // Symtab entry:[def slot #][name]
1059 output_typeid((unsigned)Table.getSlot(TI->second));
1063 // Now do each of the type planes in order.
1064 for (SymbolTable::plane_const_iterator PI = MST.plane_begin(),
1065 PE = MST.plane_end(); PI != PE; ++PI) {
1066 SymbolTable::value_const_iterator I = MST.value_begin(PI->first);
1067 SymbolTable::value_const_iterator End = MST.value_end(PI->first);
1070 if (I == End) continue; // Don't mess with an absent type...
1072 // Write the number of values in this plane
1073 output_vbr(MST.type_size(PI->first));
1075 // Write the slot number of the type for this plane
1076 Slot = Table.getSlot(PI->first);
1077 assert(Slot != -1 && "Type in symtab, but not in table!");
1078 output_typeid((unsigned)Slot);
1080 // Write each of the values in this plane
1081 for (; I != End; ++I) {
1082 // Symtab entry: [def slot #][name]
1083 Slot = Table.getSlot(I->second);
1084 assert(Slot != -1 && "Value in symtab but has no slot number!!");
1085 output_vbr((unsigned)Slot);
1091 void llvm::WriteBytecodeToFile(const Module *M, std::ostream &Out,
1093 assert(M && "You can't write a null module!!");
1095 // Create a vector of unsigned char for the bytecode output. We
1096 // reserve 256KBytes of space in the vector so that we avoid doing
1097 // lots of little allocations. 256KBytes is sufficient for a large
1098 // proportion of the bytecode files we will encounter. Larger files
1099 // will be automatically doubled in size as needed (std::vector
1101 std::vector<unsigned char> Buffer;
1102 Buffer.reserve(256 * 1024);
1104 // The BytecodeWriter populates Buffer for us.
1105 BytecodeWriter BCW(Buffer, M);
1107 // Keep track of how much we've written
1108 BytesWritten += Buffer.size();
1110 // Determine start and end points of the Buffer
1111 const unsigned char *FirstByte = &Buffer.front();
1113 // If we're supposed to compress this mess ...
1116 // We signal compression by using an alternate magic number for the
1117 // file. The compressed bytecode file's magic number is "llvc" instead
1119 char compressed_magic[4];
1120 compressed_magic[0] = 'l';
1121 compressed_magic[1] = 'l';
1122 compressed_magic[2] = 'v';
1123 compressed_magic[3] = 'c';
1125 Out.write(compressed_magic,4);
1127 // Compress everything after the magic number (which we altered)
1128 uint64_t zipSize = Compressor::compressToStream(
1129 (char*)(FirstByte+4), // Skip the magic number
1130 Buffer.size()-4, // Skip the magic number
1131 Out, // Where to write compressed data
1132 Compressor::COMP_TYPE_BZIP2 // Try bzip2 compression first
1137 // We're not compressing, so just write the entire block.
1138 Out.write((char*)FirstByte, Buffer.size());
1141 // make sure it hits disk now