Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- MSP430AsmPrinter.cpp - MSP430 LLVM assembly writer ----------------===//
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 contains a printer that converts from our internal representation
10
// of machine-dependent LLVM code to the MSP430 assembly language.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "MCTargetDesc/MSP430InstPrinter.h"
15
#include "MSP430.h"
16
#include "MSP430InstrInfo.h"
17
#include "MSP430MCInstLower.h"
18
#include "MSP430TargetMachine.h"
19
#include "TargetInfo/MSP430TargetInfo.h"
20
#include "llvm/BinaryFormat/ELF.h"
21
#include "llvm/CodeGen/AsmPrinter.h"
22
#include "llvm/CodeGen/MachineConstantPool.h"
23
#include "llvm/CodeGen/MachineFunctionPass.h"
24
#include "llvm/CodeGen/MachineInstr.h"
25
#include "llvm/CodeGen/MachineModuleInfo.h"
26
#include "llvm/IR/Constants.h"
27
#include "llvm/IR/DerivedTypes.h"
28
#include "llvm/IR/Mangler.h"
29
#include "llvm/IR/Module.h"
30
#include "llvm/MC/MCAsmInfo.h"
31
#include "llvm/MC/MCInst.h"
32
#include "llvm/MC/MCSectionELF.h"
33
#include "llvm/MC/MCStreamer.h"
34
#include "llvm/MC/MCSymbol.h"
35
#include "llvm/Support/TargetRegistry.h"
36
#include "llvm/Support/raw_ostream.h"
37
using namespace llvm;
38
39
#define DEBUG_TYPE "asm-printer"
40
41
namespace {
42
  class MSP430AsmPrinter : public AsmPrinter {
43
  public:
44
    MSP430AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
45
72
        : AsmPrinter(TM, std::move(Streamer)) {}
46
47
317
    StringRef getPassName() const override { return "MSP430 Assembly Printer"; }
48
49
    bool runOnMachineFunction(MachineFunction &MF) override;
50
51
    void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &O) override;
52
    void printOperand(const MachineInstr *MI, int OpNum,
53
                      raw_ostream &O, const char* Modifier = nullptr);
54
    void printSrcMemOperand(const MachineInstr *MI, int OpNum,
55
                            raw_ostream &O);
56
    bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
57
                         const char *ExtraCode, raw_ostream &O) override;
58
    bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
59
                               const char *ExtraCode, raw_ostream &O) override;
60
    void EmitInstruction(const MachineInstr *MI) override;
61
62
    void EmitInterruptVectorSection(MachineFunction &ISR);
63
  };
64
} // end of anonymous namespace
65
66
void MSP430AsmPrinter::PrintSymbolOperand(const MachineOperand &MO,
67
5
                                          raw_ostream &O) {
68
5
  uint64_t Offset = MO.getOffset();
69
5
  if (Offset)
70
1
    O << '(' << Offset << '+';
71
5
72
5
  getSymbol(MO.getGlobal())->print(O, MAI);
73
5
74
5
  if (Offset)
75
1
    O << ')';
76
5
}
77
78
void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
79
76
                                    raw_ostream &O, const char *Modifier) {
80
76
  const MachineOperand &MO = MI->getOperand(OpNum);
81
76
  switch (MO.getType()) {
82
76
  
default: 0
llvm_unreachable0
("Not implemented yet!");
83
76
  case MachineOperand::MO_Register:
84
56
    O << MSP430InstPrinter::getRegisterName(MO.getReg());
85
56
    return;
86
76
  case MachineOperand::MO_Immediate:
87
16
    if (!Modifier || 
strcmp(Modifier, "nohash")8
)
88
8
      O << '#';
89
16
    O << MO.getImm();
90
16
    return;
91
76
  case MachineOperand::MO_MachineBasicBlock:
92
0
    MO.getMBB()->getSymbol()->print(O, MAI);
93
0
    return;
94
76
  case MachineOperand::MO_GlobalAddress: {
95
4
    // If the global address expression is a part of displacement field with a
96
4
    // register base, we should not emit any prefix symbol here, e.g.
97
4
    //   mov.w glb(r1), r2
98
4
    // Otherwise (!) msp430-as will silently miscompile the output :(
99
4
    if (!Modifier || 
strcmp(Modifier, "nohash")3
)
100
1
      O << '#';
101
4
    PrintSymbolOperand(MO, O);
102
4
    return;
103
76
  }
104
76
  }
105
76
}
106
107
void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
108
11
                                          raw_ostream &O) {
109
11
  const MachineOperand &Base = MI->getOperand(OpNum);
110
11
  const MachineOperand &Disp = MI->getOperand(OpNum+1);
111
11
112
11
  // Print displacement first
113
11
114
11
  // Imm here is in fact global address - print extra modifier.
115
11
  if (Disp.isImm() && 
Base.getReg() == MSP430::SR8
)
116
1
    O << '&';
117
11
  printOperand(MI, OpNum+1, O, "nohash");
118
11
119
11
  // Print register base field
120
11
  if (Base.getReg() != MSP430::SR && 
Base.getReg() != MSP430::PC7
) {
121
7
    O << '(';
122
7
    printOperand(MI, OpNum, O);
123
7
    O << ')';
124
7
  }
125
11
}
126
127
/// PrintAsmOperand - Print out an operand for an inline asm expression.
128
///
129
bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
130
61
                                       const char *ExtraCode, raw_ostream &O) {
131
61
  // Does this asm operand have a single letter operand modifier?
132
61
  if (ExtraCode && 
ExtraCode[0]3
)
133
3
    return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);
