LLVM  8.0.1
MSP430AsmPrinter.cpp
Go to the documentation of this file.
1 //===-- MSP430AsmPrinter.cpp - MSP430 LLVM assembly writer ----------------===//
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 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to the MSP430 assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 #include "MSP430.h"
17 #include "MSP430InstrInfo.h"
18 #include "MSP430MCInstLower.h"
19 #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 printOperand(const MachineInstr *MI, int OpNum,
52  raw_ostream &O, const char* Modifier = nullptr);
53  void printSrcMemOperand(const MachineInstr *MI, int OpNum,
54  raw_ostream &O);
55  bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
56  unsigned AsmVariant, const char *ExtraCode,
57  raw_ostream &O) override;
58  bool PrintAsmMemoryOperand(const MachineInstr *MI,
59  unsigned OpNo, unsigned AsmVariant,
60  const char *ExtraCode, raw_ostream &O) override;
61  void EmitInstruction(const MachineInstr *MI) override;
62 
63  void EmitInterruptVectorSection(MachineFunction &ISR);
64  };
65 } // end of anonymous namespace
66 
67 
68 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
69  raw_ostream &O, const char *Modifier) {
70  const MachineOperand &MO = MI->getOperand(OpNum);
71  switch (MO.getType()) {
72  default: llvm_unreachable("Not implemented yet!");
75  return;
77  if (!Modifier || strcmp(Modifier, "nohash"))
78  O << '#';
79  O << MO.getImm();
80  return;
82  MO.getMBB()->getSymbol()->print(O, MAI);
83  return;
85  bool isMemOp = Modifier && !strcmp(Modifier, "mem");
86  uint64_t Offset = MO.getOffset();
87 
88  // If the global address expression is a part of displacement field with a
89  // register base, we should not emit any prefix symbol here, e.g.
90  // mov.w &foo, r1
91  // vs
92  // mov.w glb(r1), r2
93  // Otherwise (!) msp430-as will silently miscompile the output :(
94  if (!Modifier || strcmp(Modifier, "nohash"))
95  O << (isMemOp ? '&' : '#');
96  if (Offset)
97  O << '(' << Offset << '+';
98 
99  getSymbol(MO.getGlobal())->print(O, MAI);
100 
101  if (Offset)
102  O << ')';
103 
104  return;
105  }
106  }
107 }
108 
109 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
110  raw_ostream &O) {
111  const MachineOperand &Base = MI->getOperand(OpNum);
112  const MachineOperand &Disp = MI->getOperand(OpNum+1);
113 
114  // Print displacement first
115 
116  // Imm here is in fact global address - print extra modifier.
117  if (Disp.isImm() && !Base.getReg())
118  O << '&';
119  printOperand(MI, OpNum+1, O, "nohash");
120 
121  // Print register base field
122  if (Base.getReg()) {
123  O << '(';
124  printOperand(MI, OpNum, O);
125  O << ')';
126  }
127 }
128 
129 /// PrintAsmOperand - Print out an operand for an inline asm expression.
130 ///
131 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
132  unsigned AsmVariant,
133  const char *ExtraCode, raw_ostream &O) {
134  // Does this asm operand have a single letter operand modifier?
135  if (ExtraCode && ExtraCode[0])
136  return true; // Unknown modifier.
137 
138  printOperand(MI, OpNo, O);
139  return false;
140 }
141 
142 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
143  unsigned OpNo, unsigned AsmVariant,
144  const char *ExtraCode,
145  raw_ostream &O) {
146  if (ExtraCode && ExtraCode[0]) {
147  return true; // Unknown modifier.
148  }
149  printSrcMemOperand(MI, OpNo, O);
150  return false;
151 }
152 
153 //===----------------------------------------------------------------------===//
154 void MSP430AsmPrinter::EmitInstruction(const MachineInstr *MI) {
155  MSP430MCInstLower MCInstLowering(OutContext, *this);
156 
157  MCInst TmpInst;
158  MCInstLowering.Lower(MI, TmpInst);
159  EmitToStreamer(*OutStreamer, TmpInst);
160 }
161 
162 void MSP430AsmPrinter::EmitInterruptVectorSection(MachineFunction &ISR) {
163  MCSection *Cur = OutStreamer->getCurrentSectionOnly();
164  const auto *F = &ISR.getFunction();
165  assert(F->hasFnAttribute("interrupt") &&
166  "Functions with MSP430_INTR CC should have 'interrupt' attribute");
167  StringRef IVIdx = F->getFnAttribute("interrupt").getValueAsString();
168  MCSection *IV = OutStreamer->getContext().getELFSection(
169  "__interrupt_vector_" + IVIdx,
171  OutStreamer->SwitchSection(IV);
172 
173  const MCSymbol *FunctionSymbol = getSymbol(F);
174  OutStreamer->EmitSymbolValue(FunctionSymbol, TM.getProgramPointerSize());
175  OutStreamer->SwitchSection(Cur);
176 }
177 
178 bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
179  // Emit separate section for an interrupt vector if ISR
181  EmitInterruptVectorSection(MF);
182 
183  SetupMachineFunction(MF);
184  EmitFunctionBody();
185  return false;
186 }
187 
188 // Force static initialization.
191 }
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
This class represents lattice values for constants.
Definition: AllocatorList.h:24
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
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:337
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:161
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:79
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:213
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: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.
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:46
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:59
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:414
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
MSP430_INTR - Calling convention used for MSP430 interrupt routines.
Definition: CallingConv.h:106
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:60