LLVM 20.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/MCAssembler.h"
12#include "llvm/MC/MCContext.h"
13#include "llvm/MC/MCStreamer.h"
14#include "llvm/MC/MCValue.h"
15
16namespace llvm {
17
18namespace {
19
20const struct ModifierEntry {
21 const char *const Spelling;
22 AVRMCExpr::VariantKind VariantKind;
23} ModifierNames[] = {
25 {"hh8", AVRMCExpr::VK_AVR_HH8}, // synonym with hlo8
27
30
33};
34
35} // end of anonymous namespace
36
38 bool Negated, MCContext &Ctx) {
39 return new (Ctx) AVRMCExpr(Kind, Expr, Negated);
40}
41
42void AVRMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
43 assert(Kind != VK_AVR_None);
44 OS << getName() << '(';
45 if (isNegated())
46 OS << '-' << '(';
47 getSubExpr()->print(OS, MAI);
48 if (isNegated())
49 OS << ')';
50 OS << ')';
51}
52
53bool AVRMCExpr::evaluateAsConstant(int64_t &Result) const {
55
56 bool isRelocatable =
57 getSubExpr()->evaluateAsRelocatable(Value, nullptr, nullptr);
58
59 if (!isRelocatable)
60 return false;
61
62 if (Value.isAbsolute()) {
63 Result = evaluateAsInt64(Value.getConstant());
64 return true;
65 }
66
67 return false;
68}
69
71 const MCAssembler *Asm,
72 const MCFixup *Fixup) const {
74 bool isRelocatable = SubExpr->evaluateAsRelocatable(Value, Asm, Fixup);
75
76 if (!isRelocatable)
77 return false;
78
79 if (Value.isAbsolute()) {
80 Result = MCValue::get(evaluateAsInt64(Value.getConstant()));
81 } else {
82 if (!Asm || !Asm->hasLayout())
83 return false;
84
85 MCContext &Context = Asm->getContext();
86 const MCSymbolRefExpr *Sym = Value.getSymA();
87 MCSymbolRefExpr::VariantKind Modifier = Sym->getKind();
88 if (Modifier != MCSymbolRefExpr::VK_None)
89 return false;
90 if (Kind == VK_AVR_PM) {
92 }
93
94 Sym = MCSymbolRefExpr::create(&Sym->getSymbol(), Modifier, Context);
95 Result = MCValue::get(Sym, Value.getSymB(), Value.getConstant());
96 }
97
98 return true;
99}
100
101int64_t AVRMCExpr::evaluateAsInt64(int64_t Value) const {
102 if (Negated)
103 Value *= -1;
104
105 switch (Kind) {
107 Value &= 0xff;
108 break;
110 Value &= 0xff00;
111 Value >>= 8;
112 break;
114 Value &= 0xff0000;
115 Value >>= 16;
116 break;
118 Value &= 0xff000000;
119 Value >>= 24;
120 break;
123 Value >>= 1; // Program memory addresses must always be shifted by one.
124 Value &= 0xff;
125 break;
128 Value >>= 1; // Program memory addresses must always be shifted by one.
129 Value &= 0xff00;
130 Value >>= 8;
131 break;
133 Value >>= 1; // Program memory addresses must always be shifted by one.
134 Value &= 0xff0000;
135 Value >>= 16;
136 break;
139 Value >>= 1; // Program memory addresses must always be shifted by one.
140 break;
141
143 llvm_unreachable("Uninitialized expression.");
144 }
145 return static_cast<uint64_t>(Value) & 0xff;
146}
147
150
151 switch (getKind()) {
152 case VK_AVR_LO8:
154 break;
155 case VK_AVR_HI8:
157 break;
158 case VK_AVR_HH8:
160 break;
161 case VK_AVR_HHI8:
163 break;
164
165 case VK_AVR_PM_LO8:
167 break;
168 case VK_AVR_PM_HI8:
170 break;
171 case VK_AVR_PM_HH8:
173 break;
174 case VK_AVR_PM:
175 case VK_AVR_GS:
176 Kind = AVR::fixup_16_pm;
177 break;
178 case VK_AVR_LO8_GS:
180 break;
181 case VK_AVR_HI8_GS:
183 break;
184
185 case VK_AVR_None:
186 llvm_unreachable("Uninitialized expression");
187 }
188
189 return Kind;
190}
191
193 Streamer.visitUsedExpr(*getSubExpr());
194}
195
196const char *AVRMCExpr::getName() const {
197 const auto &Modifier =
198 llvm::find_if(ModifierNames, [this](ModifierEntry const &Mod) {
199 return Mod.VariantKind == Kind;
200 });
201
202 if (Modifier != std::end(ModifierNames)) {
203 return Modifier->Spelling;
204 }
205 return nullptr;
206}
207
209 const auto &Modifier =
210 llvm::find_if(ModifierNames, [&Name](ModifierEntry const &Mod) {
211 return Mod.Spelling == Name;
212 });
213
214 if (Modifier != std::end(ModifierNames)) {
215 return Modifier->VariantKind;
216 }
217 return VK_AVR_None;
218}
219
220} // end of namespace llvm
std::string Name
Symbol * Sym
Definition: ELF_riscv.cpp:479
PowerPC TLS Dynamic Call Fixup
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
A expression in AVR machine code.
Definition: AVRMCExpr.h:19
static const AVRMCExpr * create(VariantKind Kind, const MCExpr *Expr, bool isNegated, MCContext &Ctx)
Creates an AVR machine code expression.
Definition: AVRMCExpr.cpp:37
void visitUsedExpr(MCStreamer &streamer) const override
Definition: AVRMCExpr.cpp:192
static VariantKind getKindByName(StringRef Name)
Definition: AVRMCExpr.cpp:208
AVR::Fixups getFixupKind() const
Gets the fixup which corresponds to the expression.
Definition: AVRMCExpr.cpp:148
bool evaluateAsConstant(int64_t &Result) const
Evaluates the fixup as a constant value.
Definition: AVRMCExpr.cpp:53
void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override
Definition: AVRMCExpr.cpp:42
VariantKind getKind() const
Gets the type of the expression.
Definition: AVRMCExpr.h:46
const MCExpr * getSubExpr() const
Definition: AVRMCExpr.h:49
VariantKind
Specifies the type of an expression.
Definition: AVRMCExpr.h:22
@ VK_AVR_PM_LO8
Corresponds to pm_lo8().
Definition: AVRMCExpr.h:31
@ VK_AVR_LO8
Corresponds to lo8().
Definition: AVRMCExpr.h:26
@ VK_AVR_PM_HI8
Corresponds to pm_hi8().
Definition: AVRMCExpr.h:32
@ VK_AVR_PM
Corresponds to pm(), reference to program memory.
Definition: AVRMCExpr.h:30
@ VK_AVR_HHI8
Corresponds to hhi8().
Definition: AVRMCExpr.h:28
@ VK_AVR_HI8_GS
Corresponds to hi8(gs()).
Definition: AVRMCExpr.h:36
@ VK_AVR_HH8
Corresponds to hlo8() and hh8().
Definition: AVRMCExpr.h:27
@ VK_AVR_LO8_GS
Corresponds to lo8(gs()).
Definition: AVRMCExpr.h:35
@ VK_AVR_PM_HH8
Corresponds to pm_hh8().
Definition: AVRMCExpr.h:33
@ VK_AVR_GS
Corresponds to gs().
Definition: AVRMCExpr.h:37
@ VK_AVR_HI8
Corresponds to hi8().
Definition: AVRMCExpr.h:25
bool isNegated() const
Definition: AVRMCExpr.h:55
const char * getName() const
Gets the name of the expression.
Definition: AVRMCExpr.cpp:196
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm, const MCFixup *Fixup) const override
Definition: AVRMCExpr.cpp:70
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
Context object for machine code objects.
Definition: MCContext.h:83
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
bool evaluateAsRelocatable(MCValue &Res, const MCAssembler *Asm, const MCFixup *Fixup) const
Try to evaluate the expression to a relocatable value, i.e.
Definition: MCExpr.cpp:788
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:40
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
Streaming machine code generation interface.
Definition: MCStreamer.h:213
void visitUsedExpr(const MCExpr &Expr)
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:188
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:393
This represents an "assembler immediate".
Definition: MCValue.h:36
static MCValue get(const MCSymbolRefExpr *SymA, const MCSymbolRefExpr *SymB=nullptr, int64_t Val=0, uint32_t RefKind=0)
Definition: MCValue.h:59
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
LLVM Value Representation.
Definition: Value.h:74
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Fixups
The set of supported fixups.
Definition: AVRFixupKinds.h:26
@ fixup_16_pm
A 16-bit program memory address.
Definition: AVRFixupKinds.h:46
@ 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
@ LastTargetFixupKind
@ fixup_ms8_ldi_neg
Replaces the immediate operand of a 16-bit Rd, K instruction with the upper 8 bits of a negated 32-bi...
Definition: AVRFixupKinds.h:75
@ 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
@ 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
@ 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
@ 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
@ 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
@ 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
@ fixup_hh8_ldi_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:72
@ 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
@ 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
@ 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
@ 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...
@ 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
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Mod
The access may modify the value stored in memory.
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:1749