LLVM  10.0.0svn
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 
29  {"pm_lo8", AVRMCExpr::VK_AVR_PM_LO8}, {"pm_hi8", AVRMCExpr::VK_AVR_PM_HI8},
30  {"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) return false;
84 
86  const MCSymbolRefExpr *Sym = Value.getSymA();
87  MCSymbolRefExpr::VariantKind Modifier = Sym->getKind();
88  if (Modifier != MCSymbolRefExpr::VK_None)
89  return false;
90 
91  Sym = MCSymbolRefExpr::create(&Sym->getSymbol(), Modifier, Context);
92  Result = MCValue::get(Sym, Value.getSymB(), Value.getConstant());
93  }
94 
95  return true;
96 }
97 
98 int64_t AVRMCExpr::evaluateAsInt64(int64_t Value) const {
99  if (Negated)
100  Value *= -1;
101 
102  switch (Kind) {
104  Value &= 0xff;
105  break;
107  Value &= 0xff00;
108  Value >>= 8;
109  break;
111  Value &= 0xff0000;
112  Value >>= 16;
113  break;
115  Value &= 0xff000000;
116  Value >>= 24;
117  break;
120  Value >>= 1; // Program memory addresses must always be shifted by one.
121  Value &= 0xff;
122  break;
125  Value >>= 1; // Program memory addresses must always be shifted by one.
126  Value &= 0xff00;
127  Value >>= 8;
128  break;
130  Value >>= 1; // Program memory addresses must always be shifted by one.
131  Value &= 0xff0000;
132  Value >>= 16;
133  break;
135  Value >>= 1; // Program memory addresses must always be shifted by one.
136  break;
137 
139  llvm_unreachable("Uninitialized expression.");
140  }
141  return static_cast<uint64_t>(Value) & 0xff;
142 }
143 
146 
147  switch (getKind()) {
148  case VK_AVR_LO8:
149  Kind = isNegated() ? AVR::fixup_lo8_ldi_neg : AVR::fixup_lo8_ldi;
150  break;
151  case VK_AVR_HI8:
152  Kind = isNegated() ? AVR::fixup_hi8_ldi_neg : AVR::fixup_hi8_ldi;
153  break;
154  case VK_AVR_HH8:
155  Kind = isNegated() ? AVR::fixup_hh8_ldi_neg : AVR::fixup_hh8_ldi;
156  break;
157  case VK_AVR_HHI8:
158  Kind = isNegated() ? AVR::fixup_ms8_ldi_neg : AVR::fixup_ms8_ldi;
159  break;
160 
161  case VK_AVR_PM_LO8:
162  Kind = isNegated() ? AVR::fixup_lo8_ldi_pm_neg : AVR::fixup_lo8_ldi_pm;
163  break;
164  case VK_AVR_PM_HI8:
165  Kind = isNegated() ? AVR::fixup_hi8_ldi_pm_neg : AVR::fixup_hi8_ldi_pm;
166  break;
167  case VK_AVR_PM_HH8:
168  Kind = isNegated() ? AVR::fixup_hh8_ldi_pm_neg : AVR::fixup_hh8_ldi_pm;
169  break;
170  case VK_AVR_GS:
171  Kind = AVR::fixup_16_pm;
172  break;
173  case VK_AVR_LO8_GS:
174  Kind = AVR::fixup_lo8_ldi_gs;
175  break;
176  case VK_AVR_HI8_GS:
177  Kind = AVR::fixup_hi8_ldi_gs;
178  break;
179 
180  case VK_AVR_None:
181  llvm_unreachable("Uninitialized expression");
182  }
183 
184  return Kind;
185 }
186 
187 void AVRMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
188  Streamer.visitUsedExpr(*getSubExpr());
189 }
190 
191 const char *AVRMCExpr::getName() const {
192  const auto &Modifier = std::find_if(
193  std::begin(ModifierNames), std::end(ModifierNames),
194  [this](ModifierEntry const &Mod) { return Mod.VariantKind == Kind; });
195 
196  if (Modifier != std::end(ModifierNames)) {
197  return Modifier->Spelling;
198  }
199  return nullptr;
200 }
201 
203  const auto &Modifier = std::find_if(
204  std::begin(ModifierNames), std::end(ModifierNames),
205  [&Name](ModifierEntry const &Mod) { return Mod.Spelling == Name; });
206 
207  if (Modifier != std::end(ModifierNames)) {
208  return Modifier->VariantKind;
209  }
210  return VK_AVR_None;
211 }
212 
213 } // end of namespace llvm
214 
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:233
LLVMContext & Context
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:224
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
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:329
This class represents lattice values for constants.
Definition: AllocatorList.h:23
This represents an "assembler immediate".
Definition: MCValue.h:39
VariantKind getKind() const
Definition: MCExpr.h:344
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
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
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
bool isAbsolute() const
Is this an absolute (as opposed to relocatable) value.
Definition: MCValue.h:52
Corresponds to gs().
Definition: AVRMCExpr.h:36
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
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:77
void visitUsedExpr(MCStreamer &streamer) const override
Definition: AVRMCExpr.cpp:187
MCContext & getContext() const
Definition: MCAssembler.h:284
int64_t getConstant() const
Definition: MCValue.h:46
const MCSymbolRefExpr * getSymB() const
Definition: MCValue.h:48
VariantKind
Specifies the type of an expression.
Definition: AVRMCExpr.h:22
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
A expression in AVR machine code.
Definition: AVRMCExpr.h:19
Replaces the immediate operand of a 16-bit Rd, K instruction with the upper 8 bits of a negated negat...
Definition: AVRFixupKinds.h:75
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
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
static StringRef getName(Value *V)
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:169
Context object for machine code objects.
Definition: MCContext.h:65
void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override
Definition: AVRMCExpr.cpp:43
Replaces the immediate operand of a 16-bit Rd, K instruction with the upper 8 bits of a negated negat...
Definition: AVRFixupKinds.h:72
MCAssembler & getAssembler() const
Get the assembler object this is a layout for.
Definition: MCAsmLayout.h:50
AVR::Fixups getFixupKind() const
Gets the fixup which corresponds to the expression.
Definition: AVRMCExpr.cpp:144
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
Streaming machine code generation interface.
Definition: MCStreamer.h:196
Corresponds to hlo8() and hh8().
Definition: AVRMCExpr.h:27
Corresponds to pm_lo8().
Definition: AVRMCExpr.h:30
bool evaluateAsConstant(int64_t &Result) const
Evaluates the fixup as a constant value.
Definition: AVRMCExpr.cpp:54
const char * getName() const
Gets the name of the expression.
Definition: AVRMCExpr.cpp:191
Corresponds to lo8(gs()).
Definition: AVRMCExpr.h:34
Corresponds to hi8().
Definition: AVRMCExpr.h:25
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:47
A 16-bit program memory address.
Definition: AVRFixupKinds.h:46
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
auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1193
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Corresponds to pm_hh8().
Definition: AVRMCExpr.h:32
Corresponds to hi8(gs()).
Definition: AVRMCExpr.h:35
Replaces the immediate operand of a 16-bit Rd, K instruction with the upper 8 bits of a negated 24-bi...
const MCSymbol & getSymbol() const
Definition: MCExpr.h:342
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
Fixups
The set of supported fixups.
Definition: AVRFixupKinds.h:26
The access may modify the value stored in memory.
void visitUsedExpr(const MCExpr &Expr)
Definition: MCStreamer.cpp:988
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const override
Definition: AVRMCExpr.cpp:71
Corresponds to pm_hi8().
Definition: AVRMCExpr.h:31
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
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
Corresponds to lo8().
Definition: AVRMCExpr.h:26
static MCValue get(const MCSymbolRefExpr *SymA, const MCSymbolRefExpr *SymB=nullptr, int64_t Val=0, uint32_t RefKind=0)
Definition: MCValue.h:62
static const AVRMCExpr * create(VariantKind Kind, const MCExpr *Expr, bool isNegated, MCContext &Ctx)
Creates an AVR machine code expression.
Definition: AVRMCExpr.cpp:38
static VariantKind getKindByName(StringRef Name)
Definition: AVRMCExpr.cpp:202
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:74
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
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Corresponds to hhi8().
Definition: AVRMCExpr.h:28
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