[llvm] 0365c54 - [CSKY] Add CSKYTargetObjectFile to support exception handling

Zi Xuan Wu via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 29 01:06:31 PDT 2022


Author: Zi Xuan Wu
Date: 2022-03-29T16:05:30+08:00
New Revision: 0365c54ca393eee2250448dd5834a7e10d306d34

URL: https://github.com/llvm/llvm-project/commit/0365c54ca393eee2250448dd5834a7e10d306d34
DIFF: https://github.com/llvm/llvm-project/commit/0365c54ca393eee2250448dd5834a7e10d306d34.diff

LOG: [CSKY] Add CSKYTargetObjectFile to support exception handling

Initialize TargetLoweringObjectFileELF and EH header.

Added: 
    llvm/lib/Target/CSKY/CSKYTargetObjectFile.cpp
    llvm/lib/Target/CSKY/CSKYTargetObjectFile.h
    llvm/test/CodeGen/CSKY/dwarf-eh.ll

Modified: 
    llvm/lib/Target/CSKY/CMakeLists.txt
    llvm/lib/Target/CSKY/CSKYTargetMachine.cpp
    llvm/lib/Target/CSKY/MCTargetDesc/CSKYAsmBackend.cpp
    llvm/lib/Target/CSKY/MCTargetDesc/CSKYELFObjectWriter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/CSKY/CMakeLists.txt b/llvm/lib/Target/CSKY/CMakeLists.txt
