LLVM  4.0.0
AVRAsmPrinter.cpp
Go to the documentation of this file.
1 //===-- AVRAsmPrinter.cpp - AVR 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 GAS-format AVR assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "AVR.h"
16 #include "AVRMCInstLower.h"
17 #include "AVRSubtarget.h"
19 
23 #include "llvm/IR/Mangler.h"
24 #include "llvm/MC/MCInst.h"
25 #include "llvm/MC/MCStreamer.h"
26 #include "llvm/MC/MCSymbol.h"
32 
33 #define DEBUG_TYPE "avr-asm-printer"
34 
35 namespace llvm {
36 
37 /// An AVR assembly code printer.
38 class AVRAsmPrinter : public AsmPrinter {
39 public:
41  std::unique_ptr<MCStreamer> Streamer)
42  : AsmPrinter(TM, std::move(Streamer)), MRI(*TM.getMCRegisterInfo()) { }
43 
44  StringRef getPassName() const override { return "AVR Assembly Printer"; }
45 
46  void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O,
47  const char *Modifier = 0);
48 
49  bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
50  unsigned AsmVariant, const char *ExtraCode,
51  raw_ostream &O) override;
52 
53  bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
54  unsigned AsmVariant, const char *ExtraCode,
55  raw_ostream &O) override;
56 
57  void EmitInstruction(const MachineInstr *MI) override;
58 
59 private:
60  const MCRegisterInfo &MRI;
61 };
62 
63 void AVRAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
64  raw_ostream &O, const char *Modifier) {
65  const MachineOperand &MO = MI->getOperand(OpNo);
66 
67  switch (MO.getType()) {
70  break;
72  O << MO.getImm();
73  break;
75  O << getSymbol(MO.getGlobal());
76  break;
79  break;
81  O << *MO.getMBB()->getSymbol();
82  break;
83  default:
84  llvm_unreachable("Not implemented yet!");
85  }
86 }
87 
88 bool AVRAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
89  unsigned AsmVariant, const char *ExtraCode,
90  raw_ostream &O) {
91  // Default asm printer can only deal with some extra codes,
92  // so try it first.
93  bool Error = AsmPrinter::PrintAsmOperand(MI, OpNum, AsmVariant, ExtraCode, O);
94 
95  if (Error && ExtraCode && ExtraCode[0]) {
96  if (ExtraCode[1] != 0)
97  return true; // Unknown modifier.
98 
99  if (ExtraCode[0] >= 'A' && ExtraCode[0] <= 'Z') {
100  const MachineOperand &RegOp = MI->getOperand(OpNum);
101 
102  assert(RegOp.isReg() && "Operand must be a register when you're"
103  "using 'A'..'Z' operand extracodes.");
104  unsigned Reg = RegOp.getReg();
105 
106  unsigned ByteNumber = ExtraCode[0] - 'A';
107 
108  unsigned OpFlags = MI->getOperand(OpNum - 1).getImm();
109  unsigned NumOpRegs = InlineAsm::getNumOperandRegisters(OpFlags);
110  (void)NumOpRegs;
111 
112  const AVRSubtarget &STI = MF->getSubtarget<AVRSubtarget>();
113  const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
114 
115  unsigned BytesPerReg = TRI.getMinimalPhysRegClass(Reg)->getSize();
116  assert(BytesPerReg <= 2 && "Only 8 and 16 bit regs are supported.");
117 
118  unsigned RegIdx = ByteNumber / BytesPerReg;
119  assert(RegIdx < NumOpRegs && "Multibyte index out of range.");
120 
121  Reg = MI->getOperand(OpNum + RegIdx).getReg();
122 
123  if (BytesPerReg == 2) {
124  Reg = TRI.getSubReg(Reg, ByteNumber % BytesPerReg ? AVR::sub_hi
125  : AVR::sub_lo);
126  }
127 
129  return false;
130  }
131  }
132 
133  printOperand(MI, OpNum, O);
134 
135  return false;
136 }
137 
139  unsigned OpNum, unsigned AsmVariant,
140  const char *ExtraCode,
141  raw_ostream &O) {
142  if (ExtraCode && ExtraCode[0]) {
143  llvm_unreachable("This branch is not implemented yet");
144  }
145 
146  const MachineOperand &MO = MI->getOperand(OpNum);
147  (void)MO;
148  assert(MO.isReg() && "Unexpected inline asm memory operand");
149 
150  // TODO: We can look up the alternative name for the register if it's given.
151  if (MI->getOperand(OpNum).getReg() == AVR::R31R30) {
152  O << "Z";
153  } else {
154  assert(MI->getOperand(OpNum).getReg() == AVR::R29R28 &&
155  "Wrong register class for memory operand.");
156  O << "Y";
157  }
158 
159  // If NumOpRegs == 2, then we assume it is product of a FrameIndex expansion
160  // and the second operand is an Imm.
161  unsigned OpFlags = MI->getOperand(OpNum - 1).getImm();
162  unsigned NumOpRegs = InlineAsm::getNumOperandRegisters(OpFlags);
163 
164  if (NumOpRegs == 2) {
165  O << '+' << MI->getOperand(OpNum + 1).getImm();
166  }
167 
168  return false;
169 }
170 
172  AVRMCInstLower MCInstLowering(OutContext, *this);
173 
174  MCInst I;
175  MCInstLowering.lowerInstruction(*MI, I);
177 }
178 
179 } // end of namespace llvm
180 
181 extern "C" void LLVMInitializeAVRAsmPrinter() {
183 }
184 
void EmitInstruction(const MachineInstr *MI) override
Targets should implement this to emit instructions.
const GlobalValue * getGlobal() const
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:84
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:371
MachineBasicBlock * getMBB() const
bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum, unsigned AsmVariant, const char *ExtraCode, raw_ostream &O) override
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant as...
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:79
const MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:87
MachineBasicBlock reference.
const char * getSymbolName() const
Target & getTheAVRTarget()
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Name of external global symbol.
Reg
All possible values of the reg field in the ModR/M byte.
int64_t getImm() const
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:150
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O, const char *Modifier=0)
void LLVMInitializeAVRAsmPrinter()
Address of a global value.
Lowers MachineInstr objects into MCInst objects.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:279
virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, unsigned AsmVariant, const char *ExtraCode, raw_ostream &OS)
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant...
AVRAsmPrinter(TargetMachine &TM, std::unique_ptr< MCStreamer > Streamer)
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:71
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:67
static unsigned getNumOperandRegisters(unsigned Flag)
getNumOperandRegisters - Extract the number of registers field from the inline asm operand flag...
Definition: InlineAsm.h:335
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void lowerInstruction(const MachineInstr &MI, MCInst &OutMI) const
Lowers a MachineInstr into a MCInst.
MachineOperand class - Representation of each machine instruction operand.
void EmitToStreamer(MCStreamer &S, const MCInst &Inst)
Definition: AsmPrinter.cpp:161
An AVR assembly code printer.
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum, unsigned AsmVariant, const char *ExtraCode, raw_ostream &O) override
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant...
A specific AVR target MCU.
Definition: AVRSubtarget.h:33
Representation of each machine instruction.
Definition: MachineInstr.h:52
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
#define I(x, y, z)
Definition: MD5.cpp:54
static const char * getPrettyRegisterName(unsigned RegNo, MCRegisterInfo const &MRI)
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
RegisterAsmPrinter - Helper template for registering a target specific assembly printer, for use in the target machine initialization function.
Lightweight error class with error context and mandatory checking.
const AVRRegisterInfo * getRegisterInfo() const override
Definition: AVRSubtarget.h:47
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
Primary interface to the complete machine description for the target machine.
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
MCSymbol * GetExternalSymbolSymbol(StringRef Sym) const
Return the MCSymbol for the specified ExternalSymbol.