LLVM  7.0.0
Nios2AsmPrinter.cpp
Go to the documentation of this file.
1 //===-- Nios2AsmPrinter.cpp - Nios2 LLVM Assembly Printer -----------------===//
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 GAS-format NIOS2 assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14 
17 #include "Nios2.h"
18 #include "Nios2TargetMachine.h"
20 #include "llvm/MC/MCStreamer.h"
22 
23 using namespace llvm;
24 
25 #define DEBUG_TYPE "nios2-asm-printer"
26 
27 namespace {
28 
29 class Nios2AsmPrinter : public AsmPrinter {
30 
31 public:
32  explicit Nios2AsmPrinter(TargetMachine &TM,
33  std::unique_ptr<MCStreamer> Streamer)
34  : AsmPrinter(TM, std::move(Streamer)) {}
35 
36  StringRef getPassName() const override { return "Nios2 Assembly Printer"; }
37 
38  //- EmitInstruction() must exists or will have run time error.
39  void EmitInstruction(const MachineInstr *MI) override;
40  bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
41  unsigned AsmVariant, const char *ExtraCode,
42  raw_ostream &O) override;
43  bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
44  unsigned AsmVariant, const char *ExtraCode,
45  raw_ostream &O) override;
46  void printOperand(const MachineInstr *MI, int opNum, raw_ostream &O);
47  void EmitFunctionEntryLabel() override;
48 };
49 } // namespace
50 
51 //- EmitInstruction() must exists or will have run time error.
52 void Nios2AsmPrinter::EmitInstruction(const MachineInstr *MI) {
53 
54  // Print out both ordinary instruction and boudle instruction
57 
58  do {
59 
60  if (I->isPseudo()) {
61  llvm_unreachable("Pseudo opcode found in EmitInstruction()");
62  }
63 
64  MCInst TmpInst0;
65  LowerNios2MachineInstToMCInst(&*I, TmpInst0, *this);
66  EmitToStreamer(*OutStreamer, TmpInst0);
67  } while ((++I != E) && I->isInsideBundle()); // Delay slot check
68 }
69 
70 // .type main,@function
71 //-> .ent main # @main
72 // main:
73 void Nios2AsmPrinter::EmitFunctionEntryLabel() {
74  OutStreamer->EmitLabel(CurrentFnSym);
75 }
76 
77 // Print out an operand for an inline asm expression.
78 bool Nios2AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
79  unsigned AsmVariant,
80  const char *ExtraCode, raw_ostream &O) {
81  printOperand(MI, OpNum, O);
82  return false;
83 }
84 
85 bool Nios2AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
86  unsigned OpNum, unsigned AsmVariant,
87  const char *ExtraCode,
88  raw_ostream &O) {
89  if (ExtraCode && ExtraCode[0])
90  return true; // Unknown modifier
91 
92  const MachineOperand &MO = MI->getOperand(OpNum);
93  assert(MO.isReg() && "unexpected inline asm memory operand");
94  O << "($" << Nios2InstPrinter::getRegisterName(MO.getReg()) << ")";
95 
96  return false;
97 }
98 
99 void Nios2AsmPrinter::printOperand(const MachineInstr *MI, int opNum,
100  raw_ostream &O) {
101  const MachineOperand &MO = MI->getOperand(opNum);
102  bool closeP = false;
103 
104  if (MO.getTargetFlags())
105  closeP = true;
106 
107  switch (MO.getTargetFlags()) {
108  case Nios2FG::MO_ABS_HI:
109  O << "%hiadj(";
110  break;
111  case Nios2FG::MO_ABS_LO:
112  O << "%lo(";
113  break;
114  }
115 
116  switch (MO.getType()) {
118  O << '$'
120  break;
121 
123  O << MO.getImm();
124  break;
125 
127  MO.getMBB()->getSymbol()->print(O, MAI);
128  return;
129 
131  getSymbol(MO.getGlobal())->print(O, MAI);
132  break;
133 
135  O << GetBlockAddressSymbol(MO.getBlockAddress())->getName();
136  break;
137 
139  O << MO.getSymbolName();
140  break;
141 
142  default:
143  llvm_unreachable("<unknown operand type>");
144  }
145 
146  if (closeP)
147  O << ")";
148 }
149 
150 // Force static initialization.
151 extern "C" void LLVMInitializeNios2AsmPrinter() {
153 }
unsigned getTargetFlags() const
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
instr_iterator instr_end()
MachineBasicBlock * getMBB() const
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
unsigned getReg() const
getReg - Returns the register number.
MachineBasicBlock reference.
Target & getTheNios2Target()
Name of external global symbol.
static StringRef getName(Value *V)
const char * getSymbolName() const
RegisterAsmPrinter - Helper template for registering a target specific assembly printer, for use in the target machine initialization function.
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.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const GlobalValue * getGlobal() const
void LowerNios2MachineInstToMCInst(const MachineInstr *MI, MCInst &OutMI, AsmPrinter &AP)
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:78
Address of a basic block.
self_iterator getIterator()
Definition: ilist_node.h:82
void print(raw_ostream &OS) const
Definition: MCInst.cpp:64
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Iterator for intrusive lists based on ilist_node.
MachineOperand class - Representation of each machine instruction operand.
void LLVMInitializeNios2AsmPrinter()
int64_t getImm() const
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:156
Representation of each machine instruction.
Definition: MachineInstr.h:60
static bool printOperand(raw_ostream &OS, const SelectionDAG *G, const SDValue Value)
const BlockAddress * getBlockAddress() const
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
#define I(x, y, z)
Definition: MD5.cpp:58
bool isReg() const
isReg - Tests if this is a MO_Register operand.
LLVM_NODISCARD std::string lower() const
Definition: StringRef.cpp:108
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
MO_ABS_HI/LO - Represents the hi or low part of an absolute symbol address.
Definition: Nios2BaseInfo.h:31
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:316
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
static const char * getRegisterName(unsigned RegNo)
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:60