[PATCH] D69411: [MC] Calculate difference of symbols in two fragments when possible

Jian Cai via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 28 10:20:10 PDT 2019


jcai19 updated this revision to Diff 226699.
jcai19 added a comment.

Add a test case.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D69411/new/

https://reviews.llvm.org/D69411

Files:
  llvm/lib/MC/MCExpr.cpp
  llvm/test/MC/AsmParser/directive_if_with_dot_symbol.s


Index: llvm/test/MC/AsmParser/directive_if_with_dot_symbol.s
===================================================================
--- /dev/null
+++ llvm/test/MC/AsmParser/directive_if_with_dot_symbol.s
@@ -0,0 +1,6 @@
+@ RUN: llvm-mc -filetype=obj -triple armv7a-linux-gnueabihf %s -o -
+
+nop
+.arch_extension sec
+9997: nop ; .if . - 9997b == 2 ; nop ; .endif ;
+// CHECK-MESSAGES: :[[@LINE-1]]:17: error: expected absolute expression
Index: llvm/lib/MC/MCExpr.cpp
===================================================================
--- llvm/lib/MC/MCExpr.cpp
+++ llvm/lib/MC/MCExpr.cpp
@@ -517,6 +517,8 @@
 
   const MCSymbol &SA = A->getSymbol();
   const MCSymbol &SB = B->getSymbol();
+  const MCFragment *FragA = SA.getFragment();
+  const MCFragment *FragB = SB.getFragment();
 
   if (SA.isUndefined() || SB.isUndefined())
     return;
@@ -524,8 +526,9 @@
   if (!Asm->getWriter().isSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet))
     return;
 
-  if (SA.getFragment() == SB.getFragment() && !SA.isVariable() &&
-      !SA.isUnset() && !SB.isVariable() && !SB.isUnset()) {
+  if (FragA == FragB && !SA.isVariable() && !SA.isUnset() && !SB.isVariable() &&
+      !SB.isUnset()) {
+
     Addend += (SA.getOffset() - SB.getOffset());
 
     // Pointers to Thumb symbols need to have their low-bit set to allow
@@ -544,13 +547,39 @@
     return;
   }
 
-  if (!Layout)
+  const MCSection &SecA = *FragA->getParent();
+  const MCSection &SecB = *FragB->getParent();
+
+  if ((&SecA != &SecB) && !Addrs)
     return;
 
-  const MCSection &SecA = *SA.getFragment()->getParent();
-  const MCSection &SecB = *SB.getFragment()->getParent();
+     // Pointers to Thumb symbols need to have their low-bit set to allow
+   if ((&SecA != &SecB) && !Addrs)
+     return;
 
-  if ((&SecA != &SecB) && !Addrs)
+   if (SecB.getFragmentList().getNextNode(*FragB) == FragA &&
+       dyn_cast<MCDataFragment>(FragA) && dyn_cast<MCDataFragment>(FragB)) {
+     Addend +=
+         (SA.getOffset() + (cast<MCDataFragment>(FragB))->getContents().size() -
+          SB.getOffset());
+
+    // Pointers to Thumb symbols need to have their low-bit set to allow
+    // for interworking.
+    if (Asm->isThumbFunc(&SA))
+      Addend |= 1;
+
+    // If symbol is labeled as micromips, we set low-bit to ensure
+    // correct offset in .gcc_except_table
+    if (Asm->getBackend().isMicroMips(&SA))
+      Addend |= 1;
+
+    // Clear the symbol expr pointers to indicate we have folded these
+    // operands.
+    A = B = nullptr;
+    return;
+  }
+
+  if (!Layout)
     return;
 
   // Eagerly evaluate.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69411.226699.patch
Type: text/x-patch
Size: 2596 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191028/5c53cbe6/attachment.bin>


More information about the llvm-commits mailing list