#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"
/// An AVR assembly code printer.
class AVRAsmPrinter : public AsmPrinter {
public:
- AVRAsmPrinter(TargetMachine &TM,
- std::unique_ptr<MCStreamer> Streamer)
- : AsmPrinter(TM, std::move(Streamer)), MRI(*TM.getMCRegisterInfo()) { }
+ AVRAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
+ : AsmPrinter(TM, std::move(Streamer)), MRI(*TM.getMCRegisterInfo()) {}
StringRef getPassName() const override { return "AVR Assembly Printer"; }
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,
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<llvm::AVRAsmPrinter> X(llvm::getTheAVRTarget());
}
-
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
-
--- /dev/null
+; 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 }]