LLVM 24.0.0git
AVRMCAsmInfo.cpp
Go to the documentation of this file.
1//===-- AVRMCAsmInfo.cpp - AVR asm properties -----------------------------===//
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// This file contains the declarations of the AVRMCAsmInfo properties.
10//
11//===----------------------------------------------------------------------===//
12
13#include "AVRMCAsmInfo.h"
14#include "llvm/MC/MCAssembler.h"
15#include "llvm/MC/MCContext.h"
16#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCValue.h"
19
20using namespace llvm;
21
23 : MCAsmInfoELF(TT, Options) {
26 CommentString = ";";
27 SeparatorString = "$";
29
30 SpecifierExprPrintFn = [](const MCAsmInfo &MAI, raw_ostream &OS,
31 const MCSpecifierExpr &Expr) {
32 auto &E = static_cast<const AVRMCExpr &>(Expr);
33 assert(E.getSpecifier() != AVR::S_AVR_NONE);
34 OS << E.getName() << '(';
35 if (E.isNegated())
36 OS << '-' << '(';
37 MAI.printExpr(OS, *E.getSubExpr());
38 if (E.isNegated())
39 OS << ')';
40 OS << ')';
41 };
42 SpecifierExprEvaluateFn = [](const MCSpecifierExpr &Expr, MCValue &Result,
43 const MCAssembler *Asm) {
44 auto &E = static_cast<const AVRMCExpr &>(Expr);
46 bool isRelocatable = E.getSubExpr()->evaluateAsRelocatable(Value, Asm);
47 if (!isRelocatable)
48 return false;
49
50 if (Value.isAbsolute()) {
51 Result = MCValue::get(E.evaluateAsInt64(Value.getConstant()));
52 } else {
53 if (!Asm || !Asm->hasLayout())
54 return false;
55
56 auto Spec = AVR::S_None;
57 if (Value.getSpecifier())
58 return false;
59 assert(!Value.getSubSym());
60 if (E.getSpecifier() == AVR::S_PM)
62
63 // TODO: Don't attach specifier to MCSymbolRefExpr.
64 Result =
65 MCValue::get(Value.getAddSym(), nullptr, Value.getConstant(), Spec);
66 }
67
68 return true;
69 };
70}
71
72namespace {
73const struct ModifierEntry {
74 const char *const Spelling;
75 AVRMCExpr::Specifier specifier;
76} ModifierNames[] = {
77 {"lo8", AVR::S_LO8}, {"hi8", AVR::S_HI8},
78 {"hh8", AVR::S_HH8}, // synonym with hlo8
79 {"hlo8", AVR::S_HH8}, {"hhi8", AVR::S_HHI8},
80
81 {"pm", AVR::S_PM}, {"pm_lo8", AVR::S_PM_LO8},
82 {"pm_hi8", AVR::S_PM_HI8}, {"pm_hh8", AVR::S_PM_HH8},
83
84 {"lo8_gs", AVR::S_LO8_GS}, {"hi8_gs", AVR::S_HI8_GS},
85 {"gs", AVR::S_GS},
86};
87
88} // end of anonymous namespace
89
91 const auto &Modifier =
92 llvm::find_if(ModifierNames, [&Name](ModifierEntry const &Mod) {
93 return Mod.Spelling == Name;
94 });
95
96 if (Modifier != std::end(ModifierNames)) {
97 return Modifier->specifier;
98 }
99 return AVR::S_AVR_NONE;
100}
101
102const char *AVRMCExpr::getName() const {
103 const auto &Modifier =
104 llvm::find_if(ModifierNames, [this](ModifierEntry const &Mod) {
105 return Mod.specifier == getSpecifier();
106 });
107
108 if (Modifier != std::end(ModifierNames)) {
109 return Modifier->Spelling;
110 }
111 return nullptr;
112}
113
116
117 switch (getSpecifier()) {
118 case AVR::S_LO8:
120 break;
121 case AVR::S_HI8:
123 break;
124 case AVR::S_HH8:
126 break;
127 case AVR::S_HHI8:
129 break;
130
131 case AVR::S_PM_LO8:
133 break;
134 case AVR::S_PM_HI8:
136 break;
137 case AVR::S_PM_HH8:
139 break;
140 case AVR::S_PM:
141 case AVR::S_GS:
142 Kind = AVR::fixup_16_pm;
143 break;
144 case AVR::S_LO8_GS:
146 break;
147 case AVR::S_HI8_GS:
149 break;
150
151 default:
152 llvm_unreachable("Uninitialized expression");
153 }
154
155 return Kind;
156}
157
158int64_t AVRMCExpr::evaluateAsInt64(int64_t Value) const {
159 if (Negated)
160 Value *= -1;
161
162 switch (getSpecifier()) {
163 case AVR::S_LO8:
164 Value &= 0xff;
165 break;
166 case AVR::S_HI8:
167 Value &= 0xff00;
168 Value >>= 8;
169 break;
170 case AVR::S_HH8:
171 Value &= 0xff0000;
172 Value >>= 16;
173 break;
174 case AVR::S_HHI8:
175 Value &= 0xff000000;
176 Value >>= 24;
177 break;
178 case AVR::S_PM_LO8:
179 case AVR::S_LO8_GS:
180 Value >>= 1; // Program memory addresses must always be shifted by one.
181 Value &= 0xff;
182 break;
183 case AVR::S_PM_HI8:
184 case AVR::S_HI8_GS:
185 Value >>= 1; // Program memory addresses must always be shifted by one.
186 Value &= 0xff00;
187 Value >>= 8;
188 break;
189 case AVR::S_PM_HH8:
190 Value >>= 1; // Program memory addresses must always be shifted by one.
191 Value &= 0xff0000;
192 Value >>= 16;
193 break;
194 case AVR::S_PM:
195 case AVR::S_GS:
196 Value >>= 1; // Program memory addresses must always be shifted by one.
197 break;
198
199 case AVR::S_AVR_NONE:
200 default:
201 llvm_unreachable("Uninitialized expression.");
202 }
203 return static_cast<uint64_t>(Value) & 0xff;
204}
205
206bool AVRMCExpr::evaluateAsConstant(int64_t &Result) const {
208 bool isRelocatable = getSubExpr()->evaluateAsRelocatable(Value, nullptr);
209 if (!isRelocatable)
210 return false;
211
212 if (Value.isAbsolute()) {
213 Result = evaluateAsInt64(Value.getConstant());
214 return true;
215 }
216
217 return false;
218}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
static LVOptions Options
Definition LVOptions.cpp:25
AVRMCAsmInfo(const Triple &TT, const MCTargetOptions &Options)
A expression in AVR machine code.
Definition AVRMCExpr.h:19
bool evaluateAsConstant(int64_t &Result) const
Evaluates the fixup as a constant value.
bool isNegated() const
Definition AVRMCExpr.h:37
const char * getName() const
Gets the name of the expression.
static Specifier parseSpecifier(StringRef Name)
AVR::Fixups getFixupKind() const
Gets the fixup which corresponds to the expression.
MCAsmInfoELF(const Triple &TT, const MCTargetOptions &Options)
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition MCAsmInfo.h:69
bool(* SpecifierExprEvaluateFn)(const MCSpecifierExpr &E, MCValue &Res, const MCAssembler *Asm)
Definition MCAsmInfo.h:231
void printExpr(raw_ostream &, const MCExpr &) const
bool SupportsDebugInformation
True if target supports emission of debugging information.
Definition MCAsmInfo.h:372
void(* SpecifierExprPrintFn)(const MCAsmInfo &, raw_ostream &, const MCSpecifierExpr &)
Definition MCAsmInfo.h:228
const char * SeparatorString
This string, if specified, is used to separate instructions from each other when on the same line.
Definition MCAsmInfo.h:140
unsigned CodePointerSize
Code pointer size in bytes. Default is 4.
Definition MCAsmInfo.h:88
unsigned CalleeSaveStackSlotSize
Size of the stack slot reserved for callee-saved registers, in bytes.
Definition MCAsmInfo.h:92
StringRef CommentString
This indicates the comment string used by the assembler.
Definition MCAsmInfo.h:144
LLVM_ABI bool evaluateAsRelocatable(MCValue &Res, const MCAssembler *Asm) const
Try to evaluate the expression to a relocatable value, i.e.
Definition MCExpr.cpp:450
Extension point for target-specific MCExpr subclasses with a relocation specifier,...
Definition MCExpr.h:491
const MCExpr * getSubExpr() const
Definition MCExpr.h:505
Spec getSpecifier() const
Definition MCExpr.h:504
static MCValue get(const MCSymbol *SymA, const MCSymbol *SymB=nullptr, int64_t Val=0, uint32_t Specifier=0)
Definition MCValue.h:56
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Fixups
The set of supported fixups.
@ fixup_16_pm
A 16-bit program memory address.
@ fixup_hh8_ldi
Replaces the immediate operand of a 16-bit Rd, K instruction with the upper 8 bits of a 24-bit value ...
@ 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...
@ 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...
@ fixup_ms8_ldi
Replaces the immediate operand of a 16-bit Rd, K instruction with the upper 8 bits of a 32-bit value ...
@ fixup_lo8_ldi
Replaces the immediate operand of a 16-bit Rd, K instruction with the lower 8 bits of a 16-bit value ...
@ 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...
@ fixup_hi8_ldi
Replaces the immediate operand of a 16-bit Rd, K instruction with the upper 8 bits of a 16-bit value ...
@ 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...
@ 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...
@ 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...
@ 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...
@ 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...
@ 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...
@ S_LO8
Corresponds to lo8().
@ S_LO8_GS
Corresponds to lo8(gs()).
@ S_HH8
Corresponds to hlo8() and hh8().
@ S_PM_HH8
Corresponds to pm_hh8().
@ S_HHI8
Corresponds to hhi8().
@ S_PM_LO8
Corresponds to pm_lo8().
@ S_PM
Corresponds to pm(), reference to program memory.
@ S_GS
Corresponds to gs().
@ S_HI8_GS
Corresponds to hi8(gs()).
@ S_PM_HI8
Corresponds to pm_hi8().
@ S_HI8
Corresponds to hi8().
This is an optimization pass for GlobalISel generic memory operations.
@ Mod
The access may modify the value stored in memory.
Definition ModRef.h:34
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:1788