-TargetData::TargetData(const std::string &TargetName,
- bool isLittleEndian, unsigned char PtrSize,
- unsigned char PtrAl, unsigned char DoubleAl,
- unsigned char FloatAl, unsigned char LongAl,
- unsigned char IntAl, unsigned char ShortAl,
- unsigned char ByteAl)
- : AID(AnnotationManager::getID("TargetData::" + TargetName)) {
- AnnotationManager::registerAnnotationFactory(AID, TypeAnFactory, this);
-
- // If this assert triggers, a pass "required" TargetData information, but the
- // top level tool did not provide once for it. We do not want to default
- // construct, or else we might end up using a bad endianness or pointer size!
- //
- assert(!TargetName.empty() &&
- "ERROR: Tool did not specify a target data to use!");
-
- LittleEndian = isLittleEndian;
- PointerSize = PtrSize;
- PointerAlignment = PtrAl;
- DoubleAlignment = DoubleAl;
- assert(DoubleAlignment == PtrAl &&
- "Double alignment and pointer alignment agree for now!");
- FloatAlignment = FloatAl;
- LongAlignment = LongAl;
- IntAlignment = IntAl;
- ShortAlignment = ShortAl;
- ByteAlignment = ByteAl;
-}
-
-TargetData::TargetData(const std::string &ToolName, const Module *M)
- : AID(AnnotationManager::getID("TargetData::" + ToolName)) {
- AnnotationManager::registerAnnotationFactory(AID, TypeAnFactory, this);
-
- LittleEndian = M->isLittleEndian();
- PointerSize = M->has32BitPointers() ? 4 : 8;
- PointerAlignment = PointerSize;
- DoubleAlignment = PointerSize;
- FloatAlignment = 4;
- LongAlignment = 8;
- IntAlignment = 4;
- ShortAlignment = 2;
- ByteAlignment = 1;
+/*!
+ A TargetDescription string consists of a sequence of hyphen-delimited
+ specifiers for target endianness, pointer size and alignments, and various
+ primitive type sizes and alignments. A typical string looks something like:
+ <br><br>
+ "E-p:32:32:32-i1:8:8-i8:8:8-i32:32:32-i64:32:64-f32:32:32-f64:32:64"
+ <br><br>
+ (note: this string is not fully specified and is only an example.)
+ \p
+ Alignments come in two flavors: ABI and preferred. ABI alignment (abi_align,
+ below) dictates how a type will be aligned within an aggregate and when used
+ as an argument. Preferred alignment (pref_align, below) determines a type's
+ alignment when emitted as a global.
+ \p
+ Specifier string details:
+ <br><br>
+ <i>[E|e]</i>: Endianness. "E" specifies a big-endian target data model, "e"
+ specifies a little-endian target data model.
+ <br><br>
+ <i>p:@verbatim<size>:<abi_align>:<pref_align>@endverbatim</i>: Pointer size,
+ ABI and preferred alignment.
+ <br><br>
+ <i>@verbatim<type><size>:<abi_align>:<pref_align>@endverbatim</i>: Numeric type
+ alignment. Type is
+ one of <i>i|f|v|a</i>, corresponding to integer, floating point, vector (aka
+ packed) or aggregate. Size indicates the size, e.g., 32 or 64 bits.
+ \p
+ The default string, fully specified is:
+ <br><br>
+ "E-p:64:64:64-a0:0:0-f32:32:32-f64:0:64"
+ "-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:0:64"
+ "-v64:64:64-v128:128:128"
+ <br><br>
+ Note that in the case of aggregates, 0 is the default ABI and preferred
+ alignment. This is a special case, where the aggregate's computed worst-case
+ alignment will be used.
+ */
+void TargetData::init(const std::string &TargetDescription) {
+ std::string temp = TargetDescription;
+
+ LittleEndian = false;
+ PointerMemSize = 8;
+ PointerABIAlign = 8;
+ PointerPrefAlign = PointerABIAlign;
+
+ // Default alignments
+ setAlignment(INTEGER_ALIGN, 1, 1, 1); // Bool
+ setAlignment(INTEGER_ALIGN, 1, 1, 8); // Byte
+ setAlignment(INTEGER_ALIGN, 2, 2, 16); // short
+ setAlignment(INTEGER_ALIGN, 4, 4, 32); // int
+ setAlignment(INTEGER_ALIGN, 4, 8, 64); // long
+ setAlignment(FLOAT_ALIGN, 4, 4, 32); // float
+ setAlignment(FLOAT_ALIGN, 8, 8, 64); // double
+ setAlignment(VECTOR_ALIGN, 8, 8, 64); // v2i32
+ setAlignment(VECTOR_ALIGN, 16, 16, 128); // v16i8, v8i16, v4i32, ...
+ setAlignment(AGGREGATE_ALIGN, 0, 8, 0); // struct, union, class, ...
+
+ while (!temp.empty()) {
+ std::string token = getToken(temp, "-");
+ std::string arg0 = getToken(token, ":");
+ const char *p = arg0.c_str();
+ switch(*p) {
+ case 'E':
+ LittleEndian = false;
+ break;
+ case 'e':
+ LittleEndian = true;
+ break;
+ case 'p':
+ PointerMemSize = atoi(getToken(token,":").c_str()) / 8;
+ PointerABIAlign = atoi(getToken(token,":").c_str()) / 8;
+ PointerPrefAlign = atoi(getToken(token,":").c_str()) / 8;
+ if (PointerPrefAlign == 0)
+ PointerPrefAlign = PointerABIAlign;
+ break;
+ case 'i':
+ case 'v':
+ case 'f':
+ case 'a':
+ case 's': {
+ AlignTypeEnum align_type = STACK_ALIGN; // Dummy init, silence warning
+ switch(*p) {
+ case 'i': align_type = INTEGER_ALIGN; break;
+ case 'v': align_type = VECTOR_ALIGN; break;
+ case 'f': align_type = FLOAT_ALIGN; break;
+ case 'a': align_type = AGGREGATE_ALIGN; break;
+ case 's': align_type = STACK_ALIGN; break;
+ }
+ uint32_t size = (uint32_t) atoi(++p);
+ unsigned char abi_align = atoi(getToken(token, ":").c_str()) / 8;
+ unsigned char pref_align = atoi(getToken(token, ":").c_str()) / 8;
+ if (pref_align == 0)
+ pref_align = abi_align;
+ setAlignment(align_type, abi_align, pref_align, size);
+ break;
+ }
+ default:
+ break;
+ }
+ }
+}
+
+TargetData::TargetData(const Module *M)
+ : ImmutablePass((intptr_t)&ID) {
+ init(M->getDataLayout());
+}
+
+void
+TargetData::setAlignment(AlignTypeEnum align_type, unsigned char abi_align,
+ unsigned char pref_align, uint32_t bit_width) {
+ assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
+ for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
+ if (Alignments[i].AlignType == align_type &&
+ Alignments[i].TypeBitWidth == bit_width) {
+ // Update the abi, preferred alignments.
+ Alignments[i].ABIAlign = abi_align;
+ Alignments[i].PrefAlign = pref_align;
+ return;
+ }
+ }
+
+ Alignments.push_back(TargetAlignElem::get(align_type, abi_align,
+ pref_align, bit_width));