Skip to content

[PowerPC] Add assembly directives to change endianness #149811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

DanilaZhebryakov
Copy link

Add simple assembly directives to change endianness of the code being assembled. This is useful in some edge cases to write code involving different endian modes.
Relocations in 'non-native' endian don't work.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added backend:PowerPC mc Machine (object) code labels Jul 21, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 21, 2025

@llvm/pr-subscribers-mc

@llvm/pr-subscribers-backend-powerpc

Author: None (DanilaZhebryakov)

Changes

Add simple assembly directives to change endianness of the code being assembled. This is useful in some edge cases to write code involving different endian modes.
Relocations in 'non-native' endian don't work.


Full diff: https://github.com/llvm/llvm-project/pull/149811.diff

5 Files Affected:

  • (modified) llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp (+11-1)
  • (modified) llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCCodeEmitter.h (+2)
  • (modified) llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCTargetDesc.cpp (+22)
  • (modified) llvm/lib/Target/PowerPC/MCTargetDesc/PPCTargetStreamer.h (+1)
  • (added) llvm/test/MC/PowerPC/endian-directive.s (+10)
diff --git a/llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp b/llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp
index 58766b16e86fe..17877954758be 100644
--- a/llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp
+++ b/llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp
@@ -1655,7 +1655,17 @@ bool PPCAsmParser::ParseDirective(AsmToken DirectiveID) {
     parseDirectiveTC(isPPC64() ? 8 : 4, DirectiveID);
   else if (IDVal == ".machine")
     parseDirectiveMachine(DirectiveID.getLoc());
-  else if (IDVal == ".abiversion")
+  else if (IDVal == ".big") {
+    PPCTargetStreamer *TStreamer = static_cast<PPCTargetStreamer *>(
+        getParser().getStreamer().getTargetStreamer());
+    if (TStreamer != nullptr)
+      TStreamer->emitEndianSet(false);
+  } else if (IDVal == ".little") {
+    PPCTargetStreamer *TStreamer = static_cast<PPCTargetStreamer *>(
+        getParser().getStreamer().getTargetStreamer());
+    if (TStreamer != nullptr)
+      TStreamer->emitEndianSet(true);
+  } else if (IDVal == ".abiversion")
     parseDirectiveAbiVersion(DirectiveID.getLoc());
   else if (IDVal == ".localentry")
     parseDirectiveLocalEntry(DirectiveID.getLoc());
diff --git a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCCodeEmitter.h b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCCodeEmitter.h
index b574557183194..a24b061eead8c 100644
--- a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCCodeEmitter.h
+++ b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCCodeEmitter.h
@@ -125,6 +125,8 @@ class PPCMCCodeEmitter : public MCCodeEmitter {
   /// Check if Opcode corresponds to a call instruction that should be marked
   /// with the NOTOC relocation.
   bool isNoTOCCallInstr(const MCInst &MI) const;
+
+  void setLittleEndian(bool little_endian) { IsLittleEndian = little_endian; }
 };
 
 } // namespace llvm
diff --git a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCTargetDesc.cpp b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCTargetDesc.cpp
index 54497d9c2fedf..68cc96c58e122 100644
--- a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCTargetDesc.cpp
+++ b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCTargetDesc.cpp
@@ -15,6 +15,7 @@
 #include "MCTargetDesc/PPCMCAsmInfo.h"
 #include "PPCELFStreamer.h"
 #include "PPCMCAsmInfo.h"
+#include "PPCMCCodeEmitter.h"
 #include "PPCTargetStreamer.h"
 #include "PPCXCOFFStreamer.h"
 #include "TargetInfo/PowerPCTargetInfo.h"
@@ -246,6 +247,10 @@ class PPCTargetAsmStreamer : public PPCTargetStreamer {
       OS << "\t.machine " << CPU << '\n';
   }
 
+  void emitEndianSet(bool little) override {
+    OS << (little ? "\t.little\n" : "\t.big\n");
+  }
+
   void emitAbiVersion(int AbiVersion) override {
     OS << "\t.abiversion " << AbiVersion << '\n';
   }
@@ -280,6 +285,13 @@ class PPCTargetELFStreamer : public PPCTargetStreamer {
     // limit the parser?
   }
 
+  void emitEndianSet(bool little) override {
+    PPCMCCodeEmitter *emitter = static_cast<PPCMCCodeEmitter *>(
+        getStreamer().getAssembler().getEmitterPtr());
+    if (emitter != nullptr)
+      emitter->setLittleEndian(little);
+  }
+
   void emitAbiVersion(int AbiVersion) override {
     ELFObjectWriter &W = getStreamer().getWriter();
     unsigned Flags = W.getELFHeaderEFlags();
@@ -408,6 +420,16 @@ class PPCTargetXCOFFStreamer : public PPCTargetStreamer {
         .setCPU(CPU);
   }
 
+  void emitEndianSet(bool little) override {
+    MCAssembler *assembler = getStreamer().getAssemblerPtr();
+    if (assembler == nullptr)
+      return;
+    PPCMCCodeEmitter *emitter =
+        static_cast<PPCMCCodeEmitter *>(assembler->getEmitterPtr());
+    if (emitter != nullptr)
+      emitter->setLittleEndian(little);
+  }
+
   void emitAbiVersion(int AbiVersion) override {
     llvm_unreachable("ABI-version pseudo-ops are invalid for XCOFF.");
   }
diff --git a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCTargetStreamer.h b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCTargetStreamer.h
index 3da034d4909ff..9c8d50a300a2a 100644
--- a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCTargetStreamer.h
+++ b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCTargetStreamer.h
@@ -27,6 +27,7 @@ class PPCTargetStreamer : public MCTargetStreamer {
 
   virtual void emitTCEntry(const MCSymbol &S, PPCMCExpr::Specifier Kind) {}
   virtual void emitMachine(StringRef CPU){};
+  virtual void emitEndianSet(bool little) {};
   virtual void emitAbiVersion(int AbiVersion){};
   virtual void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset){};
 };
diff --git a/llvm/test/MC/PowerPC/endian-directive.s b/llvm/test/MC/PowerPC/endian-directive.s
new file mode 100644
index 0000000000000..69e225135f662
--- /dev/null
+++ b/llvm/test/MC/PowerPC/endian-directive.s
@@ -0,0 +1,10 @@
+# RUN: llvm-mc -filetype=obj -triple=powerpc-unknown-linux-gnu %s | llvm-readobj -x .text - | FileCheck -check-prefix=CHECK-BE %s
+# RUN: llvm-mc -filetype=obj -triple=ppc32le-unknown-linux-gnu %s | llvm-readobj -x .text - | FileCheck -check-prefix=CHECK-LE %s
+add %r0, %r1, %r2
+.big
+add %r0, %r1, %r2
+.little
+add %r0, %r1, %r2
+
+# CHECK-BE: 0x00000000 7c011214 7c011214 1412017c          |...|......|
+# CHECK-LE: 0x00000000 1412017c 7c011214 1412017c          ...||......|

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:PowerPC mc Machine (object) code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants