46#define DEBUG_TYPE "avr-asm-printer"
58 StringRef getPassName()
const override {
return "AVR Assembly Printer"; }
75 bool doFinalization(
Module &M)
override;
77 void emitStartOfAsmFile(
Module &M)
override;
83 bool EmittedStructorSymbolAttrs =
false;
88void AVRAsmPrinter::printOperand(
const MachineInstr *
MI,
unsigned OpNo,
90 const MachineOperand &MO =
MI->getOperand(OpNo);
113bool AVRAsmPrinter::PrintAsmOperand(
const MachineInstr *
MI,
unsigned OpNum,
114 const char *ExtraCode, raw_ostream &O) {
120 const MachineOperand &MO =
MI->getOperand(OpNum);
123 if (ExtraCode && ExtraCode[0] && MO.
isReg()) {
125 if (ExtraCode[1] != 0 || ExtraCode[0] <
'A' || ExtraCode[0] >
'Z')
130 unsigned ByteNumber = ExtraCode[0] -
'A';
131 const InlineAsm::Flag OpFlags(
MI->getOperand(OpNum - 1).getImm());
132 const unsigned NumOpRegs = OpFlags.getNumOperandRegisters();
134 const AVRSubtarget &STI = MF->getSubtarget<AVRSubtarget>();
138 unsigned BytesPerReg =
TRI.getRegSizeInBits(*RC) / 8;
139 assert(BytesPerReg <= 2 &&
"Only 8 and 16 bit regs are supported.");
141 unsigned RegIdx = ByteNumber / BytesPerReg;
142 if (RegIdx >= NumOpRegs)
144 Reg =
MI->getOperand(OpNum + RegIdx).getReg();
146 if (BytesPerReg == 2) {
147 Reg =
TRI.getSubReg(
Reg, (ByteNumber % BytesPerReg) ? AVR::sub_hi
156 PrintSymbolOperand(MO, O);
163bool AVRAsmPrinter::PrintAsmMemoryOperand(
const MachineInstr *
MI,
164 unsigned OpNum,
const char *ExtraCode,
166 if (ExtraCode && ExtraCode[0])
169 const MachineOperand &MO =
MI->getOperand(OpNum);
173 PrintSymbolOperand(MO, O);
177 assert(MO.
isReg() &&
"Unexpected inline asm memory operand");
183 if (
MI->getOperand(OpNum).getReg() == AVR::R31R30) {
185 }
else if (
MI->getOperand(OpNum).getReg() == AVR::R29R28) {
187 }
else if (
MI->getOperand(OpNum).getReg() == AVR::R27R26) {
190 assert(
false &&
"Wrong register class for memory operand.");
195 const InlineAsm::Flag OpFlags(
MI->getOperand(OpNum - 1).getImm());
196 const unsigned NumOpRegs = OpFlags.getNumOperandRegisters();
198 if (NumOpRegs == 2) {
199 assert(
MI->getOperand(OpNum).getReg() != AVR::R27R26 &&
200 "Base register X can not have offset/displacement.");
201 O <<
'+' <<
MI->getOperand(OpNum + 1).getImm();
207void AVRAsmPrinter::emitInstruction(
const MachineInstr *
MI) {
208 AVR_MC::verifyInstructionPredicates(
MI->getOpcode(),
209 getSubtargetInfo().getFeatureBits());
211 AVRMCInstLower MCInstLowering(OutContext, *
this);
214 MCInstLowering.lowerInstruction(*
MI,
I);
215 EmitToStreamer(*OutStreamer,
I);
218const MCExpr *AVRAsmPrinter::lowerConstant(
const Constant *CV,
219 const Constant *BaseCV,
221 MCContext &Ctx = OutContext;
234void AVRAsmPrinter::emitXXStructor(
const DataLayout &
DL,
const Constant *CV) {
235 if (!EmittedStructorSymbolAttrs) {
236 OutStreamer->emitRawComment(
237 " Emitting these undefined symbol references causes us to link the"
238 " libgcc code that runs our constructors/destructors");
239 OutStreamer->emitRawComment(
" This matches GCC's behavior");
241 MCSymbol *CtorsSym = OutContext.getOrCreateSymbol(
"__do_global_ctors");
242 OutStreamer->emitSymbolAttribute(CtorsSym,
MCSA_Global);
244 MCSymbol *DtorsSym = OutContext.getOrCreateSymbol(
"__do_global_dtors");
245 OutStreamer->emitSymbolAttribute(DtorsSym,
MCSA_Global);
247 EmittedStructorSymbolAttrs =
true;
253bool AVRAsmPrinter::doFinalization(
Module &M) {
254 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
255 const AVRTargetMachine &TM = (
const AVRTargetMachine &)MMI->getTarget();
258 bool NeedsCopyData =
false;
259 bool NeedsClearBSS =
false;
260 for (
const auto &GO :
M.globals()) {
261 if (!GO.hasInitializer() || GO.hasAvailableExternallyLinkage())
265 if (GO.hasCommonLinkage()) {
267 NeedsClearBSS =
true;
272 if (
Section->getName().starts_with(
".data"))
273 NeedsCopyData =
true;
274 else if (
Section->getName().starts_with(
".rodata") && SubTM->hasLPM())
277 NeedsCopyData =
true;
278 else if (
Section->getName().starts_with(
".bss"))
279 NeedsClearBSS =
true;
282 MCSymbol *DoCopyData = OutContext.getOrCreateSymbol(
"__do_copy_data");
283 MCSymbol *DoClearBss = OutContext.getOrCreateSymbol(
"__do_clear_bss");
286 OutStreamer->emitRawComment(
287 " Declaring this symbol tells the CRT that it should");
288 OutStreamer->emitRawComment(
289 "copy all variables from program memory to RAM on startup");
290 OutStreamer->emitSymbolAttribute(DoCopyData,
MCSA_Global);
294 OutStreamer->emitRawComment(
295 " Declaring this symbol tells the CRT that it should");
296 OutStreamer->emitRawComment(
"clear the zeroed data section on startup");
297 OutStreamer->emitSymbolAttribute(DoClearBss,
MCSA_Global);
303void AVRAsmPrinter::emitStartOfAsmFile(
Module &M) {
304 const AVRTargetMachine &TM = (
const AVRTargetMachine &)MMI->
getTarget();
310 OutStreamer->emitAssignment(
311 MMI->getContext().getOrCreateSymbol(StringRef(
"__tmp_reg__")),
314 OutStreamer->emitAssignment(
315 MMI->getContext().getOrCreateSymbol(StringRef(
"__zero_reg__")),
318 OutStreamer->emitAssignment(
319 MMI->getContext().getOrCreateSymbol(StringRef(
"__SREG__")),
322 if (!SubTM->hasSmallStack())
323 OutStreamer->emitAssignment(
324 MMI->getContext().getOrCreateSymbol(StringRef(
"__SP_H__")),
327 OutStreamer->emitAssignment(
328 MMI->getContext().getOrCreateSymbol(StringRef(
"__SP_L__")),
331 if (SubTM->hasEIJMPCALL())
332 OutStreamer->emitAssignment(
333 MMI->getContext().getOrCreateSymbol(StringRef(
"__EIND__")),
336 if (SubTM->hasELPM())
337 OutStreamer->emitAssignment(
338 MMI->getContext().getOrCreateSymbol(StringRef(
"__RAMPZ__")),
342char AVRAsmPrinter::ID = 0;
348LLVMInitializeAVRAsmPrinter() {
354 AVRAsmPrinter &
AsmPrinter =
static_cast<AVRAsmPrinter &
>(
363 AVRAsmPrinter &
AsmPrinter =
static_cast<AVRAsmPrinter &
>(
374 AVRAsmPrinter &
AsmPrinter =
static_cast<AVRAsmPrinter &
>(
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static const Function * getParent(const Value *V)
#define LLVM_EXTERNAL_VISIBILITY
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
Machine Check Debug Module
Register const TargetRegisterInfo * TRI
Promote Memory to Register
ModuleAnalysisManager MAM
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static SDValue lowerConstant(SDValue Op, SelectionDAG &DAG, const RISCVSubtarget &Subtarget)
MachineRegisterInfo & MRI
static bool printOperand(raw_ostream &OS, const SelectionDAG *G, const SDValue Value)
std::unique_ptr< MCStreamer > && Streamer
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM)
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM)
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
static const char * getPrettyRegisterName(MCRegister Reg, MCRegisterInfo const &MRI)
static const AVRMCExpr * create(Specifier S, const MCExpr *Expr, bool isNegated, MCContext &Ctx)
Specifies the type of an expression.
int getIORegRAMPZ() const
Get I/O register addresses.
int getRegTmpIndex() const
Get GPR aliases.
int getRegZeroIndex() const
const AVRRegisterInfo * getRegisterInfo() const override
const AVRSubtarget * getSubtargetImpl() const
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
This class is intended to be used as a driving class for all asm writers.
virtual const MCExpr * lowerConstant(const Constant *CV, const Constant *BaseCV=nullptr, uint64_t Offset=0)
Lower the specified LLVM Constant to an MCExpr.
bool doInitialization(Module &M) override
Set up the AsmPrinter when we are working on a new module.
bool doFinalization(Module &M) override
Shut down the asmprinter.
bool runOnMachineFunction(MachineFunction &MF) override
Emit the specified function out to the OutStreamer.
virtual void emitXXStructor(const DataLayout &DL, const Constant *CV)
Targets can override this to change how global constants that are part of a C++ static/global constru...
virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS)
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant.
This is an important base class in LLVM.
A parsed version of the target data layout string in and methods for querying it.
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Base class for the full range of assembler expressions which are needed for parsing.
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
LLVM_ABI MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
const GlobalValue * getGlobal() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
MachineBasicBlock * getMBB() const
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
const char * getSymbolName() const
Register getReg() const
getReg - Returns the register number.
@ MO_Immediate
Immediate operand.
@ MO_GlobalAddress
Address of a global value.
@ MO_MachineBasicBlock
MachineBasicBlock reference.
@ MO_Register
Register operand.
@ MO_ExternalSymbol
Name of external global symbol.
A Module instance is used to store all the information related to an LLVM module.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Represent a constant reference to a string, i.e.
MCSection * SectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const
This method computes the appropriate section to emit the specified global variable or function defini...
Primary interface to the complete machine description for the target machine.
const Target & getTarget() const
const MCRegisterInfo & getMCRegisterInfo() const
This class implements an extremely fast bulk output stream that can only output to a stream.
Pass manager infrastructure for declaring and invalidating analyses.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ S_PM
Corresponds to pm(), reference to program memory.
This is an optimization pass for GlobalISel generic memory operations.
OuterAnalysisManagerProxy< ModuleAnalysisManager, MachineFunction > ModuleAnalysisManagerMachineFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Target & getTheAVRTarget()
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI void setupModuleAsmPrinter(Module &M, ModuleAnalysisManager &MAM, AsmPrinter &AsmPrinter)
LLVM_ABI void setupMachineFunctionAsmPrinter(MachineFunctionAnalysisManager &MFAM, MachineFunction &MF, AsmPrinter &AsmPrinter)
@ MCSA_Global
.type _foo, @gnu_unique_object
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
MCRegisterClass TargetRegisterClass
RegisterAsmPrinter - Helper template for registering a target specific assembly printer,...