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/CallingConv.h"
23 #include "llvm/Constants.h"
24 #include "llvm/DerivedTypes.h"
25 #include "llvm/InlineAsm.h"
26 #include "llvm/Instructions.h"
27 #include "llvm/Module.h"
28 #include "llvm/SymbolTable.h"
29 #include "llvm/Support/GetElementPtrTypeIterator.h"
30 #include "llvm/Support/Compressor.h"
31 #include "llvm/Support/MathExtras.h"
32 #include "llvm/Support/Streams.h"
33 #include "llvm/System/Program.h"
34 #include "llvm/ADT/STLExtras.h"
35 #include "llvm/ADT/Statistic.h"
40 /// This value needs to be incremented every time the bytecode format changes
41 /// so that the reader can distinguish which format of the bytecode file has
43 /// @brief The bytecode version number
44 const unsigned BCVersionNum = 7;
46 static RegisterPass<WriteBytecodePass> X("emitbytecode", "Bytecode Writer");
49 BytesWritten("bytecodewriter", "Number of bytecode bytes written");
51 //===----------------------------------------------------------------------===//
52 //=== Output Primitives ===//
53 //===----------------------------------------------------------------------===//
55 // output - If a position is specified, it must be in the valid portion of the
56 // string... note that this should be inlined always so only the relevant IF
57 // body should be included.
58 inline void BytecodeWriter::output(unsigned i, int pos) {
59 if (pos == -1) { // Be endian clean, little endian is our friend
60 Out.push_back((unsigned char)i);
61 Out.push_back((unsigned char)(i >> 8));
62 Out.push_back((unsigned char)(i >> 16));
63 Out.push_back((unsigned char)(i >> 24));
65 Out[pos ] = (unsigned char)i;
66 Out[pos+1] = (unsigned char)(i >> 8);
67 Out[pos+2] = (unsigned char)(i >> 16);
68 Out[pos+3] = (unsigned char)(i >> 24);
72 inline void BytecodeWriter::output(int i) {
76 /// output_vbr - Output an unsigned value, by using the least number of bytes
77 /// possible. This is useful because many of our "infinite" values are really
78 /// very small most of the time; but can be large a few times.
79 /// Data format used: If you read a byte with the high bit set, use the low
80 /// seven bits as data and then read another byte.
81 inline void BytecodeWriter::output_vbr(uint64_t i) {
83 if (i < 0x80) { // done?
84 Out.push_back((unsigned char)i); // We know the high bit is clear...
88 // Nope, we are bigger than a character, output the next 7 bits and set the
89 // high bit to say that there is more coming...
90 Out.push_back(0x80 | ((unsigned char)i & 0x7F));
91 i >>= 7; // Shift out 7 bits now...
95 inline void BytecodeWriter::output_vbr(unsigned i) {
97 if (i < 0x80) { // done?
98 Out.push_back((unsigned char)i); // We know the high bit is clear...
102 // Nope, we are bigger than a character, output the next 7 bits and set the
103 // high bit to say that there is more coming...
104 Out.push_back(0x80 | ((unsigned char)i & 0x7F));
105 i >>= 7; // Shift out 7 bits now...
109 inline void BytecodeWriter::output_typeid(unsigned i) {
113 this->output_vbr(0x00FFFFFF);
118 inline void BytecodeWriter::output_vbr(int64_t i) {
120 output_vbr(((uint64_t)(-i) << 1) | 1); // Set low order sign bit...
122 output_vbr((uint64_t)i << 1); // Low order bit is clear.
126 inline void BytecodeWriter::output_vbr(int i) {
128 output_vbr(((unsigned)(-i) << 1) | 1); // Set low order sign bit...
130 output_vbr((unsigned)i << 1); // Low order bit is clear.
133 inline void BytecodeWriter::output(const std::string &s) {
134 unsigned Len = s.length();
135 output_vbr(Len); // Strings may have an arbitrary length.
136 Out.insert(Out.end(), s.begin(), s.end());
139 inline void BytecodeWriter::output_data(const void *Ptr, const void *End) {
140 Out.insert(Out.end(), (const unsigned char*)Ptr, (const unsigned char*)End);
143 inline void BytecodeWriter::output_float(float& FloatVal) {
144 /// FIXME: This isn't optimal, it has size problems on some platforms
145 /// where FP is not IEEE.
146 uint32_t i = FloatToBits(FloatVal);
147 Out.push_back( static_cast<unsigned char>( (i ) & 0xFF));
148 Out.push_back( static_cast<unsigned char>( (i >> 8 ) & 0xFF));
149 Out.push_back( static_cast<unsigned char>( (i >> 16) & 0xFF));
150 Out.push_back( static_cast<unsigned char>( (i >> 24) & 0xFF));
153 inline void BytecodeWriter::output_double(double& DoubleVal) {
154 /// FIXME: This isn't optimal, it has size problems on some platforms
155 /// where FP is not IEEE.
156 uint64_t i = DoubleToBits(DoubleVal);
157 Out.push_back( static_cast<unsigned char>( (i ) & 0xFF));
158 Out.push_back( static_cast<unsigned char>( (i >> 8 ) & 0xFF));
159 Out.push_back( static_cast<unsigned char>( (i >> 16) & 0xFF));
160 Out.push_back( static_cast<unsigned char>( (i >> 24) & 0xFF));
161 Out.push_back( static_cast<unsigned char>( (i >> 32) & 0xFF));
162 Out.push_back( static_cast<unsigned char>( (i >> 40) & 0xFF));
163 Out.push_back( static_cast<unsigned char>( (i >> 48) & 0xFF));
164 Out.push_back( static_cast<unsigned char>( (i >> 56) & 0xFF));
167 inline BytecodeBlock::BytecodeBlock(unsigned ID, BytecodeWriter &w,
168 bool elideIfEmpty, bool hasLongFormat)
169 : Id(ID), Writer(w), ElideIfEmpty(elideIfEmpty), HasLongFormat(hasLongFormat){
173 w.output(0U); // For length in long format
175 w.output(0U); /// Place holder for ID and length for this block
180 inline BytecodeBlock::~BytecodeBlock() { // Do backpatch when block goes out
182 if (Loc == Writer.size() && ElideIfEmpty) {
183 // If the block is empty, and we are allowed to, do not emit the block at
185 Writer.resize(Writer.size()-(HasLongFormat?8:4));
190 Writer.output(unsigned(Writer.size()-Loc), int(Loc-4));
192 Writer.output(unsigned(Writer.size()-Loc) << 5 | (Id & 0x1F), int(Loc-4));
195 //===----------------------------------------------------------------------===//
196 //=== Constant Output ===//
197 //===----------------------------------------------------------------------===//
199 void BytecodeWriter::outputType(const Type *T) {
200 output_vbr((unsigned)T->getTypeID());
202 // That's all there is to handling primitive types...
203 if (T->isPrimitiveType()) {
204 return; // We might do this if we alias a prim type: %x = type int
207 switch (T->getTypeID()) { // Handle derived types now.
208 case Type::FunctionTyID: {
209 const FunctionType *MT = cast<FunctionType>(T);
210 int Slot = Table.getSlot(MT->getReturnType());
211 assert(Slot != -1 && "Type used but not available!!");
212 output_typeid((unsigned)Slot);
214 // Output the number of arguments to function (+1 if varargs):
215 output_vbr((unsigned)MT->getNumParams()+MT->isVarArg());
217 // Output all of the arguments...
218 FunctionType::param_iterator I = MT->param_begin();
219 for (; I != MT->param_end(); ++I) {
220 Slot = Table.getSlot(*I);
221 assert(Slot != -1 && "Type used but not available!!");
222 output_typeid((unsigned)Slot);
225 // Terminate list with VoidTy if we are a varargs function...
227 output_typeid((unsigned)Type::VoidTyID);
231 case Type::ArrayTyID: {
232 const ArrayType *AT = cast<ArrayType>(T);
233 int Slot = Table.getSlot(AT->getElementType());
234 assert(Slot != -1 && "Type used but not available!!");
235 output_typeid((unsigned)Slot);
236 output_vbr(AT->getNumElements());
240 case Type::PackedTyID: {
241 const PackedType *PT = cast<PackedType>(T);
242 int Slot = Table.getSlot(PT->getElementType());
243 assert(Slot != -1 && "Type used but not available!!");
244 output_typeid((unsigned)Slot);
245 output_vbr(PT->getNumElements());
250 case Type::StructTyID: {
251 const StructType *ST = cast<StructType>(T);
253 // Output all of the element types...
254 for (StructType::element_iterator I = ST->element_begin(),
255 E = ST->element_end(); I != E; ++I) {
256 int Slot = Table.getSlot(*I);
257 assert(Slot != -1 && "Type used but not available!!");
258 output_typeid((unsigned)Slot);
261 // Terminate list with VoidTy
262 output_typeid((unsigned)Type::VoidTyID);
266 case Type::PointerTyID: {
267 const PointerType *PT = cast<PointerType>(T);
268 int Slot = Table.getSlot(PT->getElementType());
269 assert(Slot != -1 && "Type used but not available!!");
270 output_typeid((unsigned)Slot);
274 case Type::OpaqueTyID:
275 // No need to emit anything, just the count of opaque types is enough.
279 llvm_cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
280 << " Type '" << T->getDescription() << "'\n";
285 void BytecodeWriter::outputConstant(const Constant *CPV) {
286 assert((CPV->getType()->isPrimitiveType() || !CPV->isNullValue()) &&
287 "Shouldn't output null constants!");
289 // We must check for a ConstantExpr before switching by type because
290 // a ConstantExpr can be of any type, and has no explicit value.
292 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
293 // FIXME: Encoding of constant exprs could be much more compact!
294 assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
295 assert(CE->getNumOperands() != 1 || CE->isCast());
296 output_vbr(1+CE->getNumOperands()); // flags as an expr
297 output_vbr(CE->getOpcode()); // Put out the CE op code
299 for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
300 int Slot = Table.getSlot(*OI);
301 assert(Slot != -1 && "Unknown constant used in ConstantExpr!!");
302 output_vbr((unsigned)Slot);
303 Slot = Table.getSlot((*OI)->getType());
304 output_typeid((unsigned)Slot);
307 } else if (isa<UndefValue>(CPV)) {
308 output_vbr(1U); // 1 -> UndefValue constant.
311 output_vbr(0U); // flag as not a ConstantExpr (i.e. 0 operands)
314 switch (CPV->getType()->getTypeID()) {
315 case Type::BoolTyID: // Boolean Types
316 if (cast<ConstantBool>(CPV)->getValue())
322 case Type::UByteTyID: // Unsigned integer types...
323 case Type::UShortTyID:
325 case Type::ULongTyID:
326 output_vbr(cast<ConstantInt>(CPV)->getZExtValue());
329 case Type::SByteTyID: // Signed integer types...
330 case Type::ShortTyID:
333 output_vbr(cast<ConstantInt>(CPV)->getSExtValue());
336 case Type::ArrayTyID: {
337 const ConstantArray *CPA = cast<ConstantArray>(CPV);
338 assert(!CPA->isString() && "Constant strings should be handled specially!");
340 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) {
341 int Slot = Table.getSlot(CPA->getOperand(i));
342 assert(Slot != -1 && "Constant used but not available!!");
343 output_vbr((unsigned)Slot);
348 case Type::PackedTyID: {
349 const ConstantPacked *CP = cast<ConstantPacked>(CPV);
351 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) {
352 int Slot = Table.getSlot(CP->getOperand(i));
353 assert(Slot != -1 && "Constant used but not available!!");
354 output_vbr((unsigned)Slot);
359 case Type::StructTyID: {
360 const ConstantStruct *CPS = cast<ConstantStruct>(CPV);
362 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) {
363 int Slot = Table.getSlot(CPS->getOperand(i));
364 assert(Slot != -1 && "Constant used but not available!!");
365 output_vbr((unsigned)Slot);
370 case Type::PointerTyID:
371 assert(0 && "No non-null, non-constant-expr constants allowed!");
374 case Type::FloatTyID: { // Floating point types...
375 float Tmp = (float)cast<ConstantFP>(CPV)->getValue();
379 case Type::DoubleTyID: {
380 double Tmp = cast<ConstantFP>(CPV)->getValue();
386 case Type::LabelTyID:
388 llvm_cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
389 << " type '" << *CPV->getType() << "'\n";
395 /// outputInlineAsm - InlineAsm's get emitted to the constant pool, so they can
396 /// be shared by multiple uses.
397 void BytecodeWriter::outputInlineAsm(const InlineAsm *IA) {
398 // Output a marker, so we know when we have one one parsing the constant pool.
399 // Note that this encoding is 5 bytes: not very efficient for a marker. Since
400 // unique inline asms are rare, this should hardly matter.
403 output(IA->getAsmString());
404 output(IA->getConstraintString());
405 output_vbr(unsigned(IA->hasSideEffects()));
408 void BytecodeWriter::outputConstantStrings() {
409 SlotCalculator::string_iterator I = Table.string_begin();
410 SlotCalculator::string_iterator E = Table.string_end();
411 if (I == E) return; // No strings to emit
413 // If we have != 0 strings to emit, output them now. Strings are emitted into
414 // the 'void' type plane.
415 output_vbr(unsigned(E-I));
416 output_typeid(Type::VoidTyID);
418 // Emit all of the strings.
419 for (I = Table.string_begin(); I != E; ++I) {
420 const ConstantArray *Str = *I;
421 int Slot = Table.getSlot(Str->getType());
422 assert(Slot != -1 && "Constant string of unknown type?");
423 output_typeid((unsigned)Slot);
425 // Now that we emitted the type (which indicates the size of the string),
426 // emit all of the characters.
427 std::string Val = Str->getAsString();
428 output_data(Val.c_str(), Val.c_str()+Val.size());
432 //===----------------------------------------------------------------------===//
433 //=== Instruction Output ===//
434 //===----------------------------------------------------------------------===//
436 // outputInstructionFormat0 - Output those weird instructions that have a large
437 // number of operands or have large operands themselves.
439 // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
441 void BytecodeWriter::outputInstructionFormat0(const Instruction *I,
443 const SlotCalculator &Table,
445 // Opcode must have top two bits clear...
446 output_vbr(Opcode << 2); // Instruction Opcode ID
447 output_typeid(Type); // Result type
449 unsigned NumArgs = I->getNumOperands();
450 output_vbr(NumArgs + (isa<CastInst>(I) || isa<InvokeInst>(I) ||
451 isa<VAArgInst>(I) || Opcode == 58));
453 if (!isa<GetElementPtrInst>(&I)) {
454 for (unsigned i = 0; i < NumArgs; ++i) {
455 int Slot = Table.getSlot(I->getOperand(i));
456 assert(Slot >= 0 && "No slot number for value!?!?");
457 output_vbr((unsigned)Slot);
460 if (isa<CastInst>(I) || isa<VAArgInst>(I)) {
461 int Slot = Table.getSlot(I->getType());
462 assert(Slot != -1 && "Cast return type unknown?");
463 output_typeid((unsigned)Slot);
464 } else if (isa<InvokeInst>(I)) {
465 output_vbr(cast<InvokeInst>(I)->getCallingConv());
466 } else if (Opcode == 58) { // Call escape sequence
467 output_vbr((cast<CallInst>(I)->getCallingConv() << 1) |
468 unsigned(cast<CallInst>(I)->isTailCall()));
471 int Slot = Table.getSlot(I->getOperand(0));
472 assert(Slot >= 0 && "No slot number for value!?!?");
473 output_vbr(unsigned(Slot));
475 // We need to encode the type of sequential type indices into their slot #
477 for (gep_type_iterator TI = gep_type_begin(I), E = gep_type_end(I);
478 Idx != NumArgs; ++TI, ++Idx) {
479 Slot = Table.getSlot(I->getOperand(Idx));
480 assert(Slot >= 0 && "No slot number for value!?!?");
482 if (isa<SequentialType>(*TI)) {
484 switch (I->getOperand(Idx)->getType()->getTypeID()) {
485 default: assert(0 && "Unknown index type!");
486 case Type::UIntTyID: IdxId = 0; break;
487 case Type::IntTyID: IdxId = 1; break;
488 case Type::ULongTyID: IdxId = 2; break;
489 case Type::LongTyID: IdxId = 3; break;
491 Slot = (Slot << 2) | IdxId;
493 output_vbr(unsigned(Slot));
499 // outputInstrVarArgsCall - Output the absurdly annoying varargs function calls.
500 // This are more annoying than most because the signature of the call does not
501 // tell us anything about the types of the arguments in the varargs portion.
502 // Because of this, we encode (as type 0) all of the argument types explicitly
503 // before the argument value. This really sucks, but you shouldn't be using
504 // varargs functions in your code! *death to printf*!
506 // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
508 void BytecodeWriter::outputInstrVarArgsCall(const Instruction *I,
510 const SlotCalculator &Table,
512 assert(isa<CallInst>(I) || isa<InvokeInst>(I));
513 // Opcode must have top two bits clear...
514 output_vbr(Opcode << 2); // Instruction Opcode ID
515 output_typeid(Type); // Result type (varargs type)
517 const PointerType *PTy = cast<PointerType>(I->getOperand(0)->getType());
518 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
519 unsigned NumParams = FTy->getNumParams();
521 unsigned NumFixedOperands;
522 if (isa<CallInst>(I)) {
523 // Output an operand for the callee and each fixed argument, then two for
524 // each variable argument.
525 NumFixedOperands = 1+NumParams;
527 assert(isa<InvokeInst>(I) && "Not call or invoke??");
528 // Output an operand for the callee and destinations, then two for each
529 // variable argument.
530 NumFixedOperands = 3+NumParams;
532 output_vbr(2 * I->getNumOperands()-NumFixedOperands +
533 unsigned(Opcode == 58 || isa<InvokeInst>(I)));
535 // The type for the function has already been emitted in the type field of the
536 // instruction. Just emit the slot # now.
537 for (unsigned i = 0; i != NumFixedOperands; ++i) {
538 int Slot = Table.getSlot(I->getOperand(i));
539 assert(Slot >= 0 && "No slot number for value!?!?");
540 output_vbr((unsigned)Slot);
543 for (unsigned i = NumFixedOperands, e = I->getNumOperands(); i != e; ++i) {
544 // Output Arg Type ID
545 int Slot = Table.getSlot(I->getOperand(i)->getType());
546 assert(Slot >= 0 && "No slot number for value!?!?");
547 output_typeid((unsigned)Slot);
549 // Output arg ID itself
550 Slot = Table.getSlot(I->getOperand(i));
551 assert(Slot >= 0 && "No slot number for value!?!?");
552 output_vbr((unsigned)Slot);
555 if (isa<InvokeInst>(I)) {
556 // Emit the tail call/calling conv for invoke instructions
557 output_vbr(cast<InvokeInst>(I)->getCallingConv());
558 } else if (Opcode == 58) {
559 const CallInst *CI = cast<CallInst>(I);
560 output_vbr((CI->getCallingConv() << 1) | unsigned(CI->isTailCall()));
565 // outputInstructionFormat1 - Output one operand instructions, knowing that no
566 // operand index is >= 2^12.
568 inline void BytecodeWriter::outputInstructionFormat1(const Instruction *I,
572 // bits Instruction format:
573 // --------------------------
574 // 01-00: Opcode type, fixed to 1.
576 // 19-08: Resulting type plane
577 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
579 output(1 | (Opcode << 2) | (Type << 8) | (Slots[0] << 20));
583 // outputInstructionFormat2 - Output two operand instructions, knowing that no
584 // operand index is >= 2^8.
586 inline void BytecodeWriter::outputInstructionFormat2(const Instruction *I,
590 // bits Instruction format:
591 // --------------------------
592 // 01-00: Opcode type, fixed to 2.
594 // 15-08: Resulting type plane
598 output(2 | (Opcode << 2) | (Type << 8) | (Slots[0] << 16) | (Slots[1] << 24));
602 // outputInstructionFormat3 - Output three operand instructions, knowing that no
603 // operand index is >= 2^6.
605 inline void BytecodeWriter::outputInstructionFormat3(const Instruction *I,
609 // bits Instruction format:
610 // --------------------------
611 // 01-00: Opcode type, fixed to 3.
613 // 13-08: Resulting type plane
618 output(3 | (Opcode << 2) | (Type << 8) |
619 (Slots[0] << 14) | (Slots[1] << 20) | (Slots[2] << 26));
622 void BytecodeWriter::outputInstruction(const Instruction &I) {
623 assert(I.getOpcode() < 57 && "Opcode too big???");
624 unsigned Opcode = I.getOpcode();
625 unsigned NumOperands = I.getNumOperands();
627 // Encode 'tail call' as 61, 'volatile load' as 62, and 'volatile store' as
629 if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
630 if (CI->getCallingConv() == CallingConv::C) {
631 if (CI->isTailCall())
632 Opcode = 61; // CCC + Tail Call
634 ; // Opcode = Instruction::Call
635 } else if (CI->getCallingConv() == CallingConv::Fast) {
636 if (CI->isTailCall())
637 Opcode = 59; // FastCC + TailCall
639 Opcode = 60; // FastCC + Not Tail Call
641 Opcode = 58; // Call escape sequence.
643 } else if (isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) {
645 } else if (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) {
649 // Figure out which type to encode with the instruction. Typically we want
650 // the type of the first parameter, as opposed to the type of the instruction
651 // (for example, with setcc, we always know it returns bool, but the type of
652 // the first param is actually interesting). But if we have no arguments
653 // we take the type of the instruction itself.
656 switch (I.getOpcode()) {
657 case Instruction::Select:
658 case Instruction::Malloc:
659 case Instruction::Alloca:
660 Ty = I.getType(); // These ALWAYS want to encode the return type
662 case Instruction::Store:
663 Ty = I.getOperand(1)->getType(); // Encode the pointer type...
664 assert(isa<PointerType>(Ty) && "Store to nonpointer type!?!?");
666 default: // Otherwise use the default behavior...
667 Ty = NumOperands ? I.getOperand(0)->getType() : I.getType();
672 int Slot = Table.getSlot(Ty);
673 assert(Slot != -1 && "Type not available!!?!");
674 Type = (unsigned)Slot;
676 // Varargs calls and invokes are encoded entirely different from any other
678 if (const CallInst *CI = dyn_cast<CallInst>(&I)){
679 const PointerType *Ty =cast<PointerType>(CI->getCalledValue()->getType());
680 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
681 outputInstrVarArgsCall(CI, Opcode, Table, Type);
684 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
685 const PointerType *Ty =cast<PointerType>(II->getCalledValue()->getType());
686 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
687 outputInstrVarArgsCall(II, Opcode, Table, Type);
692 if (NumOperands <= 3) {
693 // Make sure that we take the type number into consideration. We don't want
694 // to overflow the field size for the instruction format we select.
696 unsigned MaxOpSlot = Type;
697 unsigned Slots[3]; Slots[0] = (1 << 12)-1; // Marker to signify 0 operands
699 for (unsigned i = 0; i != NumOperands; ++i) {
700 int slot = Table.getSlot(I.getOperand(i));
701 assert(slot != -1 && "Broken bytecode!");
702 if (unsigned(slot) > MaxOpSlot) MaxOpSlot = unsigned(slot);
703 Slots[i] = unsigned(slot);
706 // Handle the special cases for various instructions...
707 if (isa<CastInst>(I) || isa<VAArgInst>(I)) {
708 // Cast has to encode the destination type as the second argument in the
709 // packet, or else we won't know what type to cast to!
710 Slots[1] = Table.getSlot(I.getType());
711 assert(Slots[1] != ~0U && "Cast return type unknown?");
712 if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
714 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
715 assert(NumOperands == 1 && "Bogus allocation!");
716 if (AI->getAlignment()) {
717 Slots[1] = Log2_32(AI->getAlignment())+1;
718 if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
721 } else if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I)) {
722 // We need to encode the type of sequential type indices into their slot #
724 for (gep_type_iterator I = gep_type_begin(GEP), E = gep_type_end(GEP);
726 if (isa<SequentialType>(*I)) {
728 switch (GEP->getOperand(Idx)->getType()->getTypeID()) {
729 default: assert(0 && "Unknown index type!");
730 case Type::UIntTyID: IdxId = 0; break;
731 case Type::IntTyID: IdxId = 1; break;
732 case Type::ULongTyID: IdxId = 2; break;
733 case Type::LongTyID: IdxId = 3; break;
735 Slots[Idx] = (Slots[Idx] << 2) | IdxId;
736 if (Slots[Idx] > MaxOpSlot) MaxOpSlot = Slots[Idx];
738 } else if (Opcode == 58) {
739 // If this is the escape sequence for call, emit the tailcall/cc info.
740 const CallInst &CI = cast<CallInst>(I);
742 if (NumOperands <= 3) {
743 Slots[NumOperands-1] =
744 (CI.getCallingConv() << 1)|unsigned(CI.isTailCall());
745 if (Slots[NumOperands-1] > MaxOpSlot)
746 MaxOpSlot = Slots[NumOperands-1];
748 } else if (isa<InvokeInst>(I)) {
749 // Invoke escape seq has at least 4 operands to encode.
753 // Decide which instruction encoding to use. This is determined primarily
754 // by the number of operands, and secondarily by whether or not the max
755 // operand will fit into the instruction encoding. More operands == fewer
758 switch (NumOperands) {
761 if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops
762 outputInstructionFormat1(&I, Opcode, Slots, Type);
768 if (MaxOpSlot < (1 << 8)) {
769 outputInstructionFormat2(&I, Opcode, Slots, Type);
775 if (MaxOpSlot < (1 << 6)) {
776 outputInstructionFormat3(&I, Opcode, Slots, Type);
785 // If we weren't handled before here, we either have a large number of
786 // operands or a large operand index that we are referring to.
787 outputInstructionFormat0(&I, Opcode, Table, Type);
790 //===----------------------------------------------------------------------===//
791 //=== Block Output ===//
792 //===----------------------------------------------------------------------===//
794 BytecodeWriter::BytecodeWriter(std::vector<unsigned char> &o, const Module *M)
797 // Emit the signature...
798 static const unsigned char *Sig = (const unsigned char*)"llvm";
799 output_data(Sig, Sig+4);
801 // Emit the top level CLASS block.
802 BytecodeBlock ModuleBlock(BytecodeFormat::ModuleBlockID, *this, false, true);
804 bool isBigEndian = M->getEndianness() == Module::BigEndian;
805 bool hasLongPointers = M->getPointerSize() == Module::Pointer64;
806 bool hasNoEndianness = M->getEndianness() == Module::AnyEndianness;
807 bool hasNoPointerSize = M->getPointerSize() == Module::AnyPointerSize;
809 // Output the version identifier and other information.
810 unsigned Version = (BCVersionNum << 4) |
811 (unsigned)isBigEndian | (hasLongPointers << 1) |
812 (hasNoEndianness << 2) |
813 (hasNoPointerSize << 3);
816 // The Global type plane comes first
818 BytecodeBlock CPool(BytecodeFormat::GlobalTypePlaneBlockID, *this);
819 outputTypes(Type::FirstDerivedTyID);
822 // The ModuleInfoBlock follows directly after the type information
823 outputModuleInfoBlock(M);
825 // Output module level constants, used for global variable initializers
826 outputConstants(false);
828 // Do the whole module now! Process each function at a time...
829 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
832 // If needed, output the symbol table for the module...
833 outputSymbolTable(M->getSymbolTable());
836 void BytecodeWriter::outputTypes(unsigned TypeNum) {
837 // Write the type plane for types first because earlier planes (e.g. for a
838 // primitive type like float) may have constants constructed using types
839 // coming later (e.g., via getelementptr from a pointer type). The type
840 // plane is needed before types can be fwd or bkwd referenced.
841 const std::vector<const Type*>& Types = Table.getTypes();
842 assert(!Types.empty() && "No types at all?");
843 assert(TypeNum <= Types.size() && "Invalid TypeNo index");
845 unsigned NumEntries = Types.size() - TypeNum;
847 // Output type header: [num entries]
848 output_vbr(NumEntries);
850 for (unsigned i = TypeNum; i < TypeNum+NumEntries; ++i)
851 outputType(Types[i]);
854 // Helper function for outputConstants().
855 // Writes out all the constants in the plane Plane starting at entry StartNo.
857 void BytecodeWriter::outputConstantsInPlane(const std::vector<const Value*>
858 &Plane, unsigned StartNo) {
859 unsigned ValNo = StartNo;
861 // Scan through and ignore function arguments, global values, and constant
863 for (; ValNo < Plane.size() &&
864 (isa<Argument>(Plane[ValNo]) || isa<GlobalValue>(Plane[ValNo]) ||
865 (isa<ConstantArray>(Plane[ValNo]) &&
866 cast<ConstantArray>(Plane[ValNo])->isString())); ValNo++)
869 unsigned NC = ValNo; // Number of constants
870 for (; NC < Plane.size() && (isa<Constant>(Plane[NC]) ||
871 isa<InlineAsm>(Plane[NC])); NC++)
873 NC -= ValNo; // Convert from index into count
874 if (NC == 0) return; // Skip empty type planes...
876 // FIXME: Most slabs only have 1 or 2 entries! We should encode this much
879 // Put out type header: [num entries][type id number]
883 // Put out the Type ID Number...
884 int Slot = Table.getSlot(Plane.front()->getType());
885 assert (Slot != -1 && "Type in constant pool but not in function!!");
886 output_typeid((unsigned)Slot);
888 for (unsigned i = ValNo; i < ValNo+NC; ++i) {
889 const Value *V = Plane[i];
890 if (const Constant *C = dyn_cast<Constant>(V))
893 outputInlineAsm(cast<InlineAsm>(V));
897 static inline bool hasNullValue(const Type *Ty) {
898 return Ty != Type::LabelTy && Ty != Type::VoidTy && !isa<OpaqueType>(Ty);
901 void BytecodeWriter::outputConstants(bool isFunction) {
902 BytecodeBlock CPool(BytecodeFormat::ConstantPoolBlockID, *this,
903 true /* Elide block if empty */);
905 unsigned NumPlanes = Table.getNumPlanes();
908 // Output the type plane before any constants!
909 outputTypes(Table.getModuleTypeLevel());
911 // Output module-level string constants before any other constants.
912 outputConstantStrings();
914 for (unsigned pno = 0; pno != NumPlanes; pno++) {
915 const std::vector<const Value*> &Plane = Table.getPlane(pno);
916 if (!Plane.empty()) { // Skip empty type planes...
918 if (isFunction) // Don't re-emit module constants
919 ValNo += Table.getModuleLevel(pno);
921 if (hasNullValue(Plane[0]->getType())) {
922 // Skip zero initializer
927 // Write out constants in the plane
928 outputConstantsInPlane(Plane, ValNo);
933 static unsigned getEncodedLinkage(const GlobalValue *GV) {
934 switch (GV->getLinkage()) {
935 default: assert(0 && "Invalid linkage!");
936 case GlobalValue::ExternalLinkage: return 0;
937 case GlobalValue::WeakLinkage: return 1;
938 case GlobalValue::AppendingLinkage: return 2;
939 case GlobalValue::InternalLinkage: return 3;
940 case GlobalValue::LinkOnceLinkage: return 4;
941 case GlobalValue::DLLImportLinkage: return 5;
942 case GlobalValue::DLLExportLinkage: return 6;
943 case GlobalValue::ExternalWeakLinkage: return 7;
947 void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
948 BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfoBlockID, *this);
950 // Give numbers to sections as we encounter them.
951 unsigned SectionIDCounter = 0;
952 std::vector<std::string> SectionNames;
953 std::map<std::string, unsigned> SectionID;
955 // Output the types for the global variables in the module...
956 for (Module::const_global_iterator I = M->global_begin(),
957 End = M->global_end(); I != End; ++I) {
958 int Slot = Table.getSlot(I->getType());
959 assert(Slot != -1 && "Module global vars is broken!");
961 assert((I->hasInitializer() || !I->hasInternalLinkage()) &&
962 "Global must have an initializer or have external linkage!");
964 // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2-4=Linkage,
965 // bit5+ = Slot # for type.
966 bool HasExtensionWord = (I->getAlignment() != 0) || I->hasSection();
968 // If we need to use the extension byte, set linkage=3(internal) and
969 // initializer = 0 (impossible!).
970 if (!HasExtensionWord) {
971 unsigned oSlot = ((unsigned)Slot << 5) | (getEncodedLinkage(I) << 2) |
972 (I->hasInitializer() << 1) | (unsigned)I->isConstant();
975 unsigned oSlot = ((unsigned)Slot << 5) | (3 << 2) |
976 (0 << 1) | (unsigned)I->isConstant();
979 // The extension word has this format: bit 0 = has initializer, bit 1-3 =
980 // linkage, bit 4-8 = alignment (log2), bit 9 = has SectionID,
981 // bits 10+ = future use.
982 unsigned ExtWord = (unsigned)I->hasInitializer() |
983 (getEncodedLinkage(I) << 1) |
984 ((Log2_32(I->getAlignment())+1) << 4) |
985 ((unsigned)I->hasSection() << 9);
987 if (I->hasSection()) {
988 // Give section names unique ID's.
989 unsigned &Entry = SectionID[I->getSection()];
991 Entry = ++SectionIDCounter;
992 SectionNames.push_back(I->getSection());
998 // If we have an initializer, output it now.
999 if (I->hasInitializer()) {
1000 Slot = Table.getSlot((Value*)I->getInitializer());
1001 assert(Slot != -1 && "No slot for global var initializer!");
1002 output_vbr((unsigned)Slot);
1005 output_typeid((unsigned)Table.getSlot(Type::VoidTy));
1007 // Output the types of the functions in this module.
1008 for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) {
1009 int Slot = Table.getSlot(I->getType());
1010 assert(Slot != -1 && "Module slot calculator is broken!");
1011 assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!");
1012 assert(((Slot << 6) >> 6) == Slot && "Slot # too big!");
1013 unsigned CC = I->getCallingConv()+1;
1014 unsigned ID = (Slot << 5) | (CC & 15);
1016 if (I->isExternal()) // If external, we don't have an FunctionInfo block.
1019 if (I->getAlignment() || I->hasSection() || (CC & ~15) != 0 ||
1020 (I->isExternal() && I->hasDLLImportLinkage()) ||
1021 (I->isExternal() && I->hasExternalWeakLinkage())
1023 ID |= 1 << 31; // Do we need an extension word?
1027 if (ID & (1 << 31)) {
1028 // Extension byte: bits 0-4 = alignment, bits 5-9 = top nibble of calling
1029 // convention, bit 10 = hasSectionID., bits 11-12 = external linkage type
1030 unsigned extLinkage = 0;
1032 if (I->isExternal()) {
1033 if (I->hasDLLImportLinkage()) {
1035 } else if (I->hasExternalWeakLinkage()) {
1040 ID = (Log2_32(I->getAlignment())+1) | ((CC >> 4) << 5) |
1041 (I->hasSection() << 10) |
1042 ((extLinkage & 3) << 11);
1045 // Give section names unique ID's.
1046 if (I->hasSection()) {
1047 unsigned &Entry = SectionID[I->getSection()];
1049 Entry = ++SectionIDCounter;
1050 SectionNames.push_back(I->getSection());
1056 output_vbr((unsigned)Table.getSlot(Type::VoidTy) << 5);
1058 // Emit the list of dependent libraries for the Module.
1059 Module::lib_iterator LI = M->lib_begin();
1060 Module::lib_iterator LE = M->lib_end();
1061 output_vbr(unsigned(LE - LI)); // Emit the number of dependent libraries.
1062 for (; LI != LE; ++LI)
1065 // Output the target triple from the module
1066 output(M->getTargetTriple());
1068 // Emit the table of section names.
1069 output_vbr((unsigned)SectionNames.size());
1070 for (unsigned i = 0, e = SectionNames.size(); i != e; ++i)
1071 output(SectionNames[i]);
1073 // Output the inline asm string.
1074 output(M->getModuleInlineAsm());
1077 void BytecodeWriter::outputInstructions(const Function *F) {
1078 BytecodeBlock ILBlock(BytecodeFormat::InstructionListBlockID, *this);
1079 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
1080 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
1081 outputInstruction(*I);
1084 void BytecodeWriter::outputFunction(const Function *F) {
1085 // If this is an external function, there is nothing else to emit!
1086 if (F->isExternal()) return;
1088 BytecodeBlock FunctionBlock(BytecodeFormat::FunctionBlockID, *this);
1089 output_vbr(getEncodedLinkage(F));
1091 // Get slot information about the function...
1092 Table.incorporateFunction(F);
1094 if (Table.getCompactionTable().empty()) {
1095 // Output information about the constants in the function if the compaction
1096 // table is not being used.
1097 outputConstants(true);
1099 // Otherwise, emit the compaction table.
1100 outputCompactionTable();
1103 // Output all of the instructions in the body of the function
1104 outputInstructions(F);
1106 // If needed, output the symbol table for the function...
1107 outputSymbolTable(F->getSymbolTable());
1109 Table.purgeFunction();
1112 void BytecodeWriter::outputCompactionTablePlane(unsigned PlaneNo,
1113 const std::vector<const Value*> &Plane,
1115 unsigned End = Table.getModuleLevel(PlaneNo);
1116 if (Plane.empty() || StartNo == End || End == 0) return; // Nothing to emit
1117 assert(StartNo < End && "Cannot emit negative range!");
1118 assert(StartNo < Plane.size() && End <= Plane.size());
1120 // Do not emit the null initializer!
1123 // Figure out which encoding to use. By far the most common case we have is
1124 // to emit 0-2 entries in a compaction table plane.
1125 switch (End-StartNo) {
1126 case 0: // Avoid emitting two vbr's if possible.
1129 output_vbr((PlaneNo << 2) | End-StartNo);
1132 // Output the number of things.
1133 output_vbr((unsigned(End-StartNo) << 2) | 3);
1134 output_typeid(PlaneNo); // Emit the type plane this is
1138 for (unsigned i = StartNo; i != End; ++i)
1139 output_vbr(Table.getGlobalSlot(Plane[i]));
1142 void BytecodeWriter::outputCompactionTypes(unsigned StartNo) {
1143 // Get the compaction type table from the slot calculator
1144 const std::vector<const Type*> &CTypes = Table.getCompactionTypes();
1146 // The compaction types may have been uncompactified back to the
1147 // global types. If so, we just write an empty table
1148 if (CTypes.size() == 0) {
1153 assert(CTypes.size() >= StartNo && "Invalid compaction types start index");
1155 // Determine how many types to write
1156 unsigned NumTypes = CTypes.size() - StartNo;
1158 // Output the number of types.
1159 output_vbr(NumTypes);
1161 for (unsigned i = StartNo; i < StartNo+NumTypes; ++i)
1162 output_typeid(Table.getGlobalSlot(CTypes[i]));
1165 void BytecodeWriter::outputCompactionTable() {
1166 // Avoid writing the compaction table at all if there is no content.
1167 if (Table.getCompactionTypes().size() >= Type::FirstDerivedTyID ||
1168 (!Table.CompactionTableIsEmpty())) {
1169 BytecodeBlock CTB(BytecodeFormat::CompactionTableBlockID, *this,
1170 true/*ElideIfEmpty*/);
1171 const std::vector<std::vector<const Value*> > &CT =
1172 Table.getCompactionTable();
1174 // First things first, emit the type compaction table if there is one.
1175 outputCompactionTypes(Type::FirstDerivedTyID);
1177 for (unsigned i = 0, e = CT.size(); i != e; ++i)
1178 outputCompactionTablePlane(i, CT[i], 0);
1182 void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
1183 // Do not output the Bytecode block for an empty symbol table, it just wastes
1185 if (MST.isEmpty()) return;
1187 BytecodeBlock SymTabBlock(BytecodeFormat::SymbolTableBlockID, *this,
1188 true/*ElideIfEmpty*/);
1190 // Write the number of types
1191 output_vbr(MST.num_types());
1193 // Write each of the types
1194 for (SymbolTable::type_const_iterator TI = MST.type_begin(),
1195 TE = MST.type_end(); TI != TE; ++TI) {
1196 // Symtab entry:[def slot #][name]
1197 output_typeid((unsigned)Table.getSlot(TI->second));
1201 // Now do each of the type planes in order.
1202 for (SymbolTable::plane_const_iterator PI = MST.plane_begin(),
1203 PE = MST.plane_end(); PI != PE; ++PI) {
1204 SymbolTable::value_const_iterator I = MST.value_begin(PI->first);
1205 SymbolTable::value_const_iterator End = MST.value_end(PI->first);
1208 if (I == End) continue; // Don't mess with an absent type...
1210 // Write the number of values in this plane
1211 output_vbr((unsigned)PI->second.size());
1213 // Write the slot number of the type for this plane
1214 Slot = Table.getSlot(PI->first);
1215 assert(Slot != -1 && "Type in symtab, but not in table!");
1216 output_typeid((unsigned)Slot);
1218 // Write each of the values in this plane
1219 for (; I != End; ++I) {
1220 // Symtab entry: [def slot #][name]
1221 Slot = Table.getSlot(I->second);
1222 assert(Slot != -1 && "Value in symtab but has no slot number!!");
1223 output_vbr((unsigned)Slot);
1229 void llvm::WriteBytecodeToFile(const Module *M, llvm_ostream &Out,
1231 assert(M && "You can't write a null module!!");
1233 // Make sure that std::cout is put into binary mode for systems
1235 if (Out == llvm_cout)
1236 sys::Program::ChangeStdoutToBinary();
1238 // Create a vector of unsigned char for the bytecode output. We
1239 // reserve 256KBytes of space in the vector so that we avoid doing
1240 // lots of little allocations. 256KBytes is sufficient for a large
1241 // proportion of the bytecode files we will encounter. Larger files
1242 // will be automatically doubled in size as needed (std::vector
1244 std::vector<unsigned char> Buffer;
1245 Buffer.reserve(256 * 1024);
1247 // The BytecodeWriter populates Buffer for us.
1248 BytecodeWriter BCW(Buffer, M);
1250 // Keep track of how much we've written
1251 BytesWritten += Buffer.size();
1253 // Determine start and end points of the Buffer
1254 const unsigned char *FirstByte = &Buffer.front();
1256 // If we're supposed to compress this mess ...
1259 // We signal compression by using an alternate magic number for the
1260 // file. The compressed bytecode file's magic number is "llvc" instead
1262 char compressed_magic[4];
1263 compressed_magic[0] = 'l';
1264 compressed_magic[1] = 'l';
1265 compressed_magic[2] = 'v';
1266 compressed_magic[3] = 'c';
1268 Out.stream()->write(compressed_magic,4);
1270 // Compress everything after the magic number (which we altered)
1271 Compressor::compressToStream(
1272 (char*)(FirstByte+4), // Skip the magic number
1273 Buffer.size()-4, // Skip the magic number
1274 *Out.stream() // Where to write compressed data
1279 // We're not compressing, so just write the entire block.
1280 Out.stream()->write((char*)FirstByte, Buffer.size());
1283 // make sure it hits disk now
1284 Out.stream()->flush();