From a54b7cbd452b3adb2f51346140d996b29c2cdb30 Mon Sep 17 00:00:00 2001 From: Reid Spencer Date: Fri, 12 Jan 2007 07:05:14 +0000 Subject: For PR1064: Implement the arbitrary bit-width integer feature. The feature allows integers of any bitwidth (up to 64) to be defined instead of just 1, 8, 16, 32, and 64 bit integers. This change does several things: 1. Introduces a new Derived Type, IntegerType, to represent the number of bits in an integer. The Type classes SubclassData field is used to store the number of bits. This allows 2^23 bits in an integer type. 2. Removes the five integer Type::TypeID values for the 1, 8, 16, 32 and 64-bit integers. These are replaced with just IntegerType which is not a primitive any more. 3. Adjust the rest of LLVM to account for this change. Note that while this incremental change lays the foundation for arbitrary bit-width integers, LLVM has not yet been converted to actually deal with them in any significant way. Most optimization passes, for example, will still only deal with the byte-width integer types. Future increments will rectify this situation. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33113 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/MachOWriter.cpp | 68 ++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 29 deletions(-) (limited to 'lib/CodeGen/MachOWriter.cpp') diff --git a/lib/CodeGen/MachOWriter.cpp b/lib/CodeGen/MachOWriter.cpp index af870e4..49c7457 100644 --- a/lib/CodeGen/MachOWriter.cpp +++ b/lib/CodeGen/MachOWriter.cpp @@ -708,8 +708,7 @@ void MachOWriter::InitMem(const Constant *C, void *Addr, intptr_t Offset, if (isa(PC)) { continue; } else if (const ConstantPacked *CP = dyn_cast(PC)) { - unsigned ElementSize = - CP->getType()->getElementType()->getPrimitiveSize(); + unsigned ElementSize = TD->getTypeSize(CP->getType()->getElementType()); for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) WorkList.push_back(CPair(CP->getOperand(i), PA+i*ElementSize)); } else if (const ConstantExpr *CE = dyn_cast(PC)) { @@ -726,27 +725,42 @@ void MachOWriter::InitMem(const Constant *C, void *Addr, intptr_t Offset, } } else if (PC->getType()->isFirstClassType()) { unsigned char *ptr = (unsigned char *)PA; - uint64_t val; - switch (PC->getType()->getTypeID()) { - case Type::Int1TyID: - case Type::Int8TyID: - ptr[0] = cast(PC)->getZExtValue(); - break; - case Type::Int16TyID: - val = cast(PC)->getZExtValue(); - if (TD->isBigEndian()) - val = ByteSwap_16(val); - ptr[0] = val; - ptr[1] = val >> 8; - break; - case Type::Int32TyID: - case Type::FloatTyID: - if (PC->getType()->getTypeID() == Type::FloatTyID) { - val = FloatToBits(cast(PC)->getValue()); + case Type::IntegerTyID: { + unsigned NumBits = cast(PC->getType())->getBitWidth(); + uint64_t val = cast(PC)->getZExtValue(); + if (NumBits <= 8) + ptr[0] = val; + else if (NumBits <= 16) { + if (TD->isBigEndian()) + val = ByteSwap_16(val); + ptr[0] = val; + ptr[1] = val >> 8; + } else if (NumBits <= 32) { + if (TD->isBigEndian()) + val = ByteSwap_32(val); + ptr[0] = val; + ptr[1] = val >> 8; + ptr[2] = val >> 16; + ptr[3] = val >> 24; + } else if (NumBits <= 64) { + if (TD->isBigEndian()) + val = ByteSwap_64(val); + ptr[0] = val; + ptr[1] = val >> 8; + ptr[2] = val >> 16; + ptr[3] = val >> 24; + ptr[4] = val >> 32; + ptr[5] = val >> 40; + ptr[6] = val >> 48; + ptr[7] = val >> 56; } else { - val = cast(PC)->getZExtValue(); + assert(0 && "Not implemented: bit widths > 64"); } + break; + } + case Type::FloatTyID: { + uint64_t val = FloatToBits(cast(PC)->getValue()); if (TD->isBigEndian()) val = ByteSwap_32(val); ptr[0] = val; @@ -754,13 +768,9 @@ void MachOWriter::InitMem(const Constant *C, void *Addr, intptr_t Offset, ptr[2] = val >> 16; ptr[3] = val >> 24; break; - case Type::DoubleTyID: - case Type::Int64TyID: - if (PC->getType()->getTypeID() == Type::DoubleTyID) { - val = DoubleToBits(cast(PC)->getValue()); - } else { - val = cast(PC)->getZExtValue(); - } + } + case Type::DoubleTyID: { + uint64_t val = DoubleToBits(cast(PC)->getValue()); if (TD->isBigEndian()) val = ByteSwap_64(val); ptr[0] = val; @@ -772,6 +782,7 @@ void MachOWriter::InitMem(const Constant *C, void *Addr, intptr_t Offset, ptr[6] = val >> 48; ptr[7] = val >> 56; break; + } case Type::PointerTyID: if (isa(C)) memset(ptr, 0, TD->getPointerSize()); @@ -790,8 +801,7 @@ void MachOWriter::InitMem(const Constant *C, void *Addr, intptr_t Offset, } else if (isa(PC)) { memset((void*)PA, 0, (size_t)TD->getTypeSize(PC->getType())); } else if (const ConstantArray *CPA = dyn_cast(PC)) { - unsigned ElementSize = - CPA->getType()->getElementType()->getPrimitiveSize(); + unsigned ElementSize = TD->getTypeSize(CPA->getType()->getElementType()); for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) WorkList.push_back(CPair(CPA->getOperand(i), PA+i*ElementSize)); } else if (const ConstantStruct *CPS = dyn_cast(PC)) { -- cgit v1.1