LLVM  10.0.0svn
MSP430AsmPrinter.cpp
Go to the documentation of this file.
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 
15 #include "MSP430.h"
16 #include "MSP430InstrInfo.h"
17 #include "MSP430MCInstLower.h"
18 #include "MSP430TargetMachine.h"
20 #include "llvm/BinaryFormat/ELF.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"
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  : AsmPrinter(TM, std::move(Streamer)) {}
46 
47  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  raw_ostream &O) {
68  uint64_t Offset = MO.getOffset();
69  if (Offset)
70  O << '(' << Offset << '+';
71 
72  getSymbol(MO.getGlobal())->print(O, MAI);
73 
74  if (Offset)
75  O << ')';
76 }
77 
78 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
79  raw_ostream &O, const char *Modifier) {
80  const MachineOperand &MO = MI->getOperand(OpNum);
81  switch (MO.getType()) {
82  default: llvm_unreachable("Not implemented yet!");
85  return;
87  if (!Modifier || strcmp(Modifier, "nohash"))
88  O << '#';
89  O << MO.getImm();
90  return;
92  MO.getMBB()->getSymbol()->print(O, MAI);
93  return;
95  // If the global address expression is a part of displacement field with a
96  // register base, we should not emit any prefix symbol here, e.g.
97  // mov.w glb(r1), r2
98  // Otherwise (!) msp430-as will silently miscompile the output :(
99  if (!Modifier || strcmp(Modifier, "nohash"))
100  O << '#';
101  PrintSymbolOperand(MO, O);
102  return;
103  }
104  }
105 }
106 
107 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
108  raw_ostream &O) {
109  const MachineOperand &Base = MI->getOperand(OpNum);
110  const MachineOperand &Disp = MI->getOperand(OpNum+1);
111 
112  // Print displacement first
113 
114  // Imm here is in fact global address - print extra modifier.
115  if (Disp.isImm() && Base.getReg() == MSP430::SR)
116  O << '&';
117  printOperand(MI, OpNum+1, O, "nohash");
118 
119  // Print register base field
120  if (Base.getReg() != MSP430::SR && Base.getReg() != MSP430::PC) {
121  O << '(';
122  printOperand(MI, OpNum, O);
123  O << ')';
124  }
125 }
126 
127 /// PrintAsmOperand - Print out an operand for an inline asm expression.
128 ///
129 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
130  const char *ExtraCode, raw_ostream &O) {
131  // Does this asm operand have a single letter operand modifier?
132  if (ExtraCode && ExtraCode[0])
133  return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);
134 
135  printOperand(MI, OpNo, O);
136  return false;
137 }
138 
139 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
140  unsigned OpNo,
141  const char *ExtraCode,
142  raw_ostream &O) {
143  if (ExtraCode && ExtraCode[0]) {
144  return true; // Unknown modifier.
145  }
146  printSrcMemOperand(MI, OpNo, O);
147  return false;
148 }
149 
150 //===----------------------------------------------------------------------===//
151 void MSP430AsmPrinter::EmitInstruction(const MachineInstr *MI) {
152  MSP430MCInstLower MCInstLowering(OutContext, *this);
153 
154  MCInst TmpInst;
155  MCInstLowering.Lower(MI, TmpInst);
156  EmitToStreamer(*OutStreamer, TmpInst);
157 }
158 
159 void MSP430AsmPrinter::EmitInterruptVectorSection(MachineFunction &ISR) {
160  MCSection *Cur = OutStreamer->getCurrentSectionOnly();
161  const auto *F = &ISR.getFunction();
162  if (F->getCallingConv() != CallingConv::MSP430_INTR) {
163  report_fatal_error("Functions with 'interrupt' attribute must have msp430_intrcc CC");
164  }
165  StringRef IVIdx = F->getFnAttribute("interrupt").getValueAsString();
166  MCSection *IV = OutStreamer->getContext().getELFSection(
167  "__interrupt_vector_" + IVIdx,
169  OutStreamer->SwitchSection(IV);
170 
171  const MCSymbol *FunctionSymbol = getSymbol(F);
172  OutStreamer->EmitSymbolValue(FunctionSymbol, TM.getProgramPointerSize());
173  OutStreamer->SwitchSection(Cur);
174 }
175 
176 bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
177  // Emit separate section for an interrupt vector if ISR
178  if (MF.getFunction().hasFnAttribute("interrupt")) {
179  EmitInterruptVectorSection(MF);
180  }
181 
182  SetupMachineFunction(MF);
183  EmitFunctionBody();
184  return false;
185 }
186 
187 // Force static initialization.
190 }
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
Target & getTheMSP430Target()
MachineBasicBlock * getMBB() const
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
static const char * getRegisterName(unsigned RegNo)
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:323
MachineBasicBlock reference.
F(f)
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
RegisterAsmPrinter - Helper template for registering a target specific assembly printer, for use in the target machine initialization function.
MSP430MCInstLower - This class is used to lower an MachineInstr into an MCInst.
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
Address of a global value.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const GlobalValue * getGlobal() const
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:78
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
void LLVMInitializeMSP430AsmPrinter()
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
MachineOperand class - Representation of each machine instruction operand.
Module.h This file contains the declarations for the Module class.
virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS)
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant...
int64_t getImm() const
const Function & getFunction() const
Return the LLVM function that this machine code represents.
MSP430_INTR - Calling convention used for MSP430 interrupt routines.
Definition: CallingConv.h:110
void Lower(const MachineInstr *MI, MCInst &OutMI) const
Representation of each machine instruction.
Definition: MachineInstr.h:64
static bool printOperand(raw_ostream &OS, const SelectionDAG *G, const SDValue Value)
int64_t getOffset() const
Return the offset from the symbol in this operand.
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:65
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:416
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:59