LLVM  14.0.0git
AVRMCExpr.cpp
Go to the documentation of this file.
1 //===-- AVRMCExpr.cpp - AVR specific MC expression classes ----------------===//
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 
9 #include "AVRMCExpr.h"
10 
11 #include "llvm/MC/MCAsmLayout.h"
12 #include "llvm/MC/MCAssembler.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCStreamer.h"
15 #include "llvm/MC/MCValue.h"
16 
17 namespace llvm {
18 
19 namespace {
20 
21 const struct ModifierEntry {
22  const char *const Spelling;
23  AVRMCExpr::VariantKind VariantKind;
24 } ModifierNames[] = {
26  {"hh8", AVRMCExpr::VK_AVR_HH8}, // synonym with hlo8
27  {"hlo8", AVRMCExpr::VK_AVR_HH8}, {"hhi8", AVRMCExpr::VK_AVR_HHI8},
28 
30  {"pm_hi8", AVRMCExpr::VK_AVR_PM_HI8}, {"pm_hh8", AVRMCExpr::VK_AVR_PM_HH8},
31 
32  {"lo8_gs", AVRMCExpr::VK_AVR_LO8_GS}, {"hi8_gs", AVRMCExpr::VK_AVR_HI8_GS},
33  {"gs", AVRMCExpr::VK_AVR_GS},
34 };
35 
36 } // end of anonymous namespace
37 
39  bool Negated, MCContext &Ctx) {
40  return new (Ctx) AVRMCExpr(Kind, Expr, Negated);
41 }
42 
43 void AVRMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
44  assert(Kind != VK_AVR_None);
45 
46  if (isNegated())
47  OS << '-';
48 
49  OS << getName() << '(';
50  getSubExpr()->print(OS, MAI);
51  OS << ')';
52 }
53 
54 bool AVRMCExpr::evaluateAsConstant(int64_t &Result) const {
55  MCValue Value;
56 
57  bool isRelocatable =
58  getSubExpr()->evaluateAsRelocatable(Value, nullptr, nullptr);
59 
60  if (!isRelocatable)
61  return false;
62 
63  if (Value.isAbsolute()) {
64  Result = evaluateAsInt64(Value.getConstant());
65  return true;
66  }
67 
68  return false;
69 }
70 
72  const MCAsmLayout *Layout,
73  const MCFixup *Fixup) const {
74  MCValue Value;
75  bool isRelocatable = SubExpr->evaluateAsRelocatable(Value, Layout, Fixup);
76 
77  if (!isRelocatable)
78  return false;
79 
80  if (Value.isAbsolute()) {
81  Result = MCValue::get(evaluateAsInt64(Value.getConstant()));
82  } else {
83  if (!Layout)
84  return false;
85 
87  const MCSymbolRefExpr *Sym = Value.getSymA();
88  MCSymbolRefExpr::VariantKind Modifier = Sym->getKind();
89  if (Modifier != MCSymbolRefExpr::VK_None)
90  return false;
91  if (Kind == VK_AVR_PM) {
92  Modifier = MCSymbolRefExpr::VK_AVR_PM;
93  }
94 
95  Sym = MCSymbolRefExpr::create(&Sym->getSymbol(), Modifier, Context);
96  Result = MCValue::get(Sym, Value.getSymB(), Value.getConstant());
97  }
98 
99  return true;
100 }
101 
102 int64_t AVRMCExpr::evaluateAsInt64(int64_t Value) const {
103  if (Negated)
104  Value *= -1;
105 
106  switch (Kind) {
108  Value &= 0xff;
109  break;
111  Value &= 0xff00;
112  Value >>= 8;
113  break;
115  Value &= 0xff0000;
116  Value >>= 16;
117  break;
119  Value &= 0xff000000;
120  Value >>= 24;
121  break;
124  Value >>= 1; // Program memory addresses must always be shifted by one.
125  Value &= 0xff;
126  break;
129  Value >>= 1; // Program memory addresses must always be shifted by one.
130  Value &= 0xff00;
131  Value >>= 8;
132  break;
134  Value >>= 1; // Program memory addresses must always be shifted by one.
135  Value &= 0xff0000;
136  Value >>= 16;
137  break;
140  Value >>= 1; // Program memory addresses must always be shifted by one.
141  break;
142 
144  llvm_unreachable("Uninitialized expression.");
145  }
146  return static_cast<uint64_t>(Value) & 0xff;
147 }
148 
151 
152  switch (getKind()) {
153  case VK_AVR_LO8:
155  break;
156  case VK_AVR_HI8:
158  break;
159  case VK_AVR_HH8:
161  break;
162  case VK_AVR_HHI8:
164  break;
165 
166  case VK_AVR_PM_LO8:
168  break;
169  case VK_AVR_PM_HI8:
171  break;
172  case VK_AVR_PM_HH8:
174  break;
175  case VK_AVR_PM:
176  case VK_AVR_GS:
177  Kind = AVR::fixup_16_pm;
178  break;
179  case VK_AVR_LO8_GS:
180  Kind = AVR::fixup_lo8_ldi_gs;
181  break;
182  case VK_AVR_HI8_GS:
183  Kind = AVR::fixup_hi8_ldi_gs;
184  break;
185 
186  case VK_AVR_None:
187  llvm_unreachable("Uninitialized expression");
188  }
189 
190  return Kind;
191 }
192 
193 void AVRMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
194  Streamer.visitUsedExpr(*getSubExpr());
195 }
196 
197 const char *AVRMCExpr::getName() const {
198  const auto &Modifier =
199  llvm::find_if(ModifierNames, [this](ModifierEntry const &Mod) {
200  return Mod.VariantKind == Kind;
201  });
202 
203  if (Modifier != std::end(ModifierNames)) {
204  return Modifier->Spelling;
205  }
206  return nullptr;
207 }
208 
210  const auto &Modifier =
211  llvm::find_if(ModifierNames, [&Name](ModifierEntry const &Mod) {
212  return Mod.Spelling == Name;
213  });
214 
215  if (Modifier != std::end(ModifierNames)) {
216  return Modifier->VariantKind;
217  }
218  return VK_AVR_None;
219 }
220 
221 } // end of namespace llvm
llvm::MCSymbolRefExpr::getKind
VariantKind getKind() const
Definition: MCExpr.h:400
llvm::AVRMCExpr::VK_AVR_PM_HI8
@ VK_AVR_PM_HI8
Corresponds to pm_hi8().
Definition: AVRMCExpr.h:32
llvm::MCStreamer::visitUsedExpr
void visitUsedExpr(const MCExpr &Expr)
Definition: MCStreamer.cpp:1061
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::AVRMCExpr::getKindByName
static VariantKind getKindByName(StringRef Name)
Definition: AVRMCExpr.cpp:209
llvm::AVRMCExpr::VK_AVR_HH8
@ VK_AVR_HH8
Corresponds to hlo8() and hh8().
Definition: AVRMCExpr.h:27
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:72
llvm::AVRMCExpr::isNegated
bool isNegated() const
Definition: AVRMCExpr.h:55
llvm::MCAsmInfo
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
MCAssembler.h
llvm::AVRMCExpr::VK_AVR_LO8
@ VK_AVR_LO8
Corresponds to lo8().
Definition: AVRMCExpr.h:26
llvm::AVR::fixup_hh8_ldi_neg
@ fixup_hh8_ldi_neg
Replaces the immediate operand of a 16-bit Rd, K instruction with the upper 8 bits of a negated negat...
Definition: AVRFixupKinds.h:72
llvm::sys::path::end
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:233
llvm::AVR::fixup_lo8_ldi_pm_neg
@ fixup_lo8_ldi_pm_neg
Replaces the immediate operand of a 16-bit Rd, K instruction with the lower 8 bits of a negated 16-bi...
Definition: AVRFixupKinds.h:92
llvm::AVR::fixup_hh8_ldi_pm
@ fixup_hh8_ldi_pm
Replaces the immediate operand of a 16-bit Rd, K instruction with the upper 8 bits of a 24-bit progra...
Definition: AVRFixupKinds.h:87
llvm::AVR::fixup_hh8_ldi
@ fixup_hh8_ldi
Replaces the immediate operand of a 16-bit Rd, K instruction with the upper 8 bits of a 24-bit value ...
Definition: AVRFixupKinds.h:59
AVRMCExpr.h
llvm::MCExpr::evaluateAsRelocatable
bool evaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const
Try to evaluate the expression to a relocatable value, i.e.
Definition: MCExpr.cpp:747
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::AVR::fixup_lo8_ldi
@ fixup_lo8_ldi
Replaces the immediate operand of a 16-bit Rd, K instruction with the lower 8 bits of a 16-bit value ...
Definition: AVRFixupKinds.h:53
llvm::AVR::fixup_hi8_ldi_neg
@ fixup_hi8_ldi_neg
Replaces the immediate operand of a 16-bit Rd, K instruction with the upper 8 bits of a negated 16-bi...
Definition: AVRFixupKinds.h:69
llvm::MCStreamer
Streaming machine code generation interface.
Definition: MCStreamer.h:199
llvm::AVRMCExpr::VK_AVR_None
@ VK_AVR_None
Definition: AVRMCExpr.h:23
llvm::MCAssembler::getContext
MCContext & getContext() const
Definition: MCAssembler.h:292
MCContext.h
llvm::AVRMCExpr::getKind
VariantKind getKind() const
Gets the type of the expression.
Definition: AVRMCExpr.h:46
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::MCSymbolRefExpr::getSymbol
const MCSymbol & getSymbol() const
Definition: MCExpr.h:398
llvm::AVRMCExpr
A expression in AVR machine code.
Definition: AVRMCExpr.h:19
llvm::AVR::fixup_lo8_ldi_pm
@ fixup_lo8_ldi_pm
Replaces the immediate operand of a 16-bit Rd, K instruction with the lower 8 bits of a 16-bit progra...
Definition: AVRFixupKinds.h:79
llvm::AVR::fixup_lo8_ldi_neg
@ fixup_lo8_ldi_neg
Replaces the immediate operand of a 16-bit Rd, K instruction with the lower 8 bits of a negated 16-bi...
Definition: AVRFixupKinds.h:66
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::AVRMCExpr::getSubExpr
const MCExpr * getSubExpr() const
Definition: AVRMCExpr.h:49
llvm::AVR::fixup_hi8_ldi_pm
@ fixup_hi8_ldi_pm
Replaces the immediate operand of a 16-bit Rd, K instruction with the upper 8 bits of a 16-bit progra...
Definition: AVRFixupKinds.h:83
llvm::AVR::fixup_ms8_ldi_neg
@ fixup_ms8_ldi_neg
Replaces the immediate operand of a 16-bit Rd, K instruction with the upper 8 bits of a negated negat...
Definition: AVRFixupKinds.h:75
llvm::AVRMCExpr::VK_AVR_HI8_GS
@ VK_AVR_HI8_GS
Corresponds to hi8(gs()).
Definition: AVRMCExpr.h:36
llvm::AVRMCExpr::create
static const AVRMCExpr * create(VariantKind Kind, const MCExpr *Expr, bool isNegated, MCContext &Ctx)
Creates an AVR machine code expression.
Definition: AVRMCExpr.cpp:38
llvm::MCSymbolRefExpr::VariantKind
VariantKind
Definition: MCExpr.h:194
llvm::AVRMCExpr::VK_AVR_GS
@ VK_AVR_GS
Corresponds to gs().
Definition: AVRMCExpr.h:37
uint64_t
llvm::MCValue::get
static MCValue get(const MCSymbolRefExpr *SymA, const MCSymbolRefExpr *SymB=nullptr, int64_t Val=0, uint32_t RefKind=0)
Definition: MCValue.h:60
llvm::AVRMCExpr::getFixupKind
AVR::Fixups getFixupKind() const
Gets the fixup which corresponds to the expression.
Definition: AVRMCExpr.cpp:149
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::AVRMCExpr::getName
const char * getName() const
Gets the name of the expression.
Definition: AVRMCExpr.cpp:197
llvm::AVRMCExpr::evaluateAsRelocatableImpl
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const override
Definition: AVRMCExpr.cpp:71
llvm::MCSymbolRefExpr
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
llvm::AVR::fixup_hi8_ldi_gs
@ fixup_hi8_ldi_gs
Definition: AVRFixupKinds.h:110
llvm::MCSymbolRefExpr::VK_AVR_PM
@ VK_AVR_PM
Definition: MCExpr.h:245
llvm::AVRMCExpr::VK_AVR_PM
@ VK_AVR_PM
Corresponds to pm(), reference to program memory.
Definition: AVRMCExpr.h:30
Fixup
PowerPC TLS Dynamic Call Fixup
Definition: PPCTLSDynamicCall.cpp:235
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:134
llvm::AVRMCExpr::VK_AVR_LO8_GS
@ VK_AVR_LO8_GS
Corresponds to lo8(gs()).
Definition: AVRMCExpr.h:35
llvm::find_if
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1578
MCAsmLayout.h
llvm::AVRMCExpr::printImpl
void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override
Definition: AVRMCExpr.cpp:43
llvm::AArch64::LastTargetFixupKind
@ LastTargetFixupKind
Definition: AArch64FixupKinds.h:59
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::AVR::Fixups
Fixups
The set of supported fixups.
Definition: AVRFixupKinds.h:26
llvm::MCAsmLayout
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
llvm::AVRMCExpr::VariantKind
VariantKind
Specifies the type of an expression.
Definition: AVRMCExpr.h:22
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::MCSymbolRefExpr::create
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:385
MCValue.h
llvm::AVRMCExpr::visitUsedExpr
void visitUsedExpr(MCStreamer &streamer) const override
Definition: AVRMCExpr.cpp:193
llvm::AVR::fixup_ms8_ldi
@ fixup_ms8_ldi
Replaces the immediate operand of a 16-bit Rd, K instruction with the upper 8 bits of a 32-bit value ...
Definition: AVRFixupKinds.h:62
llvm::MCExpr::print
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:42
MCStreamer.h
llvm::AVRMCExpr::VK_AVR_PM_HH8
@ VK_AVR_PM_HH8
Corresponds to pm_hh8().
Definition: AVRMCExpr.h:33
llvm::AVR::fixup_hi8_ldi
@ fixup_hi8_ldi
Replaces the immediate operand of a 16-bit Rd, K instruction with the upper 8 bits of a 16-bit value ...
Definition: AVRFixupKinds.h:56
llvm::AVRMCExpr::evaluateAsConstant
bool evaluateAsConstant(int64_t &Result) const
Evaluates the fixup as a constant value.
Definition: AVRMCExpr.cpp:54
llvm::AVR::fixup_hh8_ldi_pm_neg
@ fixup_hh8_ldi_pm_neg
Replaces the immediate operand of a 16-bit Rd, K instruction with the upper 8 bits of a negated 24-bi...
Definition: AVRFixupKinds.h:100
llvm::MCAsmLayout::getAssembler
MCAssembler & getAssembler() const
Get the assembler object this is a layout for.
Definition: MCAsmLayout.h:50
llvm::MCValue
This represents an "assembler immediate".
Definition: MCValue.h:37
llvm::MCSymbolRefExpr::VK_None
@ VK_None
Definition: MCExpr.h:195
Mod
Module * Mod
Definition: PassBuilderBindings.cpp:54
llvm::AVRMCExpr::VK_AVR_HI8
@ VK_AVR_HI8
Corresponds to hi8().
Definition: AVRMCExpr.h:25
llvm::AVR::fixup_16_pm
@ fixup_16_pm
A 16-bit program memory address.
Definition: AVRFixupKinds.h:46
llvm::AVR::fixup_lo8_ldi_gs
@ fixup_lo8_ldi_gs
Definition: AVRFixupKinds.h:109
llvm::MCFixup
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
llvm::AVRMCExpr::VK_AVR_HHI8
@ VK_AVR_HHI8
Corresponds to hhi8().
Definition: AVRMCExpr.h:28
llvm::AVRMCExpr::VK_AVR_PM_LO8
@ VK_AVR_PM_LO8
Corresponds to pm_lo8().
Definition: AVRMCExpr.h:31
llvm::AVR::fixup_hi8_ldi_pm_neg
@ fixup_hi8_ldi_pm_neg
Replaces the immediate operand of a 16-bit Rd, K instruction with the upper 8 bits of a negated 16-bi...
Definition: AVRFixupKinds.h:96