LLVM  9.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"
19 #include "llvm/BinaryFormat/ELF.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/DerivedTypes.h"
27 #include "llvm/IR/Mangler.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/MC/MCAsmInfo.h"
30 #include "llvm/MC/MCInst.h"
31 #include "llvm/MC/MCSectionELF.h"
32 #include "llvm/MC/MCStreamer.h"
33 #include "llvm/MC/MCSymbol.h"
36 using namespace llvm;
37 
38 #define DEBUG_TYPE "asm-printer"
39 
40 namespace {
41  class MSP430AsmPrinter : public AsmPrinter {
42  public:
43  MSP430AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
44  : AsmPrinter(TM, std::move(Streamer)) {}
45 
46  StringRef getPassName() const override { return "MSP430 Assembly Printer"; }
47 
48  bool runOnMachineFunction(MachineFunction &MF) override;
49 
50  void printOperand(const MachineInstr *MI, int OpNum,
51  raw_ostream &O, const char* Modifier = nullptr);
52  void printSrcMemOperand(const MachineInstr *MI, int OpNum,
53  raw_ostream &O);
54  bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
55  const char *ExtraCode, raw_ostream &O) override;
56  bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
57  const char *ExtraCode, raw_ostream &O) override;
58  void EmitInstruction(const MachineInstr *MI) override;
59 
60  void EmitInterruptVectorSection(MachineFunction &ISR);
61  };
62 } // end of anonymous namespace
63 
64 
65 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
66  raw_ostream &O, const char *Modifier) {
67  const MachineOperand &MO = MI->getOperand(OpNum);
68  switch (MO.getType()) {
69  default: llvm_unreachable("Not implemented yet!");
72  return;
74  if (!Modifier || strcmp(Modifier, "nohash"))
75  O << '#';
76  O << MO.getImm();
77  return;
79  MO.getMBB()->getSymbol()->print(O, MAI);
80  return;
82  bool isMemOp = Modifier && !strcmp(Modifier, "mem");
83  uint64_t Offset = MO.getOffset();
84 
85  // If the global address expression is a part of displacement field with a
86  // register base, we should not emit any prefix symbol here, e.g.
87  // mov.w &foo, r1
88  // vs
89  // mov.w glb(r1), r2
90  // Otherwise (!) msp430-as will silently miscompile the output :(
91  if (!Modifier || strcmp(Modifier, "nohash"))
92  O << (isMemOp ? '&' : '#');
93  if (Offset)
94  O << '(' << Offset << '+';
95 
96  getSymbol(MO.getGlobal())->print(O, MAI);
97 
98  if (Offset)
99  O << ')';
100 
101  return;
102  }
103  }
104 }
105 
106 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
107  raw_ostream &O) {
108  const MachineOperand &Base = MI->getOperand(OpNum);
109  const MachineOperand &Disp = MI->getOperand(OpNum+1);
110 
111  // Print displacement first
112 
113  // Imm here is in fact global address - print extra modifier.
114  if (Disp.isImm() && Base.getReg() == MSP430::SR)
115  O << '&';
116  printOperand(MI, OpNum+1, O, "nohash");
117 
118  // Print register base field
119  if (Base.getReg() != MSP430::SR && Base.getReg() != MSP430::PC) {
120  O << '(';
121  printOperand(MI, OpNum, O);
122  O << ')';
123  }
124 }
125 
126 /// PrintAsmOperand - Print out an operand for an inline asm expression.
127 ///
128 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
129  const char *ExtraCode, raw_ostream &O) {
130  // Does this asm operand have a single letter operand modifier?
131  if (ExtraCode && ExtraCode[0])
132  return true; // Unknown modifier.
133 
134  printOperand(MI, OpNo, O);
135  return false;
136 }
137 
138 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
139  unsigned OpNo,
140  const char *ExtraCode,
141  raw_ostream &O) {
142  if (ExtraCode && ExtraCode[0]) {
143  return true; // Unknown modifier.
144  }
145  printSrcMemOperand(MI, OpNo, O);
146  return false;
147 }
148 
149 //===----------------------------------------------------------------------===//
150 void MSP430AsmPrinter::EmitInstruction(const MachineInstr *MI) {
151  MSP430MCInstLower MCInstLowering(OutContext, *this);
152 
153  MCInst TmpInst;
154  MCInstLowering.Lower(MI, TmpInst);
155  EmitToStreamer(*OutStreamer, TmpInst);
156 }
157 
158 void MSP430AsmPrinter::EmitInterruptVectorSection(MachineFunction &ISR) {
159  MCSection *Cur = OutStreamer->getCurrentSectionOnly();
160  const auto *F = &ISR.getFunction();
161  assert(F->hasFnAttribute("interrupt") &&
162  "Functions with MSP430_INTR CC should have 'interrupt' attribute");
163  StringRef IVIdx = F->getFnAttribute("interrupt").getValueAsString();
164  MCSection *IV = OutStreamer->getContext().getELFSection(
165  "__interrupt_vector_" + IVIdx,
167  OutStreamer->SwitchSection(IV);
168 
169  const MCSymbol *FunctionSymbol = getSymbol(F);
170  OutStreamer->EmitSymbolValue(FunctionSymbol, TM.getProgramPointerSize());
171  OutStreamer->SwitchSection(Cur);
172 }
173 
174 bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
175  // Emit separate section for an interrupt vector if ISR
177  EmitInterruptVectorSection(MF);
178 
179  SetupMachineFunction(MF);
180  EmitFunctionBody();
181  return false;
182 }
183 
184 // Force static initialization.
187 }
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:38
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
Target & getTheMSP430Target()
MachineBasicBlock * getMBB() const
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)
unsigned getReg() const
getReg - Returns the register number.
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.
Expected< const typename ELFT::Sym * > getSymbol(typename ELFT::SymRange Symbols, uint32_t Index)
Definition: ELF.h:334
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
Address of a global value.
MSP430_INTR - Calling convention used for MSP430 interrupt routines.
Definition: CallingConv.h:105
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.
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:212
MachineOperand class - Representation of each machine instruction operand.
Module.h This file contains the declarations for the Module class.
int64_t getImm() const
const Function & getFunction() const
Return the LLVM function that this machine code represents.
void Lower(const MachineInstr *MI, MCInst &OutMI) const
Representation of each machine instruction.
Definition: MachineInstr.h:63
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.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:413
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