index 8af00e97fbbb3..7f04cc4a21fe1 100644
--- a/llvm/lib/Target/CSKY/CMakeLists.txt
+++ b/llvm/lib/Target/CSKY/CMakeLists.txt
@@ -28,6 +28,7 @@ add_llvm_target(CSKYCodeGen
   CSKYRegisterInfo.cpp
   CSKYSubtarget.cpp
   CSKYTargetMachine.cpp
+  CSKYTargetObjectFile.cpp
 
   LINK_COMPONENTS
   Analysis

diff  --git a/llvm/lib/Target/CSKY/CSKYTargetMachine.cpp b/llvm/lib/Target/CSKY/CSKYTargetMachine.cpp
index a2d2db3be0a9d..3f84641ecd0c2 100644
--- a/llvm/lib/Target/CSKY/CSKYTargetMachine.cpp
+++ b/llvm/lib/Target/CSKY/CSKYTargetMachine.cpp
@@ -13,6 +13,7 @@
 #include "CSKYTargetMachine.h"
 #include "CSKY.h"
 #include "CSKYSubtarget.h"
+#include "CSKYTargetObjectFile.h"
 #include "TargetInfo/CSKYTargetInfo.h"
 #include "llvm/CodeGen/MachineFrameInfo.h"
 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
@@ -53,7 +54,7 @@ CSKYTargetMachine::CSKYTargetMachine(const Target &T, const Triple &TT,
     : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,
                         RM.getValueOr(Reloc::Static),
                         getEffectiveCodeModel(CM, CodeModel::Small), OL),
-      TLOF(std::make_unique<TargetLoweringObjectFileELF>()) {
+      TLOF(std::make_unique<CSKYELFTargetObjectFile>()) {
   initAsmInfo();
 }
 

diff  --git a/llvm/lib/Target/CSKY/CSKYTargetObjectFile.cpp b/llvm/lib/Target/CSKY/CSKYTargetObjectFile.cpp
new file mode 100644
index 0000000000000..b5592d34ca54b
--- /dev/null
+++ b/llvm/lib/Target/CSKY/CSKYTargetObjectFile.cpp
@@ -0,0 +1,25 @@
+//===-- CSKYTargetObjectFile.h - CSKY Object Info -*- C++ ---------------*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "CSKYTargetObjectFile.h"
+#include "CSKYTargetMachine.h"
+#include "llvm/BinaryFormat/Dwarf.h"
+#include "llvm/CodeGen/MachineFrameInfo.h"
+
+using namespace llvm;
+
+void CSKYELFTargetObjectFile::Initialize(MCContext &Ctx,
+                                         const TargetMachine &TM) {
+  TargetLoweringObjectFileELF::Initialize(Ctx, TM);
+
+  LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
+  PersonalityEncoding =
+      dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
+  TTypeEncoding =
+      dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
+}

diff  --git a/llvm/lib/Target/CSKY/CSKYTargetObjectFile.h b/llvm/lib/Target/CSKY/CSKYTargetObjectFile.h
new file mode 100644
index 0000000000000..a82f2681c12ae
--- /dev/null
+++ b/llvm/lib/Target/CSKY/CSKYTargetObjectFile.h
@@ -0,0 +1,24 @@
+//===-- CSKYTargetObjectFile.h - CSKY Object Info -*- C++ ---------------*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_CSKY_CSKYTARGETOBJECTFILE_H
+#define LLVM_LIB_TARGET_CSKY_CSKYTARGETOBJECTFILE_H
+
+#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
+#include "llvm/Target/TargetLoweringObjectFile.h"
+
+namespace llvm {
+
+class CSKYELFTargetObjectFile : public TargetLoweringObjectFileELF {
+public:
+  void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
+};
+
+} // namespace llvm
+
+#endif // LLVM_LIB_TARGET_CSKY_CSKYTARGETOBJECTFILE_H

diff  --git a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYAsmBackend.cpp b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYAsmBackend.cpp
index 215b453739a3e..41da382df3e00 100644
--- a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYAsmBackend.cpp
+++ b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYAsmBackend.cpp
@@ -224,8 +224,9 @@ void CSKYAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
   // For each byte of the fragment that the fixup touches, mask in the
   // bits from the fixup value.
   bool IsLittleEndian = (Endian == support::little);
+  bool IsInstFixup = (Kind >= FirstTargetFixupKind);
 
-  if (IsLittleEndian && (NumBytes == 4)) {
+  if (IsLittleEndian && IsInstFixup && (NumBytes == 4)) {
     Data[Offset + 0] |= uint8_t((Value >> 16) & 0xff);
     Data[Offset + 1] |= uint8_t((Value >> 24) & 0xff);
     Data[Offset + 2] |= uint8_t(Value & 0xff);

diff  --git a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYELFObjectWriter.cpp b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYELFObjectWriter.cpp
index 321a4145b207b..a07e6282cf8f1 100644
--- a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYELFObjectWriter.cpp
+++ b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYELFObjectWriter.cpp
@@ -7,8 +7,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "CSKYFixupKinds.h"
-#include "CSKYMCTargetDesc.h"
 #include "CSKYMCExpr.h"
+#include "CSKYMCTargetDesc.h"
 #include "llvm/MC/MCContext.h"
 #include "llvm/MC/MCELFObjectWriter.h"
 #include "llvm/MC/MCObjectWriter.h"

diff  --git a/llvm/test/CodeGen/CSKY/dwarf-eh.ll b/llvm/test/CodeGen/CSKY/dwarf-eh.ll
new file mode 100644
index 0000000000000..706db07c3036c
--- /dev/null
+++ b/llvm/test/CodeGen/CSKY/dwarf-eh.ll
@@ -0,0 +1,193 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc -mtriple=csky -csky-no-aliases --code-model=small -mattr=+2e3 < %s \
+; RUN:     | FileCheck -check-prefixes=SMALL,CHECK %s
+; RUN: llc -mtriple=csky -csky-no-aliases --code-model=medium -mattr=+2e3 < %s \
+; RUN:     | FileCheck -check-prefixes=MEDIUM,CHECK %s
+; RUN: llc -mtriple=csky -csky-no-aliases --code-model=small  -relocation-model=pic -mattr=+2e3 < %s \
+; RUN:     | FileCheck -check-prefixes=SMALL-PIC,CHECK %s
+; RUN: llc -mtriple=csky -csky-no-aliases --code-model=medium -relocation-model=pic -mattr=+2e3 < %s \
+; RUN:     | FileCheck -check-prefixes=MEDIUM-PIC,CHECK %s
+
+declare void @throw_exception()
+
+declare i32 @__gxx_personality_v0(...)
+
+declare i8* @__cxa_begin_catch(i8*)
+
+declare void @__cxa_end_catch()
+
+; CHECK: .cfi_startproc
+; PersonalityEncoding = DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4
+; CHECK-NEXT:	.cfi_personality 155, DW.ref.__gxx_personality_v0
+; LSDAEncoding = DW_EH_PE_pcrel | DW_EH_PE_sdata4
+; CHECK-NEXT:	.cfi_lsda 27, .Lexception0
+
+define void @test1() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
+; SMALL:       # %bb.0: # %entry
+; SMALL-NEXT:    subi16 sp, sp, 4
+; SMALL-NEXT:    .cfi_def_cfa_offset 4
+; SMALL-NEXT:    st32.w lr, (sp, 0) # 4-byte Folded Spill
+; SMALL-NEXT:    .cfi_offset lr, -4
+; SMALL-NEXT:    .cfi_def_cfa_offset 4
+; SMALL-NEXT:  .Ltmp0:
+; SMALL-NEXT:    jsri32 [.LCPI0_0]
+; SMALL-NEXT:  .Ltmp1:
+; SMALL-NEXT:  .LBB0_1: # %try.cont
+; SMALL-NEXT:    ld32.w lr, (sp, 0) # 4-byte Folded Reload
+; SMALL-NEXT:    addi16 sp, sp, 4
+; SMALL-NEXT:    rts16
+; SMALL-NEXT:  .LBB0_2: # %lpad
+; SMALL-NEXT:  .Ltmp2:
+; SMALL-NEXT:    jsri32 [.LCPI0_1]
+; SMALL-NEXT:    jsri32 [.LCPI0_2]
+; SMALL-NEXT:    br32 .LBB0_1
+; SMALL-NEXT:    .p2align 1
+; SMALL-NEXT:  # %bb.3:
+; SMALL-NEXT:    .p2align 2
+; SMALL-NEXT:  .LCPI0_0:
+; SMALL-NEXT:    .long throw_exception
+; SMALL-NEXT:  .LCPI0_1:
+; SMALL-NEXT:    .long __cxa_begin_catch
+; SMALL-NEXT:  .LCPI0_2:
+; SMALL-NEXT:    .long __cxa_end_catch
+;
+; MEDIUM:       # %bb.0: # %entry
+; MEDIUM-NEXT:    subi16 sp, sp, 4
+; MEDIUM-NEXT:    .cfi_def_cfa_offset 4
+; MEDIUM-NEXT:    st32.w lr, (sp, 0) # 4-byte Folded Spill
+; MEDIUM-NEXT:    .cfi_offset lr, -4
+; MEDIUM-NEXT:    .cfi_def_cfa_offset 4
+; MEDIUM-NEXT:  .Ltmp0:
+; MEDIUM-NEXT:    jsri32 [.LCPI0_0]
+; MEDIUM-NEXT:  .Ltmp1:
+; MEDIUM-NEXT:  .LBB0_1: # %try.cont
+; MEDIUM-NEXT:    ld32.w lr, (sp, 0) # 4-byte Folded Reload
+; MEDIUM-NEXT:    addi16 sp, sp, 4
+; MEDIUM-NEXT:    rts16
+; MEDIUM-NEXT:  .LBB0_2: # %lpad
+; MEDIUM-NEXT:  .Ltmp2:
+; MEDIUM-NEXT:    jsri32 [.LCPI0_1]
+; MEDIUM-NEXT:    jsri32 [.LCPI0_2]
+; MEDIUM-NEXT:    br32 .LBB0_1
+; MEDIUM-NEXT:    .p2align 1
+; MEDIUM-NEXT:  # %bb.3:
+; MEDIUM-NEXT:    .p2align 2
+; MEDIUM-NEXT:  .LCPI0_0:
+; MEDIUM-NEXT:    .long throw_exception
+; MEDIUM-NEXT:  .LCPI0_1:
+; MEDIUM-NEXT:    .long __cxa_begin_catch
+; MEDIUM-NEXT:  .LCPI0_2:
+; MEDIUM-NEXT:    .long __cxa_end_catch
+;
+; SMALL-PIC:       # %bb.0: # %entry
+; SMALL-PIC-NEXT:    subi16 sp, sp, 8
+; SMALL-PIC-NEXT:    .cfi_def_cfa_offset 8
+; SMALL-PIC-NEXT:    st32.w rgb, (sp, 4) # 4-byte Folded Spill
+; SMALL-PIC-NEXT:    st32.w lr, (sp, 0) # 4-byte Folded Spill
+; SMALL-PIC-NEXT:    .cfi_offset rgb, -4
+; SMALL-PIC-NEXT:    .cfi_offset lr, -8
+; SMALL-PIC-NEXT:    .cfi_def_cfa_offset 8
+; SMALL-PIC-NEXT:    lrw32 rgb, [.LCPI0_0]
+; SMALL-PIC-NEXT:  .Ltmp0:
+; SMALL-PIC-NEXT:    lrw32 a0, [.LCPI0_1]
+; SMALL-PIC-NEXT:    ldr32.w a0, (rgb, a0 << 0)
+; SMALL-PIC-NEXT:    jsr16 a0
+; SMALL-PIC-NEXT:  .Ltmp1:
+; SMALL-PIC-NEXT:  .LBB0_1: # %try.cont
+; SMALL-PIC-NEXT:    ld32.w lr, (sp, 0) # 4-byte Folded Reload
+; SMALL-PIC-NEXT:    ld32.w rgb, (sp, 4) # 4-byte Folded Reload
+; SMALL-PIC-NEXT:    addi16 sp, sp, 8
+; SMALL-PIC-NEXT:    rts16
+; SMALL-PIC-NEXT:  .LBB0_2: # %lpad
+; SMALL-PIC-NEXT:  .Ltmp2:
+; SMALL-PIC-NEXT:    lrw32 a1, [.LCPI0_2]
+; SMALL-PIC-NEXT:    ldr32.w a1, (rgb, a1 << 0)
+; SMALL-PIC-NEXT:    jsr16 a1
+; SMALL-PIC-NEXT:    lrw32 a0, [.LCPI0_3]
+; SMALL-PIC-NEXT:    ldr32.w a0, (rgb, a0 << 0)
+; SMALL-PIC-NEXT:    jsr16 a0
+; SMALL-PIC-NEXT:    br32 .LBB0_1
+; SMALL-PIC-NEXT:    .p2align 1
+; SMALL-PIC-NEXT:  # %bb.3:
+; SMALL-PIC-NEXT:    .p2align 2
+; SMALL-PIC-NEXT:  .LCPI0_0:
+; SMALL-PIC-NEXT:    .long _GLOBAL_OFFSET_TABLE_
+; SMALL-PIC-NEXT:  .LCPI0_1:
+; SMALL-PIC-NEXT:    .long throw_exception at PLT
+; SMALL-PIC-NEXT:  .LCPI0_2:
+; SMALL-PIC-NEXT:    .long __cxa_begin_catch at PLT
+; SMALL-PIC-NEXT:  .LCPI0_3:
+; SMALL-PIC-NEXT:    .long __cxa_end_catch at PLT
+;
+; MEDIUM-PIC:       # %bb.0: # %entry
+; MEDIUM-PIC-NEXT:    subi16 sp, sp, 8
+; MEDIUM-PIC-NEXT:    .cfi_def_cfa_offset 8
+; MEDIUM-PIC-NEXT:    st32.w rgb, (sp, 4) # 4-byte Folded Spill
+; MEDIUM-PIC-NEXT:    st32.w lr, (sp, 0) # 4-byte Folded Spill
+; MEDIUM-PIC-NEXT:    .cfi_offset rgb, -4
+; MEDIUM-PIC-NEXT:    .cfi_offset lr, -8
+; MEDIUM-PIC-NEXT:    .cfi_def_cfa_offset 8
+; MEDIUM-PIC-NEXT:    lrw32 rgb, [.LCPI0_0]
+; MEDIUM-PIC-NEXT:  .Ltmp0:
+; MEDIUM-PIC-NEXT:    lrw32 a0, [.LCPI0_1]
+; MEDIUM-PIC-NEXT:    ldr32.w a0, (rgb, a0 << 0)
+; MEDIUM-PIC-NEXT:    jsr16 a0
+; MEDIUM-PIC-NEXT:  .Ltmp1:
+; MEDIUM-PIC-NEXT:  .LBB0_1: # %try.cont
+; MEDIUM-PIC-NEXT:    ld32.w lr, (sp, 0) # 4-byte Folded Reload
+; MEDIUM-PIC-NEXT:    ld32.w rgb, (sp, 4) # 4-byte Folded Reload
+; MEDIUM-PIC-NEXT:    addi16 sp, sp, 8
+; MEDIUM-PIC-NEXT:    rts16
+; MEDIUM-PIC-NEXT:  .LBB0_2: # %lpad
+; MEDIUM-PIC-NEXT:  .Ltmp2:
+; MEDIUM-PIC-NEXT:    lrw32 a1, [.LCPI0_2]
+; MEDIUM-PIC-NEXT:    ldr32.w a1, (rgb, a1 << 0)
+; MEDIUM-PIC-NEXT:    jsr16 a1
+; MEDIUM-PIC-NEXT:    lrw32 a0, [.LCPI0_3]
+; MEDIUM-PIC-NEXT:    ldr32.w a0, (rgb, a0 << 0)
+; MEDIUM-PIC-NEXT:    jsr16 a0
+; MEDIUM-PIC-NEXT:    br32 .LBB0_1
+; MEDIUM-PIC-NEXT:    .p2align 1
+; MEDIUM-PIC-NEXT:  # %bb.3:
+; MEDIUM-PIC-NEXT:    .p2align 2
+; MEDIUM-PIC-NEXT:  .LCPI0_0:
+; MEDIUM-PIC-NEXT:    .long _GLOBAL_OFFSET_TABLE_
+; MEDIUM-PIC-NEXT:  .LCPI0_1:
+; MEDIUM-PIC-NEXT:    .long throw_exception at PLT
+; MEDIUM-PIC-NEXT:  .LCPI0_2:
+; MEDIUM-PIC-NEXT:    .long __cxa_begin_catch at PLT
+; MEDIUM-PIC-NEXT:  .LCPI0_3:
+; MEDIUM-PIC-NEXT:    .long __cxa_end_catch at PLT
+entry:
+  invoke void @throw_exception() to label %try.cont unwind label %lpad
+
+lpad:
+  %0 = landingpad { i8*, i32 }
+          catch i8* null
+  %1 = extractvalue { i8*, i32 } %0, 0
+  %2 = tail call i8* @__cxa_begin_catch(i8* %1)
+  tail call void @__cxa_end_catch()
+  br label %try.cont
+
+try.cont:
+  ret void
+}
+
+; CHECK-LABEL: GCC_except_table0:
+; CHECK-NEXT: .Lexception0:
+; CHECK-NEXT: .byte	255 # @LPStart Encoding = omit
+; TTypeEncoding = DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4
+; CHECK-NEXT: .byte 155 # @TType Encoding = indirect pcrel sdata4
+; CHECK: .Lttbaseref0:
+; CallSiteEncoding = dwarf::DW_EH_PE_udata4
+; CHECK-NEXT: .byte	1                       # Call site Encoding = uleb128
+; CHECK-NEXT: .uleb128 .Lcst_end0-.Lcst_begin0
+; CHECK-NEXT: .Lcst_begin0:
+; CHECK-NEXT: .uleb128 .Ltmp0-.Lfunc_begin0   # >> Call Site 1 <<
+; CHECK-NEXT: .uleb128 .Ltmp1-.Ltmp0          #   Call between .Ltmp0 and .Ltmp1
+; CHECK-NEXT: .uleb128 .Ltmp2-.Lfunc_begin0   #     jumps to .Ltmp2
+; CHECK-NEXT: .byte	1                       #   On action: 1
+; CHECK-NEXT: .uleb128 .Ltmp1-.Lfunc_begin0   # >> Call Site 2 <<
+; CHECK-NEXT: .uleb128 .Lfunc_end0-.Ltmp1     #   Call between .Ltmp1 and .Lfunc_end0
+; CHECK-NEXT: .byte	0                       #     has no landing pad
+; CHECK-NEXT: .byte	0                       #   On action: cleanup


        


More information about the llvm-commits mailing list