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