Skip to content
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

[M68k] implement move to and from sr #111145

Merged
merged 3 commits into from
Jan 9, 2025
Merged

Conversation

TechnoElf
Copy link
Contributor

This PR enables the use of the status register in inline assembly.
This is necessary to, for example, set and retrieve the current interrupt mask.

Copy link

github-actions bot commented Oct 4, 2024

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
Copy link
Member

llvmbot commented Oct 4, 2024

@llvm/pr-subscribers-mc

@llvm/pr-subscribers-backend-m68k

Author: Janis Heims (TechnoElf)

Changes

This PR enables the use of the status register in inline assembly.
This is necessary to, for example, set and retrieve the current interrupt mask.


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

4 Files Affected:

  • (modified) llvm/lib/Target/M68k/AsmParser/M68kAsmParser.cpp (+5-1)
  • (modified) llvm/lib/Target/M68k/Disassembler/M68kDisassembler.cpp (+6)
  • (modified) llvm/lib/Target/M68k/M68kInstrData.td (+61-1)
  • (modified) llvm/test/CodeGen/M68k/inline-asm.ll (+23)
diff --git a/llvm/lib/Target/M68k/AsmParser/M68kAsmParser.cpp b/llvm/lib/Target/M68k/AsmParser/M68kAsmParser.cpp
index 126176133dc027..31583eba658462 100644
--- a/llvm/lib/Target/M68k/AsmParser/M68kAsmParser.cpp
+++ b/llvm/lib/Target/M68k/AsmParser/M68kAsmParser.cpp
@@ -258,6 +258,7 @@ static inline unsigned getRegisterIndex(unsigned Register) {
   // We don't care about the indices of these registers.
   case M68k::PC:
   case M68k::CCR:
+  case M68k::SR:
   case M68k::FPC:
   case M68k::FPS:
   case M68k::FPIAR:
@@ -636,10 +637,13 @@ bool M68kAsmParser::parseRegisterName(MCRegister &RegNo, SMLoc Loc,
                                       StringRef RegisterName) {
   auto RegisterNameLower = RegisterName.lower();
 
-  // CCR register
+  // CCR and SR register
   if (RegisterNameLower == "ccr") {
     RegNo = M68k::CCR;
     return true;
+  } else if (RegisterNameLower == "sr") {
+    RegNo = M68k::SR;
+    return true;
   }
 
   // Parse simple general-purpose registers.
diff --git a/llvm/lib/Target/M68k/Disassembler/M68kDisassembler.cpp b/llvm/lib/Target/M68k/Disassembler/M68kDisassembler.cpp
index 7f0f737faccd0d..43120d8cdef1ee 100644
--- a/llvm/lib/Target/M68k/Disassembler/M68kDisassembler.cpp
+++ b/llvm/lib/Target/M68k/Disassembler/M68kDisassembler.cpp
@@ -111,6 +111,12 @@ static DecodeStatus DecodeCCRCRegisterClass(MCInst &Inst, APInt &Insn,
   llvm_unreachable("unimplemented");
 }
 
+static DecodeStatus DecodeSRCRegisterClass(MCInst &Inst, APInt &Insn,
+                                           uint64_t Address,
+                                           const void *Decoder) {
+  llvm_unreachable("unimplemented");
+}
+
 static DecodeStatus DecodeImm32(MCInst &Inst, uint64_t Imm, uint64_t Address,
                                 const void *Decoder) {
   Inst.addOperand(MCOperand::createImm(M68k::swapWord<uint32_t>(Imm)));
diff --git a/llvm/lib/Target/M68k/M68kInstrData.td b/llvm/lib/Target/M68k/M68kInstrData.td
index dc777a933e2786..5f5cbfbc55ce15 100644
--- a/llvm/lib/Target/M68k/M68kInstrData.td
+++ b/llvm/lib/Target/M68k/M68kInstrData.td
@@ -365,13 +365,14 @@ def MOVM32mp_P : MxMOVEM_RM_Pseudo<MxType32r, MxType32.POp>;
 // ons that will be resolved sometime after RA pass.
 //===----------------------------------------------------------------------===//
 
+/// Move to CCR
 /// --------------------------------------------------
 ///  F  E  D  C  B  A  9  8  7  6 | 5  4  3 | 2  1  0
 /// --------------------------------------------------
 ///                               | EFFECTIVE ADDRESS
 ///  0  1  0  0  0  1  0  0  1  1 |   MODE  |   REG
 /// --------------------------------------------------
-let Defs = [CCR] in
+let Defs = [CCR] in {
 class MxMoveToCCR<MxOperand MEMOp, MxEncMemOp SRC_ENC>
     : MxInst<(outs CCRC:$dst), (ins MEMOp:$src), "move.w\t$src, $dst", []> {
   let Inst = (ascend
@@ -382,6 +383,7 @@ class MxMoveToCCR<MxOperand MEMOp, MxEncMemOp SRC_ENC>
 
 class MxMoveToCCRPseudo<MxOperand MEMOp>
     : MxPseudo<(outs CCRC:$dst), (ins MEMOp:$src)>;
+} // let Defs = [CCR]
 
 let mayLoad = 1 in
 foreach AM = MxMoveSupportedAMs in {
@@ -434,6 +436,64 @@ foreach AM = MxMoveSupportedAMs in {
 def MOV16dc : MxMoveFromCCR_R;
 def MOV8dc  : MxMoveFromCCRPseudo<MxOp8AddrMode_d.Op>;
 
+/// Move to SR
+/// --------------------------------------------------
+///  F  E  D  C  B  A  9  8  7  6 | 5  4  3 | 2  1  0
+/// --------------------------------------------------
+///                               | EFFECTIVE ADDRESS
+///  0  1  0  0  0  1  1  0  1  1 |   MODE  |   REG
+/// --------------------------------------------------
+let Defs = [SR] in {
+class MxMoveToSR<MxOperand MEMOp, MxEncMemOp SRC_ENC>
+    : MxInst<(outs SRC:$dst), (ins MEMOp:$src), "move.w\t$src, $dst", []> {
+  let Inst = (ascend
+    (descend 0b0100011011, SRC_ENC.EA),
+    SRC_ENC.Supplement
+  );
+}
+} // let Defs = [SR]
+
+let mayLoad = 1 in
+foreach AM = MxMoveSupportedAMs in {
+  def MOV16s # AM : MxMoveToSR<!cast<MxOpBundle>("MxOp16AddrMode_"#AM).Op,
+                                !cast<MxEncMemOp>("MxMoveSrcOpEnc_"#AM)>;
+} // foreach AM
+
+def MOV16sd : MxMoveToSR<MxOp16AddrMode_d.Op, MxMoveSrcOpEnc_d>;
+
+/// Move from SR
+/// --------------------------------------------------
+///  F  E  D  C  B  A  9  8  7  6 | 5  4  3 | 2  1  0
+/// --------------------------------------------------
+///                               | EFFECTIVE ADDRESS
+///  0  1  0  0  0  0  0  0  1  1 |   MODE  |   REG
+/// --------------------------------------------------
+let Uses = [SR] in {
+class MxMoveFromSR_R
+    : MxInst<(outs MxDRD16:$dst), (ins SRC:$src), "move.w\t$src, $dst", []>,
+      Requires<[ AtLeastM68010 ]> {
+  let Inst = (descend 0b0100000011, MxEncAddrMode_d<"dst">.EA);
+}
+
+class MxMoveFromSR_M<MxOperand MEMOp, MxEncMemOp DST_ENC>
+    : MxInst<(outs), (ins MEMOp:$dst, SRC:$src), "move.w\t$src, $dst", []>,
+      Requires<[ AtLeastM68010 ]> {
+  let Inst = (ascend
+    (descend 0b0100000011, DST_ENC.EA),
+    DST_ENC.Supplement
+  );
+}
+} // let Uses = [SR]
+
+let mayStore = 1 in
+foreach AM = MxMoveSupportedAMs in {
+  def MOV16 # AM # s
+    : MxMoveFromSR_M<!cast<MxOpBundle>("MxOp16AddrMode_"#AM).Op,
+                      !cast<MxEncMemOp>("MxMoveDstOpEnc_"#AM)>;
+} // foreach AM
+
+def MOV16ds : MxMoveFromSR_R;
+
 //===----------------------------------------------------------------------===//
 // LEA
 //===----------------------------------------------------------------------===//
diff --git a/llvm/test/CodeGen/M68k/inline-asm.ll b/llvm/test/CodeGen/M68k/inline-asm.ll
index dda943920788d4..70e2d16ac1844b 100644
--- a/llvm/test/CodeGen/M68k/inline-asm.ll
+++ b/llvm/test/CodeGen/M68k/inline-asm.ll
@@ -152,3 +152,26 @@ entry:
   ret void
 }
 
+define void @move_sr_ccr() {
+; CHECK-LABEL: move_sr_ccr:
+; CHECK:         .cfi_startproc
+; CHECK-NEXT:  ; %bb.0:
+; CHECK-NEXT:    ;APP
+; CHECK-NEXT:    move.w %sr, %d0
+; CHECK-NEXT:    ;NO_APP
+; CHECK-NEXT:    ;APP
+; CHECK-NEXT:    move.w %d0, %sr
+; CHECK-NEXT:    ;NO_APP
+; CHECK-NEXT:    ;APP
+; CHECK-NEXT:    move.w %ccr, %d0
+; CHECK-NEXT:    ;NO_APP
+; CHECK-NEXT:    ;APP
+; CHECK-NEXT:    move.w %d0, %ccr
+; CHECK-NEXT:    ;NO_APP
+; CHECK-NEXT:    rts
+  %1 = call i16 asm sideeffect "move.w %sr, $0", "=r"()
+  call void asm sideeffect "move.w $0, %sr", "r"(i16 %1)
+  %2 = call i16 asm sideeffect "move.w %ccr, $0", "=r"()
+  call void asm sideeffect "move.w $0, %ccr", "r"(i16 %2)
+  ret void
+}

@TechnoElf
Copy link
Contributor Author

Ping @0x59616e @mshockwave

@0x59616e 0x59616e self-requested a review October 4, 2024 13:44
Copy link
Member

@mshockwave mshockwave left a comment

Choose a reason for hiding this comment

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

Thank you for submitting the patch. I only have a minor comment

llvm/test/CodeGen/M68k/inline-asm.ll Outdated Show resolved Hide resolved
@knickish
Copy link
Contributor

@TechnoElf I just ran in to the need for this, are you planning to revisit? Happy to add more tests on top of your commit to get it in if you don't feel like it

@TechnoElf
Copy link
Contributor Author

@knickish Yeah, sorry - my priorities at work got shifted around a bit, so I haven't had the time to look into the tests further. I would definitely like to finish this up though. Currently I'm stuck on trying to figure out how to write an MC test. If you have any pointers to docs or examples, I could probably throw something together quickly.

@knickish
Copy link
Contributor

@knickish Yeah, sorry - my priorities at work got shifted around a bit, so I haven't had the time to look into the tests further. I would definitely like to finish this up though. Currently I'm stuck on trying to figure out how to write an MC test. If you have any pointers to docs or examples, I could probably throw something together quickly.

Understood, will make a PR against your fork then with a test if I can figure it out, and can do what you like with it

@llvmbot llvmbot added the mc Machine (object) code label Jan 2, 2025
@TechnoElf TechnoElf force-pushed the m68k-sr-move branch 2 times, most recently from c147748 to 5e166f7 Compare January 2, 2025 13:15
@TechnoElf
Copy link
Contributor Author

I've merged your test @knickish. Thanks a bunch!
@mshockwave this should be ready to review now

example MC test for MOVE to/from SR
Copy link
Member

@mshockwave mshockwave left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@knickish knickish merged commit a0d7749 into llvm:main Jan 9, 2025
8 checks passed
Copy link

github-actions bot commented Jan 9, 2025

@TechnoElf Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@llvm-ci
Copy link
Collaborator

llvm-ci commented Jan 9, 2025

LLVM Buildbot has detected a new failure on builder clang-m68k-linux-cross running on suse-gary-m68k-cross while building llvm at step 5 "ninja check 1".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/27/builds/4380

Here is the relevant piece of the build log for the reference
Step 5 (ninja check 1) failure: stage 1 checked (failure)
******************** TEST 'LLVM :: MC/M68k/Data/Classes/MxMoveSR.s' FAILED ********************
Exit Code: 1

Command Output (stderr):
--
RUN: at line 1: /var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/stage1/bin/llvm-mc -triple=m68k -mcpu=M68000 -show-encoding /var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/llvm/test/MC/M68k/Data/Classes/MxMoveSR.s | /var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/stage1/bin/FileCheck /var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/llvm/test/MC/M68k/Data/Classes/MxMoveSR.s
+ /var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/stage1/bin/llvm-mc -triple=m68k -mcpu=M68000 -show-encoding /var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/llvm/test/MC/M68k/Data/Classes/MxMoveSR.s
+ /var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/stage1/bin/FileCheck /var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/llvm/test/MC/M68k/Data/Classes/MxMoveSR.s
/var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/llvm/test/MC/M68k/Data/Classes/MxMoveSR.s:8:15: error: CHECK-SAME: expected string not found in input
; CHECK-SAME: encoding: [0x40,0xc1]
              ^
<stdin>:4:17: note: scanning from here
 move.w %sr, %d1 ; encoding: [0x32,0x00]
                ^
<stdin>:4:20: note: possible intended match here
 move.w %sr, %d1 ; encoding: [0x32,0x00]
                   ^

Input file: <stdin>
Check file: /var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/llvm/test/MC/M68k/Data/Classes/MxMoveSR.s

-dump-input=help explains the following input dump.

Input was:
<<<<<<
          1:  
          2:  move.w %d1, %sr ; encoding: [0x46,0xc1] 
          3:  
          4:  move.w %sr, %d1 ; encoding: [0x32,0x00] 
same:8'0                     X~~~~~~~~~~~~~~~~~~~~~~~~ error: no match found
same:8'1                        ?                      possible intended match
>>>>>>

--

********************


@TechnoElf
Copy link
Contributor Author

TechnoElf commented Jan 9, 2025

I'm currently looking into this error ^. [0x40, 0xc1] is the correct encoding for a move from SR (PRM page 229) and is also what I got from my local build.

@llvm-ci
Copy link
Collaborator

llvm-ci commented Jan 9, 2025

LLVM Buildbot has detected a new failure on builder arc-builder running on arc-worker while building llvm at step 6 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/3/builds/10098

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-all) failure: 1200 seconds without output running [b'ninja', b'check-all'], attempting to kill
...
9.042 [39/18/21] Linking CXX executable unittests/Bitcode/BitcodeTests
9.498 [38/18/22] Linking CXX executable tools/clang/unittests/Driver/ClangDriverTests
9.738 [37/18/23] Linking CXX executable unittests/DebugInfo/BTF/DebugInfoBTFTests
10.443 [36/18/24] Linking CXX executable unittests/DebugInfo/GSYM/DebugInfoGSYMTests
11.556 [35/18/25] Linking CXX executable tools/clang/unittests/Tooling/ToolingTests
11.721 [34/18/26] Linking CXX executable unittests/DebugInfo/PDB/DebugInfoPDBTests
11.865 [33/18/27] Linking CXX executable unittests/DebugInfo/Symbolizer/DebugInfoSymbolizerTests
12.123 [32/18/28] Linking CXX executable unittests/DWARFLinkerParallel/DWARFLinkerParallelTests
12.302 [31/18/29] Linking CXX executable tools/clang/unittests/Analysis/ClangAnalysisTests
12.512 [30/18/30] Linking CXX executable unittests/Debuginfod/DebuginfodTests
command timed out: 1200 seconds without output running [b'ninja', b'check-all'], attempting to kill
process killed by signal 9
program finished with exit code -1
elapsedTime=1213.586252

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

Successfully merging this pull request may close these issues.

5 participants