Skip to content

Commit e1c38dd

Browse files
committed
[CSKY 1/n] Add basic stub or infra of csky backend
This patch introduce files that just enough for lib/Target/CSKY to compile. Notably a basic CSKYTargetMachine and CSKYTargetInfo. Differential Revision: https://reviews.llvm.org/D88466
1 parent 2bd4730 commit e1c38dd

File tree

11 files changed

+221
-0
lines changed

11 files changed

+221
-0
lines changed

llvm/CODE_OWNERS.TXT

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,7 @@ D: llvm-objcopy (tools/llvm-objcopy)
232232
N: Martin Storsjö
233233
E: martin@martin.st
234234
D: MinGW
235+
236+
N: Zi Xuan Wu (Zeson)
237+
E: zixuan.wu@linux.alibaba.com
238+
D: C-SKY backend (lib/Target/CSKY/*)

llvm/docs/CompilerWriterInfo.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ RISC-V
102102
------
103103
* `RISC-V User-Level ISA Specification <https://riscv.org/specifications/>`_
104104

105+
C-SKY
106+
------
107+
* `C-SKY Architecture User Guide <https://github.com/c-sky/csky-doc/blob/master/CSKY%20Architecture%20user_guide.pdf>`_
108+
* `C-SKY V2 ABI <https://github.com/c-sky/csky-doc/blob/master/C-SKY_V2_CPU_Applications_Binary_Interface_Standards_Manual.pdf>`_
109+
105110
SPARC
106111
-----
107112

llvm/lib/Target/CSKY/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
add_llvm_target(CSKYCodeGen
2+
CSKYTargetMachine.cpp
3+
)
4+
5+
add_subdirectory(TargetInfo)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//===--- CSKYTargetMachine.cpp - Define TargetMachine for CSKY ------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Implements the info about CSKY target spec.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "CSKYTargetMachine.h"
14+
#include "TargetInfo/CSKYTargetInfo.h"
15+
#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
16+
#include "llvm/CodeGen/TargetPassConfig.h"
17+
#include "llvm/Support/TargetRegistry.h"
18+
19+
using namespace llvm;
20+
21+
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeCSKYTarget() {
22+
RegisterTargetMachine<CSKYTargetMachine> X(getTheCSKYTarget());
23+
}
24+
25+
static std::string computeDataLayout(const Triple &TT) {
26+
std::string Ret;
27+
28+
// Only support little endian for now.
29+
// TODO: Add support for big endian.
30+
Ret += "e";
31+
32+
// CSKY is always 32-bit target with the CSKYv2 ABI as prefer now.
33+
// It's a 4-byte aligned stack with ELF mangling only.
34+
Ret += "-m:e-S32-p:32:32-i32:32:32-i64:32:32-f32:32:32-f64:32:32-v64:32:32"
35+
"-v128:32:32-a:0:32-Fi32-n32";
36+
37+
return Ret;
38+
}
39+
40+
CSKYTargetMachine::CSKYTargetMachine(const Target &T, const Triple &TT,
41+
StringRef CPU, StringRef FS,
42+
const TargetOptions &Options,
43+
Optional<Reloc::Model> RM,
44+
Optional<CodeModel::Model> CM,
45+
CodeGenOpt::Level OL, bool JIT)
46+
: LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,
47+
!RM.hasValue() ? Reloc::Static : *RM,
48+
getEffectiveCodeModel(CM, CodeModel::Small), OL),
49+
TLOF(std::make_unique<TargetLoweringObjectFileELF>()) {
50+
initAsmInfo();
51+
}
52+
53+
namespace {
54+
class CSKYPassConfig : public TargetPassConfig {
55+
public:
56+
CSKYPassConfig(CSKYTargetMachine &TM, PassManagerBase &PM)
57+
: TargetPassConfig(TM, PM) {}
58+
59+
CSKYTargetMachine &getCSKYTargetMachine() const {
60+
return getTM<CSKYTargetMachine>();
61+
}
62+
};
63+
64+
} // namespace
65+
66+
TargetPassConfig *CSKYTargetMachine::createPassConfig(PassManagerBase &PM) {
67+
return new CSKYPassConfig(*this, PM);
68+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===--- CSKYTargetMachine.h - Define TargetMachine for CSKY ----*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file declares the CSKY specific subclass of TargetMachine.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_LIB_TARGET_CSKY_CSKYTARGETMACHINE_H
14+
#define LLVM_LIB_TARGET_CSKY_CSKYTARGETMACHINE_H
15+
16+
#include "llvm/IR/DataLayout.h"
17+
#include "llvm/Target/TargetMachine.h"
18+
19+
namespace llvm {
20+
21+
class CSKYTargetMachine : public LLVMTargetMachine {
22+
std::unique_ptr<TargetLoweringObjectFile> TLOF;
23+
24+
public:
25+
CSKYTargetMachine(const Target &T, const Triple &TT, StringRef CPU,
26+
StringRef FS, const TargetOptions &Options,
27+
Optional<Reloc::Model> RM, Optional<CodeModel::Model> CM,
28+
CodeGenOpt::Level OL, bool JIT);
29+
30+
TargetPassConfig *createPassConfig(PassManagerBase &PM) override;
31+
32+
TargetLoweringObjectFile *getObjFileLowering() const override {
33+
return TLOF.get();
34+
}
35+
};
36+
} // namespace llvm
37+
38+
#endif

llvm/lib/Target/CSKY/LLVMBuild.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
;===----- ./lib/Target/CSKY/LLVMBuild.txt ----------------------*- Conf -*--===;
2+
;
3+
; Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
; See https://llvm.org/LICENSE.txt for license information.
5+
; SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
;
7+
;===------------------------------------------------------------------------===;
8+
;
9+
; This is an LLVMBuild description file for the components in this subdirectory.
10+
;
11+
; For more information on the LLVMBuild system, please see:
12+
;
13+
; http://llvm.org/docs/LLVMBuild.html
14+
;
15+
;===------------------------------------------------------------------------===;
16+
17+
[common]
18+
subdirectories = TargetInfo
19+
20+
[component_0]
21+
type = TargetGroup
22+
name = CSKY
23+
parent = Target
24+
25+
[component_1]
26+
type = Library
27+
name = CSKYCodeGen
28+
parent = CSKY
29+
required_libraries = Core CodeGen CSKYInfo Support Target
30+
add_to_library_groups = CSKY
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
add_llvm_library(LLVMCSKYInfo
2+
CSKYTargetInfo.cpp
3+
)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===-- CSKYTargetInfo.cpp - CSKY Target Implementation -------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "TargetInfo/CSKYTargetInfo.h"
10+
#include "llvm/Support/TargetRegistry.h"
11+
using namespace llvm;
12+
13+
Target &llvm::getTheCSKYTarget() {
14+
static Target TheCSKYTarget;
15+
return TheCSKYTarget;
16+
}
17+
18+
extern "C" void LLVMInitializeCSKYTargetInfo() {
19+
RegisterTarget<Triple::csky> X(getTheCSKYTarget(), "csky", "C-SKY", "CSKY");
20+
}
21+
22+
// FIXME: Temporary stub - this function must be defined for linking
23+
// to succeed and will be called unconditionally by llc, so must be a no-op.
24+
// Remove once this function is properly implemented.
25+
extern "C" void LLVMInitializeCSKYTargetMC() {}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- CSKYTargetInfo.cpp - CSKY Target Implementation -------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIB_TARGET_CSKY_TARGETINFO_CSKYTARGETINFO_H
10+
#define LLVM_LIB_TARGET_CSKY_TARGETINFO_CSKYTARGETINFO_H
11+
12+
namespace llvm {
13+
14+
class Target;
15+
16+
Target &getTheCSKYTarget();
17+
18+
} // namespace llvm
19+
20+
#endif // LLVM_LIB_TARGET_CSKY_TARGETINFO_CSKYTARGETINFO_H
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
;===-- ./lib/Target/CSKY/TargetInfo/LLVMBuild.txt --------------*- Conf -*--===;
2+
;
3+
; Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
; See https://llvm.org/LICENSE.txt for license information.
5+
; SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
;
7+
;===------------------------------------------------------------------------===;
8+
;
9+
; This is an LLVMBuild description file for the components in this subdirectory.
10+
;
11+
; For more information on the LLVMBuild system, please see:
12+
;
13+
; http://llvm.org/docs/LLVMBuild.html
14+
;
15+
;===------------------------------------------------------------------------===;
16+
17+
[component_0]
18+
type = Library
19+
name = CSKYInfo
20+
parent = CSKY
21+
required_libraries = Support
22+
add_to_library_groups = CSKY

llvm/lib/Target/LLVMBuild.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ subdirectories =
2424
ARM
2525
AVR
2626
BPF
27+
CSKY
2728
Hexagon
2829
Lanai
2930
MSP430

0 commit comments

Comments
 (0)