[llvm] 2feb6e9 - [ms] [llvm-ml] Fix STRUCT field alignment

Eric Astor via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 7 10:59:13 PDT 2020


Author: Eric Astor
Date: 2020-09-07T13:58:59-04:00
New Revision: 2feb6e9b8418b29c002bc830a3e2fdcbe9e39449

URL: https://github.com/llvm/llvm-project/commit/2feb6e9b8418b29c002bc830a3e2fdcbe9e39449
DIFF: https://github.com/llvm/llvm-project/commit/2feb6e9b8418b29c002bc830a3e2fdcbe9e39449.diff

LOG: [ms] [llvm-ml] Fix STRUCT field alignment

MASM aligns fields to the _minimum_ of the STRUCT alignment value and the size of the next field.

Reviewed By: thakis

Differential Revision: https://reviews.llvm.org/D86945

Added: 
    

Modified: 
    llvm/lib/MC/MCParser/MasmParser.cpp
    llvm/test/tools/llvm-ml/struct.test

Removed: 
    


################################################################################
diff  --git a/llvm/lib/MC/MCParser/MasmParser.cpp b/llvm/lib/MC/MCParser/MasmParser.cpp
index 94cef83bc405..333eef2f698f 100644
--- a/llvm/lib/MC/MCParser/MasmParser.cpp
+++ b/llvm/lib/MC/MCParser/MasmParser.cpp
@@ -127,7 +127,7 @@ struct StructInfo {
   std::vector<FieldInfo> Fields;
   StringMap<size_t> FieldsByName;
 
-  FieldInfo &addField(StringRef FieldName, FieldType FT);
+  FieldInfo &addField(StringRef FieldName, FieldType FT, size_t FieldSize);
 
   StructInfo() = default;
 
@@ -330,7 +330,8 @@ struct FieldInfo {
   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 @@ FieldInfo &StructInfo::addField(StringRef FieldName, FieldType FT) {
   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 @@ class MasmParser : public MCAsmParser {
 
   // "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<APInt> &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 @@ bool MasmParser::parseStatement(ParseStatementInfo &Info,
     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 @@ bool MasmParser::parseStatement(ParseStatementInfo &Info,
     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 @@ bool MasmParser::emitIntegralValues(unsigned Size) {
 // 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 @@ bool MasmParser::emitRealValues(const fltSemantics &Semantics) {
 }
 
 // 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 @@ bool MasmParser::addRealField(StringRef Name, const fltSemantics &Semantics) {
 /// 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 @@ bool MasmParser::parseDirectiveRealValue(StringRef IDVal,
 ///  ::= 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 @@ bool MasmParser::emitStructValues(const StructInfo &Structure) {
 // 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 @@ bool MasmParser::parseDirectiveNestedEnds() {
     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
index fa85ecd455dd..38fc763fc7e1 100644
--- a/llvm/test/tools/llvm-ml/struct.test
+++ b/llvm/test/tools/llvm-ml/struct.test
@@ -34,11 +34,9 @@ t1 foobar <>
 ; CHECK-NEXT: .byte 1
 ; CHECK-NEXT: .byte 2
 ;
-; <BYTE 6, BYTE 7>, with internal alignment padding
+; <BYTE 6, BYTE 7>, 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 @@ t2 FOOBAR <"gh",,<10,11>,<12>,"ijk">
 ; CHECK-NEXT: .byte 10
 ; CHECK-NEXT: .byte 11
 ;
-; <BYTE 6, BYTE 7>, with internal alignment padding
+; <BYTE 12, BYTE 7>, 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
 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
 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
 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 @@ mov eax, FOOBAR.f.h
 
 ; CHECK: t10:
 ; CHECK-NEXT: mov eax, 10
-; CHECK-NEXT: mov eax, 12
+; CHECK-NEXT: mov eax, 11
 
 t11:
 mov eax, (FOOBAR PTR [ebx]).f


        


More information about the llvm-commits mailing list