+ // We must check for a ConstantExpr before switching by type because
+ // a ConstantExpr can be of any type, and has no explicit value.
+ //
+ if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
+ // FIXME: Encoding of constant exprs could be much more compact!
+ assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
+ output_vbr(CE->getNumOperands()); // flags as an expr
+ output_vbr(CE->getOpcode()); // flags as an expr
+
+ for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
+ int Slot = Table.getSlot(*OI);
+ assert(Slot != -1 && "Unknown constant used in ConstantExpr!!");
+ output_vbr((unsigned)Slot);
+ Slot = Table.getSlot((*OI)->getType());
+ output_typeid((unsigned)Slot);
+ }
+ return;
+ } else {
+ output_vbr(0U); // flag as not a ConstantExpr
+ }
+
+ switch (CPV->getType()->getTypeID()) {
+ case Type::BoolTyID: // Boolean Types
+ if (cast<ConstantBool>(CPV)->getValue())
+ output_vbr(1U);
+ else
+ output_vbr(0U);
+ break;
+
+ case Type::UByteTyID: // Unsigned integer types...
+ case Type::UShortTyID:
+ case Type::UIntTyID:
+ case Type::ULongTyID:
+ output_vbr(cast<ConstantUInt>(CPV)->getValue());
+ break;
+
+ case Type::SByteTyID: // Signed integer types...
+ case Type::ShortTyID:
+ case Type::IntTyID:
+ case Type::LongTyID:
+ output_vbr(cast<ConstantSInt>(CPV)->getValue());
+ break;
+
+ case Type::ArrayTyID: {
+ const ConstantArray *CPA = cast<ConstantArray>(CPV);
+ assert(!CPA->isString() && "Constant strings should be handled specially!");
+
+ for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) {
+ int Slot = Table.getSlot(CPA->getOperand(i));
+ assert(Slot != -1 && "Constant used but not available!!");
+ output_vbr((unsigned)Slot);
+ }
+ break;
+ }
+
+ case Type::StructTyID: {
+ const ConstantStruct *CPS = cast<ConstantStruct>(CPV);
+
+ for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) {
+ int Slot = Table.getSlot(CPS->getOperand(i));
+ assert(Slot != -1 && "Constant used but not available!!");
+ output_vbr((unsigned)Slot);
+ }
+ break;
+ }
+
+ case Type::PointerTyID:
+ assert(0 && "No non-null, non-constant-expr constants allowed!");
+ abort();
+
+ case Type::FloatTyID: { // Floating point types...
+ float Tmp = (float)cast<ConstantFP>(CPV)->getValue();
+ output_float(Tmp);
+ break;
+ }
+ case Type::DoubleTyID: {
+ double Tmp = cast<ConstantFP>(CPV)->getValue();
+ output_double(Tmp);
+ break;
+ }
+
+ case Type::VoidTyID:
+ case Type::LabelTyID:
+ default:
+ std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
+ << " type '" << *CPV->getType() << "'\n";
+ break;
+ }
+ return;
+}
+
+void BytecodeWriter::outputConstantStrings() {
+ SlotCalculator::string_iterator I = Table.string_begin();
+ SlotCalculator::string_iterator E = Table.string_end();
+ if (I == E) return; // No strings to emit
+
+ // If we have != 0 strings to emit, output them now. Strings are emitted into
+ // the 'void' type plane.
+ output_vbr(unsigned(E-I));
+ output_typeid(Type::VoidTyID);
+
+ // Emit all of the strings.
+ for (I = Table.string_begin(); I != E; ++I) {
+ const ConstantArray *Str = *I;
+ int Slot = Table.getSlot(Str->getType());
+ assert(Slot != -1 && "Constant string of unknown type?");
+ output_typeid((unsigned)Slot);
+
+ // Now that we emitted the type (which indicates the size of the string),
+ // emit all of the characters.
+ std::string Val = Str->getAsString();
+ output_data(Val.c_str(), Val.c_str()+Val.size());
+ }
+}
+
+//===----------------------------------------------------------------------===//
+//=== Instruction Output ===//
+//===----------------------------------------------------------------------===//
+typedef unsigned char uchar;
+
+// outputInstructionFormat0 - Output those wierd instructions that have a large
+// number of operands or have large operands themselves...
+//
+// Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
+//
+void BytecodeWriter::outputInstructionFormat0(const Instruction *I, unsigned Opcode,
+ const SlotCalculator &Table,
+ unsigned Type) {
+ // Opcode must have top two bits clear...
+ output_vbr(Opcode << 2); // Instruction Opcode ID
+ output_typeid(Type); // Result type
+
+ unsigned NumArgs = I->getNumOperands();
+ output_vbr(NumArgs + (isa<CastInst>(I) || isa<VANextInst>(I) ||
+ isa<VAArgInst>(I)));
+
+ if (!isa<GetElementPtrInst>(&I)) {
+ for (unsigned i = 0; i < NumArgs; ++i) {
+ int Slot = Table.getSlot(I->getOperand(i));
+ assert(Slot >= 0 && "No slot number for value!?!?");
+ output_vbr((unsigned)Slot);
+ }
+
+ if (isa<CastInst>(I) || isa<VAArgInst>(I)) {
+ int Slot = Table.getSlot(I->getType());
+ assert(Slot != -1 && "Cast return type unknown?");
+ output_typeid((unsigned)Slot);
+ } else if (const VANextInst *VAI = dyn_cast<VANextInst>(I)) {
+ int Slot = Table.getSlot(VAI->getArgType());
+ assert(Slot != -1 && "VarArg argument type unknown?");
+ output_typeid((unsigned)Slot);
+ }
+
+ } else {
+ int Slot = Table.getSlot(I->getOperand(0));
+ assert(Slot >= 0 && "No slot number for value!?!?");
+ output_vbr(unsigned(Slot));
+
+ // We need to encode the type of sequential type indices into their slot #
+ unsigned Idx = 1;
+ for (gep_type_iterator TI = gep_type_begin(I), E = gep_type_end(I);
+ Idx != NumArgs; ++TI, ++Idx) {
+ Slot = Table.getSlot(I->getOperand(Idx));
+ assert(Slot >= 0 && "No slot number for value!?!?");
+
+ if (isa<SequentialType>(*TI)) {
+ unsigned IdxId;
+ switch (I->getOperand(Idx)->getType()->getTypeID()) {
+ default: assert(0 && "Unknown index type!");
+ case Type::UIntTyID: IdxId = 0; break;
+ case Type::IntTyID: IdxId = 1; break;
+ case Type::ULongTyID: IdxId = 2; break;
+ case Type::LongTyID: IdxId = 3; break;
+ }
+ Slot = (Slot << 2) | IdxId;
+ }
+ output_vbr(unsigned(Slot));
+ }
+ }
+}
+
+
+// outputInstrVarArgsCall - Output the absurdly annoying varargs function calls.
+// This are more annoying than most because the signature of the call does not
+// tell us anything about the types of the arguments in the varargs portion.
+// Because of this, we encode (as type 0) all of the argument types explicitly
+// before the argument value. This really sucks, but you shouldn't be using
+// varargs functions in your code! *death to printf*!
+//
+// Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
+//
+void BytecodeWriter::outputInstrVarArgsCall(const Instruction *I,
+ unsigned Opcode,
+ const SlotCalculator &Table,
+ unsigned Type) {
+ assert(isa<CallInst>(I) || isa<InvokeInst>(I));
+ // Opcode must have top two bits clear...
+ output_vbr(Opcode << 2); // Instruction Opcode ID
+ output_typeid(Type); // Result type (varargs type)
+
+ const PointerType *PTy = cast<PointerType>(I->getOperand(0)->getType());
+ const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
+ unsigned NumParams = FTy->getNumParams();
+
+ unsigned NumFixedOperands;
+ if (isa<CallInst>(I)) {
+ // Output an operand for the callee and each fixed argument, then two for
+ // each variable argument.
+ NumFixedOperands = 1+NumParams;
+ } else {
+ assert(isa<InvokeInst>(I) && "Not call or invoke??");
+ // Output an operand for the callee and destinations, then two for each
+ // variable argument.
+ NumFixedOperands = 3+NumParams;
+ }
+ output_vbr(2 * I->getNumOperands()-NumFixedOperands);
+
+ // The type for the function has already been emitted in the type field of the
+ // instruction. Just emit the slot # now.
+ for (unsigned i = 0; i != NumFixedOperands; ++i) {
+ int Slot = Table.getSlot(I->getOperand(i));
+ assert(Slot >= 0 && "No slot number for value!?!?");
+ output_vbr((unsigned)Slot);
+ }
+
+ for (unsigned i = NumFixedOperands, e = I->getNumOperands(); i != e; ++i) {
+ // Output Arg Type ID
+ int Slot = Table.getSlot(I->getOperand(i)->getType());
+ assert(Slot >= 0 && "No slot number for value!?!?");
+ output_typeid((unsigned)Slot);
+
+ // Output arg ID itself
+ Slot = Table.getSlot(I->getOperand(i));
+ assert(Slot >= 0 && "No slot number for value!?!?");
+ output_vbr((unsigned)Slot);
+ }
+}
+
+
+// outputInstructionFormat1 - Output one operand instructions, knowing that no
+// operand index is >= 2^12.
+//
+inline void BytecodeWriter::outputInstructionFormat1(const Instruction *I,
+ unsigned Opcode,
+ unsigned *Slots,
+ unsigned Type) {
+ // bits Instruction format:
+ // --------------------------
+ // 01-00: Opcode type, fixed to 1.
+ // 07-02: Opcode
+ // 19-08: Resulting type plane
+ // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
+ //
+ unsigned Bits = 1 | (Opcode << 2) | (Type << 8) | (Slots[0] << 20);
+ // cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl;
+ output(Bits);
+}
+
+
+// outputInstructionFormat2 - Output two operand instructions, knowing that no
+// operand index is >= 2^8.
+//
+inline void BytecodeWriter::outputInstructionFormat2(const Instruction *I,
+ unsigned Opcode,
+ unsigned *Slots,
+ unsigned Type) {
+ // bits Instruction format:
+ // --------------------------
+ // 01-00: Opcode type, fixed to 2.
+ // 07-02: Opcode
+ // 15-08: Resulting type plane
+ // 23-16: Operand #1
+ // 31-24: Operand #2
+ //
+ unsigned Bits = 2 | (Opcode << 2) | (Type << 8) |
+ (Slots[0] << 16) | (Slots[1] << 24);
+ // cerr << "2 " << IType << " " << Type << " " << Slots[0] << " "
+ // << Slots[1] << endl;
+ output(Bits);
+}
+
+
+// outputInstructionFormat3 - Output three operand instructions, knowing that no
+// operand index is >= 2^6.
+//
+inline void BytecodeWriter::outputInstructionFormat3(const Instruction *I,
+ unsigned Opcode,
+ unsigned *Slots,
+ unsigned Type) {
+ // bits Instruction format:
+ // --------------------------
+ // 01-00: Opcode type, fixed to 3.
+ // 07-02: Opcode
+ // 13-08: Resulting type plane
+ // 19-14: Operand #1
+ // 25-20: Operand #2
+ // 31-26: Operand #3
+ //
+ unsigned Bits = 3 | (Opcode << 2) | (Type << 8) |
+ (Slots[0] << 14) | (Slots[1] << 20) | (Slots[2] << 26);
+ //cerr << "3 " << IType << " " << Type << " " << Slots[0] << " "
+ // << Slots[1] << " " << Slots[2] << endl;
+ output(Bits);
+}
+
+void BytecodeWriter::outputInstruction(const Instruction &I) {
+ assert(I.getOpcode() < 62 && "Opcode too big???");
+ unsigned Opcode = I.getOpcode();
+ unsigned NumOperands = I.getNumOperands();
+
+ // Encode 'volatile load' as 62 and 'volatile store' as 63.
+ if (isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile())
+ Opcode = 62;
+ if (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())
+ Opcode = 63;
+
+ // Figure out which type to encode with the instruction. Typically we want
+ // the type of the first parameter, as opposed to the type of the instruction
+ // (for example, with setcc, we always know it returns bool, but the type of
+ // the first param is actually interesting). But if we have no arguments
+ // we take the type of the instruction itself.
+ //
+ const Type *Ty;
+ switch (I.getOpcode()) {
+ case Instruction::Select:
+ case Instruction::Malloc:
+ case Instruction::Alloca:
+ Ty = I.getType(); // These ALWAYS want to encode the return type
+ break;
+ case Instruction::Store:
+ Ty = I.getOperand(1)->getType(); // Encode the pointer type...
+ assert(isa<PointerType>(Ty) && "Store to nonpointer type!?!?");
+ break;
+ default: // Otherwise use the default behavior...
+ Ty = NumOperands ? I.getOperand(0)->getType() : I.getType();
+ break;
+ }
+
+ unsigned Type;
+ int Slot = Table.getSlot(Ty);
+ assert(Slot != -1 && "Type not available!!?!");
+ Type = (unsigned)Slot;
+
+ // Varargs calls and invokes are encoded entirely different from any other
+ // instructions.
+ if (const CallInst *CI = dyn_cast<CallInst>(&I)){
+ const PointerType *Ty =cast<PointerType>(CI->getCalledValue()->getType());
+ if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
+ outputInstrVarArgsCall(CI, Opcode, Table, Type);
+ return;
+ }
+ } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
+ const PointerType *Ty =cast<PointerType>(II->getCalledValue()->getType());
+ if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
+ outputInstrVarArgsCall(II, Opcode, Table, Type);
+ return;
+ }
+ }
+
+ if (NumOperands <= 3) {
+ // Make sure that we take the type number into consideration. We don't want
+ // to overflow the field size for the instruction format we select.
+ //
+ unsigned MaxOpSlot = Type;
+ unsigned Slots[3]; Slots[0] = (1 << 12)-1; // Marker to signify 0 operands
+
+ for (unsigned i = 0; i != NumOperands; ++i) {
+ int slot = Table.getSlot(I.getOperand(i));
+ assert(slot != -1 && "Broken bytecode!");
+ if (unsigned(slot) > MaxOpSlot) MaxOpSlot = unsigned(slot);
+ Slots[i] = unsigned(slot);
+ }
+
+ // Handle the special cases for various instructions...
+ if (isa<CastInst>(I) || isa<VAArgInst>(I)) {
+ // Cast has to encode the destination type as the second argument in the
+ // packet, or else we won't know what type to cast to!
+ Slots[1] = Table.getSlot(I.getType());
+ assert(Slots[1] != ~0U && "Cast return type unknown?");
+ if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
+ NumOperands++;
+ } else if (const VANextInst *VANI = dyn_cast<VANextInst>(&I)) {
+ Slots[1] = Table.getSlot(VANI->getArgType());
+ assert(Slots[1] != ~0U && "va_next return type unknown?");
+ if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
+ NumOperands++;
+ } else if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I)) {
+ // We need to encode the type of sequential type indices into their slot #
+ unsigned Idx = 1;
+ for (gep_type_iterator I = gep_type_begin(GEP), E = gep_type_end(GEP);
+ I != E; ++I, ++Idx)
+ if (isa<SequentialType>(*I)) {
+ unsigned IdxId;
+ switch (GEP->getOperand(Idx)->getType()->getTypeID()) {
+ default: assert(0 && "Unknown index type!");
+ case Type::UIntTyID: IdxId = 0; break;
+ case Type::IntTyID: IdxId = 1; break;
+ case Type::ULongTyID: IdxId = 2; break;
+ case Type::LongTyID: IdxId = 3; break;
+ }
+ Slots[Idx] = (Slots[Idx] << 2) | IdxId;
+ if (Slots[Idx] > MaxOpSlot) MaxOpSlot = Slots[Idx];
+ }
+ }
+
+ // Decide which instruction encoding to use. This is determined primarily
+ // by the number of operands, and secondarily by whether or not the max
+ // operand will fit into the instruction encoding. More operands == fewer
+ // bits per operand.
+ //
+ switch (NumOperands) {
+ case 0:
+ case 1:
+ if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops
+ outputInstructionFormat1(&I, Opcode, Slots, Type);
+ return;
+ }
+ break;
+
+ case 2:
+ if (MaxOpSlot < (1 << 8)) {
+ outputInstructionFormat2(&I, Opcode, Slots, Type);
+ return;
+ }
+ break;
+
+ case 3:
+ if (MaxOpSlot < (1 << 6)) {
+ outputInstructionFormat3(&I, Opcode, Slots, Type);
+ return;
+ }
+ break;
+ default:
+ break;
+ }
+ }
+
+ // If we weren't handled before here, we either have a large number of
+ // operands or a large operand index that we are referring to.
+ outputInstructionFormat0(&I, Opcode, Table, Type);
+}
+
+//===----------------------------------------------------------------------===//
+//=== Block Output ===//
+//===----------------------------------------------------------------------===//
+
+BytecodeWriter::BytecodeWriter(std::vector<unsigned char> &o, const Module *M)
+ : Out(o), Table(M) {
+
+ // Emit the signature...
+ static const unsigned char *Sig = (const unsigned char*)"llvm";
+ output_data(Sig, Sig+4);