blob: 01f44e266d7bc10b0cc65ab7965821a648820ed2 [file] [log] [blame]
Anton Korobeynikov10138002009-05-03 12:57:15 +00001//===-- 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
Anton Korobeynikov10138002009-05-03 12:57:15 +000014#include "MSP430TargetMachine.h"
Craig Topperb25fda92012-03-17 18:46:09 +000015#include "MSP430.h"
Anton Korobeynikov10138002009-05-03 12:57:15 +000016#include "llvm/CodeGen/Passes.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000017#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000018#include "llvm/CodeGen/TargetPassConfig.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000019#include "llvm/IR/LegacyPassManager.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000020#include "llvm/MC/MCAsmInfo.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000021#include "llvm/Support/TargetRegistry.h"
Anton Korobeynikov10138002009-05-03 12:57:15 +000022using namespace llvm;
23
Anton Korobeynikovc1524d42009-08-14 19:06:50 +000024extern "C" void LLVMInitializeMSP430Target() {
25 // Register the target.
Mehdi Aminif42454b2016-10-09 23:00:34 +000026 RegisterTargetMachine<MSP430TargetMachine> X(getTheMSP430Target());
Anton Korobeynikovc1524d42009-08-14 19:06:50 +000027}
28
Rafael Espindola8c34dd82016-05-18 22:04:49 +000029static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
30 if (!RM.hasValue())
31 return Reloc::Static;
32 return *RM;
33}
34
Rafael Espindola79e238a2017-08-03 02:16:21 +000035static CodeModel::Model getEffectiveCodeModel(Optional<CodeModel::Model> CM) {
36 if (CM)
37 return *CM;
38 return CodeModel::Small;
39}
40
Vadzim Dambrouski9e0d3872017-06-23 21:11:45 +000041static std::string computeDataLayout(const Triple &TT, StringRef CPU,
42 const TargetOptions &Options) {
43 return "e-m:e-p:16:16-i32:16-i64:16-f32:16-f64:16-a:8-n8:16-S16";
44}
45
Daniel Sanders3e5de882015-06-11 19:41:26 +000046MSP430TargetMachine::MSP430TargetMachine(const Target &T, const Triple &TT,
Eric Christopher1f86cca2014-06-27 01:14:54 +000047 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000048 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +000049 Optional<Reloc::Model> RM,
Rafael Espindola79e238a2017-08-03 02:16:21 +000050 Optional<CodeModel::Model> CM,
51 CodeGenOpt::Level OL, bool JIT)
Matthias Braunbb8507e2017-10-12 22:57:28 +000052 : LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options), TT, CPU, FS,
53 Options, getEffectiveRelocModel(RM),
54 getEffectiveCodeModel(CM), OL),
Aditya Nandakumara2719322014-11-13 09:26:31 +000055 TLOF(make_unique<TargetLoweringObjectFileELF>()),
Daniel Sanders3e5de882015-06-11 19:41:26 +000056 Subtarget(TT, CPU, FS, *this) {
Rafael Espindola227144c2013-05-13 01:16:13 +000057 initAsmInfo();
58}
Anton Korobeynikov10138002009-05-03 12:57:15 +000059
Reid Kleckner357600e2014-11-20 23:37:18 +000060MSP430TargetMachine::~MSP430TargetMachine() {}
61
Andrew Trickccb67362012-02-03 05:12:41 +000062namespace {
63/// MSP430 Code Generator Pass Configuration Options.
64class MSP430PassConfig : public TargetPassConfig {
65public:
Matthias Braun5e394c32017-05-30 21:36:41 +000066 MSP430PassConfig(MSP430TargetMachine &TM, PassManagerBase &PM)
Andrew Trickf8ea1082012-02-04 02:56:59 +000067 : TargetPassConfig(TM, PM) {}
Anton Korobeynikov10138002009-05-03 12:57:15 +000068
Andrew Trickccb67362012-02-03 05:12:41 +000069 MSP430TargetMachine &getMSP430TargetMachine() const {
70 return getTM<MSP430TargetMachine>();
71 }
72
Craig Topper6f9e59e2014-04-29 07:58:09 +000073 bool addInstSelector() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +000074 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +000075};
76} // namespace
77
Andrew Trickf8ea1082012-02-04 02:56:59 +000078TargetPassConfig *MSP430TargetMachine::createPassConfig(PassManagerBase &PM) {
Matthias Braun5e394c32017-05-30 21:36:41 +000079 return new MSP430PassConfig(*this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +000080}
81
82bool MSP430PassConfig::addInstSelector() {
Anton Korobeynikov10138002009-05-03 12:57:15 +000083 // Install an instruction selector.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +000084 addPass(createMSP430ISelDag(getMSP430TargetMachine(), getOptLevel()));
Anton Korobeynikov10138002009-05-03 12:57:15 +000085 return false;
86}
87
Matthias Braun7e37a5f2014-12-11 21:26:47 +000088void MSP430PassConfig::addPreEmitPass() {
Anton Korobeynikovce52fd52010-01-15 21:19:05 +000089 // Must run branch selection immediately preceding the asm printer.
Matthias Braun7e37a5f2014-12-11 21:26:47 +000090 addPass(createMSP430BranchSelectionPass(), false);
Anton Korobeynikovce52fd52010-01-15 21:19:05 +000091}