//===----------------------------------------------------------------------===//
#include "Reader.h"
-#include "llvm/Assembly/AutoUpgrade.h"
#include "llvm/Bytecode/BytecodeHandler.h"
#include "llvm/BasicBlock.h"
#include "llvm/CallingConv.h"
#include "llvm/Constants.h"
#include "llvm/InlineAsm.h"
#include "llvm/Instructions.h"
-#include "llvm/SymbolTable.h"
+#include "llvm/TypeSymbolTable.h"
#include "llvm/Bytecode/Format.h"
#include "llvm/Config/alloca.h"
#include "llvm/Support/GetElementPtrTypeIterator.h"
-#include "llvm/Support/Compressor.h"
#include "llvm/Support/MathExtras.h"
+#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include <sstream>
#include <algorithm>
Use Op;
ConstantPlaceHolder(const Type *Ty)
: ConstantExpr(Ty, Instruction::UserOp1, &Op, 1),
- Op(UndefValue::get(Type::IntTy), this) {
+ Op(UndefValue::get(Type::Int32Ty), this) {
}
};
}
inline void BytecodeReader::error(const std::string& err) {
ErrorMsg = err + " (Vers=" + itostr(RevisionNum) + ", Pos="
+ itostr(At-MemStart) + ")";
+ if (Handler) Handler->handleError(ErrorMsg);
longjmp(context,1);
}
inline unsigned BytecodeReader::read_vbr_uint() {
unsigned Shift = 0;
unsigned Result = 0;
- BufPtr Save = At;
do {
if (At == BlockEnd)
Result |= (unsigned)((*At++) & 0x7F) << Shift;
Shift += 7;
} while (At[-1] & 0x80);
- if (Handler) Handler->handleVBR32(At-Save);
return Result;
}
inline uint64_t BytecodeReader::read_vbr_uint64() {
unsigned Shift = 0;
uint64_t Result = 0;
- BufPtr Save = At;
do {
if (At == BlockEnd)
Result |= (uint64_t)((*At++) & 0x7F) << Shift;
Shift += 7;
} while (At[-1] & 0x80);
- if (Handler) Handler->handleVBR64(At-Save);
return Result;
}
return std::string((char*)OldAt, Size);
}
+void BytecodeReader::read_str(SmallVectorImpl<char> &StrData) {
+ StrData.clear();
+ unsigned Size = read_vbr_uint();
+ const unsigned char *OldAt = At;
+ At += Size;
+ if (At > BlockEnd) // Size invalid?
+ error("Ran out of data reading a string!");
+ StrData.append(OldAt, At);
+}
+
+
/// Read an arbitrary block of data
inline void BytecodeReader::read_data(void *Ptr, void *End) {
unsigned char *Start = (unsigned char *)Ptr;
return TyID != Type::LabelTyID && TyID != Type::VoidTyID;
}
-/// Obtain a type given a typeid and account for things like compaction tables,
-/// function level vs module level, and the offsetting for the primitive types.
+/// Obtain a type given a typeid and account for things like function level vs
+/// module level, and the offsetting for the primitive types.
const Type *BytecodeReader::getType(unsigned ID) {
- if (ID < Type::FirstDerivedTyID)
+ if (ID <= Type::LastPrimitiveTyID)
if (const Type *T = Type::getPrimitiveType((Type::TypeID)ID))
return T; // Asked for a primitive type...
// Otherwise, derived types need offset...
ID -= Type::FirstDerivedTyID;
- if (!CompactionTypes.empty()) {
- if (ID >= CompactionTypes.size())
- error("Type ID out of range for compaction table!");
- return CompactionTypes[ID].first;
- }
-
// Is it a module-level type?
if (ID < ModuleTypes.size())
return ModuleTypes[ID].get();
return Type::VoidTy;
}
-/// This method just saves some coding. It uses read_vbr_uint to read
-/// in a sanitized type id, errors that its not the type type, and
-/// then calls getType to return the type value.
+/// This method just saves some coding. It uses read_vbr_uint to read in a
+/// type id, errors that its not the type type, and then calls getType to
+/// return the type value.
inline const Type* BytecodeReader::readType() {
return getType(read_vbr_uint());
}
/// Get the slot number associated with a type accounting for primitive
-/// types, compaction tables, and function level vs module level.
+/// types and function level vs module level.
unsigned BytecodeReader::getTypeSlot(const Type *Ty) {
if (Ty->isPrimitiveType())
return Ty->getTypeID();
- // Scan the compaction table for the type if needed.
- if (!CompactionTypes.empty()) {
- for (unsigned i = 0, e = CompactionTypes.size(); i != e; ++i)
- if (CompactionTypes[i].first == Ty)
- return Type::FirstDerivedTyID + i;
-
- error("Couldn't find type specified in compaction table!");
- }
-
// Check the function level types first...
TypeListTy::iterator I = std::find(FunctionTypes.begin(),
FunctionTypes.end(), Ty);
return Type::FirstDerivedTyID + IT->second;
}
-/// This is just like getType, but when a compaction table is in use, it is
-/// ignored. It also ignores function level types.
-/// @see getType
-const Type *BytecodeReader::getGlobalTableType(unsigned Slot) {
- if (Slot < Type::FirstDerivedTyID) {
- const Type *Ty = Type::getPrimitiveType((Type::TypeID)Slot);
- if (!Ty)
- error("Not a primitive type ID?");
- return Ty;
- }
- Slot -= Type::FirstDerivedTyID;
- if (Slot >= ModuleTypes.size())
- error("Illegal compaction table type reference!");
- return ModuleTypes[Slot];
-}
-
-/// This is just like getTypeSlot, but when a compaction table is in use, it
-/// is ignored. It also ignores function level types.
-unsigned BytecodeReader::getGlobalTableTypeSlot(const Type *Ty) {
- if (Ty->isPrimitiveType())
- return Ty->getTypeID();
-
- // If we don't have our cache yet, build it now.
- if (ModuleTypeIDCache.empty()) {
- unsigned N = 0;
- ModuleTypeIDCache.reserve(ModuleTypes.size());
- for (TypeListTy::iterator I = ModuleTypes.begin(), E = ModuleTypes.end();
- I != E; ++I, ++N)
- ModuleTypeIDCache.push_back(std::make_pair(*I, N));
-
- std::sort(ModuleTypeIDCache.begin(), ModuleTypeIDCache.end());
- }
-
- // Binary search the cache for the entry.
- std::vector<std::pair<const Type*, unsigned> >::iterator IT =
- std::lower_bound(ModuleTypeIDCache.begin(), ModuleTypeIDCache.end(),
- std::make_pair(Ty, 0U));
- if (IT == ModuleTypeIDCache.end() || IT->first != Ty)
- error("Didn't find type in ModuleTypes.");
-
- return Type::FirstDerivedTyID + IT->second;
-}
-
/// Retrieve a value of a given type and slot number, possibly creating
/// it if it doesn't already exist.
Value * BytecodeReader::getValue(unsigned type, unsigned oNum, bool Create) {
assert(type != Type::LabelTyID && "getValue() cannot get blocks!");
unsigned Num = oNum;
- // If there is a compaction table active, it defines the low-level numbers.
- // If not, the module values define the low-level numbers.
- if (CompactionValues.size() > type && !CompactionValues[type].empty()) {
- if (Num < CompactionValues[type].size())
- return CompactionValues[type][Num];
- Num -= CompactionValues[type].size();
- } else {
- // By default, the global type id is the type id passed in
- unsigned GlobalTyID = type;
-
- // If the type plane was compactified, figure out the global type ID by
- // adding the derived type ids and the distance.
- if (!CompactionTypes.empty() && type >= Type::FirstDerivedTyID)
- GlobalTyID = CompactionTypes[type-Type::FirstDerivedTyID].second;
-
- if (hasImplicitNull(GlobalTyID)) {
- const Type *Ty = getType(type);
- if (!isa<OpaqueType>(Ty)) {
- if (Num == 0)
- return Constant::getNullValue(Ty);
- --Num;
- }
- }
+ // By default, the global type id is the type id passed in
+ unsigned GlobalTyID = type;
- if (GlobalTyID < ModuleValues.size() && ModuleValues[GlobalTyID]) {
- if (Num < ModuleValues[GlobalTyID]->size())
- return ModuleValues[GlobalTyID]->getOperand(Num);
- Num -= ModuleValues[GlobalTyID]->size();
+ if (hasImplicitNull(GlobalTyID)) {
+ const Type *Ty = getType(type);
+ if (!isa<OpaqueType>(Ty)) {
+ if (Num == 0)
+ return Constant::getNullValue(Ty);
+ --Num;
}
}
+ if (GlobalTyID < ModuleValues.size() && ModuleValues[GlobalTyID]) {
+ if (Num < ModuleValues[GlobalTyID]->size())
+ return ModuleValues[GlobalTyID]->getOperand(Num);
+ Num -= ModuleValues[GlobalTyID]->size();
+ }
+
if (FunctionValues.size() > type &&
FunctionValues[type] &&
Num < FunctionValues[type]->size())
return 0; // just silence warning, error calls longjmp
}
-/// This is just like getValue, but when a compaction table is in use, it
-/// is ignored. Also, no forward references or other fancy features are
-/// supported.
-Value* BytecodeReader::getGlobalTableValue(unsigned TyID, unsigned SlotNo) {
- if (SlotNo == 0)
- return Constant::getNullValue(getType(TyID));
-
- if (!CompactionTypes.empty() && TyID >= Type::FirstDerivedTyID) {
- TyID -= Type::FirstDerivedTyID;
- if (TyID >= CompactionTypes.size())
- error("Type ID out of range for compaction table!");
- TyID = CompactionTypes[TyID].second;
- }
-
- --SlotNo;
-
- if (TyID >= ModuleValues.size() || ModuleValues[TyID] == 0 ||
- SlotNo >= ModuleValues[TyID]->size()) {
- if (TyID >= ModuleValues.size() || ModuleValues[TyID] == 0)
- error("Corrupt compaction table entry!"
- + utostr(TyID) + ", " + utostr(SlotNo) + ": "
- + utostr(ModuleValues.size()));
- else
- error("Corrupt compaction table entry!"
- + utostr(TyID) + ", " + utostr(SlotNo) + ": "
- + utostr(ModuleValues.size()) + ", "
- + utohexstr(reinterpret_cast<uint64_t>(((void*)ModuleValues[TyID])))
- + ", "
- + utostr(ModuleValues[TyID]->size()));
- }
- return ModuleValues[TyID]->getOperand(SlotNo);
-}
/// Just like getValue, except that it returns a null pointer
/// only on error. It always returns a constant (meaning that if the value is
insertValue(AI, getTypeSlot(AI->getType()), FunctionValues);
}
-/// Convert previous opcode values into the current value and/or construct
-/// the instruction. This function handles all *abnormal* cases for instruction
-/// generation based on obsolete opcode values. The normal cases are handled
-/// in ParseInstruction below. Generally this function just produces a new
-/// Opcode value (first argument). In a few cases (VAArg, VANext) the upgrade
-/// path requies that the instruction (sequence) be generated differently from
-/// the normal case in order to preserve the original semantics. In these
-/// cases the result of the function will be a non-zero Instruction pointer. In
-/// all other cases, zero will be returned indicating that the *normal*
-/// instruction generation should be used, but with the new Opcode value.
-Instruction*
-BytecodeReader::upgradeInstrOpcodes(
- unsigned &Opcode, ///< The old opcode, possibly updated by this function
- std::vector<unsigned> &Oprnds, ///< The operands to the instruction
- unsigned &iType, ///< The type code from the bytecode file
- const Type *InstTy, ///< The type of the instruction
- BasicBlock *BB ///< The basic block to insert into, if we need to
-) {
-
- // First, short circuit this if no conversion is required. When signless
- // instructions were implemented the entire opcode sequence was revised in
- // two stages: first Div/Rem became signed, then Shr/Cast/Setcc became
- // signed. If all of these instructions are signed then we don't have to
- // upgrade the opcode.
- if (!hasSignlessDivRem && !hasSignlessShrCastSetcc)
- return 0; // The opcode is fine the way it is.
-
- // If this is bytecode version 6, that only had signed Rem and Div
- // instructions, then we must compensate for those two instructions only.
- // So that the switch statement below works, we're trying to turn this into
- // a version 5 opcode. To do that we must adjust the opcode to 10 (Div) if its
- // any of the UDiv, SDiv or FDiv instructions; or, adjust the opcode to
- // 11 (Rem) if its any of the URem, SRem, or FRem instructions; or, simply
- // decrement the instruction code if its beyond FRem.
- if (!hasSignlessDivRem) {
- // If its one of the signed Div/Rem opcodes, its fine the way it is
- if (Opcode >= 10 && Opcode <= 12) // UDiv through FDiv
- Opcode = 10; // Div
- else if (Opcode >=13 && Opcode <= 15) // URem through FRem
- Opcode = 11; // Rem
- else if (Opcode >= 16 && Opcode <= 35) // And through Shr
- // Adjust for new instruction codes
- Opcode -= 4;
- else if (Opcode >= 36 && Opcode <= 42) // Everything after Select
- // In vers 6 bytecode we eliminated the placeholders for the obsolete
- // VAARG and VANEXT instructions. Consequently those two slots were
- // filled starting with Select (36) which was 34. So now we only need
- // to subtract two. This circumvents hitting opcodes 32 and 33
- Opcode -= 2;
- else { // Opcode < 10 or > 42
- // No upgrade necessary.
- return 0;
- }
- }
-
- // Declare the resulting instruction we might build. In general we just
- // change the Opcode argument but in a few cases we need to generate the
- // Instruction here because the upgrade case is significantly different from
- // the normal case.
- Instruction *Result = 0;
-
- // We're dealing with an upgrade situation. For each of the opcode values,
- // perform the necessary conversion.
- switch (Opcode) {
- default: // Error
- // This switch statement provides cases for all known opcodes prior to
- // version 6 bytecode format. We know we're in an upgrade situation so
- // if there isn't a match in this switch, then something is horribly
- // wrong.
- error("Unknown obsolete opcode encountered.");
- break;
- case 1: // Ret
- Opcode = Instruction::Ret;
- break;
- case 2: // Br
- Opcode = Instruction::Br;
- break;
- case 3: // Switch
- Opcode = Instruction::Switch;
- break;
- case 4: // Invoke
- Opcode = Instruction::Invoke;
- break;
- case 5: // Unwind
- Opcode = Instruction::Unwind;
- break;
- case 6: // Unreachable
- Opcode = Instruction::Unreachable;
- break;
- case 7: // Add
- Opcode = Instruction::Add;
- break;
- case 8: // Sub
- Opcode = Instruction::Sub;
- break;
- case 9: // Mul
- Opcode = Instruction::Mul;
- break;
- case 10: // Div
- // The type of the instruction is based on the operands. We need to select
- // fdiv, udiv or sdiv based on that type. The iType values are hardcoded
- // to the values used in bytecode version 5 (and prior) because it is
- // likely these codes will change in future versions of LLVM.
- if (iType == 10 || iType == 11 )
- Opcode = Instruction::FDiv;
- else if (iType >= 2 && iType <= 9 && iType % 2 != 0)
- Opcode = Instruction::SDiv;
- else
- Opcode = Instruction::UDiv;
- break;
-
- case 11: // Rem
- // As with "Div", make the signed/unsigned or floating point Rem
- // instruction choice based on the type of the operands.
- if (iType == 10 || iType == 11)
- Opcode = Instruction::FRem;
- else if (iType >= 2 && iType <= 9 && iType % 2 != 0)
- Opcode = Instruction::SRem;
- else
- Opcode = Instruction::URem;
- break;
- case 12: // And
- Opcode = Instruction::And;
- break;
- case 13: // Or
- Opcode = Instruction::Or;
- break;
- case 14: // Xor
- Opcode = Instruction::Xor;
- break;
- case 15: // SetEQ
- Opcode = Instruction::SetEQ;
- break;
- case 16: // SetNE
- Opcode = Instruction::SetNE;
- break;
- case 17: // SetLE
- Opcode = Instruction::SetLE;
- break;
- case 18: // SetGE
- Opcode = Instruction::SetGE;
- break;
- case 19: // SetLT
- Opcode = Instruction::SetLT;
- break;
- case 20: // SetGT
- Opcode = Instruction::SetGT;
- break;
- case 21: // Malloc
- Opcode = Instruction::Malloc;
- break;
- case 22: // Free
- Opcode = Instruction::Free;
- break;
- case 23: // Alloca
- Opcode = Instruction::Alloca;
- break;
- case 24: // Load
- Opcode = Instruction::Load;
- break;
- case 25: // Store
- Opcode = Instruction::Store;
- break;
- case 26: // GetElementPtr
- Opcode = Instruction::GetElementPtr;
- break;
- case 27: // PHI
- Opcode = Instruction::PHI;
- break;
- case 28: // Cast
- {
- Value *Source = getValue(iType, Oprnds[0]);
- const Type *DestTy = getType(Oprnds[1]);
- // The previous definition of cast to bool was a compare against zero.
- // We have to retain that semantic so we do it here.
- if (DestTy == Type::BoolTy) { // if its a cast to bool
- Opcode = Instruction::SetNE;
- Result = new SetCondInst(Instruction::SetNE, Source,
- Constant::getNullValue(Source->getType()));
- } else if (Source->getType()->isFloatingPoint() &&
- isa<PointerType>(DestTy)) {
- // Upgrade what is now an illegal cast (fp -> ptr) into two casts,
- // fp -> ui, and ui -> ptr
- CastInst *CI = new FPToUIInst(Source, Type::ULongTy);
- BB->getInstList().push_back(CI);
- Result = new IntToPtrInst(CI, DestTy);
- } else {
- Result = CastInst::createInferredCast(Source, DestTy);
- }
- break;
- }
- case 29: // Call
- Opcode = Instruction::Call;
- break;
- case 30: // Shl
- Opcode = Instruction::Shl;
- break;
- case 31: // Shr
- // The type of the instruction is based on the operands. We need to
- // select ashr or lshr based on that type. The iType values are hardcoded
- // to the values used in bytecode version 5 (and prior) because it is
- // likely these codes will change in future versions of LLVM. This if
- // statement says "if (integer type and signed)"
- if (iType >= 2 && iType <= 9 && iType % 2 != 0)
- Opcode = Instruction::AShr;
- else
- Opcode = Instruction::LShr;
- break;
- case 32: { //VANext_old ( <= llvm 1.5 )
- const Type* ArgTy = getValue(iType, Oprnds[0])->getType();
- Function* NF = TheModule->getOrInsertFunction(
- "llvm.va_copy", ArgTy, ArgTy, (Type *)0);
-
- // In llvm 1.6 the VANext instruction was dropped because it was only
- // necessary to have a VAArg instruction. The code below transforms an
- // old vanext instruction into the equivalent code given only the
- // availability of the new vaarg instruction. Essentially, the transform
- // is as follows:
- // b = vanext a, t ->
- // foo = alloca 1 of t
- // bar = vacopy a
- // store bar -> foo
- // tmp = vaarg foo, t
- // b = load foo
- AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
- BB->getInstList().push_back(foo);
- CallInst* bar = new CallInst(NF, getValue(iType, Oprnds[0]));
- BB->getInstList().push_back(bar);
- BB->getInstList().push_back(new StoreInst(bar, foo));
- Instruction* tmp = new VAArgInst(foo, getType(Oprnds[1]));
- BB->getInstList().push_back(tmp);
- Result = new LoadInst(foo);
- break;
- }
- case 33: { //VAArg_old
- const Type* ArgTy = getValue(iType, Oprnds[0])->getType();
- Function* NF = TheModule->getOrInsertFunction(
- "llvm.va_copy", ArgTy, ArgTy, (Type *)0);
-
- // In llvm 1.6 the VAArg's instruction semantics were changed. The code
- // below transforms an old vaarg instruction into the equivalent code
- // given only the availability of the new vaarg instruction. Essentially,
- // the transform is as follows:
- // b = vaarg a, t ->
- // foo = alloca 1 of t
- // bar = vacopy a
- // store bar -> foo
- // b = vaarg foo, t
- AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
- BB->getInstList().push_back(foo);
- CallInst* bar = new CallInst(NF, getValue(iType, Oprnds[0]));
- BB->getInstList().push_back(bar);
- BB->getInstList().push_back(new StoreInst(bar, foo));
- Result = new VAArgInst(foo, getType(Oprnds[1]));
- break;
- }
- case 34: // Select
- Opcode = Instruction::Select;
- break;
- case 35: // UserOp1
- Opcode = Instruction::UserOp1;
- break;
- case 36: // UserOp2
- Opcode = Instruction::UserOp2;
- break;
- case 37: // VAArg
- Opcode = Instruction::VAArg;
- break;
- case 38: // ExtractElement
- Opcode = Instruction::ExtractElement;
- break;
- case 39: // InsertElement
- Opcode = Instruction::InsertElement;
- break;
- case 40: // ShuffleVector
- Opcode = Instruction::ShuffleVector;
- break;
- case 56: // Invoke with encoded CC
- case 57: { // Invoke Fast CC
- if (Oprnds.size() < 3)
- error("Invalid invoke instruction!");
- Value *F = getValue(iType, Oprnds[0]);
-
- // Check to make sure we have a pointer to function type
- const PointerType *PTy = dyn_cast<PointerType>(F->getType());
- if (PTy == 0)
- error("Invoke to non function pointer value!");
- const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
- if (FTy == 0)
- error("Invoke to non function pointer value!");
-
- std::vector<Value *> Params;
- BasicBlock *Normal, *Except;
- unsigned CallingConv = CallingConv::C;
- if (Opcode == 57)
- CallingConv = CallingConv::Fast;
- else if (Opcode == 56) {
- CallingConv = Oprnds.back();
- Oprnds.pop_back();
- }
- Opcode = Instruction::Invoke;
-
- if (!FTy->isVarArg()) {
- Normal = getBasicBlock(Oprnds[1]);
- Except = getBasicBlock(Oprnds[2]);
-
- FunctionType::param_iterator It = FTy->param_begin();
- for (unsigned i = 3, e = Oprnds.size(); i != e; ++i) {
- if (It == FTy->param_end())
- error("Invalid invoke instruction!");
- Params.push_back(getValue(getTypeSlot(*It++), Oprnds[i]));
- }
- if (It != FTy->param_end())
- error("Invalid invoke instruction!");
- } else {
- Oprnds.erase(Oprnds.begin(), Oprnds.begin()+1);
-
- Normal = getBasicBlock(Oprnds[0]);
- Except = getBasicBlock(Oprnds[1]);
-
- unsigned FirstVariableArgument = FTy->getNumParams()+2;
- for (unsigned i = 2; i != FirstVariableArgument; ++i)
- Params.push_back(getValue(getTypeSlot(FTy->getParamType(i-2)),
- Oprnds[i]));
-
- // Must be type/value pairs. If not, error out.
- if (Oprnds.size()-FirstVariableArgument & 1)
- error("Invalid invoke instruction!");
-
- for (unsigned i = FirstVariableArgument; i < Oprnds.size(); i += 2)
- Params.push_back(getValue(Oprnds[i], Oprnds[i+1]));
- }
-
- Result = new InvokeInst(F, Normal, Except, Params);
- if (CallingConv) cast<InvokeInst>(Result)->setCallingConv(CallingConv);
- break;
- }
- case 58: // Call with extra operand for calling conv
- case 59: // tail call, Fast CC
- case 60: // normal call, Fast CC
- case 61: // tail call, C Calling Conv
- case 62: // volatile load
- case 63: // volatile store
- // In all these cases, we pass the opcode through. The new version uses
- // the same code (for now, this might change in 2.0). These are listed
- // here to document the opcodes in use in vers 5 bytecode and to make it
- // easier to migrate these opcodes in the future.
- break;
- }
- return Result;
-}
-
//===----------------------------------------------------------------------===//
// Bytecode Parsing Methods
//===----------------------------------------------------------------------===//
/// This method parses a single instruction. The instruction is
/// inserted at the end of the \p BB provided. The arguments of
/// the instruction are provided in the \p Oprnds vector.
-void BytecodeReader::ParseInstruction(std::vector<unsigned> &Oprnds,
+void BytecodeReader::ParseInstruction(SmallVector<unsigned, 8> &Oprnds,
BasicBlock* BB) {
BufPtr SaveAt = At;
// Make the necessary adjustments for dealing with backwards compatibility
// of opcodes.
- Instruction* Result =
- upgradeInstrOpcodes(Opcode, Oprnds, iType, InstTy, BB);
-
- // We have enough info to inform the handler now.
- if (Handler)
- Handler->handleInstruction(Opcode, InstTy, Oprnds, At-SaveAt);
-
- // If the backwards compatibility code didn't produce an instruction then
- // we do the *normal* thing ..
- if (!Result) {
- // First, handle the easy binary operators case
- if (Opcode >= Instruction::BinaryOpsBegin &&
- Opcode < Instruction::BinaryOpsEnd && Oprnds.size() == 2)
- Result = BinaryOperator::create(Instruction::BinaryOps(Opcode),
- getValue(iType, Oprnds[0]),
- getValue(iType, Oprnds[1]));
-
+ Instruction* Result = 0;
+
+ // First, handle the easy binary operators case
+ if (Opcode >= Instruction::BinaryOpsBegin &&
+ Opcode < Instruction::BinaryOpsEnd && Oprnds.size() == 2) {
+ Result = BinaryOperator::create(Instruction::BinaryOps(Opcode),
+ getValue(iType, Oprnds[0]),
+ getValue(iType, Oprnds[1]));
+ } else {
// Indicate that we don't think this is a call instruction (yet).
// Process based on the Opcode read
switch (Opcode) {
if (Oprnds.size() != 2)
error("Invalid extractelement instruction!");
Value *V1 = getValue(iType, Oprnds[0]);
- Value *V2 = getValue(Type::UIntTyID, Oprnds[1]);
+ Value *V2 = getValue(Int32TySlot, Oprnds[1]);
if (!ExtractElementInst::isValidOperands(V1, V2))
error("Invalid extractelement instruction!");
break;
}
case Instruction::InsertElement: {
- const PackedType *PackedTy = dyn_cast<PackedType>(InstTy);
- if (!PackedTy || Oprnds.size() != 3)
+ const VectorType *VectorTy = dyn_cast<VectorType>(InstTy);
+ if (!VectorTy || Oprnds.size() != 3)
error("Invalid insertelement instruction!");
Value *V1 = getValue(iType, Oprnds[0]);
- Value *V2 = getValue(getTypeSlot(PackedTy->getElementType()),Oprnds[1]);
- Value *V3 = getValue(Type::UIntTyID, Oprnds[2]);
+ Value *V2 = getValue(getTypeSlot(VectorTy->getElementType()),Oprnds[1]);
+ Value *V3 = getValue(Int32TySlot, Oprnds[2]);
if (!InsertElementInst::isValidOperands(V1, V2, V3))
error("Invalid insertelement instruction!");
break;
}
case Instruction::ShuffleVector: {
- const PackedType *PackedTy = dyn_cast<PackedType>(InstTy);
- if (!PackedTy || Oprnds.size() != 3)
+ const VectorType *VectorTy = dyn_cast<VectorType>(InstTy);
+ if (!VectorTy || Oprnds.size() != 3)
error("Invalid shufflevector instruction!");
Value *V1 = getValue(iType, Oprnds[0]);
Value *V2 = getValue(iType, Oprnds[1]);
- const PackedType *EltTy =
- PackedType::get(Type::UIntTy, PackedTy->getNumElements());
+ const VectorType *EltTy =
+ VectorType::get(Type::Int32Ty, VectorTy->getNumElements());
Value *V3 = getValue(getTypeSlot(EltTy), Oprnds[2]);
if (!ShuffleVectorInst::isValidOperands(V1, V2, V3))
error("Invalid shufflevector instruction!");
case Instruction::Select:
if (Oprnds.size() != 3)
error("Invalid Select instruction!");
- Result = new SelectInst(getValue(Type::BoolTyID, Oprnds[0]),
+ Result = new SelectInst(getValue(BoolTySlot, Oprnds[0]),
getValue(iType, Oprnds[1]),
getValue(iType, Oprnds[2]));
break;
Result = PN;
break;
}
- case Instruction::Shl:
- case Instruction::LShr:
- case Instruction::AShr:
- Result = new ShiftInst(Instruction::OtherOps(Opcode),
- getValue(iType, Oprnds[0]),
- getValue(Type::UByteTyID, Oprnds[1]));
+ case Instruction::ICmp:
+ case Instruction::FCmp:
+ if (Oprnds.size() != 3)
+ error("Cmp instructions requires 3 operands");
+ // These instructions encode the comparison predicate as the 3rd operand.
+ Result = CmpInst::create(Instruction::OtherOps(Opcode),
+ static_cast<unsigned short>(Oprnds[2]),
+ getValue(iType, Oprnds[0]), getValue(iType, Oprnds[1]));
break;
case Instruction::Ret:
if (Oprnds.size() == 0)
Result = new BranchInst(getBasicBlock(Oprnds[0]));
else if (Oprnds.size() == 3)
Result = new BranchInst(getBasicBlock(Oprnds[0]),
- getBasicBlock(Oprnds[1]), getValue(Type::BoolTyID , Oprnds[2]));
+ getBasicBlock(Oprnds[1]), getValue(BoolTySlot, Oprnds[2]));
else
error("Invalid number of operands for a 'br' instruction!");
break;
const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
if (FTy == 0) error("Call to non function pointer value!");
- std::vector<Value *> Params;
+ SmallVector<Value *, 8> Params;
if (!FTy->isVarArg()) {
FunctionType::param_iterator It = FTy->param_begin();
Params.push_back(getValue(Oprnds[i], Oprnds[i+1]));
}
- Result = new CallInst(F, Params);
+ Result = new CallInst(F, &Params[0], Params.size());
if (isTailCall) cast<CallInst>(Result)->setTailCall();
if (CallingConv) cast<CallInst>(Result)->setCallingConv(CallingConv);
break;
if (FTy == 0)
error("Invoke to non function pointer value!");
- std::vector<Value *> Params;
+ SmallVector<Value *, 8> Params;
BasicBlock *Normal, *Except;
unsigned CallingConv = Oprnds.back();
Oprnds.pop_back();
Params.push_back(getValue(Oprnds[i], Oprnds[i+1]));
}
- Result = new InvokeInst(F, Normal, Except, Params);
+ Result = new InvokeInst(F, Normal, Except, &Params[0], Params.size());
if (CallingConv) cast<InvokeInst>(Result)->setCallingConv(CallingConv);
break;
}
error("Invalid malloc instruction!");
Result = new MallocInst(cast<PointerType>(InstTy)->getElementType(),
- getValue(Type::UIntTyID, Oprnds[0]), Align);
+ getValue(Int32TySlot, Oprnds[0]), Align);
break;
}
case Instruction::Alloca: {
error("Invalid alloca instruction!");
Result = new AllocaInst(cast<PointerType>(InstTy)->getElementType(),
- getValue(Type::UIntTyID, Oprnds[0]), Align);
+ getValue(Int32TySlot, Oprnds[0]), Align);
break;
}
case Instruction::Free:
if (Oprnds.size() == 0 || !isa<PointerType>(InstTy))
error("Invalid getelementptr instruction!");
- std::vector<Value*> Idx;
+ SmallVector<Value*, 8> Idx;
const Type *NextTy = InstTy;
for (unsigned i = 1, e = Oprnds.size(); i != e; ++i) {
unsigned IdxTy = 0;
// Struct indices are always uints, sequential type indices can be
// any of the 32 or 64-bit integer types. The actual choice of
- // type is encoded in the low two bits of the slot number.
+ // type is encoded in the low bit of the slot number.
if (isa<StructType>(TopTy))
- IdxTy = Type::UIntTyID;
+ IdxTy = Int32TySlot;
else {
- switch (ValIdx & 3) {
+ switch (ValIdx & 1) {
default:
- case 0: IdxTy = Type::UIntTyID; break;
- case 1: IdxTy = Type::IntTyID; break;
- case 2: IdxTy = Type::ULongTyID; break;
- case 3: IdxTy = Type::LongTyID; break;
+ case 0: IdxTy = Int32TySlot; break;
+ case 1: IdxTy = Int64TySlot; break;
}
- ValIdx >>= 2;
+ ValIdx >>= 1;
}
Idx.push_back(getValue(IdxTy, ValIdx));
- NextTy = GetElementPtrInst::getIndexedType(InstTy, Idx, true);
+ NextTy = GetElementPtrInst::getIndexedType(InstTy, &Idx[0], Idx.size(),
+ true);
}
- Result = new GetElementPtrInst(getValue(iType, Oprnds[0]), Idx);
+ Result = new GetElementPtrInst(getValue(iType, Oprnds[0]),
+ &Idx[0], Idx.size());
break;
}
case 62: // volatile load
Result = new UnreachableInst();
break;
} // end switch(Opcode)
- } // end if *normal*
+ } // end if !Result
BB->getInstList().push_back(Result);
else
TypeSlot = getTypeSlot(Result->getType());
+ // We have enough info to inform the handler now.
+ if (Handler)
+ Handler->handleInstruction(Opcode, InstTy, &Oprnds[0], Oprnds.size(),
+ Result, At-SaveAt);
+
insertValue(Result, TypeSlot, FunctionValues);
}
/// @returns the number of basic blocks encountered.
unsigned BytecodeReader::ParseInstructionList(Function* F) {
unsigned BlockNo = 0;
- std::vector<unsigned> Args;
+ SmallVector<unsigned, 8> Args;
while (moreInBlock()) {
if (Handler) Handler->handleBasicBlockBegin(BlockNo);
return BlockNo;
}
-/// Parse a symbol table. This works for both module level and function
+/// Parse a type symbol table.
+void BytecodeReader::ParseTypeSymbolTable(TypeSymbolTable *TST) {
+ // Type Symtab block header: [num entries]
+ unsigned NumEntries = read_vbr_uint();
+ for (unsigned i = 0; i < NumEntries; ++i) {
+ // Symtab entry: [type slot #][name]
+ unsigned slot = read_vbr_uint();
+ std::string Name = read_str();
+ const Type* T = getType(slot);
+ TST->insert(Name, T);
+ }
+}
+
+/// Parse a value symbol table. This works for both module level and function
/// level symbol tables. For function level symbol tables, the CurrentFunction
/// parameter must be non-zero and the ST parameter must correspond to
/// CurrentFunction's symbol table. For Module level symbol tables, the
/// CurrentFunction argument must be zero.
-void BytecodeReader::ParseSymbolTable(Function *CurrentFunction,
- SymbolTable *ST) {
- if (Handler) Handler->handleSymbolTableBegin(CurrentFunction,ST);
+void BytecodeReader::ParseValueSymbolTable(Function *CurrentFunction,
+ ValueSymbolTable *VST) {
+
+ if (Handler) Handler->handleValueSymbolTableBegin(CurrentFunction,VST);
// Allow efficient basic block lookup by number.
- std::vector<BasicBlock*> BBMap;
+ SmallVector<BasicBlock*, 32> BBMap;
if (CurrentFunction)
for (Function::iterator I = CurrentFunction->begin(),
E = CurrentFunction->end(); I != E; ++I)
BBMap.push_back(I);
- // Symtab block header: [num entries]
- unsigned NumEntries = read_vbr_uint();
- for (unsigned i = 0; i < NumEntries; ++i) {
- // Symtab entry: [def slot #][name]
- unsigned slot = read_vbr_uint();
- std::string Name = read_str();
- const Type* T = getType(slot);
- ST->insert(Name, T);
- }
-
+ SmallVector<char, 32> NameStr;
+
while (moreInBlock()) {
// Symtab block header: [num entries][type id number]
unsigned NumEntries = read_vbr_uint();
for (unsigned i = 0; i != NumEntries; ++i) {
// Symtab entry: [def slot #][name]
unsigned slot = read_vbr_uint();
- std::string Name = read_str();
+ read_str(NameStr);
Value *V = 0;
- if (Typ == Type::LabelTyID) {
- if (slot < BBMap.size())
- V = BBMap[slot];
+ if (Typ == LabelTySlot) {
+ V = (slot < BBMap.size()) ? BBMap[slot] : 0;
} else {
- V = getValue(Typ, slot, false); // Find mapping...
+ V = getValue(Typ, slot, false); // Find mapping.
}
+ if (Handler) Handler->handleSymbolTableValue(Typ, slot,
+ &NameStr[0], NameStr.size());
if (V == 0)
- error("Failed value look-up for name '" + Name + "'");
- V->setName(Name);
+ error("Failed value look-up for name '" +
+ std::string(NameStr.begin(), NameStr.end()) + "', type #" +
+ utostr(Typ) + " slot #" + utostr(slot));
+ V->setName(&NameStr[0], NameStr.size());
+
+ NameStr.clear();
}
}
checkPastBlockEnd("Symbol Table");
- if (Handler) Handler->handleSymbolTableEnd();
-}
-
-/// Read in the types portion of a compaction table.
-void BytecodeReader::ParseCompactionTypes(unsigned NumEntries) {
- for (unsigned i = 0; i != NumEntries; ++i) {
- unsigned TypeSlot = read_vbr_uint();
- const Type *Typ = getGlobalTableType(TypeSlot);
- CompactionTypes.push_back(std::make_pair(Typ, TypeSlot));
- if (Handler) Handler->handleCompactionTableType(i, TypeSlot, Typ);
- }
-}
-
-/// Parse a compaction table.
-void BytecodeReader::ParseCompactionTable() {
-
- // Notify handler that we're beginning a compaction table.
- if (Handler) Handler->handleCompactionTableBegin();
-
- // Get the types for the compaction table.
- unsigned NumEntries = read_vbr_uint();
- ParseCompactionTypes(NumEntries);
-
- // Compaction tables live in separate blocks so we have to loop
- // until we've read the whole thing.
- while (moreInBlock()) {
- // Read the number of Value* entries in the compaction table
- unsigned NumEntries = read_vbr_uint();
- unsigned Ty = 0;
-
- // Decode the type from value read in. Most compaction table
- // planes will have one or two entries in them. If that's the
- // case then the length is encoded in the bottom two bits and
- // the higher bits encode the type. This saves another VBR value.
- if ((NumEntries & 3) == 3) {
- // In this case, both low-order bits are set (value 3). This
- // is a signal that the typeid follows.
- NumEntries >>= 2;
- Ty = read_vbr_uint();
- } else {
- // In this case, the low-order bits specify the number of entries
- // and the high order bits specify the type.
- Ty = NumEntries >> 2;
- NumEntries &= 3;
- }
-
- // Make sure we have enough room for the plane.
- if (Ty >= CompactionValues.size())
- CompactionValues.resize(Ty+1);
-
- // Make sure the plane is empty or we have some kind of error.
- if (!CompactionValues[Ty].empty())
- error("Compaction table plane contains multiple entries!");
-
- // Notify handler about the plane.
- if (Handler) Handler->handleCompactionTablePlane(Ty, NumEntries);
-
- // Push the implicit zero.
- CompactionValues[Ty].push_back(Constant::getNullValue(getType(Ty)));
-
- // Read in each of the entries, put them in the compaction table
- // and notify the handler that we have a new compaction table value.
- for (unsigned i = 0; i != NumEntries; ++i) {
- unsigned ValSlot = read_vbr_uint();
- Value *V = getGlobalTableValue(Ty, ValSlot);
- CompactionValues[Ty].push_back(V);
- if (Handler) Handler->handleCompactionTableValue(i, Ty, ValSlot);
- }
- }
- // Notify handler that the compaction table is done.
- if (Handler) Handler->handleCompactionTableEnd();
+ if (Handler) Handler->handleValueSymbolTableEnd();
}
// Parse a single type. The typeid is read in first. If its a primitive type
return Result;
switch (PrimType) {
+ case Type::IntegerTyID: {
+ unsigned NumBits = read_vbr_uint();
+ Result = IntegerType::get(NumBits);
+ break;
+ }
case Type::FunctionTyID: {
const Type *RetType = readType();
+ unsigned RetAttr = read_vbr_uint();
unsigned NumParams = read_vbr_uint();
std::vector<const Type*> Params;
- while (NumParams--)
+ std::vector<FunctionType::ParameterAttributes> Attrs;
+ Attrs.push_back(FunctionType::ParameterAttributes(RetAttr));
+ while (NumParams--) {
Params.push_back(readType());
+ if (Params.back() != Type::VoidTy)
+ Attrs.push_back(FunctionType::ParameterAttributes(read_vbr_uint()));
+ }
bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
if (isVarArg) Params.pop_back();
- Result = FunctionType::get(RetType, Params, isVarArg);
+ Result = FunctionType::get(RetType, Params, isVarArg, Attrs);
break;
}
case Type::ArrayTyID: {
Result = ArrayType::get(ElementType, NumElements);
break;
}
- case Type::PackedTyID: {
+ case Type::VectorTyID: {
const Type *ElementType = readType();
unsigned NumElements = read_vbr_uint();
- Result = PackedType::get(ElementType, NumElements);
+ Result = VectorType::get(ElementType, NumElements);
break;
}
case Type::StructTyID: {
Typ = read_vbr_uint();
}
- Result = StructType::get(Elements);
+ Result = StructType::get(Elements, false);
+ break;
+ }
+ case Type::PackedStructTyID: {
+ std::vector<const Type*> Elements;
+ unsigned Typ = read_vbr_uint();
+ while (Typ) { // List is terminated by void/0 typeid
+ Elements.push_back(getType(Typ));
+ Typ = read_vbr_uint();
+ }
+
+ Result = StructType::get(Elements, true);
break;
}
case Type::PointerTyID: {
}
}
-// Upgrade obsolete constant expression opcodes (ver. 5 and prior) to the new
-// values used after ver 6. bytecode format. The operands are provided to the
-// function so that decisions based on the operand type can be made when
-// auto-upgrading obsolete opcodes to the new ones.
-// NOTE: This code needs to be kept synchronized with upgradeInstrOpcodes.
-// We can't use that function because of that functions argument requirements.
-// This function only deals with the subset of opcodes that are applicable to
-// constant expressions and is therefore simpler than upgradeInstrOpcodes.
-inline Constant *BytecodeReader::upgradeCEOpcodes(
- unsigned &Opcode, const std::vector<Constant*> &ArgVec, unsigned TypeID
-) {
- // Determine if no upgrade necessary
- if (!hasSignlessDivRem && !hasSignlessShrCastSetcc)
- return 0;
-
- // If this is bytecode version 6, that only had signed Rem and Div
- // instructions, then we must compensate for those two instructions only.
- // So that the switch statement below works, we're trying to turn this into
- // a version 5 opcode. To do that we must adjust the opcode to 10 (Div) if its
- // any of the UDiv, SDiv or FDiv instructions; or, adjust the opcode to
- // 11 (Rem) if its any of the URem, SRem, or FRem instructions; or, simply
- // decrement the instruction code if its beyond FRem.
- if (!hasSignlessDivRem) {
- // If its one of the signed Div/Rem opcodes, its fine the way it is
- if (Opcode >= 10 && Opcode <= 12) // UDiv through FDiv
- Opcode = 10; // Div
- else if (Opcode >=13 && Opcode <= 15) // URem through FRem
- Opcode = 11; // Rem
- else if (Opcode >= 16 && Opcode <= 35) // And through Shr
- // Adjust for new instruction codes
- Opcode -= 4;
- else if (Opcode >= 36 && Opcode <= 42) // Everything after Select
- // In vers 6 bytecode we eliminated the placeholders for the obsolete
- // VAARG and VANEXT instructions. Consequently those two slots were
- // filled starting with Select (36) which was 34. So now we only need
- // to subtract two. This circumvents hitting opcodes 32 and 33
- Opcode -= 2;
- else { // Opcode < 10 or > 42
- // No upgrade necessary.
- return 0;
- }
- }
-
- switch (Opcode) {
- default: // Pass Through
- // If we don't match any of the cases here then the opcode is fine the
- // way it is.
- break;
- case 7: // Add
- Opcode = Instruction::Add;
- break;
- case 8: // Sub
- Opcode = Instruction::Sub;
- break;
- case 9: // Mul
- Opcode = Instruction::Mul;
- break;
- case 10: // Div
- // The type of the instruction is based on the operands. We need to select
- // either udiv or sdiv based on that type. This expression selects the
- // cases where the type is floating point or signed in which case we
- // generated an sdiv instruction.
- if (ArgVec[0]->getType()->isFloatingPoint())
- Opcode = Instruction::FDiv;
- else if (ArgVec[0]->getType()->isSigned())
- Opcode = Instruction::SDiv;
- else
- Opcode = Instruction::UDiv;
- break;
- case 11: // Rem
- // As with "Div", make the signed/unsigned or floating point Rem
- // instruction choice based on the type of the operands.
- if (ArgVec[0]->getType()->isFloatingPoint())
- Opcode = Instruction::FRem;
- else if (ArgVec[0]->getType()->isSigned())
- Opcode = Instruction::SRem;
- else
- Opcode = Instruction::URem;
- break;
- case 12: // And
- Opcode = Instruction::And;
- break;
- case 13: // Or
- Opcode = Instruction::Or;
- break;
- case 14: // Xor
- Opcode = Instruction::Xor;
- break;
- case 15: // SetEQ
- Opcode = Instruction::SetEQ;
- break;
- case 16: // SetNE
- Opcode = Instruction::SetNE;
- break;
- case 17: // SetLE
- Opcode = Instruction::SetLE;
- break;
- case 18: // SetGE
- Opcode = Instruction::SetGE;
- break;
- case 19: // SetLT
- Opcode = Instruction::SetLT;
- break;
- case 20: // SetGT
- Opcode = Instruction::SetGT;
- break;
- case 26: // GetElementPtr
- Opcode = Instruction::GetElementPtr;
- break;
- case 28: { // Cast
- const Type *Ty = getType(TypeID);
- if (Ty == Type::BoolTy) {
- // The previous definition of cast to bool was a compare against zero.
- // We have to retain that semantic so we do it here.
- Opcode = Instruction::SetEQ;
- return ConstantExpr::get(Instruction::SetEQ, ArgVec[0],
- Constant::getNullValue(ArgVec[0]->getType()));
- } else if (ArgVec[0]->getType()->isFloatingPoint() &&
- isa<PointerType>(Ty)) {
- // Upgrade what is now an illegal cast (fp -> ptr) into two casts,
- // fp -> ui, and ui -> ptr
- Constant *CE = ConstantExpr::getFPToUI(ArgVec[0], Type::ULongTy);
- return ConstantExpr::getIntToPtr(CE, Ty);
- } else {
- Opcode = CastInst::getCastOpcode(ArgVec[0], Ty);
- }
- break;
- }
- case 30: // Shl
- Opcode = Instruction::Shl;
- break;
- case 31: // Shr
- if (ArgVec[0]->getType()->isSigned())
- Opcode = Instruction::AShr;
- else
- Opcode = Instruction::LShr;
- break;
- case 34: // Select
- Opcode = Instruction::Select;
- break;
- case 38: // ExtractElement
- Opcode = Instruction::ExtractElement;
- break;
- case 39: // InsertElement
- Opcode = Instruction::InsertElement;
- break;
- case 40: // ShuffleVector
- Opcode = Instruction::ShuffleVector;
- break;
- }
- return 0;
-}
-
/// Parse a single constant value
Value *BytecodeReader::ParseConstantPoolValue(unsigned TypeID) {
// We must check for a ConstantExpr before switching by type because
--isExprNumArgs;
// FIXME: Encoding of constant exprs could be much more compact!
- std::vector<Constant*> ArgVec;
+ SmallVector<Constant*, 8> ArgVec;
ArgVec.reserve(isExprNumArgs);
unsigned Opcode = read_vbr_uint();
ArgVec.push_back(getConstantValue(ArgTypeSlot, ArgValSlot));
}
- // Handle backwards compatibility for the opcode numbers
- if (Constant *C = upgradeCEOpcodes(Opcode, ArgVec, TypeID)) {
- if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, C);
- return C;
- }
-
// Construct a ConstantExpr of the appropriate kind
if (isExprNumArgs == 1) { // All one-operand expressions
if (!Instruction::isCast(Opcode))
error("Only cast instruction has one argument for ConstantExpr");
- Constant *Result = ConstantExpr::getCast(ArgVec[0], getType(TypeID));
- if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
+ Constant *Result = ConstantExpr::getCast(Opcode, ArgVec[0],
+ getType(TypeID));
+ if (Handler) Handler->handleConstantExpression(Opcode, &ArgVec[0],
+ ArgVec.size(), Result);
return Result;
} else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr
- std::vector<Constant*> IdxList(ArgVec.begin()+1, ArgVec.end());
- Constant *Result = ConstantExpr::getGetElementPtr(ArgVec[0], IdxList);
- if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
+ Constant *Result = ConstantExpr::getGetElementPtr(ArgVec[0], &ArgVec[1],
+ ArgVec.size()-1);
+ if (Handler) Handler->handleConstantExpression(Opcode, &ArgVec[0],
+ ArgVec.size(), Result);
return Result;
} else if (Opcode == Instruction::Select) {
if (ArgVec.size() != 3)
error("Select instruction must have three arguments.");
Constant* Result = ConstantExpr::getSelect(ArgVec[0], ArgVec[1],
ArgVec[2]);
- if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
+ if (Handler) Handler->handleConstantExpression(Opcode, &ArgVec[0],
+ ArgVec.size(), Result);
return Result;
} else if (Opcode == Instruction::ExtractElement) {
if (ArgVec.size() != 2 ||
!ExtractElementInst::isValidOperands(ArgVec[0], ArgVec[1]))
error("Invalid extractelement constand expr arguments");
Constant* Result = ConstantExpr::getExtractElement(ArgVec[0], ArgVec[1]);
- if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
+ if (Handler) Handler->handleConstantExpression(Opcode, &ArgVec[0],
+ ArgVec.size(), Result);
return Result;
} else if (Opcode == Instruction::InsertElement) {
if (ArgVec.size() != 3 ||
Constant *Result =
ConstantExpr::getInsertElement(ArgVec[0], ArgVec[1], ArgVec[2]);
- if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
+ if (Handler) Handler->handleConstantExpression(Opcode, &ArgVec[0],
+ ArgVec.size(), Result);
return Result;
} else if (Opcode == Instruction::ShuffleVector) {
if (ArgVec.size() != 3 ||
error("Invalid shufflevector constant expr arguments.");
Constant *Result =
ConstantExpr::getShuffleVector(ArgVec[0], ArgVec[1], ArgVec[2]);
- if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
+ if (Handler) Handler->handleConstantExpression(Opcode, &ArgVec[0],
+ ArgVec.size(), Result);
+ return Result;
+ } else if (Opcode == Instruction::ICmp) {
+ if (ArgVec.size() != 2)
+ error("Invalid ICmp constant expr arguments.");
+ unsigned predicate = read_vbr_uint();
+ Constant *Result = ConstantExpr::getICmp(predicate, ArgVec[0], ArgVec[1]);
+ if (Handler) Handler->handleConstantExpression(Opcode, &ArgVec[0],
+ ArgVec.size(), Result);
+ return Result;
+ } else if (Opcode == Instruction::FCmp) {
+ if (ArgVec.size() != 2)
+ error("Invalid FCmp constant expr arguments.");
+ unsigned predicate = read_vbr_uint();
+ Constant *Result = ConstantExpr::getFCmp(predicate, ArgVec[0], ArgVec[1]);
+ if (Handler) Handler->handleConstantExpression(Opcode, &ArgVec[0],
+ ArgVec.size(), Result);
return Result;
} else { // All other 2-operand expressions
Constant* Result = ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]);
- if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
+ if (Handler) Handler->handleConstantExpression(Opcode, &ArgVec[0],
+ ArgVec.size(), Result);
return Result;
}
}
const Type *Ty = getType(TypeID);
Constant *Result = 0;
switch (Ty->getTypeID()) {
- case Type::BoolTyID: {
- unsigned Val = read_vbr_uint();
- if (Val != 0 && Val != 1)
- error("Invalid boolean value read.");
- Result = ConstantBool::get(Val == 1);
- if (Handler) Handler->handleConstantValue(Result);
+ case Type::IntegerTyID: {
+ const IntegerType *IT = cast<IntegerType>(Ty);
+ if (IT->getBitWidth() <= 32) {
+ uint32_t Val = read_vbr_uint();
+ if (!ConstantInt::isValueValidForType(Ty, uint64_t(Val)))
+ error("Integer value read is invalid for type.");
+ Result = ConstantInt::get(IT, Val);
+ if (Handler) Handler->handleConstantValue(Result);
+ } else if (IT->getBitWidth() <= 64) {
+ uint64_t Val = read_vbr_uint64();
+ if (!ConstantInt::isValueValidForType(Ty, Val))
+ error("Invalid constant integer read.");
+ Result = ConstantInt::get(IT, Val);
+ if (Handler) Handler->handleConstantValue(Result);
+ } else
+ assert(0 && "Integer types > 64 bits not supported");
break;
}
-
- case Type::UByteTyID: // Unsigned integer types...
- case Type::UShortTyID:
- case Type::UIntTyID: {
- unsigned Val = read_vbr_uint();
- if (!ConstantInt::isValueValidForType(Ty, uint64_t(Val)))
- error("Invalid unsigned byte/short/int read.");
- Result = ConstantInt::get(Ty, Val);
- if (Handler) Handler->handleConstantValue(Result);
- break;
- }
-
- case Type::ULongTyID:
- Result = ConstantInt::get(Ty, read_vbr_uint64());
- if (Handler) Handler->handleConstantValue(Result);
- break;
-
- case Type::SByteTyID: // Signed integer types...
- case Type::ShortTyID:
- case Type::IntTyID:
- case Type::LongTyID: {
- int64_t Val = read_vbr_int64();
- if (!ConstantInt::isValueValidForType(Ty, Val))
- error("Invalid signed byte/short/int/long read.");
- Result = ConstantInt::get(Ty, Val);
- if (Handler) Handler->handleConstantValue(Result);
- break;
- }
-
case Type::FloatTyID: {
float Val;
read_float(Val);
Elements.push_back(getConstantValue(TypeSlot,
read_vbr_uint()));
Result = ConstantArray::get(AT, Elements);
- if (Handler) Handler->handleConstantArray(AT, Elements, TypeSlot, Result);
+ if (Handler) Handler->handleConstantArray(AT, &Elements[0], Elements.size(),
+ TypeSlot, Result);
break;
}
read_vbr_uint()));
Result = ConstantStruct::get(ST, Elements);
- if (Handler) Handler->handleConstantStruct(ST, Elements, Result);
+ if (Handler) Handler->handleConstantStruct(ST, &Elements[0],Elements.size(),
+ Result);
break;
}
- case Type::PackedTyID: {
- const PackedType *PT = cast<PackedType>(Ty);
+ case Type::VectorTyID: {
+ const VectorType *PT = cast<VectorType>(Ty);
unsigned NumElements = PT->getNumElements();
unsigned TypeSlot = getTypeSlot(PT->getElementType());
std::vector<Constant*> Elements;
while (NumElements--) // Read all of the elements of the constant.
Elements.push_back(getConstantValue(TypeSlot,
read_vbr_uint()));
- Result = ConstantPacked::get(PT, Elements);
- if (Handler) Handler->handleConstantPacked(PT, Elements, TypeSlot, Result);
+ Result = ConstantVector::get(PT, Elements);
+ if (Handler) Handler->handleConstantVector(PT, &Elements[0],Elements.size(),
+ TypeSlot, Result);
break;
}
error("String constant data invalid!");
const ArrayType *ATy = cast<ArrayType>(Ty);
- if (ATy->getElementType() != Type::SByteTy &&
- ATy->getElementType() != Type::UByteTy)
+ if (ATy->getElementType() != Type::Int8Ty &&
+ ATy->getElementType() != Type::Int8Ty)
error("String constant data invalid!");
// Read character data. The type tells us how long the string is.
unsigned FuncSize = BlockEnd - At;
GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage;
+ GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
- unsigned LinkageType = read_vbr_uint();
- switch (LinkageType) {
+ unsigned rWord = read_vbr_uint();
+ unsigned LinkageID = rWord & 65535;
+ unsigned VisibilityID = rWord >> 16;
+ switch (LinkageID) {
case 0: Linkage = GlobalValue::ExternalLinkage; break;
case 1: Linkage = GlobalValue::WeakLinkage; break;
case 2: Linkage = GlobalValue::AppendingLinkage; break;
Linkage = GlobalValue::InternalLinkage;
break;
}
+ switch (VisibilityID) {
+ case 0: Visibility = GlobalValue::DefaultVisibility; break;
+ case 1: Visibility = GlobalValue::HiddenVisibility; break;
+ default:
+ error("Unknown visibility type: " + utostr(VisibilityID));
+ Visibility = GlobalValue::DefaultVisibility;
+ break;
+ }
F->setLinkage(Linkage);
+ F->setVisibility(Visibility);
if (Handler) Handler->handleFunctionBegin(F,FuncSize);
// Keep track of how many basic blocks we have read in...
case BytecodeFormat::ConstantPoolBlockID:
if (!InsertedArguments) {
// Insert arguments into the value table before we parse the first basic
- // block in the function, but after we potentially read in the
- // compaction table.
+ // block in the function
insertArguments(F);
InsertedArguments = true;
}
ParseConstantPool(FunctionValues, FunctionTypes, true);
break;
- case BytecodeFormat::CompactionTableBlockID:
- ParseCompactionTable();
- break;
-
case BytecodeFormat::InstructionListBlockID: {
// Insert arguments into the value table before we parse the instruction
- // list for the function, but after we potentially read in the compaction
- // table.
+ // list for the function
if (!InsertedArguments) {
insertArguments(F);
InsertedArguments = true;
break;
}
- case BytecodeFormat::SymbolTableBlockID:
- ParseSymbolTable(F, &F->getSymbolTable());
+ case BytecodeFormat::ValueSymbolTableBlockID:
+ ParseValueSymbolTable(F, &F->getValueSymbolTable());
+ break;
+
+ case BytecodeFormat::TypeSymbolTableBlockID:
+ error("Functions don't have type symbol tables");
break;
default:
delete PlaceHolder;
}
- // If upgraded intrinsic functions were detected during reading of the
- // module information, then we need to look for instructions that need to
- // be upgraded. This can't be done while the instructions are read in because
- // additional instructions inserted mess up the slot numbering.
- if (!upgradedFunctions.empty()) {
- for (Function::iterator BI = F->begin(), BE = F->end(); BI != BE; ++BI)
- for (BasicBlock::iterator II = BI->begin(), IE = BI->end();
- II != IE;)
- if (CallInst* CI = dyn_cast<CallInst>(II++)) {
- std::map<Function*,Function*>::iterator FI =
- upgradedFunctions.find(CI->getCalledFunction());
- if (FI != upgradedFunctions.end())
- UpgradeIntrinsicCall(CI, FI->second);
- }
- }
-
// Clear out function-level types...
FunctionTypes.clear();
- CompactionTypes.clear();
- CompactionValues.clear();
freeTable(FunctionValues);
if (Handler) Handler->handleFunctionEnd(F);
/// @see ParseBytecode
bool BytecodeReader::ParseFunction(Function* Func, std::string* ErrMsg) {
- if (setjmp(context))
+ if (setjmp(context)) {
+ // Set caller's error message, if requested
+ if (ErrMsg)
+ *ErrMsg = ErrorMsg;
+ // Indicate an error occurred
return true;
+ }
// Find {start, end} pointers and slot in the map. If not there, we're done.
LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.find(Func);
/// to materialize the functions.
/// @see ParseBytecode
bool BytecodeReader::ParseAllFunctionBodies(std::string* ErrMsg) {
- if (setjmp(context))
+ if (setjmp(context)) {
+ // Set caller's error message, if requested
+ if (ErrMsg)
+ *ErrMsg = ErrorMsg;
+ // Indicate an error occurred
return true;
+ }
LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.begin();
LazyFunctionMap::iterator Fe = LazyFunctionLoadMap.end();
// Linkage, bit4+ = slot#
unsigned SlotNo = VarType >> 5;
unsigned LinkageID = (VarType >> 2) & 7;
+ unsigned VisibilityID = 0;
bool isConstant = VarType & 1;
bool hasInitializer = (VarType & 2) != 0;
unsigned Alignment = 0;
if (LinkageID == 3 && !hasInitializer) {
unsigned ExtWord = read_vbr_uint();
// The extension word has this format: bit 0 = has initializer, bit 1-3 =
- // linkage, bit 4-8 = alignment (log2), bits 10+ = future use.
+ // linkage, bit 4-8 = alignment (log2), bit 9 = has section,
+ // bits 10-12 = visibility, bits 13+ = future use.
hasInitializer = ExtWord & 1;
LinkageID = (ExtWord >> 1) & 7;
Alignment = (1 << ((ExtWord >> 4) & 31)) >> 1;
+ VisibilityID = (ExtWord >> 10) & 7;
if (ExtWord & (1 << 9)) // Has a section ID.
GlobalSectionID = read_vbr_uint();
Linkage = GlobalValue::InternalLinkage;
break;
}
-
+ GlobalValue::VisibilityTypes Visibility;
+ switch (VisibilityID) {
+ case 0: Visibility = GlobalValue::DefaultVisibility; break;
+ case 1: Visibility = GlobalValue::HiddenVisibility; break;
+ default:
+ error("Unknown visibility type: " + utostr(VisibilityID));
+ Visibility = GlobalValue::DefaultVisibility;
+ break;
+ }
+
const Type *Ty = getType(SlotNo);
if (!Ty)
error("Global has no type! SlotNo=" + utostr(SlotNo));
GlobalVariable *GV = new GlobalVariable(ElTy, isConstant, Linkage,
0, "", TheModule);
GV->setAlignment(Alignment);
+ GV->setVisibility(Visibility);
insertValue(GV, SlotNo, ModuleValues);
if (GlobalSectionID != 0)
// Notify handler about the global value.
if (Handler)
- Handler->handleGlobalVariable(ElTy, isConstant, Linkage, SlotNo,initSlot);
+ Handler->handleGlobalVariable(ElTy, isConstant, Linkage, Visibility,
+ SlotNo, initSlot);
// Get next item
VarType = read_vbr_uint();
if (Handler)
Handler->handleTargetTriple(triple);
+ // Read the data layout string and place into the module.
+ std::string datalayout = read_str();
+ TheModule->setDataLayout(datalayout);
+ // FIXME: Implement
+ // if (Handler)
+ // Handler->handleDataLayout(datalayout);
+
if (At != BlockEnd) {
// If the file has section info in it, read the section names now.
unsigned NumSections = read_vbr_uint();
/// Parse the version information and decode it by setting flags on the
/// Reader that enable backward compatibility of the reader.
void BytecodeReader::ParseVersionInfo() {
- unsigned Version = read_vbr_uint();
-
- // Unpack version number: low four bits are for flags, top bits = version
- Module::Endianness Endianness;
- Module::PointerSize PointerSize;
- Endianness = (Version & 1) ? Module::BigEndian : Module::LittleEndian;
- PointerSize = (Version & 2) ? Module::Pointer64 : Module::Pointer32;
-
- bool hasNoEndianness = Version & 4;
- bool hasNoPointerSize = Version & 8;
-
- RevisionNum = Version >> 4;
-
- // Default the backwards compatibility flag values for the current BC version
- hasSignlessDivRem = false;
- hasSignlessShrCastSetcc = false;
-
- // Determine which backwards compatibility flags to set based on the
- // bytecode file's version number
- switch (RevisionNum) {
- case 0: // LLVM 1.0, 1.1 (Released)
- case 1: // LLVM 1.2 (Released)
- case 2: // 1.2.5 (Not Released)
- case 3: // LLVM 1.3 (Released)
- case 4: // 1.3.1 (Not Released)
- error("Old bytecode formats no longer supported");
- break;
+ unsigned RevisionNum = read_vbr_uint();
- case 5: // 1.4 (Released)
- // In version 6, the Div and Rem instructions were converted to their
- // signed and floating point counterparts: UDiv, SDiv, FDiv, URem, SRem,
- // and FRem. Versions prior to 6 need to indicate that they have the
- // signless Div and Rem instructions.
- hasSignlessDivRem = true;
+ // We don't provide backwards compatibility in the Reader any more. To
+ // upgrade, the user should use llvm-upgrade.
+ if (RevisionNum < 7)
+ error("Bytecode formats < 7 are no longer supported. Use llvm-upgrade.");
- // FALL THROUGH
-
- case 6: // 1.9 (Released)
- // In version 5 and prior, instructions were signless while integer types
- // were signed. In version 6, instructions became signed and types became
- // signless. For example in version 5 we have the DIV instruction but in
- // version 6 we have FDIV, SDIV and UDIV to replace it. This caused a
- // renumbering of the instruction codes in version 6 that must be dealt with
- // when reading old bytecode files.
- hasSignlessShrCastSetcc = true;
-
- // FALL THROUGH
-
- case 7:
- break;
-
- default:
- error("Unknown bytecode version number: " + itostr(RevisionNum));
- }
-
- if (hasNoEndianness) Endianness = Module::AnyEndianness;
- if (hasNoPointerSize) PointerSize = Module::AnyPointerSize;
-
- TheModule->setEndianness(Endianness);
- TheModule->setPointerSize(PointerSize);
-
- if (Handler) Handler->handleVersionInfo(RevisionNum, Endianness, PointerSize);
+ if (Handler) Handler->handleVersionInfo(RevisionNum);
}
/// Parse a whole module.
ParseFunctionLazily();
break;
- case BytecodeFormat::SymbolTableBlockID:
- ParseSymbolTable(0, &TheModule->getSymbolTable());
+ case BytecodeFormat::ValueSymbolTableBlockID:
+ ParseValueSymbolTable(0, &TheModule->getValueSymbolTable());
+ break;
+
+ case BytecodeFormat::TypeSymbolTableBlockID:
+ ParseTypeSymbolTable(&TheModule->getTypeSymbolTable());
break;
default:
/// and \p Length parameters.
bool BytecodeReader::ParseBytecode(volatile BufPtr Buf, unsigned Length,
const std::string &ModuleID,
+ BCDecompressor_t *Decompressor,
std::string* ErrMsg) {
/// We handle errors by
// If this is a compressed file
if (Sig == ('l' | ('l' << 8) | ('v' << 16) | ('c' << 24))) {
+ if (!Decompressor) {
+ error("Compressed bytecode found, but not decompressor available");
+ }
// Invoke the decompression of the bytecode. Note that we have to skip the
// file's magic number which is not part of the compressed block. Hence,
// the Buf+4 and Length-4. The result goes into decompressedBlock, a data
// member for retention until BytecodeReader is destructed.
- unsigned decompressedLength = Compressor::decompressToNewBuffer(
- (char*)Buf+4,Length-4,decompressedBlock);
+ unsigned decompressedLength =
+ Decompressor((char*)Buf+4,Length-4,decompressedBlock, 0);
// We must adjust the buffer pointers used by the bytecode reader to point
// into the new decompressed block. After decompression, the
if (hasFunctions())
error("Function expected, but bytecode stream ended!");
- // Look for intrinsic functions to upgrade, upgrade them, and save the
- // mapping from old function to new for use later when instructions are
- // converted.
- for (Module::iterator FI = TheModule->begin(), FE = TheModule->end();
- FI != FE; ++FI)
- if (Function* newF = UpgradeIntrinsicFunction(FI)) {
- upgradedFunctions.insert(std::make_pair(FI, newF));
- FI->setName("");
- }
-
// Tell the handler we're done with the module
if (Handler)
Handler->handleModuleEnd(ModuleID);