xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AVR/AVRAsmPrinter.cpp (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1 //===-- AVRAsmPrinter.cpp - AVR 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 GAS-format AVR assembly language.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "AVR.h"
15 #include "AVRMCInstLower.h"
16 #include "AVRSubtarget.h"
17 #include "MCTargetDesc/AVRInstPrinter.h"
18 #include "MCTargetDesc/AVRMCExpr.h"
19 #include "TargetInfo/AVRTargetInfo.h"
20 
21 #include "llvm/CodeGen/AsmPrinter.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineInstr.h"
24 #include "llvm/CodeGen/TargetRegisterInfo.h"
25 #include "llvm/CodeGen/TargetSubtargetInfo.h"
26 #include "llvm/IR/Mangler.h"
27 #include "llvm/MC/MCContext.h"
28 #include "llvm/MC/MCInst.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/MC/MCSymbol.h"
31 #include "llvm/MC/TargetRegistry.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/raw_ostream.h"
34 
35 #define DEBUG_TYPE "avr-asm-printer"
36 
37 namespace llvm {
38 
39 /// An AVR assembly code printer.
40 class AVRAsmPrinter : public AsmPrinter {
41 public:
42   AVRAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
43       : AsmPrinter(TM, std::move(Streamer)), MRI(*TM.getMCRegisterInfo()) {}
44 
45   StringRef getPassName() const override { return "AVR Assembly Printer"; }
46 
47   void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);
48 
49   bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
50                        const char *ExtraCode, raw_ostream &O) override;
51 
52   bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
53                              const char *ExtraCode, raw_ostream &O) override;
54 
55   void emitInstruction(const MachineInstr *MI) override;
56 
57   const MCExpr *lowerConstant(const Constant *CV) override;
58 
59   void emitXXStructor(const DataLayout &DL, const Constant *CV) override;
60 
61   bool doFinalization(Module &M) override;
62 
63 private:
64   const MCRegisterInfo &MRI;
65   bool EmittedStructorSymbolAttrs = false;
66 };
67 
68 void AVRAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
69                                  raw_ostream &O) {
70   const MachineOperand &MO = MI->getOperand(OpNo);
71 
72   switch (MO.getType()) {
73   case MachineOperand::MO_Register:
74     O << AVRInstPrinter::getPrettyRegisterName(MO.getReg(), MRI);
75     break;
76   case MachineOperand::MO_Immediate:
77     O << MO.getImm();
78     break;
79   case MachineOperand::MO_GlobalAddress:
80     O << getSymbol(MO.getGlobal());
81     break;
82   case MachineOperand::MO_ExternalSymbol:
83     O << *GetExternalSymbolSymbol(MO.getSymbolName());
84     break;
85   case MachineOperand::MO_MachineBasicBlock:
86     O << *MO.getMBB()->getSymbol();
87     break;
88   default:
89     llvm_unreachable("Not implemented yet!");
90   }
91 }
92 
93 bool AVRAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
94                                     const char *ExtraCode, raw_ostream &O) {
95   // Default asm printer can only deal with some extra codes,
96   // so try it first.
97   bool Error = AsmPrinter::PrintAsmOperand(MI, OpNum, ExtraCode, O);
98 
99   if (Error && ExtraCode && ExtraCode[0]) {
100     if (ExtraCode[1] != 0)
101       return true; // Unknown modifier.
102 
103     if (ExtraCode[0] >= 'A' && ExtraCode[0] <= 'Z') {
104       const MachineOperand &RegOp = MI->getOperand(OpNum);
105 
106       assert(RegOp.isReg() && "Operand must be a register when you're"
107                               "using 'A'..'Z' operand extracodes.");
108       Register Reg = RegOp.getReg();
109 
110       unsigned ByteNumber = ExtraCode[0] - 'A';
111 
112       unsigned OpFlags = MI->getOperand(OpNum - 1).getImm();
113       unsigned NumOpRegs = InlineAsm::getNumOperandRegisters(OpFlags);
114       (void)NumOpRegs;
115 
116       const AVRSubtarget &STI = MF->getSubtarget<AVRSubtarget>();
117       const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
118 
119       const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(Reg);
120       unsigned BytesPerReg = TRI.getRegSizeInBits(*RC) / 8;
121       assert(BytesPerReg <= 2 && "Only 8 and 16 bit regs are supported.");
122 
123       unsigned RegIdx = ByteNumber / BytesPerReg;
124       assert(RegIdx < NumOpRegs && "Multibyte index out of range.");
125 
126       Reg = MI->getOperand(OpNum + RegIdx).getReg();
127 
128       if (BytesPerReg == 2) {
129         Reg = TRI.getSubReg(Reg, ByteNumber % BytesPerReg ? AVR::sub_hi
130                                                           : AVR::sub_lo);
131       }
132 
133       O << AVRInstPrinter::getPrettyRegisterName(Reg, MRI);
134       return false;
135     }
136   }
137 
138   if (Error)
139     printOperand(MI, OpNum, O);
140 
141   return false;
142 }
143 
144 bool AVRAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
145                                           unsigned OpNum, const char *ExtraCode,
146                                           raw_ostream &O) {
147   if (ExtraCode && ExtraCode[0])
148     return true; // Unknown modifier
149 
150   const MachineOperand &MO = MI->getOperand(OpNum);
151   (void)MO;
152   assert(MO.isReg() && "Unexpected inline asm memory operand");
153 
154   // TODO: We should be able to look up the alternative name for
155   // the register if it's given.
156   // TableGen doesn't expose a way of getting retrieving names
157   // for registers.
158   if (MI->getOperand(OpNum).getReg() == AVR::R31R30) {
159     O << "Z";
160   } else {
161     assert(MI->getOperand(OpNum).getReg() == AVR::R29R28 &&
162            "Wrong register class for memory operand.");
163     O << "Y";
164   }
165 
166   // If NumOpRegs == 2, then we assume it is product of a FrameIndex expansion
167   // and the second operand is an Imm.
168   unsigned OpFlags = MI->getOperand(OpNum - 1).getImm();
169   unsigned NumOpRegs = InlineAsm::getNumOperandRegisters(OpFlags);
170 
171   if (NumOpRegs == 2) {
172     O << '+' << MI->getOperand(OpNum + 1).getImm();
173   }
174 
175   return false;
176 }
177 
178 void AVRAsmPrinter::emitInstruction(const MachineInstr *MI) {
179   AVRMCInstLower MCInstLowering(OutContext, *this);
180 
181   MCInst I;
182   MCInstLowering.lowerInstruction(*MI, I);
183   EmitToStreamer(*OutStreamer, I);
184 }
185 
186 const MCExpr *AVRAsmPrinter::lowerConstant(const Constant *CV) {
187   MCContext &Ctx = OutContext;
188 
189   if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
190     bool IsProgMem = GV->getAddressSpace() == AVR::ProgramMemory;
191     if (IsProgMem) {
192       const MCExpr *Expr = MCSymbolRefExpr::create(getSymbol(GV), Ctx);
193       return AVRMCExpr::create(AVRMCExpr::VK_AVR_PM, Expr, false, Ctx);
194     }
195   }
196 
197   return AsmPrinter::lowerConstant(CV);
198 }
199 
200 void AVRAsmPrinter::emitXXStructor(const DataLayout &DL, const Constant *CV) {
201   if (!EmittedStructorSymbolAttrs) {
202     OutStreamer->emitRawComment(
203         " Emitting these undefined symbol references causes us to link the"
204         " libgcc code that runs our constructors/destructors");
205     OutStreamer->emitRawComment(" This matches GCC's behavior");
206 
207     MCSymbol *CtorsSym = OutContext.getOrCreateSymbol("__do_global_ctors");
208     OutStreamer->emitSymbolAttribute(CtorsSym, MCSA_Global);
209 
210     MCSymbol *DtorsSym = OutContext.getOrCreateSymbol("__do_global_dtors");
211     OutStreamer->emitSymbolAttribute(DtorsSym, MCSA_Global);
212 
213     EmittedStructorSymbolAttrs = true;
214   }
215 
216   AsmPrinter::emitXXStructor(DL, CV);
217 }
218 
219 bool AVRAsmPrinter::doFinalization(Module &M) {
220   MCSymbol *DoCopyData = OutContext.getOrCreateSymbol("__do_copy_data");
221   MCSymbol *DoClearBss = OutContext.getOrCreateSymbol("__do_clear_bss");
222 
223   // FIXME: We can disable __do_copy_data if there are no static RAM variables.
224 
225   OutStreamer->emitRawComment(
226       " Declaring this symbol tells the CRT that it should");
227   OutStreamer->emitRawComment(
228       "copy all variables from program memory to RAM on startup");
229   OutStreamer->emitSymbolAttribute(DoCopyData, MCSA_Global);
230 
231   OutStreamer->emitRawComment(
232       " Declaring this symbol tells the CRT that it should");
233   OutStreamer->emitRawComment("clear the zeroed data section on startup");
234   OutStreamer->emitSymbolAttribute(DoClearBss, MCSA_Global);
235 
236   return AsmPrinter::doFinalization(M);
237 }
238 
239 } // end of namespace llvm
240 
241 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAVRAsmPrinter() {
242   llvm::RegisterAsmPrinter<llvm::AVRAsmPrinter> X(llvm::getTheAVRTarget());
243 }
244