diff --git a/llvm/lib/MC/MCParser/MasmParser.cpp b/llvm/lib/MC/MCParser/MasmParser.cpp --- a/llvm/lib/MC/MCParser/MasmParser.cpp +++ b/llvm/lib/MC/MCParser/MasmParser.cpp @@ -127,7 +127,7 @@ std::vector Fields; StringMap FieldsByName; - FieldInfo &addField(StringRef FieldName, FieldType FT); + FieldInfo &addField(StringRef FieldName, FieldType FT, size_t FieldSize); StructInfo() = default; @@ -330,7 +330,8 @@ FieldInfo(FieldType FT) : Contents(FT) {} }; -FieldInfo &StructInfo::addField(StringRef FieldName, FieldType FT) { +FieldInfo &StructInfo::addField(StringRef FieldName, FieldType FT, + size_t FieldSize) { if (!FieldName.empty()) FieldsByName[FieldName] = Fields.size(); Fields.emplace_back(FT); @@ -338,7 +339,7 @@ if (IsUnion) { Field.Offset = 0; } else { - Size = llvm::alignTo(Size, Alignment); + Size = llvm::alignTo(Size, std::min(Alignment, FieldSize)); Field.Offset = Size; } return Field; @@ -759,13 +760,14 @@ // "real4", "real8" bool emitRealValues(const fltSemantics &Semantics); - bool addRealField(StringRef Name, const fltSemantics &Semantics); - bool parseDirectiveRealValue(StringRef IDVal, const fltSemantics &Semantics); + bool addRealField(StringRef Name, const fltSemantics &Semantics, size_t Size); + bool parseDirectiveRealValue(StringRef IDVal, const fltSemantics &Semantics, + size_t Size); bool parseRealInstList( const fltSemantics &Semantics, SmallVectorImpl &Values, const AsmToken::TokenKind EndToken = AsmToken::EndOfStatement); bool parseDirectiveNamedRealValue(StringRef IDVal, - const fltSemantics &Semantics, + const fltSemantics &Semantics, size_t Size, StringRef Name, SMLoc NameLoc); bool parseOptionalAngleBracketOpen(); @@ -2118,9 +2120,9 @@ case DK_DQ: return parseDirectiveValue(IDVal, 8); case DK_REAL4: - return parseDirectiveRealValue(IDVal, APFloat::IEEEsingle()); + return parseDirectiveRealValue(IDVal, APFloat::IEEEsingle(), 4); case DK_REAL8: - return parseDirectiveRealValue(IDVal, APFloat::IEEEdouble()); + return parseDirectiveRealValue(IDVal, APFloat::IEEEdouble(), 8); case DK_STRUCT: case DK_UNION: return parseDirectiveNestedStruct(IDVal, DirKind); @@ -2343,12 +2345,12 @@ return parseDirectiveNamedValue(nextVal, 8, IDVal, IDLoc); case DK_REAL4: Lex(); - return parseDirectiveNamedRealValue(nextVal, APFloat::IEEEsingle(), IDVal, - IDLoc); + return parseDirectiveNamedRealValue(nextVal, APFloat::IEEEsingle(), 4, + IDVal, IDLoc); case DK_REAL8: Lex(); - return parseDirectiveNamedRealValue(nextVal, APFloat::IEEEdouble(), IDVal, - IDLoc); + return parseDirectiveNamedRealValue(nextVal, APFloat::IEEEdouble(), 8, + IDVal, IDLoc); case DK_STRUCT: case DK_UNION: Lex(); @@ -3306,7 +3308,7 @@ // Add a field to the current structure. bool MasmParser::addIntegralField(StringRef Name, unsigned Size) { StructInfo &Struct = StructInProgress.back(); - FieldInfo &Field = Struct.addField(Name, FT_INTEGRAL); + FieldInfo &Field = Struct.addField(Name, FT_INTEGRAL, Size); IntFieldInfo &IntInfo = Field.Contents.IntInfo; Field.Type = Size; @@ -3481,9 +3483,10 @@ } // Add a real field to the current struct. -bool MasmParser::addRealField(StringRef Name, const fltSemantics &Semantics) { +bool MasmParser::addRealField(StringRef Name, const fltSemantics &Semantics, + size_t Size) { StructInfo &Struct = StructInProgress.back(); - FieldInfo &Field = Struct.addField(Name, FT_REAL); + FieldInfo &Field = Struct.addField(Name, FT_REAL, Size); RealFieldInfo &RealInfo = Field.Contents.RealInfo; Field.SizeOf = 0; @@ -3504,12 +3507,13 @@ /// parseDirectiveRealValue /// ::= (real4 | real8) [ expression (, expression)* ] bool MasmParser::parseDirectiveRealValue(StringRef IDVal, - const fltSemantics &Semantics) { + const fltSemantics &Semantics, + size_t Size) { if (StructInProgress.empty()) { // Initialize data value. if (emitRealValues(Semantics)) return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); - } else if (addRealField("", Semantics)) { + } else if (addRealField("", Semantics, Size)) { return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); } return false; @@ -3519,14 +3523,15 @@ /// ::= name (real4 | real8) [ expression (, expression)* ] bool MasmParser::parseDirectiveNamedRealValue(StringRef IDVal, const fltSemantics &Semantics, - StringRef Name, SMLoc NameLoc) { + size_t Size, StringRef Name, + SMLoc NameLoc) { if (StructInProgress.empty()) { // Initialize named data value. MCSymbol *Sym = getContext().getOrCreateSymbol(Name); getStreamer().emitLabel(Sym); if (emitRealValues(Semantics)) return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); - } else if (addRealField(Name, Semantics)) { + } else if (addRealField(Name, Semantics, Size)) { return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); } return false; @@ -3956,7 +3961,7 @@ // Declare a field in the current struct. bool MasmParser::addStructField(StringRef Name, const StructInfo &Structure) { StructInfo &OwningStruct = StructInProgress.back(); - FieldInfo &Field = OwningStruct.addField(Name, FT_STRUCT); + FieldInfo &Field = OwningStruct.addField(Name, FT_STRUCT, Structure.Size); StructFieldInfo &StructInfo = Field.Contents.StructInfo; StructInfo.Structure = Structure; @@ -4130,7 +4135,8 @@ else ParentStruct.Size += Structure.Size; } else { - FieldInfo &Field = ParentStruct.addField(Structure.Name, FT_STRUCT); + FieldInfo &Field = + ParentStruct.addField(Structure.Name, FT_STRUCT, Structure.Size); StructFieldInfo &StructInfo = Field.Contents.StructInfo; Field.Type = Structure.Size; Field.LengthOf = 1; diff --git a/llvm/test/tools/llvm-ml/struct.test b/llvm/test/tools/llvm-ml/struct.test --- a/llvm/test/tools/llvm-ml/struct.test +++ b/llvm/test/tools/llvm-ml/struct.test @@ -34,11 +34,9 @@ ; CHECK-NEXT: .byte 1 ; CHECK-NEXT: .byte 2 ; -; , with internal alignment padding +; , with no alignment padding (field size < alignment) ; CHECK-NEXT: .byte 6 -; CHECK-NEXT: .zero 1 ; CHECK-NEXT: .byte 7 -; CHECK-NEXT: .zero 1 ; ; BYTE "abcde", plus alignment padding ; CHECK-NEXT: .byte 97 @@ -65,11 +63,9 @@ ; CHECK-NEXT: .byte 10 ; CHECK-NEXT: .byte 11 ; -; , with internal alignment padding +; , with no alignment padding (field size < alignment) ; CHECK-NEXT: .byte 12 -; CHECK-NEXT: .zero 1 ; CHECK-NEXT: .byte 7 -; CHECK-NEXT: .zero 1 ; ; BYTE "ijk", padded with " ", plus alignment padding ; CHECK-NEXT: .byte 105 @@ -87,16 +83,16 @@ mov eax, [t2.f.h] ; CHECK: t3: -; CHECK-NEXT: mov eax, dword ptr [rip + t2+12] -; CHECK-NEXT: mov eax, dword ptr [rip + t2+12] -; CHECK-NEXT: mov eax, dword ptr [rip + t2+12] +; CHECK-NEXT: mov eax, dword ptr [rip + t2+11] +; CHECK-NEXT: mov eax, dword ptr [rip + t2+11] +; CHECK-NEXT: mov eax, dword ptr [rip + t2+11] t4: mov eax, j.FOOBAR.f.h mov eax, j.baz.b ; CHECK: t4: -; CHECK-NEXT: mov eax, dword ptr [rip + j+12] +; CHECK-NEXT: mov eax, dword ptr [rip + j+11] ; CHECK-NEXT: mov eax, dword ptr [rip + j+1] t5: @@ -105,9 +101,9 @@ mov eax, [ebx.FOOBAR.f.h] ; CHECK: t5: -; CHECK-NEXT: mov eax, dword ptr [ebx + 12] -; CHECK-NEXT: mov eax, dword ptr [ebx + 12] -; CHECK-NEXT: mov eax, dword ptr [ebx + 12] +; CHECK-NEXT: mov eax, dword ptr [ebx + 11] +; CHECK-NEXT: mov eax, dword ptr [ebx + 11] +; CHECK-NEXT: mov eax, dword ptr [ebx + 11] t6: mov eax, t2.FOOBAR.f.h @@ -116,10 +112,10 @@ mov eax, [t2.FOOBAR.f.h] ; CHECK: t6: -; CHECK-NEXT: mov eax, dword ptr [rip + t2+12] -; CHECK-NEXT: mov eax, dword ptr [rip + t2+12] -; CHECK-NEXT: mov eax, dword ptr [rip + t2+12] -; CHECK-NEXT: mov eax, dword ptr [rip + t2+12] +; CHECK-NEXT: mov eax, dword ptr [rip + t2+11] +; CHECK-NEXT: mov eax, dword ptr [rip + t2+11] +; CHECK-NEXT: mov eax, dword ptr [rip + t2+11] +; CHECK-NEXT: mov eax, dword ptr [rip + t2+11] t7: mov eax, [ebx].FOOBAR.e.b @@ -185,7 +181,7 @@ ; CHECK: t10: ; CHECK-NEXT: mov eax, 10 -; CHECK-NEXT: mov eax, 12 +; CHECK-NEXT: mov eax, 11 t11: mov eax, (FOOBAR PTR [ebx]).f