Skip to content

Conversation

@tltao
Copy link
Contributor

@tltao tltao commented Sep 5, 2024

Currently the AsmStreamer class MCAsmStreamer.cpp only outputs GNU assembly syntax. However, we wish to add additional AsmStreamer classes for specific targets that can output alternative assembly syntaxes (the immediate use case is HLASM syntax for z/OS).

This is the first patch to add this ability by utilizing the TargetRegistry functions.

@llvmbot llvmbot added the llvm:mc Machine (object) code label Sep 5, 2024
@llvmbot
Copy link
Member

llvmbot commented Sep 5, 2024

@llvm/pr-subscribers-mc

Author: None (tltao)

Changes

Currently the AsmStreamer class MCAsmStreamer.cpp only outputs GNU assembly syntax. However, we wish to add additional AsmStreamer classes for specific targets that can output alternative assembly syntaxes (the immediate use case is HLASM syntax for z/OS).

This is the first patch to add this ability by utilizing the TargetRegistry functions.


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

2 Files Affected:

  • (modified) llvm/include/llvm/MC/TargetRegistry.h (+12)
  • (modified) llvm/lib/MC/TargetRegistry.cpp (+8-2)
diff --git a/llvm/include/llvm/MC/TargetRegistry.h b/llvm/include/llvm/MC/TargetRegistry.h
index 42d510c17bce39..1850139ba81491 100644
--- a/llvm/include/llvm/MC/TargetRegistry.h
+++ b/llvm/include/llvm/MC/TargetRegistry.h
@@ -208,6 +208,10 @@ class Target {
   using AsmTargetStreamerCtorTy =
       MCTargetStreamer *(*)(MCStreamer &S, formatted_raw_ostream &OS,
                             MCInstPrinter *InstPrint);
+  using AsmStreamerCtorTy =
+      MCStreamer *(*)(MCContext &Ctx, std::unique_ptr<formatted_raw_ostream> OS,
+                      MCInstPrinter *IP, std::unique_ptr<MCCodeEmitter> CE,
+                      std::unique_ptr<MCAsmBackend> TAB);
   using ObjectTargetStreamerCtorTy = MCTargetStreamer *(*)(
       MCStreamer &S, const MCSubtargetInfo &STI);
   using MCRelocationInfoCtorTy = MCRelocationInfo *(*)(const Triple &TT,
@@ -316,6 +320,10 @@ class Target {
   /// registered (default = nullptr).
   AsmTargetStreamerCtorTy AsmTargetStreamerCtorFn = nullptr;
 
+  /// Construction function for this target's AsmStreamer, if
+  /// registered (default = nullptr).
+  AsmStreamerCtorTy AsmStreamerCtorFn = nullptr;
+
   /// Construction function for this target's obj TargetStreamer, if
   /// registered (default = nullptr).
   ObjectTargetStreamerCtorTy ObjectTargetStreamerCtorFn = nullptr;
@@ -927,6 +935,10 @@ struct TargetRegistry {
     T.NullTargetStreamerCtorFn = Fn;
   }
 
+  static void RegisterAsmStreamer(Target &T, Target::AsmStreamerCtorTy Fn) {
+    T.AsmStreamerCtorFn = Fn;
+  }
+
   static void RegisterAsmTargetStreamer(Target &T,
                                         Target::AsmTargetStreamerCtorTy Fn) {
     T.AsmTargetStreamerCtorFn = Fn;
diff --git a/llvm/lib/MC/TargetRegistry.cpp b/llvm/lib/MC/TargetRegistry.cpp
index 3be6f1d4634990..0d343a2c4e4d5f 100644
--- a/llvm/lib/MC/TargetRegistry.cpp
+++ b/llvm/lib/MC/TargetRegistry.cpp
@@ -93,8 +93,14 @@ MCStreamer *Target::createAsmStreamer(MCContext &Ctx,
                                       std::unique_ptr<MCCodeEmitter> CE,
                                       std::unique_ptr<MCAsmBackend> TAB) const {
   formatted_raw_ostream &OSRef = *OS;
-  MCStreamer *S = llvm::createAsmStreamer(Ctx, std::move(OS), IP,
-                                          std::move(CE), std::move(TAB));
+  MCStreamer *S;
+  if (AsmStreamerCtorFn)
+    S = AsmStreamerCtorFn(Ctx, std::move(OS), IP, std::move(CE),
+                          std::move(TAB));
+  else
+    S = llvm::createAsmStreamer(Ctx, std::move(OS), IP, std::move(CE),
+                                std::move(TAB));
+
   createAsmTargetStreamer(*S, OSRef, IP);
   return S;
 }

@tltao
Copy link
Contributor Author

tltao commented Sep 5, 2024

Copy link
Contributor

@abhina-sree abhina-sree left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, assuming testcases that traverse this code path will be added in the future PRs

@tltao
Copy link
Contributor Author

tltao commented Sep 6, 2024

LGTM, assuming testcases that traverse this code path will be added in the future PRs

Yeah I didn't include any test case because the behaviour in this PR is identical to the current behaviour, and I don't see a way to test anything else with the existing changes.

@tltao
Copy link
Contributor Author

tltao commented Sep 12, 2024

@iliya-diyachkov @lhames FYI

Also added use case in #108433

@tltao
Copy link
Contributor Author

tltao commented Mar 12, 2025

Closing this in favour of #130535.

@tltao tltao closed this Mar 12, 2025
tltao added a commit that referenced this pull request Mar 21, 2025
)

A more fleshed out version of a previous PR
#107415. The goal is to provide
platforms an alternative to the current MCAsmStreamer which only
supports the GNU Asm syntax.

RFC:
https://discourse.llvm.org/t/rfc-llvm-add-support-for-target-specific-asm-streamer/85095

---------

Co-authored-by: Tony Tao <[email protected]>
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Mar 21, 2025
… z/OS (#130535)

A more fleshed out version of a previous PR
llvm/llvm-project#107415. The goal is to provide
platforms an alternative to the current MCAsmStreamer which only
supports the GNU Asm syntax.

RFC:
https://discourse.llvm.org/t/rfc-llvm-add-support-for-target-specific-asm-streamer/85095

---------

Co-authored-by: Tony Tao <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:mc Machine (object) code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants