xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/AVR/AVRTargetObjectFile.cpp (revision 04eeddc0aa8e0a417a16eaf9d7d095207f4a8623)
10b57cec5SDimitry Andric //===-- AVRTargetObjectFile.cpp - AVR Object Files ------------------------===//
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 #include "AVRTargetObjectFile.h"
10*04eeddc0SDimitry Andric #include "AVRTargetMachine.h"
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELF.h"
130b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h"
140b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h"
150b57cec5SDimitry Andric #include "llvm/IR/Mangler.h"
160b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
170b57cec5SDimitry Andric #include "llvm/MC/MCSectionELF.h"
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric #include "AVR.h"
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric namespace llvm {
Initialize(MCContext & Ctx,const TargetMachine & TM)220b57cec5SDimitry Andric void AVRTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM) {
230b57cec5SDimitry Andric   Base::Initialize(Ctx, TM);
240b57cec5SDimitry Andric   ProgmemDataSection =
250b57cec5SDimitry Andric       Ctx.getELFSection(".progmem.data", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
26*04eeddc0SDimitry Andric   Progmem1DataSection =
27*04eeddc0SDimitry Andric       Ctx.getELFSection(".progmem1.data", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
28*04eeddc0SDimitry Andric   Progmem2DataSection =
29*04eeddc0SDimitry Andric       Ctx.getELFSection(".progmem2.data", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
30*04eeddc0SDimitry Andric   Progmem3DataSection =
31*04eeddc0SDimitry Andric       Ctx.getELFSection(".progmem3.data", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
32*04eeddc0SDimitry Andric   Progmem4DataSection =
33*04eeddc0SDimitry Andric       Ctx.getELFSection(".progmem4.data", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
34*04eeddc0SDimitry Andric   Progmem5DataSection =
35*04eeddc0SDimitry Andric       Ctx.getELFSection(".progmem5.data", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
360b57cec5SDimitry Andric }
370b57cec5SDimitry Andric 
SelectSectionForGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const38349cc55cSDimitry Andric MCSection *AVRTargetObjectFile::SelectSectionForGlobal(
39349cc55cSDimitry Andric     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
40*04eeddc0SDimitry Andric   // Global values in flash memory are placed in the progmem*.data section
410b57cec5SDimitry Andric   // unless they already have a user assigned section.
42*04eeddc0SDimitry Andric   const auto &AVRTM = static_cast<const AVRTargetMachine &>(TM);
43*04eeddc0SDimitry Andric   if (AVR::isProgramMemoryAddress(GO) && !GO->hasSection() &&
44*04eeddc0SDimitry Andric       Kind.isReadOnly()) {
45*04eeddc0SDimitry Andric     // The AVR subtarget should support LPM to access section '.progmem*.data'.
46*04eeddc0SDimitry Andric     if (!AVRTM.getSubtargetImpl()->hasLPM()) {
47*04eeddc0SDimitry Andric       // TODO: Get the global object's location in source file.
48*04eeddc0SDimitry Andric       getContext().reportError(
49*04eeddc0SDimitry Andric           SMLoc(),
50*04eeddc0SDimitry Andric           "Current AVR subtarget does not support accessing program memory");
51*04eeddc0SDimitry Andric       return Base::SelectSectionForGlobal(GO, Kind, TM);
52*04eeddc0SDimitry Andric     }
53*04eeddc0SDimitry Andric     // The AVR subtarget should support ELPM to access section
54*04eeddc0SDimitry Andric     // '.progmem[1|2|3|4|5].data'.
55*04eeddc0SDimitry Andric     if (!AVRTM.getSubtargetImpl()->hasELPM() &&
56*04eeddc0SDimitry Andric         AVR::getAddressSpace(GO) != AVR::ProgramMemory) {
57*04eeddc0SDimitry Andric       // TODO: Get the global object's location in source file.
58*04eeddc0SDimitry Andric       getContext().reportError(SMLoc(),
59*04eeddc0SDimitry Andric                                "Current AVR subtarget does not support "
60*04eeddc0SDimitry Andric                                "accessing extended program memory");
610b57cec5SDimitry Andric       return ProgmemDataSection;
62*04eeddc0SDimitry Andric     }
63*04eeddc0SDimitry Andric     switch (AVR::getAddressSpace(GO)) {
64*04eeddc0SDimitry Andric     case AVR::ProgramMemory: // address space 1
65*04eeddc0SDimitry Andric       return ProgmemDataSection;
66*04eeddc0SDimitry Andric     case AVR::ProgramMemory1: // address space 2
67*04eeddc0SDimitry Andric       return Progmem1DataSection;
68*04eeddc0SDimitry Andric     case AVR::ProgramMemory2: // address space 3
69*04eeddc0SDimitry Andric       return Progmem2DataSection;
70*04eeddc0SDimitry Andric     case AVR::ProgramMemory3: // address space 4
71*04eeddc0SDimitry Andric       return Progmem3DataSection;
72*04eeddc0SDimitry Andric     case AVR::ProgramMemory4: // address space 5
73*04eeddc0SDimitry Andric       return Progmem4DataSection;
74*04eeddc0SDimitry Andric     case AVR::ProgramMemory5: // address space 6
75*04eeddc0SDimitry Andric       return Progmem5DataSection;
76*04eeddc0SDimitry Andric     default:
77*04eeddc0SDimitry Andric       llvm_unreachable("unexpected program memory index");
78*04eeddc0SDimitry Andric     }
79*04eeddc0SDimitry Andric   }
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric   // Otherwise, we work the same way as ELF.
820b57cec5SDimitry Andric   return Base::SelectSectionForGlobal(GO, Kind, TM);
830b57cec5SDimitry Andric }
840b57cec5SDimitry Andric } // end of namespace llvm
85