134
58
135
58
  printOperand(MI, OpNo, O);
136
58
  return false;
137
58
}
138
139
bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
140
                                             unsigned OpNo,
141
                                             const char *ExtraCode,
142
11
                                             raw_ostream &O) {
143
11
  if (ExtraCode && 
ExtraCode[0]0
) {
144
0
    return true; // Unknown modifier.
145
0
  }
146
11
  printSrcMemOperand(MI, OpNo, O);
147
11
  return false;
148
11
}
149
150
//===----------------------------------------------------------------------===//
151
2.74k
void MSP430AsmPrinter::EmitInstruction(const MachineInstr *MI) {
152
2.74k
  MSP430MCInstLower MCInstLowering(OutContext, *this);
153
2.74k
154
2.74k
  MCInst TmpInst;
155
2.74k
  MCInstLowering.Lower(MI, TmpInst);
156
2.74k
  EmitToStreamer(*OutStreamer, TmpInst);
157
2.74k
}
158
159
3
void MSP430AsmPrinter::EmitInterruptVectorSection(MachineFunction &ISR) {
160
3
  MCSection *Cur = OutStreamer->getCurrentSectionOnly();
161
3
  const auto *F = &ISR.getFunction();
162
3
  assert(F->hasFnAttribute("interrupt") &&
163
3
         "Functions with MSP430_INTR CC should have 'interrupt' attribute");
164
3
  StringRef IVIdx = F->getFnAttribute("interrupt").getValueAsString();
165
3
  MCSection *IV = OutStreamer->getContext().getELFSection(
166
3
    "__interrupt_vector_" + IVIdx,
167
3
    ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_EXECINSTR);
168
3
  OutStreamer->SwitchSection(IV);
169
3
170
3
  const MCSymbol *FunctionSymbol = getSymbol(F);
171
3
  OutStreamer->EmitSymbolValue(FunctionSymbol, TM.getProgramPointerSize());
172
3
  OutStreamer->SwitchSection(Cur);
173
3
}
174
175
317
bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
176
317
  // Emit separate section for an interrupt vector if ISR
177
317
  if (MF.getFunction().getCallingConv() == CallingConv::MSP430_INTR)
178
3
    EmitInterruptVectorSection(MF);
179
317
180
317
  SetupMachineFunction(MF);
181
317
  EmitFunctionBody();
182
317
  return false;
183
317
}
184
185
// Force static initialization.
186
78.9k
extern "C" void LLVMInitializeMSP430AsmPrinter() {
187
78.9k
  RegisterAsmPrinter<MSP430AsmPrinter> X(getTheMSP430Target());
188
78.9k
}