[PATCH] D145659: [clang] Add AVR specific inline assembly escaped characters

Ben Shi via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 9 00:11:47 PST 2023


benshi001 created this revision.
benshi001 added reviewers: jacquesguan, aykevl.
Herald added subscribers: Jim, dylanmckay.
Herald added a project: All.
benshi001 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Fixes https://github.com/llvm/llvm-project/issues/60204


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D145659

Files:
  clang/lib/Basic/Targets/AVR.cpp
  clang/lib/Basic/Targets/AVR.h
  clang/test/CodeGen/avr/avr-inline-asm-constraints.c


Index: clang/test/CodeGen/avr/avr-inline-asm-constraints.c
===================================================================
--- clang/test/CodeGen/avr/avr-inline-asm-constraints.c
+++ clang/test/CodeGen/avr/avr-inline-asm-constraints.c
@@ -1,5 +1,10 @@
 // REQUIRES: avr-registered-target
-// RUN: %clang_cc1 -triple avr-unknown-unknown -emit-llvm -o - %s | FileCheck %s
+// RUN: %clang_cc1 -x c -triple avr -target-cpu at90s8515 -emit-llvm -o - %s \
+// RUN:     | FileCheck --check-prefixes=CHECK,AVR25 %s
+// RUN: %clang_cc1 -x c -triple avr -target-cpu atmega328 -emit-llvm -o - %s \
+// RUN:     | FileCheck --check-prefixes=CHECK,AVR51 %s
+// RUN: %clang_cc1 -x c -triple avr -target-cpu atmega2560 -emit-llvm -o - %s \
+// RUN:     | FileCheck --check-prefixes=CHECK,AVR6 %s
 
 int data;
 
@@ -122,3 +127,23 @@
   // CHECK: call addrspace(0) i16 asm "subi r30, $0", "=ra"()
   asm("subi r30, %0" : "=ra"(data));
 }
+
+void escapeChar(void) {
+  asm("_foo:");
+  // AVR25: call addrspace(0) void asm sideeffect "rcall _foo"
+  // AVR51: call addrspace(0) void asm sideeffect "call _foo"
+  // AVR6:  call addrspace(0) void asm sideeffect "call _foo"
+  asm("%~call _foo" ::);
+  // AVR25: call addrspace(0) void asm sideeffect "rjmp _foo"
+  // AVR51: call addrspace(0) void asm sideeffect "jmp _foo"
+  // AVR6:  call addrspace(0) void asm sideeffect "jmp _foo"
+  asm("%~jmp _foo" ::);
+  // AVR25: call addrspace(0) void asm sideeffect "icall"
+  // AVR51: call addrspace(0) void asm sideeffect "icall"
+  // AVR6:  call addrspace(0) void asm sideeffect "eicall"
+  asm("%!icall" ::);
+  // AVR25: call addrspace(0) void asm sideeffect "ijmp"
+  // AVR51: call addrspace(0) void asm sideeffect "ijmp"
+  // AVR6:  call addrspace(0) void asm sideeffect "eijmp"
+  asm("%!ijmp" ::);
+}
Index: clang/lib/Basic/Targets/AVR.h
===================================================================
--- clang/lib/Basic/Targets/AVR.h
+++ clang/lib/Basic/Targets/AVR.h
@@ -170,6 +170,7 @@
   bool isValidCPUName(StringRef Name) const override;
   void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override;
   bool setCPU(const std::string &Name) override;
+  std::optional<std::string> handleAsmEscapedChar(char EscChar) const override;
   StringRef getABI() const override { return ABI; }
 
 protected:
Index: clang/lib/Basic/Targets/AVR.cpp
===================================================================
--- clang/lib/Basic/Targets/AVR.cpp
+++ clang/lib/Basic/Targets/AVR.cpp
@@ -428,6 +428,24 @@
   return false;
 }
 
+std::optional<std::string>
+AVRTargetInfo::handleAsmEscapedChar(char EscChar) const {
+  char C;
+  switch (EscChar) {
+  // "%~" represents for 'r' depends on the device has long jump/call.
+  case '~':
+    return ArchHasJMPCALL(Arch) ? std::string("") : std::string(1, 'r');
+
+  // "%!" represents for 'e' depends on the PC register size.
+  case '!':
+    return ArchHas3BytePC(Arch) ? std::string(1, 'e') : std::string("");
+
+  // This is an invalid escape character for AVR.
+  default:
+    return std::nullopt;
+  }
+}
+
 void AVRTargetInfo::getTargetDefines(const LangOptions &Opts,
                                      MacroBuilder &Builder) const {
   Builder.defineMacro("AVR");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145659.503654.patch
Type: text/x-patch
Size: 3236 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230309/5379b4ea/attachment-0001.bin>


More information about the cfe-commits mailing list