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" 1781ad6265SDimitry Andric #include "AVRTargetMachine.h" 180b57cec5SDimitry Andric #include "MCTargetDesc/AVRInstPrinter.h" 19fe6060f1SDimitry Andric #include "MCTargetDesc/AVRMCExpr.h" 200b57cec5SDimitry Andric #include "TargetInfo/AVRTargetInfo.h" 210b57cec5SDimitry Andric 220b57cec5SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h" 230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 2581ad6265SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h" 260b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 270b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 280b57cec5SDimitry Andric #include "llvm/IR/Mangler.h" 29349cc55cSDimitry Andric #include "llvm/MC/MCContext.h" 300b57cec5SDimitry Andric #include "llvm/MC/MCInst.h" 310b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h" 320b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h" 33349cc55cSDimitry Andric #include "llvm/MC/TargetRegistry.h" 340b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 350b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric #define DEBUG_TYPE "avr-asm-printer" 380b57cec5SDimitry Andric 390b57cec5SDimitry Andric namespace llvm { 400b57cec5SDimitry Andric 410b57cec5SDimitry Andric /// An AVR assembly code printer. 420b57cec5SDimitry Andric class AVRAsmPrinter : public AsmPrinter { 430b57cec5SDimitry Andric public: 44349cc55cSDimitry Andric AVRAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer) 450b57cec5SDimitry Andric : AsmPrinter(TM, std::move(Streamer)), MRI(*TM.getMCRegisterInfo()) {} 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric StringRef getPassName() const override { return "AVR Assembly Printer"; } 480b57cec5SDimitry Andric 490b57cec5SDimitry Andric void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O); 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum, 520b57cec5SDimitry Andric const char *ExtraCode, raw_ostream &O) override; 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum, 550b57cec5SDimitry Andric const char *ExtraCode, raw_ostream &O) override; 560b57cec5SDimitry Andric 575ffd83dbSDimitry Andric void emitInstruction(const MachineInstr *MI) override; 580b57cec5SDimitry Andric 59fe6060f1SDimitry Andric const MCExpr *lowerConstant(const Constant *CV) override; 60fe6060f1SDimitry Andric 61349cc55cSDimitry Andric void emitXXStructor(const DataLayout &DL, const Constant *CV) override; 62349cc55cSDimitry Andric 63349cc55cSDimitry Andric bool doFinalization(Module &M) override; 64349cc55cSDimitry Andric 6581ad6265SDimitry Andric void emitStartOfAsmFile(Module &M) override; 6681ad6265SDimitry Andric 670b57cec5SDimitry Andric private: 680b57cec5SDimitry Andric const MCRegisterInfo &MRI; 69349cc55cSDimitry Andric bool EmittedStructorSymbolAttrs = false; 700b57cec5SDimitry Andric }; 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric void AVRAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo, 730b57cec5SDimitry Andric raw_ostream &O) { 740b57cec5SDimitry Andric const MachineOperand &MO = MI->getOperand(OpNo); 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric switch (MO.getType()) { 770b57cec5SDimitry Andric case MachineOperand::MO_Register: 780b57cec5SDimitry Andric O << AVRInstPrinter::getPrettyRegisterName(MO.getReg(), MRI); 790b57cec5SDimitry Andric break; 800b57cec5SDimitry Andric case MachineOperand::MO_Immediate: 810b57cec5SDimitry Andric O << MO.getImm(); 820b57cec5SDimitry Andric break; 830b57cec5SDimitry Andric case MachineOperand::MO_GlobalAddress: 840b57cec5SDimitry Andric O << getSymbol(MO.getGlobal()); 850b57cec5SDimitry Andric break; 860b57cec5SDimitry Andric case MachineOperand::MO_ExternalSymbol: 870b57cec5SDimitry Andric O << *GetExternalSymbolSymbol(MO.getSymbolName()); 880b57cec5SDimitry Andric break; 890b57cec5SDimitry Andric case MachineOperand::MO_MachineBasicBlock: 900b57cec5SDimitry Andric O << *MO.getMBB()->getSymbol(); 910b57cec5SDimitry Andric break; 920b57cec5SDimitry Andric default: 930b57cec5SDimitry Andric llvm_unreachable("Not implemented yet!"); 940b57cec5SDimitry Andric } 950b57cec5SDimitry Andric } 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric bool AVRAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum, 980b57cec5SDimitry Andric const char *ExtraCode, raw_ostream &O) { 990b57cec5SDimitry Andric // Default asm printer can only deal with some extra codes, 1000b57cec5SDimitry Andric // so try it first. 1010b57cec5SDimitry Andric bool Error = AsmPrinter::PrintAsmOperand(MI, OpNum, ExtraCode, O); 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric if (Error && ExtraCode && ExtraCode[0]) { 1040b57cec5SDimitry Andric if (ExtraCode[1] != 0) 1050b57cec5SDimitry Andric return true; // Unknown modifier. 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric if (ExtraCode[0] >= 'A' && ExtraCode[0] <= 'Z') { 1080b57cec5SDimitry Andric const MachineOperand &RegOp = MI->getOperand(OpNum); 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric assert(RegOp.isReg() && "Operand must be a register when you're" 1110b57cec5SDimitry Andric "using 'A'..'Z' operand extracodes."); 1128bcb0991SDimitry Andric Register Reg = RegOp.getReg(); 1130b57cec5SDimitry Andric 1140b57cec5SDimitry Andric unsigned ByteNumber = ExtraCode[0] - 'A'; 1150b57cec5SDimitry Andric 1160b57cec5SDimitry Andric unsigned OpFlags = MI->getOperand(OpNum - 1).getImm(); 1170b57cec5SDimitry Andric unsigned NumOpRegs = InlineAsm::getNumOperandRegisters(OpFlags); 1180b57cec5SDimitry Andric (void)NumOpRegs; 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric const AVRSubtarget &STI = MF->getSubtarget<AVRSubtarget>(); 1210b57cec5SDimitry Andric const TargetRegisterInfo &TRI = *STI.getRegisterInfo(); 1220b57cec5SDimitry Andric 1230b57cec5SDimitry Andric const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(Reg); 1240b57cec5SDimitry Andric unsigned BytesPerReg = TRI.getRegSizeInBits(*RC) / 8; 1250b57cec5SDimitry Andric assert(BytesPerReg <= 2 && "Only 8 and 16 bit regs are supported."); 1260b57cec5SDimitry Andric 1270b57cec5SDimitry Andric unsigned RegIdx = ByteNumber / BytesPerReg; 1280b57cec5SDimitry Andric assert(RegIdx < NumOpRegs && "Multibyte index out of range."); 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric Reg = MI->getOperand(OpNum + RegIdx).getReg(); 1310b57cec5SDimitry Andric 1320b57cec5SDimitry Andric if (BytesPerReg == 2) { 1330b57cec5SDimitry Andric Reg = TRI.getSubReg(Reg, ByteNumber % BytesPerReg ? AVR::sub_hi 1340b57cec5SDimitry Andric : AVR::sub_lo); 1350b57cec5SDimitry Andric } 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric O << AVRInstPrinter::getPrettyRegisterName(Reg, MRI); 1380b57cec5SDimitry Andric return false; 1390b57cec5SDimitry Andric } 1400b57cec5SDimitry Andric } 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric if (Error) 1430b57cec5SDimitry Andric printOperand(MI, OpNum, O); 1440b57cec5SDimitry Andric 1450b57cec5SDimitry Andric return false; 1460b57cec5SDimitry Andric } 1470b57cec5SDimitry Andric 1480b57cec5SDimitry Andric bool AVRAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, 1490b57cec5SDimitry Andric unsigned OpNum, const char *ExtraCode, 1500b57cec5SDimitry Andric raw_ostream &O) { 151349cc55cSDimitry Andric if (ExtraCode && ExtraCode[0]) 152349cc55cSDimitry Andric return true; // Unknown modifier 1530b57cec5SDimitry Andric 1540b57cec5SDimitry Andric const MachineOperand &MO = MI->getOperand(OpNum); 1550b57cec5SDimitry Andric (void)MO; 1560b57cec5SDimitry Andric assert(MO.isReg() && "Unexpected inline asm memory operand"); 1570b57cec5SDimitry Andric 1580b57cec5SDimitry Andric // TODO: We should be able to look up the alternative name for 1590b57cec5SDimitry Andric // the register if it's given. 1600b57cec5SDimitry Andric // TableGen doesn't expose a way of getting retrieving names 1610b57cec5SDimitry Andric // for registers. 1620b57cec5SDimitry Andric if (MI->getOperand(OpNum).getReg() == AVR::R31R30) { 1630b57cec5SDimitry Andric O << "Z"; 1640b57cec5SDimitry Andric } else { 1650b57cec5SDimitry Andric assert(MI->getOperand(OpNum).getReg() == AVR::R29R28 && 1660b57cec5SDimitry Andric "Wrong register class for memory operand."); 1670b57cec5SDimitry Andric O << "Y"; 1680b57cec5SDimitry Andric } 1690b57cec5SDimitry Andric 1700b57cec5SDimitry Andric // If NumOpRegs == 2, then we assume it is product of a FrameIndex expansion 1710b57cec5SDimitry Andric // and the second operand is an Imm. 1720b57cec5SDimitry Andric unsigned OpFlags = MI->getOperand(OpNum - 1).getImm(); 1730b57cec5SDimitry Andric unsigned NumOpRegs = InlineAsm::getNumOperandRegisters(OpFlags); 1740b57cec5SDimitry Andric 1750b57cec5SDimitry Andric if (NumOpRegs == 2) { 1760b57cec5SDimitry Andric O << '+' << MI->getOperand(OpNum + 1).getImm(); 1770b57cec5SDimitry Andric } 1780b57cec5SDimitry Andric 1790b57cec5SDimitry Andric return false; 1800b57cec5SDimitry Andric } 1810b57cec5SDimitry Andric 1825ffd83dbSDimitry Andric void AVRAsmPrinter::emitInstruction(const MachineInstr *MI) { 183*753f127fSDimitry Andric // FIXME: Enable feature predicate checks once all the test pass. 184*753f127fSDimitry Andric // AVR_MC::verifyInstructionPredicates(MI->getOpcode(), 185*753f127fSDimitry Andric // getSubtargetInfo().getFeatureBits()); 186*753f127fSDimitry Andric 1870b57cec5SDimitry Andric AVRMCInstLower MCInstLowering(OutContext, *this); 1880b57cec5SDimitry Andric 1890b57cec5SDimitry Andric MCInst I; 1900b57cec5SDimitry Andric MCInstLowering.lowerInstruction(*MI, I); 1910b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, I); 1920b57cec5SDimitry Andric } 1930b57cec5SDimitry Andric 194fe6060f1SDimitry Andric const MCExpr *AVRAsmPrinter::lowerConstant(const Constant *CV) { 195fe6060f1SDimitry Andric MCContext &Ctx = OutContext; 196fe6060f1SDimitry Andric 197fe6060f1SDimitry Andric if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) { 198fe6060f1SDimitry Andric bool IsProgMem = GV->getAddressSpace() == AVR::ProgramMemory; 199fe6060f1SDimitry Andric if (IsProgMem) { 200fe6060f1SDimitry Andric const MCExpr *Expr = MCSymbolRefExpr::create(getSymbol(GV), Ctx); 201fe6060f1SDimitry Andric return AVRMCExpr::create(AVRMCExpr::VK_AVR_PM, Expr, false, Ctx); 202fe6060f1SDimitry Andric } 203fe6060f1SDimitry Andric } 204fe6060f1SDimitry Andric 205fe6060f1SDimitry Andric return AsmPrinter::lowerConstant(CV); 206fe6060f1SDimitry Andric } 207fe6060f1SDimitry Andric 208349cc55cSDimitry Andric void AVRAsmPrinter::emitXXStructor(const DataLayout &DL, const Constant *CV) { 209349cc55cSDimitry Andric if (!EmittedStructorSymbolAttrs) { 210349cc55cSDimitry Andric OutStreamer->emitRawComment( 211349cc55cSDimitry Andric " Emitting these undefined symbol references causes us to link the" 212349cc55cSDimitry Andric " libgcc code that runs our constructors/destructors"); 213349cc55cSDimitry Andric OutStreamer->emitRawComment(" This matches GCC's behavior"); 214349cc55cSDimitry Andric 215349cc55cSDimitry Andric MCSymbol *CtorsSym = OutContext.getOrCreateSymbol("__do_global_ctors"); 216349cc55cSDimitry Andric OutStreamer->emitSymbolAttribute(CtorsSym, MCSA_Global); 217349cc55cSDimitry Andric 218349cc55cSDimitry Andric MCSymbol *DtorsSym = OutContext.getOrCreateSymbol("__do_global_dtors"); 219349cc55cSDimitry Andric OutStreamer->emitSymbolAttribute(DtorsSym, MCSA_Global); 220349cc55cSDimitry Andric 221349cc55cSDimitry Andric EmittedStructorSymbolAttrs = true; 222349cc55cSDimitry Andric } 223349cc55cSDimitry Andric 224349cc55cSDimitry Andric AsmPrinter::emitXXStructor(DL, CV); 225349cc55cSDimitry Andric } 226349cc55cSDimitry Andric 227349cc55cSDimitry Andric bool AVRAsmPrinter::doFinalization(Module &M) { 228349cc55cSDimitry Andric MCSymbol *DoCopyData = OutContext.getOrCreateSymbol("__do_copy_data"); 229349cc55cSDimitry Andric MCSymbol *DoClearBss = OutContext.getOrCreateSymbol("__do_clear_bss"); 230349cc55cSDimitry Andric 231349cc55cSDimitry Andric // FIXME: We can disable __do_copy_data if there are no static RAM variables. 232349cc55cSDimitry Andric 233349cc55cSDimitry Andric OutStreamer->emitRawComment( 234349cc55cSDimitry Andric " Declaring this symbol tells the CRT that it should"); 235349cc55cSDimitry Andric OutStreamer->emitRawComment( 236349cc55cSDimitry Andric "copy all variables from program memory to RAM on startup"); 237349cc55cSDimitry Andric OutStreamer->emitSymbolAttribute(DoCopyData, MCSA_Global); 238349cc55cSDimitry Andric 239349cc55cSDimitry Andric OutStreamer->emitRawComment( 240349cc55cSDimitry Andric " Declaring this symbol tells the CRT that it should"); 241349cc55cSDimitry Andric OutStreamer->emitRawComment("clear the zeroed data section on startup"); 242349cc55cSDimitry Andric OutStreamer->emitSymbolAttribute(DoClearBss, MCSA_Global); 243349cc55cSDimitry Andric 244349cc55cSDimitry Andric return AsmPrinter::doFinalization(M); 245349cc55cSDimitry Andric } 246349cc55cSDimitry Andric 24781ad6265SDimitry Andric void AVRAsmPrinter::emitStartOfAsmFile(Module &M) { 24881ad6265SDimitry Andric const AVRTargetMachine &TM = (const AVRTargetMachine &)MMI->getTarget(); 24981ad6265SDimitry Andric const AVRSubtarget *SubTM = (const AVRSubtarget *)TM.getSubtargetImpl(); 25081ad6265SDimitry Andric if (!SubTM) 25181ad6265SDimitry Andric return; 25281ad6265SDimitry Andric 25381ad6265SDimitry Andric // Emit __tmp_reg__. 25481ad6265SDimitry Andric OutStreamer->emitAssignment( 25581ad6265SDimitry Andric MMI->getContext().getOrCreateSymbol(StringRef("__tmp_reg__")), 25681ad6265SDimitry Andric MCConstantExpr::create(SubTM->getRegTmpIndex(), MMI->getContext())); 25781ad6265SDimitry Andric // Emit __zero_reg__. 25881ad6265SDimitry Andric OutStreamer->emitAssignment( 25981ad6265SDimitry Andric MMI->getContext().getOrCreateSymbol(StringRef("__zero_reg__")), 26081ad6265SDimitry Andric MCConstantExpr::create(SubTM->getRegZeroIndex(), MMI->getContext())); 26181ad6265SDimitry Andric // Emit __SREG__. 26281ad6265SDimitry Andric OutStreamer->emitAssignment( 26381ad6265SDimitry Andric MMI->getContext().getOrCreateSymbol(StringRef("__SREG__")), 26481ad6265SDimitry Andric MCConstantExpr::create(SubTM->getIORegSREG(), MMI->getContext())); 26581ad6265SDimitry Andric // Emit __SP_H__ if available. 26681ad6265SDimitry Andric if (!SubTM->hasSmallStack()) 26781ad6265SDimitry Andric OutStreamer->emitAssignment( 26881ad6265SDimitry Andric MMI->getContext().getOrCreateSymbol(StringRef("__SP_H__")), 26981ad6265SDimitry Andric MCConstantExpr::create(SubTM->getIORegSPH(), MMI->getContext())); 27081ad6265SDimitry Andric // Emit __SP_L__. 27181ad6265SDimitry Andric OutStreamer->emitAssignment( 27281ad6265SDimitry Andric MMI->getContext().getOrCreateSymbol(StringRef("__SP_L__")), 27381ad6265SDimitry Andric MCConstantExpr::create(SubTM->getIORegSPL(), MMI->getContext())); 27481ad6265SDimitry Andric // Emit __EIND__ if available. 27581ad6265SDimitry Andric if (SubTM->hasEIJMPCALL()) 27681ad6265SDimitry Andric OutStreamer->emitAssignment( 27781ad6265SDimitry Andric MMI->getContext().getOrCreateSymbol(StringRef("__EIND__")), 27881ad6265SDimitry Andric MCConstantExpr::create(SubTM->getIORegEIND(), MMI->getContext())); 27981ad6265SDimitry Andric // Emit __RAMPZ__ if available. 28081ad6265SDimitry Andric if (SubTM->hasELPM()) 28181ad6265SDimitry Andric OutStreamer->emitAssignment( 28281ad6265SDimitry Andric MMI->getContext().getOrCreateSymbol(StringRef("__RAMPZ__")), 28381ad6265SDimitry Andric MCConstantExpr::create(SubTM->getIORegRAMPZ(), MMI->getContext())); 28481ad6265SDimitry Andric } 28581ad6265SDimitry Andric 2860b57cec5SDimitry Andric } // end of namespace llvm 2870b57cec5SDimitry Andric 288480093f4SDimitry Andric extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAVRAsmPrinter() { 2890b57cec5SDimitry Andric llvm::RegisterAsmPrinter<llvm::AVRAsmPrinter> X(llvm::getTheAVRTarget()); 2900b57cec5SDimitry Andric } 291