[ARM] Fix endianness of Thumb .inst.w directive

Wide Thumb2 instructions should be emitted into the object file as pairs of
16-bit words of the appropriate endianness, not one 32-bit word.

Differential revision: https://reviews.llvm.org/D41185

llvm-svn: 321799
diff --git a/llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp b/llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp
index d465da1..9d73c76 100644
--- a/llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp
+++ b/llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp
@@ -512,9 +512,11 @@
 
       assert(IsThumb);
       EmitThumbMappingSymbol();
+      // Thumb wide instructions are emitted as a pair of 16-bit words of the
+      // appropriate endianness.
       for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
-        const unsigned I0 = LittleEndian ? II + 0 : (Size - II - 1);
-        const unsigned I1 = LittleEndian ? II + 1 : (Size - II - 2);
+        const unsigned I0 = LittleEndian ? II + 0 : II + 1;
+        const unsigned I1 = LittleEndian ? II + 1 : II + 0;
         Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
         Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
       }
diff --git a/llvm/test/MC/ARM/inst-directive.s b/llvm/test/MC/ARM/inst-directive.s
index a3fd8c2..d3f2a36 100644
--- a/llvm/test/MC/ARM/inst-directive.s
+++ b/llvm/test/MC/ARM/inst-directive.s
@@ -1,5 +1,8 @@
 @ RUN: llvm-mc %s -triple=armv7-linux-gnueabi -filetype=obj -o - \
-@ RUN:   | llvm-readobj -s -sd | FileCheck %s
+@ RUN:   | llvm-readobj -s -sd | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-LE
+
+@ RUN: llvm-mc %s -triple=armebv7-linux-gnueabi -filetype=obj -o - \
+@ RUN:   | llvm-readobj -s -sd | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-BE
 
 	.syntax unified
 
@@ -19,7 +22,8 @@
 @ CHECK: Section {
 @ CHECK:   Name: .inst.arm_inst
 @ CHECK:   SectionData (
-@ CHECK-NEXT:     0000: FEDE0000
+@ CHECK-LE-NEXT:     0000: FEDE0000
+@ CHECK-BE-NEXT:     0000: 0000DEFE
 @ CHECK-NEXT:   )
 
 @-------------------------------------------------------------------------------
@@ -38,7 +42,8 @@
 @ CHECK: Section {
 @ CHECK:   Name: .inst.thumb_inst_n
 @ CHECK:   SectionData (
-@ CHECK-NEXT:     0000: FEDE
+@ CHECK-LE-NEXT:     0000: FEDE
+@ CHECK-BE-NEXT:     0000: DEFE
 @ CHECK-NEXT:   )
 
 @-------------------------------------------------------------------------------
@@ -52,12 +57,13 @@
 	.global	thumb_inst_w
 	.type	thumb_inst_w,%function
 thumb_inst_w:
-	.inst.w 0x00000000
+	.inst.w 0x12345678
 
 @ CHECK: Section {
 @ CHECK:   Name: .inst.thumb_inst_w
 @ CHECK:   SectionData (
-@ CHECK-NEXT:     0000: 00000000
+@ CHECK-LE-NEXT:     0000: 34127856
+@ CHECK-BE-NEXT:     0000: 12345678
 @ CHECK-NEXT:   )
 
 @-------------------------------------------------------------------------------
@@ -76,6 +82,7 @@
 @ CHECK: Section {
 @ CHECK:   Name: .inst.thumb_inst_inst
 @ CHECK:   SectionData (
-@ CHECK-NEXT:     0000: 40F20000 C0F20000
+@ CHECK-LE-NEXT:     0000: 40F20000 C0F20000
+@ CHECK-BE-NEXT:     0000: F2400000 F2C00000
 @ CHECK-NEXT:   )