xref: /freebsd/contrib/llvm-project/llvm/lib/Target/MSP430/MSP430TargetMachine.cpp (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
10b57cec5SDimitry Andric //===-- MSP430TargetMachine.cpp - Define TargetMachine for MSP430 ---------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // Top-level implementation for the MSP430 target.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "MSP430TargetMachine.h"
140b57cec5SDimitry Andric #include "MSP430.h"
150b57cec5SDimitry Andric #include "TargetInfo/MSP430TargetInfo.h"
160b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
170b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h"
190b57cec5SDimitry Andric #include "llvm/IR/LegacyPassManager.h"
200b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
21*349cc55cSDimitry Andric #include "llvm/MC/TargetRegistry.h"
220b57cec5SDimitry Andric using namespace llvm;
230b57cec5SDimitry Andric 
24480093f4SDimitry Andric extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeMSP430Target() {
250b57cec5SDimitry Andric   // Register the target.
260b57cec5SDimitry Andric   RegisterTargetMachine<MSP430TargetMachine> X(getTheMSP430Target());
270b57cec5SDimitry Andric }
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
300b57cec5SDimitry Andric   if (!RM.hasValue())
310b57cec5SDimitry Andric     return Reloc::Static;
320b57cec5SDimitry Andric   return *RM;
330b57cec5SDimitry Andric }
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric static std::string computeDataLayout(const Triple &TT, StringRef CPU,
360b57cec5SDimitry Andric                                      const TargetOptions &Options) {
370b57cec5SDimitry Andric   return "e-m:e-p:16:16-i32:16-i64:16-f32:16-f64:16-a:8-n8:16-S16";
380b57cec5SDimitry Andric }
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric MSP430TargetMachine::MSP430TargetMachine(const Target &T, const Triple &TT,
410b57cec5SDimitry Andric                                          StringRef CPU, StringRef FS,
420b57cec5SDimitry Andric                                          const TargetOptions &Options,
430b57cec5SDimitry Andric                                          Optional<Reloc::Model> RM,
440b57cec5SDimitry Andric                                          Optional<CodeModel::Model> CM,
450b57cec5SDimitry Andric                                          CodeGenOpt::Level OL, bool JIT)
460b57cec5SDimitry Andric     : LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options), TT, CPU, FS,
470b57cec5SDimitry Andric                         Options, getEffectiveRelocModel(RM),
480b57cec5SDimitry Andric                         getEffectiveCodeModel(CM, CodeModel::Small), OL),
498bcb0991SDimitry Andric       TLOF(std::make_unique<TargetLoweringObjectFileELF>()),
505ffd83dbSDimitry Andric       Subtarget(TT, std::string(CPU), std::string(FS), *this) {
510b57cec5SDimitry Andric   initAsmInfo();
520b57cec5SDimitry Andric }
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric MSP430TargetMachine::~MSP430TargetMachine() {}
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric namespace {
570b57cec5SDimitry Andric /// MSP430 Code Generator Pass Configuration Options.
580b57cec5SDimitry Andric class MSP430PassConfig : public TargetPassConfig {
590b57cec5SDimitry Andric public:
600b57cec5SDimitry Andric   MSP430PassConfig(MSP430TargetMachine &TM, PassManagerBase &PM)
610b57cec5SDimitry Andric     : TargetPassConfig(TM, PM) {}
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric   MSP430TargetMachine &getMSP430TargetMachine() const {
640b57cec5SDimitry Andric     return getTM<MSP430TargetMachine>();
650b57cec5SDimitry Andric   }
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric   bool addInstSelector() override;
680b57cec5SDimitry Andric   void addPreEmitPass() override;
690b57cec5SDimitry Andric };
700b57cec5SDimitry Andric } // namespace
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric TargetPassConfig *MSP430TargetMachine::createPassConfig(PassManagerBase &PM) {
730b57cec5SDimitry Andric   return new MSP430PassConfig(*this, PM);
740b57cec5SDimitry Andric }
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric bool MSP430PassConfig::addInstSelector() {
770b57cec5SDimitry Andric   // Install an instruction selector.
780b57cec5SDimitry Andric   addPass(createMSP430ISelDag(getMSP430TargetMachine(), getOptLevel()));
790b57cec5SDimitry Andric   return false;
800b57cec5SDimitry Andric }
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric void MSP430PassConfig::addPreEmitPass() {
830b57cec5SDimitry Andric   // Must run branch selection immediately preceding the asm printer.
84*349cc55cSDimitry Andric   addPass(createMSP430BranchSelectionPass());
850b57cec5SDimitry Andric }
86