LLVM  6.0.0svn
AVRMCExpr.cpp
Go to the documentation of this file.
1 //===-- AVRMCExpr.cpp - AVR specific MC expression classes ----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "AVRMCExpr.h"
11 
12 #include "llvm/MC/MCAsmLayout.h"
13 #include "llvm/MC/MCAssembler.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCStreamer.h"
16 #include "llvm/MC/MCValue.h"
17 
18 namespace llvm {
19 
20 namespace {
21 
22 const struct ModifierEntry {
23  const char * const Spelling;
24  AVRMCExpr::VariantKind VariantKind;
25 } ModifierNames[] = {
27  {"hh8", AVRMCExpr::VK_AVR_HH8}, // synonym with hlo8
28  {"hlo8", AVRMCExpr::VK_AVR_HH8}, {"hhi8", AVRMCExpr::VK_AVR_HHI8},
29 
30  {"pm_lo8", AVRMCExpr::VK_AVR_PM_LO8}, {"pm_hi8", AVRMCExpr::VK_AVR_PM_HI8},
31  {"pm_hh8", AVRMCExpr::VK_AVR_PM_HH8},
32 };
33 
34 } // end of anonymous namespace
35 
37  bool Negated, MCContext &Ctx) {
38  return new (Ctx) AVRMCExpr(Kind, Expr, Negated);
39 }
40 
41 void AVRMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
42  assert(Kind != VK_AVR_None);
43 
44  if (isNegated())
45  OS << '-';
46 
47  OS << getName() << '(';
48  getSubExpr()->print(OS, MAI);
49  OS << ')';
50 }
51 
52 bool AVRMCExpr::evaluateAsConstant(int64_t &Result) const {
53  MCValue Value;
54 
55  bool isRelocatable =
56  getSubExpr()->evaluateAsRelocatable(Value, nullptr, nullptr);
57 
58  if (!isRelocatable)
59  return false;
60 
61  if (Value.isAbsolute()) {
62  Result = evaluateAsInt64(Value.getConstant());
63  return true;
64  }
65 
66  return false;
67 }
68 
70  const MCAsmLayout *Layout,
71  const MCFixup *Fixup) const {
72  MCValue Value;
73  bool isRelocatable = SubExpr->evaluateAsRelocatable(Value, Layout, Fixup);
74 
75  if (!isRelocatable)
76  return false;
77 
78  if (Value.isAbsolute()) {
79  Result = MCValue::get(evaluateAsInt64(Value.getConstant()));
80  } else {
81  if (!Layout) return false;
82 
84  const MCSymbolRefExpr *Sym = Value.getSymA();
85  MCSymbolRefExpr::VariantKind Modifier = Sym->getKind();
86  if (Modifier != MCSymbolRefExpr::VK_None)
87  return false;
88 
89  Sym = MCSymbolRefExpr::create(&Sym->getSymbol(), Modifier, Context);
90  Result = MCValue::get(Sym, Value.getSymB(), Value.getConstant());
91  }
92 
93  return true;
94 }
95 
96 int64_t AVRMCExpr::evaluateAsInt64(int64_t Value) const {
97  if (Negated)
98  Value *= -1;
99 
100  switch (Kind) {
102  break;
104  Value >>= 8;
105  break;
107  Value >>= 16;
108  break;
110  Value >>= 24;
111  break;
113  Value >>= 1;
114  break;
116  Value >>= 9;
117  break;
119  Value >>= 17;
120  break;
121 
123  llvm_unreachable("Uninitialized expression.");
124  }
125  return static_cast<uint64_t>(Value) & 0xff;
126 }
127 
130 
131  switch (getKind()) {
132  case VK_AVR_LO8:
133  Kind = isNegated() ? AVR::fixup_lo8_ldi_neg : AVR::fixup_lo8_ldi;
134  break;
135  case VK_AVR_HI8:
136  Kind = isNegated() ? AVR::fixup_hi8_ldi_neg : AVR::fixup_hi8_ldi;
137  break;
138  case VK_AVR_HH8:
139  Kind = isNegated() ? AVR::fixup_hh8_ldi_neg : AVR::fixup_hh8_ldi;
140  break;
141  case VK_AVR_HHI8:
142  Kind = isNegated() ? AVR::fixup_ms8_ldi_neg : AVR::fixup_ms8_ldi;
143  break;
144 
145  case VK_AVR_PM_LO8:
146  Kind = isNegated() ? AVR::fixup_lo8_ldi_pm_neg : AVR::fixup_lo8_ldi_pm;
147  break;
148  case VK_AVR_PM_HI8:
149  Kind = isNegated() ? AVR::fixup_hi8_ldi_pm_neg : AVR::fixup_hi8_ldi_pm;
150  break;
151  case VK_AVR_PM_HH8:
152  Kind = isNegated() ? AVR::fixup_hh8_ldi_pm_neg : AVR::fixup_hh8_ldi_pm;
153  break;
154 
155  case VK_AVR_None:
156  llvm_unreachable("Uninitialized expression");
157  }
158 
159  return Kind;
160 }
161 
162 void AVRMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
163  Streamer.visitUsedExpr(*getSubExpr());
164 }
165 
166 const char *AVRMCExpr::getName() const {
167  const auto &Modifier = std::find_if(
168  std::begin(ModifierNames), std::end(ModifierNames),
169  [this](ModifierEntry const &Mod) { return Mod.VariantKind == Kind; });
170 
171  if (Modifier != std::end(ModifierNames)) {
172  return Modifier->Spelling;
173  }
174  return nullptr;
175 }
176 
178  const auto &Modifier = std::find_if(
179  std::begin(ModifierNames), std::end(ModifierNames),
180  [&Name](ModifierEntry const &Mod) { return Mod.Spelling == Name; });
181 
182  if (Modifier != std::end(ModifierNames)) {
183  return Modifier->VariantKind;
184  }
185  return VK_AVR_None;
186 }
187 
188 } // end of namespace llvm
189 
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:244
LLVMContext & Context
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:235
Replaces the immediate operand of a 16-bit Rd, K instruction with the upper 8 bits of a negated 16-bi...
Definition: AVRFixupKinds.h:70
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:305
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
This represents an "assembler immediate".
Definition: MCValue.h:40
VariantKind getKind() const
Definition: MCExpr.h:320
Replaces the immediate operand of a 16-bit Rd, K instruction with the lower 8 bits of a 16-bit progra...
Definition: AVRFixupKinds.h:80
Replaces the immediate operand of a 16-bit Rd, K instruction with the upper 8 bits of a negated 16-bi...
Definition: AVRFixupKinds.h:97
bool isAbsolute() const
Is this an absolute (as opposed to relocatable) value.
Definition: MCValue.h:53
Replaces the immediate operand of a 16-bit Rd, K instruction with the lower 8 bits of a negated 16-bi...
Definition: AVRFixupKinds.h:67
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:66
void visitUsedExpr(MCStreamer &streamer) const override
Definition: AVRMCExpr.cpp:162
MCContext & getContext() const
Definition: MCAssembler.h:259
int64_t getConstant() const
Definition: MCValue.h:47
const MCSymbolRefExpr * getSymB() const
Definition: MCValue.h:49
VariantKind
Specifies the type of an expression.
Definition: AVRMCExpr.h:23
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:29
A expression in AVR machine code.
Definition: AVRMCExpr.h:20
Replaces the immediate operand of a 16-bit Rd, K instruction with the upper 8 bits of a negated negat...
Definition: AVRFixupKinds.h:76
Replaces the immediate operand of a 16-bit Rd, K instruction with the lower 8 bits of a 16-bit value ...
Definition: AVRFixupKinds.h:54
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
static StringRef getName(Value *V)
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:165
Context object for machine code objects.
Definition: MCContext.h:59
void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override
Definition: AVRMCExpr.cpp:41
Replaces the immediate operand of a 16-bit Rd, K instruction with the upper 8 bits of a negated negat...
Definition: AVRFixupKinds.h:73
MCAssembler & getAssembler() const
Get the assembler object this is a layout for.
Definition: MCAsmLayout.h:51
AVR::Fixups getFixupKind() const
Gets the fixup which corresponds to the expression.
Definition: AVRMCExpr.cpp:128
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:169
Corresponds to hlo8() and hh8().
Definition: AVRMCExpr.h:28
Corresponds to pm_lo8().
Definition: AVRMCExpr.h:31
bool evaluateAsConstant(int64_t &Result) const
Evaluates the fixup as a constant value.
Definition: AVRMCExpr.cpp:52
const char * getName() const
Gets the name of the expression.
Definition: AVRMCExpr.cpp:166
Corresponds to hi8().
Definition: AVRMCExpr.h:26
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:48
Replaces the immediate operand of a 16-bit Rd, K instruction with the upper 8 bits of a 16-bit progra...
Definition: AVRFixupKinds.h:84
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:841
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Corresponds to pm_hh8().
Definition: AVRMCExpr.h:33
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:318
Replaces the immediate operand of a 16-bit Rd, K instruction with the upper 8 bits of a 16-bit value ...
Definition: AVRFixupKinds.h:57
Fixups
The set of supported fixups.
Definition: AVRFixupKinds.h:27
void visitUsedExpr(const MCExpr &Expr)
Definition: MCStreamer.cpp:837
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const override
Definition: AVRMCExpr.cpp:69
Corresponds to pm_hi8().
Definition: AVRMCExpr.h:32
Replaces the immediate operand of a 16-bit Rd, K instruction with the lower 8 bits of a negated 16-bi...
Definition: AVRFixupKinds.h:93
Replaces the immediate operand of a 16-bit Rd, K instruction with the upper 8 bits of a 32-bit value ...
Definition: AVRFixupKinds.h:63
Corresponds to lo8().
Definition: AVRMCExpr.h:27
static MCValue get(const MCSymbolRefExpr *SymA, const MCSymbolRefExpr *SymB=nullptr, int64_t Val=0, uint32_t RefKind=0)
Definition: MCValue.h:63
static const AVRMCExpr * create(VariantKind Kind, const MCExpr *Expr, bool isNegated, MCContext &Ctx)
Creates an AVR machine code expression.
Definition: AVRMCExpr.cpp:36
static VariantKind getKindByName(StringRef Name)
Definition: AVRMCExpr.cpp:177
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
Replaces the immediate operand of a 16-bit Rd, K instruction with the upper 8 bits of a 24-bit progra...
Definition: AVRFixupKinds.h:88
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Corresponds to hhi8().
Definition: AVRMCExpr.h:29
Replaces the immediate operand of a 16-bit Rd, K instruction with the upper 8 bits of a 24-bit value ...
Definition: AVRFixupKinds.h:60