xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AVR/AVRAsmPrinter.cpp (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
10b57cec5SDimitry Andric //===-- AVRAsmPrinter.cpp - AVR LLVM assembly writer ----------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains a printer that converts from our internal representation
100b57cec5SDimitry Andric // of machine-dependent LLVM code to GAS-format AVR assembly language.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "AVR.h"
150b57cec5SDimitry Andric #include "AVRMCInstLower.h"
160b57cec5SDimitry Andric #include "AVRSubtarget.h"
170b57cec5SDimitry Andric #include "MCTargetDesc/AVRInstPrinter.h"
18fe6060f1SDimitry Andric #include "MCTargetDesc/AVRMCExpr.h"
190b57cec5SDimitry Andric #include "TargetInfo/AVRTargetInfo.h"
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
260b57cec5SDimitry Andric #include "llvm/IR/Mangler.h"
27*349cc55cSDimitry Andric #include "llvm/MC/MCContext.h"
280b57cec5SDimitry Andric #include "llvm/MC/MCInst.h"
290b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h"
300b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h"
31*349cc55cSDimitry Andric #include "llvm/MC/TargetRegistry.h"
320b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
330b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric #define DEBUG_TYPE "avr-asm-printer"
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric namespace llvm {
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric /// An AVR assembly code printer.
400b57cec5SDimitry Andric class AVRAsmPrinter : public AsmPrinter {
410b57cec5SDimitry Andric public:
42*349cc55cSDimitry Andric   AVRAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
430b57cec5SDimitry Andric       : AsmPrinter(TM, std::move(Streamer)), MRI(*TM.getMCRegisterInfo()) {}
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric   StringRef getPassName() const override { return "AVR Assembly Printer"; }
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric   void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric   bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
500b57cec5SDimitry Andric                        const char *ExtraCode, raw_ostream &O) override;
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric   bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
530b57cec5SDimitry Andric                              const char *ExtraCode, raw_ostream &O) override;
540b57cec5SDimitry Andric 
555ffd83dbSDimitry Andric   void emitInstruction(const MachineInstr *MI) override;
560b57cec5SDimitry Andric 
57fe6060f1SDimitry Andric   const MCExpr *lowerConstant(const Constant *CV) override;
58fe6060f1SDimitry Andric 
59*349cc55cSDimitry Andric   void emitXXStructor(const DataLayout &DL, const Constant *CV) override;
60*349cc55cSDimitry Andric 
61*349cc55cSDimitry Andric   bool doFinalization(Module &M) override;
62*349cc55cSDimitry Andric 
630b57cec5SDimitry Andric private:
640b57cec5SDimitry Andric   const MCRegisterInfo &MRI;
65*349cc55cSDimitry Andric   bool EmittedStructorSymbolAttrs = false;
660b57cec5SDimitry Andric };
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric void AVRAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
690b57cec5SDimitry Andric                                  raw_ostream &O) {
700b57cec5SDimitry Andric   const MachineOperand &MO = MI->getOperand(OpNo);
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric   switch (MO.getType()) {
730b57cec5SDimitry Andric   case MachineOperand::MO_Register:
740b57cec5SDimitry Andric     O << AVRInstPrinter::getPrettyRegisterName(MO.getReg(), MRI);
750b57cec5SDimitry Andric     break;
760b57cec5SDimitry Andric   case MachineOperand::MO_Immediate:
770b57cec5SDimitry Andric     O << MO.getImm();
780b57cec5SDimitry Andric     break;
790b57cec5SDimitry Andric   case MachineOperand::MO_GlobalAddress:
800b57cec5SDimitry Andric     O << getSymbol(MO.getGlobal());
810b57cec5SDimitry Andric     break;
820b57cec5SDimitry Andric   case MachineOperand::MO_ExternalSymbol:
830b57cec5SDimitry Andric     O << *GetExternalSymbolSymbol(MO.getSymbolName());
840b57cec5SDimitry Andric     break;
850b57cec5SDimitry Andric   case MachineOperand::MO_MachineBasicBlock:
860b57cec5SDimitry Andric     O << *MO.getMBB()->getSymbol();
870b57cec5SDimitry Andric     break;
880b57cec5SDimitry Andric   default:
890b57cec5SDimitry Andric     llvm_unreachable("Not implemented yet!");
900b57cec5SDimitry Andric   }
910b57cec5SDimitry Andric }
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric bool AVRAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
940b57cec5SDimitry Andric                                     const char *ExtraCode, raw_ostream &O) {
950b57cec5SDimitry Andric   // Default asm printer can only deal with some extra codes,
960b57cec5SDimitry Andric   // so try it first.
970b57cec5SDimitry Andric   bool Error = AsmPrinter::PrintAsmOperand(MI, OpNum, ExtraCode, O);
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric   if (Error && ExtraCode && ExtraCode[0]) {
1000b57cec5SDimitry Andric     if (ExtraCode[1] != 0)
1010b57cec5SDimitry Andric       return true; // Unknown modifier.
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric     if (ExtraCode[0] >= 'A' && ExtraCode[0] <= 'Z') {
1040b57cec5SDimitry Andric       const MachineOperand &RegOp = MI->getOperand(OpNum);
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric       assert(RegOp.isReg() && "Operand must be a register when you're"
1070b57cec5SDimitry Andric                               "using 'A'..'Z' operand extracodes.");
1088bcb0991SDimitry Andric       Register Reg = RegOp.getReg();
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric       unsigned ByteNumber = ExtraCode[0] - 'A';
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric       unsigned OpFlags = MI->getOperand(OpNum - 1).getImm();
1130b57cec5SDimitry Andric       unsigned NumOpRegs = InlineAsm::getNumOperandRegisters(OpFlags);
1140b57cec5SDimitry Andric       (void)NumOpRegs;
1150b57cec5SDimitry Andric 
1160b57cec5SDimitry Andric       const AVRSubtarget &STI = MF->getSubtarget<AVRSubtarget>();
1170b57cec5SDimitry Andric       const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric       const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(Reg);
1200b57cec5SDimitry Andric       unsigned BytesPerReg = TRI.getRegSizeInBits(*RC) / 8;
1210b57cec5SDimitry Andric       assert(BytesPerReg <= 2 && "Only 8 and 16 bit regs are supported.");
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric       unsigned RegIdx = ByteNumber / BytesPerReg;
1240b57cec5SDimitry Andric       assert(RegIdx < NumOpRegs && "Multibyte index out of range.");
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric       Reg = MI->getOperand(OpNum + RegIdx).getReg();
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric       if (BytesPerReg == 2) {
1290b57cec5SDimitry Andric         Reg = TRI.getSubReg(Reg, ByteNumber % BytesPerReg ? AVR::sub_hi
1300b57cec5SDimitry Andric                                                           : AVR::sub_lo);
1310b57cec5SDimitry Andric       }
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric       O << AVRInstPrinter::getPrettyRegisterName(Reg, MRI);
1340b57cec5SDimitry Andric       return false;
1350b57cec5SDimitry Andric     }
1360b57cec5SDimitry Andric   }
1370b57cec5SDimitry Andric 
1380b57cec5SDimitry Andric   if (Error)
1390b57cec5SDimitry Andric     printOperand(MI, OpNum, O);
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric   return false;
1420b57cec5SDimitry Andric }
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric bool AVRAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
1450b57cec5SDimitry Andric                                           unsigned OpNum, const char *ExtraCode,
1460b57cec5SDimitry Andric                                           raw_ostream &O) {
147*349cc55cSDimitry Andric   if (ExtraCode && ExtraCode[0])
148*349cc55cSDimitry Andric     return true; // Unknown modifier
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric   const MachineOperand &MO = MI->getOperand(OpNum);
1510b57cec5SDimitry Andric   (void)MO;
1520b57cec5SDimitry Andric   assert(MO.isReg() && "Unexpected inline asm memory operand");
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric   // TODO: We should be able to look up the alternative name for
1550b57cec5SDimitry Andric   // the register if it's given.
1560b57cec5SDimitry Andric   // TableGen doesn't expose a way of getting retrieving names
1570b57cec5SDimitry Andric   // for registers.
1580b57cec5SDimitry Andric   if (MI->getOperand(OpNum).getReg() == AVR::R31R30) {
1590b57cec5SDimitry Andric     O << "Z";
1600b57cec5SDimitry Andric   } else {
1610b57cec5SDimitry Andric     assert(MI->getOperand(OpNum).getReg() == AVR::R29R28 &&
1620b57cec5SDimitry Andric            "Wrong register class for memory operand.");
1630b57cec5SDimitry Andric     O << "Y";
1640b57cec5SDimitry Andric   }
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric   // If NumOpRegs == 2, then we assume it is product of a FrameIndex expansion
1670b57cec5SDimitry Andric   // and the second operand is an Imm.
1680b57cec5SDimitry Andric   unsigned OpFlags = MI->getOperand(OpNum - 1).getImm();
1690b57cec5SDimitry Andric   unsigned NumOpRegs = InlineAsm::getNumOperandRegisters(OpFlags);
1700b57cec5SDimitry Andric 
1710b57cec5SDimitry Andric   if (NumOpRegs == 2) {
1720b57cec5SDimitry Andric     O << '+' << MI->getOperand(OpNum + 1).getImm();
1730b57cec5SDimitry Andric   }
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric   return false;
1760b57cec5SDimitry Andric }
1770b57cec5SDimitry Andric 
1785ffd83dbSDimitry Andric void AVRAsmPrinter::emitInstruction(const MachineInstr *MI) {
1790b57cec5SDimitry Andric   AVRMCInstLower MCInstLowering(OutContext, *this);
1800b57cec5SDimitry Andric 
1810b57cec5SDimitry Andric   MCInst I;
1820b57cec5SDimitry Andric   MCInstLowering.lowerInstruction(*MI, I);
1830b57cec5SDimitry Andric   EmitToStreamer(*OutStreamer, I);
1840b57cec5SDimitry Andric }
1850b57cec5SDimitry Andric 
186fe6060f1SDimitry Andric const MCExpr *AVRAsmPrinter::lowerConstant(const Constant *CV) {
187fe6060f1SDimitry Andric   MCContext &Ctx = OutContext;
188fe6060f1SDimitry Andric 
189fe6060f1SDimitry Andric   if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
190fe6060f1SDimitry Andric     bool IsProgMem = GV->getAddressSpace() == AVR::ProgramMemory;
191fe6060f1SDimitry Andric     if (IsProgMem) {
192fe6060f1SDimitry Andric       const MCExpr *Expr = MCSymbolRefExpr::create(getSymbol(GV), Ctx);
193fe6060f1SDimitry Andric       return AVRMCExpr::create(AVRMCExpr::VK_AVR_PM, Expr, false, Ctx);
194fe6060f1SDimitry Andric     }
195fe6060f1SDimitry Andric   }
196fe6060f1SDimitry Andric 
197fe6060f1SDimitry Andric   return AsmPrinter::lowerConstant(CV);
198fe6060f1SDimitry Andric }
199fe6060f1SDimitry Andric 
200*349cc55cSDimitry Andric void AVRAsmPrinter::emitXXStructor(const DataLayout &DL, const Constant *CV) {
201*349cc55cSDimitry Andric   if (!EmittedStructorSymbolAttrs) {
202*349cc55cSDimitry Andric     OutStreamer->emitRawComment(
203*349cc55cSDimitry Andric         " Emitting these undefined symbol references causes us to link the"
204*349cc55cSDimitry Andric         " libgcc code that runs our constructors/destructors");
205*349cc55cSDimitry Andric     OutStreamer->emitRawComment(" This matches GCC's behavior");
206*349cc55cSDimitry Andric 
207*349cc55cSDimitry Andric     MCSymbol *CtorsSym = OutContext.getOrCreateSymbol("__do_global_ctors");
208*349cc55cSDimitry Andric     OutStreamer->emitSymbolAttribute(CtorsSym, MCSA_Global);
209*349cc55cSDimitry Andric 
210*349cc55cSDimitry Andric     MCSymbol *DtorsSym = OutContext.getOrCreateSymbol("__do_global_dtors");
211*349cc55cSDimitry Andric     OutStreamer->emitSymbolAttribute(DtorsSym, MCSA_Global);
212*349cc55cSDimitry Andric 
213*349cc55cSDimitry Andric     EmittedStructorSymbolAttrs = true;
214*349cc55cSDimitry Andric   }
215*349cc55cSDimitry Andric 
216*349cc55cSDimitry Andric   AsmPrinter::emitXXStructor(DL, CV);
217*349cc55cSDimitry Andric }
218*349cc55cSDimitry Andric 
219*349cc55cSDimitry Andric bool AVRAsmPrinter::doFinalization(Module &M) {
220*349cc55cSDimitry Andric   MCSymbol *DoCopyData = OutContext.getOrCreateSymbol("__do_copy_data");
221*349cc55cSDimitry Andric   MCSymbol *DoClearBss = OutContext.getOrCreateSymbol("__do_clear_bss");
222*349cc55cSDimitry Andric 
223*349cc55cSDimitry Andric   // FIXME: We can disable __do_copy_data if there are no static RAM variables.
224*349cc55cSDimitry Andric 
225*349cc55cSDimitry Andric   OutStreamer->emitRawComment(
226*349cc55cSDimitry Andric       " Declaring this symbol tells the CRT that it should");
227*349cc55cSDimitry Andric   OutStreamer->emitRawComment(
228*349cc55cSDimitry Andric       "copy all variables from program memory to RAM on startup");
229*349cc55cSDimitry Andric   OutStreamer->emitSymbolAttribute(DoCopyData, MCSA_Global);
230*349cc55cSDimitry Andric 
231*349cc55cSDimitry Andric   OutStreamer->emitRawComment(
232*349cc55cSDimitry Andric       " Declaring this symbol tells the CRT that it should");
233*349cc55cSDimitry Andric   OutStreamer->emitRawComment("clear the zeroed data section on startup");
234*349cc55cSDimitry Andric   OutStreamer->emitSymbolAttribute(DoClearBss, MCSA_Global);
235*349cc55cSDimitry Andric 
236*349cc55cSDimitry Andric   return AsmPrinter::doFinalization(M);
237*349cc55cSDimitry Andric }
238*349cc55cSDimitry Andric 
2390b57cec5SDimitry Andric } // end of namespace llvm
2400b57cec5SDimitry Andric 
241480093f4SDimitry Andric extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAVRAsmPrinter() {
2420b57cec5SDimitry Andric   llvm::RegisterAsmPrinter<llvm::AVRAsmPrinter> X(llvm::getTheAVRTarget());
2430b57cec5SDimitry Andric }
244