Index: llvm/lib/Target/AVR/AVRAsmPrinter.cpp =================================================================== --- llvm/lib/Target/AVR/AVRAsmPrinter.cpp +++ llvm/lib/Target/AVR/AVRAsmPrinter.cpp @@ -24,6 +24,7 @@ #include "llvm/CodeGen/TargetRegisterInfo.h" #include "llvm/CodeGen/TargetSubtargetInfo.h" #include "llvm/IR/Mangler.h" +#include "llvm/MC/MCContext.h" #include "llvm/MC/MCInst.h" #include "llvm/MC/MCStreamer.h" #include "llvm/MC/MCSymbol.h" @@ -38,9 +39,8 @@ /// An AVR assembly code printer. class AVRAsmPrinter : public AsmPrinter { public: - AVRAsmPrinter(TargetMachine &TM, - std::unique_ptr Streamer) - : AsmPrinter(TM, std::move(Streamer)), MRI(*TM.getMCRegisterInfo()) { } + AVRAsmPrinter(TargetMachine &TM, std::unique_ptr Streamer) + : AsmPrinter(TM, std::move(Streamer)), MRI(*TM.getMCRegisterInfo()) {} StringRef getPassName() const override { return "AVR Assembly Printer"; } @@ -56,8 +56,13 @@ const MCExpr *lowerConstant(const Constant *CV) override; + void emitXXStructor(const DataLayout &DL, const Constant *CV) override; + + bool doFinalization(Module &M) override; + private: const MCRegisterInfo &MRI; + bool EmittedStructorSymbolAttrs = false; }; void AVRAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo, @@ -193,9 +198,47 @@ return AsmPrinter::lowerConstant(CV); } +void AVRAsmPrinter::emitXXStructor(const DataLayout &DL, const Constant *CV) { + if (!EmittedStructorSymbolAttrs) { + OutStreamer->emitRawComment( + " Emitting these undefined symbol references causes us to link the" + " libgcc code that runs our constructors/destructors"); + OutStreamer->emitRawComment(" This matches GCC's behavior"); + + MCSymbol *CtorsSym = OutContext.getOrCreateSymbol("__do_global_ctors"); + OutStreamer->emitSymbolAttribute(CtorsSym, MCSA_Global); + + MCSymbol *DtorsSym = OutContext.getOrCreateSymbol("__do_global_dtors"); + OutStreamer->emitSymbolAttribute(DtorsSym, MCSA_Global); + + EmittedStructorSymbolAttrs = true; + } + + AsmPrinter::emitXXStructor(DL, CV); +} + +bool AVRAsmPrinter::doFinalization(Module &M) { + MCSymbol *DoCopyData = OutContext.getOrCreateSymbol("__do_copy_data"); + MCSymbol *DoClearBss = OutContext.getOrCreateSymbol("__do_clear_bss"); + + // FIXME: We can disable __do_copy_data if there are no static RAM variables. + + OutStreamer->emitRawComment( + " Declaring this symbol tells the CRT that it should"); + OutStreamer->emitRawComment( + "copy all variables from program memory to RAM on startup"); + OutStreamer->emitSymbolAttribute(DoCopyData, MCSA_Global); + + OutStreamer->emitRawComment( + " Declaring this symbol tells the CRT that it should"); + OutStreamer->emitRawComment("clear the zeroed data section on startup"); + OutStreamer->emitSymbolAttribute(DoClearBss, MCSA_Global); + + return AsmPrinter::doFinalization(M); +} + } // end of namespace llvm extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAVRAsmPrinter() { llvm::RegisterAsmPrinter X(llvm::getTheAVRTarget()); } - Index: llvm/lib/Target/AVR/MCTargetDesc/AVRTargetStreamer.h =================================================================== --- llvm/lib/Target/AVR/MCTargetDesc/AVRTargetStreamer.h +++ llvm/lib/Target/AVR/MCTargetDesc/AVRTargetStreamer.h @@ -18,8 +18,6 @@ class AVRTargetStreamer : public MCTargetStreamer { public: explicit AVRTargetStreamer(MCStreamer &S); - - void finish() override; }; /// A target streamer for textual AVR assembly code. Index: llvm/lib/Target/AVR/MCTargetDesc/AVRTargetStreamer.cpp =================================================================== --- llvm/lib/Target/AVR/MCTargetDesc/AVRTargetStreamer.cpp +++ llvm/lib/Target/AVR/MCTargetDesc/AVRTargetStreamer.cpp @@ -21,23 +21,4 @@ AVRTargetAsmStreamer::AVRTargetAsmStreamer(MCStreamer &S) : AVRTargetStreamer(S) {} -void AVRTargetStreamer::finish() { - MCStreamer &OS = getStreamer(); - MCContext &Context = OS.getContext(); - - MCSymbol *DoCopyData = Context.getOrCreateSymbol("__do_copy_data"); - MCSymbol *DoClearBss = Context.getOrCreateSymbol("__do_clear_bss"); - - // FIXME: We can disable __do_copy_data if there are no static RAM variables. - - OS.emitRawComment(" Declaring this symbol tells the CRT that it should"); - OS.emitRawComment("copy all variables from program memory to RAM on startup"); - OS.emitSymbolAttribute(DoCopyData, MCSA_Global); - - OS.emitRawComment(" Declaring this symbol tells the CRT that it should"); - OS.emitRawComment("clear the zeroed data section on startup"); - OS.emitSymbolAttribute(DoClearBss, MCSA_Global); -} - } // end namespace llvm - Index: llvm/test/CodeGen/AVR/ctors.ll =================================================================== --- /dev/null +++ llvm/test/CodeGen/AVR/ctors.ll @@ -0,0 +1,15 @@ +; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py +; RUN: llc < %s -march=avr | FileCheck %s + +define void @do_nothing() addrspace(1) #0 { +; CHECK-LABEL: do_nothing: +; CHECK: ; %bb.0: +; CHECK-NEXT: ret + ret void +} + +; CHECK: .globl __do_global_ctors +@llvm.global_ctors = appending global [1 x { i32, void () addrspace(1)*, i8* }] [{ i32, void () addrspace(1)*, i8* } { i32 65535, void () addrspace(1)* @do_nothing, i8* null }] + +; CHECK: .globl __do_global_dtors +@llvm.global_dtors = appending global [1 x { i32, void () addrspace(1)*, i8* }] [{ i32, void () addrspace(1)*, i8* } { i32 65535, void () addrspace(1)* @do_nothing, i8* null }]