1 //===-- MSP430TargetMachine.cpp - Define TargetMachine for MSP430 ---------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Top-level implementation for the MSP430 target.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "MSP430TargetMachine.h"
15 #include "MSP430.h"
16 #include "llvm/CodeGen/Passes.h"
17 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
18 #include "llvm/CodeGen/TargetPassConfig.h"
19 #include "llvm/IR/LegacyPassManager.h"
20 #include "llvm/MC/MCAsmInfo.h"
21 #include "llvm/Support/TargetRegistry.h"
22 using namespace llvm;
23
LLVMInitializeMSP430Target()24 extern "C" void LLVMInitializeMSP430Target() {
25 // Register the target.
26 RegisterTargetMachine<MSP430TargetMachine> X(TheMSP430Target);
27 }
28
getEffectiveRelocModel(Optional<Reloc::Model> RM)29 static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
30 if (!RM.hasValue())
31 return Reloc::Static;
32 return *RM;
33 }
34
MSP430TargetMachine(const Target & T,const Triple & TT,StringRef CPU,StringRef FS,const TargetOptions & Options,Optional<Reloc::Model> RM,CodeModel::Model CM,CodeGenOpt::Level OL)35 MSP430TargetMachine::MSP430TargetMachine(const Target &T, const Triple &TT,
36 StringRef CPU, StringRef FS,
37 const TargetOptions &Options,
38 Optional<Reloc::Model> RM,
39 CodeModel::Model CM,
40 CodeGenOpt::Level OL)
41 : LLVMTargetMachine(T, "e-m:e-p:16:16-i32:16:32-a:16-n8:16", TT, CPU, FS,
42 Options, getEffectiveRelocModel(RM), CM, OL),
43 TLOF(make_unique<TargetLoweringObjectFileELF>()),
44 // FIXME: Check DataLayout string.
45 Subtarget(TT, CPU, FS, *this) {
46 initAsmInfo();
47 }
48
~MSP430TargetMachine()49 MSP430TargetMachine::~MSP430TargetMachine() {}
50
51 namespace {
52 /// MSP430 Code Generator Pass Configuration Options.
53 class MSP430PassConfig : public TargetPassConfig {
54 public:
MSP430PassConfig(MSP430TargetMachine * TM,PassManagerBase & PM)55 MSP430PassConfig(MSP430TargetMachine *TM, PassManagerBase &PM)
56 : TargetPassConfig(TM, PM) {}
57
getMSP430TargetMachine() const58 MSP430TargetMachine &getMSP430TargetMachine() const {
59 return getTM<MSP430TargetMachine>();
60 }
61
62 bool addInstSelector() override;
63 void addPreEmitPass() override;
64 };
65 } // namespace
66
createPassConfig(PassManagerBase & PM)67 TargetPassConfig *MSP430TargetMachine::createPassConfig(PassManagerBase &PM) {
68 return new MSP430PassConfig(this, PM);
69 }
70
addInstSelector()71 bool MSP430PassConfig::addInstSelector() {
72 // Install an instruction selector.
73 addPass(createMSP430ISelDag(getMSP430TargetMachine(), getOptLevel()));
74 return false;
75 }
76
addPreEmitPass()77 void MSP430PassConfig::addPreEmitPass() {
78 // Must run branch selection immediately preceding the asm printer.
79 addPass(createMSP430BranchSelectionPass(), false);
80 }
81