LLVM  12.0.0git
MSP430AsmBackend.cpp
Go to the documentation of this file.
1 //===-- MSP430AsmBackend.cpp - MSP430 Assembler Backend -------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
11 #include "llvm/ADT/APInt.h"
12 #include "llvm/MC/MCAsmBackend.h"
13 #include "llvm/MC/MCAssembler.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCDirectives.h"
17 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCObjectWriter.h"
21 #include "llvm/MC/MCSymbol.h"
25 
26 using namespace llvm;
27 
28 namespace {
29 class MSP430AsmBackend : public MCAsmBackend {
30  uint8_t OSABI;
31 
32  uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
33  MCContext &Ctx) const;
34 
35 public:
36  MSP430AsmBackend(const MCSubtargetInfo &STI, uint8_t OSABI)
37  : MCAsmBackend(support::little), OSABI(OSABI) {}
38  ~MSP430AsmBackend() override {}
39 
40  void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
42  uint64_t Value, bool IsResolved,
43  const MCSubtargetInfo *STI) const override;
44 
45  std::unique_ptr<MCObjectTargetWriter>
46  createObjectTargetWriter() const override {
47  return createMSP430ELFObjectWriter(OSABI);
48  }
49 
50  bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
51  const MCRelaxableFragment *DF,
52  const MCAsmLayout &Layout) const override {
53  return false;
54  }
55 
56  bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved,
57  uint64_t Value,
58  const MCRelaxableFragment *DF,
59  const MCAsmLayout &Layout,
60  const bool WasForced) const override {
61  return false;
62  }
63 
64  unsigned getNumFixupKinds() const override {
66  }
67 
68  const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
69  const static MCFixupKindInfo Infos[MSP430::NumTargetFixupKinds] = {
70  // This table must be in the same order of enum in MSP430FixupKinds.h.
71  //
72  // name offset bits flags
73  {"fixup_32", 0, 32, 0},
74  {"fixup_10_pcrel", 0, 10, MCFixupKindInfo::FKF_IsPCRel},
75  {"fixup_16", 0, 16, 0},
76  {"fixup_16_pcrel", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
77  {"fixup_16_byte", 0, 16, 0},
78  {"fixup_16_pcrel_byte", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
79  {"fixup_2x_pcrel", 0, 10, MCFixupKindInfo::FKF_IsPCRel},
80  {"fixup_rl_pcrel", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
81  {"fixup_8", 0, 8, 0},
82  {"fixup_sym_diff", 0, 32, 0},
83  };
84  static_assert((array_lengthof(Infos)) == MSP430::NumTargetFixupKinds,
85  "Not all fixup kinds added to Infos array");
86 
87  if (Kind < FirstTargetFixupKind)
88  return MCAsmBackend::getFixupKindInfo(Kind);
89 
90  return Infos[Kind - FirstTargetFixupKind];
91  }
92 
93  bool mayNeedRelaxation(const MCInst &Inst,
94  const MCSubtargetInfo &STI) const override {
95  return false;
96  }
97 
98  bool writeNopData(raw_ostream &OS, uint64_t Count) const override;
99 };
100 
101 uint64_t MSP430AsmBackend::adjustFixupValue(const MCFixup &Fixup,
102  uint64_t Value,
103  MCContext &Ctx) const {
104  unsigned Kind = Fixup.getKind();
105  switch (Kind) {
106  case MSP430::fixup_10_pcrel: {
107  if (Value & 0x1)
108  Ctx.reportError(Fixup.getLoc(), "fixup value must be 2-byte aligned");
109 
110  // Offset is signed
111  int16_t Offset = Value;
112  // Jumps are in words
113  Offset >>= 1;
114  // PC points to the next instruction so decrement by one
115  --Offset;
116 
117  if (Offset < -512 || Offset > 511)
118  Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
119 
120  // Mask 10 bits
121  Offset &= 0x3ff;
122 
123  return Offset;
124  }
125  default:
126  return Value;
127  }
128 }
129 
130 void MSP430AsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
131  const MCValue &Target,
133  uint64_t Value, bool IsResolved,
134  const MCSubtargetInfo *STI) const {
135  Value = adjustFixupValue(Fixup, Value, Asm.getContext());
136  MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
137  if (!Value)
138  return; // Doesn't change encoding.
139 
140  // Shift the value into position.
141  Value <<= Info.TargetOffset;
142 
143  unsigned Offset = Fixup.getOffset();
144  unsigned NumBytes = alignTo(Info.TargetSize + Info.TargetOffset, 8) / 8;
145 
146  assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
147 
148  // For each byte of the fragment that the fixup touches, mask in the
149  // bits from the fixup value.
150  for (unsigned i = 0; i != NumBytes; ++i) {
151  Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
152  }
153 }
154 
155 bool MSP430AsmBackend::writeNopData(raw_ostream &OS, uint64_t Count) const {
156  if ((Count % 2) != 0)
157  return false;
158 
159  // The canonical nop on MSP430 is mov #0, r3
160  uint64_t NopCount = Count / 2;
161  while (NopCount--)
162  OS.write("\x03\x43", 2);
163 
164  return true;
165 }
166 
167 } // end anonymous namespace
168 
170  const MCSubtargetInfo &STI,
171  const MCRegisterInfo &MRI,
172  const MCTargetOptions &Options) {
173  return new MSP430AsmBackend(STI, ELF::ELFOSABI_STANDALONE);
174 }
This class represents lattice values for constants.
Definition: AllocatorList.h:23
This represents an "assembler immediate".
Definition: MCValue.h:37
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:82
Is this fixup kind PCrelative? This is used by the assembler backend to evaluate fixup values in a ta...
MCContext & getContext() const
Definition: MCAssembler.h:284
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
This file implements a class to represent arbitrary precision integral constant values and operations...
Context object for machine code objects.
Definition: MCContext.h:67
Analysis containing CSE Info
Definition: CSEInfo.cpp:25
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:261
unsigned const MachineRegisterInfo * MRI
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:298
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:156
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:22
static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target, uint64_t Value, MCContext &Ctx, const Triple &TheTriple, bool IsResolved)
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:811
uint32_t getOffset() const
Definition: MCFixup.h:135
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
raw_ostream & write(unsigned char C)
SMLoc getLoc() const
Definition: MCFixup.h:202
constexpr size_t array_lengthof(T(&)[N])
Find the length of an array.
Definition: STLExtras.h:1335
std::unique_ptr< MCObjectTargetWriter > createMSP430ELFObjectWriter(uint8_t OSABI)
Target - Wrapper for Target specific information.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:158
Generic base class for all target subtargets.
Target independent information on a fixup kind.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:74
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:36
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
MCAsmBackend * createMSP430MCAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
MCFixupKind getKind() const
Definition: MCFixup.h:131